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 | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 18 | #include <cmath> |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 21 | // ConstantFoldInstruction - Attempt to constant fold the specified instruction. |
| 22 | // If successful, the constant result is returned, if not, null is returned. |
| 23 | // |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 24 | Constant *llvm::ConstantFoldInstruction(Instruction *I) { |
Chris Lattner | 78a0d42 | 2002-05-07 20:44:59 +0000 | [diff] [blame] | 25 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 26 | if (PN->getNumIncomingValues() == 0) |
| 27 | return Constant::getNullValue(PN->getType()); |
| 28 | |
| 29 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 30 | if (Result == 0) return 0; |
| 31 | |
| 32 | // Handle PHI nodes specially here... |
| 33 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 34 | if (PN->getIncomingValue(i) != Result) |
| 35 | return 0; // Not all the same incoming constants... |
| 36 | |
| 37 | // If we reach here, all incoming values are the same constant. |
| 38 | return Result; |
| 39 | } |
| 40 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 41 | Constant *Op0 = 0; |
| 42 | Constant *Op1 = 0; |
| 43 | |
| 44 | if (I->getNumOperands() != 0) { // Get first operand if it's a constant... |
| 45 | Op0 = dyn_cast<Constant>(I->getOperand(0)); |
| 46 | if (Op0 == 0) return 0; // Not a constant?, can't fold |
| 47 | |
| 48 | if (I->getNumOperands() != 1) { // Get second operand if it's a constant... |
| 49 | Op1 = dyn_cast<Constant>(I->getOperand(1)); |
| 50 | if (Op1 == 0) return 0; // Not a constant?, can't fold |
| 51 | } |
| 52 | } |
| 53 | |
Chris Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 54 | if (isa<BinaryOperator>(I)) |
| 55 | return ConstantExpr::get(I->getOpcode(), Op0, Op1); |
| 56 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 57 | switch (I->getOpcode()) { |
| 58 | case Instruction::Cast: |
Chris Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 59 | return ConstantExpr::getCast(Op0, I->getType()); |
| 60 | case Instruction::Shl: |
| 61 | case Instruction::Shr: |
| 62 | return ConstantExpr::getShift(I->getOpcode(), Op0, Op1); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 63 | case Instruction::GetElementPtr: { |
| 64 | std::vector<Constant*> IdxList; |
| 65 | IdxList.reserve(I->getNumOperands()-1); |
| 66 | if (Op1) IdxList.push_back(Op1); |
| 67 | for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i) |
| 68 | if (Constant *C = dyn_cast<Constant>(I->getOperand(i))) |
| 69 | IdxList.push_back(C); |
| 70 | else |
| 71 | return 0; // Non-constant operand |
Chris Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 72 | return ConstantExpr::getGetElementPtr(Op0, IdxList); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 74 | default: |
| 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 79 | static unsigned getSize(const Type *Ty) { |
| 80 | unsigned S = Ty->getPrimitiveSize(); |
| 81 | return S ? S : 8; // Treat pointers at 8 bytes |
| 82 | } |
| 83 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 84 | Constant *llvm::ConstantFoldCastInstruction(const Constant *V, |
| 85 | const Type *DestTy) { |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 86 | if (V->getType() == DestTy) return (Constant*)V; |
| 87 | |
| 88 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 89 | if (CE->getOpcode() == Instruction::Cast) { |
Chris Lattner | 55ed656 | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 90 | Constant *Op = const_cast<Constant*>(CE->getOperand(0)); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 91 | // Try to not produce a cast of a cast, which is almost always redundant. |
| 92 | if (!Op->getType()->isFloatingPoint() && |
| 93 | !CE->getType()->isFloatingPoint() && |
| 94 | !DestTy->getType()->isFloatingPoint()) { |
| 95 | unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType()); |
| 96 | unsigned S3 = getSize(DestTy); |
| 97 | if (Op->getType() == DestTy && S3 >= S2) |
| 98 | return Op; |
| 99 | if (S1 >= S2 && S2 >= S3) |
| 100 | return ConstantExpr::getCast(Op, DestTy); |
| 101 | if (S1 <= S2 && S2 >= S3 && S1 <= S3) |
| 102 | return ConstantExpr::getCast(Op, DestTy); |
| 103 | } |
Chris Lattner | 941b06b | 2003-08-21 19:45:55 +0000 | [diff] [blame] | 104 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 105 | // If all of the indexes in the GEP are null values, there is no pointer |
| 106 | // adjustment going on. We might as well cast the source pointer. |
| 107 | bool isAllNull = true; |
| 108 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 109 | if (!CE->getOperand(i)->isNullValue()) { |
| 110 | isAllNull = false; |
| 111 | break; |
| 112 | } |
| 113 | if (isAllNull) |
| 114 | return ConstantExpr::getCast(CE->getOperand(0), DestTy); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 117 | return ConstRules::get(*V, *V).castTo(V, DestTy); |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 120 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 121 | const Constant *V1, |
| 122 | const Constant *V2) { |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 123 | switch (Opcode) { |
| 124 | case Instruction::Add: return *V1 + *V2; |
| 125 | case Instruction::Sub: return *V1 - *V2; |
| 126 | case Instruction::Mul: return *V1 * *V2; |
| 127 | case Instruction::Div: return *V1 / *V2; |
| 128 | case Instruction::Rem: return *V1 % *V2; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 129 | case Instruction::And: return *V1 & *V2; |
| 130 | case Instruction::Or: return *V1 | *V2; |
| 131 | case Instruction::Xor: return *V1 ^ *V2; |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 132 | |
| 133 | case Instruction::SetEQ: return *V1 == *V2; |
| 134 | case Instruction::SetNE: return *V1 != *V2; |
| 135 | case Instruction::SetLE: return *V1 <= *V2; |
| 136 | case Instruction::SetGE: return *V1 >= *V2; |
| 137 | case Instruction::SetLT: return *V1 < *V2; |
| 138 | case Instruction::SetGT: return *V1 > *V2; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 143 | Constant *llvm::ConstantFoldShiftInstruction(unsigned Opcode, |
| 144 | const Constant *V1, |
| 145 | const Constant *V2) { |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 146 | switch (Opcode) { |
| 147 | case Instruction::Shl: return *V1 << *V2; |
| 148 | case Instruction::Shr: return *V1 >> *V2; |
| 149 | default: return 0; |
| 150 | } |
| 151 | } |
| 152 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 153 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
| 154 | const std::vector<Constant*> &IdxList) { |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 155 | if (IdxList.size() == 0 || |
| 156 | (IdxList.size() == 1 && IdxList[0]->isNullValue())) |
| 157 | return const_cast<Constant*>(C); |
| 158 | |
Chris Lattner | 941b06b | 2003-08-21 19:45:55 +0000 | [diff] [blame] | 159 | // 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] | 160 | |
Chris Lattner | c2ceed1 | 2003-05-13 21:50:52 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 162 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 163 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 164 | // is a getelementptr instruction, combine the indices of the two |
| 165 | // getelementptr instructions into a single instruction. |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 166 | // |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 167 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 168 | if (CE->getOperand(CE->getNumOperands()-1)->getType() == Type::LongTy) { |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 169 | std::vector<Constant*> NewIndices; |
| 170 | NewIndices.reserve(IdxList.size() + CE->getNumOperands()); |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 171 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 172 | NewIndices.push_back(cast<Constant>(CE->getOperand(i))); |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 173 | |
| 174 | // Add the last index of the source with the first index of the new GEP. |
| 175 | Constant *Combined = |
| 176 | ConstantExpr::get(Instruction::Add, IdxList[0], |
| 177 | CE->getOperand(CE->getNumOperands()-1)); |
| 178 | |
| 179 | NewIndices.push_back(Combined); |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 180 | NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); |
| 181 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); |
| 182 | } |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 184 | |
| 185 | // Implement folding of: |
| 186 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 187 | // long 0, long 0) |
| 188 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 189 | // |
Chris Lattner | 69f6af1 | 2003-05-14 02:47:13 +0000 | [diff] [blame] | 190 | if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 && |
| 191 | IdxList[0]->isNullValue()) |
Chris Lattner | c2ceed1 | 2003-05-13 21:50:52 +0000 | [diff] [blame] | 192 | if (const PointerType *SPT = |
| 193 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 194 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 195 | if (const ArrayType *CAT = |
| 196 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
| 197 | if (CAT->getElementType() == SAT->getElementType()) |
| 198 | return ConstantExpr::getGetElementPtr( |
Chris Lattner | 55ed656 | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 199 | (Constant*)CE->getOperand(0), IdxList); |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
| 206 | // TemplateRules Class |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | // |
| 209 | // TemplateRules - Implement a subclass of ConstRules that provides all |
| 210 | // operations as noops. All other rules classes inherit from this class so |
| 211 | // that if functionality is needed in the future, it can simply be added here |
| 212 | // and to ConstRules without changing anything else... |
| 213 | // |
| 214 | // This class also provides subclasses with typesafe implementations of methods |
| 215 | // so that don't have to do type casting. |
| 216 | // |
| 217 | template<class ArgType, class SubClassName> |
| 218 | class TemplateRules : public ConstRules { |
| 219 | |
| 220 | //===--------------------------------------------------------------------===// |
| 221 | // Redirecting functions that cast to the appropriate types |
| 222 | //===--------------------------------------------------------------------===// |
| 223 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 224 | virtual Constant *add(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 225 | return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2); |
| 226 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 227 | virtual Constant *sub(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2); |
| 229 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 230 | virtual Constant *mul(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 231 | return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); |
| 232 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 233 | virtual Constant *div(const Constant *V1, const Constant *V2) const { |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 234 | return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2); |
| 235 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 236 | virtual Constant *rem(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 237 | return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2); |
| 238 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 239 | virtual Constant *op_and(const Constant *V1, const Constant *V2) const { |
| 240 | return SubClassName::And((const ArgType *)V1, (const ArgType *)V2); |
| 241 | } |
| 242 | virtual Constant *op_or(const Constant *V1, const Constant *V2) const { |
| 243 | return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2); |
| 244 | } |
| 245 | virtual Constant *op_xor(const Constant *V1, const Constant *V2) const { |
| 246 | return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2); |
| 247 | } |
| 248 | virtual Constant *shl(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 249 | return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2); |
| 250 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 251 | virtual Constant *shr(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 252 | return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2); |
| 253 | } |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 255 | virtual ConstantBool *lessthan(const Constant *V1, |
| 256 | const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2); |
| 258 | } |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame^] | 259 | virtual ConstantBool *equalto(const Constant *V1, |
| 260 | const Constant *V2) const { |
| 261 | return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2); |
| 262 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 264 | // Casting operators. ick |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 265 | virtual ConstantBool *castToBool(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 266 | return SubClassName::CastToBool((const ArgType*)V); |
| 267 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 268 | virtual ConstantSInt *castToSByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 269 | return SubClassName::CastToSByte((const ArgType*)V); |
| 270 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 271 | virtual ConstantUInt *castToUByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 272 | return SubClassName::CastToUByte((const ArgType*)V); |
| 273 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 274 | virtual ConstantSInt *castToShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 275 | return SubClassName::CastToShort((const ArgType*)V); |
| 276 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 277 | virtual ConstantUInt *castToUShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 278 | return SubClassName::CastToUShort((const ArgType*)V); |
| 279 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 280 | virtual ConstantSInt *castToInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 281 | return SubClassName::CastToInt((const ArgType*)V); |
| 282 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 283 | virtual ConstantUInt *castToUInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 284 | return SubClassName::CastToUInt((const ArgType*)V); |
| 285 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 286 | virtual ConstantSInt *castToLong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 287 | return SubClassName::CastToLong((const ArgType*)V); |
| 288 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 289 | virtual ConstantUInt *castToULong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 290 | return SubClassName::CastToULong((const ArgType*)V); |
| 291 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 292 | virtual ConstantFP *castToFloat(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 293 | return SubClassName::CastToFloat((const ArgType*)V); |
| 294 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 295 | virtual ConstantFP *castToDouble(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 296 | return SubClassName::CastToDouble((const ArgType*)V); |
| 297 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 298 | virtual Constant *castToPointer(const Constant *V, |
| 299 | const PointerType *Ty) const { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 300 | return SubClassName::CastToPointer((const ArgType*)V, Ty); |
| 301 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 303 | //===--------------------------------------------------------------------===// |
| 304 | // Default "noop" implementations |
| 305 | //===--------------------------------------------------------------------===// |
| 306 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 307 | static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; } |
| 308 | static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; } |
| 309 | static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; } |
| 310 | static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; } |
| 311 | static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; } |
| 312 | static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; } |
| 313 | static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; } |
| 314 | static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; } |
| 315 | static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; } |
| 316 | static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; } |
| 317 | static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame^] | 320 | static ConstantBool *EqualTo(const ArgType *V1, const ArgType *V2) { |
| 321 | return 0; |
| 322 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 323 | |
| 324 | // Casting operators. ick |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 325 | static ConstantBool *CastToBool (const Constant *V) { return 0; } |
| 326 | static ConstantSInt *CastToSByte (const Constant *V) { return 0; } |
| 327 | static ConstantUInt *CastToUByte (const Constant *V) { return 0; } |
| 328 | static ConstantSInt *CastToShort (const Constant *V) { return 0; } |
| 329 | static ConstantUInt *CastToUShort(const Constant *V) { return 0; } |
| 330 | static ConstantSInt *CastToInt (const Constant *V) { return 0; } |
| 331 | static ConstantUInt *CastToUInt (const Constant *V) { return 0; } |
| 332 | static ConstantSInt *CastToLong (const Constant *V) { return 0; } |
| 333 | static ConstantUInt *CastToULong (const Constant *V) { return 0; } |
| 334 | static ConstantFP *CastToFloat (const Constant *V) { return 0; } |
| 335 | static ConstantFP *CastToDouble(const Constant *V) { return 0; } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 336 | static Constant *CastToPointer(const Constant *, |
| 337 | const PointerType *) {return 0;} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | |
| 341 | |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | // EmptyRules Class |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | // |
| 346 | // EmptyRules provides a concrete base class of ConstRules that does nothing |
| 347 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 348 | struct EmptyRules : public TemplateRules<Constant, EmptyRules> { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame^] | 349 | static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) { |
| 350 | if (V1 == V2) return ConstantBool::True; |
| 351 | return 0; |
| 352 | } |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 353 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 354 | |
| 355 | |
| 356 | |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | // BoolRules Class |
| 359 | //===----------------------------------------------------------------------===// |
| 360 | // |
| 361 | // BoolRules provides a concrete base class of ConstRules for the 'bool' type. |
| 362 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 363 | struct BoolRules : public TemplateRules<ConstantBool, BoolRules> { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 07507a4 | 2002-09-03 20:09:49 +0000 | [diff] [blame] | 365 | static ConstantBool *LessThan(const ConstantBool *V1, const ConstantBool *V2){ |
| 366 | return ConstantBool::get(V1->getValue() < V2->getValue()); |
| 367 | } |
| 368 | |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame^] | 369 | static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) { |
| 370 | return ConstantBool::get(V1 == V2); |
| 371 | } |
| 372 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 373 | static Constant *And(const ConstantBool *V1, const ConstantBool *V2) { |
| 374 | return ConstantBool::get(V1->getValue() & V2->getValue()); |
| 375 | } |
| 376 | |
| 377 | static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 378 | return ConstantBool::get(V1->getValue() | V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 381 | static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) { |
| 382 | return ConstantBool::get(V1->getValue() ^ V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 383 | } |
Chris Lattner | cea4d8c | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 384 | |
| 385 | // Casting operators. ick |
| 386 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
| 387 | static CLASS *CastTo##TYPE (const ConstantBool *V) { \ |
| 388 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \ |
| 389 | } |
| 390 | |
| 391 | DEF_CAST(Bool , ConstantBool, bool) |
| 392 | DEF_CAST(SByte , ConstantSInt, signed char) |
| 393 | DEF_CAST(UByte , ConstantUInt, unsigned char) |
| 394 | DEF_CAST(Short , ConstantSInt, signed short) |
| 395 | DEF_CAST(UShort, ConstantUInt, unsigned short) |
| 396 | DEF_CAST(Int , ConstantSInt, signed int) |
| 397 | DEF_CAST(UInt , ConstantUInt, unsigned int) |
| 398 | DEF_CAST(Long , ConstantSInt, int64_t) |
| 399 | DEF_CAST(ULong , ConstantUInt, uint64_t) |
| 400 | DEF_CAST(Float , ConstantFP , float) |
| 401 | DEF_CAST(Double, ConstantFP , double) |
| 402 | #undef DEF_CAST |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 403 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 404 | |
| 405 | |
| 406 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 407 | // NullPointerRules Class |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 408 | //===----------------------------------------------------------------------===// |
| 409 | // |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 410 | // NullPointerRules provides a concrete base class of ConstRules for null |
| 411 | // pointers. |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 412 | // |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 413 | struct NullPointerRules : public TemplateRules<ConstantPointerNull, |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 414 | NullPointerRules> { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame^] | 415 | static ConstantBool *EqualTo(const Constant *V1, const Constant *V2) { |
| 416 | return ConstantBool::True; // Null pointers are always equal |
| 417 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 418 | static ConstantBool *CastToBool (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 419 | return ConstantBool::False; |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 420 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 421 | static ConstantSInt *CastToSByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 422 | return ConstantSInt::get(Type::SByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 423 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 424 | static ConstantUInt *CastToUByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 425 | return ConstantUInt::get(Type::UByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 426 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 427 | static ConstantSInt *CastToShort (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 428 | return ConstantSInt::get(Type::ShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 429 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 430 | static ConstantUInt *CastToUShort(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 431 | return ConstantUInt::get(Type::UShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 433 | static ConstantSInt *CastToInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 434 | return ConstantSInt::get(Type::IntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 435 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 436 | static ConstantUInt *CastToUInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 437 | return ConstantUInt::get(Type::UIntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 438 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 439 | static ConstantSInt *CastToLong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 440 | return ConstantSInt::get(Type::LongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 441 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 442 | static ConstantUInt *CastToULong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 443 | return ConstantUInt::get(Type::ULongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 444 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 445 | static ConstantFP *CastToFloat (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 446 | return ConstantFP::get(Type::FloatTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 447 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 448 | static ConstantFP *CastToDouble(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 449 | return ConstantFP::get(Type::DoubleTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 452 | static Constant *CastToPointer(const ConstantPointerNull *V, |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 453 | const PointerType *PTy) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 454 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 455 | } |
| 456 | }; |
| 457 | |
| 458 | |
| 459 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 460 | // DirectRules Class |
| 461 | //===----------------------------------------------------------------------===// |
| 462 | // |
| 463 | // DirectRules provides a concrete base classes of ConstRules for a variety of |
| 464 | // different types. This allows the C++ compiler to automatically generate our |
| 465 | // constant handling operations in a typesafe and accurate manner. |
| 466 | // |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 467 | template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass> |
| 468 | struct DirectRules : public TemplateRules<ConstantClass, SuperClass> { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 469 | static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) { |
| 470 | BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue(); |
| 471 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 474 | static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) { |
| 475 | BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue(); |
| 476 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 479 | static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) { |
| 480 | BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue(); |
| 481 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 484 | static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 485 | if (V2->isNullValue()) return 0; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 486 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
| 487 | return ConstantClass::get(*Ty, R); |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 490 | static ConstantBool *LessThan(const ConstantClass *V1, |
| 491 | const ConstantClass *V2) { |
| 492 | bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); |
| 493 | return ConstantBool::get(R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 494 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 495 | |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame^] | 496 | static ConstantBool *EqualTo(const ConstantClass *V1, |
| 497 | const ConstantClass *V2) { |
| 498 | bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue(); |
| 499 | return ConstantBool::get(R); |
| 500 | } |
| 501 | |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 502 | static Constant *CastToPointer(const ConstantClass *V, |
| 503 | const PointerType *PTy) { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 504 | if (V->isNullValue()) // Is it a FP or Integral null value? |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 505 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 506 | return 0; // Can't const prop other types of pointers |
| 507 | } |
| 508 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 509 | // Casting operators. ick |
| 510 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 511 | static CLASS *CastTo##TYPE (const ConstantClass *V) { \ |
Chris Lattner | bbb2296 | 2001-09-07 16:40:34 +0000 | [diff] [blame] | 512 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 515 | DEF_CAST(Bool , ConstantBool, bool) |
| 516 | DEF_CAST(SByte , ConstantSInt, signed char) |
| 517 | DEF_CAST(UByte , ConstantUInt, unsigned char) |
| 518 | DEF_CAST(Short , ConstantSInt, signed short) |
| 519 | DEF_CAST(UShort, ConstantUInt, unsigned short) |
| 520 | DEF_CAST(Int , ConstantSInt, signed int) |
| 521 | DEF_CAST(UInt , ConstantUInt, unsigned int) |
| 522 | DEF_CAST(Long , ConstantSInt, int64_t) |
| 523 | DEF_CAST(ULong , ConstantUInt, uint64_t) |
| 524 | DEF_CAST(Float , ConstantFP , float) |
| 525 | DEF_CAST(Double, ConstantFP , double) |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 526 | #undef DEF_CAST |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 527 | }; |
| 528 | |
Chris Lattner | 62af86e | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 529 | |
| 530 | //===----------------------------------------------------------------------===// |
| 531 | // DirectIntRules Class |
| 532 | //===----------------------------------------------------------------------===// |
| 533 | // |
| 534 | // DirectIntRules provides implementations of functions that are valid on |
| 535 | // integer types, but not all types in general. |
| 536 | // |
| 537 | template <class ConstantClass, class BuiltinType, Type **Ty> |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 538 | struct DirectIntRules |
| 539 | : public DirectRules<ConstantClass, BuiltinType, Ty, |
| 540 | DirectIntRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 26891626 | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 542 | static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { |
| 543 | if (V2->isNullValue()) return 0; |
| 544 | if (V2->isAllOnesValue() && // MIN_INT / -1 |
| 545 | (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue()) |
| 546 | return 0; |
| 547 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
| 548 | return ConstantClass::get(*Ty, R); |
| 549 | } |
| 550 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 551 | static Constant *Rem(const ConstantClass *V1, |
| 552 | const ConstantClass *V2) { |
Chris Lattner | 26891626 | 2003-05-12 15:26:25 +0000 | [diff] [blame] | 553 | if (V2->isNullValue()) return 0; // X / 0 |
| 554 | if (V2->isAllOnesValue() && // MIN_INT / -1 |
| 555 | (BuiltinType)V1->getValue() == -(BuiltinType)V1->getValue()) |
| 556 | return 0; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 557 | BuiltinType R = (BuiltinType)V1->getValue() % (BuiltinType)V2->getValue(); |
| 558 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 559 | } |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 560 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 561 | static Constant *And(const ConstantClass *V1, const ConstantClass *V2) { |
| 562 | BuiltinType R = (BuiltinType)V1->getValue() & (BuiltinType)V2->getValue(); |
| 563 | return ConstantClass::get(*Ty, R); |
| 564 | } |
| 565 | static Constant *Or(const ConstantClass *V1, const ConstantClass *V2) { |
| 566 | BuiltinType R = (BuiltinType)V1->getValue() | (BuiltinType)V2->getValue(); |
| 567 | return ConstantClass::get(*Ty, R); |
| 568 | } |
| 569 | static Constant *Xor(const ConstantClass *V1, const ConstantClass *V2) { |
| 570 | BuiltinType R = (BuiltinType)V1->getValue() ^ (BuiltinType)V2->getValue(); |
| 571 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 574 | static Constant *Shl(const ConstantClass *V1, const ConstantClass *V2) { |
| 575 | BuiltinType R = (BuiltinType)V1->getValue() << (BuiltinType)V2->getValue(); |
| 576 | return ConstantClass::get(*Ty, R); |
| 577 | } |
| 578 | |
| 579 | static Constant *Shr(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 | } |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | |
| 586 | //===----------------------------------------------------------------------===// |
| 587 | // DirectFPRules Class |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | // |
| 590 | // DirectFPRules provides implementations of functions that are valid on |
| 591 | // floating point types, but not all types in general. |
| 592 | // |
| 593 | template <class ConstantClass, class BuiltinType, Type **Ty> |
| 594 | struct DirectFPRules |
| 595 | : public DirectRules<ConstantClass, BuiltinType, Ty, |
| 596 | DirectFPRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 597 | static Constant *Rem(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 598 | if (V2->isNullValue()) return 0; |
| 599 | BuiltinType Result = std::fmod((BuiltinType)V1->getValue(), |
| 600 | (BuiltinType)V2->getValue()); |
| 601 | return ConstantClass::get(*Ty, Result); |
| 602 | } |
Chris Lattner | 62af86e | 2002-05-03 20:09:52 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 605 | ConstRules &ConstRules::get(const Constant &V1, const Constant &V2) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 606 | static EmptyRules EmptyR; |
| 607 | static BoolRules BoolR; |
| 608 | static NullPointerRules NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 609 | static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR; |
| 610 | static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR; |
| 611 | static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR; |
| 612 | static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR; |
| 613 | static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR; |
| 614 | static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR; |
| 615 | static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR; |
| 616 | static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR; |
| 617 | static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR; |
| 618 | static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 620 | if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) || |
| 621 | isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2)) |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 622 | return EmptyR; |
| 623 | |
| 624 | // FIXME: This assert doesn't work because shifts pass both operands in to |
| 625 | // check for constant exprs. :( |
| 626 | //assert(V1.getType() == V2.getType() &&"Nonequal types to constant folder?"); |
| 627 | |
| 628 | switch (V1.getType()->getPrimitiveID()) { |
| 629 | default: assert(0 && "Unknown value type for constant folding!"); |
| 630 | case Type::BoolTyID: return BoolR; |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 631 | case Type::PointerTyID: return NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 632 | case Type::SByteTyID: return SByteR; |
| 633 | case Type::UByteTyID: return UByteR; |
| 634 | case Type::ShortTyID: return ShortR; |
| 635 | case Type::UShortTyID: return UShortR; |
| 636 | case Type::IntTyID: return IntR; |
| 637 | case Type::UIntTyID: return UIntR; |
| 638 | case Type::LongTyID: return LongR; |
| 639 | case Type::ULongTyID: return ULongR; |
| 640 | case Type::FloatTyID: return FloatR; |
| 641 | case Type::DoubleTyID: return DoubleR; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 642 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 643 | } |