blob: 956397701ff27c87e6ebb655fc9b011698804698 [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 Lattner8acf3462003-05-27 19:16:07 +00009#include "llvm/InstrTypes.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000010#include "llvm/DerivedTypes.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000011#include <cmath>
Chris Lattnerd42d4922001-06-30 04:36:40 +000012
Chris Lattner61607ee2001-09-09 21:01:20 +000013AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
14 &ConstRules::find));
15
Chris Lattner9f307732002-05-06 17:54:27 +000016// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
17// If successful, the constant result is returned, if not, null is returned.
18//
19Constant *ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000020 if (PHINode *PN = dyn_cast<PHINode>(I)) {
21 if (PN->getNumIncomingValues() == 0)
22 return Constant::getNullValue(PN->getType());
23
24 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
25 if (Result == 0) return 0;
26
27 // Handle PHI nodes specially here...
28 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
29 if (PN->getIncomingValue(i) != Result)
30 return 0; // Not all the same incoming constants...
31
32 // If we reach here, all incoming values are the same constant.
33 return Result;
34 }
35
Chris Lattner9f307732002-05-06 17:54:27 +000036 Constant *Op0 = 0;
37 Constant *Op1 = 0;
38
39 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
40 Op0 = dyn_cast<Constant>(I->getOperand(0));
41 if (Op0 == 0) return 0; // Not a constant?, can't fold
42
43 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
44 Op1 = dyn_cast<Constant>(I->getOperand(1));
45 if (Op1 == 0) return 0; // Not a constant?, can't fold
46 }
47 }
48
Chris Lattner8acf3462003-05-27 19:16:07 +000049 if (isa<BinaryOperator>(I))
50 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
51
Chris Lattner9f307732002-05-06 17:54:27 +000052 switch (I->getOpcode()) {
53 case Instruction::Cast:
Chris Lattner8acf3462003-05-27 19:16:07 +000054 return ConstantExpr::getCast(Op0, I->getType());
55 case Instruction::Shl:
56 case Instruction::Shr:
57 return ConstantExpr::getShift(I->getOpcode(), Op0, Op1);
Chris Lattner1f0049c2003-04-17 19:24:18 +000058 case Instruction::GetElementPtr: {
59 std::vector<Constant*> IdxList;
60 IdxList.reserve(I->getNumOperands()-1);
61 if (Op1) IdxList.push_back(Op1);
62 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
63 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
64 IdxList.push_back(C);
65 else
66 return 0; // Non-constant operand
Chris Lattner8acf3462003-05-27 19:16:07 +000067 return ConstantExpr::getGetElementPtr(Op0, IdxList);
Chris Lattner1f0049c2003-04-17 19:24:18 +000068 }
Chris Lattner9f307732002-05-06 17:54:27 +000069 default:
70 return 0;
71 }
72}
73
Chris Lattner1f0049c2003-04-17 19:24:18 +000074static unsigned getSize(const Type *Ty) {
75 unsigned S = Ty->getPrimitiveSize();
76 return S ? S : 8; // Treat pointers at 8 bytes
77}
78
Chris Lattner9f307732002-05-06 17:54:27 +000079Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000080 if (V->getType() == DestTy) return (Constant*)V;
81
82 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
83 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000084 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000085 // Try to not produce a cast of a cast, which is almost always redundant.
86 if (!Op->getType()->isFloatingPoint() &&
87 !CE->getType()->isFloatingPoint() &&
88 !DestTy->getType()->isFloatingPoint()) {
89 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
90 unsigned S3 = getSize(DestTy);
91 if (Op->getType() == DestTy && S3 >= S2)
92 return Op;
93 if (S1 >= S2 && S2 >= S3)
94 return ConstantExpr::getCast(Op, DestTy);
95 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
96 return ConstantExpr::getCast(Op, DestTy);
97 }
Chris Lattner941b06b2003-08-21 19:45:55 +000098 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
99 // If all of the indexes in the GEP are null values, there is no pointer
100 // adjustment going on. We might as well cast the source pointer.
101 bool isAllNull = true;
102 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
103 if (!CE->getOperand(i)->isNullValue()) {
104 isAllNull = false;
105 break;
106 }
107 if (isAllNull)
108 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +0000109 }
110
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000111 return ConstRules::get(*V, *V)->castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000112}
113
Chris Lattner9f307732002-05-06 17:54:27 +0000114Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
115 const Constant *V2) {
116 switch (Opcode) {
117 case Instruction::Add: return *V1 + *V2;
118 case Instruction::Sub: return *V1 - *V2;
119 case Instruction::Mul: return *V1 * *V2;
120 case Instruction::Div: return *V1 / *V2;
121 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000122 case Instruction::And: return *V1 & *V2;
123 case Instruction::Or: return *V1 | *V2;
124 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +0000125
126 case Instruction::SetEQ: return *V1 == *V2;
127 case Instruction::SetNE: return *V1 != *V2;
128 case Instruction::SetLE: return *V1 <= *V2;
129 case Instruction::SetGE: return *V1 >= *V2;
130 case Instruction::SetLT: return *V1 < *V2;
131 case Instruction::SetGT: return *V1 > *V2;
132 }
133 return 0;
134}
135
136Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
137 const Constant *V2) {
138 switch (Opcode) {
139 case Instruction::Shl: return *V1 << *V2;
140 case Instruction::Shr: return *V1 >> *V2;
141 default: return 0;
142 }
143}
144
Chris Lattner1f0049c2003-04-17 19:24:18 +0000145Constant *ConstantFoldGetElementPtr(const Constant *C,
146 const std::vector<Constant*> &IdxList) {
147 if (IdxList.size() == 0 ||
148 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
149 return const_cast<Constant*>(C);
150
Chris Lattner941b06b2003-08-21 19:45:55 +0000151 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000152
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000153
Chris Lattner4ede64e2003-06-26 05:22:45 +0000154 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000155 // Combine Indices - If the source pointer to this getelementptr instruction
156 // is a getelementptr instruction, combine the indices of the two
157 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000158 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000159 if (CE->getOpcode() == Instruction::GetElementPtr) {
160 if (CE->getOperand(CE->getNumOperands()-1)->getType() == Type::LongTy) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000161 std::vector<Constant*> NewIndices;
162 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000163 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000164 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000165
166 // Add the last index of the source with the first index of the new GEP.
167 Constant *Combined =
168 ConstantExpr::get(Instruction::Add, IdxList[0],
169 CE->getOperand(CE->getNumOperands()-1));
170
171 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000172 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
173 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
174 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000175 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000176
177 // Implement folding of:
178 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
179 // long 0, long 0)
180 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
181 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000182 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
183 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000184 if (const PointerType *SPT =
185 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
186 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
187 if (const ArrayType *CAT =
188 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
189 if (CAT->getElementType() == SAT->getElementType())
190 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000191 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000192 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000193 return 0;
194}
195
Chris Lattner9f307732002-05-06 17:54:27 +0000196
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197//===----------------------------------------------------------------------===//
198// TemplateRules Class
199//===----------------------------------------------------------------------===//
200//
201// TemplateRules - Implement a subclass of ConstRules that provides all
202// operations as noops. All other rules classes inherit from this class so
203// that if functionality is needed in the future, it can simply be added here
204// and to ConstRules without changing anything else...
205//
206// This class also provides subclasses with typesafe implementations of methods
207// so that don't have to do type casting.
208//
209template<class ArgType, class SubClassName>
210class TemplateRules : public ConstRules {
211
212 //===--------------------------------------------------------------------===//
213 // Redirecting functions that cast to the appropriate types
214 //===--------------------------------------------------------------------===//
215
Chris Lattnere87f65e2002-07-30 16:24:28 +0000216 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
218 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000219 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
221 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000222 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000223 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
224 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000225 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000226 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
227 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000228 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000229 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
230 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000231 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
232 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
233 }
234 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
235 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
236 }
237 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
238 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
239 }
240 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000241 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
242 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000243 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000244 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
245 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000246
Chris Lattner3462ae32001-12-03 22:26:30 +0000247 virtual ConstantBool *lessthan(const Constant *V1,
248 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
250 }
251
Chris Lattner55406842001-07-21 19:10:49 +0000252 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000253 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000254 return SubClassName::CastToBool((const ArgType*)V);
255 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000256 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000257 return SubClassName::CastToSByte((const ArgType*)V);
258 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000259 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000260 return SubClassName::CastToUByte((const ArgType*)V);
261 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000263 return SubClassName::CastToShort((const ArgType*)V);
264 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000265 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000266 return SubClassName::CastToUShort((const ArgType*)V);
267 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000268 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000269 return SubClassName::CastToInt((const ArgType*)V);
270 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000271 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000272 return SubClassName::CastToUInt((const ArgType*)V);
273 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000274 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000275 return SubClassName::CastToLong((const ArgType*)V);
276 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000277 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000278 return SubClassName::CastToULong((const ArgType*)V);
279 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000280 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000281 return SubClassName::CastToFloat((const ArgType*)V);
282 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000283 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000284 return SubClassName::CastToDouble((const ArgType*)V);
285 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000286 virtual Constant *castToPointer(const Constant *V,
287 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000288 return SubClassName::CastToPointer((const ArgType*)V, Ty);
289 }
Chris Lattner55406842001-07-21 19:10:49 +0000290
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291 //===--------------------------------------------------------------------===//
292 // Default "noop" implementations
293 //===--------------------------------------------------------------------===//
294
Chris Lattnere87f65e2002-07-30 16:24:28 +0000295 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
296 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
297 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
298 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
299 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
300 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
301 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
302 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
303 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
304 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
305 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306 return 0;
307 }
Chris Lattner55406842001-07-21 19:10:49 +0000308
309 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000310 static ConstantBool *CastToBool (const Constant *V) { return 0; }
311 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
312 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
313 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
314 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
315 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
316 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
317 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
318 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
319 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
320 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000321 static Constant *CastToPointer(const Constant *,
322 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000323};
324
325
326
327//===----------------------------------------------------------------------===//
328// EmptyRules Class
329//===----------------------------------------------------------------------===//
330//
331// EmptyRules provides a concrete base class of ConstRules that does nothing
332//
Chris Lattner3462ae32001-12-03 22:26:30 +0000333struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000334};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000335
336
337
338//===----------------------------------------------------------------------===//
339// BoolRules Class
340//===----------------------------------------------------------------------===//
341//
342// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
343//
Chris Lattner3462ae32001-12-03 22:26:30 +0000344struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345
Chris Lattner07507a42002-09-03 20:09:49 +0000346 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
347 return ConstantBool::get(V1->getValue() < V2->getValue());
348 }
349
Chris Lattnere87f65e2002-07-30 16:24:28 +0000350 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
351 return ConstantBool::get(V1->getValue() & V2->getValue());
352 }
353
354 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000355 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000356 }
357
Chris Lattnere87f65e2002-07-30 16:24:28 +0000358 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
359 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000361
362 // Casting operators. ick
363#define DEF_CAST(TYPE, CLASS, CTYPE) \
364 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
365 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
366 }
367
368 DEF_CAST(Bool , ConstantBool, bool)
369 DEF_CAST(SByte , ConstantSInt, signed char)
370 DEF_CAST(UByte , ConstantUInt, unsigned char)
371 DEF_CAST(Short , ConstantSInt, signed short)
372 DEF_CAST(UShort, ConstantUInt, unsigned short)
373 DEF_CAST(Int , ConstantSInt, signed int)
374 DEF_CAST(UInt , ConstantUInt, unsigned int)
375 DEF_CAST(Long , ConstantSInt, int64_t)
376 DEF_CAST(ULong , ConstantUInt, uint64_t)
377 DEF_CAST(Float , ConstantFP , float)
378 DEF_CAST(Double, ConstantFP , double)
379#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000380};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000381
382
383//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000384// PointerRules Class
385//===----------------------------------------------------------------------===//
386//
387// PointerRules provides a concrete base class of ConstRules for pointer types
388//
Chris Lattner3462ae32001-12-03 22:26:30 +0000389struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000390 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000391 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000392 return 0; // Can't const prop other types of pointers
393 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000394 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000395 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000396 return 0; // Can't const prop other types of pointers
397 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000398 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000399 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000400 return 0; // Can't const prop other types of pointers
401 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000402 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000403 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000404 return 0; // Can't const prop other types of pointers
405 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000406 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000407 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000408 return 0; // Can't const prop other types of pointers
409 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000410 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000411 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000412 return 0; // Can't const prop other types of pointers
413 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000414 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000415 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000416 return 0; // Can't const prop other types of pointers
417 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000418 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000419 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000420 return 0; // Can't const prop other types of pointers
421 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000422 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000423 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000424 return 0; // Can't const prop other types of pointers
425 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000426 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000427 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000428 return 0; // Can't const prop other types of pointers
429 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000430 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000431 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000432 return 0; // Can't const prop other types of pointers
433 }
434
Chris Lattner1f0049c2003-04-17 19:24:18 +0000435 static Constant *CastToPointer(const ConstantPointer *V,
436 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000437 if (V->getType() == PTy)
438 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000439 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000440 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000441 return 0; // Can't const prop other types of pointers
442 }
443};
444
445
446//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447// DirectRules Class
448//===----------------------------------------------------------------------===//
449//
450// DirectRules provides a concrete base classes of ConstRules for a variety of
451// different types. This allows the C++ compiler to automatically generate our
452// constant handling operations in a typesafe and accurate manner.
453//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000454template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
455struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000456 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
457 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
458 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000459 }
460
Chris Lattnere87f65e2002-07-30 16:24:28 +0000461 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
462 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
463 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464 }
465
Chris Lattnere87f65e2002-07-30 16:24:28 +0000466 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
467 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
468 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000469 }
470
Chris Lattnere87f65e2002-07-30 16:24:28 +0000471 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000472 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000473 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
474 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000475 }
476
Chris Lattnere87f65e2002-07-30 16:24:28 +0000477 static ConstantBool *LessThan(const ConstantClass *V1,
478 const ConstantClass *V2) {
479 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
480 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000481 }
Chris Lattner55406842001-07-21 19:10:49 +0000482
Chris Lattner1f0049c2003-04-17 19:24:18 +0000483 static Constant *CastToPointer(const ConstantClass *V,
484 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000485 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000486 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000487 return 0; // Can't const prop other types of pointers
488 }
489
Chris Lattner55406842001-07-21 19:10:49 +0000490 // Casting operators. ick
491#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000492 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000493 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000494 }
495
Chris Lattner3462ae32001-12-03 22:26:30 +0000496 DEF_CAST(Bool , ConstantBool, bool)
497 DEF_CAST(SByte , ConstantSInt, signed char)
498 DEF_CAST(UByte , ConstantUInt, unsigned char)
499 DEF_CAST(Short , ConstantSInt, signed short)
500 DEF_CAST(UShort, ConstantUInt, unsigned short)
501 DEF_CAST(Int , ConstantSInt, signed int)
502 DEF_CAST(UInt , ConstantUInt, unsigned int)
503 DEF_CAST(Long , ConstantSInt, int64_t)
504 DEF_CAST(ULong , ConstantUInt, uint64_t)
505 DEF_CAST(Float , ConstantFP , float)
506 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000507#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000508};
509
Chris Lattner62af86e2002-05-03 20:09:52 +0000510
511//===----------------------------------------------------------------------===//
512// DirectIntRules Class
513//===----------------------------------------------------------------------===//
514//
515// DirectIntRules provides implementations of functions that are valid on
516// integer types, but not all types in general.
517//
518template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000519struct DirectIntRules
520 : public DirectRules<ConstantClass, BuiltinType, Ty,
521 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000522
Chris Lattner268916262003-05-12 15:26:25 +0000523 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
524 if (V2->isNullValue()) return 0;
525 if (V2->isAllOnesValue() && // MIN_INT / -1
526 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
527 return 0;
528 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
529 return ConstantClass::get(*Ty, R);
530 }
531
Chris Lattnere87f65e2002-07-30 16:24:28 +0000532 static Constant *Rem(const ConstantClass *V1,
533 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000534 if (V2->isNullValue()) return 0; // X / 0
535 if (V2->isAllOnesValue() && // MIN_INT / -1
536 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
537 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000538 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
539 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000540 }
Chris Lattner6670d862002-05-06 03:00:54 +0000541
Chris Lattnere87f65e2002-07-30 16:24:28 +0000542 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
543 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
544 return ConstantClass::get(*Ty, R);
545 }
546 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
547 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
548 return ConstantClass::get(*Ty, R);
549 }
550 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
551 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
552 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000553 }
554
Chris Lattnere87f65e2002-07-30 16:24:28 +0000555 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
556 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
557 return ConstantClass::get(*Ty, R);
558 }
559
560 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
561 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
562 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000563 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000564};
565
566
567//===----------------------------------------------------------------------===//
568// DirectFPRules Class
569//===----------------------------------------------------------------------===//
570//
571// DirectFPRules provides implementations of functions that are valid on
572// floating point types, but not all types in general.
573//
574template <class ConstantClass, class BuiltinType, Type **Ty>
575struct DirectFPRules
576 : public DirectRules<ConstantClass, BuiltinType, Ty,
577 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000578 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000579 if (V2->isNullValue()) return 0;
580 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
581 (BuiltinType)V2->getValue());
582 return ConstantClass::get(*Ty, Result);
583 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000584};
585
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586//===----------------------------------------------------------------------===//
587// DirectRules Subclasses
588//===----------------------------------------------------------------------===//
589//
590// Given the DirectRules class we can now implement lots of types with little
591// code. Thank goodness C++ compilers are great at stomping out layers of
592// templates... can you imagine having to do this all by hand? (/me is lazy :)
593//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594
595// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000596// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000597//
Chris Lattner61607ee2001-09-09 21:01:20 +0000598Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
599 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000600 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000601
Chris Lattner2f7c9632001-06-06 20:29:01 +0000602 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000603 case Type::BoolTyID: return new BoolRules();
604 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000605 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000606 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000607 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000608 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000609 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000610 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000611 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000612 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000613 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000614 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000615 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000616 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000617 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000618 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000619 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000620 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000621 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000622 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000623 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000624 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000625 default:
626 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000627 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000628}
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000629
630ConstRules *ConstRules::getConstantExprRules() {
631 static EmptyRules CERules;
632 return &CERules;
633}