blob: c21529a010e5a13010c5a41c5e89238859bb0bee [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 Lattner9f307732002-05-06 17:54:27 +000022// ConstantFoldInstruction - Attempt to constant fold the specified instruction.
23// If successful, the constant result is returned, if not, null is returned.
24//
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000025Constant *llvm::ConstantFoldInstruction(Instruction *I) {
Chris Lattner78a0d422002-05-07 20:44:59 +000026 if (PHINode *PN = dyn_cast<PHINode>(I)) {
27 if (PN->getNumIncomingValues() == 0)
28 return Constant::getNullValue(PN->getType());
29
30 Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
31 if (Result == 0) return 0;
32
33 // Handle PHI nodes specially here...
34 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
35 if (PN->getIncomingValue(i) != Result)
36 return 0; // Not all the same incoming constants...
37
38 // If we reach here, all incoming values are the same constant.
39 return Result;
40 }
41
Chris Lattner9f307732002-05-06 17:54:27 +000042 Constant *Op0 = 0;
43 Constant *Op1 = 0;
44
45 if (I->getNumOperands() != 0) { // Get first operand if it's a constant...
46 Op0 = dyn_cast<Constant>(I->getOperand(0));
47 if (Op0 == 0) return 0; // Not a constant?, can't fold
48
49 if (I->getNumOperands() != 1) { // Get second operand if it's a constant...
50 Op1 = dyn_cast<Constant>(I->getOperand(1));
51 if (Op1 == 0) return 0; // Not a constant?, can't fold
52 }
53 }
54
Chris Lattner8acf3462003-05-27 19:16:07 +000055 if (isa<BinaryOperator>(I))
56 return ConstantExpr::get(I->getOpcode(), Op0, Op1);
57
Chris Lattner9f307732002-05-06 17:54:27 +000058 switch (I->getOpcode()) {
59 case Instruction::Cast:
Chris Lattner8acf3462003-05-27 19:16:07 +000060 return ConstantExpr::getCast(Op0, I->getType());
61 case Instruction::Shl:
62 case Instruction::Shr:
63 return ConstantExpr::getShift(I->getOpcode(), Op0, Op1);
Chris Lattner1f0049c2003-04-17 19:24:18 +000064 case Instruction::GetElementPtr: {
65 std::vector<Constant*> IdxList;
66 IdxList.reserve(I->getNumOperands()-1);
67 if (Op1) IdxList.push_back(Op1);
68 for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i)
69 if (Constant *C = dyn_cast<Constant>(I->getOperand(i)))
70 IdxList.push_back(C);
71 else
72 return 0; // Non-constant operand
Chris Lattner8acf3462003-05-27 19:16:07 +000073 return ConstantExpr::getGetElementPtr(Op0, IdxList);
Chris Lattner1f0049c2003-04-17 19:24:18 +000074 }
Chris Lattner9f307732002-05-06 17:54:27 +000075 default:
76 return 0;
77 }
78}
79
Chris Lattner1f0049c2003-04-17 19:24:18 +000080static unsigned getSize(const Type *Ty) {
81 unsigned S = Ty->getPrimitiveSize();
82 return S ? S : 8; // Treat pointers at 8 bytes
83}
84
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000085Constant *llvm::ConstantFoldCastInstruction(const Constant *V,
86 const Type *DestTy) {
Chris Lattner1f0049c2003-04-17 19:24:18 +000087 if (V->getType() == DestTy) return (Constant*)V;
88
89 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
90 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner55ed6562003-05-14 17:51:05 +000091 Constant *Op = const_cast<Constant*>(CE->getOperand(0));
Chris Lattner1f0049c2003-04-17 19:24:18 +000092 // Try to not produce a cast of a cast, which is almost always redundant.
93 if (!Op->getType()->isFloatingPoint() &&
94 !CE->getType()->isFloatingPoint() &&
95 !DestTy->getType()->isFloatingPoint()) {
96 unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
97 unsigned S3 = getSize(DestTy);
98 if (Op->getType() == DestTy && S3 >= S2)
99 return Op;
100 if (S1 >= S2 && S2 >= S3)
101 return ConstantExpr::getCast(Op, DestTy);
102 if (S1 <= S2 && S2 >= S3 && S1 <= S3)
103 return ConstantExpr::getCast(Op, DestTy);
104 }
Chris Lattner941b06b2003-08-21 19:45:55 +0000105 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
106 // If all of the indexes in the GEP are null values, there is no pointer
107 // adjustment going on. We might as well cast the source pointer.
108 bool isAllNull = true;
109 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
110 if (!CE->getOperand(i)->isNullValue()) {
111 isAllNull = false;
112 break;
113 }
114 if (isAllNull)
115 return ConstantExpr::getCast(CE->getOperand(0), DestTy);
Chris Lattner1f0049c2003-04-17 19:24:18 +0000116 }
117
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000118 return ConstRules::get(*V, *V).castTo(V, DestTy);
Chris Lattner9f307732002-05-06 17:54:27 +0000119}
120
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000121Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
122 const Constant *V1,
123 const Constant *V2) {
Chris Lattner9f307732002-05-06 17:54:27 +0000124 switch (Opcode) {
125 case Instruction::Add: return *V1 + *V2;
126 case Instruction::Sub: return *V1 - *V2;
127 case Instruction::Mul: return *V1 * *V2;
128 case Instruction::Div: return *V1 / *V2;
129 case Instruction::Rem: return *V1 % *V2;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000130 case Instruction::And: return *V1 & *V2;
131 case Instruction::Or: return *V1 | *V2;
132 case Instruction::Xor: return *V1 ^ *V2;
Chris Lattner9f307732002-05-06 17:54:27 +0000133
134 case Instruction::SetEQ: return *V1 == *V2;
135 case Instruction::SetNE: return *V1 != *V2;
136 case Instruction::SetLE: return *V1 <= *V2;
137 case Instruction::SetGE: return *V1 >= *V2;
138 case Instruction::SetLT: return *V1 < *V2;
139 case Instruction::SetGT: return *V1 > *V2;
140 }
141 return 0;
142}
143
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000144Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode,
145 const Constant *V1,
146 const Constant *V2) {
Chris Lattner9f307732002-05-06 17:54:27 +0000147 switch (Opcode) {
148 case Instruction::Shl: return *V1 << *V2;
149 case Instruction::Shr: return *V1 >> *V2;
150 default: return 0;
151 }
152}
153
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000154Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
155 const std::vector<Constant*> &IdxList) {
Chris Lattner1f0049c2003-04-17 19:24:18 +0000156 if (IdxList.size() == 0 ||
157 (IdxList.size() == 1 && IdxList[0]->isNullValue()))
158 return const_cast<Constant*>(C);
159
Chris Lattner941b06b2003-08-21 19:45:55 +0000160 // TODO If C is null and all idx's are null, return null of the right type.
Chris Lattner1f0049c2003-04-17 19:24:18 +0000161
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000162
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000163 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
Chris Lattnerddab2392003-08-20 16:11:27 +0000164 // Combine Indices - If the source pointer to this getelementptr instruction
165 // is a getelementptr instruction, combine the indices of the two
166 // getelementptr instructions into a single instruction.
Chris Lattner4ede64e2003-06-26 05:22:45 +0000167 //
Chris Lattnerddab2392003-08-20 16:11:27 +0000168 if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000169 const Type *LastTy = 0;
170 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
171 I != E; ++I)
172 LastTy = *I;
173
Chris Lattneraacf2a52004-01-11 23:56:33 +0000174 if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) {
Chris Lattner4ede64e2003-06-26 05:22:45 +0000175 std::vector<Constant*> NewIndices;
176 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
Chris Lattnerddab2392003-08-20 16:11:27 +0000177 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner4ede64e2003-06-26 05:22:45 +0000178 NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
Chris Lattnerddab2392003-08-20 16:11:27 +0000179
180 // Add the last index of the source with the first index of the new GEP.
Chris Lattnerad70d4a2003-11-25 21:21:46 +0000181 // Make sure to handle the case when they are actually different types.
Chris Lattneraacf2a52004-01-11 23:56:33 +0000182 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
183 if (!IdxList[0]->isNullValue()) // Otherwise it must be an array
184 Combined =
185 ConstantExpr::get(Instruction::Add,
186 ConstantExpr::getCast(IdxList[0], Type::LongTy),
187 ConstantExpr::getCast(Combined, Type::LongTy));
188
Chris Lattnerddab2392003-08-20 16:11:27 +0000189 NewIndices.push_back(Combined);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000190 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
191 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
192 }
Chris Lattnerddab2392003-08-20 16:11:27 +0000193 }
Chris Lattner4ede64e2003-06-26 05:22:45 +0000194
195 // Implement folding of:
196 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
197 // long 0, long 0)
198 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
199 //
Chris Lattner69f6af12003-05-14 02:47:13 +0000200 if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
201 IdxList[0]->isNullValue())
Chris Lattnerc2ceed12003-05-13 21:50:52 +0000202 if (const PointerType *SPT =
203 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
204 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
205 if (const ArrayType *CAT =
206 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
207 if (CAT->getElementType() == SAT->getElementType())
208 return ConstantExpr::getGetElementPtr(
Chris Lattner55ed6562003-05-14 17:51:05 +0000209 (Constant*)CE->getOperand(0), IdxList);
Chris Lattner4ede64e2003-06-26 05:22:45 +0000210 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000211 return 0;
212}
213
Chris Lattner9f307732002-05-06 17:54:27 +0000214
Chris Lattner2f7c9632001-06-06 20:29:01 +0000215//===----------------------------------------------------------------------===//
216// TemplateRules Class
217//===----------------------------------------------------------------------===//
218//
219// TemplateRules - Implement a subclass of ConstRules that provides all
220// operations as noops. All other rules classes inherit from this class so
221// that if functionality is needed in the future, it can simply be added here
222// and to ConstRules without changing anything else...
223//
224// This class also provides subclasses with typesafe implementations of methods
225// so that don't have to do type casting.
226//
227template<class ArgType, class SubClassName>
228class TemplateRules : public ConstRules {
229
230 //===--------------------------------------------------------------------===//
231 // Redirecting functions that cast to the appropriate types
232 //===--------------------------------------------------------------------===//
233
Chris Lattnere87f65e2002-07-30 16:24:28 +0000234 virtual Constant *add(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2);
236 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000237 virtual Constant *sub(const Constant *V1, const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238 return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2);
239 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000240 virtual Constant *mul(const Constant *V1, const Constant *V2) const {
Chris Lattner4f6031f2001-07-20 19:15:36 +0000241 return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2);
242 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000243 virtual Constant *div(const Constant *V1, const Constant *V2) const {
Chris Lattneraf259a72002-04-07 08:10:14 +0000244 return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2);
245 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000246 virtual Constant *rem(const Constant *V1, const Constant *V2) const {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000247 return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2);
248 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000249 virtual Constant *op_and(const Constant *V1, const Constant *V2) const {
250 return SubClassName::And((const ArgType *)V1, (const ArgType *)V2);
251 }
252 virtual Constant *op_or(const Constant *V1, const Constant *V2) const {
253 return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2);
254 }
255 virtual Constant *op_xor(const Constant *V1, const Constant *V2) const {
256 return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2);
257 }
258 virtual Constant *shl(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000259 return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2);
260 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000261 virtual Constant *shr(const Constant *V1, const Constant *V2) const {
Chris Lattner6670d862002-05-06 03:00:54 +0000262 return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2);
263 }
Chris Lattner4f6031f2001-07-20 19:15:36 +0000264
Chris Lattner3462ae32001-12-03 22:26:30 +0000265 virtual ConstantBool *lessthan(const Constant *V1,
266 const Constant *V2) const {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267 return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2);
268 }
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000269 virtual ConstantBool *equalto(const Constant *V1,
270 const Constant *V2) const {
271 return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2);
272 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273
Chris Lattner55406842001-07-21 19:10:49 +0000274 // Casting operators. ick
Chris Lattner3462ae32001-12-03 22:26:30 +0000275 virtual ConstantBool *castToBool(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000276 return SubClassName::CastToBool((const ArgType*)V);
277 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000278 virtual ConstantSInt *castToSByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000279 return SubClassName::CastToSByte((const ArgType*)V);
280 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000281 virtual ConstantUInt *castToUByte(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000282 return SubClassName::CastToUByte((const ArgType*)V);
283 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000284 virtual ConstantSInt *castToShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000285 return SubClassName::CastToShort((const ArgType*)V);
286 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000287 virtual ConstantUInt *castToUShort(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000288 return SubClassName::CastToUShort((const ArgType*)V);
289 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000290 virtual ConstantSInt *castToInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000291 return SubClassName::CastToInt((const ArgType*)V);
292 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000293 virtual ConstantUInt *castToUInt(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000294 return SubClassName::CastToUInt((const ArgType*)V);
295 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000296 virtual ConstantSInt *castToLong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000297 return SubClassName::CastToLong((const ArgType*)V);
298 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000299 virtual ConstantUInt *castToULong(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000300 return SubClassName::CastToULong((const ArgType*)V);
301 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000302 virtual ConstantFP *castToFloat(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000303 return SubClassName::CastToFloat((const ArgType*)V);
304 }
Chris Lattner3462ae32001-12-03 22:26:30 +0000305 virtual ConstantFP *castToDouble(const Constant *V) const {
Chris Lattner55406842001-07-21 19:10:49 +0000306 return SubClassName::CastToDouble((const ArgType*)V);
307 }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000308 virtual Constant *castToPointer(const Constant *V,
309 const PointerType *Ty) const {
Chris Lattner977f0042001-11-01 05:55:13 +0000310 return SubClassName::CastToPointer((const ArgType*)V, Ty);
311 }
Chris Lattner55406842001-07-21 19:10:49 +0000312
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313 //===--------------------------------------------------------------------===//
314 // Default "noop" implementations
315 //===--------------------------------------------------------------------===//
316
Chris Lattnere87f65e2002-07-30 16:24:28 +0000317 static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; }
318 static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; }
319 static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; }
320 static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; }
321 static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; }
322 static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; }
323 static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; }
324 static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; }
325 static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; }
326 static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; }
327 static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328 return 0;
329 }
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000330 static ConstantBool *EqualTo(const ArgType *V1, const ArgType *V2) {
331 return 0;
332 }
Chris Lattner55406842001-07-21 19:10:49 +0000333
334 // Casting operators. ick
Chris Lattnere87f65e2002-07-30 16:24:28 +0000335 static ConstantBool *CastToBool (const Constant *V) { return 0; }
336 static ConstantSInt *CastToSByte (const Constant *V) { return 0; }
337 static ConstantUInt *CastToUByte (const Constant *V) { return 0; }
338 static ConstantSInt *CastToShort (const Constant *V) { return 0; }
339 static ConstantUInt *CastToUShort(const Constant *V) { return 0; }
340 static ConstantSInt *CastToInt (const Constant *V) { return 0; }
341 static ConstantUInt *CastToUInt (const Constant *V) { return 0; }
342 static ConstantSInt *CastToLong (const Constant *V) { return 0; }
343 static ConstantUInt *CastToULong (const Constant *V) { return 0; }
344 static ConstantFP *CastToFloat (const Constant *V) { return 0; }
345 static ConstantFP *CastToDouble(const Constant *V) { return 0; }
Chris Lattner1f0049c2003-04-17 19:24:18 +0000346 static Constant *CastToPointer(const Constant *,
347 const PointerType *) {return 0;}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000348};
349
350
351
352//===----------------------------------------------------------------------===//
353// EmptyRules Class
354//===----------------------------------------------------------------------===//
355//
356// EmptyRules provides a concrete base class of ConstRules that does nothing
357//
Chris Lattner3462ae32001-12-03 22:26:30 +0000358struct EmptyRules : public TemplateRules<Constant, EmptyRules> {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000359 static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
360 if (V1 == V2) return ConstantBool::True;
361 return 0;
362 }
Chris Lattner61607ee2001-09-09 21:01:20 +0000363};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000364
365
366
367//===----------------------------------------------------------------------===//
368// BoolRules Class
369//===----------------------------------------------------------------------===//
370//
371// BoolRules provides a concrete base class of ConstRules for the 'bool' type.
372//
Chris Lattner3462ae32001-12-03 22:26:30 +0000373struct BoolRules : public TemplateRules<ConstantBool, BoolRules> {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374
Chris Lattner07507a42002-09-03 20:09:49 +0000375 static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){
376 return ConstantBool::get(V1->getValue() < V2->getValue());
377 }
378
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000379 static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
380 return ConstantBool::get(V1 == V2);
381 }
382
Chris Lattnere87f65e2002-07-30 16:24:28 +0000383 static Constant *And(const ConstantBool *V1, const ConstantBool *V2) {
384 return ConstantBool::get(V1->getValue() & V2->getValue());
385 }
386
387 static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000388 return ConstantBool::get(V1->getValue() | V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389 }
390
Chris Lattnere87f65e2002-07-30 16:24:28 +0000391 static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) {
392 return ConstantBool::get(V1->getValue() ^ V2->getValue());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000393 }
Chris Lattnercea4d8c2003-08-13 15:52:25 +0000394
395 // Casting operators. ick
396#define DEF_CAST(TYPE, CLASS, CTYPE) \
397 static CLASS *CastTo##TYPE (const ConstantBool *V) { \
398 return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \
399 }
400
401 DEF_CAST(Bool , ConstantBool, bool)
402 DEF_CAST(SByte , ConstantSInt, signed char)
403 DEF_CAST(UByte , ConstantUInt, unsigned char)
404 DEF_CAST(Short , ConstantSInt, signed short)
405 DEF_CAST(UShort, ConstantUInt, unsigned short)
406 DEF_CAST(Int , ConstantSInt, signed int)
407 DEF_CAST(UInt , ConstantUInt, unsigned int)
408 DEF_CAST(Long , ConstantSInt, int64_t)
409 DEF_CAST(ULong , ConstantUInt, uint64_t)
410 DEF_CAST(Float , ConstantFP , float)
411 DEF_CAST(Double, ConstantFP , double)
412#undef DEF_CAST
Chris Lattner61607ee2001-09-09 21:01:20 +0000413};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000414
415
416//===----------------------------------------------------------------------===//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000417// NullPointerRules Class
Chris Lattner977f0042001-11-01 05:55:13 +0000418//===----------------------------------------------------------------------===//
419//
Chris Lattner4b6addf2003-11-17 19:19:32 +0000420// NullPointerRules provides a concrete base class of ConstRules for null
421// pointers.
Chris Lattner977f0042001-11-01 05:55:13 +0000422//
Chris Lattner77f20dc2003-11-17 19:21:04 +0000423struct NullPointerRules : public TemplateRules<ConstantPointerNull,
Chris Lattner4b6addf2003-11-17 19:19:32 +0000424 NullPointerRules> {
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000425 static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) {
426 return ConstantBool::True; // Null pointers are always equal
427 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000428 static ConstantBool *CastToBool (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000429 return ConstantBool::False;
Chris Lattner977f0042001-11-01 05:55:13 +0000430 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000431 static ConstantSInt *CastToSByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000432 return ConstantSInt::get(Type::SByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000433 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000434 static ConstantUInt *CastToUByte (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000435 return ConstantUInt::get(Type::UByteTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000436 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000437 static ConstantSInt *CastToShort (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000438 return ConstantSInt::get(Type::ShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000439 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000440 static ConstantUInt *CastToUShort(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000441 return ConstantUInt::get(Type::UShortTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000442 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000443 static ConstantSInt *CastToInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000444 return ConstantSInt::get(Type::IntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000445 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000446 static ConstantUInt *CastToUInt (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000447 return ConstantUInt::get(Type::UIntTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000448 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000449 static ConstantSInt *CastToLong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000450 return ConstantSInt::get(Type::LongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000451 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000452 static ConstantUInt *CastToULong (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000453 return ConstantUInt::get(Type::ULongTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000454 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000455 static ConstantFP *CastToFloat (const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000456 return ConstantFP::get(Type::FloatTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000457 }
Chris Lattnere87f65e2002-07-30 16:24:28 +0000458 static ConstantFP *CastToDouble(const Constant *V) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000459 return ConstantFP::get(Type::DoubleTy, 0);
Chris Lattner977f0042001-11-01 05:55:13 +0000460 }
461
Chris Lattner77f20dc2003-11-17 19:21:04 +0000462 static Constant *CastToPointer(const ConstantPointerNull *V,
Chris Lattner1f0049c2003-04-17 19:24:18 +0000463 const PointerType *PTy) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000464 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000465 }
466};
467
468
469//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +0000470// DirectRules Class
471//===----------------------------------------------------------------------===//
472//
473// DirectRules provides a concrete base classes of ConstRules for a variety of
474// different types. This allows the C++ compiler to automatically generate our
475// constant handling operations in a typesafe and accurate manner.
476//
Chris Lattner0a144ad2002-05-03 21:41:07 +0000477template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass>
478struct DirectRules : public TemplateRules<ConstantClass, SuperClass> {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000479 static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) {
480 BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue();
481 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000482 }
483
Chris Lattnere87f65e2002-07-30 16:24:28 +0000484 static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) {
485 BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue();
486 return ConstantClass::get(*Ty, R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000487 }
488
Chris Lattnere87f65e2002-07-30 16:24:28 +0000489 static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) {
490 BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue();
491 return ConstantClass::get(*Ty, R);
Chris Lattner4f6031f2001-07-20 19:15:36 +0000492 }
493
Chris Lattnere87f65e2002-07-30 16:24:28 +0000494 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000495 if (V2->isNullValue()) return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000496 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
497 return ConstantClass::get(*Ty, R);
Chris Lattneraf259a72002-04-07 08:10:14 +0000498 }
499
Chris Lattnere87f65e2002-07-30 16:24:28 +0000500 static ConstantBool *LessThan(const ConstantClass *V1,
501 const ConstantClass *V2) {
502 bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue();
503 return ConstantBool::get(R);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504 }
Chris Lattner55406842001-07-21 19:10:49 +0000505
Chris Lattnerdc2e3912003-11-17 20:19:35 +0000506 static ConstantBool *EqualTo(const ConstantClass *V1,
507 const ConstantClass *V2) {
508 bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue();
509 return ConstantBool::get(R);
510 }
511
Chris Lattner1f0049c2003-04-17 19:24:18 +0000512 static Constant *CastToPointer(const ConstantClass *V,
513 const PointerType *PTy) {
Chris Lattner977f0042001-11-01 05:55:13 +0000514 if (V->isNullValue()) // Is it a FP or Integral null value?
Chris Lattner3462ae32001-12-03 22:26:30 +0000515 return ConstantPointerNull::get(PTy);
Chris Lattner977f0042001-11-01 05:55:13 +0000516 return 0; // Can't const prop other types of pointers
517 }
518
Chris Lattner55406842001-07-21 19:10:49 +0000519 // Casting operators. ick
520#define DEF_CAST(TYPE, CLASS, CTYPE) \
Chris Lattnere87f65e2002-07-30 16:24:28 +0000521 static CLASS *CastTo##TYPE (const ConstantClass *V) { \
Chris Lattnerbbb22962001-09-07 16:40:34 +0000522 return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \
Chris Lattner55406842001-07-21 19:10:49 +0000523 }
524
Chris Lattner3462ae32001-12-03 22:26:30 +0000525 DEF_CAST(Bool , ConstantBool, bool)
526 DEF_CAST(SByte , ConstantSInt, signed char)
527 DEF_CAST(UByte , ConstantUInt, unsigned char)
528 DEF_CAST(Short , ConstantSInt, signed short)
529 DEF_CAST(UShort, ConstantUInt, unsigned short)
530 DEF_CAST(Int , ConstantSInt, signed int)
531 DEF_CAST(UInt , ConstantUInt, unsigned int)
532 DEF_CAST(Long , ConstantSInt, int64_t)
533 DEF_CAST(ULong , ConstantUInt, uint64_t)
534 DEF_CAST(Float , ConstantFP , float)
535 DEF_CAST(Double, ConstantFP , double)
Chris Lattner55406842001-07-21 19:10:49 +0000536#undef DEF_CAST
Chris Lattner2f7c9632001-06-06 20:29:01 +0000537};
538
Chris Lattner62af86e2002-05-03 20:09:52 +0000539
540//===----------------------------------------------------------------------===//
541// DirectIntRules Class
542//===----------------------------------------------------------------------===//
543//
544// DirectIntRules provides implementations of functions that are valid on
545// integer types, but not all types in general.
546//
547template <class ConstantClass, class BuiltinType, Type **Ty>
Chris Lattner0a144ad2002-05-03 21:41:07 +0000548struct DirectIntRules
549 : public DirectRules<ConstantClass, BuiltinType, Ty,
550 DirectIntRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000551
Chris Lattner268916262003-05-12 15:26:25 +0000552 static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) {
553 if (V2->isNullValue()) return 0;
554 if (V2->isAllOnesValue() && // MIN_INT / -1
555 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
556 return 0;
557 BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue();
558 return ConstantClass::get(*Ty, R);
559 }
560
Chris Lattnere87f65e2002-07-30 16:24:28 +0000561 static Constant *Rem(const ConstantClass *V1,
562 const ConstantClass *V2) {
Chris Lattner268916262003-05-12 15:26:25 +0000563 if (V2->isNullValue()) return 0; // X / 0
564 if (V2->isAllOnesValue() && // MIN_INT / -1
565 (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue())
566 return 0;
Chris Lattnere87f65e2002-07-30 16:24:28 +0000567 BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue();
568 return ConstantClass::get(*Ty, R);
Chris Lattner0a144ad2002-05-03 21:41:07 +0000569 }
Chris Lattner6670d862002-05-06 03:00:54 +0000570
Chris Lattnere87f65e2002-07-30 16:24:28 +0000571 static Constant *And(const ConstantClass *V1, const ConstantClass *V2) {
572 BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue();
573 return ConstantClass::get(*Ty, R);
574 }
575 static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) {
576 BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue();
577 return ConstantClass::get(*Ty, R);
578 }
579 static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) {
580 BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue();
581 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000582 }
583
Chris Lattnere87f65e2002-07-30 16:24:28 +0000584 static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) {
585 BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue();
586 return ConstantClass::get(*Ty, R);
587 }
588
589 static Constant *Shr(const ConstantClass *V1, const ConstantClass *V2) {
590 BuiltinType R = (BuiltinType)V1->getValue() >> (BuiltinType)V2->getValue();
591 return ConstantClass::get(*Ty, R);
Chris Lattner6670d862002-05-06 03:00:54 +0000592 }
Chris Lattner0a144ad2002-05-03 21:41:07 +0000593};
594
595
596//===----------------------------------------------------------------------===//
597// DirectFPRules Class
598//===----------------------------------------------------------------------===//
599//
600// DirectFPRules provides implementations of functions that are valid on
601// floating point types, but not all types in general.
602//
603template <class ConstantClass, class BuiltinType, Type **Ty>
604struct DirectFPRules
605 : public DirectRules<ConstantClass, BuiltinType, Ty,
606 DirectFPRules<ConstantClass, BuiltinType, Ty> > {
Chris Lattnere87f65e2002-07-30 16:24:28 +0000607 static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) {
Chris Lattner0a144ad2002-05-03 21:41:07 +0000608 if (V2->isNullValue()) return 0;
609 BuiltinType Result = std::fmod((BuiltinType)V1->getValue(),
610 (BuiltinType)V2->getValue());
611 return ConstantClass::get(*Ty, Result);
612 }
Chris Lattner62af86e2002-05-03 20:09:52 +0000613};
614
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000615ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) {
Chris Lattner4b6addf2003-11-17 19:19:32 +0000616 static EmptyRules EmptyR;
617 static BoolRules BoolR;
618 static NullPointerRules NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000619 static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR;
620 static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR;
621 static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR;
622 static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR;
623 static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR;
624 static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR;
625 static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR;
626 static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR;
627 static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR;
628 static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629
Chris Lattner4b6addf2003-11-17 19:19:32 +0000630 if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
631 isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000632 return EmptyR;
633
634 // FIXME: This assert doesn't work because shifts pass both operands in to
635 // check for constant exprs. :(
636 //assert(V1.getType() == V2.getType() &&"Nonequal types to constant folder?");
637
638 switch (V1.getType()->getPrimitiveID()) {
639 default: assert(0 && "Unknown value type for constant folding!");
640 case Type::BoolTyID: return BoolR;
Chris Lattner4b6addf2003-11-17 19:19:32 +0000641 case Type::PointerTyID: return NullPointerR;
Chris Lattner9d9cbcf2003-11-17 19:05:17 +0000642 case Type::SByteTyID: return SByteR;
643 case Type::UByteTyID: return UByteR;
644 case Type::ShortTyID: return ShortR;
645 case Type::UShortTyID: return UShortR;
646 case Type::IntTyID: return IntR;
647 case Type::UIntTyID: return UIntR;
648 case Type::LongTyID: return LongR;
649 case Type::ULongTyID: return ULongR;
650 case Type::FloatTyID: return FloatR;
651 case Type::DoubleTyID: return DoubleR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000653}