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