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 |
| 15 | // template-based folder for simple primitive constants like ConstantInt, and |
| 16 | // the special case hackery that we use to symbolically evaluate expressions |
| 17 | // that use ConstantExprs. |
| 18 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | 33e93b8 | 2007-02-27 03:05:06 +0000 | [diff] [blame] | 21 | #include "ConstantFold.h" |
Chris Lattner | 6ff6cea | 2004-01-12 21:02:29 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | a9eddae | 2004-02-22 06:25:38 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Chris Lattner | 1f0049c | 2003-04-17 19:24:18 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ea0789c | 2004-03-08 06:17:35 +0000 | [diff] [blame] | 25 | #include "llvm/Function.h" |
Chris Lattner | 52fe869 | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 26 | #include "llvm/GlobalAlias.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; |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +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 |
| 76 | const ConstantExpr*Op, ///< the first cast constant expression |
| 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"); |
| 82 | |
| 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, |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 91 | Type::Int64Ty); |
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 |
| 99 | |
| 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; |
Dan Gohman | 7673374 | 2009-08-12 00:32:55 +0000 | [diff] [blame^] | 106 | Value *Zero = Constant::getNullValue(Type::Int32Ty); |
| 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 | } |
Nate Begeman | f2b0b0e | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 123 | |
| 124 | if (ElTy == DPTy->getElementType()) |
Dan Gohman | 7673374 | 2009-08-12 00:32:55 +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 | } |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 129 | |
| 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); |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +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 | } |
| 152 | |
| 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)); |
Chris Lattner | e8ea037 | 2007-12-11 05:55:02 +0000 | [diff] [blame] | 157 | |
| 158 | // Handle integral constant input. |
| 159 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 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 | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 167 | DestTy != Type::PPC_FP128Ty)); |
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. |
Duncan Sands | e7d5479 | 2009-02-04 11:17:06 +0000 | [diff] [blame] | 174 | if (const 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 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 182 | Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context, |
| 183 | unsigned opc, const Constant *V, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 184 | const Type *DestTy) { |
Chris Lattner | 363485d | 2007-07-20 22:09:02 +0000 | [diff] [blame] | 185 | if (isa<UndefValue>(V)) { |
| 186 | // zext(undef) = 0, because the top bits will be zero. |
| 187 | // sext(undef) = 0, because the top bits will all be the same. |
Chris Lattner | b4c6cc9 | 2008-02-19 06:22:12 +0000 | [diff] [blame] | 188 | // [us]itofp(undef) = 0, because the result value is bounded. |
| 189 | if (opc == Instruction::ZExt || opc == Instruction::SExt || |
| 190 | opc == Instruction::UIToFP || opc == Instruction::SIToFP) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 191 | return Constant::getNullValue(DestTy); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 192 | return UndefValue::get(DestTy); |
Chris Lattner | 363485d | 2007-07-20 22:09:02 +0000 | [diff] [blame] | 193 | } |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 194 | // No compile-time operations on this type yet. |
| 195 | if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty) |
| 196 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 197 | |
| 198 | // If the cast operand is a constant expression, there's a few things we can |
| 199 | // do to try to simplify it. |
| 200 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 201 | if (CE->isCast()) { |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 202 | // Try hard to fold cast of cast because they are often eliminable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 203 | if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 204 | return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 205 | } else if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 206 | // If all of the indexes in the GEP are null values, there is no pointer |
| 207 | // adjustment going on. We might as well cast the source pointer. |
| 208 | bool isAllNull = true; |
| 209 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) |
| 210 | if (!CE->getOperand(i)->isNullValue()) { |
| 211 | isAllNull = false; |
| 212 | break; |
| 213 | } |
| 214 | if (isAllNull) |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 215 | // This is casting one pointer type to another, always BitCast |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 216 | return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 217 | } |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 219 | |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 220 | // If the cast operand is a constant vector, perform the cast by |
| 221 | // operating on each element. In the cast of bitcasts, the element |
| 222 | // count may be mismatched; don't attempt to handle that here. |
| 223 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 224 | if (isa<VectorType>(DestTy) && |
| 225 | cast<VectorType>(DestTy)->getNumElements() == |
| 226 | CV->getType()->getNumElements()) { |
| 227 | std::vector<Constant*> res; |
| 228 | const VectorType *DestVecTy = cast<VectorType>(DestTy); |
| 229 | const Type *DstEltTy = DestVecTy->getElementType(); |
| 230 | for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 231 | res.push_back(ConstantExpr::getCast(opc, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 232 | CV->getOperand(i), DstEltTy)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 233 | return ConstantVector::get(DestVecTy, res); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 236 | // We actually have to do a cast now. Perform the cast according to the |
| 237 | // opcode specified. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 238 | switch (opc) { |
| 239 | case Instruction::FPTrunc: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 240 | case Instruction::FPExt: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 241 | if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 242 | bool ignored; |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 243 | APFloat Val = FPC->getValueAPF(); |
| 244 | Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle : |
| 245 | DestTy == Type::DoubleTy ? APFloat::IEEEdouble : |
| 246 | DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended : |
| 247 | DestTy == Type::FP128Ty ? APFloat::IEEEquad : |
| 248 | APFloat::Bogus, |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 249 | APFloat::rmNearestTiesToEven, &ignored); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 250 | return ConstantFP::get(Context, Val); |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 251 | } |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 252 | return 0; // Can't fold. |
| 253 | case Instruction::FPToUI: |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 254 | case Instruction::FPToSI: |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 255 | if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
Chris Lattner | 2b827fd | 2007-10-15 05:34:10 +0000 | [diff] [blame] | 256 | const APFloat &V = FPC->getValueAPF(); |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 257 | bool ignored; |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 258 | uint64_t x[2]; |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 259 | uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
Dale Johannesen | 17663f4 | 2007-09-25 23:32:20 +0000 | [diff] [blame] | 260 | (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, |
Dale Johannesen | 4f0bd68 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 261 | APFloat::rmTowardZero, &ignored); |
Dale Johannesen | f4bad97 | 2007-09-19 14:22:58 +0000 | [diff] [blame] | 262 | APInt Val(DestBitWidth, 2, x); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 263 | return ConstantInt::get(Context, Val); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 264 | } |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 265 | return 0; // Can't fold. |
| 266 | case Instruction::IntToPtr: //always treated as unsigned |
| 267 | if (V->isNullValue()) // Is it an integral null value? |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 268 | return ConstantPointerNull::get(cast<PointerType>(DestTy)); |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 269 | return 0; // Other pointer types cannot be casted |
| 270 | case Instruction::PtrToInt: // always treated as unsigned |
| 271 | if (V->isNullValue()) // is it a null pointer value? |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 272 | return ConstantInt::get(DestTy, 0); |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 273 | return 0; // Other pointer types cannot be casted |
| 274 | case Instruction::UIToFP: |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 275 | case Instruction::SIToFP: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 276 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Dale Johannesen | 9150652 | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 277 | APInt api = CI->getValue(); |
| 278 | const uint64_t zero[] = {0, 0}; |
Dale Johannesen | 9150652 | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 279 | APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(), |
| 280 | 2, zero)); |
Dan Gohman | 06c45d5 | 2008-02-29 01:42:52 +0000 | [diff] [blame] | 281 | (void)apf.convertFromAPInt(api, |
| 282 | opc==Instruction::SIToFP, |
| 283 | APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 284 | return ConstantFP::get(Context, apf); |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 285 | } |
Reid Spencer | 8dabca4 | 2006-12-19 07:41:40 +0000 | [diff] [blame] | 286 | return 0; |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 287 | case Instruction::ZExt: |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 288 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 289 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 290 | APInt Result(CI->getValue()); |
| 291 | Result.zext(BitWidth); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 292 | return ConstantInt::get(Context, Result); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 293 | } |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 294 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 295 | case Instruction::SExt: |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 296 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 297 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 298 | APInt Result(CI->getValue()); |
| 299 | Result.sext(BitWidth); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 300 | return ConstantInt::get(Context, Result); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 301 | } |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 302 | return 0; |
Chris Lattner | 710ebaf | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 303 | case Instruction::Trunc: |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 304 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 305 | uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); |
| 306 | APInt Result(CI->getValue()); |
| 307 | Result.trunc(BitWidth); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 308 | return ConstantInt::get(Context, Result); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 710ebaf | 2006-12-01 19:22:41 +0000 | [diff] [blame] | 310 | return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 311 | case Instruction::BitCast: |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 312 | return FoldBitCast(Context, const_cast<Constant*>(V), DestTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 313 | default: |
| 314 | assert(!"Invalid CE CastInst opcode"); |
| 315 | break; |
Chris Lattner | 6b3f475 | 2006-04-02 01:38:28 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | b2b7f90 | 2004-10-11 03:57:30 +0000 | [diff] [blame] | 317 | |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 318 | llvm_unreachable("Failed to cast constant expression"); |
Reid Spencer | f5fc34a | 2006-12-19 03:15:47 +0000 | [diff] [blame] | 319 | return 0; |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 322 | Constant *llvm::ConstantFoldSelectInstruction(LLVMContext&, |
| 323 | const Constant *Cond, |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 324 | const Constant *V1, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 325 | const Constant *V2) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 326 | if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond)) |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 327 | return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2); |
Chris Lattner | fd7bf72 | 2004-10-16 23:31:32 +0000 | [diff] [blame] | 328 | |
| 329 | if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2); |
| 330 | if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1); |
| 331 | if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1); |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 332 | if (V1 == V2) return const_cast<Constant*>(V1); |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 336 | Constant *llvm::ConstantFoldExtractElementInstruction(LLVMContext &Context, |
| 337 | const Constant *Val, |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 338 | const Constant *Idx) { |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 339 | if (isa<UndefValue>(Val)) // ee(undef, x) -> undef |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 340 | return UndefValue::get(cast<VectorType>(Val->getType())->getElementType()); |
Chris Lattner | e4f9d7b | 2006-04-07 04:44:06 +0000 | [diff] [blame] | 341 | if (Val->isNullValue()) // ee(zero, x) -> zero |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 342 | return Constant::getNullValue( |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 343 | cast<VectorType>(Val->getType())->getElementType()); |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 344 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 345 | if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 346 | if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 347 | return CVal->getOperand(CIdx->getZExtValue()); |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 348 | } else if (isa<UndefValue>(Idx)) { |
| 349 | // ee({w,x,y,z}, undef) -> w (an arbitrary value). |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 350 | return CVal->getOperand(0); |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | e52f29b | 2006-03-31 18:31:40 +0000 | [diff] [blame] | 352 | } |
Robert Bocchino | de7f1c9 | 2006-01-10 20:03:46 +0000 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 356 | Constant *llvm::ConstantFoldInsertElementInstruction(LLVMContext &Context, |
| 357 | const Constant *Val, |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 358 | const Constant *Elt, |
| 359 | const Constant *Idx) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 360 | const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 361 | if (!CIdx) return 0; |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 362 | APInt idxVal = CIdx->getValue(); |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 363 | if (isa<UndefValue>(Val)) { |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 364 | // Insertion of scalar constant into vector undef |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 365 | // Optimize away insertion of undef |
| 366 | if (isa<UndefValue>(Elt)) |
| 367 | return const_cast<Constant*>(Val); |
| 368 | // Otherwise break the aggregate undef into multiple undefs and do |
| 369 | // the insertion |
| 370 | unsigned numOps = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 371 | cast<VectorType>(Val->getType())->getNumElements(); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 372 | std::vector<Constant*> Ops; |
| 373 | Ops.reserve(numOps); |
| 374 | for (unsigned i = 0; i < numOps; ++i) { |
| 375 | const Constant *Op = |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 376 | (idxVal == i) ? Elt : UndefValue::get(Elt->getType()); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 377 | Ops.push_back(const_cast<Constant*>(Op)); |
| 378 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 379 | return ConstantVector::get(Ops); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 380 | } |
Reid Spencer | 3054b14 | 2006-11-02 08:18:15 +0000 | [diff] [blame] | 381 | if (isa<ConstantAggregateZero>(Val)) { |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 382 | // Insertion of scalar constant into vector aggregate zero |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 383 | // Optimize away insertion of zero |
| 384 | if (Elt->isNullValue()) |
| 385 | return const_cast<Constant*>(Val); |
| 386 | // Otherwise break the aggregate zero into multiple zeros and do |
| 387 | // the insertion |
| 388 | unsigned numOps = |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 389 | cast<VectorType>(Val->getType())->getNumElements(); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 390 | std::vector<Constant*> Ops; |
| 391 | Ops.reserve(numOps); |
| 392 | for (unsigned i = 0; i < numOps; ++i) { |
| 393 | const Constant *Op = |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 394 | (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType()); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 395 | Ops.push_back(const_cast<Constant*>(Op)); |
| 396 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 397 | return ConstantVector::get(Ops); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 398 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 399 | if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 400 | // Insertion of scalar constant into vector constant |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 401 | std::vector<Constant*> Ops; |
| 402 | Ops.reserve(CVal->getNumOperands()); |
| 403 | for (unsigned i = 0; i < CVal->getNumOperands(); ++i) { |
| 404 | const Constant *Op = |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 405 | (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i)); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 406 | Ops.push_back(const_cast<Constant*>(Op)); |
| 407 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 408 | return ConstantVector::get(Ops); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 409 | } |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 410 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 414 | /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef |
| 415 | /// return the specified element value. Otherwise return null. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 416 | static Constant *GetVectorElement(LLVMContext &Context, const Constant *C, |
| 417 | unsigned EltNo) { |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 418 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 419 | return CV->getOperand(EltNo); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 420 | |
| 421 | const Type *EltTy = cast<VectorType>(C->getType())->getElementType(); |
| 422 | if (isa<ConstantAggregateZero>(C)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 423 | return Constant::getNullValue(EltTy); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 424 | if (isa<UndefValue>(C)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 425 | return UndefValue::get(EltTy); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 429 | Constant *llvm::ConstantFoldShuffleVectorInstruction(LLVMContext &Context, |
| 430 | const Constant *V1, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 431 | const Constant *V2, |
| 432 | const Constant *Mask) { |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 433 | // Undefined shuffle mask -> undefined value. |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 434 | if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType()); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 435 | |
| 436 | unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements(); |
| 437 | unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements(); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 438 | const Type *EltTy = cast<VectorType>(V1->getType())->getElementType(); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 440 | // Loop over the shuffle mask, evaluating each element. |
| 441 | SmallVector<Constant*, 32> Result; |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 442 | for (unsigned i = 0; i != MaskNumElts; ++i) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 443 | Constant *InElt = GetVectorElement(Context, Mask, i); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 444 | if (InElt == 0) return 0; |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 446 | if (isa<UndefValue>(InElt)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 447 | InElt = UndefValue::get(EltTy); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 448 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) { |
| 449 | unsigned Elt = CI->getZExtValue(); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 450 | if (Elt >= SrcNumElts*2) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 451 | InElt = UndefValue::get(EltTy); |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 452 | else if (Elt >= SrcNumElts) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 453 | InElt = GetVectorElement(Context, V2, Elt - SrcNumElts); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 454 | else |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 455 | InElt = GetVectorElement(Context, V1, Elt); |
Chris Lattner | 8e6a8f9 | 2007-12-11 07:49:37 +0000 | [diff] [blame] | 456 | if (InElt == 0) return 0; |
| 457 | } else { |
| 458 | // Unknown value. |
| 459 | return 0; |
| 460 | } |
| 461 | Result.push_back(InElt); |
| 462 | } |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 463 | |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 464 | return ConstantVector::get(&Result[0], Result.size()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 467 | Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context, |
| 468 | const Constant *Agg, |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 469 | const unsigned *Idxs, |
| 470 | unsigned NumIdx) { |
| 471 | // Base case: no indices, so return the entire value. |
| 472 | if (NumIdx == 0) |
| 473 | return const_cast<Constant *>(Agg); |
| 474 | |
| 475 | if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 476 | return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 477 | Idxs, |
| 478 | Idxs + NumIdx)); |
| 479 | |
| 480 | if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0 |
| 481 | return |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 482 | Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 483 | Idxs, |
| 484 | Idxs + NumIdx)); |
| 485 | |
| 486 | // Otherwise recurse. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 487 | return ConstantFoldExtractValueInstruction(Context, Agg->getOperand(*Idxs), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 488 | Idxs+1, NumIdx-1); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 491 | Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context, |
| 492 | const Constant *Agg, |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 493 | const Constant *Val, |
| 494 | const unsigned *Idxs, |
| 495 | unsigned NumIdx) { |
| 496 | // Base case: no indices, so replace the entire value. |
| 497 | if (NumIdx == 0) |
| 498 | return const_cast<Constant *>(Val); |
| 499 | |
| 500 | if (isa<UndefValue>(Agg)) { |
| 501 | // Insertion of constant into aggregate undef |
| 502 | // Optimize away insertion of undef |
| 503 | if (isa<UndefValue>(Val)) |
| 504 | return const_cast<Constant*>(Agg); |
| 505 | // Otherwise break the aggregate undef into multiple undefs and do |
| 506 | // the insertion |
| 507 | const CompositeType *AggTy = cast<CompositeType>(Agg->getType()); |
| 508 | unsigned numOps; |
| 509 | if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy)) |
| 510 | numOps = AR->getNumElements(); |
| 511 | else |
| 512 | numOps = cast<StructType>(AggTy)->getNumElements(); |
| 513 | std::vector<Constant*> Ops(numOps); |
| 514 | for (unsigned i = 0; i < numOps; ++i) { |
| 515 | const Type *MemberTy = AggTy->getTypeAtIndex(i); |
| 516 | const Constant *Op = |
| 517 | (*Idxs == i) ? |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 518 | ConstantFoldInsertValueInstruction(Context, UndefValue::get(MemberTy), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 519 | Val, Idxs+1, NumIdx-1) : |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 520 | UndefValue::get(MemberTy); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 521 | Ops[i] = const_cast<Constant*>(Op); |
| 522 | } |
| 523 | if (isa<StructType>(AggTy)) |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 524 | return ConstantStruct::get(Context, Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 525 | else |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 526 | return ConstantArray::get(cast<ArrayType>(AggTy), Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 527 | } |
| 528 | if (isa<ConstantAggregateZero>(Agg)) { |
| 529 | // Insertion of constant into aggregate zero |
| 530 | // Optimize away insertion of zero |
| 531 | if (Val->isNullValue()) |
| 532 | return const_cast<Constant*>(Agg); |
| 533 | // Otherwise break the aggregate zero into multiple zeros and do |
| 534 | // the insertion |
| 535 | const CompositeType *AggTy = cast<CompositeType>(Agg->getType()); |
| 536 | unsigned numOps; |
| 537 | if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy)) |
| 538 | numOps = AR->getNumElements(); |
| 539 | else |
| 540 | numOps = cast<StructType>(AggTy)->getNumElements(); |
| 541 | std::vector<Constant*> Ops(numOps); |
| 542 | for (unsigned i = 0; i < numOps; ++i) { |
| 543 | const Type *MemberTy = AggTy->getTypeAtIndex(i); |
| 544 | const Constant *Op = |
| 545 | (*Idxs == i) ? |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 546 | ConstantFoldInsertValueInstruction(Context, |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 547 | Constant::getNullValue(MemberTy), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 548 | Val, Idxs+1, NumIdx-1) : |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 549 | Constant::getNullValue(MemberTy); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 550 | Ops[i] = const_cast<Constant*>(Op); |
| 551 | } |
| 552 | if (isa<StructType>(AggTy)) |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 553 | return ConstantStruct::get(Context, Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 554 | else |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 555 | return ConstantArray::get(cast<ArrayType>(AggTy), Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 556 | } |
| 557 | if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) { |
| 558 | // Insertion of constant into aggregate constant |
| 559 | std::vector<Constant*> Ops(Agg->getNumOperands()); |
| 560 | for (unsigned i = 0; i < Agg->getNumOperands(); ++i) { |
| 561 | const Constant *Op = |
| 562 | (*Idxs == i) ? |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 563 | ConstantFoldInsertValueInstruction(Context, Agg->getOperand(i), |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 564 | Val, Idxs+1, NumIdx-1) : |
| 565 | Agg->getOperand(i); |
| 566 | Ops[i] = const_cast<Constant*>(Op); |
| 567 | } |
| 568 | Constant *C; |
| 569 | if (isa<StructType>(Agg->getType())) |
Owen Anderson | 03cb69f | 2009-08-05 23:16:16 +0000 | [diff] [blame] | 570 | C = ConstantStruct::get(Context, Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 571 | else |
Owen Anderson | c2c7932 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 572 | C = ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops); |
Dan Gohman | 3db11c2 | 2008-06-03 00:15:20 +0000 | [diff] [blame] | 573 | return C; |
| 574 | } |
| 575 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 579 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 580 | Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context, |
| 581 | unsigned Opcode, |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 582 | const Constant *C1, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 583 | const Constant *C2) { |
Dale Johannesen | e5facd5 | 2007-10-16 23:38:29 +0000 | [diff] [blame] | 584 | // No compile-time operations on this type yet. |
| 585 | if (C1->getType() == Type::PPC_FP128Ty) |
| 586 | return 0; |
| 587 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 588 | // Handle UndefValue up front |
| 589 | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { |
| 590 | switch (Opcode) { |
Evan Cheng | df1690d | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 591 | case Instruction::Xor: |
| 592 | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) |
| 593 | // Handle undef ^ undef -> 0 special case. This is a common |
| 594 | // idiom (misuse). |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 595 | return Constant::getNullValue(C1->getType()); |
Evan Cheng | df1690d | 2008-03-25 20:08:07 +0000 | [diff] [blame] | 596 | // Fallthrough |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 597 | case Instruction::Add: |
| 598 | case Instruction::Sub: |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 599 | return UndefValue::get(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 600 | case Instruction::Mul: |
| 601 | case Instruction::And: |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 602 | return Constant::getNullValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 603 | case Instruction::UDiv: |
| 604 | case Instruction::SDiv: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 605 | case Instruction::URem: |
| 606 | case Instruction::SRem: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 607 | if (!isa<UndefValue>(C2)) // undef / X -> 0 |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 608 | return Constant::getNullValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 609 | return const_cast<Constant*>(C2); // X / undef -> undef |
| 610 | case Instruction::Or: // X | undef -> -1 |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 611 | if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType())) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 612 | return Constant::getAllOnesValue(PTy); |
| 613 | return Constant::getAllOnesValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 614 | case Instruction::LShr: |
| 615 | if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) |
| 616 | return const_cast<Constant*>(C1); // undef lshr undef -> undef |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 617 | return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 618 | // undef lshr X -> 0 |
| 619 | case Instruction::AShr: |
| 620 | if (!isa<UndefValue>(C2)) |
| 621 | return const_cast<Constant*>(C1); // undef ashr X --> undef |
| 622 | else if (isa<UndefValue>(C1)) |
| 623 | return const_cast<Constant*>(C1); // undef ashr undef -> undef |
| 624 | else |
| 625 | return const_cast<Constant*>(C1); // X ashr undef --> X |
| 626 | case Instruction::Shl: |
| 627 | // undef << X -> 0 or X << undef -> 0 |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 628 | return Constant::getNullValue(C1->getType()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
Nick Lewycky | 6b8320f | 2009-06-21 01:56:41 +0000 | [diff] [blame] | 632 | // Handle simplifications when the RHS is a constant int. |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 633 | if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
| 634 | switch (Opcode) { |
| 635 | case Instruction::Add: |
| 636 | if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X + 0 == X |
| 637 | break; |
| 638 | case Instruction::Sub: |
| 639 | if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X - 0 == X |
| 640 | break; |
| 641 | case Instruction::Mul: |
| 642 | if (CI2->equalsInt(0)) return const_cast<Constant*>(C2); // X * 0 == 0 |
| 643 | if (CI2->equalsInt(1)) |
| 644 | return const_cast<Constant*>(C1); // X * 1 == X |
| 645 | break; |
| 646 | case Instruction::UDiv: |
| 647 | case Instruction::SDiv: |
| 648 | if (CI2->equalsInt(1)) |
| 649 | return const_cast<Constant*>(C1); // X / 1 == X |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 650 | if (CI2->equalsInt(0)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 651 | return UndefValue::get(CI2->getType()); // X / 0 == undef |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 652 | break; |
| 653 | case Instruction::URem: |
| 654 | case Instruction::SRem: |
| 655 | if (CI2->equalsInt(1)) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 656 | return Constant::getNullValue(CI2->getType()); // X % 1 == 0 |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 657 | if (CI2->equalsInt(0)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 658 | return UndefValue::get(CI2->getType()); // X % 0 == undef |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 659 | break; |
| 660 | case Instruction::And: |
| 661 | if (CI2->isZero()) return const_cast<Constant*>(C2); // X & 0 == 0 |
| 662 | if (CI2->isAllOnesValue()) |
| 663 | return const_cast<Constant*>(C1); // X & -1 == X |
| 664 | |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 665 | if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 666 | // (zext i32 to i64) & 4294967295 -> (zext i32 to i64) |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 667 | if (CE1->getOpcode() == Instruction::ZExt) { |
| 668 | unsigned DstWidth = CI2->getType()->getBitWidth(); |
| 669 | unsigned SrcWidth = |
| 670 | CE1->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 671 | APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth)); |
| 672 | if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits) |
| 673 | return const_cast<Constant*>(C1); |
Chris Lattner | 6d94bb7 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 674 | } |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 675 | |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 676 | // 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] | 677 | if (CE1->getOpcode() == Instruction::PtrToInt && |
| 678 | isa<GlobalValue>(CE1->getOperand(0))) { |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 679 | GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 680 | |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 681 | // Functions are at least 4-byte aligned. |
| 682 | unsigned GVAlign = GV->getAlignment(); |
| 683 | if (isa<Function>(GV)) |
| 684 | GVAlign = std::max(GVAlign, 4U); |
| 685 | |
| 686 | if (GVAlign > 1) { |
| 687 | unsigned DstWidth = CI2->getType()->getBitWidth(); |
Chris Lattner | 912bec7 | 2008-04-20 19:59:12 +0000 | [diff] [blame] | 688 | unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign)); |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 689 | APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth)); |
| 690 | |
| 691 | // If checking bits we know are clear, return zero. |
| 692 | if ((CI2->getValue() & BitsNotSet) == CI2->getValue()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 693 | return Constant::getNullValue(CI2->getType()); |
Chris Lattner | bc26e1b | 2008-04-19 22:17:26 +0000 | [diff] [blame] | 694 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 696 | } |
| 697 | break; |
| 698 | case Instruction::Or: |
| 699 | if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X | 0 == X |
| 700 | if (CI2->isAllOnesValue()) |
| 701 | return const_cast<Constant*>(C2); // X | -1 == -1 |
| 702 | break; |
| 703 | case Instruction::Xor: |
| 704 | if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X ^ 0 == X |
| 705 | break; |
| 706 | case Instruction::AShr: |
| 707 | // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2 |
| 708 | if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) |
Chris Lattner | 6d94bb7 | 2007-03-25 05:47:04 +0000 | [diff] [blame] | 709 | if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero. |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 710 | return ConstantExpr::getLShr(const_cast<Constant*>(C1), |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 711 | const_cast<Constant*>(C2)); |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 712 | break; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 713 | } |
Chris Lattner | 334d33c | 2008-04-19 21:58:19 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 716 | // At this point we know neither constant is an UndefValue. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 717 | if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { |
| 718 | if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 719 | using namespace APIntOps; |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 720 | const APInt &C1V = CI1->getValue(); |
| 721 | const APInt &C2V = CI2->getValue(); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 722 | switch (Opcode) { |
| 723 | default: |
| 724 | break; |
| 725 | case Instruction::Add: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 726 | return ConstantInt::get(Context, C1V + C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 727 | case Instruction::Sub: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 728 | return ConstantInt::get(Context, C1V - C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 729 | case Instruction::Mul: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 730 | return ConstantInt::get(Context, C1V * C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 731 | case Instruction::UDiv: |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 732 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 733 | return ConstantInt::get(Context, C1V.udiv(C2V)); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 734 | case Instruction::SDiv: |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 735 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 736 | if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 737 | return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 738 | return ConstantInt::get(Context, C1V.sdiv(C2V)); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 739 | case Instruction::URem: |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 740 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 741 | return ConstantInt::get(Context, C1V.urem(C2V)); |
Chris Lattner | dd8ef1b | 2009-01-19 21:55:26 +0000 | [diff] [blame] | 742 | case Instruction::SRem: |
| 743 | assert(!CI2->isNullValue() && "Div by zero handled above"); |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 744 | if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 745 | return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 746 | return ConstantInt::get(Context, C1V.srem(C2V)); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 747 | case Instruction::And: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 748 | return ConstantInt::get(Context, C1V & C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 749 | case Instruction::Or: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 750 | return ConstantInt::get(Context, C1V | C2V); |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 751 | case Instruction::Xor: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 752 | return ConstantInt::get(Context, C1V ^ C2V); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 753 | case Instruction::Shl: { |
| 754 | uint32_t shiftAmt = C2V.getZExtValue(); |
| 755 | if (shiftAmt < C1V.getBitWidth()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 756 | return ConstantInt::get(Context, C1V.shl(shiftAmt)); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 757 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 758 | return UndefValue::get(C1->getType()); // too big shift is undef |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 759 | } |
| 760 | case Instruction::LShr: { |
| 761 | uint32_t shiftAmt = C2V.getZExtValue(); |
| 762 | if (shiftAmt < C1V.getBitWidth()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 763 | return ConstantInt::get(Context, C1V.lshr(shiftAmt)); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 764 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 765 | return UndefValue::get(C1->getType()); // too big shift is undef |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 766 | } |
| 767 | case Instruction::AShr: { |
| 768 | uint32_t shiftAmt = C2V.getZExtValue(); |
| 769 | if (shiftAmt < C1V.getBitWidth()) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 770 | return ConstantInt::get(Context, C1V.ashr(shiftAmt)); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 771 | else |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 772 | return UndefValue::get(C1->getType()); // too big shift is undef |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 773 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
Nick Lewycky | 6b8320f | 2009-06-21 01:56:41 +0000 | [diff] [blame] | 776 | |
| 777 | switch (Opcode) { |
| 778 | case Instruction::SDiv: |
| 779 | case Instruction::UDiv: |
| 780 | case Instruction::URem: |
| 781 | case Instruction::SRem: |
| 782 | case Instruction::LShr: |
| 783 | case Instruction::AShr: |
| 784 | case Instruction::Shl: |
| 785 | if (CI1->equalsInt(0)) return const_cast<Constant*>(C1); |
| 786 | break; |
| 787 | default: |
| 788 | break; |
| 789 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 790 | } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { |
| 791 | if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 792 | APFloat C1V = CFP1->getValueAPF(); |
| 793 | APFloat C2V = CFP2->getValueAPF(); |
| 794 | APFloat C3V = C1V; // copy for modification |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 795 | switch (Opcode) { |
| 796 | default: |
| 797 | break; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 798 | case Instruction::FAdd: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 799 | (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 800 | return ConstantFP::get(Context, C3V); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 801 | case Instruction::FSub: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 802 | (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 803 | return ConstantFP::get(Context, C3V); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 804 | case Instruction::FMul: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 805 | (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 806 | return ConstantFP::get(Context, C3V); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 807 | case Instruction::FDiv: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 808 | (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 809 | return ConstantFP::get(Context, C3V); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 810 | case Instruction::FRem: |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 811 | (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven); |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 812 | return ConstantFP::get(Context, C3V); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
Dan Gohman | 9f39660 | 2007-10-30 19:00:49 +0000 | [diff] [blame] | 815 | } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { |
| 816 | const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1); |
| 817 | const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2); |
Dan Gohman | b43e020 | 2007-10-31 21:36:31 +0000 | [diff] [blame] | 818 | if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) && |
| 819 | (CP2 != NULL || isa<ConstantAggregateZero>(C2))) { |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 820 | std::vector<Constant*> Res; |
| 821 | const Type* EltTy = VTy->getElementType(); |
| 822 | const Constant *C1 = 0; |
| 823 | const Constant *C2 = 0; |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 824 | switch (Opcode) { |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 825 | default: |
| 826 | break; |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 827 | case Instruction::Add: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 828 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 829 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 830 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 831 | Res.push_back(ConstantExpr::getAdd(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 832 | const_cast<Constant*>(C2))); |
| 833 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 834 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 835 | case Instruction::FAdd: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 836 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 837 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 838 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 839 | Res.push_back(ConstantExpr::getFAdd(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 840 | const_cast<Constant*>(C2))); |
| 841 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 842 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 843 | case Instruction::Sub: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 844 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 845 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 846 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 847 | Res.push_back(ConstantExpr::getSub(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 848 | const_cast<Constant*>(C2))); |
| 849 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 850 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 851 | case Instruction::FSub: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 852 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 853 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 854 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 855 | Res.push_back(ConstantExpr::getFSub(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 856 | const_cast<Constant*>(C2))); |
| 857 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 858 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 859 | case Instruction::Mul: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 860 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 861 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 862 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 863 | Res.push_back(ConstantExpr::getMul(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 864 | const_cast<Constant*>(C2))); |
| 865 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 866 | return ConstantVector::get(Res); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 867 | case Instruction::FMul: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 868 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 869 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 870 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 871 | Res.push_back(ConstantExpr::getFMul(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 872 | const_cast<Constant*>(C2))); |
| 873 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 874 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 875 | case Instruction::UDiv: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 876 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 877 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 878 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 879 | Res.push_back(ConstantExpr::getUDiv(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 880 | const_cast<Constant*>(C2))); |
| 881 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 882 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 883 | case Instruction::SDiv: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 884 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 885 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 886 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 887 | Res.push_back(ConstantExpr::getSDiv(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 888 | const_cast<Constant*>(C2))); |
| 889 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 890 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 891 | case Instruction::FDiv: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 892 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 893 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 894 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 895 | Res.push_back(ConstantExpr::getFDiv(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 896 | const_cast<Constant*>(C2))); |
| 897 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 898 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 899 | case Instruction::URem: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 900 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 901 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 902 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 903 | Res.push_back(ConstantExpr::getURem(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 904 | const_cast<Constant*>(C2))); |
| 905 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 906 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 907 | case Instruction::SRem: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 908 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 909 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 910 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 911 | Res.push_back(ConstantExpr::getSRem(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 912 | const_cast<Constant*>(C2))); |
| 913 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 914 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 915 | case Instruction::FRem: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 916 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 917 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 918 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 919 | Res.push_back(ConstantExpr::getFRem(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 920 | const_cast<Constant*>(C2))); |
| 921 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 922 | return ConstantVector::get(Res); |
Chris Lattner | 6072ead | 2008-04-19 21:13:00 +0000 | [diff] [blame] | 923 | case Instruction::And: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 924 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 925 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 926 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 927 | Res.push_back(ConstantExpr::getAnd(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 928 | const_cast<Constant*>(C2))); |
| 929 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 930 | return ConstantVector::get(Res); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 931 | case Instruction::Or: |
| 932 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 933 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 934 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 935 | Res.push_back(ConstantExpr::getOr(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 936 | const_cast<Constant*>(C2))); |
| 937 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 938 | return ConstantVector::get(Res); |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 939 | case Instruction::Xor: |
| 940 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 941 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 942 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 943 | Res.push_back(ConstantExpr::getXor(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 944 | const_cast<Constant*>(C2))); |
| 945 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 946 | return ConstantVector::get(Res); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 947 | case Instruction::LShr: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 948 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 949 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 950 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 951 | Res.push_back(ConstantExpr::getLShr(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 952 | const_cast<Constant*>(C2))); |
| 953 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 954 | return ConstantVector::get(Res); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 955 | case Instruction::AShr: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 956 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 957 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 958 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 959 | Res.push_back(ConstantExpr::getAShr(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 960 | const_cast<Constant*>(C2))); |
| 961 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 962 | return ConstantVector::get(Res); |
Dan Gohman | 79975d5 | 2009-03-14 17:09:17 +0000 | [diff] [blame] | 963 | case Instruction::Shl: |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 964 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 965 | C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy); |
| 966 | C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 967 | Res.push_back(ConstantExpr::getShl(const_cast<Constant*>(C1), |
Owen Anderson | 85f86dc | 2009-07-13 22:41:06 +0000 | [diff] [blame] | 968 | const_cast<Constant*>(C2))); |
| 969 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 970 | return ConstantVector::get(Res); |
Dan Gohman | b43e020 | 2007-10-31 21:36:31 +0000 | [diff] [blame] | 971 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 975 | if (isa<ConstantExpr>(C1)) { |
| 976 | // There are many possible foldings we could do here. We should probably |
| 977 | // at least fold add of a pointer with an integer into the appropriate |
| 978 | // getelementptr. This will improve alias analysis a bit. |
| 979 | } else if (isa<ConstantExpr>(C2)) { |
| 980 | // If C2 is a constant expr and C1 isn't, flop them around and fold the |
| 981 | // other way if possible. |
| 982 | switch (Opcode) { |
| 983 | case Instruction::Add: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 984 | case Instruction::FAdd: |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 985 | case Instruction::Mul: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 986 | case Instruction::FMul: |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 987 | case Instruction::And: |
| 988 | case Instruction::Or: |
| 989 | case Instruction::Xor: |
| 990 | // No change of opcode required. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 991 | return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1); |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 992 | |
| 993 | case Instruction::Shl: |
| 994 | case Instruction::LShr: |
| 995 | case Instruction::AShr: |
| 996 | case Instruction::Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 997 | case Instruction::FSub: |
Chris Lattner | 6b05605 | 2008-04-20 18:24:14 +0000 | [diff] [blame] | 998 | case Instruction::SDiv: |
| 999 | case Instruction::UDiv: |
| 1000 | case Instruction::FDiv: |
| 1001 | case Instruction::URem: |
| 1002 | case Instruction::SRem: |
| 1003 | case Instruction::FRem: |
| 1004 | default: // These instructions cannot be flopped around. |
| 1005 | break; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // We don't know how to fold this. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1010 | return 0; |
| 1011 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1013 | /// isZeroSizedType - This type is zero sized if its an array or structure of |
| 1014 | /// zero sized types. The only leaf zero sized type is an empty structure. |
| 1015 | static bool isMaybeZeroSizedType(const Type *Ty) { |
| 1016 | if (isa<OpaqueType>(Ty)) return true; // Can't say. |
| 1017 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 1018 | |
| 1019 | // If all of elements have zero size, this does too. |
| 1020 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Chris Lattner | feaf92f | 2005-01-28 23:17:27 +0000 | [diff] [blame] | 1021 | if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1022 | return true; |
| 1023 | |
| 1024 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 1025 | return isMaybeZeroSizedType(ATy->getElementType()); |
| 1026 | } |
| 1027 | return false; |
| 1028 | } |
Chris Lattner | 6ea4b52 | 2004-03-12 05:53:32 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1030 | /// IdxCompare - Compare the two constants as though they were getelementptr |
| 1031 | /// indices. This allows coersion of the types to be the same thing. |
| 1032 | /// |
| 1033 | /// If the two constants are the "same" (after coersion), return 0. If the |
| 1034 | /// first is less than the second, return -1, if the second is less than the |
| 1035 | /// first, return 1. If the constants are not integral, return -2. |
| 1036 | /// |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1037 | static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2, |
| 1038 | const Type *ElTy) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1039 | if (C1 == C2) return 0; |
| 1040 | |
Reid Spencer | c90cf77 | 2006-12-31 21:43:30 +0000 | [diff] [blame] | 1041 | // Ok, we found a different index. If they are not ConstantInt, we can't do |
| 1042 | // anything with them. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1043 | if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) |
| 1044 | return -2; // don't know! |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | 69193f9 | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 1046 | // Ok, we have two differing integer indices. Sign extend them to be the same |
| 1047 | // type. Long is always big enough, so we use it. |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1048 | if (C1->getType() != Type::Int64Ty) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1049 | C1 = ConstantExpr::getSExt(C1, Type::Int64Ty); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1050 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1051 | if (C2->getType() != Type::Int64Ty) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1052 | C2 = ConstantExpr::getSExt(C2, Type::Int64Ty); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1053 | |
| 1054 | if (C1 == C2) return 0; // They are equal |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1056 | // If the type being indexed over is really just a zero sized type, there is |
| 1057 | // no pointer difference being made here. |
| 1058 | if (isMaybeZeroSizedType(ElTy)) |
| 1059 | return -2; // dunno. |
| 1060 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1061 | // If they are really different, now that they are the same type, then we |
| 1062 | // found a difference! |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1063 | if (cast<ConstantInt>(C1)->getSExtValue() < |
| 1064 | cast<ConstantInt>(C2)->getSExtValue()) |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1065 | return -1; |
| 1066 | else |
| 1067 | return 1; |
| 1068 | } |
| 1069 | |
Chris Lattner | 858f4e9 | 2007-01-04 02:13:20 +0000 | [diff] [blame] | 1070 | /// evaluateFCmpRelation - This function determines if there is anything we can |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1071 | /// decide about the two constants provided. This doesn't need to handle simple |
| 1072 | /// things like ConstantFP comparisons, but should instead handle ConstantExprs. |
| 1073 | /// If we can determine that the two constants have a particular relation to |
| 1074 | /// each other, we should return the corresponding FCmpInst predicate, |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1075 | /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in |
| 1076 | /// ConstantFoldCompareInstruction. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1077 | /// |
| 1078 | /// To simplify this code we canonicalize the relation so that the first |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1079 | /// operand is always the most "complex" of the two. We consider ConstantFP |
| 1080 | /// to be the simplest, and ConstantExprs to be the most complex. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1081 | static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context, |
| 1082 | const Constant *V1, |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1083 | const Constant *V2) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1084 | assert(V1->getType() == V2->getType() && |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1085 | "Cannot compare values of different types!"); |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1086 | |
| 1087 | // No compile-time operations on this type yet. |
| 1088 | if (V1->getType() == Type::PPC_FP128Ty) |
| 1089 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 1090 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1091 | // Handle degenerate case quickly |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1092 | if (V1 == V2) return FCmpInst::FCMP_OEQ; |
| 1093 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1094 | if (!isa<ConstantExpr>(V1)) { |
| 1095 | if (!isa<ConstantExpr>(V2)) { |
| 1096 | // We distilled thisUse the standard constant folder for a few cases |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1097 | ConstantInt *R = 0; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1098 | Constant *C1 = const_cast<Constant*>(V1); |
| 1099 | Constant *C2 = const_cast<Constant*>(V2); |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1100 | R = dyn_cast<ConstantInt>( |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1101 | ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1102 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1103 | return FCmpInst::FCMP_OEQ; |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1104 | R = dyn_cast<ConstantInt>( |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1105 | ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1106 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1107 | return FCmpInst::FCMP_OLT; |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1108 | R = dyn_cast<ConstantInt>( |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1109 | ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1110 | if (R && !R->isZero()) |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1111 | return FCmpInst::FCMP_OGT; |
| 1112 | |
| 1113 | // Nothing more we can do |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1114 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 1115 | } |
| 1116 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1117 | // If the first operand is simple and second is ConstantExpr, swap operands. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1118 | FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1119 | if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE) |
| 1120 | return FCmpInst::getSwappedPredicate(SwappedRelation); |
| 1121 | } else { |
| 1122 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 1123 | // constantexpr or a simple constant. |
| 1124 | const ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
| 1125 | switch (CE1->getOpcode()) { |
| 1126 | case Instruction::FPTrunc: |
| 1127 | case Instruction::FPExt: |
| 1128 | case Instruction::UIToFP: |
| 1129 | case Instruction::SIToFP: |
| 1130 | // We might be able to do something with these but we don't right now. |
| 1131 | break; |
| 1132 | default: |
| 1133 | break; |
| 1134 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1135 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1136 | // There are MANY other foldings that we could perform here. They will |
| 1137 | // probably be added on demand, as they seem needed. |
| 1138 | return FCmpInst::BAD_FCMP_PREDICATE; |
| 1139 | } |
| 1140 | |
| 1141 | /// evaluateICmpRelation - This function determines if there is anything we can |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1142 | /// 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] | 1143 | /// things like integer comparisons, but should instead handle ConstantExprs |
Chris Lattner | 8410beb | 2006-12-11 02:16:58 +0000 | [diff] [blame] | 1144 | /// and GlobalValues. If we can determine that the two constants have a |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1145 | /// particular relation to each other, we should return the corresponding ICmp |
| 1146 | /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE. |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1147 | /// |
| 1148 | /// To simplify this code we canonicalize the relation so that the first |
| 1149 | /// operand is always the most "complex" of the two. We consider simple |
| 1150 | /// constants (like ConstantInt) to be the simplest, followed by |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1151 | /// GlobalValues, followed by ConstantExpr's (the most complex). |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1152 | /// |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1153 | static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context, |
| 1154 | const Constant *V1, |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1155 | const Constant *V2, |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1156 | bool isSigned) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1157 | assert(V1->getType() == V2->getType() && |
| 1158 | "Cannot compare different types of values!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1159 | if (V1 == V2) return ICmpInst::ICMP_EQ; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1160 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1161 | if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) { |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1162 | if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) { |
| 1163 | // We distilled this down to a simple case, use the standard constant |
| 1164 | // folder. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1165 | ConstantInt *R = 0; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1166 | Constant *C1 = const_cast<Constant*>(V1); |
| 1167 | Constant *C2 = const_cast<Constant*>(V2); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1168 | ICmpInst::Predicate pred = ICmpInst::ICMP_EQ; |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1169 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1170 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1171 | return pred; |
| 1172 | pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1173 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1174 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1175 | return pred; |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1176 | pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1177 | R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2)); |
Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1178 | if (R && !R->isZero()) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1179 | return pred; |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1180 | |
| 1181 | // If we couldn't figure it out, bail. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1182 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1185 | // If the first operand is simple, swap operands. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1186 | ICmpInst::Predicate SwappedRelation = |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1187 | evaluateICmpRelation(Context, V2, V1, isSigned); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1188 | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
| 1189 | return ICmpInst::getSwappedPredicate(SwappedRelation); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1190 | |
Chris Lattner | 0f7e9f5 | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1191 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) { |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1192 | if (isa<ConstantExpr>(V2)) { // Swap as necessary. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1193 | ICmpInst::Predicate SwappedRelation = |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1194 | evaluateICmpRelation(Context, V2, V1, isSigned); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1195 | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
| 1196 | return ICmpInst::getSwappedPredicate(SwappedRelation); |
Chris Lattner | 0f7e9f5 | 2006-01-05 07:19:51 +0000 | [diff] [blame] | 1197 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1198 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 125ed54 | 2004-02-01 01:23:19 +0000 | [diff] [blame] | 1199 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1200 | |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1201 | // Now we know that the RHS is a GlobalValue or simple constant, |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1202 | // which (since the types must match) means that it's a ConstantPointerNull. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1203 | if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 52fe869 | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 1204 | // Don't try to decide equality of aliases. |
| 1205 | if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2)) |
| 1206 | if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage()) |
| 1207 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1208 | } else { |
| 1209 | assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); |
Chris Lattner | 52fe869 | 2007-09-10 23:42:42 +0000 | [diff] [blame] | 1210 | // GlobalVals can never be null. Don't try to evaluate aliases. |
| 1211 | if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1212 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1213 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | // Ok, the LHS is known to be a constantexpr. The RHS can be any of a |
| 1216 | // constantexpr, a CPR, or a simple constant. |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1217 | const ConstantExpr *CE1 = cast<ConstantExpr>(V1); |
| 1218 | const Constant *CE1Op0 = CE1->getOperand(0); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1219 | |
| 1220 | switch (CE1->getOpcode()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1221 | case Instruction::Trunc: |
| 1222 | case Instruction::FPTrunc: |
| 1223 | case Instruction::FPExt: |
| 1224 | case Instruction::FPToUI: |
| 1225 | case Instruction::FPToSI: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1226 | break; // We can't evaluate floating point casts or truncations. |
| 1227 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1228 | case Instruction::UIToFP: |
| 1229 | case Instruction::SIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1230 | case Instruction::BitCast: |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1231 | case Instruction::ZExt: |
| 1232 | case Instruction::SExt: |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1233 | // If the cast is not actually changing bits, and the second operand is a |
| 1234 | // null pointer, do the comparison with the pre-casted value. |
| 1235 | if (V2->isNullValue() && |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1236 | (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) { |
Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 1237 | bool sgnd = isSigned; |
| 1238 | if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; |
| 1239 | if (CE1->getOpcode() == Instruction::SExt) isSigned = true; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1240 | return evaluateICmpRelation(Context, CE1Op0, |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1241 | Constant::getNullValue(CE1Op0->getType()), |
Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 1242 | sgnd); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1243 | } |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1244 | |
| 1245 | // If the dest type is a pointer type, and the RHS is a constantexpr cast |
| 1246 | // from the same type as the src of the LHS, evaluate the inputs. This is |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1247 | // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)", |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1248 | // which happens a lot in compilers with tagged integers. |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1249 | if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2)) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1250 | if (CE2->isCast() && isa<PointerType>(CE1->getType()) && |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1251 | CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() && |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1252 | CE1->getOperand(0)->getType()->isInteger()) { |
Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 1253 | bool sgnd = isSigned; |
| 1254 | if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; |
| 1255 | if (CE1->getOpcode() == Instruction::SExt) isSigned = true; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1256 | return evaluateICmpRelation(Context, CE1->getOperand(0), |
| 1257 | CE2->getOperand(0), sgnd); |
Chris Lattner | fed8ceb | 2006-01-05 07:49:30 +0000 | [diff] [blame] | 1258 | } |
Chris Lattner | 192e326 | 2004-04-11 01:29:30 +0000 | [diff] [blame] | 1259 | break; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1260 | |
| 1261 | case Instruction::GetElementPtr: |
| 1262 | // Ok, since this is a getelementptr, we know that the constant has a |
| 1263 | // pointer type. Check the various cases. |
| 1264 | if (isa<ConstantPointerNull>(V2)) { |
| 1265 | // If we are comparing a GEP to a null pointer, check to see if the base |
| 1266 | // of the GEP equals the null pointer. |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1267 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1268 | if (GV->hasExternalWeakLinkage()) |
| 1269 | // Weak linkage GVals could be zero or not. We're comparing that |
| 1270 | // to null pointer so its greater-or-equal |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1271 | return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1272 | else |
| 1273 | // If its not weak linkage, the GVal must have a non-zero address |
| 1274 | // so the result is greater-than |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1275 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1276 | } else if (isa<ConstantPointerNull>(CE1Op0)) { |
| 1277 | // If we are indexing from a null pointer, check to see if we have any |
| 1278 | // non-zero indices. |
| 1279 | for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) |
| 1280 | if (!CE1->getOperand(i)->isNullValue()) |
| 1281 | // Offsetting from null, must not be equal. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1282 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1283 | // Only zero indexes from null, must still be zero. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1284 | return ICmpInst::ICMP_EQ; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1285 | } |
| 1286 | // 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] | 1287 | } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1288 | if (isa<ConstantPointerNull>(CE1Op0)) { |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1289 | if (CPR2->hasExternalWeakLinkage()) |
| 1290 | // Weak linkage GVals could be zero or not. We're comparing it to |
| 1291 | // a null pointer, so its less-or-equal |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1292 | return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; |
Reid Spencer | 876f722 | 2006-12-06 00:25:09 +0000 | [diff] [blame] | 1293 | else |
| 1294 | // If its not weak linkage, the GVal must have a non-zero address |
| 1295 | // so the result is less-than |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1296 | return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1297 | } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1298 | if (CPR1 == CPR2) { |
| 1299 | // If this is a getelementptr of the same global, then it must be |
| 1300 | // different. Because the types must match, the getelementptr could |
| 1301 | // only have at most one index, and because we fold getelementptr's |
| 1302 | // with a single zero index, it must be nonzero. |
| 1303 | assert(CE1->getNumOperands() == 2 && |
| 1304 | !CE1->getOperand(1)->isNullValue() && |
| 1305 | "Suprising getelementptr!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1306 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1307 | } else { |
| 1308 | // If they are different globals, we don't know what the value is, |
| 1309 | // but they can't be equal. |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1310 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | } else { |
| 1314 | const ConstantExpr *CE2 = cast<ConstantExpr>(V2); |
| 1315 | const Constant *CE2Op0 = CE2->getOperand(0); |
| 1316 | |
| 1317 | // There are MANY other foldings that we could perform here. They will |
| 1318 | // probably be added on demand, as they seem needed. |
| 1319 | switch (CE2->getOpcode()) { |
| 1320 | default: break; |
| 1321 | case Instruction::GetElementPtr: |
| 1322 | // By far the most common case to handle is when the base pointers are |
| 1323 | // obviously to the same or different globals. |
Reid Spencer | accd7c7 | 2004-07-17 23:47:01 +0000 | [diff] [blame] | 1324 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1325 | if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1326 | return ICmpInst::ICMP_NE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1327 | // Ok, we know that both getelementptr instructions are based on the |
| 1328 | // same global. From this, we can precisely determine the relative |
| 1329 | // ordering of the resultant pointers. |
| 1330 | unsigned i = 1; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1332 | // Compare all of the operands the GEP's have in common. |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1333 | gep_type_iterator GTI = gep_type_begin(CE1); |
| 1334 | for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); |
| 1335 | ++i, ++GTI) |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1336 | switch (IdxCompare(Context, CE1->getOperand(i), |
| 1337 | CE2->getOperand(i), GTI.getIndexedType())) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1338 | case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT; |
| 1339 | case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT; |
| 1340 | case -2: return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | // Ok, we ran out of things they have in common. If any leftovers |
| 1344 | // are non-zero then we have a difference, otherwise we are equal. |
| 1345 | for (; i < CE1->getNumOperands(); ++i) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1346 | if (!CE1->getOperand(i)->isNullValue()) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1347 | if (isa<ConstantInt>(CE1->getOperand(i))) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1348 | return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1349 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1350 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1351 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1353 | for (; i < CE2->getNumOperands(); ++i) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1354 | if (!CE2->getOperand(i)->isNullValue()) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1355 | if (isa<ConstantInt>(CE2->getOperand(i))) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1356 | return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
Chris Lattner | 60c4726 | 2005-01-28 19:09:51 +0000 | [diff] [blame] | 1357 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1358 | return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1359 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1360 | return ICmpInst::ICMP_EQ; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1364 | default: |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1369 | return ICmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1372 | Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context, |
| 1373 | unsigned short pred, |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1374 | const Constant *C1, |
| 1375 | const Constant *C2) { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1376 | const Type *ResultTy; |
| 1377 | if (const VectorType *VT = dyn_cast<VectorType>(C1->getType())) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1378 | ResultTy = VectorType::get(Type::Int1Ty, VT->getNumElements()); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1379 | else |
| 1380 | ResultTy = Type::Int1Ty; |
| 1381 | |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1382 | // Fold FCMP_FALSE/FCMP_TRUE unconditionally. |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1383 | if (pred == FCmpInst::FCMP_FALSE) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1384 | return Constant::getNullValue(ResultTy); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (pred == FCmpInst::FCMP_TRUE) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1387 | return Constant::getAllOnesValue(ResultTy); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1388 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1389 | // Handle some degenerate cases first |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1390 | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1391 | return UndefValue::get(ResultTy); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1392 | |
Dale Johannesen | 19db093 | 2007-10-14 01:56:47 +0000 | [diff] [blame] | 1393 | // No compile-time operations on this type yet. |
| 1394 | if (C1->getType() == Type::PPC_FP128Ty) |
| 1395 | return 0; |
| 1396 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1397 | // icmp eq/ne(null,GV) -> false/true |
| 1398 | if (C1->isNullValue()) { |
| 1399 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2)) |
Duncan Sands | a38e527 | 2007-09-19 10:16:17 +0000 | [diff] [blame] | 1400 | // Don't try to evaluate aliases. External weak GV can be null. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1401 | if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) { |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1402 | if (pred == ICmpInst::ICMP_EQ) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1403 | return ConstantInt::getFalse(Context); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1404 | else if (pred == ICmpInst::ICMP_NE) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1405 | return ConstantInt::getTrue(Context); |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1406 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1407 | // icmp eq/ne(GV,null) -> false/true |
| 1408 | } else if (C2->isNullValue()) { |
| 1409 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) |
Duncan Sands | a38e527 | 2007-09-19 10:16:17 +0000 | [diff] [blame] | 1410 | // Don't try to evaluate aliases. External weak GV can be null. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1411 | if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) { |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1412 | if (pred == ICmpInst::ICMP_EQ) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1413 | return ConstantInt::getFalse(Context); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1414 | else if (pred == ICmpInst::ICMP_NE) |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1415 | return ConstantInt::getTrue(Context); |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1416 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Chris Lattner | 344da52 | 2007-01-12 18:42:52 +0000 | [diff] [blame] | 1419 | if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) { |
Reid Spencer | 81658a8 | 2007-02-27 06:23:51 +0000 | [diff] [blame] | 1420 | APInt V1 = cast<ConstantInt>(C1)->getValue(); |
| 1421 | APInt V2 = cast<ConstantInt>(C2)->getValue(); |
| 1422 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1423 | default: llvm_unreachable("Invalid ICmp Predicate"); return 0; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1424 | case ICmpInst::ICMP_EQ: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1425 | return ConstantInt::get(Type::Int1Ty, V1 == V2); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1426 | case ICmpInst::ICMP_NE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1427 | return ConstantInt::get(Type::Int1Ty, V1 != V2); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1428 | case ICmpInst::ICMP_SLT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1429 | return ConstantInt::get(Type::Int1Ty, V1.slt(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1430 | case ICmpInst::ICMP_SGT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1431 | return ConstantInt::get(Type::Int1Ty, V1.sgt(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1432 | case ICmpInst::ICMP_SLE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1433 | return ConstantInt::get(Type::Int1Ty, V1.sle(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1434 | case ICmpInst::ICMP_SGE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1435 | return ConstantInt::get(Type::Int1Ty, V1.sge(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1436 | case ICmpInst::ICMP_ULT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1437 | return ConstantInt::get(Type::Int1Ty, V1.ult(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1438 | case ICmpInst::ICMP_UGT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1439 | return ConstantInt::get(Type::Int1Ty, V1.ugt(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1440 | case ICmpInst::ICMP_ULE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1441 | return ConstantInt::get(Type::Int1Ty, V1.ule(V2)); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1442 | case ICmpInst::ICMP_UGE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1443 | return ConstantInt::get(Type::Int1Ty, V1.uge(V2)); |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1444 | } |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1445 | } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) { |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1446 | APFloat C1V = cast<ConstantFP>(C1)->getValueAPF(); |
| 1447 | APFloat C2V = cast<ConstantFP>(C2)->getValueAPF(); |
| 1448 | APFloat::cmpResult R = C1V.compare(C2V); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1449 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1450 | default: llvm_unreachable("Invalid FCmp Predicate"); return 0; |
Owen Anderson | 23a204d | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 1451 | case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse(Context); |
| 1452 | case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue(Context); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1453 | case FCmpInst::FCMP_UNO: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1454 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1455 | case FCmpInst::FCMP_ORD: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1456 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpUnordered); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1457 | case FCmpInst::FCMP_UEQ: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1458 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1459 | R==APFloat::cmpEqual); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1460 | case FCmpInst::FCMP_OEQ: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1461 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpEqual); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1462 | case FCmpInst::FCMP_UNE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1463 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpEqual); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1464 | case FCmpInst::FCMP_ONE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1465 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1466 | R==APFloat::cmpGreaterThan); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1467 | case FCmpInst::FCMP_ULT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1468 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1469 | R==APFloat::cmpLessThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1470 | case FCmpInst::FCMP_OLT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1471 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1472 | case FCmpInst::FCMP_UGT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1473 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1474 | R==APFloat::cmpGreaterThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1475 | case FCmpInst::FCMP_OGT: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1476 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan); |
Reid Spencer | 74bd036 | 2007-01-11 00:25:45 +0000 | [diff] [blame] | 1477 | case FCmpInst::FCMP_ULE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1478 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpGreaterThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1479 | case FCmpInst::FCMP_OLE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1480 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1481 | R==APFloat::cmpEqual); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1482 | case FCmpInst::FCMP_UGE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1483 | return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpLessThan); |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1484 | case FCmpInst::FCMP_OGE: |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1485 | return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan || |
Dale Johannesen | bed9dc4 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1486 | R==APFloat::cmpEqual); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1487 | } |
Chris Lattner | 67136cf | 2008-07-10 00:29:28 +0000 | [diff] [blame] | 1488 | } else if (isa<VectorType>(C1->getType())) { |
| 1489 | SmallVector<Constant*, 16> C1Elts, C2Elts; |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1490 | C1->getVectorElements(Context, C1Elts); |
| 1491 | C2->getVectorElements(Context, C2Elts); |
Chris Lattner | 67136cf | 2008-07-10 00:29:28 +0000 | [diff] [blame] | 1492 | |
| 1493 | // If we can constant fold the comparison of each element, constant fold |
| 1494 | // the whole vector comparison. |
| 1495 | SmallVector<Constant*, 4> ResElts; |
Chris Lattner | 67136cf | 2008-07-10 00:29:28 +0000 | [diff] [blame] | 1496 | for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) { |
| 1497 | // Compare the elements, producing an i1 result or constant expr. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1498 | ResElts.push_back( |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1499 | ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i])); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1500 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1501 | return ConstantVector::get(&ResElts[0], ResElts.size()); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1502 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1503 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1504 | if (C1->getType()->isFloatingPoint()) { |
Chris Lattner | 350e417 | 2008-07-08 18:47:38 +0000 | [diff] [blame] | 1505 | int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1506 | switch (evaluateFCmpRelation(Context, C1, C2)) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1507 | default: llvm_unreachable("Unknown relation!"); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1508 | case FCmpInst::FCMP_UNO: |
| 1509 | case FCmpInst::FCMP_ORD: |
| 1510 | case FCmpInst::FCMP_UEQ: |
| 1511 | case FCmpInst::FCMP_UNE: |
| 1512 | case FCmpInst::FCMP_ULT: |
| 1513 | case FCmpInst::FCMP_UGT: |
| 1514 | case FCmpInst::FCMP_ULE: |
| 1515 | case FCmpInst::FCMP_UGE: |
| 1516 | case FCmpInst::FCMP_TRUE: |
| 1517 | case FCmpInst::FCMP_FALSE: |
| 1518 | case FCmpInst::BAD_FCMP_PREDICATE: |
| 1519 | break; // Couldn't determine anything about these constants. |
| 1520 | case FCmpInst::FCMP_OEQ: // We know that C1 == C2 |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1521 | Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ || |
| 1522 | pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE || |
| 1523 | pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); |
| 1524 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1525 | case FCmpInst::FCMP_OLT: // We know that C1 < C2 |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1526 | Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || |
| 1527 | pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT || |
| 1528 | pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE); |
| 1529 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1530 | case FCmpInst::FCMP_OGT: // We know that C1 > C2 |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1531 | Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || |
| 1532 | pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT || |
| 1533 | pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); |
| 1534 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1535 | case FCmpInst::FCMP_OLE: // We know that C1 <= C2 |
| 1536 | // We can only partially decide this relation. |
| 1537 | if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1538 | Result = 0; |
| 1539 | else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) |
| 1540 | Result = 1; |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1541 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1542 | case FCmpInst::FCMP_OGE: // We known that C1 >= C2 |
| 1543 | // We can only partially decide this relation. |
| 1544 | if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1545 | Result = 0; |
| 1546 | else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) |
| 1547 | Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1548 | break; |
| 1549 | case ICmpInst::ICMP_NE: // We know that C1 != C2 |
| 1550 | // We can only partially decide this relation. |
| 1551 | if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1552 | Result = 0; |
| 1553 | else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) |
| 1554 | Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1555 | break; |
| 1556 | } |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1557 | |
| 1558 | // If we evaluated the result, return it now. |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1559 | if (Result != -1) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1560 | return ConstantInt::get(Type::Int1Ty, Result); |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1561 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | // Evaluate the relation between the two constants, per the predicate. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1564 | int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1565 | switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1566 | default: llvm_unreachable("Unknown relational!"); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1567 | case ICmpInst::BAD_ICMP_PREDICATE: |
| 1568 | break; // Couldn't determine anything about these constants. |
| 1569 | case ICmpInst::ICMP_EQ: // We know the constants are equal! |
| 1570 | // If we know the constants are equal, we can decide the result of this |
| 1571 | // computation precisely. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1572 | Result = (pred == ICmpInst::ICMP_EQ || |
| 1573 | pred == ICmpInst::ICMP_ULE || |
| 1574 | pred == ICmpInst::ICMP_SLE || |
| 1575 | pred == ICmpInst::ICMP_UGE || |
| 1576 | pred == ICmpInst::ICMP_SGE); |
| 1577 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1578 | case ICmpInst::ICMP_ULT: |
| 1579 | // If we know that C1 < C2, we can decide the result of this computation |
| 1580 | // precisely. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1581 | Result = (pred == ICmpInst::ICMP_ULT || |
| 1582 | pred == ICmpInst::ICMP_NE || |
| 1583 | pred == ICmpInst::ICMP_ULE); |
| 1584 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1585 | case ICmpInst::ICMP_SLT: |
| 1586 | // If we know that C1 < C2, we can decide the result of this computation |
| 1587 | // precisely. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1588 | Result = (pred == ICmpInst::ICMP_SLT || |
| 1589 | pred == ICmpInst::ICMP_NE || |
| 1590 | pred == ICmpInst::ICMP_SLE); |
| 1591 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1592 | case ICmpInst::ICMP_UGT: |
| 1593 | // If we know that C1 > C2, we can decide the result of this computation |
| 1594 | // precisely. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1595 | Result = (pred == ICmpInst::ICMP_UGT || |
| 1596 | pred == ICmpInst::ICMP_NE || |
| 1597 | pred == ICmpInst::ICMP_UGE); |
| 1598 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1599 | case ICmpInst::ICMP_SGT: |
| 1600 | // If we know that C1 > C2, we can decide the result of this computation |
| 1601 | // precisely. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1602 | Result = (pred == ICmpInst::ICMP_SGT || |
| 1603 | pred == ICmpInst::ICMP_NE || |
| 1604 | pred == ICmpInst::ICMP_SGE); |
| 1605 | break; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1606 | case ICmpInst::ICMP_ULE: |
| 1607 | // If we know that C1 <= C2, we can only partially decide this relation. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1608 | if (pred == ICmpInst::ICMP_UGT) Result = 0; |
| 1609 | if (pred == ICmpInst::ICMP_ULT) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1610 | break; |
| 1611 | case ICmpInst::ICMP_SLE: |
| 1612 | // If we know that C1 <= C2, we can only partially decide this relation. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1613 | if (pred == ICmpInst::ICMP_SGT) Result = 0; |
| 1614 | if (pred == ICmpInst::ICMP_SLT) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | |
| 1617 | case ICmpInst::ICMP_UGE: |
| 1618 | // If we know that C1 >= C2, we can only partially decide this relation. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1619 | if (pred == ICmpInst::ICMP_ULT) Result = 0; |
| 1620 | if (pred == ICmpInst::ICMP_UGT) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | case ICmpInst::ICMP_SGE: |
| 1623 | // If we know that C1 >= C2, we can only partially decide this relation. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1624 | if (pred == ICmpInst::ICMP_SLT) Result = 0; |
| 1625 | if (pred == ICmpInst::ICMP_SGT) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | |
| 1628 | case ICmpInst::ICMP_NE: |
| 1629 | // If we know that C1 != C2, we can only partially decide this relation. |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1630 | if (pred == ICmpInst::ICMP_EQ) Result = 0; |
| 1631 | if (pred == ICmpInst::ICMP_NE) Result = 1; |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1632 | break; |
| 1633 | } |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1634 | |
| 1635 | // If we evaluated the result, return it now. |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1636 | if (Result != -1) |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1637 | return ConstantInt::get(Type::Int1Ty, Result); |
Chris Lattner | d137a08 | 2008-07-08 05:46:34 +0000 | [diff] [blame] | 1638 | |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1639 | if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) { |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1640 | // 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] | 1641 | // other way if possible. |
| 1642 | switch (pred) { |
| 1643 | case ICmpInst::ICMP_EQ: |
| 1644 | case ICmpInst::ICMP_NE: |
| 1645 | // No change of predicate required. |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1646 | return ConstantFoldCompareInstruction(Context, pred, C2, C1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1647 | |
| 1648 | case ICmpInst::ICMP_ULT: |
| 1649 | case ICmpInst::ICMP_SLT: |
| 1650 | case ICmpInst::ICMP_UGT: |
| 1651 | case ICmpInst::ICMP_SGT: |
| 1652 | case ICmpInst::ICMP_ULE: |
| 1653 | case ICmpInst::ICMP_SLE: |
| 1654 | case ICmpInst::ICMP_UGE: |
| 1655 | case ICmpInst::ICMP_SGE: |
| 1656 | // Change the predicate as necessary to swap the operands. |
| 1657 | pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1658 | return ConstantFoldCompareInstruction(Context, pred, C2, C1); |
Reid Spencer | 9d36acf | 2006-12-24 18:52:08 +0000 | [diff] [blame] | 1659 | |
| 1660 | default: // These predicates cannot be flopped around. |
| 1661 | break; |
| 1662 | } |
Chris Lattner | 061da2f | 2004-01-13 05:51:55 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | return 0; |
Nick Lewycky | a21d3da | 2009-07-08 03:04:38 +0000 | [diff] [blame] | 1666 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1667 | |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1668 | Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context, |
| 1669 | const Constant *C, |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1670 | Constant* const *Idxs, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1671 | unsigned NumIdx) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1672 | if (NumIdx == 0 || |
| 1673 | (NumIdx == 1 && Idxs[0]->isNullValue())) |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1674 | return const_cast<Constant*>(C); |
| 1675 | |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1676 | if (isa<UndefValue>(C)) { |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1677 | const PointerType *Ptr = cast<PointerType>(C->getType()); |
| 1678 | const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1679 | (Value **)Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1680 | (Value **)Idxs+NumIdx); |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1681 | assert(Ty != 0 && "Invalid indices for GEP!"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1682 | return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); |
Chris Lattner | f601375 | 2004-10-17 21:54:55 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1685 | Constant *Idx0 = Idxs[0]; |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1686 | if (C->isNullValue()) { |
| 1687 | bool isNull = true; |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1688 | for (unsigned i = 0, e = NumIdx; i != e; ++i) |
| 1689 | if (!Idxs[i]->isNullValue()) { |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1690 | isNull = false; |
| 1691 | break; |
| 1692 | } |
| 1693 | if (isNull) { |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1694 | const PointerType *Ptr = cast<PointerType>(C->getType()); |
| 1695 | const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 1696 | (Value**)Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1697 | (Value**)Idxs+NumIdx); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1698 | assert(Ty != 0 && "Invalid indices for GEP!"); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1699 | return ConstantPointerNull::get( |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1700 | PointerType::get(Ty,Ptr->getAddressSpace())); |
Chris Lattner | 04b60fe | 2004-02-16 20:46:13 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1703 | |
| 1704 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) { |
| 1705 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 1706 | // is a getelementptr instruction, combine the indices of the two |
| 1707 | // getelementptr instructions into a single instruction. |
| 1708 | // |
| 1709 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 1710 | const Type *LastTy = 0; |
| 1711 | for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 1712 | I != E; ++I) |
| 1713 | LastTy = *I; |
| 1714 | |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1715 | if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1716 | SmallVector<Value*, 16> NewIndices; |
| 1717 | NewIndices.reserve(NumIdx + CE->getNumOperands()); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1718 | for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1719 | NewIndices.push_back(CE->getOperand(i)); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Add the last index of the source with the first index of the new GEP. |
| 1722 | // Make sure to handle the case when they are actually different types. |
| 1723 | Constant *Combined = CE->getOperand(CE->getNumOperands()-1); |
Chris Lattner | 13128ab | 2004-10-11 22:52:25 +0000 | [diff] [blame] | 1724 | // Otherwise it must be an array. |
| 1725 | if (!Idx0->isNullValue()) { |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1726 | const Type *IdxTy = Combined->getType(); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1727 | if (IdxTy != Idx0->getType()) { |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1728 | Constant *C1 = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1729 | ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty); |
| 1730 | Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1731 | Type::Int64Ty); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1732 | Combined = ConstantExpr::get(Instruction::Add, C1, C2); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1733 | } else { |
| 1734 | Combined = |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1735 | ConstantExpr::get(Instruction::Add, Idx0, Combined); |
Reid Spencer | 1a06389 | 2006-12-04 02:46:44 +0000 | [diff] [blame] | 1736 | } |
Chris Lattner | 71068a0 | 2004-07-07 04:45:13 +0000 | [diff] [blame] | 1737 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1738 | |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1739 | NewIndices.push_back(Combined); |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 1740 | NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx); |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1741 | return ConstantExpr::getGetElementPtr(CE->getOperand(0), |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 1742 | &NewIndices[0], |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1743 | NewIndices.size()); |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | // Implement folding of: |
| 1748 | // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*), |
| 1749 | // long 0, long 0) |
| 1750 | // To: int* getelementptr ([3 x int]* %X, long 0, long 0) |
| 1751 | // |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1752 | if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1753 | if (const PointerType *SPT = |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1754 | dyn_cast<PointerType>(CE->getOperand(0)->getType())) |
| 1755 | if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType())) |
| 1756 | if (const ArrayType *CAT = |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 1757 | dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType())) |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1758 | if (CAT->getElementType() == SAT->getElementType()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1759 | return ConstantExpr::getGetElementPtr( |
Owen Anderson | 0d2de8c | 2009-06-20 00:24:58 +0000 | [diff] [blame] | 1760 | (Constant*)CE->getOperand(0), Idxs, NumIdx); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1) |
| 1764 | // Into: inttoptr (i64 0 to i8*) |
| 1765 | // This happens with pointers to member functions in C++. |
| 1766 | if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 && |
| 1767 | isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) && |
| 1768 | cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) { |
| 1769 | Constant *Base = CE->getOperand(0); |
| 1770 | Constant *Offset = Idxs[0]; |
| 1771 | |
| 1772 | // Convert the smaller integer to the larger type. |
| 1773 | if (Offset->getType()->getPrimitiveSizeInBits() < |
| 1774 | Base->getType()->getPrimitiveSizeInBits()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1775 | Offset = ConstantExpr::getSExt(Offset, Base->getType()); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1776 | else if (Base->getType()->getPrimitiveSizeInBits() < |
| 1777 | Offset->getType()->getPrimitiveSizeInBits()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1778 | Base = ConstantExpr::getZExt(Base, Offset->getType()); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1779 | |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1780 | Base = ConstantExpr::getAdd(Base, Offset); |
| 1781 | return ConstantExpr::getIntToPtr(Base, CE->getType()); |
Chris Lattner | aadc778 | 2007-08-13 17:09:08 +0000 | [diff] [blame] | 1782 | } |
Chris Lattner | 1dd054c | 2004-01-12 22:07:24 +0000 | [diff] [blame] | 1783 | } |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |