Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 1 | //===- ConstantFold.cpp - LLVM constant folder ----------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 10 | // This file implements folding of constants for LLVM. This implements the |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 11 | // (internal) ConstantFold.h interface, which is used by the |
Chris Lattner | cbfd406 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 12 | // ConstantExpr::get* methods to automatically fold constants when possible. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 14 | // The current constant folding implementation is implemented in two pieces: the |
| 15 | // template-based folder for simple primitive constants like ConstantInt, and |
| 16 | // the special case hackery that we use to symbolically evaluate expressions |
| 17 | // that use ConstantExprs. |
| 18 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | 92f6fea | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 21 | #include "ConstantFold.h" |
Chris Lattner | 8b0f0cb | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | b228f9c | 2004-02-22 06:25:38 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Chris Lattner | 27287de | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 802b5ab | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 25 | #include "llvm/Function.h" |
Chris Lattner | a0ef5ed | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 26 | #include "llvm/GlobalAlias.h" |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 0eff5ad | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 29 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 30 | #include "llvm/Support/ManagedStatic.h" |
| 31 | #include "llvm/Support/MathExtras.h" |
Jeff Cohen | a97e8db | 2005-05-03 03:13:01 +0000 | [diff] [blame] | 32 | #include <limits> |
Chris Lattner | 0dc3969 | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Chris Lattner | 14712a6 | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 34 | |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // ConstantFold*Instruction Implementations |
| 37 | //===----------------------------------------------------------------------===// |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 9eaa835 | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 39 | /// BitCastConstantVector - Convert the specified ConstantVector node to the |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 40 | /// specified vector type. At this point, we know that the elements of the |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 41 | /// input vector constant are all simple integer or FP values. |
Chris Lattner | 9eaa835 | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 42 | static Constant *BitCastConstantVector(ConstantVector *CV, |
| 43 | const VectorType *DstTy) { |
| 44 | // If this cast changes element count then we can't handle it here: |
| 45 | // doing so requires endianness information. This should be handled by |
| 46 | // Analysis/ConstantFolding.cpp |
| 47 | unsigned NumElts = DstTy->getNumElements(); |
| 48 | if (NumElts != CV->getNumOperands()) |
| 49 | return 0; |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 9eaa835 | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 51 | // Check to verify that all elements of the input are simple. |
| 52 | for (unsigned i = 0; i != NumElts; ++i) { |
| 53 | if (!isa<ConstantInt>(CV->getOperand(i)) && |
| 54 | !isa<ConstantFP>(CV->getOperand(i))) |
| 55 | return 0; |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 56 | } |
Chris Lattner | 9eaa835 | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 57 | |
| 58 | // Bitcast each element now. |
| 59 | std::vector<Constant*> Result; |
| 60 | const Type *DstEltTy = DstTy->getElementType(); |
| 61 | for (unsigned i = 0; i != NumElts; ++i) |
| 62 | Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i), DstEltTy)); |
| 63 | return ConstantVector::get(Result); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 66 | /// This function determines which opcode to use to fold two constant cast |
| 67 | /// expressions together. It uses CastInst::isEliminableCastPair to determine |
| 68 | /// the opcode. Consequently its just a wrapper around that function. |
Reid Spencer | daa10a4 | 2007-08-05 19:27:01 +0000 | [diff] [blame] | 69 | /// @brief Determine if it is valid to fold a cast of a cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 70 | static unsigned |
| 71 | foldConstantCastPair( |
| 72 | unsigned opc, ///< opcode of the second cast constant expression |
| 73 | const ConstantExpr*Op, ///< the first cast constant expression |
| 74 | const Type *DstTy ///< desintation type of the first cast |
| 75 | ) { |
| 76 | assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); |
| 77 | assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); |
| 78 | assert(CastInst::isCast(opc) && "Invalid cast opcode"); |
| 79 | |
| 80 | // The the types and opcodes for the two Cast constant expressions |
| 81 | const Type *SrcTy = Op->getOperand(0)->getType(); |
| 82 | const Type *MidTy = Op->getType(); |
| 83 | Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); |
| 84 | Instruction::CastOps secondOp = Instruction::CastOps(opc); |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 85 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 86 | // Let CastInst::isEliminableCastPair do the heavy lifting. |
| 87 | return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 88 | Type::Int64Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chris Lattner | 2b0f806 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 91 | static Constant *FoldBitCast(Constant *V, const Type *DestTy) { |
| 92 | const Type *SrcTy = V->getType(); |
| 93 | if (SrcTy == DestTy) |
| 94 | return V; // no-op cast |
| 95 | |
| 96 | // Check to see if we are casting a pointer to an aggregate to a pointer to |
| 97 | // the first element. If so, return the appropriate GEP instruction. |
| 98 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) |
| 99 | if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) { |
| 100 | SmallVector<Value*, 8> IdxList; |
| 101 | IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); |
| 102 | const Type *ElTy = PTy->getElementType(); |
| 103 | while (ElTy != DPTy->getElementType()) { |
| 104 | if (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 105 | if (STy->getNumElements() == 0) break; |
| 106 | ElTy = STy->getElementType(0); |
| 107 | IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); |
| 108 | } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) { |
| 109 | if (isa<PointerType>(ElTy)) break; // Can't index into pointers! |
| 110 | ElTy = STy->getElementType(); |
| 111 | IdxList.push_back(IdxList[0]); |
| 112 | } else { |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (ElTy == DPTy->getElementType()) |
| 118 | return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size()); |
| 119 | } |
| 120 | |
| 121 | // Handle casts from one vector constant to another. We know that the src |
| 122 | // and dest type have the same size (otherwise its an illegal cast). |
| 123 | if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 124 | if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) { |
| 125 | assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() && |
| 126 | "Not cast between same sized vectors!"); |
| 127 | // First, check for null. Undef is already handled. |
| 128 | if (isa<ConstantAggregateZero>(V)) |
| 129 | return Constant::getNullValue(DestTy); |
| 130 | |
Chris Lattner | 9eaa835 | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 131 | if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 132 | return BitCastConstantVector(CV, DestPTy); |
Chris Lattner | 2b0f806 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | // Finally, implement bitcast folding now. The code below doesn't handle |
| 137 | // bitcast right. |
| 138 | if (isa<ConstantPointerNull>(V)) // ptr->ptr cast. |
| 139 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
| 140 | |
| 141 | // Handle integral constant input. |
| 142 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 143 | if (DestTy->isInteger()) |
| 144 | // Integral -> Integral. This is a no-op because the bit widths must |
| 145 | // be the same. Consequently, we just fold to V. |
| 146 | return V; |
| 147 | |
| 148 | if (DestTy->isFloatingPoint()) { |
| 149 | assert((DestTy == Type::DoubleTy || DestTy == Type::FloatTy) && |
| 150 | "Unknown FP type!"); |
| 151 | return ConstantFP::get(DestTy, APFloat(CI->getValue())); |
| 152 | } |
| 153 | // Otherwise, can't fold this (vector?) |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | // Handle ConstantFP input. |
| 158 | if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) { |
| 159 | // FP -> Integral. |
| 160 | if (DestTy == Type::Int32Ty) { |
| 161 | return ConstantInt::get(FP->getValueAPF().convertToAPInt()); |
| 162 | } else { |
| 163 | assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!"); |
| 164 | return ConstantInt::get(FP->getValueAPF().convertToAPInt()); |
| 165 | } |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 171 | Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 172 | const Type *DestTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 173 | const Type *SrcTy = V->getType(); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 174 | |
Chris Lattner | d2f0996 | 2007-07-20 22:09:02 +0000 | [diff] [blame] | 175 | if (isa<UndefValue>(V)) { |
| 176 | // zext(undef) = 0, because the top bits will be zero. |
| 177 | // sext(undef) = 0, because the top bits will all be the same. |
| 178 | if (opc == Instruction::ZExt || opc == Instruction::SExt) |
| 179 | return Constant::getNullValue(DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 180 | return UndefValue::get(DestTy); |
Chris Lattner | d2f0996 | 2007-07-20 22:09:02 +0000 | [diff] [blame] | 181 | } |
Dale Johannesen | 5927d8e | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 182 | // No compile-time operations on this type yet. |
| 183 | if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty) |
| 184 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 185 | |
| 186 | // If the cast operand is a constant expression, there's a few things we can |
| 187 | // do to try to simplify it. |
| 188 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 189 | if (CE->isCast()) { |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 190 | // Try hard to fold cast of cast because they are often eliminable. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 191 | if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) |
| 192 | return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 193 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 194 | // If all of the indexes in the GEP are null values, there is no pointer |
| 195 | // adjustment going on. We might as well cast the source pointer. |
| 196 | bool isAllNull = true; |
| 197 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 198 | if (!CE->getOperand(i)->isNullValue()) { |
| 199 | isAllNull = false; |
| 200 | break; |
| 201 | } |
| 202 | if (isAllNull) |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 203 | // This is casting one pointer type to another, always BitCast |
Reid Spencer | 57c6993 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 204 | return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 207 | |
Reid Spencer | 390437f | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 208 | // We actually have to do a cast now. Perform the cast according to the |
| 209 | // opcode specified. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 210 | switch (opc) { |
| 211 | case Instruction::FPTrunc: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 212 | case Instruction::FPExt: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 213 | if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
Dale Johannesen | ac12205 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 214 | APFloat Val = FPC->getValueAPF(); |
| 215 | Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle : |
| 216 | DestTy == Type::DoubleTy ? APFloat::IEEEdouble : |
| 217 | DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended : |
| 218 | DestTy == Type::FP128Ty ? APFloat::IEEEquad : |
| 219 | APFloat::Bogus, |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 220 | APFloat::rmNearestTiesToEven); |
| 221 | return ConstantFP::get(DestTy, Val); |
| 222 | } |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 223 | return 0; // Can't fold. |
| 224 | case Instruction::FPToUI: |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 225 | case Instruction::FPToSI: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 226 | if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
Chris Lattner | 3cab1c7 | 2007-10-15 05:34:10 +0000 | [diff] [blame] | 227 | const APFloat &V = FPC->getValueAPF(); |
Dale Johannesen | ac12205 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 228 | uint64_t x[2]; |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 229 | uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
Dale Johannesen | 350add8 | 2007-09-25 23:32:20 +0000 | [diff] [blame] | 230 | (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, |
| 231 | APFloat::rmTowardZero); |
Dale Johannesen | ac12205 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 232 | APInt Val(DestBitWidth, 2, x); |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 233 | return ConstantInt::get(Val); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 234 | } |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 235 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) { |
| 236 | std::vector<Constant*> res; |
| 237 | const VectorType *DestVecTy = cast<VectorType>(DestTy); |
| 238 | const Type *DstEltTy = DestVecTy->getElementType(); |
| 239 | for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) |
| 240 | res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i), |
| 241 | DstEltTy)); |
| 242 | return ConstantVector::get(DestVecTy, res); |
| 243 | } |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 244 | return 0; // Can't fold. |
| 245 | case Instruction::IntToPtr: //always treated as unsigned |
| 246 | if (V->isNullValue()) // Is it an integral null value? |
Reid Spencer | 390437f | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 247 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 248 | return 0; // Other pointer types cannot be casted |
| 249 | case Instruction::PtrToInt: // always treated as unsigned |
| 250 | if (V->isNullValue()) // is it a null pointer value? |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 251 | return ConstantInt::get(DestTy, 0); |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 252 | return 0; // Other pointer types cannot be casted |
| 253 | case Instruction::UIToFP: |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 254 | case Instruction::SIToFP: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 255 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dale Johannesen | 88216af | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 256 | APInt api = CI->getValue(); |
| 257 | const uint64_t zero[] = {0, 0}; |
| 258 | uint32_t BitWidth = cast<IntegerType>(SrcTy)->getBitWidth(); |
| 259 | APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(), |
| 260 | 2, zero)); |
Neil Booth | ccf596a | 2007-10-07 11:45:55 +0000 | [diff] [blame] | 261 | (void)apf.convertFromZeroExtendedInteger(api.getRawData(), BitWidth, |
Dale Johannesen | 88216af | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 262 | opc==Instruction::SIToFP, |
| 263 | APFloat::rmNearestTiesToEven); |
| 264 | return ConstantFP::get(DestTy, apf); |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 265 | } |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 266 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) { |
| 267 | std::vector<Constant*> res; |
| 268 | const VectorType *DestVecTy = cast<VectorType>(DestTy); |
| 269 | const Type *DstEltTy = DestVecTy->getElementType(); |
| 270 | for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) |
| 271 | res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i), |
| 272 | DstEltTy)); |
| 273 | return ConstantVector::get(DestVecTy, res); |
| 274 | } |
Reid Spencer | 85e36e4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 275 | return 0; |
Reid Spencer | 390437f | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 276 | case Instruction::ZExt: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 277 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 278 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 279 | APInt Result(CI->getValue()); |
| 280 | Result.zext(BitWidth); |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 281 | return ConstantInt::get(Result); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 282 | } |
Reid Spencer | 390437f | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 283 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 284 | case Instruction::SExt: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 285 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 286 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 287 | APInt Result(CI->getValue()); |
| 288 | Result.sext(BitWidth); |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 289 | return ConstantInt::get(Result); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 290 | } |
Reid Spencer | 390437f | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 291 | return 0; |
Chris Lattner | 5be6625 | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 292 | case Instruction::Trunc: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 293 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 294 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 295 | APInt Result(CI->getValue()); |
| 296 | Result.trunc(BitWidth); |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 297 | return ConstantInt::get(Result); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 298 | } |
Chris Lattner | 5be6625 | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 299 | return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 300 | case Instruction::BitCast: |
Chris Lattner | 2b0f806 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 301 | return FoldBitCast(const_cast<Constant*>(V), DestTy); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 302 | default: |
| 303 | assert(!"Invalid CE CastInst opcode"); |
| 304 | break; |
Chris Lattner | 4460f40 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | f8d1097 | 2004-10-11 03:57:30 +0000 | [diff] [blame] | 306 | |
Reid Spencer | 390437f | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 307 | assert(0 && "Failed to cast constant expression"); |
| 308 | return 0; |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | e971486 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 311 | Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond, |
| 312 | const Constant *V1, |
| 313 | const Constant *V2) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 314 | if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond)) |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 315 | return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2); |
Chris Lattner | 71d3778 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 316 | |
| 317 | if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2); |
| 318 | if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1); |
| 319 | if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1); |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 320 | if (V1 == V2) return const_cast<Constant*>(V1); |
Chris Lattner | e971486 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 324 | Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, |
| 325 | const Constant *Idx) { |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 326 | if (isa<UndefValue>(Val)) // ee(undef, x) -> undef |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 327 | return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); |
Chris Lattner | f38d471 | 2006-04-07 04:44:06 +0000 | [diff] [blame] | 328 | if (Val->isNullValue()) // ee(zero, x) -> zero |
| 329 | return Constant::getNullValue( |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 330 | cast<VectorType>(Val->getType())->getElementType()); |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 331 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 332 | if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 333 | if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { |
| 334 | return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue())); |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 335 | } else if (isa<UndefValue>(Idx)) { |
| 336 | // ee({w,x,y,z}, undef) -> w (an arbitrary value). |
| 337 | return const_cast<Constant*>(CVal->getOperand(0)); |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 338 | } |
Chris Lattner | 6fa4cdf | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 339 | } |
Robert Bocchino | bb90a7a | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 343 | Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, |
| 344 | const Constant *Elt, |
| 345 | const Constant *Idx) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 346 | const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 347 | if (!CIdx) return 0; |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 348 | APInt idxVal = CIdx->getValue(); |
Reid Spencer | 4ab09d4 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 349 | if (isa<UndefValue>(Val)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 350 | // Insertion of scalar constant into vector undef |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 351 | // Optimize away insertion of undef |
| 352 | if (isa<UndefValue>(Elt)) |
| 353 | return const_cast<Constant*>(Val); |
| 354 | // Otherwise break the aggregate undef into multiple undefs and do |
| 355 | // the insertion |
| 356 | unsigned numOps = |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 357 | cast<VectorType>(Val->getType())->getNumElements(); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 358 | std::vector<Constant*> Ops; |
| 359 | Ops.reserve(numOps); |
| 360 | for (unsigned i = 0; i < numOps; ++i) { |
| 361 | const Constant *Op = |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 362 | (idxVal == i) ? Elt : UndefValue::get(Elt->getType()); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 363 | Ops.push_back(const_cast<Constant*>(Op)); |
| 364 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 365 | return ConstantVector::get(Ops); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 366 | } |
Reid Spencer | 4ab09d4 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 367 | if (isa<ConstantAggregateZero>(Val)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 368 | // Insertion of scalar constant into vector aggregate zero |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 369 | // Optimize away insertion of zero |
| 370 | if (Elt->isNullValue()) |
| 371 | return const_cast<Constant*>(Val); |
| 372 | // Otherwise break the aggregate zero into multiple zeros and do |
| 373 | // the insertion |
| 374 | unsigned numOps = |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 375 | cast<VectorType>(Val->getType())->getNumElements(); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 376 | std::vector<Constant*> Ops; |
| 377 | Ops.reserve(numOps); |
| 378 | for (unsigned i = 0; i < numOps; ++i) { |
| 379 | const Constant *Op = |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 380 | (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType()); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 381 | Ops.push_back(const_cast<Constant*>(Op)); |
| 382 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 383 | return ConstantVector::get(Ops); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 384 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 385 | if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 386 | // Insertion of scalar constant into vector constant |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 387 | std::vector<Constant*> Ops; |
| 388 | Ops.reserve(CVal->getNumOperands()); |
| 389 | for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { |
| 390 | const Constant *Op = |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 391 | (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i)); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 392 | Ops.push_back(const_cast<Constant*>(Op)); |
| 393 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 394 | return ConstantVector::get(Ops); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
Chris Lattner | 7431c2b | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 399 | /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef |
| 400 | /// return the specified element value. Otherwise return null. |
| 401 | static Constant *GetVectorElement(const Constant *C, unsigned EltNo) { |
| 402 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) |
| 403 | return const_cast<Constant*>(CV->getOperand(EltNo)); |
| 404 | |
| 405 | const Type *EltTy = cast<VectorType>(C->getType())->getElementType(); |
| 406 | if (isa<ConstantAggregateZero>(C)) |
| 407 | return Constant::getNullValue(EltTy); |
| 408 | if (isa<UndefValue>(C)) |
| 409 | return UndefValue::get(EltTy); |
| 410 | return 0; |
| 411 | } |
| 412 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 413 | Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1, |
| 414 | const Constant *V2, |
| 415 | const Constant *Mask) { |
Chris Lattner | 7431c2b | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 416 | // Undefined shuffle mask -> undefined value. |
| 417 | if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType()); |
| 418 | |
| 419 | unsigned NumElts = cast<VectorType>(V1->getType())->getNumElements(); |
| 420 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
| 421 | |
| 422 | // Loop over the shuffle mask, evaluating each element. |
| 423 | SmallVector<Constant*, 32> Result; |
| 424 | for (unsigned i = 0; i != NumElts; ++i) { |
| 425 | Constant *InElt = GetVectorElement(Mask, i); |
| 426 | if (InElt == 0) return 0; |
| 427 | |
| 428 | if (isa<UndefValue>(InElt)) |
| 429 | InElt = UndefValue::get(EltTy); |
| 430 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) { |
| 431 | unsigned Elt = CI->getZExtValue(); |
| 432 | if (Elt >= NumElts*2) |
| 433 | InElt = UndefValue::get(EltTy); |
| 434 | else if (Elt >= NumElts) |
| 435 | InElt = GetVectorElement(V2, Elt-NumElts); |
| 436 | else |
| 437 | InElt = GetVectorElement(V1, Elt); |
| 438 | if (InElt == 0) return 0; |
| 439 | } else { |
| 440 | // Unknown value. |
| 441 | return 0; |
| 442 | } |
| 443 | Result.push_back(InElt); |
| 444 | } |
| 445 | |
| 446 | return ConstantVector::get(&Result[0], Result.size()); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 449 | /// EvalVectorOp - Given two vector constants and a function pointer, apply the |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 450 | /// function pointer to each element pair, producing a new ConstantVector |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 451 | /// constant. Either or both of V1 and V2 may be NULL, meaning a |
| 452 | /// ConstantAggregateZero operand. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 453 | static Constant *EvalVectorOp(const ConstantVector *V1, |
| 454 | const ConstantVector *V2, |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 455 | const VectorType *VTy, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 456 | Constant *(*FP)(Constant*, Constant*)) { |
| 457 | std::vector<Constant*> Res; |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 458 | const Type *EltTy = VTy->getElementType(); |
| 459 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 460 | const Constant *C1 = V1 ? V1->getOperand(i) : Constant::getNullValue(EltTy); |
| 461 | const Constant *C2 = V2 ? V2->getOperand(i) : Constant::getNullValue(EltTy); |
| 462 | Res.push_back(FP(const_cast<Constant*>(C1), |
| 463 | const_cast<Constant*>(C2))); |
| 464 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 465 | return ConstantVector::get(Res); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, |
| 469 | const Constant *C1, |
| 470 | const Constant *C2) { |
Dale Johannesen | db44bf8 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 471 | // No compile-time operations on this type yet. |
| 472 | if (C1->getType() == Type::PPC_FP128Ty) |
| 473 | return 0; |
| 474 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 475 | // Handle UndefValue up front |
| 476 | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { |
| 477 | switch (Opcode) { |
| 478 | case Instruction::Add: |
| 479 | case Instruction::Sub: |
| 480 | case Instruction::Xor: |
| 481 | return UndefValue::get(C1->getType()); |
| 482 | case Instruction::Mul: |
| 483 | case Instruction::And: |
| 484 | return Constant::getNullValue(C1->getType()); |
| 485 | case Instruction::UDiv: |
| 486 | case Instruction::SDiv: |
| 487 | case Instruction::FDiv: |
| 488 | case Instruction::URem: |
| 489 | case Instruction::SRem: |
| 490 | case Instruction::FRem: |
| 491 | if (!isa<UndefValue>(C2)) // undef / X -> 0 |
| 492 | return Constant::getNullValue(C1->getType()); |
| 493 | return const_cast<Constant*>(C2); // X / undef -> undef |
| 494 | case Instruction::Or: // X | undef -> -1 |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 495 | if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType())) |
| 496 | return ConstantVector::getAllOnesValue(PTy); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 497 | return ConstantInt::getAllOnesValue(C1->getType()); |
| 498 | case Instruction::LShr: |
| 499 | if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) |
| 500 | return const_cast<Constant*>(C1); // undef lshr undef -> undef |
| 501 | return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 |
| 502 | // undef lshr X -> 0 |
| 503 | case Instruction::AShr: |
| 504 | if (!isa<UndefValue>(C2)) |
| 505 | return const_cast<Constant*>(C1); // undef ashr X --> undef |
| 506 | else if (isa<UndefValue>(C1)) |
| 507 | return const_cast<Constant*>(C1); // undef ashr undef -> undef |
| 508 | else |
| 509 | return const_cast<Constant*>(C1); // X ashr undef --> X |
| 510 | case Instruction::Shl: |
| 511 | // undef << X -> 0 or X << undef -> 0 |
| 512 | return Constant::getNullValue(C1->getType()); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { |
| 517 | if (isa<ConstantExpr>(C2)) { |
| 518 | // There are many possible foldings we could do here. We should probably |
| 519 | // at least fold add of a pointer with an integer into the appropriate |
| 520 | // getelementptr. This will improve alias analysis a bit. |
| 521 | } else { |
| 522 | // Just implement a couple of simple identities. |
| 523 | switch (Opcode) { |
| 524 | case Instruction::Add: |
| 525 | if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X |
| 526 | break; |
| 527 | case Instruction::Sub: |
| 528 | if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X |
| 529 | break; |
| 530 | case Instruction::Mul: |
| 531 | if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0 |
| 532 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 533 | if (CI->equalsInt(1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 534 | return const_cast<Constant*>(C1); // X * 1 == X |
| 535 | break; |
| 536 | case Instruction::UDiv: |
| 537 | case Instruction::SDiv: |
| 538 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 539 | if (CI->equalsInt(1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 540 | return const_cast<Constant*>(C1); // X / 1 == X |
| 541 | break; |
| 542 | case Instruction::URem: |
| 543 | case Instruction::SRem: |
| 544 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 545 | if (CI->equalsInt(1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 546 | return Constant::getNullValue(CI->getType()); // X % 1 == 0 |
| 547 | break; |
| 548 | case Instruction::And: |
Chris Lattner | 2ef14d9 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 549 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) { |
| 550 | if (CI->isZero()) return const_cast<Constant*>(C2); // X & 0 == 0 |
Chris Lattner | e94d8b2 | 2007-01-04 01:56:39 +0000 | [diff] [blame] | 551 | if (CI->isAllOnesValue()) |
| 552 | return const_cast<Constant*>(C1); // X & -1 == X |
Chris Lattner | 2ef14d9 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 553 | |
| 554 | // (zext i32 to i64) & 4294967295 -> (zext i32 to i64) |
| 555 | if (CE1->getOpcode() == Instruction::ZExt) { |
| 556 | APInt PossiblySetBits |
| 557 | = cast<IntegerType>(CE1->getOperand(0)->getType())->getMask(); |
| 558 | PossiblySetBits.zext(C1->getType()->getPrimitiveSizeInBits()); |
| 559 | if ((PossiblySetBits & CI->getValue()) == PossiblySetBits) |
| 560 | return const_cast<Constant*>(C1); |
| 561 | } |
| 562 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 563 | if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) { |
| 564 | GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0)); |
| 565 | |
| 566 | // Functions are at least 4-byte aligned. If and'ing the address of a |
| 567 | // function with a constant < 4, fold it to zero. |
| 568 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 569 | if (CI->getValue().ult(APInt(CI->getType()->getBitWidth(),4)) && |
| 570 | isa<Function>(CPR)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 571 | return Constant::getNullValue(CI->getType()); |
| 572 | } |
| 573 | break; |
| 574 | case Instruction::Or: |
| 575 | if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X |
Chris Lattner | e94d8b2 | 2007-01-04 01:56:39 +0000 | [diff] [blame] | 576 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) |
| 577 | if (CI->isAllOnesValue()) |
| 578 | return const_cast<Constant*>(C2); // X | -1 == -1 |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 579 | break; |
| 580 | case Instruction::Xor: |
| 581 | if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X |
| 582 | break; |
Chris Lattner | 2ef14d9 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 583 | case Instruction::AShr: |
Reid Spencer | 4ada00d | 2007-03-26 20:09:02 +0000 | [diff] [blame] | 584 | // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2 |
Chris Lattner | 2ef14d9 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 585 | if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero. |
| 586 | return ConstantExpr::getLShr(const_cast<Constant*>(C1), |
| 587 | const_cast<Constant*>(C2)); |
| 588 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } else if (isa<ConstantExpr>(C2)) { |
| 592 | // If C2 is a constant expr and C1 isn't, flop them around and fold the |
| 593 | // other way if possible. |
| 594 | switch (Opcode) { |
| 595 | case Instruction::Add: |
| 596 | case Instruction::Mul: |
| 597 | case Instruction::And: |
| 598 | case Instruction::Or: |
| 599 | case Instruction::Xor: |
| 600 | // No change of opcode required. |
| 601 | return ConstantFoldBinaryInstruction(Opcode, C2, C1); |
| 602 | |
| 603 | case Instruction::Shl: |
| 604 | case Instruction::LShr: |
| 605 | case Instruction::AShr: |
| 606 | case Instruction::Sub: |
| 607 | case Instruction::SDiv: |
| 608 | case Instruction::UDiv: |
| 609 | case Instruction::FDiv: |
| 610 | case Instruction::URem: |
| 611 | case Instruction::SRem: |
| 612 | case Instruction::FRem: |
| 613 | default: // These instructions cannot be flopped around. |
| 614 | return 0; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | // At this point we know neither constant is an UndefValue nor a ConstantExpr |
Chris Lattner | e94d8b2 | 2007-01-04 01:56:39 +0000 | [diff] [blame] | 619 | // so look at directly computing the value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 620 | if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { |
| 621 | if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 622 | using namespace APIntOps; |
| 623 | APInt C1V = CI1->getValue(); |
| 624 | APInt C2V = CI2->getValue(); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 625 | switch (Opcode) { |
| 626 | default: |
| 627 | break; |
| 628 | case Instruction::Add: |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 629 | return ConstantInt::get(C1V + C2V); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 630 | case Instruction::Sub: |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 631 | return ConstantInt::get(C1V - C2V); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 632 | case Instruction::Mul: |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 633 | return ConstantInt::get(C1V * C2V); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 634 | case Instruction::UDiv: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 635 | if (CI2->isNullValue()) |
| 636 | return 0; // X / 0 -> can't fold |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 637 | return ConstantInt::get(C1V.udiv(C2V)); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 638 | case Instruction::SDiv: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 639 | if (CI2->isNullValue()) |
| 640 | return 0; // X / 0 -> can't fold |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 641 | if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) |
| 642 | return 0; // MIN_INT / -1 -> overflow |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 643 | return ConstantInt::get(C1V.sdiv(C2V)); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 644 | case Instruction::URem: |
| 645 | if (C2->isNullValue()) |
| 646 | return 0; // X / 0 -> can't fold |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 647 | return ConstantInt::get(C1V.urem(C2V)); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 648 | case Instruction::SRem: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 649 | if (CI2->isNullValue()) |
| 650 | return 0; // X % 0 -> can't fold |
| 651 | if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) |
| 652 | return 0; // MIN_INT % -1 -> overflow |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 653 | return ConstantInt::get(C1V.srem(C2V)); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 654 | case Instruction::And: |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 655 | return ConstantInt::get(C1V & C2V); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 656 | case Instruction::Or: |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 657 | return ConstantInt::get(C1V | C2V); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 658 | case Instruction::Xor: |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 659 | return ConstantInt::get(C1V ^ C2V); |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 660 | case Instruction::Shl: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 661 | if (uint32_t shiftAmt = C2V.getZExtValue()) |
Reid Spencer | a5c7472 | 2007-02-27 19:29:54 +0000 | [diff] [blame] | 662 | if (shiftAmt < C1V.getBitWidth()) |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 663 | return ConstantInt::get(C1V.shl(shiftAmt)); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 664 | else |
| 665 | return UndefValue::get(C1->getType()); // too big shift is undef |
| 666 | return const_cast<ConstantInt*>(CI1); // Zero shift is identity |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 667 | case Instruction::LShr: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 668 | if (uint32_t shiftAmt = C2V.getZExtValue()) |
Reid Spencer | a5c7472 | 2007-02-27 19:29:54 +0000 | [diff] [blame] | 669 | if (shiftAmt < C1V.getBitWidth()) |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 670 | return ConstantInt::get(C1V.lshr(shiftAmt)); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 671 | else |
| 672 | return UndefValue::get(C1->getType()); // too big shift is undef |
| 673 | return const_cast<ConstantInt*>(CI1); // Zero shift is identity |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 674 | case Instruction::AShr: |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 675 | if (uint32_t shiftAmt = C2V.getZExtValue()) |
Reid Spencer | a5c7472 | 2007-02-27 19:29:54 +0000 | [diff] [blame] | 676 | if (shiftAmt < C1V.getBitWidth()) |
Reid Spencer | ef2185b | 2007-03-01 19:31:12 +0000 | [diff] [blame] | 677 | return ConstantInt::get(C1V.ashr(shiftAmt)); |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 678 | else |
| 679 | return UndefValue::get(C1->getType()); // too big shift is undef |
| 680 | return const_cast<ConstantInt*>(CI1); // Zero shift is identity |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { |
| 684 | if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 685 | APFloat C1V = CFP1->getValueAPF(); |
| 686 | APFloat C2V = CFP2->getValueAPF(); |
| 687 | APFloat C3V = C1V; // copy for modification |
| 688 | bool isDouble = CFP1->getType()==Type::DoubleTy; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 689 | switch (Opcode) { |
| 690 | default: |
| 691 | break; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 692 | case Instruction::Add: |
| 693 | (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); |
| 694 | return ConstantFP::get(CFP1->getType(), C3V); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 695 | case Instruction::Sub: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 696 | (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); |
| 697 | return ConstantFP::get(CFP1->getType(), C3V); |
| 698 | case Instruction::Mul: |
| 699 | (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); |
| 700 | return ConstantFP::get(CFP1->getType(), C3V); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 701 | case Instruction::FDiv: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 702 | (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); |
| 703 | return ConstantFP::get(CFP1->getType(), C3V); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 704 | case Instruction::FRem: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 705 | if (C2V.isZero()) |
Reid Spencer | b8f1c16 | 2007-03-23 05:33:23 +0000 | [diff] [blame] | 706 | // IEEE 754, Section 7.1, #5 |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 707 | return ConstantFP::get(CFP1->getType(), isDouble ? |
| 708 | APFloat(std::numeric_limits<double>::quiet_NaN()) : |
| 709 | APFloat(std::numeric_limits<float>::quiet_NaN())); |
| 710 | (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven); |
| 711 | return ConstantFP::get(CFP1->getType(), C3V); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 714 | } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { |
| 715 | const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1); |
| 716 | const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2); |
Dan Gohman | 2cda939 | 2007-10-31 21:36:31 +0000 | [diff] [blame] | 717 | if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) && |
| 718 | (CP2 != NULL || isa<ConstantAggregateZero>(C2))) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 719 | switch (Opcode) { |
| 720 | default: |
| 721 | break; |
| 722 | case Instruction::Add: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 723 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAdd); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 724 | case Instruction::Sub: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 725 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSub); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 726 | case Instruction::Mul: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 727 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getMul); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 728 | case Instruction::UDiv: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 729 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getUDiv); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 730 | case Instruction::SDiv: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 731 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSDiv); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 732 | case Instruction::FDiv: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 733 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getFDiv); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 734 | case Instruction::URem: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 735 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getURem); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 736 | case Instruction::SRem: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 737 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSRem); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 738 | case Instruction::FRem: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 739 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getFRem); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 740 | case Instruction::And: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 741 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAnd); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 742 | case Instruction::Or: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 743 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getOr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 744 | case Instruction::Xor: |
Dan Gohman | d61de35 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 745 | return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getXor); |
Dan Gohman | 2cda939 | 2007-10-31 21:36:31 +0000 | [diff] [blame] | 746 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | // We don't know how to fold this |
| 751 | return 0; |
| 752 | } |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 753 | |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 754 | /// isZeroSizedType - This type is zero sized if its an array or structure of |
| 755 | /// zero sized types. The only leaf zero sized type is an empty structure. |
| 756 | static bool isMaybeZeroSizedType(const Type *Ty) { |
| 757 | if (isa<OpaqueType>(Ty)) return true; // Can't say. |
| 758 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 759 | |
| 760 | // If all of elements have zero size, this does too. |
| 761 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Chris Lattner | f4aa335 | 2005-01-28 23:17:27 +0000 | [diff] [blame] | 762 | if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 763 | return true; |
| 764 | |
| 765 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 766 | return isMaybeZeroSizedType(ATy->getElementType()); |
| 767 | } |
| 768 | return false; |
| 769 | } |
Chris Lattner | e971486 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 771 | /// IdxCompare - Compare the two constants as though they were getelementptr |
| 772 | /// indices. This allows coersion of the types to be the same thing. |
| 773 | /// |
| 774 | /// If the two constants are the "same" (after coersion), return 0. If the |
| 775 | /// first is less than the second, return -1, if the second is less than the |
| 776 | /// first, return 1. If the constants are not integral, return -2. |
| 777 | /// |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 778 | static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 779 | if (C1 == C2) return 0; |
| 780 | |
Reid Spencer | cc615c3 | 2006-12-31 21:43:30 +0000 | [diff] [blame] | 781 | // Ok, we found a different index. If they are not ConstantInt, we can't do |
| 782 | // anything with them. |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 783 | if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) |
| 784 | return -2; // don't know! |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 785 | |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 786 | // Ok, we have two differing integer indices. Sign extend them to be the same |
| 787 | // type. Long is always big enough, so we use it. |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 788 | if (C1->getType() != Type::Int64Ty) |
| 789 | C1 = ConstantExpr::getSExt(C1, Type::Int64Ty); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 790 | |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 791 | if (C2->getType() != Type::Int64Ty) |
Reid Spencer | cc615c3 | 2006-12-31 21:43:30 +0000 | [diff] [blame] | 792 | C2 = ConstantExpr::getSExt(C2, Type::Int64Ty); |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 793 | |
| 794 | if (C1 == C2) return 0; // They are equal |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 795 | |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 796 | // If the type being indexed over is really just a zero sized type, there is |
| 797 | // no pointer difference being made here. |
| 798 | if (isMaybeZeroSizedType(ElTy)) |
| 799 | return -2; // dunno. |
| 800 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 801 | // If they are really different, now that they are the same type, then we |
| 802 | // found a difference! |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 803 | if (cast<ConstantInt>(C1)->getSExtValue() < |
| 804 | cast<ConstantInt>(C2)->getSExtValue()) |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 805 | return -1; |
| 806 | else |
| 807 | return 1; |
| 808 | } |
| 809 | |
Chris Lattner | 898b2d5 | 2007-01-04 02:13:20 +0000 | [diff] [blame] | 810 | /// evaluateFCmpRelation - This function determines if there is anything we can |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 811 | /// decide about the two constants provided. This doesn't need to handle simple |
| 812 | /// things like ConstantFP comparisons, but should instead handle ConstantExprs. |
| 813 | /// If we can determine that the two constants have a particular relation to |
| 814 | /// each other, we should return the corresponding FCmpInst predicate, |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 815 | /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in |
| 816 | /// ConstantFoldCompareInstruction. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 817 | /// |
| 818 | /// To simplify this code we canonicalize the relation so that the first |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 819 | /// operand is always the most "complex" of the two. We consider ConstantFP |
| 820 | /// to be the simplest, and ConstantExprs to be the most complex. |
| 821 | static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1, |
| 822 | const Constant *V2) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 823 | assert(V1->getType() == V2->getType() && |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 824 | "Cannot compare values of different types!"); |
Dale Johannesen | 5927d8e | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 825 | |
| 826 | // No compile-time operations on this type yet. |
| 827 | if (V1->getType() == Type::PPC_FP128Ty) |
| 828 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 829 | |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 830 | // Handle degenerate case quickly |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 831 | if (V1 == V2) return FCmpInst::FCMP_OEQ; |
| 832 | |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 833 | if (!isa<ConstantExpr>(V1)) { |
| 834 | if (!isa<ConstantExpr>(V2)) { |
| 835 | // We distilled thisUse the standard constant folder for a few cases |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 836 | ConstantInt *R = 0; |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 837 | Constant *C1 = const_cast<Constant*>(V1); |
| 838 | Constant *C2 = const_cast<Constant*>(V2); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 839 | R = dyn_cast<ConstantInt>( |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 840 | ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2)); |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 841 | if (R && !R->isZero()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 842 | return FCmpInst::FCMP_OEQ; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 843 | R = dyn_cast<ConstantInt>( |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 844 | ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2)); |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 845 | if (R && !R->isZero()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 846 | return FCmpInst::FCMP_OLT; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 847 | R = dyn_cast<ConstantInt>( |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 848 | ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2)); |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 849 | if (R && !R->isZero()) |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 850 | return FCmpInst::FCMP_OGT; |
| 851 | |
| 852 | // Nothing more we can do |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 853 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 854 | } |
| 855 | |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 856 | // If the first operand is simple and second is ConstantExpr, swap operands. |
| 857 | FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1); |
| 858 | if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE) |
| 859 | return FCmpInst::getSwappedPredicate(SwappedRelation); |
| 860 | } else { |
| 861 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 862 | // constantexpr or a simple constant. |
| 863 | const ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
| 864 | switch (CE1->getOpcode()) { |
| 865 | case Instruction::FPTrunc: |
| 866 | case Instruction::FPExt: |
| 867 | case Instruction::UIToFP: |
| 868 | case Instruction::SIToFP: |
| 869 | // We might be able to do something with these but we don't right now. |
| 870 | break; |
| 871 | default: |
| 872 | break; |
| 873 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 874 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 875 | // There are MANY other foldings that we could perform here. They will |
| 876 | // probably be added on demand, as they seem needed. |
| 877 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 878 | } |
| 879 | |
| 880 | /// evaluateICmpRelation - This function determines if there is anything we can |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 881 | /// decide about the two constants provided. This doesn't need to handle simple |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 882 | /// things like integer comparisons, but should instead handle ConstantExprs |
Chris Lattner | 36c2451 | 2006-12-11 02:16:58 +0000 | [diff] [blame] | 883 | /// and GlobalValues. If we can determine that the two constants have a |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 884 | /// particular relation to each other, we should return the corresponding ICmp |
| 885 | /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE. |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 886 | /// |
| 887 | /// To simplify this code we canonicalize the relation so that the first |
| 888 | /// operand is always the most "complex" of the two. We consider simple |
| 889 | /// constants (like ConstantInt) to be the simplest, followed by |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 890 | /// GlobalValues, followed by ConstantExpr's (the most complex). |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 891 | /// |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 892 | static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1, |
| 893 | const Constant *V2, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 894 | bool isSigned) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 895 | assert(V1->getType() == V2->getType() && |
| 896 | "Cannot compare different types of values!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 897 | if (V1 == V2) return ICmpInst::ICMP_EQ; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 898 | |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 899 | if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) { |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 900 | if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) { |
| 901 | // We distilled this down to a simple case, use the standard constant |
| 902 | // folder. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 903 | ConstantInt *R = 0; |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 904 | Constant *C1 = const_cast<Constant*>(V1); |
| 905 | Constant *C2 = const_cast<Constant*>(V2); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 906 | ICmpInst::Predicate pred = ICmpInst::ICMP_EQ; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 907 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2)); |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 908 | if (R && !R->isZero()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 909 | return pred; |
| 910 | pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 911 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2)); |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 912 | if (R && !R->isZero()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 913 | return pred; |
| 914 | pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 915 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2)); |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 916 | if (R && !R->isZero()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 917 | return pred; |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 918 | |
| 919 | // If we couldn't figure it out, bail. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 920 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 923 | // If the first operand is simple, swap operands. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 924 | ICmpInst::Predicate SwappedRelation = |
| 925 | evaluateICmpRelation(V2, V1, isSigned); |
| 926 | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
| 927 | return ICmpInst::getSwappedPredicate(SwappedRelation); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 928 | |
Chris Lattner | 2c822cc | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 929 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) { |
Chris Lattner | b97e278 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 930 | if (isa<ConstantExpr>(V2)) { // Swap as necessary. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 931 | ICmpInst::Predicate SwappedRelation = |
| 932 | evaluateICmpRelation(V2, V1, isSigned); |
| 933 | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
| 934 | return ICmpInst::getSwappedPredicate(SwappedRelation); |
Chris Lattner | 2c822cc | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 935 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 936 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | b97e278 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 937 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 938 | |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 939 | // Now we know that the RHS is a GlobalValue or simple constant, |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 940 | // which (since the types must match) means that it's a ConstantPointerNull. |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 941 | if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | a0ef5ed | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 942 | // Don't try to decide equality of aliases. |
| 943 | if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2)) |
| 944 | if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage()) |
| 945 | return ICmpInst::ICMP_NE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 946 | } else { |
| 947 | assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); |
Chris Lattner | a0ef5ed | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 948 | // GlobalVals can never be null. Don't try to evaluate aliases. |
| 949 | if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 950 | return ICmpInst::ICMP_NE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 951 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 952 | } else { |
| 953 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 954 | // constantexpr, a CPR, or a simple constant. |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 955 | const ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
| 956 | const Constant *CE1Op0 = CE1->getOperand(0); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 957 | |
| 958 | switch (CE1->getOpcode()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 959 | case Instruction::Trunc: |
| 960 | case Instruction::FPTrunc: |
| 961 | case Instruction::FPExt: |
| 962 | case Instruction::FPToUI: |
| 963 | case Instruction::FPToSI: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 964 | break; // We can't evaluate floating point casts or truncations. |
| 965 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 966 | case Instruction::UIToFP: |
| 967 | case Instruction::SIToFP: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 968 | case Instruction::BitCast: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 969 | case Instruction::ZExt: |
| 970 | case Instruction::SExt: |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 971 | // If the cast is not actually changing bits, and the second operand is a |
| 972 | // null pointer, do the comparison with the pre-casted value. |
| 973 | if (V2->isNullValue() && |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 974 | (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) { |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 975 | bool sgnd = isSigned; |
| 976 | if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; |
| 977 | if (CE1->getOpcode() == Instruction::SExt) isSigned = true; |
| 978 | return evaluateICmpRelation(CE1Op0, |
| 979 | Constant::getNullValue(CE1Op0->getType()), |
| 980 | sgnd); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 981 | } |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 982 | |
| 983 | // If the dest type is a pointer type, and the RHS is a constantexpr cast |
| 984 | // from the same type as the src of the LHS, evaluate the inputs. This is |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 985 | // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)", |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 986 | // which happens a lot in compilers with tagged integers. |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 987 | if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 988 | if (CE2->isCast() && isa<PointerType>(CE1->getType()) && |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 989 | CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() && |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 990 | CE1->getOperand(0)->getType()->isInteger()) { |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 991 | bool sgnd = isSigned; |
| 992 | if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; |
| 993 | if (CE1->getOpcode() == Instruction::SExt) isSigned = true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 994 | return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0), |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 995 | sgnd); |
Chris Lattner | 2f690c8 | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 996 | } |
Chris Lattner | a0ae819 | 2004-04-11 01:29:30 +0000 | [diff] [blame] | 997 | break; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 998 | |
| 999 | case Instruction::GetElementPtr: |
| 1000 | // Ok, since this is a getelementptr, we know that the constant has a |
| 1001 | // pointer type. Check the various cases. |
| 1002 | if (isa<ConstantPointerNull>(V2)) { |
| 1003 | // If we are comparing a GEP to a null pointer, check to see if the base |
| 1004 | // of the GEP equals the null pointer. |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1005 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1006 | if (GV->hasExternalWeakLinkage()) |
| 1007 | // Weak linkage GVals could be zero or not. We're comparing that |
| 1008 | // to null pointer so its greater-or-equal |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1009 | return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1010 | else |
| 1011 | // If its not weak linkage, the GVal must have a non-zero address |
| 1012 | // so the result is greater-than |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1013 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1014 | } else if (isa<ConstantPointerNull>(CE1Op0)) { |
| 1015 | // If we are indexing from a null pointer, check to see if we have any |
| 1016 | // non-zero indices. |
| 1017 | for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) |
| 1018 | if (!CE1->getOperand(i)->isNullValue()) |
| 1019 | // Offsetting from null, must not be equal. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1020 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1021 | // Only zero indexes from null, must still be zero. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1022 | return ICmpInst::ICMP_EQ; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1023 | } |
| 1024 | // Otherwise, we can't really say if the first operand is null or not. |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1025 | } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1026 | if (isa<ConstantPointerNull>(CE1Op0)) { |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1027 | if (CPR2->hasExternalWeakLinkage()) |
| 1028 | // Weak linkage GVals could be zero or not. We're comparing it to |
| 1029 | // a null pointer, so its less-or-equal |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1030 | return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
Reid Spencer | 87d5f6c | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1031 | else |
| 1032 | // If its not weak linkage, the GVal must have a non-zero address |
| 1033 | // so the result is less-than |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1034 | return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1035 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1036 | if (CPR1 == CPR2) { |
| 1037 | // If this is a getelementptr of the same global, then it must be |
| 1038 | // different. Because the types must match, the getelementptr could |
| 1039 | // only have at most one index, and because we fold getelementptr's |
| 1040 | // with a single zero index, it must be nonzero. |
| 1041 | assert(CE1->getNumOperands() == 2 && |
| 1042 | !CE1->getOperand(1)->isNullValue() && |
| 1043 | "Suprising getelementptr!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1044 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1045 | } else { |
| 1046 | // If they are different globals, we don't know what the value is, |
| 1047 | // but they can't be equal. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1048 | return ICmpInst::ICMP_NE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } else { |
| 1052 | const ConstantExpr *CE2 = cast<ConstantExpr>(V2); |
| 1053 | const Constant *CE2Op0 = CE2->getOperand(0); |
| 1054 | |
| 1055 | // There are MANY other foldings that we could perform here. They will |
| 1056 | // probably be added on demand, as they seem needed. |
| 1057 | switch (CE2->getOpcode()) { |
| 1058 | default: break; |
| 1059 | case Instruction::GetElementPtr: |
| 1060 | // By far the most common case to handle is when the base pointers are |
| 1061 | // obviously to the same or different globals. |
Reid Spencer | 7970396 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1062 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1063 | if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1064 | return ICmpInst::ICMP_NE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1065 | // Ok, we know that both getelementptr instructions are based on the |
| 1066 | // same global. From this, we can precisely determine the relative |
| 1067 | // ordering of the resultant pointers. |
| 1068 | unsigned i = 1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1070 | // Compare all of the operands the GEP's have in common. |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1071 | gep_type_iterator GTI = gep_type_begin(CE1); |
| 1072 | for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); |
| 1073 | ++i, ++GTI) |
| 1074 | switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i), |
| 1075 | GTI.getIndexedType())) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1076 | case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT; |
| 1077 | case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT; |
| 1078 | case -2: return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | // Ok, we ran out of things they have in common. If any leftovers |
| 1082 | // are non-zero then we have a difference, otherwise we are equal. |
| 1083 | for (; i < CE1->getNumOperands(); ++i) |
| 1084 | if (!CE1->getOperand(i)->isNullValue()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1085 | if (isa<ConstantInt>(CE1->getOperand(i))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1086 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1087 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1088 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1090 | for (; i < CE2->getNumOperands(); ++i) |
| 1091 | if (!CE2->getOperand(i)->isNullValue()) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1092 | if (isa<ConstantInt>(CE2->getOperand(i))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1093 | return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Chris Lattner | ce04a6d | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1094 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1095 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
| 1096 | return ICmpInst::ICMP_EQ; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1100 | default: |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1105 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1108 | Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, |
| 1109 | const Constant *C1, |
| 1110 | const Constant *C2) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1111 | |
| 1112 | // Handle some degenerate cases first |
| 1113 | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1114 | return UndefValue::get(Type::Int1Ty); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1115 | |
Dale Johannesen | 5927d8e | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1116 | // No compile-time operations on this type yet. |
| 1117 | if (C1->getType() == Type::PPC_FP128Ty) |
| 1118 | return 0; |
| 1119 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1120 | // icmp eq/ne(null,GV) -> false/true |
| 1121 | if (C1->isNullValue()) { |
| 1122 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2)) |
Duncan Sands | 7801644 | 2007-09-19 10:16:17 +0000 | [diff] [blame] | 1123 | // Don't try to evaluate aliases. External weak GV can be null. |
| 1124 | if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1125 | if (pred == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1126 | return ConstantInt::getFalse(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1127 | else if (pred == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1128 | return ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1129 | // icmp eq/ne(GV,null) -> false/true |
| 1130 | } else if (C2->isNullValue()) { |
| 1131 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) |
Duncan Sands | 7801644 | 2007-09-19 10:16:17 +0000 | [diff] [blame] | 1132 | // Don't try to evaluate aliases. External weak GV can be null. |
| 1133 | if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1134 | if (pred == ICmpInst::ICMP_EQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1135 | return ConstantInt::getFalse(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1136 | else if (pred == ICmpInst::ICMP_NE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1137 | return ConstantInt::getTrue(); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Chris Lattner | d333d90 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 1140 | if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) { |
Reid Spencer | 9472c37 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 1141 | APInt V1 = cast<ConstantInt>(C1)->getValue(); |
| 1142 | APInt V2 = cast<ConstantInt>(C2)->getValue(); |
| 1143 | switch (pred) { |
| 1144 | default: assert(0 && "Invalid ICmp Predicate"); return 0; |
| 1145 | case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2); |
| 1146 | case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2); |
| 1147 | case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1.slt(V2)); |
| 1148 | case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1.sgt(V2)); |
| 1149 | case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1.sle(V2)); |
| 1150 | case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1.sge(V2)); |
| 1151 | case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1.ult(V2)); |
| 1152 | case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1.ugt(V2)); |
| 1153 | case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1.ule(V2)); |
| 1154 | case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1.uge(V2)); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1155 | } |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1156 | } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) { |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1157 | APFloat C1V = cast<ConstantFP>(C1)->getValueAPF(); |
| 1158 | APFloat C2V = cast<ConstantFP>(C2)->getValueAPF(); |
| 1159 | APFloat::cmpResult R = C1V.compare(C2V); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1160 | switch (pred) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1161 | default: assert(0 && "Invalid FCmp Predicate"); return 0; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1162 | case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse(); |
| 1163 | case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1164 | case FCmpInst::FCMP_UNO: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1165 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered); |
Reid Spencer | 5305478 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1166 | case FCmpInst::FCMP_ORD: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1167 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpUnordered); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1168 | case FCmpInst::FCMP_UEQ: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1169 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered || |
| 1170 | R==APFloat::cmpEqual); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1171 | case FCmpInst::FCMP_OEQ: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1172 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpEqual); |
Reid Spencer | 5305478 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1173 | case FCmpInst::FCMP_UNE: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1174 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpEqual); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1175 | case FCmpInst::FCMP_ONE: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1176 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan || |
| 1177 | R==APFloat::cmpGreaterThan); |
Reid Spencer | 5305478 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1178 | case FCmpInst::FCMP_ULT: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1179 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered || |
| 1180 | R==APFloat::cmpLessThan); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1181 | case FCmpInst::FCMP_OLT: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1182 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1183 | case FCmpInst::FCMP_UGT: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1184 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered || |
| 1185 | R==APFloat::cmpGreaterThan); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1186 | case FCmpInst::FCMP_OGT: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1187 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan); |
Reid Spencer | 5305478 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1188 | case FCmpInst::FCMP_ULE: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1189 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpGreaterThan); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1190 | case FCmpInst::FCMP_OLE: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1191 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan || |
| 1192 | R==APFloat::cmpEqual); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1193 | case FCmpInst::FCMP_UGE: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1194 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpLessThan); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1195 | case FCmpInst::FCMP_OGE: |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1196 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan || |
| 1197 | R==APFloat::cmpEqual); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1198 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1199 | } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) { |
| 1200 | if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) { |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1201 | if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1202 | for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) { |
| 1203 | Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, |
| 1204 | const_cast<Constant*>(CP1->getOperand(i)), |
| 1205 | const_cast<Constant*>(CP2->getOperand(i))); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1206 | if (ConstantInt *CB = dyn_cast<ConstantInt>(C)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1207 | return CB; |
| 1208 | } |
| 1209 | // Otherwise, could not decide from any element pairs. |
| 1210 | return 0; |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1211 | } else if (pred == ICmpInst::ICMP_EQ) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1212 | for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) { |
| 1213 | Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ, |
| 1214 | const_cast<Constant*>(CP1->getOperand(i)), |
| 1215 | const_cast<Constant*>(CP2->getOperand(i))); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1216 | if (ConstantInt *CB = dyn_cast<ConstantInt>(C)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1217 | return CB; |
| 1218 | } |
| 1219 | // Otherwise, could not decide from any element pairs. |
| 1220 | return 0; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1224 | |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1225 | if (C1->getType()->isFloatingPoint()) { |
| 1226 | switch (evaluateFCmpRelation(C1, C2)) { |
| 1227 | default: assert(0 && "Unknown relation!"); |
| 1228 | case FCmpInst::FCMP_UNO: |
| 1229 | case FCmpInst::FCMP_ORD: |
| 1230 | case FCmpInst::FCMP_UEQ: |
| 1231 | case FCmpInst::FCMP_UNE: |
| 1232 | case FCmpInst::FCMP_ULT: |
| 1233 | case FCmpInst::FCMP_UGT: |
| 1234 | case FCmpInst::FCMP_ULE: |
| 1235 | case FCmpInst::FCMP_UGE: |
| 1236 | case FCmpInst::FCMP_TRUE: |
| 1237 | case FCmpInst::FCMP_FALSE: |
| 1238 | case FCmpInst::BAD_FCMP_PREDICATE: |
| 1239 | break; // Couldn't determine anything about these constants. |
| 1240 | case FCmpInst::FCMP_OEQ: // We know that C1 == C2 |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1241 | return ConstantInt::get(Type::Int1Ty, |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1242 | pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ || |
| 1243 | pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE || |
| 1244 | pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); |
| 1245 | case FCmpInst::FCMP_OLT: // We know that C1 < C2 |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1246 | return ConstantInt::get(Type::Int1Ty, |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1247 | pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || |
| 1248 | pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT || |
| 1249 | pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE); |
| 1250 | case FCmpInst::FCMP_OGT: // We know that C1 > C2 |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1251 | return ConstantInt::get(Type::Int1Ty, |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1252 | pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || |
| 1253 | pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT || |
| 1254 | pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); |
| 1255 | case FCmpInst::FCMP_OLE: // We know that C1 <= C2 |
| 1256 | // We can only partially decide this relation. |
| 1257 | if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1258 | return ConstantInt::getFalse(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1259 | if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1260 | return ConstantInt::getTrue(); |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1261 | break; |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1262 | case FCmpInst::FCMP_OGE: // We known that C1 >= C2 |
| 1263 | // We can only partially decide this relation. |
| 1264 | if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1265 | return ConstantInt::getFalse(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1266 | if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1267 | return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | case ICmpInst::ICMP_NE: // We know that C1 != C2 |
| 1270 | // We can only partially decide this relation. |
| 1271 | if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1272 | return ConstantInt::getFalse(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1273 | if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1274 | return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | } |
| 1277 | } else { |
| 1278 | // Evaluate the relation between the two constants, per the predicate. |
| 1279 | switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) { |
| 1280 | default: assert(0 && "Unknown relational!"); |
| 1281 | case ICmpInst::BAD_ICMP_PREDICATE: |
| 1282 | break; // Couldn't determine anything about these constants. |
| 1283 | case ICmpInst::ICMP_EQ: // We know the constants are equal! |
| 1284 | // If we know the constants are equal, we can decide the result of this |
| 1285 | // computation precisely. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1286 | return ConstantInt::get(Type::Int1Ty, |
| 1287 | pred == ICmpInst::ICMP_EQ || |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1288 | pred == ICmpInst::ICMP_ULE || |
| 1289 | pred == ICmpInst::ICMP_SLE || |
| 1290 | pred == ICmpInst::ICMP_UGE || |
| 1291 | pred == ICmpInst::ICMP_SGE); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1292 | case ICmpInst::ICMP_ULT: |
| 1293 | // If we know that C1 < C2, we can decide the result of this computation |
| 1294 | // precisely. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1295 | return ConstantInt::get(Type::Int1Ty, |
| 1296 | pred == ICmpInst::ICMP_ULT || |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1297 | pred == ICmpInst::ICMP_NE || |
| 1298 | pred == ICmpInst::ICMP_ULE); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1299 | case ICmpInst::ICMP_SLT: |
| 1300 | // If we know that C1 < C2, we can decide the result of this computation |
| 1301 | // precisely. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1302 | return ConstantInt::get(Type::Int1Ty, |
| 1303 | pred == ICmpInst::ICMP_SLT || |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1304 | pred == ICmpInst::ICMP_NE || |
| 1305 | pred == ICmpInst::ICMP_SLE); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1306 | case ICmpInst::ICMP_UGT: |
| 1307 | // If we know that C1 > C2, we can decide the result of this computation |
| 1308 | // precisely. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1309 | return ConstantInt::get(Type::Int1Ty, |
| 1310 | pred == ICmpInst::ICMP_UGT || |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1311 | pred == ICmpInst::ICMP_NE || |
| 1312 | pred == ICmpInst::ICMP_UGE); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1313 | case ICmpInst::ICMP_SGT: |
| 1314 | // If we know that C1 > C2, we can decide the result of this computation |
| 1315 | // precisely. |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1316 | return ConstantInt::get(Type::Int1Ty, |
| 1317 | pred == ICmpInst::ICMP_SGT || |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1318 | pred == ICmpInst::ICMP_NE || |
| 1319 | pred == ICmpInst::ICMP_SGE); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1320 | case ICmpInst::ICMP_ULE: |
| 1321 | // If we know that C1 <= C2, we can only partially decide this relation. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1322 | if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse(); |
| 1323 | if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | case ICmpInst::ICMP_SLE: |
| 1326 | // If we know that C1 <= C2, we can only partially decide this relation. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1327 | if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse(); |
| 1328 | if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | |
| 1331 | case ICmpInst::ICMP_UGE: |
| 1332 | // If we know that C1 >= C2, we can only partially decide this relation. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1333 | if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse(); |
| 1334 | if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1335 | break; |
| 1336 | case ICmpInst::ICMP_SGE: |
| 1337 | // If we know that C1 >= C2, we can only partially decide this relation. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1338 | if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse(); |
| 1339 | if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1340 | break; |
| 1341 | |
| 1342 | case ICmpInst::ICMP_NE: |
| 1343 | // If we know that C1 != C2, we can only partially decide this relation. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1344 | if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse(); |
| 1345 | if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue(); |
Reid Spencer | b913bba | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | |
| 1349 | if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) { |
| 1350 | // If C2 is a constant expr and C1 isn't, flop them around and fold the |
| 1351 | // other way if possible. |
| 1352 | switch (pred) { |
| 1353 | case ICmpInst::ICMP_EQ: |
| 1354 | case ICmpInst::ICMP_NE: |
| 1355 | // No change of predicate required. |
| 1356 | return ConstantFoldCompareInstruction(pred, C2, C1); |
| 1357 | |
| 1358 | case ICmpInst::ICMP_ULT: |
| 1359 | case ICmpInst::ICMP_SLT: |
| 1360 | case ICmpInst::ICMP_UGT: |
| 1361 | case ICmpInst::ICMP_SGT: |
| 1362 | case ICmpInst::ICMP_ULE: |
| 1363 | case ICmpInst::ICMP_SLE: |
| 1364 | case ICmpInst::ICMP_UGE: |
| 1365 | case ICmpInst::ICMP_SGE: |
| 1366 | // Change the predicate as necessary to swap the operands. |
| 1367 | pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); |
| 1368 | return ConstantFoldCompareInstruction(pred, C2, C1); |
| 1369 | |
| 1370 | default: // These predicates cannot be flopped around. |
| 1371 | break; |
| 1372 | } |
Chris Lattner | 504e8fb | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | return 0; |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | Constant *llvm::ConstantFoldGetElementPtr(const Constant *C, |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1379 | Constant* const *Idxs, |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1380 | unsigned NumIdx) { |
| 1381 | if (NumIdx == 0 || |
| 1382 | (NumIdx == 1 && Idxs[0]->isNullValue())) |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1383 | return const_cast<Constant*>(C); |
| 1384 | |
Chris Lattner | cfbf9fa | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1385 | if (isa<UndefValue>(C)) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1386 | const PointerType *Ptr = cast<PointerType>(C->getType()); |
| 1387 | const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1388 | (Value **)Idxs, |
| 1389 | (Value **)Idxs+NumIdx, |
Chris Lattner | cfbf9fa | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1390 | true); |
| 1391 | assert(Ty != 0 && "Invalid indices for GEP!"); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1392 | return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); |
Chris Lattner | cfbf9fa | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1395 | Constant *Idx0 = Idxs[0]; |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1396 | if (C->isNullValue()) { |
| 1397 | bool isNull = true; |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1398 | for (unsigned i = 0, e = NumIdx; i != e; ++i) |
| 1399 | if (!Idxs[i]->isNullValue()) { |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1400 | isNull = false; |
| 1401 | break; |
| 1402 | } |
| 1403 | if (isNull) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1404 | const PointerType *Ptr = cast<PointerType>(C->getType()); |
| 1405 | const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1406 | (Value**)Idxs, |
| 1407 | (Value**)Idxs+NumIdx, |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1408 | true); |
| 1409 | assert(Ty != 0 && "Invalid indices for GEP!"); |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1410 | return |
| 1411 | ConstantPointerNull::get(PointerType::get(Ty,Ptr->getAddressSpace())); |
Chris Lattner | 2e9bb1a | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1414 | |
| 1415 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
| 1416 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 1417 | // is a getelementptr instruction, combine the indices of the two |
| 1418 | // getelementptr instructions into a single instruction. |
| 1419 | // |
| 1420 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1421 | const Type *LastTy = 0; |
| 1422 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 1423 | I != E; ++I) |
| 1424 | LastTy = *I; |
| 1425 | |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1426 | if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) { |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1427 | SmallVector<Value*, 16> NewIndices; |
| 1428 | NewIndices.reserve(NumIdx + CE->getNumOperands()); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1429 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1430 | NewIndices.push_back(CE->getOperand(i)); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Add the last index of the source with the first index of the new GEP. |
| 1433 | // Make sure to handle the case when they are actually different types. |
| 1434 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
Chris Lattner | 7fa6e66 | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1435 | // Otherwise it must be an array. |
| 1436 | if (!Idx0->isNullValue()) { |
Chris Lattner | d340867 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1437 | const Type *IdxTy = Combined->getType(); |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1438 | if (IdxTy != Idx0->getType()) { |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1439 | Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty); |
Reid Spencer | 57c6993 | 2006-12-05 03:30:09 +0000 | [diff] [blame] | 1440 | Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1441 | Type::Int64Ty); |
Reid Spencer | 575d95c | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1442 | Combined = ConstantExpr::get(Instruction::Add, C1, C2); |
| 1443 | } else { |
| 1444 | Combined = |
| 1445 | ConstantExpr::get(Instruction::Add, Idx0, Combined); |
| 1446 | } |
Chris Lattner | d340867 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1447 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1449 | NewIndices.push_back(Combined); |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1450 | NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx); |
| 1451 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0], |
| 1452 | NewIndices.size()); |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | // Implement folding of: |
| 1457 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 1458 | // long 0, long 0) |
| 1459 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 1460 | // |
Chris Lattner | fe9d82a | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1461 | if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1462 | if (const PointerType *SPT = |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1463 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 1464 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 1465 | if (const ArrayType *CAT = |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1466 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1467 | if (CAT->getElementType() == SAT->getElementType()) |
| 1468 | return ConstantExpr::getGetElementPtr( |
Chris Lattner | 2b9a5da | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1469 | (Constant*)CE->getOperand(0), Idxs, NumIdx); |
Chris Lattner | fe9d82a | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1) |
| 1473 | // Into: inttoptr (i64 0 to i8*) |
| 1474 | // This happens with pointers to member functions in C++. |
| 1475 | if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 && |
| 1476 | isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) && |
| 1477 | cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) { |
| 1478 | Constant *Base = CE->getOperand(0); |
| 1479 | Constant *Offset = Idxs[0]; |
| 1480 | |
| 1481 | // Convert the smaller integer to the larger type. |
| 1482 | if (Offset->getType()->getPrimitiveSizeInBits() < |
| 1483 | Base->getType()->getPrimitiveSizeInBits()) |
| 1484 | Offset = ConstantExpr::getSExt(Offset, Base->getType()); |
| 1485 | else if (Base->getType()->getPrimitiveSizeInBits() < |
| 1486 | Offset->getType()->getPrimitiveSizeInBits()) |
| 1487 | Base = ConstantExpr::getZExt(Base, Base->getType()); |
| 1488 | |
| 1489 | Base = ConstantExpr::getAdd(Base, Offset); |
| 1490 | return ConstantExpr::getIntToPtr(Base, CE->getType()); |
| 1491 | } |
Chris Lattner | eab20b5 | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1492 | } |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |