blob: 48afb5d8e963d32aacf9ea601f40ee4b21ed33b7 [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
11// (internal) ConstantFolding.h interface, which is used by the
12// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner5a945e32004-01-12 21:13:12 +000021#include "ConstantFolding.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattnerad70d4a2003-11-25 21:21:46 +000026#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000027#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000028using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000029
Chris Lattner5a945e32004-01-12 21:13:12 +000030namespace {
31 struct ConstRules {
32 ConstRules() {}
33
34 // Binary Operators...
35 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
36 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
37 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
38 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
39 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
40 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
42 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
43 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
45 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0;
46 virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0;
47
48 // Casting operators.
49 virtual Constant *castToBool (const Constant *V) const = 0;
50 virtual Constant *castToSByte (const Constant *V) const = 0;
51 virtual Constant *castToUByte (const Constant *V) const = 0;
52 virtual Constant *castToShort (const Constant *V) const = 0;
53 virtual Constant *castToUShort(const Constant *V) const = 0;
54 virtual Constant *castToInt (const Constant *V) const = 0;
55 virtual Constant *castToUInt (const Constant *V) const = 0;
56 virtual Constant *castToLong (const Constant *V) const = 0;
57 virtual Constant *castToULong (const Constant *V) const = 0;
58 virtual Constant *castToFloat (const Constant *V) const = 0;
59 virtual Constant *castToDouble(const Constant *V) const = 0;
60 virtual Constant *castToPointer(const Constant *V,
61 const PointerType *Ty) const = 0;
62
63 // ConstRules::get - Return an instance of ConstRules for the specified
64 // constant operands.
65 //
66 static ConstRules &get(const Constant *V1, const Constant *V2);
67 private:
68 ConstRules(const ConstRules &); // Do not implement
69 ConstRules &operator=(const ConstRules &); // Do not implement
70 };
71}
72
73
Chris Lattner2f7c9632001-06-06 20:29:01 +000074//===----------------------------------------------------------------------===//
75// TemplateRules Class
76//===----------------------------------------------------------------------===//
77//
78// TemplateRules - Implement a subclass of ConstRules that provides all
79// operations as noops. All other rules classes inherit from this class so
80// that if functionality is needed in the future, it can simply be added here
81// and to ConstRules without changing anything else...
82//
83// This class also provides subclasses with typesafe implementations of methods
84// so that don't have to do type casting.
85//
86template<class ArgType, class SubClassName>
87class TemplateRules : public ConstRules {
88
89 //===--------------------------------------------------------------------===//
90 // Redirecting functions that cast to the appropriate types
91 //===--------------------------------------------------------------------===//
92
Chris Lattnere87f65e2002-07-30 16:24:28 +000093 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000094 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
95 }
Chris Lattnere87f65e2002-07-30 16:24:28 +000096 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
98 }
Chris Lattnere87f65e2002-07-30 16:24:28 +000099 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000100 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
101 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000102 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000103 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
104 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000105 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000106 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
107 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000108 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
109 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
110 }
111 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
112 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
113 }
114 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
115 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
116 }
117 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000118 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
119 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000120 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000121 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
122 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000123
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000124 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000125 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
126 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000127 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000128 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
129 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130
Chris Lattner55406842001-07-21 19:10:49 +0000131 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000132 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000133 return SubClassName::CastToBool((const ArgType*)V);
134 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000135 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000136 return SubClassName::CastToSByte((const ArgType*)V);
137 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000138 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000139 return SubClassName::CastToUByte((const ArgType*)V);
140 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000141 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000142 return SubClassName::CastToShort((const ArgType*)V);
143 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000144 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000145 return SubClassName::CastToUShort((const ArgType*)V);
146 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000147 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000148 return SubClassName::CastToInt((const ArgType*)V);
149 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000150 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000151 return SubClassName::CastToUInt((const ArgType*)V);
152 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000153 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000154 return SubClassName::CastToLong((const ArgType*)V);
155 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000156 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000157 return SubClassName::CastToULong((const ArgType*)V);
158 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000159 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000160 return SubClassName::CastToFloat((const ArgType*)V);
161 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000162 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000163 return SubClassName::CastToDouble((const ArgType*)V);
164 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000165 virtual Constant *castToPointer(const Constant *V,
166 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000167 return SubClassName::CastToPointer((const ArgType*)V, Ty);
168 }
Chris Lattner55406842001-07-21 19:10:49 +0000169
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170 //===--------------------------------------------------------------------===//
171 // Default "noop" implementations
172 //===--------------------------------------------------------------------===//
173
Chris Lattnere87f65e2002-07-30 16:24:28 +0000174 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
175 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
176 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
177 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
178 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
179 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
180 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
181 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
182 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
183 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000184 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185 return 0;
186 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000187 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000188 return 0;
189 }
Chris Lattner55406842001-07-21 19:10:49 +0000190
191 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000192 static Constant *CastToBool (const Constant *V) { return 0; }
193 static Constant *CastToSByte (const Constant *V) { return 0; }
194 static Constant *CastToUByte (const Constant *V) { return 0; }
195 static Constant *CastToShort (const Constant *V) { return 0; }
196 static Constant *CastToUShort(const Constant *V) { return 0; }
197 static Constant *CastToInt (const Constant *V) { return 0; }
198 static Constant *CastToUInt (const Constant *V) { return 0; }
199 static Constant *CastToLong (const Constant *V) { return 0; }
200 static Constant *CastToULong (const Constant *V) { return 0; }
201 static Constant *CastToFloat (const Constant *V) { return 0; }
202 static Constant *CastToDouble(const Constant *V) { return 0; }
203 static Constant *CastToPointer(const Constant *,
204 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205};
206
207
208
209//===----------------------------------------------------------------------===//
210// EmptyRules Class
211//===----------------------------------------------------------------------===//
212//
213// EmptyRules provides a concrete base class of ConstRules that does nothing
214//
Chris Lattner3462ae32001-12-03 22:26:30 +0000215struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000216 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000217 if (V1 == V2) return ConstantBool::True;
218 return 0;
219 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000220};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221
222
223
224//===----------------------------------------------------------------------===//
225// BoolRules Class
226//===----------------------------------------------------------------------===//
227//
228// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
229//
Chris Lattner3462ae32001-12-03 22:26:30 +0000230struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000232 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){
Chris Lattner07507a42002-09-03 20:09:49 +0000233 return ConstantBool::get(V1->getValue() < V2->getValue());
234 }
235
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000236 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000237 return ConstantBool::get(V1 == V2);
238 }
239
Chris Lattnere87f65e2002-07-30 16:24:28 +0000240 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
241 return ConstantBool::get(V1->getValue() & V2->getValue());
242 }
243
244 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000245 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000246 }
247
Chris Lattnere87f65e2002-07-30 16:24:28 +0000248 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
249 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000251
252 // Casting operators. ick
253#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000254 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000255 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
256 }
257
258 DEF_CAST(Bool , ConstantBool, bool)
259 DEF_CAST(SByte , ConstantSInt, signed char)
260 DEF_CAST(UByte , ConstantUInt, unsigned char)
261 DEF_CAST(Short , ConstantSInt, signed short)
262 DEF_CAST(UShort, ConstantUInt, unsigned short)
263 DEF_CAST(Int , ConstantSInt, signed int)
264 DEF_CAST(UInt , ConstantUInt, unsigned int)
265 DEF_CAST(Long , ConstantSInt, int64_t)
266 DEF_CAST(ULong , ConstantUInt, uint64_t)
267 DEF_CAST(Float , ConstantFP , float)
268 DEF_CAST(Double, ConstantFP , double)
269#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000270};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271
272
273//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000274// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000275//===----------------------------------------------------------------------===//
276//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000277// NullPointerRules provides a concrete base class of ConstRules for null
278// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000279//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000280struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000281 NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000282 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000283 return ConstantBool::True; // Null pointers are always equal
284 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000285 static Constant *CastToBool(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000286 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000287 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000288 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000289 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000290 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000291 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000292 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000293 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000294 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000295 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000296 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000297 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000298 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000299 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000300 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000301 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000302 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000303 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000304 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000305 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000306 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000307 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000308 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000309 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000310 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000311 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000312 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000313 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000314 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000315 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000316 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000317 }
318
Chris Lattner77f20dc2003-11-17 19:21:04 +0000319 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000320 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000321 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000322 }
323};
324
325
326//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000327// DirectRules Class
328//===----------------------------------------------------------------------===//
329//
330// DirectRules provides a concrete base classes of ConstRules for a variety of
331// different types. This allows the C++ compiler to automatically generate our
332// constant handling operations in a typesafe and accurate manner.
333//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000334template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
335struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000336 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
337 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
338 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000339 }
340
Chris Lattnere87f65e2002-07-30 16:24:28 +0000341 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
342 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
343 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344 }
345
Chris Lattnere87f65e2002-07-30 16:24:28 +0000346 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
347 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
348 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000349 }
350
Chris Lattnere87f65e2002-07-30 16:24:28 +0000351 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000352 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000353 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
354 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000355 }
356
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000357 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000358 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
359 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360 }
Chris Lattner55406842001-07-21 19:10:49 +0000361
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000362 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000363 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
364 return ConstantBool::get(R);
365 }
366
Chris Lattner1f0049c2003-04-17 19:24:18 +0000367 static Constant *CastToPointer(const ConstantClass *V,
368 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000369 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000370 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000371 return 0; // Can't const prop other types of pointers
372 }
373
Chris Lattner55406842001-07-21 19:10:49 +0000374 // Casting operators. ick
375#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000376 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000377 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000378 }
379
Chris Lattner3462ae32001-12-03 22:26:30 +0000380 DEF_CAST(Bool , ConstantBool, bool)
381 DEF_CAST(SByte , ConstantSInt, signed char)
382 DEF_CAST(UByte , ConstantUInt, unsigned char)
383 DEF_CAST(Short , ConstantSInt, signed short)
384 DEF_CAST(UShort, ConstantUInt, unsigned short)
385 DEF_CAST(Int , ConstantSInt, signed int)
386 DEF_CAST(UInt , ConstantUInt, unsigned int)
387 DEF_CAST(Long , ConstantSInt, int64_t)
388 DEF_CAST(ULong , ConstantUInt, uint64_t)
389 DEF_CAST(Float , ConstantFP , float)
390 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000391#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392};
393
Chris Lattner62af86e2002-05-03 20:09:52 +0000394
395//===----------------------------------------------------------------------===//
396// DirectIntRules Class
397//===----------------------------------------------------------------------===//
398//
399// DirectIntRules provides implementations of functions that are valid on
400// integer types, but not all types in general.
401//
402template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000403struct DirectIntRules
404 : public DirectRules<ConstantClass, BuiltinType, Ty,
405 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000406
Chris Lattner268916262003-05-12 15:26:25 +0000407 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
408 if (V2->isNullValue()) return 0;
409 if (V2->isAllOnesValue() && // MIN_INT / -1
410 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
411 return 0;
412 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
413 return ConstantClass::get(*Ty, R);
414 }
415
Chris Lattnere87f65e2002-07-30 16:24:28 +0000416 static Constant *Rem(const ConstantClass *V1,
417 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000418 if (V2->isNullValue()) return 0; // X / 0
419 if (V2->isAllOnesValue() && // MIN_INT / -1
420 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
421 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000422 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
423 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000424 }
Chris Lattner6670d862002-05-06 03:00:54 +0000425
Chris Lattnere87f65e2002-07-30 16:24:28 +0000426 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
427 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
428 return ConstantClass::get(*Ty, R);
429 }
430 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
431 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
432 return ConstantClass::get(*Ty, R);
433 }
434 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
435 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
436 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000437 }
438
Chris Lattnere87f65e2002-07-30 16:24:28 +0000439 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
440 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
441 return ConstantClass::get(*Ty, R);
442 }
443
444 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
445 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
446 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000447 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000448};
449
450
451//===----------------------------------------------------------------------===//
452// DirectFPRules Class
453//===----------------------------------------------------------------------===//
454//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000455/// DirectFPRules provides implementations of functions that are valid on
456/// floating point types, but not all types in general.
457///
Chris Lattner0a144ad2002-05-03 21:41:07 +0000458template <class ConstantClass, class BuiltinType, Type **Ty>
459struct DirectFPRules
460 : public DirectRules<ConstantClass, BuiltinType, Ty,
461 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000462 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000463 if (V2->isNullValue()) return 0;
464 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
465 (BuiltinType)V2->getValue());
466 return ConstantClass::get(*Ty, Result);
467 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000468};
469
Chris Lattner1dd054c2004-01-12 22:07:24 +0000470
471/// ConstRules::get - This method returns the constant rules implementation that
472/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000473ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000474 static EmptyRules EmptyR;
475 static BoolRules BoolR;
476 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000477 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
478 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
479 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
480 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
481 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
482 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
483 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
484 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
485 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
486 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000487
Chris Lattner4b6addf2003-11-17 19:19:32 +0000488 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000489 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
490 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000491 return EmptyR;
492
Chris Lattner6b727592004-06-17 18:19:28 +0000493 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000494 default: assert(0 && "Unknown value type for constant folding!");
495 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000496 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000497 case Type::SByteTyID: return SByteR;
498 case Type::UByteTyID: return UByteR;
499 case Type::ShortTyID: return ShortR;
500 case Type::UShortTyID: return UShortR;
501 case Type::IntTyID: return IntR;
502 case Type::UIntTyID: return UIntR;
503 case Type::LongTyID: return LongR;
504 case Type::ULongTyID: return ULongR;
505 case Type::FloatTyID: return FloatR;
506 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000507 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000508}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000509
510
511//===----------------------------------------------------------------------===//
512// ConstantFold*Instruction Implementations
513//===----------------------------------------------------------------------===//
514//
515// These methods contain the special case hackery required to symbolically
516// evaluate some constant expression cases, and use the ConstantRules class to
517// evaluate normal constants.
518//
519static unsigned getSize(const Type *Ty) {
520 unsigned S = Ty->getPrimitiveSize();
521 return S ? S : 8; // Treat pointers at 8 bytes
522}
523
524Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
525 const Type *DestTy) {
526 if (V->getType() == DestTy) return (Constant*)V;
527
Chris Lattnerea0789c2004-03-08 06:17:35 +0000528 // Cast of a global address to boolean is always true.
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000529 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerea0789c2004-03-08 06:17:35 +0000530 if (DestTy == Type::BoolTy)
531 // FIXME: When we support 'external weak' references, we have to prevent
Chris Lattnercd4003e2005-01-06 16:26:38 +0000532 // this transformation from happening. This code will need to be updated
533 // to ignore external weak symbols when we support it.
534 return ConstantBool::True;
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000535 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000536 if (CE->getOpcode() == Instruction::Cast) {
537 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
538 // Try to not produce a cast of a cast, which is almost always redundant.
539 if (!Op->getType()->isFloatingPoint() &&
540 !CE->getType()->isFloatingPoint() &&
Reid Spencer8eb06df2004-05-30 01:19:48 +0000541 !DestTy->isFloatingPoint()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000542 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
543 unsigned S3 = getSize(DestTy);
544 if (Op->getType() == DestTy && S3 >= S2)
545 return Op;
546 if (S1 >= S2 && S2 >= S3)
547 return ConstantExpr::getCast(Op, DestTy);
548 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
549 return ConstantExpr::getCast(Op, DestTy);
550 }
551 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
552 // If all of the indexes in the GEP are null values, there is no pointer
553 // adjustment going on. We might as well cast the source pointer.
554 bool isAllNull = true;
555 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
556 if (!CE->getOperand(i)->isNullValue()) {
557 isAllNull = false;
558 break;
559 }
560 if (isAllNull)
561 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
562 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000563 } else if (isa<UndefValue>(V)) {
564 return UndefValue::get(DestTy);
565 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000566
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000567 // Check to see if we are casting an pointer to an aggregate to a pointer to
568 // the first element. If so, return the appropriate GEP instruction.
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000569 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000570 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
571 std::vector<Value*> IdxList;
572 IdxList.push_back(Constant::getNullValue(Type::IntTy));
573 const Type *ElTy = PTy->getElementType();
574 while (ElTy != DPTy->getElementType()) {
575 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
Chris Lattner9e907202004-11-22 19:15:27 +0000576 if (STy->getNumElements() == 0) break;
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000577 ElTy = STy->getElementType(0);
578 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
579 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
580 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
581 ElTy = STy->getElementType();
582 IdxList.push_back(IdxList[0]);
583 } else {
584 break;
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000585 }
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000586 }
587
588 if (ElTy == DPTy->getElementType())
589 return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
590 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000591
Chris Lattner1dd054c2004-01-12 22:07:24 +0000592 ConstRules &Rules = ConstRules::get(V, V);
593
Chris Lattner6b727592004-06-17 18:19:28 +0000594 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000595 case Type::BoolTyID: return Rules.castToBool(V);
596 case Type::UByteTyID: return Rules.castToUByte(V);
597 case Type::SByteTyID: return Rules.castToSByte(V);
598 case Type::UShortTyID: return Rules.castToUShort(V);
599 case Type::ShortTyID: return Rules.castToShort(V);
600 case Type::UIntTyID: return Rules.castToUInt(V);
601 case Type::IntTyID: return Rules.castToInt(V);
602 case Type::ULongTyID: return Rules.castToULong(V);
603 case Type::LongTyID: return Rules.castToLong(V);
604 case Type::FloatTyID: return Rules.castToFloat(V);
605 case Type::DoubleTyID: return Rules.castToDouble(V);
606 case Type::PointerTyID:
607 return Rules.castToPointer(V, cast<PointerType>(DestTy));
608 default: return 0;
609 }
610}
611
Chris Lattner6ea4b522004-03-12 05:53:32 +0000612Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
613 const Constant *V1,
614 const Constant *V2) {
615 if (Cond == ConstantBool::True)
616 return const_cast<Constant*>(V1);
617 else if (Cond == ConstantBool::False)
618 return const_cast<Constant*>(V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000619
620 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
621 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
622 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000623 return 0;
624}
625
626
Chris Lattner061da2f2004-01-13 05:51:55 +0000627/// IdxCompare - Compare the two constants as though they were getelementptr
628/// indices. This allows coersion of the types to be the same thing.
629///
630/// If the two constants are the "same" (after coersion), return 0. If the
631/// first is less than the second, return -1, if the second is less than the
632/// first, return 1. If the constants are not integral, return -2.
633///
634static int IdxCompare(Constant *C1, Constant *C2) {
635 if (C1 == C2) return 0;
636
637 // Ok, we found a different index. Are either of the operands
638 // ConstantExprs? If so, we can't do anything with them.
639 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
640 return -2; // don't know!
641
Chris Lattner69193f92004-04-05 01:30:19 +0000642 // Ok, we have two differing integer indices. Sign extend them to be the same
643 // type. Long is always big enough, so we use it.
644 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
645 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000646 if (C1 == C2) return 0; // Are they just differing types?
647
648 // If they are really different, now that they are the same type, then we
649 // found a difference!
650 if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
651 return -1;
652 else
653 return 1;
654}
655
656/// evaluateRelation - This function determines if there is anything we can
657/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000658/// things like integer comparisons, but should instead handle ConstantExprs
659/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +0000660/// particular relation to each other, we should return the corresponding SetCC
661/// code, otherwise return Instruction::BinaryOpsEnd.
662///
663/// To simplify this code we canonicalize the relation so that the first
664/// operand is always the most "complex" of the two. We consider simple
665/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000666/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000667///
668static Instruction::BinaryOps evaluateRelation(const Constant *V1,
669 const Constant *V2) {
670 assert(V1->getType() == V2->getType() &&
671 "Cannot compare different types of values!");
672 if (V1 == V2) return Instruction::SetEQ;
673
Reid Spenceraccd7c72004-07-17 23:47:01 +0000674 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000675 // If the first operand is simple, swap operands.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000676 assert((isa<GlobalValue>(V2) || isa<ConstantExpr>(V2)) &&
Chris Lattner061da2f2004-01-13 05:51:55 +0000677 "Simple cases should have been handled by caller!");
Chris Lattner125ed542004-02-01 01:23:19 +0000678 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
679 if (SwappedRelation != Instruction::BinaryOpsEnd)
680 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000681
Reid Spenceraccd7c72004-07-17 23:47:01 +0000682 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)){
Chris Lattner125ed542004-02-01 01:23:19 +0000683 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
684 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
685 if (SwappedRelation != Instruction::BinaryOpsEnd)
686 return SetCondInst::getSwappedCondition(SwappedRelation);
687 else
688 return Instruction::BinaryOpsEnd;
689 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000690
Reid Spenceraccd7c72004-07-17 23:47:01 +0000691 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000692 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000693 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
694 assert(CPR1 != CPR2 &&
695 "GVs for the same value exist at different addresses??");
Chris Lattner061da2f2004-01-13 05:51:55 +0000696 // FIXME: If both globals are external weak, they might both be null!
697 return Instruction::SetNE;
698 } else {
699 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
700 // Global can never be null. FIXME: if we implement external weak
701 // linkage, this is not necessarily true!
702 return Instruction::SetNE;
703 }
704
705 } else {
706 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
707 // constantexpr, a CPR, or a simple constant.
708 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
709 Constant *CE1Op0 = CE1->getOperand(0);
710
711 switch (CE1->getOpcode()) {
712 case Instruction::Cast:
713 // If the cast is not actually changing bits, and the second operand is a
714 // null pointer, do the comparison with the pre-casted value.
715 if (V2->isNullValue() &&
716 CE1->getType()->isLosslesslyConvertibleTo(CE1Op0->getType()))
717 return evaluateRelation(CE1Op0,
718 Constant::getNullValue(CE1Op0->getType()));
Chris Lattner192e3262004-04-11 01:29:30 +0000719 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000720
721 case Instruction::GetElementPtr:
722 // Ok, since this is a getelementptr, we know that the constant has a
723 // pointer type. Check the various cases.
724 if (isa<ConstantPointerNull>(V2)) {
725 // If we are comparing a GEP to a null pointer, check to see if the base
726 // of the GEP equals the null pointer.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000727 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000728 // FIXME: this is not true when we have external weak references!
729 // No offset can go from a global to a null pointer.
730 return Instruction::SetGT;
731 } else if (isa<ConstantPointerNull>(CE1Op0)) {
732 // If we are indexing from a null pointer, check to see if we have any
733 // non-zero indices.
734 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
735 if (!CE1->getOperand(i)->isNullValue())
736 // Offsetting from null, must not be equal.
737 return Instruction::SetGT;
738 // Only zero indexes from null, must still be zero.
739 return Instruction::SetEQ;
740 }
741 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000742 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000743 if (isa<ConstantPointerNull>(CE1Op0)) {
744 // FIXME: This is not true with external weak references.
745 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000746 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000747 if (CPR1 == CPR2) {
748 // If this is a getelementptr of the same global, then it must be
749 // different. Because the types must match, the getelementptr could
750 // only have at most one index, and because we fold getelementptr's
751 // with a single zero index, it must be nonzero.
752 assert(CE1->getNumOperands() == 2 &&
753 !CE1->getOperand(1)->isNullValue() &&
754 "Suprising getelementptr!");
755 return Instruction::SetGT;
756 } else {
757 // If they are different globals, we don't know what the value is,
758 // but they can't be equal.
759 return Instruction::SetNE;
760 }
761 }
762 } else {
763 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
764 const Constant *CE2Op0 = CE2->getOperand(0);
765
766 // There are MANY other foldings that we could perform here. They will
767 // probably be added on demand, as they seem needed.
768 switch (CE2->getOpcode()) {
769 default: break;
770 case Instruction::GetElementPtr:
771 // By far the most common case to handle is when the base pointers are
772 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000773 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000774 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
775 return Instruction::SetNE;
776 // Ok, we know that both getelementptr instructions are based on the
777 // same global. From this, we can precisely determine the relative
778 // ordering of the resultant pointers.
779 unsigned i = 1;
780
781 // Compare all of the operands the GEP's have in common.
782 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); ++i)
783 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i))) {
784 case -1: return Instruction::SetLT;
785 case 1: return Instruction::SetGT;
786 case -2: return Instruction::BinaryOpsEnd;
787 }
788
789 // Ok, we ran out of things they have in common. If any leftovers
790 // are non-zero then we have a difference, otherwise we are equal.
791 for (; i < CE1->getNumOperands(); ++i)
792 if (!CE1->getOperand(i)->isNullValue())
793 return Instruction::SetGT;
794 for (; i < CE2->getNumOperands(); ++i)
795 if (!CE2->getOperand(i)->isNullValue())
796 return Instruction::SetLT;
797 return Instruction::SetEQ;
798 }
799 }
800 }
801
802 default:
803 break;
804 }
805 }
806
807 return Instruction::BinaryOpsEnd;
808}
809
Chris Lattner1dd054c2004-01-12 22:07:24 +0000810Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
811 const Constant *V1,
812 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000813 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000814 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000815 default: break;
816 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
817 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
818 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
819 case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break;
820 case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break;
821 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
822 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
823 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
824 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
825 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
826 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
827 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
828 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000829 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
830 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner061da2f2004-01-13 05:51:55 +0000831 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000832 break;
833 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
834 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner061da2f2004-01-13 05:51:55 +0000835 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000836 break;
837 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
838 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner061da2f2004-01-13 05:51:55 +0000839 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000840 break;
841 }
842
Chris Lattner061da2f2004-01-13 05:51:55 +0000843 // If we successfully folded the expression, return it now.
844 if (C) return C;
845
Chris Lattner192eacc2004-10-17 04:01:51 +0000846 if (SetCondInst::isRelational(Opcode)) {
847 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
848 return UndefValue::get(Type::BoolTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000849 switch (evaluateRelation(V1, V2)) {
850 default: assert(0 && "Unknown relational!");
851 case Instruction::BinaryOpsEnd:
852 break; // Couldn't determine anything about these constants.
853 case Instruction::SetEQ: // We know the constants are equal!
854 // If we know the constants are equal, we can decide the result of this
855 // computation precisely.
856 return ConstantBool::get(Opcode == Instruction::SetEQ ||
857 Opcode == Instruction::SetLE ||
858 Opcode == Instruction::SetGE);
859 case Instruction::SetLT:
860 // If we know that V1 < V2, we can decide the result of this computation
861 // precisely.
862 return ConstantBool::get(Opcode == Instruction::SetLT ||
863 Opcode == Instruction::SetNE ||
864 Opcode == Instruction::SetLE);
865 case Instruction::SetGT:
866 // If we know that V1 > V2, we can decide the result of this computation
867 // precisely.
868 return ConstantBool::get(Opcode == Instruction::SetGT ||
869 Opcode == Instruction::SetNE ||
870 Opcode == Instruction::SetGE);
871 case Instruction::SetLE:
872 // If we know that V1 <= V2, we can only partially decide this relation.
873 if (Opcode == Instruction::SetGT) return ConstantBool::False;
874 if (Opcode == Instruction::SetLT) return ConstantBool::True;
875 break;
876
877 case Instruction::SetGE:
878 // If we know that V1 >= V2, we can only partially decide this relation.
879 if (Opcode == Instruction::SetLT) return ConstantBool::False;
880 if (Opcode == Instruction::SetGT) return ConstantBool::True;
881 break;
882
883 case Instruction::SetNE:
884 // If we know that V1 != V2, we can only partially decide this relation.
885 if (Opcode == Instruction::SetEQ) return ConstantBool::False;
886 if (Opcode == Instruction::SetNE) return ConstantBool::True;
887 break;
888 }
Chris Lattner192eacc2004-10-17 04:01:51 +0000889 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000890
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000891 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
892 switch (Opcode) {
893 case Instruction::Add:
894 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000895 case Instruction::Xor:
896 return UndefValue::get(V1->getType());
897
898 case Instruction::Mul:
899 case Instruction::And:
900 return Constant::getNullValue(V1->getType());
901 case Instruction::Div:
902 case Instruction::Rem:
903 if (!isa<UndefValue>(V2)) // undef/X -> 0
904 return Constant::getNullValue(V1->getType());
905 return const_cast<Constant*>(V2); // X/undef -> undef
906 case Instruction::Or: // X|undef -> -1
907 return ConstantInt::getAllOnesValue(V1->getType());
908 case Instruction::Shr:
909 if (!isa<UndefValue>(V2)) {
910 if (V1->getType()->isSigned())
911 return const_cast<Constant*>(V1); // undef >>s X -> undef
912 // undef >>u X -> 0
913 } else if (isa<UndefValue>(V1)) {
914 return const_cast<Constant*>(V1); // undef >> undef -> undef
915 } else {
916 if (V1->getType()->isSigned())
917 return const_cast<Constant*>(V1); // X >>s undef -> X
918 // X >>u undef -> 0
919 }
920 return Constant::getNullValue(V1->getType());
921
922 case Instruction::Shl:
923 // undef << X -> 0 X << undef -> 0
924 return Constant::getNullValue(V1->getType());
925 }
926 }
927
Chris Lattner061da2f2004-01-13 05:51:55 +0000928 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
929 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
930 // There are many possible foldings we could do here. We should probably
931 // at least fold add of a pointer with an integer into the appropriate
932 // getelementptr. This will improve alias analysis a bit.
933
934
935
936
937 } else {
938 // Just implement a couple of simple identities.
939 switch (Opcode) {
940 case Instruction::Add:
941 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
942 break;
943 case Instruction::Sub:
944 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
945 break;
946 case Instruction::Mul:
947 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
948 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
949 if (CI->getRawValue() == 1)
950 return const_cast<Constant*>(V1); // X * 1 == X
951 break;
952 case Instruction::Div:
953 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
954 if (CI->getRawValue() == 1)
955 return const_cast<Constant*>(V1); // X / 1 == X
956 break;
957 case Instruction::Rem:
958 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
959 if (CI->getRawValue() == 1)
960 return Constant::getNullValue(CI->getType()); // X % 1 == 0
961 break;
962 case Instruction::And:
963 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
964 return const_cast<Constant*>(V1); // X & -1 == X
965 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Chris Lattnerea0789c2004-03-08 06:17:35 +0000966 if (CE1->getOpcode() == Instruction::Cast &&
Reid Spenceraccd7c72004-07-17 23:47:01 +0000967 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +0000968 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +0000969
970 // Functions are at least 4-byte aligned. If and'ing the address of a
971 // function with a constant < 4, fold it to zero.
972 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spenceraccd7c72004-07-17 23:47:01 +0000973 if (CI->getRawValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +0000974 return Constant::getNullValue(CI->getType());
975 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000976 break;
977 case Instruction::Or:
978 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
979 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
980 return const_cast<Constant*>(V2); // X | -1 == -1
981 break;
982 case Instruction::Xor:
983 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
984 break;
985 }
986 }
987
988 } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
989 // If V2 is a constant expr and V1 isn't, flop them around and fold the
990 // other way if possible.
991 switch (Opcode) {
992 case Instruction::Add:
993 case Instruction::Mul:
994 case Instruction::And:
995 case Instruction::Or:
996 case Instruction::Xor:
997 case Instruction::SetEQ:
998 case Instruction::SetNE:
999 // No change of opcode required.
1000 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1001
1002 case Instruction::SetLT:
1003 case Instruction::SetGT:
1004 case Instruction::SetLE:
1005 case Instruction::SetGE:
1006 // Change the opcode as necessary to swap the operands.
1007 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1008 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1009
1010 case Instruction::Shl:
1011 case Instruction::Shr:
1012 case Instruction::Sub:
1013 case Instruction::Div:
1014 case Instruction::Rem:
1015 default: // These instructions cannot be flopped around.
1016 break;
1017 }
1018 }
1019 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001020}
1021
1022Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001023 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001024 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001025 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001026 return const_cast<Constant*>(C);
1027
Chris Lattnerf6013752004-10-17 21:54:55 +00001028 if (isa<UndefValue>(C)) {
1029 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1030 true);
1031 assert(Ty != 0 && "Invalid indices for GEP!");
1032 return UndefValue::get(PointerType::get(Ty));
1033 }
1034
1035 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001036 if (C->isNullValue()) {
1037 bool isNull = true;
1038 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001039 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001040 isNull = false;
1041 break;
1042 }
1043 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001044 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001045 true);
1046 assert(Ty != 0 && "Invalid indices for GEP!");
1047 return ConstantPointerNull::get(PointerType::get(Ty));
1048 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001049
1050 if (IdxList.size() == 1) {
1051 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
1052 if (unsigned ElSize = ElTy->getPrimitiveSize()) {
1053 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1054 // type, we can statically fold this.
1055 Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
Chris Lattner13128ab2004-10-11 22:52:25 +00001056 R = ConstantExpr::getCast(R, Idx0->getType());
1057 R = ConstantExpr::getMul(R, Idx0);
Chris Lattner4bbd4092004-07-15 01:16:59 +00001058 return ConstantExpr::getCast(R, C->getType());
1059 }
1060 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001061 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001062
1063 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1064 // Combine Indices - If the source pointer to this getelementptr instruction
1065 // is a getelementptr instruction, combine the indices of the two
1066 // getelementptr instructions into a single instruction.
1067 //
1068 if (CE->getOpcode() == Instruction::GetElementPtr) {
1069 const Type *LastTy = 0;
1070 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1071 I != E; ++I)
1072 LastTy = *I;
1073
Chris Lattner13128ab2004-10-11 22:52:25 +00001074 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1075 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001076 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1077 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001078 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001079
1080 // Add the last index of the source with the first index of the new GEP.
1081 // Make sure to handle the case when they are actually different types.
1082 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001083 // Otherwise it must be an array.
1084 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001085 const Type *IdxTy = Combined->getType();
Chris Lattner13128ab2004-10-11 22:52:25 +00001086 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001087 Combined =
1088 ConstantExpr::get(Instruction::Add,
Chris Lattner13128ab2004-10-11 22:52:25 +00001089 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattner71068a02004-07-07 04:45:13 +00001090 ConstantExpr::getCast(Combined, IdxTy));
1091 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001092
1093 NewIndices.push_back(Combined);
1094 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1095 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1096 }
1097 }
1098
1099 // Implement folding of:
1100 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1101 // long 0, long 0)
1102 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1103 //
1104 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
Chris Lattner13128ab2004-10-11 22:52:25 +00001105 Idx0->isNullValue())
Chris Lattner1dd054c2004-01-12 22:07:24 +00001106 if (const PointerType *SPT =
1107 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1108 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1109 if (const ArrayType *CAT =
1110 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
1111 if (CAT->getElementType() == SAT->getElementType())
1112 return ConstantExpr::getGetElementPtr(
1113 (Constant*)CE->getOperand(0), IdxList);
1114 }
1115 return 0;
1116}
1117