blob: 055d41c063d2f85b2f6dbe79c2d388c66f0108f5 [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 Lattner0a144ad2002-05-03 21:41:07 +000031#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000032using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000033
Chris Lattner5a945e32004-01-12 21:13:12 +000034namespace {
Chris Lattner02157b02006-06-28 21:38:54 +000035 struct VISIBILITY_HIDDEN ConstRules {
Chris Lattner5a945e32004-01-12 21:13:12 +000036 ConstRules() {}
Reid Spencer9c47b252005-04-24 22:27:20 +000037 virtual ~ConstRules() {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000038
Chris Lattner5a945e32004-01-12 21:13:12 +000039 // Binary Operators...
40 virtual Constant *add(const Constant *V1, const Constant *V2) const = 0;
41 virtual Constant *sub(const Constant *V1, const Constant *V2) const = 0;
42 virtual Constant *mul(const Constant *V1, const Constant *V2) const = 0;
Reid Spencer7eb55b32006-11-02 01:53:59 +000043 virtual Constant *urem(const Constant *V1, const Constant *V2) const = 0;
44 virtual Constant *srem(const Constant *V1, const Constant *V2) const = 0;
45 virtual Constant *frem(const Constant *V1, const Constant *V2) const = 0;
Reid Spencer7e80b0b2006-10-26 06:15:43 +000046 virtual Constant *udiv(const Constant *V1, const Constant *V2) const = 0;
47 virtual Constant *sdiv(const Constant *V1, const Constant *V2) const = 0;
48 virtual Constant *fdiv(const Constant *V1, const Constant *V2) const = 0;
Chris Lattner5a945e32004-01-12 21:13:12 +000049 virtual Constant *op_and(const Constant *V1, const Constant *V2) const = 0;
50 virtual Constant *op_or (const Constant *V1, const Constant *V2) const = 0;
51 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const = 0;
52 virtual Constant *shl(const Constant *V1, const Constant *V2) const = 0;
53 virtual Constant *shr(const Constant *V1, const Constant *V2) const = 0;
54 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 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000143 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
144 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000145 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000146
Misha Brukmanb1c93172005-04-21 23:48:37 +0000147 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
149 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000150 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000151 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
152 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153
Chris Lattner55406842001-07-21 19:10:49 +0000154 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000155 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000156 return SubClassName::CastToBool((const ArgType*)V);
157 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000158 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000159 return SubClassName::CastToSByte((const ArgType*)V);
160 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000161 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000162 return SubClassName::CastToUByte((const ArgType*)V);
163 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000164 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000165 return SubClassName::CastToShort((const ArgType*)V);
166 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000167 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000168 return SubClassName::CastToUShort((const ArgType*)V);
169 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000170 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000171 return SubClassName::CastToInt((const ArgType*)V);
172 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000173 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000174 return SubClassName::CastToUInt((const ArgType*)V);
175 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000176 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000177 return SubClassName::CastToLong((const ArgType*)V);
178 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000179 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000180 return SubClassName::CastToULong((const ArgType*)V);
181 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000182 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000183 return SubClassName::CastToFloat((const ArgType*)V);
184 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000185 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000186 return SubClassName::CastToDouble((const ArgType*)V);
187 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000188 virtual Constant *castToPointer(const Constant *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000189 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000190 return SubClassName::CastToPointer((const ArgType*)V, Ty);
191 }
Chris Lattner55406842001-07-21 19:10:49 +0000192
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193 //===--------------------------------------------------------------------===//
194 // Default "noop" implementations
195 //===--------------------------------------------------------------------===//
196
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000197 static Constant *Add (const ArgType *V1, const ArgType *V2) { return 0; }
198 static Constant *Sub (const ArgType *V1, const ArgType *V2) { return 0; }
199 static Constant *Mul (const ArgType *V1, const ArgType *V2) { return 0; }
200 static Constant *SDiv(const ArgType *V1, const ArgType *V2) { return 0; }
201 static Constant *UDiv(const ArgType *V1, const ArgType *V2) { return 0; }
202 static Constant *FDiv(const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer7eb55b32006-11-02 01:53:59 +0000203 static Constant *URem(const ArgType *V1, const ArgType *V2) { return 0; }
204 static Constant *SRem(const ArgType *V1, const ArgType *V2) { return 0; }
205 static Constant *FRem(const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000206 static Constant *And (const ArgType *V1, const ArgType *V2) { return 0; }
207 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
208 static Constant *Xor (const ArgType *V1, const ArgType *V2) { return 0; }
209 static Constant *Shl (const ArgType *V1, const ArgType *V2) { return 0; }
210 static Constant *Shr (const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000211 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212 return 0;
213 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000214 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000215 return 0;
216 }
Chris Lattner55406842001-07-21 19:10:49 +0000217
218 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000219 static Constant *CastToBool (const Constant *V) { return 0; }
220 static Constant *CastToSByte (const Constant *V) { return 0; }
221 static Constant *CastToUByte (const Constant *V) { return 0; }
222 static Constant *CastToShort (const Constant *V) { return 0; }
223 static Constant *CastToUShort(const Constant *V) { return 0; }
224 static Constant *CastToInt (const Constant *V) { return 0; }
225 static Constant *CastToUInt (const Constant *V) { return 0; }
226 static Constant *CastToLong (const Constant *V) { return 0; }
227 static Constant *CastToULong (const Constant *V) { return 0; }
228 static Constant *CastToFloat (const Constant *V) { return 0; }
229 static Constant *CastToDouble(const Constant *V) { return 0; }
230 static Constant *CastToPointer(const Constant *,
231 const PointerType *) {return 0;}
Reid Spencer9c47b252005-04-24 22:27:20 +0000232
233public:
234 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235};
Chris Lattner6a871e12006-06-21 18:13:36 +0000236} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237
238
239//===----------------------------------------------------------------------===//
240// EmptyRules Class
241//===----------------------------------------------------------------------===//
242//
243// EmptyRules provides a concrete base class of ConstRules that does nothing
244//
Chris Lattner6a871e12006-06-21 18:13:36 +0000245namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000246struct VISIBILITY_HIDDEN EmptyRules
247 : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000248 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000249 if (V1 == V2) return ConstantBool::getTrue();
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000250 return 0;
251 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000252};
Chris Lattner6a871e12006-06-21 18:13:36 +0000253} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000254
255
256
257//===----------------------------------------------------------------------===//
258// BoolRules Class
259//===----------------------------------------------------------------------===//
260//
261// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
262//
Chris Lattner6a871e12006-06-21 18:13:36 +0000263namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000264struct VISIBILITY_HIDDEN BoolRules
265 : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000267 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner07507a42002-09-03 20:09:49 +0000268 return ConstantBool::get(V1->getValue() < V2->getValue());
269 }
270
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000271 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000272 return ConstantBool::get(V1 == V2);
273 }
274
Chris Lattnere87f65e2002-07-30 16:24:28 +0000275 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
276 return ConstantBool::get(V1->getValue() & V2->getValue());
277 }
278
279 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000280 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000281 }
282
Chris Lattnere87f65e2002-07-30 16:24:28 +0000283 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
284 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000285 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000286
287 // Casting operators. ick
288#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000289 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000290 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
291 }
292
293 DEF_CAST(Bool , ConstantBool, bool)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000294 DEF_CAST(SByte , ConstantInt, signed char)
295 DEF_CAST(UByte , ConstantInt, unsigned char)
296 DEF_CAST(Short , ConstantInt, signed short)
297 DEF_CAST(UShort, ConstantInt, unsigned short)
298 DEF_CAST(Int , ConstantInt, signed int)
299 DEF_CAST(UInt , ConstantInt, unsigned int)
300 DEF_CAST(Long , ConstantInt, int64_t)
301 DEF_CAST(ULong , ConstantInt, uint64_t)
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000302 DEF_CAST(Float , ConstantFP , float)
303 DEF_CAST(Double, ConstantFP , double)
304#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000305};
Chris Lattner6a871e12006-06-21 18:13:36 +0000306} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000307
308
309//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000310// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000311//===----------------------------------------------------------------------===//
312//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000313// NullPointerRules provides a concrete base class of ConstRules for null
314// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000315//
Chris Lattner6a871e12006-06-21 18:13:36 +0000316namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000317struct VISIBILITY_HIDDEN NullPointerRules
318 : public TemplateRules<ConstantPointerNull, NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000319 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000320 return ConstantBool::getTrue(); // Null pointers are always equal
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000321 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000322 static Constant *CastToBool(const Constant *V) {
Chris Lattner78430662006-09-28 23:34:49 +0000323 return ConstantBool::getFalse();
Chris Lattner977f0042001-11-01 05:55:13 +0000324 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000325 static Constant *CastToSByte (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000326 return ConstantInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000327 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000328 static Constant *CastToUByte (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000329 return ConstantInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000330 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000331 static Constant *CastToShort (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000332 return ConstantInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000333 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000334 static Constant *CastToUShort(const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000335 return ConstantInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000336 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000337 static Constant *CastToInt (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000338 return ConstantInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000339 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000340 static Constant *CastToUInt (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000341 return ConstantInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000342 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000343 static Constant *CastToLong (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000344 return ConstantInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000345 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000346 static Constant *CastToULong (const Constant *V) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000347 return ConstantInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000348 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000349 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000350 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000351 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000352 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000353 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000354 }
355
Chris Lattner77f20dc2003-11-17 19:21:04 +0000356 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000357 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000358 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000359 }
360};
Chris Lattner6a871e12006-06-21 18:13:36 +0000361} // end anonymous namespace
Chris Lattner977f0042001-11-01 05:55:13 +0000362
Chris Lattner1171d952006-01-04 02:03:29 +0000363//===----------------------------------------------------------------------===//
364// ConstantPackedRules Class
365//===----------------------------------------------------------------------===//
366
Chris Lattnerf0f40682006-01-04 02:15:02 +0000367/// DoVectorOp - Given two packed constants and a function pointer, apply the
368/// function pointer to each element pair, producing a new ConstantPacked
369/// constant.
370static Constant *EvalVectorOp(const ConstantPacked *V1,
371 const ConstantPacked *V2,
372 Constant *(*FP)(Constant*, Constant*)) {
373 std::vector<Constant*> Res;
374 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
375 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
376 const_cast<Constant*>(V2->getOperand(i))));
377 return ConstantPacked::get(Res);
378}
379
Chris Lattner1171d952006-01-04 02:03:29 +0000380/// PackedTypeRules provides a concrete base class of ConstRules for
381/// ConstantPacked operands.
382///
Chris Lattner6a871e12006-06-21 18:13:36 +0000383namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000384struct VISIBILITY_HIDDEN ConstantPackedRules
Chris Lattner1171d952006-01-04 02:03:29 +0000385 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerf0f40682006-01-04 02:15:02 +0000386
387 static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
388 return EvalVectorOp(V1, V2, ConstantExpr::getAdd);
389 }
390 static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) {
391 return EvalVectorOp(V1, V2, ConstantExpr::getSub);
392 }
393 static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
394 return EvalVectorOp(V1, V2, ConstantExpr::getMul);
395 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000396 static Constant *UDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
397 return EvalVectorOp(V1, V2, ConstantExpr::getUDiv);
398 }
399 static Constant *SDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
400 return EvalVectorOp(V1, V2, ConstantExpr::getSDiv);
401 }
402 static Constant *FDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
403 return EvalVectorOp(V1, V2, ConstantExpr::getFDiv);
Chris Lattnerf0f40682006-01-04 02:15:02 +0000404 }
Reid Spencer7eb55b32006-11-02 01:53:59 +0000405 static Constant *URem(const ConstantPacked *V1, const ConstantPacked *V2) {
406 return EvalVectorOp(V1, V2, ConstantExpr::getURem);
407 }
408 static Constant *SRem(const ConstantPacked *V1, const ConstantPacked *V2) {
409 return EvalVectorOp(V1, V2, ConstantExpr::getSRem);
410 }
411 static Constant *FRem(const ConstantPacked *V1, const ConstantPacked *V2) {
412 return EvalVectorOp(V1, V2, ConstantExpr::getFRem);
Chris Lattnerf0f40682006-01-04 02:15:02 +0000413 }
414 static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) {
415 return EvalVectorOp(V1, V2, ConstantExpr::getAnd);
416 }
417 static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) {
418 return EvalVectorOp(V1, V2, ConstantExpr::getOr);
419 }
420 static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) {
421 return EvalVectorOp(V1, V2, ConstantExpr::getXor);
422 }
423 static Constant *Shl(const ConstantPacked *V1, const ConstantPacked *V2) {
424 return EvalVectorOp(V1, V2, ConstantExpr::getShl);
425 }
426 static Constant *Shr(const ConstantPacked *V1, const ConstantPacked *V2) {
427 return EvalVectorOp(V1, V2, ConstantExpr::getShr);
428 }
429 static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){
430 return 0;
431 }
432 static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) {
Chris Lattner6b52be62006-01-04 02:20:54 +0000433 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) {
434 Constant *C =
435 ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)),
436 const_cast<Constant*>(V2->getOperand(i)));
437 if (ConstantBool *CB = dyn_cast<ConstantBool>(C))
438 return CB;
439 }
440 // Otherwise, could not decide from any element pairs.
Chris Lattnerf0f40682006-01-04 02:15:02 +0000441 return 0;
442 }
Chris Lattner1171d952006-01-04 02:03:29 +0000443};
Chris Lattner6a871e12006-06-21 18:13:36 +0000444} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000445
446
447//===----------------------------------------------------------------------===//
448// GeneralPackedRules Class
449//===----------------------------------------------------------------------===//
450
451/// GeneralPackedRules provides a concrete base class of ConstRules for
452/// PackedType operands, where both operands are not ConstantPacked. The usual
453/// cause for this is that one operand is a ConstantAggregateZero.
454///
Chris Lattner6a871e12006-06-21 18:13:36 +0000455namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000456struct VISIBILITY_HIDDEN GeneralPackedRules
457 : public TemplateRules<Constant, GeneralPackedRules> {
Chris Lattner1171d952006-01-04 02:03:29 +0000458};
Chris Lattner6a871e12006-06-21 18:13:36 +0000459} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000460
Chris Lattner977f0042001-11-01 05:55:13 +0000461
462//===----------------------------------------------------------------------===//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000463// DirectIntRules Class
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464//===----------------------------------------------------------------------===//
465//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000466// DirectIntRules provides implementations of functions that are valid on
467// integer types, but not all types in general.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468//
Chris Lattner6a871e12006-06-21 18:13:36 +0000469namespace {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000470template <class BuiltinType, Type **Ty>
471struct VISIBILITY_HIDDEN DirectIntRules
472 : public TemplateRules<ConstantInt, DirectIntRules<BuiltinType, Ty> > {
473
474 static Constant *Add(const ConstantInt *V1, const ConstantInt *V2) {
475 BuiltinType R = (BuiltinType)V1->getZExtValue() +
476 (BuiltinType)V2->getZExtValue();
477 return ConstantInt::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000478 }
479
Reid Spencere0fc4df2006-10-20 07:07:24 +0000480 static Constant *Sub(const ConstantInt *V1, const ConstantInt *V2) {
481 BuiltinType R = (BuiltinType)V1->getZExtValue() -
482 (BuiltinType)V2->getZExtValue();
483 return ConstantInt::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000484 }
485
Reid Spencere0fc4df2006-10-20 07:07:24 +0000486 static Constant *Mul(const ConstantInt *V1, const ConstantInt *V2) {
487 BuiltinType R = (BuiltinType)V1->getZExtValue() *
488 (BuiltinType)V2->getZExtValue();
489 return ConstantInt::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000490 }
491
Reid Spencere0fc4df2006-10-20 07:07:24 +0000492 static Constant *LessThan(const ConstantInt *V1, const ConstantInt *V2) {
493 bool R = (BuiltinType)V1->getZExtValue() < (BuiltinType)V2->getZExtValue();
Chris Lattnere87f65e2002-07-30 16:24:28 +0000494 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000495 }
Chris Lattner55406842001-07-21 19:10:49 +0000496
Reid Spencere0fc4df2006-10-20 07:07:24 +0000497 static Constant *EqualTo(const ConstantInt *V1, const ConstantInt *V2) {
498 bool R = (BuiltinType)V1->getZExtValue() == (BuiltinType)V2->getZExtValue();
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000499 return ConstantBool::get(R);
500 }
501
Reid Spencere0fc4df2006-10-20 07:07:24 +0000502 static Constant *CastToPointer(const ConstantInt *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000503 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000504 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000505 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000506 return 0; // Can't const prop other types of pointers
507 }
508
Chris Lattner55406842001-07-21 19:10:49 +0000509 // Casting operators. ick
510#define DEF_CAST(TYPE, CLASS, CTYPE) \
Reid Spencere0fc4df2006-10-20 07:07:24 +0000511 static Constant *CastTo##TYPE (const ConstantInt *V) { \
512 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getZExtValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000513 }
514
Chris Lattner3462ae32001-12-03 22:26:30 +0000515 DEF_CAST(Bool , ConstantBool, bool)
Reid Spencere0fc4df2006-10-20 07:07:24 +0000516 DEF_CAST(SByte , ConstantInt, signed char)
517 DEF_CAST(UByte , ConstantInt, unsigned char)
518 DEF_CAST(Short , ConstantInt, signed short)
519 DEF_CAST(UShort, ConstantInt, unsigned short)
520 DEF_CAST(Int , ConstantInt, signed int)
521 DEF_CAST(UInt , ConstantInt, unsigned int)
522 DEF_CAST(Long , ConstantInt, int64_t)
523 DEF_CAST(ULong , ConstantInt, uint64_t)
524 DEF_CAST(Float , ConstantFP , float)
525 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000526#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000527
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000528 static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000529 if (V2->isNullValue()) // X / 0
Chris Lattner268916262003-05-12 15:26:25 +0000530 return 0;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000531 BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000532 return ConstantInt::get(*Ty, R);
Chris Lattner268916262003-05-12 15:26:25 +0000533 }
534
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000535 static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000536 if (V2->isNullValue()) // X / 0
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000537 return 0;
538 if (V2->isAllOnesValue() && // MIN_INT / -1
539 (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
540 return 0;
Reid Spencer7eb55b32006-11-02 01:53:59 +0000541 BuiltinType R = (BuiltinType)(V1->getSExtValue() / V2->getSExtValue());
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000542 return ConstantInt::get(*Ty, R);
543 }
544
Reid Spencer7eb55b32006-11-02 01:53:59 +0000545 static Constant *URem(const ConstantInt *V1,
546 const ConstantInt *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000547 if (V2->isNullValue()) return 0; // X / 0
Reid Spencer7eb55b32006-11-02 01:53:59 +0000548 BuiltinType R = (BuiltinType)(V1->getZExtValue() % V2->getZExtValue());
549 return ConstantInt::get(*Ty, R);
550 }
551
552 static Constant *SRem(const ConstantInt *V1,
553 const ConstantInt *V2) {
554 if (V2->isNullValue()) return 0; // X % 0
555 if (V2->isAllOnesValue() && // MIN_INT % -1
556 (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
Chris Lattner268916262003-05-12 15:26:25 +0000557 return 0;
Reid Spencer7eb55b32006-11-02 01:53:59 +0000558 BuiltinType R = (BuiltinType)(V1->getSExtValue() % V2->getSExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000559 return ConstantInt::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000560 }
Chris Lattner6670d862002-05-06 03:00:54 +0000561
Reid Spencere0fc4df2006-10-20 07:07:24 +0000562 static Constant *And(const ConstantInt *V1, const ConstantInt *V2) {
563 BuiltinType R =
564 (BuiltinType)V1->getZExtValue() & (BuiltinType)V2->getZExtValue();
565 return ConstantInt::get(*Ty, R);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000566 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000567 static Constant *Or(const ConstantInt *V1, const ConstantInt *V2) {
568 BuiltinType R =
569 (BuiltinType)V1->getZExtValue() | (BuiltinType)V2->getZExtValue();
570 return ConstantInt::get(*Ty, R);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000571 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000572 static Constant *Xor(const ConstantInt *V1, const ConstantInt *V2) {
573 BuiltinType R =
574 (BuiltinType)V1->getZExtValue() ^ (BuiltinType)V2->getZExtValue();
575 return ConstantInt::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000576 }
577
Reid Spencere0fc4df2006-10-20 07:07:24 +0000578 static Constant *Shl(const ConstantInt *V1, const ConstantInt *V2) {
579 BuiltinType R =
580 (BuiltinType)V1->getZExtValue() << (BuiltinType)V2->getZExtValue();
581 return ConstantInt::get(*Ty, R);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000582 }
583
Reid Spencere0fc4df2006-10-20 07:07:24 +0000584 static Constant *Shr(const ConstantInt *V1, const ConstantInt *V2) {
585 BuiltinType R =
586 (BuiltinType)V1->getZExtValue() >> (BuiltinType)V2->getZExtValue();
587 return ConstantInt::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000588 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000589};
Chris Lattner6a871e12006-06-21 18:13:36 +0000590} // end anonymous namespace
Chris Lattner0a144ad2002-05-03 21:41:07 +0000591
592
593//===----------------------------------------------------------------------===//
594// DirectFPRules Class
595//===----------------------------------------------------------------------===//
596//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000597/// DirectFPRules provides implementations of functions that are valid on
598/// floating point types, but not all types in general.
599///
Chris Lattner6a871e12006-06-21 18:13:36 +0000600namespace {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000601template <class BuiltinType, Type **Ty>
Chris Lattner02157b02006-06-28 21:38:54 +0000602struct VISIBILITY_HIDDEN DirectFPRules
Reid Spencere0fc4df2006-10-20 07:07:24 +0000603 : public TemplateRules<ConstantFP, DirectFPRules<BuiltinType, Ty> > {
604
605 static Constant *Add(const ConstantFP *V1, const ConstantFP *V2) {
606 BuiltinType R = (BuiltinType)V1->getValue() +
607 (BuiltinType)V2->getValue();
608 return ConstantFP::get(*Ty, R);
609 }
610
611 static Constant *Sub(const ConstantFP *V1, const ConstantFP *V2) {
612 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
613 return ConstantFP::get(*Ty, R);
614 }
615
616 static Constant *Mul(const ConstantFP *V1, const ConstantFP *V2) {
617 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
618 return ConstantFP::get(*Ty, R);
619 }
620
621 static Constant *LessThan(const ConstantFP *V1, const ConstantFP *V2) {
622 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
623 return ConstantBool::get(R);
624 }
625
626 static Constant *EqualTo(const ConstantFP *V1, const ConstantFP *V2) {
627 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
628 return ConstantBool::get(R);
629 }
630
631 static Constant *CastToPointer(const ConstantFP *V,
632 const PointerType *PTy) {
633 if (V->isNullValue()) // Is it a FP or Integral null value?
634 return ConstantPointerNull::get(PTy);
635 return 0; // Can't const prop other types of pointers
636 }
637
638 // Casting operators. ick
639#define DEF_CAST(TYPE, CLASS, CTYPE) \
640 static Constant *CastTo##TYPE (const ConstantFP *V) { \
641 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
642 }
643
644 DEF_CAST(Bool , ConstantBool, bool)
645 DEF_CAST(SByte , ConstantInt, signed char)
646 DEF_CAST(UByte , ConstantInt, unsigned char)
647 DEF_CAST(Short , ConstantInt, signed short)
648 DEF_CAST(UShort, ConstantInt, unsigned short)
649 DEF_CAST(Int , ConstantInt, signed int)
650 DEF_CAST(UInt , ConstantInt, unsigned int)
651 DEF_CAST(Long , ConstantInt, int64_t)
652 DEF_CAST(ULong , ConstantInt, uint64_t)
653 DEF_CAST(Float , ConstantFP , float)
654 DEF_CAST(Double, ConstantFP , double)
655#undef DEF_CAST
656
Reid Spencer7eb55b32006-11-02 01:53:59 +0000657 static Constant *FRem(const ConstantFP *V1, const ConstantFP *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000658 if (V2->isNullValue()) return 0;
659 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
660 (BuiltinType)V2->getValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000661 return ConstantFP::get(*Ty, Result);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000662 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000663 static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) {
Jeff Cohen4e3aede2005-05-03 03:13:01 +0000664 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000665 if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf);
666 if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000667 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000668 return ConstantFP::get(*Ty, R);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000669 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000670};
Chris Lattner6a871e12006-06-21 18:13:36 +0000671} // end anonymous namespace
Chris Lattner62af86e2002-05-03 20:09:52 +0000672
Chris Lattner057083f2006-10-13 17:22:21 +0000673static ManagedStatic<EmptyRules> EmptyR;
674static ManagedStatic<BoolRules> BoolR;
675static ManagedStatic<NullPointerRules> NullPointerR;
676static ManagedStatic<ConstantPackedRules> ConstantPackedR;
677static ManagedStatic<GeneralPackedRules> GeneralPackedR;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000678static ManagedStatic<DirectIntRules<signed char , &Type::SByteTy> > SByteR;
679static ManagedStatic<DirectIntRules<unsigned char , &Type::UByteTy> > UByteR;
680static ManagedStatic<DirectIntRules<signed short , &Type::ShortTy> > ShortR;
681static ManagedStatic<DirectIntRules<unsigned short, &Type::UShortTy> > UShortR;
682static ManagedStatic<DirectIntRules<signed int , &Type::IntTy> > IntR;
683static ManagedStatic<DirectIntRules<unsigned int , &Type::UIntTy> > UIntR;
684static ManagedStatic<DirectIntRules<int64_t , &Type::LongTy> > LongR;
685static ManagedStatic<DirectIntRules<uint64_t , &Type::ULongTy> > ULongR;
686static ManagedStatic<DirectFPRules <float , &Type::FloatTy> > FloatR;
687static ManagedStatic<DirectFPRules <double , &Type::DoubleTy> > DoubleR;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000688
689/// ConstRules::get - This method returns the constant rules implementation that
690/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000691ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000692 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000693 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
694 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner057083f2006-10-13 17:22:21 +0000695 return *EmptyR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000696
Chris Lattner6b727592004-06-17 18:19:28 +0000697 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000698 default: assert(0 && "Unknown value type for constant folding!");
Chris Lattner057083f2006-10-13 17:22:21 +0000699 case Type::BoolTyID: return *BoolR;
700 case Type::PointerTyID: return *NullPointerR;
701 case Type::SByteTyID: return *SByteR;
702 case Type::UByteTyID: return *UByteR;
703 case Type::ShortTyID: return *ShortR;
704 case Type::UShortTyID: return *UShortR;
705 case Type::IntTyID: return *IntR;
706 case Type::UIntTyID: return *UIntR;
707 case Type::LongTyID: return *LongR;
708 case Type::ULongTyID: return *ULongR;
709 case Type::FloatTyID: return *FloatR;
710 case Type::DoubleTyID: return *DoubleR;
Chris Lattner1171d952006-01-04 02:03:29 +0000711 case Type::PackedTyID:
712 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
Chris Lattner057083f2006-10-13 17:22:21 +0000713 return *ConstantPackedR;
714 return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000716}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000717
718
719//===----------------------------------------------------------------------===//
720// ConstantFold*Instruction Implementations
721//===----------------------------------------------------------------------===//
722//
723// These methods contain the special case hackery required to symbolically
724// evaluate some constant expression cases, and use the ConstantRules class to
725// evaluate normal constants.
726//
727static unsigned getSize(const Type *Ty) {
728 unsigned S = Ty->getPrimitiveSize();
729 return S ? S : 8; // Treat pointers at 8 bytes
730}
731
Chris Lattner6b3f4752006-04-02 01:38:28 +0000732/// CastConstantPacked - Convert the specified ConstantPacked node to the
733/// specified packed type. At this point, we know that the elements of the
734/// input packed constant are all simple integer or FP values.
735static Constant *CastConstantPacked(ConstantPacked *CP,
736 const PackedType *DstTy) {
737 unsigned SrcNumElts = CP->getType()->getNumElements();
738 unsigned DstNumElts = DstTy->getNumElements();
739 const Type *SrcEltTy = CP->getType()->getElementType();
740 const Type *DstEltTy = DstTy->getElementType();
741
742 // If both vectors have the same number of elements (thus, the elements
743 // are the same size), perform the conversion now.
744 if (SrcNumElts == DstNumElts) {
745 std::vector<Constant*> Result;
746
747 // If the src and dest elements are both integers, just cast each one
748 // which will do the appropriate bit-convert.
749 if (SrcEltTy->isIntegral() && DstEltTy->isIntegral()) {
750 for (unsigned i = 0; i != SrcNumElts; ++i)
751 Result.push_back(ConstantExpr::getCast(CP->getOperand(i),
752 DstEltTy));
753 return ConstantPacked::get(Result);
754 }
755
756 if (SrcEltTy->isIntegral()) {
757 // Otherwise, this is an int-to-fp cast.
758 assert(DstEltTy->isFloatingPoint());
759 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
760 for (unsigned i = 0; i != SrcNumElts; ++i) {
761 double V =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000762 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +0000763 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
764 }
765 return ConstantPacked::get(Result);
766 }
767 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
768 for (unsigned i = 0; i != SrcNumElts; ++i) {
769 float V =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000770 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +0000771 Result.push_back(ConstantFP::get(Type::FloatTy, V));
772 }
773 return ConstantPacked::get(Result);
774 }
775
776 // Otherwise, this is an fp-to-int cast.
777 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
778
779 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
780 for (unsigned i = 0; i != SrcNumElts; ++i) {
781 uint64_t V =
782 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000783 Constant *C = ConstantInt::get(Type::ULongTy, V);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000784 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
785 }
786 return ConstantPacked::get(Result);
787 }
788
789 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
790 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000791 uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
792 Constant *C = ConstantInt::get(Type::UIntTy, V);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000793 Result.push_back(ConstantExpr::getCast(C, DstEltTy));
794 }
795 return ConstantPacked::get(Result);
796 }
797
798 // Otherwise, this is a cast that changes element count and size. Handle
799 // casts which shrink the elements here.
800
801 // FIXME: We need to know endianness to do this!
802
803 return 0;
804}
805
806
Chris Lattner1dd054c2004-01-12 22:07:24 +0000807Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
808 const Type *DestTy) {
809 if (V->getType() == DestTy) return (Constant*)V;
810
Chris Lattnerea0789c2004-03-08 06:17:35 +0000811 // Cast of a global address to boolean is always true.
Reid Spencer3054b142006-11-02 08:18:15 +0000812 if (isa<GlobalValue>(V)) {
Chris Lattnerea0789c2004-03-08 06:17:35 +0000813 if (DestTy == Type::BoolTy)
814 // FIXME: When we support 'external weak' references, we have to prevent
Chris Lattnercd4003e2005-01-06 16:26:38 +0000815 // this transformation from happening. This code will need to be updated
816 // to ignore external weak symbols when we support it.
Chris Lattner78430662006-09-28 23:34:49 +0000817 return ConstantBool::getTrue();
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000818 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000819 if (CE->getOpcode() == Instruction::Cast) {
820 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
821 // Try to not produce a cast of a cast, which is almost always redundant.
822 if (!Op->getType()->isFloatingPoint() &&
823 !CE->getType()->isFloatingPoint() &&
Reid Spencer8eb06df2004-05-30 01:19:48 +0000824 !DestTy->isFloatingPoint()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000825 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
826 unsigned S3 = getSize(DestTy);
827 if (Op->getType() == DestTy && S3 >= S2)
828 return Op;
829 if (S1 >= S2 && S2 >= S3)
830 return ConstantExpr::getCast(Op, DestTy);
831 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
832 return ConstantExpr::getCast(Op, DestTy);
833 }
834 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
835 // If all of the indexes in the GEP are null values, there is no pointer
836 // adjustment going on. We might as well cast the source pointer.
837 bool isAllNull = true;
838 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
839 if (!CE->getOperand(i)->isNullValue()) {
840 isAllNull = false;
841 break;
842 }
843 if (isAllNull)
844 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
845 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000846 } else if (isa<UndefValue>(V)) {
847 return UndefValue::get(DestTy);
848 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000849
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000850 // Check to see if we are casting an pointer to an aggregate to a pointer to
851 // the first element. If so, return the appropriate GEP instruction.
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000852 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000853 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
854 std::vector<Value*> IdxList;
855 IdxList.push_back(Constant::getNullValue(Type::IntTy));
856 const Type *ElTy = PTy->getElementType();
857 while (ElTy != DPTy->getElementType()) {
858 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
Chris Lattner9e907202004-11-22 19:15:27 +0000859 if (STy->getNumElements() == 0) break;
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000860 ElTy = STy->getElementType(0);
861 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
862 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
863 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
864 ElTy = STy->getElementType();
865 IdxList.push_back(IdxList[0]);
866 } else {
867 break;
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000868 }
Chris Lattnerba18b9a2004-11-17 17:59:35 +0000869 }
870
871 if (ElTy == DPTy->getElementType())
872 return ConstantExpr::getGetElementPtr(const_cast<Constant*>(V),IdxList);
873 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000874
875 // Handle casts from one packed constant to another. We know that the src and
876 // dest type have the same size.
877 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
878 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
879 assert(DestPTy->getElementType()->getPrimitiveSizeInBits() *
880 DestPTy->getNumElements() ==
881 SrcTy->getElementType()->getPrimitiveSizeInBits() *
882 SrcTy->getNumElements() && "Not cast between same sized vectors!");
883 if (isa<ConstantAggregateZero>(V))
884 return Constant::getNullValue(DestTy);
885 if (isa<UndefValue>(V))
886 return UndefValue::get(DestTy);
887 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
888 // This is a cast from a ConstantPacked of one type to a ConstantPacked
889 // of another type. Check to see if all elements of the input are
890 // simple.
891 bool AllSimpleConstants = true;
892 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
893 if (!isa<ConstantInt>(CP->getOperand(i)) &&
894 !isa<ConstantFP>(CP->getOperand(i))) {
895 AllSimpleConstants = false;
896 break;
897 }
898 }
899
900 // If all of the elements are simple constants, we can fold this.
901 if (AllSimpleConstants)
902 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
903 }
904 }
905 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000906
Chris Lattner1dd054c2004-01-12 22:07:24 +0000907 ConstRules &Rules = ConstRules::get(V, V);
908
Chris Lattner6b727592004-06-17 18:19:28 +0000909 switch (DestTy->getTypeID()) {
Chris Lattner1dd054c2004-01-12 22:07:24 +0000910 case Type::BoolTyID: return Rules.castToBool(V);
911 case Type::UByteTyID: return Rules.castToUByte(V);
912 case Type::SByteTyID: return Rules.castToSByte(V);
913 case Type::UShortTyID: return Rules.castToUShort(V);
914 case Type::ShortTyID: return Rules.castToShort(V);
915 case Type::UIntTyID: return Rules.castToUInt(V);
916 case Type::IntTyID: return Rules.castToInt(V);
917 case Type::ULongTyID: return Rules.castToULong(V);
918 case Type::LongTyID: return Rules.castToLong(V);
919 case Type::FloatTyID: return Rules.castToFloat(V);
920 case Type::DoubleTyID: return Rules.castToDouble(V);
921 case Type::PointerTyID:
922 return Rules.castToPointer(V, cast<PointerType>(DestTy));
923 default: return 0;
924 }
925}
926
Chris Lattner6ea4b522004-03-12 05:53:32 +0000927Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
928 const Constant *V1,
929 const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000930 if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
931 return const_cast<Constant*>(CB->getValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000932
933 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
934 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
935 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000936 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000937 return 0;
938}
939
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000940Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
941 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000942 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
943 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000944 if (Val->isNullValue()) // ee(zero, x) -> zero
945 return Constant::getNullValue(
946 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000947
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000948 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000949 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
950 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000951 } else if (isa<UndefValue>(Idx)) {
952 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
953 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000954 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000955 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000956 return 0;
957}
958
Robert Bocchinoca27f032006-01-17 20:07:22 +0000959Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
960 const Constant *Elt,
961 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000962 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000963 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000964 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000965 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000966 // Insertion of scalar constant into packed undef
967 // Optimize away insertion of undef
968 if (isa<UndefValue>(Elt))
969 return const_cast<Constant*>(Val);
970 // Otherwise break the aggregate undef into multiple undefs and do
971 // the insertion
972 unsigned numOps =
973 cast<PackedType>(Val->getType())->getNumElements();
974 std::vector<Constant*> Ops;
975 Ops.reserve(numOps);
976 for (unsigned i = 0; i < numOps; ++i) {
977 const Constant *Op =
978 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
979 Ops.push_back(const_cast<Constant*>(Op));
980 }
981 return ConstantPacked::get(Ops);
982 }
Reid Spencer3054b142006-11-02 08:18:15 +0000983 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000984 // Insertion of scalar constant into packed aggregate zero
985 // Optimize away insertion of zero
986 if (Elt->isNullValue())
987 return const_cast<Constant*>(Val);
988 // Otherwise break the aggregate zero into multiple zeros and do
989 // the insertion
990 unsigned numOps =
991 cast<PackedType>(Val->getType())->getNumElements();
992 std::vector<Constant*> Ops;
993 Ops.reserve(numOps);
994 for (unsigned i = 0; i < numOps; ++i) {
995 const Constant *Op =
996 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
997 Ops.push_back(const_cast<Constant*>(Op));
998 }
999 return ConstantPacked::get(Ops);
1000 }
1001 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
1002 // Insertion of scalar constant into packed constant
1003 std::vector<Constant*> Ops;
1004 Ops.reserve(CVal->getNumOperands());
1005 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
1006 const Constant *Op =
1007 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
1008 Ops.push_back(const_cast<Constant*>(Op));
1009 }
1010 return ConstantPacked::get(Ops);
1011 }
1012 return 0;
1013}
1014
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001015Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
1016 const Constant *V2,
1017 const Constant *Mask) {
1018 // TODO:
1019 return 0;
1020}
1021
1022
Chris Lattner60c47262005-01-28 19:09:51 +00001023/// isZeroSizedType - This type is zero sized if its an array or structure of
1024/// zero sized types. The only leaf zero sized type is an empty structure.
1025static bool isMaybeZeroSizedType(const Type *Ty) {
1026 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1027 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1028
1029 // If all of elements have zero size, this does too.
1030 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +00001031 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +00001032 return true;
1033
1034 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1035 return isMaybeZeroSizedType(ATy->getElementType());
1036 }
1037 return false;
1038}
Chris Lattner6ea4b522004-03-12 05:53:32 +00001039
Chris Lattner061da2f2004-01-13 05:51:55 +00001040/// IdxCompare - Compare the two constants as though they were getelementptr
1041/// indices. This allows coersion of the types to be the same thing.
1042///
1043/// If the two constants are the "same" (after coersion), return 0. If the
1044/// first is less than the second, return -1, if the second is less than the
1045/// first, return 1. If the constants are not integral, return -2.
1046///
Chris Lattner60c47262005-01-28 19:09:51 +00001047static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001048 if (C1 == C2) return 0;
1049
1050 // Ok, we found a different index. Are either of the operands
1051 // ConstantExprs? If so, we can't do anything with them.
1052 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1053 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001054
Chris Lattner69193f92004-04-05 01:30:19 +00001055 // Ok, we have two differing integer indices. Sign extend them to be the same
1056 // type. Long is always big enough, so we use it.
1057 C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
1058 C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
Chris Lattner061da2f2004-01-13 05:51:55 +00001059 if (C1 == C2) return 0; // Are they just differing types?
1060
Chris Lattner60c47262005-01-28 19:09:51 +00001061 // If the type being indexed over is really just a zero sized type, there is
1062 // no pointer difference being made here.
1063 if (isMaybeZeroSizedType(ElTy))
1064 return -2; // dunno.
1065
Chris Lattner061da2f2004-01-13 05:51:55 +00001066 // If they are really different, now that they are the same type, then we
1067 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +00001068 if (cast<ConstantInt>(C1)->getSExtValue() <
1069 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001070 return -1;
1071 else
1072 return 1;
1073}
1074
1075/// evaluateRelation - This function determines if there is anything we can
1076/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001077/// things like integer comparisons, but should instead handle ConstantExprs
1078/// and GlobalValuess. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +00001079/// particular relation to each other, we should return the corresponding SetCC
1080/// code, otherwise return Instruction::BinaryOpsEnd.
1081///
1082/// To simplify this code we canonicalize the relation so that the first
1083/// operand is always the most "complex" of the two. We consider simple
1084/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001085/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001086///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001087static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001088 assert(V1->getType() == V2->getType() &&
1089 "Cannot compare different types of values!");
1090 if (V1 == V2) return Instruction::SetEQ;
1091
Reid Spenceraccd7c72004-07-17 23:47:01 +00001092 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001093 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1094 // We distilled this down to a simple case, use the standard constant
1095 // folder.
1096 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001097 if (R && R->getValue()) return Instruction::SetEQ;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001098 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001099 if (R && R->getValue()) return Instruction::SetLT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001100 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001101 if (R && R->getValue()) return Instruction::SetGT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001102
1103 // If we couldn't figure it out, bail.
1104 return Instruction::BinaryOpsEnd;
1105 }
1106
Chris Lattner061da2f2004-01-13 05:51:55 +00001107 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +00001108 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1109 if (SwappedRelation != Instruction::BinaryOpsEnd)
1110 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001111
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001112 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001113 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001114 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1115 if (SwappedRelation != Instruction::BinaryOpsEnd)
1116 return SetCondInst::getSwappedCondition(SwappedRelation);
1117 else
1118 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +00001119 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001120
Reid Spenceraccd7c72004-07-17 23:47:01 +00001121 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001122 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001123 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
1124 assert(CPR1 != CPR2 &&
1125 "GVs for the same value exist at different addresses??");
Chris Lattner061da2f2004-01-13 05:51:55 +00001126 // FIXME: If both globals are external weak, they might both be null!
1127 return Instruction::SetNE;
1128 } else {
1129 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1130 // Global can never be null. FIXME: if we implement external weak
1131 // linkage, this is not necessarily true!
1132 return Instruction::SetNE;
1133 }
1134
1135 } else {
1136 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1137 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001138 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +00001139 Constant *CE1Op0 = CE1->getOperand(0);
1140
1141 switch (CE1->getOpcode()) {
1142 case Instruction::Cast:
1143 // If the cast is not actually changing bits, and the second operand is a
1144 // null pointer, do the comparison with the pre-casted value.
1145 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001146 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +00001147 return evaluateRelation(CE1Op0,
1148 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001149
1150 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1151 // from the same type as the src of the LHS, evaluate the inputs. This is
1152 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1153 // which happens a lot in compilers with tagged integers.
1154 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
1155 if (isa<PointerType>(CE1->getType()) &&
1156 CE2->getOpcode() == Instruction::Cast &&
1157 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1158 CE1->getOperand(0)->getType()->isIntegral()) {
1159 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1160 }
Chris Lattner192e3262004-04-11 01:29:30 +00001161 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001162
1163 case Instruction::GetElementPtr:
1164 // Ok, since this is a getelementptr, we know that the constant has a
1165 // pointer type. Check the various cases.
1166 if (isa<ConstantPointerNull>(V2)) {
1167 // If we are comparing a GEP to a null pointer, check to see if the base
1168 // of the GEP equals the null pointer.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001169 if (isa<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001170 // FIXME: this is not true when we have external weak references!
1171 // No offset can go from a global to a null pointer.
1172 return Instruction::SetGT;
1173 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1174 // If we are indexing from a null pointer, check to see if we have any
1175 // non-zero indices.
1176 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1177 if (!CE1->getOperand(i)->isNullValue())
1178 // Offsetting from null, must not be equal.
1179 return Instruction::SetGT;
1180 // Only zero indexes from null, must still be zero.
1181 return Instruction::SetEQ;
1182 }
1183 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001184 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001185 if (isa<ConstantPointerNull>(CE1Op0)) {
1186 // FIXME: This is not true with external weak references.
1187 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001188 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001189 if (CPR1 == CPR2) {
1190 // If this is a getelementptr of the same global, then it must be
1191 // different. Because the types must match, the getelementptr could
1192 // only have at most one index, and because we fold getelementptr's
1193 // with a single zero index, it must be nonzero.
1194 assert(CE1->getNumOperands() == 2 &&
1195 !CE1->getOperand(1)->isNullValue() &&
1196 "Suprising getelementptr!");
1197 return Instruction::SetGT;
1198 } else {
1199 // If they are different globals, we don't know what the value is,
1200 // but they can't be equal.
1201 return Instruction::SetNE;
1202 }
1203 }
1204 } else {
1205 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1206 const Constant *CE2Op0 = CE2->getOperand(0);
1207
1208 // There are MANY other foldings that we could perform here. They will
1209 // probably be added on demand, as they seem needed.
1210 switch (CE2->getOpcode()) {
1211 default: break;
1212 case Instruction::GetElementPtr:
1213 // By far the most common case to handle is when the base pointers are
1214 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001215 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001216 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1217 return Instruction::SetNE;
1218 // Ok, we know that both getelementptr instructions are based on the
1219 // same global. From this, we can precisely determine the relative
1220 // ordering of the resultant pointers.
1221 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001222
Chris Lattner061da2f2004-01-13 05:51:55 +00001223 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001224 gep_type_iterator GTI = gep_type_begin(CE1);
1225 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1226 ++i, ++GTI)
1227 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1228 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001229 case -1: return Instruction::SetLT;
1230 case 1: return Instruction::SetGT;
1231 case -2: return Instruction::BinaryOpsEnd;
1232 }
1233
1234 // Ok, we ran out of things they have in common. If any leftovers
1235 // are non-zero then we have a difference, otherwise we are equal.
1236 for (; i < CE1->getNumOperands(); ++i)
1237 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001238 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1239 return Instruction::SetGT;
1240 else
1241 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001242
Chris Lattner061da2f2004-01-13 05:51:55 +00001243 for (; i < CE2->getNumOperands(); ++i)
1244 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001245 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1246 return Instruction::SetLT;
1247 else
1248 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001249 return Instruction::SetEQ;
1250 }
1251 }
1252 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001253
Chris Lattner061da2f2004-01-13 05:51:55 +00001254 default:
1255 break;
1256 }
1257 }
1258
1259 return Instruction::BinaryOpsEnd;
1260}
1261
Chris Lattner1dd054c2004-01-12 22:07:24 +00001262Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1263 const Constant *V1,
1264 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001265 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001266 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001267 default: break;
1268 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1269 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1270 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001271 case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break;
1272 case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break;
1273 case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001274 case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break;
1275 case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break;
1276 case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001277 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1278 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1279 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1280 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
1281 case Instruction::Shr: C = ConstRules::get(V1, V2).shr(V1, V2); break;
1282 case Instruction::SetEQ: C = ConstRules::get(V1, V2).equalto(V1, V2); break;
1283 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1284 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001285 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
1286 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001287 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001288 break;
1289 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1290 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001291 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001292 break;
1293 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1294 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001295 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001296 break;
1297 }
1298
Chris Lattner061da2f2004-01-13 05:51:55 +00001299 // If we successfully folded the expression, return it now.
1300 if (C) return C;
1301
Chris Lattnere1496fb2006-09-17 19:14:47 +00001302 if (SetCondInst::isComparison(Opcode)) {
Chris Lattner192eacc2004-10-17 04:01:51 +00001303 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1304 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001305 switch (evaluateRelation(const_cast<Constant*>(V1),
1306 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001307 default: assert(0 && "Unknown relational!");
1308 case Instruction::BinaryOpsEnd:
1309 break; // Couldn't determine anything about these constants.
1310 case Instruction::SetEQ: // We know the constants are equal!
1311 // If we know the constants are equal, we can decide the result of this
1312 // computation precisely.
1313 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1314 Opcode == Instruction::SetLE ||
1315 Opcode == Instruction::SetGE);
1316 case Instruction::SetLT:
1317 // If we know that V1 < V2, we can decide the result of this computation
1318 // precisely.
1319 return ConstantBool::get(Opcode == Instruction::SetLT ||
1320 Opcode == Instruction::SetNE ||
1321 Opcode == Instruction::SetLE);
1322 case Instruction::SetGT:
1323 // If we know that V1 > V2, we can decide the result of this computation
1324 // precisely.
1325 return ConstantBool::get(Opcode == Instruction::SetGT ||
1326 Opcode == Instruction::SetNE ||
1327 Opcode == Instruction::SetGE);
1328 case Instruction::SetLE:
1329 // If we know that V1 <= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001330 if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
1331 if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001332 break;
1333
1334 case Instruction::SetGE:
1335 // If we know that V1 >= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001336 if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
1337 if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001338 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001339
Chris Lattner061da2f2004-01-13 05:51:55 +00001340 case Instruction::SetNE:
1341 // If we know that V1 != V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001342 if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
1343 if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001344 break;
1345 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001346 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001347
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001348 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1349 switch (Opcode) {
1350 case Instruction::Add:
1351 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001352 case Instruction::Xor:
1353 return UndefValue::get(V1->getType());
1354
1355 case Instruction::Mul:
1356 case Instruction::And:
1357 return Constant::getNullValue(V1->getType());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001358 case Instruction::UDiv:
1359 case Instruction::SDiv:
1360 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001361 case Instruction::URem:
1362 case Instruction::SRem:
1363 case Instruction::FRem:
1364 if (!isa<UndefValue>(V2)) // undef / X -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001365 return Constant::getNullValue(V1->getType());
Reid Spencer7eb55b32006-11-02 01:53:59 +00001366 return const_cast<Constant*>(V2); // X / undef -> undef
1367 case Instruction::Or: // X | undef -> -1
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001368 return ConstantInt::getAllOnesValue(V1->getType());
1369 case Instruction::Shr:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001370 if (!isa<UndefValue>(V2)) {
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001371 if (V1->getType()->isSigned())
Reid Spencer7eb55b32006-11-02 01:53:59 +00001372 return const_cast<Constant*>(V1); // undef >>s X -> undef
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001373 // undef >>u X -> 0
1374 } else if (isa<UndefValue>(V1)) {
Reid Spencer7eb55b32006-11-02 01:53:59 +00001375 return const_cast<Constant*>(V1); // undef >> undef -> undef
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001376 } else {
1377 if (V1->getType()->isSigned())
Reid Spencer7eb55b32006-11-02 01:53:59 +00001378 return const_cast<Constant*>(V1); // X >>s undef -> X
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001379 }
Reid Spencer7eb55b32006-11-02 01:53:59 +00001380 return Constant::getNullValue(V1->getType());// X >>u undef -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001381
1382 case Instruction::Shl:
1383 // undef << X -> 0 X << undef -> 0
1384 return Constant::getNullValue(V1->getType());
1385 }
1386 }
1387
Chris Lattner061da2f2004-01-13 05:51:55 +00001388 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
Reid Spencer3054b142006-11-02 08:18:15 +00001389 if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001390 // There are many possible foldings we could do here. We should probably
1391 // at least fold add of a pointer with an integer into the appropriate
1392 // getelementptr. This will improve alias analysis a bit.
Chris Lattner061da2f2004-01-13 05:51:55 +00001393 } else {
1394 // Just implement a couple of simple identities.
1395 switch (Opcode) {
1396 case Instruction::Add:
1397 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1398 break;
1399 case Instruction::Sub:
1400 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1401 break;
1402 case Instruction::Mul:
1403 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1404 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001405 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001406 return const_cast<Constant*>(V1); // X * 1 == X
1407 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001408 case Instruction::UDiv:
1409 case Instruction::SDiv:
Chris Lattner061da2f2004-01-13 05:51:55 +00001410 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001411 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001412 return const_cast<Constant*>(V1); // X / 1 == X
1413 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001414 case Instruction::URem:
1415 case Instruction::SRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001416 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001417 if (CI->getZExtValue() == 1)
Reid Spencer7eb55b32006-11-02 01:53:59 +00001418 return Constant::getNullValue(CI->getType()); // X % 1 == 0
Chris Lattner061da2f2004-01-13 05:51:55 +00001419 break;
1420 case Instruction::And:
1421 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1422 return const_cast<Constant*>(V1); // X & -1 == X
1423 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Chris Lattnerea0789c2004-03-08 06:17:35 +00001424 if (CE1->getOpcode() == Instruction::Cast &&
Reid Spenceraccd7c72004-07-17 23:47:01 +00001425 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001426 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001427
1428 // Functions are at least 4-byte aligned. If and'ing the address of a
1429 // function with a constant < 4, fold it to zero.
1430 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001431 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001432 return Constant::getNullValue(CI->getType());
1433 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001434 break;
1435 case Instruction::Or:
1436 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1437 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1438 return const_cast<Constant*>(V2); // X | -1 == -1
1439 break;
1440 case Instruction::Xor:
1441 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1442 break;
1443 }
1444 }
1445
Reid Spencer3054b142006-11-02 08:18:15 +00001446 } else if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001447 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1448 // other way if possible.
1449 switch (Opcode) {
1450 case Instruction::Add:
1451 case Instruction::Mul:
1452 case Instruction::And:
1453 case Instruction::Or:
1454 case Instruction::Xor:
1455 case Instruction::SetEQ:
1456 case Instruction::SetNE:
1457 // No change of opcode required.
1458 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1459
1460 case Instruction::SetLT:
1461 case Instruction::SetGT:
1462 case Instruction::SetLE:
1463 case Instruction::SetGE:
1464 // Change the opcode as necessary to swap the operands.
1465 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1466 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1467
1468 case Instruction::Shl:
1469 case Instruction::Shr:
1470 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001471 case Instruction::SDiv:
1472 case Instruction::UDiv:
1473 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001474 case Instruction::URem:
1475 case Instruction::SRem:
1476 case Instruction::FRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001477 default: // These instructions cannot be flopped around.
1478 break;
1479 }
1480 }
1481 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001482}
1483
1484Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001485 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001486 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001487 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001488 return const_cast<Constant*>(C);
1489
Chris Lattnerf6013752004-10-17 21:54:55 +00001490 if (isa<UndefValue>(C)) {
1491 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1492 true);
1493 assert(Ty != 0 && "Invalid indices for GEP!");
1494 return UndefValue::get(PointerType::get(Ty));
1495 }
1496
1497 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001498 if (C->isNullValue()) {
1499 bool isNull = true;
1500 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001501 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001502 isNull = false;
1503 break;
1504 }
1505 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001506 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001507 true);
1508 assert(Ty != 0 && "Invalid indices for GEP!");
1509 return ConstantPointerNull::get(PointerType::get(Ty));
1510 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001511
1512 if (IdxList.size() == 1) {
1513 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
Reid Spencere0fc4df2006-10-20 07:07:24 +00001514 if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
Chris Lattner4bbd4092004-07-15 01:16:59 +00001515 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1516 // type, we can statically fold this.
Reid Spencere0fc4df2006-10-20 07:07:24 +00001517 Constant *R = ConstantInt::get(Type::UIntTy, ElSize);
Chris Lattner13128ab2004-10-11 22:52:25 +00001518 R = ConstantExpr::getCast(R, Idx0->getType());
1519 R = ConstantExpr::getMul(R, Idx0);
Chris Lattner4bbd4092004-07-15 01:16:59 +00001520 return ConstantExpr::getCast(R, C->getType());
1521 }
1522 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001523 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001524
1525 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1526 // Combine Indices - If the source pointer to this getelementptr instruction
1527 // is a getelementptr instruction, combine the indices of the two
1528 // getelementptr instructions into a single instruction.
1529 //
1530 if (CE->getOpcode() == Instruction::GetElementPtr) {
1531 const Type *LastTy = 0;
1532 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1533 I != E; ++I)
1534 LastTy = *I;
1535
Chris Lattner13128ab2004-10-11 22:52:25 +00001536 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1537 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001538 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1539 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001540 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001541
1542 // Add the last index of the source with the first index of the new GEP.
1543 // Make sure to handle the case when they are actually different types.
1544 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001545 // Otherwise it must be an array.
1546 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001547 const Type *IdxTy = Combined->getType();
Chris Lattner13128ab2004-10-11 22:52:25 +00001548 if (IdxTy != Idx0->getType()) IdxTy = Type::LongTy;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001549 Combined =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001550 ConstantExpr::get(Instruction::Add,
Chris Lattner13128ab2004-10-11 22:52:25 +00001551 ConstantExpr::getCast(Idx0, IdxTy),
Chris Lattner71068a02004-07-07 04:45:13 +00001552 ConstantExpr::getCast(Combined, IdxTy));
1553 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001554
Chris Lattner1dd054c2004-01-12 22:07:24 +00001555 NewIndices.push_back(Combined);
1556 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1557 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1558 }
1559 }
1560
1561 // Implement folding of:
1562 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1563 // long 0, long 0)
1564 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1565 //
1566 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
Chris Lattner13128ab2004-10-11 22:52:25 +00001567 Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001568 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001569 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1570 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1571 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001572 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001573 if (CAT->getElementType() == SAT->getElementType())
1574 return ConstantExpr::getGetElementPtr(
1575 (Constant*)CE->getOperand(0), IdxList);
1576 }
1577 return 0;
1578}
1579