blob: ddec284942a238ae7665bb56b6a184b4c384ad81 [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 Lattner2361bd02004-01-12 20:48:11 +000014#include "ConstantHandling.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000015#include "llvm/Constants.h"
Chris Lattner78a0d422002-05-07 20:44:59 +000016#include "llvm/iPHINode.h"
Chris Lattner8acf3462003-05-27 19:16:07 +000017#include "llvm/InstrTypes.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerad70d4a2003-11-25 21:21:46 +000019#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000020#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000021using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000022
Chris Lattner1f0049c2003-04-17 19:24:18 +000023static unsigned getSize(const Type *Ty) {
24 unsigned S = Ty->getPrimitiveSize();
25 return S ? S : 8; // Treat pointers at 8 bytes
26}
27
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000028Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
29 const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000030 if (V->getType() == DestTy) return (Constant*)V;
31
32 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
33 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000034 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000035 // Try to not produce a cast of a cast, which is almost always redundant.
36 if (!Op->getType()->isFloatingPoint() &&
37 !CE->getType()->isFloatingPoint() &&
38 !DestTy->getType()->isFloatingPoint()) {
39 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
40 unsigned S3 = getSize(DestTy);
41 if (Op->getType() == DestTy && S3 >= S2)
42 return Op;
43 if (S1 >= S2 && S2 >= S3)
44 return ConstantExpr::getCast(Op, DestTy);
45 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
46 return ConstantExpr::getCast(Op, DestTy);
47 }
Chris Lattner941b06b2003-08-21 19:45:55 +000048 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
49 // If all of the indexes in the GEP are null values, there is no pointer
50 // adjustment going on. We might as well cast the source pointer.
51 bool isAllNull = true;
52 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
53 if (!CE->getOperand(i)->isNullValue()) {
54 isAllNull = false;
55 break;
56 }
57 if (isAllNull)
58 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +000059 }
60
Chris Lattner6ff6cea2004-01-12 21:02:29 +000061 ConstRules &Rules = ConstRules::get(V, V);
62
63 switch (DestTy->getPrimitiveID()) {
64 case Type::BoolTyID: return Rules.castToBool(V);
65 case Type::UByteTyID: return Rules.castToUByte(V);
66 case Type::SByteTyID: return Rules.castToSByte(V);
67 case Type::UShortTyID: return Rules.castToUShort(V);
68 case Type::ShortTyID: return Rules.castToShort(V);
69 case Type::UIntTyID: return Rules.castToUInt(V);
70 case Type::IntTyID: return Rules.castToInt(V);
71 case Type::ULongTyID: return Rules.castToULong(V);
72 case Type::LongTyID: return Rules.castToLong(V);
73 case Type::FloatTyID: return Rules.castToFloat(V);
74 case Type::DoubleTyID: return Rules.castToDouble(V);
75 case Type::PointerTyID:
76 return Rules.castToPointer(V, cast<PointerType>(DestTy));
77 default: return 0;
78 }
Chris Lattner9f307732002-05-06 17:54:27 +000079}
80
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000081Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
82 const Constant *V1,
83 const Constant *V2) {
Chris Lattnerf8348c32004-01-12 20:41:05 +000084 Constant *C;
Chris Lattner9f307732002-05-06 17:54:27 +000085 switch (Opcode) {
Chris Lattner9f307732002-05-06 17:54:27 +000086 default: return 0;
Chris Lattnerf8348c32004-01-12 20:41:05 +000087 case Instruction::Add: return ConstRules::get(V1, V2).add(V1, V2);
88 case Instruction::Sub: return ConstRules::get(V1, V2).sub(V1, V2);
89 case Instruction::Mul: return ConstRules::get(V1, V2).mul(V1, V2);
90 case Instruction::Div: return ConstRules::get(V1, V2).div(V1, V2);
91 case Instruction::Rem: return ConstRules::get(V1, V2).rem(V1, V2);
92 case Instruction::And: return ConstRules::get(V1, V2).op_and(V1, V2);
93 case Instruction::Or: return ConstRules::get(V1, V2).op_or (V1, V2);
94 case Instruction::Xor: return ConstRules::get(V1, V2).op_xor(V1, V2);
95
96 case Instruction::Shl: return ConstRules::get(V1, V2).shl(V1, V2);
97 case Instruction::Shr: return ConstRules::get(V1, V2).shr(V1, V2);
98
99 case Instruction::SetEQ: return ConstRules::get(V1, V2).equalto(V1, V2);
100 case Instruction::SetLT: return ConstRules::get(V1, V2).lessthan(V1, V2);
101 case Instruction::SetGT: return ConstRules::get(V1, V2).lessthan(V2, V1);
102 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
103 C = ConstRules::get(V1, V2).equalto(V1, V2);
104 break;
105 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
106 C = ConstRules::get(V1, V2).lessthan(V2, V1);
107 break;
108 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
109 C = ConstRules::get(V1, V2).lessthan(V1, V2);
110 break;
Chris Lattner9f307732002-05-06 17:54:27 +0000111 }
Chris Lattnerf8348c32004-01-12 20:41:05 +0000112
113 // If the folder broke out of the switch statement, invert the boolean
114 // constant value, if it exists, and return it.
115 if (!C) return 0;
116 return ConstantExpr::get(Instruction::Xor, ConstantBool::True, C);
Chris Lattner9f307732002-05-06 17:54:27 +0000117}
118
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000119Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
120 const std::vector<Constant*> &IdxList) {
Chris Lattner1f0049c2003-04-17 19:24:18 +0000121 if (IdxList.size() == 0 ||
122 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
123 return const_cast<Constant*>(C);
124
Chris Lattner941b06b2003-08-21 19:45:55 +0000125 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000126
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000127
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000128 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000129 // Combine Indices - If the source pointer to this getelementptr instruction
130 // is a getelementptr instruction, combine the indices of the two
131 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000132 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000133 if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000134 const Type *LastTy = 0;
135 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
136 I != E; ++I)
137 LastTy = *I;
138
Chris Lattneraacf2a52004-01-11 23:56:33 +0000139 if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000140 std::vector<Constant*> NewIndices;
141 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000142 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000143 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000144
145 // Add the last index of the source with the first index of the new GEP.
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000146 // Make sure to handle the case when they are actually different types.
Chris Lattneraacf2a52004-01-11 23:56:33 +0000147 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
148 if (!IdxList[0]->isNullValue()) // Otherwise it must be an array
149 Combined =
150 ConstantExpr::get(Instruction::Add,
151 ConstantExpr::getCast(IdxList[0], Type::LongTy),
152 ConstantExpr::getCast(Combined, Type::LongTy));
153
Chris Lattnerddab2392003-08-20 16:11:27 +0000154 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000155 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
156 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
157 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000158 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000159
160 // Implement folding of:
161 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
162 // long 0, long 0)
163 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
164 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000165 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
166 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000167 if (const PointerType *SPT =
168 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
169 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
170 if (const ArrayType *CAT =
171 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
172 if (CAT->getElementType() == SAT->getElementType())
173 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000174 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000175 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000176 return 0;
177}
178
Chris Lattner9f307732002-05-06 17:54:27 +0000179
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180//===----------------------------------------------------------------------===//
181// TemplateRules Class
182//===----------------------------------------------------------------------===//
183//
184// TemplateRules - Implement a subclass of ConstRules that provides all
185// operations as noops. All other rules classes inherit from this class so
186// that if functionality is needed in the future, it can simply be added here
187// and to ConstRules without changing anything else...
188//
189// This class also provides subclasses with typesafe implementations of methods
190// so that don't have to do type casting.
191//
192template<class ArgType, class SubClassName>
193class TemplateRules : public ConstRules {
194
195 //===--------------------------------------------------------------------===//
196 // Redirecting functions that cast to the appropriate types
197 //===--------------------------------------------------------------------===//
198
Chris Lattnere87f65e2002-07-30 16:24:28 +0000199 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000200 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
201 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000202 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
204 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000205 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000206 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
207 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000208 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000209 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
210 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000211 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000212 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
213 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000214 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
215 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
216 }
217 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
218 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
219 }
220 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
221 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
222 }
223 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000224 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
225 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000226 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000227 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
228 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000229
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000230 virtual Constant *lessthan(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
232 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000233 virtual Constant *equalto(const Constant *V1, const Constant *V2) const {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000234 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
235 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236
Chris Lattner55406842001-07-21 19:10:49 +0000237 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000238 virtual Constant *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000239 return SubClassName::CastToBool((const ArgType*)V);
240 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000241 virtual Constant *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000242 return SubClassName::CastToSByte((const ArgType*)V);
243 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000244 virtual Constant *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000245 return SubClassName::CastToUByte((const ArgType*)V);
246 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000247 virtual Constant *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000248 return SubClassName::CastToShort((const ArgType*)V);
249 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000250 virtual Constant *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000251 return SubClassName::CastToUShort((const ArgType*)V);
252 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000253 virtual Constant *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000254 return SubClassName::CastToInt((const ArgType*)V);
255 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000256 virtual Constant *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000257 return SubClassName::CastToUInt((const ArgType*)V);
258 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000259 virtual Constant *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000260 return SubClassName::CastToLong((const ArgType*)V);
261 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000262 virtual Constant *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000263 return SubClassName::CastToULong((const ArgType*)V);
264 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000265 virtual Constant *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000266 return SubClassName::CastToFloat((const ArgType*)V);
267 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000268 virtual Constant *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000269 return SubClassName::CastToDouble((const ArgType*)V);
270 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000271 virtual Constant *castToPointer(const Constant *V,
272 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000273 return SubClassName::CastToPointer((const ArgType*)V, Ty);
274 }
Chris Lattner55406842001-07-21 19:10:49 +0000275
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276 //===--------------------------------------------------------------------===//
277 // Default "noop" implementations
278 //===--------------------------------------------------------------------===//
279
Chris Lattnere87f65e2002-07-30 16:24:28 +0000280 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
281 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
282 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
283 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
284 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
285 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
286 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
287 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
288 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
289 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000290 static Constant *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291 return 0;
292 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000293 static Constant *EqualTo(const ArgType *V1, const ArgType *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000294 return 0;
295 }
Chris Lattner55406842001-07-21 19:10:49 +0000296
297 // Casting operators. ick
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000298 static Constant *CastToBool (const Constant *V) { return 0; }
299 static Constant *CastToSByte (const Constant *V) { return 0; }
300 static Constant *CastToUByte (const Constant *V) { return 0; }
301 static Constant *CastToShort (const Constant *V) { return 0; }
302 static Constant *CastToUShort(const Constant *V) { return 0; }
303 static Constant *CastToInt (const Constant *V) { return 0; }
304 static Constant *CastToUInt (const Constant *V) { return 0; }
305 static Constant *CastToLong (const Constant *V) { return 0; }
306 static Constant *CastToULong (const Constant *V) { return 0; }
307 static Constant *CastToFloat (const Constant *V) { return 0; }
308 static Constant *CastToDouble(const Constant *V) { return 0; }
309 static Constant *CastToPointer(const Constant *,
310 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311};
312
313
314
315//===----------------------------------------------------------------------===//
316// EmptyRules Class
317//===----------------------------------------------------------------------===//
318//
319// EmptyRules provides a concrete base class of ConstRules that does nothing
320//
Chris Lattner3462ae32001-12-03 22:26:30 +0000321struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000322 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000323 if (V1 == V2) return ConstantBool::True;
324 return 0;
325 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000326};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000327
328
329
330//===----------------------------------------------------------------------===//
331// BoolRules Class
332//===----------------------------------------------------------------------===//
333//
334// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
335//
Chris Lattner3462ae32001-12-03 22:26:30 +0000336struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000337
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000338 static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){
Chris Lattner07507a42002-09-03 20:09:49 +0000339 return ConstantBool::get(V1->getValue() < V2->getValue());
340 }
341
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000342 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000343 return ConstantBool::get(V1 == V2);
344 }
345
Chris Lattnere87f65e2002-07-30 16:24:28 +0000346 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
347 return ConstantBool::get(V1->getValue() & V2->getValue());
348 }
349
350 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000351 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352 }
353
Chris Lattnere87f65e2002-07-30 16:24:28 +0000354 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
355 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000356 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000357
358 // Casting operators. ick
359#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000360 static Constant *CastTo##TYPE (const ConstantBool *V) { \
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000361 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
362 }
363
364 DEF_CAST(Bool , ConstantBool, bool)
365 DEF_CAST(SByte , ConstantSInt, signed char)
366 DEF_CAST(UByte , ConstantUInt, unsigned char)
367 DEF_CAST(Short , ConstantSInt, signed short)
368 DEF_CAST(UShort, ConstantUInt, unsigned short)
369 DEF_CAST(Int , ConstantSInt, signed int)
370 DEF_CAST(UInt , ConstantUInt, unsigned int)
371 DEF_CAST(Long , ConstantSInt, int64_t)
372 DEF_CAST(ULong , ConstantUInt, uint64_t)
373 DEF_CAST(Float , ConstantFP , float)
374 DEF_CAST(Double, ConstantFP , double)
375#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000376};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000377
378
379//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000380// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000381//===----------------------------------------------------------------------===//
382//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000383// NullPointerRules provides a concrete base class of ConstRules for null
384// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000385//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000386struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000387 NullPointerRules> {
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000388 static Constant *EqualTo(const Constant *V1, const Constant *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000389 return ConstantBool::True; // Null pointers are always equal
390 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000391 static Constant *CastToBool(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000392 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000393 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000394 static Constant *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000395 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000396 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000397 static Constant *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000398 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000399 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000400 static Constant *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000401 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000402 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000403 static Constant *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000404 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000405 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000406 static Constant *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000407 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000408 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000409 static Constant *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000410 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000411 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000412 static Constant *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000413 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000414 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000415 static Constant *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000416 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000417 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000418 static Constant *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000419 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000420 }
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000421 static Constant *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000422 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000423 }
424
Chris Lattner77f20dc2003-11-17 19:21:04 +0000425 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000426 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000427 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000428 }
429};
430
431
432//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000433// DirectRules Class
434//===----------------------------------------------------------------------===//
435//
436// DirectRules provides a concrete base classes of ConstRules for a variety of
437// different types. This allows the C++ compiler to automatically generate our
438// constant handling operations in a typesafe and accurate manner.
439//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000440template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
441struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000442 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
443 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
444 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000445 }
446
Chris Lattnere87f65e2002-07-30 16:24:28 +0000447 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
448 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
449 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000450 }
451
Chris Lattnere87f65e2002-07-30 16:24:28 +0000452 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
453 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
454 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000455 }
456
Chris Lattnere87f65e2002-07-30 16:24:28 +0000457 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000458 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000459 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
460 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000461 }
462
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000463 static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000464 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
465 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466 }
Chris Lattner55406842001-07-21 19:10:49 +0000467
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000468 static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000469 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
470 return ConstantBool::get(R);
471 }
472
Chris Lattner1f0049c2003-04-17 19:24:18 +0000473 static Constant *CastToPointer(const ConstantClass *V,
474 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000475 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000476 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000477 return 0; // Can't const prop other types of pointers
478 }
479
Chris Lattner55406842001-07-21 19:10:49 +0000480 // Casting operators. ick
481#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattner6ff6cea2004-01-12 21:02:29 +0000482 static Constant *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000483 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000484 }
485
Chris Lattner3462ae32001-12-03 22:26:30 +0000486 DEF_CAST(Bool , ConstantBool, bool)
487 DEF_CAST(SByte , ConstantSInt, signed char)
488 DEF_CAST(UByte , ConstantUInt, unsigned char)
489 DEF_CAST(Short , ConstantSInt, signed short)
490 DEF_CAST(UShort, ConstantUInt, unsigned short)
491 DEF_CAST(Int , ConstantSInt, signed int)
492 DEF_CAST(UInt , ConstantUInt, unsigned int)
493 DEF_CAST(Long , ConstantSInt, int64_t)
494 DEF_CAST(ULong , ConstantUInt, uint64_t)
495 DEF_CAST(Float , ConstantFP , float)
496 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000497#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000498};
499
Chris Lattner62af86e2002-05-03 20:09:52 +0000500
501//===----------------------------------------------------------------------===//
502// DirectIntRules Class
503//===----------------------------------------------------------------------===//
504//
505// DirectIntRules provides implementations of functions that are valid on
506// integer types, but not all types in general.
507//
508template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000509struct DirectIntRules
510 : public DirectRules<ConstantClass, BuiltinType, Ty,
511 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000512
Chris Lattner268916262003-05-12 15:26:25 +0000513 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
514 if (V2->isNullValue()) return 0;
515 if (V2->isAllOnesValue() && // MIN_INT / -1
516 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
517 return 0;
518 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
519 return ConstantClass::get(*Ty, R);
520 }
521
Chris Lattnere87f65e2002-07-30 16:24:28 +0000522 static Constant *Rem(const ConstantClass *V1,
523 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000524 if (V2->isNullValue()) return 0; // X / 0
525 if (V2->isAllOnesValue() && // MIN_INT / -1
526 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
527 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000528 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
529 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000530 }
Chris Lattner6670d862002-05-06 03:00:54 +0000531
Chris Lattnere87f65e2002-07-30 16:24:28 +0000532 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
533 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
534 return ConstantClass::get(*Ty, R);
535 }
536 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
537 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
538 return ConstantClass::get(*Ty, R);
539 }
540 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
541 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
542 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000543 }
544
Chris Lattnere87f65e2002-07-30 16:24:28 +0000545 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
546 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
547 return ConstantClass::get(*Ty, R);
548 }
549
550 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
551 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
552 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000553 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000554};
555
556
557//===----------------------------------------------------------------------===//
558// DirectFPRules Class
559//===----------------------------------------------------------------------===//
560//
561// DirectFPRules provides implementations of functions that are valid on
562// floating point types, but not all types in general.
563//
564template <class ConstantClass, class BuiltinType, Type **Ty>
565struct DirectFPRules
566 : public DirectRules<ConstantClass, BuiltinType, Ty,
567 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000568 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000569 if (V2->isNullValue()) return 0;
570 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
571 (BuiltinType)V2->getValue());
572 return ConstantClass::get(*Ty, Result);
573 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000574};
575
Chris Lattnerf8348c32004-01-12 20:41:05 +0000576ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000577 static EmptyRules EmptyR;
578 static BoolRules BoolR;
579 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000580 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
581 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
582 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
583 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
584 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
585 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
586 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
587 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
588 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
589 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590
Chris Lattner4b6addf2003-11-17 19:19:32 +0000591 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
592 isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000593 return EmptyR;
594
Chris Lattnerf8348c32004-01-12 20:41:05 +0000595 switch (V1->getType()->getPrimitiveID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000596 default: assert(0 && "Unknown value type for constant folding!");
597 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000598 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000599 case Type::SByteTyID: return SByteR;
600 case Type::UByteTyID: return UByteR;
601 case Type::ShortTyID: return ShortR;
602 case Type::UShortTyID: return UShortR;
603 case Type::IntTyID: return IntR;
604 case Type::UIntTyID: return UIntR;
605 case Type::LongTyID: return LongR;
606 case Type::ULongTyID: return ULongR;
607 case Type::FloatTyID: return FloatR;
608 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000609 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000610}