blob: 5115ede9a1f58e53f687bc89cc04b21b21ca7637 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===//
2//
3// This file implements the various intrinsic operations, on constant values.
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattner65b529f2002-04-08 20:18:09 +00007#include "llvm/ConstantHandling.h"
Chris Lattner78a0d422002-05-07 20:44:59 +00008#include "llvm/iPHINode.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +00009#include <cmath>
Chris Lattnerd42d4922001-06-30 04:36:40 +000010
Chris Lattner61607ee2001-09-09 21:01:20 +000011AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules",
12 &ConstRules::find));
13
Chris Lattner9f307732002-05-06 17:54:27 +000014// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
15// If successful, the constant result is returned, if not, null is returned.
16//
17Constant *ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000018 if (PHINode *PN = dyn_cast<PHINode>(I)) {
19 if (PN->getNumIncomingValues() == 0)
20 return Constant::getNullValue(PN->getType());
21
22 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
23 if (Result == 0) return 0;
24
25 // Handle PHI nodes specially here...
26 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
27 if (PN->getIncomingValue(i) != Result)
28 return 0; // Not all the same incoming constants...
29
30 // If we reach here, all incoming values are the same constant.
31 return Result;
32 }
33
Chris Lattner9f307732002-05-06 17:54:27 +000034 Constant *Op0 = 0;
35 Constant *Op1 = 0;
36
37 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
38 Op0 = dyn_cast<Constant>(I->getOperand(0));
39 if (Op0 == 0) return 0; // Not a constant?, can't fold
40
41 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
42 Op1 = dyn_cast<Constant>(I->getOperand(1));
43 if (Op1 == 0) return 0; // Not a constant?, can't fold
44 }
45 }
46
47 switch (I->getOpcode()) {
48 case Instruction::Cast:
49 return ConstRules::get(*Op0)->castTo(Op0, I->getType());
Chris Lattner9f307732002-05-06 17:54:27 +000050 case Instruction::Add: return *Op0 + *Op1;
51 case Instruction::Sub: return *Op0 - *Op1;
52 case Instruction::Mul: return *Op0 * *Op1;
53 case Instruction::Div: return *Op0 / *Op1;
54 case Instruction::Rem: return *Op0 % *Op1;
Chris Lattnere87f65e2002-07-30 16:24:28 +000055 case Instruction::And: return *Op0 & *Op1;
56 case Instruction::Or: return *Op0 | *Op1;
57 case Instruction::Xor: return *Op0 ^ *Op1;
Chris Lattner9f307732002-05-06 17:54:27 +000058
59 case Instruction::SetEQ: return *Op0 == *Op1;
60 case Instruction::SetNE: return *Op0 != *Op1;
61 case Instruction::SetLE: return *Op0 <= *Op1;
62 case Instruction::SetGE: return *Op0 >= *Op1;
63 case Instruction::SetLT: return *Op0 < *Op1;
64 case Instruction::SetGT: return *Op0 > *Op1;
65 case Instruction::Shl: return *Op0 << *Op1;
66 case Instruction::Shr: return *Op0 >> *Op1;
67 default:
68 return 0;
69 }
70}
71
72Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) {
73 return ConstRules::get(*V)->castTo(V, DestTy);
74}
75
Chris Lattner9f307732002-05-06 17:54:27 +000076Constant *ConstantFoldBinaryInstruction(unsigned Opcode, const Constant *V1,
77 const Constant *V2) {
78 switch (Opcode) {
79 case Instruction::Add: return *V1 + *V2;
80 case Instruction::Sub: return *V1 - *V2;
81 case Instruction::Mul: return *V1 * *V2;
82 case Instruction::Div: return *V1 / *V2;
83 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +000084 case Instruction::And: return *V1 & *V2;
85 case Instruction::Or: return *V1 | *V2;
86 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +000087
88 case Instruction::SetEQ: return *V1 == *V2;
89 case Instruction::SetNE: return *V1 != *V2;
90 case Instruction::SetLE: return *V1 <= *V2;
91 case Instruction::SetGE: return *V1 >= *V2;
92 case Instruction::SetLT: return *V1 < *V2;
93 case Instruction::SetGT: return *V1 > *V2;
94 }
95 return 0;
96}
97
98Constant *ConstantFoldShiftInstruction(unsigned Opcode, const Constant *V1,
99 const Constant *V2) {
100 switch (Opcode) {
101 case Instruction::Shl: return *V1 << *V2;
102 case Instruction::Shr: return *V1 >> *V2;
103 default: return 0;
104 }
105}
106
107
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108//===----------------------------------------------------------------------===//
109// TemplateRules Class
110//===----------------------------------------------------------------------===//
111//
112// TemplateRules - Implement a subclass of ConstRules that provides all
113// operations as noops. All other rules classes inherit from this class so
114// that if functionality is needed in the future, it can simply be added here
115// and to ConstRules without changing anything else...
116//
117// This class also provides subclasses with typesafe implementations of methods
118// so that don't have to do type casting.
119//
120template<class ArgType, class SubClassName>
121class TemplateRules : public ConstRules {
122
123 //===--------------------------------------------------------------------===//
124 // Redirecting functions that cast to the appropriate types
125 //===--------------------------------------------------------------------===//
126
Chris Lattnere87f65e2002-07-30 16:24:28 +0000127 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
129 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000130 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
132 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000133 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000134 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
135 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000136 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000137 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
138 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000139 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000140 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
141 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000142 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
143 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
144 }
145 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
146 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
147 }
148 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
149 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
150 }
151 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000152 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
153 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000154 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000155 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
156 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000157
Chris Lattner3462ae32001-12-03 22:26:30 +0000158 virtual ConstantBool *lessthan(const Constant *V1,
159 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
161 }
162
Chris Lattner55406842001-07-21 19:10:49 +0000163 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000164 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000165 return SubClassName::CastToBool((const ArgType*)V);
166 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000167 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000168 return SubClassName::CastToSByte((const ArgType*)V);
169 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000170 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000171 return SubClassName::CastToUByte((const ArgType*)V);
172 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000173 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000174 return SubClassName::CastToShort((const ArgType*)V);
175 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000176 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000177 return SubClassName::CastToUShort((const ArgType*)V);
178 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000179 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000180 return SubClassName::CastToInt((const ArgType*)V);
181 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000182 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000183 return SubClassName::CastToUInt((const ArgType*)V);
184 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000185 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000186 return SubClassName::CastToLong((const ArgType*)V);
187 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000188 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000189 return SubClassName::CastToULong((const ArgType*)V);
190 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000191 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000192 return SubClassName::CastToFloat((const ArgType*)V);
193 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000194 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000195 return SubClassName::CastToDouble((const ArgType*)V);
196 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000197 virtual ConstantPointer *castToPointer(const Constant *V,
198 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000199 return SubClassName::CastToPointer((const ArgType*)V, Ty);
200 }
Chris Lattner55406842001-07-21 19:10:49 +0000201
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202 //===--------------------------------------------------------------------===//
203 // Default "noop" implementations
204 //===--------------------------------------------------------------------===//
205
Chris Lattnere87f65e2002-07-30 16:24:28 +0000206 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
207 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
208 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
209 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
210 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
211 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
212 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
213 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
214 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
215 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
216 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000217 return 0;
218 }
Chris Lattner55406842001-07-21 19:10:49 +0000219
220 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000221 static ConstantBool *CastToBool (const Constant *V) { return 0; }
222 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
223 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
224 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
225 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
226 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
227 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
228 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
229 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
230 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
231 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
232 static ConstantPointer *CastToPointer(const Constant *,
233 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234};
235
236
237
238//===----------------------------------------------------------------------===//
239// EmptyRules Class
240//===----------------------------------------------------------------------===//
241//
242// EmptyRules provides a concrete base class of ConstRules that does nothing
243//
Chris Lattner3462ae32001-12-03 22:26:30 +0000244struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner61607ee2001-09-09 21:01:20 +0000245};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000246
247
248
249//===----------------------------------------------------------------------===//
250// BoolRules Class
251//===----------------------------------------------------------------------===//
252//
253// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
254//
Chris Lattner3462ae32001-12-03 22:26:30 +0000255struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000256
Chris Lattnere87f65e2002-07-30 16:24:28 +0000257 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
258 return ConstantBool::get(V1->getValue() & V2->getValue());
259 }
260
261 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000262 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263 }
264
Chris Lattnere87f65e2002-07-30 16:24:28 +0000265 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
266 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000268};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269
270
271//===----------------------------------------------------------------------===//
Chris Lattner977f0042001-11-01 05:55:13 +0000272// PointerRules Class
273//===----------------------------------------------------------------------===//
274//
275// PointerRules provides a concrete base class of ConstRules for pointer types
276//
Chris Lattner3462ae32001-12-03 22:26:30 +0000277struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000278 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000279 if (V->isNullValue()) return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000280 return 0; // Can't const prop other types of pointers
281 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000282 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000283 if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000284 return 0; // Can't const prop other types of pointers
285 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000286 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000287 if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000288 return 0; // Can't const prop other types of pointers
289 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000290 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000291 if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000292 return 0; // Can't const prop other types of pointers
293 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000294 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000295 if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000296 return 0; // Can't const prop other types of pointers
297 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000298 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000299 if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000300 return 0; // Can't const prop other types of pointers
301 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000302 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000303 if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000304 return 0; // Can't const prop other types of pointers
305 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000306 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000307 if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000308 return 0; // Can't const prop other types of pointers
309 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000310 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000311 if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000312 return 0; // Can't const prop other types of pointers
313 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000314 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000315 if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000316 return 0; // Can't const prop other types of pointers
317 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000318 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000319 if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000320 return 0; // Can't const prop other types of pointers
321 }
322
Chris Lattnere87f65e2002-07-30 16:24:28 +0000323 static ConstantPointer *CastToPointer(const ConstantPointer *V,
324 const PointerType *PTy) {
Chris Lattner62af86e2002-05-03 20:09:52 +0000325 if (V->getType() == PTy)
326 return const_cast<ConstantPointer*>(V); // Allow cast %PTy %ptr to %PTy
Chris Lattner977f0042001-11-01 05:55:13 +0000327 if (V->isNullValue())
Chris Lattner3462ae32001-12-03 22:26:30 +0000328 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000329 return 0; // Can't const prop other types of pointers
330 }
331};
332
333
334//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000335// DirectRules Class
336//===----------------------------------------------------------------------===//
337//
338// DirectRules provides a concrete base classes of ConstRules for a variety of
339// different types. This allows the C++ compiler to automatically generate our
340// constant handling operations in a typesafe and accurate manner.
341//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000342template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
343struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000344 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
345 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
346 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347 }
348
Chris Lattnere87f65e2002-07-30 16:24:28 +0000349 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
350 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
351 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352 }
353
Chris Lattnere87f65e2002-07-30 16:24:28 +0000354 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
355 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
356 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000357 }
358
Chris Lattnere87f65e2002-07-30 16:24:28 +0000359 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000360 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000361 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
362 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000363 }
364
Chris Lattnere87f65e2002-07-30 16:24:28 +0000365 static ConstantBool *LessThan(const ConstantClass *V1,
366 const ConstantClass *V2) {
367 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
368 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369 }
Chris Lattner55406842001-07-21 19:10:49 +0000370
Chris Lattnere87f65e2002-07-30 16:24:28 +0000371 static ConstantPointer *CastToPointer(const ConstantClass *V,
372 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000373 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000374 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000375 return 0; // Can't const prop other types of pointers
376 }
377
Chris Lattner55406842001-07-21 19:10:49 +0000378 // Casting operators. ick
379#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000380 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000381 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000382 }
383
Chris Lattner3462ae32001-12-03 22:26:30 +0000384 DEF_CAST(Bool , ConstantBool, bool)
385 DEF_CAST(SByte , ConstantSInt, signed char)
386 DEF_CAST(UByte , ConstantUInt, unsigned char)
387 DEF_CAST(Short , ConstantSInt, signed short)
388 DEF_CAST(UShort, ConstantUInt, unsigned short)
389 DEF_CAST(Int , ConstantSInt, signed int)
390 DEF_CAST(UInt , ConstantUInt, unsigned int)
391 DEF_CAST(Long , ConstantSInt, int64_t)
392 DEF_CAST(ULong , ConstantUInt, uint64_t)
393 DEF_CAST(Float , ConstantFP , float)
394 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000395#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000396};
397
Chris Lattner62af86e2002-05-03 20:09:52 +0000398
399//===----------------------------------------------------------------------===//
400// DirectIntRules Class
401//===----------------------------------------------------------------------===//
402//
403// DirectIntRules provides implementations of functions that are valid on
404// integer types, but not all types in general.
405//
406template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000407struct DirectIntRules
408 : public DirectRules<ConstantClass, BuiltinType, Ty,
409 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000410
Chris Lattnere87f65e2002-07-30 16:24:28 +0000411 static Constant *Rem(const ConstantClass *V1,
412 const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000413 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000414 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
415 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000416 }
Chris Lattner6670d862002-05-06 03:00:54 +0000417
Chris Lattnere87f65e2002-07-30 16:24:28 +0000418 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
419 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
420 return ConstantClass::get(*Ty, R);
421 }
422 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
423 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
424 return ConstantClass::get(*Ty, R);
425 }
426 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
427 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
428 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000429 }
430
Chris Lattnere87f65e2002-07-30 16:24:28 +0000431 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
432 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
433 return ConstantClass::get(*Ty, R);
434 }
435
436 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
437 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
438 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000439 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000440};
441
442
443//===----------------------------------------------------------------------===//
444// DirectFPRules Class
445//===----------------------------------------------------------------------===//
446//
447// DirectFPRules provides implementations of functions that are valid on
448// floating point types, but not all types in general.
449//
450template <class ConstantClass, class BuiltinType, Type **Ty>
451struct DirectFPRules
452 : public DirectRules<ConstantClass, BuiltinType, Ty,
453 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000454 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000455 if (V2->isNullValue()) return 0;
456 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
457 (BuiltinType)V2->getValue());
458 return ConstantClass::get(*Ty, Result);
459 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000460};
461
462
Chris Lattner2f7c9632001-06-06 20:29:01 +0000463//===----------------------------------------------------------------------===//
464// DirectRules Subclasses
465//===----------------------------------------------------------------------===//
466//
467// Given the DirectRules class we can now implement lots of types with little
468// code. Thank goodness C++ compilers are great at stomping out layers of
469// templates... can you imagine having to do this all by hand? (/me is lazy :)
470//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000471
472// ConstRules::find - Return the constant rules that take care of the specified
Chris Lattner61607ee2001-09-09 21:01:20 +0000473// type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000474//
Chris Lattner61607ee2001-09-09 21:01:20 +0000475Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) {
476 assert(AID == ConstRules::AID && "Bad annotation for factory!");
Chris Lattner8f191122001-10-01 18:26:53 +0000477 const Type *Ty = cast<Type>((const Value*)TyA);
Chris Lattner61607ee2001-09-09 21:01:20 +0000478
Chris Lattner2f7c9632001-06-06 20:29:01 +0000479 switch (Ty->getPrimitiveID()) {
Chris Lattner977f0042001-11-01 05:55:13 +0000480 case Type::BoolTyID: return new BoolRules();
481 case Type::PointerTyID: return new PointerRules();
Chris Lattner61607ee2001-09-09 21:01:20 +0000482 case Type::SByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000483 return new DirectIntRules<ConstantSInt, signed char , &Type::SByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000484 case Type::UByteTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000485 return new DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000486 case Type::ShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000487 return new DirectIntRules<ConstantSInt, signed short, &Type::ShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000488 case Type::UShortTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000489 return new DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000490 case Type::IntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000491 return new DirectIntRules<ConstantSInt, signed int , &Type::IntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000492 case Type::UIntTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000493 return new DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000494 case Type::LongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000495 return new DirectIntRules<ConstantSInt, int64_t , &Type::LongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000496 case Type::ULongTyID:
Chris Lattner62af86e2002-05-03 20:09:52 +0000497 return new DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000498 case Type::FloatTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000499 return new DirectFPRules<ConstantFP , float , &Type::FloatTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000500 case Type::DoubleTyID:
Chris Lattner0a144ad2002-05-03 21:41:07 +0000501 return new DirectFPRules<ConstantFP , double , &Type::DoubleTy>();
Chris Lattner61607ee2001-09-09 21:01:20 +0000502 default:
503 return new EmptyRules();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000505}