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