blob: 29910b7bc90bc4fec43bce7816f6299556fa2bb2 [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 Spencerf5fc34a2006-12-19 03:15:47 +0000683 return ConstantFP::get(DestTy, cast<ConstantFP>(V)->getValue());
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000684 case Instruction::FPToUI: {
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000685 double dVal = cast<ConstantFP>(V)->getValue();
686 uint64_t iVal = (uint64_t) dVal;
687 return ConstantIntegral::get(DestTy, iVal);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000688 }
689 case Instruction::FPToSI: {
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000690 double dVal = cast<ConstantFP>(V)->getValue();
691 int64_t iVal = (int64_t) dVal;
692 return ConstantIntegral::get(DestTy, iVal);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000693 }
694 case Instruction::IntToPtr: //always treated as unsigned
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000695 if (V->isNullValue()) // Is it a FP or Integral null value?
696 return ConstantPointerNull::get(cast<PointerType>(DestTy));
697 return 0; // Other pointer types cannot be casted
698 case Instruction::PtrToInt: // always treated as unsigned
699 if (V->isNullValue())
700 return ConstantIntegral::get(DestTy, 0);
701 return 0; // Other pointer types cannot be casted
702 case Instruction::UIToFP: {
703 // First, extract the unsigned integer value
704 uint64_t Val;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000705 if (isa<ConstantInt>(V))
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000706 Val = cast<ConstantIntegral>(V)->getZExtValue();
707 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
708 Val = CB->getValue() ? 1 : 0;
709 // Now generate the equivalent floating point value
710 double dVal = (double) Val;
711 return ConstantFP::get(DestTy, dVal);
712 }
713 case Instruction::SIToFP: {
714 // First, extract the signed integer value
715 int64_t Val;
716 if (isa<ConstantInt>(V))
717 Val = cast<ConstantIntegral>(V)->getSExtValue();
718 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
719 Val = CB->getValue() ? -1 : 0;
720 // Now generate the equivalent floating point value
721 double dVal = (double) Val;
722 return ConstantFP::get(DestTy, dVal);
723 }
724 case Instruction::ZExt:
725 // Handle trunc directly here if it is a ConstantIntegral.
726 if (isa<ConstantInt>(V))
727 return ConstantInt::get(DestTy, cast<ConstantInt>(V)->getZExtValue());
728 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
729 return ConstantInt::get(DestTy, CB->getValue() ? 1 : 0);
730 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000731 case Instruction::SExt:
732 // A SExt always produces a signed value so we need to cast the value
733 // now before we try to cast it to the destiniation type.
734 if (isa<ConstantInt>(V))
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000735 return ConstantInt::get(DestTy, cast<ConstantInt>(V)->getSExtValue());
Chris Lattnerdefd8472006-12-01 19:50:54 +0000736 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000737 return ConstantInt::get(DestTy, CB->getValue() ? -1 : 0);
738 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000739 case Instruction::Trunc:
740 // We just handle trunc directly here. The code below doesn't work for
741 // trunc to bool.
742 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
743 return ConstantIntegral::get(DestTy, CI->getZExtValue());
744 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000745 case Instruction::BitCast:
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000746 if (SrcTy == DestTy)
747 return (Constant*)V; // no-op cast
Chris Lattner4d1da162006-12-11 18:30:27 +0000748
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000749 // Check to see if we are casting a pointer to an aggregate to a pointer to
750 // the first element. If so, return the appropriate GEP instruction.
751 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
752 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
753 std::vector<Value*> IdxList;
754 IdxList.push_back(Constant::getNullValue(Type::IntTy));
755 const Type *ElTy = PTy->getElementType();
756 while (ElTy != DPTy->getElementType()) {
757 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
758 if (STy->getNumElements() == 0) break;
759 ElTy = STy->getElementType(0);
760 IdxList.push_back(Constant::getNullValue(Type::UIntTy));
761 } else if (const SequentialType *STy =
762 dyn_cast<SequentialType>(ElTy)) {
763 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
764 ElTy = STy->getElementType();
765 IdxList.push_back(IdxList[0]);
766 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000767 break;
768 }
769 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000770
771 if (ElTy == DPTy->getElementType())
772 return ConstantExpr::getGetElementPtr(
773 const_cast<Constant*>(V),IdxList);
774 }
775
776 // Handle casts from one packed constant to another. We know that the src
777 // and dest type have the same size (otherwise its an illegal cast).
778 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
779 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
780 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
781 "Not cast between same sized vectors!");
782 // First, check for null and undef
783 if (isa<ConstantAggregateZero>(V))
784 return Constant::getNullValue(DestTy);
785 if (isa<UndefValue>(V))
786 return UndefValue::get(DestTy);
787
788 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
789 // This is a cast from a ConstantPacked of one type to a
790 // ConstantPacked of another type. Check to see if all elements of
791 // the input are simple.
792 bool AllSimpleConstants = true;
793 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
794 if (!isa<ConstantInt>(CP->getOperand(i)) &&
795 !isa<ConstantFP>(CP->getOperand(i))) {
796 AllSimpleConstants = false;
797 break;
798 }
799 }
800
801 // If all of the elements are simple constants, we can fold this.
802 if (AllSimpleConstants)
803 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
804 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000805 }
806 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000807
Chris Lattner4d1da162006-12-11 18:30:27 +0000808 // Finally, implement bitcast folding now. The code below doesn't handle
809 // bitcast right.
810 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
811 return ConstantPointerNull::get(cast<PointerType>(DestTy));
812
813 // Handle integral constant input.
814 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
815 // Integral -> Integral, must be changing sign.
816 if (DestTy->isIntegral())
817 return ConstantInt::get(DestTy, CI->getZExtValue());
818
819 if (DestTy->isFloatingPoint()) {
820 if (DestTy == Type::FloatTy)
821 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
822 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
823 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
824 }
825 // Otherwise, can't fold this (packed?)
826 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000827 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000828
829 // Handle ConstantFP input.
830 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
831 // FP -> Integral.
832 if (DestTy->isIntegral()) {
Reid Spencer3db7d372006-12-11 21:27:28 +0000833 if (DestTy == Type::IntTy || DestTy == Type::UIntTy)
Chris Lattner4d1da162006-12-11 18:30:27 +0000834 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
Reid Spencer3db7d372006-12-11 21:27:28 +0000835 assert((DestTy == Type::LongTy || DestTy == Type::ULongTy)
836 && "Incorrect integer type for bitcast!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000837 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
838 }
839 }
840 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000841 default:
842 assert(!"Invalid CE CastInst opcode");
843 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000844 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000845
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000846 assert(0 && "Failed to cast constant expression");
847 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000848}
849
Chris Lattner6ea4b522004-03-12 05:53:32 +0000850Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
851 const Constant *V1,
852 const Constant *V2) {
Chris Lattner78430662006-09-28 23:34:49 +0000853 if (const ConstantBool *CB = dyn_cast<ConstantBool>(Cond))
854 return const_cast<Constant*>(CB->getValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000855
856 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
857 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
858 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000859 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000860 return 0;
861}
862
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000863Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
864 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000865 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
866 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000867 if (Val->isNullValue()) // ee(zero, x) -> zero
868 return Constant::getNullValue(
869 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000870
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000871 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000872 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
873 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000874 } else if (isa<UndefValue>(Idx)) {
875 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
876 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000877 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000878 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000879 return 0;
880}
881
Robert Bocchinoca27f032006-01-17 20:07:22 +0000882Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
883 const Constant *Elt,
884 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000885 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000886 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000887 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000888 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000889 // Insertion of scalar constant into packed undef
890 // Optimize away insertion of undef
891 if (isa<UndefValue>(Elt))
892 return const_cast<Constant*>(Val);
893 // Otherwise break the aggregate undef into multiple undefs and do
894 // the insertion
895 unsigned numOps =
896 cast<PackedType>(Val->getType())->getNumElements();
897 std::vector<Constant*> Ops;
898 Ops.reserve(numOps);
899 for (unsigned i = 0; i < numOps; ++i) {
900 const Constant *Op =
901 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
902 Ops.push_back(const_cast<Constant*>(Op));
903 }
904 return ConstantPacked::get(Ops);
905 }
Reid Spencer3054b142006-11-02 08:18:15 +0000906 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000907 // Insertion of scalar constant into packed aggregate zero
908 // Optimize away insertion of zero
909 if (Elt->isNullValue())
910 return const_cast<Constant*>(Val);
911 // Otherwise break the aggregate zero into multiple zeros and do
912 // the insertion
913 unsigned numOps =
914 cast<PackedType>(Val->getType())->getNumElements();
915 std::vector<Constant*> Ops;
916 Ops.reserve(numOps);
917 for (unsigned i = 0; i < numOps; ++i) {
918 const Constant *Op =
919 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
920 Ops.push_back(const_cast<Constant*>(Op));
921 }
922 return ConstantPacked::get(Ops);
923 }
924 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
925 // Insertion of scalar constant into packed constant
926 std::vector<Constant*> Ops;
927 Ops.reserve(CVal->getNumOperands());
928 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
929 const Constant *Op =
930 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
931 Ops.push_back(const_cast<Constant*>(Op));
932 }
933 return ConstantPacked::get(Ops);
934 }
935 return 0;
936}
937
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000938Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
939 const Constant *V2,
940 const Constant *Mask) {
941 // TODO:
942 return 0;
943}
944
945
Chris Lattner60c47262005-01-28 19:09:51 +0000946/// isZeroSizedType - This type is zero sized if its an array or structure of
947/// zero sized types. The only leaf zero sized type is an empty structure.
948static bool isMaybeZeroSizedType(const Type *Ty) {
949 if (isa<OpaqueType>(Ty)) return true; // Can't say.
950 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
951
952 // If all of elements have zero size, this does too.
953 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000954 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000955 return true;
956
957 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
958 return isMaybeZeroSizedType(ATy->getElementType());
959 }
960 return false;
961}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000962
Chris Lattner061da2f2004-01-13 05:51:55 +0000963/// IdxCompare - Compare the two constants as though they were getelementptr
964/// indices. This allows coersion of the types to be the same thing.
965///
966/// If the two constants are the "same" (after coersion), return 0. If the
967/// first is less than the second, return -1, if the second is less than the
968/// first, return 1. If the constants are not integral, return -2.
969///
Chris Lattner60c47262005-01-28 19:09:51 +0000970static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000971 if (C1 == C2) return 0;
972
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000973 // Ok, we found a different index. Are either of the operands ConstantExprs?
974 // If so, we can't do anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000975 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
976 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000977
Chris Lattner69193f92004-04-05 01:30:19 +0000978 // Ok, we have two differing integer indices. Sign extend them to be the same
979 // type. Long is always big enough, so we use it.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000980 if (C1->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
Reid Spencerbb65ebf2006-12-12 23:36:14 +0000981 C1 = ConstantExpr::getSExt(C1, Type::LongTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000982 else
983 C1 = ConstantExpr::getBitCast(C1, Type::LongTy);
984 if (C2->getType() != Type::LongTy && C1->getType() != Type::ULongTy)
Reid Spencerbb65ebf2006-12-12 23:36:14 +0000985 C2 = ConstantExpr::getSExt(C2, Type::LongTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000986 else
987 C2 = ConstantExpr::getBitCast(C2, Type::LongTy);
988
Chris Lattner061da2f2004-01-13 05:51:55 +0000989 if (C1 == C2) return 0; // Are they just differing types?
990
Chris Lattner60c47262005-01-28 19:09:51 +0000991 // If the type being indexed over is really just a zero sized type, there is
992 // no pointer difference being made here.
993 if (isMaybeZeroSizedType(ElTy))
994 return -2; // dunno.
995
Chris Lattner061da2f2004-01-13 05:51:55 +0000996 // If they are really different, now that they are the same type, then we
997 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000998 if (cast<ConstantInt>(C1)->getSExtValue() <
999 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001000 return -1;
1001 else
1002 return 1;
1003}
1004
1005/// evaluateRelation - This function determines if there is anything we can
1006/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001007/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +00001008/// and GlobalValues. If we can determine that the two constants have a
Chris Lattner061da2f2004-01-13 05:51:55 +00001009/// particular relation to each other, we should return the corresponding SetCC
1010/// code, otherwise return Instruction::BinaryOpsEnd.
1011///
1012/// To simplify this code we canonicalize the relation so that the first
1013/// operand is always the most "complex" of the two. We consider simple
1014/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001015/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001016///
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001017static Instruction::BinaryOps evaluateRelation(Constant *V1, Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001018 assert(V1->getType() == V2->getType() &&
1019 "Cannot compare different types of values!");
1020 if (V1 == V2) return Instruction::SetEQ;
1021
Reid Spenceraccd7c72004-07-17 23:47:01 +00001022 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001023 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1024 // We distilled this down to a simple case, use the standard constant
1025 // folder.
1026 ConstantBool *R = dyn_cast<ConstantBool>(ConstantExpr::getSetEQ(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001027 if (R && R->getValue()) return Instruction::SetEQ;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001028 R = dyn_cast<ConstantBool>(ConstantExpr::getSetLT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001029 if (R && R->getValue()) return Instruction::SetLT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001030 R = dyn_cast<ConstantBool>(ConstantExpr::getSetGT(V1, V2));
Chris Lattner78430662006-09-28 23:34:49 +00001031 if (R && R->getValue()) return Instruction::SetGT;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001032
1033 // If we couldn't figure it out, bail.
1034 return Instruction::BinaryOpsEnd;
1035 }
1036
Chris Lattner061da2f2004-01-13 05:51:55 +00001037 // If the first operand is simple, swap operands.
Chris Lattner125ed542004-02-01 01:23:19 +00001038 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1039 if (SwappedRelation != Instruction::BinaryOpsEnd)
1040 return SetCondInst::getSwappedCondition(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001041
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001042 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001043 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001044 Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
1045 if (SwappedRelation != Instruction::BinaryOpsEnd)
1046 return SetCondInst::getSwappedCondition(SwappedRelation);
1047 else
1048 return Instruction::BinaryOpsEnd;
Chris Lattner125ed542004-02-01 01:23:19 +00001049 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001050
Reid Spenceraccd7c72004-07-17 23:47:01 +00001051 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001052 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001053 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001054 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1055 return Instruction::SetNE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001056 } else {
Reid Spencer876f7222006-12-06 00:25:09 +00001057 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +00001058 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +00001059 if (!CPR1->hasExternalWeakLinkage())
1060 return Instruction::SetNE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001061 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001062 } else {
1063 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1064 // constantexpr, a CPR, or a simple constant.
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001065 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Chris Lattner061da2f2004-01-13 05:51:55 +00001066 Constant *CE1Op0 = CE1->getOperand(0);
1067
1068 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001069 case Instruction::Trunc:
1070 case Instruction::FPTrunc:
1071 case Instruction::FPExt:
1072 case Instruction::FPToUI:
1073 case Instruction::FPToSI:
1074 break; // We don't do anything with floating point.
1075 case Instruction::ZExt:
1076 case Instruction::SExt:
1077 case Instruction::UIToFP:
1078 case Instruction::SIToFP:
1079 case Instruction::PtrToInt:
1080 case Instruction::IntToPtr:
1081 case Instruction::BitCast:
Chris Lattner061da2f2004-01-13 05:51:55 +00001082 // If the cast is not actually changing bits, and the second operand is a
1083 // null pointer, do the comparison with the pre-casted value.
1084 if (V2->isNullValue() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001085 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral()))
Chris Lattner061da2f2004-01-13 05:51:55 +00001086 return evaluateRelation(CE1Op0,
1087 Constant::getNullValue(CE1Op0->getType()));
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001088
1089 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1090 // from the same type as the src of the LHS, evaluate the inputs. This is
1091 // important for things like "seteq (cast 4 to int*), (cast 5 to int*)",
1092 // which happens a lot in compilers with tagged integers.
1093 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001094 if (isa<PointerType>(CE1->getType()) && CE2->isCast() &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001095 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
1096 CE1->getOperand(0)->getType()->isIntegral()) {
1097 return evaluateRelation(CE1->getOperand(0), CE2->getOperand(0));
1098 }
Chris Lattner192e3262004-04-11 01:29:30 +00001099 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001100
1101 case Instruction::GetElementPtr:
1102 // Ok, since this is a getelementptr, we know that the constant has a
1103 // pointer type. Check the various cases.
1104 if (isa<ConstantPointerNull>(V2)) {
1105 // If we are comparing a GEP to a null pointer, check to see if the base
1106 // of the GEP equals the null pointer.
Reid Spencer876f7222006-12-06 00:25:09 +00001107 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1108 if (GV->hasExternalWeakLinkage())
1109 // Weak linkage GVals could be zero or not. We're comparing that
1110 // to null pointer so its greater-or-equal
1111 return Instruction::SetGE;
1112 else
1113 // If its not weak linkage, the GVal must have a non-zero address
1114 // so the result is greater-than
1115 return Instruction::SetGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001116 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1117 // If we are indexing from a null pointer, check to see if we have any
1118 // non-zero indices.
1119 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1120 if (!CE1->getOperand(i)->isNullValue())
1121 // Offsetting from null, must not be equal.
1122 return Instruction::SetGT;
1123 // Only zero indexes from null, must still be zero.
1124 return Instruction::SetEQ;
1125 }
1126 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001127 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001128 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001129 if (CPR2->hasExternalWeakLinkage())
1130 // Weak linkage GVals could be zero or not. We're comparing it to
1131 // a null pointer, so its less-or-equal
1132 return Instruction::SetLE;
1133 else
1134 // If its not weak linkage, the GVal must have a non-zero address
1135 // so the result is less-than
1136 return Instruction::SetLT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001137 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001138 if (CPR1 == CPR2) {
1139 // If this is a getelementptr of the same global, then it must be
1140 // different. Because the types must match, the getelementptr could
1141 // only have at most one index, and because we fold getelementptr's
1142 // with a single zero index, it must be nonzero.
1143 assert(CE1->getNumOperands() == 2 &&
1144 !CE1->getOperand(1)->isNullValue() &&
1145 "Suprising getelementptr!");
1146 return Instruction::SetGT;
1147 } else {
1148 // If they are different globals, we don't know what the value is,
1149 // but they can't be equal.
1150 return Instruction::SetNE;
1151 }
1152 }
1153 } else {
1154 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1155 const Constant *CE2Op0 = CE2->getOperand(0);
1156
1157 // There are MANY other foldings that we could perform here. They will
1158 // probably be added on demand, as they seem needed.
1159 switch (CE2->getOpcode()) {
1160 default: break;
1161 case Instruction::GetElementPtr:
1162 // By far the most common case to handle is when the base pointers are
1163 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001164 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001165 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1166 return Instruction::SetNE;
1167 // Ok, we know that both getelementptr instructions are based on the
1168 // same global. From this, we can precisely determine the relative
1169 // ordering of the resultant pointers.
1170 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001171
Chris Lattner061da2f2004-01-13 05:51:55 +00001172 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001173 gep_type_iterator GTI = gep_type_begin(CE1);
1174 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1175 ++i, ++GTI)
1176 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1177 GTI.getIndexedType())) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001178 case -1: return Instruction::SetLT;
1179 case 1: return Instruction::SetGT;
1180 case -2: return Instruction::BinaryOpsEnd;
1181 }
1182
1183 // Ok, we ran out of things they have in common. If any leftovers
1184 // are non-zero then we have a difference, otherwise we are equal.
1185 for (; i < CE1->getNumOperands(); ++i)
1186 if (!CE1->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001187 if (isa<ConstantIntegral>(CE1->getOperand(i)))
1188 return Instruction::SetGT;
1189 else
1190 return Instruction::BinaryOpsEnd; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001191
Chris Lattner061da2f2004-01-13 05:51:55 +00001192 for (; i < CE2->getNumOperands(); ++i)
1193 if (!CE2->getOperand(i)->isNullValue())
Chris Lattner60c47262005-01-28 19:09:51 +00001194 if (isa<ConstantIntegral>(CE2->getOperand(i)))
1195 return Instruction::SetLT;
1196 else
1197 return Instruction::BinaryOpsEnd; // Might be equal.
Chris Lattner061da2f2004-01-13 05:51:55 +00001198 return Instruction::SetEQ;
1199 }
1200 }
1201 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001202
Chris Lattner061da2f2004-01-13 05:51:55 +00001203 default:
1204 break;
1205 }
1206 }
1207
1208 return Instruction::BinaryOpsEnd;
1209}
1210
Chris Lattner1dd054c2004-01-12 22:07:24 +00001211Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
1212 const Constant *V1,
1213 const Constant *V2) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001214 Constant *C = 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001215 switch (Opcode) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001216 default: break;
1217 case Instruction::Add: C = ConstRules::get(V1, V2).add(V1, V2); break;
1218 case Instruction::Sub: C = ConstRules::get(V1, V2).sub(V1, V2); break;
1219 case Instruction::Mul: C = ConstRules::get(V1, V2).mul(V1, V2); break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001220 case Instruction::UDiv: C = ConstRules::get(V1, V2).udiv(V1, V2); break;
1221 case Instruction::SDiv: C = ConstRules::get(V1, V2).sdiv(V1, V2); break;
1222 case Instruction::FDiv: C = ConstRules::get(V1, V2).fdiv(V1, V2); break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001223 case Instruction::URem: C = ConstRules::get(V1, V2).urem(V1, V2); break;
1224 case Instruction::SRem: C = ConstRules::get(V1, V2).srem(V1, V2); break;
1225 case Instruction::FRem: C = ConstRules::get(V1, V2).frem(V1, V2); break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001226 case Instruction::And: C = ConstRules::get(V1, V2).op_and(V1, V2); break;
1227 case Instruction::Or: C = ConstRules::get(V1, V2).op_or (V1, V2); break;
1228 case Instruction::Xor: C = ConstRules::get(V1, V2).op_xor(V1, V2); break;
1229 case Instruction::Shl: C = ConstRules::get(V1, V2).shl(V1, V2); break;
Reid Spencerfdff9382006-11-08 06:47:33 +00001230 case Instruction::LShr: C = ConstRules::get(V1, V2).lshr(V1, V2); break;
1231 case Instruction::AShr: C = ConstRules::get(V1, V2).ashr(V1, V2); break;
Reid Spencer6f05d732006-12-01 03:56:30 +00001232 case Instruction::SetEQ:
1233 // SetEQ(null,GV) -> false
1234 if (V1->isNullValue()) {
1235 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2))
1236 if (!GV->hasExternalWeakLinkage())
1237 return ConstantBool::getFalse();
1238 // SetEQ(GV,null) -> false
1239 } else if (V2->isNullValue()) {
1240 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1))
1241 if (!GV->hasExternalWeakLinkage())
1242 return ConstantBool::getFalse();
1243 }
1244 C = ConstRules::get(V1, V2).equalto(V1, V2);
1245 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001246 case Instruction::SetLT: C = ConstRules::get(V1, V2).lessthan(V1, V2);break;
1247 case Instruction::SetGT: C = ConstRules::get(V1, V2).lessthan(V2, V1);break;
Reid Spencer6f05d732006-12-01 03:56:30 +00001248 case Instruction::SetNE:
1249 // SetNE(null,GV) -> true
1250 if (V1->isNullValue()) {
1251 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V2))
1252 if (!GV->hasExternalWeakLinkage())
1253 return ConstantBool::getTrue();
1254 // SetNE(GV,null) -> true
1255 } else if (V2->isNullValue()) {
1256 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1))
1257 if (!GV->hasExternalWeakLinkage())
1258 return ConstantBool::getTrue();
1259 }
1260 // V1 != V2 === !(V1 == V2)
Chris Lattner1dd054c2004-01-12 22:07:24 +00001261 C = ConstRules::get(V1, V2).equalto(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001262 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001263 break;
1264 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
1265 C = ConstRules::get(V1, V2).lessthan(V2, V1);
Chris Lattner6b52be62006-01-04 02:20:54 +00001266 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001267 break;
1268 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
1269 C = ConstRules::get(V1, V2).lessthan(V1, V2);
Chris Lattner6b52be62006-01-04 02:20:54 +00001270 if (C) return ConstantExpr::getNot(C);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001271 break;
1272 }
1273
Chris Lattner061da2f2004-01-13 05:51:55 +00001274 // If we successfully folded the expression, return it now.
1275 if (C) return C;
1276
Chris Lattnere1496fb2006-09-17 19:14:47 +00001277 if (SetCondInst::isComparison(Opcode)) {
Chris Lattner192eacc2004-10-17 04:01:51 +00001278 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1279 return UndefValue::get(Type::BoolTy);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001280 switch (evaluateRelation(const_cast<Constant*>(V1),
1281 const_cast<Constant*>(V2))) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001282 default: assert(0 && "Unknown relational!");
1283 case Instruction::BinaryOpsEnd:
1284 break; // Couldn't determine anything about these constants.
1285 case Instruction::SetEQ: // We know the constants are equal!
1286 // If we know the constants are equal, we can decide the result of this
1287 // computation precisely.
1288 return ConstantBool::get(Opcode == Instruction::SetEQ ||
1289 Opcode == Instruction::SetLE ||
1290 Opcode == Instruction::SetGE);
1291 case Instruction::SetLT:
1292 // If we know that V1 < V2, we can decide the result of this computation
1293 // precisely.
1294 return ConstantBool::get(Opcode == Instruction::SetLT ||
1295 Opcode == Instruction::SetNE ||
1296 Opcode == Instruction::SetLE);
1297 case Instruction::SetGT:
1298 // If we know that V1 > V2, we can decide the result of this computation
1299 // precisely.
1300 return ConstantBool::get(Opcode == Instruction::SetGT ||
1301 Opcode == Instruction::SetNE ||
1302 Opcode == Instruction::SetGE);
1303 case Instruction::SetLE:
1304 // If we know that V1 <= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001305 if (Opcode == Instruction::SetGT) return ConstantBool::getFalse();
1306 if (Opcode == Instruction::SetLT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001307 break;
1308
1309 case Instruction::SetGE:
1310 // If we know that V1 >= V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001311 if (Opcode == Instruction::SetLT) return ConstantBool::getFalse();
1312 if (Opcode == Instruction::SetGT) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001313 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001314
Chris Lattner061da2f2004-01-13 05:51:55 +00001315 case Instruction::SetNE:
1316 // If we know that V1 != V2, we can only partially decide this relation.
Chris Lattner78430662006-09-28 23:34:49 +00001317 if (Opcode == Instruction::SetEQ) return ConstantBool::getFalse();
1318 if (Opcode == Instruction::SetNE) return ConstantBool::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001319 break;
1320 }
Chris Lattner192eacc2004-10-17 04:01:51 +00001321 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001322
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001323 if (isa<UndefValue>(V1) || isa<UndefValue>(V2)) {
1324 switch (Opcode) {
1325 case Instruction::Add:
1326 case Instruction::Sub:
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001327 case Instruction::Xor:
1328 return UndefValue::get(V1->getType());
1329
1330 case Instruction::Mul:
1331 case Instruction::And:
1332 return Constant::getNullValue(V1->getType());
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001333 case Instruction::UDiv:
1334 case Instruction::SDiv:
1335 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001336 case Instruction::URem:
1337 case Instruction::SRem:
1338 case Instruction::FRem:
1339 if (!isa<UndefValue>(V2)) // undef / X -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001340 return Constant::getNullValue(V1->getType());
Reid Spencer7eb55b32006-11-02 01:53:59 +00001341 return const_cast<Constant*>(V2); // X / undef -> undef
1342 case Instruction::Or: // X | undef -> -1
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001343 return ConstantInt::getAllOnesValue(V1->getType());
Reid Spencerfdff9382006-11-08 06:47:33 +00001344 case Instruction::LShr:
1345 if (isa<UndefValue>(V2) && isa<UndefValue>(V1))
1346 return const_cast<Constant*>(V1); // undef lshr undef -> undef
1347 return Constant::getNullValue(V1->getType()); // X lshr undef -> 0
1348 // undef lshr X -> 0
1349 case Instruction::AShr:
1350 if (!isa<UndefValue>(V2))
1351 return const_cast<Constant*>(V1); // undef ashr X --> undef
1352 else if (isa<UndefValue>(V1))
1353 return const_cast<Constant*>(V1); // undef ashr undef -> undef
1354 else
1355 return const_cast<Constant*>(V1); // X ashr undef --> X
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001356 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001357 // undef << X -> 0 or X << undef -> 0
Chris Lattnerfd7bf722004-10-16 23:31:32 +00001358 return Constant::getNullValue(V1->getType());
1359 }
1360 }
1361
Chris Lattner061da2f2004-01-13 05:51:55 +00001362 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(V1)) {
Reid Spencer3054b142006-11-02 08:18:15 +00001363 if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001364 // There are many possible foldings we could do here. We should probably
1365 // at least fold add of a pointer with an integer into the appropriate
1366 // getelementptr. This will improve alias analysis a bit.
Chris Lattner061da2f2004-01-13 05:51:55 +00001367 } else {
1368 // Just implement a couple of simple identities.
1369 switch (Opcode) {
1370 case Instruction::Add:
1371 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X + 0 == X
1372 break;
1373 case Instruction::Sub:
1374 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X - 0 == X
1375 break;
1376 case Instruction::Mul:
1377 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X * 0 == 0
1378 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001379 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001380 return const_cast<Constant*>(V1); // X * 1 == X
1381 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001382 case Instruction::UDiv:
1383 case Instruction::SDiv:
Chris Lattner061da2f2004-01-13 05:51:55 +00001384 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001385 if (CI->getZExtValue() == 1)
Chris Lattner061da2f2004-01-13 05:51:55 +00001386 return const_cast<Constant*>(V1); // X / 1 == X
1387 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001388 case Instruction::URem:
1389 case Instruction::SRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001390 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001391 if (CI->getZExtValue() == 1)
Reid Spencer7eb55b32006-11-02 01:53:59 +00001392 return Constant::getNullValue(CI->getType()); // X % 1 == 0
Chris Lattner061da2f2004-01-13 05:51:55 +00001393 break;
1394 case Instruction::And:
1395 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1396 return const_cast<Constant*>(V1); // X & -1 == X
1397 if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001398 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001399 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattnerea0789c2004-03-08 06:17:35 +00001400
1401 // Functions are at least 4-byte aligned. If and'ing the address of a
1402 // function with a constant < 4, fold it to zero.
1403 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
Reid Spencere0fc4df2006-10-20 07:07:24 +00001404 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
Chris Lattnerea0789c2004-03-08 06:17:35 +00001405 return Constant::getNullValue(CI->getType());
1406 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001407 break;
1408 case Instruction::Or:
1409 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
1410 if (cast<ConstantIntegral>(V2)->isAllOnesValue())
1411 return const_cast<Constant*>(V2); // X | -1 == -1
1412 break;
1413 case Instruction::Xor:
1414 if (V2->isNullValue()) return const_cast<Constant*>(V1); // X ^ 0 == X
1415 break;
1416 }
1417 }
1418
Reid Spencer3054b142006-11-02 08:18:15 +00001419 } else if (isa<ConstantExpr>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001420 // If V2 is a constant expr and V1 isn't, flop them around and fold the
1421 // other way if possible.
1422 switch (Opcode) {
1423 case Instruction::Add:
1424 case Instruction::Mul:
1425 case Instruction::And:
1426 case Instruction::Or:
1427 case Instruction::Xor:
1428 case Instruction::SetEQ:
1429 case Instruction::SetNE:
1430 // No change of opcode required.
1431 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1432
1433 case Instruction::SetLT:
1434 case Instruction::SetGT:
1435 case Instruction::SetLE:
1436 case Instruction::SetGE:
1437 // Change the opcode as necessary to swap the operands.
1438 Opcode = SetCondInst::getSwappedCondition((Instruction::BinaryOps)Opcode);
1439 return ConstantFoldBinaryInstruction(Opcode, V2, V1);
1440
1441 case Instruction::Shl:
Reid Spencerfdff9382006-11-08 06:47:33 +00001442 case Instruction::LShr:
1443 case Instruction::AShr:
Chris Lattner061da2f2004-01-13 05:51:55 +00001444 case Instruction::Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001445 case Instruction::SDiv:
1446 case Instruction::UDiv:
1447 case Instruction::FDiv:
Reid Spencer7eb55b32006-11-02 01:53:59 +00001448 case Instruction::URem:
1449 case Instruction::SRem:
1450 case Instruction::FRem:
Chris Lattner061da2f2004-01-13 05:51:55 +00001451 default: // These instructions cannot be flopped around.
1452 break;
1453 }
1454 }
1455 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001456}
1457
Reid Spencerb471d8e2006-12-04 05:19:34 +00001458Constant *llvm::ConstantFoldCompare(
1459 unsigned opcode, Constant *C1, Constant *C2, unsigned short predicate)
1460{
1461 // Place holder for future folding of ICmp and FCmp instructions
1462 return 0;
1463}
1464
Chris Lattner1dd054c2004-01-12 22:07:24 +00001465Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001466 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001467 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001468 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001469 return const_cast<Constant*>(C);
1470
Chris Lattnerf6013752004-10-17 21:54:55 +00001471 if (isa<UndefValue>(C)) {
1472 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1473 true);
1474 assert(Ty != 0 && "Invalid indices for GEP!");
1475 return UndefValue::get(PointerType::get(Ty));
1476 }
1477
1478 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001479 if (C->isNullValue()) {
1480 bool isNull = true;
1481 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001482 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001483 isNull = false;
1484 break;
1485 }
1486 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001487 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001488 true);
1489 assert(Ty != 0 && "Invalid indices for GEP!");
1490 return ConstantPointerNull::get(PointerType::get(Ty));
1491 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001492
1493 if (IdxList.size() == 1) {
1494 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
Reid Spencere0fc4df2006-10-20 07:07:24 +00001495 if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
Chris Lattner4bbd4092004-07-15 01:16:59 +00001496 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1497 // type, we can statically fold this.
Reid Spencere0fc4df2006-10-20 07:07:24 +00001498 Constant *R = ConstantInt::get(Type::UIntTy, ElSize);
Reid Spencer1a063892006-12-04 02:46:44 +00001499 // We know R is unsigned, Idx0 is signed because it must be an index
1500 // through a sequential type (gep pointer operand) which is always
1501 // signed.
Reid Spencer27720a92006-12-05 03:30:09 +00001502 R = ConstantExpr::getSExtOrBitCast(R, Idx0->getType());
Reid Spencer1a063892006-12-04 02:46:44 +00001503 R = ConstantExpr::getMul(R, Idx0); // signed multiply
1504 // R is a signed integer, C is the GEP pointer so -> IntToPtr
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001505 return ConstantExpr::getIntToPtr(R, C->getType());
Chris Lattner4bbd4092004-07-15 01:16:59 +00001506 }
1507 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001508 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001509
1510 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1511 // Combine Indices - If the source pointer to this getelementptr instruction
1512 // is a getelementptr instruction, combine the indices of the two
1513 // getelementptr instructions into a single instruction.
1514 //
1515 if (CE->getOpcode() == Instruction::GetElementPtr) {
1516 const Type *LastTy = 0;
1517 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1518 I != E; ++I)
1519 LastTy = *I;
1520
Chris Lattner13128ab2004-10-11 22:52:25 +00001521 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1522 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001523 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1524 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001525 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001526
1527 // Add the last index of the source with the first index of the new GEP.
1528 // Make sure to handle the case when they are actually different types.
1529 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001530 // Otherwise it must be an array.
1531 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001532 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001533 if (IdxTy != Idx0->getType()) {
Reid Spencer27720a92006-12-05 03:30:09 +00001534 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::LongTy);
1535 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
1536 Type::LongTy);
Reid Spencer1a063892006-12-04 02:46:44 +00001537 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1538 } else {
1539 Combined =
1540 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1541 }
Chris Lattner71068a02004-07-07 04:45:13 +00001542 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001543
Chris Lattner1dd054c2004-01-12 22:07:24 +00001544 NewIndices.push_back(Combined);
1545 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1546 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1547 }
1548 }
1549
1550 // Implement folding of:
1551 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1552 // long 0, long 0)
1553 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1554 //
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001555 if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001556 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001557 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1558 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1559 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001560 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001561 if (CAT->getElementType() == SAT->getElementType())
1562 return ConstantExpr::getGetElementPtr(
1563 (Constant*)CE->getOperand(0), IdxList);
1564 }
1565 return 0;
1566}
1567