blob: 98fcf1a32e43c005d8f8c4aef82ff424a2fdaaf8 [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
11// (internal) ConstantFolding.h interface, which is used by the
12// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner5a945e32004-01-12 21:13:12 +000021#include "ConstantFolding.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattnerad70d4a2003-11-25 21:21:46 +000026#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000027#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000028using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000029
Chris Lattner5a945e32004-01-12 21:13:12 +000030namespace {
31 struct ConstRules {
32 ConstRules() {}
Reid Spencer9c47b252005-04-24 22:27:20 +000033 virtual ~ConstRules() {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000034
Chris Lattner5a945e32004-01-12 21:13:12 +000035 // 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;
Misha Brukmanb1c93172005-04-21 23:48:37 +000063
Chris Lattner5a945e32004-01-12 21:13:12 +000064 // 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//
Misha Brukmanb1c93172005-04-21 23:48:37 +000079// 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
Chris Lattner2f7c9632001-06-06 20:29:01 +000082// and to ConstRules without changing anything else...
Misha Brukmanb1c93172005-04-21 23:48:37 +000083//
Chris Lattner2f7c9632001-06-06 20:29:01 +000084// 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
Reid Spencer9c47b252005-04-24 22:27:20 +000090
Chris Lattner2f7c9632001-06-06 20:29:01 +000091 //===--------------------------------------------------------------------===//
92 // Redirecting functions that cast to the appropriate types
93 //===--------------------------------------------------------------------===//
94
Misha Brukmanb1c93172005-04-21 23:48:37 +000095 virtual Constant *add(const Constant *V1, const Constant *V2) const {
96 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000098 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
99 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
102 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000103 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000104 virtual Constant *div(const Constant *V1, const Constant *V2) const {
105 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
Chris Lattneraf259a72002-04-07 08:10:14 +0000106 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000107 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
108 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000109 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000110 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
111 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000112 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000113 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
114 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000115 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000116 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
117 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000118 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000119 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
120 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000121 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
123 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000124 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000125
Misha Brukmanb1c93172005-04-21 23:48:37 +0000126 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
128 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000129 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000130 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
131 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132
Chris Lattner55406842001-07-21 19:10:49 +0000133 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000134 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000135 return SubClassName::CastToBool((const ArgType*)V);
136 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000137 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000138 return SubClassName::CastToSByte((const ArgType*)V);
139 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000140 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000141 return SubClassName::CastToUByte((const ArgType*)V);
142 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000143 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000144 return SubClassName::CastToShort((const ArgType*)V);
145 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000146 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000147 return SubClassName::CastToUShort((const ArgType*)V);
148 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000149 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000150 return SubClassName::CastToInt((const ArgType*)V);
151 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000152 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000153 return SubClassName::CastToUInt((const ArgType*)V);
154 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000155 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000156 return SubClassName::CastToLong((const ArgType*)V);
157 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000158 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000159 return SubClassName::CastToULong((const ArgType*)V);
160 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000161 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000162 return SubClassName::CastToFloat((const ArgType*)V);
163 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000164 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000165 return SubClassName::CastToDouble((const ArgType*)V);
166 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000167 virtual Constant *castToPointer(const Constant *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000168 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000169 return SubClassName::CastToPointer((const ArgType*)V, Ty);
170 }
Chris Lattner55406842001-07-21 19:10:49 +0000171
Chris Lattner2f7c9632001-06-06 20:29:01 +0000172 //===--------------------------------------------------------------------===//
173 // Default "noop" implementations
174 //===--------------------------------------------------------------------===//
175
Chris Lattnere87f65e2002-07-30 16:24:28 +0000176 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
177 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
178 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
179 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
180 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
181 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
182 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
183 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
184 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
185 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000186 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187 return 0;
188 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000189 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000190 return 0;
191 }
Chris Lattner55406842001-07-21 19:10:49 +0000192
193 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000194 static Constant *CastToBool (const Constant *V) { return 0; }
195 static Constant *CastToSByte (const Constant *V) { return 0; }
196 static Constant *CastToUByte (const Constant *V) { return 0; }
197 static Constant *CastToShort (const Constant *V) { return 0; }
198 static Constant *CastToUShort(const Constant *V) { return 0; }
199 static Constant *CastToInt (const Constant *V) { return 0; }
200 static Constant *CastToUInt (const Constant *V) { return 0; }
201 static Constant *CastToLong (const Constant *V) { return 0; }
202 static Constant *CastToULong (const Constant *V) { return 0; }
203 static Constant *CastToFloat (const Constant *V) { return 0; }
204 static Constant *CastToDouble(const Constant *V) { return 0; }
205 static Constant *CastToPointer(const Constant *,
206 const PointerType *) {return 0;}
Reid Spencer9c47b252005-04-24 22:27:20 +0000207
208public:
209 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210};
211
212
213
214//===----------------------------------------------------------------------===//
215// EmptyRules Class
216//===----------------------------------------------------------------------===//
217//
218// EmptyRules provides a concrete base class of ConstRules that does nothing
219//
Chris Lattner3462ae32001-12-03 22:26:30 +0000220struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000221 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000222 if (V1 == V2) return ConstantBool::True;
223 return 0;
224 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000225};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226
227
228
229//===----------------------------------------------------------------------===//
230// BoolRules Class
231//===----------------------------------------------------------------------===//
232//
233// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
234//
Chris Lattner3462ae32001-12-03 22:26:30 +0000235struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000237 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){
Chris Lattner07507a42002-09-03 20:09:49 +0000238 return ConstantBool::get(V1->getValue() < V2->getValue());
239 }
240
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000241 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000242 return ConstantBool::get(V1 == V2);
243 }
244
Chris Lattnere87f65e2002-07-30 16:24:28 +0000245 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
246 return ConstantBool::get(V1->getValue() & V2->getValue());
247 }
248
249 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000250 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251 }
252
Chris Lattnere87f65e2002-07-30 16:24:28 +0000253 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
254 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000256
257 // Casting operators. ick
258#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000259 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000260 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
261 }
262
263 DEF_CAST(Bool , ConstantBool, bool)
264 DEF_CAST(SByte , ConstantSInt, signed char)
265 DEF_CAST(UByte , ConstantUInt, unsigned char)
266 DEF_CAST(Short , ConstantSInt, signed short)
267 DEF_CAST(UShort, ConstantUInt, unsigned short)
268 DEF_CAST(Int , ConstantSInt, signed int)
269 DEF_CAST(UInt , ConstantUInt, unsigned int)
270 DEF_CAST(Long , ConstantSInt, int64_t)
271 DEF_CAST(ULong , ConstantUInt, uint64_t)
272 DEF_CAST(Float , ConstantFP , float)
273 DEF_CAST(Double, ConstantFP , double)
274#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000275};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276
277
278//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000279// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000280//===----------------------------------------------------------------------===//
281//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000282// NullPointerRules provides a concrete base class of ConstRules for null
283// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000284//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000285struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000286 NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000287 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000288 return ConstantBool::True; // Null pointers are always equal
289 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000290 static Constant *CastToBool(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000291 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000292 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000293 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000294 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000295 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000296 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000297 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000298 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000299 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000300 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000301 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000302 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000303 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000304 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000305 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000306 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000307 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000308 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000309 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000310 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000311 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000312 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000313 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000314 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000315 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000316 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000317 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000318 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000319 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000320 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000321 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000322 }
323
Chris Lattner77f20dc2003-11-17 19:21:04 +0000324 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000325 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000326 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000327 }
328};
329
330
331//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332// DirectRules Class
333//===----------------------------------------------------------------------===//
334//
335// DirectRules provides a concrete base classes of ConstRules for a variety of
336// different types. This allows the C++ compiler to automatically generate our
337// constant handling operations in a typesafe and accurate manner.
338//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000339template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
340struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000341 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
342 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
343 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344 }
345
Chris Lattnere87f65e2002-07-30 16:24:28 +0000346 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
347 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
348 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000349 }
350
Chris Lattnere87f65e2002-07-30 16:24:28 +0000351 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
352 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
353 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000354 }
355
Chris Lattnere87f65e2002-07-30 16:24:28 +0000356 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000357 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000358 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
359 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000360 }
361
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000362 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000363 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
364 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000365 }
Chris Lattner55406842001-07-21 19:10:49 +0000366
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000367 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000368 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
369 return ConstantBool::get(R);
370 }
371
Chris Lattner1f0049c2003-04-17 19:24:18 +0000372 static Constant *CastToPointer(const ConstantClass *V,
373 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000374 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000375 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000376 return 0; // Can't const prop other types of pointers
377 }
378
Chris Lattner55406842001-07-21 19:10:49 +0000379 // Casting operators. ick
380#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000381 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000382 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000383 }
384
Chris Lattner3462ae32001-12-03 22:26:30 +0000385 DEF_CAST(Bool , ConstantBool, bool)
386 DEF_CAST(SByte , ConstantSInt, signed char)
387 DEF_CAST(UByte , ConstantUInt, unsigned char)
388 DEF_CAST(Short , ConstantSInt, signed short)
389 DEF_CAST(UShort, ConstantUInt, unsigned short)
390 DEF_CAST(Int , ConstantSInt, signed int)
391 DEF_CAST(UInt , ConstantUInt, unsigned int)
392 DEF_CAST(Long , ConstantSInt, int64_t)
393 DEF_CAST(ULong , ConstantUInt, uint64_t)
394 DEF_CAST(Float , ConstantFP , float)
395 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000396#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000397};
398
Chris Lattner62af86e2002-05-03 20:09:52 +0000399
400//===----------------------------------------------------------------------===//
401// DirectIntRules Class
402//===----------------------------------------------------------------------===//
403//
404// DirectIntRules provides implementations of functions that are valid on
405// integer types, but not all types in general.
406//
407template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000408struct DirectIntRules
409 : public DirectRules<ConstantClass, BuiltinType, Ty,
410 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000411
Chris Lattner268916262003-05-12 15:26:25 +0000412 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
413 if (V2->isNullValue()) return 0;
414 if (V2->isAllOnesValue() && // MIN_INT / -1
415 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
416 return 0;
417 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
418 return ConstantClass::get(*Ty, R);
419 }
420
Chris Lattnere87f65e2002-07-30 16:24:28 +0000421 static Constant *Rem(const ConstantClass *V1,
422 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000423 if (V2->isNullValue()) return 0; // X / 0
424 if (V2->isAllOnesValue() && // MIN_INT / -1
425 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
426 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000427 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
428 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000429 }
Chris Lattner6670d862002-05-06 03:00:54 +0000430
Chris Lattnere87f65e2002-07-30 16:24:28 +0000431 static Constant *And(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 *Or(const ConstantClass *V1, const ConstantClass *V2) {
436 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
437 return ConstantClass::get(*Ty, R);
438 }
439 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
440 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
441 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000442 }
443
Chris Lattnere87f65e2002-07-30 16:24:28 +0000444 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
445 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
446 return ConstantClass::get(*Ty, R);
447 }
448
449 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
450 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
451 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000452 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000453};
454
455
456//===----------------------------------------------------------------------===//
457// DirectFPRules Class
458//===----------------------------------------------------------------------===//
459//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000460/// DirectFPRules provides implementations of functions that are valid on
461/// floating point types, but not all types in general.
462///
Chris Lattner0a144ad2002-05-03 21:41:07 +0000463template <class ConstantClass, class BuiltinType, Type **Ty>
464struct DirectFPRules
465 : public DirectRules<ConstantClass, BuiltinType, Ty,
466 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000467 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000468 if (V2->isNullValue()) return 0;
469 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
470 (BuiltinType)V2->getValue());
471 return ConstantClass::get(*Ty, Result);
472 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000473};
474
Chris Lattner1dd054c2004-01-12 22:07:24 +0000475
476/// ConstRules::get - This method returns the constant rules implementation that
477/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000478ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000479 static EmptyRules EmptyR;
480 static BoolRules BoolR;
481 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000482 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
483 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
484 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
485 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
486 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
487 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
488 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
489 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
490 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
491 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000492
Chris Lattner4b6addf2003-11-17 19:19:32 +0000493 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000494 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
495 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000496 return EmptyR;
497
Chris Lattner6b727592004-06-17 18:19:28 +0000498 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000499 default: assert(0 && "Unknown value type for constant folding!");
500 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000501 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000502 case Type::SByteTyID: return SByteR;
503 case Type::UByteTyID: return UByteR;
504 case Type::ShortTyID: return ShortR;
505 case Type::UShortTyID: return UShortR;
506 case Type::IntTyID: return IntR;
507 case Type::UIntTyID: return UIntR;
508 case Type::LongTyID: return LongR;
509 case Type::ULongTyID: return ULongR;
510 case Type::FloatTyID: return FloatR;
511 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000512 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000514
515
516//===----------------------------------------------------------------------===//
517// ConstantFold*Instruction Implementations
518//===----------------------------------------------------------------------===//
519//
520// These methods contain the special case hackery required to symbolically
521// evaluate some constant expression cases, and use the ConstantRules class to
522// evaluate normal constants.
523//
524static unsigned getSize(const Type *Ty) {
525 unsigned S = Ty->getPrimitiveSize();
526 return S ? S : 8; // Treat pointers at 8 bytes
527}
528
529Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
530 const Type *DestTy) {
531 if (V->getType() == DestTy) return (Constant*)V;
532
Chris Lattnerea0789c2004-03-08 06:17:35 +0000533 // Cast of a global address to boolean is always true.
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000534 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerea0789c2004-03-08 06:17:35 +0000535 if (DestTy == Type::BoolTy)
536 // FIXME: When we support 'external weak' references, we have to prevent
Chris Lattnercd4003e2005-01-06 16:26:38 +0000537 // this transformation from happening. This code will need to be updated
538 // to ignore external weak symbols when we support it.
539 return ConstantBool::True;
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000540 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000541 if (CE->getOpcode() == Instruction::Cast) {
542 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
543 // Try to not produce a cast of a cast, which is almost always redundant.
544 if (!Op->getType()->isFloatingPoint() &&
545 !CE->getType()->isFloatingPoint() &&
Reid Spencer8eb06df2004-05-30 01:19:48 +0000546 !DestTy->isFloatingPoint()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000547 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
548 unsigned S3 = getSize(DestTy);
549 if (Op->getType() == DestTy && S3 >= S2)
550 return Op;
551 if (S1 >= S2 && S2 >= S3)
552 return ConstantExpr::getCast(Op, DestTy);
553 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
554 return ConstantExpr::getCast(Op, DestTy);
555 }
556 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
557 // If all of the indexes in the GEP are null values, there is no pointer
558 // adjustment going on. We might as well cast the source pointer.
559 bool isAllNull = true;
560 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
561 if (!CE->getOperand(i)->isNullValue()) {
562 isAllNull = false;
563 break;
564 }
565 if (isAllNull)
566 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
567 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000568 } else if (isa<UndefValue>(V)) {
569 return UndefValue::get(DestTy);
570 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000571
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000572 // Check to see if we are casting an pointer to an aggregate to a pointer to
573 // the first element. If so, return the appropriate GEP instruction.
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000574 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000575 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
576 std::vector<Value*> IdxList;
577 IdxList.push_back(Constant::getNullValue(Type::IntTy));
578 const Type *ElTy = PTy->getElementType();
579 while (ElTy != DPTy->getElementType()) {
580 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
Chris Lattner9e907202004-11-22 19:15:27 +0000581 if (STy->getNumElements() == 0) break;
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000582 ElTy = STy->getElementType(0);
583 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
584 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
585 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
586 ElTy = STy->getElementType();
587 IdxList.push_back(IdxList[0]);
588 } else {
589 break;
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000590 }
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000591 }
592
593 if (ElTy == DPTy->getElementType())
594 return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
595 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000596
Chris Lattner1dd054c2004-01-12 22:07:24 +0000597 ConstRules &Rules = ConstRules::get(V, V);
598
Chris Lattner6b727592004-06-17 18:19:28 +0000599 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000600 case Type::BoolTyID: return Rules.castToBool(V);
601 case Type::UByteTyID: return Rules.castToUByte(V);
602 case Type::SByteTyID: return Rules.castToSByte(V);
603 case Type::UShortTyID: return Rules.castToUShort(V);
604 case Type::ShortTyID: return Rules.castToShort(V);
605 case Type::UIntTyID: return Rules.castToUInt(V);
606 case Type::IntTyID: return Rules.castToInt(V);
607 case Type::ULongTyID: return Rules.castToULong(V);
608 case Type::LongTyID: return Rules.castToLong(V);
609 case Type::FloatTyID: return Rules.castToFloat(V);
610 case Type::DoubleTyID: return Rules.castToDouble(V);
611 case Type::PointerTyID:
612 return Rules.castToPointer(V, cast<PointerType>(DestTy));
613 default: return 0;
614 }
615}
616
Chris Lattner6ea4b522004-03-12 05:53:32 +0000617Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
618 const Constant *V1,
619 const Constant *V2) {
620 if (Cond == ConstantBool::True)
621 return const_cast<Constant*>(V1);
622 else if (Cond == ConstantBool::False)
623 return const_cast<Constant*>(V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000624
625 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
626 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
627 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000628 return 0;
629}
630
Chris Lattner60c47262005-01-28 19:09:51 +0000631/// isZeroSizedType - This type is zero sized if its an array or structure of
632/// zero sized types. The only leaf zero sized type is an empty structure.
633static bool isMaybeZeroSizedType(const Type *Ty) {
634 if (isa<OpaqueType>(Ty)) return true; // Can't say.
635 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
636
637 // If all of elements have zero size, this does too.
638 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000639 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000640 return true;
641
642 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
643 return isMaybeZeroSizedType(ATy->getElementType());
644 }
645 return false;
646}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000647
Chris Lattner061da2f2004-01-13 05:51:55 +0000648/// IdxCompare - Compare the two constants as though they were getelementptr
649/// indices. This allows coersion of the types to be the same thing.
650///
651/// If the two constants are the "same" (after coersion), return 0. If the
652/// first is less than the second, return -1, if the second is less than the
653/// first, return 1. If the constants are not integral, return -2.
654///
Chris Lattner60c47262005-01-28 19:09:51 +0000655static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000656 if (C1 == C2) return 0;
657
658 // Ok, we found a different index. Are either of the operands
659 // ConstantExprs? If so, we can't do anything with them.
660 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
661 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000662
Chris Lattner69193f92004-04-05 01:30:19 +0000663 // Ok, we have two differing integer indices. Sign extend them to be the same
664 // type. Long is always big enough, so we use it.
665 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
666 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000667 if (C1 == C2) return 0; // Are they just differing types?
668
Chris Lattner60c47262005-01-28 19:09:51 +0000669 // If the type being indexed over is really just a zero sized type, there is
670 // no pointer difference being made here.
671 if (isMaybeZeroSizedType(ElTy))
672 return -2; // dunno.
673
Chris Lattner061da2f2004-01-13 05:51:55 +0000674 // If they are really different, now that they are the same type, then we
675 // found a difference!
676 if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
677 return -1;
678 else
679 return 1;
680}
681
682/// evaluateRelation - This function determines if there is anything we can
683/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000684/// things like integer comparisons, but should instead handle ConstantExprs
685/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +0000686/// particular relation to each other, we should return the corresponding SetCC
687/// code, otherwise return Instruction::BinaryOpsEnd.
688///
689/// To simplify this code we canonicalize the relation so that the first
690/// operand is always the most "complex" of the two. We consider simple
691/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000692/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000693///
694static Instruction::BinaryOps evaluateRelation(const Constant *V1,
695 const Constant *V2) {
696 assert(V1->getType() == V2->getType() &&
697 "Cannot compare different types of values!");
698 if (V1 == V2) return Instruction::SetEQ;
699
Reid Spenceraccd7c72004-07-17 23:47:01 +0000700 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000701 // If the first operand is simple, swap operands.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000702 assert((isa<GlobalValue>(V2) || isa<ConstantExpr>(V2)) &&
Chris Lattner061da2f2004-01-13 05:51:55 +0000703 "Simple cases should have been handled by caller!");
Chris Lattner125ed542004-02-01 01:23:19 +0000704 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
705 if (SwappedRelation != Instruction::BinaryOpsEnd)
706 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000707
Reid Spenceraccd7c72004-07-17 23:47:01 +0000708 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)){
Chris Lattner125ed542004-02-01 01:23:19 +0000709 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
710 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
711 if (SwappedRelation != Instruction::BinaryOpsEnd)
712 return SetCondInst::getSwappedCondition(SwappedRelation);
713 else
714 return Instruction::BinaryOpsEnd;
715 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000716
Reid Spenceraccd7c72004-07-17 23:47:01 +0000717 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000718 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000719 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
720 assert(CPR1 != CPR2 &&
721 "GVs for the same value exist at different addresses??");
Chris Lattner061da2f2004-01-13 05:51:55 +0000722 // FIXME: If both globals are external weak, they might both be null!
723 return Instruction::SetNE;
724 } else {
725 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
726 // Global can never be null. FIXME: if we implement external weak
727 // linkage, this is not necessarily true!
728 return Instruction::SetNE;
729 }
730
731 } else {
732 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
733 // constantexpr, a CPR, or a simple constant.
734 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
735 Constant *CE1Op0 = CE1->getOperand(0);
736
737 switch (CE1->getOpcode()) {
738 case Instruction::Cast:
739 // If the cast is not actually changing bits, and the second operand is a
740 // null pointer, do the comparison with the pre-casted value.
741 if (V2->isNullValue() &&
742 CE1->getType()->isLosslesslyConvertibleTo(CE1Op0->getType()))
743 return evaluateRelation(CE1Op0,
744 Constant::getNullValue(CE1Op0->getType()));
Chris Lattner192e3262004-04-11 01:29:30 +0000745 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000746
747 case Instruction::GetElementPtr:
748 // Ok, since this is a getelementptr, we know that the constant has a
749 // pointer type. Check the various cases.
750 if (isa<ConstantPointerNull>(V2)) {
751 // If we are comparing a GEP to a null pointer, check to see if the base
752 // of the GEP equals the null pointer.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000753 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000754 // FIXME: this is not true when we have external weak references!
755 // No offset can go from a global to a null pointer.
756 return Instruction::SetGT;
757 } else if (isa<ConstantPointerNull>(CE1Op0)) {
758 // If we are indexing from a null pointer, check to see if we have any
759 // non-zero indices.
760 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
761 if (!CE1->getOperand(i)->isNullValue())
762 // Offsetting from null, must not be equal.
763 return Instruction::SetGT;
764 // Only zero indexes from null, must still be zero.
765 return Instruction::SetEQ;
766 }
767 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000768 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000769 if (isa<ConstantPointerNull>(CE1Op0)) {
770 // FIXME: This is not true with external weak references.
771 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000772 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000773 if (CPR1 == CPR2) {
774 // If this is a getelementptr of the same global, then it must be
775 // different. Because the types must match, the getelementptr could
776 // only have at most one index, and because we fold getelementptr's
777 // with a single zero index, it must be nonzero.
778 assert(CE1->getNumOperands() == 2 &&
779 !CE1->getOperand(1)->isNullValue() &&
780 "Suprising getelementptr!");
781 return Instruction::SetGT;
782 } else {
783 // If they are different globals, we don't know what the value is,
784 // but they can't be equal.
785 return Instruction::SetNE;
786 }
787 }
788 } else {
789 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
790 const Constant *CE2Op0 = CE2->getOperand(0);
791
792 // There are MANY other foldings that we could perform here. They will
793 // probably be added on demand, as they seem needed.
794 switch (CE2->getOpcode()) {
795 default: break;
796 case Instruction::GetElementPtr:
797 // By far the most common case to handle is when the base pointers are
798 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000799 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000800 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
801 return Instruction::SetNE;
802 // Ok, we know that both getelementptr instructions are based on the
803 // same global. From this, we can precisely determine the relative
804 // ordering of the resultant pointers.
805 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000806
Chris Lattner061da2f2004-01-13 05:51:55 +0000807 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +0000808 gep_type_iterator GTI = gep_type_begin(CE1);
809 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
810 ++i, ++GTI)
811 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
812 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000813 case -1: return Instruction::SetLT;
814 case 1: return Instruction::SetGT;
815 case -2: return Instruction::BinaryOpsEnd;
816 }
817
818 // Ok, we ran out of things they have in common. If any leftovers
819 // are non-zero then we have a difference, otherwise we are equal.
820 for (; i < CE1->getNumOperands(); ++i)
821 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +0000822 if (isa<ConstantIntegral>(CE1->getOperand(i)))
823 return Instruction::SetGT;
824 else
825 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000826
Chris Lattner061da2f2004-01-13 05:51:55 +0000827 for (; i < CE2->getNumOperands(); ++i)
828 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +0000829 if (isa<ConstantIntegral>(CE2->getOperand(i)))
830 return Instruction::SetLT;
831 else
832 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +0000833 return Instruction::SetEQ;
834 }
835 }
836 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000837
Chris Lattner061da2f2004-01-13 05:51:55 +0000838 default:
839 break;
840 }
841 }
842
843 return Instruction::BinaryOpsEnd;
844}
845
Chris Lattner1dd054c2004-01-12 22:07:24 +0000846Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
847 const Constant *V1,
848 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000849 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000850 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000851 default: break;
852 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
853 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
854 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
855 case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break;
856 case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break;
857 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
858 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
859 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
860 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
861 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
862 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
863 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
864 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000865 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
866 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner061da2f2004-01-13 05:51:55 +0000867 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000868 break;
869 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
870 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner061da2f2004-01-13 05:51:55 +0000871 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000872 break;
873 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
874 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner061da2f2004-01-13 05:51:55 +0000875 if (C) return ConstantExpr::get(Instruction::Xor, C, ConstantBool::True);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000876 break;
877 }
878
Chris Lattner061da2f2004-01-13 05:51:55 +0000879 // If we successfully folded the expression, return it now.
880 if (C) return C;
881
Chris Lattner192eacc2004-10-17 04:01:51 +0000882 if (SetCondInst::isRelational(Opcode)) {
883 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
884 return UndefValue::get(Type::BoolTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000885 switch (evaluateRelation(V1, V2)) {
886 default: assert(0 && "Unknown relational!");
887 case Instruction::BinaryOpsEnd:
888 break; // Couldn't determine anything about these constants.
889 case Instruction::SetEQ: // We know the constants are equal!
890 // If we know the constants are equal, we can decide the result of this
891 // computation precisely.
892 return ConstantBool::get(Opcode == Instruction::SetEQ ||
893 Opcode == Instruction::SetLE ||
894 Opcode == Instruction::SetGE);
895 case Instruction::SetLT:
896 // If we know that V1 < V2, we can decide the result of this computation
897 // precisely.
898 return ConstantBool::get(Opcode == Instruction::SetLT ||
899 Opcode == Instruction::SetNE ||
900 Opcode == Instruction::SetLE);
901 case Instruction::SetGT:
902 // If we know that V1 > V2, we can decide the result of this computation
903 // precisely.
904 return ConstantBool::get(Opcode == Instruction::SetGT ||
905 Opcode == Instruction::SetNE ||
906 Opcode == Instruction::SetGE);
907 case Instruction::SetLE:
908 // If we know that V1 <= V2, we can only partially decide this relation.
909 if (Opcode == Instruction::SetGT) return ConstantBool::False;
910 if (Opcode == Instruction::SetLT) return ConstantBool::True;
911 break;
912
913 case Instruction::SetGE:
914 // If we know that V1 >= V2, we can only partially decide this relation.
915 if (Opcode == Instruction::SetLT) return ConstantBool::False;
916 if (Opcode == Instruction::SetGT) return ConstantBool::True;
917 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000918
Chris Lattner061da2f2004-01-13 05:51:55 +0000919 case Instruction::SetNE:
920 // If we know that V1 != V2, we can only partially decide this relation.
921 if (Opcode == Instruction::SetEQ) return ConstantBool::False;
922 if (Opcode == Instruction::SetNE) return ConstantBool::True;
923 break;
924 }
Chris Lattner192eacc2004-10-17 04:01:51 +0000925 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000926
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000927 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
928 switch (Opcode) {
929 case Instruction::Add:
930 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000931 case Instruction::Xor:
932 return UndefValue::get(V1->getType());
933
934 case Instruction::Mul:
935 case Instruction::And:
936 return Constant::getNullValue(V1->getType());
937 case Instruction::Div:
938 case Instruction::Rem:
939 if (!isa<UndefValue>(V2)) // undef/X -> 0
940 return Constant::getNullValue(V1->getType());
941 return const_cast<Constant*>(V2); // X/undef -> undef
942 case Instruction::Or: // X|undef -> -1
943 return ConstantInt::getAllOnesValue(V1->getType());
944 case Instruction::Shr:
945 if (!isa<UndefValue>(V2)) {
946 if (V1->getType()->isSigned())
947 return const_cast<Constant*>(V1); // undef >>s X -> undef
948 // undef >>u X -> 0
949 } else if (isa<UndefValue>(V1)) {
950 return const_cast<Constant*>(V1); // undef >> undef -> undef
951 } else {
952 if (V1->getType()->isSigned())
953 return const_cast<Constant*>(V1); // X >>s undef -> X
954 // X >>u undef -> 0
955 }
956 return Constant::getNullValue(V1->getType());
957
958 case Instruction::Shl:
959 // undef << X -> 0 X << undef -> 0
960 return Constant::getNullValue(V1->getType());
961 }
962 }
963
Chris Lattner061da2f2004-01-13 05:51:55 +0000964 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
965 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
966 // There are many possible foldings we could do here. We should probably
967 // at least fold add of a pointer with an integer into the appropriate
968 // getelementptr. This will improve alias analysis a bit.
969
970
971
972
973 } else {
974 // Just implement a couple of simple identities.
975 switch (Opcode) {
976 case Instruction::Add:
977 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
978 break;
979 case Instruction::Sub:
980 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
981 break;
982 case Instruction::Mul:
983 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
984 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
985 if (CI->getRawValue() == 1)
986 return const_cast<Constant*>(V1); // X * 1 == X
987 break;
988 case Instruction::Div:
989 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
990 if (CI->getRawValue() == 1)
991 return const_cast<Constant*>(V1); // X / 1 == X
992 break;
993 case Instruction::Rem:
994 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
995 if (CI->getRawValue() == 1)
996 return Constant::getNullValue(CI->getType()); // X % 1 == 0
997 break;
998 case Instruction::And:
999 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1000 return const_cast<Constant*>(V1); // X & -1 == X
1001 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Chris Lattnerea0789c2004-03-08 06:17:35 +00001002 if (CE1->getOpcode() == Instruction::Cast &&
Reid Spenceraccd7c72004-07-17 23:47:01 +00001003 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001004 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001005
1006 // Functions are at least 4-byte aligned. If and'ing the address of a
1007 // function with a constant < 4, fold it to zero.
1008 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spenceraccd7c72004-07-17 23:47:01 +00001009 if (CI->getRawValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001010 return Constant::getNullValue(CI->getType());
1011 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001012 break;
1013 case Instruction::Or:
1014 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1015 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1016 return const_cast<Constant*>(V2); // X | -1 == -1
1017 break;
1018 case Instruction::Xor:
1019 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1020 break;
1021 }
1022 }
1023
1024 } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1025 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1026 // other way if possible.
1027 switch (Opcode) {
1028 case Instruction::Add:
1029 case Instruction::Mul:
1030 case Instruction::And:
1031 case Instruction::Or:
1032 case Instruction::Xor:
1033 case Instruction::SetEQ:
1034 case Instruction::SetNE:
1035 // No change of opcode required.
1036 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1037
1038 case Instruction::SetLT:
1039 case Instruction::SetGT:
1040 case Instruction::SetLE:
1041 case Instruction::SetGE:
1042 // Change the opcode as necessary to swap the operands.
1043 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1044 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1045
1046 case Instruction::Shl:
1047 case Instruction::Shr:
1048 case Instruction::Sub:
1049 case Instruction::Div:
1050 case Instruction::Rem:
1051 default: // These instructions cannot be flopped around.
1052 break;
1053 }
1054 }
1055 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001056}
1057
1058Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001059 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001060 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001061 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001062 return const_cast<Constant*>(C);
1063
Chris Lattnerf6013752004-10-17 21:54:55 +00001064 if (isa<UndefValue>(C)) {
1065 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1066 true);
1067 assert(Ty != 0 && "Invalid indices for GEP!");
1068 return UndefValue::get(PointerType::get(Ty));
1069 }
1070
1071 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001072 if (C->isNullValue()) {
1073 bool isNull = true;
1074 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001075 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001076 isNull = false;
1077 break;
1078 }
1079 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001080 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001081 true);
1082 assert(Ty != 0 && "Invalid indices for GEP!");
1083 return ConstantPointerNull::get(PointerType::get(Ty));
1084 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001085
1086 if (IdxList.size() == 1) {
1087 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
1088 if (unsigned ElSize = ElTy->getPrimitiveSize()) {
1089 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1090 // type, we can statically fold this.
1091 Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
Chris Lattner13128ab2004-10-11 22:52:25 +00001092 R = ConstantExpr::getCast(R, Idx0->getType());
1093 R = ConstantExpr::getMul(R, Idx0);
Chris Lattner4bbd4092004-07-15 01:16:59 +00001094 return ConstantExpr::getCast(R, C->getType());
1095 }
1096 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001097 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001098
1099 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1100 // Combine Indices - If the source pointer to this getelementptr instruction
1101 // is a getelementptr instruction, combine the indices of the two
1102 // getelementptr instructions into a single instruction.
1103 //
1104 if (CE->getOpcode() == Instruction::GetElementPtr) {
1105 const Type *LastTy = 0;
1106 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1107 I != E; ++I)
1108 LastTy = *I;
1109
Chris Lattner13128ab2004-10-11 22:52:25 +00001110 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1111 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001112 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1113 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001114 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001115
1116 // Add the last index of the source with the first index of the new GEP.
1117 // Make sure to handle the case when they are actually different types.
1118 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001119 // Otherwise it must be an array.
1120 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001121 const Type *IdxTy = Combined->getType();
Chris Lattner13128ab2004-10-11 22:52:25 +00001122 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001123 Combined =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001124 ConstantExpr::get(Instruction::Add,
Chris Lattner13128ab2004-10-11 22:52:25 +00001125 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattner71068a02004-07-07 04:45:13 +00001126 ConstantExpr::getCast(Combined, IdxTy));
1127 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001128
Chris Lattner1dd054c2004-01-12 22:07:24 +00001129 NewIndices.push_back(Combined);
1130 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1131 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1132 }
1133 }
1134
1135 // Implement folding of:
1136 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1137 // long 0, long 0)
1138 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1139 //
1140 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
Chris Lattner13128ab2004-10-11 22:52:25 +00001141 Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001142 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001143 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1144 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1145 if (const ArrayType *CAT =
1146 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
1147 if (CAT->getElementType() == SAT->getElementType())
1148 return ConstantExpr::getGetElementPtr(
1149 (Constant*)CE->getOperand(0), IdxList);
1150 }
1151 return 0;
1152}
1153