Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 1 | //===- ConstantFold.cpp - LLVM constant folder ----------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 10 | // This file implements folding of constants for LLVM. This implements the |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 11 | // (internal) ConstantFold.h interface, which is used by the |
Chris Lattner | 5a945e3 | 2004-01-12 21:13:12 +0000 | [diff] [blame] | 12 | // ConstantExpr::get* methods to automatically fold constants when possible. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | // |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 14 | // The current constant folding implementation is implemented in two pieces: the |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 15 | // pieces that don't need TargetData, and the pieces that do. This is to avoid |
| 16 | // a dependence in VMCore on Target. |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 17 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | 33e93b8 | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 20 | #include "ConstantFold.h" |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | a9eddae | 2004-02-22 06:25:38 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Chris Lattner | 52fe869 | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 25 | #include "llvm/GlobalAlias.h" |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 26 | #include "llvm/GlobalVariable.h" |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 27 | #include "llvm/LLVMContext.h" |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 057083f | 2006-10-13 17:22:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 32 | #include "llvm/Support/ManagedStatic.h" |
| 33 | #include "llvm/Support/MathExtras.h" |
Jeff Cohen | 4e3aede | 2005-05-03 03:13:01 +0000 | [diff] [blame] | 34 | #include <limits> |
Chris Lattner | 9d9cbcf | 2003-11-17 19:05:17 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Chris Lattner | 61607ee | 2001-09-09 21:01:20 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // ConstantFold*Instruction Implementations |
| 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 5c6399e | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 41 | /// BitCastConstantVector - Convert the specified ConstantVector node to the |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 42 | /// specified vector type. At this point, we know that the elements of the |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 43 | /// input vector constant are all simple integer or FP values. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 44 | static Constant *BitCastConstantVector(LLVMContext &Context, ConstantVector *CV, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 45 | const VectorType *DstTy) { |
Chris Lattner | 5c6399e | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 46 | // If this cast changes element count then we can't handle it here: |
| 47 | // doing so requires endianness information. This should be handled by |
| 48 | // Analysis/ConstantFolding.cpp |
| 49 | unsigned NumElts = DstTy->getNumElements(); |
| 50 | if (NumElts != CV->getNumOperands()) |
| 51 | return 0; |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 5c6399e | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 53 | // Check to verify that all elements of the input are simple. |
| 54 | for (unsigned i = 0; i != NumElts; ++i) { |
| 55 | if (!isa<ConstantInt>(CV->getOperand(i)) && |
| 56 | !isa<ConstantFP>(CV->getOperand(i))) |
| 57 | return 0; |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 5c6399e | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 59 | |
| 60 | // Bitcast each element now. |
| 61 | std::vector<Constant*> Result; |
| 62 | const Type *DstEltTy = DstTy->getElementType(); |
| 63 | for (unsigned i = 0; i != NumElts; ++i) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 64 | Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i), |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 65 | DstEltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 66 | return ConstantVector::get(Result); |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 69 | /// This function determines which opcode to use to fold two constant cast |
| 70 | /// expressions together. It uses CastInst::isEliminableCastPair to determine |
| 71 | /// the opcode. Consequently its just a wrapper around that function. |
Reid Spencer | 05d55b3 | 2007-08-05 19:27:01 +0000 | [diff] [blame] | 72 | /// @brief Determine if it is valid to fold a cast of a cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 73 | static unsigned |
| 74 | foldConstantCastPair( |
| 75 | unsigned opc, ///< opcode of the second cast constant expression |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 76 | ConstantExpr *Op, ///< the first cast constant expression |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 77 | const Type *DstTy ///< desintation type of the first cast |
| 78 | ) { |
| 79 | assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); |
| 80 | assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); |
| 81 | assert(CastInst::isCast(opc) && "Invalid cast opcode"); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 82 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 83 | // The the types and opcodes for the two Cast constant expressions |
| 84 | const Type *SrcTy = Op->getOperand(0)->getType(); |
| 85 | const Type *MidTy = Op->getType(); |
| 86 | Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); |
| 87 | Instruction::CastOps secondOp = Instruction::CastOps(opc); |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 88 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 89 | // Let CastInst::isEliminableCastPair do the heavy lifting. |
| 90 | return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 91 | Type::getInt64Ty(DstTy->getContext())); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 94 | static Constant *FoldBitCast(LLVMContext &Context, |
| 95 | Constant *V, const Type *DestTy) { |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 96 | const Type *SrcTy = V->getType(); |
| 97 | if (SrcTy == DestTy) |
| 98 | return V; // no-op cast |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 99 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 100 | // Check to see if we are casting a pointer to an aggregate to a pointer to |
| 101 | // the first element. If so, return the appropriate GEP instruction. |
| 102 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) |
Nate Begeman | f2b0b0e | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 103 | if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) |
| 104 | if (PTy->getAddressSpace() == DPTy->getAddressSpace()) { |
| 105 | SmallVector<Value*, 8> IdxList; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 106 | Value *Zero = Constant::getNullValue(Type::getInt32Ty(Context)); |
Dan Gohman | 7673374 | 2009-08-12 00:32:55 +0000 | [diff] [blame] | 107 | IdxList.push_back(Zero); |
Nate Begeman | f2b0b0e | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 108 | const Type *ElTy = PTy->getElementType(); |
| 109 | while (ElTy != DPTy->getElementType()) { |
| 110 | if (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 111 | if (STy->getNumElements() == 0) break; |
| 112 | ElTy = STy->getElementType(0); |
Dan Gohman | 7673374 | 2009-08-12 00:32:55 +0000 | [diff] [blame] | 113 | IdxList.push_back(Zero); |
Nate Begeman | f2b0b0e | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 114 | } else if (const SequentialType *STy = |
| 115 | dyn_cast<SequentialType>(ElTy)) { |
| 116 | if (isa<PointerType>(ElTy)) break; // Can't index into pointers! |
| 117 | ElTy = STy->getElementType(); |
Dan Gohman | 7673374 | 2009-08-12 00:32:55 +0000 | [diff] [blame] | 118 | IdxList.push_back(Zero); |
Nate Begeman | f2b0b0e | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 119 | } else { |
| 120 | break; |
| 121 | } |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 122 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 123 | |
Nate Begeman | f2b0b0e | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 124 | if (ElTy == DPTy->getElementType()) |
Dan Gohman | e4ca02d | 2009-09-03 23:34:49 +0000 | [diff] [blame] | 125 | // This GEP is inbounds because all indices are zero. |
| 126 | return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0], |
| 127 | IdxList.size()); |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 128 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 129 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 130 | // Handle casts from one vector constant to another. We know that the src |
| 131 | // and dest type have the same size (otherwise its an illegal cast). |
| 132 | if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 133 | if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) { |
| 134 | assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() && |
| 135 | "Not cast between same sized vectors!"); |
Devang Patel | d26344d | 2008-11-03 23:20:04 +0000 | [diff] [blame] | 136 | SrcTy = NULL; |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 137 | // First, check for null. Undef is already handled. |
| 138 | if (isa<ConstantAggregateZero>(V)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 139 | return Constant::getNullValue(DestTy); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 5c6399e | 2007-12-11 06:07:39 +0000 | [diff] [blame] | 141 | if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 142 | return BitCastConstantVector(Context, CV, DestPTy); |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | 1baace0 | 2008-10-16 05:26:51 +0000 | [diff] [blame] | 144 | |
| 145 | // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts |
| 146 | // This allows for other simplifications (although some of them |
| 147 | // can only be handled by Analysis/ConstantFolding.cpp). |
| 148 | if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 149 | return ConstantExpr::getBitCast( |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 150 | ConstantVector::get(&V, 1), DestPTy); |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 151 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 152 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 153 | // Finally, implement bitcast folding now. The code below doesn't handle |
| 154 | // bitcast right. |
| 155 | if (isa<ConstantPointerNull>(V)) // ptr->ptr cast. |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 156 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 157 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 158 | // Handle integral constant input. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 159 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 160 | if (DestTy->isInteger()) |
| 161 | // Integral -> Integral. This is a no-op because the bit widths must |
| 162 | // be the same. Consequently, we just fold to V. |
| 163 | return V; |
Duncan Sands | 1ea1173 | 2009-02-04 10:17:14 +0000 | [diff] [blame] | 164 | |
| 165 | if (DestTy->isFloatingPoint()) |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 166 | return ConstantFP::get(Context, APFloat(CI->getValue(), |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 167 | DestTy != Type::getPPC_FP128Ty(Context))); |
Duncan Sands | 1ea1173 | 2009-02-04 10:17:14 +0000 | [diff] [blame] | 168 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 169 | // Otherwise, can't fold this (vector?) |
| 170 | return 0; |
| 171 | } |
Duncan Sands | e7d5479 | 2009-02-04 11:17:06 +0000 | [diff] [blame] | 172 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 173 | // Handle ConstantFP input. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 174 | if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 175 | // FP -> Integral. |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 176 | return ConstantInt::get(Context, FP->getValueAPF().bitcastToAPInt()); |
Duncan Sands | e7d5479 | 2009-02-04 11:17:06 +0000 | [diff] [blame] | 177 | |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 182 | /// ExtractConstantBytes - V is an integer constant which only has a subset of |
| 183 | /// its bytes used. The bytes used are indicated by ByteStart (which is the |
| 184 | /// first byte used, counting from the least significant byte) and ByteSize, |
| 185 | /// which is the number of bytes used. |
| 186 | /// |
| 187 | /// This function analyzes the specified constant to see if the specified byte |
| 188 | /// range can be returned as a simplified constant. If so, the constant is |
| 189 | /// returned, otherwise null is returned. |
| 190 | /// |
| 191 | static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, |
| 192 | unsigned ByteSize) { |
| 193 | assert(isa<IntegerType>(C->getType()) && |
| 194 | (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 && |
| 195 | "Non-byte sized integer input"); |
| 196 | unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8; |
| 197 | assert(ByteSize && "Must be accessing some piece"); |
| 198 | assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input"); |
| 199 | assert(ByteSize != CSize && "Should not extract everything"); |
| 200 | |
| 201 | // Constant Integers are simple. |
| 202 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
| 203 | APInt V = CI->getValue(); |
| 204 | if (ByteStart) |
| 205 | V = V.lshr(ByteStart*8); |
| 206 | V.trunc(ByteSize*8); |
| 207 | return ConstantInt::get(CI->getContext(), V); |
| 208 | } |
| 209 | |
| 210 | // In the input is a constant expr, we might be able to recursively simplify. |
| 211 | // If not, we definitely can't do anything. |
| 212 | ConstantExpr *CE = dyn_cast<ConstantExpr>(C); |
| 213 | if (CE == 0) return 0; |
| 214 | |
| 215 | switch (CE->getOpcode()) { |
| 216 | default: return 0; |
| 217 | case Instruction::Or: { |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 218 | Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 219 | if (RHS == 0) |
| 220 | return 0; |
| 221 | |
| 222 | // X | -1 -> -1. |
| 223 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) |
| 224 | if (RHSC->isAllOnesValue()) |
| 225 | return RHSC; |
| 226 | |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 227 | Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 228 | if (LHS == 0) |
| 229 | return 0; |
| 230 | return ConstantExpr::getOr(LHS, RHS); |
| 231 | } |
| 232 | case Instruction::And: { |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 233 | Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 234 | if (RHS == 0) |
| 235 | return 0; |
| 236 | |
| 237 | // X & 0 -> 0. |
| 238 | if (RHS->isNullValue()) |
| 239 | return RHS; |
| 240 | |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 241 | Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 242 | if (LHS == 0) |
| 243 | return 0; |
| 244 | return ConstantExpr::getAnd(LHS, RHS); |
| 245 | } |
| 246 | case Instruction::LShr: { |
| 247 | ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); |
| 248 | if (Amt == 0) |
| 249 | return 0; |
| 250 | unsigned ShAmt = Amt->getZExtValue(); |
| 251 | // Cannot analyze non-byte shifts. |
| 252 | if ((ShAmt & 7) != 0) |
| 253 | return 0; |
| 254 | ShAmt >>= 3; |
| 255 | |
| 256 | // If the extract is known to be all zeros, return zero. |
| 257 | if (ByteStart >= CSize-ShAmt) |
| 258 | return Constant::getNullValue(IntegerType::get(CE->getContext(), |
| 259 | ByteSize*8)); |
| 260 | // If the extract is known to be fully in the input, extract it. |
| 261 | if (ByteStart+ByteSize+ShAmt <= CSize) |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 262 | return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 263 | |
| 264 | // TODO: Handle the 'partially zero' case. |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | case Instruction::Shl: { |
| 269 | ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); |
| 270 | if (Amt == 0) |
| 271 | return 0; |
| 272 | unsigned ShAmt = Amt->getZExtValue(); |
| 273 | // Cannot analyze non-byte shifts. |
| 274 | if ((ShAmt & 7) != 0) |
| 275 | return 0; |
| 276 | ShAmt >>= 3; |
| 277 | |
| 278 | // If the extract is known to be all zeros, return zero. |
| 279 | if (ByteStart+ByteSize <= ShAmt) |
| 280 | return Constant::getNullValue(IntegerType::get(CE->getContext(), |
| 281 | ByteSize*8)); |
| 282 | // If the extract is known to be fully in the input, extract it. |
| 283 | if (ByteStart >= ShAmt) |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 284 | return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 285 | |
| 286 | // TODO: Handle the 'partially zero' case. |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | case Instruction::ZExt: { |
| 291 | unsigned SrcBitSize = |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 292 | cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth(); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 293 | |
| 294 | // If extracting something that is completely zero, return 0. |
| 295 | if (ByteStart*8 >= SrcBitSize) |
| 296 | return Constant::getNullValue(IntegerType::get(CE->getContext(), |
| 297 | ByteSize*8)); |
| 298 | |
| 299 | // If exactly extracting the input, return it. |
| 300 | if (ByteStart == 0 && ByteSize*8 == SrcBitSize) |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 301 | return CE->getOperand(0); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 302 | |
| 303 | // If extracting something completely in the input, if if the input is a |
| 304 | // multiple of 8 bits, recurse. |
| 305 | if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize) |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 306 | return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 307 | |
| 308 | // Otherwise, if extracting a subset of the input, which is not multiple of |
| 309 | // 8 bits, do a shift and trunc to get the bits. |
| 310 | if ((ByteStart+ByteSize)*8 < SrcBitSize) { |
| 311 | assert((SrcBitSize&7) && "Shouldn't get byte sized case here"); |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 312 | Constant *Res = CE->getOperand(0); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 313 | if (ByteStart) |
| 314 | Res = ConstantExpr::getLShr(Res, |
| 315 | ConstantInt::get(Res->getType(), ByteStart*8)); |
| 316 | return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(), |
| 317 | ByteSize*8)); |
| 318 | } |
| 319 | |
| 320 | // TODO: Handle the 'partially zero' case. |
| 321 | return 0; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 327 | Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 328 | unsigned opc, Constant *V, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 329 | const Type *DestTy) { |
Chris Lattner | 363485d | 2007-07-20 22:09:02 +0000 | [diff] [blame] | 330 | if (isa<UndefValue>(V)) { |
| 331 | // zext(undef) = 0, because the top bits will be zero. |
| 332 | // sext(undef) = 0, because the top bits will all be the same. |
Chris Lattner | b4c6cc9 | 2008-02-19 06:22:12 +0000 | [diff] [blame] | 333 | // [us]itofp(undef) = 0, because the result value is bounded. |
| 334 | if (opc == Instruction::ZExt || opc == Instruction::SExt || |
| 335 | opc == Instruction::UIToFP || opc == Instruction::SIToFP) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 336 | return Constant::getNullValue(DestTy); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 337 | return UndefValue::get(DestTy); |
Chris Lattner | 363485d | 2007-07-20 22:09:02 +0000 | [diff] [blame] | 338 | } |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 339 | // No compile-time operations on this type yet. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 340 | if (V->getType()->isPPC_FP128Ty() || DestTy->isPPC_FP128Ty()) |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 341 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 342 | |
| 343 | // If the cast operand is a constant expression, there's a few things we can |
| 344 | // do to try to simplify it. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 345 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 346 | if (CE->isCast()) { |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 347 | // Try hard to fold cast of cast because they are often eliminable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 348 | if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 349 | return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 350 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 351 | // If all of the indexes in the GEP are null values, there is no pointer |
| 352 | // adjustment going on. We might as well cast the source pointer. |
| 353 | bool isAllNull = true; |
| 354 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 355 | if (!CE->getOperand(i)->isNullValue()) { |
| 356 | isAllNull = false; |
| 357 | break; |
| 358 | } |
| 359 | if (isAllNull) |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 360 | // This is casting one pointer type to another, always BitCast |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 361 | return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 363 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 364 | |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 365 | // If the cast operand is a constant vector, perform the cast by |
| 366 | // operating on each element. In the cast of bitcasts, the element |
| 367 | // count may be mismatched; don't attempt to handle that here. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 368 | if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 369 | if (isa<VectorType>(DestTy) && |
| 370 | cast<VectorType>(DestTy)->getNumElements() == |
| 371 | CV->getType()->getNumElements()) { |
| 372 | std::vector<Constant*> res; |
| 373 | const VectorType *DestVecTy = cast<VectorType>(DestTy); |
| 374 | const Type *DstEltTy = DestVecTy->getElementType(); |
| 375 | for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 376 | res.push_back(ConstantExpr::getCast(opc, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 377 | CV->getOperand(i), DstEltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 378 | return ConstantVector::get(DestVecTy, res); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 381 | // We actually have to do a cast now. Perform the cast according to the |
| 382 | // opcode specified. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 383 | switch (opc) { |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 384 | default: |
| 385 | llvm_unreachable("Failed to cast constant expression"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 386 | case Instruction::FPTrunc: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 387 | case Instruction::FPExt: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 388 | if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 389 | bool ignored; |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 390 | APFloat Val = FPC->getValueAPF(); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 391 | Val.convert(DestTy->isFloatTy() ? APFloat::IEEEsingle : |
| 392 | DestTy->isDoubleTy() ? APFloat::IEEEdouble : |
| 393 | DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended : |
| 394 | DestTy->isFP128Ty() ? APFloat::IEEEquad : |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 395 | APFloat::Bogus, |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 396 | APFloat::rmNearestTiesToEven, &ignored); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 397 | return ConstantFP::get(Context, Val); |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 398 | } |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 399 | return 0; // Can't fold. |
| 400 | case Instruction::FPToUI: |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 401 | case Instruction::FPToSI: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 402 | if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
Chris Lattner | 2b827fd | 2007-10-15 05:34:10 +0000 | [diff] [blame] | 403 | const APFloat &V = FPC->getValueAPF(); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 404 | bool ignored; |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 405 | uint64_t x[2]; |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 406 | uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
Dale Johannesen | 17663f4 | 2007-09-25 23:32:20 +0000 | [diff] [blame] | 407 | (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 408 | APFloat::rmTowardZero, &ignored); |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 409 | APInt Val(DestBitWidth, 2, x); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 410 | return ConstantInt::get(Context, Val); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 411 | } |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 412 | return 0; // Can't fold. |
| 413 | case Instruction::IntToPtr: //always treated as unsigned |
| 414 | if (V->isNullValue()) // Is it an integral null value? |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 415 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 416 | return 0; // Other pointer types cannot be casted |
| 417 | case Instruction::PtrToInt: // always treated as unsigned |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame^] | 418 | // Is it a null pointer value? |
| 419 | if (V->isNullValue()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 420 | return ConstantInt::get(DestTy, 0); |
Dan Gohman | cf91383 | 2010-01-28 02:15:55 +0000 | [diff] [blame^] | 421 | // If this is a sizeof of an array or vector, pull out a multiplication |
| 422 | // by the element size to expose it to subsequent folding. |
| 423 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 424 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 425 | CE->getNumOperands() == 2 && |
| 426 | CE->getOperand(0)->isNullValue()) |
| 427 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1))) |
| 428 | if (CI->isOne()) { |
| 429 | const Type *Ty = |
| 430 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 431 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 432 | Constant *N = ConstantInt::get(DestTy, ATy->getNumElements()); |
| 433 | Constant *E = ConstantExpr::getSizeOf(ATy->getElementType()); |
| 434 | E = ConstantExpr::getCast(CastInst::getCastOpcode(E, false, |
| 435 | DestTy, false), |
| 436 | E, DestTy); |
| 437 | return ConstantExpr::getMul(N, E); |
| 438 | } |
| 439 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 440 | Constant *N = ConstantInt::get(DestTy, VTy->getNumElements()); |
| 441 | Constant *E = ConstantExpr::getSizeOf(VTy->getElementType()); |
| 442 | E = ConstantExpr::getCast(CastInst::getCastOpcode(E, false, |
| 443 | DestTy, false), |
| 444 | E, DestTy); |
| 445 | return ConstantExpr::getMul(N, E); |
| 446 | } |
| 447 | } |
| 448 | // Other pointer types cannot be casted |
| 449 | return 0; |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 450 | case Instruction::UIToFP: |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 451 | case Instruction::SIToFP: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 452 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dale Johannesen | 9150652 | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 453 | APInt api = CI->getValue(); |
| 454 | const uint64_t zero[] = {0, 0}; |
Dale Johannesen | 9150652 | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 455 | APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(), |
| 456 | 2, zero)); |
Dan Gohman | 06c45d5 | 2008-02-29 01:42:52 +0000 | [diff] [blame] | 457 | (void)apf.convertFromAPInt(api, |
| 458 | opc==Instruction::SIToFP, |
| 459 | APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 460 | return ConstantFP::get(Context, apf); |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 461 | } |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 462 | return 0; |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 463 | case Instruction::ZExt: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 464 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 465 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 466 | APInt Result(CI->getValue()); |
| 467 | Result.zext(BitWidth); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 468 | return ConstantInt::get(Context, Result); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 469 | } |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 470 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 471 | case Instruction::SExt: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 472 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 473 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 474 | APInt Result(CI->getValue()); |
| 475 | Result.sext(BitWidth); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 476 | return ConstantInt::get(Context, Result); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 477 | } |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 478 | return 0; |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 479 | case Instruction::Trunc: { |
| 480 | uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 481 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 482 | APInt Result(CI->getValue()); |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 483 | Result.trunc(DestBitWidth); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 484 | return ConstantInt::get(Context, Result); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 485 | } |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 486 | |
| 487 | // The input must be a constantexpr. See if we can simplify this based on |
| 488 | // the bytes we are demanding. Only do this if the source and dest are an |
| 489 | // even multiple of a byte. |
| 490 | if ((DestBitWidth & 7) == 0 && |
| 491 | (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0) |
| 492 | if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8)) |
| 493 | return Res; |
| 494 | |
Chris Lattner | 710ebaf | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 495 | return 0; |
Chris Lattner | f67d297 | 2009-10-17 21:53:27 +0000 | [diff] [blame] | 496 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 497 | case Instruction::BitCast: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 498 | return FoldBitCast(Context, V, DestTy); |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 499 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 502 | Constant *llvm::ConstantFoldSelectInstruction(LLVMContext&, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 503 | Constant *Cond, |
| 504 | Constant *V1, Constant *V2) { |
| 505 | if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond)) |
| 506 | return CB->getZExtValue() ? V1 : V2; |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 507 | |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 508 | if (isa<UndefValue>(V1)) return V2; |
| 509 | if (isa<UndefValue>(V2)) return V1; |
| 510 | if (isa<UndefValue>(Cond)) return V1; |
| 511 | if (V1 == V2) return V1; |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 515 | Constant *llvm::ConstantFoldExtractElementInstruction(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 516 | Constant *Val, |
| 517 | Constant *Idx) { |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 518 | if (isa<UndefValue>(Val)) // ee(undef, x) -> undef |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 519 | return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); |
Chris Lattner | e4f9d7b | 2006-04-07 04:44:06 +0000 | [diff] [blame] | 520 | if (Val->isNullValue()) // ee(zero, x) -> zero |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 521 | return Constant::getNullValue( |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 522 | cast<VectorType>(Val->getType())->getElementType()); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 523 | |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 524 | if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { |
| 525 | if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 526 | return CVal->getOperand(CIdx->getZExtValue()); |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 527 | } else if (isa<UndefValue>(Idx)) { |
| 528 | // ee({w,x,y,z}, undef) -> w (an arbitrary value). |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 529 | return CVal->getOperand(0); |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 530 | } |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 531 | } |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 535 | Constant *llvm::ConstantFoldInsertElementInstruction(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 536 | Constant *Val, |
| 537 | Constant *Elt, |
| 538 | Constant *Idx) { |
| 539 | ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 540 | if (!CIdx) return 0; |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 541 | APInt idxVal = CIdx->getValue(); |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 542 | if (isa<UndefValue>(Val)) { |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 543 | // Insertion of scalar constant into vector undef |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 544 | // Optimize away insertion of undef |
| 545 | if (isa<UndefValue>(Elt)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 546 | return Val; |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 547 | // Otherwise break the aggregate undef into multiple undefs and do |
| 548 | // the insertion |
| 549 | unsigned numOps = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 550 | cast<VectorType>(Val->getType())->getNumElements(); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 551 | std::vector<Constant*> Ops; |
| 552 | Ops.reserve(numOps); |
| 553 | for (unsigned i = 0; i < numOps; ++i) { |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 554 | Constant *Op = |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 555 | (idxVal == i) ? Elt : UndefValue::get(Elt->getType()); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 556 | Ops.push_back(Op); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 557 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 558 | return ConstantVector::get(Ops); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 559 | } |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 560 | if (isa<ConstantAggregateZero>(Val)) { |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 561 | // Insertion of scalar constant into vector aggregate zero |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 562 | // Optimize away insertion of zero |
| 563 | if (Elt->isNullValue()) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 564 | return Val; |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 565 | // Otherwise break the aggregate zero into multiple zeros and do |
| 566 | // the insertion |
| 567 | unsigned numOps = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 568 | cast<VectorType>(Val->getType())->getNumElements(); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 569 | std::vector<Constant*> Ops; |
| 570 | Ops.reserve(numOps); |
| 571 | for (unsigned i = 0; i < numOps; ++i) { |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 572 | Constant *Op = |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 573 | (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType()); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 574 | Ops.push_back(Op); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 575 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 576 | return ConstantVector::get(Ops); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 577 | } |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 578 | if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 579 | // Insertion of scalar constant into vector constant |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 580 | std::vector<Constant*> Ops; |
| 581 | Ops.reserve(CVal->getNumOperands()); |
| 582 | for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 583 | Constant *Op = |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 584 | (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i)); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 585 | Ops.push_back(Op); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 586 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 587 | return ConstantVector::get(Ops); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 588 | } |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 589 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 593 | /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef |
| 594 | /// return the specified element value. Otherwise return null. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 595 | static Constant *GetVectorElement(LLVMContext &Context, Constant *C, |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 596 | unsigned EltNo) { |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 597 | if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 598 | return CV->getOperand(EltNo); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 600 | const Type *EltTy = cast<VectorType>(C->getType())->getElementType(); |
| 601 | if (isa<ConstantAggregateZero>(C)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 602 | return Constant::getNullValue(EltTy); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 603 | if (isa<UndefValue>(C)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 604 | return UndefValue::get(EltTy); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 608 | Constant *llvm::ConstantFoldShuffleVectorInstruction(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 609 | Constant *V1, |
| 610 | Constant *V2, |
| 611 | Constant *Mask) { |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 612 | // Undefined shuffle mask -> undefined value. |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 613 | if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType()); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 614 | |
| 615 | unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements(); |
| 616 | unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements(); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 617 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 619 | // Loop over the shuffle mask, evaluating each element. |
| 620 | SmallVector<Constant*, 32> Result; |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 621 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 622 | Constant *InElt = GetVectorElement(Context, Mask, i); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 623 | if (InElt == 0) return 0; |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 625 | if (isa<UndefValue>(InElt)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 626 | InElt = UndefValue::get(EltTy); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 627 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) { |
| 628 | unsigned Elt = CI->getZExtValue(); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 629 | if (Elt >= SrcNumElts*2) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 630 | InElt = UndefValue::get(EltTy); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 631 | else if (Elt >= SrcNumElts) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 632 | InElt = GetVectorElement(Context, V2, Elt - SrcNumElts); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 633 | else |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 634 | InElt = GetVectorElement(Context, V1, Elt); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 635 | if (InElt == 0) return 0; |
| 636 | } else { |
| 637 | // Unknown value. |
| 638 | return 0; |
| 639 | } |
| 640 | Result.push_back(InElt); |
| 641 | } |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 642 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 643 | return ConstantVector::get(&Result[0], Result.size()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 646 | Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 647 | Constant *Agg, |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 648 | const unsigned *Idxs, |
| 649 | unsigned NumIdx) { |
| 650 | // Base case: no indices, so return the entire value. |
| 651 | if (NumIdx == 0) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 652 | return Agg; |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 653 | |
| 654 | if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 655 | return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 656 | Idxs, |
| 657 | Idxs + NumIdx)); |
| 658 | |
| 659 | if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0 |
| 660 | return |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 661 | Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 662 | Idxs, |
| 663 | Idxs + NumIdx)); |
| 664 | |
| 665 | // Otherwise recurse. |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 666 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) |
| 667 | return ConstantFoldExtractValueInstruction(Context, CS->getOperand(*Idxs), |
| 668 | Idxs+1, NumIdx-1); |
| 669 | |
| 670 | if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) |
| 671 | return ConstantFoldExtractValueInstruction(Context, CA->getOperand(*Idxs), |
| 672 | Idxs+1, NumIdx-1); |
| 673 | ConstantVector *CV = cast<ConstantVector>(Agg); |
| 674 | return ConstantFoldExtractValueInstruction(Context, CV->getOperand(*Idxs), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 675 | Idxs+1, NumIdx-1); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 678 | Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 679 | Constant *Agg, |
| 680 | Constant *Val, |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 681 | const unsigned *Idxs, |
| 682 | unsigned NumIdx) { |
| 683 | // Base case: no indices, so replace the entire value. |
| 684 | if (NumIdx == 0) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 685 | return Val; |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 686 | |
| 687 | if (isa<UndefValue>(Agg)) { |
| 688 | // Insertion of constant into aggregate undef |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 689 | // Optimize away insertion of undef. |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 690 | if (isa<UndefValue>(Val)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 691 | return Agg; |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 692 | |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 693 | // Otherwise break the aggregate undef into multiple undefs and do |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 694 | // the insertion. |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 695 | const CompositeType *AggTy = cast<CompositeType>(Agg->getType()); |
| 696 | unsigned numOps; |
| 697 | if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy)) |
| 698 | numOps = AR->getNumElements(); |
| 699 | else |
| 700 | numOps = cast<StructType>(AggTy)->getNumElements(); |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 701 | |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 702 | std::vector<Constant*> Ops(numOps); |
| 703 | for (unsigned i = 0; i < numOps; ++i) { |
| 704 | const Type *MemberTy = AggTy->getTypeAtIndex(i); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 705 | Constant *Op = |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 706 | (*Idxs == i) ? |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 707 | ConstantFoldInsertValueInstruction(Context, UndefValue::get(MemberTy), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 708 | Val, Idxs+1, NumIdx-1) : |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 709 | UndefValue::get(MemberTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 710 | Ops[i] = Op; |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 711 | } |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 712 | |
| 713 | if (const StructType* ST = dyn_cast<StructType>(AggTy)) |
| 714 | return ConstantStruct::get(Context, Ops, ST->isPacked()); |
| 715 | return ConstantArray::get(cast<ArrayType>(AggTy), Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 716 | } |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 717 | |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 718 | if (isa<ConstantAggregateZero>(Agg)) { |
| 719 | // Insertion of constant into aggregate zero |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 720 | // Optimize away insertion of zero. |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 721 | if (Val->isNullValue()) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 722 | return Agg; |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 723 | |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 724 | // Otherwise break the aggregate zero into multiple zeros and do |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 725 | // the insertion. |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 726 | const CompositeType *AggTy = cast<CompositeType>(Agg->getType()); |
| 727 | unsigned numOps; |
| 728 | if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy)) |
| 729 | numOps = AR->getNumElements(); |
| 730 | else |
| 731 | numOps = cast<StructType>(AggTy)->getNumElements(); |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 732 | |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 733 | std::vector<Constant*> Ops(numOps); |
| 734 | for (unsigned i = 0; i < numOps; ++i) { |
| 735 | const Type *MemberTy = AggTy->getTypeAtIndex(i); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 736 | Constant *Op = |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 737 | (*Idxs == i) ? |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 738 | ConstantFoldInsertValueInstruction(Context, |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 739 | Constant::getNullValue(MemberTy), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 740 | Val, Idxs+1, NumIdx-1) : |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 741 | Constant::getNullValue(MemberTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 742 | Ops[i] = Op; |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 743 | } |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 744 | |
| 745 | if (const StructType* ST = dyn_cast<StructType>(AggTy)) |
| 746 | return ConstantStruct::get(Context, Ops, ST->isPacked()); |
| 747 | return ConstantArray::get(cast<ArrayType>(AggTy), Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 748 | } |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 749 | |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 750 | if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) { |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 751 | // Insertion of constant into aggregate constant. |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 752 | std::vector<Constant*> Ops(Agg->getNumOperands()); |
| 753 | for (unsigned i = 0; i < Agg->getNumOperands(); ++i) { |
Chris Lattner | a91a563 | 2009-10-28 05:14:34 +0000 | [diff] [blame] | 754 | Constant *Op = cast<Constant>(Agg->getOperand(i)); |
| 755 | if (*Idxs == i) |
| 756 | Op = ConstantFoldInsertValueInstruction(Context, Op, |
| 757 | Val, Idxs+1, NumIdx-1); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 758 | Ops[i] = Op; |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 759 | } |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 760 | |
| 761 | if (const StructType* ST = dyn_cast<StructType>(Agg->getType())) |
| 762 | return ConstantStruct::get(Context, Ops, ST->isPacked()); |
| 763 | return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 766 | return 0; |
| 767 | } |
| 768 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 769 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 770 | Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context, |
| 771 | unsigned Opcode, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 772 | Constant *C1, Constant *C2) { |
Dale Johannesen | e5facd5 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 773 | // No compile-time operations on this type yet. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 774 | if (C1->getType()->isPPC_FP128Ty()) |
Dale Johannesen | e5facd5 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 775 | return 0; |
| 776 | |
Chris Lattner | ee8f74f | 2009-09-15 06:28:12 +0000 | [diff] [blame] | 777 | // Handle UndefValue up front. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 778 | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { |
| 779 | switch (Opcode) { |
Evan Cheng | df1690d | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 780 | case Instruction::Xor: |
| 781 | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) |
| 782 | // Handle undef ^ undef -> 0 special case. This is a common |
| 783 | // idiom (misuse). |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 784 | return Constant::getNullValue(C1->getType()); |
Evan Cheng | df1690d | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 785 | // Fallthrough |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 786 | case Instruction::Add: |
| 787 | case Instruction::Sub: |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 788 | return UndefValue::get(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 789 | case Instruction::Mul: |
| 790 | case Instruction::And: |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 791 | return Constant::getNullValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 792 | case Instruction::UDiv: |
| 793 | case Instruction::SDiv: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 794 | case Instruction::URem: |
| 795 | case Instruction::SRem: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 796 | if (!isa<UndefValue>(C2)) // undef / X -> 0 |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 797 | return Constant::getNullValue(C1->getType()); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 798 | return C2; // X / undef -> undef |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 799 | case Instruction::Or: // X | undef -> -1 |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 800 | if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType())) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 801 | return Constant::getAllOnesValue(PTy); |
| 802 | return Constant::getAllOnesValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 803 | case Instruction::LShr: |
| 804 | if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 805 | return C1; // undef lshr undef -> undef |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 806 | return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 807 | // undef lshr X -> 0 |
| 808 | case Instruction::AShr: |
| 809 | if (!isa<UndefValue>(C2)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 810 | return C1; // undef ashr X --> undef |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 811 | else if (isa<UndefValue>(C1)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 812 | return C1; // undef ashr undef -> undef |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 813 | else |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 814 | return C1; // X ashr undef --> X |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 815 | case Instruction::Shl: |
| 816 | // undef << X -> 0 or X << undef -> 0 |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 817 | return Constant::getNullValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 818 | } |
| 819 | } |
| 820 | |
Nick Lewycky | 6b8320f | 2009-06-21 01:56:41 +0000 | [diff] [blame] | 821 | // Handle simplifications when the RHS is a constant int. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 822 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 823 | switch (Opcode) { |
| 824 | case Instruction::Add: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 825 | if (CI2->equalsInt(0)) return C1; // X + 0 == X |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 826 | break; |
| 827 | case Instruction::Sub: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 828 | if (CI2->equalsInt(0)) return C1; // X - 0 == X |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 829 | break; |
| 830 | case Instruction::Mul: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 831 | if (CI2->equalsInt(0)) return C2; // X * 0 == 0 |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 832 | if (CI2->equalsInt(1)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 833 | return C1; // X * 1 == X |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 834 | break; |
| 835 | case Instruction::UDiv: |
| 836 | case Instruction::SDiv: |
| 837 | if (CI2->equalsInt(1)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 838 | return C1; // X / 1 == X |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 839 | if (CI2->equalsInt(0)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 840 | return UndefValue::get(CI2->getType()); // X / 0 == undef |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 841 | break; |
| 842 | case Instruction::URem: |
| 843 | case Instruction::SRem: |
| 844 | if (CI2->equalsInt(1)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 845 | return Constant::getNullValue(CI2->getType()); // X % 1 == 0 |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 846 | if (CI2->equalsInt(0)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 847 | return UndefValue::get(CI2->getType()); // X % 0 == undef |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 848 | break; |
| 849 | case Instruction::And: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 850 | if (CI2->isZero()) return C2; // X & 0 == 0 |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 851 | if (CI2->isAllOnesValue()) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 852 | return C1; // X & -1 == X |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 853 | |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 854 | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 855 | // (zext i32 to i64) & 4294967295 -> (zext i32 to i64) |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 856 | if (CE1->getOpcode() == Instruction::ZExt) { |
| 857 | unsigned DstWidth = CI2->getType()->getBitWidth(); |
| 858 | unsigned SrcWidth = |
| 859 | CE1->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 860 | APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth)); |
| 861 | if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 862 | return C1; |
Chris Lattner | 6d94bb7 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 863 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 864 | |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 865 | // If and'ing the address of a global with a constant, fold it. |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 866 | if (CE1->getOpcode() == Instruction::PtrToInt && |
| 867 | isa<GlobalValue>(CE1->getOperand(0))) { |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 868 | GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 869 | |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 870 | // Functions are at least 4-byte aligned. |
| 871 | unsigned GVAlign = GV->getAlignment(); |
| 872 | if (isa<Function>(GV)) |
| 873 | GVAlign = std::max(GVAlign, 4U); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 874 | |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 875 | if (GVAlign > 1) { |
| 876 | unsigned DstWidth = CI2->getType()->getBitWidth(); |
Chris Lattner | 912bec7 | 2008-04-20 19:59:12 +0000 | [diff] [blame] | 877 | unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign)); |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 878 | APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth)); |
| 879 | |
| 880 | // If checking bits we know are clear, return zero. |
| 881 | if ((CI2->getValue() & BitsNotSet) == CI2->getValue()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 882 | return Constant::getNullValue(CI2->getType()); |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 883 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 884 | } |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 885 | } |
| 886 | break; |
| 887 | case Instruction::Or: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 888 | if (CI2->equalsInt(0)) return C1; // X | 0 == X |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 889 | if (CI2->isAllOnesValue()) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 890 | return C2; // X | -1 == -1 |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 891 | break; |
| 892 | case Instruction::Xor: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 893 | if (CI2->equalsInt(0)) return C1; // X ^ 0 == X |
Nick Lewycky | 2826040 | 2009-09-20 06:24:51 +0000 | [diff] [blame] | 894 | |
| 895 | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { |
| 896 | switch (CE1->getOpcode()) { |
| 897 | default: break; |
| 898 | case Instruction::ICmp: |
| 899 | case Instruction::FCmp: |
Nick Lewycky | ff550aa | 2009-09-20 06:27:35 +0000 | [diff] [blame] | 900 | // cmp pred ^ true -> cmp !pred |
Nick Lewycky | 2826040 | 2009-09-20 06:24:51 +0000 | [diff] [blame] | 901 | assert(CI2->equalsInt(1)); |
Nick Lewycky | 0f8348e | 2009-09-20 06:26:34 +0000 | [diff] [blame] | 902 | CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate(); |
Nick Lewycky | 2826040 | 2009-09-20 06:24:51 +0000 | [diff] [blame] | 903 | pred = CmpInst::getInversePredicate(pred); |
| 904 | return ConstantExpr::getCompare(pred, CE1->getOperand(0), |
| 905 | CE1->getOperand(1)); |
| 906 | } |
| 907 | } |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 908 | break; |
| 909 | case Instruction::AShr: |
| 910 | // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2 |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 911 | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) |
Chris Lattner | 6d94bb7 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 912 | if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 913 | return ConstantExpr::getLShr(C1, C2); |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 914 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 915 | } |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 916 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 918 | // At this point we know neither constant is an UndefValue. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 919 | if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { |
| 920 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 921 | using namespace APIntOps; |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 922 | const APInt &C1V = CI1->getValue(); |
| 923 | const APInt &C2V = CI2->getValue(); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 924 | switch (Opcode) { |
| 925 | default: |
| 926 | break; |
| 927 | case Instruction::Add: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 928 | return ConstantInt::get(Context, C1V + C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 929 | case Instruction::Sub: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 930 | return ConstantInt::get(Context, C1V - C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 931 | case Instruction::Mul: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 932 | return ConstantInt::get(Context, C1V * C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 933 | case Instruction::UDiv: |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 934 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 935 | return ConstantInt::get(Context, C1V.udiv(C2V)); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 936 | case Instruction::SDiv: |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 937 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 938 | if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 939 | return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 940 | return ConstantInt::get(Context, C1V.sdiv(C2V)); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 941 | case Instruction::URem: |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 942 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 943 | return ConstantInt::get(Context, C1V.urem(C2V)); |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 944 | case Instruction::SRem: |
| 945 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 946 | if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 947 | return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 948 | return ConstantInt::get(Context, C1V.srem(C2V)); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 949 | case Instruction::And: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 950 | return ConstantInt::get(Context, C1V & C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 951 | case Instruction::Or: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 952 | return ConstantInt::get(Context, C1V | C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 953 | case Instruction::Xor: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 954 | return ConstantInt::get(Context, C1V ^ C2V); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 955 | case Instruction::Shl: { |
| 956 | uint32_t shiftAmt = C2V.getZExtValue(); |
| 957 | if (shiftAmt < C1V.getBitWidth()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 958 | return ConstantInt::get(Context, C1V.shl(shiftAmt)); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 959 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 960 | return UndefValue::get(C1->getType()); // too big shift is undef |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 961 | } |
| 962 | case Instruction::LShr: { |
| 963 | uint32_t shiftAmt = C2V.getZExtValue(); |
| 964 | if (shiftAmt < C1V.getBitWidth()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 965 | return ConstantInt::get(Context, C1V.lshr(shiftAmt)); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 966 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 967 | return UndefValue::get(C1->getType()); // too big shift is undef |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 968 | } |
| 969 | case Instruction::AShr: { |
| 970 | uint32_t shiftAmt = C2V.getZExtValue(); |
| 971 | if (shiftAmt < C1V.getBitWidth()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 972 | return ConstantInt::get(Context, C1V.ashr(shiftAmt)); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 973 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 974 | return UndefValue::get(C1->getType()); // too big shift is undef |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 975 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
Nick Lewycky | 6b8320f | 2009-06-21 01:56:41 +0000 | [diff] [blame] | 978 | |
| 979 | switch (Opcode) { |
| 980 | case Instruction::SDiv: |
| 981 | case Instruction::UDiv: |
| 982 | case Instruction::URem: |
| 983 | case Instruction::SRem: |
| 984 | case Instruction::LShr: |
| 985 | case Instruction::AShr: |
| 986 | case Instruction::Shl: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 987 | if (CI1->equalsInt(0)) return C1; |
Nick Lewycky | 6b8320f | 2009-06-21 01:56:41 +0000 | [diff] [blame] | 988 | break; |
| 989 | default: |
| 990 | break; |
| 991 | } |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 992 | } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { |
| 993 | if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 994 | APFloat C1V = CFP1->getValueAPF(); |
| 995 | APFloat C2V = CFP2->getValueAPF(); |
| 996 | APFloat C3V = C1V; // copy for modification |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 997 | switch (Opcode) { |
| 998 | default: |
| 999 | break; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1000 | case Instruction::FAdd: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1001 | (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1002 | return ConstantFP::get(Context, C3V); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1003 | case Instruction::FSub: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1004 | (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1005 | return ConstantFP::get(Context, C3V); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1006 | case Instruction::FMul: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1007 | (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1008 | return ConstantFP::get(Context, C3V); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1009 | case Instruction::FDiv: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1010 | (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1011 | return ConstantFP::get(Context, C3V); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1012 | case Instruction::FRem: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1013 | (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1014 | return ConstantFP::get(Context, C3V); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
Dan Gohman | 9f39660 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 1017 | } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1018 | ConstantVector *CP1 = dyn_cast<ConstantVector>(C1); |
| 1019 | ConstantVector *CP2 = dyn_cast<ConstantVector>(C2); |
Dan Gohman | b43e020 | 2007-10-31 21:36:31 +0000 | [diff] [blame] | 1020 | if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) && |
| 1021 | (CP2 != NULL || isa<ConstantAggregateZero>(C2))) { |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1022 | std::vector<Constant*> Res; |
| 1023 | const Type* EltTy = VTy->getElementType(); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1024 | Constant *C1 = 0; |
| 1025 | Constant *C2 = 0; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1026 | switch (Opcode) { |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1027 | default: |
| 1028 | break; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1029 | case Instruction::Add: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1030 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1031 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1032 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1033 | Res.push_back(ConstantExpr::getAdd(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1034 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1035 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1036 | case Instruction::FAdd: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1037 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1038 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1039 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1040 | Res.push_back(ConstantExpr::getFAdd(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1041 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1042 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1043 | case Instruction::Sub: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1044 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1045 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1046 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1047 | Res.push_back(ConstantExpr::getSub(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1048 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1049 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1050 | case Instruction::FSub: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1051 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1052 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1053 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1054 | Res.push_back(ConstantExpr::getFSub(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1055 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1056 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1057 | case Instruction::Mul: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1058 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1059 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1060 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1061 | Res.push_back(ConstantExpr::getMul(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1062 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1063 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1064 | case Instruction::FMul: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1065 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1066 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1067 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1068 | Res.push_back(ConstantExpr::getFMul(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1069 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1070 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1071 | case Instruction::UDiv: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1072 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1073 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1074 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1075 | Res.push_back(ConstantExpr::getUDiv(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1076 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1077 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1078 | case Instruction::SDiv: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1079 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1080 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1081 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1082 | Res.push_back(ConstantExpr::getSDiv(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1083 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1084 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1085 | case Instruction::FDiv: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1086 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1087 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1088 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1089 | Res.push_back(ConstantExpr::getFDiv(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1090 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1091 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1092 | case Instruction::URem: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1093 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1094 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1095 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1096 | Res.push_back(ConstantExpr::getURem(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1097 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1098 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1099 | case Instruction::SRem: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1100 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1101 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1102 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1103 | Res.push_back(ConstantExpr::getSRem(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1104 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1105 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1106 | case Instruction::FRem: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1107 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1108 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1109 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1110 | Res.push_back(ConstantExpr::getFRem(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1111 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1112 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 1113 | case Instruction::And: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1114 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1115 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1116 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1117 | Res.push_back(ConstantExpr::getAnd(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1118 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1119 | return ConstantVector::get(Res); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1120 | case Instruction::Or: |
| 1121 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1122 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1123 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1124 | Res.push_back(ConstantExpr::getOr(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1125 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1126 | return ConstantVector::get(Res); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1127 | case Instruction::Xor: |
| 1128 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1129 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1130 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1131 | Res.push_back(ConstantExpr::getXor(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1132 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1133 | return ConstantVector::get(Res); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 1134 | case Instruction::LShr: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1135 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1136 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1137 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1138 | Res.push_back(ConstantExpr::getLShr(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1139 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1140 | return ConstantVector::get(Res); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 1141 | case Instruction::AShr: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1142 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1143 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1144 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1145 | Res.push_back(ConstantExpr::getAShr(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1146 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1147 | return ConstantVector::get(Res); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 1148 | case Instruction::Shl: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1149 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1150 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 1151 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1152 | Res.push_back(ConstantExpr::getShl(C1, C2)); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 1153 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1154 | return ConstantVector::get(Res); |
Dan Gohman | b43e020 | 2007-10-31 21:36:31 +0000 | [diff] [blame] | 1155 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 1159 | if (isa<ConstantExpr>(C1)) { |
| 1160 | // There are many possible foldings we could do here. We should probably |
| 1161 | // at least fold add of a pointer with an integer into the appropriate |
| 1162 | // getelementptr. This will improve alias analysis a bit. |
| 1163 | } else if (isa<ConstantExpr>(C2)) { |
| 1164 | // If C2 is a constant expr and C1 isn't, flop them around and fold the |
| 1165 | // other way if possible. |
| 1166 | switch (Opcode) { |
| 1167 | case Instruction::Add: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1168 | case Instruction::FAdd: |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 1169 | case Instruction::Mul: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1170 | case Instruction::FMul: |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 1171 | case Instruction::And: |
| 1172 | case Instruction::Or: |
| 1173 | case Instruction::Xor: |
| 1174 | // No change of opcode required. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1175 | return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1176 | |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 1177 | case Instruction::Shl: |
| 1178 | case Instruction::LShr: |
| 1179 | case Instruction::AShr: |
| 1180 | case Instruction::Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1181 | case Instruction::FSub: |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 1182 | case Instruction::SDiv: |
| 1183 | case Instruction::UDiv: |
| 1184 | case Instruction::FDiv: |
| 1185 | case Instruction::URem: |
| 1186 | case Instruction::SRem: |
| 1187 | case Instruction::FRem: |
| 1188 | default: // These instructions cannot be flopped around. |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1192 | |
Nick Lewycky | 605109d | 2009-09-20 00:04:02 +0000 | [diff] [blame] | 1193 | // i1 can be simplified in many cases. |
Benjamin Kramer | a81a6df | 2010-01-05 20:07:06 +0000 | [diff] [blame] | 1194 | if (C1->getType()->isInteger(1)) { |
Nick Lewycky | 605109d | 2009-09-20 00:04:02 +0000 | [diff] [blame] | 1195 | switch (Opcode) { |
| 1196 | case Instruction::Add: |
| 1197 | case Instruction::Sub: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1198 | return ConstantExpr::getXor(C1, C2); |
Nick Lewycky | 605109d | 2009-09-20 00:04:02 +0000 | [diff] [blame] | 1199 | case Instruction::Mul: |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1200 | return ConstantExpr::getAnd(C1, C2); |
Nick Lewycky | 605109d | 2009-09-20 00:04:02 +0000 | [diff] [blame] | 1201 | case Instruction::Shl: |
| 1202 | case Instruction::LShr: |
| 1203 | case Instruction::AShr: |
| 1204 | // We can assume that C2 == 0. If it were one the result would be |
| 1205 | // undefined because the shift value is as large as the bitwidth. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1206 | return C1; |
Nick Lewycky | 605109d | 2009-09-20 00:04:02 +0000 | [diff] [blame] | 1207 | case Instruction::SDiv: |
| 1208 | case Instruction::UDiv: |
| 1209 | // We can assume that C2 == 1. If it were zero the result would be |
| 1210 | // undefined through division by zero. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1211 | return C1; |
Nick Lewycky | 605109d | 2009-09-20 00:04:02 +0000 | [diff] [blame] | 1212 | case Instruction::URem: |
| 1213 | case Instruction::SRem: |
| 1214 | // We can assume that C2 == 1. If it were zero the result would be |
| 1215 | // undefined through division by zero. |
| 1216 | return ConstantInt::getFalse(Context); |
| 1217 | default: |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 1222 | // We don't know how to fold this. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1226 | /// isZeroSizedType - This type is zero sized if its an array or structure of |
| 1227 | /// zero sized types. The only leaf zero sized type is an empty structure. |
| 1228 | static bool isMaybeZeroSizedType(const Type *Ty) { |
| 1229 | if (isa<OpaqueType>(Ty)) return true; // Can't say. |
| 1230 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 1231 | |
| 1232 | // If all of elements have zero size, this does too. |
| 1233 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Chris Lattner | feaf92f | 2005-01-28 23:17:27 +0000 | [diff] [blame] | 1234 | if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1235 | return true; |
| 1236 | |
| 1237 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 1238 | return isMaybeZeroSizedType(ATy->getElementType()); |
| 1239 | } |
| 1240 | return false; |
| 1241 | } |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 1242 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1243 | /// IdxCompare - Compare the two constants as though they were getelementptr |
| 1244 | /// indices. This allows coersion of the types to be the same thing. |
| 1245 | /// |
| 1246 | /// If the two constants are the "same" (after coersion), return 0. If the |
| 1247 | /// first is less than the second, return -1, if the second is less than the |
| 1248 | /// first, return 1. If the constants are not integral, return -2. |
| 1249 | /// |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1250 | static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2, |
| 1251 | const Type *ElTy) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1252 | if (C1 == C2) return 0; |
| 1253 | |
Reid Spencer | c90cf77 | 2006-12-31 21:43:30 +0000 | [diff] [blame] | 1254 | // Ok, we found a different index. If they are not ConstantInt, we can't do |
| 1255 | // anything with them. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1256 | if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) |
| 1257 | return -2; // don't know! |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 1259 | // Ok, we have two differing integer indices. Sign extend them to be the same |
| 1260 | // type. Long is always big enough, so we use it. |
Benjamin Kramer | d2564e3 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 1261 | if (!C1->getType()->isInteger(64)) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1262 | C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(Context)); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1263 | |
Benjamin Kramer | d2564e3 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 1264 | if (!C2->getType()->isInteger(64)) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1265 | C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(Context)); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1266 | |
| 1267 | if (C1 == C2) return 0; // They are equal |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1269 | // If the type being indexed over is really just a zero sized type, there is |
| 1270 | // no pointer difference being made here. |
| 1271 | if (isMaybeZeroSizedType(ElTy)) |
| 1272 | return -2; // dunno. |
| 1273 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1274 | // If they are really different, now that they are the same type, then we |
| 1275 | // found a difference! |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1276 | if (cast<ConstantInt>(C1)->getSExtValue() < |
| 1277 | cast<ConstantInt>(C2)->getSExtValue()) |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1278 | return -1; |
| 1279 | else |
| 1280 | return 1; |
| 1281 | } |
| 1282 | |
Chris Lattner | 858f4e9 | 2007-01-04 02:13:20 +0000 | [diff] [blame] | 1283 | /// evaluateFCmpRelation - This function determines if there is anything we can |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1284 | /// decide about the two constants provided. This doesn't need to handle simple |
| 1285 | /// things like ConstantFP comparisons, but should instead handle ConstantExprs. |
| 1286 | /// If we can determine that the two constants have a particular relation to |
| 1287 | /// each other, we should return the corresponding FCmpInst predicate, |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1288 | /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in |
| 1289 | /// ConstantFoldCompareInstruction. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1290 | /// |
| 1291 | /// To simplify this code we canonicalize the relation so that the first |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1292 | /// operand is always the most "complex" of the two. We consider ConstantFP |
| 1293 | /// to be the simplest, and ConstantExprs to be the most complex. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1294 | static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1295 | Constant *V1, Constant *V2) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1296 | assert(V1->getType() == V2->getType() && |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1297 | "Cannot compare values of different types!"); |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1298 | |
| 1299 | // No compile-time operations on this type yet. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1300 | if (V1->getType()->isPPC_FP128Ty()) |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1301 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 1302 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1303 | // Handle degenerate case quickly |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1304 | if (V1 == V2) return FCmpInst::FCMP_OEQ; |
| 1305 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1306 | if (!isa<ConstantExpr>(V1)) { |
| 1307 | if (!isa<ConstantExpr>(V2)) { |
| 1308 | // We distilled thisUse the standard constant folder for a few cases |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1309 | ConstantInt *R = 0; |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1310 | R = dyn_cast<ConstantInt>( |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1311 | ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1312 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1313 | return FCmpInst::FCMP_OEQ; |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1314 | R = dyn_cast<ConstantInt>( |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1315 | ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1316 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1317 | return FCmpInst::FCMP_OLT; |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1318 | R = dyn_cast<ConstantInt>( |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1319 | ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1320 | if (R && !R->isZero()) |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1321 | return FCmpInst::FCMP_OGT; |
| 1322 | |
| 1323 | // Nothing more we can do |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1324 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 1325 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1326 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1327 | // If the first operand is simple and second is ConstantExpr, swap operands. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1328 | FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1329 | if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE) |
| 1330 | return FCmpInst::getSwappedPredicate(SwappedRelation); |
| 1331 | } else { |
| 1332 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 1333 | // constantexpr or a simple constant. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1334 | ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1335 | switch (CE1->getOpcode()) { |
| 1336 | case Instruction::FPTrunc: |
| 1337 | case Instruction::FPExt: |
| 1338 | case Instruction::UIToFP: |
| 1339 | case Instruction::SIToFP: |
| 1340 | // We might be able to do something with these but we don't right now. |
| 1341 | break; |
| 1342 | default: |
| 1343 | break; |
| 1344 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1345 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1346 | // There are MANY other foldings that we could perform here. They will |
| 1347 | // probably be added on demand, as they seem needed. |
| 1348 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 1349 | } |
| 1350 | |
| 1351 | /// evaluateICmpRelation - This function determines if there is anything we can |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1352 | /// 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] | 1353 | /// things like integer comparisons, but should instead handle ConstantExprs |
Chris Lattner | 8410beb | 2006-12-11 02:16:58 +0000 | [diff] [blame] | 1354 | /// and GlobalValues. If we can determine that the two constants have a |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1355 | /// particular relation to each other, we should return the corresponding ICmp |
| 1356 | /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1357 | /// |
| 1358 | /// To simplify this code we canonicalize the relation so that the first |
| 1359 | /// operand is always the most "complex" of the two. We consider simple |
| 1360 | /// constants (like ConstantInt) to be the simplest, followed by |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1361 | /// GlobalValues, followed by ConstantExpr's (the most complex). |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1362 | /// |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1363 | static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1364 | Constant *V1, |
| 1365 | Constant *V2, |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1366 | bool isSigned) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1367 | assert(V1->getType() == V2->getType() && |
| 1368 | "Cannot compare different types of values!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1369 | if (V1 == V2) return ICmpInst::ICMP_EQ; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1370 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1371 | if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) { |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1372 | if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) { |
| 1373 | // We distilled this down to a simple case, use the standard constant |
| 1374 | // folder. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1375 | ConstantInt *R = 0; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1376 | ICmpInst::Predicate pred = ICmpInst::ICMP_EQ; |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1377 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1378 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1379 | return pred; |
| 1380 | pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1381 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1382 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1383 | return pred; |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1384 | pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1385 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1386 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1387 | return pred; |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1388 | |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1389 | // If we couldn't figure it out, bail. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1390 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1391 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1393 | // If the first operand is simple, swap operands. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1394 | ICmpInst::Predicate SwappedRelation = |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1395 | evaluateICmpRelation(Context, V2, V1, isSigned); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1396 | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
| 1397 | return ICmpInst::getSwappedPredicate(SwappedRelation); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1398 | |
Chris Lattner | 0f7e9f5 | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1399 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) { |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1400 | if (isa<ConstantExpr>(V2)) { // Swap as necessary. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1401 | ICmpInst::Predicate SwappedRelation = |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1402 | evaluateICmpRelation(Context, V2, V1, isSigned); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1403 | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
| 1404 | return ICmpInst::getSwappedPredicate(SwappedRelation); |
Chris Lattner | 0f7e9f5 | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1405 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1406 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1407 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1408 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1409 | // Now we know that the RHS is a GlobalValue or simple constant, |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1410 | // which (since the types must match) means that it's a ConstantPointerNull. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1411 | if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 52fe869 | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 1412 | // Don't try to decide equality of aliases. |
| 1413 | if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2)) |
| 1414 | if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage()) |
| 1415 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1416 | } else { |
| 1417 | assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); |
Chris Lattner | 52fe869 | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 1418 | // GlobalVals can never be null. Don't try to evaluate aliases. |
| 1419 | if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1420 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1421 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1422 | } else { |
| 1423 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 1424 | // constantexpr, a CPR, or a simple constant. |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1425 | ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
| 1426 | Constant *CE1Op0 = CE1->getOperand(0); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1427 | |
| 1428 | switch (CE1->getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1429 | case Instruction::Trunc: |
| 1430 | case Instruction::FPTrunc: |
| 1431 | case Instruction::FPExt: |
| 1432 | case Instruction::FPToUI: |
| 1433 | case Instruction::FPToSI: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1434 | break; // We can't evaluate floating point casts or truncations. |
| 1435 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1436 | case Instruction::UIToFP: |
| 1437 | case Instruction::SIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1438 | case Instruction::BitCast: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1439 | case Instruction::ZExt: |
| 1440 | case Instruction::SExt: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1441 | // If the cast is not actually changing bits, and the second operand is a |
| 1442 | // null pointer, do the comparison with the pre-casted value. |
| 1443 | if (V2->isNullValue() && |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1444 | (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) { |
Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 1445 | if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; |
| 1446 | if (CE1->getOpcode() == Instruction::SExt) isSigned = true; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1447 | return evaluateICmpRelation(Context, CE1Op0, |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1448 | Constant::getNullValue(CE1Op0->getType()), |
Nick Lewycky | 2949a23 | 2009-09-20 03:48:46 +0000 | [diff] [blame] | 1449 | isSigned); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1450 | } |
Chris Lattner | 192e326 | 2004-04-11 01:29:30 +0000 | [diff] [blame] | 1451 | break; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1452 | |
| 1453 | case Instruction::GetElementPtr: |
| 1454 | // Ok, since this is a getelementptr, we know that the constant has a |
| 1455 | // pointer type. Check the various cases. |
| 1456 | if (isa<ConstantPointerNull>(V2)) { |
| 1457 | // If we are comparing a GEP to a null pointer, check to see if the base |
| 1458 | // of the GEP equals the null pointer. |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1459 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1460 | if (GV->hasExternalWeakLinkage()) |
| 1461 | // Weak linkage GVals could be zero or not. We're comparing that |
| 1462 | // to null pointer so its greater-or-equal |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1463 | return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1464 | else |
| 1465 | // If its not weak linkage, the GVal must have a non-zero address |
| 1466 | // so the result is greater-than |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1467 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1468 | } else if (isa<ConstantPointerNull>(CE1Op0)) { |
| 1469 | // If we are indexing from a null pointer, check to see if we have any |
| 1470 | // non-zero indices. |
| 1471 | for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) |
| 1472 | if (!CE1->getOperand(i)->isNullValue()) |
| 1473 | // Offsetting from null, must not be equal. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1474 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1475 | // Only zero indexes from null, must still be zero. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1476 | return ICmpInst::ICMP_EQ; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1477 | } |
| 1478 | // 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] | 1479 | } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1480 | if (isa<ConstantPointerNull>(CE1Op0)) { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1481 | if (CPR2->hasExternalWeakLinkage()) |
| 1482 | // Weak linkage GVals could be zero or not. We're comparing it to |
| 1483 | // a null pointer, so its less-or-equal |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1484 | return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1485 | else |
| 1486 | // If its not weak linkage, the GVal must have a non-zero address |
| 1487 | // so the result is less-than |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1488 | return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1489 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1490 | if (CPR1 == CPR2) { |
| 1491 | // If this is a getelementptr of the same global, then it must be |
| 1492 | // different. Because the types must match, the getelementptr could |
| 1493 | // only have at most one index, and because we fold getelementptr's |
| 1494 | // with a single zero index, it must be nonzero. |
| 1495 | assert(CE1->getNumOperands() == 2 && |
| 1496 | !CE1->getOperand(1)->isNullValue() && |
| 1497 | "Suprising getelementptr!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1498 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1499 | } else { |
| 1500 | // If they are different globals, we don't know what the value is, |
| 1501 | // but they can't be equal. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1502 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | } else { |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1506 | ConstantExpr *CE2 = cast<ConstantExpr>(V2); |
| 1507 | Constant *CE2Op0 = CE2->getOperand(0); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1508 | |
| 1509 | // There are MANY other foldings that we could perform here. They will |
| 1510 | // probably be added on demand, as they seem needed. |
| 1511 | switch (CE2->getOpcode()) { |
| 1512 | default: break; |
| 1513 | case Instruction::GetElementPtr: |
| 1514 | // By far the most common case to handle is when the base pointers are |
| 1515 | // obviously to the same or different globals. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1516 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1517 | if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1518 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1519 | // Ok, we know that both getelementptr instructions are based on the |
| 1520 | // same global. From this, we can precisely determine the relative |
| 1521 | // ordering of the resultant pointers. |
| 1522 | unsigned i = 1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1523 | |
Dan Gohman | 7190d48 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 1524 | // The logic below assumes that the result of the comparison |
| 1525 | // can be determined by finding the first index that differs. |
| 1526 | // This doesn't work if there is over-indexing in any |
| 1527 | // subsequent indices, so check for that case first. |
| 1528 | if (!CE1->isGEPWithNoNotionalOverIndexing() || |
| 1529 | !CE2->isGEPWithNoNotionalOverIndexing()) |
| 1530 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
| 1531 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1532 | // Compare all of the operands the GEP's have in common. |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1533 | gep_type_iterator GTI = gep_type_begin(CE1); |
| 1534 | for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); |
| 1535 | ++i, ++GTI) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1536 | switch (IdxCompare(Context, CE1->getOperand(i), |
| 1537 | CE2->getOperand(i), GTI.getIndexedType())) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1538 | case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT; |
| 1539 | case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT; |
| 1540 | case -2: return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | // Ok, we ran out of things they have in common. If any leftovers |
| 1544 | // are non-zero then we have a difference, otherwise we are equal. |
| 1545 | for (; i < CE1->getNumOperands(); ++i) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1546 | if (!CE1->getOperand(i)->isNullValue()) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1547 | if (isa<ConstantInt>(CE1->getOperand(i))) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1548 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1549 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1550 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1551 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1553 | for (; i < CE2->getNumOperands(); ++i) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1554 | if (!CE2->getOperand(i)->isNullValue()) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1555 | if (isa<ConstantInt>(CE2->getOperand(i))) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1556 | return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1557 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1558 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1559 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1560 | return ICmpInst::ICMP_EQ; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1564 | default: |
| 1565 | break; |
| 1566 | } |
| 1567 | } |
| 1568 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1569 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1572 | Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context, |
| 1573 | unsigned short pred, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1574 | Constant *C1, Constant *C2) { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1575 | const Type *ResultTy; |
| 1576 | if (const VectorType *VT = dyn_cast<VectorType>(C1->getType())) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1577 | ResultTy = VectorType::get(Type::getInt1Ty(Context), VT->getNumElements()); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1578 | else |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1579 | ResultTy = Type::getInt1Ty(Context); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1580 | |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1581 | // Fold FCMP_FALSE/FCMP_TRUE unconditionally. |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1582 | if (pred == FCmpInst::FCMP_FALSE) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1583 | return Constant::getNullValue(ResultTy); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (pred == FCmpInst::FCMP_TRUE) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1586 | return Constant::getAllOnesValue(ResultTy); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1587 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1588 | // Handle some degenerate cases first |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1589 | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1590 | return UndefValue::get(ResultTy); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1591 | |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1592 | // No compile-time operations on this type yet. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1593 | if (C1->getType()->isPPC_FP128Ty()) |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1594 | return 0; |
| 1595 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1596 | // icmp eq/ne(null,GV) -> false/true |
| 1597 | if (C1->isNullValue()) { |
| 1598 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2)) |
Duncan Sands | a38e527 | 2007-09-19 10:16:17 +0000 | [diff] [blame] | 1599 | // Don't try to evaluate aliases. External weak GV can be null. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1600 | if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) { |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1601 | if (pred == ICmpInst::ICMP_EQ) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1602 | return ConstantInt::getFalse(Context); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1603 | else if (pred == ICmpInst::ICMP_NE) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1604 | return ConstantInt::getTrue(Context); |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1605 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1606 | // icmp eq/ne(GV,null) -> false/true |
| 1607 | } else if (C2->isNullValue()) { |
| 1608 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) |
Duncan Sands | a38e527 | 2007-09-19 10:16:17 +0000 | [diff] [blame] | 1609 | // Don't try to evaluate aliases. External weak GV can be null. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1610 | if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) { |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1611 | if (pred == ICmpInst::ICMP_EQ) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1612 | return ConstantInt::getFalse(Context); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1613 | else if (pred == ICmpInst::ICMP_NE) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1614 | return ConstantInt::getTrue(Context); |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1615 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Nick Lewycky | b0225ba | 2009-09-20 07:21:39 +0000 | [diff] [blame] | 1618 | // If the comparison is a comparison between two i1's, simplify it. |
Benjamin Kramer | a81a6df | 2010-01-05 20:07:06 +0000 | [diff] [blame] | 1619 | if (C1->getType()->isInteger(1)) { |
Nick Lewycky | b0225ba | 2009-09-20 07:21:39 +0000 | [diff] [blame] | 1620 | switch(pred) { |
| 1621 | case ICmpInst::ICMP_EQ: |
| 1622 | if (isa<ConstantInt>(C2)) |
| 1623 | return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2)); |
| 1624 | return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2); |
| 1625 | case ICmpInst::ICMP_NE: |
| 1626 | return ConstantExpr::getXor(C1, C2); |
| 1627 | default: |
| 1628 | break; |
| 1629 | } |
| 1630 | } |
| 1631 | |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 1632 | if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 1633 | APInt V1 = cast<ConstantInt>(C1)->getValue(); |
| 1634 | APInt V2 = cast<ConstantInt>(C2)->getValue(); |
| 1635 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1636 | default: llvm_unreachable("Invalid ICmp Predicate"); return 0; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1637 | case ICmpInst::ICMP_EQ: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1638 | return ConstantInt::get(Type::getInt1Ty(Context), V1 == V2); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1639 | case ICmpInst::ICMP_NE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1640 | return ConstantInt::get(Type::getInt1Ty(Context), V1 != V2); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1641 | case ICmpInst::ICMP_SLT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1642 | return ConstantInt::get(Type::getInt1Ty(Context), V1.slt(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1643 | case ICmpInst::ICMP_SGT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1644 | return ConstantInt::get(Type::getInt1Ty(Context), V1.sgt(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1645 | case ICmpInst::ICMP_SLE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1646 | return ConstantInt::get(Type::getInt1Ty(Context), V1.sle(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1647 | case ICmpInst::ICMP_SGE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1648 | return ConstantInt::get(Type::getInt1Ty(Context), V1.sge(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1649 | case ICmpInst::ICMP_ULT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1650 | return ConstantInt::get(Type::getInt1Ty(Context), V1.ult(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1651 | case ICmpInst::ICMP_UGT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1652 | return ConstantInt::get(Type::getInt1Ty(Context), V1.ugt(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1653 | case ICmpInst::ICMP_ULE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1654 | return ConstantInt::get(Type::getInt1Ty(Context), V1.ule(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1655 | case ICmpInst::ICMP_UGE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1656 | return ConstantInt::get(Type::getInt1Ty(Context), V1.uge(V2)); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1657 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1658 | } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) { |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1659 | APFloat C1V = cast<ConstantFP>(C1)->getValueAPF(); |
| 1660 | APFloat C2V = cast<ConstantFP>(C2)->getValueAPF(); |
| 1661 | APFloat::cmpResult R = C1V.compare(C2V); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1662 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1663 | default: llvm_unreachable("Invalid FCmp Predicate"); return 0; |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1664 | case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse(Context); |
| 1665 | case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue(Context); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1666 | case FCmpInst::FCMP_UNO: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1667 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1668 | case FCmpInst::FCMP_ORD: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1669 | return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpUnordered); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1670 | case FCmpInst::FCMP_UEQ: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1671 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1672 | R==APFloat::cmpEqual); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1673 | case FCmpInst::FCMP_OEQ: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1674 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpEqual); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1675 | case FCmpInst::FCMP_UNE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1676 | return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpEqual); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1677 | case FCmpInst::FCMP_ONE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1678 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1679 | R==APFloat::cmpGreaterThan); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1680 | case FCmpInst::FCMP_ULT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1681 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1682 | R==APFloat::cmpLessThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1683 | case FCmpInst::FCMP_OLT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1684 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1685 | case FCmpInst::FCMP_UGT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1686 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1687 | R==APFloat::cmpGreaterThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1688 | case FCmpInst::FCMP_OGT: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1689 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1690 | case FCmpInst::FCMP_ULE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1691 | return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpGreaterThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1692 | case FCmpInst::FCMP_OLE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1693 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1694 | R==APFloat::cmpEqual); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1695 | case FCmpInst::FCMP_UGE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1696 | return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpLessThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1697 | case FCmpInst::FCMP_OGE: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1698 | return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1699 | R==APFloat::cmpEqual); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1700 | } |
Chris Lattner | 67136cf | 2008-07-10 00:29:28 +0000 | [diff] [blame] | 1701 | } else if (isa<VectorType>(C1->getType())) { |
| 1702 | SmallVector<Constant*, 16> C1Elts, C2Elts; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1703 | C1->getVectorElements(Context, C1Elts); |
| 1704 | C2->getVectorElements(Context, C2Elts); |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1705 | if (C1Elts.empty() || C2Elts.empty()) |
| 1706 | return 0; |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1707 | |
Chris Lattner | 67136cf | 2008-07-10 00:29:28 +0000 | [diff] [blame] | 1708 | // If we can constant fold the comparison of each element, constant fold |
| 1709 | // the whole vector comparison. |
| 1710 | SmallVector<Constant*, 4> ResElts; |
Chris Lattner | 67136cf | 2008-07-10 00:29:28 +0000 | [diff] [blame] | 1711 | for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) { |
| 1712 | // Compare the elements, producing an i1 result or constant expr. |
Nick Lewycky | 9e26c1c | 2010-01-21 07:03:21 +0000 | [diff] [blame] | 1713 | ResElts.push_back(ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i])); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1714 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1715 | return ConstantVector::get(&ResElts[0], ResElts.size()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1716 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1717 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1718 | if (C1->getType()->isFloatingPoint()) { |
Chris Lattner | 350e417 | 2008-07-08 18:47:38 +0000 | [diff] [blame] | 1719 | int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1720 | switch (evaluateFCmpRelation(Context, C1, C2)) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1721 | default: llvm_unreachable("Unknown relation!"); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1722 | case FCmpInst::FCMP_UNO: |
| 1723 | case FCmpInst::FCMP_ORD: |
| 1724 | case FCmpInst::FCMP_UEQ: |
| 1725 | case FCmpInst::FCMP_UNE: |
| 1726 | case FCmpInst::FCMP_ULT: |
| 1727 | case FCmpInst::FCMP_UGT: |
| 1728 | case FCmpInst::FCMP_ULE: |
| 1729 | case FCmpInst::FCMP_UGE: |
| 1730 | case FCmpInst::FCMP_TRUE: |
| 1731 | case FCmpInst::FCMP_FALSE: |
| 1732 | case FCmpInst::BAD_FCMP_PREDICATE: |
| 1733 | break; // Couldn't determine anything about these constants. |
| 1734 | case FCmpInst::FCMP_OEQ: // We know that C1 == C2 |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1735 | Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ || |
| 1736 | pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE || |
| 1737 | pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); |
| 1738 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1739 | case FCmpInst::FCMP_OLT: // We know that C1 < C2 |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1740 | Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || |
| 1741 | pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT || |
| 1742 | pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE); |
| 1743 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1744 | case FCmpInst::FCMP_OGT: // We know that C1 > C2 |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1745 | Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || |
| 1746 | pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT || |
| 1747 | pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); |
| 1748 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1749 | case FCmpInst::FCMP_OLE: // We know that C1 <= C2 |
| 1750 | // We can only partially decide this relation. |
| 1751 | if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1752 | Result = 0; |
| 1753 | else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) |
| 1754 | Result = 1; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1755 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1756 | case FCmpInst::FCMP_OGE: // We known that C1 >= C2 |
| 1757 | // We can only partially decide this relation. |
| 1758 | if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1759 | Result = 0; |
| 1760 | else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) |
| 1761 | Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1762 | break; |
| 1763 | case ICmpInst::ICMP_NE: // We know that C1 != C2 |
| 1764 | // We can only partially decide this relation. |
| 1765 | if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1766 | Result = 0; |
| 1767 | else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) |
| 1768 | Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1771 | |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1772 | // If we evaluated the result, return it now. |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1773 | if (Result != -1) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1774 | return ConstantInt::get(Type::getInt1Ty(Context), Result); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1775 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1776 | } else { |
| 1777 | // Evaluate the relation between the two constants, per the predicate. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1778 | int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1779 | switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1780 | default: llvm_unreachable("Unknown relational!"); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1781 | case ICmpInst::BAD_ICMP_PREDICATE: |
| 1782 | break; // Couldn't determine anything about these constants. |
| 1783 | case ICmpInst::ICMP_EQ: // We know the constants are equal! |
| 1784 | // If we know the constants are equal, we can decide the result of this |
| 1785 | // computation precisely. |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1786 | Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred); |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1787 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1788 | case ICmpInst::ICMP_ULT: |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1789 | switch (pred) { |
| 1790 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1791 | Result = 1; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1792 | case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1793 | Result = 0; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1794 | } |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1795 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1796 | case ICmpInst::ICMP_SLT: |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1797 | switch (pred) { |
| 1798 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1799 | Result = 1; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1800 | case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1801 | Result = 0; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1802 | } |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1803 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1804 | case ICmpInst::ICMP_UGT: |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1805 | switch (pred) { |
| 1806 | case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1807 | Result = 1; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1808 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1809 | Result = 0; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1810 | } |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1811 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1812 | case ICmpInst::ICMP_SGT: |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1813 | switch (pred) { |
| 1814 | case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1815 | Result = 1; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1816 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE: |
Nick Lewycky | 2b31b53 | 2009-09-20 05:47:45 +0000 | [diff] [blame] | 1817 | Result = 0; break; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1818 | } |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1819 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1820 | case ICmpInst::ICMP_ULE: |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1821 | if (pred == ICmpInst::ICMP_UGT) Result = 0; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1822 | if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1823 | break; |
| 1824 | case ICmpInst::ICMP_SLE: |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1825 | if (pred == ICmpInst::ICMP_SGT) Result = 0; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1826 | if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1827 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1828 | case ICmpInst::ICMP_UGE: |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1829 | if (pred == ICmpInst::ICMP_ULT) Result = 0; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1830 | if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | case ICmpInst::ICMP_SGE: |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1833 | if (pred == ICmpInst::ICMP_SLT) Result = 0; |
Nick Lewycky | 9e08554 | 2009-09-20 04:27:06 +0000 | [diff] [blame] | 1834 | if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1835 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1836 | case ICmpInst::ICMP_NE: |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1837 | if (pred == ICmpInst::ICMP_EQ) Result = 0; |
| 1838 | if (pred == ICmpInst::ICMP_NE) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1839 | break; |
| 1840 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1842 | // If we evaluated the result, return it now. |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1843 | if (Result != -1) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1844 | return ConstantInt::get(Type::getInt1Ty(Context), Result); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1845 | |
Nick Lewycky | 4a03452 | 2009-09-20 05:48:50 +0000 | [diff] [blame] | 1846 | // If the right hand side is a bitcast, try using its inverse to simplify |
| 1847 | // it by moving it to the left hand side. |
| 1848 | if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) { |
| 1849 | if (CE2->getOpcode() == Instruction::BitCast) { |
| 1850 | Constant *CE2Op0 = CE2->getOperand(0); |
| 1851 | Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType()); |
| 1852 | return ConstantExpr::getICmp(pred, Inverse, CE2Op0); |
| 1853 | } |
| 1854 | } |
| 1855 | |
Nick Lewycky | 9b3ed87 | 2009-09-20 07:31:25 +0000 | [diff] [blame] | 1856 | // If the left hand side is an extension, try eliminating it. |
| 1857 | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { |
| 1858 | if (CE1->getOpcode() == Instruction::SExt || |
| 1859 | CE1->getOpcode() == Instruction::ZExt) { |
| 1860 | Constant *CE1Op0 = CE1->getOperand(0); |
| 1861 | Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType()); |
| 1862 | if (CE1Inverse == CE1Op0) { |
| 1863 | // Check whether we can safely truncate the right hand side. |
| 1864 | Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType()); |
| 1865 | if (ConstantExpr::getZExt(C2Inverse, C2->getType()) == C2) { |
| 1866 | return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse); |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | |
Eli Friedman | e67cae3 | 2009-12-17 06:07:04 +0000 | [diff] [blame] | 1872 | if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) || |
| 1873 | (C1->isNullValue() && !C2->isNullValue())) { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1874 | // If C2 is a constant expr and C1 isn't, flip them around and fold the |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1875 | // other way if possible. |
Eli Friedman | e67cae3 | 2009-12-17 06:07:04 +0000 | [diff] [blame] | 1876 | // Also, if C1 is null and C2 isn't, flip them around. |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1877 | switch (pred) { |
| 1878 | case ICmpInst::ICMP_EQ: |
| 1879 | case ICmpInst::ICMP_NE: |
| 1880 | // No change of predicate required. |
Eli Friedman | e67cae3 | 2009-12-17 06:07:04 +0000 | [diff] [blame] | 1881 | return ConstantExpr::getICmp(pred, C2, C1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1882 | |
| 1883 | case ICmpInst::ICMP_ULT: |
| 1884 | case ICmpInst::ICMP_SLT: |
| 1885 | case ICmpInst::ICMP_UGT: |
| 1886 | case ICmpInst::ICMP_SGT: |
| 1887 | case ICmpInst::ICMP_ULE: |
| 1888 | case ICmpInst::ICMP_SLE: |
| 1889 | case ICmpInst::ICMP_UGE: |
| 1890 | case ICmpInst::ICMP_SGE: |
| 1891 | // Change the predicate as necessary to swap the operands. |
| 1892 | pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); |
Eli Friedman | e67cae3 | 2009-12-17 06:07:04 +0000 | [diff] [blame] | 1893 | return ConstantExpr::getICmp(pred, C2, C1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1894 | |
| 1895 | default: // These predicates cannot be flopped around. |
| 1896 | break; |
| 1897 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | return 0; |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 1901 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1902 | |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 1903 | /// isInBoundsIndices - Test whether the given sequence of *normalized* indices |
| 1904 | /// is "inbounds". |
| 1905 | static bool isInBoundsIndices(Constant *const *Idxs, size_t NumIdx) { |
| 1906 | // No indices means nothing that could be out of bounds. |
| 1907 | if (NumIdx == 0) return true; |
| 1908 | |
| 1909 | // If the first index is zero, it's in bounds. |
| 1910 | if (Idxs[0]->isNullValue()) return true; |
| 1911 | |
| 1912 | // If the first index is one and all the rest are zero, it's in bounds, |
| 1913 | // by the one-past-the-end rule. |
| 1914 | if (!cast<ConstantInt>(Idxs[0])->isOne()) |
| 1915 | return false; |
| 1916 | for (unsigned i = 1, e = NumIdx; i != e; ++i) |
| 1917 | if (!Idxs[i]->isNullValue()) |
| 1918 | return false; |
| 1919 | return true; |
| 1920 | } |
| 1921 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1922 | Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context, |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1923 | Constant *C, |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 1924 | bool inBounds, |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1925 | Constant* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1926 | unsigned NumIdx) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1927 | if (NumIdx == 0 || |
| 1928 | (NumIdx == 1 && Idxs[0]->isNullValue())) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1929 | return C; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1930 | |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1931 | if (isa<UndefValue>(C)) { |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1932 | const PointerType *Ptr = cast<PointerType>(C->getType()); |
| 1933 | const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1934 | (Value **)Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1935 | (Value **)Idxs+NumIdx); |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1936 | assert(Ty != 0 && "Invalid indices for GEP!"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1937 | return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1940 | Constant *Idx0 = Idxs[0]; |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1941 | if (C->isNullValue()) { |
| 1942 | bool isNull = true; |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1943 | for (unsigned i = 0, e = NumIdx; i != e; ++i) |
| 1944 | if (!Idxs[i]->isNullValue()) { |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1945 | isNull = false; |
| 1946 | break; |
| 1947 | } |
| 1948 | if (isNull) { |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1949 | const PointerType *Ptr = cast<PointerType>(C->getType()); |
| 1950 | const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1951 | (Value**)Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1952 | (Value**)Idxs+NumIdx); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1953 | assert(Ty != 0 && "Invalid indices for GEP!"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1954 | return ConstantPointerNull::get( |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1955 | PointerType::get(Ty,Ptr->getAddressSpace())); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1958 | |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 1959 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1960 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 1961 | // is a getelementptr instruction, combine the indices of the two |
| 1962 | // getelementptr instructions into a single instruction. |
| 1963 | // |
| 1964 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1965 | const Type *LastTy = 0; |
| 1966 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 1967 | I != E; ++I) |
| 1968 | LastTy = *I; |
| 1969 | |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1970 | if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1971 | SmallVector<Value*, 16> NewIndices; |
| 1972 | NewIndices.reserve(NumIdx + CE->getNumOperands()); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1973 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1974 | NewIndices.push_back(CE->getOperand(i)); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Add the last index of the source with the first index of the new GEP. |
| 1977 | // Make sure to handle the case when they are actually different types. |
| 1978 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1979 | // Otherwise it must be an array. |
| 1980 | if (!Idx0->isNullValue()) { |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1981 | const Type *IdxTy = Combined->getType(); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1982 | if (IdxTy != Idx0->getType()) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1983 | Constant *C1 = |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1984 | ConstantExpr::getSExtOrBitCast(Idx0, Type::getInt64Ty(Context)); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1985 | Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1986 | Type::getInt64Ty(Context)); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1987 | Combined = ConstantExpr::get(Instruction::Add, C1, C2); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1988 | } else { |
| 1989 | Combined = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1990 | ConstantExpr::get(Instruction::Add, Idx0, Combined); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1991 | } |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1992 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1993 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1994 | NewIndices.push_back(Combined); |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1995 | NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx); |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 1996 | return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ? |
| 1997 | ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0), |
| 1998 | &NewIndices[0], |
| 1999 | NewIndices.size()) : |
| 2000 | ConstantExpr::getGetElementPtr(CE->getOperand(0), |
| 2001 | &NewIndices[0], |
| 2002 | NewIndices.size()); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | // Implement folding of: |
| 2007 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 2008 | // long 0, long 0) |
| 2009 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 2010 | // |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2011 | if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2012 | if (const PointerType *SPT = |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 2013 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 2014 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 2015 | if (const ArrayType *CAT = |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 2016 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 2017 | if (CAT->getElementType() == SAT->getElementType()) |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 2018 | return inBounds ? |
| 2019 | ConstantExpr::getInBoundsGetElementPtr( |
| 2020 | (Constant*)CE->getOperand(0), Idxs, NumIdx) : |
| 2021 | ConstantExpr::getGetElementPtr( |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 2022 | (Constant*)CE->getOperand(0), Idxs, NumIdx); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2023 | } |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 2024 | |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2025 | // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1) |
| 2026 | // Into: inttoptr (i64 0 to i8*) |
| 2027 | // This happens with pointers to member functions in C++. |
| 2028 | if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 && |
| 2029 | isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) && |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 2030 | cast<PointerType>(CE->getType())->getElementType() == |
| 2031 | Type::getInt8Ty(Context)) { |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2032 | Constant *Base = CE->getOperand(0); |
| 2033 | Constant *Offset = Idxs[0]; |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2035 | // Convert the smaller integer to the larger type. |
| 2036 | if (Offset->getType()->getPrimitiveSizeInBits() < |
| 2037 | Base->getType()->getPrimitiveSizeInBits()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2038 | Offset = ConstantExpr::getSExt(Offset, Base->getType()); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2039 | else if (Base->getType()->getPrimitiveSizeInBits() < |
| 2040 | Offset->getType()->getPrimitiveSizeInBits()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2041 | Base = ConstantExpr::getZExt(Base, Offset->getType()); |
Dan Gohman | 062d378 | 2009-08-29 23:35:16 +0000 | [diff] [blame] | 2042 | |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2043 | Base = ConstantExpr::getAdd(Base, Offset); |
| 2044 | return ConstantExpr::getIntToPtr(Base, CE->getType()); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 2045 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 2046 | } |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 2047 | |
| 2048 | // Check to see if any array indices are not within the corresponding |
| 2049 | // notional array bounds. If so, try to determine if they can be factored |
| 2050 | // out into preceding dimensions. |
| 2051 | bool Unknown = false; |
| 2052 | SmallVector<Constant *, 8> NewIdxs; |
| 2053 | const Type *Ty = C->getType(); |
| 2054 | const Type *Prev = 0; |
| 2055 | for (unsigned i = 0; i != NumIdx; |
| 2056 | Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) { |
| 2057 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) { |
| 2058 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) |
| 2059 | if (ATy->getNumElements() <= INT64_MAX && |
| 2060 | ATy->getNumElements() != 0 && |
| 2061 | CI->getSExtValue() >= (int64_t)ATy->getNumElements()) { |
| 2062 | if (isa<SequentialType>(Prev)) { |
| 2063 | // It's out of range, but we can factor it into the prior |
| 2064 | // dimension. |
| 2065 | NewIdxs.resize(NumIdx); |
| 2066 | ConstantInt *Factor = ConstantInt::get(CI->getType(), |
| 2067 | ATy->getNumElements()); |
| 2068 | NewIdxs[i] = ConstantExpr::getSRem(CI, Factor); |
| 2069 | |
| 2070 | Constant *PrevIdx = Idxs[i-1]; |
| 2071 | Constant *Div = ConstantExpr::getSDiv(CI, Factor); |
| 2072 | |
| 2073 | // Before adding, extend both operands to i64 to avoid |
| 2074 | // overflow trouble. |
Benjamin Kramer | d2564e3 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 2075 | if (!PrevIdx->getType()->isInteger(64)) |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 2076 | PrevIdx = ConstantExpr::getSExt(PrevIdx, |
| 2077 | Type::getInt64Ty(Context)); |
Benjamin Kramer | d2564e3 | 2010-01-05 21:05:54 +0000 | [diff] [blame] | 2078 | if (!Div->getType()->isInteger(64)) |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 2079 | Div = ConstantExpr::getSExt(Div, |
| 2080 | Type::getInt64Ty(Context)); |
| 2081 | |
| 2082 | NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div); |
| 2083 | } else { |
| 2084 | // It's out of range, but the prior dimension is a struct |
| 2085 | // so we can't do anything about it. |
| 2086 | Unknown = true; |
| 2087 | } |
| 2088 | } |
| 2089 | } else { |
| 2090 | // We don't know if it's in range or not. |
| 2091 | Unknown = true; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | // If we did any factoring, start over with the adjusted indices. |
| 2096 | if (!NewIdxs.empty()) { |
| 2097 | for (unsigned i = 0; i != NumIdx; ++i) |
| 2098 | if (!NewIdxs[i]) NewIdxs[i] = Idxs[i]; |
| 2099 | return inBounds ? |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 2100 | ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(), |
| 2101 | NewIdxs.size()) : |
| 2102 | ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size()); |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | // If all indices are known integers and normalized, we can do a simple |
| 2106 | // check for the "inbounds" property. |
| 2107 | if (!Unknown && !inBounds && |
| 2108 | isa<GlobalVariable>(C) && isInBoundsIndices(Idxs, NumIdx)) |
Nick Lewycky | e033298 | 2009-09-20 01:35:59 +0000 | [diff] [blame] | 2109 | return ConstantExpr::getInBoundsGetElementPtr(C, Idxs, NumIdx); |
Dan Gohman | 21c6216 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 2110 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 2111 | return 0; |
| 2112 | } |