blob: 7fb7a05e55d3df40ed8fbb8e5c817e5f1264e7e1 [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
Brian Gaeke960707c2003-11-11 22:41:34 +000020namespace llvm {
21
Chris Lattner61607ee2001-09-09 21:01:20 +000022AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
23 &ConstRules::find));
24
Chris Lattner9f307732002-05-06 17:54:27 +000025// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
26// If successful, the constant result is returned, if not, null is returned.
27//
28Constant *ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000029 if (PHINode *PN = dyn_cast<PHINode>(I)) {
30 if (PN->getNumIncomingValues() == 0)
31 return Constant::getNullValue(PN->getType());
32
33 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
34 if (Result == 0) return 0;
35
36 // Handle PHI nodes specially here...
37 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
38 if (PN->getIncomingValue(i) != Result)
39 return 0; // Not all the same incoming constants...
40
41 // If we reach here, all incoming values are the same constant.
42 return Result;
43 }
44
Chris Lattner9f307732002-05-06 17:54:27 +000045 Constant *Op0 = 0;
46 Constant *Op1 = 0;
47
48 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
49 Op0 = dyn_cast<Constant>(I->getOperand(0));
50 if (Op0 == 0) return 0; // Not a constant?, can't fold
51
52 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
53 Op1 = dyn_cast<Constant>(I->getOperand(1));
54 if (Op1 == 0) return 0; // Not a constant?, can't fold
55 }
56 }
57
Chris Lattner8acf3462003-05-27 19:16:07 +000058 if (isa<BinaryOperator>(I))
59 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
60
Chris Lattner9f307732002-05-06 17:54:27 +000061 switch (I->getOpcode()) {
62 case Instruction::Cast:
Chris Lattner8acf3462003-05-27 19:16:07 +000063 return ConstantExpr::getCast(Op0, I->getType());
64 case Instruction::Shl:
65 case Instruction::Shr:
66 return ConstantExpr::getShift(I->getOpcode(), Op0, Op1);
Chris Lattner1f0049c2003-04-17 19:24:18 +000067 case Instruction::GetElementPtr: {
68 std::vector<Constant*> IdxList;
69 IdxList.reserve(I->getNumOperands()-1);
70 if (Op1) IdxList.push_back(Op1);
71 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
72 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
73 IdxList.push_back(C);
74 else
75 return 0; // Non-constant operand
Chris Lattner8acf3462003-05-27 19:16:07 +000076 return ConstantExpr::getGetElementPtr(Op0, IdxList);
Chris Lattner1f0049c2003-04-17 19:24:18 +000077 }
Chris Lattner9f307732002-05-06 17:54:27 +000078 default:
79 return 0;
80 }
81}
82
Chris Lattner1f0049c2003-04-17 19:24:18 +000083static unsigned getSize(const Type *Ty) {
84 unsigned S = Ty->getPrimitiveSize();
85 return S ? S : 8; // Treat pointers at 8 bytes
86}
87
Chris Lattner9f307732002-05-06 17:54:27 +000088Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000089 if (V->getType() == DestTy) return (Constant*)V;
90
91 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
92 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000093 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000094 // Try to not produce a cast of a cast, which is almost always redundant.
95 if (!Op->getType()->isFloatingPoint() &&
96 !CE->getType()->isFloatingPoint() &&
97 !DestTy->getType()->isFloatingPoint()) {
98 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
99 unsigned S3 = getSize(DestTy);
100 if (Op->getType() == DestTy && S3 >= S2)
101 return Op;
102 if (S1 >= S2 && S2 >= S3)
103 return ConstantExpr::getCast(Op, DestTy);
104 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
105 return ConstantExpr::getCast(Op, DestTy);
106 }
Chris Lattner941b06b2003-08-21 19:45:55 +0000107 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
108 // If all of the indexes in the GEP are null values, there is no pointer
109 // adjustment going on. We might as well cast the source pointer.
110 bool isAllNull = true;
111 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
112 if (!CE->getOperand(i)->isNullValue()) {
113 isAllNull = false;
114 break;
115 }
116 if (isAllNull)
117 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +0000118 }
119
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000120 return ConstRules::get(*V, *V)->castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000121}
122
Chris Lattner9f307732002-05-06 17:54:27 +0000123Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
124 const Constant *V2) {
125 switch (Opcode) {
126 case Instruction::Add: return *V1 + *V2;
127 case Instruction::Sub: return *V1 - *V2;
128 case Instruction::Mul: return *V1 * *V2;
129 case Instruction::Div: return *V1 / *V2;
130 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000131 case Instruction::And: return *V1 & *V2;
132 case Instruction::Or: return *V1 | *V2;
133 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +0000134
135 case Instruction::SetEQ: return *V1 == *V2;
136 case Instruction::SetNE: return *V1 != *V2;
137 case Instruction::SetLE: return *V1 <= *V2;
138 case Instruction::SetGE: return *V1 >= *V2;
139 case Instruction::SetLT: return *V1 < *V2;
140 case Instruction::SetGT: return *V1 > *V2;
141 }
142 return 0;
143}
144
145Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
146 const Constant *V2) {
147 switch (Opcode) {
148 case Instruction::Shl: return *V1 << *V2;
149 case Instruction::Shr: return *V1 >> *V2;
150 default: return 0;
151 }
152}
153
Chris Lattner1f0049c2003-04-17 19:24:18 +0000154Constant *ConstantFoldGetElementPtr(const Constant *C,
155 const std::vector<Constant*> &IdxList) {
156 if (IdxList.size() == 0 ||
157 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
158 return const_cast<Constant*>(C);
159
Chris Lattner941b06b2003-08-21 19:45:55 +0000160 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000161
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000162
Chris Lattner4ede64e2003-06-26 05:22:45 +0000163 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000164 // Combine Indices - If the source pointer to this getelementptr instruction
165 // is a getelementptr instruction, combine the indices of the two
166 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000167 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000168 if (CE->getOpcode() == Instruction::GetElementPtr) {
169 if (CE->getOperand(CE->getNumOperands()-1)->getType() == Type::LongTy) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000170 std::vector<Constant*> NewIndices;
171 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000172 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000173 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000174
175 // Add the last index of the source with the first index of the new GEP.
176 Constant *Combined =
177 ConstantExpr::get(Instruction::Add, IdxList[0],
178 CE->getOperand(CE->getNumOperands()-1));
179
180 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000181 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
182 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
183 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000184 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000185
186 // Implement folding of:
187 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
188 // long 0, long 0)
189 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
190 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000191 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
192 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000193 if (const PointerType *SPT =
194 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
195 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
196 if (const ArrayType *CAT =
197 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
198 if (CAT->getElementType() == SAT->getElementType())
199 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000200 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000201 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000202 return 0;
203}
204
Chris Lattner9f307732002-05-06 17:54:27 +0000205
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206//===----------------------------------------------------------------------===//
207// TemplateRules Class
208//===----------------------------------------------------------------------===//
209//
210// TemplateRules - Implement a subclass of ConstRules that provides all
211// operations as noops. All other rules classes inherit from this class so
212// that if functionality is needed in the future, it can simply be added here
213// and to ConstRules without changing anything else...
214//
215// This class also provides subclasses with typesafe implementations of methods
216// so that don't have to do type casting.
217//
218template<class ArgType, class SubClassName>
219class TemplateRules : public ConstRules {
220
221 //===--------------------------------------------------------------------===//
222 // Redirecting functions that cast to the appropriate types
223 //===--------------------------------------------------------------------===//
224
Chris Lattnere87f65e2002-07-30 16:24:28 +0000225 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
227 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000228 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000229 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
230 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000231 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000232 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
233 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000234 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000235 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
236 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000237 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000238 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
239 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000240 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
241 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
242 }
243 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
244 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
245 }
246 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
247 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
248 }
249 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000250 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
251 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000252 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000253 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
254 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000255
Chris Lattner3462ae32001-12-03 22:26:30 +0000256 virtual ConstantBool *lessthan(const Constant *V1,
257 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000258 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
259 }
260
Chris Lattner55406842001-07-21 19:10:49 +0000261 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000263 return SubClassName::CastToBool((const ArgType*)V);
264 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000265 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000266 return SubClassName::CastToSByte((const ArgType*)V);
267 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000268 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000269 return SubClassName::CastToUByte((const ArgType*)V);
270 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000271 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000272 return SubClassName::CastToShort((const ArgType*)V);
273 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000274 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000275 return SubClassName::CastToUShort((const ArgType*)V);
276 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000277 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000278 return SubClassName::CastToInt((const ArgType*)V);
279 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000280 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000281 return SubClassName::CastToUInt((const ArgType*)V);
282 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000283 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000284 return SubClassName::CastToLong((const ArgType*)V);
285 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000286 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000287 return SubClassName::CastToULong((const ArgType*)V);
288 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000289 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000290 return SubClassName::CastToFloat((const ArgType*)V);
291 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000292 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000293 return SubClassName::CastToDouble((const ArgType*)V);
294 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000295 virtual Constant *castToPointer(const Constant *V,
296 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000297 return SubClassName::CastToPointer((const ArgType*)V, Ty);
298 }
Chris Lattner55406842001-07-21 19:10:49 +0000299
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300 //===--------------------------------------------------------------------===//
301 // Default "noop" implementations
302 //===--------------------------------------------------------------------===//
303
Chris Lattnere87f65e2002-07-30 16:24:28 +0000304 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
305 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
306 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
307 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
308 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
309 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
310 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
311 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
312 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
313 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
314 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 return 0;
316 }
Chris Lattner55406842001-07-21 19:10:49 +0000317
318 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000319 static ConstantBool *CastToBool (const Constant *V) { return 0; }
320 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
321 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
322 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
323 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
324 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
325 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
326 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
327 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
328 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
329 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000330 static Constant *CastToPointer(const Constant *,
331 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332};
333
334
335
336//===----------------------------------------------------------------------===//
337// EmptyRules Class
338//===----------------------------------------------------------------------===//
339//
340// EmptyRules provides a concrete base class of ConstRules that does nothing
341//
Chris Lattner3462ae32001-12-03 22:26:30 +0000342struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000343};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344
345
346
347//===----------------------------------------------------------------------===//
348// BoolRules Class
349//===----------------------------------------------------------------------===//
350//
351// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
352//
Chris Lattner3462ae32001-12-03 22:26:30 +0000353struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354
Chris Lattner07507a42002-09-03 20:09:49 +0000355 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
356 return ConstantBool::get(V1->getValue() < V2->getValue());
357 }
358
Chris Lattnere87f65e2002-07-30 16:24:28 +0000359 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
360 return ConstantBool::get(V1->getValue() & V2->getValue());
361 }
362
363 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000364 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000365 }
366
Chris Lattnere87f65e2002-07-30 16:24:28 +0000367 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
368 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000370
371 // Casting operators. ick
372#define DEF_CAST(TYPE, CLASS, CTYPE) \
373 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
374 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
375 }
376
377 DEF_CAST(Bool , ConstantBool, bool)
378 DEF_CAST(SByte , ConstantSInt, signed char)
379 DEF_CAST(UByte , ConstantUInt, unsigned char)
380 DEF_CAST(Short , ConstantSInt, signed short)
381 DEF_CAST(UShort, ConstantUInt, unsigned short)
382 DEF_CAST(Int , ConstantSInt, signed int)
383 DEF_CAST(UInt , ConstantUInt, unsigned int)
384 DEF_CAST(Long , ConstantSInt, int64_t)
385 DEF_CAST(ULong , ConstantUInt, uint64_t)
386 DEF_CAST(Float , ConstantFP , float)
387 DEF_CAST(Double, ConstantFP , double)
388#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000389};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000390
391
392//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000393// PointerRules Class
394//===----------------------------------------------------------------------===//
395//
396// PointerRules provides a concrete base class of ConstRules for pointer types
397//
Chris Lattner3462ae32001-12-03 22:26:30 +0000398struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000399 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000400 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000401 return 0; // Can't const prop other types of pointers
402 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000403 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000404 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000405 return 0; // Can't const prop other types of pointers
406 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000407 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000408 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000409 return 0; // Can't const prop other types of pointers
410 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000411 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000412 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000413 return 0; // Can't const prop other types of pointers
414 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000415 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000416 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000417 return 0; // Can't const prop other types of pointers
418 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000419 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000420 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000421 return 0; // Can't const prop other types of pointers
422 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000423 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000424 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000425 return 0; // Can't const prop other types of pointers
426 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000427 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000428 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000429 return 0; // Can't const prop other types of pointers
430 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000431 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000432 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000433 return 0; // Can't const prop other types of pointers
434 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000435 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000436 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000437 return 0; // Can't const prop other types of pointers
438 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000439 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000440 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000441 return 0; // Can't const prop other types of pointers
442 }
443
Chris Lattner1f0049c2003-04-17 19:24:18 +0000444 static Constant *CastToPointer(const ConstantPointer *V,
445 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000446 if (V->getType() == PTy)
447 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000448 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000449 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000450 return 0; // Can't const prop other types of pointers
451 }
452};
453
454
455//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456// DirectRules Class
457//===----------------------------------------------------------------------===//
458//
459// DirectRules provides a concrete base classes of ConstRules for a variety of
460// different types. This allows the C++ compiler to automatically generate our
461// constant handling operations in a typesafe and accurate manner.
462//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000463template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
464struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000465 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
466 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
467 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468 }
469
Chris Lattnere87f65e2002-07-30 16:24:28 +0000470 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
471 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
472 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473 }
474
Chris Lattnere87f65e2002-07-30 16:24:28 +0000475 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
476 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
477 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000478 }
479
Chris Lattnere87f65e2002-07-30 16:24:28 +0000480 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000481 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000482 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
483 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000484 }
485
Chris Lattnere87f65e2002-07-30 16:24:28 +0000486 static ConstantBool *LessThan(const ConstantClass *V1,
487 const ConstantClass *V2) {
488 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
489 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000490 }
Chris Lattner55406842001-07-21 19:10:49 +0000491
Chris Lattner1f0049c2003-04-17 19:24:18 +0000492 static Constant *CastToPointer(const ConstantClass *V,
493 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000494 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000495 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000496 return 0; // Can't const prop other types of pointers
497 }
498
Chris Lattner55406842001-07-21 19:10:49 +0000499 // Casting operators. ick
500#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000501 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000502 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000503 }
504
Chris Lattner3462ae32001-12-03 22:26:30 +0000505 DEF_CAST(Bool , ConstantBool, bool)
506 DEF_CAST(SByte , ConstantSInt, signed char)
507 DEF_CAST(UByte , ConstantUInt, unsigned char)
508 DEF_CAST(Short , ConstantSInt, signed short)
509 DEF_CAST(UShort, ConstantUInt, unsigned short)
510 DEF_CAST(Int , ConstantSInt, signed int)
511 DEF_CAST(UInt , ConstantUInt, unsigned int)
512 DEF_CAST(Long , ConstantSInt, int64_t)
513 DEF_CAST(ULong , ConstantUInt, uint64_t)
514 DEF_CAST(Float , ConstantFP , float)
515 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000516#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000517};
518
Chris Lattner62af86e2002-05-03 20:09:52 +0000519
520//===----------------------------------------------------------------------===//
521// DirectIntRules Class
522//===----------------------------------------------------------------------===//
523//
524// DirectIntRules provides implementations of functions that are valid on
525// integer types, but not all types in general.
526//
527template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000528struct DirectIntRules
529 : public DirectRules<ConstantClass, BuiltinType, Ty,
530 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000531
Chris Lattner268916262003-05-12 15:26:25 +0000532 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
533 if (V2->isNullValue()) return 0;
534 if (V2->isAllOnesValue() && // MIN_INT / -1
535 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
536 return 0;
537 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
538 return ConstantClass::get(*Ty, R);
539 }
540
Chris Lattnere87f65e2002-07-30 16:24:28 +0000541 static Constant *Rem(const ConstantClass *V1,
542 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000543 if (V2->isNullValue()) return 0; // X / 0
544 if (V2->isAllOnesValue() && // MIN_INT / -1
545 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
546 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000547 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
548 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000549 }
Chris Lattner6670d862002-05-06 03:00:54 +0000550
Chris Lattnere87f65e2002-07-30 16:24:28 +0000551 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
552 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
553 return ConstantClass::get(*Ty, R);
554 }
555 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
556 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
557 return ConstantClass::get(*Ty, R);
558 }
559 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
560 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
561 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000562 }
563
Chris Lattnere87f65e2002-07-30 16:24:28 +0000564 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
565 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
566 return ConstantClass::get(*Ty, R);
567 }
568
569 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
570 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
571 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000572 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000573};
574
575
576//===----------------------------------------------------------------------===//
577// DirectFPRules Class
578//===----------------------------------------------------------------------===//
579//
580// DirectFPRules provides implementations of functions that are valid on
581// floating point types, but not all types in general.
582//
583template <class ConstantClass, class BuiltinType, Type **Ty>
584struct DirectFPRules
585 : public DirectRules<ConstantClass, BuiltinType, Ty,
586 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000587 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000588 if (V2->isNullValue()) return 0;
589 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
590 (BuiltinType)V2->getValue());
591 return ConstantClass::get(*Ty, Result);
592 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000593};
594
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595//===----------------------------------------------------------------------===//
596// DirectRules Subclasses
597//===----------------------------------------------------------------------===//
598//
599// Given the DirectRules class we can now implement lots of types with little
600// code. Thank goodness C++ compilers are great at stomping out layers of
601// templates... can you imagine having to do this all by hand? (/me is lazy :)
602//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000603
604// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000605// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000606//
Chris Lattner61607ee2001-09-09 21:01:20 +0000607Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
608 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000609 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000610
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000612 case Type::BoolTyID: return new BoolRules();
613 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000614 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000615 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000616 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000617 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000618 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000619 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000620 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000621 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000622 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000623 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000624 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000625 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000626 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000627 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000628 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000629 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000630 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000631 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000632 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000633 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000634 default:
635 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000637}
Chris Lattnere17c1c5a2003-04-25 02:52:06 +0000638
639ConstRules *ConstRules::getConstantExprRules() {
640 static EmptyRules CERules;
641 return &CERules;
642}
Brian Gaeke960707c2003-11-11 22:41:34 +0000643
644} // End llvm namespace