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 | 2361bd0 | 2004-01-12 20:48:11 +0000 | [diff] [blame] | 14 | #include "ConstantHandling.h" |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 78a0d42 | 2002-05-07 20:44:59 +0000 | [diff] [blame] | 16 | #include "llvm/iPHINode.h" |
Chris Lattner | 8acf346 | 2003-05-27 19:16:07 +0000 | [diff] [blame] | 17 | #include "llvm/InstrTypes.h" |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 20 | #include <cmath> |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 23 | static unsigned getSize(const Type *Ty) { |
| 24 | unsigned S = Ty->getPrimitiveSize(); |
| 25 | return S ? S : 8; // Treat pointers at 8 bytes |
| 26 | } |
| 27 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 28 | Constant *llvm::ConstantFoldCastInstruction(const Constant *V, |
| 29 | const Type *DestTy) { |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 30 | if (V->getType() == DestTy) return (Constant*)V; |
| 31 | |
| 32 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 33 | if (CE->getOpcode() == Instruction::Cast) { |
Chris Lattner | 55ed656 | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 34 | Constant *Op = const_cast<Constant*>(CE->getOperand(0)); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 35 | // Try to not produce a cast of a cast, which is almost always redundant. |
| 36 | if (!Op->getType()->isFloatingPoint() && |
| 37 | !CE->getType()->isFloatingPoint() && |
| 38 | !DestTy->getType()->isFloatingPoint()) { |
| 39 | unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType()); |
| 40 | unsigned S3 = getSize(DestTy); |
| 41 | if (Op->getType() == DestTy && S3 >= S2) |
| 42 | return Op; |
| 43 | if (S1 >= S2 && S2 >= S3) |
| 44 | return ConstantExpr::getCast(Op, DestTy); |
| 45 | if (S1 <= S2 && S2 >= S3 && S1 <= S3) |
| 46 | return ConstantExpr::getCast(Op, DestTy); |
| 47 | } |
Chris Lattner | 941b06b | 2003-08-21 19:45:55 +0000 | [diff] [blame] | 48 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 49 | // If all of the indexes in the GEP are null values, there is no pointer |
| 50 | // adjustment going on. We might as well cast the source pointer. |
| 51 | bool isAllNull = true; |
| 52 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 53 | if (!CE->getOperand(i)->isNullValue()) { |
| 54 | isAllNull = false; |
| 55 | break; |
| 56 | } |
| 57 | if (isAllNull) |
| 58 | return ConstantExpr::getCast(CE->getOperand(0), DestTy); |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 61 | ConstRules &Rules = ConstRules::get(V, V); |
| 62 | |
| 63 | switch (DestTy->getPrimitiveID()) { |
| 64 | case Type::BoolTyID: return Rules.castToBool(V); |
| 65 | case Type::UByteTyID: return Rules.castToUByte(V); |
| 66 | case Type::SByteTyID: return Rules.castToSByte(V); |
| 67 | case Type::UShortTyID: return Rules.castToUShort(V); |
| 68 | case Type::ShortTyID: return Rules.castToShort(V); |
| 69 | case Type::UIntTyID: return Rules.castToUInt(V); |
| 70 | case Type::IntTyID: return Rules.castToInt(V); |
| 71 | case Type::ULongTyID: return Rules.castToULong(V); |
| 72 | case Type::LongTyID: return Rules.castToLong(V); |
| 73 | case Type::FloatTyID: return Rules.castToFloat(V); |
| 74 | case Type::DoubleTyID: return Rules.castToDouble(V); |
| 75 | case Type::PointerTyID: |
| 76 | return Rules.castToPointer(V, cast<PointerType>(DestTy)); |
| 77 | default: return 0; |
| 78 | } |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 81 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 82 | const Constant *V1, |
| 83 | const Constant *V2) { |
Chris Lattner | f8348c3 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 84 | Constant *C; |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 85 | switch (Opcode) { |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 86 | default: return 0; |
Chris Lattner | f8348c3 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 87 | case Instruction::Add: return ConstRules::get(V1, V2).add(V1, V2); |
| 88 | case Instruction::Sub: return ConstRules::get(V1, V2).sub(V1, V2); |
| 89 | case Instruction::Mul: return ConstRules::get(V1, V2).mul(V1, V2); |
| 90 | case Instruction::Div: return ConstRules::get(V1, V2).div(V1, V2); |
| 91 | case Instruction::Rem: return ConstRules::get(V1, V2).rem(V1, V2); |
| 92 | case Instruction::And: return ConstRules::get(V1, V2).op_and(V1, V2); |
| 93 | case Instruction::Or: return ConstRules::get(V1, V2).op_or (V1, V2); |
| 94 | case Instruction::Xor: return ConstRules::get(V1, V2).op_xor(V1, V2); |
| 95 | |
| 96 | case Instruction::Shl: return ConstRules::get(V1, V2).shl(V1, V2); |
| 97 | case Instruction::Shr: return ConstRules::get(V1, V2).shr(V1, V2); |
| 98 | |
| 99 | case Instruction::SetEQ: return ConstRules::get(V1, V2).equalto(V1, V2); |
| 100 | case Instruction::SetLT: return ConstRules::get(V1, V2).lessthan(V1, V2); |
| 101 | case Instruction::SetGT: return ConstRules::get(V1, V2).lessthan(V2, V1); |
| 102 | case Instruction::SetNE: // V1 != V2 === !(V1 == V2) |
| 103 | C = ConstRules::get(V1, V2).equalto(V1, V2); |
| 104 | break; |
| 105 | case Instruction::SetLE: // V1 <= V2 === !(V2 < V1) |
| 106 | C = ConstRules::get(V1, V2).lessthan(V2, V1); |
| 107 | break; |
| 108 | case Instruction::SetGE: // V1 >= V2 === !(V1 < V2) |
| 109 | C = ConstRules::get(V1, V2).lessthan(V1, V2); |
| 110 | break; |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | f8348c3 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 112 | |
| 113 | // If the folder broke out of the switch statement, invert the boolean |
| 114 | // constant value, if it exists, and return it. |
| 115 | if (!C) return 0; |
| 116 | return ConstantExpr::get(Instruction::Xor, ConstantBool::True, C); |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 119 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
| 120 | const std::vector<Constant*> &IdxList) { |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 121 | if (IdxList.size() == 0 || |
| 122 | (IdxList.size() == 1 && IdxList[0]->isNullValue())) |
| 123 | return const_cast<Constant*>(C); |
| 124 | |
Chris Lattner | 941b06b | 2003-08-21 19:45:55 +0000 | [diff] [blame] | 125 | // 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] | 126 | |
Chris Lattner | c2ceed1 | 2003-05-13 21:50:52 +0000 | [diff] [blame] | 127 | |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 128 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 129 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 130 | // is a getelementptr instruction, combine the indices of the two |
| 131 | // getelementptr instructions into a single instruction. |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 132 | // |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 133 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 134 | const Type *LastTy = 0; |
| 135 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 136 | I != E; ++I) |
| 137 | LastTy = *I; |
| 138 | |
Chris Lattner | aacf2a5 | 2004-01-11 23:56:33 +0000 | [diff] [blame] | 139 | if ((LastTy && isa<ArrayType>(LastTy)) || IdxList[0]->isNullValue()) { |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 140 | std::vector<Constant*> NewIndices; |
| 141 | NewIndices.reserve(IdxList.size() + CE->getNumOperands()); |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 142 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 143 | NewIndices.push_back(cast<Constant>(CE->getOperand(i))); |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 144 | |
| 145 | // Add the last index of the source with the first index of the new GEP. |
Chris Lattner | ad70d4a | 2003-11-25 21:21:46 +0000 | [diff] [blame] | 146 | // Make sure to handle the case when they are actually different types. |
Chris Lattner | aacf2a5 | 2004-01-11 23:56:33 +0000 | [diff] [blame] | 147 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
| 148 | if (!IdxList[0]->isNullValue()) // Otherwise it must be an array |
| 149 | Combined = |
| 150 | ConstantExpr::get(Instruction::Add, |
| 151 | ConstantExpr::getCast(IdxList[0], Type::LongTy), |
| 152 | ConstantExpr::getCast(Combined, Type::LongTy)); |
| 153 | |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 154 | NewIndices.push_back(Combined); |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 155 | NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end()); |
| 156 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices); |
| 157 | } |
Chris Lattner | ddab239 | 2003-08-20 16:11:27 +0000 | [diff] [blame] | 158 | } |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 159 | |
| 160 | // Implement folding of: |
| 161 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 162 | // long 0, long 0) |
| 163 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 164 | // |
Chris Lattner | 69f6af1 | 2003-05-14 02:47:13 +0000 | [diff] [blame] | 165 | if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 && |
| 166 | IdxList[0]->isNullValue()) |
Chris Lattner | c2ceed1 | 2003-05-13 21:50:52 +0000 | [diff] [blame] | 167 | if (const PointerType *SPT = |
| 168 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 169 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 170 | if (const ArrayType *CAT = |
| 171 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
| 172 | if (CAT->getElementType() == SAT->getElementType()) |
| 173 | return ConstantExpr::getGetElementPtr( |
Chris Lattner | 55ed656 | 2003-05-14 17:51:05 +0000 | [diff] [blame] | 174 | (Constant*)CE->getOperand(0), IdxList); |
Chris Lattner | 4ede64e | 2003-06-26 05:22:45 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Chris Lattner | 9f30773 | 2002-05-06 17:54:27 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 180 | //===----------------------------------------------------------------------===// |
| 181 | // TemplateRules Class |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | // |
| 184 | // TemplateRules - Implement a subclass of ConstRules that provides all |
| 185 | // operations as noops. All other rules classes inherit from this class so |
| 186 | // that if functionality is needed in the future, it can simply be added here |
| 187 | // and to ConstRules without changing anything else... |
| 188 | // |
| 189 | // This class also provides subclasses with typesafe implementations of methods |
| 190 | // so that don't have to do type casting. |
| 191 | // |
| 192 | template<class ArgType, class SubClassName> |
| 193 | class TemplateRules : public ConstRules { |
| 194 | |
| 195 | //===--------------------------------------------------------------------===// |
| 196 | // Redirecting functions that cast to the appropriate types |
| 197 | //===--------------------------------------------------------------------===// |
| 198 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 199 | virtual Constant *add(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 200 | return SubClassName::Add((const ArgType *)V1, (const ArgType *)V2); |
| 201 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 202 | virtual Constant *sub(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 203 | return SubClassName::Sub((const ArgType *)V1, (const ArgType *)V2); |
| 204 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 205 | virtual Constant *mul(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 206 | return SubClassName::Mul((const ArgType *)V1, (const ArgType *)V2); |
| 207 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 208 | virtual Constant *div(const Constant *V1, const Constant *V2) const { |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 209 | return SubClassName::Div((const ArgType *)V1, (const ArgType *)V2); |
| 210 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 211 | virtual Constant *rem(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 212 | return SubClassName::Rem((const ArgType *)V1, (const ArgType *)V2); |
| 213 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 214 | virtual Constant *op_and(const Constant *V1, const Constant *V2) const { |
| 215 | return SubClassName::And((const ArgType *)V1, (const ArgType *)V2); |
| 216 | } |
| 217 | virtual Constant *op_or(const Constant *V1, const Constant *V2) const { |
| 218 | return SubClassName::Or((const ArgType *)V1, (const ArgType *)V2); |
| 219 | } |
| 220 | virtual Constant *op_xor(const Constant *V1, const Constant *V2) const { |
| 221 | return SubClassName::Xor((const ArgType *)V1, (const ArgType *)V2); |
| 222 | } |
| 223 | virtual Constant *shl(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 224 | return SubClassName::Shl((const ArgType *)V1, (const ArgType *)V2); |
| 225 | } |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 226 | virtual Constant *shr(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 6670d86 | 2002-05-06 03:00:54 +0000 | [diff] [blame] | 227 | return SubClassName::Shr((const ArgType *)V1, (const ArgType *)V2); |
| 228 | } |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 230 | virtual Constant *lessthan(const Constant *V1, const Constant *V2) const { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 231 | return SubClassName::LessThan((const ArgType *)V1, (const ArgType *)V2); |
| 232 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 233 | virtual Constant *equalto(const Constant *V1, const Constant *V2) const { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 234 | return SubClassName::EqualTo((const ArgType *)V1, (const ArgType *)V2); |
| 235 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 237 | // Casting operators. ick |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 238 | virtual Constant *castToBool(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 239 | return SubClassName::CastToBool((const ArgType*)V); |
| 240 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 241 | virtual Constant *castToSByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 242 | return SubClassName::CastToSByte((const ArgType*)V); |
| 243 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 244 | virtual Constant *castToUByte(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 245 | return SubClassName::CastToUByte((const ArgType*)V); |
| 246 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 247 | virtual Constant *castToShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 248 | return SubClassName::CastToShort((const ArgType*)V); |
| 249 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 250 | virtual Constant *castToUShort(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 251 | return SubClassName::CastToUShort((const ArgType*)V); |
| 252 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 253 | virtual Constant *castToInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 254 | return SubClassName::CastToInt((const ArgType*)V); |
| 255 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 256 | virtual Constant *castToUInt(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 257 | return SubClassName::CastToUInt((const ArgType*)V); |
| 258 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 259 | virtual Constant *castToLong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 260 | return SubClassName::CastToLong((const ArgType*)V); |
| 261 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 262 | virtual Constant *castToULong(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 263 | return SubClassName::CastToULong((const ArgType*)V); |
| 264 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 265 | virtual Constant *castToFloat(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 266 | return SubClassName::CastToFloat((const ArgType*)V); |
| 267 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 268 | virtual Constant *castToDouble(const Constant *V) const { |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 269 | return SubClassName::CastToDouble((const ArgType*)V); |
| 270 | } |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 271 | virtual Constant *castToPointer(const Constant *V, |
| 272 | const PointerType *Ty) const { |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 273 | return SubClassName::CastToPointer((const ArgType*)V, Ty); |
| 274 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 276 | //===--------------------------------------------------------------------===// |
| 277 | // Default "noop" implementations |
| 278 | //===--------------------------------------------------------------------===// |
| 279 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 280 | static Constant *Add(const ArgType *V1, const ArgType *V2) { return 0; } |
| 281 | static Constant *Sub(const ArgType *V1, const ArgType *V2) { return 0; } |
| 282 | static Constant *Mul(const ArgType *V1, const ArgType *V2) { return 0; } |
| 283 | static Constant *Div(const ArgType *V1, const ArgType *V2) { return 0; } |
| 284 | static Constant *Rem(const ArgType *V1, const ArgType *V2) { return 0; } |
| 285 | static Constant *And(const ArgType *V1, const ArgType *V2) { return 0; } |
| 286 | static Constant *Or (const ArgType *V1, const ArgType *V2) { return 0; } |
| 287 | static Constant *Xor(const ArgType *V1, const ArgType *V2) { return 0; } |
| 288 | static Constant *Shl(const ArgType *V1, const ArgType *V2) { return 0; } |
| 289 | static Constant *Shr(const ArgType *V1, const ArgType *V2) { return 0; } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 290 | static Constant *LessThan(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 293 | static Constant *EqualTo(const ArgType *V1, const ArgType *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 294 | return 0; |
| 295 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 296 | |
| 297 | // Casting operators. ick |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 298 | static Constant *CastToBool (const Constant *V) { return 0; } |
| 299 | static Constant *CastToSByte (const Constant *V) { return 0; } |
| 300 | static Constant *CastToUByte (const Constant *V) { return 0; } |
| 301 | static Constant *CastToShort (const Constant *V) { return 0; } |
| 302 | static Constant *CastToUShort(const Constant *V) { return 0; } |
| 303 | static Constant *CastToInt (const Constant *V) { return 0; } |
| 304 | static Constant *CastToUInt (const Constant *V) { return 0; } |
| 305 | static Constant *CastToLong (const Constant *V) { return 0; } |
| 306 | static Constant *CastToULong (const Constant *V) { return 0; } |
| 307 | static Constant *CastToFloat (const Constant *V) { return 0; } |
| 308 | static Constant *CastToDouble(const Constant *V) { return 0; } |
| 309 | static Constant *CastToPointer(const Constant *, |
| 310 | const PointerType *) {return 0;} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | |
| 314 | |
| 315 | //===----------------------------------------------------------------------===// |
| 316 | // EmptyRules Class |
| 317 | //===----------------------------------------------------------------------===// |
| 318 | // |
| 319 | // EmptyRules provides a concrete base class of ConstRules that does nothing |
| 320 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 321 | struct EmptyRules : public TemplateRules<Constant, EmptyRules> { |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 322 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 323 | if (V1 == V2) return ConstantBool::True; |
| 324 | return 0; |
| 325 | } |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 326 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 327 | |
| 328 | |
| 329 | |
| 330 | //===----------------------------------------------------------------------===// |
| 331 | // BoolRules Class |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | // |
| 334 | // BoolRules provides a concrete base class of ConstRules for the 'bool' type. |
| 335 | // |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 336 | struct BoolRules : public TemplateRules<ConstantBool, BoolRules> { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 338 | static Constant *LessThan(const ConstantBool *V1, const ConstantBool *V2){ |
Chris Lattner | 07507a4 | 2002-09-03 20:09:49 +0000 | [diff] [blame] | 339 | return ConstantBool::get(V1->getValue() < V2->getValue()); |
| 340 | } |
| 341 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 342 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 343 | return ConstantBool::get(V1 == V2); |
| 344 | } |
| 345 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 346 | static Constant *And(const ConstantBool *V1, const ConstantBool *V2) { |
| 347 | return ConstantBool::get(V1->getValue() & V2->getValue()); |
| 348 | } |
| 349 | |
| 350 | static Constant *Or(const ConstantBool *V1, const ConstantBool *V2) { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 351 | return ConstantBool::get(V1->getValue() | V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 354 | static Constant *Xor(const ConstantBool *V1, const ConstantBool *V2) { |
| 355 | return ConstantBool::get(V1->getValue() ^ V2->getValue()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | cea4d8c | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 357 | |
| 358 | // Casting operators. ick |
| 359 | #define DEF_CAST(TYPE, CLASS, CTYPE) \ |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 360 | static Constant *CastTo##TYPE (const ConstantBool *V) { \ |
Chris Lattner | cea4d8c | 2003-08-13 15:52:25 +0000 | [diff] [blame] | 361 | return CLASS::get(Type::TYPE##Ty, (CTYPE)(bool)V->getValue()); \ |
| 362 | } |
| 363 | |
| 364 | DEF_CAST(Bool , ConstantBool, bool) |
| 365 | DEF_CAST(SByte , ConstantSInt, signed char) |
| 366 | DEF_CAST(UByte , ConstantUInt, unsigned char) |
| 367 | DEF_CAST(Short , ConstantSInt, signed short) |
| 368 | DEF_CAST(UShort, ConstantUInt, unsigned short) |
| 369 | DEF_CAST(Int , ConstantSInt, signed int) |
| 370 | DEF_CAST(UInt , ConstantUInt, unsigned int) |
| 371 | DEF_CAST(Long , ConstantSInt, int64_t) |
| 372 | DEF_CAST(ULong , ConstantUInt, uint64_t) |
| 373 | DEF_CAST(Float , ConstantFP , float) |
| 374 | DEF_CAST(Double, ConstantFP , double) |
| 375 | #undef DEF_CAST |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 376 | }; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 377 | |
| 378 | |
| 379 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 380 | // NullPointerRules Class |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 381 | //===----------------------------------------------------------------------===// |
| 382 | // |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 383 | // NullPointerRules provides a concrete base class of ConstRules for null |
| 384 | // pointers. |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 385 | // |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 386 | struct NullPointerRules : public TemplateRules<ConstantPointerNull, |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 387 | NullPointerRules> { |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 388 | static Constant *EqualTo(const Constant *V1, const Constant *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 389 | return ConstantBool::True; // Null pointers are always equal |
| 390 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 391 | static Constant *CastToBool(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 392 | return ConstantBool::False; |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 393 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 394 | static Constant *CastToSByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 395 | return ConstantSInt::get(Type::SByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 396 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 397 | static Constant *CastToUByte (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 398 | return ConstantUInt::get(Type::UByteTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 399 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 400 | static Constant *CastToShort (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 401 | return ConstantSInt::get(Type::ShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 402 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 403 | static Constant *CastToUShort(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 404 | return ConstantUInt::get(Type::UShortTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 405 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 406 | static Constant *CastToInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 407 | return ConstantSInt::get(Type::IntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 408 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 409 | static Constant *CastToUInt (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 410 | return ConstantUInt::get(Type::UIntTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 412 | static Constant *CastToLong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 413 | return ConstantSInt::get(Type::LongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 414 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 415 | static Constant *CastToULong (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 416 | return ConstantUInt::get(Type::ULongTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 418 | static Constant *CastToFloat (const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 419 | return ConstantFP::get(Type::FloatTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 420 | } |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 421 | static Constant *CastToDouble(const Constant *V) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 422 | return ConstantFP::get(Type::DoubleTy, 0); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Chris Lattner | 77f20dc | 2003-11-17 19:21:04 +0000 | [diff] [blame] | 425 | static Constant *CastToPointer(const ConstantPointerNull *V, |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 426 | const PointerType *PTy) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 427 | return ConstantPointerNull::get(PTy); |
Chris Lattner | 977f004 | 2001-11-01 05:55:13 +0000 | [diff] [blame] | 428 | } |
| 429 | }; |
| 430 | |
| 431 | |
| 432 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 433 | // DirectRules Class |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | // |
| 436 | // DirectRules provides a concrete base classes of ConstRules for a variety of |
| 437 | // different types. This allows the C++ compiler to automatically generate our |
| 438 | // constant handling operations in a typesafe and accurate manner. |
| 439 | // |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 440 | template<class ConstantClass, class BuiltinType, Type **Ty, class SuperClass> |
| 441 | struct DirectRules : public TemplateRules<ConstantClass, SuperClass> { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 442 | static Constant *Add(const ConstantClass *V1, const ConstantClass *V2) { |
| 443 | BuiltinType R = (BuiltinType)V1->getValue() + (BuiltinType)V2->getValue(); |
| 444 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 447 | static Constant *Sub(const ConstantClass *V1, const ConstantClass *V2) { |
| 448 | BuiltinType R = (BuiltinType)V1->getValue() - (BuiltinType)V2->getValue(); |
| 449 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 452 | static Constant *Mul(const ConstantClass *V1, const ConstantClass *V2) { |
| 453 | BuiltinType R = (BuiltinType)V1->getValue() * (BuiltinType)V2->getValue(); |
| 454 | return ConstantClass::get(*Ty, R); |
Chris Lattner | 4f6031f | 2001-07-20 19:15:36 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 457 | static Constant *Div(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | 0a144ad | 2002-05-03 21:41:07 +0000 | [diff] [blame] | 458 | if (V2->isNullValue()) return 0; |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 459 | BuiltinType R = (BuiltinType)V1->getValue() / (BuiltinType)V2->getValue(); |
| 460 | return ConstantClass::get(*Ty, R); |
Chris Lattner | af259a7 | 2002-04-07 08:10:14 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 463 | static Constant *LessThan(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | e87f65e | 2002-07-30 16:24:28 +0000 | [diff] [blame] | 464 | bool R = (BuiltinType)V1->getValue() < (BuiltinType)V2->getValue(); |
| 465 | return ConstantBool::get(R); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 466 | } |
Chris Lattner | 5540684 | 2001-07-21 19:10:49 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 468 | static Constant *EqualTo(const ConstantClass *V1, const ConstantClass *V2) { |
Chris Lattner | dc2e391 | 2003-11-17 20:19:35 +0000 | [diff] [blame] | 469 | bool R = (BuiltinType)V1->getValue() == (BuiltinType)V2->getValue(); |
| 470 | return ConstantBool::get(R); |
| 471 | } |
| 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 | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame^] | 482 | static Constant *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 | f8348c3 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 576 | ConstRules &ConstRules::get(const Constant *V1, const Constant *V2) { |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 577 | static EmptyRules EmptyR; |
| 578 | static BoolRules BoolR; |
| 579 | static NullPointerRules NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 580 | static DirectIntRules<ConstantSInt, signed char , &Type::SByteTy> SByteR; |
| 581 | static DirectIntRules<ConstantUInt, unsigned char , &Type::UByteTy> UByteR; |
| 582 | static DirectIntRules<ConstantSInt, signed short, &Type::ShortTy> ShortR; |
| 583 | static DirectIntRules<ConstantUInt, unsigned short, &Type::UShortTy> UShortR; |
| 584 | static DirectIntRules<ConstantSInt, signed int , &Type::IntTy> IntR; |
| 585 | static DirectIntRules<ConstantUInt, unsigned int , &Type::UIntTy> UIntR; |
| 586 | static DirectIntRules<ConstantSInt, int64_t , &Type::LongTy> LongR; |
| 587 | static DirectIntRules<ConstantUInt, uint64_t , &Type::ULongTy> ULongR; |
| 588 | static DirectFPRules <ConstantFP , float , &Type::FloatTy> FloatR; |
| 589 | static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 591 | if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) || |
| 592 | isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2)) |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 593 | return EmptyR; |
| 594 | |
Chris Lattner | f8348c3 | 2004-01-12 20:41:05 +0000 | [diff] [blame] | 595 | switch (V1->getType()->getPrimitiveID()) { |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 596 | default: assert(0 && "Unknown value type for constant folding!"); |
| 597 | case Type::BoolTyID: return BoolR; |
Chris Lattner | 4b6addf | 2003-11-17 19:19:32 +0000 | [diff] [blame] | 598 | case Type::PointerTyID: return NullPointerR; |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 599 | case Type::SByteTyID: return SByteR; |
| 600 | case Type::UByteTyID: return UByteR; |
| 601 | case Type::ShortTyID: return ShortR; |
| 602 | case Type::UShortTyID: return UShortR; |
| 603 | case Type::IntTyID: return IntR; |
| 604 | case Type::UIntTyID: return UIntR; |
| 605 | case Type::LongTyID: return LongR; |
| 606 | case Type::ULongTyID: return ULongR; |
| 607 | case Type::FloatTyID: return FloatR; |
| 608 | case Type::DoubleTyID: return DoubleR; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 610 | } |