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