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