blob: 9974071385b71a460c5b56643fc6df4d30492dea [file] [log] [blame]
Chris Lattnercbfd4062004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnercbfd4062004-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 Lattner00950542001-06-06 20:29:01 +000013//
Chris Lattnereab20b52004-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 Lattner00950542001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattnercbfd4062004-01-12 21:13:12 +000021#include "ConstantFolding.h"
Chris Lattner8b0f0cb2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnerb228f9c2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner27287de2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattner802b5ab2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattner0eff5ad2006-10-13 17:22:21 +000027#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Jeff Cohena97e8db2005-05-03 03:13:01 +000030#include <limits>
Chris Lattner0dc39692003-11-17 19:05:17 +000031using namespace llvm;
Chris Lattner14712a62001-09-09 21:01:20 +000032
Chris Lattnercbfd4062004-01-12 21:13:12 +000033namespace {
Chris Lattnerf190d382006-06-28 21:38:54 +000034 struct VISIBILITY_HIDDEN ConstRules {
Chris Lattnercbfd4062004-01-12 21:13:12 +000035 ConstRules() {}
Reid Spencer2b2a5282005-04-24 22:27:20 +000036 virtual ~ConstRules() {}
Misha Brukmanfd939082005-04-21 23:48:37 +000037
Chris Lattnercbfd4062004-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;
Reid Spencer0a783f72006-11-02 01:53:59 +000042 virtual Constant *urem(const Constant *V1, const Constant *V2) const = 0;
43 virtual Constant *srem(const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *frem(const Constant *V1, const Constant *V2) const = 0;
Reid Spencer1628cec2006-10-26 06:15:43 +000045 virtual Constant *udiv(const Constant *V1, const Constant *V2) const = 0;
46 virtual Constant *sdiv(const Constant *V1, const Constant *V2) const = 0;
47 virtual Constant *fdiv(const Constant *V1, const Constant *V2) const = 0;
Chris Lattnercbfd4062004-01-12 21:13:12 +000048 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
49 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
50 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
51 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
Reid Spencer3822ff52006-11-08 06:47:33 +000052 virtual Constant *lshr(const Constant *V1, const Constant *V2) const = 0;
53 virtual Constant *ashr(const Constant *V1, const Constant *V2) const = 0;
Chris Lattnercbfd4062004-01-12 21:13:12 +000054 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const =0;
55 virtual Constant *equalto(const Constant *V1, const Constant *V2) const = 0;
56
57 // Casting operators.
58 virtual Constant *castToBool (const Constant *V) const = 0;
59 virtual Constant *castToSByte (const Constant *V) const = 0;
60 virtual Constant *castToUByte (const Constant *V) const = 0;
61 virtual Constant *castToShort (const Constant *V) const = 0;
62 virtual Constant *castToUShort(const Constant *V) const = 0;
63 virtual Constant *castToInt (const Constant *V) const = 0;
64 virtual Constant *castToUInt (const Constant *V) const = 0;
65 virtual Constant *castToLong (const Constant *V) const = 0;
66 virtual Constant *castToULong (const Constant *V) const = 0;
67 virtual Constant *castToFloat (const Constant *V) const = 0;
68 virtual Constant *castToDouble(const Constant *V) const = 0;
69 virtual Constant *castToPointer(const Constant *V,
70 const PointerType *Ty) const = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000071
Chris Lattnercbfd4062004-01-12 21:13:12 +000072 // ConstRules::get - Return an instance of ConstRules for the specified
73 // constant operands.
74 //
75 static ConstRules &get(const Constant *V1, const Constant *V2);
76 private:
77 ConstRules(const ConstRules &); // Do not implement
78 ConstRules &operator=(const ConstRules &); // Do not implement
79 };
80}
81
82
Chris Lattner00950542001-06-06 20:29:01 +000083//===----------------------------------------------------------------------===//
84// TemplateRules Class
85//===----------------------------------------------------------------------===//
86//
Misha Brukmanfd939082005-04-21 23:48:37 +000087// TemplateRules - Implement a subclass of ConstRules that provides all
88// operations as noops. All other rules classes inherit from this class so
89// that if functionality is needed in the future, it can simply be added here
Chris Lattner00950542001-06-06 20:29:01 +000090// and to ConstRules without changing anything else...
Misha Brukmanfd939082005-04-21 23:48:37 +000091//
Chris Lattner00950542001-06-06 20:29:01 +000092// This class also provides subclasses with typesafe implementations of methods
93// so that don't have to do type casting.
94//
Chris Lattner51df6d52006-06-21 18:13:36 +000095namespace {
Chris Lattner00950542001-06-06 20:29:01 +000096template<class ArgType, class SubClassName>
Chris Lattnerf190d382006-06-28 21:38:54 +000097class VISIBILITY_HIDDEN TemplateRules : public ConstRules {
Chris Lattner00950542001-06-06 20:29:01 +000098
Reid Spencer2b2a5282005-04-24 22:27:20 +000099
Chris Lattner00950542001-06-06 20:29:01 +0000100 //===--------------------------------------------------------------------===//
101 // Redirecting functions that cast to the appropriate types
102 //===--------------------------------------------------------------------===//
103
Misha Brukmanfd939082005-04-21 23:48:37 +0000104 virtual Constant *add(const Constant *V1, const Constant *V2) const {
105 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner00950542001-06-06 20:29:01 +0000106 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000107 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
108 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner00950542001-06-06 20:29:01 +0000109 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000110 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
111 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnerb5bcbc32001-07-20 19:15:36 +0000112 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000113 virtual Constant *udiv(const Constant *V1, const Constant *V2) const {
114 return SubClassName::UDiv((const ArgType *)V1, (const ArgType *)V2);
115 }
116 virtual Constant *sdiv(const Constant *V1, const Constant *V2) const {
117 return SubClassName::SDiv((const ArgType *)V1, (const ArgType *)V2);
118 }
119 virtual Constant *fdiv(const Constant *V1, const Constant *V2) const {
120 return SubClassName::FDiv((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner05c05ea2002-04-07 08:10:14 +0000121 }
Reid Spencer0a783f72006-11-02 01:53:59 +0000122 virtual Constant *urem(const Constant *V1, const Constant *V2) const {
123 return SubClassName::URem((const ArgType *)V1, (const ArgType *)V2);
124 }
125 virtual Constant *srem(const Constant *V1, const Constant *V2) const {
126 return SubClassName::SRem((const ArgType *)V1, (const ArgType *)V2);
127 }
128 virtual Constant *frem(const Constant *V1, const Constant *V2) const {
129 return SubClassName::FRem((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner44540dd2002-05-03 21:41:07 +0000130 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000131 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
132 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner5fa829c2002-07-30 16:24:28 +0000133 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000134 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
135 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner5fa829c2002-07-30 16:24:28 +0000136 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000137 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
138 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner5fa829c2002-07-30 16:24:28 +0000139 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000140 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
141 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnercf4929f2002-05-06 03:00:54 +0000142 }
Reid Spencer3822ff52006-11-08 06:47:33 +0000143 virtual Constant *lshr(const Constant *V1, const Constant *V2) const {
144 return SubClassName::LShr((const ArgType *)V1, (const ArgType *)V2);
145 }
146 virtual Constant *ashr(const Constant *V1, const Constant *V2) const {
147 return SubClassName::AShr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnercf4929f2002-05-06 03:00:54 +0000148 }
Chris Lattnerb5bcbc32001-07-20 19:15:36 +0000149
Misha Brukmanfd939082005-04-21 23:48:37 +0000150 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner00950542001-06-06 20:29:01 +0000151 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
152 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000153 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattner5d5a7142003-11-17 20:19:35 +0000154 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
155 }
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattnerf59bd532001-07-21 19:10:49 +0000157 // Casting operators. ick
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000158 virtual Constant *castToBool(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000159 return SubClassName::CastToBool((const ArgType*)V);
160 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000161 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000162 return SubClassName::CastToSByte((const ArgType*)V);
163 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000164 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000165 return SubClassName::CastToUByte((const ArgType*)V);
166 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000167 virtual Constant *castToShort(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000168 return SubClassName::CastToShort((const ArgType*)V);
169 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000170 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000171 return SubClassName::CastToUShort((const ArgType*)V);
172 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000173 virtual Constant *castToInt(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000174 return SubClassName::CastToInt((const ArgType*)V);
175 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000176 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000177 return SubClassName::CastToUInt((const ArgType*)V);
178 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000179 virtual Constant *castToLong(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000180 return SubClassName::CastToLong((const ArgType*)V);
181 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000182 virtual Constant *castToULong(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000183 return SubClassName::CastToULong((const ArgType*)V);
184 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000185 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000186 return SubClassName::CastToFloat((const ArgType*)V);
187 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000188 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattnerf59bd532001-07-21 19:10:49 +0000189 return SubClassName::CastToDouble((const ArgType*)V);
190 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000191 virtual Constant *castToPointer(const Constant *V,
Chris Lattner27287de2003-04-17 19:24:18 +0000192 const PointerType *Ty) const {
Chris Lattner76ac1a42001-11-01 05:55:13 +0000193 return SubClassName::CastToPointer((const ArgType*)V, Ty);
194 }
Chris Lattnerf59bd532001-07-21 19:10:49 +0000195
Chris Lattner00950542001-06-06 20:29:01 +0000196 //===--------------------------------------------------------------------===//
197 // Default "noop" implementations
198 //===--------------------------------------------------------------------===//
199
Reid Spencer1628cec2006-10-26 06:15:43 +0000200 static Constant *Add (const ArgType *V1, const ArgType *V2) { return 0; }
201 static Constant *Sub (const ArgType *V1, const ArgType *V2) { return 0; }
202 static Constant *Mul (const ArgType *V1, const ArgType *V2) { return 0; }
203 static Constant *SDiv(const ArgType *V1, const ArgType *V2) { return 0; }
204 static Constant *UDiv(const ArgType *V1, const ArgType *V2) { return 0; }
205 static Constant *FDiv(const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer0a783f72006-11-02 01:53:59 +0000206 static Constant *URem(const ArgType *V1, const ArgType *V2) { return 0; }
207 static Constant *SRem(const ArgType *V1, const ArgType *V2) { return 0; }
208 static Constant *FRem(const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer1628cec2006-10-26 06:15:43 +0000209 static Constant *And (const ArgType *V1, const ArgType *V2) { return 0; }
210 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
211 static Constant *Xor (const ArgType *V1, const ArgType *V2) { return 0; }
212 static Constant *Shl (const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer3822ff52006-11-08 06:47:33 +0000213 static Constant *LShr(const ArgType *V1, const ArgType *V2) { return 0; }
214 static Constant *AShr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000215 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner00950542001-06-06 20:29:01 +0000216 return 0;
217 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000218 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattner5d5a7142003-11-17 20:19:35 +0000219 return 0;
220 }
Chris Lattnerf59bd532001-07-21 19:10:49 +0000221
222 // Casting operators. ick
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000223 static Constant *CastToBool (const Constant *V) { return 0; }
224 static Constant *CastToSByte (const Constant *V) { return 0; }
225 static Constant *CastToUByte (const Constant *V) { return 0; }
226 static Constant *CastToShort (const Constant *V) { return 0; }
227 static Constant *CastToUShort(const Constant *V) { return 0; }
228 static Constant *CastToInt (const Constant *V) { return 0; }
229 static Constant *CastToUInt (const Constant *V) { return 0; }
230 static Constant *CastToLong (const Constant *V) { return 0; }
231 static Constant *CastToULong (const Constant *V) { return 0; }
232 static Constant *CastToFloat (const Constant *V) { return 0; }
233 static Constant *CastToDouble(const Constant *V) { return 0; }
234 static Constant *CastToPointer(const Constant *,
235 const PointerType *) {return 0;}
Reid Spencer2b2a5282005-04-24 22:27:20 +0000236
237public:
238 virtual ~TemplateRules() {}
Chris Lattner00950542001-06-06 20:29:01 +0000239};
Chris Lattner51df6d52006-06-21 18:13:36 +0000240} // end anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +0000241
242
243//===----------------------------------------------------------------------===//
244// EmptyRules Class
245//===----------------------------------------------------------------------===//
246//
247// EmptyRules provides a concrete base class of ConstRules that does nothing
248//
Chris Lattner51df6d52006-06-21 18:13:36 +0000249namespace {
Chris Lattnerf190d382006-06-28 21:38:54 +0000250struct VISIBILITY_HIDDEN EmptyRules
251 : public TemplateRules<Constant, EmptyRules> {
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000252 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner850ba992006-09-28 23:34:49 +0000253 if (V1 == V2) return ConstantBool::getTrue();
Chris Lattner5d5a7142003-11-17 20:19:35 +0000254 return 0;
255 }
Chris Lattner14712a62001-09-09 21:01:20 +0000256};
Chris Lattner51df6d52006-06-21 18:13:36 +0000257} // end anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +0000258
259
260
261//===----------------------------------------------------------------------===//
262// BoolRules Class
263//===----------------------------------------------------------------------===//
264//
265// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
266//
Chris Lattner51df6d52006-06-21 18:13:36 +0000267namespace {
Chris Lattnerf190d382006-06-28 21:38:54 +0000268struct VISIBILITY_HIDDEN BoolRules
269 : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner00950542001-06-06 20:29:01 +0000270
Chris Lattner2c822cc2006-01-05 07:19:51 +0000271 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner252030e2002-09-03 20:09:49 +0000272 return ConstantBool::get(V1->getValue() < V2->getValue());
273 }
274
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000275 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner5d5a7142003-11-17 20:19:35 +0000276 return ConstantBool::get(V1 == V2);
277 }
278
Chris Lattner5fa829c2002-07-30 16:24:28 +0000279 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
280 return ConstantBool::get(V1->getValue() & V2->getValue());
281 }
282
283 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000284 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner00950542001-06-06 20:29:01 +0000285 }
286
Chris Lattner5fa829c2002-07-30 16:24:28 +0000287 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
288 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner00950542001-06-06 20:29:01 +0000289 }
Chris Lattner3ccd0fd2003-08-13 15:52:25 +0000290
291 // Casting operators. ick
292#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000293 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattner3ccd0fd2003-08-13 15:52:25 +0000294 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
295 }
296
297 DEF_CAST(Bool , ConstantBool, bool)
Reid Spencerb83eb642006-10-20 07:07:24 +0000298 DEF_CAST(SByte , ConstantInt, signed char)
299 DEF_CAST(UByte , ConstantInt, unsigned char)
300 DEF_CAST(Short , ConstantInt, signed short)
301 DEF_CAST(UShort, ConstantInt, unsigned short)
302 DEF_CAST(Int , ConstantInt, signed int)
303 DEF_CAST(UInt , ConstantInt, unsigned int)
304 DEF_CAST(Long , ConstantInt, int64_t)
305 DEF_CAST(ULong , ConstantInt, uint64_t)
Chris Lattner3ccd0fd2003-08-13 15:52:25 +0000306 DEF_CAST(Float , ConstantFP , float)
307 DEF_CAST(Double, ConstantFP , double)
308#undef DEF_CAST
Chris Lattner14712a62001-09-09 21:01:20 +0000309};
Chris Lattner51df6d52006-06-21 18:13:36 +0000310} // end anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +0000311
312
313//===----------------------------------------------------------------------===//
Chris Lattnercf09f512003-11-17 19:19:32 +0000314// NullPointerRules Class
Chris Lattner76ac1a42001-11-01 05:55:13 +0000315//===----------------------------------------------------------------------===//
316//
Chris Lattnercf09f512003-11-17 19:19:32 +0000317// NullPointerRules provides a concrete base class of ConstRules for null
318// pointers.
Chris Lattner76ac1a42001-11-01 05:55:13 +0000319//
Chris Lattner51df6d52006-06-21 18:13:36 +0000320namespace {
Chris Lattnerf190d382006-06-28 21:38:54 +0000321struct VISIBILITY_HIDDEN NullPointerRules
322 : public TemplateRules<ConstantPointerNull, NullPointerRules> {
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000323 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner850ba992006-09-28 23:34:49 +0000324 return ConstantBool::getTrue(); // Null pointers are always equal
Chris Lattner5d5a7142003-11-17 20:19:35 +0000325 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000326 static Constant *CastToBool(const Constant *V) {
Chris Lattner850ba992006-09-28 23:34:49 +0000327 return ConstantBool::getFalse();
Chris Lattner76ac1a42001-11-01 05:55:13 +0000328 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000329 static Constant *CastToSByte (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000330 return ConstantInt::get(Type::SByteTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000331 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000332 static Constant *CastToUByte (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000333 return ConstantInt::get(Type::UByteTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000334 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000335 static Constant *CastToShort (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000336 return ConstantInt::get(Type::ShortTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000337 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000338 static Constant *CastToUShort(const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000339 return ConstantInt::get(Type::UShortTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000340 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000341 static Constant *CastToInt (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000342 return ConstantInt::get(Type::IntTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000343 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000344 static Constant *CastToUInt (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000345 return ConstantInt::get(Type::UIntTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000346 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000347 static Constant *CastToLong (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000348 return ConstantInt::get(Type::LongTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000349 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000350 static Constant *CastToULong (const Constant *V) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000351 return ConstantInt::get(Type::ULongTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000352 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000353 static Constant *CastToFloat (const Constant *V) {
Chris Lattnercf09f512003-11-17 19:19:32 +0000354 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000355 }
Chris Lattner8b0f0cb2004-01-12 21:02:29 +0000356 static Constant *CastToDouble(const Constant *V) {
Chris Lattnercf09f512003-11-17 19:19:32 +0000357 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000358 }
359
Chris Lattnera83a6e42003-11-17 19:21:04 +0000360 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner27287de2003-04-17 19:24:18 +0000361 const PointerType *PTy) {
Chris Lattnercf09f512003-11-17 19:19:32 +0000362 return ConstantPointerNull::get(PTy);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000363 }
364};
Chris Lattner51df6d52006-06-21 18:13:36 +0000365} // end anonymous namespace
Chris Lattner76ac1a42001-11-01 05:55:13 +0000366
Chris Lattnerd15ed592006-01-04 02:03:29 +0000367//===----------------------------------------------------------------------===//
368// ConstantPackedRules Class
369//===----------------------------------------------------------------------===//
370
Chris Lattnerd2a7ea42006-01-04 02:15:02 +0000371/// DoVectorOp - Given two packed constants and a function pointer, apply the
372/// function pointer to each element pair, producing a new ConstantPacked
373/// constant.
374static Constant *EvalVectorOp(const ConstantPacked *V1,
375 const ConstantPacked *V2,
376 Constant *(*FP)(Constant*, Constant*)) {
377 std::vector<Constant*> Res;
378 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
379 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
380 const_cast<Constant*>(V2->getOperand(i))));
381 return ConstantPacked::get(Res);
382}
383
Chris Lattnerd15ed592006-01-04 02:03:29 +0000384/// PackedTypeRules provides a concrete base class of ConstRules for
385/// ConstantPacked operands.
386///
Chris Lattner51df6d52006-06-21 18:13:36 +0000387namespace {
Chris Lattnerf190d382006-06-28 21:38:54 +0000388struct VISIBILITY_HIDDEN ConstantPackedRules
Chris Lattnerd15ed592006-01-04 02:03:29 +0000389 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerd2a7ea42006-01-04 02:15:02 +0000390
391 static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
392 return EvalVectorOp(V1, V2, ConstantExpr::getAdd);
393 }
394 static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) {
395 return EvalVectorOp(V1, V2, ConstantExpr::getSub);
396 }
397 static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
398 return EvalVectorOp(V1, V2, ConstantExpr::getMul);
399 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000400 static Constant *UDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
401 return EvalVectorOp(V1, V2, ConstantExpr::getUDiv);
402 }
403 static Constant *SDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
404 return EvalVectorOp(V1, V2, ConstantExpr::getSDiv);
405 }
406 static Constant *FDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
407 return EvalVectorOp(V1, V2, ConstantExpr::getFDiv);
Chris Lattnerd2a7ea42006-01-04 02:15:02 +0000408 }
Reid Spencer0a783f72006-11-02 01:53:59 +0000409 static Constant *URem(const ConstantPacked *V1, const ConstantPacked *V2) {
410 return EvalVectorOp(V1, V2, ConstantExpr::getURem);
411 }
412 static Constant *SRem(const ConstantPacked *V1, const ConstantPacked *V2) {
413 return EvalVectorOp(V1, V2, ConstantExpr::getSRem);
414 }
415 static Constant *FRem(const ConstantPacked *V1, const ConstantPacked *V2) {
416 return EvalVectorOp(V1, V2, ConstantExpr::getFRem);
Chris Lattnerd2a7ea42006-01-04 02:15:02 +0000417 }
418 static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) {
419 return EvalVectorOp(V1, V2, ConstantExpr::getAnd);
420 }
421 static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) {
422 return EvalVectorOp(V1, V2, ConstantExpr::getOr);
423 }
424 static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) {
425 return EvalVectorOp(V1, V2, ConstantExpr::getXor);
426 }
Chris Lattnerd2a7ea42006-01-04 02:15:02 +0000427 static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){
428 return 0;
429 }
430 static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) {
Chris Lattner84e10642006-01-04 02:20:54 +0000431 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) {
432 Constant *C =
433 ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)),
434 const_cast<Constant*>(V2->getOperand(i)));
435 if (ConstantBool *CB = dyn_cast<ConstantBool>(C))
436 return CB;
437 }
438 // Otherwise, could not decide from any element pairs.
Chris Lattnerd2a7ea42006-01-04 02:15:02 +0000439 return 0;
440 }
Chris Lattnerd15ed592006-01-04 02:03:29 +0000441};
Chris Lattner51df6d52006-06-21 18:13:36 +0000442} // end anonymous namespace
Chris Lattnerd15ed592006-01-04 02:03:29 +0000443
444
445//===----------------------------------------------------------------------===//
446// GeneralPackedRules Class
447//===----------------------------------------------------------------------===//
448
449/// GeneralPackedRules provides a concrete base class of ConstRules for
450/// PackedType operands, where both operands are not ConstantPacked. The usual
451/// cause for this is that one operand is a ConstantAggregateZero.
452///
Chris Lattner51df6d52006-06-21 18:13:36 +0000453namespace {
Chris Lattnerf190d382006-06-28 21:38:54 +0000454struct VISIBILITY_HIDDEN GeneralPackedRules
455 : public TemplateRules<Constant, GeneralPackedRules> {
Chris Lattnerd15ed592006-01-04 02:03:29 +0000456};
Chris Lattner51df6d52006-06-21 18:13:36 +0000457} // end anonymous namespace
Chris Lattnerd15ed592006-01-04 02:03:29 +0000458
Chris Lattner76ac1a42001-11-01 05:55:13 +0000459
460//===----------------------------------------------------------------------===//
Reid Spencerb83eb642006-10-20 07:07:24 +0000461// DirectIntRules Class
Chris Lattner00950542001-06-06 20:29:01 +0000462//===----------------------------------------------------------------------===//
463//
Reid Spencerb83eb642006-10-20 07:07:24 +0000464// DirectIntRules provides implementations of functions that are valid on
465// integer types, but not all types in general.
Chris Lattner00950542001-06-06 20:29:01 +0000466//
Chris Lattner51df6d52006-06-21 18:13:36 +0000467namespace {
Reid Spencerb83eb642006-10-20 07:07:24 +0000468template <class BuiltinType, Type **Ty>
469struct VISIBILITY_HIDDEN DirectIntRules
470 : public TemplateRules<ConstantInt, DirectIntRules<BuiltinType, Ty> > {
471
472 static Constant *Add(const ConstantInt *V1, const ConstantInt *V2) {
473 BuiltinType R = (BuiltinType)V1->getZExtValue() +
474 (BuiltinType)V2->getZExtValue();
475 return ConstantInt::get(*Ty, R);
Chris Lattner00950542001-06-06 20:29:01 +0000476 }
477
Reid Spencerb83eb642006-10-20 07:07:24 +0000478 static Constant *Sub(const ConstantInt *V1, const ConstantInt *V2) {
479 BuiltinType R = (BuiltinType)V1->getZExtValue() -
480 (BuiltinType)V2->getZExtValue();
481 return ConstantInt::get(*Ty, R);
Chris Lattner00950542001-06-06 20:29:01 +0000482 }
483
Reid Spencerb83eb642006-10-20 07:07:24 +0000484 static Constant *Mul(const ConstantInt *V1, const ConstantInt *V2) {
485 BuiltinType R = (BuiltinType)V1->getZExtValue() *
486 (BuiltinType)V2->getZExtValue();
487 return ConstantInt::get(*Ty, R);
Chris Lattnerb5bcbc32001-07-20 19:15:36 +0000488 }
489
Reid Spencerb83eb642006-10-20 07:07:24 +0000490 static Constant *LessThan(const ConstantInt *V1, const ConstantInt *V2) {
491 bool R = (BuiltinType)V1->getZExtValue() < (BuiltinType)V2->getZExtValue();
Chris Lattner5fa829c2002-07-30 16:24:28 +0000492 return ConstantBool::get(R);
Misha Brukmanfd939082005-04-21 23:48:37 +0000493 }
Chris Lattnerf59bd532001-07-21 19:10:49 +0000494
Reid Spencerb83eb642006-10-20 07:07:24 +0000495 static Constant *EqualTo(const ConstantInt *V1, const ConstantInt *V2) {
496 bool R = (BuiltinType)V1->getZExtValue() == (BuiltinType)V2->getZExtValue();
Chris Lattner5d5a7142003-11-17 20:19:35 +0000497 return ConstantBool::get(R);
498 }
499
Reid Spencerb83eb642006-10-20 07:07:24 +0000500 static Constant *CastToPointer(const ConstantInt *V,
Chris Lattner27287de2003-04-17 19:24:18 +0000501 const PointerType *PTy) {
Chris Lattner76ac1a42001-11-01 05:55:13 +0000502 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000503 return ConstantPointerNull::get(PTy);
Chris Lattner76ac1a42001-11-01 05:55:13 +0000504 return 0; // Can't const prop other types of pointers
505 }
506
Chris Lattnerf59bd532001-07-21 19:10:49 +0000507 // Casting operators. ick
508#define DEF_CAST(TYPE, CLASS, CTYPE) \
Reid Spencerb83eb642006-10-20 07:07:24 +0000509 static Constant *CastTo##TYPE (const ConstantInt *V) { \
Reid Spencer3da59db2006-11-27 01:05:10 +0000510 return CLASS::get(Type::TYPE##Ty, (CTYPE)((BuiltinType)V->getZExtValue()));\
Chris Lattnerf59bd532001-07-21 19:10:49 +0000511 }
512
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000513 DEF_CAST(Bool , ConstantBool, bool)
Reid Spencerb83eb642006-10-20 07:07:24 +0000514 DEF_CAST(SByte , ConstantInt, signed char)
515 DEF_CAST(UByte , ConstantInt, unsigned char)
516 DEF_CAST(Short , ConstantInt, signed short)
517 DEF_CAST(UShort, ConstantInt, unsigned short)
518 DEF_CAST(Int , ConstantInt, signed int)
519 DEF_CAST(UInt , ConstantInt, unsigned int)
520 DEF_CAST(Long , ConstantInt, int64_t)
521 DEF_CAST(ULong , ConstantInt, uint64_t)
522 DEF_CAST(Float , ConstantFP , float)
523 DEF_CAST(Double, ConstantFP , double)
Chris Lattnerf59bd532001-07-21 19:10:49 +0000524#undef DEF_CAST
Chris Lattner00950542001-06-06 20:29:01 +0000525
Reid Spencer1628cec2006-10-26 06:15:43 +0000526 static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000527 if (V2->isNullValue()) // X / 0
Chris Lattner893af8c2003-05-12 15:26:25 +0000528 return 0;
Reid Spencer1628cec2006-10-26 06:15:43 +0000529 BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue());
Reid Spencerb83eb642006-10-20 07:07:24 +0000530 return ConstantInt::get(*Ty, R);
Chris Lattner893af8c2003-05-12 15:26:25 +0000531 }
532
Reid Spencer1628cec2006-10-26 06:15:43 +0000533 static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer0a783f72006-11-02 01:53:59 +0000534 if (V2->isNullValue()) // X / 0
Reid Spencer1628cec2006-10-26 06:15:43 +0000535 return 0;
536 if (V2->isAllOnesValue() && // MIN_INT / -1
537 (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
538 return 0;
Reid Spencer0a783f72006-11-02 01:53:59 +0000539 BuiltinType R = (BuiltinType)(V1->getSExtValue() / V2->getSExtValue());
Reid Spencer1628cec2006-10-26 06:15:43 +0000540 return ConstantInt::get(*Ty, R);
541 }
542
Reid Spencer0a783f72006-11-02 01:53:59 +0000543 static Constant *URem(const ConstantInt *V1,
544 const ConstantInt *V2) {
Chris Lattner893af8c2003-05-12 15:26:25 +0000545 if (V2->isNullValue()) return 0; // X / 0
Reid Spencer0a783f72006-11-02 01:53:59 +0000546 BuiltinType R = (BuiltinType)(V1->getZExtValue() % V2->getZExtValue());
547 return ConstantInt::get(*Ty, R);
548 }
549
550 static Constant *SRem(const ConstantInt *V1,
551 const ConstantInt *V2) {
552 if (V2->isNullValue()) return 0; // X % 0
553 if (V2->isAllOnesValue() && // MIN_INT % -1
554 (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
Chris Lattner893af8c2003-05-12 15:26:25 +0000555 return 0;
Reid Spencer0a783f72006-11-02 01:53:59 +0000556 BuiltinType R = (BuiltinType)(V1->getSExtValue() % V2->getSExtValue());
Reid Spencerb83eb642006-10-20 07:07:24 +0000557 return ConstantInt::get(*Ty, R);
Chris Lattner44540dd2002-05-03 21:41:07 +0000558 }
Chris Lattnercf4929f2002-05-06 03:00:54 +0000559
Reid Spencerb83eb642006-10-20 07:07:24 +0000560 static Constant *And(const ConstantInt *V1, const ConstantInt *V2) {
561 BuiltinType R =
562 (BuiltinType)V1->getZExtValue() & (BuiltinType)V2->getZExtValue();
563 return ConstantInt::get(*Ty, R);
Chris Lattner5fa829c2002-07-30 16:24:28 +0000564 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000565 static Constant *Or(const ConstantInt *V1, const ConstantInt *V2) {
566 BuiltinType R =
567 (BuiltinType)V1->getZExtValue() | (BuiltinType)V2->getZExtValue();
568 return ConstantInt::get(*Ty, R);
Chris Lattner5fa829c2002-07-30 16:24:28 +0000569 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000570 static Constant *Xor(const ConstantInt *V1, const ConstantInt *V2) {
571 BuiltinType R =
572 (BuiltinType)V1->getZExtValue() ^ (BuiltinType)V2->getZExtValue();
573 return ConstantInt::get(*Ty, R);
Chris Lattnercf4929f2002-05-06 03:00:54 +0000574 }
575
Reid Spencerb83eb642006-10-20 07:07:24 +0000576 static Constant *Shl(const ConstantInt *V1, const ConstantInt *V2) {
577 BuiltinType R =
578 (BuiltinType)V1->getZExtValue() << (BuiltinType)V2->getZExtValue();
579 return ConstantInt::get(*Ty, R);
Chris Lattner5fa829c2002-07-30 16:24:28 +0000580 }
581
Reid Spencer3822ff52006-11-08 06:47:33 +0000582 static Constant *LShr(const ConstantInt *V1, const ConstantInt *V2) {
583 BuiltinType R = BuiltinType(V1->getZExtValue() >> V2->getZExtValue());
584 return ConstantInt::get(*Ty, R);
585 }
586
587 static Constant *AShr(const ConstantInt *V1, const ConstantInt *V2) {
588 BuiltinType R = BuiltinType(V1->getSExtValue() >> V2->getZExtValue());
Reid Spencerb83eb642006-10-20 07:07:24 +0000589 return ConstantInt::get(*Ty, R);
Chris Lattnercf4929f2002-05-06 03:00:54 +0000590 }
Chris Lattner44540dd2002-05-03 21:41:07 +0000591};
Chris Lattner51df6d52006-06-21 18:13:36 +0000592} // end anonymous namespace
Chris Lattner44540dd2002-05-03 21:41:07 +0000593
594
595//===----------------------------------------------------------------------===//
596// DirectFPRules Class
597//===----------------------------------------------------------------------===//
598//
Chris Lattnereab20b52004-01-12 22:07:24 +0000599/// DirectFPRules provides implementations of functions that are valid on
600/// floating point types, but not all types in general.
601///
Chris Lattner51df6d52006-06-21 18:13:36 +0000602namespace {
Reid Spencerb83eb642006-10-20 07:07:24 +0000603template <class BuiltinType, Type **Ty>
Chris Lattnerf190d382006-06-28 21:38:54 +0000604struct VISIBILITY_HIDDEN DirectFPRules
Reid Spencerb83eb642006-10-20 07:07:24 +0000605 : public TemplateRules<ConstantFP, DirectFPRules<BuiltinType, Ty> > {
606
607 static Constant *Add(const ConstantFP *V1, const ConstantFP *V2) {
608 BuiltinType R = (BuiltinType)V1->getValue() +
609 (BuiltinType)V2->getValue();
610 return ConstantFP::get(*Ty, R);
611 }
612
613 static Constant *Sub(const ConstantFP *V1, const ConstantFP *V2) {
614 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
615 return ConstantFP::get(*Ty, R);
616 }
617
618 static Constant *Mul(const ConstantFP *V1, const ConstantFP *V2) {
619 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
620 return ConstantFP::get(*Ty, R);
621 }
622
623 static Constant *LessThan(const ConstantFP *V1, const ConstantFP *V2) {
624 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
625 return ConstantBool::get(R);
626 }
627
628 static Constant *EqualTo(const ConstantFP *V1, const ConstantFP *V2) {
629 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
630 return ConstantBool::get(R);
631 }
632
633 static Constant *CastToPointer(const ConstantFP *V,
634 const PointerType *PTy) {
635 if (V->isNullValue()) // Is it a FP or Integral null value?
636 return ConstantPointerNull::get(PTy);
637 return 0; // Can't const prop other types of pointers
638 }
639
640 // Casting operators. ick
641#define DEF_CAST(TYPE, CLASS, CTYPE) \
642 static Constant *CastTo##TYPE (const ConstantFP *V) { \
643 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
644 }
645
646 DEF_CAST(Bool , ConstantBool, bool)
647 DEF_CAST(SByte , ConstantInt, signed char)
648 DEF_CAST(UByte , ConstantInt, unsigned char)
649 DEF_CAST(Short , ConstantInt, signed short)
650 DEF_CAST(UShort, ConstantInt, unsigned short)
651 DEF_CAST(Int , ConstantInt, signed int)
652 DEF_CAST(UInt , ConstantInt, unsigned int)
653 DEF_CAST(Long , ConstantInt, int64_t)
654 DEF_CAST(ULong , ConstantInt, uint64_t)
655 DEF_CAST(Float , ConstantFP , float)
656 DEF_CAST(Double, ConstantFP , double)
657#undef DEF_CAST
658
Reid Spencer0a783f72006-11-02 01:53:59 +0000659 static Constant *FRem(const ConstantFP *V1, const ConstantFP *V2) {
Chris Lattner44540dd2002-05-03 21:41:07 +0000660 if (V2->isNullValue()) return 0;
661 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
662 (BuiltinType)V2->getValue());
Reid Spencerb83eb642006-10-20 07:07:24 +0000663 return ConstantFP::get(*Ty, Result);
Chris Lattner44540dd2002-05-03 21:41:07 +0000664 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000665 static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) {
Jeff Cohena97e8db2005-05-03 03:13:01 +0000666 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
Reid Spencerb83eb642006-10-20 07:07:24 +0000667 if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf);
668 if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf);
Andrew Lenharth8b9e3e62005-05-02 21:25:47 +0000669 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000670 return ConstantFP::get(*Ty, R);
Andrew Lenharth8b9e3e62005-05-02 21:25:47 +0000671 }
Chris Lattner905f3112002-05-03 20:09:52 +0000672};
Chris Lattner51df6d52006-06-21 18:13:36 +0000673} // end anonymous namespace
Chris Lattner905f3112002-05-03 20:09:52 +0000674
Chris Lattner0eff5ad2006-10-13 17:22:21 +0000675static ManagedStatic<EmptyRules> EmptyR;
676static ManagedStatic<BoolRules> BoolR;
677static ManagedStatic<NullPointerRules> NullPointerR;
678static ManagedStatic<ConstantPackedRules> ConstantPackedR;
679static ManagedStatic<GeneralPackedRules> GeneralPackedR;
Reid Spencerb83eb642006-10-20 07:07:24 +0000680static ManagedStatic<DirectIntRules<signed char , &Type::SByteTy> > SByteR;
681static ManagedStatic<DirectIntRules<unsigned char , &Type::UByteTy> > UByteR;
682static ManagedStatic<DirectIntRules<signed short , &Type::ShortTy> > ShortR;
683static ManagedStatic<DirectIntRules<unsigned short, &Type::UShortTy> > UShortR;
684static ManagedStatic<DirectIntRules<signed int , &Type::IntTy> > IntR;
685static ManagedStatic<DirectIntRules<unsigned int , &Type::UIntTy> > UIntR;
686static ManagedStatic<DirectIntRules<int64_t , &Type::LongTy> > LongR;
687static ManagedStatic<DirectIntRules<uint64_t , &Type::ULongTy> > ULongR;
688static ManagedStatic<DirectFPRules <float , &Type::FloatTy> > FloatR;
689static ManagedStatic<DirectFPRules <double , &Type::DoubleTy> > DoubleR;
Chris Lattnereab20b52004-01-12 22:07:24 +0000690
691/// ConstRules::get - This method returns the constant rules implementation that
692/// implements the semantics of the two specified constants.
Chris Lattner4a0ecc82004-01-12 20:41:05 +0000693ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattnercf09f512003-11-17 19:19:32 +0000694 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattner71d37782004-10-16 23:31:32 +0000695 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
696 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner0eff5ad2006-10-13 17:22:21 +0000697 return *EmptyR;
Chris Lattner0dc39692003-11-17 19:05:17 +0000698
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000699 switch (V1->getType()->getTypeID()) {
Chris Lattner0dc39692003-11-17 19:05:17 +0000700 default: assert(0 && "Unknown value type for constant folding!");
Chris Lattner0eff5ad2006-10-13 17:22:21 +0000701 case Type::BoolTyID: return *BoolR;
702 case Type::PointerTyID: return *NullPointerR;
703 case Type::SByteTyID: return *SByteR;
704 case Type::UByteTyID: return *UByteR;
705 case Type::ShortTyID: return *ShortR;
706 case Type::UShortTyID: return *UShortR;
707 case Type::IntTyID: return *IntR;
708 case Type::UIntTyID: return *UIntR;
709 case Type::LongTyID: return *LongR;
710 case Type::ULongTyID: return *ULongR;
711 case Type::FloatTyID: return *FloatR;
712 case Type::DoubleTyID: return *DoubleR;
Chris Lattnerd15ed592006-01-04 02:03:29 +0000713 case Type::PackedTyID:
714 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
Chris Lattner0eff5ad2006-10-13 17:22:21 +0000715 return *ConstantPackedR;
716 return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner00950542001-06-06 20:29:01 +0000717 }
Chris Lattner00950542001-06-06 20:29:01 +0000718}
Chris Lattnereab20b52004-01-12 22:07:24 +0000719
720
721//===----------------------------------------------------------------------===//
722// ConstantFold*Instruction Implementations
723//===----------------------------------------------------------------------===//
Chris Lattnereab20b52004-01-12 22:07:24 +0000724
Chris Lattner4460f402006-04-02 01:38:28 +0000725/// CastConstantPacked - Convert the specified ConstantPacked node to the
726/// specified packed type. At this point, we know that the elements of the
727/// input packed constant are all simple integer or FP values.
728static Constant *CastConstantPacked(ConstantPacked *CP,
729 const PackedType *DstTy) {
730 unsigned SrcNumElts = CP->getType()->getNumElements();
731 unsigned DstNumElts = DstTy->getNumElements();
732 const Type *SrcEltTy = CP->getType()->getElementType();
733 const Type *DstEltTy = DstTy->getElementType();
734
735 // If both vectors have the same number of elements (thus, the elements
736 // are the same size), perform the conversion now.
737 if (SrcNumElts == DstNumElts) {
738 std::vector<Constant*> Result;
739
Reid Spencer3da59db2006-11-27 01:05:10 +0000740 // If the src and dest elements are both integers, or both floats, we can
741 // just BitCast each element because the elements are the same size.
742 if ((SrcEltTy->isIntegral() && DstEltTy->isIntegral()) ||
743 (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) {
Chris Lattner4460f402006-04-02 01:38:28 +0000744 for (unsigned i = 0; i != SrcNumElts; ++i)
Reid Spencer3da59db2006-11-27 01:05:10 +0000745 Result.push_back(
746 ConstantExpr::getCast(Instruction::BitCast, CP->getOperand(1),
747 DstEltTy));
Chris Lattner4460f402006-04-02 01:38:28 +0000748 return ConstantPacked::get(Result);
749 }
750
Reid Spencer3da59db2006-11-27 01:05:10 +0000751 // If this is an int-to-fp cast ..
Chris Lattner4460f402006-04-02 01:38:28 +0000752 if (SrcEltTy->isIntegral()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000753 // Ensure that it is int-to-fp cast
Chris Lattner4460f402006-04-02 01:38:28 +0000754 assert(DstEltTy->isFloatingPoint());
755 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
756 for (unsigned i = 0; i != SrcNumElts; ++i) {
757 double V =
Reid Spencerb83eb642006-10-20 07:07:24 +0000758 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner4460f402006-04-02 01:38:28 +0000759 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
760 }
761 return ConstantPacked::get(Result);
762 }
763 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
764 for (unsigned i = 0; i != SrcNumElts; ++i) {
765 float V =
Reid Spencerb83eb642006-10-20 07:07:24 +0000766 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner4460f402006-04-02 01:38:28 +0000767 Result.push_back(ConstantFP::get(Type::FloatTy, V));
768 }
769 return ConstantPacked::get(Result);
770 }
771
772 // Otherwise, this is an fp-to-int cast.
773 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
774
775 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
776 for (unsigned i = 0; i != SrcNumElts; ++i) {
777 uint64_t V =
778 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencerb83eb642006-10-20 07:07:24 +0000779 Constant *C = ConstantInt::get(Type::ULongTy, V);
Chris Lattner4460f402006-04-02 01:38:28 +0000780 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
781 }
782 return ConstantPacked::get(Result);
783 }
784
785 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
786 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000787 uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
788 Constant *C = ConstantInt::get(Type::UIntTy, V);
Chris Lattner4460f402006-04-02 01:38:28 +0000789 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
790 }
791 return ConstantPacked::get(Result);
792 }
793
794 // Otherwise, this is a cast that changes element count and size. Handle
795 // casts which shrink the elements here.
796
797 // FIXME: We need to know endianness to do this!
798
799 return 0;
800}
801
Reid Spencer3da59db2006-11-27 01:05:10 +0000802/// This function determines which opcode to use to fold two constant cast
803/// expressions together. It uses CastInst::isEliminableCastPair to determine
804/// the opcode. Consequently its just a wrapper around that function.
805/// @Determine if it is valid to fold a cast of a cast
806static unsigned
807foldConstantCastPair(
808 unsigned opc, ///< opcode of the second cast constant expression
809 const ConstantExpr*Op, ///< the first cast constant expression
810 const Type *DstTy ///< desintation type of the first cast
811) {
812 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
813 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
814 assert(CastInst::isCast(opc) && "Invalid cast opcode");
815
816 // The the types and opcodes for the two Cast constant expressions
817 const Type *SrcTy = Op->getOperand(0)->getType();
818 const Type *MidTy = Op->getType();
819 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
820 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner4460f402006-04-02 01:38:28 +0000821
Reid Spencer3da59db2006-11-27 01:05:10 +0000822 // Let CastInst::isEliminableCastPair do the heavy lifting.
823 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
824 Type::ULongTy);
825}
826
827Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattnereab20b52004-01-12 22:07:24 +0000828 const Type *DestTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000829 const Type *SrcTy = V->getType();
Chris Lattnereab20b52004-01-12 22:07:24 +0000830
Reid Spencer3da59db2006-11-27 01:05:10 +0000831 // Handle some simple cases
832 if (SrcTy == DestTy)
833 return (Constant*)V; // no-op cast
834
835 if (isa<UndefValue>(V))
836 return UndefValue::get(DestTy);
837
838 // If the cast operand is a constant expression, there's a few things we can
839 // do to try to simplify it.
840 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
841 if (CE->isCast()) {
842 // Try hard to fold cast of cast because they are almost always
843 // eliminable.
844 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
845 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattnereab20b52004-01-12 22:07:24 +0000846 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
847 // If all of the indexes in the GEP are null values, there is no pointer
848 // adjustment going on. We might as well cast the source pointer.
849 bool isAllNull = true;
850 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
851 if (!CE->getOperand(i)->isNullValue()) {
852 isAllNull = false;
853 break;
854 }
855 if (isAllNull)
856 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
857 }
Chris Lattner71d37782004-10-16 23:31:32 +0000858 }
Chris Lattnereab20b52004-01-12 22:07:24 +0000859
Reid Spencer3da59db2006-11-27 01:05:10 +0000860 // We actually have to do a cast now, but first, we might need to fix up
861 // the value of the operand.
862 switch (opc) {
863 case Instruction::FPTrunc:
864 case Instruction::Trunc:
865 case Instruction::FPExt:
866 break; // floating point input & output, no fixup needed
867 case Instruction::FPToUI: {
868 ConstRules &Rules = ConstRules::get(V, V);
869 V = Rules.castToULong(V); // make sure we get an unsigned value first
870 break;
871 }
872 case Instruction::FPToSI: {
873 ConstRules &Rules = ConstRules::get(V, V);
874 V = Rules.castToLong(V); // make sure we get a signed value first
875 break;
876 }
877 case Instruction::IntToPtr: //always treated as unsigned
878 case Instruction::UIToFP:
879 case Instruction::ZExt:
880 // A ZExt always produces an unsigned value so we need to cast the value
881 // now before we try to cast it to the destination type
882 if (isa<ConstantInt>(V))
883 V = ConstantInt::get(SrcTy->getUnsignedVersion(),
884 cast<ConstantIntegral>(V)->getZExtValue());
885 break;
886 case Instruction::SIToFP:
887 case Instruction::SExt:
888 // A SExt always produces a signed value so we need to cast the value
889 // now before we try to cast it to the destiniation type.
890 if (isa<ConstantInt>(V))
891 V = ConstantInt::get(SrcTy->getSignedVersion(),
892 cast<ConstantIntegral>(V)->getSExtValue());
893 break;
Chris Lattner1b9fe1c2004-11-17 17:59:35 +0000894
Reid Spencer3da59db2006-11-27 01:05:10 +0000895 case Instruction::PtrToInt:
896 // Cast of a global address to boolean is always true.
897 if (isa<GlobalValue>(V)) {
898 if (DestTy == Type::BoolTy)
899 // FIXME: When we support 'external weak' references, we have to
900 // prevent this transformation from happening. This code will need
901 // to be updated to ignore external weak symbols when we support it.
902 return ConstantBool::getTrue();
Chris Lattner1b9fe1c2004-11-17 17:59:35 +0000903 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000904 break;
905 case Instruction::BitCast:
906 // Check to see if we are casting a pointer to an aggregate to a pointer to
907 // the first element. If so, return the appropriate GEP instruction.
908 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
909 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
910 std::vector<Value*> IdxList;
911 IdxList.push_back(Constant::getNullValue(Type::IntTy));
912 const Type *ElTy = PTy->getElementType();
913 while (ElTy != DPTy->getElementType()) {
914 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
915 if (STy->getNumElements() == 0) break;
916 ElTy = STy->getElementType(0);
917 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
918 } else if (const SequentialType *STy =
919 dyn_cast<SequentialType>(ElTy)) {
920 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
921 ElTy = STy->getElementType();
922 IdxList.push_back(IdxList[0]);
923 } else {
Chris Lattner4460f402006-04-02 01:38:28 +0000924 break;
925 }
926 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000927
928 if (ElTy == DPTy->getElementType())
929 return ConstantExpr::getGetElementPtr(
930 const_cast<Constant*>(V),IdxList);
931 }
932
933 // Handle casts from one packed constant to another. We know that the src
934 // and dest type have the same size (otherwise its an illegal cast).
935 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
936 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
937 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
938 "Not cast between same sized vectors!");
939 // First, check for null and undef
940 if (isa<ConstantAggregateZero>(V))
941 return Constant::getNullValue(DestTy);
942 if (isa<UndefValue>(V))
943 return UndefValue::get(DestTy);
944
945 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
946 // This is a cast from a ConstantPacked of one type to a
947 // ConstantPacked of another type. Check to see if all elements of
948 // the input are simple.
949 bool AllSimpleConstants = true;
950 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
951 if (!isa<ConstantInt>(CP->getOperand(i)) &&
952 !isa<ConstantFP>(CP->getOperand(i))) {
953 AllSimpleConstants = false;
954 break;
955 }
956 }
957
958 // If all of the elements are simple constants, we can fold this.
959 if (AllSimpleConstants)
960 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
961 }
Chris Lattner4460f402006-04-02 01:38:28 +0000962 }
963 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000964
965 // Handle sign conversion for integer no-op casts. We need to cast the
966 // value to the correct signedness before we try to cast it to the
967 // destination type. Be careful to do this only for integer types.
968 if (isa<ConstantIntegral>(V) && SrcTy->isInteger()) {
969 if (SrcTy->isSigned())
970 V = ConstantInt::get(SrcTy->getUnsignedVersion(),
971 cast<ConstantIntegral>(V)->getZExtValue());
972 else
973 V = ConstantInt::get(SrcTy->getSignedVersion(),
974 cast<ConstantIntegral>(V)->getSExtValue());
975 }
976 break;
977 default:
978 assert(!"Invalid CE CastInst opcode");
979 break;
Chris Lattner4460f402006-04-02 01:38:28 +0000980 }
Chris Lattnerf8d10972004-10-11 03:57:30 +0000981
Reid Spencer3da59db2006-11-27 01:05:10 +0000982 // Okay, no more folding possible, time to cast
Chris Lattnereab20b52004-01-12 22:07:24 +0000983 ConstRules &Rules = ConstRules::get(V, V);
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000984 switch (DestTy->getTypeID()) {
Chris Lattnereab20b52004-01-12 22:07:24 +0000985 case Type::BoolTyID: return Rules.castToBool(V);
986 case Type::UByteTyID: return Rules.castToUByte(V);
987 case Type::SByteTyID: return Rules.castToSByte(V);
988 case Type::UShortTyID: return Rules.castToUShort(V);
989 case Type::ShortTyID: return Rules.castToShort(V);
990 case Type::UIntTyID: return Rules.castToUInt(V);
991 case Type::IntTyID: return Rules.castToInt(V);
992 case Type::ULongTyID: return Rules.castToULong(V);
993 case Type::LongTyID: return Rules.castToLong(V);
994 case Type::FloatTyID: return Rules.castToFloat(V);
995 case Type::DoubleTyID: return Rules.castToDouble(V);
996 case Type::PointerTyID:
997 return Rules.castToPointer(V, cast<PointerType>(DestTy));
Reid Spencer3da59db2006-11-27 01:05:10 +0000998 // what about packed ?
Chris Lattnereab20b52004-01-12 22:07:24 +0000999 default: return 0;
1000 }
1001}
1002
Chris Lattnere9714862004-03-12 05:53:32 +00001003Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
1004 const Constant *V1,
1005 const Constant *V2) {
Chris Lattner850ba992006-09-28 23:34:49 +00001006 if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
1007 return const_cast<Constant*>(CB->getValue() ? V1 : V2);
Chris Lattner71d37782004-10-16 23:31:32 +00001008
1009 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
1010 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
1011 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattner2f690c82006-01-05 07:49:30 +00001012 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattnere9714862004-03-12 05:53:32 +00001013 return 0;
1014}
1015
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001016Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
1017 const Constant *Idx) {
Chris Lattner6fa4cdf2006-03-31 18:31:40 +00001018 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
1019 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnerf38d4712006-04-07 04:44:06 +00001020 if (Val->isNullValue()) // ee(zero, x) -> zero
1021 return Constant::getNullValue(
1022 cast<PackedType>(Val->getType())->getElementType());
Chris Lattner6fa4cdf2006-03-31 18:31:40 +00001023
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001024 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencerb83eb642006-10-20 07:07:24 +00001025 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
1026 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattner6fa4cdf2006-03-31 18:31:40 +00001027 } else if (isa<UndefValue>(Idx)) {
1028 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
1029 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001030 }
Chris Lattner6fa4cdf2006-03-31 18:31:40 +00001031 }
Robert Bocchinobb90a7a2006-01-10 20:03:46 +00001032 return 0;
1033}
1034
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001035Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
1036 const Constant *Elt,
1037 const Constant *Idx) {
Reid Spencerb83eb642006-10-20 07:07:24 +00001038 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001039 if (!CIdx) return 0;
Reid Spencerb83eb642006-10-20 07:07:24 +00001040 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer4ab09d42006-11-02 08:18:15 +00001041 if (isa<UndefValue>(Val)) {
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001042 // Insertion of scalar constant into packed undef
1043 // Optimize away insertion of undef
1044 if (isa<UndefValue>(Elt))
1045 return const_cast<Constant*>(Val);
1046 // Otherwise break the aggregate undef into multiple undefs and do
1047 // the insertion
1048 unsigned numOps =
1049 cast<PackedType>(Val->getType())->getNumElements();
1050 std::vector<Constant*> Ops;
1051 Ops.reserve(numOps);
1052 for (unsigned i = 0; i < numOps; ++i) {
1053 const Constant *Op =
1054 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
1055 Ops.push_back(const_cast<Constant*>(Op));
1056 }
1057 return ConstantPacked::get(Ops);
1058 }
Reid Spencer4ab09d42006-11-02 08:18:15 +00001059 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001060 // Insertion of scalar constant into packed aggregate zero
1061 // Optimize away insertion of zero
1062 if (Elt->isNullValue())
1063 return const_cast<Constant*>(Val);
1064 // Otherwise break the aggregate zero into multiple zeros and do
1065 // the insertion
1066 unsigned numOps =
1067 cast<PackedType>(Val->getType())->getNumElements();
1068 std::vector<Constant*> Ops;
1069 Ops.reserve(numOps);
1070 for (unsigned i = 0; i < numOps; ++i) {
1071 const Constant *Op =
1072 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
1073 Ops.push_back(const_cast<Constant*>(Op));
1074 }
1075 return ConstantPacked::get(Ops);
1076 }
1077 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
1078 // Insertion of scalar constant into packed constant
1079 std::vector<Constant*> Ops;
1080 Ops.reserve(CVal->getNumOperands());
1081 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
1082 const Constant *Op =
1083 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
1084 Ops.push_back(const_cast<Constant*>(Op));
1085 }
1086 return ConstantPacked::get(Ops);
1087 }
1088 return 0;
1089}
1090
Chris Lattner00f10232006-04-08 01:18:18 +00001091Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
1092 const Constant *V2,
1093 const Constant *Mask) {
1094 // TODO:
1095 return 0;
1096}
1097
1098
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001099/// isZeroSizedType - This type is zero sized if its an array or structure of
1100/// zero sized types. The only leaf zero sized type is an empty structure.
1101static bool isMaybeZeroSizedType(const Type *Ty) {
1102 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1103 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1104
1105 // If all of elements have zero size, this does too.
1106 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerf4aa3352005-01-28 23:17:27 +00001107 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001108 return true;
1109
1110 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1111 return isMaybeZeroSizedType(ATy->getElementType());
1112 }
1113 return false;
1114}
Chris Lattnere9714862004-03-12 05:53:32 +00001115
Chris Lattner504e8fb2004-01-13 05:51:55 +00001116/// IdxCompare - Compare the two constants as though they were getelementptr
1117/// indices. This allows coersion of the types to be the same thing.
1118///
1119/// If the two constants are the "same" (after coersion), return 0. If the
1120/// first is less than the second, return -1, if the second is less than the
1121/// first, return 1. If the constants are not integral, return -2.
1122///
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001123static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001124 if (C1 == C2) return 0;
1125
Reid Spencer3da59db2006-11-27 01:05:10 +00001126 // Ok, we found a different index. Are either of the operands ConstantExprs?
1127 // If so, we can't do anything with them.
Chris Lattner504e8fb2004-01-13 05:51:55 +00001128 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1129 return -2; // don't know!
Misha Brukmanfd939082005-04-21 23:48:37 +00001130
Chris Lattner28977af2004-04-05 01:30:19 +00001131 // Ok, we have two differing integer indices. Sign extend them to be the same
1132 // type. Long is always big enough, so we use it.
Reid Spencer3da59db2006-11-27 01:05:10 +00001133 if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
1134 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
1135 else
1136 C1 = ConstantExpr::getBitCast(C1, Type::LongTy);
1137 if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
1138 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
1139 else
1140 C2 = ConstantExpr::getBitCast(C2, Type::LongTy);
1141
Chris Lattner504e8fb2004-01-13 05:51:55 +00001142 if (C1 == C2) return 0; // Are they just differing types?
1143
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001144 // If the type being indexed over is really just a zero sized type, there is
1145 // no pointer difference being made here.
1146 if (isMaybeZeroSizedType(ElTy))
1147 return -2; // dunno.
1148
Chris Lattner504e8fb2004-01-13 05:51:55 +00001149 // If they are really different, now that they are the same type, then we
1150 // found a difference!
Reid Spencerb83eb642006-10-20 07:07:24 +00001151 if (cast<ConstantInt>(C1)->getSExtValue() <
1152 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner504e8fb2004-01-13 05:51:55 +00001153 return -1;
1154 else
1155 return 1;
1156}
1157
1158/// evaluateRelation - This function determines if there is anything we can
1159/// decide about the two constants provided. This doesn't need to handle simple
Reid Spencer79703962004-07-17 23:47:01 +00001160/// things like integer comparisons, but should instead handle ConstantExprs
1161/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner504e8fb2004-01-13 05:51:55 +00001162/// particular relation to each other, we should return the corresponding SetCC
1163/// code, otherwise return Instruction::BinaryOpsEnd.
1164///
1165/// To simplify this code we canonicalize the relation so that the first
1166/// operand is always the most "complex" of the two. We consider simple
1167/// constants (like ConstantInt) to be the simplest, followed by
Reid Spencer79703962004-07-17 23:47:01 +00001168/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner504e8fb2004-01-13 05:51:55 +00001169///
Chris Lattner2f690c82006-01-05 07:49:30 +00001170static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001171 assert(V1->getType() == V2->getType() &&
1172 "Cannot compare different types of values!");
1173 if (V1 == V2) return Instruction::SetEQ;
1174
Reid Spencer79703962004-07-17 23:47:01 +00001175 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattner2f690c82006-01-05 07:49:30 +00001176 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1177 // We distilled this down to a simple case, use the standard constant
1178 // folder.
1179 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
Chris Lattner850ba992006-09-28 23:34:49 +00001180 if (R && R->getValue()) return Instruction::SetEQ;
Chris Lattner2f690c82006-01-05 07:49:30 +00001181 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
Chris Lattner850ba992006-09-28 23:34:49 +00001182 if (R && R->getValue()) return Instruction::SetLT;
Chris Lattner2f690c82006-01-05 07:49:30 +00001183 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
Chris Lattner850ba992006-09-28 23:34:49 +00001184 if (R && R->getValue()) return Instruction::SetGT;
Chris Lattner2f690c82006-01-05 07:49:30 +00001185
1186 // If we couldn't figure it out, bail.
1187 return Instruction::BinaryOpsEnd;
1188 }
1189
Chris Lattner504e8fb2004-01-13 05:51:55 +00001190 // If the first operand is simple, swap operands.
Chris Lattnerb97e2782004-02-01 01:23:19 +00001191 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1192 if (SwappedRelation != Instruction::BinaryOpsEnd)
1193 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner504e8fb2004-01-13 05:51:55 +00001194
Chris Lattner2c822cc2006-01-05 07:19:51 +00001195 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattnerb97e2782004-02-01 01:23:19 +00001196 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner2c822cc2006-01-05 07:19:51 +00001197 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1198 if (SwappedRelation != Instruction::BinaryOpsEnd)
1199 return SetCondInst::getSwappedCondition(SwappedRelation);
1200 else
1201 return Instruction::BinaryOpsEnd;
Chris Lattnerb97e2782004-02-01 01:23:19 +00001202 }
Chris Lattner504e8fb2004-01-13 05:51:55 +00001203
Reid Spencer79703962004-07-17 23:47:01 +00001204 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner504e8fb2004-01-13 05:51:55 +00001205 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spencer79703962004-07-17 23:47:01 +00001206 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
1207 assert(CPR1 != CPR2 &&
1208 "GVs for the same value exist at different addresses??");
Chris Lattner504e8fb2004-01-13 05:51:55 +00001209 // FIXME: If both globals are external weak, they might both be null!
1210 return Instruction::SetNE;
1211 } else {
1212 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1213 // Global can never be null. FIXME: if we implement external weak
1214 // linkage, this is not necessarily true!
1215 return Instruction::SetNE;
1216 }
1217
1218 } else {
1219 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1220 // constantexpr, a CPR, or a simple constant.
Chris Lattner2f690c82006-01-05 07:49:30 +00001221 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner504e8fb2004-01-13 05:51:55 +00001222 Constant *CE1Op0 = CE1->getOperand(0);
1223
1224 switch (CE1->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +00001225 case Instruction::Trunc:
1226 case Instruction::FPTrunc:
1227 case Instruction::FPExt:
1228 case Instruction::FPToUI:
1229 case Instruction::FPToSI:
1230 break; // We don't do anything with floating point.
1231 case Instruction::ZExt:
1232 case Instruction::SExt:
1233 case Instruction::UIToFP:
1234 case Instruction::SIToFP:
1235 case Instruction::PtrToInt:
1236 case Instruction::IntToPtr:
1237 case Instruction::BitCast:
Chris Lattner504e8fb2004-01-13 05:51:55 +00001238 // If the cast is not actually changing bits, and the second operand is a
1239 // null pointer, do the comparison with the pre-casted value.
1240 if (V2->isNullValue() &&
Chris Lattner2f690c82006-01-05 07:49:30 +00001241 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner504e8fb2004-01-13 05:51:55 +00001242 return evaluateRelation(CE1Op0,
1243 Constant::getNullValue(CE1Op0->getType()));
Chris Lattner2f690c82006-01-05 07:49:30 +00001244
1245 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1246 // from the same type as the src of the LHS, evaluate the inputs. This is
1247 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1248 // which happens a lot in compilers with tagged integers.
1249 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer3da59db2006-11-27 01:05:10 +00001250 if (isa<PointerType>(CE1->getType()) && CE2->isCast() &&
Chris Lattner2f690c82006-01-05 07:49:30 +00001251 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1252 CE1->getOperand(0)->getType()->isIntegral()) {
1253 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1254 }
Chris Lattnera0ae8192004-04-11 01:29:30 +00001255 break;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001256
1257 case Instruction::GetElementPtr:
1258 // Ok, since this is a getelementptr, we know that the constant has a
1259 // pointer type. Check the various cases.
1260 if (isa<ConstantPointerNull>(V2)) {
1261 // If we are comparing a GEP to a null pointer, check to see if the base
1262 // of the GEP equals the null pointer.
Reid Spencer79703962004-07-17 23:47:01 +00001263 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001264 // FIXME: this is not true when we have external weak references!
1265 // No offset can go from a global to a null pointer.
1266 return Instruction::SetGT;
1267 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1268 // If we are indexing from a null pointer, check to see if we have any
1269 // non-zero indices.
1270 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1271 if (!CE1->getOperand(i)->isNullValue())
1272 // Offsetting from null, must not be equal.
1273 return Instruction::SetGT;
1274 // Only zero indexes from null, must still be zero.
1275 return Instruction::SetEQ;
1276 }
1277 // Otherwise, we can't really say if the first operand is null or not.
Reid Spencer79703962004-07-17 23:47:01 +00001278 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001279 if (isa<ConstantPointerNull>(CE1Op0)) {
1280 // FIXME: This is not true with external weak references.
1281 return Instruction::SetLT;
Reid Spencer79703962004-07-17 23:47:01 +00001282 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001283 if (CPR1 == CPR2) {
1284 // If this is a getelementptr of the same global, then it must be
1285 // different. Because the types must match, the getelementptr could
1286 // only have at most one index, and because we fold getelementptr's
1287 // with a single zero index, it must be nonzero.
1288 assert(CE1->getNumOperands() == 2 &&
1289 !CE1->getOperand(1)->isNullValue() &&
1290 "Suprising getelementptr!");
1291 return Instruction::SetGT;
1292 } else {
1293 // If they are different globals, we don't know what the value is,
1294 // but they can't be equal.
1295 return Instruction::SetNE;
1296 }
1297 }
1298 } else {
1299 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1300 const Constant *CE2Op0 = CE2->getOperand(0);
1301
1302 // There are MANY other foldings that we could perform here. They will
1303 // probably be added on demand, as they seem needed.
1304 switch (CE2->getOpcode()) {
1305 default: break;
1306 case Instruction::GetElementPtr:
1307 // By far the most common case to handle is when the base pointers are
1308 // obviously to the same or different globals.
Reid Spencer79703962004-07-17 23:47:01 +00001309 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001310 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1311 return Instruction::SetNE;
1312 // Ok, we know that both getelementptr instructions are based on the
1313 // same global. From this, we can precisely determine the relative
1314 // ordering of the resultant pointers.
1315 unsigned i = 1;
Misha Brukmanfd939082005-04-21 23:48:37 +00001316
Chris Lattner504e8fb2004-01-13 05:51:55 +00001317 // Compare all of the operands the GEP's have in common.
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001318 gep_type_iterator GTI = gep_type_begin(CE1);
1319 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1320 ++i, ++GTI)
1321 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1322 GTI.getIndexedType())) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001323 case -1: return Instruction::SetLT;
1324 case 1: return Instruction::SetGT;
1325 case -2: return Instruction::BinaryOpsEnd;
1326 }
1327
1328 // Ok, we ran out of things they have in common. If any leftovers
1329 // are non-zero then we have a difference, otherwise we are equal.
1330 for (; i < CE1->getNumOperands(); ++i)
1331 if (!CE1->getOperand(i)->isNullValue())
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001332 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1333 return Instruction::SetGT;
1334 else
1335 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanfd939082005-04-21 23:48:37 +00001336
Chris Lattner504e8fb2004-01-13 05:51:55 +00001337 for (; i < CE2->getNumOperands(); ++i)
1338 if (!CE2->getOperand(i)->isNullValue())
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001339 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1340 return Instruction::SetLT;
1341 else
1342 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner504e8fb2004-01-13 05:51:55 +00001343 return Instruction::SetEQ;
1344 }
1345 }
1346 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001347
Chris Lattner504e8fb2004-01-13 05:51:55 +00001348 default:
1349 break;
1350 }
1351 }
1352
1353 return Instruction::BinaryOpsEnd;
1354}
1355
Chris Lattnereab20b52004-01-12 22:07:24 +00001356Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1357 const Constant *V1,
1358 const Constant *V2) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001359 Constant *C = 0;
Chris Lattnereab20b52004-01-12 22:07:24 +00001360 switch (Opcode) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001361 default: break;
1362 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1363 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1364 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001365 case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break;
1366 case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break;
1367 case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001368 case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break;
1369 case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break;
1370 case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001371 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1372 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1373 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1374 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001375 case Instruction::LShr: C = ConstRules::get(V1, V2).lshr(V1, V2); break;
1376 case Instruction::AShr: C = ConstRules::get(V1, V2).ashr(V1, V2); break;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001377 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
1378 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1379 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattnereab20b52004-01-12 22:07:24 +00001380 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
1381 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner84e10642006-01-04 02:20:54 +00001382 if (C) return ConstantExpr::getNot(C);
Chris Lattnereab20b52004-01-12 22:07:24 +00001383 break;
1384 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1385 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner84e10642006-01-04 02:20:54 +00001386 if (C) return ConstantExpr::getNot(C);
Chris Lattnereab20b52004-01-12 22:07:24 +00001387 break;
1388 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1389 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner84e10642006-01-04 02:20:54 +00001390 if (C) return ConstantExpr::getNot(C);
Chris Lattnereab20b52004-01-12 22:07:24 +00001391 break;
1392 }
1393
Chris Lattner504e8fb2004-01-13 05:51:55 +00001394 // If we successfully folded the expression, return it now.
1395 if (C) return C;
1396
Chris Lattnera5b07402006-09-17 19:14:47 +00001397 if (SetCondInst::isComparison(Opcode)) {
Chris Lattner35f92ae2004-10-17 04:01:51 +00001398 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1399 return UndefValue::get(Type::BoolTy);
Chris Lattner2f690c82006-01-05 07:49:30 +00001400 switch (evaluateRelation(const_cast<Constant*>(V1),
1401 const_cast<Constant*>(V2))) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001402 default: assert(0 && "Unknown relational!");
1403 case Instruction::BinaryOpsEnd:
1404 break; // Couldn't determine anything about these constants.
1405 case Instruction::SetEQ: // We know the constants are equal!
1406 // If we know the constants are equal, we can decide the result of this
1407 // computation precisely.
1408 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1409 Opcode == Instruction::SetLE ||
1410 Opcode == Instruction::SetGE);
1411 case Instruction::SetLT:
1412 // If we know that V1 < V2, we can decide the result of this computation
1413 // precisely.
1414 return ConstantBool::get(Opcode == Instruction::SetLT ||
1415 Opcode == Instruction::SetNE ||
1416 Opcode == Instruction::SetLE);
1417 case Instruction::SetGT:
1418 // If we know that V1 > V2, we can decide the result of this computation
1419 // precisely.
1420 return ConstantBool::get(Opcode == Instruction::SetGT ||
1421 Opcode == Instruction::SetNE ||
1422 Opcode == Instruction::SetGE);
1423 case Instruction::SetLE:
1424 // If we know that V1 <= V2, we can only partially decide this relation.
Chris Lattner850ba992006-09-28 23:34:49 +00001425 if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
1426 if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
Chris Lattner504e8fb2004-01-13 05:51:55 +00001427 break;
1428
1429 case Instruction::SetGE:
1430 // If we know that V1 >= V2, we can only partially decide this relation.
Chris Lattner850ba992006-09-28 23:34:49 +00001431 if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
1432 if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
Chris Lattner504e8fb2004-01-13 05:51:55 +00001433 break;
Misha Brukmanfd939082005-04-21 23:48:37 +00001434
Chris Lattner504e8fb2004-01-13 05:51:55 +00001435 case Instruction::SetNE:
1436 // If we know that V1 != V2, we can only partially decide this relation.
Chris Lattner850ba992006-09-28 23:34:49 +00001437 if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
1438 if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
Chris Lattner504e8fb2004-01-13 05:51:55 +00001439 break;
1440 }
Chris Lattner35f92ae2004-10-17 04:01:51 +00001441 }
Chris Lattner504e8fb2004-01-13 05:51:55 +00001442
Chris Lattner71d37782004-10-16 23:31:32 +00001443 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1444 switch (Opcode) {
1445 case Instruction::Add:
1446 case Instruction::Sub:
Chris Lattner71d37782004-10-16 23:31:32 +00001447 case Instruction::Xor:
1448 return UndefValue::get(V1->getType());
1449
1450 case Instruction::Mul:
1451 case Instruction::And:
1452 return Constant::getNullValue(V1->getType());
Reid Spencer1628cec2006-10-26 06:15:43 +00001453 case Instruction::UDiv:
1454 case Instruction::SDiv:
1455 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001456 case Instruction::URem:
1457 case Instruction::SRem:
1458 case Instruction::FRem:
1459 if (!isa<UndefValue>(V2)) // undef / X -> 0
Chris Lattner71d37782004-10-16 23:31:32 +00001460 return Constant::getNullValue(V1->getType());
Reid Spencer0a783f72006-11-02 01:53:59 +00001461 return const_cast<Constant*>(V2); // X / undef -> undef
1462 case Instruction::Or: // X | undef -> -1
Chris Lattner71d37782004-10-16 23:31:32 +00001463 return ConstantInt::getAllOnesValue(V1->getType());
Reid Spencer3822ff52006-11-08 06:47:33 +00001464 case Instruction::LShr:
1465 if (isa<UndefValue>(V2) && isa<UndefValue>(V1))
1466 return const_cast<Constant*>(V1); // undef lshr undef -> undef
1467 return Constant::getNullValue(V1->getType()); // X lshr undef -> 0
1468 // undef lshr X -> 0
1469 case Instruction::AShr:
1470 if (!isa<UndefValue>(V2))
1471 return const_cast<Constant*>(V1); // undef ashr X --> undef
1472 else if (isa<UndefValue>(V1))
1473 return const_cast<Constant*>(V1); // undef ashr undef -> undef
1474 else
1475 return const_cast<Constant*>(V1); // X ashr undef --> X
Chris Lattner71d37782004-10-16 23:31:32 +00001476 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001477 // undef << X -> 0 or X << undef -> 0
Chris Lattner71d37782004-10-16 23:31:32 +00001478 return Constant::getNullValue(V1->getType());
1479 }
1480 }
1481
Chris Lattner504e8fb2004-01-13 05:51:55 +00001482 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
Reid Spencer4ab09d42006-11-02 08:18:15 +00001483 if (isa<ConstantExpr>(V2)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001484 // There are many possible foldings we could do here. We should probably
1485 // at least fold add of a pointer with an integer into the appropriate
1486 // getelementptr. This will improve alias analysis a bit.
Chris Lattner504e8fb2004-01-13 05:51:55 +00001487 } else {
1488 // Just implement a couple of simple identities.
1489 switch (Opcode) {
1490 case Instruction::Add:
1491 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1492 break;
1493 case Instruction::Sub:
1494 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1495 break;
1496 case Instruction::Mul:
1497 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1498 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencerb83eb642006-10-20 07:07:24 +00001499 if (CI->getZExtValue() == 1)
Chris Lattner504e8fb2004-01-13 05:51:55 +00001500 return const_cast<Constant*>(V1); // X * 1 == X
1501 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001502 case Instruction::UDiv:
1503 case Instruction::SDiv:
Chris Lattner504e8fb2004-01-13 05:51:55 +00001504 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencerb83eb642006-10-20 07:07:24 +00001505 if (CI->getZExtValue() == 1)
Chris Lattner504e8fb2004-01-13 05:51:55 +00001506 return const_cast<Constant*>(V1); // X / 1 == X
1507 break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001508 case Instruction::URem:
1509 case Instruction::SRem:
Chris Lattner504e8fb2004-01-13 05:51:55 +00001510 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencerb83eb642006-10-20 07:07:24 +00001511 if (CI->getZExtValue() == 1)
Reid Spencer0a783f72006-11-02 01:53:59 +00001512 return Constant::getNullValue(CI->getType()); // X % 1 == 0
Chris Lattner504e8fb2004-01-13 05:51:55 +00001513 break;
1514 case Instruction::And:
1515 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1516 return const_cast<Constant*>(V1); // X & -1 == X
1517 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Reid Spencer3da59db2006-11-27 01:05:10 +00001518 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner7fa6e662004-10-11 22:52:25 +00001519 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattner802b5ab2004-03-08 06:17:35 +00001520
1521 // Functions are at least 4-byte aligned. If and'ing the address of a
1522 // function with a constant < 4, fold it to zero.
1523 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencerb83eb642006-10-20 07:07:24 +00001524 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
Chris Lattner802b5ab2004-03-08 06:17:35 +00001525 return Constant::getNullValue(CI->getType());
1526 }
Chris Lattner504e8fb2004-01-13 05:51:55 +00001527 break;
1528 case Instruction::Or:
1529 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1530 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1531 return const_cast<Constant*>(V2); // X | -1 == -1
1532 break;
1533 case Instruction::Xor:
1534 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1535 break;
1536 }
1537 }
1538
Reid Spencer4ab09d42006-11-02 08:18:15 +00001539 } else if (isa<ConstantExpr>(V2)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001540 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1541 // other way if possible.
1542 switch (Opcode) {
1543 case Instruction::Add:
1544 case Instruction::Mul:
1545 case Instruction::And:
1546 case Instruction::Or:
1547 case Instruction::Xor:
1548 case Instruction::SetEQ:
1549 case Instruction::SetNE:
1550 // No change of opcode required.
1551 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1552
1553 case Instruction::SetLT:
1554 case Instruction::SetGT:
1555 case Instruction::SetLE:
1556 case Instruction::SetGE:
1557 // Change the opcode as necessary to swap the operands.
1558 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1559 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1560
1561 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001562 case Instruction::LShr:
1563 case Instruction::AShr:
Chris Lattner504e8fb2004-01-13 05:51:55 +00001564 case Instruction::Sub:
Reid Spencer1628cec2006-10-26 06:15:43 +00001565 case Instruction::SDiv:
1566 case Instruction::UDiv:
1567 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001568 case Instruction::URem:
1569 case Instruction::SRem:
1570 case Instruction::FRem:
Chris Lattner504e8fb2004-01-13 05:51:55 +00001571 default: // These instructions cannot be flopped around.
1572 break;
1573 }
1574 }
1575 return 0;
Chris Lattnereab20b52004-01-12 22:07:24 +00001576}
1577
1578Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner7fa6e662004-10-11 22:52:25 +00001579 const std::vector<Value*> &IdxList) {
Chris Lattnereab20b52004-01-12 22:07:24 +00001580 if (IdxList.size() == 0 ||
Chris Lattner7fa6e662004-10-11 22:52:25 +00001581 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattnereab20b52004-01-12 22:07:24 +00001582 return const_cast<Constant*>(C);
1583
Chris Lattnercfbf9fa2004-10-17 21:54:55 +00001584 if (isa<UndefValue>(C)) {
1585 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1586 true);
1587 assert(Ty != 0 && "Invalid indices for GEP!");
1588 return UndefValue::get(PointerType::get(Ty));
1589 }
1590
1591 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001592 if (C->isNullValue()) {
1593 bool isNull = true;
1594 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner7fa6e662004-10-11 22:52:25 +00001595 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001596 isNull = false;
1597 break;
1598 }
1599 if (isNull) {
Chris Lattner7fa6e662004-10-11 22:52:25 +00001600 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001601 true);
1602 assert(Ty != 0 && "Invalid indices for GEP!");
1603 return ConstantPointerNull::get(PointerType::get(Ty));
1604 }
Chris Lattnerdd684692004-07-15 01:16:59 +00001605
1606 if (IdxList.size() == 1) {
1607 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
Reid Spencerb83eb642006-10-20 07:07:24 +00001608 if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
Chris Lattnerdd684692004-07-15 01:16:59 +00001609 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1610 // type, we can statically fold this.
Reid Spencerb83eb642006-10-20 07:07:24 +00001611 Constant *R = ConstantInt::get(Type::UIntTy, ElSize);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001612 R = ConstantExpr::getCast(R, Idx0->getType());
1613 R = ConstantExpr::getMul(R, Idx0);
Chris Lattnerdd684692004-07-15 01:16:59 +00001614 return ConstantExpr::getCast(R, C->getType());
1615 }
1616 }
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001617 }
Chris Lattnereab20b52004-01-12 22:07:24 +00001618
1619 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1620 // Combine Indices - If the source pointer to this getelementptr instruction
1621 // is a getelementptr instruction, combine the indices of the two
1622 // getelementptr instructions into a single instruction.
1623 //
1624 if (CE->getOpcode() == Instruction::GetElementPtr) {
1625 const Type *LastTy = 0;
1626 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1627 I != E; ++I)
1628 LastTy = *I;
1629
Chris Lattner7fa6e662004-10-11 22:52:25 +00001630 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1631 std::vector<Value*> NewIndices;
Chris Lattnereab20b52004-01-12 22:07:24 +00001632 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1633 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner7fa6e662004-10-11 22:52:25 +00001634 NewIndices.push_back(CE->getOperand(i));
Chris Lattnereab20b52004-01-12 22:07:24 +00001635
1636 // Add the last index of the source with the first index of the new GEP.
1637 // Make sure to handle the case when they are actually different types.
1638 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001639 // Otherwise it must be an array.
1640 if (!Idx0->isNullValue()) {
Chris Lattnerd3408672004-07-07 04:45:13 +00001641 const Type *IdxTy = Combined->getType();
Chris Lattner7fa6e662004-10-11 22:52:25 +00001642 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Misha Brukmanfd939082005-04-21 23:48:37 +00001643 Combined =
Chris Lattnereab20b52004-01-12 22:07:24 +00001644 ConstantExpr::get(Instruction::Add,
Chris Lattner7fa6e662004-10-11 22:52:25 +00001645 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattnerd3408672004-07-07 04:45:13 +00001646 ConstantExpr::getCast(Combined, IdxTy));
1647 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001648
Chris Lattnereab20b52004-01-12 22:07:24 +00001649 NewIndices.push_back(Combined);
1650 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1651 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1652 }
1653 }
1654
1655 // Implement folding of:
1656 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1657 // long 0, long 0)
1658 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1659 //
Reid Spencer3da59db2006-11-27 01:05:10 +00001660 if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue())
Misha Brukmanfd939082005-04-21 23:48:37 +00001661 if (const PointerType *SPT =
Chris Lattnereab20b52004-01-12 22:07:24 +00001662 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1663 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1664 if (const ArrayType *CAT =
Chris Lattnerf190d382006-06-28 21:38:54 +00001665 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattnereab20b52004-01-12 22:07:24 +00001666 if (CAT->getElementType() == SAT->getElementType())
1667 return ConstantExpr::getGetElementPtr(
1668 (Constant*)CE->getOperand(0), IdxList);
1669 }
1670 return 0;
1671}
1672