Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===- ConstantHandling.cpp - Implement ConstantHandling.h ----------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the various intrinsic operations, on constant values. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 65b529f | 2002-04-08 20:18:09 +0000 | [diff] [blame] | 14 | #include "llvm/ConstantHandling.h" |
Chris Lattner | 78a0d42 | 2002-05-07 20:44:59 +0000 | [diff] [blame] | 15 | #include "llvm/iPHINode.h" |
Chris Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 16 | #include "llvm/InstrTypes.h" |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 19 | #include <cmath> |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 22 | // ConstantFoldInstruction - Attempt to constant fold the specified instruction. |
| 23 | // If successful, the constant result is returned, if not, null is returned. |
| 24 | // |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 25 | Constant *llvm::ConstantFoldInstruction(Instruction *I) { |
Chris Lattner | 78a0d42 | 2002-05-07 20:44:59 +0000 | [diff] [blame] | 26 | 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 Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 42 | 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 Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 55 | if (isa<BinaryOperator>(I)) |
| 56 | return ConstantExpr::get(I->getOpcode(), Op0, Op1); |
| 57 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 58 | switch (I->getOpcode()) { |
| 59 | case Instruction::Cast: |
Chris Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 60 | return ConstantExpr::getCast(Op0, I->getType()); |
| 61 | case Instruction::Shl: |
| 62 | case Instruction::Shr: |
| 63 | return ConstantExpr::getShift(I->getOpcode(), Op0, Op1); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 64 | 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 Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 73 | return ConstantExpr::getGetElementPtr(Op0, IdxList); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 75 | default: |
| 76 | return 0; |
| 77 | } |
| 78 | } |
| 79 | |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 80 | static unsigned getSize(const Type *Ty) { |
| 81 | unsigned S = Ty->getPrimitiveSize(); |
| 82 | return S ? S : 8; // Treat pointers at 8 bytes |
| 83 | } |
| 84 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 85 | Constant *llvm::ConstantFoldCastInstruction(const Constant *V, |
| 86 | const Type *DestTy) { |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 87 | if (V->getType() == DestTy) return (Constant*)V; |
| 88 | |
| 89 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 90 | if (CE->getOpcode() == Instruction::Cast) { |
Chris Lattner | 55ed656 | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 91 | Constant *Op = const_cast<Constant*>(CE->getOperand(0)); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 92 | // 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 Lattner | 941b06b | 2003-08-21 19:45:55 +0000 | [diff] [blame] | 105 | } 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 Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 118 | return ConstRules::get(*V, *V).castTo(V, DestTy); |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 121 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 122 | const Constant *V1, |
| 123 | const Constant *V2) { |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 124 | 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 Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 130 | case Instruction::And: return *V1 & *V2; |
| 131 | case Instruction::Or: return *V1 | *V2; |
| 132 | case Instruction::Xor: return *V1 ^ *V2; |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 133 | |
| 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 Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 144 | Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode, |
| 145 | const Constant *V1, |
| 146 | const Constant *V2) { |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 147 | switch (Opcode) { |
| 148 | case Instruction::Shl: return *V1 << *V2; |
| 149 | case Instruction::Shr: return *V1 >> *V2; |
| 150 | default: return 0; |
| 151 | } |
| 152 | } |
| 153 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 154 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
| 155 | const std::vector<Constant*> &IdxList) { |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 156 | if (IdxList.size() == 0 || |
| 157 | (IdxList.size() == 1 && IdxList[0]->isNullValue())) |
| 158 | return const_cast<Constant*>(C); |
| 159 | |
Chris Lattner | 941b06b | 2003-08-21 19:45:55 +0000 | [diff] [blame] | 160 | // TODO If C is null and all idx's are null, return null of the right type. |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 161 | |
Chris Lattner | c2ceed1 | 2003-05-13 21:50:52 +0000 | [diff] [blame] | 162 | |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 163 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 164 | // 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 Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 167 | // |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 168 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 169 | 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 Lattner | aacf2a5 | 2004-01-11 23:56:33 +0000 | [diff] [blame^] | 174 | if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) { |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 175 | std::vector<Constant*> NewIndices; |
| 176 | NewIndices.reserve(IdxList.size() + CE->getNumOperands()); |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 177 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 178 | NewIndices.push_back(cast<Constant>(CE->getOperand(i))); |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 179 | |
| 180 | // Add the last index of the source with the first index of the new GEP. |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 181 | // Make sure to handle the case when they are actually different types. |
Chris Lattner | aacf2a5 | 2004-01-11 23:56:33 +0000 | [diff] [blame^] | 182 | 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 Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 189 | NewIndices.push_back(Combined); |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 190 | NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); |
| 191 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); |
| 192 | } |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 194 | |
| 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 Lattner | 69f6af1 | 2003-05-14 02:47:13 +0000 | [diff] [blame] | 200 | if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 && |
| 201 | IdxList[0]->isNullValue()) |
Chris Lattner | c2ceed1 | 2003-05-13 21:50:52 +0000 | [diff] [blame] | 202 | 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 Lattner | 55ed656 | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 209 | (Constant*)CE->getOperand(0), IdxList); |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 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 | // |
| 227 | template<class ArgType, class SubClassName> |
| 228 | class TemplateRules : public ConstRules { |
| 229 | |
| 230 | //===--------------------------------------------------------------------===// |
| 231 | // Redirecting functions that cast to the appropriate types |
| 232 | //===--------------------------------------------------------------------===// |
| 233 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 234 | virtual Constant *add(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 235 | return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2); |
| 236 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 237 | virtual Constant *sub(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 238 | return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2); |
| 239 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 240 | virtual Constant *mul(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 241 | return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); |
| 242 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 243 | virtual Constant *div(const Constant *V1, const Constant *V2) const { |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 244 | return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2); |
| 245 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 246 | virtual Constant *rem(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 247 | return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2); |
| 248 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 249 | 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 Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 259 | return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2); |
| 260 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 261 | virtual Constant *shr(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 262 | return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2); |
| 263 | } |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 265 | virtual ConstantBool *lessthan(const Constant *V1, |
| 266 | const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 267 | return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2); |
| 268 | } |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 269 | virtual ConstantBool *equalto(const Constant *V1, |
| 270 | const Constant *V2) const { |
| 271 | return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2); |
| 272 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 274 | // Casting operators. ick |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 275 | virtual ConstantBool *castToBool(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 276 | return SubClassName::CastToBool((const ArgType*)V); |
| 277 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 278 | virtual ConstantSInt *castToSByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 279 | return SubClassName::CastToSByte((const ArgType*)V); |
| 280 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 281 | virtual ConstantUInt *castToUByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 282 | return SubClassName::CastToUByte((const ArgType*)V); |
| 283 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 284 | virtual ConstantSInt *castToShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 285 | return SubClassName::CastToShort((const ArgType*)V); |
| 286 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 287 | virtual ConstantUInt *castToUShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 288 | return SubClassName::CastToUShort((const ArgType*)V); |
| 289 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 290 | virtual ConstantSInt *castToInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 291 | return SubClassName::CastToInt((const ArgType*)V); |
| 292 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 293 | virtual ConstantUInt *castToUInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 294 | return SubClassName::CastToUInt((const ArgType*)V); |
| 295 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 296 | virtual ConstantSInt *castToLong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 297 | return SubClassName::CastToLong((const ArgType*)V); |
| 298 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 299 | virtual ConstantUInt *castToULong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 300 | return SubClassName::CastToULong((const ArgType*)V); |
| 301 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 302 | virtual ConstantFP *castToFloat(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 303 | return SubClassName::CastToFloat((const ArgType*)V); |
| 304 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 305 | virtual ConstantFP *castToDouble(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 306 | return SubClassName::CastToDouble((const ArgType*)V); |
| 307 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 308 | virtual Constant *castToPointer(const Constant *V, |
| 309 | const PointerType *Ty) const { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 310 | return SubClassName::CastToPointer((const ArgType*)V, Ty); |
| 311 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 313 | //===--------------------------------------------------------------------===// |
| 314 | // Default "noop" implementations |
| 315 | //===--------------------------------------------------------------------===// |
| 316 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 317 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 328 | return 0; |
| 329 | } |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 330 | static ConstantBool *EqualTo(const ArgType *V1, const ArgType *V2) { |
| 331 | return 0; |
| 332 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 333 | |
| 334 | // Casting operators. ick |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 335 | 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 Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 346 | static Constant *CastToPointer(const Constant *, |
| 347 | const PointerType *) {return 0;} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | |
| 351 | |
| 352 | //===----------------------------------------------------------------------===// |
| 353 | // EmptyRules Class |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | // |
| 356 | // EmptyRules provides a concrete base class of ConstRules that does nothing |
| 357 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 358 | struct EmptyRules : public TemplateRules<Constant, EmptyRules> { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 359 | static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) { |
| 360 | if (V1 == V2) return ConstantBool::True; |
| 361 | return 0; |
| 362 | } |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 363 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 364 | |
| 365 | |
| 366 | |
| 367 | //===----------------------------------------------------------------------===// |
| 368 | // BoolRules Class |
| 369 | //===----------------------------------------------------------------------===// |
| 370 | // |
| 371 | // BoolRules provides a concrete base class of ConstRules for the 'bool' type. |
| 372 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 373 | struct BoolRules : public TemplateRules<ConstantBool, BoolRules> { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 07507a4 | 2002-09-03 20:09:49 +0000 | [diff] [blame] | 375 | static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){ |
| 376 | return ConstantBool::get(V1->getValue() < V2->getValue()); |
| 377 | } |
| 378 | |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 379 | static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) { |
| 380 | return ConstantBool::get(V1 == V2); |
| 381 | } |
| 382 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 383 | 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 Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 388 | return ConstantBool::get(V1->getValue() | V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 391 | static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) { |
| 392 | return ConstantBool::get(V1->getValue() ^ V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 393 | } |
Chris Lattner | cea4d8c | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 394 | |
| 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 Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 413 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 414 | |
| 415 | |
| 416 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 417 | // NullPointerRules Class |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 418 | //===----------------------------------------------------------------------===// |
| 419 | // |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 420 | // NullPointerRules provides a concrete base class of ConstRules for null |
| 421 | // pointers. |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 422 | // |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 423 | struct NullPointerRules : public TemplateRules<ConstantPointerNull, |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 424 | NullPointerRules> { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 425 | static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) { |
| 426 | return ConstantBool::True; // Null pointers are always equal |
| 427 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 428 | static ConstantBool *CastToBool (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 429 | return ConstantBool::False; |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 430 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 431 | static ConstantSInt *CastToSByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 432 | return ConstantSInt::get(Type::SByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 433 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 434 | static ConstantUInt *CastToUByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 435 | return ConstantUInt::get(Type::UByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 436 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 437 | static ConstantSInt *CastToShort (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 438 | return ConstantSInt::get(Type::ShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 439 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 440 | static ConstantUInt *CastToUShort(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 441 | return ConstantUInt::get(Type::UShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 442 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 443 | static ConstantSInt *CastToInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 444 | return ConstantSInt::get(Type::IntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 445 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 446 | static ConstantUInt *CastToUInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 447 | return ConstantUInt::get(Type::UIntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 449 | static ConstantSInt *CastToLong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 450 | return ConstantSInt::get(Type::LongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 452 | static ConstantUInt *CastToULong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 453 | return ConstantUInt::get(Type::ULongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 454 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 455 | static ConstantFP *CastToFloat (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 456 | return ConstantFP::get(Type::FloatTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 458 | static ConstantFP *CastToDouble(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 459 | return ConstantFP::get(Type::DoubleTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 462 | static Constant *CastToPointer(const ConstantPointerNull *V, |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 463 | const PointerType *PTy) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 464 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 465 | } |
| 466 | }; |
| 467 | |
| 468 | |
| 469 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 470 | // 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 Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 477 | template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass> |
| 478 | struct DirectRules : public TemplateRules<ConstantClass, SuperClass> { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 479 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 484 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 489 | 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 Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 494 | static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 495 | if (V2->isNullValue()) return 0; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 496 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
| 497 | return ConstantClass::get(*Ty, R); |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 500 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 504 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 505 | |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 506 | 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 Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 512 | static Constant *CastToPointer(const ConstantClass *V, |
| 513 | const PointerType *PTy) { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 514 | if (V->isNullValue()) // Is it a FP or Integral null value? |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 515 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 516 | return 0; // Can't const prop other types of pointers |
| 517 | } |
| 518 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 519 | // Casting operators. ick |
| 520 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 521 | static CLASS *CastTo##TYPE (const ConstantClass *V) { \ |
Chris Lattner | bbb2296 | 2001-09-07 16:40:34 +0000 | [diff] [blame] | 522 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 525 | 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 Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 536 | #undef DEF_CAST |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 537 | }; |
| 538 | |
Chris Lattner | 62af86e | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 539 | |
| 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 | // |
| 547 | template <class ConstantClass, class BuiltinType, Type **Ty> |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 548 | struct DirectIntRules |
| 549 | : public DirectRules<ConstantClass, BuiltinType, Ty, |
| 550 | DirectIntRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 26891626 | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 552 | 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 Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 561 | static Constant *Rem(const ConstantClass *V1, |
| 562 | const ConstantClass *V2) { |
Chris Lattner | 26891626 | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 563 | 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 Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 567 | BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue(); |
| 568 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 569 | } |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 570 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 571 | 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 Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 584 | 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 Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 593 | }; |
| 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 | // |
| 603 | template <class ConstantClass, class BuiltinType, Type **Ty> |
| 604 | struct DirectFPRules |
| 605 | : public DirectRules<ConstantClass, BuiltinType, Ty, |
| 606 | DirectFPRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 607 | static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 608 | 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 Lattner | 62af86e | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 613 | }; |
| 614 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 615 | ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 616 | static EmptyRules EmptyR; |
| 617 | static BoolRules BoolR; |
| 618 | static NullPointerRules NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 619 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 630 | if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) || |
| 631 | isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2)) |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 632 | 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 Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 641 | case Type::PointerTyID: return NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 642 | 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 Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 653 | } |