Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1 | //===- InstCombineCompares.cpp --------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visitICmp and visitFCmp functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "InstCombine.h" |
| 15 | #include "llvm/IntrinsicInst.h" |
Eli Friedman | 7470325 | 2011-07-20 21:57:23 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/InstructionSimplify.h" |
| 18 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 19 | #include "llvm/Target/TargetData.h" |
| 20 | #include "llvm/Support/ConstantRange.h" |
| 21 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 22 | #include "llvm/Support/PatternMatch.h" |
| 23 | using namespace llvm; |
| 24 | using namespace PatternMatch; |
| 25 | |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 26 | static ConstantInt *getOne(Constant *C) { |
| 27 | return ConstantInt::get(cast<IntegerType>(C->getType()), 1); |
| 28 | } |
| 29 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 30 | /// AddOne - Add one to a ConstantInt |
| 31 | static Constant *AddOne(Constant *C) { |
| 32 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); |
| 33 | } |
| 34 | /// SubOne - Subtract one from a ConstantInt |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 35 | static Constant *SubOne(Constant *C) { |
| 36 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static ConstantInt *ExtractElement(Constant *V, Constant *Idx) { |
| 40 | return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx)); |
| 41 | } |
| 42 | |
| 43 | static bool HasAddOverflow(ConstantInt *Result, |
| 44 | ConstantInt *In1, ConstantInt *In2, |
| 45 | bool IsSigned) { |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 46 | if (!IsSigned) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 47 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 48 | |
| 49 | if (In2->isNegative()) |
| 50 | return Result->getValue().sgt(In1->getValue()); |
| 51 | return Result->getValue().slt(In1->getValue()); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 55 | /// overflowed for this type. |
| 56 | static bool AddWithOverflow(Constant *&Result, Constant *In1, |
| 57 | Constant *In2, bool IsSigned = false) { |
| 58 | Result = ConstantExpr::getAdd(In1, In2); |
| 59 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 60 | if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 62 | Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i); |
| 63 | if (HasAddOverflow(ExtractElement(Result, Idx), |
| 64 | ExtractElement(In1, Idx), |
| 65 | ExtractElement(In2, Idx), |
| 66 | IsSigned)) |
| 67 | return true; |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return HasAddOverflow(cast<ConstantInt>(Result), |
| 73 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 74 | IsSigned); |
| 75 | } |
| 76 | |
| 77 | static bool HasSubOverflow(ConstantInt *Result, |
| 78 | ConstantInt *In1, ConstantInt *In2, |
| 79 | bool IsSigned) { |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 80 | if (!IsSigned) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 81 | return Result->getValue().ugt(In1->getValue()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 82 | |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 83 | if (In2->isNegative()) |
| 84 | return Result->getValue().slt(In1->getValue()); |
| 85 | |
| 86 | return Result->getValue().sgt(In1->getValue()); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /// SubWithOverflow - Compute Result = In1-In2, returning true if the result |
| 90 | /// overflowed for this type. |
| 91 | static bool SubWithOverflow(Constant *&Result, Constant *In1, |
| 92 | Constant *In2, bool IsSigned = false) { |
| 93 | Result = ConstantExpr::getSub(In1, In2); |
| 94 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 95 | if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 96 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 97 | Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i); |
| 98 | if (HasSubOverflow(ExtractElement(Result, Idx), |
| 99 | ExtractElement(In1, Idx), |
| 100 | ExtractElement(In2, Idx), |
| 101 | IsSigned)) |
| 102 | return true; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return HasSubOverflow(cast<ConstantInt>(Result), |
| 108 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 109 | IsSigned); |
| 110 | } |
| 111 | |
| 112 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 113 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 114 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 115 | /// signed. |
| 116 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 117 | bool &TrueIfSigned) { |
| 118 | switch (pred) { |
| 119 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 120 | TrueIfSigned = true; |
| 121 | return RHS->isZero(); |
| 122 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 123 | TrueIfSigned = true; |
| 124 | return RHS->isAllOnesValue(); |
| 125 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 126 | TrueIfSigned = false; |
| 127 | return RHS->isAllOnesValue(); |
| 128 | case ICmpInst::ICMP_UGT: |
| 129 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 130 | TrueIfSigned = true; |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 131 | return RHS->isMaxValue(true); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 132 | case ICmpInst::ICMP_UGE: |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 133 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 134 | TrueIfSigned = true; |
| 135 | return RHS->getValue().isSignBit(); |
| 136 | default: |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 142 | // This is the same as lowones(~X). |
| 143 | static bool isHighOnes(const ConstantInt *CI) { |
| 144 | return (~CI->getValue() + 1).isPowerOf2(); |
| 145 | } |
| 146 | |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 147 | /// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 148 | /// set of known zero and one bits, compute the maximum and minimum values that |
| 149 | /// could have the specified known zero and known one bits, returning them in |
| 150 | /// min/max. |
| 151 | static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero, |
| 152 | const APInt& KnownOne, |
| 153 | APInt& Min, APInt& Max) { |
| 154 | assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() && |
| 155 | KnownZero.getBitWidth() == Min.getBitWidth() && |
| 156 | KnownZero.getBitWidth() == Max.getBitWidth() && |
| 157 | "KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
| 158 | APInt UnknownBits = ~(KnownZero|KnownOne); |
| 159 | |
| 160 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 161 | // bit if it is unknown. |
| 162 | Min = KnownOne; |
| 163 | Max = KnownOne|UnknownBits; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 165 | if (UnknownBits.isNegative()) { // Sign bit is unknown |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 166 | Min.setBit(Min.getBitWidth()-1); |
| 167 | Max.clearBit(Max.getBitWidth()-1); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 172 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 173 | // could have the specified known zero and known one bits, returning them in |
| 174 | // min/max. |
| 175 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero, |
| 176 | const APInt &KnownOne, |
| 177 | APInt &Min, APInt &Max) { |
| 178 | assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() && |
| 179 | KnownZero.getBitWidth() == Min.getBitWidth() && |
| 180 | KnownZero.getBitWidth() == Max.getBitWidth() && |
| 181 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
| 182 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 184 | // The minimum value is when the unknown bits are all zeros. |
| 185 | Min = KnownOne; |
| 186 | // The maximum value is when the unknown bits are all ones. |
| 187 | Max = KnownOne|UnknownBits; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | /// FoldCmpLoadFromIndexedGlobal - Called we see this pattern: |
| 193 | /// cmp pred (load (gep GV, ...)), cmpcst |
| 194 | /// where GV is a global variable with a constant initializer. Try to simplify |
| 195 | /// this into some simple computation that does not need the load. For example |
| 196 | /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3". |
| 197 | /// |
| 198 | /// If AndCst is non-null, then the loaded value is masked with that constant |
| 199 | /// before doing the comparison. This handles cases like "A[i]&4 == 0". |
| 200 | Instruction *InstCombiner:: |
| 201 | FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV, |
| 202 | CmpInst &ICI, ConstantInt *AndCst) { |
Chris Lattner | d7f5a58 | 2010-01-04 18:57:15 +0000 | [diff] [blame] | 203 | // We need TD information to know the pointer size unless this is inbounds. |
| 204 | if (!GEP->isInBounds() && TD == 0) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 205 | |
Chris Lattner | c8d75c7 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 206 | Constant *Init = GV->getInitializer(); |
| 207 | if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init)) |
| 208 | return 0; |
| 209 | |
| 210 | uint64_t ArrayElementCount = Init->getType()->getArrayNumElements(); |
| 211 | if (ArrayElementCount > 1024) return 0; // Don't blow up on huge arrays. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 213 | // There are many forms of this optimization we can handle, for now, just do |
| 214 | // the simple index into a single-dimensional array. |
| 215 | // |
| 216 | // Require: GEP GV, 0, i {{, constant indices}} |
| 217 | if (GEP->getNumOperands() < 3 || |
| 218 | !isa<ConstantInt>(GEP->getOperand(1)) || |
| 219 | !cast<ConstantInt>(GEP->getOperand(1))->isZero() || |
| 220 | isa<Constant>(GEP->getOperand(2))) |
| 221 | return 0; |
| 222 | |
| 223 | // Check that indices after the variable are constants and in-range for the |
| 224 | // type they index. Collect the indices. This is typically for arrays of |
| 225 | // structs. |
| 226 | SmallVector<unsigned, 4> LaterIndices; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 227 | |
Chris Lattner | c8d75c7 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 228 | Type *EltTy = Init->getType()->getArrayElementType(); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 229 | for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) { |
| 230 | ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 231 | if (Idx == 0) return 0; // Variable index. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 233 | uint64_t IdxVal = Idx->getZExtValue(); |
| 234 | if ((unsigned)IdxVal != IdxVal) return 0; // Too large array index. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 235 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 236 | if (StructType *STy = dyn_cast<StructType>(EltTy)) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 237 | EltTy = STy->getElementType(IdxVal); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 238 | else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 239 | if (IdxVal >= ATy->getNumElements()) return 0; |
| 240 | EltTy = ATy->getElementType(); |
| 241 | } else { |
| 242 | return 0; // Unknown type. |
| 243 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 245 | LaterIndices.push_back(IdxVal); |
| 246 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 248 | enum { Overdefined = -3, Undefined = -2 }; |
| 249 | |
| 250 | // Variables for our state machines. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 252 | // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form |
| 253 | // "i == 47 | i == 87", where 47 is the first index the condition is true for, |
| 254 | // and 87 is the second (and last) index. FirstTrueElement is -2 when |
| 255 | // undefined, otherwise set to the first true element. SecondTrueElement is |
| 256 | // -2 when undefined, -3 when overdefined and >= 0 when that index is true. |
| 257 | int FirstTrueElement = Undefined, SecondTrueElement = Undefined; |
| 258 | |
| 259 | // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the |
| 260 | // form "i != 47 & i != 87". Same state transitions as for true elements. |
| 261 | int FirstFalseElement = Undefined, SecondFalseElement = Undefined; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 263 | /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these |
| 264 | /// define a state machine that triggers for ranges of values that the index |
| 265 | /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'. |
| 266 | /// This is -2 when undefined, -3 when overdefined, and otherwise the last |
| 267 | /// index in the range (inclusive). We use -2 for undefined here because we |
| 268 | /// use relative comparisons and don't want 0-1 to match -1. |
| 269 | int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 271 | // MagicBitvector - This is a magic bitvector where we set a bit if the |
| 272 | // comparison is true for element 'i'. If there are 64 elements or less in |
| 273 | // the array, this will fully represent all the comparison results. |
| 274 | uint64_t MagicBitvector = 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 275 | |
| 276 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 277 | // Scan the array and see if one of our patterns matches. |
| 278 | Constant *CompareRHS = cast<Constant>(ICI.getOperand(1)); |
Chris Lattner | c8d75c7 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 279 | for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) { |
| 280 | Constant *Elt = Init->getAggregateElement(i); |
| 281 | if (Elt == 0) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 283 | // If this is indexing an array of structures, get the structure element. |
| 284 | if (!LaterIndices.empty()) |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 285 | Elt = ConstantExpr::getExtractValue(Elt, LaterIndices); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 287 | // If the element is masked, handle it. |
| 288 | if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 290 | // Find out if the comparison would be true or false for the i'th element. |
| 291 | Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt, |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 292 | CompareRHS, TD, TLI); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 293 | // If the result is undef for this element, ignore it. |
| 294 | if (isa<UndefValue>(C)) { |
| 295 | // Extend range state machines to cover this element in case there is an |
| 296 | // undef in the middle of the range. |
| 297 | if (TrueRangeEnd == (int)i-1) |
| 298 | TrueRangeEnd = i; |
| 299 | if (FalseRangeEnd == (int)i-1) |
| 300 | FalseRangeEnd = i; |
| 301 | continue; |
| 302 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 304 | // If we can't compute the result for any of the elements, we have to give |
| 305 | // up evaluating the entire conditional. |
| 306 | if (!isa<ConstantInt>(C)) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 308 | // Otherwise, we know if the comparison is true or false for this element, |
| 309 | // update our state machines. |
| 310 | bool IsTrueForElt = !cast<ConstantInt>(C)->isZero(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 312 | // State machine for single/double/range index comparison. |
| 313 | if (IsTrueForElt) { |
| 314 | // Update the TrueElement state machine. |
| 315 | if (FirstTrueElement == Undefined) |
| 316 | FirstTrueElement = TrueRangeEnd = i; // First true element. |
| 317 | else { |
| 318 | // Update double-compare state machine. |
| 319 | if (SecondTrueElement == Undefined) |
| 320 | SecondTrueElement = i; |
| 321 | else |
| 322 | SecondTrueElement = Overdefined; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 324 | // Update range state machine. |
| 325 | if (TrueRangeEnd == (int)i-1) |
| 326 | TrueRangeEnd = i; |
| 327 | else |
| 328 | TrueRangeEnd = Overdefined; |
| 329 | } |
| 330 | } else { |
| 331 | // Update the FalseElement state machine. |
| 332 | if (FirstFalseElement == Undefined) |
| 333 | FirstFalseElement = FalseRangeEnd = i; // First false element. |
| 334 | else { |
| 335 | // Update double-compare state machine. |
| 336 | if (SecondFalseElement == Undefined) |
| 337 | SecondFalseElement = i; |
| 338 | else |
| 339 | SecondFalseElement = Overdefined; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 341 | // Update range state machine. |
| 342 | if (FalseRangeEnd == (int)i-1) |
| 343 | FalseRangeEnd = i; |
| 344 | else |
| 345 | FalseRangeEnd = Overdefined; |
| 346 | } |
| 347 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 348 | |
| 349 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 350 | // If this element is in range, update our magic bitvector. |
| 351 | if (i < 64 && IsTrueForElt) |
| 352 | MagicBitvector |= 1ULL << i; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 354 | // If all of our states become overdefined, bail out early. Since the |
| 355 | // predicate is expensive, only check it every 8 elements. This is only |
| 356 | // really useful for really huge arrays. |
| 357 | if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined && |
| 358 | SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined && |
| 359 | FalseRangeEnd == Overdefined) |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | // Now that we've scanned the entire array, emit our new comparison(s). We |
| 364 | // order the state machines in complexity of the generated code. |
| 365 | Value *Idx = GEP->getOperand(2); |
| 366 | |
Chris Lattner | d7f5a58 | 2010-01-04 18:57:15 +0000 | [diff] [blame] | 367 | // If the index is larger than the pointer size of the target, truncate the |
| 368 | // index down like the GEP would do implicitly. We don't have to do this for |
| 369 | // an inbounds GEP because the index can't be out of range. |
| 370 | if (!GEP->isInBounds() && |
| 371 | Idx->getType()->getPrimitiveSizeInBits() > TD->getPointerSizeInBits()) |
| 372 | Idx = Builder->CreateTrunc(Idx, TD->getIntPtrType(Idx->getContext())); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 374 | // If the comparison is only true for one or two elements, emit direct |
| 375 | // comparisons. |
| 376 | if (SecondTrueElement != Overdefined) { |
| 377 | // None true -> false. |
| 378 | if (FirstTrueElement == Undefined) |
| 379 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(GEP->getContext())); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 381 | Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 383 | // True for one element -> 'i == 47'. |
| 384 | if (SecondTrueElement == Undefined) |
| 385 | return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 387 | // True for two elements -> 'i == 47 | i == 72'. |
| 388 | Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx); |
| 389 | Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement); |
| 390 | Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx); |
| 391 | return BinaryOperator::CreateOr(C1, C2); |
| 392 | } |
| 393 | |
| 394 | // If the comparison is only false for one or two elements, emit direct |
| 395 | // comparisons. |
| 396 | if (SecondFalseElement != Overdefined) { |
| 397 | // None false -> true. |
| 398 | if (FirstFalseElement == Undefined) |
| 399 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(GEP->getContext())); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 401 | Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement); |
| 402 | |
| 403 | // False for one element -> 'i != 47'. |
| 404 | if (SecondFalseElement == Undefined) |
| 405 | return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 407 | // False for two elements -> 'i != 47 & i != 72'. |
| 408 | Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx); |
| 409 | Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement); |
| 410 | Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx); |
| 411 | return BinaryOperator::CreateAnd(C1, C2); |
| 412 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 414 | // If the comparison can be replaced with a range comparison for the elements |
| 415 | // where it is true, emit the range check. |
| 416 | if (TrueRangeEnd != Overdefined) { |
| 417 | assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare"); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 419 | // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1). |
| 420 | if (FirstTrueElement) { |
| 421 | Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement); |
| 422 | Idx = Builder->CreateAdd(Idx, Offs); |
| 423 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 425 | Value *End = ConstantInt::get(Idx->getType(), |
| 426 | TrueRangeEnd-FirstTrueElement+1); |
| 427 | return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End); |
| 428 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 430 | // False range check. |
| 431 | if (FalseRangeEnd != Overdefined) { |
| 432 | assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare"); |
| 433 | // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse). |
| 434 | if (FirstFalseElement) { |
| 435 | Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement); |
| 436 | Idx = Builder->CreateAdd(Idx, Offs); |
| 437 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 439 | Value *End = ConstantInt::get(Idx->getType(), |
| 440 | FalseRangeEnd-FirstFalseElement); |
| 441 | return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End); |
| 442 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 443 | |
| 444 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 445 | // If a 32-bit or 64-bit magic bitvector captures the entire comparison state |
| 446 | // of this load, replace it with computation that does: |
| 447 | // ((magic_cst >> i) & 1) != 0 |
Chris Lattner | c8d75c7 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 448 | if (ArrayElementCount <= 32 || |
| 449 | (TD && ArrayElementCount <= 64 && TD->isLegalInteger(64))) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 450 | Type *Ty; |
Chris Lattner | c8d75c7 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 451 | if (ArrayElementCount <= 32) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 452 | Ty = Type::getInt32Ty(Init->getContext()); |
| 453 | else |
| 454 | Ty = Type::getInt64Ty(Init->getContext()); |
| 455 | Value *V = Builder->CreateIntCast(Idx, Ty, false); |
| 456 | V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V); |
| 457 | V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V); |
| 458 | return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0)); |
| 459 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /// EvaluateGEPOffsetExpression - Return a value that can be used to compare |
| 466 | /// the *offset* implied by a GEP to zero. For example, if we have &A[i], we |
| 467 | /// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can |
| 468 | /// be complex, and scales are involved. The above expression would also be |
| 469 | /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). |
| 470 | /// This later form is less amenable to optimization though, and we are allowed |
| 471 | /// to generate the first by knowing that pointer arithmetic doesn't overflow. |
| 472 | /// |
| 473 | /// If we can't emit an optimized form for this expression, this returns null. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 474 | /// |
Eli Friedman | 107ffd5 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 475 | static Value *EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 476 | TargetData &TD = *IC.getTargetData(); |
| 477 | gep_type_iterator GTI = gep_type_begin(GEP); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 479 | // Check to see if this gep only has a single variable index. If so, and if |
| 480 | // any constant indices are a multiple of its scale, then we can compute this |
| 481 | // in terms of the scale of the variable index. For example, if the GEP |
| 482 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 483 | // because the expression will cross zero at the same point. |
| 484 | unsigned i, e = GEP->getNumOperands(); |
| 485 | int64_t Offset = 0; |
| 486 | for (i = 1; i != e; ++i, ++GTI) { |
| 487 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 488 | // Compute the aggregate offset of constant indices. |
| 489 | if (CI->isZero()) continue; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 491 | // Handle a struct index, which adds its field offset to the pointer. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 492 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 493 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 494 | } else { |
| 495 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
| 496 | Offset += Size*CI->getSExtValue(); |
| 497 | } |
| 498 | } else { |
| 499 | // Found our variable index. |
| 500 | break; |
| 501 | } |
| 502 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 504 | // If there are no variable indices, we must have a constant offset, just |
| 505 | // evaluate it the general way. |
| 506 | if (i == e) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 508 | Value *VariableIdx = GEP->getOperand(i); |
| 509 | // Determine the scale factor of the variable element. For example, this is |
| 510 | // 4 if the variable index is into an array of i32. |
| 511 | uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 512 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 513 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 514 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 515 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 516 | if (!CI) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 518 | // Compute the aggregate offset of constant indices. |
| 519 | if (CI->isZero()) continue; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 521 | // Handle a struct index, which adds its field offset to the pointer. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 522 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 523 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 524 | } else { |
| 525 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
| 526 | Offset += Size*CI->getSExtValue(); |
| 527 | } |
| 528 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 529 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 530 | // Okay, we know we have a single variable index, which must be a |
| 531 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 532 | // the index. |
| 533 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 534 | if (Offset == 0) { |
| 535 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 536 | // we don't need to bother extending: the extension won't affect where the |
| 537 | // computation crosses zero. |
Eli Friedman | 107ffd5 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 538 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 539 | Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext()); |
Eli Friedman | 107ffd5 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 540 | VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy); |
| 541 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 542 | return VariableIdx; |
| 543 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 545 | // Otherwise, there is an index. The computation we will do will be modulo |
| 546 | // the pointer size, so get it. |
| 547 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 549 | Offset &= PtrSizeMask; |
| 550 | VariableScale &= PtrSizeMask; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 552 | // To do this transformation, any constant index must be a multiple of the |
| 553 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 554 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 555 | // multiple of the variable scale. |
| 556 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 557 | if (Offset != NewOffs*(int64_t)VariableScale) |
| 558 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 560 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 561 | Type *IntPtrTy = TD.getIntPtrType(VariableIdx->getContext()); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 562 | if (VariableIdx->getType() != IntPtrTy) |
Eli Friedman | 107ffd5 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 563 | VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy, |
| 564 | true /*Signed*/); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 565 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
Eli Friedman | 107ffd5 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 566 | return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset"); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
| 570 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 571 | Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 572 | ICmpInst::Predicate Cond, |
| 573 | Instruction &I) { |
| 574 | // Look through bitcasts. |
| 575 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) |
| 576 | RHS = BCI->getOperand(0); |
| 577 | |
| 578 | Value *PtrBase = GEPLHS->getOperand(0); |
| 579 | if (TD && PtrBase == RHS && GEPLHS->isInBounds()) { |
| 580 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 581 | // This transformation (ignoring the base and scales) is valid because we |
| 582 | // know pointers can't overflow since the gep is inbounds. See if we can |
| 583 | // output an optimized form. |
Eli Friedman | 107ffd5 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 584 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, *this); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 586 | // If not, synthesize the offset the hard way. |
| 587 | if (Offset == 0) |
| 588 | Offset = EmitGEPOffset(GEPLHS); |
| 589 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 590 | Constant::getNullValue(Offset->getType())); |
| 591 | } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) { |
| 592 | // If the base pointers are different, but the indices are the same, just |
| 593 | // compare the base pointer. |
| 594 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 595 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
| 596 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
| 597 | GEPRHS->getOperand(0)->getType(); |
| 598 | if (IndicesTheSame) |
| 599 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 600 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 601 | IndicesTheSame = false; |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | // If all indices are the same, just compare the base pointers. |
| 606 | if (IndicesTheSame) |
| 607 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), |
| 608 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
| 609 | |
| 610 | // Otherwise, the base pointers are different and the indices are |
| 611 | // different, bail out. |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | // If one of the GEPs has all zero indices, recurse. |
| 616 | bool AllZeros = true; |
| 617 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 618 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 619 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 620 | AllZeros = false; |
| 621 | break; |
| 622 | } |
| 623 | if (AllZeros) |
| 624 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 625 | ICmpInst::getSwappedPredicate(Cond), I); |
| 626 | |
| 627 | // If the other GEP has all zero indices, recurse. |
| 628 | AllZeros = true; |
| 629 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 630 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 631 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 632 | AllZeros = false; |
| 633 | break; |
| 634 | } |
| 635 | if (AllZeros) |
| 636 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 637 | |
Stuart Hastings | 67f071e | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 638 | bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds(); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 639 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 640 | // If the GEPs only differ by one index, compare it. |
| 641 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 642 | unsigned DiffOperand = 0; // The operand that differs. |
| 643 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 644 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 645 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 646 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
| 647 | // Irreconcilable differences. |
| 648 | NumDifferences = 2; |
| 649 | break; |
| 650 | } else { |
| 651 | if (NumDifferences++) break; |
| 652 | DiffOperand = i; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | if (NumDifferences == 0) // SAME GEP? |
| 657 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| 658 | ConstantInt::get(Type::getInt1Ty(I.getContext()), |
| 659 | ICmpInst::isTrueWhenEqual(Cond))); |
| 660 | |
Stuart Hastings | 67f071e | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 661 | else if (NumDifferences == 1 && GEPsInBounds) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 662 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 663 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
| 664 | // Make sure we do a signed comparison here. |
| 665 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // Only lower this if the icmp is the only user of the GEP or if we expect |
| 670 | // the result to fold to a constant! |
| 671 | if (TD && |
Stuart Hastings | 67f071e | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 672 | GEPsInBounds && |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 673 | (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| 674 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 675 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 676 | Value *L = EmitGEPOffset(GEPLHS); |
| 677 | Value *R = EmitGEPOffset(GEPRHS); |
| 678 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
| 679 | } |
| 680 | } |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /// FoldICmpAddOpCst - Fold "icmp pred (X+CI), X". |
| 685 | Instruction *InstCombiner::FoldICmpAddOpCst(ICmpInst &ICI, |
| 686 | Value *X, ConstantInt *CI, |
| 687 | ICmpInst::Predicate Pred, |
| 688 | Value *TheAdd) { |
| 689 | // If we have X+0, exit early (simplifying logic below) and let it get folded |
| 690 | // elsewhere. icmp X+0, X -> icmp X, X |
| 691 | if (CI->isZero()) { |
| 692 | bool isTrue = ICmpInst::isTrueWhenEqual(Pred); |
| 693 | return ReplaceInstUsesWith(ICI, ConstantInt::get(ICI.getType(), isTrue)); |
| 694 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 696 | // (X+4) == X -> false. |
| 697 | if (Pred == ICmpInst::ICMP_EQ) |
| 698 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(X->getContext())); |
| 699 | |
| 700 | // (X+4) != X -> true. |
| 701 | if (Pred == ICmpInst::ICMP_NE) |
| 702 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(X->getContext())); |
| 703 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 704 | // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0, |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 705 | // so the values can never be equal. Similarly for all other "or equals" |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 706 | // operators. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 9aa1e24 | 2010-01-08 17:48:19 +0000 | [diff] [blame] | 708 | // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255 |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 709 | // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253 |
| 710 | // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0 |
| 711 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 712 | Value *R = |
Chris Lattner | 9aa1e24 | 2010-01-08 17:48:19 +0000 | [diff] [blame] | 713 | ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 714 | return new ICmpInst(ICmpInst::ICMP_UGT, X, R); |
| 715 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 717 | // (X+1) >u X --> X <u (0-1) --> X != 255 |
| 718 | // (X+2) >u X --> X <u (0-2) --> X <u 254 |
| 719 | // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0 |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 720 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 721 | return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 723 | unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits(); |
| 724 | ConstantInt *SMax = ConstantInt::get(X->getContext(), |
| 725 | APInt::getSignedMaxValue(BitWidth)); |
| 726 | |
| 727 | // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127 |
| 728 | // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125 |
| 729 | // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0 |
| 730 | // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1 |
| 731 | // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126 |
| 732 | // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127 |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 733 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 734 | return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 736 | // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127 |
| 737 | // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126 |
| 738 | // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1 |
| 739 | // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2 |
| 740 | // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126 |
| 741 | // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128 |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 743 | assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE); |
| 744 | Constant *C = ConstantInt::get(X->getContext(), CI->getValue()-1); |
| 745 | return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C)); |
| 746 | } |
| 747 | |
| 748 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 749 | /// and CmpRHS are both known to be integer constants. |
| 750 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 751 | ConstantInt *DivRHS) { |
| 752 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 753 | const APInt &CmpRHSV = CmpRHS->getValue(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 754 | |
| 755 | // FIXME: If the operand types don't match the type of the divide |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 756 | // then don't attempt this transform. The code below doesn't have the |
| 757 | // logic to deal with a signed divide and an unsigned compare (and |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 758 | // vice versa). This is because (x /s C1) <s C2 produces different |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 759 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 760 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 761 | // work. :( The if statement below tests that condition and bails |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 762 | // if it finds it. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 763 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 764 | if (!ICI.isEquality() && DivIsSigned != ICI.isSigned()) |
| 765 | return 0; |
| 766 | if (DivRHS->isZero()) |
| 767 | return 0; // The ProdOV computation fails on divide by zero. |
| 768 | if (DivIsSigned && DivRHS->isAllOnesValue()) |
| 769 | return 0; // The overflow computation also screws up here |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 770 | if (DivRHS->isOne()) { |
| 771 | // This eliminates some funny cases with INT_MIN. |
| 772 | ICI.setOperand(0, DivI->getOperand(0)); // X/1 == X. |
| 773 | return &ICI; |
| 774 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 775 | |
| 776 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 777 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 778 | // C2 (CI). By solving for X we can turn this into a range check |
| 779 | // instead of computing a divide. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 780 | Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS); |
| 781 | |
| 782 | // Determine if the product overflows by seeing if the product is |
| 783 | // not equal to the divide. Make sure we do the same kind of divide |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 784 | // as in the LHS instruction that we're folding. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 785 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 786 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 787 | |
| 788 | // Get the ICmp opcode |
| 789 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
| 790 | |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 791 | /// If the division is known to be exact, then there is no remainder from the |
| 792 | /// divide, so the covered range size is unit, otherwise it is the divisor. |
| 793 | ConstantInt *RangeSize = DivI->isExact() ? getOne(Prod) : DivRHS; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 794 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 795 | // Figure out the interval that is being checked. For example, a comparison |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 796 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 797 | // Compute this interval based on the constants involved and the signedness of |
| 798 | // the compare/divide. This computes a half-open interval, keeping track of |
| 799 | // whether either value in the interval overflows. After analysis each |
| 800 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 801 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 802 | int LoOverflow = 0, HiOverflow = 0; |
| 803 | Constant *LoBound = 0, *HiBound = 0; |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 804 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 805 | if (!DivIsSigned) { // udiv |
| 806 | // e.g. X/5 op 3 --> [15, 20) |
| 807 | LoBound = Prod; |
| 808 | HiOverflow = LoOverflow = ProdOV; |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 809 | if (!HiOverflow) { |
| 810 | // If this is not an exact divide, then many values in the range collapse |
| 811 | // to the same result value. |
| 812 | HiOverflow = AddWithOverflow(HiBound, LoBound, RangeSize, false); |
| 813 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 814 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 815 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
| 816 | if (CmpRHSV == 0) { // (X / pos) op 0 |
| 817 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 818 | LoBound = ConstantExpr::getNeg(SubOne(RangeSize)); |
| 819 | HiBound = RangeSize; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 820 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
| 821 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 822 | HiOverflow = LoOverflow = ProdOV; |
| 823 | if (!HiOverflow) |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 824 | HiOverflow = AddWithOverflow(HiBound, Prod, RangeSize, true); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 825 | } else { // (X / pos) op neg |
| 826 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
| 827 | HiBound = AddOne(Prod); |
| 828 | LoOverflow = HiOverflow = ProdOV ? -1 : 0; |
| 829 | if (!LoOverflow) { |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 830 | ConstantInt *DivNeg =cast<ConstantInt>(ConstantExpr::getNeg(RangeSize)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 831 | LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0; |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 832 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 833 | } |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 834 | } else if (DivRHS->isNegative()) { // Divisor is < 0. |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 835 | if (DivI->isExact()) |
| 836 | RangeSize = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 837 | if (CmpRHSV == 0) { // (X / neg) op 0 |
| 838 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 839 | LoBound = AddOne(RangeSize); |
| 840 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 841 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 842 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 843 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 844 | } |
| 845 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
| 846 | // e.g. X/-5 op 3 --> [-19, -14) |
| 847 | HiBound = AddOne(Prod); |
| 848 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
| 849 | if (!LoOverflow) |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 850 | LoOverflow = AddWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 851 | } else { // (X / neg) op neg |
| 852 | LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20) |
| 853 | LoOverflow = HiOverflow = ProdOV; |
| 854 | if (!HiOverflow) |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 855 | HiOverflow = SubWithOverflow(HiBound, Prod, RangeSize, true); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 856 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 858 | // Dividing by a negative swaps the condition. LT <-> GT |
| 859 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 860 | } |
| 861 | |
| 862 | Value *X = DivI->getOperand(0); |
| 863 | switch (Pred) { |
| 864 | default: llvm_unreachable("Unhandled icmp opcode!"); |
| 865 | case ICmpInst::ICMP_EQ: |
| 866 | if (LoOverflow && HiOverflow) |
| 867 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext())); |
Chris Lattner | f34f48c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 868 | if (HiOverflow) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 869 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 870 | ICmpInst::ICMP_UGE, X, LoBound); |
Chris Lattner | f34f48c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 871 | if (LoOverflow) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 872 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 873 | ICmpInst::ICMP_ULT, X, HiBound); |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 874 | return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound, |
| 875 | DivIsSigned, true)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 876 | case ICmpInst::ICMP_NE: |
| 877 | if (LoOverflow && HiOverflow) |
| 878 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext())); |
Chris Lattner | f34f48c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 879 | if (HiOverflow) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 880 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 881 | ICmpInst::ICMP_ULT, X, LoBound); |
Chris Lattner | f34f48c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 882 | if (LoOverflow) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 883 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 884 | ICmpInst::ICMP_UGE, X, HiBound); |
Chris Lattner | f34f48c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 885 | return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound, |
| 886 | DivIsSigned, false)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 887 | case ICmpInst::ICMP_ULT: |
| 888 | case ICmpInst::ICMP_SLT: |
| 889 | if (LoOverflow == +1) // Low bound is greater than input range. |
| 890 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext())); |
| 891 | if (LoOverflow == -1) // Low bound is less than input range. |
| 892 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext())); |
| 893 | return new ICmpInst(Pred, X, LoBound); |
| 894 | case ICmpInst::ICMP_UGT: |
| 895 | case ICmpInst::ICMP_SGT: |
| 896 | if (HiOverflow == +1) // High bound greater than input range. |
| 897 | return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(ICI.getContext())); |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 898 | if (HiOverflow == -1) // High bound less than input range. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 899 | return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(ICI.getContext())); |
| 900 | if (Pred == ICmpInst::ICMP_UGT) |
| 901 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 902 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 906 | /// FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)". |
| 907 | Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr, |
| 908 | ConstantInt *ShAmt) { |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 909 | const APInt &CmpRHSV = cast<ConstantInt>(ICI.getOperand(1))->getValue(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 910 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 911 | // Check that the shift amount is in range. If not, don't perform |
| 912 | // undefined shifts. When the shift is visited it will be |
| 913 | // simplified. |
| 914 | uint32_t TypeBits = CmpRHSV.getBitWidth(); |
| 915 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 916 | if (ShAmtVal >= TypeBits || ShAmtVal == 0) |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 917 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 918 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 919 | if (!ICI.isEquality()) { |
| 920 | // If we have an unsigned comparison and an ashr, we can't simplify this. |
| 921 | // Similarly for signed comparisons with lshr. |
| 922 | if (ICI.isSigned() != (Shr->getOpcode() == Instruction::AShr)) |
| 923 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 924 | |
Eli Friedman | a831a9b | 2011-05-25 23:26:20 +0000 | [diff] [blame] | 925 | // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv |
| 926 | // by a power of 2. Since we already have logic to simplify these, |
| 927 | // transform to div and then simplify the resultant comparison. |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 928 | if (Shr->getOpcode() == Instruction::AShr && |
Eli Friedman | a831a9b | 2011-05-25 23:26:20 +0000 | [diff] [blame] | 929 | (!Shr->isExact() || ShAmtVal == TypeBits - 1)) |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 930 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 931 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 932 | // Revisit the shift (to delete it). |
| 933 | Worklist.Add(Shr); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 934 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 935 | Constant *DivCst = |
| 936 | ConstantInt::get(Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 937 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 938 | Value *Tmp = |
| 939 | Shr->getOpcode() == Instruction::AShr ? |
| 940 | Builder->CreateSDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()) : |
| 941 | Builder->CreateUDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 942 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 943 | ICI.setOperand(0, Tmp); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 944 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 945 | // If the builder folded the binop, just return it. |
| 946 | BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp); |
| 947 | if (TheDiv == 0) |
| 948 | return &ICI; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 949 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 950 | // Otherwise, fold this div/compare. |
| 951 | assert(TheDiv->getOpcode() == Instruction::SDiv || |
| 952 | TheDiv->getOpcode() == Instruction::UDiv); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 953 | |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 954 | Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst)); |
| 955 | assert(Res && "This div/cst should have folded!"); |
| 956 | return Res; |
| 957 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 958 | |
| 959 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 960 | // If we are comparing against bits always shifted out, the |
| 961 | // comparison cannot succeed. |
| 962 | APInt Comp = CmpRHSV << ShAmtVal; |
| 963 | ConstantInt *ShiftedCmpRHS = ConstantInt::get(ICI.getContext(), Comp); |
| 964 | if (Shr->getOpcode() == Instruction::LShr) |
| 965 | Comp = Comp.lshr(ShAmtVal); |
| 966 | else |
| 967 | Comp = Comp.ashr(ShAmtVal); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 968 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 969 | if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero. |
| 970 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 971 | Constant *Cst = ConstantInt::get(Type::getInt1Ty(ICI.getContext()), |
| 972 | IsICMP_NE); |
| 973 | return ReplaceInstUsesWith(ICI, Cst); |
| 974 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 975 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 976 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 977 | // If so, we can compare against the unshifted value: |
| 978 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Chris Lattner | e5116f8 | 2011-02-13 18:30:09 +0000 | [diff] [blame] | 979 | if (Shr->hasOneUse() && Shr->isExact()) |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 980 | return new ICmpInst(ICI.getPredicate(), Shr->getOperand(0), ShiftedCmpRHS); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 981 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 982 | if (Shr->hasOneUse()) { |
| 983 | // Otherwise strength reduce the shift into an and. |
| 984 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 985 | Constant *Mask = ConstantInt::get(ICI.getContext(), Val); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 986 | |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 987 | Value *And = Builder->CreateAnd(Shr->getOperand(0), |
| 988 | Mask, Shr->getName()+".mask"); |
| 989 | return new ICmpInst(ICI.getPredicate(), And, ShiftedCmpRHS); |
| 990 | } |
| 991 | return 0; |
| 992 | } |
| 993 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 994 | |
| 995 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 996 | /// |
| 997 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 998 | Instruction *LHSI, |
| 999 | ConstantInt *RHS) { |
| 1000 | const APInt &RHSV = RHS->getValue(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1002 | switch (LHSI->getOpcode()) { |
| 1003 | case Instruction::Trunc: |
| 1004 | if (ICI.isEquality() && LHSI->hasOneUse()) { |
| 1005 | // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all |
| 1006 | // of the high bits truncated out of x are known. |
| 1007 | unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(), |
| 1008 | SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1009 | APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits)); |
| 1010 | APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0); |
| 1011 | ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1013 | // If all the high bits are known, we can do this xform. |
| 1014 | if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) { |
| 1015 | // Pull in the high bits from known-ones set. |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1016 | APInt NewRHS = RHS->getValue().zext(SrcBits); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1017 | NewRHS |= KnownOne; |
| 1018 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 1019 | ConstantInt::get(ICI.getContext(), NewRHS)); |
| 1020 | } |
| 1021 | } |
| 1022 | break; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1024 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
| 1025 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 1026 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 1027 | // fold the xor. |
| 1028 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 1029 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
| 1030 | Value *CompareVal = LHSI->getOperand(0); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1032 | // If the sign bit of the XorCST is not set, there is no change to |
| 1033 | // the operation, just stop using the Xor. |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1034 | if (!XorCST->isNegative()) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1035 | ICI.setOperand(0, CompareVal); |
| 1036 | Worklist.Add(LHSI); |
| 1037 | return &ICI; |
| 1038 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1040 | // Was the old condition true if the operand is positive? |
| 1041 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1042 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1043 | // If so, the new one isn't. |
| 1044 | isTrueIfPositive ^= true; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1046 | if (isTrueIfPositive) |
| 1047 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, |
| 1048 | SubOne(RHS)); |
| 1049 | else |
| 1050 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, |
| 1051 | AddOne(RHS)); |
| 1052 | } |
| 1053 | |
| 1054 | if (LHSI->hasOneUse()) { |
| 1055 | // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit)) |
| 1056 | if (!ICI.isEquality() && XorCST->getValue().isSignBit()) { |
| 1057 | const APInt &SignBit = XorCST->getValue(); |
| 1058 | ICmpInst::Predicate Pred = ICI.isSigned() |
| 1059 | ? ICI.getUnsignedPredicate() |
| 1060 | : ICI.getSignedPredicate(); |
| 1061 | return new ICmpInst(Pred, LHSI->getOperand(0), |
| 1062 | ConstantInt::get(ICI.getContext(), |
| 1063 | RHSV ^ SignBit)); |
| 1064 | } |
| 1065 | |
| 1066 | // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A) |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1067 | if (!ICI.isEquality() && XorCST->isMaxValue(true)) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1068 | const APInt &NotSignBit = XorCST->getValue(); |
| 1069 | ICmpInst::Predicate Pred = ICI.isSigned() |
| 1070 | ? ICI.getUnsignedPredicate() |
| 1071 | : ICI.getSignedPredicate(); |
| 1072 | Pred = ICI.getSwappedPredicate(Pred); |
| 1073 | return new ICmpInst(Pred, LHSI->getOperand(0), |
| 1074 | ConstantInt::get(ICI.getContext(), |
| 1075 | RHSV ^ NotSignBit)); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | break; |
| 1080 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 1081 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 1082 | LHSI->getOperand(0)->hasOneUse()) { |
| 1083 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1085 | // If the LHS is an AND of a truncating cast, we can widen the |
| 1086 | // and/compare to be the input width without changing the value |
| 1087 | // produced, eliminating a cast. |
| 1088 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 1089 | // We can do this transformation if either the AND constant does not |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1090 | // have its sign bit set or if it is an equality comparison. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1091 | // Extending a relational comparison when we're checking the sign |
| 1092 | // bit would not work. |
Benjamin Kramer | 7e7c9cc | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1093 | if (ICI.isEquality() || |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1094 | (!AndCST->isNegative() && RHSV.isNonNegative())) { |
Benjamin Kramer | 7e7c9cc | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1095 | Value *NewAnd = |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1096 | Builder->CreateAnd(Cast->getOperand(0), |
Benjamin Kramer | 7e7c9cc | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1097 | ConstantExpr::getZExt(AndCST, Cast->getSrcTy())); |
| 1098 | NewAnd->takeName(LHSI); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1099 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
Benjamin Kramer | 7e7c9cc | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1100 | ConstantExpr::getZExt(RHS, Cast->getSrcTy())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
Benjamin Kramer | ffd0ae6 | 2011-06-12 22:48:00 +0000 | [diff] [blame] | 1103 | |
| 1104 | // If the LHS is an AND of a zext, and we have an equality compare, we can |
| 1105 | // shrink the and/compare to the smaller type, eliminating the cast. |
| 1106 | if (ZExtInst *Cast = dyn_cast<ZExtInst>(LHSI->getOperand(0))) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1107 | IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy()); |
Benjamin Kramer | ffd0ae6 | 2011-06-12 22:48:00 +0000 | [diff] [blame] | 1108 | // Make sure we don't compare the upper bits, SimplifyDemandedBits |
| 1109 | // should fold the icmp to true/false in that case. |
| 1110 | if (ICI.isEquality() && RHSV.getActiveBits() <= Ty->getBitWidth()) { |
| 1111 | Value *NewAnd = |
| 1112 | Builder->CreateAnd(Cast->getOperand(0), |
| 1113 | ConstantExpr::getTrunc(AndCST, Ty)); |
| 1114 | NewAnd->takeName(LHSI); |
| 1115 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 1116 | ConstantExpr::getTrunc(RHS, Ty)); |
| 1117 | } |
| 1118 | } |
| 1119 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1120 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 1121 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 1122 | // happens a LOT in code produced by the C front-end, for bitfield |
| 1123 | // access. |
| 1124 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 1125 | if (Shift && !Shift->isShift()) |
| 1126 | Shift = 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1128 | ConstantInt *ShAmt; |
| 1129 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1130 | Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 1131 | Type *AndTy = AndCST->getType(); // Type of the and. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1133 | // We can fold this as long as we can't shift unknown bits |
| 1134 | // into the mask. This can only happen with signed shift |
| 1135 | // rights, as they sign-extend. |
| 1136 | if (ShAmt) { |
| 1137 | bool CanFold = Shift->isLogicalShift(); |
| 1138 | if (!CanFold) { |
| 1139 | // To test for the bad case of the signed shr, see if any |
| 1140 | // of the bits shifted in could be tested after the mask. |
| 1141 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 1142 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1144 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1145 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1146 | AndCST->getValue()) == 0) |
| 1147 | CanFold = true; |
| 1148 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1150 | if (CanFold) { |
| 1151 | Constant *NewCst; |
| 1152 | if (Shift->getOpcode() == Instruction::Shl) |
| 1153 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 1154 | else |
| 1155 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1157 | // Check to see if we are shifting out any of the bits being |
| 1158 | // compared. |
| 1159 | if (ConstantExpr::get(Shift->getOpcode(), |
| 1160 | NewCst, ShAmt) != RHS) { |
| 1161 | // If we shifted bits out, the fold is not going to work out. |
| 1162 | // As a special case, check to see if this means that the |
| 1163 | // result is always true or false now. |
| 1164 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 1165 | return ReplaceInstUsesWith(ICI, |
| 1166 | ConstantInt::getFalse(ICI.getContext())); |
| 1167 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| 1168 | return ReplaceInstUsesWith(ICI, |
| 1169 | ConstantInt::getTrue(ICI.getContext())); |
| 1170 | } else { |
| 1171 | ICI.setOperand(1, NewCst); |
| 1172 | Constant *NewAndCST; |
| 1173 | if (Shift->getOpcode() == Instruction::Shl) |
| 1174 | NewAndCST = ConstantExpr::getLShr(AndCST, ShAmt); |
| 1175 | else |
| 1176 | NewAndCST = ConstantExpr::getShl(AndCST, ShAmt); |
| 1177 | LHSI->setOperand(1, NewAndCST); |
| 1178 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 1179 | Worklist.Add(Shift); // Shift is dead. |
| 1180 | return &ICI; |
| 1181 | } |
| 1182 | } |
| 1183 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1184 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1185 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 1186 | // preferable because it allows the C<<Y expression to be hoisted out |
| 1187 | // of a loop if Y is invariant and X is not. |
| 1188 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 1189 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 1190 | !isa<Constant>(Shift->getOperand(0))) { |
| 1191 | // Compute C << Y. |
| 1192 | Value *NS; |
| 1193 | if (Shift->getOpcode() == Instruction::LShr) { |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1194 | NS = Builder->CreateShl(AndCST, Shift->getOperand(1)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1195 | } else { |
| 1196 | // Insert a logical shift. |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1197 | NS = Builder->CreateLShr(AndCST, Shift->getOperand(1)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1198 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1200 | // Compute X & (C << Y). |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1201 | Value *NewAnd = |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1202 | Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1204 | ICI.setOperand(0, NewAnd); |
| 1205 | return &ICI; |
| 1206 | } |
| 1207 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1209 | // Try to optimize things like "A[i]&42 == 0" to index computations. |
| 1210 | if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) { |
| 1211 | if (GetElementPtrInst *GEP = |
| 1212 | dyn_cast<GetElementPtrInst>(LI->getOperand(0))) |
| 1213 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 1214 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 1215 | !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) { |
| 1216 | ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1217 | if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C)) |
| 1218 | return Res; |
| 1219 | } |
| 1220 | } |
| 1221 | break; |
| 1222 | |
| 1223 | case Instruction::Or: { |
| 1224 | if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse()) |
| 1225 | break; |
| 1226 | Value *P, *Q; |
| 1227 | if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) { |
| 1228 | // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0 |
| 1229 | // -> and (icmp eq P, null), (icmp eq Q, null). |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1230 | Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P, |
| 1231 | Constant::getNullValue(P->getType())); |
| 1232 | Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q, |
| 1233 | Constant::getNullValue(Q->getType())); |
| 1234 | Instruction *Op; |
| 1235 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 1236 | Op = BinaryOperator::CreateAnd(ICIP, ICIQ); |
| 1237 | else |
| 1238 | Op = BinaryOperator::CreateOr(ICIP, ICIQ); |
| 1239 | return Op; |
| 1240 | } |
| 1241 | break; |
| 1242 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1244 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 1245 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 1246 | if (!ShAmt) break; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1248 | uint32_t TypeBits = RHSV.getBitWidth(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1250 | // Check that the shift amount is in range. If not, don't perform |
| 1251 | // undefined shifts. When the shift is visited it will be |
| 1252 | // simplified. |
| 1253 | if (ShAmt->uge(TypeBits)) |
| 1254 | break; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1255 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1256 | if (ICI.isEquality()) { |
| 1257 | // If we are comparing against bits always shifted out, the |
| 1258 | // comparison cannot succeed. |
| 1259 | Constant *Comp = |
| 1260 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), |
| 1261 | ShAmt); |
| 1262 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 1263 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 1264 | Constant *Cst = |
| 1265 | ConstantInt::get(Type::getInt1Ty(ICI.getContext()), IsICMP_NE); |
| 1266 | return ReplaceInstUsesWith(ICI, Cst); |
| 1267 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1269 | // If the shift is NUW, then it is just shifting out zeros, no need for an |
| 1270 | // AND. |
| 1271 | if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap()) |
| 1272 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 1273 | ConstantExpr::getLShr(RHS, ShAmt)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1275 | if (LHSI->hasOneUse()) { |
| 1276 | // Otherwise strength reduce the shift into an and. |
| 1277 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 1278 | Constant *Mask = |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1279 | ConstantInt::get(ICI.getContext(), APInt::getLowBitsSet(TypeBits, |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1280 | TypeBits-ShAmtVal)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1281 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1282 | Value *And = |
| 1283 | Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask"); |
| 1284 | return new ICmpInst(ICI.getPredicate(), And, |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1285 | ConstantExpr::getLShr(RHS, ShAmt)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1286 | } |
| 1287 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1289 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 1290 | bool TrueIfSigned = false; |
| 1291 | if (LHSI->hasOneUse() && |
| 1292 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 1293 | // (X << 31) <s 0 --> (X&1) != 0 |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1294 | Constant *Mask = ConstantInt::get(LHSI->getOperand(0)->getType(), |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1295 | APInt::getOneBitSet(TypeBits, |
Chris Lattner | bb75d33 | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1296 | TypeBits-ShAmt->getZExtValue()-1)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1297 | Value *And = |
| 1298 | Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask"); |
| 1299 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 1300 | And, Constant::getNullValue(And->getType())); |
| 1301 | } |
| 1302 | break; |
| 1303 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1305 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Nick Lewycky | b042f8e | 2011-02-28 08:31:40 +0000 | [diff] [blame] | 1306 | case Instruction::AShr: { |
| 1307 | // Handle equality comparisons of shift-by-constant. |
| 1308 | BinaryOperator *BO = cast<BinaryOperator>(LHSI); |
| 1309 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 1310 | if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt)) |
Chris Lattner | 74542aa | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1311 | return Res; |
Nick Lewycky | b042f8e | 2011-02-28 08:31:40 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | // Handle exact shr's. |
| 1315 | if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) { |
| 1316 | if (RHSV.isMinValue()) |
| 1317 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS); |
| 1318 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1319 | break; |
Nick Lewycky | b042f8e | 2011-02-28 08:31:40 +0000 | [diff] [blame] | 1320 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1321 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1322 | case Instruction::SDiv: |
| 1323 | case Instruction::UDiv: |
| 1324 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1325 | // Fold this div into the comparison, producing a range check. |
| 1326 | // Determine, based on the divide type, what the range is being |
| 1327 | // checked. If there is an overflow on the low or high side, remember |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1328 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 1329 | // See: InsertRangeTest above for the kinds of replacements possible. |
| 1330 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 1331 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 1332 | DivRHS)) |
| 1333 | return R; |
| 1334 | break; |
| 1335 | |
| 1336 | case Instruction::Add: |
| 1337 | // Fold: icmp pred (add X, C1), C2 |
| 1338 | if (!ICI.isEquality()) { |
| 1339 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 1340 | if (!LHSC) break; |
| 1341 | const APInt &LHSV = LHSC->getValue(); |
| 1342 | |
| 1343 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 1344 | .subtract(LHSV); |
| 1345 | |
| 1346 | if (ICI.isSigned()) { |
| 1347 | if (CR.getLower().isSignBit()) { |
| 1348 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| 1349 | ConstantInt::get(ICI.getContext(),CR.getUpper())); |
| 1350 | } else if (CR.getUpper().isSignBit()) { |
| 1351 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| 1352 | ConstantInt::get(ICI.getContext(),CR.getLower())); |
| 1353 | } |
| 1354 | } else { |
| 1355 | if (CR.getLower().isMinValue()) { |
| 1356 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| 1357 | ConstantInt::get(ICI.getContext(),CR.getUpper())); |
| 1358 | } else if (CR.getUpper().isMinValue()) { |
| 1359 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| 1360 | ConstantInt::get(ICI.getContext(),CR.getLower())); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | break; |
| 1365 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1367 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 1368 | if (ICI.isEquality()) { |
| 1369 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1370 | |
| 1371 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1372 | // the second operand is a constant, simplify a bit. |
| 1373 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 1374 | switch (BO->getOpcode()) { |
| 1375 | case Instruction::SRem: |
| 1376 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 1377 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 1378 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 1379 | if (V.sgt(1) && V.isPowerOf2()) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1380 | Value *NewRem = |
| 1381 | Builder->CreateURem(BO->getOperand(0), BO->getOperand(1), |
| 1382 | BO->getName()); |
| 1383 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 1384 | Constant::getNullValue(BO->getType())); |
| 1385 | } |
| 1386 | } |
| 1387 | break; |
| 1388 | case Instruction::Add: |
| 1389 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 1390 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 1391 | if (BO->hasOneUse()) |
| 1392 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 1393 | ConstantExpr::getSub(RHS, BOp1C)); |
| 1394 | } else if (RHSV == 0) { |
| 1395 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 1396 | // efficiently invertible, or if the add has just this one use. |
| 1397 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1398 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1399 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 1400 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
Chris Lattner | 5036ce4 | 2011-04-26 20:02:45 +0000 | [diff] [blame] | 1401 | if (Value *NegVal = dyn_castNegVal(BOp0)) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1402 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
Chris Lattner | 5036ce4 | 2011-04-26 20:02:45 +0000 | [diff] [blame] | 1403 | if (BO->hasOneUse()) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1404 | Value *Neg = Builder->CreateNeg(BOp1); |
| 1405 | Neg->takeName(BO); |
| 1406 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 1407 | } |
| 1408 | } |
| 1409 | break; |
| 1410 | case Instruction::Xor: |
| 1411 | // For the xor case, we can xor two constants together, eliminating |
| 1412 | // the explicit xor. |
Benjamin Kramer | e7fdcad | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 1413 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 1414 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1415 | ConstantExpr::getXor(RHS, BOC)); |
Benjamin Kramer | e7fdcad | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 1416 | } else if (RHSV == 0) { |
| 1417 | // Replace ((xor A, B) != 0) with (A != B) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1418 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 1419 | BO->getOperand(1)); |
Benjamin Kramer | e7fdcad | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 1420 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1421 | break; |
Benjamin Kramer | e7fdcad | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 1422 | case Instruction::Sub: |
| 1423 | // Replace ((sub A, B) != C) with (B != A-C) if A & C are constants. |
| 1424 | if (ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) { |
| 1425 | if (BO->hasOneUse()) |
| 1426 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(1), |
| 1427 | ConstantExpr::getSub(BOp0C, RHS)); |
| 1428 | } else if (RHSV == 0) { |
| 1429 | // Replace ((sub A, B) != 0) with (A != B) |
| 1430 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 1431 | BO->getOperand(1)); |
| 1432 | } |
| 1433 | break; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1434 | case Instruction::Or: |
| 1435 | // If bits are being or'd in that are not present in the constant we |
| 1436 | // are comparing against, then the comparison could never succeed! |
Eli Friedman | 618898e | 2010-07-29 18:03:33 +0000 | [diff] [blame] | 1437 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1438 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 1439 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
| 1440 | return ReplaceInstUsesWith(ICI, |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1441 | ConstantInt::get(Type::getInt1Ty(ICI.getContext()), |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1442 | isICMP_NE)); |
| 1443 | } |
| 1444 | break; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1446 | case Instruction::And: |
| 1447 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 1448 | // If bits are being compared against that are and'd out, then the |
| 1449 | // comparison can never succeed! |
| 1450 | if ((RHSV & ~BOC->getValue()) != 0) |
| 1451 | return ReplaceInstUsesWith(ICI, |
| 1452 | ConstantInt::get(Type::getInt1Ty(ICI.getContext()), |
| 1453 | isICMP_NE)); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1454 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1455 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 1456 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 1457 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 1458 | ICmpInst::ICMP_NE, LHSI, |
| 1459 | Constant::getNullValue(RHS->getType())); |
Benjamin Kramer | fc87cdc | 2011-07-04 20:16:36 +0000 | [diff] [blame] | 1460 | |
| 1461 | // Don't perform the following transforms if the AND has multiple uses |
| 1462 | if (!BO->hasOneUse()) |
| 1463 | break; |
| 1464 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1465 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 1466 | if (BOC->getValue().isSignBit()) { |
| 1467 | Value *X = BO->getOperand(0); |
| 1468 | Constant *Zero = Constant::getNullValue(X->getType()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1469 | ICmpInst::Predicate pred = isICMP_NE ? |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1470 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 1471 | return new ICmpInst(pred, X, Zero); |
| 1472 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1474 | // ((X & ~7) == 0) --> X < 8 |
| 1475 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 1476 | Value *X = BO->getOperand(0); |
| 1477 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1478 | ICmpInst::Predicate pred = isICMP_NE ? |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1479 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 1480 | return new ICmpInst(pred, X, NegX); |
| 1481 | } |
| 1482 | } |
| 1483 | default: break; |
| 1484 | } |
| 1485 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 1486 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
Chris Lattner | 0335740 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 1487 | switch (II->getIntrinsicID()) { |
| 1488 | case Intrinsic::bswap: |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1489 | Worklist.Add(II); |
Gabor Greif | caf70b3 | 2010-06-24 16:11:44 +0000 | [diff] [blame] | 1490 | ICI.setOperand(0, II->getArgOperand(0)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1491 | ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap())); |
| 1492 | return &ICI; |
Chris Lattner | 0335740 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 1493 | case Intrinsic::ctlz: |
| 1494 | case Intrinsic::cttz: |
| 1495 | // ctz(A) == bitwidth(a) -> A == 0 and likewise for != |
| 1496 | if (RHSV == RHS->getType()->getBitWidth()) { |
| 1497 | Worklist.Add(II); |
Gabor Greif | caf70b3 | 2010-06-24 16:11:44 +0000 | [diff] [blame] | 1498 | ICI.setOperand(0, II->getArgOperand(0)); |
Chris Lattner | 0335740 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 1499 | ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0)); |
| 1500 | return &ICI; |
| 1501 | } |
| 1502 | break; |
| 1503 | case Intrinsic::ctpop: |
| 1504 | // popcount(A) == 0 -> A == 0 and likewise for != |
| 1505 | if (RHS->isZero()) { |
| 1506 | Worklist.Add(II); |
Gabor Greif | caf70b3 | 2010-06-24 16:11:44 +0000 | [diff] [blame] | 1507 | ICI.setOperand(0, II->getArgOperand(0)); |
Chris Lattner | 0335740 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 1508 | ICI.setOperand(1, RHS); |
| 1509 | return &ICI; |
| 1510 | } |
| 1511 | break; |
| 1512 | default: |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1513 | break; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 1521 | /// We only handle extending casts so far. |
| 1522 | /// |
| 1523 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 1524 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
| 1525 | Value *LHSCIOp = LHSCI->getOperand(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1526 | Type *SrcTy = LHSCIOp->getType(); |
| 1527 | Type *DestTy = LHSCI->getType(); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1528 | Value *RHSCIOp; |
| 1529 | |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1530 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1531 | // integer type is the same size as the pointer type. |
| 1532 | if (TD && LHSCI->getOpcode() == Instruction::PtrToInt && |
| 1533 | TD->getPointerSizeInBits() == |
| 1534 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 1535 | Value *RHSOp = 0; |
| 1536 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
| 1537 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
| 1538 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 1539 | RHSOp = RHSC->getOperand(0); |
| 1540 | // If the pointer types don't match, insert a bitcast. |
| 1541 | if (LHSCIOp->getType() != RHSOp->getType()) |
| 1542 | RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType()); |
| 1543 | } |
| 1544 | |
| 1545 | if (RHSOp) |
| 1546 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 1547 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1549 | // The code below only handles extension cast instructions, so far. |
| 1550 | // Enforce this. |
| 1551 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 1552 | LHSCI->getOpcode() != Instruction::SExt) |
| 1553 | return 0; |
| 1554 | |
| 1555 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 1556 | bool isSignedCmp = ICI.isSigned(); |
| 1557 | |
| 1558 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
| 1559 | // Not an extension from the same type? |
| 1560 | RHSCIOp = CI->getOperand(0); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1561 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1562 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1564 | // If the signedness of the two casts doesn't agree (i.e. one is a sext |
| 1565 | // and the other is a zext), then we can't handle this. |
| 1566 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 1567 | return 0; |
| 1568 | |
| 1569 | // Deal with equality cases early. |
| 1570 | if (ICI.isEquality()) |
| 1571 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 1572 | |
| 1573 | // A signed comparison of sign extended values simplifies into a |
| 1574 | // signed comparison. |
| 1575 | if (isSignedCmp && isSignedExt) |
| 1576 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 1577 | |
| 1578 | // The other three cases all fold into an unsigned comparison. |
| 1579 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
| 1580 | } |
| 1581 | |
| 1582 | // If we aren't dealing with a constant on the RHS, exit early |
| 1583 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 1584 | if (!CI) |
| 1585 | return 0; |
| 1586 | |
| 1587 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1588 | // reextended to DestTy. |
| 1589 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 1590 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), |
| 1591 | Res1, DestTy); |
| 1592 | |
| 1593 | // If the re-extended constant didn't change... |
| 1594 | if (Res2 == CI) { |
| 1595 | // Deal with equality cases early. |
| 1596 | if (ICI.isEquality()) |
| 1597 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 1598 | |
| 1599 | // A signed comparison of sign extended values simplifies into a |
| 1600 | // signed comparison. |
| 1601 | if (isSignedExt && isSignedCmp) |
| 1602 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 1603 | |
| 1604 | // The other three cases all fold into an unsigned comparison. |
| 1605 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1); |
| 1606 | } |
| 1607 | |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1608 | // The re-extended constant changed so the constant cannot be represented |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1609 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1610 | // All the cases that fold to true or false will have already been handled |
| 1611 | // by SimplifyICmpInst, so only deal with the tricky case. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1612 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1613 | if (isSignedCmp || !isSignedExt) |
| 1614 | return 0; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1615 | |
| 1616 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 1617 | // should have been folded away previously and not enter in here. |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1618 | |
| 1619 | // We're performing an unsigned comp with a sign extended value. |
| 1620 | // This is true if the input is >= 0. [aka >s -1] |
| 1621 | Constant *NegOne = Constant::getAllOnesValue(SrcTy); |
| 1622 | Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName()); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1623 | |
| 1624 | // Finally, return the value computed. |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1625 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1626 | return ReplaceInstUsesWith(ICI, Result); |
| 1627 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 1628 | assert(ICI.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!"); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1629 | return BinaryOperator::CreateNot(Result); |
| 1630 | } |
| 1631 | |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1632 | /// ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form: |
| 1633 | /// I = icmp ugt (add (add A, B), CI2), CI1 |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1634 | /// If this is of the form: |
| 1635 | /// sum = a + b |
| 1636 | /// if (sum+128 >u 255) |
| 1637 | /// Then replace it with llvm.sadd.with.overflow.i8. |
| 1638 | /// |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1639 | static Instruction *ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, |
| 1640 | ConstantInt *CI2, ConstantInt *CI1, |
Chris Lattner | 0fe80bb | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 1641 | InstCombiner &IC) { |
Chris Lattner | 368397b | 2010-12-19 17:59:02 +0000 | [diff] [blame] | 1642 | // The transformation we're trying to do here is to transform this into an |
| 1643 | // llvm.sadd.with.overflow. To do this, we have to replace the original add |
| 1644 | // with a narrower add, and discard the add-with-constant that is part of the |
| 1645 | // range check (if we can't eliminate it, this isn't profitable). |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1646 | |
Chris Lattner | 368397b | 2010-12-19 17:59:02 +0000 | [diff] [blame] | 1647 | // In order to eliminate the add-with-constant, the compare can be its only |
| 1648 | // use. |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1649 | Instruction *AddWithCst = cast<Instruction>(I.getOperand(0)); |
Chris Lattner | 368397b | 2010-12-19 17:59:02 +0000 | [diff] [blame] | 1650 | if (!AddWithCst->hasOneUse()) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1652 | // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow. |
| 1653 | if (!CI2->getValue().isPowerOf2()) return 0; |
| 1654 | unsigned NewWidth = CI2->getValue().countTrailingZeros(); |
| 1655 | if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1657 | // The width of the new add formed is 1 more than the bias. |
| 1658 | ++NewWidth; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1660 | // Check to see that CI1 is an all-ones value with NewWidth bits. |
| 1661 | if (CI1->getBitWidth() == NewWidth || |
| 1662 | CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth)) |
| 1663 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1664 | |
Eli Friedman | 54b9211 | 2011-11-28 23:32:19 +0000 | [diff] [blame] | 1665 | // This is only really a signed overflow check if the inputs have been |
| 1666 | // sign-extended; check for that condition. For example, if CI2 is 2^31 and |
| 1667 | // the operands of the add are 64 bits wide, we need at least 33 sign bits. |
| 1668 | unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1; |
| 1669 | if (IC.ComputeNumSignBits(A) < NeededSignBits || |
| 1670 | IC.ComputeNumSignBits(B) < NeededSignBits) |
| 1671 | return 0; |
| 1672 | |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1673 | // In order to replace the original add with a narrower |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1674 | // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant |
| 1675 | // and truncates that discard the high bits of the add. Verify that this is |
| 1676 | // the case. |
| 1677 | Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0)); |
| 1678 | for (Value::use_iterator UI = OrigAdd->use_begin(), E = OrigAdd->use_end(); |
| 1679 | UI != E; ++UI) { |
| 1680 | if (*UI == AddWithCst) continue; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1681 | |
Chris Lattner | dd7e837 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 1682 | // Only accept truncates for now. We would really like a nice recursive |
| 1683 | // predicate like SimplifyDemandedBits, but which goes downwards the use-def |
| 1684 | // chain to see which bits of a value are actually demanded. If the |
| 1685 | // original add had another add which was then immediately truncated, we |
| 1686 | // could still do the transformation. |
| 1687 | TruncInst *TI = dyn_cast<TruncInst>(*UI); |
| 1688 | if (TI == 0 || |
| 1689 | TI->getType()->getPrimitiveSizeInBits() > NewWidth) return 0; |
| 1690 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1692 | // If the pattern matches, truncate the inputs to the narrower type and |
| 1693 | // use the sadd_with_overflow intrinsic to efficiently compute both the |
| 1694 | // result and the overflow bit. |
Chris Lattner | 0a62474 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 1695 | Module *M = I.getParent()->getParent()->getParent(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1696 | |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1697 | Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth); |
Chris Lattner | 0a62474 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 1698 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow, |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 1699 | NewType); |
Chris Lattner | 0a62474 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 0fe80bb | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 1701 | InstCombiner::BuilderTy *Builder = IC.Builder; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1702 | |
Chris Lattner | 0a62474 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 1703 | // Put the new code above the original add, in case there are any uses of the |
| 1704 | // add between the add and the compare. |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1705 | Builder->SetInsertPoint(OrigAdd); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 0a62474 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 1707 | Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName()+".trunc"); |
| 1708 | Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName()+".trunc"); |
| 1709 | CallInst *Call = Builder->CreateCall2(F, TruncA, TruncB, "sadd"); |
| 1710 | Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result"); |
| 1711 | Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1712 | |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1713 | // The inner add was the result of the narrow add, zero extended to the |
| 1714 | // wider type. Replace it with the result computed by the intrinsic. |
Chris Lattner | 0fe80bb | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 1715 | IC.ReplaceInstUsesWith(*OrigAdd, ZExt); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 0a62474 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 1717 | // The original icmp gets replaced with the overflow value. |
| 1718 | return ExtractValueInst::Create(Call, 1, "sadd.overflow"); |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1719 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1721 | static Instruction *ProcessUAddIdiom(Instruction &I, Value *OrigAddV, |
| 1722 | InstCombiner &IC) { |
| 1723 | // Don't bother doing this transformation for pointers, don't do it for |
| 1724 | // vectors. |
| 1725 | if (!isa<IntegerType>(OrigAddV->getType())) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1727 | // If the add is a constant expr, then we don't bother transforming it. |
| 1728 | Instruction *OrigAdd = dyn_cast<Instruction>(OrigAddV); |
| 1729 | if (OrigAdd == 0) return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1730 | |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1731 | Value *LHS = OrigAdd->getOperand(0), *RHS = OrigAdd->getOperand(1); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1732 | |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1733 | // Put the new code above the original add, in case there are any uses of the |
| 1734 | // add between the add and the compare. |
| 1735 | InstCombiner::BuilderTy *Builder = IC.Builder; |
| 1736 | Builder->SetInsertPoint(OrigAdd); |
| 1737 | |
| 1738 | Module *M = I.getParent()->getParent()->getParent(); |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1739 | Type *Ty = LHS->getType(); |
Benjamin Kramer | eb9a85f | 2011-07-14 17:45:39 +0000 | [diff] [blame] | 1740 | Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty); |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1741 | CallInst *Call = Builder->CreateCall2(F, LHS, RHS, "uadd"); |
| 1742 | Value *Add = Builder->CreateExtractValue(Call, 0); |
| 1743 | |
| 1744 | IC.ReplaceInstUsesWith(*OrigAdd, Add); |
| 1745 | |
| 1746 | // The original icmp gets replaced with the overflow value. |
| 1747 | return ExtractValueInst::Create(Call, 1, "uadd.overflow"); |
| 1748 | } |
| 1749 | |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1750 | // DemandedBitsLHSMask - When performing a comparison against a constant, |
| 1751 | // it is possible that not all the bits in the LHS are demanded. This helper |
| 1752 | // method computes the mask that IS demanded. |
| 1753 | static APInt DemandedBitsLHSMask(ICmpInst &I, |
| 1754 | unsigned BitWidth, bool isSignCheck) { |
| 1755 | if (isSignCheck) |
| 1756 | return APInt::getSignBit(BitWidth); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1757 | |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1758 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 1759 | if (!CI) return APInt::getAllOnesValue(BitWidth); |
Owen Anderson | a33b625 | 2011-01-11 18:26:37 +0000 | [diff] [blame] | 1760 | const APInt &RHS = CI->getValue(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1761 | |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1762 | switch (I.getPredicate()) { |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1763 | // For a UGT comparison, we don't care about any bits that |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1764 | // correspond to the trailing ones of the comparand. The value of these |
| 1765 | // bits doesn't impact the outcome of the comparison, because any value |
| 1766 | // greater than the RHS must differ in a bit higher than these due to carry. |
| 1767 | case ICmpInst::ICMP_UGT: { |
| 1768 | unsigned trailingOnes = RHS.countTrailingOnes(); |
| 1769 | APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes); |
| 1770 | return ~lowBitsSet; |
| 1771 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1772 | |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1773 | // Similarly, for a ULT comparison, we don't care about the trailing zeros. |
| 1774 | // Any value less than the RHS must differ in a higher bit because of carries. |
| 1775 | case ICmpInst::ICMP_ULT: { |
| 1776 | unsigned trailingZeros = RHS.countTrailingZeros(); |
| 1777 | APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros); |
| 1778 | return ~lowBitsSet; |
| 1779 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1780 | |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1781 | default: |
| 1782 | return APInt::getAllOnesValue(BitWidth); |
| 1783 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1784 | |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1785 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1786 | |
| 1787 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 1788 | bool Changed = false; |
Chris Lattner | 5f670d4 | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 1789 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1790 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1791 | /// Orders the operands of the compare so that they are listed from most |
| 1792 | /// complex to least complex. This puts constants before unary operators, |
| 1793 | /// before binary operators. |
Chris Lattner | 5f670d4 | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 1794 | if (getComplexity(Op0) < getComplexity(Op1)) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1795 | I.swapOperands(); |
Chris Lattner | 5f670d4 | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 1796 | std::swap(Op0, Op1); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1797 | Changed = true; |
| 1798 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1799 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1800 | if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, TD)) |
| 1801 | return ReplaceInstUsesWith(I, V); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1802 | |
Pete Cooper | 65a6b57 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 1803 | // comparing -val or val with non-zero is the same as just comparing val |
Pete Cooper | 165695d | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 1804 | // ie, abs(val) != 0 -> val != 0 |
Pete Cooper | 65a6b57 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 1805 | if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) |
| 1806 | { |
Pete Cooper | 165695d | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 1807 | Value *Cond, *SelectTrue, *SelectFalse; |
| 1808 | if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue), |
Pete Cooper | 65a6b57 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 1809 | m_Value(SelectFalse)))) { |
Pete Cooper | 165695d | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 1810 | if (Value *V = dyn_castNegVal(SelectTrue)) { |
| 1811 | if (V == SelectFalse) |
| 1812 | return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); |
| 1813 | } |
| 1814 | else if (Value *V = dyn_castNegVal(SelectFalse)) { |
| 1815 | if (V == SelectTrue) |
| 1816 | return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); |
Pete Cooper | 65a6b57 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1821 | Type *Ty = Op0->getType(); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1822 | |
| 1823 | // icmp's with boolean values can always be turned into bitwise operations |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1824 | if (Ty->isIntegerTy(1)) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1825 | switch (I.getPredicate()) { |
| 1826 | default: llvm_unreachable("Invalid icmp instruction!"); |
| 1827 | case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B) |
| 1828 | Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp"); |
| 1829 | return BinaryOperator::CreateNot(Xor); |
| 1830 | } |
| 1831 | case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B |
| 1832 | return BinaryOperator::CreateXor(Op0, Op1); |
| 1833 | |
| 1834 | case ICmpInst::ICMP_UGT: |
| 1835 | std::swap(Op0, Op1); // Change icmp ugt -> icmp ult |
| 1836 | // FALL THROUGH |
| 1837 | case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B |
| 1838 | Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp"); |
| 1839 | return BinaryOperator::CreateAnd(Not, Op1); |
| 1840 | } |
| 1841 | case ICmpInst::ICMP_SGT: |
| 1842 | std::swap(Op0, Op1); // Change icmp sgt -> icmp slt |
| 1843 | // FALL THROUGH |
| 1844 | case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B |
| 1845 | Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp"); |
| 1846 | return BinaryOperator::CreateAnd(Not, Op0); |
| 1847 | } |
| 1848 | case ICmpInst::ICMP_UGE: |
| 1849 | std::swap(Op0, Op1); // Change icmp uge -> icmp ule |
| 1850 | // FALL THROUGH |
| 1851 | case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B |
| 1852 | Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp"); |
| 1853 | return BinaryOperator::CreateOr(Not, Op1); |
| 1854 | } |
| 1855 | case ICmpInst::ICMP_SGE: |
| 1856 | std::swap(Op0, Op1); // Change icmp sge -> icmp sle |
| 1857 | // FALL THROUGH |
| 1858 | case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B |
| 1859 | Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp"); |
| 1860 | return BinaryOperator::CreateOr(Not, Op0); |
| 1861 | } |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | unsigned BitWidth = 0; |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1866 | if (Ty->isIntOrIntVectorTy()) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1867 | BitWidth = Ty->getScalarSizeInBits(); |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 1868 | else if (TD) // Pointers require TD info to get their size. |
| 1869 | BitWidth = TD->getTypeSizeInBits(Ty->getScalarType()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1871 | bool isSignBit = false; |
| 1872 | |
| 1873 | // See if we are doing a comparison with a constant. |
| 1874 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 1875 | Value *A = 0, *B = 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1876 | |
Owen Anderson | e63dda5 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 1877 | // Match the following pattern, which is a common idiom when writing |
| 1878 | // overflow-safe integer arithmetic function. The source performs an |
| 1879 | // addition in wider type, and explicitly checks for overflow using |
| 1880 | // comparisons against INT_MIN and INT_MAX. Simplify this by using the |
| 1881 | // sadd_with_overflow intrinsic. |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1882 | // |
| 1883 | // TODO: This could probably be generalized to handle other overflow-safe |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1884 | // operations if we worked out the formulas to compute the appropriate |
Owen Anderson | e63dda5 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 1885 | // magic constants. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1886 | // |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1887 | // sum = a + b |
| 1888 | // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 |
Owen Anderson | e63dda5 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 1889 | { |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1890 | ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI |
Owen Anderson | e63dda5 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 1891 | if (I.getPredicate() == ICmpInst::ICMP_UGT && |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1892 | match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) |
Chris Lattner | 0fe80bb | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 1893 | if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this)) |
Chris Lattner | f0f568b | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 1894 | return Res; |
Owen Anderson | e63dda5 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 1895 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1896 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1897 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 1898 | if (I.isEquality() && CI->isZero() && |
| 1899 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) { |
| 1900 | // (icmp cond A B) if cond is equality |
| 1901 | return new ICmpInst(I.getPredicate(), A, B); |
| 1902 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1904 | // If we have an icmp le or icmp ge instruction, turn it into the |
| 1905 | // appropriate icmp lt or icmp gt instruction. This allows us to rely on |
| 1906 | // them being folded in the code below. The SimplifyICmpInst code has |
| 1907 | // already handled the edge cases for us, so we just assert on them. |
| 1908 | switch (I.getPredicate()) { |
| 1909 | default: break; |
| 1910 | case ICmpInst::ICMP_ULE: |
| 1911 | assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE |
| 1912 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, |
| 1913 | ConstantInt::get(CI->getContext(), CI->getValue()+1)); |
| 1914 | case ICmpInst::ICMP_SLE: |
| 1915 | assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE |
| 1916 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 1917 | ConstantInt::get(CI->getContext(), CI->getValue()+1)); |
| 1918 | case ICmpInst::ICMP_UGE: |
Nick Lewycky | d8d1584 | 2011-02-28 06:20:05 +0000 | [diff] [blame] | 1919 | assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1920 | return new ICmpInst(ICmpInst::ICMP_UGT, Op0, |
| 1921 | ConstantInt::get(CI->getContext(), CI->getValue()-1)); |
| 1922 | case ICmpInst::ICMP_SGE: |
Nick Lewycky | d8d1584 | 2011-02-28 06:20:05 +0000 | [diff] [blame] | 1923 | assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1924 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 1925 | ConstantInt::get(CI->getContext(), CI->getValue()-1)); |
| 1926 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1927 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1928 | // If this comparison is a normal comparison, it demands all |
| 1929 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 1930 | bool UnusedBit; |
| 1931 | isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 1932 | } |
| 1933 | |
| 1934 | // See if we can fold the comparison based on range information we can get |
| 1935 | // by checking whether bits are known to be zero or one in the input. |
| 1936 | if (BitWidth != 0) { |
| 1937 | APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0); |
| 1938 | APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0); |
| 1939 | |
| 1940 | if (SimplifyDemandedBits(I.getOperandUse(0), |
Owen Anderson | da1c122 | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 1941 | DemandedBitsLHSMask(I, BitWidth, isSignBit), |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1942 | Op0KnownZero, Op0KnownOne, 0)) |
| 1943 | return &I; |
| 1944 | if (SimplifyDemandedBits(I.getOperandUse(1), |
| 1945 | APInt::getAllOnesValue(BitWidth), |
| 1946 | Op1KnownZero, Op1KnownOne, 0)) |
| 1947 | return &I; |
| 1948 | |
| 1949 | // Given the known and unknown bits, compute a range that the LHS could be |
| 1950 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
| 1951 | // EQ and NE we use unsigned values. |
| 1952 | APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0); |
| 1953 | APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0); |
| 1954 | if (I.isSigned()) { |
| 1955 | ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, |
| 1956 | Op0Min, Op0Max); |
| 1957 | ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, |
| 1958 | Op1Min, Op1Max); |
| 1959 | } else { |
| 1960 | ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, |
| 1961 | Op0Min, Op0Max); |
| 1962 | ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, |
| 1963 | Op1Min, Op1Max); |
| 1964 | } |
| 1965 | |
| 1966 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
| 1967 | // figured out that the LHS is a constant. Just constant fold this now so |
| 1968 | // that code below can assume that Min != Max. |
| 1969 | if (!isa<Constant>(Op0) && Op0Min == Op0Max) |
| 1970 | return new ICmpInst(I.getPredicate(), |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 1971 | ConstantInt::get(Op0->getType(), Op0Min), Op1); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1972 | if (!isa<Constant>(Op1) && Op1Min == Op1Max) |
| 1973 | return new ICmpInst(I.getPredicate(), Op0, |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 1974 | ConstantInt::get(Op1->getType(), Op1Min)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Based on the range information we know about the LHS, see if we can |
Nick Lewycky | d8d1584 | 2011-02-28 06:20:05 +0000 | [diff] [blame] | 1977 | // simplify this comparison. For example, (x&4) < 8 is always true. |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1978 | switch (I.getPredicate()) { |
| 1979 | default: llvm_unreachable("Unknown icmp opcode!"); |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 1980 | case ICmpInst::ICMP_EQ: { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1981 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 1982 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 1984 | // If all bits are known zero except for one, then we know at most one |
| 1985 | // bit is set. If the comparison is against zero, then this is a check |
| 1986 | // to see if *that* bit is set. |
| 1987 | APInt Op0KnownZeroInverted = ~Op0KnownZero; |
| 1988 | if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) { |
| 1989 | // If the LHS is an AND with the same constant, look through it. |
| 1990 | Value *LHS = 0; |
| 1991 | ConstantInt *LHSC = 0; |
| 1992 | if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) || |
| 1993 | LHSC->getValue() != Op0KnownZeroInverted) |
| 1994 | LHS = Op0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 1996 | // If the LHS is 1 << x, and we know the result is a power of 2 like 8, |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 1997 | // then turn "((1 << x)&8) == 0" into "x != 3". |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 1998 | Value *X = 0; |
| 1999 | if (match(LHS, m_Shl(m_One(), m_Value(X)))) { |
| 2000 | unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros(); |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2001 | return new ICmpInst(ICmpInst::ICMP_NE, X, |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2002 | ConstantInt::get(X->getType(), CmpVal)); |
| 2003 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2005 | // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1, |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2006 | // then turn "((8 >>u x)&1) == 0" into "x != 3". |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2007 | const APInt *CI; |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2008 | if (Op0KnownZeroInverted == 1 && |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2009 | match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2010 | return new ICmpInst(ICmpInst::ICMP_NE, X, |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2011 | ConstantInt::get(X->getType(), |
| 2012 | CI->countTrailingZeros())); |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2013 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2014 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2015 | break; |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2016 | } |
| 2017 | case ICmpInst::ICMP_NE: { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2018 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2019 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2021 | // If all bits are known zero except for one, then we know at most one |
| 2022 | // bit is set. If the comparison is against zero, then this is a check |
| 2023 | // to see if *that* bit is set. |
| 2024 | APInt Op0KnownZeroInverted = ~Op0KnownZero; |
| 2025 | if (~Op1KnownZero == 0 && Op0KnownZeroInverted.isPowerOf2()) { |
| 2026 | // If the LHS is an AND with the same constant, look through it. |
| 2027 | Value *LHS = 0; |
| 2028 | ConstantInt *LHSC = 0; |
| 2029 | if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) || |
| 2030 | LHSC->getValue() != Op0KnownZeroInverted) |
| 2031 | LHS = Op0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2033 | // If the LHS is 1 << x, and we know the result is a power of 2 like 8, |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2034 | // then turn "((1 << x)&8) != 0" into "x == 3". |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2035 | Value *X = 0; |
| 2036 | if (match(LHS, m_Shl(m_One(), m_Value(X)))) { |
| 2037 | unsigned CmpVal = Op0KnownZeroInverted.countTrailingZeros(); |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2038 | return new ICmpInst(ICmpInst::ICMP_EQ, X, |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2039 | ConstantInt::get(X->getType(), CmpVal)); |
| 2040 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2041 | |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2042 | // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1, |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2043 | // then turn "((8 >>u x)&1) != 0" into "x == 3". |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2044 | const APInt *CI; |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2045 | if (Op0KnownZeroInverted == 1 && |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2046 | match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) |
Chris Lattner | 79b967b | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 2047 | return new ICmpInst(ICmpInst::ICMP_EQ, X, |
Chris Lattner | b20c0b5 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2048 | ConstantInt::get(X->getType(), |
| 2049 | CI->countTrailingZeros())); |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2050 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2051 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2052 | break; |
Chris Lattner | 75d8f59 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 2053 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2054 | case ICmpInst::ICMP_ULT: |
| 2055 | if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2056 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2057 | if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2058 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2059 | if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B) |
| 2060 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 2061 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 2062 | if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C |
| 2063 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
| 2064 | ConstantInt::get(CI->getContext(), CI->getValue()-1)); |
| 2065 | |
| 2066 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 2067 | if (CI->isMinValue(true)) |
| 2068 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 2069 | Constant::getAllOnesValue(Op0->getType())); |
| 2070 | } |
| 2071 | break; |
| 2072 | case ICmpInst::ICMP_UGT: |
| 2073 | if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2074 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2075 | if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2076 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2077 | |
| 2078 | if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B) |
| 2079 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 2080 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 2081 | if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C |
| 2082 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
| 2083 | ConstantInt::get(CI->getContext(), CI->getValue()+1)); |
| 2084 | |
| 2085 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 2086 | if (CI->isMaxValue(true)) |
| 2087 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 2088 | Constant::getNullValue(Op0->getType())); |
| 2089 | } |
| 2090 | break; |
| 2091 | case ICmpInst::ICMP_SLT: |
| 2092 | if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2093 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2094 | if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2095 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2096 | if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B) |
| 2097 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 2098 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 2099 | if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C |
| 2100 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
| 2101 | ConstantInt::get(CI->getContext(), CI->getValue()-1)); |
| 2102 | } |
| 2103 | break; |
| 2104 | case ICmpInst::ICMP_SGT: |
| 2105 | if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2106 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2107 | if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2108 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2109 | |
| 2110 | if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B) |
| 2111 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 2112 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 2113 | if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C |
| 2114 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
| 2115 | ConstantInt::get(CI->getContext(), CI->getValue()+1)); |
| 2116 | } |
| 2117 | break; |
| 2118 | case ICmpInst::ICMP_SGE: |
| 2119 | assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!"); |
| 2120 | if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2121 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2122 | if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2123 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2124 | break; |
| 2125 | case ICmpInst::ICMP_SLE: |
| 2126 | assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!"); |
| 2127 | if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2128 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2129 | if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2130 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | case ICmpInst::ICMP_UGE: |
| 2133 | assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!"); |
| 2134 | if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2135 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2136 | if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2137 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2138 | break; |
| 2139 | case ICmpInst::ICMP_ULE: |
| 2140 | assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!"); |
| 2141 | if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2142 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2143 | if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B) |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2144 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2145 | break; |
| 2146 | } |
| 2147 | |
| 2148 | // Turn a signed comparison into an unsigned one if both operands |
| 2149 | // are known to have the same sign. |
| 2150 | if (I.isSigned() && |
| 2151 | ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) || |
| 2152 | (Op0KnownOne.isNegative() && Op1KnownOne.isNegative()))) |
| 2153 | return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1); |
| 2154 | } |
| 2155 | |
| 2156 | // Test if the ICmpInst instruction is used exclusively by a select as |
| 2157 | // part of a minimum or maximum operation. If so, refrain from doing |
| 2158 | // any other folding. This helps out other analyses which understand |
| 2159 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 2160 | // and CodeGen. And in this case, at least one of the comparison |
| 2161 | // operands has at least one user besides the compare (the select), |
| 2162 | // which would often largely negate the benefit of folding anyway. |
| 2163 | if (I.hasOneUse()) |
| 2164 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin())) |
| 2165 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 2166 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
| 2167 | return 0; |
| 2168 | |
| 2169 | // See if we are doing a comparison between a constant and an instruction that |
| 2170 | // can be folded into the comparison. |
| 2171 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2172 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
| 2173 | // instruction, see if that instruction also has constants so that the |
| 2174 | // instruction can be folded into the icmp |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2175 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 2176 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 2177 | return Res; |
| 2178 | } |
| 2179 | |
| 2180 | // Handle icmp with constant (but not simple integer constant) RHS |
| 2181 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 2182 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 2183 | switch (LHSI->getOpcode()) { |
| 2184 | case Instruction::GetElementPtr: |
| 2185 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
| 2186 | if (RHSC->isNullValue() && |
| 2187 | cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices()) |
| 2188 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
| 2189 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 2190 | break; |
| 2191 | case Instruction::PHI: |
| 2192 | // Only fold icmp into the PHI if the phi and icmp are in the same |
| 2193 | // block. If in the same block, we're encouraging jump threading. If |
| 2194 | // not, we are just pessimizing the code by making an i1 phi. |
| 2195 | if (LHSI->getParent() == I.getParent()) |
Chris Lattner | 9922ccf | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 2196 | if (Instruction *NV = FoldOpIntoPhi(I)) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2197 | return NV; |
| 2198 | break; |
| 2199 | case Instruction::Select: { |
| 2200 | // If either operand of the select is a constant, we can fold the |
| 2201 | // comparison into the select arms, which will cause one to be |
| 2202 | // constant folded and the select turned into a bitwise or. |
| 2203 | Value *Op1 = 0, *Op2 = 0; |
| 2204 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) |
| 2205 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 2206 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) |
| 2207 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 2208 | |
| 2209 | // We only want to perform this transformation if it will not lead to |
| 2210 | // additional code. This is true if either both sides of the select |
| 2211 | // fold to a constant (in which case the icmp is replaced with a select |
| 2212 | // which will usually simplify) or this is the only user of the |
| 2213 | // select (in which case we are trading a select+icmp for a simpler |
| 2214 | // select+icmp). |
| 2215 | if ((Op1 && Op2) || (LHSI->hasOneUse() && (Op1 || Op2))) { |
| 2216 | if (!Op1) |
| 2217 | Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1), |
| 2218 | RHSC, I.getName()); |
| 2219 | if (!Op2) |
| 2220 | Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2), |
| 2221 | RHSC, I.getName()); |
| 2222 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
| 2223 | } |
| 2224 | break; |
| 2225 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2226 | case Instruction::IntToPtr: |
| 2227 | // icmp pred inttoptr(X), null -> icmp pred X, 0 |
| 2228 | if (RHSC->isNullValue() && TD && |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2229 | TD->getIntPtrType(RHSC->getContext()) == |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2230 | LHSI->getOperand(0)->getType()) |
| 2231 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
| 2232 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 2233 | break; |
| 2234 | |
| 2235 | case Instruction::Load: |
| 2236 | // Try to optimize things like "A[i] > 4" to index computations. |
| 2237 | if (GetElementPtrInst *GEP = |
| 2238 | dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) { |
| 2239 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 2240 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 2241 | !cast<LoadInst>(LHSI)->isVolatile()) |
| 2242 | if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I)) |
| 2243 | return Res; |
| 2244 | } |
| 2245 | break; |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
| 2250 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0)) |
| 2251 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
| 2252 | return NI; |
| 2253 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1)) |
| 2254 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 2255 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
| 2256 | return NI; |
| 2257 | |
| 2258 | // Test to see if the operands of the icmp are casted versions of other |
| 2259 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 2260 | // now. |
| 2261 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2262 | if (Op0->getType()->isPointerTy() && |
| 2263 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2264 | // We keep moving the cast from the left operand over to the right |
| 2265 | // operand, where it can often be eliminated completely. |
| 2266 | Op0 = CI->getOperand(0); |
| 2267 | |
| 2268 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 2269 | // so eliminate it as well. |
| 2270 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 2271 | Op1 = CI2->getOperand(0); |
| 2272 | |
| 2273 | // If Op1 is a constant, we can fold the cast into the constant. |
| 2274 | if (Op0->getType() != Op1->getType()) { |
| 2275 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 2276 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
| 2277 | } else { |
| 2278 | // Otherwise, cast the RHS right before the icmp |
| 2279 | Op1 = Builder->CreateBitCast(Op1, Op0->getType()); |
| 2280 | } |
| 2281 | } |
| 2282 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
| 2283 | } |
| 2284 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2285 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2286 | if (isa<CastInst>(Op0)) { |
| 2287 | // Handle the special case of: icmp (cast bool to X), <cst> |
| 2288 | // This comes up when you have code like |
| 2289 | // int X = A < B; |
| 2290 | // if (X) ... |
| 2291 | // For generality, we handle any zero-extension of any operand comparison |
| 2292 | // with a constant or another cast from the same type. |
| 2293 | if (isa<Constant>(Op1) || isa<CastInst>(Op1)) |
| 2294 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
| 2295 | return R; |
| 2296 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2297 | |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2298 | // Special logic for binary operators. |
| 2299 | BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0); |
| 2300 | BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1); |
| 2301 | if (BO0 || BO1) { |
| 2302 | CmpInst::Predicate Pred = I.getPredicate(); |
| 2303 | bool NoOp0WrapProblem = false, NoOp1WrapProblem = false; |
| 2304 | if (BO0 && isa<OverflowingBinaryOperator>(BO0)) |
| 2305 | NoOp0WrapProblem = ICmpInst::isEquality(Pred) || |
| 2306 | (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) || |
| 2307 | (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap()); |
| 2308 | if (BO1 && isa<OverflowingBinaryOperator>(BO1)) |
| 2309 | NoOp1WrapProblem = ICmpInst::isEquality(Pred) || |
| 2310 | (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) || |
| 2311 | (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap()); |
| 2312 | |
| 2313 | // Analyze the case when either Op0 or Op1 is an add instruction. |
| 2314 | // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null). |
| 2315 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| 2316 | if (BO0 && BO0->getOpcode() == Instruction::Add) |
| 2317 | A = BO0->getOperand(0), B = BO0->getOperand(1); |
| 2318 | if (BO1 && BO1->getOpcode() == Instruction::Add) |
| 2319 | C = BO1->getOperand(0), D = BO1->getOperand(1); |
| 2320 | |
| 2321 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2322 | if ((A == Op1 || B == Op1) && NoOp0WrapProblem) |
| 2323 | return new ICmpInst(Pred, A == Op1 ? B : A, |
| 2324 | Constant::getNullValue(Op1->getType())); |
| 2325 | |
| 2326 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2327 | if ((C == Op0 || D == Op0) && NoOp1WrapProblem) |
| 2328 | return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()), |
| 2329 | C == Op0 ? D : C); |
| 2330 | |
Duncan Sands | 39a7de7 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 2331 | // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow. |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2332 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 2333 | NoOp0WrapProblem && NoOp1WrapProblem && |
| 2334 | // Try not to increase register pressure. |
| 2335 | BO0->hasOneUse() && BO1->hasOneUse()) { |
| 2336 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 2337 | Value *Y = (A == C || A == D) ? B : A; |
| 2338 | Value *Z = (C == A || C == B) ? D : C; |
| 2339 | return new ICmpInst(Pred, Y, Z); |
| 2340 | } |
| 2341 | |
| 2342 | // Analyze the case when either Op0 or Op1 is a sub instruction. |
| 2343 | // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null). |
| 2344 | A = 0; B = 0; C = 0; D = 0; |
| 2345 | if (BO0 && BO0->getOpcode() == Instruction::Sub) |
| 2346 | A = BO0->getOperand(0), B = BO0->getOperand(1); |
| 2347 | if (BO1 && BO1->getOpcode() == Instruction::Sub) |
| 2348 | C = BO1->getOperand(0), D = BO1->getOperand(1); |
| 2349 | |
Duncan Sands | 39a7de7 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 2350 | // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow. |
| 2351 | if (A == Op1 && NoOp0WrapProblem) |
| 2352 | return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); |
| 2353 | |
| 2354 | // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow. |
| 2355 | if (C == Op0 && NoOp1WrapProblem) |
| 2356 | return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); |
| 2357 | |
| 2358 | // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow. |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2359 | if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem && |
| 2360 | // Try not to increase register pressure. |
| 2361 | BO0->hasOneUse() && BO1->hasOneUse()) |
| 2362 | return new ICmpInst(Pred, A, C); |
| 2363 | |
Duncan Sands | 39a7de7 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 2364 | // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow. |
| 2365 | if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem && |
| 2366 | // Try not to increase register pressure. |
| 2367 | BO0->hasOneUse() && BO1->hasOneUse()) |
| 2368 | return new ICmpInst(Pred, D, B); |
| 2369 | |
Nick Lewycky | 9feda17 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 2370 | BinaryOperator *SRem = NULL; |
Nick Lewycky | dcf7757 | 2011-03-08 06:29:47 +0000 | [diff] [blame] | 2371 | // icmp (srem X, Y), Y |
Nick Lewycky | 9feda17 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 2372 | if (BO0 && BO0->getOpcode() == Instruction::SRem && |
| 2373 | Op1 == BO0->getOperand(1)) |
| 2374 | SRem = BO0; |
Nick Lewycky | dcf7757 | 2011-03-08 06:29:47 +0000 | [diff] [blame] | 2375 | // icmp Y, (srem X, Y) |
Nick Lewycky | 9feda17 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 2376 | else if (BO1 && BO1->getOpcode() == Instruction::SRem && |
| 2377 | Op0 == BO1->getOperand(1)) |
| 2378 | SRem = BO1; |
| 2379 | if (SRem) { |
| 2380 | // We don't check hasOneUse to avoid increasing register pressure because |
| 2381 | // the value we use is the same value this instruction was already using. |
| 2382 | switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) { |
| 2383 | default: break; |
| 2384 | case ICmpInst::ICMP_EQ: |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2385 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Nick Lewycky | 9feda17 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 2386 | case ICmpInst::ICMP_NE: |
Nick Lewycky | d01f50f | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 2387 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Nick Lewycky | 9feda17 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 2388 | case ICmpInst::ICMP_SGT: |
| 2389 | case ICmpInst::ICMP_SGE: |
| 2390 | return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1), |
| 2391 | Constant::getAllOnesValue(SRem->getType())); |
| 2392 | case ICmpInst::ICMP_SLT: |
| 2393 | case ICmpInst::ICMP_SLE: |
| 2394 | return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1), |
| 2395 | Constant::getNullValue(SRem->getType())); |
| 2396 | } |
| 2397 | } |
| 2398 | |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2399 | if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && |
| 2400 | BO0->hasOneUse() && BO1->hasOneUse() && |
| 2401 | BO0->getOperand(1) == BO1->getOperand(1)) { |
| 2402 | switch (BO0->getOpcode()) { |
| 2403 | default: break; |
| 2404 | case Instruction::Add: |
| 2405 | case Instruction::Sub: |
| 2406 | case Instruction::Xor: |
| 2407 | if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b |
| 2408 | return new ICmpInst(I.getPredicate(), BO0->getOperand(0), |
| 2409 | BO1->getOperand(0)); |
| 2410 | // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b |
| 2411 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) { |
| 2412 | if (CI->getValue().isSignBit()) { |
| 2413 | ICmpInst::Predicate Pred = I.isSigned() |
| 2414 | ? I.getUnsignedPredicate() |
| 2415 | : I.getSignedPredicate(); |
| 2416 | return new ICmpInst(Pred, BO0->getOperand(0), |
| 2417 | BO1->getOperand(0)); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2418 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | c73b24d | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 2420 | if (CI->isMaxValue(true)) { |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2421 | ICmpInst::Predicate Pred = I.isSigned() |
| 2422 | ? I.getUnsignedPredicate() |
| 2423 | : I.getSignedPredicate(); |
| 2424 | Pred = I.getSwappedPredicate(Pred); |
| 2425 | return new ICmpInst(Pred, BO0->getOperand(0), |
| 2426 | BO1->getOperand(0)); |
| 2427 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2428 | } |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2429 | break; |
| 2430 | case Instruction::Mul: |
| 2431 | if (!I.isEquality()) |
| 2432 | break; |
| 2433 | |
| 2434 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) { |
| 2435 | // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask |
| 2436 | // Mask = -1 >> count-trailing-zeros(Cst). |
| 2437 | if (!CI->isZero() && !CI->isOne()) { |
| 2438 | const APInt &AP = CI->getValue(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2439 | ConstantInt *Mask = ConstantInt::get(I.getContext(), |
Duncan Sands | a772433 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 2440 | APInt::getLowBitsSet(AP.getBitWidth(), |
| 2441 | AP.getBitWidth() - |
| 2442 | AP.countTrailingZeros())); |
| 2443 | Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask); |
| 2444 | Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask); |
| 2445 | return new ICmpInst(I.getPredicate(), And1, And2); |
| 2446 | } |
| 2447 | } |
| 2448 | break; |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2449 | case Instruction::UDiv: |
| 2450 | case Instruction::LShr: |
| 2451 | if (I.isSigned()) |
| 2452 | break; |
| 2453 | // fall-through |
| 2454 | case Instruction::SDiv: |
| 2455 | case Instruction::AShr: |
Eli Friedman | b6e7cd6 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 2456 | if (!BO0->isExact() || !BO1->isExact()) |
Nick Lewycky | 58bfcdb | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 2457 | break; |
| 2458 | return new ICmpInst(I.getPredicate(), BO0->getOperand(0), |
| 2459 | BO1->getOperand(0)); |
| 2460 | case Instruction::Shl: { |
| 2461 | bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap(); |
| 2462 | bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap(); |
| 2463 | if (!NUW && !NSW) |
| 2464 | break; |
| 2465 | if (!NSW && I.isSigned()) |
| 2466 | break; |
| 2467 | return new ICmpInst(I.getPredicate(), BO0->getOperand(0), |
| 2468 | BO1->getOperand(0)); |
| 2469 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2473 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2474 | { Value *A, *B; |
Chris Lattner | fdb5b01 | 2011-01-15 05:41:33 +0000 | [diff] [blame] | 2475 | // ~x < ~y --> y < x |
| 2476 | // ~x < cst --> ~cst < x |
| 2477 | if (match(Op0, m_Not(m_Value(A)))) { |
| 2478 | if (match(Op1, m_Not(m_Value(B)))) |
| 2479 | return new ICmpInst(I.getPredicate(), B, A); |
Chris Lattner | 27a9848 | 2011-01-15 05:42:47 +0000 | [diff] [blame] | 2480 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) |
Chris Lattner | fdb5b01 | 2011-01-15 05:41:33 +0000 | [diff] [blame] | 2481 | return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A); |
| 2482 | } |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 2483 | |
| 2484 | // (a+b) <u a --> llvm.uadd.with.overflow. |
| 2485 | // (a+b) <u b --> llvm.uadd.with.overflow. |
| 2486 | if (I.getPredicate() == ICmpInst::ICMP_ULT && |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2487 | match(Op0, m_Add(m_Value(A), m_Value(B))) && |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 2488 | (Op1 == A || Op1 == B)) |
| 2489 | if (Instruction *R = ProcessUAddIdiom(I, Op0, *this)) |
| 2490 | return R; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | e5cbdca | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 2492 | // a >u (a+b) --> llvm.uadd.with.overflow. |
| 2493 | // b >u (a+b) --> llvm.uadd.with.overflow. |
| 2494 | if (I.getPredicate() == ICmpInst::ICMP_UGT && |
| 2495 | match(Op1, m_Add(m_Value(A), m_Value(B))) && |
| 2496 | (Op0 == A || Op0 == B)) |
| 2497 | if (Instruction *R = ProcessUAddIdiom(I, Op1, *this)) |
| 2498 | return R; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2499 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2500 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2501 | if (I.isEquality()) { |
| 2502 | Value *A, *B, *C, *D; |
Duncan Sands | 39a7de7 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 2503 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2504 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 2505 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 2506 | Value *OtherVal = A == Op1 ? B : A; |
| 2507 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 2508 | Constant::getNullValue(A->getType())); |
| 2509 | } |
| 2510 | |
| 2511 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 2512 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 2513 | ConstantInt *C1, *C2; |
| 2514 | if (match(B, m_ConstantInt(C1)) && |
| 2515 | match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) { |
| 2516 | Constant *NC = ConstantInt::get(I.getContext(), |
| 2517 | C1->getValue() ^ C2->getValue()); |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2518 | Value *Xor = Builder->CreateXor(C, NC); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2519 | return new ICmpInst(I.getPredicate(), A, Xor); |
| 2520 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2521 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2522 | // A^B == A^D -> B == D |
| 2523 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 2524 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 2525 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 2526 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 2527 | } |
| 2528 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2529 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2530 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 2531 | (A == Op0 || B == Op0)) { |
| 2532 | // A == (A^B) -> B == 0 |
| 2533 | Value *OtherVal = A == Op0 ? B : A; |
| 2534 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 2535 | Constant::getNullValue(A->getType())); |
| 2536 | } |
| 2537 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2538 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2539 | if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) && |
Chris Lattner | 5036ce4 | 2011-04-26 20:02:45 +0000 | [diff] [blame] | 2540 | match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) { |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2541 | Value *X = 0, *Y = 0, *Z = 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2542 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2543 | if (A == C) { |
| 2544 | X = B; Y = D; Z = A; |
| 2545 | } else if (A == D) { |
| 2546 | X = B; Y = C; Z = A; |
| 2547 | } else if (B == C) { |
| 2548 | X = A; Y = D; Z = B; |
| 2549 | } else if (B == D) { |
| 2550 | X = A; Y = C; Z = B; |
| 2551 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2552 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2553 | if (X) { // Build (X^Y) & Z |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 2554 | Op1 = Builder->CreateXor(X, Y); |
| 2555 | Op1 = Builder->CreateAnd(Op1, Z); |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2556 | I.setOperand(0, Op1); |
| 2557 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 2558 | return &I; |
| 2559 | } |
| 2560 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | 325eeb1 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 2562 | // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to |
| 2563 | // "icmp (and X, mask), cst" |
| 2564 | uint64_t ShAmt = 0; |
| 2565 | ConstantInt *Cst1; |
| 2566 | if (Op0->hasOneUse() && |
| 2567 | match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), |
| 2568 | m_ConstantInt(ShAmt))))) && |
| 2569 | match(Op1, m_ConstantInt(Cst1)) && |
| 2570 | // Only do this when A has multiple uses. This is most important to do |
| 2571 | // when it exposes other optimizations. |
| 2572 | !A->hasOneUse()) { |
| 2573 | unsigned ASize =cast<IntegerType>(A->getType())->getPrimitiveSizeInBits(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | 325eeb1 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 2575 | if (ShAmt < ASize) { |
| 2576 | APInt MaskV = |
| 2577 | APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits()); |
| 2578 | MaskV <<= ShAmt; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2579 | |
Chris Lattner | 325eeb1 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 2580 | APInt CmpV = Cst1->getValue().zext(ASize); |
| 2581 | CmpV <<= ShAmt; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2582 | |
Chris Lattner | 325eeb1 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 2583 | Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV)); |
| 2584 | return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV)); |
| 2585 | } |
| 2586 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2587 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2588 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2589 | { |
| 2590 | Value *X; ConstantInt *Cst; |
| 2591 | // icmp X+Cst, X |
| 2592 | if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X) |
| 2593 | return FoldICmpAddOpCst(I, X, Cst, I.getPredicate(), Op0); |
| 2594 | |
| 2595 | // icmp X, X+Cst |
| 2596 | if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X) |
| 2597 | return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate(), Op1); |
| 2598 | } |
| 2599 | return Changed ? &I : 0; |
| 2600 | } |
| 2601 | |
| 2602 | |
| 2603 | |
| 2604 | |
| 2605 | |
| 2606 | |
| 2607 | /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible. |
| 2608 | /// |
| 2609 | Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, |
| 2610 | Instruction *LHSI, |
| 2611 | Constant *RHSC) { |
| 2612 | if (!isa<ConstantFP>(RHSC)) return 0; |
| 2613 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2615 | // Get the width of the mantissa. We don't want to hack on conversions that |
| 2616 | // might lose information from the integer, e.g. "i64 -> float" |
| 2617 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
| 2618 | if (MantissaWidth == -1) return 0; // Unknown. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2620 | // Check to see that the input is converted from an integer type that is small |
| 2621 | // enough that preserves all bits. TODO: check here for "known" sign bits. |
| 2622 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. |
| 2623 | unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2624 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2625 | // If this is a uitofp instruction, we need an extra bit to hold the sign. |
| 2626 | bool LHSUnsigned = isa<UIToFPInst>(LHSI); |
| 2627 | if (LHSUnsigned) |
| 2628 | ++InputSize; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2630 | // If the conversion would lose info, don't hack on this. |
| 2631 | if ((int)InputSize > MantissaWidth) |
| 2632 | return 0; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2634 | // Otherwise, we can potentially simplify the comparison. We know that it |
| 2635 | // will always come through as an integer value and we know the constant is |
| 2636 | // not a NAN (it would have been previously simplified). |
| 2637 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2639 | ICmpInst::Predicate Pred; |
| 2640 | switch (I.getPredicate()) { |
| 2641 | default: llvm_unreachable("Unexpected predicate!"); |
| 2642 | case FCmpInst::FCMP_UEQ: |
| 2643 | case FCmpInst::FCMP_OEQ: |
| 2644 | Pred = ICmpInst::ICMP_EQ; |
| 2645 | break; |
| 2646 | case FCmpInst::FCMP_UGT: |
| 2647 | case FCmpInst::FCMP_OGT: |
| 2648 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT; |
| 2649 | break; |
| 2650 | case FCmpInst::FCMP_UGE: |
| 2651 | case FCmpInst::FCMP_OGE: |
| 2652 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE; |
| 2653 | break; |
| 2654 | case FCmpInst::FCMP_ULT: |
| 2655 | case FCmpInst::FCMP_OLT: |
| 2656 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT; |
| 2657 | break; |
| 2658 | case FCmpInst::FCMP_ULE: |
| 2659 | case FCmpInst::FCMP_OLE: |
| 2660 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE; |
| 2661 | break; |
| 2662 | case FCmpInst::FCMP_UNE: |
| 2663 | case FCmpInst::FCMP_ONE: |
| 2664 | Pred = ICmpInst::ICMP_NE; |
| 2665 | break; |
| 2666 | case FCmpInst::FCMP_ORD: |
| 2667 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2668 | case FCmpInst::FCMP_UNO: |
| 2669 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2670 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2671 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2672 | IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2674 | // Now we know that the APFloat is a normal number, zero or inf. |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2675 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2676 | // See if the FP constant is too large for the integer. For example, |
| 2677 | // comparing an i8 to 300.0. |
| 2678 | unsigned IntWidth = IntTy->getScalarSizeInBits(); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2680 | if (!LHSUnsigned) { |
| 2681 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF |
| 2682 | // and large values. |
| 2683 | APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 2684 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, |
| 2685 | APFloat::rmNearestTiesToEven); |
| 2686 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 |
| 2687 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
| 2688 | Pred == ICmpInst::ICMP_SLE) |
| 2689 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2690 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2691 | } |
| 2692 | } else { |
| 2693 | // If the RHS value is > UnsignedMax, fold the comparison. This handles |
| 2694 | // +INF and large values. |
| 2695 | APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 2696 | UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false, |
| 2697 | APFloat::rmNearestTiesToEven); |
| 2698 | if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 |
| 2699 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || |
| 2700 | Pred == ICmpInst::ICMP_ULE) |
| 2701 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2702 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2703 | } |
| 2704 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2705 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2706 | if (!LHSUnsigned) { |
| 2707 | // See if the RHS value is < SignedMin. |
| 2708 | APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false); |
| 2709 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, |
| 2710 | APFloat::rmNearestTiesToEven); |
| 2711 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 |
| 2712 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
| 2713 | Pred == ICmpInst::ICMP_SGE) |
| 2714 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2715 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2716 | } |
Devang Patel | a2e0f6b | 2012-02-13 23:05:18 +0000 | [diff] [blame^] | 2717 | } else { |
| 2718 | // See if the RHS value is < UnsignedMin. |
| 2719 | APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false); |
| 2720 | SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true, |
| 2721 | APFloat::rmNearestTiesToEven); |
| 2722 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0 |
| 2723 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT || |
| 2724 | Pred == ICmpInst::ICMP_UGE) |
| 2725 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2726 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2727 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or |
| 2731 | // [0, UMAX], but it may still be fractional. See if it is fractional by |
| 2732 | // casting the FP value to the integer value and back, checking for equality. |
| 2733 | // Don't do this for zero, because -0.0 is not fractional. |
| 2734 | Constant *RHSInt = LHSUnsigned |
| 2735 | ? ConstantExpr::getFPToUI(RHSC, IntTy) |
| 2736 | : ConstantExpr::getFPToSI(RHSC, IntTy); |
| 2737 | if (!RHS.isZero()) { |
| 2738 | bool Equal = LHSUnsigned |
| 2739 | ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC |
| 2740 | : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC; |
| 2741 | if (!Equal) { |
| 2742 | // If we had a comparison against a fractional value, we have to adjust |
| 2743 | // the compare predicate and sometimes the value. RHSC is rounded towards |
| 2744 | // zero at this point. |
| 2745 | switch (Pred) { |
| 2746 | default: llvm_unreachable("Unexpected integer comparison!"); |
| 2747 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true |
| 2748 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2749 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false |
| 2750 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2751 | case ICmpInst::ICMP_ULE: |
| 2752 | // (float)int <= 4.4 --> int <= 4 |
| 2753 | // (float)int <= -4.4 --> false |
| 2754 | if (RHS.isNegative()) |
| 2755 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2756 | break; |
| 2757 | case ICmpInst::ICMP_SLE: |
| 2758 | // (float)int <= 4.4 --> int <= 4 |
| 2759 | // (float)int <= -4.4 --> int < -4 |
| 2760 | if (RHS.isNegative()) |
| 2761 | Pred = ICmpInst::ICMP_SLT; |
| 2762 | break; |
| 2763 | case ICmpInst::ICMP_ULT: |
| 2764 | // (float)int < -4.4 --> false |
| 2765 | // (float)int < 4.4 --> int <= 4 |
| 2766 | if (RHS.isNegative()) |
| 2767 | return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getContext())); |
| 2768 | Pred = ICmpInst::ICMP_ULE; |
| 2769 | break; |
| 2770 | case ICmpInst::ICMP_SLT: |
| 2771 | // (float)int < -4.4 --> int < -4 |
| 2772 | // (float)int < 4.4 --> int <= 4 |
| 2773 | if (!RHS.isNegative()) |
| 2774 | Pred = ICmpInst::ICMP_SLE; |
| 2775 | break; |
| 2776 | case ICmpInst::ICMP_UGT: |
| 2777 | // (float)int > 4.4 --> int > 4 |
| 2778 | // (float)int > -4.4 --> true |
| 2779 | if (RHS.isNegative()) |
| 2780 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2781 | break; |
| 2782 | case ICmpInst::ICMP_SGT: |
| 2783 | // (float)int > 4.4 --> int > 4 |
| 2784 | // (float)int > -4.4 --> int >= -4 |
| 2785 | if (RHS.isNegative()) |
| 2786 | Pred = ICmpInst::ICMP_SGE; |
| 2787 | break; |
| 2788 | case ICmpInst::ICMP_UGE: |
| 2789 | // (float)int >= -4.4 --> true |
| 2790 | // (float)int >= 4.4 --> int > 4 |
| 2791 | if (!RHS.isNegative()) |
| 2792 | return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getContext())); |
| 2793 | Pred = ICmpInst::ICMP_UGT; |
| 2794 | break; |
| 2795 | case ICmpInst::ICMP_SGE: |
| 2796 | // (float)int >= -4.4 --> int >= -4 |
| 2797 | // (float)int >= 4.4 --> int > 4 |
| 2798 | if (!RHS.isNegative()) |
| 2799 | Pred = ICmpInst::ICMP_SGT; |
| 2800 | break; |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | // Lower this FP comparison into an appropriate integer version of the |
| 2806 | // comparison. |
| 2807 | return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt); |
| 2808 | } |
| 2809 | |
| 2810 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 2811 | bool Changed = false; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2812 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2813 | /// Orders the operands of the compare so that they are listed from most |
| 2814 | /// complex to least complex. This puts constants before unary operators, |
| 2815 | /// before binary operators. |
| 2816 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) { |
| 2817 | I.swapOperands(); |
| 2818 | Changed = true; |
| 2819 | } |
| 2820 | |
| 2821 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2822 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2823 | if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1, TD)) |
| 2824 | return ReplaceInstUsesWith(I, V); |
| 2825 | |
| 2826 | // Simplify 'fcmp pred X, X' |
| 2827 | if (Op0 == Op1) { |
| 2828 | switch (I.getPredicate()) { |
| 2829 | default: llvm_unreachable("Unknown predicate!"); |
| 2830 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 2831 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 2832 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 2833 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 2834 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 2835 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 2836 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 2837 | return &I; |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2839 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 2840 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 2841 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 2842 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 2843 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 2844 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 2845 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 2846 | return &I; |
| 2847 | } |
| 2848 | } |
Jim Grosbach | 0cc4a95 | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2849 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2850 | // Handle fcmp with constant RHS |
| 2851 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 2852 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 2853 | switch (LHSI->getOpcode()) { |
Benjamin Kramer | b194bdc | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 2854 | case Instruction::FPExt: { |
| 2855 | // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless |
| 2856 | FPExtInst *LHSExt = cast<FPExtInst>(LHSI); |
| 2857 | ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC); |
| 2858 | if (!RHSF) |
| 2859 | break; |
| 2860 | |
Benjamin Kramer | 7ebdc37 | 2011-03-31 21:35:49 +0000 | [diff] [blame] | 2861 | // We can't convert a PPC double double. |
| 2862 | if (RHSF->getType()->isPPC_FP128Ty()) |
| 2863 | break; |
| 2864 | |
Benjamin Kramer | b194bdc | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 2865 | const fltSemantics *Sem; |
| 2866 | // FIXME: This shouldn't be here. |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 2867 | if (LHSExt->getSrcTy()->isHalfTy()) |
| 2868 | Sem = &APFloat::IEEEhalf; |
| 2869 | else if (LHSExt->getSrcTy()->isFloatTy()) |
Benjamin Kramer | b194bdc | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 2870 | Sem = &APFloat::IEEEsingle; |
| 2871 | else if (LHSExt->getSrcTy()->isDoubleTy()) |
| 2872 | Sem = &APFloat::IEEEdouble; |
| 2873 | else if (LHSExt->getSrcTy()->isFP128Ty()) |
| 2874 | Sem = &APFloat::IEEEquad; |
| 2875 | else if (LHSExt->getSrcTy()->isX86_FP80Ty()) |
| 2876 | Sem = &APFloat::x87DoubleExtended; |
Benjamin Kramer | b194bdc | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 2877 | else |
| 2878 | break; |
| 2879 | |
| 2880 | bool Lossy; |
| 2881 | APFloat F = RHSF->getValueAPF(); |
| 2882 | F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy); |
| 2883 | |
Jim Grosbach | cbf676b | 2011-09-30 18:45:50 +0000 | [diff] [blame] | 2884 | // Avoid lossy conversions and denormals. Zero is a special case |
| 2885 | // that's OK to convert. |
Jim Grosbach | 68e05fb | 2011-09-30 19:58:46 +0000 | [diff] [blame] | 2886 | APFloat Fabs = F; |
| 2887 | Fabs.clearSign(); |
Benjamin Kramer | b194bdc | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 2888 | if (!Lossy && |
Jim Grosbach | 68e05fb | 2011-09-30 19:58:46 +0000 | [diff] [blame] | 2889 | ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) != |
| 2890 | APFloat::cmpLessThan) || Fabs.isZero())) |
Jim Grosbach | cbf676b | 2011-09-30 18:45:50 +0000 | [diff] [blame] | 2891 | |
Benjamin Kramer | b194bdc | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 2892 | return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), |
| 2893 | ConstantFP::get(RHSC->getContext(), F)); |
| 2894 | break; |
| 2895 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2896 | case Instruction::PHI: |
| 2897 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
| 2898 | // block. If in the same block, we're encouraging jump threading. If |
| 2899 | // not, we are just pessimizing the code by making an i1 phi. |
| 2900 | if (LHSI->getParent() == I.getParent()) |
Chris Lattner | 9922ccf | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 2901 | if (Instruction *NV = FoldOpIntoPhi(I)) |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2902 | return NV; |
| 2903 | break; |
| 2904 | case Instruction::SIToFP: |
| 2905 | case Instruction::UIToFP: |
| 2906 | if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC)) |
| 2907 | return NV; |
| 2908 | break; |
| 2909 | case Instruction::Select: { |
| 2910 | // If either operand of the select is a constant, we can fold the |
| 2911 | // comparison into the select arms, which will cause one to be |
| 2912 | // constant folded and the select turned into a bitwise or. |
| 2913 | Value *Op1 = 0, *Op2 = 0; |
| 2914 | if (LHSI->hasOneUse()) { |
| 2915 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 2916 | // Fold the known value into the constant operand. |
| 2917 | Op1 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 2918 | // Insert a new FCmp of the other select operand. |
| 2919 | Op2 = Builder->CreateFCmp(I.getPredicate(), |
| 2920 | LHSI->getOperand(2), RHSC, I.getName()); |
| 2921 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 2922 | // Fold the known value into the constant operand. |
| 2923 | Op2 = ConstantExpr::getCompare(I.getPredicate(), C, RHSC); |
| 2924 | // Insert a new FCmp of the other select operand. |
| 2925 | Op1 = Builder->CreateFCmp(I.getPredicate(), LHSI->getOperand(1), |
| 2926 | RHSC, I.getName()); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | if (Op1) |
| 2931 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
| 2932 | break; |
| 2933 | } |
Benjamin Kramer | 0db5018 | 2011-03-31 10:12:15 +0000 | [diff] [blame] | 2934 | case Instruction::FSub: { |
| 2935 | // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C |
| 2936 | Value *Op; |
| 2937 | if (match(LHSI, m_FNeg(m_Value(Op)))) |
| 2938 | return new FCmpInst(I.getSwappedPredicate(), Op, |
| 2939 | ConstantExpr::getFNeg(RHSC)); |
| 2940 | break; |
| 2941 | } |
Dan Gohman | 39516a6 | 2010-02-24 06:46:09 +0000 | [diff] [blame] | 2942 | case Instruction::Load: |
| 2943 | if (GetElementPtrInst *GEP = |
| 2944 | dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) { |
| 2945 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 2946 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 2947 | !cast<LoadInst>(LHSI)->isVolatile()) |
| 2948 | if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I)) |
| 2949 | return Res; |
| 2950 | } |
| 2951 | break; |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2952 | } |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Benjamin Kramer | 00e00d6 | 2011-03-31 10:46:03 +0000 | [diff] [blame] | 2955 | // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y |
Benjamin Kramer | 68b4bd0 | 2011-03-31 10:12:22 +0000 | [diff] [blame] | 2956 | Value *X, *Y; |
| 2957 | if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) |
Benjamin Kramer | 00e00d6 | 2011-03-31 10:46:03 +0000 | [diff] [blame] | 2958 | return new FCmpInst(I.getSwappedPredicate(), X, Y); |
Benjamin Kramer | 68b4bd0 | 2011-03-31 10:12:22 +0000 | [diff] [blame] | 2959 | |
Benjamin Kramer | cd0274c | 2011-03-31 10:11:58 +0000 | [diff] [blame] | 2960 | // fcmp (fpext x), (fpext y) -> fcmp x, y |
| 2961 | if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) |
| 2962 | if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) |
| 2963 | if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) |
| 2964 | return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), |
| 2965 | RHSExt->getOperand(0)); |
| 2966 | |
Chris Lattner | 02446fc | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2967 | return Changed ? &I : 0; |
| 2968 | } |