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 | ee965ab | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 7 | #include "llvm/Transforms/Scalar/ConstantHandling.h" |
Chris Lattner | d42d492 | 2001-06-30 04:36:40 +0000 | [diff] [blame] | 8 | |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 9 | AnnotationID ConstRules::AID(AnnotationManager::getID("opt::ConstRules", |
| 10 | &ConstRules::find)); |
| 11 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | // TemplateRules Class |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | // |
| 16 | // TemplateRules - Implement a subclass of ConstRules that provides all |
| 17 | // operations as noops. All other rules classes inherit from this class so |
| 18 | // that if functionality is needed in the future, it can simply be added here |
| 19 | // and to ConstRules without changing anything else... |
| 20 | // |
| 21 | // This class also provides subclasses with typesafe implementations of methods |
| 22 | // so that don't have to do type casting. |
| 23 | // |
| 24 | template<class ArgType, class SubClassName> |
| 25 | class TemplateRules : public ConstRules { |
| 26 | |
| 27 | //===--------------------------------------------------------------------===// |
| 28 | // Redirecting functions that cast to the appropriate types |
| 29 | //===--------------------------------------------------------------------===// |
| 30 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 31 | virtual Constant *op_not(const Constant *V) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 32 | return SubClassName::Not((const ArgType *)V); |
| 33 | } |
| 34 | |
| 35 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 36 | virtual Constant *add(const Constant *V1, |
| 37 | const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2); |
| 39 | } |
| 40 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 41 | virtual Constant *sub(const Constant *V1, |
| 42 | const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2); |
| 44 | } |
| 45 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 46 | virtual Constant *mul(const Constant *V1, |
| 47 | const Constant *V2) const { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 48 | return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); |
| 49 | } |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame^] | 50 | virtual Constant *div(const Constant *V1, |
| 51 | const Constant *V2) const { |
| 52 | return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2); |
| 53 | } |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 55 | virtual ConstantBool *lessthan(const Constant *V1, |
| 56 | const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2); |
| 58 | } |
| 59 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 60 | // Casting operators. ick |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 61 | virtual ConstantBool *castToBool(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 62 | return SubClassName::CastToBool((const ArgType*)V); |
| 63 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 64 | virtual ConstantSInt *castToSByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 65 | return SubClassName::CastToSByte((const ArgType*)V); |
| 66 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 67 | virtual ConstantUInt *castToUByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 68 | return SubClassName::CastToUByte((const ArgType*)V); |
| 69 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 70 | virtual ConstantSInt *castToShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 71 | return SubClassName::CastToShort((const ArgType*)V); |
| 72 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 73 | virtual ConstantUInt *castToUShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 74 | return SubClassName::CastToUShort((const ArgType*)V); |
| 75 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 76 | virtual ConstantSInt *castToInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 77 | return SubClassName::CastToInt((const ArgType*)V); |
| 78 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 79 | virtual ConstantUInt *castToUInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 80 | return SubClassName::CastToUInt((const ArgType*)V); |
| 81 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 82 | virtual ConstantSInt *castToLong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 83 | return SubClassName::CastToLong((const ArgType*)V); |
| 84 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 85 | virtual ConstantUInt *castToULong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 86 | return SubClassName::CastToULong((const ArgType*)V); |
| 87 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 88 | virtual ConstantFP *castToFloat(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 89 | return SubClassName::CastToFloat((const ArgType*)V); |
| 90 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 91 | virtual ConstantFP *castToDouble(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 92 | return SubClassName::CastToDouble((const ArgType*)V); |
| 93 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 94 | virtual ConstantPointer *castToPointer(const Constant *V, |
| 95 | const PointerType *Ty) const { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 96 | return SubClassName::CastToPointer((const ArgType*)V, Ty); |
| 97 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 99 | //===--------------------------------------------------------------------===// |
| 100 | // Default "noop" implementations |
| 101 | //===--------------------------------------------------------------------===// |
| 102 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 103 | inline static Constant *Not(const ArgType *V) { return 0; } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 105 | inline static Constant *Add(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 108 | inline static Constant *Sub(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 111 | inline static Constant *Mul(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame^] | 114 | inline static Constant *Div(const ArgType *V1, const ArgType *V2) { |
| 115 | return 0; |
| 116 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 117 | inline static ConstantBool *LessThan(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 118 | return 0; |
| 119 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 120 | |
| 121 | // Casting operators. ick |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 122 | inline static ConstantBool *CastToBool (const Constant *V) { return 0; } |
| 123 | inline static ConstantSInt *CastToSByte (const Constant *V) { return 0; } |
| 124 | inline static ConstantUInt *CastToUByte (const Constant *V) { return 0; } |
| 125 | inline static ConstantSInt *CastToShort (const Constant *V) { return 0; } |
| 126 | inline static ConstantUInt *CastToUShort(const Constant *V) { return 0; } |
| 127 | inline static ConstantSInt *CastToInt (const Constant *V) { return 0; } |
| 128 | inline static ConstantUInt *CastToUInt (const Constant *V) { return 0; } |
| 129 | inline static ConstantSInt *CastToLong (const Constant *V) { return 0; } |
| 130 | inline static ConstantUInt *CastToULong (const Constant *V) { return 0; } |
| 131 | inline static ConstantFP *CastToFloat (const Constant *V) { return 0; } |
| 132 | inline static ConstantFP *CastToDouble(const Constant *V) { return 0; } |
| 133 | inline static ConstantPointer *CastToPointer(const Constant *, |
| 134 | const PointerType *) {return 0;} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | |
| 138 | |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | // EmptyRules Class |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | // |
| 143 | // EmptyRules provides a concrete base class of ConstRules that does nothing |
| 144 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 145 | struct EmptyRules : public TemplateRules<Constant, EmptyRules> { |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 146 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | // BoolRules Class |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | // |
| 154 | // BoolRules provides a concrete base class of ConstRules for the 'bool' type. |
| 155 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 156 | struct BoolRules : public TemplateRules<ConstantBool, BoolRules> { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 158 | inline static Constant *Not(const ConstantBool *V) { |
| 159 | return ConstantBool::get(!V->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 162 | inline static Constant *Or(const ConstantBool *V1, |
| 163 | const ConstantBool *V2) { |
| 164 | return ConstantBool::get(V1->getValue() | V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 167 | inline static Constant *And(const ConstantBool *V1, |
| 168 | const ConstantBool *V2) { |
| 169 | return ConstantBool::get(V1->getValue() & V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 171 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | //===----------------------------------------------------------------------===// |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 175 | // PointerRules Class |
| 176 | //===----------------------------------------------------------------------===// |
| 177 | // |
| 178 | // PointerRules provides a concrete base class of ConstRules for pointer types |
| 179 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 180 | struct PointerRules : public TemplateRules<ConstantPointer, PointerRules> { |
| 181 | inline static ConstantBool *CastToBool (const Constant *V) { |
| 182 | if (V->isNullValue()) return ConstantBool::False; |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 183 | return 0; // Can't const prop other types of pointers |
| 184 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 185 | inline static ConstantSInt *CastToSByte (const Constant *V) { |
| 186 | if (V->isNullValue()) return ConstantSInt::get(Type::SByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 187 | return 0; // Can't const prop other types of pointers |
| 188 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 189 | inline static ConstantUInt *CastToUByte (const Constant *V) { |
| 190 | if (V->isNullValue()) return ConstantUInt::get(Type::UByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 191 | return 0; // Can't const prop other types of pointers |
| 192 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 193 | inline static ConstantSInt *CastToShort (const Constant *V) { |
| 194 | if (V->isNullValue()) return ConstantSInt::get(Type::ShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 195 | return 0; // Can't const prop other types of pointers |
| 196 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 197 | inline static ConstantUInt *CastToUShort(const Constant *V) { |
| 198 | if (V->isNullValue()) return ConstantUInt::get(Type::UShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 199 | return 0; // Can't const prop other types of pointers |
| 200 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 201 | inline static ConstantSInt *CastToInt (const Constant *V) { |
| 202 | if (V->isNullValue()) return ConstantSInt::get(Type::IntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 203 | return 0; // Can't const prop other types of pointers |
| 204 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 205 | inline static ConstantUInt *CastToUInt (const Constant *V) { |
| 206 | if (V->isNullValue()) return ConstantUInt::get(Type::UIntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 207 | return 0; // Can't const prop other types of pointers |
| 208 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 209 | inline static ConstantSInt *CastToLong (const Constant *V) { |
| 210 | if (V->isNullValue()) return ConstantSInt::get(Type::LongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 211 | return 0; // Can't const prop other types of pointers |
| 212 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 213 | inline static ConstantUInt *CastToULong (const Constant *V) { |
| 214 | if (V->isNullValue()) return ConstantUInt::get(Type::ULongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 215 | return 0; // Can't const prop other types of pointers |
| 216 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 217 | inline static ConstantFP *CastToFloat (const Constant *V) { |
| 218 | if (V->isNullValue()) return ConstantFP::get(Type::FloatTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 219 | return 0; // Can't const prop other types of pointers |
| 220 | } |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 221 | inline static ConstantFP *CastToDouble(const Constant *V) { |
| 222 | if (V->isNullValue()) return ConstantFP::get(Type::DoubleTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 223 | return 0; // Can't const prop other types of pointers |
| 224 | } |
| 225 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 226 | inline static ConstantPointer *CastToPointer(const ConstantPointer *V, |
| 227 | const PointerType *PTy) { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 228 | if (V->isNullValue()) |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 229 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 230 | return 0; // Can't const prop other types of pointers |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 236 | // DirectRules Class |
| 237 | //===----------------------------------------------------------------------===// |
| 238 | // |
| 239 | // DirectRules provides a concrete base classes of ConstRules for a variety of |
| 240 | // different types. This allows the C++ compiler to automatically generate our |
| 241 | // constant handling operations in a typesafe and accurate manner. |
| 242 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 243 | template<class ConstantClass, class BuiltinType, Type **Ty> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 244 | struct DirectRules |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 245 | : public TemplateRules<ConstantClass, |
| 246 | DirectRules<ConstantClass, BuiltinType, Ty> > { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 248 | inline static Constant *Not(const ConstantClass *V) { |
| 249 | return ConstantClass::get(*Ty, !(BuiltinType)V->getValue());; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 252 | inline static Constant *Add(const ConstantClass *V1, |
| 253 | const ConstantClass *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 254 | BuiltinType Result = (BuiltinType)V1->getValue() + |
| 255 | (BuiltinType)V2->getValue(); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 256 | return ConstantClass::get(*Ty, Result); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 259 | inline static Constant *Sub(const ConstantClass *V1, |
| 260 | const ConstantClass *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 261 | BuiltinType Result = (BuiltinType)V1->getValue() - |
| 262 | (BuiltinType)V2->getValue(); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 263 | return ConstantClass::get(*Ty, Result); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 266 | inline static Constant *Mul(const ConstantClass *V1, |
| 267 | const ConstantClass *V2) { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 268 | BuiltinType Result = (BuiltinType)V1->getValue() * |
| 269 | (BuiltinType)V2->getValue(); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 270 | return ConstantClass::get(*Ty, Result); |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame^] | 273 | inline static Constant *Div(const ConstantClass *V1, |
| 274 | const ConstantClass *V2) { |
| 275 | BuiltinType Result = (BuiltinType)V1->getValue() / |
| 276 | (BuiltinType)V2->getValue(); |
| 277 | return ConstantClass::get(*Ty, Result); |
| 278 | } |
| 279 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 280 | inline static ConstantBool *LessThan(const ConstantClass *V1, |
| 281 | const ConstantClass *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 282 | bool Result = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 283 | return ConstantBool::get(Result); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 286 | inline static ConstantPointer *CastToPointer(const ConstantClass *V, |
| 287 | const PointerType *PTy) { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 288 | if (V->isNullValue()) // Is it a FP or Integral null value? |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 289 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 290 | return 0; // Can't const prop other types of pointers |
| 291 | } |
| 292 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 293 | // Casting operators. ick |
| 294 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 295 | inline static CLASS *CastTo##TYPE (const ConstantClass *V) { \ |
Chris Lattner | bbb2296 | 2001-09-07 16:40:34 +0000 | [diff] [blame] | 296 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(BuiltinType)V->getValue()); \ |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 299 | DEF_CAST(Bool , ConstantBool, bool) |
| 300 | DEF_CAST(SByte , ConstantSInt, signed char) |
| 301 | DEF_CAST(UByte , ConstantUInt, unsigned char) |
| 302 | DEF_CAST(Short , ConstantSInt, signed short) |
| 303 | DEF_CAST(UShort, ConstantUInt, unsigned short) |
| 304 | DEF_CAST(Int , ConstantSInt, signed int) |
| 305 | DEF_CAST(UInt , ConstantUInt, unsigned int) |
| 306 | DEF_CAST(Long , ConstantSInt, int64_t) |
| 307 | DEF_CAST(ULong , ConstantUInt, uint64_t) |
| 308 | DEF_CAST(Float , ConstantFP , float) |
| 309 | DEF_CAST(Double, ConstantFP , double) |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 310 | #undef DEF_CAST |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | // DirectRules Subclasses |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | // |
| 317 | // Given the DirectRules class we can now implement lots of types with little |
| 318 | // code. Thank goodness C++ compilers are great at stomping out layers of |
| 319 | // templates... can you imagine having to do this all by hand? (/me is lazy :) |
| 320 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 321 | |
| 322 | // ConstRules::find - Return the constant rules that take care of the specified |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 323 | // type. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 324 | // |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 325 | Annotation *ConstRules::find(AnnotationID AID, const Annotable *TyA, void *) { |
| 326 | assert(AID == ConstRules::AID && "Bad annotation for factory!"); |
Chris Lattner | 8f19112 | 2001-10-01 18:26:53 +0000 | [diff] [blame] | 327 | const Type *Ty = cast<Type>((const Value*)TyA); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 329 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 330 | case Type::BoolTyID: return new BoolRules(); |
| 331 | case Type::PointerTyID: return new PointerRules(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 332 | case Type::SByteTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 333 | return new DirectRules<ConstantSInt, signed char , &Type::SByteTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 334 | case Type::UByteTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 335 | return new DirectRules<ConstantUInt, unsigned char , &Type::UByteTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 336 | case Type::ShortTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 337 | return new DirectRules<ConstantSInt, signed short, &Type::ShortTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 338 | case Type::UShortTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 339 | return new DirectRules<ConstantUInt, unsigned short, &Type::UShortTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 340 | case Type::IntTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 341 | return new DirectRules<ConstantSInt, signed int , &Type::IntTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 342 | case Type::UIntTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 343 | return new DirectRules<ConstantUInt, unsigned int , &Type::UIntTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 344 | case Type::LongTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 345 | return new DirectRules<ConstantSInt, int64_t , &Type::LongTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 346 | case Type::ULongTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 347 | return new DirectRules<ConstantUInt, uint64_t , &Type::ULongTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 348 | case Type::FloatTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 349 | return new DirectRules<ConstantFP , float , &Type::FloatTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 350 | case Type::DoubleTyID: |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 351 | return new DirectRules<ConstantFP , double , &Type::DoubleTy>(); |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 352 | default: |
| 353 | return new EmptyRules(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 354 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 355 | } |