blob: 170df73272330a34d597348d7e739fc81578d758 [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.cpp - LLVM constant folder -------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
11// (internal) ConstantFolding.h interface, which is used by the
12// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner5a945e32004-01-12 21:13:12 +000021#include "ConstantFolding.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattner057083f2006-10-13 17:22:21 +000027#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000030#include <limits>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000031using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000032
Chris Lattner5a945e32004-01-12 21:13:12 +000033namespace {
Chris Lattner02157b02006-06-28 21:38:54 +000034 struct VISIBILITY_HIDDEN ConstRules {
Chris Lattner5a945e32004-01-12 21:13:12 +000035 ConstRules() {}
Reid Spencer9c47b252005-04-24 22:27:20 +000036 virtual ~ConstRules() {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000037
Chris Lattner5a945e32004-01-12 21:13:12 +000038 // Binary Operators...
39 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
40 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
Reid Spencer7eb55b32006-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 Spencer7e80b0b2006-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 Lattner5a945e32004-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 Spencerfdff9382006-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 Lattner5a945e32004-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 Brukmanb1c93172005-04-21 23:48:37 +000071
Chris Lattner5a945e32004-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 Lattner2f7c9632001-06-06 20:29:01 +000083//===----------------------------------------------------------------------===//
84// TemplateRules Class
85//===----------------------------------------------------------------------===//
86//
Misha Brukmanb1c93172005-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 Lattner2f7c9632001-06-06 20:29:01 +000090// and to ConstRules without changing anything else...
Misha Brukmanb1c93172005-04-21 23:48:37 +000091//
Chris Lattner2f7c9632001-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 Lattner6a871e12006-06-21 18:13:36 +000095namespace {
Chris Lattner2f7c9632001-06-06 20:29:01 +000096template<class ArgType, class SubClassName>
Chris Lattner02157b02006-06-28 21:38:54 +000097class VISIBILITY_HIDDEN TemplateRules : public ConstRules {
Chris Lattner2f7c9632001-06-06 20:29:01 +000098
Reid Spencer9c47b252005-04-24 22:27:20 +000099
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100 //===--------------------------------------------------------------------===//
101 // Redirecting functions that cast to the appropriate types
102 //===--------------------------------------------------------------------===//
103
Misha Brukmanb1c93172005-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 Lattner2f7c9632001-06-06 20:29:01 +0000106 }
Misha Brukmanb1c93172005-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 Lattner2f7c9632001-06-06 20:29:01 +0000109 }
Misha Brukmanb1c93172005-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 Lattner4f6031f2001-07-20 19:15:36 +0000112 }
Reid Spencer7e80b0b2006-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 Lattneraf259a72002-04-07 08:10:14 +0000121 }
Reid Spencer7eb55b32006-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 Lattner0a144ad2002-05-03 21:41:07 +0000130 }
Misha Brukmanb1c93172005-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 Lattnere87f65e2002-07-30 16:24:28 +0000133 }
Misha Brukmanb1c93172005-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 Lattnere87f65e2002-07-30 16:24:28 +0000136 }
Misha Brukmanb1c93172005-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 Lattnere87f65e2002-07-30 16:24:28 +0000139 }
Misha Brukmanb1c93172005-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 Lattner6670d862002-05-06 03:00:54 +0000142 }
Reid Spencerfdff9382006-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 Lattner6670d862002-05-06 03:00:54 +0000148 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000149
Misha Brukmanb1c93172005-04-21 23:48:37 +0000150 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
152 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000154 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
155 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156
Chris Lattner55406842001-07-21 19:10:49 +0000157 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000158 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000159 return SubClassName::CastToBool((const ArgType*)V);
160 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000161 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000162 return SubClassName::CastToSByte((const ArgType*)V);
163 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000164 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000165 return SubClassName::CastToUByte((const ArgType*)V);
166 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000167 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000168 return SubClassName::CastToShort((const ArgType*)V);
169 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000170 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000171 return SubClassName::CastToUShort((const ArgType*)V);
172 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000173 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000174 return SubClassName::CastToInt((const ArgType*)V);
175 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000176 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000177 return SubClassName::CastToUInt((const ArgType*)V);
178 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000179 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000180 return SubClassName::CastToLong((const ArgType*)V);
181 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000182 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000183 return SubClassName::CastToULong((const ArgType*)V);
184 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000185 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000186 return SubClassName::CastToFloat((const ArgType*)V);
187 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000188 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000189 return SubClassName::CastToDouble((const ArgType*)V);
190 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000191 virtual Constant *castToPointer(const Constant *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000192 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000193 return SubClassName::CastToPointer((const ArgType*)V, Ty);
194 }
Chris Lattner55406842001-07-21 19:10:49 +0000195
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 //===--------------------------------------------------------------------===//
197 // Default "noop" implementations
198 //===--------------------------------------------------------------------===//
199
Reid Spencer7e80b0b2006-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 Spencer7eb55b32006-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 Spencer7e80b0b2006-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 Spencerfdff9382006-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 Lattner6ff6cea2004-01-12 21:02:29 +0000215 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216 return 0;
217 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000218 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000219 return 0;
220 }
Chris Lattner55406842001-07-21 19:10:49 +0000221
222 // Casting operators. ick
Chris Lattner6ff6cea2004-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 Spencer9c47b252005-04-24 22:27:20 +0000236
237public:
238 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000239};
Chris Lattner6a871e12006-06-21 18:13:36 +0000240} // end anonymous namespace
Chris Lattner2f7c9632001-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 Lattner6a871e12006-06-21 18:13:36 +0000249namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000250struct VISIBILITY_HIDDEN EmptyRules
251 : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000252 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000253 if (V1 == V2) return ConstantBool::getTrue();
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000254 return 0;
255 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000256};
Chris Lattner6a871e12006-06-21 18:13:36 +0000257} // end anonymous namespace
Chris Lattner2f7c9632001-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 Lattner6a871e12006-06-21 18:13:36 +0000267namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000268struct VISIBILITY_HIDDEN BoolRules
269 : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000271 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner07507a42002-09-03 20:09:49 +0000272 return ConstantBool::get(V1->getValue() < V2->getValue());
273 }
274
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000275 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000276 return ConstantBool::get(V1 == V2);
277 }
278
Chris Lattnere87f65e2002-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 Lattner3462ae32001-12-03 22:26:30 +0000284 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000285 }
286
Chris Lattnere87f65e2002-07-30 16:24:28 +0000287 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
288 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000289 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000290
291 // Casting operators. ick
292#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000293 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-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 Spencere0fc4df2006-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 Lattnercea4d8c2003-08-13 15:52:25 +0000306 DEF_CAST(Float , ConstantFP , float)
307 DEF_CAST(Double, ConstantFP , double)
308#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000309};
Chris Lattner6a871e12006-06-21 18:13:36 +0000310} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311
312
313//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000314// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000315//===----------------------------------------------------------------------===//
316//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000317// NullPointerRules provides a concrete base class of ConstRules for null
318// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000319//
Chris Lattner6a871e12006-06-21 18:13:36 +0000320namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000321struct VISIBILITY_HIDDEN NullPointerRules
322 : public TemplateRules<ConstantPointerNull, NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000323 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000324 return ConstantBool::getTrue(); // Null pointers are always equal
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000325 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000326 static Constant *CastToBool(const Constant *V) {
Chris Lattner78430662006-09-28 23:34:49 +0000327 return ConstantBool::getFalse();
Chris Lattner977f0042001-11-01 05:55:13 +0000328 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000329 static Constant *CastToSByte (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000330 return ConstantInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000331 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000332 static Constant *CastToUByte (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000333 return ConstantInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000334 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000335 static Constant *CastToShort (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000336 return ConstantInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000337 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000338 static Constant *CastToUShort(const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000339 return ConstantInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000340 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000341 static Constant *CastToInt (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000342 return ConstantInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000343 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000344 static Constant *CastToUInt (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000345 return ConstantInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000346 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000347 static Constant *CastToLong (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000348 return ConstantInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000349 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000350 static Constant *CastToULong (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000351 return ConstantInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000352 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000353 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000354 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000355 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000356 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000357 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000358 }
359
Chris Lattner77f20dc2003-11-17 19:21:04 +0000360 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000361 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000362 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000363 }
364};
Chris Lattner6a871e12006-06-21 18:13:36 +0000365} // end anonymous namespace
Chris Lattner977f0042001-11-01 05:55:13 +0000366
Chris Lattner1171d952006-01-04 02:03:29 +0000367//===----------------------------------------------------------------------===//
368// ConstantPackedRules Class
369//===----------------------------------------------------------------------===//
370
Chris Lattnerf0f40682006-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 Lattner1171d952006-01-04 02:03:29 +0000384/// PackedTypeRules provides a concrete base class of ConstRules for
385/// ConstantPacked operands.
386///
Chris Lattner6a871e12006-06-21 18:13:36 +0000387namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000388struct VISIBILITY_HIDDEN ConstantPackedRules
Chris Lattner1171d952006-01-04 02:03:29 +0000389 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerf0f40682006-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 Spencer7e80b0b2006-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 Lattnerf0f40682006-01-04 02:15:02 +0000408 }
Reid Spencer7eb55b32006-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 Lattnerf0f40682006-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 Lattnerf0f40682006-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 Lattner6b52be62006-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 Lattnerf0f40682006-01-04 02:15:02 +0000439 return 0;
440 }
Chris Lattner1171d952006-01-04 02:03:29 +0000441};
Chris Lattner6a871e12006-06-21 18:13:36 +0000442} // end anonymous namespace
Chris Lattner1171d952006-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 Lattner6a871e12006-06-21 18:13:36 +0000453namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000454struct VISIBILITY_HIDDEN GeneralPackedRules
455 : public TemplateRules<Constant, GeneralPackedRules> {
Chris Lattner1171d952006-01-04 02:03:29 +0000456};
Chris Lattner6a871e12006-06-21 18:13:36 +0000457} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000458
Chris Lattner977f0042001-11-01 05:55:13 +0000459
460//===----------------------------------------------------------------------===//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000461// DirectIntRules Class
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462//===----------------------------------------------------------------------===//
463//
Reid Spencere0fc4df2006-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 Lattner2f7c9632001-06-06 20:29:01 +0000466//
Chris Lattner6a871e12006-06-21 18:13:36 +0000467namespace {
Reid Spencere0fc4df2006-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 Lattner2f7c9632001-06-06 20:29:01 +0000476 }
477
Reid Spencere0fc4df2006-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 Lattner2f7c9632001-06-06 20:29:01 +0000482 }
483
Reid Spencere0fc4df2006-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 Lattner4f6031f2001-07-20 19:15:36 +0000488 }
489
Reid Spencere0fc4df2006-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 Lattnere87f65e2002-07-30 16:24:28 +0000492 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000493 }
Chris Lattner55406842001-07-21 19:10:49 +0000494
Reid Spencere0fc4df2006-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 Lattnerdc2e3912003-11-17 20:19:35 +0000497 return ConstantBool::get(R);
498 }
499
Reid Spencere0fc4df2006-10-20 07:07:24 +0000500 static Constant *CastToPointer(const ConstantInt *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000501 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000502 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000503 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000504 return 0; // Can't const prop other types of pointers
505 }
506
Chris Lattner55406842001-07-21 19:10:49 +0000507 // Casting operators. ick
508#define DEF_CAST(TYPE, CLASS, CTYPE) \
Reid Spencere0fc4df2006-10-20 07:07:24 +0000509 static Constant *CastTo##TYPE (const ConstantInt *V) { \
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000510 return CLASS::get(Type::TYPE##Ty, (CTYPE)((BuiltinType)V->getZExtValue()));\
Chris Lattner55406842001-07-21 19:10:49 +0000511 }
512
Chris Lattner3462ae32001-12-03 22:26:30 +0000513 DEF_CAST(Bool , ConstantBool, bool)
Reid Spencere0fc4df2006-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 Lattner55406842001-07-21 19:10:49 +0000524#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000525
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000526 static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000527 if (V2->isNullValue()) // X / 0
Chris Lattner268916262003-05-12 15:26:25 +0000528 return 0;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000529 BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000530 return ConstantInt::get(*Ty, R);
Chris Lattner268916262003-05-12 15:26:25 +0000531 }
532
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000533 static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000534 if (V2->isNullValue()) // X / 0
Reid Spencer7e80b0b2006-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 Spencer7eb55b32006-11-02 01:53:59 +0000539 BuiltinType R = (BuiltinType)(V1->getSExtValue() / V2->getSExtValue());
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000540 return ConstantInt::get(*Ty, R);
541 }
542
Reid Spencer7eb55b32006-11-02 01:53:59 +0000543 static Constant *URem(const ConstantInt *V1,
544 const ConstantInt *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000545 if (V2->isNullValue()) return 0; // X / 0
Reid Spencer7eb55b32006-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 Lattner268916262003-05-12 15:26:25 +0000555 return 0;
Reid Spencer7eb55b32006-11-02 01:53:59 +0000556 BuiltinType R = (BuiltinType)(V1->getSExtValue() % V2->getSExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000557 return ConstantInt::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000558 }
Chris Lattner6670d862002-05-06 03:00:54 +0000559
Reid Spencere0fc4df2006-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 Lattnere87f65e2002-07-30 16:24:28 +0000564 }
Reid Spencere0fc4df2006-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 Lattnere87f65e2002-07-30 16:24:28 +0000569 }
Reid Spencere0fc4df2006-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 Lattner6670d862002-05-06 03:00:54 +0000574 }
575
Reid Spencere0fc4df2006-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 Lattnere87f65e2002-07-30 16:24:28 +0000580 }
581
Reid Spencerfdff9382006-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 Spencere0fc4df2006-10-20 07:07:24 +0000589 return ConstantInt::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000590 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000591};
Chris Lattner6a871e12006-06-21 18:13:36 +0000592} // end anonymous namespace
Chris Lattner0a144ad2002-05-03 21:41:07 +0000593
594
595//===----------------------------------------------------------------------===//
596// DirectFPRules Class
597//===----------------------------------------------------------------------===//
598//
Chris Lattner1dd054c2004-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 Lattner6a871e12006-06-21 18:13:36 +0000602namespace {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000603template <class BuiltinType, Type **Ty>
Chris Lattner02157b02006-06-28 21:38:54 +0000604struct VISIBILITY_HIDDEN DirectFPRules
Reid Spencere0fc4df2006-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 Spencer7eb55b32006-11-02 01:53:59 +0000659 static Constant *FRem(const ConstantFP *V1, const ConstantFP *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000660 if (V2->isNullValue()) return 0;
661 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
662 (BuiltinType)V2->getValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000663 return ConstantFP::get(*Ty, Result);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000664 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000665 static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) {
Jeff Cohen4e3aede2005-05-03 03:13:01 +0000666 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
Reid Spencere0fc4df2006-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 Lenharthc73e6332005-05-02 21:25:47 +0000669 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000670 return ConstantFP::get(*Ty, R);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000671 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000672};
Chris Lattner6a871e12006-06-21 18:13:36 +0000673} // end anonymous namespace
Chris Lattner62af86e2002-05-03 20:09:52 +0000674
Chris Lattner057083f2006-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 Spencere0fc4df2006-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 Lattner1dd054c2004-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 Lattnerf8348c32004-01-12 20:41:05 +0000693ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000694 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000695 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
696 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner057083f2006-10-13 17:22:21 +0000697 return *EmptyR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000698
Chris Lattner6b727592004-06-17 18:19:28 +0000699 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000700 default: assert(0 && "Unknown value type for constant folding!");
Chris Lattner057083f2006-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 Lattner1171d952006-01-04 02:03:29 +0000713 case Type::PackedTyID:
714 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
Chris Lattner057083f2006-10-13 17:22:21 +0000715 return *ConstantPackedR;
716 return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000719
720
721//===----------------------------------------------------------------------===//
722// ConstantFold*Instruction Implementations
723//===----------------------------------------------------------------------===//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000724
Chris Lattner6b3f4752006-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 Spencer6c38f0b2006-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 Lattner6b3f4752006-04-02 01:38:28 +0000744 for (unsigned i = 0; i != SrcNumElts; ++i)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000745 Result.push_back(
Reid Spencerbb65ebf2006-12-12 23:36:14 +0000746 ConstantExpr::getBitCast(CP->getOperand(i), DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000747 return ConstantPacked::get(Result);
748 }
749
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000750 // If this is an int-to-fp cast ..
Chris Lattner6b3f4752006-04-02 01:38:28 +0000751 if (SrcEltTy->isIntegral()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000752 // Ensure that it is int-to-fp cast
Chris Lattner6b3f4752006-04-02 01:38:28 +0000753 assert(DstEltTy->isFloatingPoint());
754 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
755 for (unsigned i = 0; i != SrcNumElts; ++i) {
756 double V =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000757 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +0000758 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
759 }
760 return ConstantPacked::get(Result);
761 }
762 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
763 for (unsigned i = 0; i != SrcNumElts; ++i) {
764 float V =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000765 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +0000766 Result.push_back(ConstantFP::get(Type::FloatTy, V));
767 }
768 return ConstantPacked::get(Result);
769 }
770
771 // Otherwise, this is an fp-to-int cast.
772 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
773
774 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
775 for (unsigned i = 0; i != SrcNumElts; ++i) {
776 uint64_t V =
777 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000778 Constant *C = ConstantInt::get(Type::ULongTy, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000779 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000780 }
781 return ConstantPacked::get(Result);
782 }
783
784 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
785 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000786 uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
787 Constant *C = ConstantInt::get(Type::UIntTy, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000788 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000789 }
790 return ConstantPacked::get(Result);
791 }
792
793 // Otherwise, this is a cast that changes element count and size. Handle
794 // casts which shrink the elements here.
795
796 // FIXME: We need to know endianness to do this!
797
798 return 0;
799}
800
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000801/// This function determines which opcode to use to fold two constant cast
802/// expressions together. It uses CastInst::isEliminableCastPair to determine
803/// the opcode. Consequently its just a wrapper around that function.
804/// @Determine if it is valid to fold a cast of a cast
805static unsigned
806foldConstantCastPair(
807 unsigned opc, ///< opcode of the second cast constant expression
808 const ConstantExpr*Op, ///< the first cast constant expression
809 const Type *DstTy ///< desintation type of the first cast
810) {
811 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
812 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
813 assert(CastInst::isCast(opc) && "Invalid cast opcode");
814
815 // The the types and opcodes for the two Cast constant expressions
816 const Type *SrcTy = Op->getOperand(0)->getType();
817 const Type *MidTy = Op->getType();
818 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
819 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000820
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000821 // Let CastInst::isEliminableCastPair do the heavy lifting.
822 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
823 Type::ULongTy);
824}
825
826Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattner1dd054c2004-01-12 22:07:24 +0000827 const Type *DestTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000828 const Type *SrcTy = V->getType();
Chris Lattner1dd054c2004-01-12 22:07:24 +0000829
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000830 if (isa<UndefValue>(V))
831 return UndefValue::get(DestTy);
832
833 // If the cast operand is a constant expression, there's a few things we can
834 // do to try to simplify it.
835 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
836 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000837 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000838 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
839 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000840 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
841 // If all of the indexes in the GEP are null values, there is no pointer
842 // adjustment going on. We might as well cast the source pointer.
843 bool isAllNull = true;
844 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
845 if (!CE->getOperand(i)->isNullValue()) {
846 isAllNull = false;
847 break;
848 }
849 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000850 // This is casting one pointer type to another, always BitCast
Reid Spencer27720a92006-12-05 03:30:09 +0000851 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000852 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000853 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000854
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000855 // We actually have to do a cast now, but first, we might need to fix up
856 // the value of the operand.
857 switch (opc) {
Chris Lattner710ebaf2006-12-01 19:22:41 +0000858 case Instruction::PtrToInt:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000859 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000860 case Instruction::FPExt:
Chris Lattner710ebaf2006-12-01 19:22:41 +0000861 break;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000862 case Instruction::FPToUI: {
863 ConstRules &Rules = ConstRules::get(V, V);
864 V = Rules.castToULong(V); // make sure we get an unsigned value first
865 break;
866 }
867 case Instruction::FPToSI: {
868 ConstRules &Rules = ConstRules::get(V, V);
869 V = Rules.castToLong(V); // make sure we get a signed value first
870 break;
871 }
872 case Instruction::IntToPtr: //always treated as unsigned
873 case Instruction::UIToFP:
874 case Instruction::ZExt:
875 // A ZExt always produces an unsigned value so we need to cast the value
876 // now before we try to cast it to the destination type
877 if (isa<ConstantInt>(V))
878 V = ConstantInt::get(SrcTy->getUnsignedVersion(),
879 cast<ConstantIntegral>(V)->getZExtValue());
880 break;
881 case Instruction::SIToFP:
882 case Instruction::SExt:
883 // A SExt always produces a signed value so we need to cast the value
884 // now before we try to cast it to the destiniation type.
885 if (isa<ConstantInt>(V))
886 V = ConstantInt::get(SrcTy->getSignedVersion(),
887 cast<ConstantIntegral>(V)->getSExtValue());
Chris Lattnerdefd8472006-12-01 19:50:54 +0000888 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
889 V = ConstantInt::get(Type::SByteTy, CB->getValue() ? -1 : 0);
890
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000891 break;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000892 case Instruction::Trunc:
893 // We just handle trunc directly here. The code below doesn't work for
894 // trunc to bool.
895 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
896 return ConstantIntegral::get(DestTy, CI->getZExtValue());
897 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000898 case Instruction::BitCast:
Chris Lattner4d1da162006-12-11 18:30:27 +0000899 if (SrcTy == DestTy) return (Constant*)V; // no-op cast
900
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000901 // Check to see if we are casting a pointer to an aggregate to a pointer to
902 // the first element. If so, return the appropriate GEP instruction.
903 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
904 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
905 std::vector<Value*> IdxList;
906 IdxList.push_back(Constant::getNullValue(Type::IntTy));
907 const Type *ElTy = PTy->getElementType();
908 while (ElTy != DPTy->getElementType()) {
909 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
910 if (STy->getNumElements() == 0) break;
911 ElTy = STy->getElementType(0);
912 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
913 } else if (const SequentialType *STy =
914 dyn_cast<SequentialType>(ElTy)) {
915 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
916 ElTy = STy->getElementType();
917 IdxList.push_back(IdxList[0]);
918 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000919 break;
920 }
921 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000922
923 if (ElTy == DPTy->getElementType())
924 return ConstantExpr::getGetElementPtr(
925 const_cast<Constant*>(V),IdxList);
926 }
927
928 // Handle casts from one packed constant to another. We know that the src
929 // and dest type have the same size (otherwise its an illegal cast).
930 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
931 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
932 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
933 "Not cast between same sized vectors!");
934 // First, check for null and undef
935 if (isa<ConstantAggregateZero>(V))
936 return Constant::getNullValue(DestTy);
937 if (isa<UndefValue>(V))
938 return UndefValue::get(DestTy);
939
940 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
941 // This is a cast from a ConstantPacked of one type to a
942 // ConstantPacked of another type. Check to see if all elements of
943 // the input are simple.
944 bool AllSimpleConstants = true;
945 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
946 if (!isa<ConstantInt>(CP->getOperand(i)) &&
947 !isa<ConstantFP>(CP->getOperand(i))) {
948 AllSimpleConstants = false;
949 break;
950 }
951 }
952
953 // If all of the elements are simple constants, we can fold this.
954 if (AllSimpleConstants)
955 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
956 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000957 }
958 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000959
Chris Lattner4d1da162006-12-11 18:30:27 +0000960 // Finally, implement bitcast folding now. The code below doesn't handle
961 // bitcast right.
962 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
963 return ConstantPointerNull::get(cast<PointerType>(DestTy));
964
965 // Handle integral constant input.
966 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
967 // Integral -> Integral, must be changing sign.
968 if (DestTy->isIntegral())
969 return ConstantInt::get(DestTy, CI->getZExtValue());
970
971 if (DestTy->isFloatingPoint()) {
972 if (DestTy == Type::FloatTy)
973 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
974 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
975 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
976 }
977 // Otherwise, can't fold this (packed?)
978 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000979 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000980
981 // Handle ConstantFP input.
982 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
983 // FP -> Integral.
984 if (DestTy->isIntegral()) {
Reid Spencer3db7d372006-12-11 21:27:28 +0000985 if (DestTy == Type::IntTy || DestTy == Type::UIntTy)
Chris Lattner4d1da162006-12-11 18:30:27 +0000986 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
Reid Spencer3db7d372006-12-11 21:27:28 +0000987 assert((DestTy == Type::LongTy || DestTy == Type::ULongTy)
988 && "Incorrect integer type for bitcast!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000989 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
990 }
991 }
992 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000993 default:
994 assert(!"Invalid CE CastInst opcode");
995 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000996 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000997
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000998 // Okay, no more folding possible, time to cast
Chris Lattner1dd054c2004-01-12 22:07:24 +0000999 ConstRules &Rules = ConstRules::get(V, V);
Chris Lattner6b727592004-06-17 18:19:28 +00001000 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001001 case Type::BoolTyID: return Rules.castToBool(V);
1002 case Type::UByteTyID: return Rules.castToUByte(V);
1003 case Type::SByteTyID: return Rules.castToSByte(V);
1004 case Type::UShortTyID: return Rules.castToUShort(V);
1005 case Type::ShortTyID: return Rules.castToShort(V);
1006 case Type::UIntTyID: return Rules.castToUInt(V);
1007 case Type::IntTyID: return Rules.castToInt(V);
1008 case Type::ULongTyID: return Rules.castToULong(V);
1009 case Type::LongTyID: return Rules.castToLong(V);
1010 case Type::FloatTyID: return Rules.castToFloat(V);
1011 case Type::DoubleTyID: return Rules.castToDouble(V);
1012 case Type::PointerTyID:
1013 return Rules.castToPointer(V, cast<PointerType>(DestTy));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001014 // what about packed ?
Chris Lattner1dd054c2004-01-12 22:07:24 +00001015 default: return 0;
1016 }
1017}
1018
Chris Lattner6ea4b522004-03-12 05:53:32 +00001019Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
1020 const Constant *V1,
1021 const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +00001022 if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
1023 return const_cast<Constant*>(CB->getValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001024
1025 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
1026 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
1027 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001028 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +00001029 return 0;
1030}
1031
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001032Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
1033 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +00001034 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
1035 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +00001036 if (Val->isNullValue()) // ee(zero, x) -> zero
1037 return Constant::getNullValue(
1038 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +00001039
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001040 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001041 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
1042 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +00001043 } else if (isa<UndefValue>(Idx)) {
1044 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
1045 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001046 }
Chris Lattnere52f29b2006-03-31 18:31:40 +00001047 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +00001048 return 0;
1049}
1050
Robert Bocchinoca27f032006-01-17 20:07:22 +00001051Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
1052 const Constant *Elt,
1053 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +00001054 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001055 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +00001056 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +00001057 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +00001058 // Insertion of scalar constant into packed undef
1059 // Optimize away insertion of undef
1060 if (isa<UndefValue>(Elt))
1061 return const_cast<Constant*>(Val);
1062 // Otherwise break the aggregate undef into multiple undefs and do
1063 // the insertion
1064 unsigned numOps =
1065 cast<PackedType>(Val->getType())->getNumElements();
1066 std::vector<Constant*> Ops;
1067 Ops.reserve(numOps);
1068 for (unsigned i = 0; i < numOps; ++i) {
1069 const Constant *Op =
1070 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
1071 Ops.push_back(const_cast<Constant*>(Op));
1072 }
1073 return ConstantPacked::get(Ops);
1074 }
Reid Spencer3054b142006-11-02 08:18:15 +00001075 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +00001076 // Insertion of scalar constant into packed aggregate zero
1077 // Optimize away insertion of zero
1078 if (Elt->isNullValue())
1079 return const_cast<Constant*>(Val);
1080 // Otherwise break the aggregate zero into multiple zeros and do
1081 // the insertion
1082 unsigned numOps =
1083 cast<PackedType>(Val->getType())->getNumElements();
1084 std::vector<Constant*> Ops;
1085 Ops.reserve(numOps);
1086 for (unsigned i = 0; i < numOps; ++i) {
1087 const Constant *Op =
1088 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
1089 Ops.push_back(const_cast<Constant*>(Op));
1090 }
1091 return ConstantPacked::get(Ops);
1092 }
1093 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
1094 // Insertion of scalar constant into packed constant
1095 std::vector<Constant*> Ops;
1096 Ops.reserve(CVal->getNumOperands());
1097 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
1098 const Constant *Op =
1099 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
1100 Ops.push_back(const_cast<Constant*>(Op));
1101 }
1102 return ConstantPacked::get(Ops);
1103 }
1104 return 0;
1105}
1106
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001107Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
1108 const Constant *V2,
1109 const Constant *Mask) {
1110 // TODO:
1111 return 0;
1112}
1113
1114
Chris Lattner60c47262005-01-28 19:09:51 +00001115/// isZeroSizedType - This type is zero sized if its an array or structure of
1116/// zero sized types. The only leaf zero sized type is an empty structure.
1117static bool isMaybeZeroSizedType(const Type *Ty) {
1118 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1119 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1120
1121 // If all of elements have zero size, this does too.
1122 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +00001123 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +00001124 return true;
1125
1126 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1127 return isMaybeZeroSizedType(ATy->getElementType());
1128 }
1129 return false;
1130}
Chris Lattner6ea4b522004-03-12 05:53:32 +00001131
Chris Lattner061da2f2004-01-13 05:51:55 +00001132/// IdxCompare - Compare the two constants as though they were getelementptr
1133/// indices. This allows coersion of the types to be the same thing.
1134///
1135/// If the two constants are the "same" (after coersion), return 0. If the
1136/// first is less than the second, return -1, if the second is less than the
1137/// first, return 1. If the constants are not integral, return -2.
1138///
Chris Lattner60c47262005-01-28 19:09:51 +00001139static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001140 if (C1 == C2) return 0;
1141
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001142 // Ok, we found a different index. Are either of the operands ConstantExprs?
1143 // If so, we can't do anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +00001144 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1145 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001146
Chris Lattner69193f92004-04-05 01:30:19 +00001147 // Ok, we have two differing integer indices. Sign extend them to be the same
1148 // type. Long is always big enough, so we use it.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001149 if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001150 C1 = ConstantExpr::getSExt(C1, Type::LongTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001151 else
1152 C1 = ConstantExpr::getBitCast(C1, Type::LongTy);
1153 if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001154 C2 = ConstantExpr::getSExt(C2, Type::LongTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001155 else
1156 C2 = ConstantExpr::getBitCast(C2, Type::LongTy);
1157
Chris Lattner061da2f2004-01-13 05:51:55 +00001158 if (C1 == C2) return 0; // Are they just differing types?
1159
Chris Lattner60c47262005-01-28 19:09:51 +00001160 // If the type being indexed over is really just a zero sized type, there is
1161 // no pointer difference being made here.
1162 if (isMaybeZeroSizedType(ElTy))
1163 return -2; // dunno.
1164
Chris Lattner061da2f2004-01-13 05:51:55 +00001165 // If they are really different, now that they are the same type, then we
1166 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +00001167 if (cast<ConstantInt>(C1)->getSExtValue() <
1168 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001169 return -1;
1170 else
1171 return 1;
1172}
1173
1174/// evaluateRelation - This function determines if there is anything we can
1175/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001176/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +00001177/// and GlobalValues. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +00001178/// particular relation to each other, we should return the corresponding SetCC
1179/// code, otherwise return Instruction::BinaryOpsEnd.
1180///
1181/// To simplify this code we canonicalize the relation so that the first
1182/// operand is always the most "complex" of the two. We consider simple
1183/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001184/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001185///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001186static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001187 assert(V1->getType() == V2->getType() &&
1188 "Cannot compare different types of values!");
1189 if (V1 == V2) return Instruction::SetEQ;
1190
Reid Spenceraccd7c72004-07-17 23:47:01 +00001191 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001192 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1193 // We distilled this down to a simple case, use the standard constant
1194 // folder.
1195 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001196 if (R && R->getValue()) return Instruction::SetEQ;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001197 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001198 if (R && R->getValue()) return Instruction::SetLT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001199 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001200 if (R && R->getValue()) return Instruction::SetGT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001201
1202 // If we couldn't figure it out, bail.
1203 return Instruction::BinaryOpsEnd;
1204 }
1205
Chris Lattner061da2f2004-01-13 05:51:55 +00001206 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +00001207 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1208 if (SwappedRelation != Instruction::BinaryOpsEnd)
1209 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001210
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001211 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001212 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001213 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1214 if (SwappedRelation != Instruction::BinaryOpsEnd)
1215 return SetCondInst::getSwappedCondition(SwappedRelation);
1216 else
1217 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +00001218 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001219
Reid Spenceraccd7c72004-07-17 23:47:01 +00001220 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001221 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001222 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001223 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1224 return Instruction::SetNE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001225 } else {
Reid Spencer876f7222006-12-06 00:25:09 +00001226 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +00001227 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +00001228 if (!CPR1->hasExternalWeakLinkage())
1229 return Instruction::SetNE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001230 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001231 } else {
1232 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1233 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001234 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +00001235 Constant *CE1Op0 = CE1->getOperand(0);
1236
1237 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001238 case Instruction::Trunc:
1239 case Instruction::FPTrunc:
1240 case Instruction::FPExt:
1241 case Instruction::FPToUI:
1242 case Instruction::FPToSI:
1243 break; // We don't do anything with floating point.
1244 case Instruction::ZExt:
1245 case Instruction::SExt:
1246 case Instruction::UIToFP:
1247 case Instruction::SIToFP:
1248 case Instruction::PtrToInt:
1249 case Instruction::IntToPtr:
1250 case Instruction::BitCast:
Chris Lattner061da2f2004-01-13 05:51:55 +00001251 // If the cast is not actually changing bits, and the second operand is a
1252 // null pointer, do the comparison with the pre-casted value.
1253 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001254 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +00001255 return evaluateRelation(CE1Op0,
1256 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001257
1258 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1259 // from the same type as the src of the LHS, evaluate the inputs. This is
1260 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1261 // which happens a lot in compilers with tagged integers.
1262 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001263 if (isa<PointerType>(CE1->getType()) && CE2->isCast() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001264 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1265 CE1->getOperand(0)->getType()->isIntegral()) {
1266 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1267 }
Chris Lattner192e3262004-04-11 01:29:30 +00001268 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001269
1270 case Instruction::GetElementPtr:
1271 // Ok, since this is a getelementptr, we know that the constant has a
1272 // pointer type. Check the various cases.
1273 if (isa<ConstantPointerNull>(V2)) {
1274 // If we are comparing a GEP to a null pointer, check to see if the base
1275 // of the GEP equals the null pointer.
Reid Spencer876f7222006-12-06 00:25:09 +00001276 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1277 if (GV->hasExternalWeakLinkage())
1278 // Weak linkage GVals could be zero or not. We're comparing that
1279 // to null pointer so its greater-or-equal
1280 return Instruction::SetGE;
1281 else
1282 // If its not weak linkage, the GVal must have a non-zero address
1283 // so the result is greater-than
1284 return Instruction::SetGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001285 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1286 // If we are indexing from a null pointer, check to see if we have any
1287 // non-zero indices.
1288 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1289 if (!CE1->getOperand(i)->isNullValue())
1290 // Offsetting from null, must not be equal.
1291 return Instruction::SetGT;
1292 // Only zero indexes from null, must still be zero.
1293 return Instruction::SetEQ;
1294 }
1295 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001296 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001297 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001298 if (CPR2->hasExternalWeakLinkage())
1299 // Weak linkage GVals could be zero or not. We're comparing it to
1300 // a null pointer, so its less-or-equal
1301 return Instruction::SetLE;
1302 else
1303 // If its not weak linkage, the GVal must have a non-zero address
1304 // so the result is less-than
1305 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001306 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001307 if (CPR1 == CPR2) {
1308 // If this is a getelementptr of the same global, then it must be
1309 // different. Because the types must match, the getelementptr could
1310 // only have at most one index, and because we fold getelementptr's
1311 // with a single zero index, it must be nonzero.
1312 assert(CE1->getNumOperands() == 2 &&
1313 !CE1->getOperand(1)->isNullValue() &&
1314 "Suprising getelementptr!");
1315 return Instruction::SetGT;
1316 } else {
1317 // If they are different globals, we don't know what the value is,
1318 // but they can't be equal.
1319 return Instruction::SetNE;
1320 }
1321 }
1322 } else {
1323 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1324 const Constant *CE2Op0 = CE2->getOperand(0);
1325
1326 // There are MANY other foldings that we could perform here. They will
1327 // probably be added on demand, as they seem needed.
1328 switch (CE2->getOpcode()) {
1329 default: break;
1330 case Instruction::GetElementPtr:
1331 // By far the most common case to handle is when the base pointers are
1332 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001333 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001334 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1335 return Instruction::SetNE;
1336 // Ok, we know that both getelementptr instructions are based on the
1337 // same global. From this, we can precisely determine the relative
1338 // ordering of the resultant pointers.
1339 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001340
Chris Lattner061da2f2004-01-13 05:51:55 +00001341 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001342 gep_type_iterator GTI = gep_type_begin(CE1);
1343 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1344 ++i, ++GTI)
1345 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1346 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001347 case -1: return Instruction::SetLT;
1348 case 1: return Instruction::SetGT;
1349 case -2: return Instruction::BinaryOpsEnd;
1350 }
1351
1352 // Ok, we ran out of things they have in common. If any leftovers
1353 // are non-zero then we have a difference, otherwise we are equal.
1354 for (; i < CE1->getNumOperands(); ++i)
1355 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001356 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1357 return Instruction::SetGT;
1358 else
1359 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001360
Chris Lattner061da2f2004-01-13 05:51:55 +00001361 for (; i < CE2->getNumOperands(); ++i)
1362 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001363 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1364 return Instruction::SetLT;
1365 else
1366 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001367 return Instruction::SetEQ;
1368 }
1369 }
1370 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001371
Chris Lattner061da2f2004-01-13 05:51:55 +00001372 default:
1373 break;
1374 }
1375 }
1376
1377 return Instruction::BinaryOpsEnd;
1378}
1379
Chris Lattner1dd054c2004-01-12 22:07:24 +00001380Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1381 const Constant *V1,
1382 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001383 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001384 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001385 default: break;
1386 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1387 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1388 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001389 case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break;
1390 case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break;
1391 case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001392 case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break;
1393 case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break;
1394 case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001395 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1396 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1397 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1398 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
Reid Spencerfdff9382006-11-08 06:47:33 +00001399 case Instruction::LShr: C = ConstRules::get(V1, V2).lshr(V1, V2); break;
1400 case Instruction::AShr: C = ConstRules::get(V1, V2).ashr(V1, V2); break;
Reid Spencer6f05d732006-12-01 03:56:30 +00001401 case Instruction::SetEQ:
1402 // SetEQ(null,GV) -> false
1403 if (V1->isNullValue()) {
1404 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2))
1405 if (!GV->hasExternalWeakLinkage())
1406 return ConstantBool::getFalse();
1407 // SetEQ(GV,null) -> false
1408 } else if (V2->isNullValue()) {
1409 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1))
1410 if (!GV->hasExternalWeakLinkage())
1411 return ConstantBool::getFalse();
1412 }
1413 C = ConstRules::get(V1, V2).equalto(V1, V2);
1414 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001415 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1416 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Reid Spencer6f05d732006-12-01 03:56:30 +00001417 case Instruction::SetNE:
1418 // SetNE(null,GV) -> true
1419 if (V1->isNullValue()) {
1420 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2))
1421 if (!GV->hasExternalWeakLinkage())
1422 return ConstantBool::getTrue();
1423 // SetNE(GV,null) -> true
1424 } else if (V2->isNullValue()) {
1425 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1))
1426 if (!GV->hasExternalWeakLinkage())
1427 return ConstantBool::getTrue();
1428 }
1429 // V1 != V2 === !(V1 == V2)
Chris Lattner1dd054c2004-01-12 22:07:24 +00001430 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001431 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001432 break;
1433 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1434 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001435 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001436 break;
1437 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1438 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001439 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001440 break;
1441 }
1442
Chris Lattner061da2f2004-01-13 05:51:55 +00001443 // If we successfully folded the expression, return it now.
1444 if (C) return C;
1445
Chris Lattnere1496fb2006-09-17 19:14:47 +00001446 if (SetCondInst::isComparison(Opcode)) {
Chris Lattner192eacc2004-10-17 04:01:51 +00001447 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1448 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001449 switch (evaluateRelation(const_cast<Constant*>(V1),
1450 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001451 default: assert(0 && "Unknown relational!");
1452 case Instruction::BinaryOpsEnd:
1453 break; // Couldn't determine anything about these constants.
1454 case Instruction::SetEQ: // We know the constants are equal!
1455 // If we know the constants are equal, we can decide the result of this
1456 // computation precisely.
1457 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1458 Opcode == Instruction::SetLE ||
1459 Opcode == Instruction::SetGE);
1460 case Instruction::SetLT:
1461 // If we know that V1 < V2, we can decide the result of this computation
1462 // precisely.
1463 return ConstantBool::get(Opcode == Instruction::SetLT ||
1464 Opcode == Instruction::SetNE ||
1465 Opcode == Instruction::SetLE);
1466 case Instruction::SetGT:
1467 // If we know that V1 > V2, we can decide the result of this computation
1468 // precisely.
1469 return ConstantBool::get(Opcode == Instruction::SetGT ||
1470 Opcode == Instruction::SetNE ||
1471 Opcode == Instruction::SetGE);
1472 case Instruction::SetLE:
1473 // If we know that V1 <= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001474 if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
1475 if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001476 break;
1477
1478 case Instruction::SetGE:
1479 // If we know that V1 >= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001480 if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
1481 if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001482 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001483
Chris Lattner061da2f2004-01-13 05:51:55 +00001484 case Instruction::SetNE:
1485 // If we know that V1 != V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001486 if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
1487 if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001488 break;
1489 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001490 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001491
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001492 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1493 switch (Opcode) {
1494 case Instruction::Add:
1495 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001496 case Instruction::Xor:
1497 return UndefValue::get(V1->getType());
1498
1499 case Instruction::Mul:
1500 case Instruction::And:
1501 return Constant::getNullValue(V1->getType());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001502 case Instruction::UDiv:
1503 case Instruction::SDiv:
1504 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001505 case Instruction::URem:
1506 case Instruction::SRem:
1507 case Instruction::FRem:
1508 if (!isa<UndefValue>(V2)) // undef / X -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001509 return Constant::getNullValue(V1->getType());
Reid Spencer7eb55b32006-11-02 01:53:59 +00001510 return const_cast<Constant*>(V2); // X / undef -> undef
1511 case Instruction::Or: // X | undef -> -1
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001512 return ConstantInt::getAllOnesValue(V1->getType());
Reid Spencerfdff9382006-11-08 06:47:33 +00001513 case Instruction::LShr:
1514 if (isa<UndefValue>(V2) && isa<UndefValue>(V1))
1515 return const_cast<Constant*>(V1); // undef lshr undef -> undef
1516 return Constant::getNullValue(V1->getType()); // X lshr undef -> 0
1517 // undef lshr X -> 0
1518 case Instruction::AShr:
1519 if (!isa<UndefValue>(V2))
1520 return const_cast<Constant*>(V1); // undef ashr X --> undef
1521 else if (isa<UndefValue>(V1))
1522 return const_cast<Constant*>(V1); // undef ashr undef -> undef
1523 else
1524 return const_cast<Constant*>(V1); // X ashr undef --> X
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001525 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001526 // undef << X -> 0 or X << undef -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001527 return Constant::getNullValue(V1->getType());
1528 }
1529 }
1530
Chris Lattner061da2f2004-01-13 05:51:55 +00001531 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
Reid Spencer3054b142006-11-02 08:18:15 +00001532 if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001533 // There are many possible foldings we could do here. We should probably
1534 // at least fold add of a pointer with an integer into the appropriate
1535 // getelementptr. This will improve alias analysis a bit.
Chris Lattner061da2f2004-01-13 05:51:55 +00001536 } else {
1537 // Just implement a couple of simple identities.
1538 switch (Opcode) {
1539 case Instruction::Add:
1540 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1541 break;
1542 case Instruction::Sub:
1543 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1544 break;
1545 case Instruction::Mul:
1546 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1547 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001548 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001549 return const_cast<Constant*>(V1); // X * 1 == X
1550 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001551 case Instruction::UDiv:
1552 case Instruction::SDiv:
Chris Lattner061da2f2004-01-13 05:51:55 +00001553 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001554 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001555 return const_cast<Constant*>(V1); // X / 1 == X
1556 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001557 case Instruction::URem:
1558 case Instruction::SRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001559 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001560 if (CI->getZExtValue() == 1)
Reid Spencer7eb55b32006-11-02 01:53:59 +00001561 return Constant::getNullValue(CI->getType()); // X % 1 == 0
Chris Lattner061da2f2004-01-13 05:51:55 +00001562 break;
1563 case Instruction::And:
1564 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1565 return const_cast<Constant*>(V1); // X & -1 == X
1566 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001567 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001568 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001569
1570 // Functions are at least 4-byte aligned. If and'ing the address of a
1571 // function with a constant < 4, fold it to zero.
1572 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001573 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001574 return Constant::getNullValue(CI->getType());
1575 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001576 break;
1577 case Instruction::Or:
1578 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1579 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1580 return const_cast<Constant*>(V2); // X | -1 == -1
1581 break;
1582 case Instruction::Xor:
1583 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1584 break;
1585 }
1586 }
1587
Reid Spencer3054b142006-11-02 08:18:15 +00001588 } else if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001589 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1590 // other way if possible.
1591 switch (Opcode) {
1592 case Instruction::Add:
1593 case Instruction::Mul:
1594 case Instruction::And:
1595 case Instruction::Or:
1596 case Instruction::Xor:
1597 case Instruction::SetEQ:
1598 case Instruction::SetNE:
1599 // No change of opcode required.
1600 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1601
1602 case Instruction::SetLT:
1603 case Instruction::SetGT:
1604 case Instruction::SetLE:
1605 case Instruction::SetGE:
1606 // Change the opcode as necessary to swap the operands.
1607 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1608 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1609
1610 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001611 case Instruction::LShr:
1612 case Instruction::AShr:
Chris Lattner061da2f2004-01-13 05:51:55 +00001613 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001614 case Instruction::SDiv:
1615 case Instruction::UDiv:
1616 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001617 case Instruction::URem:
1618 case Instruction::SRem:
1619 case Instruction::FRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001620 default: // These instructions cannot be flopped around.
1621 break;
1622 }
1623 }
1624 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001625}
1626
Reid Spencerb471d8e2006-12-04 05:19:34 +00001627Constant *llvm::ConstantFoldCompare(
1628 unsigned opcode, Constant *C1, Constant *C2, unsigned short predicate)
1629{
1630 // Place holder for future folding of ICmp and FCmp instructions
1631 return 0;
1632}
1633
Chris Lattner1dd054c2004-01-12 22:07:24 +00001634Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001635 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001636 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001637 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001638 return const_cast<Constant*>(C);
1639
Chris Lattnerf6013752004-10-17 21:54:55 +00001640 if (isa<UndefValue>(C)) {
1641 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1642 true);
1643 assert(Ty != 0 && "Invalid indices for GEP!");
1644 return UndefValue::get(PointerType::get(Ty));
1645 }
1646
1647 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001648 if (C->isNullValue()) {
1649 bool isNull = true;
1650 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001651 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001652 isNull = false;
1653 break;
1654 }
1655 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001656 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001657 true);
1658 assert(Ty != 0 && "Invalid indices for GEP!");
1659 return ConstantPointerNull::get(PointerType::get(Ty));
1660 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001661
1662 if (IdxList.size() == 1) {
1663 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
Reid Spencere0fc4df2006-10-20 07:07:24 +00001664 if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
Chris Lattner4bbd4092004-07-15 01:16:59 +00001665 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1666 // type, we can statically fold this.
Reid Spencere0fc4df2006-10-20 07:07:24 +00001667 Constant *R = ConstantInt::get(Type::UIntTy, ElSize);
Reid Spencer1a063892006-12-04 02:46:44 +00001668 // We know R is unsigned, Idx0 is signed because it must be an index
1669 // through a sequential type (gep pointer operand) which is always
1670 // signed.
Reid Spencer27720a92006-12-05 03:30:09 +00001671 R = ConstantExpr::getSExtOrBitCast(R, Idx0->getType());
Reid Spencer1a063892006-12-04 02:46:44 +00001672 R = ConstantExpr::getMul(R, Idx0); // signed multiply
1673 // R is a signed integer, C is the GEP pointer so -> IntToPtr
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001674 return ConstantExpr::getIntToPtr(R, C->getType());
Chris Lattner4bbd4092004-07-15 01:16:59 +00001675 }
1676 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001677 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001678
1679 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1680 // Combine Indices - If the source pointer to this getelementptr instruction
1681 // is a getelementptr instruction, combine the indices of the two
1682 // getelementptr instructions into a single instruction.
1683 //
1684 if (CE->getOpcode() == Instruction::GetElementPtr) {
1685 const Type *LastTy = 0;
1686 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1687 I != E; ++I)
1688 LastTy = *I;
1689
Chris Lattner13128ab2004-10-11 22:52:25 +00001690 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1691 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001692 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1693 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001694 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001695
1696 // Add the last index of the source with the first index of the new GEP.
1697 // Make sure to handle the case when they are actually different types.
1698 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001699 // Otherwise it must be an array.
1700 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001701 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001702 if (IdxTy != Idx0->getType()) {
Reid Spencer27720a92006-12-05 03:30:09 +00001703 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::LongTy);
1704 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
1705 Type::LongTy);
Reid Spencer1a063892006-12-04 02:46:44 +00001706 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1707 } else {
1708 Combined =
1709 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1710 }
Chris Lattner71068a02004-07-07 04:45:13 +00001711 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001712
Chris Lattner1dd054c2004-01-12 22:07:24 +00001713 NewIndices.push_back(Combined);
1714 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1715 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1716 }
1717 }
1718
1719 // Implement folding of:
1720 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1721 // long 0, long 0)
1722 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1723 //
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001724 if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001725 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001726 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1727 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1728 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001729 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001730 if (CAT->getElementType() == SAT->getElementType())
1731 return ConstantExpr::getGetElementPtr(
1732 (Constant*)CE->getOperand(0), IdxList);
1733 }
1734 return 0;
1735}
1736