blob: a6fbf42fa24a4aa59784f0c9a9fe1a11767a2075 [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"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000027#include <limits>
Chris Lattner0a144ad2002-05-03 21:41:07 +000028#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000029using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000030
Chris Lattner5a945e32004-01-12 21:13:12 +000031namespace {
32 struct ConstRules {
33 ConstRules() {}
Reid Spencer9c47b252005-04-24 22:27:20 +000034 virtual ~ConstRules() {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000035
Chris Lattner5a945e32004-01-12 21:13:12 +000036 // Binary Operators...
37 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
38 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
39 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
40 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
42 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
43 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
45 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
46 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
47 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0;
48 virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0;
49
50 // Casting operators.
51 virtual Constant *castToBool (const Constant *V) const = 0;
52 virtual Constant *castToSByte (const Constant *V) const = 0;
53 virtual Constant *castToUByte (const Constant *V) const = 0;
54 virtual Constant *castToShort (const Constant *V) const = 0;
55 virtual Constant *castToUShort(const Constant *V) const = 0;
56 virtual Constant *castToInt (const Constant *V) const = 0;
57 virtual Constant *castToUInt (const Constant *V) const = 0;
58 virtual Constant *castToLong (const Constant *V) const = 0;
59 virtual Constant *castToULong (const Constant *V) const = 0;
60 virtual Constant *castToFloat (const Constant *V) const = 0;
61 virtual Constant *castToDouble(const Constant *V) const = 0;
62 virtual Constant *castToPointer(const Constant *V,
63 const PointerType *Ty) const = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +000064
Chris Lattner5a945e32004-01-12 21:13:12 +000065 // ConstRules::get - Return an instance of ConstRules for the specified
66 // constant operands.
67 //
68 static ConstRules &get(const Constant *V1, const Constant *V2);
69 private:
70 ConstRules(const ConstRules &); // Do not implement
71 ConstRules &operator=(const ConstRules &); // Do not implement
72 };
73}
74
75
Chris Lattner2f7c9632001-06-06 20:29:01 +000076//===----------------------------------------------------------------------===//
77// TemplateRules Class
78//===----------------------------------------------------------------------===//
79//
Misha Brukmanb1c93172005-04-21 23:48:37 +000080// TemplateRules - Implement a subclass of ConstRules that provides all
81// operations as noops. All other rules classes inherit from this class so
82// that if functionality is needed in the future, it can simply be added here
Chris Lattner2f7c9632001-06-06 20:29:01 +000083// and to ConstRules without changing anything else...
Misha Brukmanb1c93172005-04-21 23:48:37 +000084//
Chris Lattner2f7c9632001-06-06 20:29:01 +000085// This class also provides subclasses with typesafe implementations of methods
86// so that don't have to do type casting.
87//
88template<class ArgType, class SubClassName>
89class TemplateRules : public ConstRules {
90
Reid Spencer9c47b252005-04-24 22:27:20 +000091
Chris Lattner2f7c9632001-06-06 20:29:01 +000092 //===--------------------------------------------------------------------===//
93 // Redirecting functions that cast to the appropriate types
94 //===--------------------------------------------------------------------===//
95
Misha Brukmanb1c93172005-04-21 23:48:37 +000096 virtual Constant *add(const Constant *V1, const Constant *V2) const {
97 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +000098 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000099 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
100 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000102 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
103 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000104 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105 virtual Constant *div(const Constant *V1, const Constant *V2) const {
106 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
Chris Lattneraf259a72002-04-07 08:10:14 +0000107 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000108 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
109 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000110 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000111 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
112 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000113 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000114 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
115 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000116 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000117 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
118 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000119 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000120 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
121 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000122 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
124 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000125 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000126
Misha Brukmanb1c93172005-04-21 23:48:37 +0000127 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
129 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000131 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
132 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133
Chris Lattner55406842001-07-21 19:10:49 +0000134 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000135 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000136 return SubClassName::CastToBool((const ArgType*)V);
137 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000138 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000139 return SubClassName::CastToSByte((const ArgType*)V);
140 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000141 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000142 return SubClassName::CastToUByte((const ArgType*)V);
143 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000144 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000145 return SubClassName::CastToShort((const ArgType*)V);
146 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000147 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000148 return SubClassName::CastToUShort((const ArgType*)V);
149 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000150 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000151 return SubClassName::CastToInt((const ArgType*)V);
152 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000153 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000154 return SubClassName::CastToUInt((const ArgType*)V);
155 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000156 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000157 return SubClassName::CastToLong((const ArgType*)V);
158 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000159 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000160 return SubClassName::CastToULong((const ArgType*)V);
161 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000162 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000163 return SubClassName::CastToFloat((const ArgType*)V);
164 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000165 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000166 return SubClassName::CastToDouble((const ArgType*)V);
167 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000168 virtual Constant *castToPointer(const Constant *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000169 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000170 return SubClassName::CastToPointer((const ArgType*)V, Ty);
171 }
Chris Lattner55406842001-07-21 19:10:49 +0000172
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173 //===--------------------------------------------------------------------===//
174 // Default "noop" implementations
175 //===--------------------------------------------------------------------===//
176
Chris Lattnere87f65e2002-07-30 16:24:28 +0000177 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
178 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
179 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
180 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
181 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
182 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
183 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
184 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
185 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
186 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000187 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 return 0;
189 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000190 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000191 return 0;
192 }
Chris Lattner55406842001-07-21 19:10:49 +0000193
194 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000195 static Constant *CastToBool (const Constant *V) { return 0; }
196 static Constant *CastToSByte (const Constant *V) { return 0; }
197 static Constant *CastToUByte (const Constant *V) { return 0; }
198 static Constant *CastToShort (const Constant *V) { return 0; }
199 static Constant *CastToUShort(const Constant *V) { return 0; }
200 static Constant *CastToInt (const Constant *V) { return 0; }
201 static Constant *CastToUInt (const Constant *V) { return 0; }
202 static Constant *CastToLong (const Constant *V) { return 0; }
203 static Constant *CastToULong (const Constant *V) { return 0; }
204 static Constant *CastToFloat (const Constant *V) { return 0; }
205 static Constant *CastToDouble(const Constant *V) { return 0; }
206 static Constant *CastToPointer(const Constant *,
207 const PointerType *) {return 0;}
Reid Spencer9c47b252005-04-24 22:27:20 +0000208
209public:
210 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211};
212
213
214
215//===----------------------------------------------------------------------===//
216// EmptyRules Class
217//===----------------------------------------------------------------------===//
218//
219// EmptyRules provides a concrete base class of ConstRules that does nothing
220//
Chris Lattner3462ae32001-12-03 22:26:30 +0000221struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000222 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000223 if (V1 == V2) return ConstantBool::True;
224 return 0;
225 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000226};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227
228
229
230//===----------------------------------------------------------------------===//
231// BoolRules Class
232//===----------------------------------------------------------------------===//
233//
234// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
235//
Chris Lattner3462ae32001-12-03 22:26:30 +0000236struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000238 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner07507a42002-09-03 20:09:49 +0000239 return ConstantBool::get(V1->getValue() < V2->getValue());
240 }
241
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000242 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000243 return ConstantBool::get(V1 == V2);
244 }
245
Chris Lattnere87f65e2002-07-30 16:24:28 +0000246 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
247 return ConstantBool::get(V1->getValue() & V2->getValue());
248 }
249
250 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000251 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252 }
253
Chris Lattnere87f65e2002-07-30 16:24:28 +0000254 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
255 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000256 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000257
258 // Casting operators. ick
259#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000260 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000261 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
262 }
263
264 DEF_CAST(Bool , ConstantBool, bool)
265 DEF_CAST(SByte , ConstantSInt, signed char)
266 DEF_CAST(UByte , ConstantUInt, unsigned char)
267 DEF_CAST(Short , ConstantSInt, signed short)
268 DEF_CAST(UShort, ConstantUInt, unsigned short)
269 DEF_CAST(Int , ConstantSInt, signed int)
270 DEF_CAST(UInt , ConstantUInt, unsigned int)
271 DEF_CAST(Long , ConstantSInt, int64_t)
272 DEF_CAST(ULong , ConstantUInt, uint64_t)
273 DEF_CAST(Float , ConstantFP , float)
274 DEF_CAST(Double, ConstantFP , double)
275#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000276};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277
278
279//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000280// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000281//===----------------------------------------------------------------------===//
282//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000283// NullPointerRules provides a concrete base class of ConstRules for null
284// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000285//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000286struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000287 NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000288 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000289 return ConstantBool::True; // Null pointers are always equal
290 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000291 static Constant *CastToBool(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000292 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000293 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000294 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000295 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000296 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000297 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000298 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000299 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000300 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000301 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000302 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000303 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000304 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000305 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000306 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000307 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000308 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000309 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000310 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000311 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000312 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000313 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000314 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000315 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000316 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000317 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000318 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000319 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000320 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000321 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000322 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000323 }
324
Chris Lattner77f20dc2003-11-17 19:21:04 +0000325 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000326 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000327 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000328 }
329};
330
Chris Lattner1171d952006-01-04 02:03:29 +0000331//===----------------------------------------------------------------------===//
332// ConstantPackedRules Class
333//===----------------------------------------------------------------------===//
334
Chris Lattnerf0f40682006-01-04 02:15:02 +0000335/// DoVectorOp - Given two packed constants and a function pointer, apply the
336/// function pointer to each element pair, producing a new ConstantPacked
337/// constant.
338static Constant *EvalVectorOp(const ConstantPacked *V1,
339 const ConstantPacked *V2,
340 Constant *(*FP)(Constant*, Constant*)) {
341 std::vector<Constant*> Res;
342 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
343 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
344 const_cast<Constant*>(V2->getOperand(i))));
345 return ConstantPacked::get(Res);
346}
347
Chris Lattner1171d952006-01-04 02:03:29 +0000348/// PackedTypeRules provides a concrete base class of ConstRules for
349/// ConstantPacked operands.
350///
351struct ConstantPackedRules
352 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerf0f40682006-01-04 02:15:02 +0000353
354 static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
355 return EvalVectorOp(V1, V2, ConstantExpr::getAdd);
356 }
357 static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) {
358 return EvalVectorOp(V1, V2, ConstantExpr::getSub);
359 }
360 static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
361 return EvalVectorOp(V1, V2, ConstantExpr::getMul);
362 }
363 static Constant *Div(const ConstantPacked *V1, const ConstantPacked *V2) {
364 return EvalVectorOp(V1, V2, ConstantExpr::getDiv);
365 }
366 static Constant *Rem(const ConstantPacked *V1, const ConstantPacked *V2) {
367 return EvalVectorOp(V1, V2, ConstantExpr::getRem);
368 }
369 static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) {
370 return EvalVectorOp(V1, V2, ConstantExpr::getAnd);
371 }
372 static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) {
373 return EvalVectorOp(V1, V2, ConstantExpr::getOr);
374 }
375 static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) {
376 return EvalVectorOp(V1, V2, ConstantExpr::getXor);
377 }
378 static Constant *Shl(const ConstantPacked *V1, const ConstantPacked *V2) {
379 return EvalVectorOp(V1, V2, ConstantExpr::getShl);
380 }
381 static Constant *Shr(const ConstantPacked *V1, const ConstantPacked *V2) {
382 return EvalVectorOp(V1, V2, ConstantExpr::getShr);
383 }
384 static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){
385 return 0;
386 }
387 static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) {
Chris Lattner6b52be62006-01-04 02:20:54 +0000388 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) {
389 Constant *C =
390 ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)),
391 const_cast<Constant*>(V2->getOperand(i)));
392 if (ConstantBool *CB = dyn_cast<ConstantBool>(C))
393 return CB;
394 }
395 // Otherwise, could not decide from any element pairs.
Chris Lattnerf0f40682006-01-04 02:15:02 +0000396 return 0;
397 }
Chris Lattner1171d952006-01-04 02:03:29 +0000398};
399
400
401//===----------------------------------------------------------------------===//
402// GeneralPackedRules Class
403//===----------------------------------------------------------------------===//
404
405/// GeneralPackedRules provides a concrete base class of ConstRules for
406/// PackedType operands, where both operands are not ConstantPacked. The usual
407/// cause for this is that one operand is a ConstantAggregateZero.
408///
409struct GeneralPackedRules : public TemplateRules<Constant, GeneralPackedRules> {
410};
411
Chris Lattner977f0042001-11-01 05:55:13 +0000412
413//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000414// DirectRules Class
415//===----------------------------------------------------------------------===//
416//
417// DirectRules provides a concrete base classes of ConstRules for a variety of
418// different types. This allows the C++ compiler to automatically generate our
419// constant handling operations in a typesafe and accurate manner.
420//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000421template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
422struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000423 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
424 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
425 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426 }
427
Chris Lattnere87f65e2002-07-30 16:24:28 +0000428 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
429 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
430 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431 }
432
Chris Lattnere87f65e2002-07-30 16:24:28 +0000433 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
434 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
435 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000436 }
437
Chris Lattnere87f65e2002-07-30 16:24:28 +0000438 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000439 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000440 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
441 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000442 }
443
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000444 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000445 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
446 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000447 }
Chris Lattner55406842001-07-21 19:10:49 +0000448
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000449 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000450 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
451 return ConstantBool::get(R);
452 }
453
Chris Lattner1f0049c2003-04-17 19:24:18 +0000454 static Constant *CastToPointer(const ConstantClass *V,
455 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000456 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000457 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000458 return 0; // Can't const prop other types of pointers
459 }
460
Chris Lattner55406842001-07-21 19:10:49 +0000461 // Casting operators. ick
462#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000463 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000464 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000465 }
466
Chris Lattner3462ae32001-12-03 22:26:30 +0000467 DEF_CAST(Bool , ConstantBool, bool)
468 DEF_CAST(SByte , ConstantSInt, signed char)
469 DEF_CAST(UByte , ConstantUInt, unsigned char)
470 DEF_CAST(Short , ConstantSInt, signed short)
471 DEF_CAST(UShort, ConstantUInt, unsigned short)
472 DEF_CAST(Int , ConstantSInt, signed int)
473 DEF_CAST(UInt , ConstantUInt, unsigned int)
474 DEF_CAST(Long , ConstantSInt, int64_t)
475 DEF_CAST(ULong , ConstantUInt, uint64_t)
476 DEF_CAST(Float , ConstantFP , float)
477 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000478#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000479};
480
Chris Lattner62af86e2002-05-03 20:09:52 +0000481
482//===----------------------------------------------------------------------===//
483// DirectIntRules Class
484//===----------------------------------------------------------------------===//
485//
486// DirectIntRules provides implementations of functions that are valid on
487// integer types, but not all types in general.
488//
489template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000490struct DirectIntRules
491 : public DirectRules<ConstantClass, BuiltinType, Ty,
492 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000493
Chris Lattner268916262003-05-12 15:26:25 +0000494 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
495 if (V2->isNullValue()) return 0;
496 if (V2->isAllOnesValue() && // MIN_INT / -1
497 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
498 return 0;
499 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
500 return ConstantClass::get(*Ty, R);
501 }
502
Chris Lattnere87f65e2002-07-30 16:24:28 +0000503 static Constant *Rem(const ConstantClass *V1,
504 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000505 if (V2->isNullValue()) return 0; // X / 0
506 if (V2->isAllOnesValue() && // MIN_INT / -1
507 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
508 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000509 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
510 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000511 }
Chris Lattner6670d862002-05-06 03:00:54 +0000512
Chris Lattnere87f65e2002-07-30 16:24:28 +0000513 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
514 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
515 return ConstantClass::get(*Ty, R);
516 }
517 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
518 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
519 return ConstantClass::get(*Ty, R);
520 }
521 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
522 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
523 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000524 }
525
Chris Lattnere87f65e2002-07-30 16:24:28 +0000526 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
527 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
528 return ConstantClass::get(*Ty, R);
529 }
530
531 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
532 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
533 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000534 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000535};
536
537
538//===----------------------------------------------------------------------===//
539// DirectFPRules Class
540//===----------------------------------------------------------------------===//
541//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000542/// DirectFPRules provides implementations of functions that are valid on
543/// floating point types, but not all types in general.
544///
Chris Lattner0a144ad2002-05-03 21:41:07 +0000545template <class ConstantClass, class BuiltinType, Type **Ty>
546struct DirectFPRules
547 : public DirectRules<ConstantClass, BuiltinType, Ty,
548 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000549 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000550 if (V2->isNullValue()) return 0;
551 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
552 (BuiltinType)V2->getValue());
553 return ConstantClass::get(*Ty, Result);
554 }
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000555 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Jeff Cohen4e3aede2005-05-03 03:13:01 +0000556 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
557 if (V2->isExactlyValue(0.0)) return ConstantClass::get(*Ty, inf);
558 if (V2->isExactlyValue(-0.0)) return ConstantClass::get(*Ty, -inf);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000559 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
560 return ConstantClass::get(*Ty, R);
561 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000562};
563
Chris Lattner1dd054c2004-01-12 22:07:24 +0000564
565/// ConstRules::get - This method returns the constant rules implementation that
566/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000567ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000568 static EmptyRules EmptyR;
569 static BoolRules BoolR;
570 static NullPointerRules NullPointerR;
Chris Lattner1171d952006-01-04 02:03:29 +0000571 static ConstantPackedRules ConstantPackedR;
572 static GeneralPackedRules GeneralPackedR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000573 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
574 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
575 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
576 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
577 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
578 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
579 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
580 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
581 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
582 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000583
Chris Lattner4b6addf2003-11-17 19:19:32 +0000584 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000585 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
586 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000587 return EmptyR;
588
Chris Lattner6b727592004-06-17 18:19:28 +0000589 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000590 default: assert(0 && "Unknown value type for constant folding!");
591 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000592 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000593 case Type::SByteTyID: return SByteR;
594 case Type::UByteTyID: return UByteR;
595 case Type::ShortTyID: return ShortR;
596 case Type::UShortTyID: return UShortR;
597 case Type::IntTyID: return IntR;
598 case Type::UIntTyID: return UIntR;
599 case Type::LongTyID: return LongR;
600 case Type::ULongTyID: return ULongR;
601 case Type::FloatTyID: return FloatR;
602 case Type::DoubleTyID: return DoubleR;
Chris Lattner1171d952006-01-04 02:03:29 +0000603 case Type::PackedTyID:
604 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
605 return ConstantPackedR;
606 return GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000607 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000608}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000609
610
611//===----------------------------------------------------------------------===//
612// ConstantFold*Instruction Implementations
613//===----------------------------------------------------------------------===//
614//
615// These methods contain the special case hackery required to symbolically
616// evaluate some constant expression cases, and use the ConstantRules class to
617// evaluate normal constants.
618//
619static unsigned getSize(const Type *Ty) {
620 unsigned S = Ty->getPrimitiveSize();
621 return S ? S : 8; // Treat pointers at 8 bytes
622}
623
624Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
625 const Type *DestTy) {
626 if (V->getType() == DestTy) return (Constant*)V;
627
Chris Lattnerea0789c2004-03-08 06:17:35 +0000628 // Cast of a global address to boolean is always true.
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000629 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerea0789c2004-03-08 06:17:35 +0000630 if (DestTy == Type::BoolTy)
631 // FIXME: When we support 'external weak' references, we have to prevent
Chris Lattnercd4003e2005-01-06 16:26:38 +0000632 // this transformation from happening. This code will need to be updated
633 // to ignore external weak symbols when we support it.
634 return ConstantBool::True;
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000635 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000636 if (CE->getOpcode() == Instruction::Cast) {
637 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
638 // Try to not produce a cast of a cast, which is almost always redundant.
639 if (!Op->getType()->isFloatingPoint() &&
640 !CE->getType()->isFloatingPoint() &&
Reid Spencer8eb06df2004-05-30 01:19:48 +0000641 !DestTy->isFloatingPoint()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000642 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
643 unsigned S3 = getSize(DestTy);
644 if (Op->getType() == DestTy && S3 >= S2)
645 return Op;
646 if (S1 >= S2 && S2 >= S3)
647 return ConstantExpr::getCast(Op, DestTy);
648 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
649 return ConstantExpr::getCast(Op, DestTy);
650 }
651 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
652 // If all of the indexes in the GEP are null values, there is no pointer
653 // adjustment going on. We might as well cast the source pointer.
654 bool isAllNull = true;
655 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
656 if (!CE->getOperand(i)->isNullValue()) {
657 isAllNull = false;
658 break;
659 }
660 if (isAllNull)
661 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
662 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000663 } else if (isa<UndefValue>(V)) {
664 return UndefValue::get(DestTy);
665 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000666
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000667 // Check to see if we are casting an pointer to an aggregate to a pointer to
668 // the first element. If so, return the appropriate GEP instruction.
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000669 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000670 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
671 std::vector<Value*> IdxList;
672 IdxList.push_back(Constant::getNullValue(Type::IntTy));
673 const Type *ElTy = PTy->getElementType();
674 while (ElTy != DPTy->getElementType()) {
675 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
Chris Lattner9e907202004-11-22 19:15:27 +0000676 if (STy->getNumElements() == 0) break;
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000677 ElTy = STy->getElementType(0);
678 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
679 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
680 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
681 ElTy = STy->getElementType();
682 IdxList.push_back(IdxList[0]);
683 } else {
684 break;
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000685 }
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000686 }
687
688 if (ElTy == DPTy->getElementType())
689 return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
690 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000691
Chris Lattner1dd054c2004-01-12 22:07:24 +0000692 ConstRules &Rules = ConstRules::get(V, V);
693
Chris Lattner6b727592004-06-17 18:19:28 +0000694 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000695 case Type::BoolTyID: return Rules.castToBool(V);
696 case Type::UByteTyID: return Rules.castToUByte(V);
697 case Type::SByteTyID: return Rules.castToSByte(V);
698 case Type::UShortTyID: return Rules.castToUShort(V);
699 case Type::ShortTyID: return Rules.castToShort(V);
700 case Type::UIntTyID: return Rules.castToUInt(V);
701 case Type::IntTyID: return Rules.castToInt(V);
702 case Type::ULongTyID: return Rules.castToULong(V);
703 case Type::LongTyID: return Rules.castToLong(V);
704 case Type::FloatTyID: return Rules.castToFloat(V);
705 case Type::DoubleTyID: return Rules.castToDouble(V);
706 case Type::PointerTyID:
707 return Rules.castToPointer(V, cast<PointerType>(DestTy));
708 default: return 0;
709 }
710}
711
Chris Lattner6ea4b522004-03-12 05:53:32 +0000712Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
713 const Constant *V1,
714 const Constant *V2) {
715 if (Cond == ConstantBool::True)
716 return const_cast<Constant*>(V1);
717 else if (Cond == ConstantBool::False)
718 return const_cast<Constant*>(V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000719
720 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
721 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
722 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000723 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000724 return 0;
725}
726
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000727Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
728 const Constant *Idx) {
729 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
730 if (const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx)) {
731 return const_cast<Constant*>(CVal->getOperand(CIdx->getValue()));
732 }
733 }
734 return 0;
735}
736
Robert Bocchinoca27f032006-01-17 20:07:22 +0000737Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
738 const Constant *Elt,
739 const Constant *Idx) {
740 const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx);
741 if (!CIdx) return 0;
742 unsigned idxVal = CIdx->getValue();
743 if (const UndefValue *UVal = dyn_cast<UndefValue>(Val)) {
744 // Insertion of scalar constant into packed undef
745 // Optimize away insertion of undef
746 if (isa<UndefValue>(Elt))
747 return const_cast<Constant*>(Val);
748 // Otherwise break the aggregate undef into multiple undefs and do
749 // the insertion
750 unsigned numOps =
751 cast<PackedType>(Val->getType())->getNumElements();
752 std::vector<Constant*> Ops;
753 Ops.reserve(numOps);
754 for (unsigned i = 0; i < numOps; ++i) {
755 const Constant *Op =
756 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
757 Ops.push_back(const_cast<Constant*>(Op));
758 }
759 return ConstantPacked::get(Ops);
760 }
761 if (const ConstantAggregateZero *CVal =
762 dyn_cast<ConstantAggregateZero>(Val)) {
763 // Insertion of scalar constant into packed aggregate zero
764 // Optimize away insertion of zero
765 if (Elt->isNullValue())
766 return const_cast<Constant*>(Val);
767 // Otherwise break the aggregate zero into multiple zeros and do
768 // the insertion
769 unsigned numOps =
770 cast<PackedType>(Val->getType())->getNumElements();
771 std::vector<Constant*> Ops;
772 Ops.reserve(numOps);
773 for (unsigned i = 0; i < numOps; ++i) {
774 const Constant *Op =
775 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
776 Ops.push_back(const_cast<Constant*>(Op));
777 }
778 return ConstantPacked::get(Ops);
779 }
780 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
781 // Insertion of scalar constant into packed constant
782 std::vector<Constant*> Ops;
783 Ops.reserve(CVal->getNumOperands());
784 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
785 const Constant *Op =
786 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
787 Ops.push_back(const_cast<Constant*>(Op));
788 }
789 return ConstantPacked::get(Ops);
790 }
791 return 0;
792}
793
Chris Lattner60c47262005-01-28 19:09:51 +0000794/// isZeroSizedType - This type is zero sized if its an array or structure of
795/// zero sized types. The only leaf zero sized type is an empty structure.
796static bool isMaybeZeroSizedType(const Type *Ty) {
797 if (isa<OpaqueType>(Ty)) return true; // Can't say.
798 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
799
800 // If all of elements have zero size, this does too.
801 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000802 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000803 return true;
804
805 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
806 return isMaybeZeroSizedType(ATy->getElementType());
807 }
808 return false;
809}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000810
Chris Lattner061da2f2004-01-13 05:51:55 +0000811/// IdxCompare - Compare the two constants as though they were getelementptr
812/// indices. This allows coersion of the types to be the same thing.
813///
814/// If the two constants are the "same" (after coersion), return 0. If the
815/// first is less than the second, return -1, if the second is less than the
816/// first, return 1. If the constants are not integral, return -2.
817///
Chris Lattner60c47262005-01-28 19:09:51 +0000818static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000819 if (C1 == C2) return 0;
820
821 // Ok, we found a different index. Are either of the operands
822 // ConstantExprs? If so, we can't do anything with them.
823 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
824 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000825
Chris Lattner69193f92004-04-05 01:30:19 +0000826 // Ok, we have two differing integer indices. Sign extend them to be the same
827 // type. Long is always big enough, so we use it.
828 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
829 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000830 if (C1 == C2) return 0; // Are they just differing types?
831
Chris Lattner60c47262005-01-28 19:09:51 +0000832 // If the type being indexed over is really just a zero sized type, there is
833 // no pointer difference being made here.
834 if (isMaybeZeroSizedType(ElTy))
835 return -2; // dunno.
836
Chris Lattner061da2f2004-01-13 05:51:55 +0000837 // If they are really different, now that they are the same type, then we
838 // found a difference!
839 if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
840 return -1;
841 else
842 return 1;
843}
844
845/// evaluateRelation - This function determines if there is anything we can
846/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000847/// things like integer comparisons, but should instead handle ConstantExprs
848/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +0000849/// particular relation to each other, we should return the corresponding SetCC
850/// code, otherwise return Instruction::BinaryOpsEnd.
851///
852/// To simplify this code we canonicalize the relation so that the first
853/// operand is always the most "complex" of the two. We consider simple
854/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000855/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000856///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000857static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000858 assert(V1->getType() == V2->getType() &&
859 "Cannot compare different types of values!");
860 if (V1 == V2) return Instruction::SetEQ;
861
Reid Spenceraccd7c72004-07-17 23:47:01 +0000862 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000863 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
864 // We distilled this down to a simple case, use the standard constant
865 // folder.
866 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
867 if (R == ConstantBool::True) return Instruction::SetEQ;
868 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
869 if (R == ConstantBool::True) return Instruction::SetLT;
870 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
871 if (R == ConstantBool::True) return Instruction::SetGT;
872
873 // If we couldn't figure it out, bail.
874 return Instruction::BinaryOpsEnd;
875 }
876
Chris Lattner061da2f2004-01-13 05:51:55 +0000877 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +0000878 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
879 if (SwappedRelation != Instruction::BinaryOpsEnd)
880 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000881
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000882 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000883 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000884 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
885 if (SwappedRelation != Instruction::BinaryOpsEnd)
886 return SetCondInst::getSwappedCondition(SwappedRelation);
887 else
888 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +0000889 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000890
Reid Spenceraccd7c72004-07-17 23:47:01 +0000891 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000892 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000893 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
894 assert(CPR1 != CPR2 &&
895 "GVs for the same value exist at different addresses??");
Chris Lattner061da2f2004-01-13 05:51:55 +0000896 // FIXME: If both globals are external weak, they might both be null!
897 return Instruction::SetNE;
898 } else {
899 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
900 // Global can never be null. FIXME: if we implement external weak
901 // linkage, this is not necessarily true!
902 return Instruction::SetNE;
903 }
904
905 } else {
906 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
907 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000908 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +0000909 Constant *CE1Op0 = CE1->getOperand(0);
910
911 switch (CE1->getOpcode()) {
912 case Instruction::Cast:
913 // If the cast is not actually changing bits, and the second operand is a
914 // null pointer, do the comparison with the pre-casted value.
915 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000916 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +0000917 return evaluateRelation(CE1Op0,
918 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000919
920 // If the dest type is a pointer type, and the RHS is a constantexpr cast
921 // from the same type as the src of the LHS, evaluate the inputs. This is
922 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
923 // which happens a lot in compilers with tagged integers.
924 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
925 if (isa<PointerType>(CE1->getType()) &&
926 CE2->getOpcode() == Instruction::Cast &&
927 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
928 CE1->getOperand(0)->getType()->isIntegral()) {
929 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
930 }
Chris Lattner192e3262004-04-11 01:29:30 +0000931 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000932
933 case Instruction::GetElementPtr:
934 // Ok, since this is a getelementptr, we know that the constant has a
935 // pointer type. Check the various cases.
936 if (isa<ConstantPointerNull>(V2)) {
937 // If we are comparing a GEP to a null pointer, check to see if the base
938 // of the GEP equals the null pointer.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000939 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000940 // FIXME: this is not true when we have external weak references!
941 // No offset can go from a global to a null pointer.
942 return Instruction::SetGT;
943 } else if (isa<ConstantPointerNull>(CE1Op0)) {
944 // If we are indexing from a null pointer, check to see if we have any
945 // non-zero indices.
946 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
947 if (!CE1->getOperand(i)->isNullValue())
948 // Offsetting from null, must not be equal.
949 return Instruction::SetGT;
950 // Only zero indexes from null, must still be zero.
951 return Instruction::SetEQ;
952 }
953 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000954 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000955 if (isa<ConstantPointerNull>(CE1Op0)) {
956 // FIXME: This is not true with external weak references.
957 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000958 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000959 if (CPR1 == CPR2) {
960 // If this is a getelementptr of the same global, then it must be
961 // different. Because the types must match, the getelementptr could
962 // only have at most one index, and because we fold getelementptr's
963 // with a single zero index, it must be nonzero.
964 assert(CE1->getNumOperands() == 2 &&
965 !CE1->getOperand(1)->isNullValue() &&
966 "Suprising getelementptr!");
967 return Instruction::SetGT;
968 } else {
969 // If they are different globals, we don't know what the value is,
970 // but they can't be equal.
971 return Instruction::SetNE;
972 }
973 }
974 } else {
975 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
976 const Constant *CE2Op0 = CE2->getOperand(0);
977
978 // There are MANY other foldings that we could perform here. They will
979 // probably be added on demand, as they seem needed.
980 switch (CE2->getOpcode()) {
981 default: break;
982 case Instruction::GetElementPtr:
983 // By far the most common case to handle is when the base pointers are
984 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000985 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000986 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
987 return Instruction::SetNE;
988 // Ok, we know that both getelementptr instructions are based on the
989 // same global. From this, we can precisely determine the relative
990 // ordering of the resultant pointers.
991 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000992
Chris Lattner061da2f2004-01-13 05:51:55 +0000993 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +0000994 gep_type_iterator GTI = gep_type_begin(CE1);
995 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
996 ++i, ++GTI)
997 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
998 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000999 case -1: return Instruction::SetLT;
1000 case 1: return Instruction::SetGT;
1001 case -2: return Instruction::BinaryOpsEnd;
1002 }
1003
1004 // Ok, we ran out of things they have in common. If any leftovers
1005 // are non-zero then we have a difference, otherwise we are equal.
1006 for (; i < CE1->getNumOperands(); ++i)
1007 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001008 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1009 return Instruction::SetGT;
1010 else
1011 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001012
Chris Lattner061da2f2004-01-13 05:51:55 +00001013 for (; i < CE2->getNumOperands(); ++i)
1014 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001015 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1016 return Instruction::SetLT;
1017 else
1018 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001019 return Instruction::SetEQ;
1020 }
1021 }
1022 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001023
Chris Lattner061da2f2004-01-13 05:51:55 +00001024 default:
1025 break;
1026 }
1027 }
1028
1029 return Instruction::BinaryOpsEnd;
1030}
1031
Chris Lattner1dd054c2004-01-12 22:07:24 +00001032Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1033 const Constant *V1,
1034 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001035 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001036 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001037 default: break;
1038 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1039 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1040 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
1041 case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break;
1042 case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break;
1043 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1044 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1045 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1046 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
1047 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
1048 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
1049 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1050 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001051 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
1052 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001053 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001054 break;
1055 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1056 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001057 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001058 break;
1059 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1060 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001061 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001062 break;
1063 }
1064
Chris Lattner061da2f2004-01-13 05:51:55 +00001065 // If we successfully folded the expression, return it now.
1066 if (C) return C;
1067
Chris Lattner192eacc2004-10-17 04:01:51 +00001068 if (SetCondInst::isRelational(Opcode)) {
1069 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1070 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001071 switch (evaluateRelation(const_cast<Constant*>(V1),
1072 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001073 default: assert(0 && "Unknown relational!");
1074 case Instruction::BinaryOpsEnd:
1075 break; // Couldn't determine anything about these constants.
1076 case Instruction::SetEQ: // We know the constants are equal!
1077 // If we know the constants are equal, we can decide the result of this
1078 // computation precisely.
1079 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1080 Opcode == Instruction::SetLE ||
1081 Opcode == Instruction::SetGE);
1082 case Instruction::SetLT:
1083 // If we know that V1 < V2, we can decide the result of this computation
1084 // precisely.
1085 return ConstantBool::get(Opcode == Instruction::SetLT ||
1086 Opcode == Instruction::SetNE ||
1087 Opcode == Instruction::SetLE);
1088 case Instruction::SetGT:
1089 // If we know that V1 > V2, we can decide the result of this computation
1090 // precisely.
1091 return ConstantBool::get(Opcode == Instruction::SetGT ||
1092 Opcode == Instruction::SetNE ||
1093 Opcode == Instruction::SetGE);
1094 case Instruction::SetLE:
1095 // If we know that V1 <= V2, we can only partially decide this relation.
1096 if (Opcode == Instruction::SetGT) return ConstantBool::False;
1097 if (Opcode == Instruction::SetLT) return ConstantBool::True;
1098 break;
1099
1100 case Instruction::SetGE:
1101 // If we know that V1 >= V2, we can only partially decide this relation.
1102 if (Opcode == Instruction::SetLT) return ConstantBool::False;
1103 if (Opcode == Instruction::SetGT) return ConstantBool::True;
1104 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001105
Chris Lattner061da2f2004-01-13 05:51:55 +00001106 case Instruction::SetNE:
1107 // If we know that V1 != V2, we can only partially decide this relation.
1108 if (Opcode == Instruction::SetEQ) return ConstantBool::False;
1109 if (Opcode == Instruction::SetNE) return ConstantBool::True;
1110 break;
1111 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001112 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001113
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001114 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1115 switch (Opcode) {
1116 case Instruction::Add:
1117 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001118 case Instruction::Xor:
1119 return UndefValue::get(V1->getType());
1120
1121 case Instruction::Mul:
1122 case Instruction::And:
1123 return Constant::getNullValue(V1->getType());
1124 case Instruction::Div:
1125 case Instruction::Rem:
1126 if (!isa<UndefValue>(V2)) // undef/X -> 0
1127 return Constant::getNullValue(V1->getType());
1128 return const_cast<Constant*>(V2); // X/undef -> undef
1129 case Instruction::Or: // X|undef -> -1
1130 return ConstantInt::getAllOnesValue(V1->getType());
1131 case Instruction::Shr:
1132 if (!isa<UndefValue>(V2)) {
1133 if (V1->getType()->isSigned())
1134 return const_cast<Constant*>(V1); // undef >>s X -> undef
1135 // undef >>u X -> 0
1136 } else if (isa<UndefValue>(V1)) {
1137 return const_cast<Constant*>(V1); // undef >> undef -> undef
1138 } else {
1139 if (V1->getType()->isSigned())
1140 return const_cast<Constant*>(V1); // X >>s undef -> X
1141 // X >>u undef -> 0
1142 }
1143 return Constant::getNullValue(V1->getType());
1144
1145 case Instruction::Shl:
1146 // undef << X -> 0 X << undef -> 0
1147 return Constant::getNullValue(V1->getType());
1148 }
1149 }
1150
Chris Lattner061da2f2004-01-13 05:51:55 +00001151 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
1152 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1153 // There are many possible foldings we could do here. We should probably
1154 // at least fold add of a pointer with an integer into the appropriate
1155 // getelementptr. This will improve alias analysis a bit.
1156
1157
1158
1159
1160 } else {
1161 // Just implement a couple of simple identities.
1162 switch (Opcode) {
1163 case Instruction::Add:
1164 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1165 break;
1166 case Instruction::Sub:
1167 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1168 break;
1169 case Instruction::Mul:
1170 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1171 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1172 if (CI->getRawValue() == 1)
1173 return const_cast<Constant*>(V1); // X * 1 == X
1174 break;
1175 case Instruction::Div:
1176 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1177 if (CI->getRawValue() == 1)
1178 return const_cast<Constant*>(V1); // X / 1 == X
1179 break;
1180 case Instruction::Rem:
1181 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1182 if (CI->getRawValue() == 1)
1183 return Constant::getNullValue(CI->getType()); // X % 1 == 0
1184 break;
1185 case Instruction::And:
1186 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1187 return const_cast<Constant*>(V1); // X & -1 == X
1188 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Chris Lattnerea0789c2004-03-08 06:17:35 +00001189 if (CE1->getOpcode() == Instruction::Cast &&
Reid Spenceraccd7c72004-07-17 23:47:01 +00001190 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001191 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001192
1193 // Functions are at least 4-byte aligned. If and'ing the address of a
1194 // function with a constant < 4, fold it to zero.
1195 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spenceraccd7c72004-07-17 23:47:01 +00001196 if (CI->getRawValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001197 return Constant::getNullValue(CI->getType());
1198 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001199 break;
1200 case Instruction::Or:
1201 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1202 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1203 return const_cast<Constant*>(V2); // X | -1 == -1
1204 break;
1205 case Instruction::Xor:
1206 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1207 break;
1208 }
1209 }
1210
1211 } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1212 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1213 // other way if possible.
1214 switch (Opcode) {
1215 case Instruction::Add:
1216 case Instruction::Mul:
1217 case Instruction::And:
1218 case Instruction::Or:
1219 case Instruction::Xor:
1220 case Instruction::SetEQ:
1221 case Instruction::SetNE:
1222 // No change of opcode required.
1223 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1224
1225 case Instruction::SetLT:
1226 case Instruction::SetGT:
1227 case Instruction::SetLE:
1228 case Instruction::SetGE:
1229 // Change the opcode as necessary to swap the operands.
1230 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1231 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1232
1233 case Instruction::Shl:
1234 case Instruction::Shr:
1235 case Instruction::Sub:
1236 case Instruction::Div:
1237 case Instruction::Rem:
1238 default: // These instructions cannot be flopped around.
1239 break;
1240 }
1241 }
1242 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001243}
1244
1245Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001246 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001247 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001248 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001249 return const_cast<Constant*>(C);
1250
Chris Lattnerf6013752004-10-17 21:54:55 +00001251 if (isa<UndefValue>(C)) {
1252 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1253 true);
1254 assert(Ty != 0 && "Invalid indices for GEP!");
1255 return UndefValue::get(PointerType::get(Ty));
1256 }
1257
1258 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001259 if (C->isNullValue()) {
1260 bool isNull = true;
1261 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001262 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001263 isNull = false;
1264 break;
1265 }
1266 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001267 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001268 true);
1269 assert(Ty != 0 && "Invalid indices for GEP!");
1270 return ConstantPointerNull::get(PointerType::get(Ty));
1271 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001272
1273 if (IdxList.size() == 1) {
1274 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
1275 if (unsigned ElSize = ElTy->getPrimitiveSize()) {
1276 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1277 // type, we can statically fold this.
1278 Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
Chris Lattner13128ab2004-10-11 22:52:25 +00001279 R = ConstantExpr::getCast(R, Idx0->getType());
1280 R = ConstantExpr::getMul(R, Idx0);
Chris Lattner4bbd4092004-07-15 01:16:59 +00001281 return ConstantExpr::getCast(R, C->getType());
1282 }
1283 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001284 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001285
1286 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1287 // Combine Indices - If the source pointer to this getelementptr instruction
1288 // is a getelementptr instruction, combine the indices of the two
1289 // getelementptr instructions into a single instruction.
1290 //
1291 if (CE->getOpcode() == Instruction::GetElementPtr) {
1292 const Type *LastTy = 0;
1293 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1294 I != E; ++I)
1295 LastTy = *I;
1296
Chris Lattner13128ab2004-10-11 22:52:25 +00001297 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1298 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001299 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1300 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001301 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001302
1303 // Add the last index of the source with the first index of the new GEP.
1304 // Make sure to handle the case when they are actually different types.
1305 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001306 // Otherwise it must be an array.
1307 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001308 const Type *IdxTy = Combined->getType();
Chris Lattner13128ab2004-10-11 22:52:25 +00001309 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001310 Combined =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001311 ConstantExpr::get(Instruction::Add,
Chris Lattner13128ab2004-10-11 22:52:25 +00001312 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattner71068a02004-07-07 04:45:13 +00001313 ConstantExpr::getCast(Combined, IdxTy));
1314 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001315
Chris Lattner1dd054c2004-01-12 22:07:24 +00001316 NewIndices.push_back(Combined);
1317 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1318 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1319 }
1320 }
1321
1322 // Implement folding of:
1323 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1324 // long 0, long 0)
1325 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1326 //
1327 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
Chris Lattner13128ab2004-10-11 22:52:25 +00001328 Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001329 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001330 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1331 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1332 if (const ArrayType *CAT =
1333 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
1334 if (CAT->getElementType() == SAT->getElementType())
1335 return ConstantExpr::getGetElementPtr(
1336 (Constant*)CE->getOperand(0), IdxList);
1337 }
1338 return 0;
1339}
1340