blob: 094ec373f1050b54ceda19e4fd948c5527557edf [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
11// (internal) ConstantFolding.h interface, which is used by the
12// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner5a945e32004-01-12 21:13:12 +000021#include "ConstantFolding.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattner78a0d422002-05-07 20:44:59 +000023#include "llvm/iPHINode.h"
Chris Lattner061da2f2004-01-13 05:51:55 +000024#include "llvm/iOperators.h"
Chris Lattner8acf3462003-05-27 19:16:07 +000025#include "llvm/InstrTypes.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000026#include "llvm/DerivedTypes.h"
Chris Lattnerad70d4a2003-11-25 21:21:46 +000027#include "llvm/Support/GetElementPtrTypeIterator.h"
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() {}
34
35 // Binary Operators...
36 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
37 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
38 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
39 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
40 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
42 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
43 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
45 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
46 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0;
47 virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0;
48
49 // Casting operators.
50 virtual Constant *castToBool (const Constant *V) const = 0;
51 virtual Constant *castToSByte (const Constant *V) const = 0;
52 virtual Constant *castToUByte (const Constant *V) const = 0;
53 virtual Constant *castToShort (const Constant *V) const = 0;
54 virtual Constant *castToUShort(const Constant *V) const = 0;
55 virtual Constant *castToInt (const Constant *V) const = 0;
56 virtual Constant *castToUInt (const Constant *V) const = 0;
57 virtual Constant *castToLong (const Constant *V) const = 0;
58 virtual Constant *castToULong (const Constant *V) const = 0;
59 virtual Constant *castToFloat (const Constant *V) const = 0;
60 virtual Constant *castToDouble(const Constant *V) const = 0;
61 virtual Constant *castToPointer(const Constant *V,
62 const PointerType *Ty) const = 0;
63
64 // ConstRules::get - Return an instance of ConstRules for the specified
65 // constant operands.
66 //
67 static ConstRules &get(const Constant *V1, const Constant *V2);
68 private:
69 ConstRules(const ConstRules &); // Do not implement
70 ConstRules &operator=(const ConstRules &); // Do not implement
71 };
72}
73
74
Chris Lattner2f7c9632001-06-06 20:29:01 +000075//===----------------------------------------------------------------------===//
76// TemplateRules Class
77//===----------------------------------------------------------------------===//
78//
79// TemplateRules - Implement a subclass of ConstRules that provides all
80// operations as noops. All other rules classes inherit from this class so
81// that if functionality is needed in the future, it can simply be added here
82// and to ConstRules without changing anything else...
83//
84// This class also provides subclasses with typesafe implementations of methods
85// so that don't have to do type casting.
86//
87template<class ArgType, class SubClassName>
88class TemplateRules : public ConstRules {
89
90 //===--------------------------------------------------------------------===//
91 // Redirecting functions that cast to the appropriate types
92 //===--------------------------------------------------------------------===//
93
Chris Lattnere87f65e2002-07-30 16:24:28 +000094 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000095 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
96 }
Chris Lattnere87f65e2002-07-30 16:24:28 +000097 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +000098 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
99 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000100 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000101 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
102 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000103 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000104 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
105 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000106 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000107 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
108 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000109 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
110 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
111 }
112 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
113 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
114 }
115 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
116 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
117 }
118 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000119 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
120 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000121 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000122 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
123 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000124
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000125 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000126 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
127 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000128 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000129 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
130 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131
Chris Lattner55406842001-07-21 19:10:49 +0000132 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000133 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000134 return SubClassName::CastToBool((const ArgType*)V);
135 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000136 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000137 return SubClassName::CastToSByte((const ArgType*)V);
138 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000139 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000140 return SubClassName::CastToUByte((const ArgType*)V);
141 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000142 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000143 return SubClassName::CastToShort((const ArgType*)V);
144 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000145 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000146 return SubClassName::CastToUShort((const ArgType*)V);
147 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000148 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000149 return SubClassName::CastToInt((const ArgType*)V);
150 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000151 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000152 return SubClassName::CastToUInt((const ArgType*)V);
153 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000154 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000155 return SubClassName::CastToLong((const ArgType*)V);
156 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000157 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000158 return SubClassName::CastToULong((const ArgType*)V);
159 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000160 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000161 return SubClassName::CastToFloat((const ArgType*)V);
162 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000163 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000164 return SubClassName::CastToDouble((const ArgType*)V);
165 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000166 virtual Constant *castToPointer(const Constant *V,
167 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000168 return SubClassName::CastToPointer((const ArgType*)V, Ty);
169 }
Chris Lattner55406842001-07-21 19:10:49 +0000170
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 //===--------------------------------------------------------------------===//
172 // Default "noop" implementations
173 //===--------------------------------------------------------------------===//
174
Chris Lattnere87f65e2002-07-30 16:24:28 +0000175 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
176 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
177 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
178 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
179 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
180 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
181 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
182 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
183 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
184 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000185 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000186 return 0;
187 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000188 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000189 return 0;
190 }
Chris Lattner55406842001-07-21 19:10:49 +0000191
192 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000193 static Constant *CastToBool (const Constant *V) { return 0; }
194 static Constant *CastToSByte (const Constant *V) { return 0; }
195 static Constant *CastToUByte (const Constant *V) { return 0; }
196 static Constant *CastToShort (const Constant *V) { return 0; }
197 static Constant *CastToUShort(const Constant *V) { return 0; }
198 static Constant *CastToInt (const Constant *V) { return 0; }
199 static Constant *CastToUInt (const Constant *V) { return 0; }
200 static Constant *CastToLong (const Constant *V) { return 0; }
201 static Constant *CastToULong (const Constant *V) { return 0; }
202 static Constant *CastToFloat (const Constant *V) { return 0; }
203 static Constant *CastToDouble(const Constant *V) { return 0; }
204 static Constant *CastToPointer(const Constant *,
205 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206};
207
208
209
210//===----------------------------------------------------------------------===//
211// EmptyRules Class
212//===----------------------------------------------------------------------===//
213//
214// EmptyRules provides a concrete base class of ConstRules that does nothing
215//
Chris Lattner3462ae32001-12-03 22:26:30 +0000216struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000217 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000218 if (V1 == V2) return ConstantBool::True;
219 return 0;
220 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000221};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222
223
224
225//===----------------------------------------------------------------------===//
226// BoolRules Class
227//===----------------------------------------------------------------------===//
228//
229// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
230//
Chris Lattner3462ae32001-12-03 22:26:30 +0000231struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000233 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){
Chris Lattner07507a42002-09-03 20:09:49 +0000234 return ConstantBool::get(V1->getValue() < V2->getValue());
235 }
236
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000237 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000238 return ConstantBool::get(V1 == V2);
239 }
240
Chris Lattnere87f65e2002-07-30 16:24:28 +0000241 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
242 return ConstantBool::get(V1->getValue() & V2->getValue());
243 }
244
245 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000246 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247 }
248
Chris Lattnere87f65e2002-07-30 16:24:28 +0000249 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
250 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000252
253 // Casting operators. ick
254#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000255 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000256 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
257 }
258
259 DEF_CAST(Bool , ConstantBool, bool)
260 DEF_CAST(SByte , ConstantSInt, signed char)
261 DEF_CAST(UByte , ConstantUInt, unsigned char)
262 DEF_CAST(Short , ConstantSInt, signed short)
263 DEF_CAST(UShort, ConstantUInt, unsigned short)
264 DEF_CAST(Int , ConstantSInt, signed int)
265 DEF_CAST(UInt , ConstantUInt, unsigned int)
266 DEF_CAST(Long , ConstantSInt, int64_t)
267 DEF_CAST(ULong , ConstantUInt, uint64_t)
268 DEF_CAST(Float , ConstantFP , float)
269 DEF_CAST(Double, ConstantFP , double)
270#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000271};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272
273
274//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000275// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000276//===----------------------------------------------------------------------===//
277//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000278// NullPointerRules provides a concrete base class of ConstRules for null
279// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000280//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000281struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000282 NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000283 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000284 return ConstantBool::True; // Null pointers are always equal
285 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000286 static Constant *CastToBool(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000287 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000288 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000289 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000290 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000291 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000292 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000293 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000294 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000295 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000296 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000297 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000298 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000299 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000300 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000301 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000302 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000303 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000304 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000305 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000306 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000307 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000308 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000309 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000310 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000311 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000312 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000313 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000314 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000315 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000316 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000317 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000318 }
319
Chris Lattner77f20dc2003-11-17 19:21:04 +0000320 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000321 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000322 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000323 }
324};
325
326
327//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328// DirectRules Class
329//===----------------------------------------------------------------------===//
330//
331// DirectRules provides a concrete base classes of ConstRules for a variety of
332// different types. This allows the C++ compiler to automatically generate our
333// constant handling operations in a typesafe and accurate manner.
334//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000335template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
336struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000337 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
338 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
339 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 }
341
Chris Lattnere87f65e2002-07-30 16:24:28 +0000342 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
343 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
344 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345 }
346
Chris Lattnere87f65e2002-07-30 16:24:28 +0000347 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
348 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
349 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000350 }
351
Chris Lattnere87f65e2002-07-30 16:24:28 +0000352 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000353 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000354 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
355 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000356 }
357
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000358 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000359 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
360 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000361 }
Chris Lattner55406842001-07-21 19:10:49 +0000362
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000363 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000364 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
365 return ConstantBool::get(R);
366 }
367
Chris Lattner1f0049c2003-04-17 19:24:18 +0000368 static Constant *CastToPointer(const ConstantClass *V,
369 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000370 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000371 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000372 return 0; // Can't const prop other types of pointers
373 }
374
Chris Lattner55406842001-07-21 19:10:49 +0000375 // Casting operators. ick
376#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000377 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000378 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000379 }
380
Chris Lattner3462ae32001-12-03 22:26:30 +0000381 DEF_CAST(Bool , ConstantBool, bool)
382 DEF_CAST(SByte , ConstantSInt, signed char)
383 DEF_CAST(UByte , ConstantUInt, unsigned char)
384 DEF_CAST(Short , ConstantSInt, signed short)
385 DEF_CAST(UShort, ConstantUInt, unsigned short)
386 DEF_CAST(Int , ConstantSInt, signed int)
387 DEF_CAST(UInt , ConstantUInt, unsigned int)
388 DEF_CAST(Long , ConstantSInt, int64_t)
389 DEF_CAST(ULong , ConstantUInt, uint64_t)
390 DEF_CAST(Float , ConstantFP , float)
391 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000392#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000393};
394
Chris Lattner62af86e2002-05-03 20:09:52 +0000395
396//===----------------------------------------------------------------------===//
397// DirectIntRules Class
398//===----------------------------------------------------------------------===//
399//
400// DirectIntRules provides implementations of functions that are valid on
401// integer types, but not all types in general.
402//
403template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000404struct DirectIntRules
405 : public DirectRules<ConstantClass, BuiltinType, Ty,
406 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000407
Chris Lattner268916262003-05-12 15:26:25 +0000408 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
409 if (V2->isNullValue()) return 0;
410 if (V2->isAllOnesValue() && // MIN_INT / -1
411 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
412 return 0;
413 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
414 return ConstantClass::get(*Ty, R);
415 }
416
Chris Lattnere87f65e2002-07-30 16:24:28 +0000417 static Constant *Rem(const ConstantClass *V1,
418 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000419 if (V2->isNullValue()) return 0; // X / 0
420 if (V2->isAllOnesValue() && // MIN_INT / -1
421 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
422 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000423 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
424 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000425 }
Chris Lattner6670d862002-05-06 03:00:54 +0000426
Chris Lattnere87f65e2002-07-30 16:24:28 +0000427 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
428 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
429 return ConstantClass::get(*Ty, R);
430 }
431 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
432 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
433 return ConstantClass::get(*Ty, R);
434 }
435 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
436 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
437 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000438 }
439
Chris Lattnere87f65e2002-07-30 16:24:28 +0000440 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
441 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
442 return ConstantClass::get(*Ty, R);
443 }
444
445 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
446 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
447 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000448 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000449};
450
451
452//===----------------------------------------------------------------------===//
453// DirectFPRules Class
454//===----------------------------------------------------------------------===//
455//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000456/// DirectFPRules provides implementations of functions that are valid on
457/// floating point types, but not all types in general.
458///
Chris Lattner0a144ad2002-05-03 21:41:07 +0000459template <class ConstantClass, class BuiltinType, Type **Ty>
460struct DirectFPRules
461 : public DirectRules<ConstantClass, BuiltinType, Ty,
462 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000463 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000464 if (V2->isNullValue()) return 0;
465 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
466 (BuiltinType)V2->getValue());
467 return ConstantClass::get(*Ty, Result);
468 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000469};
470
Chris Lattner1dd054c2004-01-12 22:07:24 +0000471
472/// ConstRules::get - This method returns the constant rules implementation that
473/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000474ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000475 static EmptyRules EmptyR;
476 static BoolRules BoolR;
477 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000478 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
479 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
480 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
481 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
482 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
483 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
484 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
485 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
486 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
487 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000488
Chris Lattner4b6addf2003-11-17 19:19:32 +0000489 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
490 isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000491 return EmptyR;
492
Chris Lattnerf8348c32004-01-12 20:41:05 +0000493 switch (V1->getType()->getPrimitiveID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000494 default: assert(0 && "Unknown value type for constant folding!");
495 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000496 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000497 case Type::SByteTyID: return SByteR;
498 case Type::UByteTyID: return UByteR;
499 case Type::ShortTyID: return ShortR;
500 case Type::UShortTyID: return UShortR;
501 case Type::IntTyID: return IntR;
502 case Type::UIntTyID: return UIntR;
503 case Type::LongTyID: return LongR;
504 case Type::ULongTyID: return ULongR;
505 case Type::FloatTyID: return FloatR;
506 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000507 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000508}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000509
510
511//===----------------------------------------------------------------------===//
512// ConstantFold*Instruction Implementations
513//===----------------------------------------------------------------------===//
514//
515// These methods contain the special case hackery required to symbolically
516// evaluate some constant expression cases, and use the ConstantRules class to
517// evaluate normal constants.
518//
519static unsigned getSize(const Type *Ty) {
520 unsigned S = Ty->getPrimitiveSize();
521 return S ? S : 8; // Treat pointers at 8 bytes
522}
523
524Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
525 const Type *DestTy) {
526 if (V->getType() == DestTy) return (Constant*)V;
527
528 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
529 if (CE->getOpcode() == Instruction::Cast) {
530 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
531 // Try to not produce a cast of a cast, which is almost always redundant.
532 if (!Op->getType()->isFloatingPoint() &&
533 !CE->getType()->isFloatingPoint() &&
534 !DestTy->getType()->isFloatingPoint()) {
535 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
536 unsigned S3 = getSize(DestTy);
537 if (Op->getType() == DestTy && S3 >= S2)
538 return Op;
539 if (S1 >= S2 && S2 >= S3)
540 return ConstantExpr::getCast(Op, DestTy);
541 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
542 return ConstantExpr::getCast(Op, DestTy);
543 }
544 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
545 // If all of the indexes in the GEP are null values, there is no pointer
546 // adjustment going on. We might as well cast the source pointer.
547 bool isAllNull = true;
548 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
549 if (!CE->getOperand(i)->isNullValue()) {
550 isAllNull = false;
551 break;
552 }
553 if (isAllNull)
554 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
555 }
556
557 ConstRules &Rules = ConstRules::get(V, V);
558
559 switch (DestTy->getPrimitiveID()) {
560 case Type::BoolTyID: return Rules.castToBool(V);
561 case Type::UByteTyID: return Rules.castToUByte(V);
562 case Type::SByteTyID: return Rules.castToSByte(V);
563 case Type::UShortTyID: return Rules.castToUShort(V);
564 case Type::ShortTyID: return Rules.castToShort(V);
565 case Type::UIntTyID: return Rules.castToUInt(V);
566 case Type::IntTyID: return Rules.castToInt(V);
567 case Type::ULongTyID: return Rules.castToULong(V);
568 case Type::LongTyID: return Rules.castToLong(V);
569 case Type::FloatTyID: return Rules.castToFloat(V);
570 case Type::DoubleTyID: return Rules.castToDouble(V);
571 case Type::PointerTyID:
572 return Rules.castToPointer(V, cast<PointerType>(DestTy));
573 default: return 0;
574 }
575}
576
Chris Lattner061da2f2004-01-13 05:51:55 +0000577/// IdxCompare - Compare the two constants as though they were getelementptr
578/// indices. This allows coersion of the types to be the same thing.
579///
580/// If the two constants are the "same" (after coersion), return 0. If the
581/// first is less than the second, return -1, if the second is less than the
582/// first, return 1. If the constants are not integral, return -2.
583///
584static int IdxCompare(Constant *C1, Constant *C2) {
585 if (C1 == C2) return 0;
586
587 // Ok, we found a different index. Are either of the operands
588 // ConstantExprs? If so, we can't do anything with them.
589 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
590 return -2; // don't know!
591
592 // Ok, we have two differing integer indices. Convert them to
593 // be the same type. Long is always big enough, so we use it.
594 C1 = ConstantExpr::getCast(C1, Type::LongTy);
595 C2 = ConstantExpr::getCast(C2, Type::LongTy);
596 if (C1 == C2) return 0; // Are they just differing types?
597
598 // If they are really different, now that they are the same type, then we
599 // found a difference!
600 if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
601 return -1;
602 else
603 return 1;
604}
605
606/// evaluateRelation - This function determines if there is anything we can
607/// decide about the two constants provided. This doesn't need to handle simple
608/// things like integer comparisons, but should instead handle ConstantExpr's
609/// and ConstantPointerRef's. If we can determine that the two constants have a
610/// particular relation to each other, we should return the corresponding SetCC
611/// code, otherwise return Instruction::BinaryOpsEnd.
612///
613/// To simplify this code we canonicalize the relation so that the first
614/// operand is always the most "complex" of the two. We consider simple
615/// constants (like ConstantInt) to be the simplest, followed by
616/// ConstantPointerRef's, followed by ConstantExpr's (the most complex).
617///
618static Instruction::BinaryOps evaluateRelation(const Constant *V1,
619 const Constant *V2) {
620 assert(V1->getType() == V2->getType() &&
621 "Cannot compare different types of values!");
622 if (V1 == V2) return Instruction::SetEQ;
623
624 if (!isa<ConstantExpr>(V1) && !isa<ConstantPointerRef>(V1)) {
625 // If the first operand is simple, swap operands.
626 assert((isa<ConstantPointerRef>(V2) || isa<ConstantExpr>(V2)) &&
627 "Simple cases should have been handled by caller!");
628 return SetCondInst::getSwappedCondition(evaluateRelation(V2, V1));
629
630 } else if (const ConstantPointerRef *CPR1 = dyn_cast<ConstantPointerRef>(V1)){
631 if (isa<ConstantExpr>(V2)) // Swap as necessary.
632 return SetCondInst::getSwappedCondition(evaluateRelation(V2, V1));
633
634 // Now we know that the RHS is a ConstantPointerRef or simple constant,
635 // which (since the types must match) means that it's a ConstantPointerNull.
636 if (const ConstantPointerRef *CPR2 = dyn_cast<ConstantPointerRef>(V2)) {
637 assert(CPR1->getValue() != CPR2->getValue() &&
638 "CPRs for the same value exist at different addresses??");
639 // FIXME: If both globals are external weak, they might both be null!
640 return Instruction::SetNE;
641 } else {
642 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
643 // Global can never be null. FIXME: if we implement external weak
644 // linkage, this is not necessarily true!
645 return Instruction::SetNE;
646 }
647
648 } else {
649 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
650 // constantexpr, a CPR, or a simple constant.
651 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
652 Constant *CE1Op0 = CE1->getOperand(0);
653
654 switch (CE1->getOpcode()) {
655 case Instruction::Cast:
656 // If the cast is not actually changing bits, and the second operand is a
657 // null pointer, do the comparison with the pre-casted value.
658 if (V2->isNullValue() &&
659 CE1->getType()->isLosslesslyConvertibleTo(CE1Op0->getType()))
660 return evaluateRelation(CE1Op0,
661 Constant::getNullValue(CE1Op0->getType()));
662
663 case Instruction::GetElementPtr:
664 // Ok, since this is a getelementptr, we know that the constant has a
665 // pointer type. Check the various cases.
666 if (isa<ConstantPointerNull>(V2)) {
667 // If we are comparing a GEP to a null pointer, check to see if the base
668 // of the GEP equals the null pointer.
669 if (isa<ConstantPointerRef>(CE1Op0)) {
670 // FIXME: this is not true when we have external weak references!
671 // No offset can go from a global to a null pointer.
672 return Instruction::SetGT;
673 } else if (isa<ConstantPointerNull>(CE1Op0)) {
674 // If we are indexing from a null pointer, check to see if we have any
675 // non-zero indices.
676 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
677 if (!CE1->getOperand(i)->isNullValue())
678 // Offsetting from null, must not be equal.
679 return Instruction::SetGT;
680 // Only zero indexes from null, must still be zero.
681 return Instruction::SetEQ;
682 }
683 // Otherwise, we can't really say if the first operand is null or not.
684 } else if (const ConstantPointerRef *CPR2 =
685 dyn_cast<ConstantPointerRef>(V2)) {
686 if (isa<ConstantPointerNull>(CE1Op0)) {
687 // FIXME: This is not true with external weak references.
688 return Instruction::SetLT;
689 } else if (const ConstantPointerRef *CPR1 =
690 dyn_cast<ConstantPointerRef>(CE1Op0)) {
691 if (CPR1 == CPR2) {
692 // If this is a getelementptr of the same global, then it must be
693 // different. Because the types must match, the getelementptr could
694 // only have at most one index, and because we fold getelementptr's
695 // with a single zero index, it must be nonzero.
696 assert(CE1->getNumOperands() == 2 &&
697 !CE1->getOperand(1)->isNullValue() &&
698 "Suprising getelementptr!");
699 return Instruction::SetGT;
700 } else {
701 // If they are different globals, we don't know what the value is,
702 // but they can't be equal.
703 return Instruction::SetNE;
704 }
705 }
706 } else {
707 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
708 const Constant *CE2Op0 = CE2->getOperand(0);
709
710 // There are MANY other foldings that we could perform here. They will
711 // probably be added on demand, as they seem needed.
712 switch (CE2->getOpcode()) {
713 default: break;
714 case Instruction::GetElementPtr:
715 // By far the most common case to handle is when the base pointers are
716 // obviously to the same or different globals.
717 if (isa<ConstantPointerRef>(CE1Op0) &&
718 isa<ConstantPointerRef>(CE2Op0)) {
719 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
720 return Instruction::SetNE;
721 // Ok, we know that both getelementptr instructions are based on the
722 // same global. From this, we can precisely determine the relative
723 // ordering of the resultant pointers.
724 unsigned i = 1;
725
726 // Compare all of the operands the GEP's have in common.
727 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); ++i)
728 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i))) {
729 case -1: return Instruction::SetLT;
730 case 1: return Instruction::SetGT;
731 case -2: return Instruction::BinaryOpsEnd;
732 }
733
734 // Ok, we ran out of things they have in common. If any leftovers
735 // are non-zero then we have a difference, otherwise we are equal.
736 for (; i < CE1->getNumOperands(); ++i)
737 if (!CE1->getOperand(i)->isNullValue())
738 return Instruction::SetGT;
739 for (; i < CE2->getNumOperands(); ++i)
740 if (!CE2->getOperand(i)->isNullValue())
741 return Instruction::SetLT;
742 return Instruction::SetEQ;
743 }
744 }
745 }
746
747 default:
748 break;
749 }
750 }
751
752 return Instruction::BinaryOpsEnd;
753}
754
Chris Lattner1dd054c2004-01-12 22:07:24 +0000755Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
756 const Constant *V1,
757 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000758 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000759 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000760 default: break;
761 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
762 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
763 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
764 case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break;
765 case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break;
766 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
767 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
768 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
769 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
770 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
771 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
772 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
773 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000774 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
775 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner061da2f2004-01-13 05:51:55 +0000776 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000777 break;
778 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
779 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner061da2f2004-01-13 05:51:55 +0000780 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000781 break;
782 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
783 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner061da2f2004-01-13 05:51:55 +0000784 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000785 break;
786 }
787
Chris Lattner061da2f2004-01-13 05:51:55 +0000788 // If we successfully folded the expression, return it now.
789 if (C) return C;
790
791 if (SetCondInst::isRelational(Opcode))
792 switch (evaluateRelation(V1, V2)) {
793 default: assert(0 && "Unknown relational!");
794 case Instruction::BinaryOpsEnd:
795 break; // Couldn't determine anything about these constants.
796 case Instruction::SetEQ: // We know the constants are equal!
797 // If we know the constants are equal, we can decide the result of this
798 // computation precisely.
799 return ConstantBool::get(Opcode == Instruction::SetEQ ||
800 Opcode == Instruction::SetLE ||
801 Opcode == Instruction::SetGE);
802 case Instruction::SetLT:
803 // If we know that V1 < V2, we can decide the result of this computation
804 // precisely.
805 return ConstantBool::get(Opcode == Instruction::SetLT ||
806 Opcode == Instruction::SetNE ||
807 Opcode == Instruction::SetLE);
808 case Instruction::SetGT:
809 // If we know that V1 > V2, we can decide the result of this computation
810 // precisely.
811 return ConstantBool::get(Opcode == Instruction::SetGT ||
812 Opcode == Instruction::SetNE ||
813 Opcode == Instruction::SetGE);
814 case Instruction::SetLE:
815 // If we know that V1 <= V2, we can only partially decide this relation.
816 if (Opcode == Instruction::SetGT) return ConstantBool::False;
817 if (Opcode == Instruction::SetLT) return ConstantBool::True;
818 break;
819
820 case Instruction::SetGE:
821 // If we know that V1 >= V2, we can only partially decide this relation.
822 if (Opcode == Instruction::SetLT) return ConstantBool::False;
823 if (Opcode == Instruction::SetGT) return ConstantBool::True;
824 break;
825
826 case Instruction::SetNE:
827 // If we know that V1 != V2, we can only partially decide this relation.
828 if (Opcode == Instruction::SetEQ) return ConstantBool::False;
829 if (Opcode == Instruction::SetNE) return ConstantBool::True;
830 break;
831 }
832
833 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
834 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
835 // There are many possible foldings we could do here. We should probably
836 // at least fold add of a pointer with an integer into the appropriate
837 // getelementptr. This will improve alias analysis a bit.
838
839
840
841
842 } else {
843 // Just implement a couple of simple identities.
844 switch (Opcode) {
845 case Instruction::Add:
846 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
847 break;
848 case Instruction::Sub:
849 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
850 break;
851 case Instruction::Mul:
852 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
853 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
854 if (CI->getRawValue() == 1)
855 return const_cast<Constant*>(V1); // X * 1 == X
856 break;
857 case Instruction::Div:
858 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
859 if (CI->getRawValue() == 1)
860 return const_cast<Constant*>(V1); // X / 1 == X
861 break;
862 case Instruction::Rem:
863 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
864 if (CI->getRawValue() == 1)
865 return Constant::getNullValue(CI->getType()); // X % 1 == 0
866 break;
867 case Instruction::And:
868 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
869 return const_cast<Constant*>(V1); // X & -1 == X
870 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
871 break;
872 case Instruction::Or:
873 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
874 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
875 return const_cast<Constant*>(V2); // X | -1 == -1
876 break;
877 case Instruction::Xor:
878 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
879 break;
880 }
881 }
882
883 } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
884 // If V2 is a constant expr and V1 isn't, flop them around and fold the
885 // other way if possible.
886 switch (Opcode) {
887 case Instruction::Add:
888 case Instruction::Mul:
889 case Instruction::And:
890 case Instruction::Or:
891 case Instruction::Xor:
892 case Instruction::SetEQ:
893 case Instruction::SetNE:
894 // No change of opcode required.
895 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
896
897 case Instruction::SetLT:
898 case Instruction::SetGT:
899 case Instruction::SetLE:
900 case Instruction::SetGE:
901 // Change the opcode as necessary to swap the operands.
902 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
903 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
904
905 case Instruction::Shl:
906 case Instruction::Shr:
907 case Instruction::Sub:
908 case Instruction::Div:
909 case Instruction::Rem:
910 default: // These instructions cannot be flopped around.
911 break;
912 }
913 }
914 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000915}
916
917Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
918 const std::vector<Constant*> &IdxList) {
919 if (IdxList.size() == 0 ||
920 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
921 return const_cast<Constant*>(C);
922
923 // TODO If C is null and all idx's are null, return null of the right type.
924
925
926 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
927 // Combine Indices - If the source pointer to this getelementptr instruction
928 // is a getelementptr instruction, combine the indices of the two
929 // getelementptr instructions into a single instruction.
930 //
931 if (CE->getOpcode() == Instruction::GetElementPtr) {
932 const Type *LastTy = 0;
933 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
934 I != E; ++I)
935 LastTy = *I;
936
937 if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) {
938 std::vector<Constant*> NewIndices;
939 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
940 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
941 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
942
943 // Add the last index of the source with the first index of the new GEP.
944 // Make sure to handle the case when they are actually different types.
945 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
946 if (!IdxList[0]->isNullValue()) // Otherwise it must be an array
947 Combined =
948 ConstantExpr::get(Instruction::Add,
949 ConstantExpr::getCast(IdxList[0], Type::LongTy),
950 ConstantExpr::getCast(Combined, Type::LongTy));
951
952 NewIndices.push_back(Combined);
953 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
954 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
955 }
956 }
957
958 // Implement folding of:
959 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
960 // long 0, long 0)
961 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
962 //
963 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
964 IdxList[0]->isNullValue())
965 if (const PointerType *SPT =
966 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
967 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
968 if (const ArrayType *CAT =
969 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
970 if (CAT->getElementType() == SAT->getElementType())
971 return ConstantExpr::getGetElementPtr(
972 (Constant*)CE->getOperand(0), IdxList);
973 }
974 return 0;
975}
976