blob: 8c1398132fd67294b0c7f8b919e80574c1f3c9e3 [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner6b3f4752006-04-02 01:38:28 +000027#include "llvm/Support/MathExtras.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000028#include <limits>
Chris Lattner0a144ad2002-05-03 21:41:07 +000029#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000030using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000031
Chris Lattner5a945e32004-01-12 21:13:12 +000032namespace {
33 struct ConstRules {
34 ConstRules() {}
Reid Spencer9c47b252005-04-24 22:27:20 +000035 virtual ~ConstRules() {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000036
Chris Lattner5a945e32004-01-12 21:13:12 +000037 // Binary Operators...
38 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
39 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
40 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
42 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
43 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
45 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
46 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
47 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
48 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0;
49 virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0;
50
51 // Casting operators.
52 virtual Constant *castToBool (const Constant *V) const = 0;
53 virtual Constant *castToSByte (const Constant *V) const = 0;
54 virtual Constant *castToUByte (const Constant *V) const = 0;
55 virtual Constant *castToShort (const Constant *V) const = 0;
56 virtual Constant *castToUShort(const Constant *V) const = 0;
57 virtual Constant *castToInt (const Constant *V) const = 0;
58 virtual Constant *castToUInt (const Constant *V) const = 0;
59 virtual Constant *castToLong (const Constant *V) const = 0;
60 virtual Constant *castToULong (const Constant *V) const = 0;
61 virtual Constant *castToFloat (const Constant *V) const = 0;
62 virtual Constant *castToDouble(const Constant *V) const = 0;
63 virtual Constant *castToPointer(const Constant *V,
64 const PointerType *Ty) const = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +000065
Chris Lattner5a945e32004-01-12 21:13:12 +000066 // ConstRules::get - Return an instance of ConstRules for the specified
67 // constant operands.
68 //
69 static ConstRules &get(const Constant *V1, const Constant *V2);
70 private:
71 ConstRules(const ConstRules &); // Do not implement
72 ConstRules &operator=(const ConstRules &); // Do not implement
73 };
74}
75
76
Chris Lattner2f7c9632001-06-06 20:29:01 +000077//===----------------------------------------------------------------------===//
78// TemplateRules Class
79//===----------------------------------------------------------------------===//
80//
Misha Brukmanb1c93172005-04-21 23:48:37 +000081// TemplateRules - Implement a subclass of ConstRules that provides all
82// operations as noops. All other rules classes inherit from this class so
83// that if functionality is needed in the future, it can simply be added here
Chris Lattner2f7c9632001-06-06 20:29:01 +000084// and to ConstRules without changing anything else...
Misha Brukmanb1c93172005-04-21 23:48:37 +000085//
Chris Lattner2f7c9632001-06-06 20:29:01 +000086// This class also provides subclasses with typesafe implementations of methods
87// so that don't have to do type casting.
88//
Chris Lattner6a871e12006-06-21 18:13:36 +000089namespace {
Chris Lattner2f7c9632001-06-06 20:29:01 +000090template<class ArgType, class SubClassName>
91class TemplateRules : public ConstRules {
92
Reid Spencer9c47b252005-04-24 22:27:20 +000093
Chris Lattner2f7c9632001-06-06 20:29:01 +000094 //===--------------------------------------------------------------------===//
95 // Redirecting functions that cast to the appropriate types
96 //===--------------------------------------------------------------------===//
97
Misha Brukmanb1c93172005-04-21 23:48:37 +000098 virtual Constant *add(const Constant *V1, const Constant *V2) const {
99 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
102 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000104 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
105 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000106 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000107 virtual Constant *div(const Constant *V1, const Constant *V2) const {
108 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
Chris Lattneraf259a72002-04-07 08:10:14 +0000109 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000110 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
111 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000112 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000113 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
114 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000115 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000116 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
117 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000118 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000119 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
120 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000121 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
123 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000124 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000125 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
126 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000127 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000128
Misha Brukmanb1c93172005-04-21 23:48:37 +0000129 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
131 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000132 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000133 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
134 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135
Chris Lattner55406842001-07-21 19:10:49 +0000136 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000137 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000138 return SubClassName::CastToBool((const ArgType*)V);
139 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000140 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000141 return SubClassName::CastToSByte((const ArgType*)V);
142 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000143 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000144 return SubClassName::CastToUByte((const ArgType*)V);
145 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000146 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000147 return SubClassName::CastToShort((const ArgType*)V);
148 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000149 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000150 return SubClassName::CastToUShort((const ArgType*)V);
151 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000152 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000153 return SubClassName::CastToInt((const ArgType*)V);
154 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000155 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000156 return SubClassName::CastToUInt((const ArgType*)V);
157 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000158 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000159 return SubClassName::CastToLong((const ArgType*)V);
160 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000161 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000162 return SubClassName::CastToULong((const ArgType*)V);
163 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000164 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000165 return SubClassName::CastToFloat((const ArgType*)V);
166 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000167 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000168 return SubClassName::CastToDouble((const ArgType*)V);
169 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000170 virtual Constant *castToPointer(const Constant *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000171 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000172 return SubClassName::CastToPointer((const ArgType*)V, Ty);
173 }
Chris Lattner55406842001-07-21 19:10:49 +0000174
Chris Lattner2f7c9632001-06-06 20:29:01 +0000175 //===--------------------------------------------------------------------===//
176 // Default "noop" implementations
177 //===--------------------------------------------------------------------===//
178
Chris Lattnere87f65e2002-07-30 16:24:28 +0000179 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
180 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
181 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
182 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
183 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
184 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
185 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
186 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
187 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
188 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000189 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190 return 0;
191 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000192 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000193 return 0;
194 }
Chris Lattner55406842001-07-21 19:10:49 +0000195
196 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000197 static Constant *CastToBool (const Constant *V) { return 0; }
198 static Constant *CastToSByte (const Constant *V) { return 0; }
199 static Constant *CastToUByte (const Constant *V) { return 0; }
200 static Constant *CastToShort (const Constant *V) { return 0; }
201 static Constant *CastToUShort(const Constant *V) { return 0; }
202 static Constant *CastToInt (const Constant *V) { return 0; }
203 static Constant *CastToUInt (const Constant *V) { return 0; }
204 static Constant *CastToLong (const Constant *V) { return 0; }
205 static Constant *CastToULong (const Constant *V) { return 0; }
206 static Constant *CastToFloat (const Constant *V) { return 0; }
207 static Constant *CastToDouble(const Constant *V) { return 0; }
208 static Constant *CastToPointer(const Constant *,
209 const PointerType *) {return 0;}
Reid Spencer9c47b252005-04-24 22:27:20 +0000210
211public:
212 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213};
Chris Lattner6a871e12006-06-21 18:13:36 +0000214} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215
216
217//===----------------------------------------------------------------------===//
218// EmptyRules Class
219//===----------------------------------------------------------------------===//
220//
221// EmptyRules provides a concrete base class of ConstRules that does nothing
222//
Chris Lattner6a871e12006-06-21 18:13:36 +0000223namespace {
Chris Lattner3462ae32001-12-03 22:26:30 +0000224struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000225 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000226 if (V1 == V2) return ConstantBool::True;
227 return 0;
228 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000229};
Chris Lattner6a871e12006-06-21 18:13:36 +0000230} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231
232
233
234//===----------------------------------------------------------------------===//
235// BoolRules Class
236//===----------------------------------------------------------------------===//
237//
238// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
239//
Chris Lattner6a871e12006-06-21 18:13:36 +0000240namespace {
Chris Lattner3462ae32001-12-03 22:26:30 +0000241struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000243 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner07507a42002-09-03 20:09:49 +0000244 return ConstantBool::get(V1->getValue() < V2->getValue());
245 }
246
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000247 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000248 return ConstantBool::get(V1 == V2);
249 }
250
Chris Lattnere87f65e2002-07-30 16:24:28 +0000251 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
252 return ConstantBool::get(V1->getValue() & V2->getValue());
253 }
254
255 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000256 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 }
258
Chris Lattnere87f65e2002-07-30 16:24:28 +0000259 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
260 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000262
263 // Casting operators. ick
264#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000265 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000266 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
267 }
268
269 DEF_CAST(Bool , ConstantBool, bool)
270 DEF_CAST(SByte , ConstantSInt, signed char)
271 DEF_CAST(UByte , ConstantUInt, unsigned char)
272 DEF_CAST(Short , ConstantSInt, signed short)
273 DEF_CAST(UShort, ConstantUInt, unsigned short)
274 DEF_CAST(Int , ConstantSInt, signed int)
275 DEF_CAST(UInt , ConstantUInt, unsigned int)
276 DEF_CAST(Long , ConstantSInt, int64_t)
277 DEF_CAST(ULong , ConstantUInt, uint64_t)
278 DEF_CAST(Float , ConstantFP , float)
279 DEF_CAST(Double, ConstantFP , double)
280#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000281};
Chris Lattner6a871e12006-06-21 18:13:36 +0000282} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283
284
285//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000286// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000287//===----------------------------------------------------------------------===//
288//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000289// NullPointerRules provides a concrete base class of ConstRules for null
290// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000291//
Chris Lattner6a871e12006-06-21 18:13:36 +0000292namespace {
Chris Lattner77f20dc2003-11-17 19:21:04 +0000293struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000294 NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000295 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000296 return ConstantBool::True; // Null pointers are always equal
297 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000298 static Constant *CastToBool(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000299 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000300 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000301 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000302 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000303 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000304 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000305 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000306 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000307 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000308 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000309 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000310 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000311 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000312 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000313 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000314 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000315 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000316 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000317 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000318 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000319 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000320 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000321 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000322 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000323 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000324 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000325 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000326 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000327 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000328 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000329 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000330 }
331
Chris Lattner77f20dc2003-11-17 19:21:04 +0000332 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000333 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000334 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000335 }
336};
Chris Lattner6a871e12006-06-21 18:13:36 +0000337} // end anonymous namespace
Chris Lattner977f0042001-11-01 05:55:13 +0000338
Chris Lattner1171d952006-01-04 02:03:29 +0000339//===----------------------------------------------------------------------===//
340// ConstantPackedRules Class
341//===----------------------------------------------------------------------===//
342
Chris Lattnerf0f40682006-01-04 02:15:02 +0000343/// DoVectorOp - Given two packed constants and a function pointer, apply the
344/// function pointer to each element pair, producing a new ConstantPacked
345/// constant.
346static Constant *EvalVectorOp(const ConstantPacked *V1,
347 const ConstantPacked *V2,
348 Constant *(*FP)(Constant*, Constant*)) {
349 std::vector<Constant*> Res;
350 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
351 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
352 const_cast<Constant*>(V2->getOperand(i))));
353 return ConstantPacked::get(Res);
354}
355
Chris Lattner1171d952006-01-04 02:03:29 +0000356/// PackedTypeRules provides a concrete base class of ConstRules for
357/// ConstantPacked operands.
358///
Chris Lattner6a871e12006-06-21 18:13:36 +0000359namespace {
Chris Lattner1171d952006-01-04 02:03:29 +0000360struct ConstantPackedRules
361 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerf0f40682006-01-04 02:15:02 +0000362
363 static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
364 return EvalVectorOp(V1, V2, ConstantExpr::getAdd);
365 }
366 static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) {
367 return EvalVectorOp(V1, V2, ConstantExpr::getSub);
368 }
369 static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
370 return EvalVectorOp(V1, V2, ConstantExpr::getMul);
371 }
372 static Constant *Div(const ConstantPacked *V1, const ConstantPacked *V2) {
373 return EvalVectorOp(V1, V2, ConstantExpr::getDiv);
374 }
375 static Constant *Rem(const ConstantPacked *V1, const ConstantPacked *V2) {
376 return EvalVectorOp(V1, V2, ConstantExpr::getRem);
377 }
378 static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) {
379 return EvalVectorOp(V1, V2, ConstantExpr::getAnd);
380 }
381 static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) {
382 return EvalVectorOp(V1, V2, ConstantExpr::getOr);
383 }
384 static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) {
385 return EvalVectorOp(V1, V2, ConstantExpr::getXor);
386 }
387 static Constant *Shl(const ConstantPacked *V1, const ConstantPacked *V2) {
388 return EvalVectorOp(V1, V2, ConstantExpr::getShl);
389 }
390 static Constant *Shr(const ConstantPacked *V1, const ConstantPacked *V2) {
391 return EvalVectorOp(V1, V2, ConstantExpr::getShr);
392 }
393 static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){
394 return 0;
395 }
396 static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) {
Chris Lattner6b52be62006-01-04 02:20:54 +0000397 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) {
398 Constant *C =
399 ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)),
400 const_cast<Constant*>(V2->getOperand(i)));
401 if (ConstantBool *CB = dyn_cast<ConstantBool>(C))
402 return CB;
403 }
404 // Otherwise, could not decide from any element pairs.
Chris Lattnerf0f40682006-01-04 02:15:02 +0000405 return 0;
406 }
Chris Lattner1171d952006-01-04 02:03:29 +0000407};
Chris Lattner6a871e12006-06-21 18:13:36 +0000408} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000409
410
411//===----------------------------------------------------------------------===//
412// GeneralPackedRules Class
413//===----------------------------------------------------------------------===//
414
415/// GeneralPackedRules provides a concrete base class of ConstRules for
416/// PackedType operands, where both operands are not ConstantPacked. The usual
417/// cause for this is that one operand is a ConstantAggregateZero.
418///
Chris Lattner6a871e12006-06-21 18:13:36 +0000419namespace {
Chris Lattner1171d952006-01-04 02:03:29 +0000420struct GeneralPackedRules : public TemplateRules<Constant, GeneralPackedRules> {
421};
Chris Lattner6a871e12006-06-21 18:13:36 +0000422} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000423
Chris Lattner977f0042001-11-01 05:55:13 +0000424
425//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426// DirectRules Class
427//===----------------------------------------------------------------------===//
428//
429// DirectRules provides a concrete base classes of ConstRules for a variety of
430// different types. This allows the C++ compiler to automatically generate our
431// constant handling operations in a typesafe and accurate manner.
432//
Chris Lattner6a871e12006-06-21 18:13:36 +0000433namespace {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000434template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
435struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000436 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
437 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
438 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439 }
440
Chris Lattnere87f65e2002-07-30 16:24:28 +0000441 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
442 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
443 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 }
445
Chris Lattnere87f65e2002-07-30 16:24:28 +0000446 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
447 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
448 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000449 }
450
Chris Lattnere87f65e2002-07-30 16:24:28 +0000451 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000452 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000453 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
454 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000455 }
456
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000457 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000458 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
459 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000460 }
Chris Lattner55406842001-07-21 19:10:49 +0000461
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000462 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000463 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
464 return ConstantBool::get(R);
465 }
466
Chris Lattner1f0049c2003-04-17 19:24:18 +0000467 static Constant *CastToPointer(const ConstantClass *V,
468 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000469 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000470 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000471 return 0; // Can't const prop other types of pointers
472 }
473
Chris Lattner55406842001-07-21 19:10:49 +0000474 // Casting operators. ick
475#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000476 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000477 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000478 }
479
Chris Lattner3462ae32001-12-03 22:26:30 +0000480 DEF_CAST(Bool , ConstantBool, bool)
481 DEF_CAST(SByte , ConstantSInt, signed char)
482 DEF_CAST(UByte , ConstantUInt, unsigned char)
483 DEF_CAST(Short , ConstantSInt, signed short)
484 DEF_CAST(UShort, ConstantUInt, unsigned short)
485 DEF_CAST(Int , ConstantSInt, signed int)
486 DEF_CAST(UInt , ConstantUInt, unsigned int)
487 DEF_CAST(Long , ConstantSInt, int64_t)
488 DEF_CAST(ULong , ConstantUInt, uint64_t)
489 DEF_CAST(Float , ConstantFP , float)
490 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000491#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000492};
Chris Lattner6a871e12006-06-21 18:13:36 +0000493} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000494
Chris Lattner62af86e2002-05-03 20:09:52 +0000495
496//===----------------------------------------------------------------------===//
497// DirectIntRules Class
498//===----------------------------------------------------------------------===//
499//
500// DirectIntRules provides implementations of functions that are valid on
501// integer types, but not all types in general.
502//
Chris Lattner6a871e12006-06-21 18:13:36 +0000503namespace {
Chris Lattner62af86e2002-05-03 20:09:52 +0000504template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000505struct DirectIntRules
506 : public DirectRules<ConstantClass, BuiltinType, Ty,
507 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000508
Chris Lattner268916262003-05-12 15:26:25 +0000509 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
510 if (V2->isNullValue()) return 0;
511 if (V2->isAllOnesValue() && // MIN_INT / -1
512 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
513 return 0;
514 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
515 return ConstantClass::get(*Ty, R);
516 }
517
Chris Lattnere87f65e2002-07-30 16:24:28 +0000518 static Constant *Rem(const ConstantClass *V1,
519 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000520 if (V2->isNullValue()) return 0; // X / 0
521 if (V2->isAllOnesValue() && // MIN_INT / -1
522 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
523 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000524 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
525 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000526 }
Chris Lattner6670d862002-05-06 03:00:54 +0000527
Chris Lattnere87f65e2002-07-30 16:24:28 +0000528 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
529 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
530 return ConstantClass::get(*Ty, R);
531 }
532 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
533 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
534 return ConstantClass::get(*Ty, R);
535 }
536 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
537 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
538 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000539 }
540
Chris Lattnere87f65e2002-07-30 16:24:28 +0000541 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
542 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
543 return ConstantClass::get(*Ty, R);
544 }
545
546 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
547 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
548 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000549 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000550};
Chris Lattner6a871e12006-06-21 18:13:36 +0000551} // end anonymous namespace
Chris Lattner0a144ad2002-05-03 21:41:07 +0000552
553
554//===----------------------------------------------------------------------===//
555// DirectFPRules Class
556//===----------------------------------------------------------------------===//
557//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000558/// DirectFPRules provides implementations of functions that are valid on
559/// floating point types, but not all types in general.
560///
Chris Lattner6a871e12006-06-21 18:13:36 +0000561namespace {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000562template <class ConstantClass, class BuiltinType, Type **Ty>
563struct DirectFPRules
564 : public DirectRules<ConstantClass, BuiltinType, Ty,
565 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000566 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000567 if (V2->isNullValue()) return 0;
568 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
569 (BuiltinType)V2->getValue());
570 return ConstantClass::get(*Ty, Result);
571 }
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000572 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Jeff Cohen4e3aede2005-05-03 03:13:01 +0000573 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
574 if (V2->isExactlyValue(0.0)) return ConstantClass::get(*Ty, inf);
575 if (V2->isExactlyValue(-0.0)) return ConstantClass::get(*Ty, -inf);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000576 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
577 return ConstantClass::get(*Ty, R);
578 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000579};
Chris Lattner6a871e12006-06-21 18:13:36 +0000580} // end anonymous namespace
Chris Lattner62af86e2002-05-03 20:09:52 +0000581
Chris Lattner1dd054c2004-01-12 22:07:24 +0000582
583/// ConstRules::get - This method returns the constant rules implementation that
584/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000585ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000586 static EmptyRules EmptyR;
587 static BoolRules BoolR;
588 static NullPointerRules NullPointerR;
Chris Lattner1171d952006-01-04 02:03:29 +0000589 static ConstantPackedRules ConstantPackedR;
590 static GeneralPackedRules GeneralPackedR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000591 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
592 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
593 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
594 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
595 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
596 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
597 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
598 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
599 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
600 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601
Chris Lattner4b6addf2003-11-17 19:19:32 +0000602 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000603 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
604 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000605 return EmptyR;
606
Chris Lattner6b727592004-06-17 18:19:28 +0000607 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000608 default: assert(0 && "Unknown value type for constant folding!");
609 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000610 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000611 case Type::SByteTyID: return SByteR;
612 case Type::UByteTyID: return UByteR;
613 case Type::ShortTyID: return ShortR;
614 case Type::UShortTyID: return UShortR;
615 case Type::IntTyID: return IntR;
616 case Type::UIntTyID: return UIntR;
617 case Type::LongTyID: return LongR;
618 case Type::ULongTyID: return ULongR;
619 case Type::FloatTyID: return FloatR;
620 case Type::DoubleTyID: return DoubleR;
Chris Lattner1171d952006-01-04 02:03:29 +0000621 case Type::PackedTyID:
622 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
623 return ConstantPackedR;
624 return GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000626}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000627
628
629//===----------------------------------------------------------------------===//
630// ConstantFold*Instruction Implementations
631//===----------------------------------------------------------------------===//
632//
633// These methods contain the special case hackery required to symbolically
634// evaluate some constant expression cases, and use the ConstantRules class to
635// evaluate normal constants.
636//
637static unsigned getSize(const Type *Ty) {
638 unsigned S = Ty->getPrimitiveSize();
639 return S ? S : 8; // Treat pointers at 8 bytes
640}
641
Chris Lattner6b3f4752006-04-02 01:38:28 +0000642/// CastConstantPacked - Convert the specified ConstantPacked node to the
643/// specified packed type. At this point, we know that the elements of the
644/// input packed constant are all simple integer or FP values.
645static Constant *CastConstantPacked(ConstantPacked *CP,
646 const PackedType *DstTy) {
647 unsigned SrcNumElts = CP->getType()->getNumElements();
648 unsigned DstNumElts = DstTy->getNumElements();
649 const Type *SrcEltTy = CP->getType()->getElementType();
650 const Type *DstEltTy = DstTy->getElementType();
651
652 // If both vectors have the same number of elements (thus, the elements
653 // are the same size), perform the conversion now.
654 if (SrcNumElts == DstNumElts) {
655 std::vector<Constant*> Result;
656
657 // If the src and dest elements are both integers, just cast each one
658 // which will do the appropriate bit-convert.
659 if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) {
660 for (unsigned i = 0; i != SrcNumElts; ++i)
661 Result.push_back(ConstantExpr::getCast(CP->getOperand(i),
662 DstEltTy));
663 return ConstantPacked::get(Result);
664 }
665
666 if (SrcEltTy->isIntegral()) {
667 // Otherwise, this is an int-to-fp cast.
668 assert(DstEltTy->isFloatingPoint());
669 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
670 for (unsigned i = 0; i != SrcNumElts; ++i) {
671 double V =
672 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getRawValue());
673 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
674 }
675 return ConstantPacked::get(Result);
676 }
677 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
678 for (unsigned i = 0; i != SrcNumElts; ++i) {
679 float V =
680 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getRawValue());
681 Result.push_back(ConstantFP::get(Type::FloatTy, V));
682 }
683 return ConstantPacked::get(Result);
684 }
685
686 // Otherwise, this is an fp-to-int cast.
687 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
688
689 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
690 for (unsigned i = 0; i != SrcNumElts; ++i) {
691 uint64_t V =
692 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
693 Constant *C = ConstantUInt::get(Type::ULongTy, V);
694 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
695 }
696 return ConstantPacked::get(Result);
697 }
698
699 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
700 for (unsigned i = 0; i != SrcNumElts; ++i) {
701 unsigned V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
702 Constant *C = ConstantUInt::get(Type::UIntTy, V);
703 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
704 }
705 return ConstantPacked::get(Result);
706 }
707
708 // Otherwise, this is a cast that changes element count and size. Handle
709 // casts which shrink the elements here.
710
711 // FIXME: We need to know endianness to do this!
712
713 return 0;
714}
715
716
Chris Lattner1dd054c2004-01-12 22:07:24 +0000717Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
718 const Type *DestTy) {
719 if (V->getType() == DestTy) return (Constant*)V;
720
Chris Lattnerea0789c2004-03-08 06:17:35 +0000721 // Cast of a global address to boolean is always true.
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000722 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerea0789c2004-03-08 06:17:35 +0000723 if (DestTy == Type::BoolTy)
724 // FIXME: When we support 'external weak' references, we have to prevent
Chris Lattnercd4003e2005-01-06 16:26:38 +0000725 // this transformation from happening. This code will need to be updated
726 // to ignore external weak symbols when we support it.
727 return ConstantBool::True;
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000728 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000729 if (CE->getOpcode() == Instruction::Cast) {
730 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
731 // Try to not produce a cast of a cast, which is almost always redundant.
732 if (!Op->getType()->isFloatingPoint() &&
733 !CE->getType()->isFloatingPoint() &&
Reid Spencer8eb06df2004-05-30 01:19:48 +0000734 !DestTy->isFloatingPoint()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000735 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
736 unsigned S3 = getSize(DestTy);
737 if (Op->getType() == DestTy && S3 >= S2)
738 return Op;
739 if (S1 >= S2 && S2 >= S3)
740 return ConstantExpr::getCast(Op, DestTy);
741 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
742 return ConstantExpr::getCast(Op, DestTy);
743 }
744 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
745 // If all of the indexes in the GEP are null values, there is no pointer
746 // adjustment going on. We might as well cast the source pointer.
747 bool isAllNull = true;
748 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
749 if (!CE->getOperand(i)->isNullValue()) {
750 isAllNull = false;
751 break;
752 }
753 if (isAllNull)
754 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
755 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000756 } else if (isa<UndefValue>(V)) {
757 return UndefValue::get(DestTy);
758 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000759
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000760 // Check to see if we are casting an pointer to an aggregate to a pointer to
761 // the first element. If so, return the appropriate GEP instruction.
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000762 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000763 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
764 std::vector<Value*> IdxList;
765 IdxList.push_back(Constant::getNullValue(Type::IntTy));
766 const Type *ElTy = PTy->getElementType();
767 while (ElTy != DPTy->getElementType()) {
768 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
Chris Lattner9e907202004-11-22 19:15:27 +0000769 if (STy->getNumElements() == 0) break;
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000770 ElTy = STy->getElementType(0);
771 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
772 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
773 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
774 ElTy = STy->getElementType();
775 IdxList.push_back(IdxList[0]);
776 } else {
777 break;
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000778 }
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000779 }
780
781 if (ElTy == DPTy->getElementType())
782 return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
783 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000784
785 // Handle casts from one packed constant to another. We know that the src and
786 // dest type have the same size.
787 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
788 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
789 assert(DestPTy->getElementType()->getPrimitiveSizeInBits() *
790 DestPTy->getNumElements() ==
791 SrcTy->getElementType()->getPrimitiveSizeInBits() *
792 SrcTy->getNumElements() && "Not cast between same sized vectors!");
793 if (isa<ConstantAggregateZero>(V))
794 return Constant::getNullValue(DestTy);
795 if (isa<UndefValue>(V))
796 return UndefValue::get(DestTy);
797 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
798 // This is a cast from a ConstantPacked of one type to a ConstantPacked
799 // of another type. Check to see if all elements of the input are
800 // simple.
801 bool AllSimpleConstants = true;
802 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
803 if (!isa<ConstantInt>(CP->getOperand(i)) &&
804 !isa<ConstantFP>(CP->getOperand(i))) {
805 AllSimpleConstants = false;
806 break;
807 }
808 }
809
810 // If all of the elements are simple constants, we can fold this.
811 if (AllSimpleConstants)
812 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
813 }
814 }
815 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000816
Chris Lattner1dd054c2004-01-12 22:07:24 +0000817 ConstRules &Rules = ConstRules::get(V, V);
818
Chris Lattner6b727592004-06-17 18:19:28 +0000819 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000820 case Type::BoolTyID: return Rules.castToBool(V);
821 case Type::UByteTyID: return Rules.castToUByte(V);
822 case Type::SByteTyID: return Rules.castToSByte(V);
823 case Type::UShortTyID: return Rules.castToUShort(V);
824 case Type::ShortTyID: return Rules.castToShort(V);
825 case Type::UIntTyID: return Rules.castToUInt(V);
826 case Type::IntTyID: return Rules.castToInt(V);
827 case Type::ULongTyID: return Rules.castToULong(V);
828 case Type::LongTyID: return Rules.castToLong(V);
829 case Type::FloatTyID: return Rules.castToFloat(V);
830 case Type::DoubleTyID: return Rules.castToDouble(V);
831 case Type::PointerTyID:
832 return Rules.castToPointer(V, cast<PointerType>(DestTy));
833 default: return 0;
834 }
835}
836
Chris Lattner6ea4b522004-03-12 05:53:32 +0000837Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
838 const Constant *V1,
839 const Constant *V2) {
840 if (Cond == ConstantBool::True)
841 return const_cast<Constant*>(V1);
842 else if (Cond == ConstantBool::False)
843 return const_cast<Constant*>(V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000844
845 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
846 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
847 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000848 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000849 return 0;
850}
851
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000852Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
853 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000854 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
855 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000856 if (Val->isNullValue()) // ee(zero, x) -> zero
857 return Constant::getNullValue(
858 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000859
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000860 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
861 if (const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx)) {
862 return const_cast<Constant*>(CVal->getOperand(CIdx->getValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000863 } else if (isa<UndefValue>(Idx)) {
864 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
865 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000866 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000867 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000868 return 0;
869}
870
Robert Bocchinoca27f032006-01-17 20:07:22 +0000871Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
872 const Constant *Elt,
873 const Constant *Idx) {
874 const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx);
875 if (!CIdx) return 0;
876 unsigned idxVal = CIdx->getValue();
877 if (const UndefValue *UVal = dyn_cast<UndefValue>(Val)) {
878 // Insertion of scalar constant into packed undef
879 // Optimize away insertion of undef
880 if (isa<UndefValue>(Elt))
881 return const_cast<Constant*>(Val);
882 // Otherwise break the aggregate undef into multiple undefs and do
883 // the insertion
884 unsigned numOps =
885 cast<PackedType>(Val->getType())->getNumElements();
886 std::vector<Constant*> Ops;
887 Ops.reserve(numOps);
888 for (unsigned i = 0; i < numOps; ++i) {
889 const Constant *Op =
890 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
891 Ops.push_back(const_cast<Constant*>(Op));
892 }
893 return ConstantPacked::get(Ops);
894 }
895 if (const ConstantAggregateZero *CVal =
896 dyn_cast<ConstantAggregateZero>(Val)) {
897 // Insertion of scalar constant into packed aggregate zero
898 // Optimize away insertion of zero
899 if (Elt->isNullValue())
900 return const_cast<Constant*>(Val);
901 // Otherwise break the aggregate zero into multiple zeros and do
902 // the insertion
903 unsigned numOps =
904 cast<PackedType>(Val->getType())->getNumElements();
905 std::vector<Constant*> Ops;
906 Ops.reserve(numOps);
907 for (unsigned i = 0; i < numOps; ++i) {
908 const Constant *Op =
909 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
910 Ops.push_back(const_cast<Constant*>(Op));
911 }
912 return ConstantPacked::get(Ops);
913 }
914 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
915 // Insertion of scalar constant into packed constant
916 std::vector<Constant*> Ops;
917 Ops.reserve(CVal->getNumOperands());
918 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
919 const Constant *Op =
920 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
921 Ops.push_back(const_cast<Constant*>(Op));
922 }
923 return ConstantPacked::get(Ops);
924 }
925 return 0;
926}
927
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000928Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
929 const Constant *V2,
930 const Constant *Mask) {
931 // TODO:
932 return 0;
933}
934
935
Chris Lattner60c47262005-01-28 19:09:51 +0000936/// isZeroSizedType - This type is zero sized if its an array or structure of
937/// zero sized types. The only leaf zero sized type is an empty structure.
938static bool isMaybeZeroSizedType(const Type *Ty) {
939 if (isa<OpaqueType>(Ty)) return true; // Can't say.
940 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
941
942 // If all of elements have zero size, this does too.
943 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000944 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000945 return true;
946
947 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
948 return isMaybeZeroSizedType(ATy->getElementType());
949 }
950 return false;
951}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000952
Chris Lattner061da2f2004-01-13 05:51:55 +0000953/// IdxCompare - Compare the two constants as though they were getelementptr
954/// indices. This allows coersion of the types to be the same thing.
955///
956/// If the two constants are the "same" (after coersion), return 0. If the
957/// first is less than the second, return -1, if the second is less than the
958/// first, return 1. If the constants are not integral, return -2.
959///
Chris Lattner60c47262005-01-28 19:09:51 +0000960static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000961 if (C1 == C2) return 0;
962
963 // Ok, we found a different index. Are either of the operands
964 // ConstantExprs? If so, we can't do anything with them.
965 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
966 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000967
Chris Lattner69193f92004-04-05 01:30:19 +0000968 // Ok, we have two differing integer indices. Sign extend them to be the same
969 // type. Long is always big enough, so we use it.
970 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
971 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000972 if (C1 == C2) return 0; // Are they just differing types?
973
Chris Lattner60c47262005-01-28 19:09:51 +0000974 // If the type being indexed over is really just a zero sized type, there is
975 // no pointer difference being made here.
976 if (isMaybeZeroSizedType(ElTy))
977 return -2; // dunno.
978
Chris Lattner061da2f2004-01-13 05:51:55 +0000979 // If they are really different, now that they are the same type, then we
980 // found a difference!
981 if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
982 return -1;
983 else
984 return 1;
985}
986
987/// evaluateRelation - This function determines if there is anything we can
988/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000989/// things like integer comparisons, but should instead handle ConstantExprs
990/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +0000991/// particular relation to each other, we should return the corresponding SetCC
992/// code, otherwise return Instruction::BinaryOpsEnd.
993///
994/// To simplify this code we canonicalize the relation so that the first
995/// operand is always the most "complex" of the two. We consider simple
996/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000997/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000998///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000999static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001000 assert(V1->getType() == V2->getType() &&
1001 "Cannot compare different types of values!");
1002 if (V1 == V2) return Instruction::SetEQ;
1003
Reid Spenceraccd7c72004-07-17 23:47:01 +00001004 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001005 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1006 // We distilled this down to a simple case, use the standard constant
1007 // folder.
1008 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
1009 if (R == ConstantBool::True) return Instruction::SetEQ;
1010 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
1011 if (R == ConstantBool::True) return Instruction::SetLT;
1012 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
1013 if (R == ConstantBool::True) return Instruction::SetGT;
1014
1015 // If we couldn't figure it out, bail.
1016 return Instruction::BinaryOpsEnd;
1017 }
1018
Chris Lattner061da2f2004-01-13 05:51:55 +00001019 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +00001020 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1021 if (SwappedRelation != Instruction::BinaryOpsEnd)
1022 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001023
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001024 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001025 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001026 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1027 if (SwappedRelation != Instruction::BinaryOpsEnd)
1028 return SetCondInst::getSwappedCondition(SwappedRelation);
1029 else
1030 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +00001031 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001032
Reid Spenceraccd7c72004-07-17 23:47:01 +00001033 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001034 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001035 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
1036 assert(CPR1 != CPR2 &&
1037 "GVs for the same value exist at different addresses??");
Chris Lattner061da2f2004-01-13 05:51:55 +00001038 // FIXME: If both globals are external weak, they might both be null!
1039 return Instruction::SetNE;
1040 } else {
1041 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1042 // Global can never be null. FIXME: if we implement external weak
1043 // linkage, this is not necessarily true!
1044 return Instruction::SetNE;
1045 }
1046
1047 } else {
1048 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1049 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001050 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +00001051 Constant *CE1Op0 = CE1->getOperand(0);
1052
1053 switch (CE1->getOpcode()) {
1054 case Instruction::Cast:
1055 // If the cast is not actually changing bits, and the second operand is a
1056 // null pointer, do the comparison with the pre-casted value.
1057 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001058 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +00001059 return evaluateRelation(CE1Op0,
1060 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001061
1062 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1063 // from the same type as the src of the LHS, evaluate the inputs. This is
1064 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1065 // which happens a lot in compilers with tagged integers.
1066 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
1067 if (isa<PointerType>(CE1->getType()) &&
1068 CE2->getOpcode() == Instruction::Cast &&
1069 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1070 CE1->getOperand(0)->getType()->isIntegral()) {
1071 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1072 }
Chris Lattner192e3262004-04-11 01:29:30 +00001073 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001074
1075 case Instruction::GetElementPtr:
1076 // Ok, since this is a getelementptr, we know that the constant has a
1077 // pointer type. Check the various cases.
1078 if (isa<ConstantPointerNull>(V2)) {
1079 // If we are comparing a GEP to a null pointer, check to see if the base
1080 // of the GEP equals the null pointer.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001081 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001082 // FIXME: this is not true when we have external weak references!
1083 // No offset can go from a global to a null pointer.
1084 return Instruction::SetGT;
1085 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1086 // If we are indexing from a null pointer, check to see if we have any
1087 // non-zero indices.
1088 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1089 if (!CE1->getOperand(i)->isNullValue())
1090 // Offsetting from null, must not be equal.
1091 return Instruction::SetGT;
1092 // Only zero indexes from null, must still be zero.
1093 return Instruction::SetEQ;
1094 }
1095 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001096 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001097 if (isa<ConstantPointerNull>(CE1Op0)) {
1098 // FIXME: This is not true with external weak references.
1099 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001100 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001101 if (CPR1 == CPR2) {
1102 // If this is a getelementptr of the same global, then it must be
1103 // different. Because the types must match, the getelementptr could
1104 // only have at most one index, and because we fold getelementptr's
1105 // with a single zero index, it must be nonzero.
1106 assert(CE1->getNumOperands() == 2 &&
1107 !CE1->getOperand(1)->isNullValue() &&
1108 "Suprising getelementptr!");
1109 return Instruction::SetGT;
1110 } else {
1111 // If they are different globals, we don't know what the value is,
1112 // but they can't be equal.
1113 return Instruction::SetNE;
1114 }
1115 }
1116 } else {
1117 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1118 const Constant *CE2Op0 = CE2->getOperand(0);
1119
1120 // There are MANY other foldings that we could perform here. They will
1121 // probably be added on demand, as they seem needed.
1122 switch (CE2->getOpcode()) {
1123 default: break;
1124 case Instruction::GetElementPtr:
1125 // By far the most common case to handle is when the base pointers are
1126 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001127 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001128 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1129 return Instruction::SetNE;
1130 // Ok, we know that both getelementptr instructions are based on the
1131 // same global. From this, we can precisely determine the relative
1132 // ordering of the resultant pointers.
1133 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001134
Chris Lattner061da2f2004-01-13 05:51:55 +00001135 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001136 gep_type_iterator GTI = gep_type_begin(CE1);
1137 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1138 ++i, ++GTI)
1139 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1140 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001141 case -1: return Instruction::SetLT;
1142 case 1: return Instruction::SetGT;
1143 case -2: return Instruction::BinaryOpsEnd;
1144 }
1145
1146 // Ok, we ran out of things they have in common. If any leftovers
1147 // are non-zero then we have a difference, otherwise we are equal.
1148 for (; i < CE1->getNumOperands(); ++i)
1149 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001150 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1151 return Instruction::SetGT;
1152 else
1153 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001154
Chris Lattner061da2f2004-01-13 05:51:55 +00001155 for (; i < CE2->getNumOperands(); ++i)
1156 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001157 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1158 return Instruction::SetLT;
1159 else
1160 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001161 return Instruction::SetEQ;
1162 }
1163 }
1164 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001165
Chris Lattner061da2f2004-01-13 05:51:55 +00001166 default:
1167 break;
1168 }
1169 }
1170
1171 return Instruction::BinaryOpsEnd;
1172}
1173
Chris Lattner1dd054c2004-01-12 22:07:24 +00001174Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1175 const Constant *V1,
1176 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001177 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001178 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001179 default: break;
1180 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1181 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1182 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
1183 case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break;
1184 case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break;
1185 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1186 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1187 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1188 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
1189 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
1190 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
1191 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1192 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001193 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
1194 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001195 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001196 break;
1197 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1198 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001199 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001200 break;
1201 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1202 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001203 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001204 break;
1205 }
1206
Chris Lattner061da2f2004-01-13 05:51:55 +00001207 // If we successfully folded the expression, return it now.
1208 if (C) return C;
1209
Chris Lattner192eacc2004-10-17 04:01:51 +00001210 if (SetCondInst::isRelational(Opcode)) {
1211 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1212 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001213 switch (evaluateRelation(const_cast<Constant*>(V1),
1214 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001215 default: assert(0 && "Unknown relational!");
1216 case Instruction::BinaryOpsEnd:
1217 break; // Couldn't determine anything about these constants.
1218 case Instruction::SetEQ: // We know the constants are equal!
1219 // If we know the constants are equal, we can decide the result of this
1220 // computation precisely.
1221 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1222 Opcode == Instruction::SetLE ||
1223 Opcode == Instruction::SetGE);
1224 case Instruction::SetLT:
1225 // If we know that V1 < V2, we can decide the result of this computation
1226 // precisely.
1227 return ConstantBool::get(Opcode == Instruction::SetLT ||
1228 Opcode == Instruction::SetNE ||
1229 Opcode == Instruction::SetLE);
1230 case Instruction::SetGT:
1231 // If we know that V1 > V2, we can decide the result of this computation
1232 // precisely.
1233 return ConstantBool::get(Opcode == Instruction::SetGT ||
1234 Opcode == Instruction::SetNE ||
1235 Opcode == Instruction::SetGE);
1236 case Instruction::SetLE:
1237 // If we know that V1 <= V2, we can only partially decide this relation.
1238 if (Opcode == Instruction::SetGT) return ConstantBool::False;
1239 if (Opcode == Instruction::SetLT) return ConstantBool::True;
1240 break;
1241
1242 case Instruction::SetGE:
1243 // If we know that V1 >= V2, we can only partially decide this relation.
1244 if (Opcode == Instruction::SetLT) return ConstantBool::False;
1245 if (Opcode == Instruction::SetGT) return ConstantBool::True;
1246 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001247
Chris Lattner061da2f2004-01-13 05:51:55 +00001248 case Instruction::SetNE:
1249 // If we know that V1 != V2, we can only partially decide this relation.
1250 if (Opcode == Instruction::SetEQ) return ConstantBool::False;
1251 if (Opcode == Instruction::SetNE) return ConstantBool::True;
1252 break;
1253 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001254 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001255
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001256 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1257 switch (Opcode) {
1258 case Instruction::Add:
1259 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001260 case Instruction::Xor:
1261 return UndefValue::get(V1->getType());
1262
1263 case Instruction::Mul:
1264 case Instruction::And:
1265 return Constant::getNullValue(V1->getType());
1266 case Instruction::Div:
1267 case Instruction::Rem:
1268 if (!isa<UndefValue>(V2)) // undef/X -> 0
1269 return Constant::getNullValue(V1->getType());
1270 return const_cast<Constant*>(V2); // X/undef -> undef
1271 case Instruction::Or: // X|undef -> -1
1272 return ConstantInt::getAllOnesValue(V1->getType());
1273 case Instruction::Shr:
1274 if (!isa<UndefValue>(V2)) {
1275 if (V1->getType()->isSigned())
1276 return const_cast<Constant*>(V1); // undef >>s X -> undef
1277 // undef >>u X -> 0
1278 } else if (isa<UndefValue>(V1)) {
1279 return const_cast<Constant*>(V1); // undef >> undef -> undef
1280 } else {
1281 if (V1->getType()->isSigned())
1282 return const_cast<Constant*>(V1); // X >>s undef -> X
1283 // X >>u undef -> 0
1284 }
1285 return Constant::getNullValue(V1->getType());
1286
1287 case Instruction::Shl:
1288 // undef << X -> 0 X << undef -> 0
1289 return Constant::getNullValue(V1->getType());
1290 }
1291 }
1292
Chris Lattner061da2f2004-01-13 05:51:55 +00001293 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
1294 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1295 // There are many possible foldings we could do here. We should probably
1296 // at least fold add of a pointer with an integer into the appropriate
1297 // getelementptr. This will improve alias analysis a bit.
1298
1299
1300
1301
1302 } else {
1303 // Just implement a couple of simple identities.
1304 switch (Opcode) {
1305 case Instruction::Add:
1306 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1307 break;
1308 case Instruction::Sub:
1309 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1310 break;
1311 case Instruction::Mul:
1312 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1313 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1314 if (CI->getRawValue() == 1)
1315 return const_cast<Constant*>(V1); // X * 1 == X
1316 break;
1317 case Instruction::Div:
1318 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1319 if (CI->getRawValue() == 1)
1320 return const_cast<Constant*>(V1); // X / 1 == X
1321 break;
1322 case Instruction::Rem:
1323 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1324 if (CI->getRawValue() == 1)
1325 return Constant::getNullValue(CI->getType()); // X % 1 == 0
1326 break;
1327 case Instruction::And:
1328 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1329 return const_cast<Constant*>(V1); // X & -1 == X
1330 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Chris Lattnerea0789c2004-03-08 06:17:35 +00001331 if (CE1->getOpcode() == Instruction::Cast &&
Reid Spenceraccd7c72004-07-17 23:47:01 +00001332 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001333 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001334
1335 // Functions are at least 4-byte aligned. If and'ing the address of a
1336 // function with a constant < 4, fold it to zero.
1337 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spenceraccd7c72004-07-17 23:47:01 +00001338 if (CI->getRawValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001339 return Constant::getNullValue(CI->getType());
1340 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001341 break;
1342 case Instruction::Or:
1343 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1344 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1345 return const_cast<Constant*>(V2); // X | -1 == -1
1346 break;
1347 case Instruction::Xor:
1348 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1349 break;
1350 }
1351 }
1352
1353 } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1354 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1355 // other way if possible.
1356 switch (Opcode) {
1357 case Instruction::Add:
1358 case Instruction::Mul:
1359 case Instruction::And:
1360 case Instruction::Or:
1361 case Instruction::Xor:
1362 case Instruction::SetEQ:
1363 case Instruction::SetNE:
1364 // No change of opcode required.
1365 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1366
1367 case Instruction::SetLT:
1368 case Instruction::SetGT:
1369 case Instruction::SetLE:
1370 case Instruction::SetGE:
1371 // Change the opcode as necessary to swap the operands.
1372 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1373 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1374
1375 case Instruction::Shl:
1376 case Instruction::Shr:
1377 case Instruction::Sub:
1378 case Instruction::Div:
1379 case Instruction::Rem:
1380 default: // These instructions cannot be flopped around.
1381 break;
1382 }
1383 }
1384 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001385}
1386
1387Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001388 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001389 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001390 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001391 return const_cast<Constant*>(C);
1392
Chris Lattnerf6013752004-10-17 21:54:55 +00001393 if (isa<UndefValue>(C)) {
1394 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1395 true);
1396 assert(Ty != 0 && "Invalid indices for GEP!");
1397 return UndefValue::get(PointerType::get(Ty));
1398 }
1399
1400 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001401 if (C->isNullValue()) {
1402 bool isNull = true;
1403 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001404 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001405 isNull = false;
1406 break;
1407 }
1408 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001409 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001410 true);
1411 assert(Ty != 0 && "Invalid indices for GEP!");
1412 return ConstantPointerNull::get(PointerType::get(Ty));
1413 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001414
1415 if (IdxList.size() == 1) {
1416 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
1417 if (unsigned ElSize = ElTy->getPrimitiveSize()) {
1418 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1419 // type, we can statically fold this.
1420 Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
Chris Lattner13128ab2004-10-11 22:52:25 +00001421 R = ConstantExpr::getCast(R, Idx0->getType());
1422 R = ConstantExpr::getMul(R, Idx0);
Chris Lattner4bbd4092004-07-15 01:16:59 +00001423 return ConstantExpr::getCast(R, C->getType());
1424 }
1425 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001426 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001427
1428 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1429 // Combine Indices - If the source pointer to this getelementptr instruction
1430 // is a getelementptr instruction, combine the indices of the two
1431 // getelementptr instructions into a single instruction.
1432 //
1433 if (CE->getOpcode() == Instruction::GetElementPtr) {
1434 const Type *LastTy = 0;
1435 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1436 I != E; ++I)
1437 LastTy = *I;
1438
Chris Lattner13128ab2004-10-11 22:52:25 +00001439 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1440 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001441 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1442 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001443 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001444
1445 // Add the last index of the source with the first index of the new GEP.
1446 // Make sure to handle the case when they are actually different types.
1447 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001448 // Otherwise it must be an array.
1449 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001450 const Type *IdxTy = Combined->getType();
Chris Lattner13128ab2004-10-11 22:52:25 +00001451 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001452 Combined =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001453 ConstantExpr::get(Instruction::Add,
Chris Lattner13128ab2004-10-11 22:52:25 +00001454 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattner71068a02004-07-07 04:45:13 +00001455 ConstantExpr::getCast(Combined, IdxTy));
1456 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001457
Chris Lattner1dd054c2004-01-12 22:07:24 +00001458 NewIndices.push_back(Combined);
1459 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1460 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1461 }
1462 }
1463
1464 // Implement folding of:
1465 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1466 // long 0, long 0)
1467 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1468 //
1469 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
Chris Lattner13128ab2004-10-11 22:52:25 +00001470 Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001471 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001472 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1473 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1474 if (const ArrayType *CAT =
1475 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
1476 if (CAT->getElementType() == SAT->getElementType())
1477 return ConstantExpr::getGetElementPtr(
1478 (Constant*)CE->getOperand(0), IdxList);
1479 }
1480 return 0;
1481}
1482