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