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