blob: 2ae554ee114bd9c00fab0f3e01f253166a591ec7 [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 Lattnerad70d4a2003-11-25 21:21:46 +000018#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner0a144ad2002-05-03 21:41:07 +000019#include <cmath>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000020using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000021
Chris Lattner1f0049c2003-04-17 19:24:18 +000022static unsigned getSize(const Type *Ty) {
23 unsigned S = Ty->getPrimitiveSize();
24 return S ? S : 8; // Treat pointers at 8 bytes
25}
26
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000027Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
28 const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000029 if (V->getType() == DestTy) return (Constant*)V;
30
31 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
32 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000033 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000034 // Try to not produce a cast of a cast, which is almost always redundant.
35 if (!Op->getType()->isFloatingPoint() &&
36 !CE->getType()->isFloatingPoint() &&
37 !DestTy->getType()->isFloatingPoint()) {
38 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
39 unsigned S3 = getSize(DestTy);
40 if (Op->getType() == DestTy && S3 >= S2)
41 return Op;
42 if (S1 >= S2 && S2 >= S3)
43 return ConstantExpr::getCast(Op, DestTy);
44 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
45 return ConstantExpr::getCast(Op, DestTy);
46 }
Chris Lattner941b06b2003-08-21 19:45:55 +000047 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
48 // If all of the indexes in the GEP are null values, there is no pointer
49 // adjustment going on. We might as well cast the source pointer.
50 bool isAllNull = true;
51 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
52 if (!CE->getOperand(i)->isNullValue()) {
53 isAllNull = false;
54 break;
55 }
56 if (isAllNull)
57 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +000058 }
59
Chris Lattnerf8348c32004-01-12 20:41:05 +000060 return ConstRules::get(V, V).castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +000061}
62
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000063Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
64 const Constant *V1,
65 const Constant *V2) {
Chris Lattnerf8348c32004-01-12 20:41:05 +000066 Constant *C;
Chris Lattner9f307732002-05-06 17:54:27 +000067 switch (Opcode) {
Chris Lattner9f307732002-05-06 17:54:27 +000068 default: return 0;
Chris Lattnerf8348c32004-01-12 20:41:05 +000069 case Instruction::Add: return ConstRules::get(V1, V2).add(V1, V2);
70 case Instruction::Sub: return ConstRules::get(V1, V2).sub(V1, V2);
71 case Instruction::Mul: return ConstRules::get(V1, V2).mul(V1, V2);
72 case Instruction::Div: return ConstRules::get(V1, V2).div(V1, V2);
73 case Instruction::Rem: return ConstRules::get(V1, V2).rem(V1, V2);
74 case Instruction::And: return ConstRules::get(V1, V2).op_and(V1, V2);
75 case Instruction::Or: return ConstRules::get(V1, V2).op_or (V1, V2);
76 case Instruction::Xor: return ConstRules::get(V1, V2).op_xor(V1, V2);
77
78 case Instruction::Shl: return ConstRules::get(V1, V2).shl(V1, V2);
79 case Instruction::Shr: return ConstRules::get(V1, V2).shr(V1, V2);
80
81 case Instruction::SetEQ: return ConstRules::get(V1, V2).equalto(V1, V2);
82 case Instruction::SetLT: return ConstRules::get(V1, V2).lessthan(V1, V2);
83 case Instruction::SetGT: return ConstRules::get(V1, V2).lessthan(V2, V1);
84 case Instruction::SetNE: // V1 != V2 === !(V1 == V2)
85 C = ConstRules::get(V1, V2).equalto(V1, V2);
86 break;
87 case Instruction::SetLE: // V1 <= V2 === !(V2 < V1)
88 C = ConstRules::get(V1, V2).lessthan(V2, V1);
89 break;
90 case Instruction::SetGE: // V1 >= V2 === !(V1 < V2)
91 C = ConstRules::get(V1, V2).lessthan(V1, V2);
92 break;
Chris Lattner9f307732002-05-06 17:54:27 +000093 }
Chris Lattnerf8348c32004-01-12 20:41:05 +000094
95 // If the folder broke out of the switch statement, invert the boolean
96 // constant value, if it exists, and return it.
97 if (!C) return 0;
98 return ConstantExpr::get(Instruction::Xor, ConstantBool::True, C);
Chris Lattner9f307732002-05-06 17:54:27 +000099}
100
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000101Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
102 const std::vector<Constant*> &IdxList) {
Chris Lattner1f0049c2003-04-17 19:24:18 +0000103 if (IdxList.size() == 0 ||
104 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
105 return const_cast<Constant*>(C);
106
Chris Lattner941b06b2003-08-21 19:45:55 +0000107 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000108
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000109
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000110 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000111 // Combine Indices - If the source pointer to this getelementptr instruction
112 // is a getelementptr instruction, combine the indices of the two
113 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000114 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000115 if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000116 const Type *LastTy = 0;
117 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
118 I != E; ++I)
119 LastTy = *I;
120
Chris Lattneraacf2a52004-01-11 23:56:33 +0000121 if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000122 std::vector<Constant*> NewIndices;
123 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000124 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000125 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000126
127 // Add the last index of the source with the first index of the new GEP.
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000128 // Make sure to handle the case when they are actually different types.
Chris Lattneraacf2a52004-01-11 23:56:33 +0000129 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
130 if (!IdxList[0]->isNullValue()) // Otherwise it must be an array
131 Combined =
132 ConstantExpr::get(Instruction::Add,
133 ConstantExpr::getCast(IdxList[0], Type::LongTy),
134 ConstantExpr::getCast(Combined, Type::LongTy));
135
Chris Lattnerddab2392003-08-20 16:11:27 +0000136 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000137 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
138 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
139 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000140 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000141
142 // Implement folding of:
143 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
144 // long 0, long 0)
145 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
146 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000147 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
148 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000149 if (const PointerType *SPT =
150 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
151 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
152 if (const ArrayType *CAT =
153 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
154 if (CAT->getElementType() == SAT->getElementType())
155 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000156 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000157 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000158 return 0;
159}
160
Chris Lattner9f307732002-05-06 17:54:27 +0000161
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162//===----------------------------------------------------------------------===//
163// TemplateRules Class
164//===----------------------------------------------------------------------===//
165//
166// TemplateRules - Implement a subclass of ConstRules that provides all
167// operations as noops. All other rules classes inherit from this class so
168// that if functionality is needed in the future, it can simply be added here
169// and to ConstRules without changing anything else...
170//
171// This class also provides subclasses with typesafe implementations of methods
172// so that don't have to do type casting.
173//
174template<class ArgType, class SubClassName>
175class TemplateRules : public ConstRules {
176
177 //===--------------------------------------------------------------------===//
178 // Redirecting functions that cast to the appropriate types
179 //===--------------------------------------------------------------------===//
180
Chris Lattnere87f65e2002-07-30 16:24:28 +0000181 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
183 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000184 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
186 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000187 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000188 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
189 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000190 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000191 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
192 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000193 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000194 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
195 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000196 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
197 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
198 }
199 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
200 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
201 }
202 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
203 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
204 }
205 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000206 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
207 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000208 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000209 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
210 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000211
Chris Lattner3462ae32001-12-03 22:26:30 +0000212 virtual ConstantBool *lessthan(const Constant *V1,
213 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000214 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
215 }
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000216 virtual ConstantBool *equalto(const Constant *V1,
217 const Constant *V2) const {
218 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
219 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220
Chris Lattner55406842001-07-21 19:10:49 +0000221 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000222 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000223 return SubClassName::CastToBool((const ArgType*)V);
224 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000225 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000226 return SubClassName::CastToSByte((const ArgType*)V);
227 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000228 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000229 return SubClassName::CastToUByte((const ArgType*)V);
230 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000231 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000232 return SubClassName::CastToShort((const ArgType*)V);
233 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000234 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000235 return SubClassName::CastToUShort((const ArgType*)V);
236 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000237 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000238 return SubClassName::CastToInt((const ArgType*)V);
239 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000240 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000241 return SubClassName::CastToUInt((const ArgType*)V);
242 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000243 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000244 return SubClassName::CastToLong((const ArgType*)V);
245 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000246 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000247 return SubClassName::CastToULong((const ArgType*)V);
248 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000249 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000250 return SubClassName::CastToFloat((const ArgType*)V);
251 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000252 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000253 return SubClassName::CastToDouble((const ArgType*)V);
254 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000255 virtual Constant *castToPointer(const Constant *V,
256 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000257 return SubClassName::CastToPointer((const ArgType*)V, Ty);
258 }
Chris Lattner55406842001-07-21 19:10:49 +0000259
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 //===--------------------------------------------------------------------===//
261 // Default "noop" implementations
262 //===--------------------------------------------------------------------===//
263
Chris Lattnere87f65e2002-07-30 16:24:28 +0000264 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
265 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
266 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
267 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
268 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
269 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
270 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
271 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
272 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
273 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
274 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000275 return 0;
276 }
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000277 static ConstantBool *EqualTo(const ArgType *V1, const ArgType *V2) {
278 return 0;
279 }
Chris Lattner55406842001-07-21 19:10:49 +0000280
281 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000282 static ConstantBool *CastToBool (const Constant *V) { return 0; }
283 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
284 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
285 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
286 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
287 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
288 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
289 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
290 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
291 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
292 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000293 static Constant *CastToPointer(const Constant *,
294 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295};
296
297
298
299//===----------------------------------------------------------------------===//
300// EmptyRules Class
301//===----------------------------------------------------------------------===//
302//
303// EmptyRules provides a concrete base class of ConstRules that does nothing
304//
Chris Lattner3462ae32001-12-03 22:26:30 +0000305struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000306 static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
307 if (V1 == V2) return ConstantBool::True;
308 return 0;
309 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000310};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311
312
313
314//===----------------------------------------------------------------------===//
315// BoolRules Class
316//===----------------------------------------------------------------------===//
317//
318// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
319//
Chris Lattner3462ae32001-12-03 22:26:30 +0000320struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321
Chris Lattner07507a42002-09-03 20:09:49 +0000322 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
323 return ConstantBool::get(V1->getValue() < V2->getValue());
324 }
325
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000326 static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
327 return ConstantBool::get(V1 == V2);
328 }
329
Chris Lattnere87f65e2002-07-30 16:24:28 +0000330 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
331 return ConstantBool::get(V1->getValue() & V2->getValue());
332 }
333
334 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000335 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000336 }
337
Chris Lattnere87f65e2002-07-30 16:24:28 +0000338 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
339 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000341
342 // Casting operators. ick
343#define DEF_CAST(TYPE, CLASS, CTYPE) \
344 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
345 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
346 }
347
348 DEF_CAST(Bool , ConstantBool, bool)
349 DEF_CAST(SByte , ConstantSInt, signed char)
350 DEF_CAST(UByte , ConstantUInt, unsigned char)
351 DEF_CAST(Short , ConstantSInt, signed short)
352 DEF_CAST(UShort, ConstantUInt, unsigned short)
353 DEF_CAST(Int , ConstantSInt, signed int)
354 DEF_CAST(UInt , ConstantUInt, unsigned int)
355 DEF_CAST(Long , ConstantSInt, int64_t)
356 DEF_CAST(ULong , ConstantUInt, uint64_t)
357 DEF_CAST(Float , ConstantFP , float)
358 DEF_CAST(Double, ConstantFP , double)
359#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000360};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000361
362
363//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000364// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000365//===----------------------------------------------------------------------===//
366//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000367// NullPointerRules provides a concrete base class of ConstRules for null
368// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000369//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000370struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000371 NullPointerRules> {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000372 static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
373 return ConstantBool::True; // Null pointers are always equal
374 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000375 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000376 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000377 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000378 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000379 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000380 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000381 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000382 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000383 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000384 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000385 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000386 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000387 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000388 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000389 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000390 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000391 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000392 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000393 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000394 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000395 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000396 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000397 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000398 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000399 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000400 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000401 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000402 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000403 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000404 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000405 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000406 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000407 }
408
Chris Lattner77f20dc2003-11-17 19:21:04 +0000409 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000410 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000411 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000412 }
413};
414
415
416//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000417// DirectRules Class
418//===----------------------------------------------------------------------===//
419//
420// DirectRules provides a concrete base classes of ConstRules for a variety of
421// different types. This allows the C++ compiler to automatically generate our
422// constant handling operations in a typesafe and accurate manner.
423//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000424template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
425struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000426 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
427 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
428 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000429 }
430
Chris Lattnere87f65e2002-07-30 16:24:28 +0000431 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
432 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
433 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434 }
435
Chris Lattnere87f65e2002-07-30 16:24:28 +0000436 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
437 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
438 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000439 }
440
Chris Lattnere87f65e2002-07-30 16:24:28 +0000441 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000442 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000443 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
444 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000445 }
446
Chris Lattnere87f65e2002-07-30 16:24:28 +0000447 static ConstantBool *LessThan(const ConstantClass *V1,
448 const ConstantClass *V2) {
449 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
450 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451 }
Chris Lattner55406842001-07-21 19:10:49 +0000452
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000453 static ConstantBool *EqualTo(const ConstantClass *V1,
454 const ConstantClass *V2) {
455 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
456 return ConstantBool::get(R);
457 }
458
Chris Lattner1f0049c2003-04-17 19:24:18 +0000459 static Constant *CastToPointer(const ConstantClass *V,
460 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000461 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000462 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000463 return 0; // Can't const prop other types of pointers
464 }
465
Chris Lattner55406842001-07-21 19:10:49 +0000466 // Casting operators. ick
467#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000468 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000469 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000470 }
471
Chris Lattner3462ae32001-12-03 22:26:30 +0000472 DEF_CAST(Bool , ConstantBool, bool)
473 DEF_CAST(SByte , ConstantSInt, signed char)
474 DEF_CAST(UByte , ConstantUInt, unsigned char)
475 DEF_CAST(Short , ConstantSInt, signed short)
476 DEF_CAST(UShort, ConstantUInt, unsigned short)
477 DEF_CAST(Int , ConstantSInt, signed int)
478 DEF_CAST(UInt , ConstantUInt, unsigned int)
479 DEF_CAST(Long , ConstantSInt, int64_t)
480 DEF_CAST(ULong , ConstantUInt, uint64_t)
481 DEF_CAST(Float , ConstantFP , float)
482 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000483#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000484};
485
Chris Lattner62af86e2002-05-03 20:09:52 +0000486
487//===----------------------------------------------------------------------===//
488// DirectIntRules Class
489//===----------------------------------------------------------------------===//
490//
491// DirectIntRules provides implementations of functions that are valid on
492// integer types, but not all types in general.
493//
494template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000495struct DirectIntRules
496 : public DirectRules<ConstantClass, BuiltinType, Ty,
497 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000498
Chris Lattner268916262003-05-12 15:26:25 +0000499 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
500 if (V2->isNullValue()) return 0;
501 if (V2->isAllOnesValue() && // MIN_INT / -1
502 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
503 return 0;
504 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
505 return ConstantClass::get(*Ty, R);
506 }
507
Chris Lattnere87f65e2002-07-30 16:24:28 +0000508 static Constant *Rem(const ConstantClass *V1,
509 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000510 if (V2->isNullValue()) return 0; // X / 0
511 if (V2->isAllOnesValue() && // MIN_INT / -1
512 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
513 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000514 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
515 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000516 }
Chris Lattner6670d862002-05-06 03:00:54 +0000517
Chris Lattnere87f65e2002-07-30 16:24:28 +0000518 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
519 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
520 return ConstantClass::get(*Ty, R);
521 }
522 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
523 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
524 return ConstantClass::get(*Ty, R);
525 }
526 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
527 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
528 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000529 }
530
Chris Lattnere87f65e2002-07-30 16:24:28 +0000531 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
532 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
533 return ConstantClass::get(*Ty, R);
534 }
535
536 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
537 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
538 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000539 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000540};
541
542
543//===----------------------------------------------------------------------===//
544// DirectFPRules Class
545//===----------------------------------------------------------------------===//
546//
547// DirectFPRules provides implementations of functions that are valid on
548// floating point types, but not all types in general.
549//
550template <class ConstantClass, class BuiltinType, Type **Ty>
551struct DirectFPRules
552 : public DirectRules<ConstantClass, BuiltinType, Ty,
553 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000554 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000555 if (V2->isNullValue()) return 0;
556 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
557 (BuiltinType)V2->getValue());
558 return ConstantClass::get(*Ty, Result);
559 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000560};
561
Chris Lattnerf8348c32004-01-12 20:41:05 +0000562ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000563 static EmptyRules EmptyR;
564 static BoolRules BoolR;
565 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000566 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
567 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
568 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
569 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
570 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
571 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
572 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
573 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
574 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
575 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000576
Chris Lattner4b6addf2003-11-17 19:19:32 +0000577 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
578 isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000579 return EmptyR;
580
Chris Lattnerf8348c32004-01-12 20:41:05 +0000581 switch (V1->getType()->getPrimitiveID()) {
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000582 default: assert(0 && "Unknown value type for constant folding!");
583 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000584 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000585 case Type::SByteTyID: return SByteR;
586 case Type::UByteTyID: return UByteR;
587 case Type::ShortTyID: return ShortR;
588 case Type::UShortTyID: return UShortR;
589 case Type::IntTyID: return IntR;
590 case Type::UIntTyID: return UIntR;
591 case Type::LongTyID: return LongR;
592 case Type::ULongTyID: return ULongR;
593 case Type::FloatTyID: return FloatR;
594 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596}