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