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