blob: ee5611ea7deb8ba7762e2dfd1bdfd7fba0ebcc6f [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
2//
3// This file implements the various intrinsic operations, on constant values.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner65b529f2002-04-08 20:18:09 +00007#include "llvm/ConstantHandling.h"
Chris Lattner78a0d422002-05-07 20:44:59 +00008#include "llvm/iPHINode.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +00009#include "llvm/DerivedTypes.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000010#include <cmath>
Chris Lattnerd42d4922001-06-30 04:36:40 +000011
Chris Lattner61607ee2001-09-09 21:01:20 +000012AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
13 &ConstRules::find));
14
Chris Lattner9f307732002-05-06 17:54:27 +000015// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
16// If successful, the constant result is returned, if not, null is returned.
17//
18Constant *ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000019 if (PHINode *PN = dyn_cast<PHINode>(I)) {
20 if (PN->getNumIncomingValues() == 0)
21 return Constant::getNullValue(PN->getType());
22
23 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
24 if (Result == 0) return 0;
25
26 // Handle PHI nodes specially here...
27 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
28 if (PN->getIncomingValue(i) != Result)
29 return 0; // Not all the same incoming constants...
30
31 // If we reach here, all incoming values are the same constant.
32 return Result;
33 }
34
Chris Lattner9f307732002-05-06 17:54:27 +000035 Constant *Op0 = 0;
36 Constant *Op1 = 0;
37
38 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
39 Op0 = dyn_cast<Constant>(I->getOperand(0));
40 if (Op0 == 0) return 0; // Not a constant?, can't fold
41
42 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
43 Op1 = dyn_cast<Constant>(I->getOperand(1));
44 if (Op1 == 0) return 0; // Not a constant?, can't fold
45 }
46 }
47
48 switch (I->getOpcode()) {
49 case Instruction::Cast:
Chris Lattnere17c1c5a2003-04-25 02:52:06 +000050 return ConstRules::get(*Op0, *Op0)->castTo(Op0, I->getType());
Chris Lattner9f307732002-05-06 17:54:27 +000051 case Instruction::Add: return *Op0 + *Op1;
52 case Instruction::Sub: return *Op0 - *Op1;
53 case Instruction::Mul: return *Op0 * *Op1;
54 case Instruction::Div: return *Op0 / *Op1;
55 case Instruction::Rem: return *Op0 % *Op1;
Chris Lattnere87f65e2002-07-30 16:24:28 +000056 case Instruction::And: return *Op0 & *Op1;
57 case Instruction::Or: return *Op0 | *Op1;
58 case Instruction::Xor: return *Op0 ^ *Op1;
Chris Lattner9f307732002-05-06 17:54:27 +000059
60 case Instruction::SetEQ: return *Op0 == *Op1;
61 case Instruction::SetNE: return *Op0 != *Op1;
62 case Instruction::SetLE: return *Op0 <= *Op1;
63 case Instruction::SetGE: return *Op0 >= *Op1;
64 case Instruction::SetLT: return *Op0 < *Op1;
65 case Instruction::SetGT: return *Op0 > *Op1;
66 case Instruction::Shl: return *Op0 << *Op1;
67 case Instruction::Shr: return *Op0 >> *Op1;
Chris Lattner1f0049c2003-04-17 19:24:18 +000068 case Instruction::GetElementPtr: {
69 std::vector<Constant*> IdxList;
70 IdxList.reserve(I->getNumOperands()-1);
71 if (Op1) IdxList.push_back(Op1);
72 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
73 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
74 IdxList.push_back(C);
75 else
76 return 0; // Non-constant operand
77 return ConstantFoldGetElementPtr(Op0, IdxList);
78 }
Chris Lattner9f307732002-05-06 17:54:27 +000079 default:
80 return 0;
81 }
82}
83
Chris Lattner1f0049c2003-04-17 19:24:18 +000084static unsigned getSize(const Type *Ty) {
85 unsigned S = Ty->getPrimitiveSize();
86 return S ? S : 8; // Treat pointers at 8 bytes
87}
88
Chris Lattner9f307732002-05-06 17:54:27 +000089Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000090 if (V->getType() == DestTy) return (Constant*)V;
91
92 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
93 if (CE->getOpcode() == Instruction::Cast) {
94 Constant *Op = (Constant*)cast<Constant>(CE->getOperand(0));
95 // Try to not produce a cast of a cast, which is almost always redundant.
96 if (!Op->getType()->isFloatingPoint() &&
97 !CE->getType()->isFloatingPoint() &&
98 !DestTy->getType()->isFloatingPoint()) {
99 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
100 unsigned S3 = getSize(DestTy);
101 if (Op->getType() == DestTy && S3 >= S2)
102 return Op;
103 if (S1 >= S2 && S2 >= S3)
104 return ConstantExpr::getCast(Op, DestTy);
105 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
106 return ConstantExpr::getCast(Op, DestTy);
107 }
108 }
109
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000110 return ConstRules::get(*V, *V)->castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000111}
112
Chris Lattner9f307732002-05-06 17:54:27 +0000113Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
114 const Constant *V2) {
115 switch (Opcode) {
116 case Instruction::Add: return *V1 + *V2;
117 case Instruction::Sub: return *V1 - *V2;
118 case Instruction::Mul: return *V1 * *V2;
119 case Instruction::Div: return *V1 / *V2;
120 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000121 case Instruction::And: return *V1 & *V2;
122 case Instruction::Or: return *V1 | *V2;
123 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +0000124
125 case Instruction::SetEQ: return *V1 == *V2;
126 case Instruction::SetNE: return *V1 != *V2;
127 case Instruction::SetLE: return *V1 <= *V2;
128 case Instruction::SetGE: return *V1 >= *V2;
129 case Instruction::SetLT: return *V1 < *V2;
130 case Instruction::SetGT: return *V1 > *V2;
131 }
132 return 0;
133}
134
135Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
136 const Constant *V2) {
137 switch (Opcode) {
138 case Instruction::Shl: return *V1 << *V2;
139 case Instruction::Shr: return *V1 >> *V2;
140 default: return 0;
141 }
142}
143
Chris Lattner1f0049c2003-04-17 19:24:18 +0000144Constant *ConstantFoldGetElementPtr(const Constant *C,
145 const std::vector<Constant*> &IdxList) {
146 if (IdxList.size() == 0 ||
147 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
148 return const_cast<Constant*>(C);
149
150 // If C is null and all idx's are null, return null of the right type.
151
152 // FIXME: Implement folding of GEP constant exprs the same as instcombine does
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000153
154 // Implement folding of:
155 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
156 // long 0, long 0)
157 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
158 //
159 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
160 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1)
161 if (const PointerType *SPT =
162 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
163 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
164 if (const ArrayType *CAT =
165 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
166 if (CAT->getElementType() == SAT->getElementType())
167 return ConstantExpr::getGetElementPtr(
168 (Constant*)cast<Constant>(CE->getOperand(0)), IdxList);
Chris Lattner1f0049c2003-04-17 19:24:18 +0000169 return 0;
170}
171
Chris Lattner9f307732002-05-06 17:54:27 +0000172
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173//===----------------------------------------------------------------------===//
174// TemplateRules Class
175//===----------------------------------------------------------------------===//
176//
177// TemplateRules - Implement a subclass of ConstRules that provides all
178// operations as noops. All other rules classes inherit from this class so
179// that if functionality is needed in the future, it can simply be added here
180// and to ConstRules without changing anything else...
181//
182// This class also provides subclasses with typesafe implementations of methods
183// so that don't have to do type casting.
184//
185template<class ArgType, class SubClassName>
186class TemplateRules : public ConstRules {
187
188 //===--------------------------------------------------------------------===//
189 // Redirecting functions that cast to the appropriate types
190 //===--------------------------------------------------------------------===//
191
Chris Lattnere87f65e2002-07-30 16:24:28 +0000192 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
194 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000195 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
197 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000198 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000199 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
200 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000201 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000202 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
203 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000204 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000205 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
206 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000207 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
208 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
209 }
210 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
211 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
212 }
213 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
214 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
215 }
216 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000217 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
218 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000219 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000220 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
221 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000222
Chris Lattner3462ae32001-12-03 22:26:30 +0000223 virtual ConstantBool *lessthan(const Constant *V1,
224 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
226 }
227
Chris Lattner55406842001-07-21 19:10:49 +0000228 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000229 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000230 return SubClassName::CastToBool((const ArgType*)V);
231 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000232 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000233 return SubClassName::CastToSByte((const ArgType*)V);
234 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000235 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000236 return SubClassName::CastToUByte((const ArgType*)V);
237 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000238 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000239 return SubClassName::CastToShort((const ArgType*)V);
240 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000241 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000242 return SubClassName::CastToUShort((const ArgType*)V);
243 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000244 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000245 return SubClassName::CastToInt((const ArgType*)V);
246 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000247 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000248 return SubClassName::CastToUInt((const ArgType*)V);
249 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000250 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000251 return SubClassName::CastToLong((const ArgType*)V);
252 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000253 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000254 return SubClassName::CastToULong((const ArgType*)V);
255 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000256 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000257 return SubClassName::CastToFloat((const ArgType*)V);
258 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000259 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000260 return SubClassName::CastToDouble((const ArgType*)V);
261 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000262 virtual Constant *castToPointer(const Constant *V,
263 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000264 return SubClassName::CastToPointer((const ArgType*)V, Ty);
265 }
Chris Lattner55406842001-07-21 19:10:49 +0000266
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267 //===--------------------------------------------------------------------===//
268 // Default "noop" implementations
269 //===--------------------------------------------------------------------===//
270
Chris Lattnere87f65e2002-07-30 16:24:28 +0000271 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
272 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
273 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
274 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
275 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
276 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
277 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
278 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
279 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
280 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
281 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000282 return 0;
283 }
Chris Lattner55406842001-07-21 19:10:49 +0000284
285 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000286 static ConstantBool *CastToBool (const Constant *V) { return 0; }
287 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
288 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
289 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
290 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
291 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
292 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
293 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
294 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
295 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
296 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000297 static Constant *CastToPointer(const Constant *,
298 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299};
300
301
302
303//===----------------------------------------------------------------------===//
304// EmptyRules Class
305//===----------------------------------------------------------------------===//
306//
307// EmptyRules provides a concrete base class of ConstRules that does nothing
308//
Chris Lattner3462ae32001-12-03 22:26:30 +0000309struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000310};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311
312
313
314//===----------------------------------------------------------------------===//
315// BoolRules Class
316//===----------------------------------------------------------------------===//
317//
318// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
319//
Chris Lattner3462ae32001-12-03 22:26:30 +0000320struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321
Chris Lattner07507a42002-09-03 20:09:49 +0000322 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
323 return ConstantBool::get(V1->getValue() < V2->getValue());
324 }
325
Chris Lattnere87f65e2002-07-30 16:24:28 +0000326 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
327 return ConstantBool::get(V1->getValue() & V2->getValue());
328 }
329
330 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000331 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332 }
333
Chris Lattnere87f65e2002-07-30 16:24:28 +0000334 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
335 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000336 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000337};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338
339
340//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000341// PointerRules Class
342//===----------------------------------------------------------------------===//
343//
344// PointerRules provides a concrete base class of ConstRules for pointer types
345//
Chris Lattner3462ae32001-12-03 22:26:30 +0000346struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000347 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000348 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000349 return 0; // Can't const prop other types of pointers
350 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000351 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000352 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000353 return 0; // Can't const prop other types of pointers
354 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000355 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000356 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000357 return 0; // Can't const prop other types of pointers
358 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000359 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000360 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000361 return 0; // Can't const prop other types of pointers
362 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000363 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000364 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000365 return 0; // Can't const prop other types of pointers
366 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000367 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000368 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000369 return 0; // Can't const prop other types of pointers
370 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000371 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000372 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000373 return 0; // Can't const prop other types of pointers
374 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000375 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000376 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000377 return 0; // Can't const prop other types of pointers
378 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000379 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000380 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000381 return 0; // Can't const prop other types of pointers
382 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000383 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000384 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000385 return 0; // Can't const prop other types of pointers
386 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000387 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000388 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000389 return 0; // Can't const prop other types of pointers
390 }
391
Chris Lattner1f0049c2003-04-17 19:24:18 +0000392 static Constant *CastToPointer(const ConstantPointer *V,
393 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000394 if (V->getType() == PTy)
395 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000396 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000397 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000398 return 0; // Can't const prop other types of pointers
399 }
400};
401
402
403//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000404// DirectRules Class
405//===----------------------------------------------------------------------===//
406//
407// DirectRules provides a concrete base classes of ConstRules for a variety of
408// different types. This allows the C++ compiler to automatically generate our
409// constant handling operations in a typesafe and accurate manner.
410//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000411template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
412struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000413 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
414 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
415 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000416 }
417
Chris Lattnere87f65e2002-07-30 16:24:28 +0000418 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
419 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
420 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000421 }
422
Chris Lattnere87f65e2002-07-30 16:24:28 +0000423 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
424 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
425 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000426 }
427
Chris Lattnere87f65e2002-07-30 16:24:28 +0000428 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000429 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000430 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
431 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000432 }
433
Chris Lattnere87f65e2002-07-30 16:24:28 +0000434 static ConstantBool *LessThan(const ConstantClass *V1,
435 const ConstantClass *V2) {
436 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
437 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438 }
Chris Lattner55406842001-07-21 19:10:49 +0000439
Chris Lattner1f0049c2003-04-17 19:24:18 +0000440 static Constant *CastToPointer(const ConstantClass *V,
441 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000442 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000443 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000444 return 0; // Can't const prop other types of pointers
445 }
446
Chris Lattner55406842001-07-21 19:10:49 +0000447 // Casting operators. ick
448#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000449 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000450 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000451 }
452
Chris Lattner3462ae32001-12-03 22:26:30 +0000453 DEF_CAST(Bool , ConstantBool, bool)
454 DEF_CAST(SByte , ConstantSInt, signed char)
455 DEF_CAST(UByte , ConstantUInt, unsigned char)
456 DEF_CAST(Short , ConstantSInt, signed short)
457 DEF_CAST(UShort, ConstantUInt, unsigned short)
458 DEF_CAST(Int , ConstantSInt, signed int)
459 DEF_CAST(UInt , ConstantUInt, unsigned int)
460 DEF_CAST(Long , ConstantSInt, int64_t)
461 DEF_CAST(ULong , ConstantUInt, uint64_t)
462 DEF_CAST(Float , ConstantFP , float)
463 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000464#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465};
466
Chris Lattner62af86e2002-05-03 20:09:52 +0000467
468//===----------------------------------------------------------------------===//
469// DirectIntRules Class
470//===----------------------------------------------------------------------===//
471//
472// DirectIntRules provides implementations of functions that are valid on
473// integer types, but not all types in general.
474//
475template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000476struct DirectIntRules
477 : public DirectRules<ConstantClass, BuiltinType, Ty,
478 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000479
Chris Lattner268916262003-05-12 15:26:25 +0000480 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
481 if (V2->isNullValue()) return 0;
482 if (V2->isAllOnesValue() && // MIN_INT / -1
483 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
484 return 0;
485 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
486 return ConstantClass::get(*Ty, R);
487 }
488
Chris Lattnere87f65e2002-07-30 16:24:28 +0000489 static Constant *Rem(const ConstantClass *V1,
490 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000491 if (V2->isNullValue()) return 0; // X / 0
492 if (V2->isAllOnesValue() && // MIN_INT / -1
493 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
494 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000495 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
496 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000497 }
Chris Lattner6670d862002-05-06 03:00:54 +0000498
Chris Lattnere87f65e2002-07-30 16:24:28 +0000499 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
500 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
501 return ConstantClass::get(*Ty, R);
502 }
503 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
504 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
505 return ConstantClass::get(*Ty, R);
506 }
507 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
508 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
509 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000510 }
511
Chris Lattnere87f65e2002-07-30 16:24:28 +0000512 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
513 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
514 return ConstantClass::get(*Ty, R);
515 }
516
517 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
518 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
519 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000520 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000521};
522
523
524//===----------------------------------------------------------------------===//
525// DirectFPRules Class
526//===----------------------------------------------------------------------===//
527//
528// DirectFPRules provides implementations of functions that are valid on
529// floating point types, but not all types in general.
530//
531template <class ConstantClass, class BuiltinType, Type **Ty>
532struct DirectFPRules
533 : public DirectRules<ConstantClass, BuiltinType, Ty,
534 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000535 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000536 if (V2->isNullValue()) return 0;
537 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
538 (BuiltinType)V2->getValue());
539 return ConstantClass::get(*Ty, Result);
540 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000541};
542
Chris Lattner2f7c9632001-06-06 20:29:01 +0000543//===----------------------------------------------------------------------===//
544// DirectRules Subclasses
545//===----------------------------------------------------------------------===//
546//
547// Given the DirectRules class we can now implement lots of types with little
548// code. Thank goodness C++ compilers are great at stomping out layers of
549// templates... can you imagine having to do this all by hand? (/me is lazy :)
550//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551
552// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000553// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000554//
Chris Lattner61607ee2001-09-09 21:01:20 +0000555Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
556 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000557 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000558
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000560 case Type::BoolTyID: return new BoolRules();
561 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000562 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000563 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000564 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000565 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000566 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000567 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000568 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000569 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000570 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000571 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000572 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000573 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000574 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000575 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000576 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000577 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000578 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000579 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000580 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000581 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000582 default:
583 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585}
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000586
587ConstRules *ConstRules::getConstantExprRules() {
588 static EmptyRules CERules;
589 return &CERules;
590}