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