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