blob: 0edf6a76a6239c4b51d43d737b503b696230f432 [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 }
98 }
99
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000100 return ConstRules::get(*V, *V)->castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000101}
102
Chris Lattner9f307732002-05-06 17:54:27 +0000103Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
104 const Constant *V2) {
105 switch (Opcode) {
106 case Instruction::Add: return *V1 + *V2;
107 case Instruction::Sub: return *V1 - *V2;
108 case Instruction::Mul: return *V1 * *V2;
109 case Instruction::Div: return *V1 / *V2;
110 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000111 case Instruction::And: return *V1 & *V2;
112 case Instruction::Or: return *V1 | *V2;
113 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +0000114
115 case Instruction::SetEQ: return *V1 == *V2;
116 case Instruction::SetNE: return *V1 != *V2;
117 case Instruction::SetLE: return *V1 <= *V2;
118 case Instruction::SetGE: return *V1 >= *V2;
119 case Instruction::SetLT: return *V1 < *V2;
120 case Instruction::SetGT: return *V1 > *V2;
121 }
122 return 0;
123}
124
125Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
126 const Constant *V2) {
127 switch (Opcode) {
128 case Instruction::Shl: return *V1 << *V2;
129 case Instruction::Shr: return *V1 >> *V2;
130 default: return 0;
131 }
132}
133
Chris Lattner1f0049c2003-04-17 19:24:18 +0000134Constant *ConstantFoldGetElementPtr(const Constant *C,
135 const std::vector<Constant*> &IdxList) {
136 if (IdxList.size() == 0 ||
137 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
138 return const_cast<Constant*>(C);
139
140 // If C is null and all idx's are null, return null of the right type.
141
142 // FIXME: Implement folding of GEP constant exprs the same as instcombine does
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000143
Chris Lattner4ede64e2003-06-26 05:22:45 +0000144 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
145 // Implement folding of:
146 // void ()** getelementptr (%struct..TorRec* getelementptr
147 // ([N x %struct..TorRec]* %llvm.global_dtors, long 0, long 0),
148 // long 0, ubyte 1)
149 // Into:
150 // %struct..TorRec* getelementptr ([N x %struct..TorRec]*
151 // %llvm.global_dtors, long 0, long 0, ubyte 1)
152 //
153 if (CE->getOpcode() == Instruction::GetElementPtr)
154 if (IdxList[0] == Constant::getNullValue(Type::LongTy)) {
155 std::vector<Constant*> NewIndices;
156 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
157 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
158 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
159 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
160 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
161 }
162
163 // Implement folding of:
164 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
165 // long 0, long 0)
166 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
167 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000168 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
169 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000170 if (const PointerType *SPT =
171 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
172 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
173 if (const ArrayType *CAT =
174 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
175 if (CAT->getElementType() == SAT->getElementType())
176 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000177 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000178 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000179 return 0;
180}
181
Chris Lattner9f307732002-05-06 17:54:27 +0000182
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183//===----------------------------------------------------------------------===//
184// TemplateRules Class
185//===----------------------------------------------------------------------===//
186//
187// TemplateRules - Implement a subclass of ConstRules that provides all
188// operations as noops. All other rules classes inherit from this class so
189// that if functionality is needed in the future, it can simply be added here
190// and to ConstRules without changing anything else...
191//
192// This class also provides subclasses with typesafe implementations of methods
193// so that don't have to do type casting.
194//
195template<class ArgType, class SubClassName>
196class TemplateRules : public ConstRules {
197
198 //===--------------------------------------------------------------------===//
199 // Redirecting functions that cast to the appropriate types
200 //===--------------------------------------------------------------------===//
201
Chris Lattnere87f65e2002-07-30 16:24:28 +0000202 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
204 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000205 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
207 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000208 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000209 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
210 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000211 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000212 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
213 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000214 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000215 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
216 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000217 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
218 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
219 }
220 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
221 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
222 }
223 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
224 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
225 }
226 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000227 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
228 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000229 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000230 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
231 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000232
Chris Lattner3462ae32001-12-03 22:26:30 +0000233 virtual ConstantBool *lessthan(const Constant *V1,
234 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
236 }
237
Chris Lattner55406842001-07-21 19:10:49 +0000238 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000239 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000240 return SubClassName::CastToBool((const ArgType*)V);
241 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000242 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000243 return SubClassName::CastToSByte((const ArgType*)V);
244 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000245 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000246 return SubClassName::CastToUByte((const ArgType*)V);
247 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000248 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000249 return SubClassName::CastToShort((const ArgType*)V);
250 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000251 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000252 return SubClassName::CastToUShort((const ArgType*)V);
253 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000254 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000255 return SubClassName::CastToInt((const ArgType*)V);
256 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000257 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000258 return SubClassName::CastToUInt((const ArgType*)V);
259 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000260 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000261 return SubClassName::CastToLong((const ArgType*)V);
262 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000263 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000264 return SubClassName::CastToULong((const ArgType*)V);
265 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000266 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000267 return SubClassName::CastToFloat((const ArgType*)V);
268 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000269 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000270 return SubClassName::CastToDouble((const ArgType*)V);
271 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000272 virtual Constant *castToPointer(const Constant *V,
273 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000274 return SubClassName::CastToPointer((const ArgType*)V, Ty);
275 }
Chris Lattner55406842001-07-21 19:10:49 +0000276
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 //===--------------------------------------------------------------------===//
278 // Default "noop" implementations
279 //===--------------------------------------------------------------------===//
280
Chris Lattnere87f65e2002-07-30 16:24:28 +0000281 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
282 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
283 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
284 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
285 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
286 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
287 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
288 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
289 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
290 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
291 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000292 return 0;
293 }
Chris Lattner55406842001-07-21 19:10:49 +0000294
295 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000296 static ConstantBool *CastToBool (const Constant *V) { return 0; }
297 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
298 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
299 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
300 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
301 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
302 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
303 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
304 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
305 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
306 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000307 static Constant *CastToPointer(const Constant *,
308 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000309};
310
311
312
313//===----------------------------------------------------------------------===//
314// EmptyRules Class
315//===----------------------------------------------------------------------===//
316//
317// EmptyRules provides a concrete base class of ConstRules that does nothing
318//
Chris Lattner3462ae32001-12-03 22:26:30 +0000319struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000320};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321
322
323
324//===----------------------------------------------------------------------===//
325// BoolRules Class
326//===----------------------------------------------------------------------===//
327//
328// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
329//
Chris Lattner3462ae32001-12-03 22:26:30 +0000330struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331
Chris Lattner07507a42002-09-03 20:09:49 +0000332 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
333 return ConstantBool::get(V1->getValue() < V2->getValue());
334 }
335
Chris Lattnere87f65e2002-07-30 16:24:28 +0000336 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
337 return ConstantBool::get(V1->getValue() & V2->getValue());
338 }
339
340 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000341 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000342 }
343
Chris Lattnere87f65e2002-07-30 16:24:28 +0000344 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
345 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000346 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000347};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000348
349
350//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000351// PointerRules Class
352//===----------------------------------------------------------------------===//
353//
354// PointerRules provides a concrete base class of ConstRules for pointer types
355//
Chris Lattner3462ae32001-12-03 22:26:30 +0000356struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000357 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000358 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000359 return 0; // Can't const prop other types of pointers
360 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000361 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000362 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000363 return 0; // Can't const prop other types of pointers
364 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000365 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000366 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000367 return 0; // Can't const prop other types of pointers
368 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000369 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000370 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000371 return 0; // Can't const prop other types of pointers
372 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000373 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000374 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000375 return 0; // Can't const prop other types of pointers
376 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000377 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000378 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000379 return 0; // Can't const prop other types of pointers
380 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000381 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000382 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000383 return 0; // Can't const prop other types of pointers
384 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000385 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000386 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000387 return 0; // Can't const prop other types of pointers
388 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000389 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000390 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000391 return 0; // Can't const prop other types of pointers
392 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000393 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000394 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000395 return 0; // Can't const prop other types of pointers
396 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000397 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000398 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000399 return 0; // Can't const prop other types of pointers
400 }
401
Chris Lattner1f0049c2003-04-17 19:24:18 +0000402 static Constant *CastToPointer(const ConstantPointer *V,
403 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000404 if (V->getType() == PTy)
405 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000406 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000407 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000408 return 0; // Can't const prop other types of pointers
409 }
410};
411
412
413//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000414// DirectRules Class
415//===----------------------------------------------------------------------===//
416//
417// DirectRules provides a concrete base classes of ConstRules for a variety of
418// different types. This allows the C++ compiler to automatically generate our
419// constant handling operations in a typesafe and accurate manner.
420//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000421template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
422struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000423 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
424 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
425 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426 }
427
Chris Lattnere87f65e2002-07-30 16:24:28 +0000428 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
429 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
430 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431 }
432
Chris Lattnere87f65e2002-07-30 16:24:28 +0000433 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
434 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
435 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000436 }
437
Chris Lattnere87f65e2002-07-30 16:24:28 +0000438 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000439 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000440 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
441 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000442 }
443
Chris Lattnere87f65e2002-07-30 16:24:28 +0000444 static ConstantBool *LessThan(const ConstantClass *V1,
445 const ConstantClass *V2) {
446 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
447 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000448 }
Chris Lattner55406842001-07-21 19:10:49 +0000449
Chris Lattner1f0049c2003-04-17 19:24:18 +0000450 static Constant *CastToPointer(const ConstantClass *V,
451 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000452 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000453 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000454 return 0; // Can't const prop other types of pointers
455 }
456
Chris Lattner55406842001-07-21 19:10:49 +0000457 // Casting operators. ick
458#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000459 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000460 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000461 }
462
Chris Lattner3462ae32001-12-03 22:26:30 +0000463 DEF_CAST(Bool , ConstantBool, bool)
464 DEF_CAST(SByte , ConstantSInt, signed char)
465 DEF_CAST(UByte , ConstantUInt, unsigned char)
466 DEF_CAST(Short , ConstantSInt, signed short)
467 DEF_CAST(UShort, ConstantUInt, unsigned short)
468 DEF_CAST(Int , ConstantSInt, signed int)
469 DEF_CAST(UInt , ConstantUInt, unsigned int)
470 DEF_CAST(Long , ConstantSInt, int64_t)
471 DEF_CAST(ULong , ConstantUInt, uint64_t)
472 DEF_CAST(Float , ConstantFP , float)
473 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000474#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000475};
476
Chris Lattner62af86e2002-05-03 20:09:52 +0000477
478//===----------------------------------------------------------------------===//
479// DirectIntRules Class
480//===----------------------------------------------------------------------===//
481//
482// DirectIntRules provides implementations of functions that are valid on
483// integer types, but not all types in general.
484//
485template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000486struct DirectIntRules
487 : public DirectRules<ConstantClass, BuiltinType, Ty,
488 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000489
Chris Lattner268916262003-05-12 15:26:25 +0000490 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
491 if (V2->isNullValue()) return 0;
492 if (V2->isAllOnesValue() && // MIN_INT / -1
493 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
494 return 0;
495 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
496 return ConstantClass::get(*Ty, R);
497 }
498
Chris Lattnere87f65e2002-07-30 16:24:28 +0000499 static Constant *Rem(const ConstantClass *V1,
500 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000501 if (V2->isNullValue()) return 0; // X / 0
502 if (V2->isAllOnesValue() && // MIN_INT / -1
503 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
504 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000505 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
506 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000507 }
Chris Lattner6670d862002-05-06 03:00:54 +0000508
Chris Lattnere87f65e2002-07-30 16:24:28 +0000509 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
510 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
511 return ConstantClass::get(*Ty, R);
512 }
513 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
514 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
515 return ConstantClass::get(*Ty, R);
516 }
517 static Constant *Xor(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 }
521
Chris Lattnere87f65e2002-07-30 16:24:28 +0000522 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
523 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
524 return ConstantClass::get(*Ty, R);
525 }
526
527 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
528 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
529 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000530 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000531};
532
533
534//===----------------------------------------------------------------------===//
535// DirectFPRules Class
536//===----------------------------------------------------------------------===//
537//
538// DirectFPRules provides implementations of functions that are valid on
539// floating point types, but not all types in general.
540//
541template <class ConstantClass, class BuiltinType, Type **Ty>
542struct DirectFPRules
543 : public DirectRules<ConstantClass, BuiltinType, Ty,
544 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000545 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000546 if (V2->isNullValue()) return 0;
547 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
548 (BuiltinType)V2->getValue());
549 return ConstantClass::get(*Ty, Result);
550 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000551};
552
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553//===----------------------------------------------------------------------===//
554// DirectRules Subclasses
555//===----------------------------------------------------------------------===//
556//
557// Given the DirectRules class we can now implement lots of types with little
558// code. Thank goodness C++ compilers are great at stomping out layers of
559// templates... can you imagine having to do this all by hand? (/me is lazy :)
560//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561
562// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000563// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000564//
Chris Lattner61607ee2001-09-09 21:01:20 +0000565Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
566 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000567 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000568
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000570 case Type::BoolTyID: return new BoolRules();
571 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000572 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000573 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000574 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000575 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000576 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000577 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000578 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000579 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000580 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000581 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000582 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000583 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000584 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000585 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000586 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000587 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000588 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000589 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000590 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000591 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000592 default:
593 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595}
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000596
597ConstRules *ConstRules::getConstantExprRules() {
598 static EmptyRules CERules;
599 return &CERules;
600}