blob: 32b9ebba742d550404467f542fe3169df82091e0 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the various intrinsic operations, on constant values.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner65b529f2002-04-08 20:18:09 +000014#include "llvm/ConstantHandling.h"
Chris Lattner78a0d422002-05-07 20:44:59 +000015#include "llvm/iPHINode.h"
Chris Lattner8acf3462003-05-27 19:16:07 +000016#include "llvm/InstrTypes.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000017#include "llvm/DerivedTypes.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000018#include <cmath>
Chris Lattnerd42d4922001-06-30 04:36:40 +000019
Chris Lattner61607ee2001-09-09 21:01:20 +000020AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
21 &ConstRules::find));
22
Chris Lattner9f307732002-05-06 17:54:27 +000023// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
24// If successful, the constant result is returned, if not, null is returned.
25//
26Constant *ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000027 if (PHINode *PN = dyn_cast<PHINode>(I)) {
28 if (PN->getNumIncomingValues() == 0)
29 return Constant::getNullValue(PN->getType());
30
31 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
32 if (Result == 0) return 0;
33
34 // Handle PHI nodes specially here...
35 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
36 if (PN->getIncomingValue(i) != Result)
37 return 0; // Not all the same incoming constants...
38
39 // If we reach here, all incoming values are the same constant.
40 return Result;
41 }
42
Chris Lattner9f307732002-05-06 17:54:27 +000043 Constant *Op0 = 0;
44 Constant *Op1 = 0;
45
46 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
47 Op0 = dyn_cast<Constant>(I->getOperand(0));
48 if (Op0 == 0) return 0; // Not a constant?, can't fold
49
50 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
51 Op1 = dyn_cast<Constant>(I->getOperand(1));
52 if (Op1 == 0) return 0; // Not a constant?, can't fold
53 }
54 }
55
Chris Lattner8acf3462003-05-27 19:16:07 +000056 if (isa<BinaryOperator>(I))
57 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
58
Chris Lattner9f307732002-05-06 17:54:27 +000059 switch (I->getOpcode()) {
60 case Instruction::Cast:
Chris Lattner8acf3462003-05-27 19:16:07 +000061 return ConstantExpr::getCast(Op0, I->getType());
62 case Instruction::Shl:
63 case Instruction::Shr:
64 return ConstantExpr::getShift(I->getOpcode(), Op0, Op1);
Chris Lattner1f0049c2003-04-17 19:24:18 +000065 case Instruction::GetElementPtr: {
66 std::vector<Constant*> IdxList;
67 IdxList.reserve(I->getNumOperands()-1);
68 if (Op1) IdxList.push_back(Op1);
69 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
70 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
71 IdxList.push_back(C);
72 else
73 return 0; // Non-constant operand
Chris Lattner8acf3462003-05-27 19:16:07 +000074 return ConstantExpr::getGetElementPtr(Op0, IdxList);
Chris Lattner1f0049c2003-04-17 19:24:18 +000075 }
Chris Lattner9f307732002-05-06 17:54:27 +000076 default:
77 return 0;
78 }
79}
80
Chris Lattner1f0049c2003-04-17 19:24:18 +000081static unsigned getSize(const Type *Ty) {
82 unsigned S = Ty->getPrimitiveSize();
83 return S ? S : 8; // Treat pointers at 8 bytes
84}
85
Chris Lattner9f307732002-05-06 17:54:27 +000086Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000087 if (V->getType() == DestTy) return (Constant*)V;
88
89 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
90 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000091 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000092 // Try to not produce a cast of a cast, which is almost always redundant.
93 if (!Op->getType()->isFloatingPoint() &&
94 !CE->getType()->isFloatingPoint() &&
95 !DestTy->getType()->isFloatingPoint()) {
96 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
97 unsigned S3 = getSize(DestTy);
98 if (Op->getType() == DestTy && S3 >= S2)
99 return Op;
100 if (S1 >= S2 && S2 >= S3)
101 return ConstantExpr::getCast(Op, DestTy);
102 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
103 return ConstantExpr::getCast(Op, DestTy);
104 }
Chris Lattner941b06b2003-08-21 19:45:55 +0000105 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
106 // If all of the indexes in the GEP are null values, there is no pointer
107 // adjustment going on. We might as well cast the source pointer.
108 bool isAllNull = true;
109 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
110 if (!CE->getOperand(i)->isNullValue()) {
111 isAllNull = false;
112 break;
113 }
114 if (isAllNull)
115 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +0000116 }
117
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000118 return ConstRules::get(*V, *V)->castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000119}
120
Chris Lattner9f307732002-05-06 17:54:27 +0000121Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
122 const Constant *V2) {
123 switch (Opcode) {
124 case Instruction::Add: return *V1 + *V2;
125 case Instruction::Sub: return *V1 - *V2;
126 case Instruction::Mul: return *V1 * *V2;
127 case Instruction::Div: return *V1 / *V2;
128 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000129 case Instruction::And: return *V1 & *V2;
130 case Instruction::Or: return *V1 | *V2;
131 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +0000132
133 case Instruction::SetEQ: return *V1 == *V2;
134 case Instruction::SetNE: return *V1 != *V2;
135 case Instruction::SetLE: return *V1 <= *V2;
136 case Instruction::SetGE: return *V1 >= *V2;
137 case Instruction::SetLT: return *V1 < *V2;
138 case Instruction::SetGT: return *V1 > *V2;
139 }
140 return 0;
141}
142
143Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
144 const Constant *V2) {
145 switch (Opcode) {
146 case Instruction::Shl: return *V1 << *V2;
147 case Instruction::Shr: return *V1 >> *V2;
148 default: return 0;
149 }
150}
151
Chris Lattner1f0049c2003-04-17 19:24:18 +0000152Constant *ConstantFoldGetElementPtr(const Constant *C,
153 const std::vector<Constant*> &IdxList) {
154 if (IdxList.size() == 0 ||
155 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
156 return const_cast<Constant*>(C);
157
Chris Lattner941b06b2003-08-21 19:45:55 +0000158 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000159
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000160
Chris Lattner4ede64e2003-06-26 05:22:45 +0000161 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000162 // Combine Indices - If the source pointer to this getelementptr instruction
163 // is a getelementptr instruction, combine the indices of the two
164 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000165 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000166 if (CE->getOpcode() == Instruction::GetElementPtr) {
167 if (CE->getOperand(CE->getNumOperands()-1)->getType() == Type::LongTy) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000168 std::vector<Constant*> NewIndices;
169 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000170 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000171 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000172
173 // Add the last index of the source with the first index of the new GEP.
174 Constant *Combined =
175 ConstantExpr::get(Instruction::Add, IdxList[0],
176 CE->getOperand(CE->getNumOperands()-1));
177
178 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000179 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
180 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
181 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000182 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000183
184 // Implement folding of:
185 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
186 // long 0, long 0)
187 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
188 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000189 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
190 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000191 if (const PointerType *SPT =
192 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
193 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
194 if (const ArrayType *CAT =
195 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
196 if (CAT->getElementType() == SAT->getElementType())
197 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000198 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000199 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000200 return 0;
201}
202
Chris Lattner9f307732002-05-06 17:54:27 +0000203
Chris Lattner2f7c9632001-06-06 20:29:01 +0000204//===----------------------------------------------------------------------===//
205// TemplateRules Class
206//===----------------------------------------------------------------------===//
207//
208// TemplateRules - Implement a subclass of ConstRules that provides all
209// operations as noops. All other rules classes inherit from this class so
210// that if functionality is needed in the future, it can simply be added here
211// and to ConstRules without changing anything else...
212//
213// This class also provides subclasses with typesafe implementations of methods
214// so that don't have to do type casting.
215//
216template<class ArgType, class SubClassName>
217class TemplateRules : public ConstRules {
218
219 //===--------------------------------------------------------------------===//
220 // Redirecting functions that cast to the appropriate types
221 //===--------------------------------------------------------------------===//
222
Chris Lattnere87f65e2002-07-30 16:24:28 +0000223 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
225 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000226 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000227 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
228 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000229 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000230 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
231 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000232 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000233 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
234 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000235 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000236 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
237 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000238 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
239 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
240 }
241 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
242 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
243 }
244 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
245 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
246 }
247 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000248 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
249 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000250 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000251 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
252 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000253
Chris Lattner3462ae32001-12-03 22:26:30 +0000254 virtual ConstantBool *lessthan(const Constant *V1,
255 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000256 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
257 }
258
Chris Lattner55406842001-07-21 19:10:49 +0000259 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000260 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000261 return SubClassName::CastToBool((const ArgType*)V);
262 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000263 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000264 return SubClassName::CastToSByte((const ArgType*)V);
265 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000266 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000267 return SubClassName::CastToUByte((const ArgType*)V);
268 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000269 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000270 return SubClassName::CastToShort((const ArgType*)V);
271 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000272 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000273 return SubClassName::CastToUShort((const ArgType*)V);
274 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000275 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000276 return SubClassName::CastToInt((const ArgType*)V);
277 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000278 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000279 return SubClassName::CastToUInt((const ArgType*)V);
280 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000281 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000282 return SubClassName::CastToLong((const ArgType*)V);
283 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000284 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000285 return SubClassName::CastToULong((const ArgType*)V);
286 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000287 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000288 return SubClassName::CastToFloat((const ArgType*)V);
289 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000290 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000291 return SubClassName::CastToDouble((const ArgType*)V);
292 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000293 virtual Constant *castToPointer(const Constant *V,
294 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000295 return SubClassName::CastToPointer((const ArgType*)V, Ty);
296 }
Chris Lattner55406842001-07-21 19:10:49 +0000297
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298 //===--------------------------------------------------------------------===//
299 // Default "noop" implementations
300 //===--------------------------------------------------------------------===//
301
Chris Lattnere87f65e2002-07-30 16:24:28 +0000302 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
303 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
304 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
305 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
306 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
307 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
308 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
309 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
310 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
311 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
312 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313 return 0;
314 }
Chris Lattner55406842001-07-21 19:10:49 +0000315
316 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000317 static ConstantBool *CastToBool (const Constant *V) { return 0; }
318 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
319 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
320 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
321 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
322 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
323 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
324 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
325 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
326 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
327 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000328 static Constant *CastToPointer(const Constant *,
329 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000330};
331
332
333
334//===----------------------------------------------------------------------===//
335// EmptyRules Class
336//===----------------------------------------------------------------------===//
337//
338// EmptyRules provides a concrete base class of ConstRules that does nothing
339//
Chris Lattner3462ae32001-12-03 22:26:30 +0000340struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000341};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000342
343
344
345//===----------------------------------------------------------------------===//
346// BoolRules Class
347//===----------------------------------------------------------------------===//
348//
349// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
350//
Chris Lattner3462ae32001-12-03 22:26:30 +0000351struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352
Chris Lattner07507a42002-09-03 20:09:49 +0000353 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
354 return ConstantBool::get(V1->getValue() < V2->getValue());
355 }
356
Chris Lattnere87f65e2002-07-30 16:24:28 +0000357 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
358 return ConstantBool::get(V1->getValue() & V2->getValue());
359 }
360
361 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000362 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363 }
364
Chris Lattnere87f65e2002-07-30 16:24:28 +0000365 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
366 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000367 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000368
369 // Casting operators. ick
370#define DEF_CAST(TYPE, CLASS, CTYPE) \
371 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
372 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
373 }
374
375 DEF_CAST(Bool , ConstantBool, bool)
376 DEF_CAST(SByte , ConstantSInt, signed char)
377 DEF_CAST(UByte , ConstantUInt, unsigned char)
378 DEF_CAST(Short , ConstantSInt, signed short)
379 DEF_CAST(UShort, ConstantUInt, unsigned short)
380 DEF_CAST(Int , ConstantSInt, signed int)
381 DEF_CAST(UInt , ConstantUInt, unsigned int)
382 DEF_CAST(Long , ConstantSInt, int64_t)
383 DEF_CAST(ULong , ConstantUInt, uint64_t)
384 DEF_CAST(Float , ConstantFP , float)
385 DEF_CAST(Double, ConstantFP , double)
386#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000387};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000388
389
390//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000391// PointerRules Class
392//===----------------------------------------------------------------------===//
393//
394// PointerRules provides a concrete base class of ConstRules for pointer types
395//
Chris Lattner3462ae32001-12-03 22:26:30 +0000396struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000397 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000398 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000399 return 0; // Can't const prop other types of pointers
400 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000401 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000402 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000403 return 0; // Can't const prop other types of pointers
404 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000405 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000406 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000407 return 0; // Can't const prop other types of pointers
408 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000409 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000410 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000411 return 0; // Can't const prop other types of pointers
412 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000413 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000414 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000415 return 0; // Can't const prop other types of pointers
416 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000417 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000418 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000419 return 0; // Can't const prop other types of pointers
420 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000421 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000422 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000423 return 0; // Can't const prop other types of pointers
424 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000425 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000426 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000427 return 0; // Can't const prop other types of pointers
428 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000429 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000430 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000431 return 0; // Can't const prop other types of pointers
432 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000433 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000434 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000435 return 0; // Can't const prop other types of pointers
436 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000437 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000438 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000439 return 0; // Can't const prop other types of pointers
440 }
441
Chris Lattner1f0049c2003-04-17 19:24:18 +0000442 static Constant *CastToPointer(const ConstantPointer *V,
443 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000444 if (V->getType() == PTy)
445 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000446 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000447 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000448 return 0; // Can't const prop other types of pointers
449 }
450};
451
452
453//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000454// DirectRules Class
455//===----------------------------------------------------------------------===//
456//
457// DirectRules provides a concrete base classes of ConstRules for a variety of
458// different types. This allows the C++ compiler to automatically generate our
459// constant handling operations in a typesafe and accurate manner.
460//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000461template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
462struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000463 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
464 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
465 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466 }
467
Chris Lattnere87f65e2002-07-30 16:24:28 +0000468 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
469 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
470 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000471 }
472
Chris Lattnere87f65e2002-07-30 16:24:28 +0000473 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
474 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
475 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000476 }
477
Chris Lattnere87f65e2002-07-30 16:24:28 +0000478 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000479 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000480 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
481 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000482 }
483
Chris Lattnere87f65e2002-07-30 16:24:28 +0000484 static ConstantBool *LessThan(const ConstantClass *V1,
485 const ConstantClass *V2) {
486 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
487 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000488 }
Chris Lattner55406842001-07-21 19:10:49 +0000489
Chris Lattner1f0049c2003-04-17 19:24:18 +0000490 static Constant *CastToPointer(const ConstantClass *V,
491 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000492 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000493 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000494 return 0; // Can't const prop other types of pointers
495 }
496
Chris Lattner55406842001-07-21 19:10:49 +0000497 // Casting operators. ick
498#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000499 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000500 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000501 }
502
Chris Lattner3462ae32001-12-03 22:26:30 +0000503 DEF_CAST(Bool , ConstantBool, bool)
504 DEF_CAST(SByte , ConstantSInt, signed char)
505 DEF_CAST(UByte , ConstantUInt, unsigned char)
506 DEF_CAST(Short , ConstantSInt, signed short)
507 DEF_CAST(UShort, ConstantUInt, unsigned short)
508 DEF_CAST(Int , ConstantSInt, signed int)
509 DEF_CAST(UInt , ConstantUInt, unsigned int)
510 DEF_CAST(Long , ConstantSInt, int64_t)
511 DEF_CAST(ULong , ConstantUInt, uint64_t)
512 DEF_CAST(Float , ConstantFP , float)
513 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000514#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000515};
516
Chris Lattner62af86e2002-05-03 20:09:52 +0000517
518//===----------------------------------------------------------------------===//
519// DirectIntRules Class
520//===----------------------------------------------------------------------===//
521//
522// DirectIntRules provides implementations of functions that are valid on
523// integer types, but not all types in general.
524//
525template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000526struct DirectIntRules
527 : public DirectRules<ConstantClass, BuiltinType, Ty,
528 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000529
Chris Lattner268916262003-05-12 15:26:25 +0000530 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
531 if (V2->isNullValue()) return 0;
532 if (V2->isAllOnesValue() && // MIN_INT / -1
533 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
534 return 0;
535 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
536 return ConstantClass::get(*Ty, R);
537 }
538
Chris Lattnere87f65e2002-07-30 16:24:28 +0000539 static Constant *Rem(const ConstantClass *V1,
540 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000541 if (V2->isNullValue()) return 0; // X / 0
542 if (V2->isAllOnesValue() && // MIN_INT / -1
543 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
544 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000545 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
546 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000547 }
Chris Lattner6670d862002-05-06 03:00:54 +0000548
Chris Lattnere87f65e2002-07-30 16:24:28 +0000549 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
550 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
551 return ConstantClass::get(*Ty, R);
552 }
553 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
554 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
555 return ConstantClass::get(*Ty, R);
556 }
557 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
558 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
559 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000560 }
561
Chris Lattnere87f65e2002-07-30 16:24:28 +0000562 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
563 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
564 return ConstantClass::get(*Ty, R);
565 }
566
567 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
568 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
569 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000570 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000571};
572
573
574//===----------------------------------------------------------------------===//
575// DirectFPRules Class
576//===----------------------------------------------------------------------===//
577//
578// DirectFPRules provides implementations of functions that are valid on
579// floating point types, but not all types in general.
580//
581template <class ConstantClass, class BuiltinType, Type **Ty>
582struct DirectFPRules
583 : public DirectRules<ConstantClass, BuiltinType, Ty,
584 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000585 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000586 if (V2->isNullValue()) return 0;
587 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
588 (BuiltinType)V2->getValue());
589 return ConstantClass::get(*Ty, Result);
590 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000591};
592
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593//===----------------------------------------------------------------------===//
594// DirectRules Subclasses
595//===----------------------------------------------------------------------===//
596//
597// Given the DirectRules class we can now implement lots of types with little
598// code. Thank goodness C++ compilers are great at stomping out layers of
599// templates... can you imagine having to do this all by hand? (/me is lazy :)
600//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601
602// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000603// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000604//
Chris Lattner61607ee2001-09-09 21:01:20 +0000605Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
606 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000607 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000608
Chris Lattner2f7c9632001-06-06 20:29:01 +0000609 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000610 case Type::BoolTyID: return new BoolRules();
611 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000612 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000613 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000614 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000615 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000616 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000617 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000618 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000619 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000620 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000621 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000622 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000623 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000624 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000625 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000626 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000627 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000628 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000629 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000630 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000631 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000632 default:
633 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000635}
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000636
637ConstRules *ConstRules::getConstantExprRules() {
638 static EmptyRules CERules;
639 return &CERules;
640}