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