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