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