blob: 5e7bd0296590e97d8ba7d915fabf1524c72760e0 [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 Lattner9d9cbcf2003-11-17 19:05:17 +000019using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000020
Chris Lattner9f307732002-05-06 17:54:27 +000021// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
22// If successful, the constant result is returned, if not, null is returned.
23//
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000024Constant *llvm::ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000025 if (PHINode *PN = dyn_cast<PHINode>(I)) {
26 if (PN->getNumIncomingValues() == 0)
27 return Constant::getNullValue(PN->getType());
28
29 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
30 if (Result == 0) return 0;
31
32 // Handle PHI nodes specially here...
33 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
34 if (PN->getIncomingValue(i) != Result)
35 return 0; // Not all the same incoming constants...
36
37 // If we reach here, all incoming values are the same constant.
38 return Result;
39 }
40
Chris Lattner9f307732002-05-06 17:54:27 +000041 Constant *Op0 = 0;
42 Constant *Op1 = 0;
43
44 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
45 Op0 = dyn_cast<Constant>(I->getOperand(0));
46 if (Op0 == 0) return 0; // Not a constant?, can't fold
47
48 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
49 Op1 = dyn_cast<Constant>(I->getOperand(1));
50 if (Op1 == 0) return 0; // Not a constant?, can't fold
51 }
52 }
53
Chris Lattner8acf3462003-05-27 19:16:07 +000054 if (isa<BinaryOperator>(I))
55 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
56
Chris Lattner9f307732002-05-06 17:54:27 +000057 switch (I->getOpcode()) {
58 case Instruction::Cast:
Chris Lattner8acf3462003-05-27 19:16:07 +000059 return ConstantExpr::getCast(Op0, I->getType());
60 case Instruction::Shl:
61 case Instruction::Shr:
62 return ConstantExpr::getShift(I->getOpcode(), Op0, Op1);
Chris Lattner1f0049c2003-04-17 19:24:18 +000063 case Instruction::GetElementPtr: {
64 std::vector<Constant*> IdxList;
65 IdxList.reserve(I->getNumOperands()-1);
66 if (Op1) IdxList.push_back(Op1);
67 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
68 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
69 IdxList.push_back(C);
70 else
71 return 0; // Non-constant operand
Chris Lattner8acf3462003-05-27 19:16:07 +000072 return ConstantExpr::getGetElementPtr(Op0, IdxList);
Chris Lattner1f0049c2003-04-17 19:24:18 +000073 }
Chris Lattner9f307732002-05-06 17:54:27 +000074 default:
75 return 0;
76 }
77}
78
Chris Lattner1f0049c2003-04-17 19:24:18 +000079static unsigned getSize(const Type *Ty) {
80 unsigned S = Ty->getPrimitiveSize();
81 return S ? S : 8; // Treat pointers at 8 bytes
82}
83
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000084Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
85 const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000086 if (V->getType() == DestTy) return (Constant*)V;
87
88 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
89 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000090 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000091 // Try to not produce a cast of a cast, which is almost always redundant.
92 if (!Op->getType()->isFloatingPoint() &&
93 !CE->getType()->isFloatingPoint() &&
94 !DestTy->getType()->isFloatingPoint()) {
95 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
96 unsigned S3 = getSize(DestTy);
97 if (Op->getType() == DestTy && S3 >= S2)
98 return Op;
99 if (S1 >= S2 && S2 >= S3)
100 return ConstantExpr::getCast(Op, DestTy);
101 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
102 return ConstantExpr::getCast(Op, DestTy);
103 }
Chris Lattner941b06b2003-08-21 19:45:55 +0000104 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
105 // If all of the indexes in the GEP are null values, there is no pointer
106 // adjustment going on. We might as well cast the source pointer.
107 bool isAllNull = true;
108 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
109 if (!CE->getOperand(i)->isNullValue()) {
110 isAllNull = false;
111 break;
112 }
113 if (isAllNull)
114 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +0000115 }
116
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000117 return ConstRules::get(*V, *V).castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000118}
119
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000120Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
121 const Constant *V1,
122 const Constant *V2) {
Chris Lattner9f307732002-05-06 17:54:27 +0000123 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
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000143Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode,
144 const Constant *V1,
145 const Constant *V2) {
Chris Lattner9f307732002-05-06 17:54:27 +0000146 switch (Opcode) {
147 case Instruction::Shl: return *V1 << *V2;
148 case Instruction::Shr: return *V1 >> *V2;
149 default: return 0;
150 }
151}
152
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000153Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
154 const std::vector<Constant*> &IdxList) {
Chris Lattner1f0049c2003-04-17 19:24:18 +0000155 if (IdxList.size() == 0 ||
156 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
157 return const_cast<Constant*>(C);
158
Chris Lattner941b06b2003-08-21 19:45:55 +0000159 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000160
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000161
Chris Lattner4ede64e2003-06-26 05:22:45 +0000162 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000163 // Combine Indices - If the source pointer to this getelementptr instruction
164 // is a getelementptr instruction, combine the indices of the two
165 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000166 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000167 if (CE->getOpcode() == Instruction::GetElementPtr) {
168 if (CE->getOperand(CE->getNumOperands()-1)->getType() == Type::LongTy) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000169 std::vector<Constant*> NewIndices;
170 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000171 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000172 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000173
174 // Add the last index of the source with the first index of the new GEP.
175 Constant *Combined =
176 ConstantExpr::get(Instruction::Add, IdxList[0],
177 CE->getOperand(CE->getNumOperands()-1));
178
179 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000180 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
181 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
182 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000183 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000184
185 // Implement folding of:
186 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
187 // long 0, long 0)
188 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
189 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000190 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
191 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000192 if (const PointerType *SPT =
193 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
194 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
195 if (const ArrayType *CAT =
196 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
197 if (CAT->getElementType() == SAT->getElementType())
198 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000199 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000200 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000201 return 0;
202}
203
Chris Lattner9f307732002-05-06 17:54:27 +0000204
Chris Lattner2f7c9632001-06-06 20:29:01 +0000205//===----------------------------------------------------------------------===//
206// TemplateRules Class
207//===----------------------------------------------------------------------===//
208//
209// TemplateRules - Implement a subclass of ConstRules that provides all
210// operations as noops. All other rules classes inherit from this class so
211// that if functionality is needed in the future, it can simply be added here
212// and to ConstRules without changing anything else...
213//
214// This class also provides subclasses with typesafe implementations of methods
215// so that don't have to do type casting.
216//
217template<class ArgType, class SubClassName>
218class TemplateRules : public ConstRules {
219
220 //===--------------------------------------------------------------------===//
221 // Redirecting functions that cast to the appropriate types
222 //===--------------------------------------------------------------------===//
223
Chris Lattnere87f65e2002-07-30 16:24:28 +0000224 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
226 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000227 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
229 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000230 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000231 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
232 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000233 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000234 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
235 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000236 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000237 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
238 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000239 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
240 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
241 }
242 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
243 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
244 }
245 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
246 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
247 }
248 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000249 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
250 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000251 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000252 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
253 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000254
Chris Lattner3462ae32001-12-03 22:26:30 +0000255 virtual ConstantBool *lessthan(const Constant *V1,
256 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
258 }
259
Chris Lattner55406842001-07-21 19:10:49 +0000260 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000261 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000262 return SubClassName::CastToBool((const ArgType*)V);
263 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000264 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000265 return SubClassName::CastToSByte((const ArgType*)V);
266 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000267 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000268 return SubClassName::CastToUByte((const ArgType*)V);
269 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000270 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000271 return SubClassName::CastToShort((const ArgType*)V);
272 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000273 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000274 return SubClassName::CastToUShort((const ArgType*)V);
275 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000276 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000277 return SubClassName::CastToInt((const ArgType*)V);
278 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000279 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000280 return SubClassName::CastToUInt((const ArgType*)V);
281 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000282 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000283 return SubClassName::CastToLong((const ArgType*)V);
284 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000285 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000286 return SubClassName::CastToULong((const ArgType*)V);
287 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000288 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000289 return SubClassName::CastToFloat((const ArgType*)V);
290 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000291 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000292 return SubClassName::CastToDouble((const ArgType*)V);
293 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000294 virtual Constant *castToPointer(const Constant *V,
295 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000296 return SubClassName::CastToPointer((const ArgType*)V, Ty);
297 }
Chris Lattner55406842001-07-21 19:10:49 +0000298
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299 //===--------------------------------------------------------------------===//
300 // Default "noop" implementations
301 //===--------------------------------------------------------------------===//
302
Chris Lattnere87f65e2002-07-30 16:24:28 +0000303 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
304 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
305 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
306 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
307 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
308 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
309 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
310 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
311 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
312 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
313 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314 return 0;
315 }
Chris Lattner55406842001-07-21 19:10:49 +0000316
317 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000318 static ConstantBool *CastToBool (const Constant *V) { return 0; }
319 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
320 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
321 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
322 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
323 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
324 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
325 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
326 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
327 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
328 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000329 static Constant *CastToPointer(const Constant *,
330 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331};
332
333
334
335//===----------------------------------------------------------------------===//
336// EmptyRules Class
337//===----------------------------------------------------------------------===//
338//
339// EmptyRules provides a concrete base class of ConstRules that does nothing
340//
Chris Lattner3462ae32001-12-03 22:26:30 +0000341struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000342};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000343
344
345
346//===----------------------------------------------------------------------===//
347// BoolRules Class
348//===----------------------------------------------------------------------===//
349//
350// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
351//
Chris Lattner3462ae32001-12-03 22:26:30 +0000352struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000353
Chris Lattner07507a42002-09-03 20:09:49 +0000354 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
355 return ConstantBool::get(V1->getValue() < V2->getValue());
356 }
357
Chris Lattnere87f65e2002-07-30 16:24:28 +0000358 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
359 return ConstantBool::get(V1->getValue() & V2->getValue());
360 }
361
362 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000363 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000364 }
365
Chris Lattnere87f65e2002-07-30 16:24:28 +0000366 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
367 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000369
370 // Casting operators. ick
371#define DEF_CAST(TYPE, CLASS, CTYPE) \
372 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
373 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
374 }
375
376 DEF_CAST(Bool , ConstantBool, bool)
377 DEF_CAST(SByte , ConstantSInt, signed char)
378 DEF_CAST(UByte , ConstantUInt, unsigned char)
379 DEF_CAST(Short , ConstantSInt, signed short)
380 DEF_CAST(UShort, ConstantUInt, unsigned short)
381 DEF_CAST(Int , ConstantSInt, signed int)
382 DEF_CAST(UInt , ConstantUInt, unsigned int)
383 DEF_CAST(Long , ConstantSInt, int64_t)
384 DEF_CAST(ULong , ConstantUInt, uint64_t)
385 DEF_CAST(Float , ConstantFP , float)
386 DEF_CAST(Double, ConstantFP , double)
387#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000388};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389
390
391//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000392// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000393//===----------------------------------------------------------------------===//
394//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000395// NullPointerRules provides a concrete base class of ConstRules for null
396// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000397//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000398struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000399 NullPointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000400 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000401 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000402 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000403 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000404 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000405 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000406 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000407 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000408 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000409 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000410 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000411 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000412 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000413 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000414 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000415 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000416 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000417 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000418 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000419 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000420 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000421 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000422 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000423 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000424 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000425 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000426 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000427 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000428 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000429 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000430 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000431 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000432 }
433
Chris Lattner77f20dc2003-11-17 19:21:04 +0000434 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000435 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000436 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000437 }
438};
439
440
441//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442// DirectRules Class
443//===----------------------------------------------------------------------===//
444//
445// DirectRules provides a concrete base classes of ConstRules for a variety of
446// different types. This allows the C++ compiler to automatically generate our
447// constant handling operations in a typesafe and accurate manner.
448//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000449template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
450struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000451 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
452 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
453 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000454 }
455
Chris Lattnere87f65e2002-07-30 16:24:28 +0000456 static Constant *Sub(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 *Mul(const ConstantClass *V1, const ConstantClass *V2) {
462 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
463 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000464 }
465
Chris Lattnere87f65e2002-07-30 16:24:28 +0000466 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000467 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000468 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
469 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000470 }
471
Chris Lattnere87f65e2002-07-30 16:24:28 +0000472 static ConstantBool *LessThan(const ConstantClass *V1,
473 const ConstantClass *V2) {
474 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
475 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000476 }
Chris Lattner55406842001-07-21 19:10:49 +0000477
Chris Lattner1f0049c2003-04-17 19:24:18 +0000478 static Constant *CastToPointer(const ConstantClass *V,
479 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000480 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000481 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000482 return 0; // Can't const prop other types of pointers
483 }
484
Chris Lattner55406842001-07-21 19:10:49 +0000485 // Casting operators. ick
486#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000487 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000488 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000489 }
490
Chris Lattner3462ae32001-12-03 22:26:30 +0000491 DEF_CAST(Bool , ConstantBool, bool)
492 DEF_CAST(SByte , ConstantSInt, signed char)
493 DEF_CAST(UByte , ConstantUInt, unsigned char)
494 DEF_CAST(Short , ConstantSInt, signed short)
495 DEF_CAST(UShort, ConstantUInt, unsigned short)
496 DEF_CAST(Int , ConstantSInt, signed int)
497 DEF_CAST(UInt , ConstantUInt, unsigned int)
498 DEF_CAST(Long , ConstantSInt, int64_t)
499 DEF_CAST(ULong , ConstantUInt, uint64_t)
500 DEF_CAST(Float , ConstantFP , float)
501 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000502#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000503};
504
Chris Lattner62af86e2002-05-03 20:09:52 +0000505
506//===----------------------------------------------------------------------===//
507// DirectIntRules Class
508//===----------------------------------------------------------------------===//
509//
510// DirectIntRules provides implementations of functions that are valid on
511// integer types, but not all types in general.
512//
513template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000514struct DirectIntRules
515 : public DirectRules<ConstantClass, BuiltinType, Ty,
516 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000517
Chris Lattner268916262003-05-12 15:26:25 +0000518 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
519 if (V2->isNullValue()) return 0;
520 if (V2->isAllOnesValue() && // MIN_INT / -1
521 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
522 return 0;
523 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
524 return ConstantClass::get(*Ty, R);
525 }
526
Chris Lattnere87f65e2002-07-30 16:24:28 +0000527 static Constant *Rem(const ConstantClass *V1,
528 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000529 if (V2->isNullValue()) return 0; // X / 0
530 if (V2->isAllOnesValue() && // MIN_INT / -1
531 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
532 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000533 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
534 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000535 }
Chris Lattner6670d862002-05-06 03:00:54 +0000536
Chris Lattnere87f65e2002-07-30 16:24:28 +0000537 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
538 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
539 return ConstantClass::get(*Ty, R);
540 }
541 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
542 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
543 return ConstantClass::get(*Ty, R);
544 }
545 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
546 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
547 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000548 }
549
Chris Lattnere87f65e2002-07-30 16:24:28 +0000550 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
551 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
552 return ConstantClass::get(*Ty, R);
553 }
554
555 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
556 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
557 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000558 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000559};
560
561
562//===----------------------------------------------------------------------===//
563// DirectFPRules Class
564//===----------------------------------------------------------------------===//
565//
566// DirectFPRules provides implementations of functions that are valid on
567// floating point types, but not all types in general.
568//
569template <class ConstantClass, class BuiltinType, Type **Ty>
570struct DirectFPRules
571 : public DirectRules<ConstantClass, BuiltinType, Ty,
572 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000573 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000574 if (V2->isNullValue()) return 0;
575 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
576 (BuiltinType)V2->getValue());
577 return ConstantClass::get(*Ty, Result);
578 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000579};
580
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000581ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000582 static EmptyRules EmptyR;
583 static BoolRules BoolR;
584 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000585 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
586 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
587 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
588 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
589 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
590 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
591 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
592 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
593 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
594 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595
Chris Lattner4b6addf2003-11-17 19:19:32 +0000596 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
597 isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000598 return EmptyR;
599
600 // FIXME: This assert doesn't work because shifts pass both operands in to
601 // check for constant exprs. :(
602 //assert(V1.getType() == V2.getType() &&"Nonequal types to constant folder?");
603
604 switch (V1.getType()->getPrimitiveID()) {
605 default: assert(0 && "Unknown value type for constant folding!");
606 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000607 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000608 case Type::SByteTyID: return SByteR;
609 case Type::UByteTyID: return UByteR;
610 case Type::ShortTyID: return ShortR;
611 case Type::UShortTyID: return UShortR;
612 case Type::IntTyID: return IntR;
613 case Type::UIntTyID: return UIntR;
614 case Type::LongTyID: return LongR;
615 case Type::ULongTyID: return ULongR;
616 case Type::FloatTyID: return FloatR;
617 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000619}