blob: 9139adf9de5e231867826b3a96f0de6e161552c4 [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
11// (internal) ConstantFolding.h interface, which is used by the
12// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner5a945e32004-01-12 21:13:12 +000021#include "ConstantFolding.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattnerad70d4a2003-11-25 21:21:46 +000026#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner6b3f4752006-04-02 01:38:28 +000027#include "llvm/Support/MathExtras.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000029#include <limits>
Chris Lattner0a144ad2002-05-03 21:41:07 +000030#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000031using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000032
Chris Lattner5a945e32004-01-12 21:13:12 +000033namespace {
Chris Lattner02157b02006-06-28 21:38:54 +000034 struct VISIBILITY_HIDDEN ConstRules {
Chris Lattner5a945e32004-01-12 21:13:12 +000035 ConstRules() {}
Reid Spencer9c47b252005-04-24 22:27:20 +000036 virtual ~ConstRules() {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000037
Chris Lattner5a945e32004-01-12 21:13:12 +000038 // Binary Operators...
39 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
40 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
42 virtual Constant *div(const Constant *V1, const Constant *V2) const = 0;
43 virtual Constant *rem(const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
45 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
46 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
47 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
48 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
49 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0;
50 virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0;
51
52 // Casting operators.
53 virtual Constant *castToBool (const Constant *V) const = 0;
54 virtual Constant *castToSByte (const Constant *V) const = 0;
55 virtual Constant *castToUByte (const Constant *V) const = 0;
56 virtual Constant *castToShort (const Constant *V) const = 0;
57 virtual Constant *castToUShort(const Constant *V) const = 0;
58 virtual Constant *castToInt (const Constant *V) const = 0;
59 virtual Constant *castToUInt (const Constant *V) const = 0;
60 virtual Constant *castToLong (const Constant *V) const = 0;
61 virtual Constant *castToULong (const Constant *V) const = 0;
62 virtual Constant *castToFloat (const Constant *V) const = 0;
63 virtual Constant *castToDouble(const Constant *V) const = 0;
64 virtual Constant *castToPointer(const Constant *V,
65 const PointerType *Ty) const = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +000066
Chris Lattner5a945e32004-01-12 21:13:12 +000067 // ConstRules::get - Return an instance of ConstRules for the specified
68 // constant operands.
69 //
70 static ConstRules &get(const Constant *V1, const Constant *V2);
71 private:
72 ConstRules(const ConstRules &); // Do not implement
73 ConstRules &operator=(const ConstRules &); // Do not implement
74 };
75}
76
77
Chris Lattner2f7c9632001-06-06 20:29:01 +000078//===----------------------------------------------------------------------===//
79// TemplateRules Class
80//===----------------------------------------------------------------------===//
81//
Misha Brukmanb1c93172005-04-21 23:48:37 +000082// TemplateRules - Implement a subclass of ConstRules that provides all
83// operations as noops. All other rules classes inherit from this class so
84// that if functionality is needed in the future, it can simply be added here
Chris Lattner2f7c9632001-06-06 20:29:01 +000085// and to ConstRules without changing anything else...
Misha Brukmanb1c93172005-04-21 23:48:37 +000086//
Chris Lattner2f7c9632001-06-06 20:29:01 +000087// This class also provides subclasses with typesafe implementations of methods
88// so that don't have to do type casting.
89//
Chris Lattner6a871e12006-06-21 18:13:36 +000090namespace {
Chris Lattner2f7c9632001-06-06 20:29:01 +000091template<class ArgType, class SubClassName>
Chris Lattner02157b02006-06-28 21:38:54 +000092class VISIBILITY_HIDDEN TemplateRules : public ConstRules {
Chris Lattner2f7c9632001-06-06 20:29:01 +000093
Reid Spencer9c47b252005-04-24 22:27:20 +000094
Chris Lattner2f7c9632001-06-06 20:29:01 +000095 //===--------------------------------------------------------------------===//
96 // Redirecting functions that cast to the appropriate types
97 //===--------------------------------------------------------------------===//
98
Misha Brukmanb1c93172005-04-21 23:48:37 +000099 virtual Constant *add(const Constant *V1, const Constant *V2) const {
100 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000102 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
103 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
106 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000107 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000108 virtual Constant *div(const Constant *V1, const Constant *V2) const {
109 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
Chris Lattneraf259a72002-04-07 08:10:14 +0000110 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000111 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
112 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000113 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000114 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
115 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000116 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000117 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
118 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000119 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000120 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
121 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000122 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
124 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000125 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000126 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
127 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000128 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000129
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
132 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000133 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000134 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
135 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136
Chris Lattner55406842001-07-21 19:10:49 +0000137 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000138 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000139 return SubClassName::CastToBool((const ArgType*)V);
140 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000141 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000142 return SubClassName::CastToSByte((const ArgType*)V);
143 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000144 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000145 return SubClassName::CastToUByte((const ArgType*)V);
146 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000147 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000148 return SubClassName::CastToShort((const ArgType*)V);
149 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000150 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000151 return SubClassName::CastToUShort((const ArgType*)V);
152 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000153 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000154 return SubClassName::CastToInt((const ArgType*)V);
155 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000156 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000157 return SubClassName::CastToUInt((const ArgType*)V);
158 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000159 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000160 return SubClassName::CastToLong((const ArgType*)V);
161 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000162 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000163 return SubClassName::CastToULong((const ArgType*)V);
164 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000165 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000166 return SubClassName::CastToFloat((const ArgType*)V);
167 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000168 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000169 return SubClassName::CastToDouble((const ArgType*)V);
170 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171 virtual Constant *castToPointer(const Constant *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000172 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000173 return SubClassName::CastToPointer((const ArgType*)V, Ty);
174 }
Chris Lattner55406842001-07-21 19:10:49 +0000175
Chris Lattner2f7c9632001-06-06 20:29:01 +0000176 //===--------------------------------------------------------------------===//
177 // Default "noop" implementations
178 //===--------------------------------------------------------------------===//
179
Chris Lattnere87f65e2002-07-30 16:24:28 +0000180 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
181 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
182 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
183 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
184 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
185 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
186 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
187 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
188 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
189 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000190 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 return 0;
192 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000193 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000194 return 0;
195 }
Chris Lattner55406842001-07-21 19:10:49 +0000196
197 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000198 static Constant *CastToBool (const Constant *V) { return 0; }
199 static Constant *CastToSByte (const Constant *V) { return 0; }
200 static Constant *CastToUByte (const Constant *V) { return 0; }
201 static Constant *CastToShort (const Constant *V) { return 0; }
202 static Constant *CastToUShort(const Constant *V) { return 0; }
203 static Constant *CastToInt (const Constant *V) { return 0; }
204 static Constant *CastToUInt (const Constant *V) { return 0; }
205 static Constant *CastToLong (const Constant *V) { return 0; }
206 static Constant *CastToULong (const Constant *V) { return 0; }
207 static Constant *CastToFloat (const Constant *V) { return 0; }
208 static Constant *CastToDouble(const Constant *V) { return 0; }
209 static Constant *CastToPointer(const Constant *,
210 const PointerType *) {return 0;}
Reid Spencer9c47b252005-04-24 22:27:20 +0000211
212public:
213 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214};
Chris Lattner6a871e12006-06-21 18:13:36 +0000215} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216
217
218//===----------------------------------------------------------------------===//
219// EmptyRules Class
220//===----------------------------------------------------------------------===//
221//
222// EmptyRules provides a concrete base class of ConstRules that does nothing
223//
Chris Lattner6a871e12006-06-21 18:13:36 +0000224namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000225struct VISIBILITY_HIDDEN EmptyRules
226 : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000227 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000228 if (V1 == V2) return ConstantBool::getTrue();
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000229 return 0;
230 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000231};
Chris Lattner6a871e12006-06-21 18:13:36 +0000232} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233
234
235
236//===----------------------------------------------------------------------===//
237// BoolRules Class
238//===----------------------------------------------------------------------===//
239//
240// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
241//
Chris Lattner6a871e12006-06-21 18:13:36 +0000242namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000243struct VISIBILITY_HIDDEN BoolRules
244 : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000245
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000246 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner07507a42002-09-03 20:09:49 +0000247 return ConstantBool::get(V1->getValue() < V2->getValue());
248 }
249
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000250 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000251 return ConstantBool::get(V1 == V2);
252 }
253
Chris Lattnere87f65e2002-07-30 16:24:28 +0000254 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
255 return ConstantBool::get(V1->getValue() & V2->getValue());
256 }
257
258 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000259 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 }
261
Chris Lattnere87f65e2002-07-30 16:24:28 +0000262 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
263 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000265
266 // Casting operators. ick
267#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000268 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000269 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
270 }
271
272 DEF_CAST(Bool , ConstantBool, bool)
273 DEF_CAST(SByte , ConstantSInt, signed char)
274 DEF_CAST(UByte , ConstantUInt, unsigned char)
275 DEF_CAST(Short , ConstantSInt, signed short)
276 DEF_CAST(UShort, ConstantUInt, unsigned short)
277 DEF_CAST(Int , ConstantSInt, signed int)
278 DEF_CAST(UInt , ConstantUInt, unsigned int)
279 DEF_CAST(Long , ConstantSInt, int64_t)
280 DEF_CAST(ULong , ConstantUInt, uint64_t)
281 DEF_CAST(Float , ConstantFP , float)
282 DEF_CAST(Double, ConstantFP , double)
283#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000284};
Chris Lattner6a871e12006-06-21 18:13:36 +0000285} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000286
287
288//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000289// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000290//===----------------------------------------------------------------------===//
291//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000292// NullPointerRules provides a concrete base class of ConstRules for null
293// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000294//
Chris Lattner6a871e12006-06-21 18:13:36 +0000295namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000296struct VISIBILITY_HIDDEN NullPointerRules
297 : public TemplateRules<ConstantPointerNull, NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000298 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000299 return ConstantBool::getTrue(); // Null pointers are always equal
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000300 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000301 static Constant *CastToBool(const Constant *V) {
Chris Lattner78430662006-09-28 23:34:49 +0000302 return ConstantBool::getFalse();
Chris Lattner977f0042001-11-01 05:55:13 +0000303 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000304 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000305 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000306 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000307 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000308 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000309 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000310 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000311 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000312 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000313 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000314 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000315 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000316 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000317 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000318 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000319 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000320 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000321 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000322 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000323 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000324 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000325 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000326 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000327 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000328 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000329 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000330 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000331 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000332 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000333 }
334
Chris Lattner77f20dc2003-11-17 19:21:04 +0000335 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000336 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000337 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000338 }
339};
Chris Lattner6a871e12006-06-21 18:13:36 +0000340} // end anonymous namespace
Chris Lattner977f0042001-11-01 05:55:13 +0000341
Chris Lattner1171d952006-01-04 02:03:29 +0000342//===----------------------------------------------------------------------===//
343// ConstantPackedRules Class
344//===----------------------------------------------------------------------===//
345
Chris Lattnerf0f40682006-01-04 02:15:02 +0000346/// DoVectorOp - Given two packed constants and a function pointer, apply the
347/// function pointer to each element pair, producing a new ConstantPacked
348/// constant.
349static Constant *EvalVectorOp(const ConstantPacked *V1,
350 const ConstantPacked *V2,
351 Constant *(*FP)(Constant*, Constant*)) {
352 std::vector<Constant*> Res;
353 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
354 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
355 const_cast<Constant*>(V2->getOperand(i))));
356 return ConstantPacked::get(Res);
357}
358
Chris Lattner1171d952006-01-04 02:03:29 +0000359/// PackedTypeRules provides a concrete base class of ConstRules for
360/// ConstantPacked operands.
361///
Chris Lattner6a871e12006-06-21 18:13:36 +0000362namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000363struct VISIBILITY_HIDDEN ConstantPackedRules
Chris Lattner1171d952006-01-04 02:03:29 +0000364 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerf0f40682006-01-04 02:15:02 +0000365
366 static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
367 return EvalVectorOp(V1, V2, ConstantExpr::getAdd);
368 }
369 static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) {
370 return EvalVectorOp(V1, V2, ConstantExpr::getSub);
371 }
372 static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
373 return EvalVectorOp(V1, V2, ConstantExpr::getMul);
374 }
375 static Constant *Div(const ConstantPacked *V1, const ConstantPacked *V2) {
376 return EvalVectorOp(V1, V2, ConstantExpr::getDiv);
377 }
378 static Constant *Rem(const ConstantPacked *V1, const ConstantPacked *V2) {
379 return EvalVectorOp(V1, V2, ConstantExpr::getRem);
380 }
381 static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) {
382 return EvalVectorOp(V1, V2, ConstantExpr::getAnd);
383 }
384 static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) {
385 return EvalVectorOp(V1, V2, ConstantExpr::getOr);
386 }
387 static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) {
388 return EvalVectorOp(V1, V2, ConstantExpr::getXor);
389 }
390 static Constant *Shl(const ConstantPacked *V1, const ConstantPacked *V2) {
391 return EvalVectorOp(V1, V2, ConstantExpr::getShl);
392 }
393 static Constant *Shr(const ConstantPacked *V1, const ConstantPacked *V2) {
394 return EvalVectorOp(V1, V2, ConstantExpr::getShr);
395 }
396 static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){
397 return 0;
398 }
399 static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) {
Chris Lattner6b52be62006-01-04 02:20:54 +0000400 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) {
401 Constant *C =
402 ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)),
403 const_cast<Constant*>(V2->getOperand(i)));
404 if (ConstantBool *CB = dyn_cast<ConstantBool>(C))
405 return CB;
406 }
407 // Otherwise, could not decide from any element pairs.
Chris Lattnerf0f40682006-01-04 02:15:02 +0000408 return 0;
409 }
Chris Lattner1171d952006-01-04 02:03:29 +0000410};
Chris Lattner6a871e12006-06-21 18:13:36 +0000411} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000412
413
414//===----------------------------------------------------------------------===//
415// GeneralPackedRules Class
416//===----------------------------------------------------------------------===//
417
418/// GeneralPackedRules provides a concrete base class of ConstRules for
419/// PackedType operands, where both operands are not ConstantPacked. The usual
420/// cause for this is that one operand is a ConstantAggregateZero.
421///
Chris Lattner6a871e12006-06-21 18:13:36 +0000422namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000423struct VISIBILITY_HIDDEN GeneralPackedRules
424 : public TemplateRules<Constant, GeneralPackedRules> {
Chris Lattner1171d952006-01-04 02:03:29 +0000425};
Chris Lattner6a871e12006-06-21 18:13:36 +0000426} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000427
Chris Lattner977f0042001-11-01 05:55:13 +0000428
429//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430// DirectRules Class
431//===----------------------------------------------------------------------===//
432//
433// DirectRules provides a concrete base classes of ConstRules for a variety of
434// different types. This allows the C++ compiler to automatically generate our
435// constant handling operations in a typesafe and accurate manner.
436//
Chris Lattner6a871e12006-06-21 18:13:36 +0000437namespace {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000438template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
Chris Lattner02157b02006-06-28 21:38:54 +0000439struct VISIBILITY_HIDDEN DirectRules
440 : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000441 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
442 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
443 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 }
445
Chris Lattnere87f65e2002-07-30 16:24:28 +0000446 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
447 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
448 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 }
450
Chris Lattnere87f65e2002-07-30 16:24:28 +0000451 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
452 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
453 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000454 }
455
Chris Lattnere87f65e2002-07-30 16:24:28 +0000456 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000457 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000458 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
459 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000460 }
461
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000462 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000463 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
464 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000465 }
Chris Lattner55406842001-07-21 19:10:49 +0000466
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000467 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000468 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
469 return ConstantBool::get(R);
470 }
471
Chris Lattner1f0049c2003-04-17 19:24:18 +0000472 static Constant *CastToPointer(const ConstantClass *V,
473 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000474 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000475 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000476 return 0; // Can't const prop other types of pointers
477 }
478
Chris Lattner55406842001-07-21 19:10:49 +0000479 // Casting operators. ick
480#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000481 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000482 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000483 }
484
Chris Lattner3462ae32001-12-03 22:26:30 +0000485 DEF_CAST(Bool , ConstantBool, bool)
486 DEF_CAST(SByte , ConstantSInt, signed char)
487 DEF_CAST(UByte , ConstantUInt, unsigned char)
488 DEF_CAST(Short , ConstantSInt, signed short)
489 DEF_CAST(UShort, ConstantUInt, unsigned short)
490 DEF_CAST(Int , ConstantSInt, signed int)
491 DEF_CAST(UInt , ConstantUInt, unsigned int)
492 DEF_CAST(Long , ConstantSInt, int64_t)
493 DEF_CAST(ULong , ConstantUInt, uint64_t)
494 DEF_CAST(Float , ConstantFP , float)
495 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000496#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000497};
Chris Lattner6a871e12006-06-21 18:13:36 +0000498} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000499
Chris Lattner62af86e2002-05-03 20:09:52 +0000500
501//===----------------------------------------------------------------------===//
502// DirectIntRules Class
503//===----------------------------------------------------------------------===//
504//
505// DirectIntRules provides implementations of functions that are valid on
506// integer types, but not all types in general.
507//
Chris Lattner6a871e12006-06-21 18:13:36 +0000508namespace {
Chris Lattner62af86e2002-05-03 20:09:52 +0000509template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner02157b02006-06-28 21:38:54 +0000510struct VISIBILITY_HIDDEN DirectIntRules
Chris Lattner0a144ad2002-05-03 21:41:07 +0000511 : public DirectRules<ConstantClass, BuiltinType, Ty,
512 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000513
Chris Lattner268916262003-05-12 15:26:25 +0000514 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
515 if (V2->isNullValue()) return 0;
516 if (V2->isAllOnesValue() && // MIN_INT / -1
517 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
518 return 0;
519 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
520 return ConstantClass::get(*Ty, R);
521 }
522
Chris Lattnere87f65e2002-07-30 16:24:28 +0000523 static Constant *Rem(const ConstantClass *V1,
524 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000525 if (V2->isNullValue()) return 0; // X / 0
526 if (V2->isAllOnesValue() && // MIN_INT / -1
527 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
528 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000529 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
530 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000531 }
Chris Lattner6670d862002-05-06 03:00:54 +0000532
Chris Lattnere87f65e2002-07-30 16:24:28 +0000533 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
534 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
535 return ConstantClass::get(*Ty, R);
536 }
537 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
538 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
539 return ConstantClass::get(*Ty, R);
540 }
541 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
542 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
543 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000544 }
545
Chris Lattnere87f65e2002-07-30 16:24:28 +0000546 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
547 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
548 return ConstantClass::get(*Ty, R);
549 }
550
551 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
552 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
553 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000554 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000555};
Chris Lattner6a871e12006-06-21 18:13:36 +0000556} // end anonymous namespace
Chris Lattner0a144ad2002-05-03 21:41:07 +0000557
558
559//===----------------------------------------------------------------------===//
560// DirectFPRules Class
561//===----------------------------------------------------------------------===//
562//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000563/// DirectFPRules provides implementations of functions that are valid on
564/// floating point types, but not all types in general.
565///
Chris Lattner6a871e12006-06-21 18:13:36 +0000566namespace {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000567template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner02157b02006-06-28 21:38:54 +0000568struct VISIBILITY_HIDDEN DirectFPRules
Chris Lattner0a144ad2002-05-03 21:41:07 +0000569 : public DirectRules<ConstantClass, BuiltinType, Ty,
570 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000571 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000572 if (V2->isNullValue()) return 0;
573 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
574 (BuiltinType)V2->getValue());
575 return ConstantClass::get(*Ty, Result);
576 }
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000577 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Jeff Cohen4e3aede2005-05-03 03:13:01 +0000578 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
579 if (V2->isExactlyValue(0.0)) return ConstantClass::get(*Ty, inf);
580 if (V2->isExactlyValue(-0.0)) return ConstantClass::get(*Ty, -inf);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000581 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
582 return ConstantClass::get(*Ty, R);
583 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000584};
Chris Lattner6a871e12006-06-21 18:13:36 +0000585} // end anonymous namespace
Chris Lattner62af86e2002-05-03 20:09:52 +0000586
Chris Lattner1dd054c2004-01-12 22:07:24 +0000587
588/// ConstRules::get - This method returns the constant rules implementation that
589/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000590ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000591 static EmptyRules EmptyR;
592 static BoolRules BoolR;
593 static NullPointerRules NullPointerR;
Chris Lattner1171d952006-01-04 02:03:29 +0000594 static ConstantPackedRules ConstantPackedR;
595 static GeneralPackedRules GeneralPackedR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000596 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
597 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
598 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
599 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
600 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
601 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
602 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
603 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
604 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
605 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000606
Chris Lattner4b6addf2003-11-17 19:19:32 +0000607 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000608 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
609 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000610 return EmptyR;
611
Chris Lattner6b727592004-06-17 18:19:28 +0000612 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000613 default: assert(0 && "Unknown value type for constant folding!");
614 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000615 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000616 case Type::SByteTyID: return SByteR;
617 case Type::UByteTyID: return UByteR;
618 case Type::ShortTyID: return ShortR;
619 case Type::UShortTyID: return UShortR;
620 case Type::IntTyID: return IntR;
621 case Type::UIntTyID: return UIntR;
622 case Type::LongTyID: return LongR;
623 case Type::ULongTyID: return ULongR;
624 case Type::FloatTyID: return FloatR;
625 case Type::DoubleTyID: return DoubleR;
Chris Lattner1171d952006-01-04 02:03:29 +0000626 case Type::PackedTyID:
627 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
628 return ConstantPackedR;
629 return GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000630 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000632
633
634//===----------------------------------------------------------------------===//
635// ConstantFold*Instruction Implementations
636//===----------------------------------------------------------------------===//
637//
638// These methods contain the special case hackery required to symbolically
639// evaluate some constant expression cases, and use the ConstantRules class to
640// evaluate normal constants.
641//
642static unsigned getSize(const Type *Ty) {
643 unsigned S = Ty->getPrimitiveSize();
644 return S ? S : 8; // Treat pointers at 8 bytes
645}
646
Chris Lattner6b3f4752006-04-02 01:38:28 +0000647/// CastConstantPacked - Convert the specified ConstantPacked node to the
648/// specified packed type. At this point, we know that the elements of the
649/// input packed constant are all simple integer or FP values.
650static Constant *CastConstantPacked(ConstantPacked *CP,
651 const PackedType *DstTy) {
652 unsigned SrcNumElts = CP->getType()->getNumElements();
653 unsigned DstNumElts = DstTy->getNumElements();
654 const Type *SrcEltTy = CP->getType()->getElementType();
655 const Type *DstEltTy = DstTy->getElementType();
656
657 // If both vectors have the same number of elements (thus, the elements
658 // are the same size), perform the conversion now.
659 if (SrcNumElts == DstNumElts) {
660 std::vector<Constant*> Result;
661
662 // If the src and dest elements are both integers, just cast each one
663 // which will do the appropriate bit-convert.
664 if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) {
665 for (unsigned i = 0; i != SrcNumElts; ++i)
666 Result.push_back(ConstantExpr::getCast(CP->getOperand(i),
667 DstEltTy));
668 return ConstantPacked::get(Result);
669 }
670
671 if (SrcEltTy->isIntegral()) {
672 // Otherwise, this is an int-to-fp cast.
673 assert(DstEltTy->isFloatingPoint());
674 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
675 for (unsigned i = 0; i != SrcNumElts; ++i) {
676 double V =
677 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getRawValue());
678 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
679 }
680 return ConstantPacked::get(Result);
681 }
682 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
683 for (unsigned i = 0; i != SrcNumElts; ++i) {
684 float V =
685 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getRawValue());
686 Result.push_back(ConstantFP::get(Type::FloatTy, V));
687 }
688 return ConstantPacked::get(Result);
689 }
690
691 // Otherwise, this is an fp-to-int cast.
692 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
693
694 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
695 for (unsigned i = 0; i != SrcNumElts; ++i) {
696 uint64_t V =
697 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
698 Constant *C = ConstantUInt::get(Type::ULongTy, V);
699 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
700 }
701 return ConstantPacked::get(Result);
702 }
703
704 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
705 for (unsigned i = 0; i != SrcNumElts; ++i) {
706 unsigned V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
707 Constant *C = ConstantUInt::get(Type::UIntTy, V);
708 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
709 }
710 return ConstantPacked::get(Result);
711 }
712
713 // Otherwise, this is a cast that changes element count and size. Handle
714 // casts which shrink the elements here.
715
716 // FIXME: We need to know endianness to do this!
717
718 return 0;
719}
720
721
Chris Lattner1dd054c2004-01-12 22:07:24 +0000722Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
723 const Type *DestTy) {
724 if (V->getType() == DestTy) return (Constant*)V;
725
Chris Lattnerea0789c2004-03-08 06:17:35 +0000726 // Cast of a global address to boolean is always true.
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000727 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattnerea0789c2004-03-08 06:17:35 +0000728 if (DestTy == Type::BoolTy)
729 // FIXME: When we support 'external weak' references, we have to prevent
Chris Lattnercd4003e2005-01-06 16:26:38 +0000730 // this transformation from happening. This code will need to be updated
731 // to ignore external weak symbols when we support it.
Chris Lattner78430662006-09-28 23:34:49 +0000732 return ConstantBool::getTrue();
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000733 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000734 if (CE->getOpcode() == Instruction::Cast) {
735 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
736 // Try to not produce a cast of a cast, which is almost always redundant.
737 if (!Op->getType()->isFloatingPoint() &&
738 !CE->getType()->isFloatingPoint() &&
Reid Spencer8eb06df2004-05-30 01:19:48 +0000739 !DestTy->isFloatingPoint()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000740 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
741 unsigned S3 = getSize(DestTy);
742 if (Op->getType() == DestTy && S3 >= S2)
743 return Op;
744 if (S1 >= S2 && S2 >= S3)
745 return ConstantExpr::getCast(Op, DestTy);
746 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
747 return ConstantExpr::getCast(Op, DestTy);
748 }
749 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
750 // If all of the indexes in the GEP are null values, there is no pointer
751 // adjustment going on. We might as well cast the source pointer.
752 bool isAllNull = true;
753 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
754 if (!CE->getOperand(i)->isNullValue()) {
755 isAllNull = false;
756 break;
757 }
758 if (isAllNull)
759 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
760 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000761 } else if (isa<UndefValue>(V)) {
762 return UndefValue::get(DestTy);
763 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000764
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000765 // Check to see if we are casting an pointer to an aggregate to a pointer to
766 // the first element. If so, return the appropriate GEP instruction.
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000767 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000768 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
769 std::vector<Value*> IdxList;
770 IdxList.push_back(Constant::getNullValue(Type::IntTy));
771 const Type *ElTy = PTy->getElementType();
772 while (ElTy != DPTy->getElementType()) {
773 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
Chris Lattner9e907202004-11-22 19:15:27 +0000774 if (STy->getNumElements() == 0) break;
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000775 ElTy = STy->getElementType(0);
776 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
777 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
778 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
779 ElTy = STy->getElementType();
780 IdxList.push_back(IdxList[0]);
781 } else {
782 break;
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000783 }
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000784 }
785
786 if (ElTy == DPTy->getElementType())
787 return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
788 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000789
790 // Handle casts from one packed constant to another. We know that the src and
791 // dest type have the same size.
792 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
793 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
794 assert(DestPTy->getElementType()->getPrimitiveSizeInBits() *
795 DestPTy->getNumElements() ==
796 SrcTy->getElementType()->getPrimitiveSizeInBits() *
797 SrcTy->getNumElements() && "Not cast between same sized vectors!");
798 if (isa<ConstantAggregateZero>(V))
799 return Constant::getNullValue(DestTy);
800 if (isa<UndefValue>(V))
801 return UndefValue::get(DestTy);
802 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
803 // This is a cast from a ConstantPacked of one type to a ConstantPacked
804 // of another type. Check to see if all elements of the input are
805 // simple.
806 bool AllSimpleConstants = true;
807 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
808 if (!isa<ConstantInt>(CP->getOperand(i)) &&
809 !isa<ConstantFP>(CP->getOperand(i))) {
810 AllSimpleConstants = false;
811 break;
812 }
813 }
814
815 // If all of the elements are simple constants, we can fold this.
816 if (AllSimpleConstants)
817 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
818 }
819 }
820 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000821
Chris Lattner1dd054c2004-01-12 22:07:24 +0000822 ConstRules &Rules = ConstRules::get(V, V);
823
Chris Lattner6b727592004-06-17 18:19:28 +0000824 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000825 case Type::BoolTyID: return Rules.castToBool(V);
826 case Type::UByteTyID: return Rules.castToUByte(V);
827 case Type::SByteTyID: return Rules.castToSByte(V);
828 case Type::UShortTyID: return Rules.castToUShort(V);
829 case Type::ShortTyID: return Rules.castToShort(V);
830 case Type::UIntTyID: return Rules.castToUInt(V);
831 case Type::IntTyID: return Rules.castToInt(V);
832 case Type::ULongTyID: return Rules.castToULong(V);
833 case Type::LongTyID: return Rules.castToLong(V);
834 case Type::FloatTyID: return Rules.castToFloat(V);
835 case Type::DoubleTyID: return Rules.castToDouble(V);
836 case Type::PointerTyID:
837 return Rules.castToPointer(V, cast<PointerType>(DestTy));
838 default: return 0;
839 }
840}
841
Chris Lattner6ea4b522004-03-12 05:53:32 +0000842Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
843 const Constant *V1,
844 const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000845 if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
846 return const_cast<Constant*>(CB->getValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000847
848 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
849 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
850 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000851 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000852 return 0;
853}
854
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000855Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
856 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000857 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
858 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000859 if (Val->isNullValue()) // ee(zero, x) -> zero
860 return Constant::getNullValue(
861 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000862
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000863 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
864 if (const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx)) {
865 return const_cast<Constant*>(CVal->getOperand(CIdx->getValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000866 } else if (isa<UndefValue>(Idx)) {
867 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
868 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000869 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000870 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000871 return 0;
872}
873
Robert Bocchinoca27f032006-01-17 20:07:22 +0000874Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
875 const Constant *Elt,
876 const Constant *Idx) {
877 const ConstantUInt *CIdx = dyn_cast<ConstantUInt>(Idx);
878 if (!CIdx) return 0;
879 unsigned idxVal = CIdx->getValue();
880 if (const UndefValue *UVal = dyn_cast<UndefValue>(Val)) {
881 // Insertion of scalar constant into packed undef
882 // Optimize away insertion of undef
883 if (isa<UndefValue>(Elt))
884 return const_cast<Constant*>(Val);
885 // Otherwise break the aggregate undef into multiple undefs and do
886 // the insertion
887 unsigned numOps =
888 cast<PackedType>(Val->getType())->getNumElements();
889 std::vector<Constant*> Ops;
890 Ops.reserve(numOps);
891 for (unsigned i = 0; i < numOps; ++i) {
892 const Constant *Op =
893 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
894 Ops.push_back(const_cast<Constant*>(Op));
895 }
896 return ConstantPacked::get(Ops);
897 }
898 if (const ConstantAggregateZero *CVal =
899 dyn_cast<ConstantAggregateZero>(Val)) {
900 // Insertion of scalar constant into packed aggregate zero
901 // Optimize away insertion of zero
902 if (Elt->isNullValue())
903 return const_cast<Constant*>(Val);
904 // Otherwise break the aggregate zero into multiple zeros and do
905 // the insertion
906 unsigned numOps =
907 cast<PackedType>(Val->getType())->getNumElements();
908 std::vector<Constant*> Ops;
909 Ops.reserve(numOps);
910 for (unsigned i = 0; i < numOps; ++i) {
911 const Constant *Op =
912 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
913 Ops.push_back(const_cast<Constant*>(Op));
914 }
915 return ConstantPacked::get(Ops);
916 }
917 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
918 // Insertion of scalar constant into packed constant
919 std::vector<Constant*> Ops;
920 Ops.reserve(CVal->getNumOperands());
921 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
922 const Constant *Op =
923 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
924 Ops.push_back(const_cast<Constant*>(Op));
925 }
926 return ConstantPacked::get(Ops);
927 }
928 return 0;
929}
930
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000931Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
932 const Constant *V2,
933 const Constant *Mask) {
934 // TODO:
935 return 0;
936}
937
938
Chris Lattner60c47262005-01-28 19:09:51 +0000939/// isZeroSizedType - This type is zero sized if its an array or structure of
940/// zero sized types. The only leaf zero sized type is an empty structure.
941static bool isMaybeZeroSizedType(const Type *Ty) {
942 if (isa<OpaqueType>(Ty)) return true; // Can't say.
943 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
944
945 // If all of elements have zero size, this does too.
946 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000947 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000948 return true;
949
950 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
951 return isMaybeZeroSizedType(ATy->getElementType());
952 }
953 return false;
954}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000955
Chris Lattner061da2f2004-01-13 05:51:55 +0000956/// IdxCompare - Compare the two constants as though they were getelementptr
957/// indices. This allows coersion of the types to be the same thing.
958///
959/// If the two constants are the "same" (after coersion), return 0. If the
960/// first is less than the second, return -1, if the second is less than the
961/// first, return 1. If the constants are not integral, return -2.
962///
Chris Lattner60c47262005-01-28 19:09:51 +0000963static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000964 if (C1 == C2) return 0;
965
966 // Ok, we found a different index. Are either of the operands
967 // ConstantExprs? If so, we can't do anything with them.
968 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
969 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000970
Chris Lattner69193f92004-04-05 01:30:19 +0000971 // Ok, we have two differing integer indices. Sign extend them to be the same
972 // type. Long is always big enough, so we use it.
973 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
974 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
Chris Lattner061da2f2004-01-13 05:51:55 +0000975 if (C1 == C2) return 0; // Are they just differing types?
976
Chris Lattner60c47262005-01-28 19:09:51 +0000977 // If the type being indexed over is really just a zero sized type, there is
978 // no pointer difference being made here.
979 if (isMaybeZeroSizedType(ElTy))
980 return -2; // dunno.
981
Chris Lattner061da2f2004-01-13 05:51:55 +0000982 // If they are really different, now that they are the same type, then we
983 // found a difference!
984 if (cast<ConstantSInt>(C1)->getValue() < cast<ConstantSInt>(C2)->getValue())
985 return -1;
986 else
987 return 1;
988}
989
990/// evaluateRelation - This function determines if there is anything we can
991/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000992/// things like integer comparisons, but should instead handle ConstantExprs
993/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +0000994/// particular relation to each other, we should return the corresponding SetCC
995/// code, otherwise return Instruction::BinaryOpsEnd.
996///
997/// To simplify this code we canonicalize the relation so that the first
998/// operand is always the most "complex" of the two. We consider simple
999/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001000/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001001///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001002static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001003 assert(V1->getType() == V2->getType() &&
1004 "Cannot compare different types of values!");
1005 if (V1 == V2) return Instruction::SetEQ;
1006
Reid Spenceraccd7c72004-07-17 23:47:01 +00001007 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001008 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1009 // We distilled this down to a simple case, use the standard constant
1010 // folder.
1011 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001012 if (R && R->getValue()) return Instruction::SetEQ;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001013 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001014 if (R && R->getValue()) return Instruction::SetLT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001015 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001016 if (R && R->getValue()) return Instruction::SetGT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001017
1018 // If we couldn't figure it out, bail.
1019 return Instruction::BinaryOpsEnd;
1020 }
1021
Chris Lattner061da2f2004-01-13 05:51:55 +00001022 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +00001023 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1024 if (SwappedRelation != Instruction::BinaryOpsEnd)
1025 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001026
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001027 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001028 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001029 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1030 if (SwappedRelation != Instruction::BinaryOpsEnd)
1031 return SetCondInst::getSwappedCondition(SwappedRelation);
1032 else
1033 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +00001034 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001035
Reid Spenceraccd7c72004-07-17 23:47:01 +00001036 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001037 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001038 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
1039 assert(CPR1 != CPR2 &&
1040 "GVs for the same value exist at different addresses??");
Chris Lattner061da2f2004-01-13 05:51:55 +00001041 // FIXME: If both globals are external weak, they might both be null!
1042 return Instruction::SetNE;
1043 } else {
1044 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1045 // Global can never be null. FIXME: if we implement external weak
1046 // linkage, this is not necessarily true!
1047 return Instruction::SetNE;
1048 }
1049
1050 } else {
1051 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1052 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001053 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +00001054 Constant *CE1Op0 = CE1->getOperand(0);
1055
1056 switch (CE1->getOpcode()) {
1057 case Instruction::Cast:
1058 // If the cast is not actually changing bits, and the second operand is a
1059 // null pointer, do the comparison with the pre-casted value.
1060 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001061 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +00001062 return evaluateRelation(CE1Op0,
1063 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001064
1065 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1066 // from the same type as the src of the LHS, evaluate the inputs. This is
1067 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1068 // which happens a lot in compilers with tagged integers.
1069 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
1070 if (isa<PointerType>(CE1->getType()) &&
1071 CE2->getOpcode() == Instruction::Cast &&
1072 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1073 CE1->getOperand(0)->getType()->isIntegral()) {
1074 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1075 }
Chris Lattner192e3262004-04-11 01:29:30 +00001076 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001077
1078 case Instruction::GetElementPtr:
1079 // Ok, since this is a getelementptr, we know that the constant has a
1080 // pointer type. Check the various cases.
1081 if (isa<ConstantPointerNull>(V2)) {
1082 // If we are comparing a GEP to a null pointer, check to see if the base
1083 // of the GEP equals the null pointer.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001084 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001085 // FIXME: this is not true when we have external weak references!
1086 // No offset can go from a global to a null pointer.
1087 return Instruction::SetGT;
1088 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1089 // If we are indexing from a null pointer, check to see if we have any
1090 // non-zero indices.
1091 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1092 if (!CE1->getOperand(i)->isNullValue())
1093 // Offsetting from null, must not be equal.
1094 return Instruction::SetGT;
1095 // Only zero indexes from null, must still be zero.
1096 return Instruction::SetEQ;
1097 }
1098 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001099 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001100 if (isa<ConstantPointerNull>(CE1Op0)) {
1101 // FIXME: This is not true with external weak references.
1102 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001103 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001104 if (CPR1 == CPR2) {
1105 // If this is a getelementptr of the same global, then it must be
1106 // different. Because the types must match, the getelementptr could
1107 // only have at most one index, and because we fold getelementptr's
1108 // with a single zero index, it must be nonzero.
1109 assert(CE1->getNumOperands() == 2 &&
1110 !CE1->getOperand(1)->isNullValue() &&
1111 "Suprising getelementptr!");
1112 return Instruction::SetGT;
1113 } else {
1114 // If they are different globals, we don't know what the value is,
1115 // but they can't be equal.
1116 return Instruction::SetNE;
1117 }
1118 }
1119 } else {
1120 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1121 const Constant *CE2Op0 = CE2->getOperand(0);
1122
1123 // There are MANY other foldings that we could perform here. They will
1124 // probably be added on demand, as they seem needed.
1125 switch (CE2->getOpcode()) {
1126 default: break;
1127 case Instruction::GetElementPtr:
1128 // By far the most common case to handle is when the base pointers are
1129 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001130 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001131 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1132 return Instruction::SetNE;
1133 // Ok, we know that both getelementptr instructions are based on the
1134 // same global. From this, we can precisely determine the relative
1135 // ordering of the resultant pointers.
1136 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001137
Chris Lattner061da2f2004-01-13 05:51:55 +00001138 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001139 gep_type_iterator GTI = gep_type_begin(CE1);
1140 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1141 ++i, ++GTI)
1142 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1143 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001144 case -1: return Instruction::SetLT;
1145 case 1: return Instruction::SetGT;
1146 case -2: return Instruction::BinaryOpsEnd;
1147 }
1148
1149 // Ok, we ran out of things they have in common. If any leftovers
1150 // are non-zero then we have a difference, otherwise we are equal.
1151 for (; i < CE1->getNumOperands(); ++i)
1152 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001153 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1154 return Instruction::SetGT;
1155 else
1156 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001157
Chris Lattner061da2f2004-01-13 05:51:55 +00001158 for (; i < CE2->getNumOperands(); ++i)
1159 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001160 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1161 return Instruction::SetLT;
1162 else
1163 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001164 return Instruction::SetEQ;
1165 }
1166 }
1167 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001168
Chris Lattner061da2f2004-01-13 05:51:55 +00001169 default:
1170 break;
1171 }
1172 }
1173
1174 return Instruction::BinaryOpsEnd;
1175}
1176
Chris Lattner1dd054c2004-01-12 22:07:24 +00001177Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1178 const Constant *V1,
1179 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001180 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001181 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001182 default: break;
1183 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1184 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1185 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
1186 case Instruction::Div: C = ConstRules::get(V1, V2).div(V1, V2); break;
1187 case Instruction::Rem: C = ConstRules::get(V1, V2).rem(V1, V2); break;
1188 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1189 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1190 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1191 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
1192 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
1193 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
1194 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1195 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001196 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
1197 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001198 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001199 break;
1200 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1201 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001202 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001203 break;
1204 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1205 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001206 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001207 break;
1208 }
1209
Chris Lattner061da2f2004-01-13 05:51:55 +00001210 // If we successfully folded the expression, return it now.
1211 if (C) return C;
1212
Chris Lattnere1496fb2006-09-17 19:14:47 +00001213 if (SetCondInst::isComparison(Opcode)) {
Chris Lattner192eacc2004-10-17 04:01:51 +00001214 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1215 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001216 switch (evaluateRelation(const_cast<Constant*>(V1),
1217 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001218 default: assert(0 && "Unknown relational!");
1219 case Instruction::BinaryOpsEnd:
1220 break; // Couldn't determine anything about these constants.
1221 case Instruction::SetEQ: // We know the constants are equal!
1222 // If we know the constants are equal, we can decide the result of this
1223 // computation precisely.
1224 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1225 Opcode == Instruction::SetLE ||
1226 Opcode == Instruction::SetGE);
1227 case Instruction::SetLT:
1228 // If we know that V1 < V2, we can decide the result of this computation
1229 // precisely.
1230 return ConstantBool::get(Opcode == Instruction::SetLT ||
1231 Opcode == Instruction::SetNE ||
1232 Opcode == Instruction::SetLE);
1233 case Instruction::SetGT:
1234 // If we know that V1 > V2, we can decide the result of this computation
1235 // precisely.
1236 return ConstantBool::get(Opcode == Instruction::SetGT ||
1237 Opcode == Instruction::SetNE ||
1238 Opcode == Instruction::SetGE);
1239 case Instruction::SetLE:
1240 // If we know that V1 <= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001241 if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
1242 if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001243 break;
1244
1245 case Instruction::SetGE:
1246 // If we know that V1 >= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001247 if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
1248 if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001249 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001250
Chris Lattner061da2f2004-01-13 05:51:55 +00001251 case Instruction::SetNE:
1252 // If we know that V1 != V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001253 if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
1254 if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001255 break;
1256 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001257 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001258
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001259 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1260 switch (Opcode) {
1261 case Instruction::Add:
1262 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001263 case Instruction::Xor:
1264 return UndefValue::get(V1->getType());
1265
1266 case Instruction::Mul:
1267 case Instruction::And:
1268 return Constant::getNullValue(V1->getType());
1269 case Instruction::Div:
1270 case Instruction::Rem:
1271 if (!isa<UndefValue>(V2)) // undef/X -> 0
1272 return Constant::getNullValue(V1->getType());
1273 return const_cast<Constant*>(V2); // X/undef -> undef
1274 case Instruction::Or: // X|undef -> -1
1275 return ConstantInt::getAllOnesValue(V1->getType());
1276 case Instruction::Shr:
1277 if (!isa<UndefValue>(V2)) {
1278 if (V1->getType()->isSigned())
1279 return const_cast<Constant*>(V1); // undef >>s X -> undef
1280 // undef >>u X -> 0
1281 } else if (isa<UndefValue>(V1)) {
1282 return const_cast<Constant*>(V1); // undef >> undef -> undef
1283 } else {
1284 if (V1->getType()->isSigned())
1285 return const_cast<Constant*>(V1); // X >>s undef -> X
1286 // X >>u undef -> 0
1287 }
1288 return Constant::getNullValue(V1->getType());
1289
1290 case Instruction::Shl:
1291 // undef << X -> 0 X << undef -> 0
1292 return Constant::getNullValue(V1->getType());
1293 }
1294 }
1295
Chris Lattner061da2f2004-01-13 05:51:55 +00001296 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
1297 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1298 // There are many possible foldings we could do here. We should probably
1299 // at least fold add of a pointer with an integer into the appropriate
1300 // getelementptr. This will improve alias analysis a bit.
1301
1302
1303
1304
1305 } else {
1306 // Just implement a couple of simple identities.
1307 switch (Opcode) {
1308 case Instruction::Add:
1309 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1310 break;
1311 case Instruction::Sub:
1312 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1313 break;
1314 case Instruction::Mul:
1315 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1316 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1317 if (CI->getRawValue() == 1)
1318 return const_cast<Constant*>(V1); // X * 1 == X
1319 break;
1320 case Instruction::Div:
1321 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1322 if (CI->getRawValue() == 1)
1323 return const_cast<Constant*>(V1); // X / 1 == X
1324 break;
1325 case Instruction::Rem:
1326 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
1327 if (CI->getRawValue() == 1)
1328 return Constant::getNullValue(CI->getType()); // X % 1 == 0
1329 break;
1330 case Instruction::And:
1331 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1332 return const_cast<Constant*>(V1); // X & -1 == X
1333 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Chris Lattnerea0789c2004-03-08 06:17:35 +00001334 if (CE1->getOpcode() == Instruction::Cast &&
Reid Spenceraccd7c72004-07-17 23:47:01 +00001335 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001336 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001337
1338 // Functions are at least 4-byte aligned. If and'ing the address of a
1339 // function with a constant < 4, fold it to zero.
1340 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spenceraccd7c72004-07-17 23:47:01 +00001341 if (CI->getRawValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001342 return Constant::getNullValue(CI->getType());
1343 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001344 break;
1345 case Instruction::Or:
1346 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1347 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1348 return const_cast<Constant*>(V2); // X | -1 == -1
1349 break;
1350 case Instruction::Xor:
1351 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1352 break;
1353 }
1354 }
1355
1356 } else if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) {
1357 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1358 // other way if possible.
1359 switch (Opcode) {
1360 case Instruction::Add:
1361 case Instruction::Mul:
1362 case Instruction::And:
1363 case Instruction::Or:
1364 case Instruction::Xor:
1365 case Instruction::SetEQ:
1366 case Instruction::SetNE:
1367 // No change of opcode required.
1368 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1369
1370 case Instruction::SetLT:
1371 case Instruction::SetGT:
1372 case Instruction::SetLE:
1373 case Instruction::SetGE:
1374 // Change the opcode as necessary to swap the operands.
1375 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1376 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1377
1378 case Instruction::Shl:
1379 case Instruction::Shr:
1380 case Instruction::Sub:
1381 case Instruction::Div:
1382 case Instruction::Rem:
1383 default: // These instructions cannot be flopped around.
1384 break;
1385 }
1386 }
1387 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001388}
1389
1390Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001391 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001392 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001393 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001394 return const_cast<Constant*>(C);
1395
Chris Lattnerf6013752004-10-17 21:54:55 +00001396 if (isa<UndefValue>(C)) {
1397 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1398 true);
1399 assert(Ty != 0 && "Invalid indices for GEP!");
1400 return UndefValue::get(PointerType::get(Ty));
1401 }
1402
1403 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001404 if (C->isNullValue()) {
1405 bool isNull = true;
1406 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001407 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001408 isNull = false;
1409 break;
1410 }
1411 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001412 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001413 true);
1414 assert(Ty != 0 && "Invalid indices for GEP!");
1415 return ConstantPointerNull::get(PointerType::get(Ty));
1416 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001417
1418 if (IdxList.size() == 1) {
1419 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
1420 if (unsigned ElSize = ElTy->getPrimitiveSize()) {
1421 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1422 // type, we can statically fold this.
1423 Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
Chris Lattner13128ab2004-10-11 22:52:25 +00001424 R = ConstantExpr::getCast(R, Idx0->getType());
1425 R = ConstantExpr::getMul(R, Idx0);
Chris Lattner4bbd4092004-07-15 01:16:59 +00001426 return ConstantExpr::getCast(R, C->getType());
1427 }
1428 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001429 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001430
1431 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1432 // Combine Indices - If the source pointer to this getelementptr instruction
1433 // is a getelementptr instruction, combine the indices of the two
1434 // getelementptr instructions into a single instruction.
1435 //
1436 if (CE->getOpcode() == Instruction::GetElementPtr) {
1437 const Type *LastTy = 0;
1438 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1439 I != E; ++I)
1440 LastTy = *I;
1441
Chris Lattner13128ab2004-10-11 22:52:25 +00001442 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1443 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001444 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1445 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001446 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001447
1448 // Add the last index of the source with the first index of the new GEP.
1449 // Make sure to handle the case when they are actually different types.
1450 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001451 // Otherwise it must be an array.
1452 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001453 const Type *IdxTy = Combined->getType();
Chris Lattner13128ab2004-10-11 22:52:25 +00001454 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001455 Combined =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001456 ConstantExpr::get(Instruction::Add,
Chris Lattner13128ab2004-10-11 22:52:25 +00001457 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattner71068a02004-07-07 04:45:13 +00001458 ConstantExpr::getCast(Combined, IdxTy));
1459 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001460
Chris Lattner1dd054c2004-01-12 22:07:24 +00001461 NewIndices.push_back(Combined);
1462 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1463 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1464 }
1465 }
1466
1467 // Implement folding of:
1468 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1469 // long 0, long 0)
1470 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1471 //
1472 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
Chris Lattner13128ab2004-10-11 22:52:25 +00001473 Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001474 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001475 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1476 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1477 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001478 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001479 if (CAT->getElementType() == SAT->getElementType())
1480 return ConstantExpr::getGetElementPtr(
1481 (Constant*)CE->getOperand(0), IdxList);
1482 }
1483 return 0;
1484}
1485