blob: 8af5b89522054ad248bd46ac477af2d5a0eae1db [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
Chris Lattner5a945e32004-01-12 21:13:12 +000057 // ConstRules::get - Return an instance of ConstRules for the specified
58 // constant operands.
59 //
60 static ConstRules &get(const Constant *V1, const Constant *V2);
61 private:
62 ConstRules(const ConstRules &); // Do not implement
63 ConstRules &operator=(const ConstRules &); // Do not implement
64 };
65}
66
67
Chris Lattner2f7c9632001-06-06 20:29:01 +000068//===----------------------------------------------------------------------===//
69// TemplateRules Class
70//===----------------------------------------------------------------------===//
71//
Misha Brukmanb1c93172005-04-21 23:48:37 +000072// TemplateRules - Implement a subclass of ConstRules that provides all
73// operations as noops. All other rules classes inherit from this class so
74// that if functionality is needed in the future, it can simply be added here
Chris Lattner2f7c9632001-06-06 20:29:01 +000075// and to ConstRules without changing anything else...
Misha Brukmanb1c93172005-04-21 23:48:37 +000076//
Chris Lattner2f7c9632001-06-06 20:29:01 +000077// This class also provides subclasses with typesafe implementations of methods
78// so that don't have to do type casting.
79//
Chris Lattner6a871e12006-06-21 18:13:36 +000080namespace {
Chris Lattner2f7c9632001-06-06 20:29:01 +000081template<class ArgType, class SubClassName>
Chris Lattner02157b02006-06-28 21:38:54 +000082class VISIBILITY_HIDDEN TemplateRules : public ConstRules {
Chris Lattner2f7c9632001-06-06 20:29:01 +000083
Reid Spencer9c47b252005-04-24 22:27:20 +000084
Chris Lattner2f7c9632001-06-06 20:29:01 +000085 //===--------------------------------------------------------------------===//
86 // Redirecting functions that cast to the appropriate types
87 //===--------------------------------------------------------------------===//
88
Misha Brukmanb1c93172005-04-21 23:48:37 +000089 virtual Constant *add(const Constant *V1, const Constant *V2) const {
90 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +000091 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000092 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
93 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner2f7c9632001-06-06 20:29:01 +000094 }
Misha Brukmanb1c93172005-04-21 23:48:37 +000095 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
96 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner4f6031f2001-07-20 19:15:36 +000097 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +000098 virtual Constant *udiv(const Constant *V1, const Constant *V2) const {
99 return SubClassName::UDiv((const ArgType *)V1, (const ArgType *)V2);
100 }
101 virtual Constant *sdiv(const Constant *V1, const Constant *V2) const {
102 return SubClassName::SDiv((const ArgType *)V1, (const ArgType *)V2);
103 }
104 virtual Constant *fdiv(const Constant *V1, const Constant *V2) const {
105 return SubClassName::FDiv((const ArgType *)V1, (const ArgType *)V2);
Chris Lattneraf259a72002-04-07 08:10:14 +0000106 }
Reid Spencer7eb55b32006-11-02 01:53:59 +0000107 virtual Constant *urem(const Constant *V1, const Constant *V2) const {
108 return SubClassName::URem((const ArgType *)V1, (const ArgType *)V2);
109 }
110 virtual Constant *srem(const Constant *V1, const Constant *V2) const {
111 return SubClassName::SRem((const ArgType *)V1, (const ArgType *)V2);
112 }
113 virtual Constant *frem(const Constant *V1, const Constant *V2) const {
114 return SubClassName::FRem((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000115 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000116 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
117 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000118 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000119 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
120 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000121 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000122 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
123 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000124 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000125 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
126 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000127 }
Reid Spencerfdff9382006-11-08 06:47:33 +0000128 virtual Constant *lshr(const Constant *V1, const Constant *V2) const {
129 return SubClassName::LShr((const ArgType *)V1, (const ArgType *)V2);
130 }
131 virtual Constant *ashr(const Constant *V1, const Constant *V2) const {
132 return SubClassName::AShr((const ArgType *)V1, (const ArgType *)V2);
Chris Lattner6670d862002-05-06 03:00:54 +0000133 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000134
Misha Brukmanb1c93172005-04-21 23:48:37 +0000135 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
137 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000138 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000139 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
140 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141
Chris Lattner55406842001-07-21 19:10:49 +0000142
Chris Lattner2f7c9632001-06-06 20:29:01 +0000143 //===--------------------------------------------------------------------===//
144 // Default "noop" implementations
145 //===--------------------------------------------------------------------===//
146
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000147 static Constant *Add (const ArgType *V1, const ArgType *V2) { return 0; }
148 static Constant *Sub (const ArgType *V1, const ArgType *V2) { return 0; }
149 static Constant *Mul (const ArgType *V1, const ArgType *V2) { return 0; }
150 static Constant *SDiv(const ArgType *V1, const ArgType *V2) { return 0; }
151 static Constant *UDiv(const ArgType *V1, const ArgType *V2) { return 0; }
152 static Constant *FDiv(const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer7eb55b32006-11-02 01:53:59 +0000153 static Constant *URem(const ArgType *V1, const ArgType *V2) { return 0; }
154 static Constant *SRem(const ArgType *V1, const ArgType *V2) { return 0; }
155 static Constant *FRem(const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000156 static Constant *And (const ArgType *V1, const ArgType *V2) { return 0; }
157 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
158 static Constant *Xor (const ArgType *V1, const ArgType *V2) { return 0; }
159 static Constant *Shl (const ArgType *V1, const ArgType *V2) { return 0; }
Reid Spencerfdff9382006-11-08 06:47:33 +0000160 static Constant *LShr(const ArgType *V1, const ArgType *V2) { return 0; }
161 static Constant *AShr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000162 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163 return 0;
164 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000165 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000166 return 0;
167 }
Chris Lattner55406842001-07-21 19:10:49 +0000168
Reid Spencer9c47b252005-04-24 22:27:20 +0000169public:
170 virtual ~TemplateRules() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171};
Chris Lattner6a871e12006-06-21 18:13:36 +0000172} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173
174
175//===----------------------------------------------------------------------===//
176// EmptyRules Class
177//===----------------------------------------------------------------------===//
178//
179// EmptyRules provides a concrete base class of ConstRules that does nothing
180//
Chris Lattner6a871e12006-06-21 18:13:36 +0000181namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000182struct VISIBILITY_HIDDEN EmptyRules
183 : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000184 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000185 if (V1 == V2) return ConstantBool::getTrue();
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000186 return 0;
187 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000188};
Chris Lattner6a871e12006-06-21 18:13:36 +0000189} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190
191
192
193//===----------------------------------------------------------------------===//
194// BoolRules Class
195//===----------------------------------------------------------------------===//
196//
197// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
198//
Chris Lattner6a871e12006-06-21 18:13:36 +0000199namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000200struct VISIBILITY_HIDDEN BoolRules
201 : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000203 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner07507a42002-09-03 20:09:49 +0000204 return ConstantBool::get(V1->getValue() < V2->getValue());
205 }
206
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000207 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000208 return ConstantBool::get(V1 == V2);
209 }
210
Chris Lattnere87f65e2002-07-30 16:24:28 +0000211 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
212 return ConstantBool::get(V1->getValue() & V2->getValue());
213 }
214
215 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000216 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217 }
218
Chris Lattnere87f65e2002-07-30 16:24:28 +0000219 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
220 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000222};
Chris Lattner6a871e12006-06-21 18:13:36 +0000223} // end anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224
225
226//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000227// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000228//===----------------------------------------------------------------------===//
229//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000230// NullPointerRules provides a concrete base class of ConstRules for null
231// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000232//
Chris Lattner6a871e12006-06-21 18:13:36 +0000233namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000234struct VISIBILITY_HIDDEN NullPointerRules
235 : public TemplateRules<ConstantPointerNull, NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000236 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000237 return ConstantBool::getTrue(); // Null pointers are always equal
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000238 }
Chris Lattner977f0042001-11-01 05:55:13 +0000239};
Chris Lattner6a871e12006-06-21 18:13:36 +0000240} // end anonymous namespace
Chris Lattner977f0042001-11-01 05:55:13 +0000241
Chris Lattner1171d952006-01-04 02:03:29 +0000242//===----------------------------------------------------------------------===//
243// ConstantPackedRules Class
244//===----------------------------------------------------------------------===//
245
Chris Lattnerf0f40682006-01-04 02:15:02 +0000246/// DoVectorOp - Given two packed constants and a function pointer, apply the
247/// function pointer to each element pair, producing a new ConstantPacked
248/// constant.
249static Constant *EvalVectorOp(const ConstantPacked *V1,
250 const ConstantPacked *V2,
251 Constant *(*FP)(Constant*, Constant*)) {
252 std::vector<Constant*> Res;
253 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
254 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
255 const_cast<Constant*>(V2->getOperand(i))));
256 return ConstantPacked::get(Res);
257}
258
Chris Lattner1171d952006-01-04 02:03:29 +0000259/// PackedTypeRules provides a concrete base class of ConstRules for
260/// ConstantPacked operands.
261///
Chris Lattner6a871e12006-06-21 18:13:36 +0000262namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000263struct VISIBILITY_HIDDEN ConstantPackedRules
Chris Lattner1171d952006-01-04 02:03:29 +0000264 : public TemplateRules<ConstantPacked, ConstantPackedRules> {
Chris Lattnerf0f40682006-01-04 02:15:02 +0000265
266 static Constant *Add(const ConstantPacked *V1, const ConstantPacked *V2) {
267 return EvalVectorOp(V1, V2, ConstantExpr::getAdd);
268 }
269 static Constant *Sub(const ConstantPacked *V1, const ConstantPacked *V2) {
270 return EvalVectorOp(V1, V2, ConstantExpr::getSub);
271 }
272 static Constant *Mul(const ConstantPacked *V1, const ConstantPacked *V2) {
273 return EvalVectorOp(V1, V2, ConstantExpr::getMul);
274 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000275 static Constant *UDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
276 return EvalVectorOp(V1, V2, ConstantExpr::getUDiv);
277 }
278 static Constant *SDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
279 return EvalVectorOp(V1, V2, ConstantExpr::getSDiv);
280 }
281 static Constant *FDiv(const ConstantPacked *V1, const ConstantPacked *V2) {
282 return EvalVectorOp(V1, V2, ConstantExpr::getFDiv);
Chris Lattnerf0f40682006-01-04 02:15:02 +0000283 }
Reid Spencer7eb55b32006-11-02 01:53:59 +0000284 static Constant *URem(const ConstantPacked *V1, const ConstantPacked *V2) {
285 return EvalVectorOp(V1, V2, ConstantExpr::getURem);
286 }
287 static Constant *SRem(const ConstantPacked *V1, const ConstantPacked *V2) {
288 return EvalVectorOp(V1, V2, ConstantExpr::getSRem);
289 }
290 static Constant *FRem(const ConstantPacked *V1, const ConstantPacked *V2) {
291 return EvalVectorOp(V1, V2, ConstantExpr::getFRem);
Chris Lattnerf0f40682006-01-04 02:15:02 +0000292 }
293 static Constant *And(const ConstantPacked *V1, const ConstantPacked *V2) {
294 return EvalVectorOp(V1, V2, ConstantExpr::getAnd);
295 }
296 static Constant *Or (const ConstantPacked *V1, const ConstantPacked *V2) {
297 return EvalVectorOp(V1, V2, ConstantExpr::getOr);
298 }
299 static Constant *Xor(const ConstantPacked *V1, const ConstantPacked *V2) {
300 return EvalVectorOp(V1, V2, ConstantExpr::getXor);
301 }
Chris Lattnerf0f40682006-01-04 02:15:02 +0000302 static Constant *LessThan(const ConstantPacked *V1, const ConstantPacked *V2){
303 return 0;
304 }
305 static Constant *EqualTo(const ConstantPacked *V1, const ConstantPacked *V2) {
Chris Lattner6b52be62006-01-04 02:20:54 +0000306 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i) {
307 Constant *C =
308 ConstantExpr::getSetEQ(const_cast<Constant*>(V1->getOperand(i)),
309 const_cast<Constant*>(V2->getOperand(i)));
310 if (ConstantBool *CB = dyn_cast<ConstantBool>(C))
311 return CB;
312 }
313 // Otherwise, could not decide from any element pairs.
Chris Lattnerf0f40682006-01-04 02:15:02 +0000314 return 0;
315 }
Chris Lattner1171d952006-01-04 02:03:29 +0000316};
Chris Lattner6a871e12006-06-21 18:13:36 +0000317} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000318
319
320//===----------------------------------------------------------------------===//
321// GeneralPackedRules Class
322//===----------------------------------------------------------------------===//
323
324/// GeneralPackedRules provides a concrete base class of ConstRules for
325/// PackedType operands, where both operands are not ConstantPacked. The usual
326/// cause for this is that one operand is a ConstantAggregateZero.
327///
Chris Lattner6a871e12006-06-21 18:13:36 +0000328namespace {
Chris Lattner02157b02006-06-28 21:38:54 +0000329struct VISIBILITY_HIDDEN GeneralPackedRules
330 : public TemplateRules<Constant, GeneralPackedRules> {
Chris Lattner1171d952006-01-04 02:03:29 +0000331};
Chris Lattner6a871e12006-06-21 18:13:36 +0000332} // end anonymous namespace
Chris Lattner1171d952006-01-04 02:03:29 +0000333
Chris Lattner977f0042001-11-01 05:55:13 +0000334
335//===----------------------------------------------------------------------===//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000336// DirectIntRules Class
Chris Lattner2f7c9632001-06-06 20:29:01 +0000337//===----------------------------------------------------------------------===//
338//
Reid Spencere0fc4df2006-10-20 07:07:24 +0000339// DirectIntRules provides implementations of functions that are valid on
340// integer types, but not all types in general.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341//
Chris Lattner6a871e12006-06-21 18:13:36 +0000342namespace {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000343template <class BuiltinType, Type **Ty>
344struct VISIBILITY_HIDDEN DirectIntRules
345 : public TemplateRules<ConstantInt, DirectIntRules<BuiltinType, Ty> > {
346
347 static Constant *Add(const ConstantInt *V1, const ConstantInt *V2) {
348 BuiltinType R = (BuiltinType)V1->getZExtValue() +
349 (BuiltinType)V2->getZExtValue();
350 return ConstantInt::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000351 }
352
Reid Spencere0fc4df2006-10-20 07:07:24 +0000353 static Constant *Sub(const ConstantInt *V1, const ConstantInt *V2) {
354 BuiltinType R = (BuiltinType)V1->getZExtValue() -
355 (BuiltinType)V2->getZExtValue();
356 return ConstantInt::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357 }
358
Reid Spencere0fc4df2006-10-20 07:07:24 +0000359 static Constant *Mul(const ConstantInt *V1, const ConstantInt *V2) {
360 BuiltinType R = (BuiltinType)V1->getZExtValue() *
361 (BuiltinType)V2->getZExtValue();
362 return ConstantInt::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000363 }
364
Reid Spencere0fc4df2006-10-20 07:07:24 +0000365 static Constant *LessThan(const ConstantInt *V1, const ConstantInt *V2) {
366 bool R = (BuiltinType)V1->getZExtValue() < (BuiltinType)V2->getZExtValue();
Chris Lattnere87f65e2002-07-30 16:24:28 +0000367 return ConstantBool::get(R);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000368 }
Chris Lattner55406842001-07-21 19:10:49 +0000369
Reid Spencere0fc4df2006-10-20 07:07:24 +0000370 static Constant *EqualTo(const ConstantInt *V1, const ConstantInt *V2) {
371 bool R = (BuiltinType)V1->getZExtValue() == (BuiltinType)V2->getZExtValue();
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000372 return ConstantBool::get(R);
373 }
374
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000375 static Constant *UDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000376 if (V2->isNullValue()) // X / 0
Chris Lattner268916262003-05-12 15:26:25 +0000377 return 0;
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000378 BuiltinType R = (BuiltinType)(V1->getZExtValue() / V2->getZExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000379 return ConstantInt::get(*Ty, R);
Chris Lattner268916262003-05-12 15:26:25 +0000380 }
381
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000382 static Constant *SDiv(const ConstantInt *V1, const ConstantInt *V2) {
Reid Spencer7eb55b32006-11-02 01:53:59 +0000383 if (V2->isNullValue()) // X / 0
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000384 return 0;
385 if (V2->isAllOnesValue() && // MIN_INT / -1
386 (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
387 return 0;
Reid Spencer7eb55b32006-11-02 01:53:59 +0000388 BuiltinType R = (BuiltinType)(V1->getSExtValue() / V2->getSExtValue());
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000389 return ConstantInt::get(*Ty, R);
390 }
391
Reid Spencer7eb55b32006-11-02 01:53:59 +0000392 static Constant *URem(const ConstantInt *V1,
393 const ConstantInt *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000394 if (V2->isNullValue()) return 0; // X / 0
Reid Spencer7eb55b32006-11-02 01:53:59 +0000395 BuiltinType R = (BuiltinType)(V1->getZExtValue() % V2->getZExtValue());
396 return ConstantInt::get(*Ty, R);
397 }
398
399 static Constant *SRem(const ConstantInt *V1,
400 const ConstantInt *V2) {
401 if (V2->isNullValue()) return 0; // X % 0
402 if (V2->isAllOnesValue() && // MIN_INT % -1
403 (BuiltinType)V1->getSExtValue() == -(BuiltinType)V1->getSExtValue())
Chris Lattner268916262003-05-12 15:26:25 +0000404 return 0;
Reid Spencer7eb55b32006-11-02 01:53:59 +0000405 BuiltinType R = (BuiltinType)(V1->getSExtValue() % V2->getSExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000406 return ConstantInt::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000407 }
Chris Lattner6670d862002-05-06 03:00:54 +0000408
Reid Spencere0fc4df2006-10-20 07:07:24 +0000409 static Constant *And(const ConstantInt *V1, const ConstantInt *V2) {
410 BuiltinType R =
411 (BuiltinType)V1->getZExtValue() & (BuiltinType)V2->getZExtValue();
412 return ConstantInt::get(*Ty, R);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000413 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000414 static Constant *Or(const ConstantInt *V1, const ConstantInt *V2) {
415 BuiltinType R =
416 (BuiltinType)V1->getZExtValue() | (BuiltinType)V2->getZExtValue();
417 return ConstantInt::get(*Ty, R);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000418 }
Reid Spencere0fc4df2006-10-20 07:07:24 +0000419 static Constant *Xor(const ConstantInt *V1, const ConstantInt *V2) {
420 BuiltinType R =
421 (BuiltinType)V1->getZExtValue() ^ (BuiltinType)V2->getZExtValue();
422 return ConstantInt::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000423 }
424
Reid Spencere0fc4df2006-10-20 07:07:24 +0000425 static Constant *Shl(const ConstantInt *V1, const ConstantInt *V2) {
426 BuiltinType R =
427 (BuiltinType)V1->getZExtValue() << (BuiltinType)V2->getZExtValue();
428 return ConstantInt::get(*Ty, R);
Chris Lattnere87f65e2002-07-30 16:24:28 +0000429 }
430
Reid Spencerfdff9382006-11-08 06:47:33 +0000431 static Constant *LShr(const ConstantInt *V1, const ConstantInt *V2) {
432 BuiltinType R = BuiltinType(V1->getZExtValue() >> V2->getZExtValue());
433 return ConstantInt::get(*Ty, R);
434 }
435
436 static Constant *AShr(const ConstantInt *V1, const ConstantInt *V2) {
437 BuiltinType R = BuiltinType(V1->getSExtValue() >> V2->getZExtValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000438 return ConstantInt::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000439 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000440};
Chris Lattner6a871e12006-06-21 18:13:36 +0000441} // end anonymous namespace
Chris Lattner0a144ad2002-05-03 21:41:07 +0000442
443
444//===----------------------------------------------------------------------===//
445// DirectFPRules Class
446//===----------------------------------------------------------------------===//
447//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000448/// DirectFPRules provides implementations of functions that are valid on
449/// floating point types, but not all types in general.
450///
Chris Lattner6a871e12006-06-21 18:13:36 +0000451namespace {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000452template <class BuiltinType, Type **Ty>
Chris Lattner02157b02006-06-28 21:38:54 +0000453struct VISIBILITY_HIDDEN DirectFPRules
Reid Spencere0fc4df2006-10-20 07:07:24 +0000454 : public TemplateRules<ConstantFP, DirectFPRules<BuiltinType, Ty> > {
455
456 static Constant *Add(const ConstantFP *V1, const ConstantFP *V2) {
457 BuiltinType R = (BuiltinType)V1->getValue() +
458 (BuiltinType)V2->getValue();
459 return ConstantFP::get(*Ty, R);
460 }
461
462 static Constant *Sub(const ConstantFP *V1, const ConstantFP *V2) {
463 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
464 return ConstantFP::get(*Ty, R);
465 }
466
467 static Constant *Mul(const ConstantFP *V1, const ConstantFP *V2) {
468 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
469 return ConstantFP::get(*Ty, R);
470 }
471
472 static Constant *LessThan(const ConstantFP *V1, const ConstantFP *V2) {
473 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
474 return ConstantBool::get(R);
475 }
476
477 static Constant *EqualTo(const ConstantFP *V1, const ConstantFP *V2) {
478 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
479 return ConstantBool::get(R);
480 }
481
Reid Spencer7eb55b32006-11-02 01:53:59 +0000482 static Constant *FRem(const ConstantFP *V1, const ConstantFP *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000483 if (V2->isNullValue()) return 0;
484 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
485 (BuiltinType)V2->getValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000486 return ConstantFP::get(*Ty, Result);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000487 }
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000488 static Constant *FDiv(const ConstantFP *V1, const ConstantFP *V2) {
Jeff Cohen4e3aede2005-05-03 03:13:01 +0000489 BuiltinType inf = std::numeric_limits<BuiltinType>::infinity();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000490 if (V2->isExactlyValue(0.0)) return ConstantFP::get(*Ty, inf);
491 if (V2->isExactlyValue(-0.0)) return ConstantFP::get(*Ty, -inf);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000492 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
Reid Spencere0fc4df2006-10-20 07:07:24 +0000493 return ConstantFP::get(*Ty, R);
Andrew Lenharthc73e6332005-05-02 21:25:47 +0000494 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000495};
Chris Lattner6a871e12006-06-21 18:13:36 +0000496} // end anonymous namespace
Chris Lattner62af86e2002-05-03 20:09:52 +0000497
Chris Lattner057083f2006-10-13 17:22:21 +0000498static ManagedStatic<EmptyRules> EmptyR;
499static ManagedStatic<BoolRules> BoolR;
500static ManagedStatic<NullPointerRules> NullPointerR;
501static ManagedStatic<ConstantPackedRules> ConstantPackedR;
502static ManagedStatic<GeneralPackedRules> GeneralPackedR;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000503static ManagedStatic<DirectIntRules<signed char , &Type::SByteTy> > SByteR;
504static ManagedStatic<DirectIntRules<unsigned char , &Type::UByteTy> > UByteR;
505static ManagedStatic<DirectIntRules<signed short , &Type::ShortTy> > ShortR;
506static ManagedStatic<DirectIntRules<unsigned short, &Type::UShortTy> > UShortR;
507static ManagedStatic<DirectIntRules<signed int , &Type::IntTy> > IntR;
508static ManagedStatic<DirectIntRules<unsigned int , &Type::UIntTy> > UIntR;
509static ManagedStatic<DirectIntRules<int64_t , &Type::LongTy> > LongR;
510static ManagedStatic<DirectIntRules<uint64_t , &Type::ULongTy> > ULongR;
511static ManagedStatic<DirectFPRules <float , &Type::FloatTy> > FloatR;
512static ManagedStatic<DirectFPRules <double , &Type::DoubleTy> > DoubleR;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000513
514/// ConstRules::get - This method returns the constant rules implementation that
515/// implements the semantics of the two specified constants.
Chris Lattnerf8348c32004-01-12 20:41:05 +0000516ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000517 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000518 isa<GlobalValue>(V1) || isa<GlobalValue>(V2) ||
519 isa<UndefValue>(V1) || isa<UndefValue>(V2))
Chris Lattner057083f2006-10-13 17:22:21 +0000520 return *EmptyR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000521
Chris Lattner6b727592004-06-17 18:19:28 +0000522 switch (V1->getType()->getTypeID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000523 default: assert(0 && "Unknown value type for constant folding!");
Chris Lattner057083f2006-10-13 17:22:21 +0000524 case Type::BoolTyID: return *BoolR;
525 case Type::PointerTyID: return *NullPointerR;
526 case Type::SByteTyID: return *SByteR;
527 case Type::UByteTyID: return *UByteR;
528 case Type::ShortTyID: return *ShortR;
529 case Type::UShortTyID: return *UShortR;
530 case Type::IntTyID: return *IntR;
531 case Type::UIntTyID: return *UIntR;
532 case Type::LongTyID: return *LongR;
533 case Type::ULongTyID: return *ULongR;
534 case Type::FloatTyID: return *FloatR;
535 case Type::DoubleTyID: return *DoubleR;
Chris Lattner1171d952006-01-04 02:03:29 +0000536 case Type::PackedTyID:
537 if (isa<ConstantPacked>(V1) && isa<ConstantPacked>(V2))
Chris Lattner057083f2006-10-13 17:22:21 +0000538 return *ConstantPackedR;
539 return *GeneralPackedR; // Constant folding rules for ConstantAggregateZero.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000540 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000541}
Chris Lattner1dd054c2004-01-12 22:07:24 +0000542
543
544//===----------------------------------------------------------------------===//
545// ConstantFold*Instruction Implementations
546//===----------------------------------------------------------------------===//
Chris Lattner1dd054c2004-01-12 22:07:24 +0000547
Chris Lattner6b3f4752006-04-02 01:38:28 +0000548/// CastConstantPacked - Convert the specified ConstantPacked node to the
549/// specified packed type. At this point, we know that the elements of the
550/// input packed constant are all simple integer or FP values.
551static Constant *CastConstantPacked(ConstantPacked *CP,
552 const PackedType *DstTy) {
553 unsigned SrcNumElts = CP->getType()->getNumElements();
554 unsigned DstNumElts = DstTy->getNumElements();
555 const Type *SrcEltTy = CP->getType()->getElementType();
556 const Type *DstEltTy = DstTy->getElementType();
557
558 // If both vectors have the same number of elements (thus, the elements
559 // are the same size), perform the conversion now.
560 if (SrcNumElts == DstNumElts) {
561 std::vector<Constant*> Result;
562
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000563 // If the src and dest elements are both integers, or both floats, we can
564 // just BitCast each element because the elements are the same size.
565 if ((SrcEltTy->isIntegral() && DstEltTy->isIntegral()) ||
566 (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000567 for (unsigned i = 0; i != SrcNumElts; ++i)
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000568 Result.push_back(
Reid Spencerbb65ebf2006-12-12 23:36:14 +0000569 ConstantExpr::getBitCast(CP->getOperand(i), DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000570 return ConstantPacked::get(Result);
571 }
572
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000573 // If this is an int-to-fp cast ..
Chris Lattner6b3f4752006-04-02 01:38:28 +0000574 if (SrcEltTy->isIntegral()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000575 // Ensure that it is int-to-fp cast
Chris Lattner6b3f4752006-04-02 01:38:28 +0000576 assert(DstEltTy->isFloatingPoint());
577 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
578 for (unsigned i = 0; i != SrcNumElts; ++i) {
579 double V =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000580 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +0000581 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
582 }
583 return ConstantPacked::get(Result);
584 }
585 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
586 for (unsigned i = 0; i != SrcNumElts; ++i) {
587 float V =
Reid Spencere0fc4df2006-10-20 07:07:24 +0000588 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +0000589 Result.push_back(ConstantFP::get(Type::FloatTy, V));
590 }
591 return ConstantPacked::get(Result);
592 }
593
594 // Otherwise, this is an fp-to-int cast.
595 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
596
597 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
598 for (unsigned i = 0; i != SrcNumElts; ++i) {
599 uint64_t V =
600 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencere0fc4df2006-10-20 07:07:24 +0000601 Constant *C = ConstantInt::get(Type::ULongTy, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000602 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000603 }
604 return ConstantPacked::get(Result);
605 }
606
607 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
608 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000609 uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
610 Constant *C = ConstantInt::get(Type::UIntTy, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000611 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000612 }
613 return ConstantPacked::get(Result);
614 }
615
616 // Otherwise, this is a cast that changes element count and size. Handle
617 // casts which shrink the elements here.
618
619 // FIXME: We need to know endianness to do this!
620
621 return 0;
622}
623
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000624/// This function determines which opcode to use to fold two constant cast
625/// expressions together. It uses CastInst::isEliminableCastPair to determine
626/// the opcode. Consequently its just a wrapper around that function.
627/// @Determine if it is valid to fold a cast of a cast
628static unsigned
629foldConstantCastPair(
630 unsigned opc, ///< opcode of the second cast constant expression
631 const ConstantExpr*Op, ///< the first cast constant expression
632 const Type *DstTy ///< desintation type of the first cast
633) {
634 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
635 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
636 assert(CastInst::isCast(opc) && "Invalid cast opcode");
637
638 // The the types and opcodes for the two Cast constant expressions
639 const Type *SrcTy = Op->getOperand(0)->getType();
640 const Type *MidTy = Op->getType();
641 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
642 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000643
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000644 // Let CastInst::isEliminableCastPair do the heavy lifting.
645 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
646 Type::ULongTy);
647}
648
649Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattner1dd054c2004-01-12 22:07:24 +0000650 const Type *DestTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000651 const Type *SrcTy = V->getType();
Chris Lattner1dd054c2004-01-12 22:07:24 +0000652
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000653 if (isa<UndefValue>(V))
654 return UndefValue::get(DestTy);
655
656 // If the cast operand is a constant expression, there's a few things we can
657 // do to try to simplify it.
658 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
659 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000660 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000661 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
662 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000663 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
664 // If all of the indexes in the GEP are null values, there is no pointer
665 // adjustment going on. We might as well cast the source pointer.
666 bool isAllNull = true;
667 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
668 if (!CE->getOperand(i)->isNullValue()) {
669 isAllNull = false;
670 break;
671 }
672 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000673 // This is casting one pointer type to another, always BitCast
Reid Spencer27720a92006-12-05 03:30:09 +0000674 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000675 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000676 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000677
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000678 // We actually have to do a cast now. Perform the cast according to the
679 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000680 switch (opc) {
681 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000682 case Instruction::FPExt:
Reid Spencer8dabca42006-12-19 07:41:40 +0000683 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
684 return ConstantFP::get(DestTy, FPC->getValue());
685 return 0; // Can't fold.
686 case Instruction::FPToUI:
687 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
688 return ConstantIntegral::get(DestTy,(uint64_t) FPC->getValue());
689 return 0; // Can't fold.
690 case Instruction::FPToSI:
691 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
692 return ConstantIntegral::get(DestTy,(int64_t) FPC->getValue());
693 return 0; // Can't fold.
694 case Instruction::IntToPtr: //always treated as unsigned
695 if (V->isNullValue()) // Is it an integral null value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000696 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000697 return 0; // Other pointer types cannot be casted
698 case Instruction::PtrToInt: // always treated as unsigned
699 if (V->isNullValue()) // is it a null pointer value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000700 return ConstantIntegral::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000701 return 0; // Other pointer types cannot be casted
702 case Instruction::UIToFP:
703 if (const ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
704 return ConstantFP::get(DestTy, double(CI->getZExtValue()));
705 return 0;
706 case Instruction::SIToFP:
707 if (const ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
708 return ConstantFP::get(DestTy, double(CI->getSExtValue()));
709 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000710 case Instruction::ZExt:
Reid Spencer8dabca42006-12-19 07:41:40 +0000711 if (const ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
712 return ConstantInt::get(DestTy, CI->getZExtValue());
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000713 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000714 case Instruction::SExt:
Reid Spencer8dabca42006-12-19 07:41:40 +0000715 if (const ConstantIntegral *CI = dyn_cast<ConstantIntegral>(V))
716 return ConstantInt::get(DestTy, CI->getSExtValue());
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000717 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000718 case Instruction::Trunc:
Reid Spencer8dabca42006-12-19 07:41:40 +0000719 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) // Can't trunc a bool
Chris Lattner710ebaf2006-12-01 19:22:41 +0000720 return ConstantIntegral::get(DestTy, CI->getZExtValue());
721 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000722 case Instruction::BitCast:
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000723 if (SrcTy == DestTy)
724 return (Constant*)V; // no-op cast
Chris Lattner4d1da162006-12-11 18:30:27 +0000725
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000726 // Check to see if we are casting a pointer to an aggregate to a pointer to
727 // the first element. If so, return the appropriate GEP instruction.
728 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
729 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
730 std::vector<Value*> IdxList;
731 IdxList.push_back(Constant::getNullValue(Type::IntTy));
732 const Type *ElTy = PTy->getElementType();
733 while (ElTy != DPTy->getElementType()) {
734 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
735 if (STy->getNumElements() == 0) break;
736 ElTy = STy->getElementType(0);
737 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
738 } else if (const SequentialType *STy =
739 dyn_cast<SequentialType>(ElTy)) {
740 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
741 ElTy = STy->getElementType();
742 IdxList.push_back(IdxList[0]);
743 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000744 break;
745 }
746 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000747
748 if (ElTy == DPTy->getElementType())
749 return ConstantExpr::getGetElementPtr(
750 const_cast<Constant*>(V),IdxList);
751 }
752
753 // Handle casts from one packed constant to another. We know that the src
754 // and dest type have the same size (otherwise its an illegal cast).
755 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
756 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
757 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
758 "Not cast between same sized vectors!");
759 // First, check for null and undef
760 if (isa<ConstantAggregateZero>(V))
761 return Constant::getNullValue(DestTy);
762 if (isa<UndefValue>(V))
763 return UndefValue::get(DestTy);
764
765 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
766 // This is a cast from a ConstantPacked of one type to a
767 // ConstantPacked of another type. Check to see if all elements of
768 // the input are simple.
769 bool AllSimpleConstants = true;
770 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
771 if (!isa<ConstantInt>(CP->getOperand(i)) &&
772 !isa<ConstantFP>(CP->getOperand(i))) {
773 AllSimpleConstants = false;
774 break;
775 }
776 }
777
778 // If all of the elements are simple constants, we can fold this.
779 if (AllSimpleConstants)
780 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
781 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000782 }
783 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000784
Chris Lattner4d1da162006-12-11 18:30:27 +0000785 // Finally, implement bitcast folding now. The code below doesn't handle
786 // bitcast right.
787 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
788 return ConstantPointerNull::get(cast<PointerType>(DestTy));
789
790 // Handle integral constant input.
791 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
792 // Integral -> Integral, must be changing sign.
793 if (DestTy->isIntegral())
794 return ConstantInt::get(DestTy, CI->getZExtValue());
795
796 if (DestTy->isFloatingPoint()) {
797 if (DestTy == Type::FloatTy)
798 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
799 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
800 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
801 }
802 // Otherwise, can't fold this (packed?)
803 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000804 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000805
806 // Handle ConstantFP input.
807 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
808 // FP -> Integral.
809 if (DestTy->isIntegral()) {
Reid Spencer3db7d372006-12-11 21:27:28 +0000810 if (DestTy == Type::IntTy || DestTy == Type::UIntTy)
Chris Lattner4d1da162006-12-11 18:30:27 +0000811 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
Reid Spencer3db7d372006-12-11 21:27:28 +0000812 assert((DestTy == Type::LongTy || DestTy == Type::ULongTy)
813 && "Incorrect integer type for bitcast!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000814 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
815 }
816 }
817 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000818 default:
819 assert(!"Invalid CE CastInst opcode");
820 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000821 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000822
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000823 assert(0 && "Failed to cast constant expression");
824 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000825}
826
Chris Lattner6ea4b522004-03-12 05:53:32 +0000827Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
828 const Constant *V1,
829 const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000830 if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
831 return const_cast<Constant*>(CB->getValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000832
833 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
834 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
835 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000836 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000837 return 0;
838}
839
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000840Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
841 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000842 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
843 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000844 if (Val->isNullValue()) // ee(zero, x) -> zero
845 return Constant::getNullValue(
846 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000847
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000848 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000849 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
850 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000851 } else if (isa<UndefValue>(Idx)) {
852 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
853 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000854 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000855 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000856 return 0;
857}
858
Robert Bocchinoca27f032006-01-17 20:07:22 +0000859Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
860 const Constant *Elt,
861 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000862 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000863 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000864 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000865 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000866 // Insertion of scalar constant into packed undef
867 // Optimize away insertion of undef
868 if (isa<UndefValue>(Elt))
869 return const_cast<Constant*>(Val);
870 // Otherwise break the aggregate undef into multiple undefs and do
871 // the insertion
872 unsigned numOps =
873 cast<PackedType>(Val->getType())->getNumElements();
874 std::vector<Constant*> Ops;
875 Ops.reserve(numOps);
876 for (unsigned i = 0; i < numOps; ++i) {
877 const Constant *Op =
878 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
879 Ops.push_back(const_cast<Constant*>(Op));
880 }
881 return ConstantPacked::get(Ops);
882 }
Reid Spencer3054b142006-11-02 08:18:15 +0000883 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000884 // Insertion of scalar constant into packed aggregate zero
885 // Optimize away insertion of zero
886 if (Elt->isNullValue())
887 return const_cast<Constant*>(Val);
888 // Otherwise break the aggregate zero into multiple zeros and do
889 // the insertion
890 unsigned numOps =
891 cast<PackedType>(Val->getType())->getNumElements();
892 std::vector<Constant*> Ops;
893 Ops.reserve(numOps);
894 for (unsigned i = 0; i < numOps; ++i) {
895 const Constant *Op =
896 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
897 Ops.push_back(const_cast<Constant*>(Op));
898 }
899 return ConstantPacked::get(Ops);
900 }
901 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
902 // Insertion of scalar constant into packed constant
903 std::vector<Constant*> Ops;
904 Ops.reserve(CVal->getNumOperands());
905 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
906 const Constant *Op =
907 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
908 Ops.push_back(const_cast<Constant*>(Op));
909 }
910 return ConstantPacked::get(Ops);
911 }
912 return 0;
913}
914
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000915Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
916 const Constant *V2,
917 const Constant *Mask) {
918 // TODO:
919 return 0;
920}
921
922
Chris Lattner60c47262005-01-28 19:09:51 +0000923/// isZeroSizedType - This type is zero sized if its an array or structure of
924/// zero sized types. The only leaf zero sized type is an empty structure.
925static bool isMaybeZeroSizedType(const Type *Ty) {
926 if (isa<OpaqueType>(Ty)) return true; // Can't say.
927 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
928
929 // If all of elements have zero size, this does too.
930 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000931 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000932 return true;
933
934 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
935 return isMaybeZeroSizedType(ATy->getElementType());
936 }
937 return false;
938}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000939
Chris Lattner061da2f2004-01-13 05:51:55 +0000940/// IdxCompare - Compare the two constants as though they were getelementptr
941/// indices. This allows coersion of the types to be the same thing.
942///
943/// If the two constants are the "same" (after coersion), return 0. If the
944/// first is less than the second, return -1, if the second is less than the
945/// first, return 1. If the constants are not integral, return -2.
946///
Chris Lattner60c47262005-01-28 19:09:51 +0000947static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000948 if (C1 == C2) return 0;
949
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000950 // Ok, we found a different index. Are either of the operands ConstantExprs?
951 // If so, we can't do anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000952 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
953 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000954
Chris Lattner69193f92004-04-05 01:30:19 +0000955 // Ok, we have two differing integer indices. Sign extend them to be the same
956 // type. Long is always big enough, so we use it.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000957 if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
Reid Spencerbb65ebf2006-12-12 23:36:14 +0000958 C1 = ConstantExpr::getSExt(C1, Type::LongTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000959 else
960 C1 = ConstantExpr::getBitCast(C1, Type::LongTy);
961 if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
Reid Spencerbb65ebf2006-12-12 23:36:14 +0000962 C2 = ConstantExpr::getSExt(C2, Type::LongTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000963 else
964 C2 = ConstantExpr::getBitCast(C2, Type::LongTy);
965
Chris Lattner061da2f2004-01-13 05:51:55 +0000966 if (C1 == C2) return 0; // Are they just differing types?
967
Chris Lattner60c47262005-01-28 19:09:51 +0000968 // If the type being indexed over is really just a zero sized type, there is
969 // no pointer difference being made here.
970 if (isMaybeZeroSizedType(ElTy))
971 return -2; // dunno.
972
Chris Lattner061da2f2004-01-13 05:51:55 +0000973 // If they are really different, now that they are the same type, then we
974 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000975 if (cast<ConstantInt>(C1)->getSExtValue() <
976 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000977 return -1;
978 else
979 return 1;
980}
981
982/// evaluateRelation - This function determines if there is anything we can
983/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000984/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000985/// and GlobalValues. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +0000986/// particular relation to each other, we should return the corresponding SetCC
987/// code, otherwise return Instruction::BinaryOpsEnd.
988///
989/// To simplify this code we canonicalize the relation so that the first
990/// operand is always the most "complex" of the two. We consider simple
991/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000992/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000993///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000994static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000995 assert(V1->getType() == V2->getType() &&
996 "Cannot compare different types of values!");
997 if (V1 == V2) return Instruction::SetEQ;
998
Reid Spenceraccd7c72004-07-17 23:47:01 +0000999 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001000 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1001 // We distilled this down to a simple case, use the standard constant
1002 // folder.
1003 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001004 if (R && R->getValue()) return Instruction::SetEQ;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001005 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001006 if (R && R->getValue()) return Instruction::SetLT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001007 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001008 if (R && R->getValue()) return Instruction::SetGT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001009
1010 // If we couldn't figure it out, bail.
1011 return Instruction::BinaryOpsEnd;
1012 }
1013
Chris Lattner061da2f2004-01-13 05:51:55 +00001014 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +00001015 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1016 if (SwappedRelation != Instruction::BinaryOpsEnd)
1017 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001018
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001019 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001020 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001021 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1022 if (SwappedRelation != Instruction::BinaryOpsEnd)
1023 return SetCondInst::getSwappedCondition(SwappedRelation);
1024 else
1025 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +00001026 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001027
Reid Spenceraccd7c72004-07-17 23:47:01 +00001028 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001029 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001030 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001031 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1032 return Instruction::SetNE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001033 } else {
Reid Spencer876f7222006-12-06 00:25:09 +00001034 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +00001035 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +00001036 if (!CPR1->hasExternalWeakLinkage())
1037 return Instruction::SetNE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001038 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001039 } else {
1040 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1041 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001042 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +00001043 Constant *CE1Op0 = CE1->getOperand(0);
1044
1045 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001046 case Instruction::Trunc:
1047 case Instruction::FPTrunc:
1048 case Instruction::FPExt:
1049 case Instruction::FPToUI:
1050 case Instruction::FPToSI:
1051 break; // We don't do anything with floating point.
1052 case Instruction::ZExt:
1053 case Instruction::SExt:
1054 case Instruction::UIToFP:
1055 case Instruction::SIToFP:
1056 case Instruction::PtrToInt:
1057 case Instruction::IntToPtr:
1058 case Instruction::BitCast:
Chris Lattner061da2f2004-01-13 05:51:55 +00001059 // If the cast is not actually changing bits, and the second operand is a
1060 // null pointer, do the comparison with the pre-casted value.
1061 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001062 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +00001063 return evaluateRelation(CE1Op0,
1064 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001065
1066 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1067 // from the same type as the src of the LHS, evaluate the inputs. This is
1068 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1069 // which happens a lot in compilers with tagged integers.
1070 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001071 if (isa<PointerType>(CE1->getType()) && CE2->isCast() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001072 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1073 CE1->getOperand(0)->getType()->isIntegral()) {
1074 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1075 }
Chris Lattner192e3262004-04-11 01:29:30 +00001076 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001077
1078 case Instruction::GetElementPtr:
1079 // Ok, since this is a getelementptr, we know that the constant has a
1080 // pointer type. Check the various cases.
1081 if (isa<ConstantPointerNull>(V2)) {
1082 // If we are comparing a GEP to a null pointer, check to see if the base
1083 // of the GEP equals the null pointer.
Reid Spencer876f7222006-12-06 00:25:09 +00001084 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1085 if (GV->hasExternalWeakLinkage())
1086 // Weak linkage GVals could be zero or not. We're comparing that
1087 // to null pointer so its greater-or-equal
1088 return Instruction::SetGE;
1089 else
1090 // If its not weak linkage, the GVal must have a non-zero address
1091 // so the result is greater-than
1092 return Instruction::SetGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001093 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1094 // If we are indexing from a null pointer, check to see if we have any
1095 // non-zero indices.
1096 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1097 if (!CE1->getOperand(i)->isNullValue())
1098 // Offsetting from null, must not be equal.
1099 return Instruction::SetGT;
1100 // Only zero indexes from null, must still be zero.
1101 return Instruction::SetEQ;
1102 }
1103 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001104 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001105 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001106 if (CPR2->hasExternalWeakLinkage())
1107 // Weak linkage GVals could be zero or not. We're comparing it to
1108 // a null pointer, so its less-or-equal
1109 return Instruction::SetLE;
1110 else
1111 // If its not weak linkage, the GVal must have a non-zero address
1112 // so the result is less-than
1113 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001114 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001115 if (CPR1 == CPR2) {
1116 // If this is a getelementptr of the same global, then it must be
1117 // different. Because the types must match, the getelementptr could
1118 // only have at most one index, and because we fold getelementptr's
1119 // with a single zero index, it must be nonzero.
1120 assert(CE1->getNumOperands() == 2 &&
1121 !CE1->getOperand(1)->isNullValue() &&
1122 "Suprising getelementptr!");
1123 return Instruction::SetGT;
1124 } else {
1125 // If they are different globals, we don't know what the value is,
1126 // but they can't be equal.
1127 return Instruction::SetNE;
1128 }
1129 }
1130 } else {
1131 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1132 const Constant *CE2Op0 = CE2->getOperand(0);
1133
1134 // There are MANY other foldings that we could perform here. They will
1135 // probably be added on demand, as they seem needed.
1136 switch (CE2->getOpcode()) {
1137 default: break;
1138 case Instruction::GetElementPtr:
1139 // By far the most common case to handle is when the base pointers are
1140 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001141 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001142 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1143 return Instruction::SetNE;
1144 // Ok, we know that both getelementptr instructions are based on the
1145 // same global. From this, we can precisely determine the relative
1146 // ordering of the resultant pointers.
1147 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001148
Chris Lattner061da2f2004-01-13 05:51:55 +00001149 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001150 gep_type_iterator GTI = gep_type_begin(CE1);
1151 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1152 ++i, ++GTI)
1153 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1154 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001155 case -1: return Instruction::SetLT;
1156 case 1: return Instruction::SetGT;
1157 case -2: return Instruction::BinaryOpsEnd;
1158 }
1159
1160 // Ok, we ran out of things they have in common. If any leftovers
1161 // are non-zero then we have a difference, otherwise we are equal.
1162 for (; i < CE1->getNumOperands(); ++i)
1163 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001164 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1165 return Instruction::SetGT;
1166 else
1167 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001168
Chris Lattner061da2f2004-01-13 05:51:55 +00001169 for (; i < CE2->getNumOperands(); ++i)
1170 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001171 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1172 return Instruction::SetLT;
1173 else
1174 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001175 return Instruction::SetEQ;
1176 }
1177 }
1178 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001179
Chris Lattner061da2f2004-01-13 05:51:55 +00001180 default:
1181 break;
1182 }
1183 }
1184
1185 return Instruction::BinaryOpsEnd;
1186}
1187
Chris Lattner1dd054c2004-01-12 22:07:24 +00001188Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1189 const Constant *V1,
1190 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001191 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001192 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001193 default: break;
1194 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1195 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1196 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001197 case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break;
1198 case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break;
1199 case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001200 case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break;
1201 case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break;
1202 case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001203 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1204 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1205 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1206 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
Reid Spencerfdff9382006-11-08 06:47:33 +00001207 case Instruction::LShr: C = ConstRules::get(V1, V2).lshr(V1, V2); break;
1208 case Instruction::AShr: C = ConstRules::get(V1, V2).ashr(V1, V2); break;
Reid Spencer6f05d732006-12-01 03:56:30 +00001209 case Instruction::SetEQ:
1210 // SetEQ(null,GV) -> false
1211 if (V1->isNullValue()) {
1212 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2))
1213 if (!GV->hasExternalWeakLinkage())
1214 return ConstantBool::getFalse();
1215 // SetEQ(GV,null) -> false
1216 } else if (V2->isNullValue()) {
1217 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1))
1218 if (!GV->hasExternalWeakLinkage())
1219 return ConstantBool::getFalse();
1220 }
1221 C = ConstRules::get(V1, V2).equalto(V1, V2);
1222 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001223 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1224 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Reid Spencer6f05d732006-12-01 03:56:30 +00001225 case Instruction::SetNE:
1226 // SetNE(null,GV) -> true
1227 if (V1->isNullValue()) {
1228 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2))
1229 if (!GV->hasExternalWeakLinkage())
1230 return ConstantBool::getTrue();
1231 // SetNE(GV,null) -> true
1232 } else if (V2->isNullValue()) {
1233 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1))
1234 if (!GV->hasExternalWeakLinkage())
1235 return ConstantBool::getTrue();
1236 }
1237 // V1 != V2 === !(V1 == V2)
Chris Lattner1dd054c2004-01-12 22:07:24 +00001238 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001239 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001240 break;
1241 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1242 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001243 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001244 break;
1245 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1246 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001247 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001248 break;
1249 }
1250
Chris Lattner061da2f2004-01-13 05:51:55 +00001251 // If we successfully folded the expression, return it now.
1252 if (C) return C;
1253
Chris Lattnere1496fb2006-09-17 19:14:47 +00001254 if (SetCondInst::isComparison(Opcode)) {
Chris Lattner192eacc2004-10-17 04:01:51 +00001255 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1256 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001257 switch (evaluateRelation(const_cast<Constant*>(V1),
1258 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001259 default: assert(0 && "Unknown relational!");
1260 case Instruction::BinaryOpsEnd:
1261 break; // Couldn't determine anything about these constants.
1262 case Instruction::SetEQ: // We know the constants are equal!
1263 // If we know the constants are equal, we can decide the result of this
1264 // computation precisely.
1265 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1266 Opcode == Instruction::SetLE ||
1267 Opcode == Instruction::SetGE);
1268 case Instruction::SetLT:
1269 // If we know that V1 < V2, we can decide the result of this computation
1270 // precisely.
1271 return ConstantBool::get(Opcode == Instruction::SetLT ||
1272 Opcode == Instruction::SetNE ||
1273 Opcode == Instruction::SetLE);
1274 case Instruction::SetGT:
1275 // If we know that V1 > V2, we can decide the result of this computation
1276 // precisely.
1277 return ConstantBool::get(Opcode == Instruction::SetGT ||
1278 Opcode == Instruction::SetNE ||
1279 Opcode == Instruction::SetGE);
1280 case Instruction::SetLE:
1281 // If we know that V1 <= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001282 if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
1283 if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001284 break;
1285
1286 case Instruction::SetGE:
1287 // If we know that V1 >= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001288 if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
1289 if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001290 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001291
Chris Lattner061da2f2004-01-13 05:51:55 +00001292 case Instruction::SetNE:
1293 // If we know that V1 != V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001294 if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
1295 if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001296 break;
1297 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001298 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001299
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001300 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1301 switch (Opcode) {
1302 case Instruction::Add:
1303 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001304 case Instruction::Xor:
1305 return UndefValue::get(V1->getType());
1306
1307 case Instruction::Mul:
1308 case Instruction::And:
1309 return Constant::getNullValue(V1->getType());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001310 case Instruction::UDiv:
1311 case Instruction::SDiv:
1312 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001313 case Instruction::URem:
1314 case Instruction::SRem:
1315 case Instruction::FRem:
1316 if (!isa<UndefValue>(V2)) // undef / X -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001317 return Constant::getNullValue(V1->getType());
Reid Spencer7eb55b32006-11-02 01:53:59 +00001318 return const_cast<Constant*>(V2); // X / undef -> undef
1319 case Instruction::Or: // X | undef -> -1
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001320 return ConstantInt::getAllOnesValue(V1->getType());
Reid Spencerfdff9382006-11-08 06:47:33 +00001321 case Instruction::LShr:
1322 if (isa<UndefValue>(V2) && isa<UndefValue>(V1))
1323 return const_cast<Constant*>(V1); // undef lshr undef -> undef
1324 return Constant::getNullValue(V1->getType()); // X lshr undef -> 0
1325 // undef lshr X -> 0
1326 case Instruction::AShr:
1327 if (!isa<UndefValue>(V2))
1328 return const_cast<Constant*>(V1); // undef ashr X --> undef
1329 else if (isa<UndefValue>(V1))
1330 return const_cast<Constant*>(V1); // undef ashr undef -> undef
1331 else
1332 return const_cast<Constant*>(V1); // X ashr undef --> X
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001333 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001334 // undef << X -> 0 or X << undef -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001335 return Constant::getNullValue(V1->getType());
1336 }
1337 }
1338
Chris Lattner061da2f2004-01-13 05:51:55 +00001339 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
Reid Spencer3054b142006-11-02 08:18:15 +00001340 if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001341 // There are many possible foldings we could do here. We should probably
1342 // at least fold add of a pointer with an integer into the appropriate
1343 // getelementptr. This will improve alias analysis a bit.
Chris Lattner061da2f2004-01-13 05:51:55 +00001344 } else {
1345 // Just implement a couple of simple identities.
1346 switch (Opcode) {
1347 case Instruction::Add:
1348 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1349 break;
1350 case Instruction::Sub:
1351 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1352 break;
1353 case Instruction::Mul:
1354 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1355 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001356 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001357 return const_cast<Constant*>(V1); // X * 1 == X
1358 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001359 case Instruction::UDiv:
1360 case Instruction::SDiv:
Chris Lattner061da2f2004-01-13 05:51:55 +00001361 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001362 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001363 return const_cast<Constant*>(V1); // X / 1 == X
1364 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001365 case Instruction::URem:
1366 case Instruction::SRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001367 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001368 if (CI->getZExtValue() == 1)
Reid Spencer7eb55b32006-11-02 01:53:59 +00001369 return Constant::getNullValue(CI->getType()); // X % 1 == 0
Chris Lattner061da2f2004-01-13 05:51:55 +00001370 break;
1371 case Instruction::And:
1372 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1373 return const_cast<Constant*>(V1); // X & -1 == X
1374 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001375 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001376 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001377
1378 // Functions are at least 4-byte aligned. If and'ing the address of a
1379 // function with a constant < 4, fold it to zero.
1380 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001381 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001382 return Constant::getNullValue(CI->getType());
1383 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001384 break;
1385 case Instruction::Or:
1386 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1387 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1388 return const_cast<Constant*>(V2); // X | -1 == -1
1389 break;
1390 case Instruction::Xor:
1391 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1392 break;
1393 }
1394 }
1395
Reid Spencer3054b142006-11-02 08:18:15 +00001396 } else if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001397 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1398 // other way if possible.
1399 switch (Opcode) {
1400 case Instruction::Add:
1401 case Instruction::Mul:
1402 case Instruction::And:
1403 case Instruction::Or:
1404 case Instruction::Xor:
1405 case Instruction::SetEQ:
1406 case Instruction::SetNE:
1407 // No change of opcode required.
1408 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1409
1410 case Instruction::SetLT:
1411 case Instruction::SetGT:
1412 case Instruction::SetLE:
1413 case Instruction::SetGE:
1414 // Change the opcode as necessary to swap the operands.
1415 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1416 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1417
1418 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001419 case Instruction::LShr:
1420 case Instruction::AShr:
Chris Lattner061da2f2004-01-13 05:51:55 +00001421 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001422 case Instruction::SDiv:
1423 case Instruction::UDiv:
1424 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001425 case Instruction::URem:
1426 case Instruction::SRem:
1427 case Instruction::FRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001428 default: // These instructions cannot be flopped around.
1429 break;
1430 }
1431 }
1432 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001433}
1434
Reid Spencerb471d8e2006-12-04 05:19:34 +00001435Constant *llvm::ConstantFoldCompare(
1436 unsigned opcode, Constant *C1, Constant *C2, unsigned short predicate)
1437{
1438 // Place holder for future folding of ICmp and FCmp instructions
1439 return 0;
1440}
1441
Chris Lattner1dd054c2004-01-12 22:07:24 +00001442Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001443 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001444 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001445 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001446 return const_cast<Constant*>(C);
1447
Chris Lattnerf6013752004-10-17 21:54:55 +00001448 if (isa<UndefValue>(C)) {
1449 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1450 true);
1451 assert(Ty != 0 && "Invalid indices for GEP!");
1452 return UndefValue::get(PointerType::get(Ty));
1453 }
1454
1455 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001456 if (C->isNullValue()) {
1457 bool isNull = true;
1458 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001459 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001460 isNull = false;
1461 break;
1462 }
1463 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001464 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001465 true);
1466 assert(Ty != 0 && "Invalid indices for GEP!");
1467 return ConstantPointerNull::get(PointerType::get(Ty));
1468 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001469
1470 if (IdxList.size() == 1) {
1471 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
Reid Spencere0fc4df2006-10-20 07:07:24 +00001472 if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
Chris Lattner4bbd4092004-07-15 01:16:59 +00001473 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1474 // type, we can statically fold this.
Reid Spencere0fc4df2006-10-20 07:07:24 +00001475 Constant *R = ConstantInt::get(Type::UIntTy, ElSize);
Reid Spencer1a063892006-12-04 02:46:44 +00001476 // We know R is unsigned, Idx0 is signed because it must be an index
1477 // through a sequential type (gep pointer operand) which is always
1478 // signed.
Reid Spencer27720a92006-12-05 03:30:09 +00001479 R = ConstantExpr::getSExtOrBitCast(R, Idx0->getType());
Reid Spencer1a063892006-12-04 02:46:44 +00001480 R = ConstantExpr::getMul(R, Idx0); // signed multiply
1481 // R is a signed integer, C is the GEP pointer so -> IntToPtr
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001482 return ConstantExpr::getIntToPtr(R, C->getType());
Chris Lattner4bbd4092004-07-15 01:16:59 +00001483 }
1484 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001485 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001486
1487 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1488 // Combine Indices - If the source pointer to this getelementptr instruction
1489 // is a getelementptr instruction, combine the indices of the two
1490 // getelementptr instructions into a single instruction.
1491 //
1492 if (CE->getOpcode() == Instruction::GetElementPtr) {
1493 const Type *LastTy = 0;
1494 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1495 I != E; ++I)
1496 LastTy = *I;
1497
Chris Lattner13128ab2004-10-11 22:52:25 +00001498 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1499 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001500 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1501 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001502 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001503
1504 // Add the last index of the source with the first index of the new GEP.
1505 // Make sure to handle the case when they are actually different types.
1506 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001507 // Otherwise it must be an array.
1508 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001509 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001510 if (IdxTy != Idx0->getType()) {
Reid Spencer27720a92006-12-05 03:30:09 +00001511 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::LongTy);
1512 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
1513 Type::LongTy);
Reid Spencer1a063892006-12-04 02:46:44 +00001514 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1515 } else {
1516 Combined =
1517 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1518 }
Chris Lattner71068a02004-07-07 04:45:13 +00001519 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001520
Chris Lattner1dd054c2004-01-12 22:07:24 +00001521 NewIndices.push_back(Combined);
1522 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1523 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1524 }
1525 }
1526
1527 // Implement folding of:
1528 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1529 // long 0, long 0)
1530 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1531 //
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001532 if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001533 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001534 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1535 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1536 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001537 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001538 if (CAT->getElementType() == SAT->getElementType())
1539 return ConstantExpr::getGetElementPtr(
1540 (Constant*)CE->getOperand(0), IdxList);
1541 }
1542 return 0;
1543}
1544