Chris Lattner | 2188e40 | 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 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APSInt.h" |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SetVector.h" |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Eli Friedman | 911e12f | 2011-07-20 21:57:23 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/InstructionSimplify.h" |
| 20 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 21 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 25 | #include "llvm/IR/PatternMatch.h" |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | using namespace PatternMatch; |
| 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "instcombine" |
| 34 | |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 35 | // How many times is a select replaced by one of its operands? |
| 36 | STATISTIC(NumSel, "Number of select opts"); |
| 37 | |
| 38 | // Initialization Routines |
| 39 | |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 40 | static ConstantInt *getOne(Constant *C) { |
| 41 | return ConstantInt::get(cast<IntegerType>(C->getType()), 1); |
| 42 | } |
| 43 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 44 | static ConstantInt *ExtractElement(Constant *V, Constant *Idx) { |
| 45 | return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx)); |
| 46 | } |
| 47 | |
| 48 | static bool HasAddOverflow(ConstantInt *Result, |
| 49 | ConstantInt *In1, ConstantInt *In2, |
| 50 | bool IsSigned) { |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 51 | if (!IsSigned) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 52 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 53 | |
| 54 | if (In2->isNegative()) |
| 55 | return Result->getValue().sgt(In1->getValue()); |
| 56 | return Result->getValue().slt(In1->getValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| 60 | /// overflowed for this type. |
| 61 | static bool AddWithOverflow(Constant *&Result, Constant *In1, |
| 62 | Constant *In2, bool IsSigned = false) { |
| 63 | Result = ConstantExpr::getAdd(In1, In2); |
| 64 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 65 | if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 66 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 67 | Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i); |
| 68 | if (HasAddOverflow(ExtractElement(Result, Idx), |
| 69 | ExtractElement(In1, Idx), |
| 70 | ExtractElement(In2, Idx), |
| 71 | IsSigned)) |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | return HasAddOverflow(cast<ConstantInt>(Result), |
| 78 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 79 | IsSigned); |
| 80 | } |
| 81 | |
| 82 | static bool HasSubOverflow(ConstantInt *Result, |
| 83 | ConstantInt *In1, ConstantInt *In2, |
| 84 | bool IsSigned) { |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 85 | if (!IsSigned) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 86 | return Result->getValue().ugt(In1->getValue()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 87 | |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 88 | if (In2->isNegative()) |
| 89 | return Result->getValue().slt(In1->getValue()); |
| 90 | |
| 91 | return Result->getValue().sgt(In1->getValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /// SubWithOverflow - Compute Result = In1-In2, returning true if the result |
| 95 | /// overflowed for this type. |
| 96 | static bool SubWithOverflow(Constant *&Result, Constant *In1, |
| 97 | Constant *In2, bool IsSigned = false) { |
| 98 | Result = ConstantExpr::getSub(In1, In2); |
| 99 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 100 | if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 101 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 102 | Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i); |
| 103 | if (HasSubOverflow(ExtractElement(Result, Idx), |
| 104 | ExtractElement(In1, Idx), |
| 105 | ExtractElement(In2, Idx), |
| 106 | IsSigned)) |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return HasSubOverflow(cast<ConstantInt>(Result), |
| 113 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 114 | IsSigned); |
| 115 | } |
| 116 | |
| 117 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 118 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 119 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 120 | /// signed. |
| 121 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 122 | bool &TrueIfSigned) { |
| 123 | switch (pred) { |
| 124 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 125 | TrueIfSigned = true; |
| 126 | return RHS->isZero(); |
| 127 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 128 | TrueIfSigned = true; |
| 129 | return RHS->isAllOnesValue(); |
| 130 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 131 | TrueIfSigned = false; |
| 132 | return RHS->isAllOnesValue(); |
| 133 | case ICmpInst::ICMP_UGT: |
| 134 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 135 | TrueIfSigned = true; |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 136 | return RHS->isMaxValue(true); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 137 | case ICmpInst::ICMP_UGE: |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 138 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 139 | TrueIfSigned = true; |
| 140 | return RHS->getValue().isSignBit(); |
| 141 | default: |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 146 | /// Returns true if the exploded icmp can be expressed as a signed comparison |
| 147 | /// to zero and updates the predicate accordingly. |
| 148 | /// The signedness of the comparison is preserved. |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 149 | static bool isSignTest(ICmpInst::Predicate &pred, const ConstantInt *RHS) { |
| 150 | if (!ICmpInst::isSigned(pred)) |
| 151 | return false; |
| 152 | |
| 153 | if (RHS->isZero()) |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 154 | return ICmpInst::isRelational(pred); |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 155 | |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 156 | if (RHS->isOne()) { |
| 157 | if (pred == ICmpInst::ICMP_SLT) { |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 158 | pred = ICmpInst::ICMP_SLE; |
| 159 | return true; |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 160 | } |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 161 | } else if (RHS->isAllOnesValue()) { |
| 162 | if (pred == ICmpInst::ICMP_SGT) { |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 163 | pred = ICmpInst::ICMP_SGE; |
| 164 | return true; |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 165 | } |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 166 | } |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 171 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 172 | // This is the same as lowones(~X). |
| 173 | static bool isHighOnes(const ConstantInt *CI) { |
| 174 | return (~CI->getValue() + 1).isPowerOf2(); |
| 175 | } |
| 176 | |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 177 | /// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 178 | /// set of known zero and one bits, compute the maximum and minimum values that |
| 179 | /// could have the specified known zero and known one bits, returning them in |
| 180 | /// min/max. |
| 181 | static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero, |
| 182 | const APInt& KnownOne, |
| 183 | APInt& Min, APInt& Max) { |
| 184 | assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() && |
| 185 | KnownZero.getBitWidth() == Min.getBitWidth() && |
| 186 | KnownZero.getBitWidth() == Max.getBitWidth() && |
| 187 | "KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
| 188 | APInt UnknownBits = ~(KnownZero|KnownOne); |
| 189 | |
| 190 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 191 | // bit if it is unknown. |
| 192 | Min = KnownOne; |
| 193 | Max = KnownOne|UnknownBits; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 195 | if (UnknownBits.isNegative()) { // Sign bit is unknown |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 196 | Min.setBit(Min.getBitWidth()-1); |
| 197 | Max.clearBit(Max.getBitWidth()-1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 202 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 203 | // could have the specified known zero and known one bits, returning them in |
| 204 | // min/max. |
| 205 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero, |
| 206 | const APInt &KnownOne, |
| 207 | APInt &Min, APInt &Max) { |
| 208 | assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() && |
| 209 | KnownZero.getBitWidth() == Min.getBitWidth() && |
| 210 | KnownZero.getBitWidth() == Max.getBitWidth() && |
| 211 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
| 212 | APInt UnknownBits = ~(KnownZero|KnownOne); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 214 | // The minimum value is when the unknown bits are all zeros. |
| 215 | Min = KnownOne; |
| 216 | // The maximum value is when the unknown bits are all ones. |
| 217 | Max = KnownOne|UnknownBits; |
| 218 | } |
| 219 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 220 | /// FoldCmpLoadFromIndexedGlobal - Called we see this pattern: |
| 221 | /// cmp pred (load (gep GV, ...)), cmpcst |
| 222 | /// where GV is a global variable with a constant initializer. Try to simplify |
| 223 | /// this into some simple computation that does not need the load. For example |
| 224 | /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3". |
| 225 | /// |
| 226 | /// If AndCst is non-null, then the loaded value is masked with that constant |
| 227 | /// before doing the comparison. This handles cases like "A[i]&4 == 0". |
| 228 | Instruction *InstCombiner:: |
| 229 | FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV, |
| 230 | CmpInst &ICI, ConstantInt *AndCst) { |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 231 | Constant *Init = GV->getInitializer(); |
| 232 | if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 233 | return nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 234 | |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 235 | uint64_t ArrayElementCount = Init->getType()->getArrayNumElements(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 236 | if (ArrayElementCount > 1024) return nullptr; // Don't blow up on huge arrays. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 238 | // There are many forms of this optimization we can handle, for now, just do |
| 239 | // the simple index into a single-dimensional array. |
| 240 | // |
| 241 | // Require: GEP GV, 0, i {{, constant indices}} |
| 242 | if (GEP->getNumOperands() < 3 || |
| 243 | !isa<ConstantInt>(GEP->getOperand(1)) || |
| 244 | !cast<ConstantInt>(GEP->getOperand(1))->isZero() || |
| 245 | isa<Constant>(GEP->getOperand(2))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 246 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 247 | |
| 248 | // Check that indices after the variable are constants and in-range for the |
| 249 | // type they index. Collect the indices. This is typically for arrays of |
| 250 | // structs. |
| 251 | SmallVector<unsigned, 4> LaterIndices; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 252 | |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 253 | Type *EltTy = Init->getType()->getArrayElementType(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 254 | for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) { |
| 255 | ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 256 | if (!Idx) return nullptr; // Variable index. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 258 | uint64_t IdxVal = Idx->getZExtValue(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 259 | if ((unsigned)IdxVal != IdxVal) return nullptr; // Too large array index. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 261 | if (StructType *STy = dyn_cast<StructType>(EltTy)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 262 | EltTy = STy->getElementType(IdxVal); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 263 | else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 264 | if (IdxVal >= ATy->getNumElements()) return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 265 | EltTy = ATy->getElementType(); |
| 266 | } else { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 267 | return nullptr; // Unknown type. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 268 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 270 | LaterIndices.push_back(IdxVal); |
| 271 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 273 | enum { Overdefined = -3, Undefined = -2 }; |
| 274 | |
| 275 | // Variables for our state machines. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 277 | // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form |
| 278 | // "i == 47 | i == 87", where 47 is the first index the condition is true for, |
| 279 | // and 87 is the second (and last) index. FirstTrueElement is -2 when |
| 280 | // undefined, otherwise set to the first true element. SecondTrueElement is |
| 281 | // -2 when undefined, -3 when overdefined and >= 0 when that index is true. |
| 282 | int FirstTrueElement = Undefined, SecondTrueElement = Undefined; |
| 283 | |
| 284 | // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the |
| 285 | // form "i != 47 & i != 87". Same state transitions as for true elements. |
| 286 | int FirstFalseElement = Undefined, SecondFalseElement = Undefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 288 | /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these |
| 289 | /// define a state machine that triggers for ranges of values that the index |
| 290 | /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'. |
| 291 | /// This is -2 when undefined, -3 when overdefined, and otherwise the last |
| 292 | /// index in the range (inclusive). We use -2 for undefined here because we |
| 293 | /// use relative comparisons and don't want 0-1 to match -1. |
| 294 | int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 296 | // MagicBitvector - This is a magic bitvector where we set a bit if the |
| 297 | // comparison is true for element 'i'. If there are 64 elements or less in |
| 298 | // the array, this will fully represent all the comparison results. |
| 299 | uint64_t MagicBitvector = 0; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 301 | // Scan the array and see if one of our patterns matches. |
| 302 | Constant *CompareRHS = cast<Constant>(ICI.getOperand(1)); |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 303 | for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) { |
| 304 | Constant *Elt = Init->getAggregateElement(i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 305 | if (!Elt) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 307 | // If this is indexing an array of structures, get the structure element. |
| 308 | if (!LaterIndices.empty()) |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 309 | Elt = ConstantExpr::getExtractValue(Elt, LaterIndices); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 311 | // If the element is masked, handle it. |
| 312 | if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 314 | // Find out if the comparison would be true or false for the i'th element. |
| 315 | Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt, |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 316 | CompareRHS, DL, TLI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 317 | // If the result is undef for this element, ignore it. |
| 318 | if (isa<UndefValue>(C)) { |
| 319 | // Extend range state machines to cover this element in case there is an |
| 320 | // undef in the middle of the range. |
| 321 | if (TrueRangeEnd == (int)i-1) |
| 322 | TrueRangeEnd = i; |
| 323 | if (FalseRangeEnd == (int)i-1) |
| 324 | FalseRangeEnd = i; |
| 325 | continue; |
| 326 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 328 | // If we can't compute the result for any of the elements, we have to give |
| 329 | // up evaluating the entire conditional. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 330 | if (!isa<ConstantInt>(C)) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 332 | // Otherwise, we know if the comparison is true or false for this element, |
| 333 | // update our state machines. |
| 334 | bool IsTrueForElt = !cast<ConstantInt>(C)->isZero(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 336 | // State machine for single/double/range index comparison. |
| 337 | if (IsTrueForElt) { |
| 338 | // Update the TrueElement state machine. |
| 339 | if (FirstTrueElement == Undefined) |
| 340 | FirstTrueElement = TrueRangeEnd = i; // First true element. |
| 341 | else { |
| 342 | // Update double-compare state machine. |
| 343 | if (SecondTrueElement == Undefined) |
| 344 | SecondTrueElement = i; |
| 345 | else |
| 346 | SecondTrueElement = Overdefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 348 | // Update range state machine. |
| 349 | if (TrueRangeEnd == (int)i-1) |
| 350 | TrueRangeEnd = i; |
| 351 | else |
| 352 | TrueRangeEnd = Overdefined; |
| 353 | } |
| 354 | } else { |
| 355 | // Update the FalseElement state machine. |
| 356 | if (FirstFalseElement == Undefined) |
| 357 | FirstFalseElement = FalseRangeEnd = i; // First false element. |
| 358 | else { |
| 359 | // Update double-compare state machine. |
| 360 | if (SecondFalseElement == Undefined) |
| 361 | SecondFalseElement = i; |
| 362 | else |
| 363 | SecondFalseElement = Overdefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 365 | // Update range state machine. |
| 366 | if (FalseRangeEnd == (int)i-1) |
| 367 | FalseRangeEnd = i; |
| 368 | else |
| 369 | FalseRangeEnd = Overdefined; |
| 370 | } |
| 371 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 373 | // If this element is in range, update our magic bitvector. |
| 374 | if (i < 64 && IsTrueForElt) |
| 375 | MagicBitvector |= 1ULL << i; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 377 | // If all of our states become overdefined, bail out early. Since the |
| 378 | // predicate is expensive, only check it every 8 elements. This is only |
| 379 | // really useful for really huge arrays. |
| 380 | if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined && |
| 381 | SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined && |
| 382 | FalseRangeEnd == Overdefined) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 383 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // Now that we've scanned the entire array, emit our new comparison(s). We |
| 387 | // order the state machines in complexity of the generated code. |
| 388 | Value *Idx = GEP->getOperand(2); |
| 389 | |
Matt Arsenault | 5aeae18 | 2013-08-19 21:40:31 +0000 | [diff] [blame] | 390 | // If the index is larger than the pointer size of the target, truncate the |
| 391 | // index down like the GEP would do implicitly. We don't have to do this for |
| 392 | // an inbounds GEP because the index can't be out of range. |
Matt Arsenault | 8468062 | 2013-09-30 21:11:01 +0000 | [diff] [blame] | 393 | if (!GEP->isInBounds()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 394 | Type *IntPtrTy = DL.getIntPtrType(GEP->getType()); |
Matt Arsenault | 8468062 | 2013-09-30 21:11:01 +0000 | [diff] [blame] | 395 | unsigned PtrSize = IntPtrTy->getIntegerBitWidth(); |
| 396 | if (Idx->getType()->getPrimitiveSizeInBits() > PtrSize) |
| 397 | Idx = Builder->CreateTrunc(Idx, IntPtrTy); |
| 398 | } |
Matt Arsenault | 5aeae18 | 2013-08-19 21:40:31 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 2188e40 | 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) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 405 | return replaceInstUsesWith(ICI, Builder->getFalse()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 407 | Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 2188e40 | 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) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 425 | return replaceInstUsesWith(ICI, Builder->getTrue()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 2188e40 | 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 | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 469 | |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 470 | // If a magic bitvector captures the entire comparison state |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 471 | // of this load, replace it with computation that does: |
| 472 | // ((magic_cst >> i) & 1) != 0 |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 473 | { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 474 | Type *Ty = nullptr; |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 475 | |
| 476 | // Look for an appropriate type: |
| 477 | // - The type of Idx if the magic fits |
| 478 | // - The smallest fitting legal type if we have a DataLayout |
| 479 | // - Default to i32 |
| 480 | if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth()) |
| 481 | Ty = Idx->getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 482 | else |
| 483 | Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount); |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 484 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 485 | if (Ty) { |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 486 | Value *V = Builder->CreateIntCast(Idx, Ty, false); |
| 487 | V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V); |
| 488 | V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V); |
| 489 | return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0)); |
| 490 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 491 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 492 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 493 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 496 | /// EvaluateGEPOffsetExpression - Return a value that can be used to compare |
| 497 | /// the *offset* implied by a GEP to zero. For example, if we have &A[i], we |
| 498 | /// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can |
| 499 | /// be complex, and scales are involved. The above expression would also be |
| 500 | /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). |
| 501 | /// This later form is less amenable to optimization though, and we are allowed |
| 502 | /// to generate the first by knowing that pointer arithmetic doesn't overflow. |
| 503 | /// |
| 504 | /// If we can't emit an optimized form for this expression, this returns null. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 505 | /// |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 506 | static Value *EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC, |
| 507 | const DataLayout &DL) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 508 | gep_type_iterator GTI = gep_type_begin(GEP); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 510 | // Check to see if this gep only has a single variable index. If so, and if |
| 511 | // any constant indices are a multiple of its scale, then we can compute this |
| 512 | // in terms of the scale of the variable index. For example, if the GEP |
| 513 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 514 | // because the expression will cross zero at the same point. |
| 515 | unsigned i, e = GEP->getNumOperands(); |
| 516 | int64_t Offset = 0; |
| 517 | for (i = 1; i != e; ++i, ++GTI) { |
| 518 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 519 | // Compute the aggregate offset of constant indices. |
| 520 | if (CI->isZero()) continue; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 522 | // Handle a struct index, which adds its field offset to the pointer. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 523 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 524 | Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 525 | } else { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 526 | uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 527 | Offset += Size*CI->getSExtValue(); |
| 528 | } |
| 529 | } else { |
| 530 | // Found our variable index. |
| 531 | break; |
| 532 | } |
| 533 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 534 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 535 | // If there are no variable indices, we must have a constant offset, just |
| 536 | // evaluate it the general way. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 537 | if (i == e) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 539 | Value *VariableIdx = GEP->getOperand(i); |
| 540 | // Determine the scale factor of the variable element. For example, this is |
| 541 | // 4 if the variable index is into an array of i32. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 542 | uint64_t VariableScale = DL.getTypeAllocSize(GTI.getIndexedType()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 544 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 545 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 546 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 547 | if (!CI) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 549 | // Compute the aggregate offset of constant indices. |
| 550 | if (CI->isZero()) continue; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 552 | // Handle a struct index, which adds its field offset to the pointer. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 553 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 554 | Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 555 | } else { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 556 | uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 557 | Offset += Size*CI->getSExtValue(); |
| 558 | } |
| 559 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 561 | // Okay, we know we have a single variable index, which must be a |
| 562 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 563 | // the index. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 564 | Type *IntPtrTy = DL.getIntPtrType(GEP->getOperand(0)->getType()); |
Matt Arsenault | 745101d | 2013-08-21 19:53:10 +0000 | [diff] [blame] | 565 | unsigned IntPtrWidth = IntPtrTy->getIntegerBitWidth(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 566 | if (Offset == 0) { |
| 567 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 568 | // we don't need to bother extending: the extension won't affect where the |
| 569 | // computation crosses zero. |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 570 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) { |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 571 | VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy); |
| 572 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 573 | return VariableIdx; |
| 574 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 576 | // Otherwise, there is an index. The computation we will do will be modulo |
| 577 | // the pointer size, so get it. |
| 578 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 580 | Offset &= PtrSizeMask; |
| 581 | VariableScale &= PtrSizeMask; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 583 | // To do this transformation, any constant index must be a multiple of the |
| 584 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 585 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 586 | // multiple of the variable scale. |
| 587 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 588 | if (Offset != NewOffs*(int64_t)VariableScale) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 589 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 591 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 592 | if (VariableIdx->getType() != IntPtrTy) |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 593 | VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy, |
| 594 | true /*Signed*/); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 595 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 596 | return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset"); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 599 | /// Returns true if we can rewrite Start as a GEP with pointer Base |
| 600 | /// and some integer offset. The nodes that need to be re-written |
| 601 | /// for this transformation will be added to Explored. |
| 602 | static bool canRewriteGEPAsOffset(Value *Start, Value *Base, |
| 603 | const DataLayout &DL, |
| 604 | SetVector<Value *> &Explored) { |
| 605 | SmallVector<Value *, 16> WorkList(1, Start); |
| 606 | Explored.insert(Base); |
| 607 | |
| 608 | // The following traversal gives us an order which can be used |
| 609 | // when doing the final transformation. Since in the final |
| 610 | // transformation we create the PHI replacement instructions first, |
| 611 | // we don't have to get them in any particular order. |
| 612 | // |
| 613 | // However, for other instructions we will have to traverse the |
| 614 | // operands of an instruction first, which means that we have to |
| 615 | // do a post-order traversal. |
| 616 | while (!WorkList.empty()) { |
| 617 | SetVector<PHINode *> PHIs; |
| 618 | |
| 619 | while (!WorkList.empty()) { |
| 620 | if (Explored.size() >= 100) |
| 621 | return false; |
| 622 | |
| 623 | Value *V = WorkList.back(); |
| 624 | |
| 625 | if (Explored.count(V) != 0) { |
| 626 | WorkList.pop_back(); |
| 627 | continue; |
| 628 | } |
| 629 | |
| 630 | if (!isa<IntToPtrInst>(V) && !isa<PtrToIntInst>(V) && |
| 631 | !isa<GEPOperator>(V) && !isa<PHINode>(V)) |
| 632 | // We've found some value that we can't explore which is different from |
| 633 | // the base. Therefore we can't do this transformation. |
| 634 | return false; |
| 635 | |
| 636 | if (isa<IntToPtrInst>(V) || isa<PtrToIntInst>(V)) { |
| 637 | auto *CI = dyn_cast<CastInst>(V); |
| 638 | if (!CI->isNoopCast(DL)) |
| 639 | return false; |
| 640 | |
| 641 | if (Explored.count(CI->getOperand(0)) == 0) |
| 642 | WorkList.push_back(CI->getOperand(0)); |
| 643 | } |
| 644 | |
| 645 | if (auto *GEP = dyn_cast<GEPOperator>(V)) { |
| 646 | // We're limiting the GEP to having one index. This will preserve |
| 647 | // the original pointer type. We could handle more cases in the |
| 648 | // future. |
| 649 | if (GEP->getNumIndices() != 1 || !GEP->isInBounds() || |
| 650 | GEP->getType() != Start->getType()) |
| 651 | return false; |
| 652 | |
| 653 | if (Explored.count(GEP->getOperand(0)) == 0) |
| 654 | WorkList.push_back(GEP->getOperand(0)); |
| 655 | } |
| 656 | |
| 657 | if (WorkList.back() == V) { |
| 658 | WorkList.pop_back(); |
| 659 | // We've finished visiting this node, mark it as such. |
| 660 | Explored.insert(V); |
| 661 | } |
| 662 | |
| 663 | if (auto *PN = dyn_cast<PHINode>(V)) { |
| 664 | Explored.insert(PN); |
| 665 | PHIs.insert(PN); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // Explore the PHI nodes further. |
| 670 | for (auto *PN : PHIs) |
| 671 | for (Value *Op : PN->incoming_values()) |
| 672 | if (Explored.count(Op) == 0) |
| 673 | WorkList.push_back(Op); |
| 674 | } |
| 675 | |
| 676 | // Make sure that we can do this. Since we can't insert GEPs in a basic |
| 677 | // block before a PHI node, we can't easily do this transformation if |
| 678 | // we have PHI node users of transformed instructions. |
| 679 | for (Value *Val : Explored) { |
| 680 | for (Value *Use : Val->uses()) { |
| 681 | |
| 682 | auto *PHI = dyn_cast<PHINode>(Use); |
| 683 | auto *Inst = dyn_cast<Instruction>(Val); |
| 684 | |
| 685 | if (Inst == Base || Inst == PHI || !Inst || !PHI || |
| 686 | Explored.count(PHI) == 0) |
| 687 | continue; |
| 688 | |
| 689 | if (PHI->getParent() == Inst->getParent()) |
| 690 | return false; |
| 691 | } |
| 692 | } |
| 693 | return true; |
| 694 | } |
| 695 | |
| 696 | // Sets the appropriate insert point on Builder where we can add |
| 697 | // a replacement Instruction for V (if that is possible). |
| 698 | static void setInsertionPoint(IRBuilder<> &Builder, Value *V, |
| 699 | bool Before = true) { |
| 700 | if (auto *PHI = dyn_cast<PHINode>(V)) { |
| 701 | Builder.SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt()); |
| 702 | return; |
| 703 | } |
| 704 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 705 | if (!Before) |
| 706 | I = &*std::next(I->getIterator()); |
| 707 | Builder.SetInsertPoint(I); |
| 708 | return; |
| 709 | } |
| 710 | if (auto *A = dyn_cast<Argument>(V)) { |
| 711 | // Set the insertion point in the entry block. |
| 712 | BasicBlock &Entry = A->getParent()->getEntryBlock(); |
| 713 | Builder.SetInsertPoint(&*Entry.getFirstInsertionPt()); |
| 714 | return; |
| 715 | } |
| 716 | // Otherwise, this is a constant and we don't need to set a new |
| 717 | // insertion point. |
| 718 | assert(isa<Constant>(V) && "Setting insertion point for unknown value!"); |
| 719 | } |
| 720 | |
| 721 | /// Returns a re-written value of Start as an indexed GEP using Base as a |
| 722 | /// pointer. |
| 723 | static Value *rewriteGEPAsOffset(Value *Start, Value *Base, |
| 724 | const DataLayout &DL, |
| 725 | SetVector<Value *> &Explored) { |
| 726 | // Perform all the substitutions. This is a bit tricky because we can |
| 727 | // have cycles in our use-def chains. |
| 728 | // 1. Create the PHI nodes without any incoming values. |
| 729 | // 2. Create all the other values. |
| 730 | // 3. Add the edges for the PHI nodes. |
| 731 | // 4. Emit GEPs to get the original pointers. |
| 732 | // 5. Remove the original instructions. |
| 733 | Type *IndexType = IntegerType::get( |
| 734 | Base->getContext(), DL.getPointerTypeSizeInBits(Start->getType())); |
| 735 | |
| 736 | DenseMap<Value *, Value *> NewInsts; |
| 737 | NewInsts[Base] = ConstantInt::getNullValue(IndexType); |
| 738 | |
| 739 | // Create the new PHI nodes, without adding any incoming values. |
| 740 | for (Value *Val : Explored) { |
| 741 | if (Val == Base) |
| 742 | continue; |
| 743 | // Create empty phi nodes. This avoids cyclic dependencies when creating |
| 744 | // the remaining instructions. |
| 745 | if (auto *PHI = dyn_cast<PHINode>(Val)) |
| 746 | NewInsts[PHI] = PHINode::Create(IndexType, PHI->getNumIncomingValues(), |
| 747 | PHI->getName() + ".idx", PHI); |
| 748 | } |
| 749 | IRBuilder<> Builder(Base->getContext()); |
| 750 | |
| 751 | // Create all the other instructions. |
| 752 | for (Value *Val : Explored) { |
| 753 | |
| 754 | if (NewInsts.find(Val) != NewInsts.end()) |
| 755 | continue; |
| 756 | |
| 757 | if (auto *CI = dyn_cast<CastInst>(Val)) { |
| 758 | NewInsts[CI] = NewInsts[CI->getOperand(0)]; |
| 759 | continue; |
| 760 | } |
| 761 | if (auto *GEP = dyn_cast<GEPOperator>(Val)) { |
| 762 | Value *Index = NewInsts[GEP->getOperand(1)] ? NewInsts[GEP->getOperand(1)] |
| 763 | : GEP->getOperand(1); |
| 764 | setInsertionPoint(Builder, GEP); |
| 765 | // Indices might need to be sign extended. GEPs will magically do |
| 766 | // this, but we need to do it ourselves here. |
| 767 | if (Index->getType()->getScalarSizeInBits() != |
| 768 | NewInsts[GEP->getOperand(0)]->getType()->getScalarSizeInBits()) { |
| 769 | Index = Builder.CreateSExtOrTrunc( |
| 770 | Index, NewInsts[GEP->getOperand(0)]->getType(), |
| 771 | GEP->getOperand(0)->getName() + ".sext"); |
| 772 | } |
| 773 | |
| 774 | auto *Op = NewInsts[GEP->getOperand(0)]; |
| 775 | if (isa<ConstantInt>(Op) && dyn_cast<ConstantInt>(Op)->isZero()) |
| 776 | NewInsts[GEP] = Index; |
| 777 | else |
| 778 | NewInsts[GEP] = Builder.CreateNSWAdd( |
| 779 | Op, Index, GEP->getOperand(0)->getName() + ".add"); |
| 780 | continue; |
| 781 | } |
| 782 | if (isa<PHINode>(Val)) |
| 783 | continue; |
| 784 | |
| 785 | llvm_unreachable("Unexpected instruction type"); |
| 786 | } |
| 787 | |
| 788 | // Add the incoming values to the PHI nodes. |
| 789 | for (Value *Val : Explored) { |
| 790 | if (Val == Base) |
| 791 | continue; |
| 792 | // All the instructions have been created, we can now add edges to the |
| 793 | // phi nodes. |
| 794 | if (auto *PHI = dyn_cast<PHINode>(Val)) { |
| 795 | PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]); |
| 796 | for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) { |
| 797 | Value *NewIncoming = PHI->getIncomingValue(I); |
| 798 | |
| 799 | if (NewInsts.find(NewIncoming) != NewInsts.end()) |
| 800 | NewIncoming = NewInsts[NewIncoming]; |
| 801 | |
| 802 | NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I)); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | for (Value *Val : Explored) { |
| 808 | if (Val == Base) |
| 809 | continue; |
| 810 | |
| 811 | // Depending on the type, for external users we have to emit |
| 812 | // a GEP or a GEP + ptrtoint. |
| 813 | setInsertionPoint(Builder, Val, false); |
| 814 | |
| 815 | // If required, create an inttoptr instruction for Base. |
| 816 | Value *NewBase = Base; |
| 817 | if (!Base->getType()->isPointerTy()) |
| 818 | NewBase = Builder.CreateBitOrPointerCast(Base, Start->getType(), |
| 819 | Start->getName() + "to.ptr"); |
| 820 | |
| 821 | Value *GEP = Builder.CreateInBoundsGEP( |
| 822 | Start->getType()->getPointerElementType(), NewBase, |
| 823 | makeArrayRef(NewInsts[Val]), Val->getName() + ".ptr"); |
| 824 | |
| 825 | if (!Val->getType()->isPointerTy()) { |
| 826 | Value *Cast = Builder.CreatePointerCast(GEP, Val->getType(), |
| 827 | Val->getName() + ".conv"); |
| 828 | GEP = Cast; |
| 829 | } |
| 830 | Val->replaceAllUsesWith(GEP); |
| 831 | } |
| 832 | |
| 833 | return NewInsts[Start]; |
| 834 | } |
| 835 | |
| 836 | /// Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express |
| 837 | /// the input Value as a constant indexed GEP. Returns a pair containing |
| 838 | /// the GEPs Pointer and Index. |
| 839 | static std::pair<Value *, Value *> |
| 840 | getAsConstantIndexedAddress(Value *V, const DataLayout &DL) { |
| 841 | Type *IndexType = IntegerType::get(V->getContext(), |
| 842 | DL.getPointerTypeSizeInBits(V->getType())); |
| 843 | |
| 844 | Constant *Index = ConstantInt::getNullValue(IndexType); |
| 845 | while (true) { |
| 846 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 847 | // We accept only inbouds GEPs here to exclude the possibility of |
| 848 | // overflow. |
| 849 | if (!GEP->isInBounds()) |
| 850 | break; |
| 851 | if (GEP->hasAllConstantIndices() && GEP->getNumIndices() == 1 && |
| 852 | GEP->getType() == V->getType()) { |
| 853 | V = GEP->getOperand(0); |
| 854 | Constant *GEPIndex = static_cast<Constant *>(GEP->getOperand(1)); |
| 855 | Index = ConstantExpr::getAdd( |
| 856 | Index, ConstantExpr::getSExtOrBitCast(GEPIndex, IndexType)); |
| 857 | continue; |
| 858 | } |
| 859 | break; |
| 860 | } |
| 861 | if (auto *CI = dyn_cast<IntToPtrInst>(V)) { |
| 862 | if (!CI->isNoopCast(DL)) |
| 863 | break; |
| 864 | V = CI->getOperand(0); |
| 865 | continue; |
| 866 | } |
| 867 | if (auto *CI = dyn_cast<PtrToIntInst>(V)) { |
| 868 | if (!CI->isNoopCast(DL)) |
| 869 | break; |
| 870 | V = CI->getOperand(0); |
| 871 | continue; |
| 872 | } |
| 873 | break; |
| 874 | } |
| 875 | return {V, Index}; |
| 876 | } |
| 877 | |
| 878 | // Converts (CMP GEPLHS, RHS) if this change would make RHS a constant. |
| 879 | // We can look through PHIs, GEPs and casts in order to determine a |
| 880 | // common base between GEPLHS and RHS. |
| 881 | static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, |
| 882 | ICmpInst::Predicate Cond, |
| 883 | const DataLayout &DL) { |
| 884 | if (!GEPLHS->hasAllConstantIndices()) |
| 885 | return nullptr; |
| 886 | |
| 887 | Value *PtrBase, *Index; |
| 888 | std::tie(PtrBase, Index) = getAsConstantIndexedAddress(GEPLHS, DL); |
| 889 | |
| 890 | // The set of nodes that will take part in this transformation. |
| 891 | SetVector<Value *> Nodes; |
| 892 | |
| 893 | if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes)) |
| 894 | return nullptr; |
| 895 | |
| 896 | // We know we can re-write this as |
| 897 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) |
| 898 | // Since we've only looked through inbouds GEPs we know that we |
| 899 | // can't have overflow on either side. We can therefore re-write |
| 900 | // this as: |
| 901 | // OFFSET1 cmp OFFSET2 |
| 902 | Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes); |
| 903 | |
| 904 | // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written |
| 905 | // GEP having PtrBase as the pointer base, and has returned in NewRHS the |
| 906 | // offset. Since Index is the offset of LHS to the base pointer, we will now |
| 907 | // compare the offsets instead of comparing the pointers. |
| 908 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Index, NewRHS); |
| 909 | } |
| 910 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 911 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
| 912 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 913 | Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 914 | ICmpInst::Predicate Cond, |
| 915 | Instruction &I) { |
Benjamin Kramer | 6ee8690 | 2012-02-21 13:31:09 +0000 | [diff] [blame] | 916 | // Don't transform signed compares of GEPs into index compares. Even if the |
| 917 | // GEP is inbounds, the final add of the base pointer can have signed overflow |
| 918 | // and would change the result of the icmp. |
| 919 | // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be |
Benjamin Kramer | c7a22fe | 2012-02-21 13:40:06 +0000 | [diff] [blame] | 920 | // the maximum signed value for the pointer type. |
Benjamin Kramer | 6ee8690 | 2012-02-21 13:31:09 +0000 | [diff] [blame] | 921 | if (ICmpInst::isSigned(Cond)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 922 | return nullptr; |
Benjamin Kramer | 6ee8690 | 2012-02-21 13:31:09 +0000 | [diff] [blame] | 923 | |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 924 | // Look through bitcasts and addrspacecasts. We do not however want to remove |
| 925 | // 0 GEPs. |
| 926 | if (!isa<GetElementPtrInst>(RHS)) |
| 927 | RHS = RHS->stripPointerCasts(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 928 | |
| 929 | Value *PtrBase = GEPLHS->getOperand(0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 930 | if (PtrBase == RHS && GEPLHS->isInBounds()) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 931 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 932 | // This transformation (ignoring the base and scales) is valid because we |
| 933 | // know pointers can't overflow since the gep is inbounds. See if we can |
| 934 | // output an optimized form. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 935 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, *this, DL); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 937 | // If not, synthesize the offset the hard way. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 938 | if (!Offset) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 939 | Offset = EmitGEPOffset(GEPLHS); |
| 940 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 941 | Constant::getNullValue(Offset->getType())); |
| 942 | } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) { |
| 943 | // If the base pointers are different, but the indices are the same, just |
| 944 | // compare the base pointer. |
| 945 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 946 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
| 947 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
| 948 | GEPRHS->getOperand(0)->getType(); |
| 949 | if (IndicesTheSame) |
| 950 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 951 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 952 | IndicesTheSame = false; |
| 953 | break; |
| 954 | } |
| 955 | |
| 956 | // If all indices are the same, just compare the base pointers. |
| 957 | if (IndicesTheSame) |
David Majnemer | 5953d37 | 2013-06-29 10:28:04 +0000 | [diff] [blame] | 958 | return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 959 | |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 960 | // If we're comparing GEPs with two base pointers that only differ in type |
| 961 | // and both GEPs have only constant indices or just one use, then fold |
| 962 | // the compare with the adjusted indices. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 963 | if (GEPLHS->isInBounds() && GEPRHS->isInBounds() && |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 964 | (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) && |
| 965 | (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) && |
| 966 | PtrBase->stripPointerCasts() == |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 967 | GEPRHS->getOperand(0)->stripPointerCasts()) { |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 968 | Value *LOffset = EmitGEPOffset(GEPLHS); |
| 969 | Value *ROffset = EmitGEPOffset(GEPRHS); |
| 970 | |
| 971 | // If we looked through an addrspacecast between different sized address |
| 972 | // spaces, the LHS and RHS pointers are different sized |
| 973 | // integers. Truncate to the smaller one. |
| 974 | Type *LHSIndexTy = LOffset->getType(); |
| 975 | Type *RHSIndexTy = ROffset->getType(); |
| 976 | if (LHSIndexTy != RHSIndexTy) { |
| 977 | if (LHSIndexTy->getPrimitiveSizeInBits() < |
| 978 | RHSIndexTy->getPrimitiveSizeInBits()) { |
| 979 | ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy); |
| 980 | } else |
| 981 | LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy); |
| 982 | } |
| 983 | |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 984 | Value *Cmp = Builder->CreateICmp(ICmpInst::getSignedPredicate(Cond), |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 985 | LOffset, ROffset); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 986 | return replaceInstUsesWith(I, Cmp); |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 989 | // Otherwise, the base pointers are different and the indices are |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 990 | // different. Try convert this to an indexed compare by looking through |
| 991 | // PHIs/casts. |
| 992 | return transformToIndexedCompare(GEPLHS, RHS, Cond, DL); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | // If one of the GEPs has all zero indices, recurse. |
Benjamin Kramer | d0993e0 | 2014-07-07 11:01:16 +0000 | [diff] [blame] | 996 | if (GEPLHS->hasAllZeroIndices()) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 997 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
David Majnemer | 92a8a7d | 2013-06-29 09:45:35 +0000 | [diff] [blame] | 998 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 999 | |
| 1000 | // If the other GEP has all zero indices, recurse. |
Benjamin Kramer | d0993e0 | 2014-07-07 11:01:16 +0000 | [diff] [blame] | 1001 | if (GEPRHS->hasAllZeroIndices()) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1002 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 1003 | |
Stuart Hastings | 66a82b9 | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 1004 | bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1005 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 1006 | // If the GEPs only differ by one index, compare it. |
| 1007 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 1008 | unsigned DiffOperand = 0; // The operand that differs. |
| 1009 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 1010 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 1011 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 1012 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
| 1013 | // Irreconcilable differences. |
| 1014 | NumDifferences = 2; |
| 1015 | break; |
| 1016 | } else { |
| 1017 | if (NumDifferences++) break; |
| 1018 | DiffOperand = i; |
| 1019 | } |
| 1020 | } |
| 1021 | |
Rafael Espindola | a7bbc0b | 2013-06-06 17:03:05 +0000 | [diff] [blame] | 1022 | if (NumDifferences == 0) // SAME GEP? |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1023 | return replaceInstUsesWith(I, // No comparison is needed here. |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1024 | Builder->getInt1(ICmpInst::isTrueWhenEqual(Cond))); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1025 | |
Stuart Hastings | 66a82b9 | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 1026 | else if (NumDifferences == 1 && GEPsInBounds) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1027 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 1028 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
| 1029 | // Make sure we do a signed comparison here. |
| 1030 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | // Only lower this if the icmp is the only user of the GEP or if we expect |
| 1035 | // the result to fold to a constant! |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1036 | if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1037 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 1038 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 1039 | Value *L = EmitGEPOffset(GEPLHS); |
| 1040 | Value *R = EmitGEPOffset(GEPRHS); |
| 1041 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
| 1042 | } |
| 1043 | } |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 1044 | |
| 1045 | // Try convert this to an indexed compare by looking through PHIs/casts as a |
| 1046 | // last resort. |
| 1047 | return transformToIndexedCompare(GEPLHS, RHS, Cond, DL); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1050 | Instruction *InstCombiner::FoldAllocaCmp(ICmpInst &ICI, AllocaInst *Alloca, |
| 1051 | Value *Other) { |
| 1052 | assert(ICI.isEquality() && "Cannot fold non-equality comparison."); |
| 1053 | |
| 1054 | // It would be tempting to fold away comparisons between allocas and any |
| 1055 | // pointer not based on that alloca (e.g. an argument). However, even |
| 1056 | // though such pointers cannot alias, they can still compare equal. |
| 1057 | // |
| 1058 | // But LLVM doesn't specify where allocas get their memory, so if the alloca |
| 1059 | // doesn't escape we can argue that it's impossible to guess its value, and we |
| 1060 | // can therefore act as if any such guesses are wrong. |
| 1061 | // |
| 1062 | // The code below checks that the alloca doesn't escape, and that it's only |
| 1063 | // used in a comparison once (the current instruction). The |
| 1064 | // single-comparison-use condition ensures that we're trivially folding all |
| 1065 | // comparisons against the alloca consistently, and avoids the risk of |
| 1066 | // erroneously folding a comparison of the pointer with itself. |
| 1067 | |
| 1068 | unsigned MaxIter = 32; // Break cycles and bound to constant-time. |
| 1069 | |
| 1070 | SmallVector<Use *, 32> Worklist; |
| 1071 | for (Use &U : Alloca->uses()) { |
| 1072 | if (Worklist.size() >= MaxIter) |
| 1073 | return nullptr; |
| 1074 | Worklist.push_back(&U); |
| 1075 | } |
| 1076 | |
| 1077 | unsigned NumCmps = 0; |
| 1078 | while (!Worklist.empty()) { |
| 1079 | assert(Worklist.size() <= MaxIter); |
| 1080 | Use *U = Worklist.pop_back_val(); |
| 1081 | Value *V = U->getUser(); |
| 1082 | --MaxIter; |
| 1083 | |
| 1084 | if (isa<BitCastInst>(V) || isa<GetElementPtrInst>(V) || isa<PHINode>(V) || |
| 1085 | isa<SelectInst>(V)) { |
| 1086 | // Track the uses. |
| 1087 | } else if (isa<LoadInst>(V)) { |
| 1088 | // Loading from the pointer doesn't escape it. |
| 1089 | continue; |
| 1090 | } else if (auto *SI = dyn_cast<StoreInst>(V)) { |
| 1091 | // Storing *to* the pointer is fine, but storing the pointer escapes it. |
| 1092 | if (SI->getValueOperand() == U->get()) |
| 1093 | return nullptr; |
| 1094 | continue; |
| 1095 | } else if (isa<ICmpInst>(V)) { |
| 1096 | if (NumCmps++) |
| 1097 | return nullptr; // Found more than one cmp. |
| 1098 | continue; |
| 1099 | } else if (auto *Intrin = dyn_cast<IntrinsicInst>(V)) { |
| 1100 | switch (Intrin->getIntrinsicID()) { |
| 1101 | // These intrinsics don't escape or compare the pointer. Memset is safe |
| 1102 | // because we don't allow ptrtoint. Memcpy and memmove are safe because |
| 1103 | // we don't allow stores, so src cannot point to V. |
| 1104 | case Intrinsic::lifetime_start: case Intrinsic::lifetime_end: |
| 1105 | case Intrinsic::dbg_declare: case Intrinsic::dbg_value: |
| 1106 | case Intrinsic::memcpy: case Intrinsic::memmove: case Intrinsic::memset: |
| 1107 | continue; |
| 1108 | default: |
| 1109 | return nullptr; |
| 1110 | } |
| 1111 | } else { |
| 1112 | return nullptr; |
| 1113 | } |
| 1114 | for (Use &U : V->uses()) { |
| 1115 | if (Worklist.size() >= MaxIter) |
| 1116 | return nullptr; |
| 1117 | Worklist.push_back(&U); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | Type *CmpTy = CmpInst::makeCmpResultType(Other->getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1122 | return replaceInstUsesWith( |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1123 | ICI, |
| 1124 | ConstantInt::get(CmpTy, !CmpInst::isTrueWhenEqual(ICI.getPredicate()))); |
| 1125 | } |
| 1126 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1127 | /// FoldICmpAddOpCst - Fold "icmp pred (X+CI), X". |
Benjamin Kramer | 0e2d162 | 2013-09-20 22:12:42 +0000 | [diff] [blame] | 1128 | Instruction *InstCombiner::FoldICmpAddOpCst(Instruction &ICI, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1129 | Value *X, ConstantInt *CI, |
Benjamin Kramer | 0e2d162 | 2013-09-20 22:12:42 +0000 | [diff] [blame] | 1130 | ICmpInst::Predicate Pred) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1131 | // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0, |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1132 | // so the values can never be equal. Similarly for all other "or equals" |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1133 | // operators. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | 8c92b57 | 2010-01-08 17:48:19 +0000 | [diff] [blame] | 1135 | // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255 |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1136 | // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253 |
| 1137 | // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0 |
| 1138 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1139 | Value *R = |
Chris Lattner | 8c92b57 | 2010-01-08 17:48:19 +0000 | [diff] [blame] | 1140 | ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1141 | return new ICmpInst(ICmpInst::ICMP_UGT, X, R); |
| 1142 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1144 | // (X+1) >u X --> X <u (0-1) --> X != 255 |
| 1145 | // (X+2) >u X --> X <u (0-2) --> X <u 254 |
| 1146 | // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0 |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 1147 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1148 | return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1150 | unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits(); |
| 1151 | ConstantInt *SMax = ConstantInt::get(X->getContext(), |
| 1152 | APInt::getSignedMaxValue(BitWidth)); |
| 1153 | |
| 1154 | // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127 |
| 1155 | // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125 |
| 1156 | // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0 |
| 1157 | // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1 |
| 1158 | // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126 |
| 1159 | // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127 |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 1160 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1161 | return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1163 | // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127 |
| 1164 | // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126 |
| 1165 | // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1 |
| 1166 | // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2 |
| 1167 | // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126 |
| 1168 | // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128 |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1169 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1170 | assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE); |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1171 | Constant *C = Builder->getInt(CI->getValue()-1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1172 | return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C)); |
| 1173 | } |
| 1174 | |
| 1175 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 1176 | /// and CmpRHS are both known to be integer constants. |
| 1177 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 1178 | ConstantInt *DivRHS) { |
| 1179 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 1180 | const APInt &CmpRHSV = CmpRHS->getValue(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1181 | |
| 1182 | // FIXME: If the operand types don't match the type of the divide |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1183 | // then don't attempt this transform. The code below doesn't have the |
| 1184 | // logic to deal with a signed divide and an unsigned compare (and |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1185 | // vice versa). This is because (x /s C1) <s C2 produces different |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1186 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1187 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 1188 | // work. :( The if statement below tests that condition and bails |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1189 | // if it finds it. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1190 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 1191 | if (!ICI.isEquality() && DivIsSigned != ICI.isSigned()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1192 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1193 | if (DivRHS->isZero()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1194 | return nullptr; // The ProdOV computation fails on divide by zero. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1195 | if (DivIsSigned && DivRHS->isAllOnesValue()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1196 | return nullptr; // The overflow computation also screws up here |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1197 | if (DivRHS->isOne()) { |
| 1198 | // This eliminates some funny cases with INT_MIN. |
| 1199 | ICI.setOperand(0, DivI->getOperand(0)); // X/1 == X. |
| 1200 | return &ICI; |
| 1201 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1204 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 1205 | // C2 (CI). By solving for X we can turn this into a range check |
| 1206 | // instead of computing a divide. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1207 | Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS); |
| 1208 | |
| 1209 | // Determine if the product overflows by seeing if the product is |
| 1210 | // not equal to the divide. Make sure we do the same kind of divide |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1211 | // as in the LHS instruction that we're folding. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1212 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) : |
| 1213 | ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
| 1214 | |
| 1215 | // Get the ICmp opcode |
| 1216 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
| 1217 | |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1218 | /// If the division is known to be exact, then there is no remainder from the |
| 1219 | /// divide, so the covered range size is unit, otherwise it is the divisor. |
| 1220 | ConstantInt *RangeSize = DivI->isExact() ? getOne(Prod) : DivRHS; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1221 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1222 | // Figure out the interval that is being checked. For example, a comparison |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1223 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1224 | // Compute this interval based on the constants involved and the signedness of |
| 1225 | // the compare/divide. This computes a half-open interval, keeping track of |
| 1226 | // whether either value in the interval overflows. After analysis each |
| 1227 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 1228 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 1229 | int LoOverflow = 0, HiOverflow = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1230 | Constant *LoBound = nullptr, *HiBound = nullptr; |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1232 | if (!DivIsSigned) { // udiv |
| 1233 | // e.g. X/5 op 3 --> [15, 20) |
| 1234 | LoBound = Prod; |
| 1235 | HiOverflow = LoOverflow = ProdOV; |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1236 | if (!HiOverflow) { |
| 1237 | // If this is not an exact divide, then many values in the range collapse |
| 1238 | // to the same result value. |
| 1239 | HiOverflow = AddWithOverflow(HiBound, LoBound, RangeSize, false); |
| 1240 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1241 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
| 1242 | if (CmpRHSV == 0) { // (X / pos) op 0 |
| 1243 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1244 | LoBound = ConstantExpr::getNeg(SubOne(RangeSize)); |
| 1245 | HiBound = RangeSize; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1246 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
| 1247 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 1248 | HiOverflow = LoOverflow = ProdOV; |
| 1249 | if (!HiOverflow) |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1250 | HiOverflow = AddWithOverflow(HiBound, Prod, RangeSize, true); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1251 | } else { // (X / pos) op neg |
| 1252 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
| 1253 | HiBound = AddOne(Prod); |
| 1254 | LoOverflow = HiOverflow = ProdOV ? -1 : 0; |
| 1255 | if (!LoOverflow) { |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1256 | ConstantInt *DivNeg =cast<ConstantInt>(ConstantExpr::getNeg(RangeSize)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1257 | LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0; |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1258 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1259 | } |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1260 | } else if (DivRHS->isNegative()) { // Divisor is < 0. |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1261 | if (DivI->isExact()) |
| 1262 | RangeSize = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1263 | if (CmpRHSV == 0) { // (X / neg) op 0 |
| 1264 | // e.g. X/-5 op 0 --> [-4, 5) |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1265 | LoBound = AddOne(RangeSize); |
| 1266 | HiBound = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1267 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 1268 | HiOverflow = 1; // [INTMIN+1, overflow) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1269 | HiBound = nullptr; // e.g. X/INTMIN = 0 --> X > INTMIN |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
| 1272 | // e.g. X/-5 op 3 --> [-19, -14) |
| 1273 | HiBound = AddOne(Prod); |
| 1274 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
| 1275 | if (!LoOverflow) |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1276 | LoOverflow = AddWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1277 | } else { // (X / neg) op neg |
| 1278 | LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20) |
| 1279 | LoOverflow = HiOverflow = ProdOV; |
| 1280 | if (!HiOverflow) |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1281 | HiOverflow = SubWithOverflow(HiBound, Prod, RangeSize, true); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1282 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1283 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1284 | // Dividing by a negative swaps the condition. LT <-> GT |
| 1285 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 1286 | } |
| 1287 | |
| 1288 | Value *X = DivI->getOperand(0); |
| 1289 | switch (Pred) { |
| 1290 | default: llvm_unreachable("Unhandled icmp opcode!"); |
| 1291 | case ICmpInst::ICMP_EQ: |
| 1292 | if (LoOverflow && HiOverflow) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1293 | return replaceInstUsesWith(ICI, Builder->getFalse()); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1294 | if (HiOverflow) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1295 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 1296 | ICmpInst::ICMP_UGE, X, LoBound); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1297 | if (LoOverflow) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1298 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 1299 | ICmpInst::ICMP_ULT, X, HiBound); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1300 | return replaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound, |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1301 | DivIsSigned, true)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1302 | case ICmpInst::ICMP_NE: |
| 1303 | if (LoOverflow && HiOverflow) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1304 | return replaceInstUsesWith(ICI, Builder->getTrue()); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1305 | if (HiOverflow) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1306 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 1307 | ICmpInst::ICMP_ULT, X, LoBound); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1308 | if (LoOverflow) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1309 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 1310 | ICmpInst::ICMP_UGE, X, HiBound); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1311 | return replaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound, |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 1312 | DivIsSigned, false)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1313 | case ICmpInst::ICMP_ULT: |
| 1314 | case ICmpInst::ICMP_SLT: |
| 1315 | if (LoOverflow == +1) // Low bound is greater than input range. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1316 | return replaceInstUsesWith(ICI, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1317 | if (LoOverflow == -1) // Low bound is less than input range. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1318 | return replaceInstUsesWith(ICI, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1319 | return new ICmpInst(Pred, X, LoBound); |
| 1320 | case ICmpInst::ICMP_UGT: |
| 1321 | case ICmpInst::ICMP_SGT: |
| 1322 | if (HiOverflow == +1) // High bound greater than input range. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1323 | return replaceInstUsesWith(ICI, Builder->getFalse()); |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1324 | if (HiOverflow == -1) // High bound less than input range. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1325 | return replaceInstUsesWith(ICI, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1326 | if (Pred == ICmpInst::ICMP_UGT) |
| 1327 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1328 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1332 | /// FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)". |
| 1333 | Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr, |
| 1334 | ConstantInt *ShAmt) { |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1335 | const APInt &CmpRHSV = cast<ConstantInt>(ICI.getOperand(1))->getValue(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1337 | // Check that the shift amount is in range. If not, don't perform |
| 1338 | // undefined shifts. When the shift is visited it will be |
| 1339 | // simplified. |
| 1340 | uint32_t TypeBits = CmpRHSV.getBitWidth(); |
| 1341 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1342 | if (ShAmtVal >= TypeBits || ShAmtVal == 0) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1343 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1345 | if (!ICI.isEquality()) { |
| 1346 | // If we have an unsigned comparison and an ashr, we can't simplify this. |
| 1347 | // Similarly for signed comparisons with lshr. |
| 1348 | if (ICI.isSigned() != (Shr->getOpcode() == Instruction::AShr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1349 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1350 | |
Eli Friedman | 865866e | 2011-05-25 23:26:20 +0000 | [diff] [blame] | 1351 | // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv |
| 1352 | // by a power of 2. Since we already have logic to simplify these, |
| 1353 | // transform to div and then simplify the resultant comparison. |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1354 | if (Shr->getOpcode() == Instruction::AShr && |
Eli Friedman | 865866e | 2011-05-25 23:26:20 +0000 | [diff] [blame] | 1355 | (!Shr->isExact() || ShAmtVal == TypeBits - 1)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1356 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1358 | // Revisit the shift (to delete it). |
| 1359 | Worklist.Add(Shr); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1361 | Constant *DivCst = |
| 1362 | ConstantInt::get(Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1364 | Value *Tmp = |
| 1365 | Shr->getOpcode() == Instruction::AShr ? |
| 1366 | Builder->CreateSDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()) : |
| 1367 | Builder->CreateUDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1368 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1369 | ICI.setOperand(0, Tmp); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1370 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1371 | // If the builder folded the binop, just return it. |
| 1372 | BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1373 | if (!TheDiv) |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1374 | return &ICI; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1376 | // Otherwise, fold this div/compare. |
| 1377 | assert(TheDiv->getOpcode() == Instruction::SDiv || |
| 1378 | TheDiv->getOpcode() == Instruction::UDiv); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 1380 | Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst)); |
| 1381 | assert(Res && "This div/cst should have folded!"); |
| 1382 | return Res; |
| 1383 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1385 | // If we are comparing against bits always shifted out, the |
| 1386 | // comparison cannot succeed. |
| 1387 | APInt Comp = CmpRHSV << ShAmtVal; |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1388 | ConstantInt *ShiftedCmpRHS = Builder->getInt(Comp); |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1389 | if (Shr->getOpcode() == Instruction::LShr) |
| 1390 | Comp = Comp.lshr(ShAmtVal); |
| 1391 | else |
| 1392 | Comp = Comp.ashr(ShAmtVal); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1394 | if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero. |
| 1395 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1396 | Constant *Cst = Builder->getInt1(IsICMP_NE); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1397 | return replaceInstUsesWith(ICI, Cst); |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1398 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1399 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1400 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 1401 | // If so, we can compare against the unshifted value: |
| 1402 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Chris Lattner | 9bd7fdf | 2011-02-13 18:30:09 +0000 | [diff] [blame] | 1403 | if (Shr->hasOneUse() && Shr->isExact()) |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1404 | return new ICmpInst(ICI.getPredicate(), Shr->getOperand(0), ShiftedCmpRHS); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1405 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1406 | if (Shr->hasOneUse()) { |
| 1407 | // Otherwise strength reduce the shift into an and. |
| 1408 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1409 | Constant *Mask = Builder->getInt(Val); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1411 | Value *And = Builder->CreateAnd(Shr->getOperand(0), |
| 1412 | Mask, Shr->getName()+".mask"); |
| 1413 | return new ICmpInst(ICI.getPredicate(), And, ShiftedCmpRHS); |
| 1414 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1415 | return nullptr; |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1418 | /// FoldICmpCstShrCst - Handle "(icmp eq/ne (ashr/lshr const2, A), const1)" -> |
| 1419 | /// (icmp eq/ne A, Log2(const2/const1)) -> |
| 1420 | /// (icmp eq/ne A, Log2(const2) - Log2(const1)). |
| 1421 | Instruction *InstCombiner::FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A, |
| 1422 | ConstantInt *CI1, |
| 1423 | ConstantInt *CI2) { |
| 1424 | assert(I.isEquality() && "Cannot fold icmp gt/lt"); |
| 1425 | |
| 1426 | auto getConstant = [&I, this](bool IsTrue) { |
| 1427 | if (I.getPredicate() == I.ICMP_NE) |
| 1428 | IsTrue = !IsTrue; |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1429 | return replaceInstUsesWith(I, ConstantInt::get(I.getType(), IsTrue)); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1430 | }; |
| 1431 | |
| 1432 | auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) { |
| 1433 | if (I.getPredicate() == I.ICMP_NE) |
| 1434 | Pred = CmpInst::getInversePredicate(Pred); |
| 1435 | return new ICmpInst(Pred, LHS, RHS); |
| 1436 | }; |
| 1437 | |
| 1438 | APInt AP1 = CI1->getValue(); |
| 1439 | APInt AP2 = CI2->getValue(); |
| 1440 | |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1441 | // Don't bother doing any work for cases which InstSimplify handles. |
| 1442 | if (AP2 == 0) |
| 1443 | return nullptr; |
| 1444 | bool IsAShr = isa<AShrOperator>(Op); |
| 1445 | if (IsAShr) { |
| 1446 | if (AP2.isAllOnesValue()) |
| 1447 | return nullptr; |
| 1448 | if (AP2.isNegative() != AP1.isNegative()) |
| 1449 | return nullptr; |
| 1450 | if (AP2.sgt(AP1)) |
| 1451 | return nullptr; |
| 1452 | } |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1453 | |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1454 | if (!AP1) |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1455 | // 'A' must be large enough to shift out the highest set bit. |
| 1456 | return getICmp(I.ICMP_UGT, A, |
| 1457 | ConstantInt::get(A->getType(), AP2.logBase2())); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1458 | |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1459 | if (AP1 == AP2) |
| 1460 | return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType())); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1461 | |
Andrea Di Biagio | 5b92b49 | 2014-09-17 11:32:31 +0000 | [diff] [blame] | 1462 | int Shift; |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1463 | if (IsAShr && AP1.isNegative()) |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1464 | Shift = AP1.countLeadingOnes() - AP2.countLeadingOnes(); |
Andrea Di Biagio | 5b92b49 | 2014-09-17 11:32:31 +0000 | [diff] [blame] | 1465 | else |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1466 | Shift = AP1.countLeadingZeros() - AP2.countLeadingZeros(); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1467 | |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1468 | if (Shift > 0) { |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1469 | if (IsAShr && AP1 == AP2.ashr(Shift)) { |
| 1470 | // There are multiple solutions if we are comparing against -1 and the LHS |
David Majnemer | 47ce0b8 | 2015-09-19 00:48:31 +0000 | [diff] [blame] | 1471 | // of the ashr is not a power of two. |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1472 | if (AP1.isAllOnesValue() && !AP2.isPowerOf2()) |
| 1473 | return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift)); |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1474 | return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1475 | } else if (AP1 == AP2.lshr(Shift)) { |
| 1476 | return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); |
| 1477 | } |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1478 | } |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1479 | // Shifting const2 will never be equal to const1. |
| 1480 | return getConstant(false); |
| 1481 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1482 | |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1483 | /// FoldICmpCstShlCst - Handle "(icmp eq/ne (shl const2, A), const1)" -> |
| 1484 | /// (icmp eq/ne A, TrailingZeros(const1) - TrailingZeros(const2)). |
| 1485 | Instruction *InstCombiner::FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A, |
| 1486 | ConstantInt *CI1, |
| 1487 | ConstantInt *CI2) { |
| 1488 | assert(I.isEquality() && "Cannot fold icmp gt/lt"); |
| 1489 | |
| 1490 | auto getConstant = [&I, this](bool IsTrue) { |
| 1491 | if (I.getPredicate() == I.ICMP_NE) |
| 1492 | IsTrue = !IsTrue; |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1493 | return replaceInstUsesWith(I, ConstantInt::get(I.getType(), IsTrue)); |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1494 | }; |
| 1495 | |
| 1496 | auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) { |
| 1497 | if (I.getPredicate() == I.ICMP_NE) |
| 1498 | Pred = CmpInst::getInversePredicate(Pred); |
| 1499 | return new ICmpInst(Pred, LHS, RHS); |
| 1500 | }; |
| 1501 | |
| 1502 | APInt AP1 = CI1->getValue(); |
| 1503 | APInt AP2 = CI2->getValue(); |
| 1504 | |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1505 | // Don't bother doing any work for cases which InstSimplify handles. |
| 1506 | if (AP2 == 0) |
| 1507 | return nullptr; |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1508 | |
| 1509 | unsigned AP2TrailingZeros = AP2.countTrailingZeros(); |
| 1510 | |
| 1511 | if (!AP1 && AP2TrailingZeros != 0) |
| 1512 | return getICmp(I.ICMP_UGE, A, |
| 1513 | ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros)); |
| 1514 | |
| 1515 | if (AP1 == AP2) |
| 1516 | return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType())); |
| 1517 | |
| 1518 | // Get the distance between the lowest bits that are set. |
| 1519 | int Shift = AP1.countTrailingZeros() - AP2TrailingZeros; |
| 1520 | |
| 1521 | if (Shift > 0 && AP2.shl(Shift) == AP1) |
| 1522 | return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); |
| 1523 | |
| 1524 | // Shifting const2 will never be equal to const1. |
| 1525 | return getConstant(false); |
| 1526 | } |
| 1527 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1528 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 1529 | /// |
| 1530 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 1531 | Instruction *LHSI, |
| 1532 | ConstantInt *RHS) { |
| 1533 | const APInt &RHSV = RHS->getValue(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1535 | switch (LHSI->getOpcode()) { |
| 1536 | case Instruction::Trunc: |
Sanjoy Das | e5f4889 | 2015-09-16 20:41:29 +0000 | [diff] [blame] | 1537 | if (RHS->isOne() && RHSV.getBitWidth() > 1) { |
| 1538 | // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1 |
| 1539 | Value *V = nullptr; |
| 1540 | if (ICI.getPredicate() == ICmpInst::ICMP_SLT && |
| 1541 | match(LHSI->getOperand(0), m_Signum(m_Value(V)))) |
| 1542 | return new ICmpInst(ICmpInst::ICMP_SLT, V, |
| 1543 | ConstantInt::get(V->getType(), 1)); |
| 1544 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1545 | if (ICI.isEquality() && LHSI->hasOneUse()) { |
| 1546 | // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all |
| 1547 | // of the high bits truncated out of x are known. |
| 1548 | unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(), |
| 1549 | SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1550 | APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1551 | computeKnownBits(LHSI->getOperand(0), KnownZero, KnownOne, 0, &ICI); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1553 | // If all the high bits are known, we can do this xform. |
| 1554 | if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) { |
| 1555 | // Pull in the high bits from known-ones set. |
Jay Foad | 583abbc | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 1556 | APInt NewRHS = RHS->getValue().zext(SrcBits); |
Eli Friedman | e0a64d8 | 2012-05-11 01:32:59 +0000 | [diff] [blame] | 1557 | NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1558 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1559 | Builder->getInt(NewRHS)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | break; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1563 | |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1564 | case Instruction::Xor: // (icmp pred (xor X, XorCst), CI) |
| 1565 | if (ConstantInt *XorCst = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1566 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 1567 | // fold the xor. |
| 1568 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 1569 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
| 1570 | Value *CompareVal = LHSI->getOperand(0); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1571 | |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1572 | // If the sign bit of the XorCst is not set, there is no change to |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1573 | // the operation, just stop using the Xor. |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1574 | if (!XorCst->isNegative()) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1575 | ICI.setOperand(0, CompareVal); |
| 1576 | Worklist.Add(LHSI); |
| 1577 | return &ICI; |
| 1578 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1580 | // Was the old condition true if the operand is positive? |
| 1581 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1582 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1583 | // If so, the new one isn't. |
| 1584 | isTrueIfPositive ^= true; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1586 | if (isTrueIfPositive) |
| 1587 | return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal, |
| 1588 | SubOne(RHS)); |
| 1589 | else |
| 1590 | return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal, |
| 1591 | AddOne(RHS)); |
| 1592 | } |
| 1593 | |
| 1594 | if (LHSI->hasOneUse()) { |
| 1595 | // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit)) |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1596 | if (!ICI.isEquality() && XorCst->getValue().isSignBit()) { |
| 1597 | const APInt &SignBit = XorCst->getValue(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1598 | ICmpInst::Predicate Pred = ICI.isSigned() |
| 1599 | ? ICI.getUnsignedPredicate() |
| 1600 | : ICI.getSignedPredicate(); |
| 1601 | return new ICmpInst(Pred, LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1602 | Builder->getInt(RHSV ^ SignBit)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A) |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1606 | if (!ICI.isEquality() && XorCst->isMaxValue(true)) { |
| 1607 | const APInt &NotSignBit = XorCst->getValue(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1608 | ICmpInst::Predicate Pred = ICI.isSigned() |
| 1609 | ? ICI.getUnsignedPredicate() |
| 1610 | : ICI.getSignedPredicate(); |
| 1611 | Pred = ICI.getSwappedPredicate(Pred); |
| 1612 | return new ICmpInst(Pred, LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1613 | Builder->getInt(RHSV ^ NotSignBit)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
David Majnemer | 72d7627 | 2013-07-09 09:20:58 +0000 | [diff] [blame] | 1616 | |
| 1617 | // (icmp ugt (xor X, C), ~C) -> (icmp ult X, C) |
| 1618 | // iff -C is a power of 2 |
| 1619 | if (ICI.getPredicate() == ICmpInst::ICMP_UGT && |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1620 | XorCst->getValue() == ~RHSV && (RHSV + 1).isPowerOf2()) |
| 1621 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), XorCst); |
David Majnemer | 72d7627 | 2013-07-09 09:20:58 +0000 | [diff] [blame] | 1622 | |
| 1623 | // (icmp ult (xor X, C), -C) -> (icmp uge X, C) |
| 1624 | // iff -C is a power of 2 |
| 1625 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1626 | XorCst->getValue() == -RHSV && RHSV.isPowerOf2()) |
| 1627 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), XorCst); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1628 | } |
| 1629 | break; |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1630 | case Instruction::And: // (icmp pred (and X, AndCst), RHS) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1631 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 1632 | LHSI->getOperand(0)->hasOneUse()) { |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1633 | ConstantInt *AndCst = cast<ConstantInt>(LHSI->getOperand(1)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1635 | // If the LHS is an AND of a truncating cast, we can widen the |
| 1636 | // and/compare to be the input width without changing the value |
| 1637 | // produced, eliminating a cast. |
| 1638 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 1639 | // We can do this transformation if either the AND constant does not |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1640 | // have its sign bit set or if it is an equality comparison. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1641 | // Extending a relational comparison when we're checking the sign |
| 1642 | // bit would not work. |
Benjamin Kramer | 35159c1 | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1643 | if (ICI.isEquality() || |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1644 | (!AndCst->isNegative() && RHSV.isNonNegative())) { |
Benjamin Kramer | 35159c1 | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1645 | Value *NewAnd = |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1646 | Builder->CreateAnd(Cast->getOperand(0), |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1647 | ConstantExpr::getZExt(AndCst, Cast->getSrcTy())); |
Benjamin Kramer | 35159c1 | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1648 | NewAnd->takeName(LHSI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1649 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
Benjamin Kramer | 35159c1 | 2011-06-12 22:47:53 +0000 | [diff] [blame] | 1650 | ConstantExpr::getZExt(RHS, Cast->getSrcTy())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1651 | } |
| 1652 | } |
Benjamin Kramer | 91f914c | 2011-06-12 22:48:00 +0000 | [diff] [blame] | 1653 | |
| 1654 | // If the LHS is an AND of a zext, and we have an equality compare, we can |
| 1655 | // shrink the and/compare to the smaller type, eliminating the cast. |
| 1656 | if (ZExtInst *Cast = dyn_cast<ZExtInst>(LHSI->getOperand(0))) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1657 | IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy()); |
Benjamin Kramer | 91f914c | 2011-06-12 22:48:00 +0000 | [diff] [blame] | 1658 | // Make sure we don't compare the upper bits, SimplifyDemandedBits |
| 1659 | // should fold the icmp to true/false in that case. |
| 1660 | if (ICI.isEquality() && RHSV.getActiveBits() <= Ty->getBitWidth()) { |
| 1661 | Value *NewAnd = |
| 1662 | Builder->CreateAnd(Cast->getOperand(0), |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1663 | ConstantExpr::getTrunc(AndCst, Ty)); |
Benjamin Kramer | 91f914c | 2011-06-12 22:48:00 +0000 | [diff] [blame] | 1664 | NewAnd->takeName(LHSI); |
| 1665 | return new ICmpInst(ICI.getPredicate(), NewAnd, |
| 1666 | ConstantExpr::getTrunc(RHS, Ty)); |
| 1667 | } |
| 1668 | } |
| 1669 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1670 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 1671 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 1672 | // happens a LOT in code produced by the C front-end, for bitfield |
| 1673 | // access. |
| 1674 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 1675 | if (Shift && !Shift->isShift()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1676 | Shift = nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1678 | ConstantInt *ShAmt; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1679 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1680 | |
Kay Tiong Khoo | a570b5a | 2013-12-19 18:07:17 +0000 | [diff] [blame] | 1681 | // This seemingly simple opportunity to fold away a shift turns out to |
| 1682 | // be rather complicated. See PR17827 |
| 1683 | // ( http://llvm.org/bugs/show_bug.cgi?id=17827 ) for details. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1684 | if (ShAmt) { |
Kay Tiong Khoo | 5389f74 | 2013-12-02 18:43:59 +0000 | [diff] [blame] | 1685 | bool CanFold = false; |
| 1686 | unsigned ShiftOpcode = Shift->getOpcode(); |
| 1687 | if (ShiftOpcode == Instruction::AShr) { |
Kay Tiong Khoo | a570b5a | 2013-12-19 18:07:17 +0000 | [diff] [blame] | 1688 | // There may be some constraints that make this possible, |
| 1689 | // but nothing simple has been discovered yet. |
| 1690 | CanFold = false; |
| 1691 | } else if (ShiftOpcode == Instruction::Shl) { |
| 1692 | // For a left shift, we can fold if the comparison is not signed. |
| 1693 | // We can also fold a signed comparison if the mask value and |
| 1694 | // comparison value are not negative. These constraints may not be |
| 1695 | // obvious, but we can prove that they are correct using an SMT |
Kay Tiong Khoo | e37d520 | 2013-12-19 18:35:54 +0000 | [diff] [blame] | 1696 | // solver. |
Kay Tiong Khoo | a570b5a | 2013-12-19 18:07:17 +0000 | [diff] [blame] | 1697 | if (!ICI.isSigned() || (!AndCst->isNegative() && !RHS->isNegative())) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1698 | CanFold = true; |
Kay Tiong Khoo | a570b5a | 2013-12-19 18:07:17 +0000 | [diff] [blame] | 1699 | } else if (ShiftOpcode == Instruction::LShr) { |
| 1700 | // For a logical right shift, we can fold if the comparison is not |
| 1701 | // signed. We can also fold a signed comparison if the shifted mask |
| 1702 | // value and the shifted comparison value are not negative. |
| 1703 | // These constraints may not be obvious, but we can prove that they |
Kay Tiong Khoo | e37d520 | 2013-12-19 18:35:54 +0000 | [diff] [blame] | 1704 | // are correct using an SMT solver. |
Kay Tiong Khoo | a570b5a | 2013-12-19 18:07:17 +0000 | [diff] [blame] | 1705 | if (!ICI.isSigned()) |
| 1706 | CanFold = true; |
| 1707 | else { |
| 1708 | ConstantInt *ShiftedAndCst = |
| 1709 | cast<ConstantInt>(ConstantExpr::getShl(AndCst, ShAmt)); |
| 1710 | ConstantInt *ShiftedRHSCst = |
| 1711 | cast<ConstantInt>(ConstantExpr::getShl(RHS, ShAmt)); |
| 1712 | |
| 1713 | if (!ShiftedAndCst->isNegative() && !ShiftedRHSCst->isNegative()) |
| 1714 | CanFold = true; |
| 1715 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1716 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1718 | if (CanFold) { |
| 1719 | Constant *NewCst; |
Kay Tiong Khoo | d7b00ca | 2013-12-02 22:23:32 +0000 | [diff] [blame] | 1720 | if (ShiftOpcode == Instruction::Shl) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1721 | NewCst = ConstantExpr::getLShr(RHS, ShAmt); |
| 1722 | else |
| 1723 | NewCst = ConstantExpr::getShl(RHS, ShAmt); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1725 | // Check to see if we are shifting out any of the bits being |
| 1726 | // compared. |
Kay Tiong Khoo | d7b00ca | 2013-12-02 22:23:32 +0000 | [diff] [blame] | 1727 | if (ConstantExpr::get(ShiftOpcode, NewCst, ShAmt) != RHS) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1728 | // If we shifted bits out, the fold is not going to work out. |
| 1729 | // As a special case, check to see if this means that the |
| 1730 | // result is always true or false now. |
| 1731 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1732 | return replaceInstUsesWith(ICI, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1733 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1734 | return replaceInstUsesWith(ICI, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1735 | } else { |
| 1736 | ICI.setOperand(1, NewCst); |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1737 | Constant *NewAndCst; |
Kay Tiong Khoo | d7b00ca | 2013-12-02 22:23:32 +0000 | [diff] [blame] | 1738 | if (ShiftOpcode == Instruction::Shl) |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1739 | NewAndCst = ConstantExpr::getLShr(AndCst, ShAmt); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1740 | else |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1741 | NewAndCst = ConstantExpr::getShl(AndCst, ShAmt); |
| 1742 | LHSI->setOperand(1, NewAndCst); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1743 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 1744 | Worklist.Add(Shift); // Shift is dead. |
| 1745 | return &ICI; |
| 1746 | } |
| 1747 | } |
| 1748 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1750 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 1751 | // preferable because it allows the C<<Y expression to be hoisted out |
| 1752 | // of a loop if Y is invariant and X is not. |
| 1753 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| 1754 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 1755 | !isa<Constant>(Shift->getOperand(0))) { |
| 1756 | // Compute C << Y. |
| 1757 | Value *NS; |
| 1758 | if (Shift->getOpcode() == Instruction::LShr) { |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1759 | NS = Builder->CreateShl(AndCst, Shift->getOperand(1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1760 | } else { |
| 1761 | // Insert a logical shift. |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1762 | NS = Builder->CreateLShr(AndCst, Shift->getOperand(1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1763 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1764 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1765 | // Compute X & (C << Y). |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1766 | Value *NewAnd = |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1767 | Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1768 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1769 | ICI.setOperand(0, NewAnd); |
| 1770 | return &ICI; |
| 1771 | } |
Paul Redmond | 5917f4c | 2012-12-19 19:47:13 +0000 | [diff] [blame] | 1772 | |
David Majnemer | 0ffccf7 | 2014-08-24 09:10:57 +0000 | [diff] [blame] | 1773 | // (icmp pred (and (or (lshr X, Y), X), 1), 0) --> |
| 1774 | // (icmp pred (and X, (or (shl 1, Y), 1), 0)) |
| 1775 | // |
| 1776 | // iff pred isn't signed |
| 1777 | { |
| 1778 | Value *X, *Y, *LShr; |
| 1779 | if (!ICI.isSigned() && RHSV == 0) { |
| 1780 | if (match(LHSI->getOperand(1), m_One())) { |
| 1781 | Constant *One = cast<Constant>(LHSI->getOperand(1)); |
| 1782 | Value *Or = LHSI->getOperand(0); |
| 1783 | if (match(Or, m_Or(m_Value(LShr), m_Value(X))) && |
| 1784 | match(LShr, m_LShr(m_Specific(X), m_Value(Y)))) { |
| 1785 | unsigned UsesRemoved = 0; |
| 1786 | if (LHSI->hasOneUse()) |
| 1787 | ++UsesRemoved; |
| 1788 | if (Or->hasOneUse()) |
| 1789 | ++UsesRemoved; |
| 1790 | if (LShr->hasOneUse()) |
| 1791 | ++UsesRemoved; |
| 1792 | Value *NewOr = nullptr; |
| 1793 | // Compute X & ((1 << Y) | 1) |
| 1794 | if (auto *C = dyn_cast<Constant>(Y)) { |
| 1795 | if (UsesRemoved >= 1) |
| 1796 | NewOr = |
| 1797 | ConstantExpr::getOr(ConstantExpr::getNUWShl(One, C), One); |
| 1798 | } else { |
| 1799 | if (UsesRemoved >= 3) |
| 1800 | NewOr = Builder->CreateOr(Builder->CreateShl(One, Y, |
| 1801 | LShr->getName(), |
| 1802 | /*HasNUW=*/true), |
| 1803 | One, Or->getName()); |
| 1804 | } |
| 1805 | if (NewOr) { |
| 1806 | Value *NewAnd = Builder->CreateAnd(X, NewOr, LHSI->getName()); |
| 1807 | ICI.setOperand(0, NewAnd); |
| 1808 | return &ICI; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1815 | // Replace ((X & AndCst) > RHSV) with ((X & AndCst) != 0), if any |
| 1816 | // bit set in (X & AndCst) will produce a result greater than RHSV. |
Paul Redmond | 5917f4c | 2012-12-19 19:47:13 +0000 | [diff] [blame] | 1817 | if (ICI.getPredicate() == ICmpInst::ICMP_UGT) { |
Kay Tiong Khoo | 564560f | 2013-12-02 22:11:56 +0000 | [diff] [blame] | 1818 | unsigned NTZ = AndCst->getValue().countTrailingZeros(); |
| 1819 | if ((NTZ < AndCst->getBitWidth()) && |
| 1820 | APInt::getOneBitSet(AndCst->getBitWidth(), NTZ).ugt(RHSV)) |
Paul Redmond | 5917f4c | 2012-12-19 19:47:13 +0000 | [diff] [blame] | 1821 | return new ICmpInst(ICmpInst::ICMP_NE, LHSI, |
| 1822 | Constant::getNullValue(RHS->getType())); |
| 1823 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1824 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1825 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1826 | // Try to optimize things like "A[i]&42 == 0" to index computations. |
| 1827 | if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) { |
| 1828 | if (GetElementPtrInst *GEP = |
| 1829 | dyn_cast<GetElementPtrInst>(LI->getOperand(0))) |
| 1830 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 1831 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 1832 | !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) { |
| 1833 | ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1)); |
| 1834 | if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C)) |
| 1835 | return Res; |
| 1836 | } |
| 1837 | } |
David Majnemer | 414d4e5 | 2013-07-09 08:09:32 +0000 | [diff] [blame] | 1838 | |
| 1839 | // X & -C == -C -> X > u ~C |
| 1840 | // X & -C != -C -> X <= u ~C |
| 1841 | // iff C is a power of 2 |
| 1842 | if (ICI.isEquality() && RHS == LHSI->getOperand(1) && (-RHSV).isPowerOf2()) |
| 1843 | return new ICmpInst( |
| 1844 | ICI.getPredicate() == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT |
| 1845 | : ICmpInst::ICMP_ULE, |
| 1846 | LHSI->getOperand(0), SubOne(RHS)); |
David Majnemer | dfa3b09 | 2015-08-16 07:09:17 +0000 | [diff] [blame] | 1847 | |
| 1848 | // (icmp eq (and %A, C), 0) -> (icmp sgt (trunc %A), -1) |
| 1849 | // iff C is a power of 2 |
| 1850 | if (ICI.isEquality() && LHSI->hasOneUse() && match(RHS, m_Zero())) { |
| 1851 | if (auto *CI = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 1852 | const APInt &AI = CI->getValue(); |
| 1853 | int32_t ExactLogBase2 = AI.exactLogBase2(); |
| 1854 | if (ExactLogBase2 != -1 && DL.isLegalInteger(ExactLogBase2 + 1)) { |
| 1855 | Type *NTy = IntegerType::get(ICI.getContext(), ExactLogBase2 + 1); |
| 1856 | Value *Trunc = Builder->CreateTrunc(LHSI->getOperand(0), NTy); |
| 1857 | return new ICmpInst(ICI.getPredicate() == ICmpInst::ICMP_EQ |
| 1858 | ? ICmpInst::ICMP_SGE |
| 1859 | : ICmpInst::ICMP_SLT, |
| 1860 | Trunc, Constant::getNullValue(NTy)); |
| 1861 | } |
| 1862 | } |
| 1863 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1864 | break; |
| 1865 | |
| 1866 | case Instruction::Or: { |
Sanjoy Das | e5f4889 | 2015-09-16 20:41:29 +0000 | [diff] [blame] | 1867 | if (RHS->isOne()) { |
| 1868 | // icmp slt signum(V) 1 --> icmp slt V, 1 |
| 1869 | Value *V = nullptr; |
| 1870 | if (ICI.getPredicate() == ICmpInst::ICMP_SLT && |
| 1871 | match(LHSI, m_Signum(m_Value(V)))) |
| 1872 | return new ICmpInst(ICmpInst::ICMP_SLT, V, |
| 1873 | ConstantInt::get(V->getType(), 1)); |
| 1874 | } |
| 1875 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1876 | if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse()) |
| 1877 | break; |
| 1878 | Value *P, *Q; |
| 1879 | if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) { |
| 1880 | // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0 |
| 1881 | // -> and (icmp eq P, null), (icmp eq Q, null). |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1882 | Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P, |
| 1883 | Constant::getNullValue(P->getType())); |
| 1884 | Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q, |
| 1885 | Constant::getNullValue(Q->getType())); |
| 1886 | Instruction *Op; |
| 1887 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| 1888 | Op = BinaryOperator::CreateAnd(ICIP, ICIQ); |
| 1889 | else |
| 1890 | Op = BinaryOperator::CreateOr(ICIP, ICIQ); |
| 1891 | return Op; |
| 1892 | } |
| 1893 | break; |
| 1894 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1895 | |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 1896 | case Instruction::Mul: { // (icmp pred (mul X, Val), CI) |
| 1897 | ConstantInt *Val = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 1898 | if (!Val) break; |
| 1899 | |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 1900 | // If this is a signed comparison to 0 and the mul is sign preserving, |
| 1901 | // use the mul LHS operand instead. |
| 1902 | ICmpInst::Predicate pred = ICI.getPredicate(); |
| 1903 | if (isSignTest(pred, RHS) && !Val->isZero() && |
| 1904 | cast<BinaryOperator>(LHSI)->hasNoSignedWrap()) |
| 1905 | return new ICmpInst(Val->isNegative() ? |
| 1906 | ICmpInst::getSwappedPredicate(pred) : pred, |
| 1907 | LHSI->getOperand(0), |
| 1908 | Constant::getNullValue(RHS->getType())); |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 1909 | |
| 1910 | break; |
| 1911 | } |
| 1912 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1913 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1914 | uint32_t TypeBits = RHSV.getBitWidth(); |
David Majnemer | b889e40 | 2013-06-28 23:42:03 +0000 | [diff] [blame] | 1915 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 1916 | if (!ShAmt) { |
| 1917 | Value *X; |
| 1918 | // (1 << X) pred P2 -> X pred Log2(P2) |
| 1919 | if (match(LHSI, m_Shl(m_One(), m_Value(X)))) { |
| 1920 | bool RHSVIsPowerOf2 = RHSV.isPowerOf2(); |
| 1921 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
| 1922 | if (ICI.isUnsigned()) { |
| 1923 | if (!RHSVIsPowerOf2) { |
| 1924 | // (1 << X) < 30 -> X <= 4 |
| 1925 | // (1 << X) <= 30 -> X <= 4 |
| 1926 | // (1 << X) >= 30 -> X > 4 |
| 1927 | // (1 << X) > 30 -> X > 4 |
| 1928 | if (Pred == ICmpInst::ICMP_ULT) |
| 1929 | Pred = ICmpInst::ICMP_ULE; |
| 1930 | else if (Pred == ICmpInst::ICMP_UGE) |
| 1931 | Pred = ICmpInst::ICMP_UGT; |
| 1932 | } |
| 1933 | unsigned RHSLog2 = RHSV.logBase2(); |
| 1934 | |
| 1935 | // (1 << X) >= 2147483648 -> X >= 31 -> X == 31 |
David Majnemer | b889e40 | 2013-06-28 23:42:03 +0000 | [diff] [blame] | 1936 | // (1 << X) < 2147483648 -> X < 31 -> X != 31 |
| 1937 | if (RHSLog2 == TypeBits-1) { |
| 1938 | if (Pred == ICmpInst::ICMP_UGE) |
| 1939 | Pred = ICmpInst::ICMP_EQ; |
David Majnemer | b889e40 | 2013-06-28 23:42:03 +0000 | [diff] [blame] | 1940 | else if (Pred == ICmpInst::ICMP_ULT) |
| 1941 | Pred = ICmpInst::ICMP_NE; |
| 1942 | } |
| 1943 | |
| 1944 | return new ICmpInst(Pred, X, |
| 1945 | ConstantInt::get(RHS->getType(), RHSLog2)); |
| 1946 | } else if (ICI.isSigned()) { |
| 1947 | if (RHSV.isAllOnesValue()) { |
| 1948 | // (1 << X) <= -1 -> X == 31 |
| 1949 | if (Pred == ICmpInst::ICMP_SLE) |
| 1950 | return new ICmpInst(ICmpInst::ICMP_EQ, X, |
| 1951 | ConstantInt::get(RHS->getType(), TypeBits-1)); |
| 1952 | |
| 1953 | // (1 << X) > -1 -> X != 31 |
| 1954 | if (Pred == ICmpInst::ICMP_SGT) |
| 1955 | return new ICmpInst(ICmpInst::ICMP_NE, X, |
| 1956 | ConstantInt::get(RHS->getType(), TypeBits-1)); |
| 1957 | } else if (!RHSV) { |
| 1958 | // (1 << X) < 0 -> X == 31 |
| 1959 | // (1 << X) <= 0 -> X == 31 |
| 1960 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
| 1961 | return new ICmpInst(ICmpInst::ICMP_EQ, X, |
| 1962 | ConstantInt::get(RHS->getType(), TypeBits-1)); |
| 1963 | |
| 1964 | // (1 << X) >= 0 -> X != 31 |
| 1965 | // (1 << X) > 0 -> X != 31 |
| 1966 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) |
| 1967 | return new ICmpInst(ICmpInst::ICMP_NE, X, |
| 1968 | ConstantInt::get(RHS->getType(), TypeBits-1)); |
| 1969 | } |
| 1970 | } else if (ICI.isEquality()) { |
| 1971 | if (RHSVIsPowerOf2) |
| 1972 | return new ICmpInst( |
| 1973 | Pred, X, ConstantInt::get(RHS->getType(), RHSV.logBase2())); |
David Majnemer | b889e40 | 2013-06-28 23:42:03 +0000 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | break; |
| 1977 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1979 | // Check that the shift amount is in range. If not, don't perform |
| 1980 | // undefined shifts. When the shift is visited it will be |
| 1981 | // simplified. |
| 1982 | if (ShAmt->uge(TypeBits)) |
| 1983 | break; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1984 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1985 | if (ICI.isEquality()) { |
| 1986 | // If we are comparing against bits always shifted out, the |
| 1987 | // comparison cannot succeed. |
| 1988 | Constant *Comp = |
| 1989 | ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt), |
| 1990 | ShAmt); |
| 1991 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 1992 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 1993 | Constant *Cst = Builder->getInt1(IsICMP_NE); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1994 | return replaceInstUsesWith(ICI, Cst); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1995 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 1997 | // If the shift is NUW, then it is just shifting out zeros, no need for an |
| 1998 | // AND. |
| 1999 | if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap()) |
| 2000 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 2001 | ConstantExpr::getLShr(RHS, ShAmt)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2002 | |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 2003 | // If the shift is NSW and we compare to 0, then it is just shifting out |
| 2004 | // sign bits, no need for an AND either. |
| 2005 | if (cast<BinaryOperator>(LHSI)->hasNoSignedWrap() && RHSV == 0) |
| 2006 | return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0), |
| 2007 | ConstantExpr::getLShr(RHS, ShAmt)); |
| 2008 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2009 | if (LHSI->hasOneUse()) { |
| 2010 | // Otherwise strength reduce the shift into an and. |
| 2011 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 2012 | Constant *Mask = Builder->getInt(APInt::getLowBitsSet(TypeBits, |
| 2013 | TypeBits - ShAmtVal)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2014 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2015 | Value *And = |
| 2016 | Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask"); |
| 2017 | return new ICmpInst(ICI.getPredicate(), And, |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 2018 | ConstantExpr::getLShr(RHS, ShAmt)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2019 | } |
| 2020 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2021 | |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 2022 | // If this is a signed comparison to 0 and the shift is sign preserving, |
| 2023 | // use the shift LHS operand instead. |
| 2024 | ICmpInst::Predicate pred = ICI.getPredicate(); |
| 2025 | if (isSignTest(pred, RHS) && |
| 2026 | cast<BinaryOperator>(LHSI)->hasNoSignedWrap()) |
| 2027 | return new ICmpInst(pred, |
| 2028 | LHSI->getOperand(0), |
| 2029 | Constant::getNullValue(RHS->getType())); |
| 2030 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2031 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 2032 | bool TrueIfSigned = false; |
| 2033 | if (LHSI->hasOneUse() && |
| 2034 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 2035 | // (X << 31) <s 0 --> (X&1) != 0 |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 2036 | Constant *Mask = ConstantInt::get(LHSI->getOperand(0)->getType(), |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2037 | APInt::getOneBitSet(TypeBits, |
Chris Lattner | 43273af | 2011-02-13 08:07:21 +0000 | [diff] [blame] | 2038 | TypeBits-ShAmt->getZExtValue()-1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2039 | Value *And = |
| 2040 | Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask"); |
| 2041 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| 2042 | And, Constant::getNullValue(And->getType())); |
| 2043 | } |
Arnaud A. de Grandmaison | 61c167c | 2013-02-15 14:35:47 +0000 | [diff] [blame] | 2044 | |
| 2045 | // Transform (icmp pred iM (shl iM %v, N), CI) |
Arnaud A. de Grandmaison | 7153305 | 2013-03-13 14:40:37 +0000 | [diff] [blame] | 2046 | // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N)) |
| 2047 | // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N. |
Arnaud A. de Grandmaison | 61c167c | 2013-02-15 14:35:47 +0000 | [diff] [blame] | 2048 | // This enables to get rid of the shift in favor of a trunc which can be |
| 2049 | // free on the target. It has the additional benefit of comparing to a |
| 2050 | // smaller constant, which will be target friendly. |
| 2051 | unsigned Amt = ShAmt->getLimitedValue(TypeBits-1); |
Arnaud A. de Grandmaison | 7153305 | 2013-03-13 14:40:37 +0000 | [diff] [blame] | 2052 | if (LHSI->hasOneUse() && |
| 2053 | Amt != 0 && RHSV.countTrailingZeros() >= Amt) { |
Arnaud A. de Grandmaison | 61c167c | 2013-02-15 14:35:47 +0000 | [diff] [blame] | 2054 | Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt); |
| 2055 | Constant *NCI = ConstantExpr::getTrunc( |
| 2056 | ConstantExpr::getAShr(RHS, |
| 2057 | ConstantInt::get(RHS->getType(), Amt)), |
| 2058 | NTy); |
| 2059 | return new ICmpInst(ICI.getPredicate(), |
| 2060 | Builder->CreateTrunc(LHSI->getOperand(0), NTy), |
Arnaud A. de Grandmaison | 1fd843e | 2013-02-15 15:18:17 +0000 | [diff] [blame] | 2061 | NCI); |
Arnaud A. de Grandmaison | 61c167c | 2013-02-15 14:35:47 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2064 | break; |
| 2065 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2066 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2067 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
Nick Lewycky | 174a705 | 2011-02-28 08:31:40 +0000 | [diff] [blame] | 2068 | case Instruction::AShr: { |
| 2069 | // Handle equality comparisons of shift-by-constant. |
| 2070 | BinaryOperator *BO = cast<BinaryOperator>(LHSI); |
| 2071 | if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 2072 | if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt)) |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 2073 | return Res; |
Nick Lewycky | 174a705 | 2011-02-28 08:31:40 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | // Handle exact shr's. |
| 2077 | if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) { |
| 2078 | if (RHSV.isMinValue()) |
| 2079 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS); |
| 2080 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2081 | break; |
Nick Lewycky | 174a705 | 2011-02-28 08:31:40 +0000 | [diff] [blame] | 2082 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2083 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2084 | case Instruction::SDiv: |
| 2085 | case Instruction::UDiv: |
| 2086 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2087 | // Fold this div into the comparison, producing a range check. |
| 2088 | // Determine, based on the divide type, what the range is being |
| 2089 | // checked. If there is an overflow on the low or high side, remember |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2090 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2091 | // See: InsertRangeTest above for the kinds of replacements possible. |
| 2092 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 2093 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 2094 | DivRHS)) |
| 2095 | return R; |
| 2096 | break; |
| 2097 | |
David Majnemer | f2a9a51 | 2013-07-09 07:50:59 +0000 | [diff] [blame] | 2098 | case Instruction::Sub: { |
| 2099 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(0)); |
| 2100 | if (!LHSC) break; |
| 2101 | const APInt &LHSV = LHSC->getValue(); |
| 2102 | |
| 2103 | // C1-X <u C2 -> (X|(C2-1)) == C1 |
| 2104 | // iff C1 & (C2-1) == C2-1 |
| 2105 | // C2 is a power of 2 |
| 2106 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() && |
| 2107 | RHSV.isPowerOf2() && (LHSV & (RHSV - 1)) == (RHSV - 1)) |
| 2108 | return new ICmpInst(ICmpInst::ICMP_EQ, |
| 2109 | Builder->CreateOr(LHSI->getOperand(1), RHSV - 1), |
| 2110 | LHSC); |
| 2111 | |
David Majnemer | eeed73b | 2013-07-09 09:24:35 +0000 | [diff] [blame] | 2112 | // C1-X >u C2 -> (X|C2) != C1 |
David Majnemer | f2a9a51 | 2013-07-09 07:50:59 +0000 | [diff] [blame] | 2113 | // iff C1 & C2 == C2 |
| 2114 | // C2+1 is a power of 2 |
| 2115 | if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() && |
| 2116 | (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == RHSV) |
| 2117 | return new ICmpInst(ICmpInst::ICMP_NE, |
| 2118 | Builder->CreateOr(LHSI->getOperand(1), RHSV), LHSC); |
| 2119 | break; |
| 2120 | } |
| 2121 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2122 | case Instruction::Add: |
| 2123 | // Fold: icmp pred (add X, C1), C2 |
| 2124 | if (!ICI.isEquality()) { |
| 2125 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 2126 | if (!LHSC) break; |
| 2127 | const APInt &LHSV = LHSC->getValue(); |
| 2128 | |
| 2129 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 2130 | .subtract(LHSV); |
| 2131 | |
| 2132 | if (ICI.isSigned()) { |
| 2133 | if (CR.getLower().isSignBit()) { |
| 2134 | return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 2135 | Builder->getInt(CR.getUpper())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2136 | } else if (CR.getUpper().isSignBit()) { |
| 2137 | return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 2138 | Builder->getInt(CR.getLower())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2139 | } |
| 2140 | } else { |
| 2141 | if (CR.getLower().isMinValue()) { |
| 2142 | return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 2143 | Builder->getInt(CR.getUpper())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2144 | } else if (CR.getUpper().isMinValue()) { |
| 2145 | return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 2146 | Builder->getInt(CR.getLower())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2147 | } |
| 2148 | } |
David Majnemer | fa90a0b | 2013-07-08 11:53:08 +0000 | [diff] [blame] | 2149 | |
David Majnemer | bafa537 | 2013-07-09 07:58:32 +0000 | [diff] [blame] | 2150 | // X-C1 <u C2 -> (X & -C2) == C1 |
| 2151 | // iff C1 & (C2-1) == 0 |
| 2152 | // C2 is a power of 2 |
David Majnemer | fa90a0b | 2013-07-08 11:53:08 +0000 | [diff] [blame] | 2153 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() && |
David Majnemer | bafa537 | 2013-07-09 07:58:32 +0000 | [diff] [blame] | 2154 | RHSV.isPowerOf2() && (LHSV & (RHSV - 1)) == 0) |
David Majnemer | fa90a0b | 2013-07-08 11:53:08 +0000 | [diff] [blame] | 2155 | return new ICmpInst(ICmpInst::ICMP_EQ, |
| 2156 | Builder->CreateAnd(LHSI->getOperand(0), -RHSV), |
| 2157 | ConstantExpr::getNeg(LHSC)); |
David Majnemer | bafa537 | 2013-07-09 07:58:32 +0000 | [diff] [blame] | 2158 | |
David Majnemer | eeed73b | 2013-07-09 09:24:35 +0000 | [diff] [blame] | 2159 | // X-C1 >u C2 -> (X & ~C2) != C1 |
David Majnemer | bafa537 | 2013-07-09 07:58:32 +0000 | [diff] [blame] | 2160 | // iff C1 & C2 == 0 |
| 2161 | // C2+1 is a power of 2 |
| 2162 | if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() && |
| 2163 | (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == 0) |
| 2164 | return new ICmpInst(ICmpInst::ICMP_NE, |
| 2165 | Builder->CreateAnd(LHSI->getOperand(0), ~RHSV), |
| 2166 | ConstantExpr::getNeg(LHSC)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2167 | } |
| 2168 | break; |
| 2169 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2170 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2171 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 2172 | if (ICI.isEquality()) { |
| 2173 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2174 | |
| 2175 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2176 | // the second operand is a constant, simplify a bit. |
| 2177 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 2178 | switch (BO->getOpcode()) { |
| 2179 | case Instruction::SRem: |
| 2180 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 2181 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 2182 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
Dan Gohman | 4ce1fb1 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 2183 | if (V.sgt(1) && V.isPowerOf2()) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2184 | Value *NewRem = |
| 2185 | Builder->CreateURem(BO->getOperand(0), BO->getOperand(1), |
| 2186 | BO->getName()); |
| 2187 | return new ICmpInst(ICI.getPredicate(), NewRem, |
| 2188 | Constant::getNullValue(BO->getType())); |
| 2189 | } |
| 2190 | } |
| 2191 | break; |
| 2192 | case Instruction::Add: |
| 2193 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 2194 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 2195 | if (BO->hasOneUse()) |
| 2196 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 2197 | ConstantExpr::getSub(RHS, BOp1C)); |
| 2198 | } else if (RHSV == 0) { |
| 2199 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 2200 | // efficiently invertible, or if the add has just this one use. |
| 2201 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2202 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2203 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
| 2204 | return new ICmpInst(ICI.getPredicate(), BOp0, NegVal); |
Chris Lattner | 31b106d | 2011-04-26 20:02:45 +0000 | [diff] [blame] | 2205 | if (Value *NegVal = dyn_castNegVal(BOp0)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2206 | return new ICmpInst(ICI.getPredicate(), NegVal, BOp1); |
Chris Lattner | 31b106d | 2011-04-26 20:02:45 +0000 | [diff] [blame] | 2207 | if (BO->hasOneUse()) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2208 | Value *Neg = Builder->CreateNeg(BOp1); |
| 2209 | Neg->takeName(BO); |
| 2210 | return new ICmpInst(ICI.getPredicate(), BOp0, Neg); |
| 2211 | } |
| 2212 | } |
| 2213 | break; |
| 2214 | case Instruction::Xor: |
David Majnemer | 0f0abc7 | 2016-02-12 18:12:38 +0000 | [diff] [blame] | 2215 | if (BO->hasOneUse()) { |
| 2216 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| 2217 | // For the xor case, we can xor two constants together, eliminating |
| 2218 | // the explicit xor. |
| 2219 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 2220 | ConstantExpr::getXor(RHS, BOC)); |
| 2221 | } else if (RHSV == 0) { |
| 2222 | // Replace ((xor A, B) != 0) with (A != B) |
| 2223 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 2224 | BO->getOperand(1)); |
| 2225 | } |
Benjamin Kramer | c970849 | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 2226 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2227 | break; |
Benjamin Kramer | c970849 | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 2228 | case Instruction::Sub: |
David Majnemer | 0f0abc7 | 2016-02-12 18:12:38 +0000 | [diff] [blame] | 2229 | if (BO->hasOneUse()) { |
| 2230 | if (ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) { |
| 2231 | // Replace ((sub A, B) != C) with (B != A-C) if A & C are constants. |
Benjamin Kramer | c970849 | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 2232 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(1), |
David Majnemer | 0f0abc7 | 2016-02-12 18:12:38 +0000 | [diff] [blame] | 2233 | ConstantExpr::getSub(BOp0C, RHS)); |
| 2234 | } else if (RHSV == 0) { |
| 2235 | // Replace ((sub A, B) != 0) with (A != B) |
| 2236 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 2237 | BO->getOperand(1)); |
| 2238 | } |
Benjamin Kramer | c970849 | 2011-06-13 15:24:24 +0000 | [diff] [blame] | 2239 | } |
| 2240 | break; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2241 | case Instruction::Or: |
| 2242 | // If bits are being or'd in that are not present in the constant we |
| 2243 | // are comparing against, then the comparison could never succeed! |
Eli Friedman | 0428a61 | 2010-07-29 18:03:33 +0000 | [diff] [blame] | 2244 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2245 | Constant *NotCI = ConstantExpr::getNot(RHS); |
| 2246 | if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2247 | return replaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2248 | } |
| 2249 | break; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2250 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2251 | case Instruction::And: |
| 2252 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 2253 | // If bits are being compared against that are and'd out, then the |
| 2254 | // comparison can never succeed! |
| 2255 | if ((RHSV & ~BOC->getValue()) != 0) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2256 | return replaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2257 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2258 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 2259 | if (RHS == BOC && RHSV.isPowerOf2()) |
| 2260 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : |
| 2261 | ICmpInst::ICMP_NE, LHSI, |
| 2262 | Constant::getNullValue(RHS->getType())); |
Benjamin Kramer | 9eca5fe | 2011-07-04 20:16:36 +0000 | [diff] [blame] | 2263 | |
| 2264 | // Don't perform the following transforms if the AND has multiple uses |
| 2265 | if (!BO->hasOneUse()) |
| 2266 | break; |
| 2267 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2268 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| 2269 | if (BOC->getValue().isSignBit()) { |
| 2270 | Value *X = BO->getOperand(0); |
| 2271 | Constant *Zero = Constant::getNullValue(X->getType()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2272 | ICmpInst::Predicate pred = isICMP_NE ? |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2273 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 2274 | return new ICmpInst(pred, X, Zero); |
| 2275 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2276 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2277 | // ((X & ~7) == 0) --> X < 8 |
| 2278 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 2279 | Value *X = BO->getOperand(0); |
| 2280 | Constant *NegX = ConstantExpr::getNeg(BOC); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2281 | ICmpInst::Predicate pred = isICMP_NE ? |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2282 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 2283 | return new ICmpInst(pred, X, NegX); |
| 2284 | } |
| 2285 | } |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 2286 | break; |
| 2287 | case Instruction::Mul: |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 2288 | if (RHSV == 0 && BO->hasNoSignedWrap()) { |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 2289 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 2290 | // The trivial case (mul X, 0) is handled by InstSimplify |
| 2291 | // General case : (mul X, C) != 0 iff X != 0 |
| 2292 | // (mul X, C) == 0 iff X == 0 |
| 2293 | if (!BOC->isZero()) |
| 2294 | return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), |
| 2295 | Constant::getNullValue(RHS->getType())); |
| 2296 | } |
| 2297 | } |
| 2298 | break; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2299 | default: break; |
| 2300 | } |
| 2301 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 2302 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
Chris Lattner | 54f4e39 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 2303 | switch (II->getIntrinsicID()) { |
| 2304 | case Intrinsic::bswap: |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2305 | Worklist.Add(II); |
Gabor Greif | 7ccec09 | 2010-06-24 16:11:44 +0000 | [diff] [blame] | 2306 | ICI.setOperand(0, II->getArgOperand(0)); |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 2307 | ICI.setOperand(1, Builder->getInt(RHSV.byteSwap())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2308 | return &ICI; |
Chris Lattner | 54f4e39 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 2309 | case Intrinsic::ctlz: |
| 2310 | case Intrinsic::cttz: |
| 2311 | // ctz(A) == bitwidth(a) -> A == 0 and likewise for != |
| 2312 | if (RHSV == RHS->getType()->getBitWidth()) { |
| 2313 | Worklist.Add(II); |
Gabor Greif | 7ccec09 | 2010-06-24 16:11:44 +0000 | [diff] [blame] | 2314 | ICI.setOperand(0, II->getArgOperand(0)); |
Chris Lattner | 54f4e39 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 2315 | ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0)); |
| 2316 | return &ICI; |
| 2317 | } |
| 2318 | break; |
| 2319 | case Intrinsic::ctpop: |
| 2320 | // popcount(A) == 0 -> A == 0 and likewise for != |
| 2321 | if (RHS->isZero()) { |
| 2322 | Worklist.Add(II); |
Gabor Greif | 7ccec09 | 2010-06-24 16:11:44 +0000 | [diff] [blame] | 2323 | ICI.setOperand(0, II->getArgOperand(0)); |
Chris Lattner | 54f4e39 | 2010-01-05 18:09:56 +0000 | [diff] [blame] | 2324 | ICI.setOperand(1, RHS); |
| 2325 | return &ICI; |
| 2326 | } |
| 2327 | break; |
| 2328 | default: |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 2329 | break; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2330 | } |
| 2331 | } |
| 2332 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2333 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 2337 | /// We only handle extending casts so far. |
| 2338 | /// |
| 2339 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 2340 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
| 2341 | Value *LHSCIOp = LHSCI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2342 | Type *SrcTy = LHSCIOp->getType(); |
| 2343 | Type *DestTy = LHSCI->getType(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2344 | Value *RHSCIOp; |
| 2345 | |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2346 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2347 | // integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2348 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 2349 | DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2350 | Value *RHSOp = nullptr; |
Michael Liao | d266b92 | 2015-02-13 04:51:26 +0000 | [diff] [blame] | 2351 | if (PtrToIntOperator *RHSC = dyn_cast<PtrToIntOperator>(ICI.getOperand(1))) { |
| 2352 | Value *RHSCIOp = RHSC->getOperand(0); |
| 2353 | if (RHSCIOp->getType()->getPointerAddressSpace() == |
| 2354 | LHSCIOp->getType()->getPointerAddressSpace()) { |
| 2355 | RHSOp = RHSC->getOperand(0); |
| 2356 | // If the pointer types don't match, insert a bitcast. |
| 2357 | if (LHSCIOp->getType() != RHSOp->getType()) |
| 2358 | RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType()); |
| 2359 | } |
| 2360 | } else if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2361 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (RHSOp) |
| 2364 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp); |
| 2365 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2366 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2367 | // The code below only handles extension cast instructions, so far. |
| 2368 | // Enforce this. |
| 2369 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 2370 | LHSCI->getOpcode() != Instruction::SExt) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2371 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2372 | |
| 2373 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 2374 | bool isSignedCmp = ICI.isSigned(); |
| 2375 | |
| 2376 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
| 2377 | // Not an extension from the same type? |
| 2378 | RHSCIOp = CI->getOperand(0); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2379 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2380 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2382 | // If the signedness of the two casts doesn't agree (i.e. one is a sext |
| 2383 | // and the other is a zext), then we can't handle this. |
| 2384 | if (CI->getOpcode() != LHSCI->getOpcode()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2385 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2386 | |
| 2387 | // Deal with equality cases early. |
| 2388 | if (ICI.isEquality()) |
| 2389 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 2390 | |
| 2391 | // A signed comparison of sign extended values simplifies into a |
| 2392 | // signed comparison. |
| 2393 | if (isSignedCmp && isSignedExt) |
| 2394 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| 2395 | |
| 2396 | // The other three cases all fold into an unsigned comparison. |
| 2397 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
| 2398 | } |
| 2399 | |
| 2400 | // If we aren't dealing with a constant on the RHS, exit early |
| 2401 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 2402 | if (!CI) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2403 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Compute the constant that would happen if we truncated to SrcTy then |
| 2406 | // reextended to DestTy. |
| 2407 | Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy); |
| 2408 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), |
| 2409 | Res1, DestTy); |
| 2410 | |
| 2411 | // If the re-extended constant didn't change... |
| 2412 | if (Res2 == CI) { |
| 2413 | // Deal with equality cases early. |
| 2414 | if (ICI.isEquality()) |
| 2415 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 2416 | |
| 2417 | // A signed comparison of sign extended values simplifies into a |
| 2418 | // signed comparison. |
| 2419 | if (isSignedExt && isSignedCmp) |
| 2420 | return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1); |
| 2421 | |
| 2422 | // The other three cases all fold into an unsigned comparison. |
| 2423 | return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1); |
| 2424 | } |
| 2425 | |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2426 | // The re-extended constant changed so the constant cannot be represented |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2427 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2428 | // All the cases that fold to true or false will have already been handled |
| 2429 | // by SimplifyICmpInst, so only deal with the tricky case. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2430 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2431 | if (isSignedCmp || !isSignedExt) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2432 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 2435 | // should have been folded away previously and not enter in here. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2436 | |
| 2437 | // We're performing an unsigned comp with a sign extended value. |
| 2438 | // This is true if the input is >= 0. [aka >s -1] |
| 2439 | Constant *NegOne = Constant::getAllOnesValue(SrcTy); |
| 2440 | Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2441 | |
| 2442 | // Finally, return the value computed. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2443 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2444 | return replaceInstUsesWith(ICI, Result); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2445 | |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 2446 | assert(ICI.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!"); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2447 | return BinaryOperator::CreateNot(Result); |
| 2448 | } |
| 2449 | |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 2450 | /// ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form: |
| 2451 | /// I = icmp ugt (add (add A, B), CI2), CI1 |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2452 | /// If this is of the form: |
| 2453 | /// sum = a + b |
| 2454 | /// if (sum+128 >u 255) |
| 2455 | /// Then replace it with llvm.sadd.with.overflow.i8. |
| 2456 | /// |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 2457 | static Instruction *ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, |
| 2458 | ConstantInt *CI2, ConstantInt *CI1, |
Chris Lattner | ce2995a | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 2459 | InstCombiner &IC) { |
Chris Lattner | f29562d | 2010-12-19 17:59:02 +0000 | [diff] [blame] | 2460 | // The transformation we're trying to do here is to transform this into an |
| 2461 | // llvm.sadd.with.overflow. To do this, we have to replace the original add |
| 2462 | // with a narrower add, and discard the add-with-constant that is part of the |
| 2463 | // range check (if we can't eliminate it, this isn't profitable). |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | f29562d | 2010-12-19 17:59:02 +0000 | [diff] [blame] | 2465 | // In order to eliminate the add-with-constant, the compare can be its only |
| 2466 | // use. |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2467 | Instruction *AddWithCst = cast<Instruction>(I.getOperand(0)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2468 | if (!AddWithCst->hasOneUse()) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2470 | // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2471 | if (!CI2->getValue().isPowerOf2()) return nullptr; |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2472 | unsigned NewWidth = CI2->getValue().countTrailingZeros(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2473 | if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2474 | |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2475 | // The width of the new add formed is 1 more than the bias. |
| 2476 | ++NewWidth; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2477 | |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2478 | // Check to see that CI1 is an all-ones value with NewWidth bits. |
| 2479 | if (CI1->getBitWidth() == NewWidth || |
| 2480 | CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2481 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2482 | |
Eli Friedman | b3f9b06 | 2011-11-28 23:32:19 +0000 | [diff] [blame] | 2483 | // This is only really a signed overflow check if the inputs have been |
| 2484 | // sign-extended; check for that condition. For example, if CI2 is 2^31 and |
| 2485 | // the operands of the add are 64 bits wide, we need at least 33 sign bits. |
| 2486 | unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 2487 | if (IC.ComputeNumSignBits(A, 0, &I) < NeededSignBits || |
| 2488 | IC.ComputeNumSignBits(B, 0, &I) < NeededSignBits) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2489 | return nullptr; |
Eli Friedman | b3f9b06 | 2011-11-28 23:32:19 +0000 | [diff] [blame] | 2490 | |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2491 | // In order to replace the original add with a narrower |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2492 | // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant |
| 2493 | // and truncates that discard the high bits of the add. Verify that this is |
| 2494 | // the case. |
| 2495 | Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0)); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2496 | for (User *U : OrigAdd->users()) { |
| 2497 | if (U == AddWithCst) continue; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2498 | |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2499 | // Only accept truncates for now. We would really like a nice recursive |
| 2500 | // predicate like SimplifyDemandedBits, but which goes downwards the use-def |
| 2501 | // chain to see which bits of a value are actually demanded. If the |
| 2502 | // original add had another add which was then immediately truncated, we |
| 2503 | // could still do the transformation. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2504 | TruncInst *TI = dyn_cast<TruncInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2505 | if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth) |
| 2506 | return nullptr; |
Chris Lattner | c56c845 | 2010-12-19 18:22:06 +0000 | [diff] [blame] | 2507 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2508 | |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 2509 | // If the pattern matches, truncate the inputs to the narrower type and |
| 2510 | // use the sadd_with_overflow intrinsic to efficiently compute both the |
| 2511 | // result and the overflow bit. |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 2512 | Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth); |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 2513 | Value *F = Intrinsic::getDeclaration(I.getModule(), |
| 2514 | Intrinsic::sadd_with_overflow, NewType); |
Chris Lattner | 7987456 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 2515 | |
Chris Lattner | ce2995a | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 2516 | InstCombiner::BuilderTy *Builder = IC.Builder; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2517 | |
Chris Lattner | 7987456 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 2518 | // Put the new code above the original add, in case there are any uses of the |
| 2519 | // add between the add and the compare. |
Chris Lattner | 5e0c0c7 | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 2520 | Builder->SetInsertPoint(OrigAdd); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2521 | |
Chris Lattner | 7987456 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 2522 | Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName()+".trunc"); |
| 2523 | Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName()+".trunc"); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 2524 | CallInst *Call = Builder->CreateCall(F, {TruncA, TruncB}, "sadd"); |
Chris Lattner | 7987456 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 2525 | Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result"); |
| 2526 | Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2527 | |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 2528 | // The inner add was the result of the narrow add, zero extended to the |
| 2529 | // wider type. Replace it with the result computed by the intrinsic. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2530 | IC.replaceInstUsesWith(*OrigAdd, ZExt); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | 7987456 | 2010-12-19 18:35:09 +0000 | [diff] [blame] | 2532 | // The original icmp gets replaced with the overflow value. |
| 2533 | return ExtractValueInst::Create(Call, 1, "sadd.overflow"); |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 2534 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2535 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2536 | bool InstCombiner::OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, |
| 2537 | Value *RHS, Instruction &OrigI, |
| 2538 | Value *&Result, Constant *&Overflow) { |
Sanjoy Das | 827529e | 2015-08-11 21:33:55 +0000 | [diff] [blame] | 2539 | if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS)) |
| 2540 | std::swap(LHS, RHS); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2541 | |
| 2542 | auto SetResult = [&](Value *OpResult, Constant *OverflowVal, bool ReuseName) { |
| 2543 | Result = OpResult; |
| 2544 | Overflow = OverflowVal; |
| 2545 | if (ReuseName) |
| 2546 | Result->takeName(&OrigI); |
| 2547 | return true; |
| 2548 | }; |
| 2549 | |
Sanjoy Das | 6f5dca7 | 2015-08-28 19:09:31 +0000 | [diff] [blame] | 2550 | // If the overflow check was an add followed by a compare, the insertion point |
| 2551 | // may be pointing to the compare. We want to insert the new instructions |
| 2552 | // before the add in case there are uses of the add between the add and the |
| 2553 | // compare. |
| 2554 | Builder->SetInsertPoint(&OrigI); |
| 2555 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2556 | switch (OCF) { |
| 2557 | case OCF_INVALID: |
| 2558 | llvm_unreachable("bad overflow check kind!"); |
| 2559 | |
| 2560 | case OCF_UNSIGNED_ADD: { |
| 2561 | OverflowResult OR = computeOverflowForUnsignedAdd(LHS, RHS, &OrigI); |
| 2562 | if (OR == OverflowResult::NeverOverflows) |
| 2563 | return SetResult(Builder->CreateNUWAdd(LHS, RHS), Builder->getFalse(), |
| 2564 | true); |
| 2565 | |
| 2566 | if (OR == OverflowResult::AlwaysOverflows) |
| 2567 | return SetResult(Builder->CreateAdd(LHS, RHS), Builder->getTrue(), true); |
| 2568 | } |
| 2569 | // FALL THROUGH uadd into sadd |
| 2570 | case OCF_SIGNED_ADD: { |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 2571 | // X + 0 -> {X, false} |
| 2572 | if (match(RHS, m_Zero())) |
| 2573 | return SetResult(LHS, Builder->getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2574 | |
| 2575 | // We can strength reduce this signed add into a regular add if we can prove |
| 2576 | // that it will never overflow. |
| 2577 | if (OCF == OCF_SIGNED_ADD) |
| 2578 | if (WillNotOverflowSignedAdd(LHS, RHS, OrigI)) |
| 2579 | return SetResult(Builder->CreateNSWAdd(LHS, RHS), Builder->getFalse(), |
| 2580 | true); |
Sanjoy Das | 72cb5e1 | 2015-06-05 18:04:42 +0000 | [diff] [blame] | 2581 | break; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | case OCF_UNSIGNED_SUB: |
| 2585 | case OCF_SIGNED_SUB: { |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 2586 | // X - 0 -> {X, false} |
| 2587 | if (match(RHS, m_Zero())) |
| 2588 | return SetResult(LHS, Builder->getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2589 | |
| 2590 | if (OCF == OCF_SIGNED_SUB) { |
| 2591 | if (WillNotOverflowSignedSub(LHS, RHS, OrigI)) |
| 2592 | return SetResult(Builder->CreateNSWSub(LHS, RHS), Builder->getFalse(), |
| 2593 | true); |
| 2594 | } else { |
| 2595 | if (WillNotOverflowUnsignedSub(LHS, RHS, OrigI)) |
| 2596 | return SetResult(Builder->CreateNUWSub(LHS, RHS), Builder->getFalse(), |
| 2597 | true); |
| 2598 | } |
| 2599 | break; |
| 2600 | } |
| 2601 | |
| 2602 | case OCF_UNSIGNED_MUL: { |
| 2603 | OverflowResult OR = computeOverflowForUnsignedMul(LHS, RHS, &OrigI); |
| 2604 | if (OR == OverflowResult::NeverOverflows) |
| 2605 | return SetResult(Builder->CreateNUWMul(LHS, RHS), Builder->getFalse(), |
| 2606 | true); |
| 2607 | if (OR == OverflowResult::AlwaysOverflows) |
| 2608 | return SetResult(Builder->CreateMul(LHS, RHS), Builder->getTrue(), true); |
| 2609 | } // FALL THROUGH |
| 2610 | case OCF_SIGNED_MUL: |
| 2611 | // X * undef -> undef |
| 2612 | if (isa<UndefValue>(RHS)) |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 2613 | return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2614 | |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 2615 | // X * 0 -> {0, false} |
| 2616 | if (match(RHS, m_Zero())) |
| 2617 | return SetResult(RHS, Builder->getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2618 | |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 2619 | // X * 1 -> {X, false} |
| 2620 | if (match(RHS, m_One())) |
| 2621 | return SetResult(LHS, Builder->getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2622 | |
| 2623 | if (OCF == OCF_SIGNED_MUL) |
| 2624 | if (WillNotOverflowSignedMul(LHS, RHS, OrigI)) |
| 2625 | return SetResult(Builder->CreateNSWMul(LHS, RHS), Builder->getFalse(), |
| 2626 | true); |
Sanjoy Das | c80dad6 | 2015-06-05 18:04:46 +0000 | [diff] [blame] | 2627 | break; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | return false; |
| 2631 | } |
| 2632 | |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2633 | /// \brief Recognize and process idiom involving test for multiplication |
| 2634 | /// overflow. |
| 2635 | /// |
| 2636 | /// The caller has matched a pattern of the form: |
| 2637 | /// I = cmp u (mul(zext A, zext B), V |
| 2638 | /// The function checks if this is a test for overflow and if so replaces |
| 2639 | /// multiplication with call to 'mul.with.overflow' intrinsic. |
| 2640 | /// |
| 2641 | /// \param I Compare instruction. |
| 2642 | /// \param MulVal Result of 'mult' instruction. It is one of the arguments of |
| 2643 | /// the compare instruction. Must be of integer type. |
| 2644 | /// \param OtherVal The other argument of compare instruction. |
| 2645 | /// \returns Instruction which must replace the compare instruction, NULL if no |
| 2646 | /// replacement required. |
| 2647 | static Instruction *ProcessUMulZExtIdiom(ICmpInst &I, Value *MulVal, |
| 2648 | Value *OtherVal, InstCombiner &IC) { |
Benjamin Kramer | c96a7f8 | 2014-06-24 10:47:52 +0000 | [diff] [blame] | 2649 | // Don't bother doing this transformation for pointers, don't do it for |
| 2650 | // vectors. |
| 2651 | if (!isa<IntegerType>(MulVal->getType())) |
| 2652 | return nullptr; |
| 2653 | |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2654 | assert(I.getOperand(0) == MulVal || I.getOperand(1) == MulVal); |
| 2655 | assert(I.getOperand(0) == OtherVal || I.getOperand(1) == OtherVal); |
David Majnemer | daa24b9 | 2015-09-05 20:44:56 +0000 | [diff] [blame] | 2656 | auto *MulInstr = dyn_cast<Instruction>(MulVal); |
| 2657 | if (!MulInstr) |
| 2658 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2659 | assert(MulInstr->getOpcode() == Instruction::Mul); |
| 2660 | |
David Majnemer | 634ca23 | 2014-11-01 23:46:05 +0000 | [diff] [blame] | 2661 | auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)), |
| 2662 | *RHS = cast<ZExtOperator>(MulInstr->getOperand(1)); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2663 | assert(LHS->getOpcode() == Instruction::ZExt); |
| 2664 | assert(RHS->getOpcode() == Instruction::ZExt); |
| 2665 | Value *A = LHS->getOperand(0), *B = RHS->getOperand(0); |
| 2666 | |
| 2667 | // Calculate type and width of the result produced by mul.with.overflow. |
| 2668 | Type *TyA = A->getType(), *TyB = B->getType(); |
| 2669 | unsigned WidthA = TyA->getPrimitiveSizeInBits(), |
| 2670 | WidthB = TyB->getPrimitiveSizeInBits(); |
| 2671 | unsigned MulWidth; |
| 2672 | Type *MulType; |
| 2673 | if (WidthB > WidthA) { |
| 2674 | MulWidth = WidthB; |
| 2675 | MulType = TyB; |
| 2676 | } else { |
| 2677 | MulWidth = WidthA; |
| 2678 | MulType = TyA; |
| 2679 | } |
| 2680 | |
| 2681 | // In order to replace the original mul with a narrower mul.with.overflow, |
| 2682 | // all uses must ignore upper bits of the product. The number of used low |
| 2683 | // bits must be not greater than the width of mul.with.overflow. |
| 2684 | if (MulVal->hasNUsesOrMore(2)) |
| 2685 | for (User *U : MulVal->users()) { |
| 2686 | if (U == &I) |
| 2687 | continue; |
| 2688 | if (TruncInst *TI = dyn_cast<TruncInst>(U)) { |
| 2689 | // Check if truncation ignores bits above MulWidth. |
| 2690 | unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits(); |
| 2691 | if (TruncWidth > MulWidth) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2692 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2693 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) { |
| 2694 | // Check if AND ignores bits above MulWidth. |
| 2695 | if (BO->getOpcode() != Instruction::And) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2696 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2697 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 2698 | const APInt &CVal = CI->getValue(); |
| 2699 | if (CVal.getBitWidth() - CVal.countLeadingZeros() > MulWidth) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2700 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } else { |
| 2703 | // Other uses prohibit this transformation. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2704 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2705 | } |
| 2706 | } |
| 2707 | |
| 2708 | // Recognize patterns |
| 2709 | switch (I.getPredicate()) { |
| 2710 | case ICmpInst::ICMP_EQ: |
| 2711 | case ICmpInst::ICMP_NE: |
| 2712 | // Recognize pattern: |
| 2713 | // mulval = mul(zext A, zext B) |
| 2714 | // cmp eq/neq mulval, zext trunc mulval |
| 2715 | if (ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal)) |
| 2716 | if (Zext->hasOneUse()) { |
| 2717 | Value *ZextArg = Zext->getOperand(0); |
| 2718 | if (TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg)) |
| 2719 | if (Trunc->getType()->getPrimitiveSizeInBits() == MulWidth) |
| 2720 | break; //Recognized |
| 2721 | } |
| 2722 | |
| 2723 | // Recognize pattern: |
| 2724 | // mulval = mul(zext A, zext B) |
| 2725 | // cmp eq/neq mulval, and(mulval, mask), mask selects low MulWidth bits. |
| 2726 | ConstantInt *CI; |
| 2727 | Value *ValToMask; |
| 2728 | if (match(OtherVal, m_And(m_Value(ValToMask), m_ConstantInt(CI)))) { |
| 2729 | if (ValToMask != MulVal) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2730 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2731 | const APInt &CVal = CI->getValue() + 1; |
| 2732 | if (CVal.isPowerOf2()) { |
| 2733 | unsigned MaskWidth = CVal.logBase2(); |
| 2734 | if (MaskWidth == MulWidth) |
| 2735 | break; // Recognized |
| 2736 | } |
| 2737 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2738 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2739 | |
| 2740 | case ICmpInst::ICMP_UGT: |
| 2741 | // Recognize pattern: |
| 2742 | // mulval = mul(zext A, zext B) |
| 2743 | // cmp ugt mulval, max |
| 2744 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
| 2745 | APInt MaxVal = APInt::getMaxValue(MulWidth); |
| 2746 | MaxVal = MaxVal.zext(CI->getBitWidth()); |
| 2747 | if (MaxVal.eq(CI->getValue())) |
| 2748 | break; // Recognized |
| 2749 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2750 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2751 | |
| 2752 | case ICmpInst::ICMP_UGE: |
| 2753 | // Recognize pattern: |
| 2754 | // mulval = mul(zext A, zext B) |
| 2755 | // cmp uge mulval, max+1 |
| 2756 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
| 2757 | APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth); |
| 2758 | if (MaxVal.eq(CI->getValue())) |
| 2759 | break; // Recognized |
| 2760 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2761 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2762 | |
| 2763 | case ICmpInst::ICMP_ULE: |
| 2764 | // Recognize pattern: |
| 2765 | // mulval = mul(zext A, zext B) |
| 2766 | // cmp ule mulval, max |
| 2767 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
| 2768 | APInt MaxVal = APInt::getMaxValue(MulWidth); |
| 2769 | MaxVal = MaxVal.zext(CI->getBitWidth()); |
| 2770 | if (MaxVal.eq(CI->getValue())) |
| 2771 | break; // Recognized |
| 2772 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2773 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2774 | |
| 2775 | case ICmpInst::ICMP_ULT: |
| 2776 | // Recognize pattern: |
| 2777 | // mulval = mul(zext A, zext B) |
| 2778 | // cmp ule mulval, max + 1 |
| 2779 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
Serge Pavlov | b5f3ddc | 2014-04-14 02:20:19 +0000 | [diff] [blame] | 2780 | APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2781 | if (MaxVal.eq(CI->getValue())) |
| 2782 | break; // Recognized |
| 2783 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2784 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2785 | |
| 2786 | default: |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2787 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | InstCombiner::BuilderTy *Builder = IC.Builder; |
| 2791 | Builder->SetInsertPoint(MulInstr); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2792 | |
| 2793 | // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B) |
| 2794 | Value *MulA = A, *MulB = B; |
| 2795 | if (WidthA < MulWidth) |
| 2796 | MulA = Builder->CreateZExt(A, MulType); |
| 2797 | if (WidthB < MulWidth) |
| 2798 | MulB = Builder->CreateZExt(B, MulType); |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 2799 | Value *F = Intrinsic::getDeclaration(I.getModule(), |
| 2800 | Intrinsic::umul_with_overflow, MulType); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 2801 | CallInst *Call = Builder->CreateCall(F, {MulA, MulB}, "umul"); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2802 | IC.Worklist.Add(MulInstr); |
| 2803 | |
| 2804 | // If there are uses of mul result other than the comparison, we know that |
| 2805 | // they are truncation or binary AND. Change them to use result of |
Serge Pavlov | b5f3ddc | 2014-04-14 02:20:19 +0000 | [diff] [blame] | 2806 | // mul.with.overflow and adjust properly mask/size. |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2807 | if (MulVal->hasNUsesOrMore(2)) { |
| 2808 | Value *Mul = Builder->CreateExtractValue(Call, 0, "umul.value"); |
| 2809 | for (User *U : MulVal->users()) { |
| 2810 | if (U == &I || U == OtherVal) |
| 2811 | continue; |
| 2812 | if (TruncInst *TI = dyn_cast<TruncInst>(U)) { |
| 2813 | if (TI->getType()->getPrimitiveSizeInBits() == MulWidth) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2814 | IC.replaceInstUsesWith(*TI, Mul); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2815 | else |
| 2816 | TI->setOperand(0, Mul); |
| 2817 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) { |
| 2818 | assert(BO->getOpcode() == Instruction::And); |
| 2819 | // Replace (mul & mask) --> zext (mul.with.overflow & short_mask) |
| 2820 | ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1)); |
| 2821 | APInt ShortMask = CI->getValue().trunc(MulWidth); |
| 2822 | Value *ShortAnd = Builder->CreateAnd(Mul, ShortMask); |
| 2823 | Instruction *Zext = |
| 2824 | cast<Instruction>(Builder->CreateZExt(ShortAnd, BO->getType())); |
| 2825 | IC.Worklist.Add(Zext); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2826 | IC.replaceInstUsesWith(*BO, Zext); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 2827 | } else { |
| 2828 | llvm_unreachable("Unexpected Binary operation"); |
| 2829 | } |
| 2830 | IC.Worklist.Add(cast<Instruction>(U)); |
| 2831 | } |
| 2832 | } |
| 2833 | if (isa<Instruction>(OtherVal)) |
| 2834 | IC.Worklist.Add(cast<Instruction>(OtherVal)); |
| 2835 | |
| 2836 | // The original icmp gets replaced with the overflow value, maybe inverted |
| 2837 | // depending on predicate. |
| 2838 | bool Inverse = false; |
| 2839 | switch (I.getPredicate()) { |
| 2840 | case ICmpInst::ICMP_NE: |
| 2841 | break; |
| 2842 | case ICmpInst::ICMP_EQ: |
| 2843 | Inverse = true; |
| 2844 | break; |
| 2845 | case ICmpInst::ICMP_UGT: |
| 2846 | case ICmpInst::ICMP_UGE: |
| 2847 | if (I.getOperand(0) == MulVal) |
| 2848 | break; |
| 2849 | Inverse = true; |
| 2850 | break; |
| 2851 | case ICmpInst::ICMP_ULT: |
| 2852 | case ICmpInst::ICMP_ULE: |
| 2853 | if (I.getOperand(1) == MulVal) |
| 2854 | break; |
| 2855 | Inverse = true; |
| 2856 | break; |
| 2857 | default: |
| 2858 | llvm_unreachable("Unexpected predicate"); |
| 2859 | } |
| 2860 | if (Inverse) { |
| 2861 | Value *Res = Builder->CreateExtractValue(Call, 1); |
| 2862 | return BinaryOperator::CreateNot(Res); |
| 2863 | } |
| 2864 | |
| 2865 | return ExtractValueInst::Create(Call, 1); |
| 2866 | } |
| 2867 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2868 | // DemandedBitsLHSMask - When performing a comparison against a constant, |
| 2869 | // it is possible that not all the bits in the LHS are demanded. This helper |
| 2870 | // method computes the mask that IS demanded. |
| 2871 | static APInt DemandedBitsLHSMask(ICmpInst &I, |
| 2872 | unsigned BitWidth, bool isSignCheck) { |
| 2873 | if (isSignCheck) |
| 2874 | return APInt::getSignBit(BitWidth); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2875 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2876 | ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1)); |
| 2877 | if (!CI) return APInt::getAllOnesValue(BitWidth); |
Owen Anderson | 0022a4b | 2011-01-11 18:26:37 +0000 | [diff] [blame] | 2878 | const APInt &RHS = CI->getValue(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2879 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2880 | switch (I.getPredicate()) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2881 | // For a UGT comparison, we don't care about any bits that |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2882 | // correspond to the trailing ones of the comparand. The value of these |
| 2883 | // bits doesn't impact the outcome of the comparison, because any value |
| 2884 | // greater than the RHS must differ in a bit higher than these due to carry. |
| 2885 | case ICmpInst::ICMP_UGT: { |
| 2886 | unsigned trailingOnes = RHS.countTrailingOnes(); |
| 2887 | APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes); |
| 2888 | return ~lowBitsSet; |
| 2889 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2890 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2891 | // Similarly, for a ULT comparison, we don't care about the trailing zeros. |
| 2892 | // Any value less than the RHS must differ in a higher bit because of carries. |
| 2893 | case ICmpInst::ICMP_ULT: { |
| 2894 | unsigned trailingZeros = RHS.countTrailingZeros(); |
| 2895 | APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros); |
| 2896 | return ~lowBitsSet; |
| 2897 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2898 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2899 | default: |
| 2900 | return APInt::getAllOnesValue(BitWidth); |
| 2901 | } |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 2902 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2903 | |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 2904 | /// \brief Check if the order of \p Op0 and \p Op1 as operand in an ICmpInst |
| 2905 | /// should be swapped. |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 2906 | /// The decision is based on how many times these two operands are reused |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 2907 | /// as subtract operands and their positions in those instructions. |
| 2908 | /// The rational is that several architectures use the same instruction for |
| 2909 | /// both subtract and cmp, thus it is better if the order of those operands |
| 2910 | /// match. |
| 2911 | /// \return true if Op0 and Op1 should be swapped. |
| 2912 | static bool swapMayExposeCSEOpportunities(const Value * Op0, |
| 2913 | const Value * Op1) { |
| 2914 | // Filter out pointer value as those cannot appears directly in subtract. |
| 2915 | // FIXME: we may want to go through inttoptrs or bitcasts. |
| 2916 | if (Op0->getType()->isPointerTy()) |
| 2917 | return false; |
| 2918 | // Count every uses of both Op0 and Op1 in a subtract. |
| 2919 | // Each time Op0 is the first operand, count -1: swapping is bad, the |
| 2920 | // subtract has already the same layout as the compare. |
| 2921 | // Each time Op0 is the second operand, count +1: swapping is good, the |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 2922 | // subtract has a different layout as the compare. |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 2923 | // At the end, if the benefit is greater than 0, Op0 should come second to |
| 2924 | // expose more CSE opportunities. |
| 2925 | int GlobalSwapBenefits = 0; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2926 | for (const User *U : Op0->users()) { |
| 2927 | const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(U); |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 2928 | if (!BinOp || BinOp->getOpcode() != Instruction::Sub) |
| 2929 | continue; |
| 2930 | // If Op0 is the first argument, this is not beneficial to swap the |
| 2931 | // arguments. |
| 2932 | int LocalSwapBenefits = -1; |
| 2933 | unsigned Op1Idx = 1; |
| 2934 | if (BinOp->getOperand(Op1Idx) == Op0) { |
| 2935 | Op1Idx = 0; |
| 2936 | LocalSwapBenefits = 1; |
| 2937 | } |
| 2938 | if (BinOp->getOperand(Op1Idx) != Op1) |
| 2939 | continue; |
| 2940 | GlobalSwapBenefits += LocalSwapBenefits; |
| 2941 | } |
| 2942 | return GlobalSwapBenefits > 0; |
| 2943 | } |
| 2944 | |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 2945 | /// \brief Check that one use is in the same block as the definition and all |
| 2946 | /// other uses are in blocks dominated by a given block |
| 2947 | /// |
| 2948 | /// \param DI Definition |
| 2949 | /// \param UI Use |
| 2950 | /// \param DB Block that must dominate all uses of \p DI outside |
| 2951 | /// the parent block |
| 2952 | /// \return true when \p UI is the only use of \p DI in the parent block |
| 2953 | /// and all other uses of \p DI are in blocks dominated by \p DB. |
| 2954 | /// |
| 2955 | bool InstCombiner::dominatesAllUses(const Instruction *DI, |
| 2956 | const Instruction *UI, |
| 2957 | const BasicBlock *DB) const { |
| 2958 | assert(DI && UI && "Instruction not defined\n"); |
| 2959 | // ignore incomplete definitions |
| 2960 | if (!DI->getParent()) |
| 2961 | return false; |
| 2962 | // DI and UI must be in the same block |
| 2963 | if (DI->getParent() != UI->getParent()) |
| 2964 | return false; |
| 2965 | // Protect from self-referencing blocks |
| 2966 | if (DI->getParent() == DB) |
| 2967 | return false; |
| 2968 | // DominatorTree available? |
| 2969 | if (!DT) |
| 2970 | return false; |
| 2971 | for (const User *U : DI->users()) { |
| 2972 | auto *Usr = cast<Instruction>(U); |
| 2973 | if (Usr != UI && !DT->dominates(DB, Usr->getParent())) |
| 2974 | return false; |
| 2975 | } |
| 2976 | return true; |
| 2977 | } |
| 2978 | |
| 2979 | /// |
| 2980 | /// true when the instruction sequence within a block is select-cmp-br. |
| 2981 | /// |
| 2982 | static bool isChainSelectCmpBranch(const SelectInst *SI) { |
| 2983 | const BasicBlock *BB = SI->getParent(); |
| 2984 | if (!BB) |
| 2985 | return false; |
| 2986 | auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator()); |
| 2987 | if (!BI || BI->getNumSuccessors() != 2) |
| 2988 | return false; |
| 2989 | auto *IC = dyn_cast<ICmpInst>(BI->getCondition()); |
| 2990 | if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI)) |
| 2991 | return false; |
| 2992 | return true; |
| 2993 | } |
| 2994 | |
| 2995 | /// |
| 2996 | /// \brief True when a select result is replaced by one of its operands |
| 2997 | /// in select-icmp sequence. This will eventually result in the elimination |
| 2998 | /// of the select. |
| 2999 | /// |
| 3000 | /// \param SI Select instruction |
| 3001 | /// \param Icmp Compare instruction |
| 3002 | /// \param SIOpd Operand that replaces the select |
| 3003 | /// |
| 3004 | /// Notes: |
| 3005 | /// - The replacement is global and requires dominator information |
| 3006 | /// - The caller is responsible for the actual replacement |
| 3007 | /// |
| 3008 | /// Example: |
| 3009 | /// |
| 3010 | /// entry: |
| 3011 | /// %4 = select i1 %3, %C* %0, %C* null |
| 3012 | /// %5 = icmp eq %C* %4, null |
| 3013 | /// br i1 %5, label %9, label %7 |
| 3014 | /// ... |
| 3015 | /// ; <label>:7 ; preds = %entry |
| 3016 | /// %8 = getelementptr inbounds %C* %4, i64 0, i32 0 |
| 3017 | /// ... |
| 3018 | /// |
| 3019 | /// can be transformed to |
| 3020 | /// |
| 3021 | /// %5 = icmp eq %C* %0, null |
| 3022 | /// %6 = select i1 %3, i1 %5, i1 true |
| 3023 | /// br i1 %6, label %9, label %7 |
| 3024 | /// ... |
| 3025 | /// ; <label>:7 ; preds = %entry |
| 3026 | /// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0! |
| 3027 | /// |
| 3028 | /// Similar when the first operand of the select is a constant or/and |
| 3029 | /// the compare is for not equal rather than equal. |
| 3030 | /// |
| 3031 | /// NOTE: The function is only called when the select and compare constants |
| 3032 | /// are equal, the optimization can work only for EQ predicates. This is not a |
| 3033 | /// major restriction since a NE compare should be 'normalized' to an equal |
| 3034 | /// compare, which usually happens in the combiner and test case |
| 3035 | /// select-cmp-br.ll |
| 3036 | /// checks for it. |
| 3037 | bool InstCombiner::replacedSelectWithOperand(SelectInst *SI, |
| 3038 | const ICmpInst *Icmp, |
| 3039 | const unsigned SIOpd) { |
David Majnemer | 83484fd | 2014-11-22 06:09:28 +0000 | [diff] [blame] | 3040 | assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!"); |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3041 | if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) { |
| 3042 | BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1); |
| 3043 | // The check for the unique predecessor is not the best that can be |
| 3044 | // done. But it protects efficiently against cases like when SI's |
| 3045 | // home block has two successors, Succ and Succ1, and Succ1 predecessor |
| 3046 | // of Succ. Then SI can't be replaced by SIOpd because the use that gets |
| 3047 | // replaced can be reached on either path. So the uniqueness check |
| 3048 | // guarantees that the path all uses of SI (outside SI's parent) are on |
| 3049 | // is disjoint from all other paths out of SI. But that information |
| 3050 | // is more expensive to compute, and the trade-off here is in favor |
| 3051 | // of compile-time. |
| 3052 | if (Succ->getUniquePredecessor() && dominatesAllUses(SI, Icmp, Succ)) { |
| 3053 | NumSel++; |
| 3054 | SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent()); |
| 3055 | return true; |
| 3056 | } |
| 3057 | } |
| 3058 | return false; |
| 3059 | } |
| 3060 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3061 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 3062 | bool Changed = false; |
Chris Lattner | 9306ffa | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 3063 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 3064 | unsigned Op0Cplxity = getComplexity(Op0); |
| 3065 | unsigned Op1Cplxity = getComplexity(Op1); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3066 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3067 | /// Orders the operands of the compare so that they are listed from most |
| 3068 | /// complex to least complex. This puts constants before unary operators, |
| 3069 | /// before binary operators. |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 3070 | if (Op0Cplxity < Op1Cplxity || |
| 3071 | (Op0Cplxity == Op1Cplxity && |
| 3072 | swapMayExposeCSEOpportunities(Op0, Op1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3073 | I.swapOperands(); |
Chris Lattner | 9306ffa | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 3074 | std::swap(Op0, Op1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3075 | Changed = true; |
| 3076 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3077 | |
Jingyue Wu | 5e34ce3 | 2015-06-25 20:14:47 +0000 | [diff] [blame] | 3078 | if (Value *V = |
| 3079 | SimplifyICmpInst(I.getPredicate(), Op0, Op1, DL, TLI, DT, AC, &I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3080 | return replaceInstUsesWith(I, V); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3081 | |
Pete Cooper | bc5c524 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 3082 | // comparing -val or val with non-zero is the same as just comparing val |
Pete Cooper | fdddc27 | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 3083 | // ie, abs(val) != 0 -> val != 0 |
Pete Cooper | bc5c524 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 3084 | if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) |
| 3085 | { |
Pete Cooper | fdddc27 | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 3086 | Value *Cond, *SelectTrue, *SelectFalse; |
| 3087 | if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue), |
Pete Cooper | bc5c524 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 3088 | m_Value(SelectFalse)))) { |
Pete Cooper | fdddc27 | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 3089 | if (Value *V = dyn_castNegVal(SelectTrue)) { |
| 3090 | if (V == SelectFalse) |
| 3091 | return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); |
| 3092 | } |
| 3093 | else if (Value *V = dyn_castNegVal(SelectFalse)) { |
| 3094 | if (V == SelectTrue) |
| 3095 | return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); |
Pete Cooper | bc5c524 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 3096 | } |
| 3097 | } |
| 3098 | } |
| 3099 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3100 | Type *Ty = Op0->getType(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3101 | |
| 3102 | // icmp's with boolean values can always be turned into bitwise operations |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3103 | if (Ty->isIntegerTy(1)) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3104 | switch (I.getPredicate()) { |
| 3105 | default: llvm_unreachable("Invalid icmp instruction!"); |
| 3106 | case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B) |
| 3107 | Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp"); |
| 3108 | return BinaryOperator::CreateNot(Xor); |
| 3109 | } |
| 3110 | case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B |
| 3111 | return BinaryOperator::CreateXor(Op0, Op1); |
| 3112 | |
| 3113 | case ICmpInst::ICMP_UGT: |
| 3114 | std::swap(Op0, Op1); // Change icmp ugt -> icmp ult |
| 3115 | // FALL THROUGH |
| 3116 | case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B |
| 3117 | Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp"); |
| 3118 | return BinaryOperator::CreateAnd(Not, Op1); |
| 3119 | } |
| 3120 | case ICmpInst::ICMP_SGT: |
| 3121 | std::swap(Op0, Op1); // Change icmp sgt -> icmp slt |
| 3122 | // FALL THROUGH |
| 3123 | case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B |
| 3124 | Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp"); |
| 3125 | return BinaryOperator::CreateAnd(Not, Op0); |
| 3126 | } |
| 3127 | case ICmpInst::ICMP_UGE: |
| 3128 | std::swap(Op0, Op1); // Change icmp uge -> icmp ule |
| 3129 | // FALL THROUGH |
| 3130 | case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B |
| 3131 | Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp"); |
| 3132 | return BinaryOperator::CreateOr(Not, Op1); |
| 3133 | } |
| 3134 | case ICmpInst::ICMP_SGE: |
| 3135 | std::swap(Op0, Op1); // Change icmp sge -> icmp sle |
| 3136 | // FALL THROUGH |
| 3137 | case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B |
| 3138 | Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp"); |
| 3139 | return BinaryOperator::CreateOr(Not, Op0); |
| 3140 | } |
| 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | unsigned BitWidth = 0; |
Chris Lattner | 5e0c0c7 | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 3145 | if (Ty->isIntOrIntVectorTy()) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3146 | BitWidth = Ty->getScalarSizeInBits(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3147 | else // Get pointer size. |
| 3148 | BitWidth = DL.getTypeSizeInBits(Ty->getScalarType()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3149 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3150 | bool isSignBit = false; |
| 3151 | |
| 3152 | // See if we are doing a comparison with a constant. |
| 3153 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3154 | Value *A = nullptr, *B = nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3155 | |
Owen Anderson | 1294ea7 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 3156 | // Match the following pattern, which is a common idiom when writing |
| 3157 | // overflow-safe integer arithmetic function. The source performs an |
| 3158 | // addition in wider type, and explicitly checks for overflow using |
| 3159 | // comparisons against INT_MIN and INT_MAX. Simplify this by using the |
| 3160 | // sadd_with_overflow intrinsic. |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 3161 | // |
| 3162 | // TODO: This could probably be generalized to handle other overflow-safe |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3163 | // operations if we worked out the formulas to compute the appropriate |
Owen Anderson | 1294ea7 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 3164 | // magic constants. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3165 | // |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 3166 | // sum = a + b |
| 3167 | // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 |
Owen Anderson | 1294ea7 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 3168 | { |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 3169 | ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI |
Owen Anderson | 1294ea7 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 3170 | if (I.getPredicate() == ICmpInst::ICMP_UGT && |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 3171 | match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) |
Chris Lattner | ce2995a | 2010-12-19 18:38:44 +0000 | [diff] [blame] | 3172 | if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this)) |
Chris Lattner | ee61c1d | 2010-12-19 17:52:50 +0000 | [diff] [blame] | 3173 | return Res; |
Owen Anderson | 1294ea7 | 2010-12-17 18:08:00 +0000 | [diff] [blame] | 3174 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3175 | |
Philip Reames | ec8a8b5 | 2016-03-09 21:05:07 +0000 | [diff] [blame] | 3176 | // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0) |
| 3177 | if (CI->isZero() && I.getPredicate() == ICmpInst::ICMP_SGT) |
| 3178 | if (auto *SI = dyn_cast<SelectInst>(Op0)) { |
| 3179 | SelectPatternResult SPR = matchSelectPattern(SI, A, B); |
| 3180 | if (SPR.Flavor == SPF_SMIN) { |
Philip Reames | 8f12eba | 2016-03-09 21:31:47 +0000 | [diff] [blame^] | 3181 | if (isKnownPositive(A, DL)) |
Philip Reames | ec8a8b5 | 2016-03-09 21:05:07 +0000 | [diff] [blame] | 3182 | return new ICmpInst(I.getPredicate(), B, CI); |
Philip Reames | 8f12eba | 2016-03-09 21:31:47 +0000 | [diff] [blame^] | 3183 | if (isKnownPositive(B, DL)) |
Philip Reames | ec8a8b5 | 2016-03-09 21:05:07 +0000 | [diff] [blame] | 3184 | return new ICmpInst(I.getPredicate(), A, CI); |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | |
David Majnemer | a0afb55 | 2015-01-14 19:26:56 +0000 | [diff] [blame] | 3189 | // The following transforms are only 'worth it' if the only user of the |
| 3190 | // subtraction is the icmp. |
| 3191 | if (Op0->hasOneUse()) { |
| 3192 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 3193 | if (I.isEquality() && CI->isZero() && |
| 3194 | match(Op0, m_Sub(m_Value(A), m_Value(B)))) |
| 3195 | return new ICmpInst(I.getPredicate(), A, B); |
| 3196 | |
| 3197 | // (icmp sgt (sub nsw A B), -1) -> (icmp sge A, B) |
| 3198 | if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isAllOnesValue() && |
| 3199 | match(Op0, m_NSWSub(m_Value(A), m_Value(B)))) |
| 3200 | return new ICmpInst(ICmpInst::ICMP_SGE, A, B); |
| 3201 | |
| 3202 | // (icmp sgt (sub nsw A B), 0) -> (icmp sgt A, B) |
| 3203 | if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isZero() && |
| 3204 | match(Op0, m_NSWSub(m_Value(A), m_Value(B)))) |
| 3205 | return new ICmpInst(ICmpInst::ICMP_SGT, A, B); |
| 3206 | |
| 3207 | // (icmp slt (sub nsw A B), 0) -> (icmp slt A, B) |
| 3208 | if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isZero() && |
| 3209 | match(Op0, m_NSWSub(m_Value(A), m_Value(B)))) |
| 3210 | return new ICmpInst(ICmpInst::ICMP_SLT, A, B); |
| 3211 | |
| 3212 | // (icmp slt (sub nsw A B), 1) -> (icmp sle A, B) |
| 3213 | if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isOne() && |
| 3214 | match(Op0, m_NSWSub(m_Value(A), m_Value(B)))) |
| 3215 | return new ICmpInst(ICmpInst::ICMP_SLE, A, B); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3216 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3217 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3218 | // If we have an icmp le or icmp ge instruction, turn it into the |
| 3219 | // appropriate icmp lt or icmp gt instruction. This allows us to rely on |
| 3220 | // them being folded in the code below. The SimplifyICmpInst code has |
| 3221 | // already handled the edge cases for us, so we just assert on them. |
| 3222 | switch (I.getPredicate()) { |
| 3223 | default: break; |
| 3224 | case ICmpInst::ICMP_ULE: |
| 3225 | assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE |
| 3226 | return new ICmpInst(ICmpInst::ICMP_ULT, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3227 | Builder->getInt(CI->getValue()+1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3228 | case ICmpInst::ICMP_SLE: |
| 3229 | assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE |
| 3230 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3231 | Builder->getInt(CI->getValue()+1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3232 | case ICmpInst::ICMP_UGE: |
Nick Lewycky | 6b445419 | 2011-02-28 06:20:05 +0000 | [diff] [blame] | 3233 | assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3234 | return new ICmpInst(ICmpInst::ICMP_UGT, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3235 | Builder->getInt(CI->getValue()-1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3236 | case ICmpInst::ICMP_SGE: |
Nick Lewycky | 6b445419 | 2011-02-28 06:20:05 +0000 | [diff] [blame] | 3237 | assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3238 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3239 | Builder->getInt(CI->getValue()-1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3240 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3241 | |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 3242 | if (I.isEquality()) { |
| 3243 | ConstantInt *CI2; |
| 3244 | if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) || |
| 3245 | match(Op0, m_LShr(m_ConstantInt(CI2), m_Value(A)))) { |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 3246 | // (icmp eq/ne (ashr/lshr const2, A), const1) |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 3247 | if (Instruction *Inst = FoldICmpCstShrCst(I, Op0, A, CI, CI2)) |
| 3248 | return Inst; |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 3249 | } |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 3250 | if (match(Op0, m_Shl(m_ConstantInt(CI2), m_Value(A)))) { |
| 3251 | // (icmp eq/ne (shl const2, A), const1) |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 3252 | if (Instruction *Inst = FoldICmpCstShlCst(I, Op0, A, CI, CI2)) |
| 3253 | return Inst; |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 3254 | } |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3257 | // If this comparison is a normal comparison, it demands all |
| 3258 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| 3259 | bool UnusedBit; |
| 3260 | isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 3261 | } |
| 3262 | |
| 3263 | // See if we can fold the comparison based on range information we can get |
| 3264 | // by checking whether bits are known to be zero or one in the input. |
| 3265 | if (BitWidth != 0) { |
| 3266 | APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0); |
| 3267 | APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0); |
| 3268 | |
| 3269 | if (SimplifyDemandedBits(I.getOperandUse(0), |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3270 | DemandedBitsLHSMask(I, BitWidth, isSignBit), |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3271 | Op0KnownZero, Op0KnownOne, 0)) |
| 3272 | return &I; |
| 3273 | if (SimplifyDemandedBits(I.getOperandUse(1), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3274 | APInt::getAllOnesValue(BitWidth), Op1KnownZero, |
| 3275 | Op1KnownOne, 0)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3276 | return &I; |
| 3277 | |
| 3278 | // Given the known and unknown bits, compute a range that the LHS could be |
| 3279 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
| 3280 | // EQ and NE we use unsigned values. |
| 3281 | APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0); |
| 3282 | APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0); |
| 3283 | if (I.isSigned()) { |
| 3284 | ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, |
| 3285 | Op0Min, Op0Max); |
| 3286 | ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, |
| 3287 | Op1Min, Op1Max); |
| 3288 | } else { |
| 3289 | ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, |
| 3290 | Op0Min, Op0Max); |
| 3291 | ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, |
| 3292 | Op1Min, Op1Max); |
| 3293 | } |
| 3294 | |
| 3295 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
| 3296 | // figured out that the LHS is a constant. Just constant fold this now so |
| 3297 | // that code below can assume that Min != Max. |
| 3298 | if (!isa<Constant>(Op0) && Op0Min == Op0Max) |
| 3299 | return new ICmpInst(I.getPredicate(), |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 3300 | ConstantInt::get(Op0->getType(), Op0Min), Op1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3301 | if (!isa<Constant>(Op1) && Op1Min == Op1Max) |
| 3302 | return new ICmpInst(I.getPredicate(), Op0, |
Nick Lewycky | 92db8e8 | 2011-03-06 03:36:19 +0000 | [diff] [blame] | 3303 | ConstantInt::get(Op1->getType(), Op1Min)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3304 | |
| 3305 | // Based on the range information we know about the LHS, see if we can |
Nick Lewycky | 6b445419 | 2011-02-28 06:20:05 +0000 | [diff] [blame] | 3306 | // simplify this comparison. For example, (x&4) < 8 is always true. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3307 | switch (I.getPredicate()) { |
| 3308 | default: llvm_unreachable("Unknown icmp opcode!"); |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3309 | case ICmpInst::ICMP_EQ: { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3310 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3311 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3312 | |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3313 | // If all bits are known zero except for one, then we know at most one |
| 3314 | // bit is set. If the comparison is against zero, then this is a check |
| 3315 | // to see if *that* bit is set. |
| 3316 | APInt Op0KnownZeroInverted = ~Op0KnownZero; |
Dinesh Dwivedi | ce5d35a | 2014-06-02 07:57:24 +0000 | [diff] [blame] | 3317 | if (~Op1KnownZero == 0) { |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3318 | // If the LHS is an AND with the same constant, look through it. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3319 | Value *LHS = nullptr; |
| 3320 | ConstantInt *LHSC = nullptr; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3321 | if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) || |
| 3322 | LHSC->getValue() != Op0KnownZeroInverted) |
| 3323 | LHS = Op0; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3325 | // If the LHS is 1 << x, and we know the result is a power of 2 like 8, |
Chris Lattner | e5afa15 | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 3326 | // then turn "((1 << x)&8) == 0" into "x != 3". |
Dinesh Dwivedi | ce5d35a | 2014-06-02 07:57:24 +0000 | [diff] [blame] | 3327 | // or turn "((1 << x)&7) == 0" into "x > 2". |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3328 | Value *X = nullptr; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3329 | if (match(LHS, m_Shl(m_One(), m_Value(X)))) { |
Dinesh Dwivedi | ce5d35a | 2014-06-02 07:57:24 +0000 | [diff] [blame] | 3330 | APInt ValToCheck = Op0KnownZeroInverted; |
| 3331 | if (ValToCheck.isPowerOf2()) { |
| 3332 | unsigned CmpVal = ValToCheck.countTrailingZeros(); |
| 3333 | return new ICmpInst(ICmpInst::ICMP_NE, X, |
| 3334 | ConstantInt::get(X->getType(), CmpVal)); |
| 3335 | } else if ((++ValToCheck).isPowerOf2()) { |
| 3336 | unsigned CmpVal = ValToCheck.countTrailingZeros() - 1; |
| 3337 | return new ICmpInst(ICmpInst::ICMP_UGT, X, |
| 3338 | ConstantInt::get(X->getType(), CmpVal)); |
| 3339 | } |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3340 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3341 | |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3342 | // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1, |
Chris Lattner | e5afa15 | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 3343 | // then turn "((8 >>u x)&1) == 0" into "x != 3". |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 3344 | const APInt *CI; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3345 | if (Op0KnownZeroInverted == 1 && |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 3346 | match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) |
Chris Lattner | e5afa15 | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 3347 | return new ICmpInst(ICmpInst::ICMP_NE, X, |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 3348 | ConstantInt::get(X->getType(), |
| 3349 | CI->countTrailingZeros())); |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3350 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3351 | break; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3352 | } |
| 3353 | case ICmpInst::ICMP_NE: { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3354 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3355 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3356 | |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3357 | // If all bits are known zero except for one, then we know at most one |
| 3358 | // bit is set. If the comparison is against zero, then this is a check |
| 3359 | // to see if *that* bit is set. |
| 3360 | APInt Op0KnownZeroInverted = ~Op0KnownZero; |
Dinesh Dwivedi | ce5d35a | 2014-06-02 07:57:24 +0000 | [diff] [blame] | 3361 | if (~Op1KnownZero == 0) { |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3362 | // If the LHS is an AND with the same constant, look through it. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3363 | Value *LHS = nullptr; |
| 3364 | ConstantInt *LHSC = nullptr; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3365 | if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) || |
| 3366 | LHSC->getValue() != Op0KnownZeroInverted) |
| 3367 | LHS = Op0; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3368 | |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3369 | // If the LHS is 1 << x, and we know the result is a power of 2 like 8, |
Chris Lattner | e5afa15 | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 3370 | // then turn "((1 << x)&8) != 0" into "x == 3". |
Dinesh Dwivedi | ce5d35a | 2014-06-02 07:57:24 +0000 | [diff] [blame] | 3371 | // or turn "((1 << x)&7) != 0" into "x < 3". |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3372 | Value *X = nullptr; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3373 | if (match(LHS, m_Shl(m_One(), m_Value(X)))) { |
Dinesh Dwivedi | ce5d35a | 2014-06-02 07:57:24 +0000 | [diff] [blame] | 3374 | APInt ValToCheck = Op0KnownZeroInverted; |
| 3375 | if (ValToCheck.isPowerOf2()) { |
| 3376 | unsigned CmpVal = ValToCheck.countTrailingZeros(); |
| 3377 | return new ICmpInst(ICmpInst::ICMP_EQ, X, |
| 3378 | ConstantInt::get(X->getType(), CmpVal)); |
| 3379 | } else if ((++ValToCheck).isPowerOf2()) { |
| 3380 | unsigned CmpVal = ValToCheck.countTrailingZeros(); |
| 3381 | return new ICmpInst(ICmpInst::ICMP_ULT, X, |
| 3382 | ConstantInt::get(X->getType(), CmpVal)); |
| 3383 | } |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3384 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3385 | |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3386 | // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1, |
Chris Lattner | e5afa15 | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 3387 | // then turn "((8 >>u x)&1) != 0" into "x == 3". |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 3388 | const APInt *CI; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3389 | if (Op0KnownZeroInverted == 1 && |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 3390 | match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) |
Chris Lattner | e5afa15 | 2010-11-23 02:42:04 +0000 | [diff] [blame] | 3391 | return new ICmpInst(ICmpInst::ICMP_EQ, X, |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 3392 | ConstantInt::get(X->getType(), |
| 3393 | CI->countTrailingZeros())); |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3394 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3395 | break; |
Chris Lattner | f7e8961 | 2010-11-21 06:44:42 +0000 | [diff] [blame] | 3396 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3397 | case ICmpInst::ICMP_ULT: |
| 3398 | if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3399 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3400 | if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3401 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3402 | if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B) |
| 3403 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 3404 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 3405 | if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C |
| 3406 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3407 | Builder->getInt(CI->getValue()-1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3408 | |
| 3409 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 3410 | if (CI->isMinValue(true)) |
| 3411 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, |
| 3412 | Constant::getAllOnesValue(Op0->getType())); |
| 3413 | } |
| 3414 | break; |
| 3415 | case ICmpInst::ICMP_UGT: |
| 3416 | if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3417 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3418 | if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3419 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3420 | |
| 3421 | if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B) |
| 3422 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 3423 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 3424 | if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C |
| 3425 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3426 | Builder->getInt(CI->getValue()+1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3427 | |
| 3428 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 3429 | if (CI->isMaxValue(true)) |
| 3430 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, |
| 3431 | Constant::getNullValue(Op0->getType())); |
| 3432 | } |
| 3433 | break; |
| 3434 | case ICmpInst::ICMP_SLT: |
| 3435 | if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3436 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3437 | if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3438 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3439 | if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B) |
| 3440 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 3441 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 3442 | if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C |
| 3443 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3444 | Builder->getInt(CI->getValue()-1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3445 | } |
| 3446 | break; |
| 3447 | case ICmpInst::ICMP_SGT: |
| 3448 | if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3449 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3450 | if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3451 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3452 | |
| 3453 | if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B) |
| 3454 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 3455 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 3456 | if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C |
| 3457 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 3458 | Builder->getInt(CI->getValue()+1)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3459 | } |
| 3460 | break; |
| 3461 | case ICmpInst::ICMP_SGE: |
| 3462 | assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!"); |
| 3463 | if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3464 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3465 | if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3466 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3467 | break; |
| 3468 | case ICmpInst::ICMP_SLE: |
| 3469 | assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!"); |
| 3470 | if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3471 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3472 | if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3473 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3474 | break; |
| 3475 | case ICmpInst::ICMP_UGE: |
| 3476 | assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!"); |
| 3477 | if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3478 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3479 | if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3480 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | case ICmpInst::ICMP_ULE: |
| 3483 | assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!"); |
| 3484 | if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3485 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3486 | if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3487 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | } |
| 3490 | |
| 3491 | // Turn a signed comparison into an unsigned one if both operands |
| 3492 | // are known to have the same sign. |
| 3493 | if (I.isSigned() && |
| 3494 | ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) || |
| 3495 | (Op0KnownOne.isNegative() && Op1KnownOne.isNegative()))) |
| 3496 | return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1); |
| 3497 | } |
| 3498 | |
| 3499 | // Test if the ICmpInst instruction is used exclusively by a select as |
| 3500 | // part of a minimum or maximum operation. If so, refrain from doing |
| 3501 | // any other folding. This helps out other analyses which understand |
| 3502 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 3503 | // and CodeGen. And in this case, at least one of the comparison |
| 3504 | // operands has at least one user besides the compare (the select), |
| 3505 | // which would often largely negate the benefit of folding anyway. |
| 3506 | if (I.hasOneUse()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3507 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin())) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3508 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 3509 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3510 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3511 | |
| 3512 | // See if we are doing a comparison between a constant and an instruction that |
| 3513 | // can be folded into the comparison. |
| 3514 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3515 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
| 3516 | // instruction, see if that instruction also has constants so that the |
| 3517 | // instruction can be folded into the icmp |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3518 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 3519 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 3520 | return Res; |
| 3521 | } |
| 3522 | |
| 3523 | // Handle icmp with constant (but not simple integer constant) RHS |
| 3524 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 3525 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 3526 | switch (LHSI->getOpcode()) { |
| 3527 | case Instruction::GetElementPtr: |
| 3528 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
| 3529 | if (RHSC->isNullValue() && |
| 3530 | cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices()) |
| 3531 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
| 3532 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 3533 | break; |
| 3534 | case Instruction::PHI: |
| 3535 | // Only fold icmp into the PHI if the phi and icmp are in the same |
| 3536 | // block. If in the same block, we're encouraging jump threading. If |
| 3537 | // not, we are just pessimizing the code by making an i1 phi. |
| 3538 | if (LHSI->getParent() == I.getParent()) |
Chris Lattner | ea7131a | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 3539 | if (Instruction *NV = FoldOpIntoPhi(I)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3540 | return NV; |
| 3541 | break; |
| 3542 | case Instruction::Select: { |
| 3543 | // If either operand of the select is a constant, we can fold the |
| 3544 | // comparison into the select arms, which will cause one to be |
| 3545 | // constant folded and the select turned into a bitwise or. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3546 | Value *Op1 = nullptr, *Op2 = nullptr; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 3547 | ConstantInt *CI = nullptr; |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3548 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3549 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3550 | CI = dyn_cast<ConstantInt>(Op1); |
| 3551 | } |
| 3552 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3553 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3554 | CI = dyn_cast<ConstantInt>(Op2); |
| 3555 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3556 | |
| 3557 | // We only want to perform this transformation if it will not lead to |
| 3558 | // additional code. This is true if either both sides of the select |
| 3559 | // fold to a constant (in which case the icmp is replaced with a select |
| 3560 | // which will usually simplify) or this is the only user of the |
| 3561 | // select (in which case we are trading a select+icmp for a simpler |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3562 | // select+icmp) or all uses of the select can be replaced based on |
| 3563 | // dominance information ("Global cases"). |
| 3564 | bool Transform = false; |
| 3565 | if (Op1 && Op2) |
| 3566 | Transform = true; |
| 3567 | else if (Op1 || Op2) { |
| 3568 | // Local case |
| 3569 | if (LHSI->hasOneUse()) |
| 3570 | Transform = true; |
| 3571 | // Global cases |
| 3572 | else if (CI && !CI->isZero()) |
| 3573 | // When Op1 is constant try replacing select with second operand. |
| 3574 | // Otherwise Op2 is constant and try replacing select with first |
| 3575 | // operand. |
| 3576 | Transform = replacedSelectWithOperand(cast<SelectInst>(LHSI), &I, |
| 3577 | Op1 ? 2 : 1); |
| 3578 | } |
| 3579 | if (Transform) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3580 | if (!Op1) |
| 3581 | Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1), |
| 3582 | RHSC, I.getName()); |
| 3583 | if (!Op2) |
| 3584 | Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2), |
| 3585 | RHSC, I.getName()); |
| 3586 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
| 3587 | } |
| 3588 | break; |
| 3589 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3590 | case Instruction::IntToPtr: |
| 3591 | // icmp pred inttoptr(X), null -> icmp pred X, 0 |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3592 | if (RHSC->isNullValue() && |
| 3593 | DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType()) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3594 | return new ICmpInst(I.getPredicate(), LHSI->getOperand(0), |
| 3595 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 3596 | break; |
| 3597 | |
| 3598 | case Instruction::Load: |
| 3599 | // Try to optimize things like "A[i] > 4" to index computations. |
| 3600 | if (GetElementPtrInst *GEP = |
| 3601 | dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) { |
| 3602 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 3603 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 3604 | !cast<LoadInst>(LHSI)->isVolatile()) |
| 3605 | if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I)) |
| 3606 | return Res; |
| 3607 | } |
| 3608 | break; |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
| 3613 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0)) |
| 3614 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
| 3615 | return NI; |
| 3616 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1)) |
| 3617 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 3618 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
| 3619 | return NI; |
| 3620 | |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 3621 | // Try to optimize equality comparisons against alloca-based pointers. |
| 3622 | if (Op0->getType()->isPointerTy() && I.isEquality()) { |
| 3623 | assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?"); |
| 3624 | if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op0, DL))) |
| 3625 | if (Instruction *New = FoldAllocaCmp(I, Alloca, Op1)) |
| 3626 | return New; |
| 3627 | if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op1, DL))) |
| 3628 | if (Instruction *New = FoldAllocaCmp(I, Alloca, Op0)) |
| 3629 | return New; |
| 3630 | } |
| 3631 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3632 | // Test to see if the operands of the icmp are casted versions of other |
| 3633 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 3634 | // now. |
| 3635 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3636 | if (Op0->getType()->isPointerTy() && |
| 3637 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3638 | // We keep moving the cast from the left operand over to the right |
| 3639 | // operand, where it can often be eliminated completely. |
| 3640 | Op0 = CI->getOperand(0); |
| 3641 | |
| 3642 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 3643 | // so eliminate it as well. |
| 3644 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 3645 | Op1 = CI2->getOperand(0); |
| 3646 | |
| 3647 | // If Op1 is a constant, we can fold the cast into the constant. |
| 3648 | if (Op0->getType() != Op1->getType()) { |
| 3649 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 3650 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
| 3651 | } else { |
| 3652 | // Otherwise, cast the RHS right before the icmp |
| 3653 | Op1 = Builder->CreateBitCast(Op1, Op0->getType()); |
| 3654 | } |
| 3655 | } |
| 3656 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
| 3657 | } |
| 3658 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3659 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3660 | if (isa<CastInst>(Op0)) { |
| 3661 | // Handle the special case of: icmp (cast bool to X), <cst> |
| 3662 | // This comes up when you have code like |
| 3663 | // int X = A < B; |
| 3664 | // if (X) ... |
| 3665 | // For generality, we handle any zero-extension of any operand comparison |
| 3666 | // with a constant or another cast from the same type. |
| 3667 | if (isa<Constant>(Op1) || isa<CastInst>(Op1)) |
| 3668 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
| 3669 | return R; |
| 3670 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3671 | |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3672 | // Special logic for binary operators. |
| 3673 | BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0); |
| 3674 | BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1); |
| 3675 | if (BO0 || BO1) { |
| 3676 | CmpInst::Predicate Pred = I.getPredicate(); |
| 3677 | bool NoOp0WrapProblem = false, NoOp1WrapProblem = false; |
| 3678 | if (BO0 && isa<OverflowingBinaryOperator>(BO0)) |
| 3679 | NoOp0WrapProblem = ICmpInst::isEquality(Pred) || |
| 3680 | (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) || |
| 3681 | (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap()); |
| 3682 | if (BO1 && isa<OverflowingBinaryOperator>(BO1)) |
| 3683 | NoOp1WrapProblem = ICmpInst::isEquality(Pred) || |
| 3684 | (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) || |
| 3685 | (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap()); |
| 3686 | |
| 3687 | // Analyze the case when either Op0 or Op1 is an add instruction. |
| 3688 | // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null). |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3689 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 3690 | if (BO0 && BO0->getOpcode() == Instruction::Add) { |
| 3691 | A = BO0->getOperand(0); |
| 3692 | B = BO0->getOperand(1); |
| 3693 | } |
| 3694 | if (BO1 && BO1->getOpcode() == Instruction::Add) { |
| 3695 | C = BO1->getOperand(0); |
| 3696 | D = BO1->getOperand(1); |
| 3697 | } |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3698 | |
David Majnemer | 549f4f2 | 2014-11-01 09:09:51 +0000 | [diff] [blame] | 3699 | // icmp (X+cst) < 0 --> X < -cst |
| 3700 | if (NoOp0WrapProblem && ICmpInst::isSigned(Pred) && match(Op1, m_Zero())) |
| 3701 | if (ConstantInt *RHSC = dyn_cast_or_null<ConstantInt>(B)) |
| 3702 | if (!RHSC->isMinValue(/*isSigned=*/true)) |
| 3703 | return new ICmpInst(Pred, A, ConstantExpr::getNeg(RHSC)); |
| 3704 | |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3705 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 3706 | if ((A == Op1 || B == Op1) && NoOp0WrapProblem) |
| 3707 | return new ICmpInst(Pred, A == Op1 ? B : A, |
| 3708 | Constant::getNullValue(Op1->getType())); |
| 3709 | |
| 3710 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 3711 | if ((C == Op0 || D == Op0) && NoOp1WrapProblem) |
| 3712 | return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()), |
| 3713 | C == Op0 ? D : C); |
| 3714 | |
Duncan Sands | 84653b3 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 3715 | // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow. |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3716 | if (A && C && (A == C || A == D || B == C || B == D) && |
| 3717 | NoOp0WrapProblem && NoOp1WrapProblem && |
| 3718 | // Try not to increase register pressure. |
| 3719 | BO0->hasOneUse() && BO1->hasOneUse()) { |
| 3720 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
Duncan Sands | 1d3acdd | 2012-11-16 18:55:49 +0000 | [diff] [blame] | 3721 | Value *Y, *Z; |
| 3722 | if (A == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 3723 | // C + B == C + D -> B == D |
Duncan Sands | 1d3acdd | 2012-11-16 18:55:49 +0000 | [diff] [blame] | 3724 | Y = B; |
| 3725 | Z = D; |
| 3726 | } else if (A == D) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 3727 | // D + B == C + D -> B == C |
Duncan Sands | 1d3acdd | 2012-11-16 18:55:49 +0000 | [diff] [blame] | 3728 | Y = B; |
| 3729 | Z = C; |
| 3730 | } else if (B == C) { |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 3731 | // A + C == C + D -> A == D |
Duncan Sands | 1d3acdd | 2012-11-16 18:55:49 +0000 | [diff] [blame] | 3732 | Y = A; |
| 3733 | Z = D; |
Duncan Sands | d7d8c09 | 2012-11-16 20:53:08 +0000 | [diff] [blame] | 3734 | } else { |
| 3735 | assert(B == D); |
| 3736 | // A + D == C + D -> A == C |
Duncan Sands | 1d3acdd | 2012-11-16 18:55:49 +0000 | [diff] [blame] | 3737 | Y = A; |
| 3738 | Z = C; |
| 3739 | } |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3740 | return new ICmpInst(Pred, Y, Z); |
| 3741 | } |
| 3742 | |
David Majnemer | b81cd63 | 2013-04-11 20:05:46 +0000 | [diff] [blame] | 3743 | // icmp slt (X + -1), Y -> icmp sle X, Y |
| 3744 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT && |
| 3745 | match(B, m_AllOnes())) |
| 3746 | return new ICmpInst(CmpInst::ICMP_SLE, A, Op1); |
| 3747 | |
| 3748 | // icmp sge (X + -1), Y -> icmp sgt X, Y |
| 3749 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE && |
| 3750 | match(B, m_AllOnes())) |
| 3751 | return new ICmpInst(CmpInst::ICMP_SGT, A, Op1); |
| 3752 | |
| 3753 | // icmp sle (X + 1), Y -> icmp slt X, Y |
| 3754 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && |
| 3755 | match(B, m_One())) |
| 3756 | return new ICmpInst(CmpInst::ICMP_SLT, A, Op1); |
| 3757 | |
| 3758 | // icmp sgt (X + 1), Y -> icmp sge X, Y |
| 3759 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && |
| 3760 | match(B, m_One())) |
| 3761 | return new ICmpInst(CmpInst::ICMP_SGE, A, Op1); |
| 3762 | |
Michael Liao | c65d386 | 2015-10-19 22:08:14 +0000 | [diff] [blame] | 3763 | // icmp sgt X, (Y + -1) -> icmp sge X, Y |
| 3764 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT && |
| 3765 | match(D, m_AllOnes())) |
| 3766 | return new ICmpInst(CmpInst::ICMP_SGE, Op0, C); |
| 3767 | |
| 3768 | // icmp sle X, (Y + -1) -> icmp slt X, Y |
| 3769 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE && |
| 3770 | match(D, m_AllOnes())) |
| 3771 | return new ICmpInst(CmpInst::ICMP_SLT, Op0, C); |
| 3772 | |
| 3773 | // icmp sge X, (Y + 1) -> icmp sgt X, Y |
| 3774 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && |
| 3775 | match(D, m_One())) |
| 3776 | return new ICmpInst(CmpInst::ICMP_SGT, Op0, C); |
| 3777 | |
| 3778 | // icmp slt X, (Y + 1) -> icmp sle X, Y |
| 3779 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && |
| 3780 | match(D, m_One())) |
| 3781 | return new ICmpInst(CmpInst::ICMP_SLE, Op0, C); |
| 3782 | |
David Majnemer | b81cd63 | 2013-04-11 20:05:46 +0000 | [diff] [blame] | 3783 | // if C1 has greater magnitude than C2: |
| 3784 | // icmp (X + C1), (Y + C2) -> icmp (X + C3), Y |
| 3785 | // s.t. C3 = C1 - C2 |
| 3786 | // |
| 3787 | // if C2 has greater magnitude than C1: |
| 3788 | // icmp (X + C1), (Y + C2) -> icmp X, (Y + C3) |
| 3789 | // s.t. C3 = C2 - C1 |
| 3790 | if (A && C && NoOp0WrapProblem && NoOp1WrapProblem && |
| 3791 | (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) |
| 3792 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 3793 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) { |
| 3794 | const APInt &AP1 = C1->getValue(); |
| 3795 | const APInt &AP2 = C2->getValue(); |
| 3796 | if (AP1.isNegative() == AP2.isNegative()) { |
| 3797 | APInt AP1Abs = C1->getValue().abs(); |
| 3798 | APInt AP2Abs = C2->getValue().abs(); |
| 3799 | if (AP1Abs.uge(AP2Abs)) { |
| 3800 | ConstantInt *C3 = Builder->getInt(AP1 - AP2); |
| 3801 | Value *NewAdd = Builder->CreateNSWAdd(A, C3); |
| 3802 | return new ICmpInst(Pred, NewAdd, C); |
| 3803 | } else { |
| 3804 | ConstantInt *C3 = Builder->getInt(AP2 - AP1); |
| 3805 | Value *NewAdd = Builder->CreateNSWAdd(C, C3); |
| 3806 | return new ICmpInst(Pred, A, NewAdd); |
| 3807 | } |
| 3808 | } |
| 3809 | } |
| 3810 | |
| 3811 | |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3812 | // Analyze the case when either Op0 or Op1 is a sub instruction. |
| 3813 | // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null). |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 3814 | A = nullptr; |
| 3815 | B = nullptr; |
| 3816 | C = nullptr; |
| 3817 | D = nullptr; |
| 3818 | if (BO0 && BO0->getOpcode() == Instruction::Sub) { |
| 3819 | A = BO0->getOperand(0); |
| 3820 | B = BO0->getOperand(1); |
| 3821 | } |
| 3822 | if (BO1 && BO1->getOpcode() == Instruction::Sub) { |
| 3823 | C = BO1->getOperand(0); |
| 3824 | D = BO1->getOperand(1); |
| 3825 | } |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3826 | |
Duncan Sands | 84653b3 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 3827 | // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow. |
| 3828 | if (A == Op1 && NoOp0WrapProblem) |
| 3829 | return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); |
| 3830 | |
| 3831 | // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow. |
| 3832 | if (C == Op0 && NoOp1WrapProblem) |
| 3833 | return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); |
| 3834 | |
| 3835 | // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow. |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3836 | if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem && |
| 3837 | // Try not to increase register pressure. |
| 3838 | BO0->hasOneUse() && BO1->hasOneUse()) |
| 3839 | return new ICmpInst(Pred, A, C); |
| 3840 | |
Duncan Sands | 84653b3 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 3841 | // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow. |
| 3842 | if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem && |
| 3843 | // Try not to increase register pressure. |
| 3844 | BO0->hasOneUse() && BO1->hasOneUse()) |
| 3845 | return new ICmpInst(Pred, D, B); |
| 3846 | |
David Majnemer | 186c942 | 2014-05-15 00:02:20 +0000 | [diff] [blame] | 3847 | // icmp (0-X) < cst --> x > -cst |
| 3848 | if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) { |
| 3849 | Value *X; |
| 3850 | if (match(BO0, m_Neg(m_Value(X)))) |
| 3851 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) |
| 3852 | if (!RHSC->isMinValue(/*isSigned=*/true)) |
| 3853 | return new ICmpInst(I.getSwappedPredicate(), X, |
| 3854 | ConstantExpr::getNeg(RHSC)); |
| 3855 | } |
| 3856 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3857 | BinaryOperator *SRem = nullptr; |
Nick Lewycky | afc8098 | 2011-03-08 06:29:47 +0000 | [diff] [blame] | 3858 | // icmp (srem X, Y), Y |
Nick Lewycky | 25cc338 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 3859 | if (BO0 && BO0->getOpcode() == Instruction::SRem && |
| 3860 | Op1 == BO0->getOperand(1)) |
| 3861 | SRem = BO0; |
Nick Lewycky | afc8098 | 2011-03-08 06:29:47 +0000 | [diff] [blame] | 3862 | // icmp Y, (srem X, Y) |
Nick Lewycky | 25cc338 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 3863 | else if (BO1 && BO1->getOpcode() == Instruction::SRem && |
| 3864 | Op0 == BO1->getOperand(1)) |
| 3865 | SRem = BO1; |
| 3866 | if (SRem) { |
| 3867 | // We don't check hasOneUse to avoid increasing register pressure because |
| 3868 | // the value we use is the same value this instruction was already using. |
| 3869 | switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) { |
| 3870 | default: break; |
| 3871 | case ICmpInst::ICMP_EQ: |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3872 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
Nick Lewycky | 25cc338 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 3873 | case ICmpInst::ICMP_NE: |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3874 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
Nick Lewycky | 25cc338 | 2011-03-05 04:28:48 +0000 | [diff] [blame] | 3875 | case ICmpInst::ICMP_SGT: |
| 3876 | case ICmpInst::ICMP_SGE: |
| 3877 | return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1), |
| 3878 | Constant::getAllOnesValue(SRem->getType())); |
| 3879 | case ICmpInst::ICMP_SLT: |
| 3880 | case ICmpInst::ICMP_SLE: |
| 3881 | return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1), |
| 3882 | Constant::getNullValue(SRem->getType())); |
| 3883 | } |
| 3884 | } |
| 3885 | |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3886 | if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && |
| 3887 | BO0->hasOneUse() && BO1->hasOneUse() && |
| 3888 | BO0->getOperand(1) == BO1->getOperand(1)) { |
| 3889 | switch (BO0->getOpcode()) { |
| 3890 | default: break; |
| 3891 | case Instruction::Add: |
| 3892 | case Instruction::Sub: |
| 3893 | case Instruction::Xor: |
| 3894 | if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b |
| 3895 | return new ICmpInst(I.getPredicate(), BO0->getOperand(0), |
| 3896 | BO1->getOperand(0)); |
| 3897 | // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b |
| 3898 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) { |
| 3899 | if (CI->getValue().isSignBit()) { |
| 3900 | ICmpInst::Predicate Pred = I.isSigned() |
| 3901 | ? I.getUnsignedPredicate() |
| 3902 | : I.getSignedPredicate(); |
| 3903 | return new ICmpInst(Pred, BO0->getOperand(0), |
| 3904 | BO1->getOperand(0)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3905 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3906 | |
David Majnemer | f8853ae | 2016-02-01 17:37:56 +0000 | [diff] [blame] | 3907 | if (BO0->getOpcode() == Instruction::Xor && CI->isMaxValue(true)) { |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3908 | ICmpInst::Predicate Pred = I.isSigned() |
| 3909 | ? I.getUnsignedPredicate() |
| 3910 | : I.getSignedPredicate(); |
| 3911 | Pred = I.getSwappedPredicate(Pred); |
| 3912 | return new ICmpInst(Pred, BO0->getOperand(0), |
| 3913 | BO1->getOperand(0)); |
| 3914 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3915 | } |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3916 | break; |
| 3917 | case Instruction::Mul: |
| 3918 | if (!I.isEquality()) |
| 3919 | break; |
| 3920 | |
| 3921 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) { |
| 3922 | // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask |
| 3923 | // Mask = -1 >> count-trailing-zeros(Cst). |
| 3924 | if (!CI->isZero() && !CI->isOne()) { |
| 3925 | const APInt &AP = CI->getValue(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3926 | ConstantInt *Mask = ConstantInt::get(I.getContext(), |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 3927 | APInt::getLowBitsSet(AP.getBitWidth(), |
| 3928 | AP.getBitWidth() - |
| 3929 | AP.countTrailingZeros())); |
| 3930 | Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask); |
| 3931 | Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask); |
| 3932 | return new ICmpInst(I.getPredicate(), And1, And2); |
| 3933 | } |
| 3934 | } |
| 3935 | break; |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 3936 | case Instruction::UDiv: |
| 3937 | case Instruction::LShr: |
| 3938 | if (I.isSigned()) |
| 3939 | break; |
| 3940 | // fall-through |
| 3941 | case Instruction::SDiv: |
| 3942 | case Instruction::AShr: |
Eli Friedman | 8a20e66 | 2011-05-05 21:59:18 +0000 | [diff] [blame] | 3943 | if (!BO0->isExact() || !BO1->isExact()) |
Nick Lewycky | 9719a71 | 2011-03-05 05:19:11 +0000 | [diff] [blame] | 3944 | break; |
| 3945 | return new ICmpInst(I.getPredicate(), BO0->getOperand(0), |
| 3946 | BO1->getOperand(0)); |
| 3947 | case Instruction::Shl: { |
| 3948 | bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap(); |
| 3949 | bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap(); |
| 3950 | if (!NUW && !NSW) |
| 3951 | break; |
| 3952 | if (!NSW && I.isSigned()) |
| 3953 | break; |
| 3954 | return new ICmpInst(I.getPredicate(), BO0->getOperand(0), |
| 3955 | BO1->getOperand(0)); |
| 3956 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3957 | } |
| 3958 | } |
Sanjoy Das | c86c162 | 2015-08-21 22:22:37 +0000 | [diff] [blame] | 3959 | |
| 3960 | if (BO0) { |
| 3961 | // Transform A & (L - 1) `ult` L --> L != 0 |
| 3962 | auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes()); |
| 3963 | auto BitwiseAnd = |
| 3964 | m_CombineOr(m_And(m_Value(), LSubOne), m_And(LSubOne, m_Value())); |
| 3965 | |
| 3966 | if (match(BO0, BitwiseAnd) && I.getPredicate() == ICmpInst::ICMP_ULT) { |
| 3967 | auto *Zero = Constant::getNullValue(BO0->getType()); |
| 3968 | return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero); |
| 3969 | } |
| 3970 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3971 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3972 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3973 | { Value *A, *B; |
David Majnemer | 1a08acc | 2013-04-12 17:25:07 +0000 | [diff] [blame] | 3974 | // Transform (A & ~B) == 0 --> (A & B) != 0 |
| 3975 | // and (A & ~B) != 0 --> (A & B) == 0 |
| 3976 | // if A is a power of 2. |
| 3977 | if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) && |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 3978 | match(Op1, m_Zero()) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3979 | isKnownToBeAPowerOfTwo(A, DL, false, 0, AC, &I, DT) && I.isEquality()) |
David Majnemer | 1a08acc | 2013-04-12 17:25:07 +0000 | [diff] [blame] | 3980 | return new ICmpInst(I.getInversePredicate(), |
| 3981 | Builder->CreateAnd(A, B), |
| 3982 | Op1); |
| 3983 | |
Chris Lattner | f3c4eef | 2011-01-15 05:41:33 +0000 | [diff] [blame] | 3984 | // ~x < ~y --> y < x |
| 3985 | // ~x < cst --> ~cst < x |
| 3986 | if (match(Op0, m_Not(m_Value(A)))) { |
| 3987 | if (match(Op1, m_Not(m_Value(B)))) |
| 3988 | return new ICmpInst(I.getPredicate(), B, A); |
Chris Lattner | 497459d | 2011-01-15 05:42:47 +0000 | [diff] [blame] | 3989 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) |
Chris Lattner | f3c4eef | 2011-01-15 05:41:33 +0000 | [diff] [blame] | 3990 | return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A); |
| 3991 | } |
Chris Lattner | 5e0c0c7 | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 3992 | |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 3993 | Instruction *AddI = nullptr; |
| 3994 | if (match(&I, m_UAddWithOverflow(m_Value(A), m_Value(B), |
| 3995 | m_Instruction(AddI))) && |
| 3996 | isa<IntegerType>(A->getType())) { |
| 3997 | Value *Result; |
| 3998 | Constant *Overflow; |
| 3999 | if (OptimizeOverflowCheck(OCF_UNSIGNED_ADD, A, B, *AddI, Result, |
| 4000 | Overflow)) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4001 | replaceInstUsesWith(*AddI, Result); |
| 4002 | return replaceInstUsesWith(I, Overflow); |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 4003 | } |
| 4004 | } |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 4005 | |
| 4006 | // (zext a) * (zext b) --> llvm.umul.with.overflow. |
| 4007 | if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { |
| 4008 | if (Instruction *R = ProcessUMulZExtIdiom(I, Op0, Op1, *this)) |
| 4009 | return R; |
| 4010 | } |
| 4011 | if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { |
| 4012 | if (Instruction *R = ProcessUMulZExtIdiom(I, Op1, Op0, *this)) |
| 4013 | return R; |
| 4014 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4015 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4016 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4017 | if (I.isEquality()) { |
| 4018 | Value *A, *B, *C, *D; |
Duncan Sands | 84653b3 | 2011-02-18 16:25:37 +0000 | [diff] [blame] | 4019 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4020 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 4021 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 4022 | Value *OtherVal = A == Op1 ? B : A; |
| 4023 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 4024 | Constant::getNullValue(A->getType())); |
| 4025 | } |
| 4026 | |
| 4027 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 4028 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 4029 | ConstantInt *C1, *C2; |
| 4030 | if (match(B, m_ConstantInt(C1)) && |
| 4031 | match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) { |
Jakub Staszak | bddea11 | 2013-06-06 20:18:46 +0000 | [diff] [blame] | 4032 | Constant *NC = Builder->getInt(C1->getValue() ^ C2->getValue()); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 4033 | Value *Xor = Builder->CreateXor(C, NC); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4034 | return new ICmpInst(I.getPredicate(), A, Xor); |
| 4035 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4036 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4037 | // A^B == A^D -> B == D |
| 4038 | if (A == C) return new ICmpInst(I.getPredicate(), B, D); |
| 4039 | if (A == D) return new ICmpInst(I.getPredicate(), B, C); |
| 4040 | if (B == C) return new ICmpInst(I.getPredicate(), A, D); |
| 4041 | if (B == D) return new ICmpInst(I.getPredicate(), A, C); |
| 4042 | } |
| 4043 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4044 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4045 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
| 4046 | (A == Op0 || B == Op0)) { |
| 4047 | // A == (A^B) -> B == 0 |
| 4048 | Value *OtherVal = A == Op0 ? B : A; |
| 4049 | return new ICmpInst(I.getPredicate(), OtherVal, |
| 4050 | Constant::getNullValue(A->getType())); |
| 4051 | } |
| 4052 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4053 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4054 | if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) && |
Chris Lattner | 31b106d | 2011-04-26 20:02:45 +0000 | [diff] [blame] | 4055 | match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4056 | Value *X = nullptr, *Y = nullptr, *Z = nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4057 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4058 | if (A == C) { |
| 4059 | X = B; Y = D; Z = A; |
| 4060 | } else if (A == D) { |
| 4061 | X = B; Y = C; Z = A; |
| 4062 | } else if (B == C) { |
| 4063 | X = A; Y = D; Z = B; |
| 4064 | } else if (B == D) { |
| 4065 | X = A; Y = C; Z = B; |
| 4066 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4067 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4068 | if (X) { // Build (X^Y) & Z |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 4069 | Op1 = Builder->CreateXor(X, Y); |
| 4070 | Op1 = Builder->CreateAnd(Op1, Z); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4071 | I.setOperand(0, Op1); |
| 4072 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 4073 | return &I; |
| 4074 | } |
| 4075 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4076 | |
Benjamin Kramer | 8b8a769 | 2012-06-10 20:35:00 +0000 | [diff] [blame] | 4077 | // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B) |
Benjamin Kramer | 2150145 | 2012-06-11 08:01:25 +0000 | [diff] [blame] | 4078 | // and (B & (1<<X)-1) == (zext A) --> A == (trunc B) |
Benjamin Kramer | 8b8a769 | 2012-06-10 20:35:00 +0000 | [diff] [blame] | 4079 | ConstantInt *Cst1; |
Benjamin Kramer | 2150145 | 2012-06-11 08:01:25 +0000 | [diff] [blame] | 4080 | if ((Op0->hasOneUse() && |
| 4081 | match(Op0, m_ZExt(m_Value(A))) && |
| 4082 | match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) || |
| 4083 | (Op1->hasOneUse() && |
| 4084 | match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) && |
| 4085 | match(Op1, m_ZExt(m_Value(A))))) { |
Benjamin Kramer | 8b8a769 | 2012-06-10 20:35:00 +0000 | [diff] [blame] | 4086 | APInt Pow2 = Cst1->getValue() + 1; |
| 4087 | if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) && |
| 4088 | Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth()) |
| 4089 | return new ICmpInst(I.getPredicate(), A, |
| 4090 | Builder->CreateTrunc(B, A->getType())); |
| 4091 | } |
| 4092 | |
Benjamin Kramer | 03f3e24 | 2013-11-16 16:00:48 +0000 | [diff] [blame] | 4093 | // (A >> C) == (B >> C) --> (A^B) u< (1 << C) |
| 4094 | // For lshr and ashr pairs. |
| 4095 | if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_ConstantInt(Cst1)))) && |
| 4096 | match(Op1, m_OneUse(m_LShr(m_Value(B), m_Specific(Cst1))))) || |
| 4097 | (match(Op0, m_OneUse(m_AShr(m_Value(A), m_ConstantInt(Cst1)))) && |
| 4098 | match(Op1, m_OneUse(m_AShr(m_Value(B), m_Specific(Cst1)))))) { |
| 4099 | unsigned TypeBits = Cst1->getBitWidth(); |
| 4100 | unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits); |
| 4101 | if (ShAmt < TypeBits && ShAmt != 0) { |
| 4102 | ICmpInst::Predicate Pred = I.getPredicate() == ICmpInst::ICMP_NE |
| 4103 | ? ICmpInst::ICMP_UGE |
| 4104 | : ICmpInst::ICMP_ULT; |
| 4105 | Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted"); |
| 4106 | APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt); |
| 4107 | return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal)); |
| 4108 | } |
| 4109 | } |
| 4110 | |
Benjamin Kramer | 7fa8c43 | 2015-03-26 17:12:06 +0000 | [diff] [blame] | 4111 | // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0 |
| 4112 | if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) && |
| 4113 | match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) { |
| 4114 | unsigned TypeBits = Cst1->getBitWidth(); |
| 4115 | unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits); |
| 4116 | if (ShAmt < TypeBits && ShAmt != 0) { |
| 4117 | Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted"); |
| 4118 | APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt); |
| 4119 | Value *And = Builder->CreateAnd(Xor, Builder->getInt(AndVal), |
| 4120 | I.getName() + ".mask"); |
| 4121 | return new ICmpInst(I.getPredicate(), And, |
| 4122 | Constant::getNullValue(Cst1->getType())); |
| 4123 | } |
| 4124 | } |
| 4125 | |
Chris Lattner | 1b06c71 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 4126 | // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to |
| 4127 | // "icmp (and X, mask), cst" |
| 4128 | uint64_t ShAmt = 0; |
Chris Lattner | 1b06c71 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 4129 | if (Op0->hasOneUse() && |
| 4130 | match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), |
| 4131 | m_ConstantInt(ShAmt))))) && |
| 4132 | match(Op1, m_ConstantInt(Cst1)) && |
| 4133 | // Only do this when A has multiple uses. This is most important to do |
| 4134 | // when it exposes other optimizations. |
| 4135 | !A->hasOneUse()) { |
| 4136 | unsigned ASize =cast<IntegerType>(A->getType())->getPrimitiveSizeInBits(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4137 | |
Chris Lattner | 1b06c71 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 4138 | if (ShAmt < ASize) { |
| 4139 | APInt MaskV = |
| 4140 | APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits()); |
| 4141 | MaskV <<= ShAmt; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4142 | |
Chris Lattner | 1b06c71 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 4143 | APInt CmpV = Cst1->getValue().zext(ASize); |
| 4144 | CmpV <<= ShAmt; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4145 | |
Chris Lattner | 1b06c71 | 2011-04-26 20:18:20 +0000 | [diff] [blame] | 4146 | Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV)); |
| 4147 | return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV)); |
| 4148 | } |
| 4149 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4150 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4151 | |
David Majnemer | c1eca5a | 2014-11-06 23:23:30 +0000 | [diff] [blame] | 4152 | // The 'cmpxchg' instruction returns an aggregate containing the old value and |
| 4153 | // an i1 which indicates whether or not we successfully did the swap. |
| 4154 | // |
| 4155 | // Replace comparisons between the old value and the expected value with the |
| 4156 | // indicator that 'cmpxchg' returns. |
| 4157 | // |
| 4158 | // N.B. This transform is only valid when the 'cmpxchg' is not permitted to |
| 4159 | // spuriously fail. In those cases, the old value may equal the expected |
| 4160 | // value but it is possible for the swap to not occur. |
| 4161 | if (I.getPredicate() == ICmpInst::ICMP_EQ) |
| 4162 | if (auto *EVI = dyn_cast<ExtractValueInst>(Op0)) |
| 4163 | if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand())) |
| 4164 | if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 && |
| 4165 | !ACXI->isWeak()) |
| 4166 | return ExtractValueInst::Create(ACXI, 1); |
| 4167 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4168 | { |
| 4169 | Value *X; ConstantInt *Cst; |
| 4170 | // icmp X+Cst, X |
| 4171 | if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X) |
Benjamin Kramer | 0e2d162 | 2013-09-20 22:12:42 +0000 | [diff] [blame] | 4172 | return FoldICmpAddOpCst(I, X, Cst, I.getPredicate()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4173 | |
| 4174 | // icmp X, X+Cst |
| 4175 | if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X) |
Benjamin Kramer | 0e2d162 | 2013-09-20 22:12:42 +0000 | [diff] [blame] | 4176 | return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4177 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4178 | return Changed ? &I : nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4181 | /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4182 | Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, |
| 4183 | Instruction *LHSI, |
| 4184 | Constant *RHSC) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4185 | if (!isa<ConstantFP>(RHSC)) return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4186 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4187 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4188 | // Get the width of the mantissa. We don't want to hack on conversions that |
| 4189 | // might lose information from the integer, e.g. "i64 -> float" |
| 4190 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4191 | if (MantissaWidth == -1) return nullptr; // Unknown. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4192 | |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4193 | IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); |
| 4194 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4195 | bool LHSUnsigned = isa<UIToFPInst>(LHSI); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4196 | |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4197 | if (I.isEquality()) { |
| 4198 | FCmpInst::Predicate P = I.getPredicate(); |
| 4199 | bool IsExact = false; |
| 4200 | APSInt RHSCvt(IntTy->getBitWidth(), LHSUnsigned); |
| 4201 | RHS.convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact); |
| 4202 | |
| 4203 | // If the floating point constant isn't an integer value, we know if we will |
| 4204 | // ever compare equal / not equal to it. |
| 4205 | if (!IsExact) { |
| 4206 | // TODO: Can never be -0.0 and other non-representable values |
| 4207 | APFloat RHSRoundInt(RHS); |
| 4208 | RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven); |
| 4209 | if (RHS.compare(RHSRoundInt) != APFloat::cmpEqual) { |
| 4210 | if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4211 | return replaceInstUsesWith(I, Builder->getFalse()); |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4212 | |
| 4213 | assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4214 | return replaceInstUsesWith(I, Builder->getTrue()); |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | // TODO: If the constant is exactly representable, is it always OK to do |
| 4219 | // equality compares as integer? |
| 4220 | } |
| 4221 | |
Arch D. Robison | 8ed0854 | 2015-09-15 17:51:59 +0000 | [diff] [blame] | 4222 | // Check to see that the input is converted from an integer type that is small |
| 4223 | // enough that preserves all bits. TODO: check here for "known" sign bits. |
| 4224 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. |
| 4225 | unsigned InputSize = IntTy->getScalarSizeInBits(); |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4226 | |
Arch D. Robison | 8ed0854 | 2015-09-15 17:51:59 +0000 | [diff] [blame] | 4227 | // Following test does NOT adjust InputSize downwards for signed inputs, |
| 4228 | // because the most negative value still requires all the mantissa bits |
| 4229 | // to distinguish it from one less than that value. |
| 4230 | if ((int)InputSize > MantissaWidth) { |
| 4231 | // Conversion would lose accuracy. Check if loss can impact comparison. |
| 4232 | int Exp = ilogb(RHS); |
| 4233 | if (Exp == APFloat::IEK_Inf) { |
| 4234 | int MaxExponent = ilogb(APFloat::getLargest(RHS.getSemantics())); |
| 4235 | if (MaxExponent < (int)InputSize - !LHSUnsigned) |
| 4236 | // Conversion could create infinity. |
| 4237 | return nullptr; |
| 4238 | } else { |
| 4239 | // Note that if RHS is zero or NaN, then Exp is negative |
| 4240 | // and first condition is trivially false. |
| 4241 | if (MantissaWidth <= Exp && Exp <= (int)InputSize - !LHSUnsigned) |
| 4242 | // Conversion could affect comparison. |
| 4243 | return nullptr; |
| 4244 | } |
| 4245 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4246 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4247 | // Otherwise, we can potentially simplify the comparison. We know that it |
| 4248 | // will always come through as an integer value and we know the constant is |
| 4249 | // not a NAN (it would have been previously simplified). |
| 4250 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4251 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4252 | ICmpInst::Predicate Pred; |
| 4253 | switch (I.getPredicate()) { |
| 4254 | default: llvm_unreachable("Unexpected predicate!"); |
| 4255 | case FCmpInst::FCMP_UEQ: |
| 4256 | case FCmpInst::FCMP_OEQ: |
| 4257 | Pred = ICmpInst::ICMP_EQ; |
| 4258 | break; |
| 4259 | case FCmpInst::FCMP_UGT: |
| 4260 | case FCmpInst::FCMP_OGT: |
| 4261 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT; |
| 4262 | break; |
| 4263 | case FCmpInst::FCMP_UGE: |
| 4264 | case FCmpInst::FCMP_OGE: |
| 4265 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE; |
| 4266 | break; |
| 4267 | case FCmpInst::FCMP_ULT: |
| 4268 | case FCmpInst::FCMP_OLT: |
| 4269 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT; |
| 4270 | break; |
| 4271 | case FCmpInst::FCMP_ULE: |
| 4272 | case FCmpInst::FCMP_OLE: |
| 4273 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE; |
| 4274 | break; |
| 4275 | case FCmpInst::FCMP_UNE: |
| 4276 | case FCmpInst::FCMP_ONE: |
| 4277 | Pred = ICmpInst::ICMP_NE; |
| 4278 | break; |
| 4279 | case FCmpInst::FCMP_ORD: |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4280 | return replaceInstUsesWith(I, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4281 | case FCmpInst::FCMP_UNO: |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4282 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4283 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4284 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4285 | // Now we know that the APFloat is a normal number, zero or inf. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4286 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4287 | // See if the FP constant is too large for the integer. For example, |
| 4288 | // comparing an i8 to 300.0. |
| 4289 | unsigned IntWidth = IntTy->getScalarSizeInBits(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4290 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4291 | if (!LHSUnsigned) { |
| 4292 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF |
| 4293 | // and large values. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4294 | APFloat SMax(RHS.getSemantics()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4295 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, |
| 4296 | APFloat::rmNearestTiesToEven); |
| 4297 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 |
| 4298 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
| 4299 | Pred == ICmpInst::ICMP_SLE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4300 | return replaceInstUsesWith(I, Builder->getTrue()); |
| 4301 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4302 | } |
| 4303 | } else { |
| 4304 | // If the RHS value is > UnsignedMax, fold the comparison. This handles |
| 4305 | // +INF and large values. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4306 | APFloat UMax(RHS.getSemantics()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4307 | UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false, |
| 4308 | APFloat::rmNearestTiesToEven); |
| 4309 | if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 |
| 4310 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || |
| 4311 | Pred == ICmpInst::ICMP_ULE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4312 | return replaceInstUsesWith(I, Builder->getTrue()); |
| 4313 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4314 | } |
| 4315 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4316 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4317 | if (!LHSUnsigned) { |
| 4318 | // See if the RHS value is < SignedMin. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4319 | APFloat SMin(RHS.getSemantics()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4320 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, |
| 4321 | APFloat::rmNearestTiesToEven); |
| 4322 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 |
| 4323 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
| 4324 | Pred == ICmpInst::ICMP_SGE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4325 | return replaceInstUsesWith(I, Builder->getTrue()); |
| 4326 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4327 | } |
Devang Patel | 698452b | 2012-02-13 23:05:18 +0000 | [diff] [blame] | 4328 | } else { |
| 4329 | // See if the RHS value is < UnsignedMin. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4330 | APFloat SMin(RHS.getSemantics()); |
Devang Patel | 698452b | 2012-02-13 23:05:18 +0000 | [diff] [blame] | 4331 | SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true, |
| 4332 | APFloat::rmNearestTiesToEven); |
| 4333 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0 |
| 4334 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT || |
| 4335 | Pred == ICmpInst::ICMP_UGE) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4336 | return replaceInstUsesWith(I, Builder->getTrue()); |
| 4337 | return replaceInstUsesWith(I, Builder->getFalse()); |
Devang Patel | 698452b | 2012-02-13 23:05:18 +0000 | [diff] [blame] | 4338 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or |
| 4342 | // [0, UMAX], but it may still be fractional. See if it is fractional by |
| 4343 | // casting the FP value to the integer value and back, checking for equality. |
| 4344 | // Don't do this for zero, because -0.0 is not fractional. |
| 4345 | Constant *RHSInt = LHSUnsigned |
| 4346 | ? ConstantExpr::getFPToUI(RHSC, IntTy) |
| 4347 | : ConstantExpr::getFPToSI(RHSC, IntTy); |
| 4348 | if (!RHS.isZero()) { |
| 4349 | bool Equal = LHSUnsigned |
| 4350 | ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC |
| 4351 | : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC; |
| 4352 | if (!Equal) { |
| 4353 | // If we had a comparison against a fractional value, we have to adjust |
| 4354 | // the compare predicate and sometimes the value. RHSC is rounded towards |
| 4355 | // zero at this point. |
| 4356 | switch (Pred) { |
| 4357 | default: llvm_unreachable("Unexpected integer comparison!"); |
| 4358 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4359 | return replaceInstUsesWith(I, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4360 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4361 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4362 | case ICmpInst::ICMP_ULE: |
| 4363 | // (float)int <= 4.4 --> int <= 4 |
| 4364 | // (float)int <= -4.4 --> false |
| 4365 | if (RHS.isNegative()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4366 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4367 | break; |
| 4368 | case ICmpInst::ICMP_SLE: |
| 4369 | // (float)int <= 4.4 --> int <= 4 |
| 4370 | // (float)int <= -4.4 --> int < -4 |
| 4371 | if (RHS.isNegative()) |
| 4372 | Pred = ICmpInst::ICMP_SLT; |
| 4373 | break; |
| 4374 | case ICmpInst::ICMP_ULT: |
| 4375 | // (float)int < -4.4 --> false |
| 4376 | // (float)int < 4.4 --> int <= 4 |
| 4377 | if (RHS.isNegative()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4378 | return replaceInstUsesWith(I, Builder->getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4379 | Pred = ICmpInst::ICMP_ULE; |
| 4380 | break; |
| 4381 | case ICmpInst::ICMP_SLT: |
| 4382 | // (float)int < -4.4 --> int < -4 |
| 4383 | // (float)int < 4.4 --> int <= 4 |
| 4384 | if (!RHS.isNegative()) |
| 4385 | Pred = ICmpInst::ICMP_SLE; |
| 4386 | break; |
| 4387 | case ICmpInst::ICMP_UGT: |
| 4388 | // (float)int > 4.4 --> int > 4 |
| 4389 | // (float)int > -4.4 --> true |
| 4390 | if (RHS.isNegative()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4391 | return replaceInstUsesWith(I, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4392 | break; |
| 4393 | case ICmpInst::ICMP_SGT: |
| 4394 | // (float)int > 4.4 --> int > 4 |
| 4395 | // (float)int > -4.4 --> int >= -4 |
| 4396 | if (RHS.isNegative()) |
| 4397 | Pred = ICmpInst::ICMP_SGE; |
| 4398 | break; |
| 4399 | case ICmpInst::ICMP_UGE: |
| 4400 | // (float)int >= -4.4 --> true |
| 4401 | // (float)int >= 4.4 --> int > 4 |
Bob Wilson | 61f3ad5 | 2012-08-07 22:35:16 +0000 | [diff] [blame] | 4402 | if (RHS.isNegative()) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4403 | return replaceInstUsesWith(I, Builder->getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4404 | Pred = ICmpInst::ICMP_UGT; |
| 4405 | break; |
| 4406 | case ICmpInst::ICMP_SGE: |
| 4407 | // (float)int >= -4.4 --> int >= -4 |
| 4408 | // (float)int >= 4.4 --> int > 4 |
| 4409 | if (!RHS.isNegative()) |
| 4410 | Pred = ICmpInst::ICMP_SGT; |
| 4411 | break; |
| 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | // Lower this FP comparison into an appropriate integer version of the |
| 4417 | // comparison. |
| 4418 | return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt); |
| 4419 | } |
| 4420 | |
| 4421 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4422 | bool Changed = false; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4423 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4424 | /// Orders the operands of the compare so that they are listed from most |
| 4425 | /// complex to least complex. This puts constants before unary operators, |
| 4426 | /// before binary operators. |
| 4427 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) { |
| 4428 | I.swapOperands(); |
| 4429 | Changed = true; |
| 4430 | } |
| 4431 | |
| 4432 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4433 | |
Benjamin Kramer | f4ebfa3 | 2015-07-10 14:02:02 +0000 | [diff] [blame] | 4434 | if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1, |
| 4435 | I.getFastMathFlags(), DL, TLI, DT, AC, &I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4436 | return replaceInstUsesWith(I, V); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4437 | |
| 4438 | // Simplify 'fcmp pred X, X' |
| 4439 | if (Op0 == Op1) { |
| 4440 | switch (I.getPredicate()) { |
| 4441 | default: llvm_unreachable("Unknown predicate!"); |
| 4442 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4443 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4444 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4445 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4446 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4447 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4448 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4449 | return &I; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4450 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4451 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4452 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4453 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4454 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4455 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4456 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4457 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4458 | return &I; |
| 4459 | } |
| 4460 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4461 | |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 4462 | // Test if the FCmpInst instruction is used exclusively by a select as |
| 4463 | // part of a minimum or maximum operation. If so, refrain from doing |
| 4464 | // any other folding. This helps out other analyses which understand |
| 4465 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 4466 | // and CodeGen. And in this case, at least one of the comparison |
| 4467 | // operands has at least one user besides the compare (the select), |
| 4468 | // which would often largely negate the benefit of folding anyway. |
| 4469 | if (I.hasOneUse()) |
| 4470 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin())) |
| 4471 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 4472 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
| 4473 | return nullptr; |
| 4474 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4475 | // Handle fcmp with constant RHS |
| 4476 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4477 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4478 | switch (LHSI->getOpcode()) { |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 4479 | case Instruction::FPExt: { |
| 4480 | // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless |
| 4481 | FPExtInst *LHSExt = cast<FPExtInst>(LHSI); |
| 4482 | ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC); |
| 4483 | if (!RHSF) |
| 4484 | break; |
| 4485 | |
| 4486 | const fltSemantics *Sem; |
| 4487 | // FIXME: This shouldn't be here. |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 4488 | if (LHSExt->getSrcTy()->isHalfTy()) |
| 4489 | Sem = &APFloat::IEEEhalf; |
| 4490 | else if (LHSExt->getSrcTy()->isFloatTy()) |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 4491 | Sem = &APFloat::IEEEsingle; |
| 4492 | else if (LHSExt->getSrcTy()->isDoubleTy()) |
| 4493 | Sem = &APFloat::IEEEdouble; |
| 4494 | else if (LHSExt->getSrcTy()->isFP128Ty()) |
| 4495 | Sem = &APFloat::IEEEquad; |
| 4496 | else if (LHSExt->getSrcTy()->isX86_FP80Ty()) |
| 4497 | Sem = &APFloat::x87DoubleExtended; |
Ulrich Weigand | 6a9bb51 | 2012-10-30 12:33:18 +0000 | [diff] [blame] | 4498 | else if (LHSExt->getSrcTy()->isPPC_FP128Ty()) |
| 4499 | Sem = &APFloat::PPCDoubleDouble; |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 4500 | else |
| 4501 | break; |
| 4502 | |
| 4503 | bool Lossy; |
| 4504 | APFloat F = RHSF->getValueAPF(); |
| 4505 | F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy); |
| 4506 | |
Jim Grosbach | 24ff834 | 2011-09-30 18:45:50 +0000 | [diff] [blame] | 4507 | // Avoid lossy conversions and denormals. Zero is a special case |
| 4508 | // that's OK to convert. |
Jim Grosbach | 011dafb | 2011-09-30 19:58:46 +0000 | [diff] [blame] | 4509 | APFloat Fabs = F; |
| 4510 | Fabs.clearSign(); |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 4511 | if (!Lossy && |
Jim Grosbach | 011dafb | 2011-09-30 19:58:46 +0000 | [diff] [blame] | 4512 | ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) != |
| 4513 | APFloat::cmpLessThan) || Fabs.isZero())) |
Jim Grosbach | 24ff834 | 2011-09-30 18:45:50 +0000 | [diff] [blame] | 4514 | |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 4515 | return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), |
| 4516 | ConstantFP::get(RHSC->getContext(), F)); |
| 4517 | break; |
| 4518 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4519 | case Instruction::PHI: |
| 4520 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
| 4521 | // block. If in the same block, we're encouraging jump threading. If |
| 4522 | // not, we are just pessimizing the code by making an i1 phi. |
| 4523 | if (LHSI->getParent() == I.getParent()) |
Chris Lattner | ea7131a | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 4524 | if (Instruction *NV = FoldOpIntoPhi(I)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4525 | return NV; |
| 4526 | break; |
| 4527 | case Instruction::SIToFP: |
| 4528 | case Instruction::UIToFP: |
| 4529 | if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC)) |
| 4530 | return NV; |
| 4531 | break; |
Benjamin Kramer | a8c5d08 | 2011-03-31 10:12:15 +0000 | [diff] [blame] | 4532 | case Instruction::FSub: { |
| 4533 | // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C |
| 4534 | Value *Op; |
| 4535 | if (match(LHSI, m_FNeg(m_Value(Op)))) |
| 4536 | return new FCmpInst(I.getSwappedPredicate(), Op, |
| 4537 | ConstantExpr::getFNeg(RHSC)); |
| 4538 | break; |
| 4539 | } |
Dan Gohman | 9473202 | 2010-02-24 06:46:09 +0000 | [diff] [blame] | 4540 | case Instruction::Load: |
| 4541 | if (GetElementPtrInst *GEP = |
| 4542 | dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) { |
| 4543 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 4544 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 4545 | !cast<LoadInst>(LHSI)->isVolatile()) |
| 4546 | if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I)) |
| 4547 | return Res; |
| 4548 | } |
| 4549 | break; |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4550 | case Instruction::Call: { |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4551 | if (!RHSC->isNullValue()) |
| 4552 | break; |
| 4553 | |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4554 | CallInst *CI = cast<CallInst>(LHSI); |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4555 | const Function *F = CI->getCalledFunction(); |
| 4556 | if (!F) |
| 4557 | break; |
| 4558 | |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4559 | // Various optimization for fabs compared with zero. |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4560 | LibFunc::Func Func; |
| 4561 | if (F->getIntrinsicID() == Intrinsic::fabs || |
| 4562 | (TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) && |
| 4563 | (Func == LibFunc::fabs || Func == LibFunc::fabsf || |
| 4564 | Func == LibFunc::fabsl))) { |
| 4565 | switch (I.getPredicate()) { |
| 4566 | default: |
| 4567 | break; |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4568 | // fabs(x) < 0 --> false |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4569 | case FCmpInst::FCMP_OLT: |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4570 | return replaceInstUsesWith(I, Builder->getFalse()); |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4571 | // fabs(x) > 0 --> x != 0 |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4572 | case FCmpInst::FCMP_OGT: |
| 4573 | return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0), RHSC); |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4574 | // fabs(x) <= 0 --> x == 0 |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4575 | case FCmpInst::FCMP_OLE: |
| 4576 | return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0), RHSC); |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4577 | // fabs(x) >= 0 --> !isnan(x) |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4578 | case FCmpInst::FCMP_OGE: |
| 4579 | return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0), RHSC); |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4580 | // fabs(x) == 0 --> x == 0 |
| 4581 | // fabs(x) != 0 --> x != 0 |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 4582 | case FCmpInst::FCMP_OEQ: |
| 4583 | case FCmpInst::FCMP_UEQ: |
| 4584 | case FCmpInst::FCMP_ONE: |
| 4585 | case FCmpInst::FCMP_UNE: |
| 4586 | return new FCmpInst(I.getPredicate(), CI->getArgOperand(0), RHSC); |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 4587 | } |
| 4588 | } |
| 4589 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4590 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
Benjamin Kramer | be209ab | 2011-03-31 10:46:03 +0000 | [diff] [blame] | 4593 | // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y |
Benjamin Kramer | d159d94 | 2011-03-31 10:12:22 +0000 | [diff] [blame] | 4594 | Value *X, *Y; |
| 4595 | if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) |
Benjamin Kramer | be209ab | 2011-03-31 10:46:03 +0000 | [diff] [blame] | 4596 | return new FCmpInst(I.getSwappedPredicate(), X, Y); |
Benjamin Kramer | d159d94 | 2011-03-31 10:12:22 +0000 | [diff] [blame] | 4597 | |
Benjamin Kramer | 2ccfbc8 | 2011-03-31 10:11:58 +0000 | [diff] [blame] | 4598 | // fcmp (fpext x), (fpext y) -> fcmp x, y |
| 4599 | if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) |
| 4600 | if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) |
| 4601 | if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) |
| 4602 | return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0), |
| 4603 | RHSExt->getOperand(0)); |
| 4604 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4605 | return Changed ? &I : nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4606 | } |