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" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 29 | #include "llvm/Support/KnownBits.h" |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | using namespace PatternMatch; |
| 33 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "instcombine" |
| 35 | |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 36 | // How many times is a select replaced by one of its operands? |
| 37 | STATISTIC(NumSel, "Number of select opts"); |
| 38 | |
Chris Lattner | 9845710 | 2011-02-10 05:23:05 +0000 | [diff] [blame] | 39 | |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 40 | static ConstantInt *extractElement(Constant *V, Constant *Idx) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 41 | return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx)); |
| 42 | } |
| 43 | |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 44 | static bool hasAddOverflow(ConstantInt *Result, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 45 | ConstantInt *In1, ConstantInt *In2, |
| 46 | bool IsSigned) { |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 47 | if (!IsSigned) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 48 | return Result->getValue().ult(In1->getValue()); |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 49 | |
| 50 | if (In2->isNegative()) |
| 51 | return Result->getValue().sgt(In1->getValue()); |
| 52 | return Result->getValue().slt(In1->getValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 55 | /// Compute Result = In1+In2, returning true if the result overflowed for this |
| 56 | /// type. |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 57 | static bool addWithOverflow(Constant *&Result, Constant *In1, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 58 | Constant *In2, bool IsSigned = false) { |
| 59 | Result = ConstantExpr::getAdd(In1, In2); |
| 60 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 61 | if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 62 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 63 | Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i); |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 64 | if (hasAddOverflow(extractElement(Result, Idx), |
| 65 | extractElement(In1, Idx), |
| 66 | extractElement(In2, Idx), |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 67 | IsSigned)) |
| 68 | return true; |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 73 | return hasAddOverflow(cast<ConstantInt>(Result), |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 74 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 75 | IsSigned); |
| 76 | } |
| 77 | |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 78 | static bool hasSubOverflow(ConstantInt *Result, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 79 | ConstantInt *In1, ConstantInt *In2, |
| 80 | bool IsSigned) { |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 81 | if (!IsSigned) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 82 | return Result->getValue().ugt(In1->getValue()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 83 | |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 84 | if (In2->isNegative()) |
| 85 | return Result->getValue().slt(In1->getValue()); |
| 86 | |
| 87 | return Result->getValue().sgt(In1->getValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 90 | /// Compute Result = In1-In2, returning true if the result overflowed for this |
| 91 | /// type. |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 92 | static bool subWithOverflow(Constant *&Result, Constant *In1, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 93 | Constant *In2, bool IsSigned = false) { |
| 94 | Result = ConstantExpr::getSub(In1, In2); |
| 95 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 96 | if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 97 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| 98 | Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i); |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 99 | if (hasSubOverflow(extractElement(Result, Idx), |
| 100 | extractElement(In1, Idx), |
| 101 | extractElement(In2, Idx), |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 102 | IsSigned)) |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 108 | return hasSubOverflow(cast<ConstantInt>(Result), |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 109 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 110 | IsSigned); |
| 111 | } |
| 112 | |
Balaram Makam | 569eaec | 2016-05-04 21:32:14 +0000 | [diff] [blame] | 113 | /// Given an icmp instruction, return true if any use of this comparison is a |
| 114 | /// branch on sign bit comparison. |
Eric Christopher | 710c1c8 | 2017-06-30 01:35:31 +0000 | [diff] [blame] | 115 | static bool hasBranchUse(ICmpInst &I) { |
Balaram Makam | 569eaec | 2016-05-04 21:32:14 +0000 | [diff] [blame] | 116 | for (auto *U : I.users()) |
| 117 | if (isa<BranchInst>(U)) |
Eric Christopher | 710c1c8 | 2017-06-30 01:35:31 +0000 | [diff] [blame] | 118 | return true; |
Balaram Makam | 569eaec | 2016-05-04 21:32:14 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 122 | /// Given an exploded icmp instruction, return true if the comparison only |
| 123 | /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if the |
| 124 | /// result of the comparison is true when the input value is signed. |
Sanjay Patel | 7926366 | 2016-08-21 15:07:45 +0000 | [diff] [blame] | 125 | static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 126 | bool &TrueIfSigned) { |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 127 | switch (Pred) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 128 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 129 | TrueIfSigned = true; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 130 | return RHS.isNullValue(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 131 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 132 | TrueIfSigned = true; |
Sanjay Patel | 7926366 | 2016-08-21 15:07:45 +0000 | [diff] [blame] | 133 | return RHS.isAllOnesValue(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 134 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 135 | TrueIfSigned = false; |
Sanjay Patel | 7926366 | 2016-08-21 15:07:45 +0000 | [diff] [blame] | 136 | return RHS.isAllOnesValue(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 137 | case ICmpInst::ICMP_UGT: |
| 138 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 139 | TrueIfSigned = true; |
Sanjay Patel | 7926366 | 2016-08-21 15:07:45 +0000 | [diff] [blame] | 140 | return RHS.isMaxSignedValue(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 141 | case ICmpInst::ICMP_UGE: |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 142 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 143 | TrueIfSigned = true; |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 144 | return RHS.isSignMask(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 145 | default: |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 150 | /// Returns true if the exploded icmp can be expressed as a signed comparison |
| 151 | /// to zero and updates the predicate accordingly. |
| 152 | /// The signedness of the comparison is preserved. |
Sanjay Patel | 5b11284 | 2016-08-18 14:59:14 +0000 | [diff] [blame] | 153 | /// TODO: Refactor with decomposeBitTestICmp()? |
| 154 | static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) { |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 155 | if (!ICmpInst::isSigned(Pred)) |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 156 | return false; |
| 157 | |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 158 | if (C.isNullValue()) |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 159 | return ICmpInst::isRelational(Pred); |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 160 | |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 161 | if (C.isOneValue()) { |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 162 | if (Pred == ICmpInst::ICMP_SLT) { |
| 163 | Pred = ICmpInst::ICMP_SLE; |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 164 | return true; |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 165 | } |
Sanjay Patel | 5b11284 | 2016-08-18 14:59:14 +0000 | [diff] [blame] | 166 | } else if (C.isAllOnesValue()) { |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 167 | if (Pred == ICmpInst::ICMP_SGT) { |
| 168 | Pred = ICmpInst::ICMP_SGE; |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 169 | return true; |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 170 | } |
Arnaud A. de Grandmaison | 3ee88e8 | 2013-03-25 11:47:38 +0000 | [diff] [blame] | 171 | } |
Arnaud A. de Grandmaison | 9c383d6 | 2013-03-25 09:48:49 +0000 | [diff] [blame] | 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 176 | /// Given a signed integer type and a set of known zero and one bits, compute |
| 177 | /// the maximum and minimum values that could have the specified known zero and |
| 178 | /// known one bits, returning them in Min/Max. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 179 | /// TODO: Move to method on KnownBits struct? |
| 180 | static void computeSignedMinMaxValuesFromKnownBits(const KnownBits &Known, |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 181 | APInt &Min, APInt &Max) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 182 | assert(Known.getBitWidth() == Min.getBitWidth() && |
| 183 | Known.getBitWidth() == Max.getBitWidth() && |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 184 | "KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 185 | APInt UnknownBits = ~(Known.Zero|Known.One); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 186 | |
| 187 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 188 | // bit if it is unknown. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 189 | Min = Known.One; |
| 190 | Max = Known.One|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 |
Craig Topper | 24db6b8 | 2017-04-28 16:58:05 +0000 | [diff] [blame] | 193 | Min.setSignBit(); |
| 194 | Max.clearSignBit(); |
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. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 201 | /// TODO: Move to method on KnownBits struct? |
| 202 | static void computeUnsignedMinMaxValuesFromKnownBits(const KnownBits &Known, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 203 | APInt &Min, APInt &Max) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 204 | assert(Known.getBitWidth() == Min.getBitWidth() && |
| 205 | Known.getBitWidth() == Max.getBitWidth() && |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 206 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 207 | APInt UnknownBits = ~(Known.Zero|Known.One); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 209 | // The minimum value is when the unknown bits are all zeros. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 210 | Min = Known.One; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 211 | // The maximum value is when the unknown bits are all ones. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 212 | Max = Known.One|UnknownBits; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 215 | /// This is called when we see this pattern: |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 216 | /// cmp pred (load (gep GV, ...)), cmpcst |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 217 | /// where GV is a global variable with a constant initializer. Try to simplify |
| 218 | /// 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] | 219 | /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3". |
| 220 | /// |
| 221 | /// 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] | 222 | /// before doing the comparison. This handles cases like "A[i]&4 == 0". |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 223 | Instruction *InstCombiner::foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, |
| 224 | GlobalVariable *GV, |
| 225 | CmpInst &ICI, |
| 226 | ConstantInt *AndCst) { |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 227 | Constant *Init = GV->getInitializer(); |
| 228 | if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 229 | return nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 230 | |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 231 | uint64_t ArrayElementCount = Init->getType()->getArrayNumElements(); |
Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 232 | // Don't blow up on huge arrays. |
| 233 | if (ArrayElementCount > MaxArraySizeForCombine) |
| 234 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 236 | // There are many forms of this optimization we can handle, for now, just do |
| 237 | // the simple index into a single-dimensional array. |
| 238 | // |
| 239 | // Require: GEP GV, 0, i {{, constant indices}} |
| 240 | if (GEP->getNumOperands() < 3 || |
| 241 | !isa<ConstantInt>(GEP->getOperand(1)) || |
| 242 | !cast<ConstantInt>(GEP->getOperand(1))->isZero() || |
| 243 | isa<Constant>(GEP->getOperand(2))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 244 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 245 | |
| 246 | // Check that indices after the variable are constants and in-range for the |
| 247 | // type they index. Collect the indices. This is typically for arrays of |
| 248 | // structs. |
| 249 | SmallVector<unsigned, 4> LaterIndices; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 250 | |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 251 | Type *EltTy = Init->getType()->getArrayElementType(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 252 | for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) { |
| 253 | ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 254 | if (!Idx) return nullptr; // Variable index. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 256 | uint64_t IdxVal = Idx->getZExtValue(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 257 | if ((unsigned)IdxVal != IdxVal) return nullptr; // Too large array index. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 259 | if (StructType *STy = dyn_cast<StructType>(EltTy)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 260 | EltTy = STy->getElementType(IdxVal); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 261 | else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 262 | if (IdxVal >= ATy->getNumElements()) return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 263 | EltTy = ATy->getElementType(); |
| 264 | } else { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 265 | return nullptr; // Unknown type. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 266 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 268 | LaterIndices.push_back(IdxVal); |
| 269 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 271 | enum { Overdefined = -3, Undefined = -2 }; |
| 272 | |
| 273 | // Variables for our state machines. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 275 | // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form |
| 276 | // "i == 47 | i == 87", where 47 is the first index the condition is true for, |
| 277 | // and 87 is the second (and last) index. FirstTrueElement is -2 when |
| 278 | // undefined, otherwise set to the first true element. SecondTrueElement is |
| 279 | // -2 when undefined, -3 when overdefined and >= 0 when that index is true. |
| 280 | int FirstTrueElement = Undefined, SecondTrueElement = Undefined; |
| 281 | |
| 282 | // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the |
| 283 | // form "i != 47 & i != 87". Same state transitions as for true elements. |
| 284 | int FirstFalseElement = Undefined, SecondFalseElement = Undefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 286 | /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these |
| 287 | /// define a state machine that triggers for ranges of values that the index |
| 288 | /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'. |
| 289 | /// This is -2 when undefined, -3 when overdefined, and otherwise the last |
| 290 | /// index in the range (inclusive). We use -2 for undefined here because we |
| 291 | /// use relative comparisons and don't want 0-1 to match -1. |
| 292 | int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 294 | // MagicBitvector - This is a magic bitvector where we set a bit if the |
| 295 | // comparison is true for element 'i'. If there are 64 elements or less in |
| 296 | // the array, this will fully represent all the comparison results. |
| 297 | uint64_t MagicBitvector = 0; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 299 | // Scan the array and see if one of our patterns matches. |
| 300 | Constant *CompareRHS = cast<Constant>(ICI.getOperand(1)); |
Chris Lattner | fe74176 | 2012-01-31 02:55:06 +0000 | [diff] [blame] | 301 | for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) { |
| 302 | Constant *Elt = Init->getAggregateElement(i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 303 | if (!Elt) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 305 | // If this is indexing an array of structures, get the structure element. |
| 306 | if (!LaterIndices.empty()) |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 307 | Elt = ConstantExpr::getExtractValue(Elt, LaterIndices); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 309 | // If the element is masked, handle it. |
| 310 | if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 312 | // Find out if the comparison would be true or false for the i'th element. |
| 313 | Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 314 | CompareRHS, DL, &TLI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 315 | // If the result is undef for this element, ignore it. |
| 316 | if (isa<UndefValue>(C)) { |
| 317 | // Extend range state machines to cover this element in case there is an |
| 318 | // undef in the middle of the range. |
| 319 | if (TrueRangeEnd == (int)i-1) |
| 320 | TrueRangeEnd = i; |
| 321 | if (FalseRangeEnd == (int)i-1) |
| 322 | FalseRangeEnd = i; |
| 323 | continue; |
| 324 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 326 | // If we can't compute the result for any of the elements, we have to give |
| 327 | // up evaluating the entire conditional. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 328 | if (!isa<ConstantInt>(C)) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 330 | // Otherwise, we know if the comparison is true or false for this element, |
| 331 | // update our state machines. |
| 332 | bool IsTrueForElt = !cast<ConstantInt>(C)->isZero(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 334 | // State machine for single/double/range index comparison. |
| 335 | if (IsTrueForElt) { |
| 336 | // Update the TrueElement state machine. |
| 337 | if (FirstTrueElement == Undefined) |
| 338 | FirstTrueElement = TrueRangeEnd = i; // First true element. |
| 339 | else { |
| 340 | // Update double-compare state machine. |
| 341 | if (SecondTrueElement == Undefined) |
| 342 | SecondTrueElement = i; |
| 343 | else |
| 344 | SecondTrueElement = Overdefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 346 | // Update range state machine. |
| 347 | if (TrueRangeEnd == (int)i-1) |
| 348 | TrueRangeEnd = i; |
| 349 | else |
| 350 | TrueRangeEnd = Overdefined; |
| 351 | } |
| 352 | } else { |
| 353 | // Update the FalseElement state machine. |
| 354 | if (FirstFalseElement == Undefined) |
| 355 | FirstFalseElement = FalseRangeEnd = i; // First false element. |
| 356 | else { |
| 357 | // Update double-compare state machine. |
| 358 | if (SecondFalseElement == Undefined) |
| 359 | SecondFalseElement = i; |
| 360 | else |
| 361 | SecondFalseElement = Overdefined; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 363 | // Update range state machine. |
| 364 | if (FalseRangeEnd == (int)i-1) |
| 365 | FalseRangeEnd = i; |
| 366 | else |
| 367 | FalseRangeEnd = Overdefined; |
| 368 | } |
| 369 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 371 | // If this element is in range, update our magic bitvector. |
| 372 | if (i < 64 && IsTrueForElt) |
| 373 | MagicBitvector |= 1ULL << i; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 375 | // If all of our states become overdefined, bail out early. Since the |
| 376 | // predicate is expensive, only check it every 8 elements. This is only |
| 377 | // really useful for really huge arrays. |
| 378 | if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined && |
| 379 | SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined && |
| 380 | FalseRangeEnd == Overdefined) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 381 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Now that we've scanned the entire array, emit our new comparison(s). We |
| 385 | // order the state machines in complexity of the generated code. |
| 386 | Value *Idx = GEP->getOperand(2); |
| 387 | |
Matt Arsenault | 5aeae18 | 2013-08-19 21:40:31 +0000 | [diff] [blame] | 388 | // If the index is larger than the pointer size of the target, truncate the |
| 389 | // index down like the GEP would do implicitly. We don't have to do this for |
| 390 | // an inbounds GEP because the index can't be out of range. |
Matt Arsenault | 8468062 | 2013-09-30 21:11:01 +0000 | [diff] [blame] | 391 | if (!GEP->isInBounds()) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 392 | Type *IntPtrTy = DL.getIntPtrType(GEP->getType()); |
Matt Arsenault | 8468062 | 2013-09-30 21:11:01 +0000 | [diff] [blame] | 393 | unsigned PtrSize = IntPtrTy->getIntegerBitWidth(); |
| 394 | if (Idx->getType()->getPrimitiveSizeInBits() > PtrSize) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 395 | Idx = Builder.CreateTrunc(Idx, IntPtrTy); |
Matt Arsenault | 8468062 | 2013-09-30 21:11:01 +0000 | [diff] [blame] | 396 | } |
Matt Arsenault | 5aeae18 | 2013-08-19 21:40:31 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 398 | // If the comparison is only true for one or two elements, emit direct |
| 399 | // comparisons. |
| 400 | if (SecondTrueElement != Overdefined) { |
| 401 | // None true -> false. |
| 402 | if (FirstTrueElement == Undefined) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 403 | return replaceInstUsesWith(ICI, Builder.getFalse()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 405 | Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 407 | // True for one element -> 'i == 47'. |
| 408 | if (SecondTrueElement == Undefined) |
| 409 | return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 411 | // True for two elements -> 'i == 47 | i == 72'. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 412 | Value *C1 = Builder.CreateICmpEQ(Idx, FirstTrueIdx); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 413 | Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 414 | Value *C2 = Builder.CreateICmpEQ(Idx, SecondTrueIdx); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 415 | return BinaryOperator::CreateOr(C1, C2); |
| 416 | } |
| 417 | |
| 418 | // If the comparison is only false for one or two elements, emit direct |
| 419 | // comparisons. |
| 420 | if (SecondFalseElement != Overdefined) { |
| 421 | // None false -> true. |
| 422 | if (FirstFalseElement == Undefined) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 423 | return replaceInstUsesWith(ICI, Builder.getTrue()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 425 | Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement); |
| 426 | |
| 427 | // False for one element -> 'i != 47'. |
| 428 | if (SecondFalseElement == Undefined) |
| 429 | return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 431 | // False for two elements -> 'i != 47 & i != 72'. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 432 | Value *C1 = Builder.CreateICmpNE(Idx, FirstFalseIdx); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 433 | Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 434 | Value *C2 = Builder.CreateICmpNE(Idx, SecondFalseIdx); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 435 | return BinaryOperator::CreateAnd(C1, C2); |
| 436 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 438 | // If the comparison can be replaced with a range comparison for the elements |
| 439 | // where it is true, emit the range check. |
| 440 | if (TrueRangeEnd != Overdefined) { |
| 441 | assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare"); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 443 | // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1). |
| 444 | if (FirstTrueElement) { |
| 445 | Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 446 | Idx = Builder.CreateAdd(Idx, Offs); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 447 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 448 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 449 | Value *End = ConstantInt::get(Idx->getType(), |
| 450 | TrueRangeEnd-FirstTrueElement+1); |
| 451 | return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End); |
| 452 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 454 | // False range check. |
| 455 | if (FalseRangeEnd != Overdefined) { |
| 456 | assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare"); |
| 457 | // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse). |
| 458 | if (FirstFalseElement) { |
| 459 | Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 460 | Idx = Builder.CreateAdd(Idx, Offs); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 461 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 463 | Value *End = ConstantInt::get(Idx->getType(), |
| 464 | FalseRangeEnd-FirstFalseElement); |
| 465 | return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End); |
| 466 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 467 | |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 468 | // If a magic bitvector captures the entire comparison state |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 469 | // of this load, replace it with computation that does: |
| 470 | // ((magic_cst >> i) & 1) != 0 |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 471 | { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 472 | Type *Ty = nullptr; |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 473 | |
| 474 | // Look for an appropriate type: |
| 475 | // - The type of Idx if the magic fits |
| 476 | // - The smallest fitting legal type if we have a DataLayout |
| 477 | // - Default to i32 |
| 478 | if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth()) |
| 479 | Ty = Idx->getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 480 | else |
| 481 | Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount); |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 482 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 483 | if (Ty) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 484 | Value *V = Builder.CreateIntCast(Idx, Ty, false); |
| 485 | V = Builder.CreateLShr(ConstantInt::get(Ty, MagicBitvector), V); |
| 486 | V = Builder.CreateAnd(ConstantInt::get(Ty, 1), V); |
Arnaud A. de Grandmaison | f364bc6 | 2013-03-22 08:25:01 +0000 | [diff] [blame] | 487 | return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0)); |
| 488 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 489 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 490 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 491 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 494 | /// Return a value that can be used to compare the *offset* implied by a GEP to |
| 495 | /// zero. For example, if we have &A[i], we want to return 'i' for |
| 496 | /// "icmp ne i, 0". Note that, in general, indices can be complex, and scales |
| 497 | /// are involved. The above expression would also be legal to codegen as |
| 498 | /// "icmp ne (i*4), 0" (assuming A is a pointer to i32). |
| 499 | /// 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] | 500 | /// to generate the first by knowing that pointer arithmetic doesn't overflow. |
| 501 | /// |
| 502 | /// 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] | 503 | /// |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 504 | static Value *evaluateGEPOffsetExpression(User *GEP, InstCombiner &IC, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 505 | const DataLayout &DL) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 506 | gep_type_iterator GTI = gep_type_begin(GEP); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 508 | // Check to see if this gep only has a single variable index. If so, and if |
| 509 | // any constant indices are a multiple of its scale, then we can compute this |
| 510 | // in terms of the scale of the variable index. For example, if the GEP |
| 511 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 512 | // because the expression will cross zero at the same point. |
| 513 | unsigned i, e = GEP->getNumOperands(); |
| 514 | int64_t Offset = 0; |
| 515 | for (i = 1; i != e; ++i, ++GTI) { |
| 516 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 517 | // Compute the aggregate offset of constant indices. |
| 518 | if (CI->isZero()) continue; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 520 | // Handle a struct index, which adds its field offset to the pointer. |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 521 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 522 | Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 523 | } else { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 524 | uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 525 | Offset += Size*CI->getSExtValue(); |
| 526 | } |
| 527 | } else { |
| 528 | // Found our variable index. |
| 529 | break; |
| 530 | } |
| 531 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 532 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 533 | // If there are no variable indices, we must have a constant offset, just |
| 534 | // evaluate it the general way. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 535 | if (i == e) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 537 | Value *VariableIdx = GEP->getOperand(i); |
| 538 | // Determine the scale factor of the variable element. For example, this is |
| 539 | // 4 if the variable index is into an array of i32. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 540 | uint64_t VariableScale = DL.getTypeAllocSize(GTI.getIndexedType()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 542 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 543 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 544 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 545 | if (!CI) return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 546 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 547 | // Compute the aggregate offset of constant indices. |
| 548 | if (CI->isZero()) continue; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 549 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 550 | // Handle a struct index, which adds its field offset to the pointer. |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 551 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 552 | Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 553 | } else { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 554 | uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 555 | Offset += Size*CI->getSExtValue(); |
| 556 | } |
| 557 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 559 | // Okay, we know we have a single variable index, which must be a |
| 560 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 561 | // the index. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 562 | Type *IntPtrTy = DL.getIntPtrType(GEP->getOperand(0)->getType()); |
Matt Arsenault | 745101d | 2013-08-21 19:53:10 +0000 | [diff] [blame] | 563 | unsigned IntPtrWidth = IntPtrTy->getIntegerBitWidth(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 564 | if (Offset == 0) { |
| 565 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 566 | // we don't need to bother extending: the extension won't affect where the |
| 567 | // computation crosses zero. |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 568 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 569 | VariableIdx = IC.Builder.CreateTrunc(VariableIdx, IntPtrTy); |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 570 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 571 | return VariableIdx; |
| 572 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 574 | // Otherwise, there is an index. The computation we will do will be modulo |
| 575 | // the pointer size, so get it. |
| 576 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 578 | Offset &= PtrSizeMask; |
| 579 | VariableScale &= PtrSizeMask; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 581 | // To do this transformation, any constant index must be a multiple of the |
| 582 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 583 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 584 | // multiple of the variable scale. |
| 585 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 586 | if (Offset != NewOffs*(int64_t)VariableScale) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 587 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 588 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 589 | // 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] | 590 | if (VariableIdx->getType() != IntPtrTy) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 591 | VariableIdx = IC.Builder.CreateIntCast(VariableIdx, IntPtrTy, |
Eli Friedman | 1754a25 | 2011-05-18 23:11:30 +0000 | [diff] [blame] | 592 | true /*Signed*/); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 593 | Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 594 | return IC.Builder.CreateAdd(VariableIdx, OffsetVal, "offset"); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 597 | /// Returns true if we can rewrite Start as a GEP with pointer Base |
| 598 | /// and some integer offset. The nodes that need to be re-written |
| 599 | /// for this transformation will be added to Explored. |
| 600 | static bool canRewriteGEPAsOffset(Value *Start, Value *Base, |
| 601 | const DataLayout &DL, |
| 602 | SetVector<Value *> &Explored) { |
| 603 | SmallVector<Value *, 16> WorkList(1, Start); |
| 604 | Explored.insert(Base); |
| 605 | |
| 606 | // The following traversal gives us an order which can be used |
| 607 | // when doing the final transformation. Since in the final |
| 608 | // transformation we create the PHI replacement instructions first, |
| 609 | // we don't have to get them in any particular order. |
| 610 | // |
| 611 | // However, for other instructions we will have to traverse the |
| 612 | // operands of an instruction first, which means that we have to |
| 613 | // do a post-order traversal. |
| 614 | while (!WorkList.empty()) { |
| 615 | SetVector<PHINode *> PHIs; |
| 616 | |
| 617 | while (!WorkList.empty()) { |
| 618 | if (Explored.size() >= 100) |
| 619 | return false; |
| 620 | |
| 621 | Value *V = WorkList.back(); |
| 622 | |
| 623 | if (Explored.count(V) != 0) { |
| 624 | WorkList.pop_back(); |
| 625 | continue; |
| 626 | } |
| 627 | |
| 628 | if (!isa<IntToPtrInst>(V) && !isa<PtrToIntInst>(V) && |
David Majnemer | 8b16da8 | 2016-09-15 20:10:09 +0000 | [diff] [blame] | 629 | !isa<GetElementPtrInst>(V) && !isa<PHINode>(V)) |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 630 | // We've found some value that we can't explore which is different from |
| 631 | // the base. Therefore we can't do this transformation. |
| 632 | return false; |
| 633 | |
| 634 | if (isa<IntToPtrInst>(V) || isa<PtrToIntInst>(V)) { |
| 635 | auto *CI = dyn_cast<CastInst>(V); |
| 636 | if (!CI->isNoopCast(DL)) |
| 637 | return false; |
| 638 | |
| 639 | if (Explored.count(CI->getOperand(0)) == 0) |
| 640 | WorkList.push_back(CI->getOperand(0)); |
| 641 | } |
| 642 | |
| 643 | if (auto *GEP = dyn_cast<GEPOperator>(V)) { |
| 644 | // We're limiting the GEP to having one index. This will preserve |
| 645 | // the original pointer type. We could handle more cases in the |
| 646 | // future. |
| 647 | if (GEP->getNumIndices() != 1 || !GEP->isInBounds() || |
| 648 | GEP->getType() != Start->getType()) |
| 649 | return false; |
| 650 | |
| 651 | if (Explored.count(GEP->getOperand(0)) == 0) |
| 652 | WorkList.push_back(GEP->getOperand(0)); |
| 653 | } |
| 654 | |
| 655 | if (WorkList.back() == V) { |
| 656 | WorkList.pop_back(); |
| 657 | // We've finished visiting this node, mark it as such. |
| 658 | Explored.insert(V); |
| 659 | } |
| 660 | |
| 661 | if (auto *PN = dyn_cast<PHINode>(V)) { |
David Majnemer | cdf2873 | 2016-03-19 04:39:52 +0000 | [diff] [blame] | 662 | // We cannot transform PHIs on unsplittable basic blocks. |
| 663 | if (isa<CatchSwitchInst>(PN->getParent()->getTerminator())) |
| 664 | return false; |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 665 | Explored.insert(PN); |
| 666 | PHIs.insert(PN); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // Explore the PHI nodes further. |
| 671 | for (auto *PN : PHIs) |
| 672 | for (Value *Op : PN->incoming_values()) |
| 673 | if (Explored.count(Op) == 0) |
| 674 | WorkList.push_back(Op); |
| 675 | } |
| 676 | |
| 677 | // Make sure that we can do this. Since we can't insert GEPs in a basic |
| 678 | // block before a PHI node, we can't easily do this transformation if |
| 679 | // we have PHI node users of transformed instructions. |
| 680 | for (Value *Val : Explored) { |
| 681 | for (Value *Use : Val->uses()) { |
| 682 | |
| 683 | auto *PHI = dyn_cast<PHINode>(Use); |
| 684 | auto *Inst = dyn_cast<Instruction>(Val); |
| 685 | |
| 686 | if (Inst == Base || Inst == PHI || !Inst || !PHI || |
| 687 | Explored.count(PHI) == 0) |
| 688 | continue; |
| 689 | |
| 690 | if (PHI->getParent() == Inst->getParent()) |
| 691 | return false; |
| 692 | } |
| 693 | } |
| 694 | return true; |
| 695 | } |
| 696 | |
| 697 | // Sets the appropriate insert point on Builder where we can add |
| 698 | // a replacement Instruction for V (if that is possible). |
| 699 | static void setInsertionPoint(IRBuilder<> &Builder, Value *V, |
| 700 | bool Before = true) { |
| 701 | if (auto *PHI = dyn_cast<PHINode>(V)) { |
| 702 | Builder.SetInsertPoint(&*PHI->getParent()->getFirstInsertionPt()); |
| 703 | return; |
| 704 | } |
| 705 | if (auto *I = dyn_cast<Instruction>(V)) { |
| 706 | if (!Before) |
| 707 | I = &*std::next(I->getIterator()); |
| 708 | Builder.SetInsertPoint(I); |
| 709 | return; |
| 710 | } |
| 711 | if (auto *A = dyn_cast<Argument>(V)) { |
| 712 | // Set the insertion point in the entry block. |
| 713 | BasicBlock &Entry = A->getParent()->getEntryBlock(); |
| 714 | Builder.SetInsertPoint(&*Entry.getFirstInsertionPt()); |
| 715 | return; |
| 716 | } |
| 717 | // Otherwise, this is a constant and we don't need to set a new |
| 718 | // insertion point. |
| 719 | assert(isa<Constant>(V) && "Setting insertion point for unknown value!"); |
| 720 | } |
| 721 | |
| 722 | /// Returns a re-written value of Start as an indexed GEP using Base as a |
| 723 | /// pointer. |
| 724 | static Value *rewriteGEPAsOffset(Value *Start, Value *Base, |
| 725 | const DataLayout &DL, |
| 726 | SetVector<Value *> &Explored) { |
| 727 | // Perform all the substitutions. This is a bit tricky because we can |
| 728 | // have cycles in our use-def chains. |
| 729 | // 1. Create the PHI nodes without any incoming values. |
| 730 | // 2. Create all the other values. |
| 731 | // 3. Add the edges for the PHI nodes. |
| 732 | // 4. Emit GEPs to get the original pointers. |
| 733 | // 5. Remove the original instructions. |
| 734 | Type *IndexType = IntegerType::get( |
| 735 | Base->getContext(), DL.getPointerTypeSizeInBits(Start->getType())); |
| 736 | |
| 737 | DenseMap<Value *, Value *> NewInsts; |
| 738 | NewInsts[Base] = ConstantInt::getNullValue(IndexType); |
| 739 | |
| 740 | // Create the new PHI nodes, without adding any incoming values. |
| 741 | for (Value *Val : Explored) { |
| 742 | if (Val == Base) |
| 743 | continue; |
| 744 | // Create empty phi nodes. This avoids cyclic dependencies when creating |
| 745 | // the remaining instructions. |
| 746 | if (auto *PHI = dyn_cast<PHINode>(Val)) |
| 747 | NewInsts[PHI] = PHINode::Create(IndexType, PHI->getNumIncomingValues(), |
| 748 | PHI->getName() + ".idx", PHI); |
| 749 | } |
| 750 | IRBuilder<> Builder(Base->getContext()); |
| 751 | |
| 752 | // Create all the other instructions. |
| 753 | for (Value *Val : Explored) { |
| 754 | |
| 755 | if (NewInsts.find(Val) != NewInsts.end()) |
| 756 | continue; |
| 757 | |
| 758 | if (auto *CI = dyn_cast<CastInst>(Val)) { |
| 759 | NewInsts[CI] = NewInsts[CI->getOperand(0)]; |
| 760 | continue; |
| 761 | } |
| 762 | if (auto *GEP = dyn_cast<GEPOperator>(Val)) { |
| 763 | Value *Index = NewInsts[GEP->getOperand(1)] ? NewInsts[GEP->getOperand(1)] |
| 764 | : GEP->getOperand(1); |
| 765 | setInsertionPoint(Builder, GEP); |
| 766 | // Indices might need to be sign extended. GEPs will magically do |
| 767 | // this, but we need to do it ourselves here. |
| 768 | if (Index->getType()->getScalarSizeInBits() != |
| 769 | NewInsts[GEP->getOperand(0)]->getType()->getScalarSizeInBits()) { |
| 770 | Index = Builder.CreateSExtOrTrunc( |
| 771 | Index, NewInsts[GEP->getOperand(0)]->getType(), |
| 772 | GEP->getOperand(0)->getName() + ".sext"); |
| 773 | } |
| 774 | |
| 775 | auto *Op = NewInsts[GEP->getOperand(0)]; |
| 776 | if (isa<ConstantInt>(Op) && dyn_cast<ConstantInt>(Op)->isZero()) |
| 777 | NewInsts[GEP] = Index; |
| 778 | else |
| 779 | NewInsts[GEP] = Builder.CreateNSWAdd( |
| 780 | Op, Index, GEP->getOperand(0)->getName() + ".add"); |
| 781 | continue; |
| 782 | } |
| 783 | if (isa<PHINode>(Val)) |
| 784 | continue; |
| 785 | |
| 786 | llvm_unreachable("Unexpected instruction type"); |
| 787 | } |
| 788 | |
| 789 | // Add the incoming values to the PHI nodes. |
| 790 | for (Value *Val : Explored) { |
| 791 | if (Val == Base) |
| 792 | continue; |
| 793 | // All the instructions have been created, we can now add edges to the |
| 794 | // phi nodes. |
| 795 | if (auto *PHI = dyn_cast<PHINode>(Val)) { |
| 796 | PHINode *NewPhi = static_cast<PHINode *>(NewInsts[PHI]); |
| 797 | for (unsigned I = 0, E = PHI->getNumIncomingValues(); I < E; ++I) { |
| 798 | Value *NewIncoming = PHI->getIncomingValue(I); |
| 799 | |
| 800 | if (NewInsts.find(NewIncoming) != NewInsts.end()) |
| 801 | NewIncoming = NewInsts[NewIncoming]; |
| 802 | |
| 803 | NewPhi->addIncoming(NewIncoming, PHI->getIncomingBlock(I)); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | for (Value *Val : Explored) { |
| 809 | if (Val == Base) |
| 810 | continue; |
| 811 | |
| 812 | // Depending on the type, for external users we have to emit |
| 813 | // a GEP or a GEP + ptrtoint. |
| 814 | setInsertionPoint(Builder, Val, false); |
| 815 | |
| 816 | // If required, create an inttoptr instruction for Base. |
| 817 | Value *NewBase = Base; |
| 818 | if (!Base->getType()->isPointerTy()) |
| 819 | NewBase = Builder.CreateBitOrPointerCast(Base, Start->getType(), |
| 820 | Start->getName() + "to.ptr"); |
| 821 | |
| 822 | Value *GEP = Builder.CreateInBoundsGEP( |
| 823 | Start->getType()->getPointerElementType(), NewBase, |
| 824 | makeArrayRef(NewInsts[Val]), Val->getName() + ".ptr"); |
| 825 | |
| 826 | if (!Val->getType()->isPointerTy()) { |
| 827 | Value *Cast = Builder.CreatePointerCast(GEP, Val->getType(), |
| 828 | Val->getName() + ".conv"); |
| 829 | GEP = Cast; |
| 830 | } |
| 831 | Val->replaceAllUsesWith(GEP); |
| 832 | } |
| 833 | |
| 834 | return NewInsts[Start]; |
| 835 | } |
| 836 | |
| 837 | /// Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express |
| 838 | /// the input Value as a constant indexed GEP. Returns a pair containing |
| 839 | /// the GEPs Pointer and Index. |
| 840 | static std::pair<Value *, Value *> |
| 841 | getAsConstantIndexedAddress(Value *V, const DataLayout &DL) { |
| 842 | Type *IndexType = IntegerType::get(V->getContext(), |
| 843 | DL.getPointerTypeSizeInBits(V->getType())); |
| 844 | |
| 845 | Constant *Index = ConstantInt::getNullValue(IndexType); |
| 846 | while (true) { |
| 847 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 848 | // We accept only inbouds GEPs here to exclude the possibility of |
| 849 | // overflow. |
| 850 | if (!GEP->isInBounds()) |
| 851 | break; |
| 852 | if (GEP->hasAllConstantIndices() && GEP->getNumIndices() == 1 && |
| 853 | GEP->getType() == V->getType()) { |
| 854 | V = GEP->getOperand(0); |
| 855 | Constant *GEPIndex = static_cast<Constant *>(GEP->getOperand(1)); |
| 856 | Index = ConstantExpr::getAdd( |
| 857 | Index, ConstantExpr::getSExtOrBitCast(GEPIndex, IndexType)); |
| 858 | continue; |
| 859 | } |
| 860 | break; |
| 861 | } |
| 862 | if (auto *CI = dyn_cast<IntToPtrInst>(V)) { |
| 863 | if (!CI->isNoopCast(DL)) |
| 864 | break; |
| 865 | V = CI->getOperand(0); |
| 866 | continue; |
| 867 | } |
| 868 | if (auto *CI = dyn_cast<PtrToIntInst>(V)) { |
| 869 | if (!CI->isNoopCast(DL)) |
| 870 | break; |
| 871 | V = CI->getOperand(0); |
| 872 | continue; |
| 873 | } |
| 874 | break; |
| 875 | } |
| 876 | return {V, Index}; |
| 877 | } |
| 878 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 879 | /// Converts (CMP GEPLHS, RHS) if this change would make RHS a constant. |
| 880 | /// We can look through PHIs, GEPs and casts in order to determine a common base |
| 881 | /// between GEPLHS and RHS. |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 882 | static Instruction *transformToIndexedCompare(GEPOperator *GEPLHS, Value *RHS, |
| 883 | ICmpInst::Predicate Cond, |
| 884 | const DataLayout &DL) { |
| 885 | if (!GEPLHS->hasAllConstantIndices()) |
| 886 | return nullptr; |
| 887 | |
Silviu Baranga | c6d21eb | 2017-01-31 14:04:15 +0000 | [diff] [blame] | 888 | // Make sure the pointers have the same type. |
| 889 | if (GEPLHS->getType() != RHS->getType()) |
| 890 | return nullptr; |
| 891 | |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 892 | Value *PtrBase, *Index; |
| 893 | std::tie(PtrBase, Index) = getAsConstantIndexedAddress(GEPLHS, DL); |
| 894 | |
| 895 | // The set of nodes that will take part in this transformation. |
| 896 | SetVector<Value *> Nodes; |
| 897 | |
| 898 | if (!canRewriteGEPAsOffset(RHS, PtrBase, DL, Nodes)) |
| 899 | return nullptr; |
| 900 | |
| 901 | // We know we can re-write this as |
| 902 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) |
| 903 | // Since we've only looked through inbouds GEPs we know that we |
| 904 | // can't have overflow on either side. We can therefore re-write |
| 905 | // this as: |
| 906 | // OFFSET1 cmp OFFSET2 |
| 907 | Value *NewRHS = rewriteGEPAsOffset(RHS, PtrBase, DL, Nodes); |
| 908 | |
| 909 | // RewriteGEPAsOffset has replaced RHS and all of its uses with a re-written |
| 910 | // GEP having PtrBase as the pointer base, and has returned in NewRHS the |
| 911 | // offset. Since Index is the offset of LHS to the base pointer, we will now |
| 912 | // compare the offsets instead of comparing the pointers. |
| 913 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Index, NewRHS); |
| 914 | } |
| 915 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 916 | /// Fold comparisons between a GEP instruction and something else. At this point |
| 917 | /// we know that the GEP is on the LHS of the comparison. |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 918 | Instruction *InstCombiner::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 919 | ICmpInst::Predicate Cond, |
| 920 | Instruction &I) { |
Benjamin Kramer | 6ee8690 | 2012-02-21 13:31:09 +0000 | [diff] [blame] | 921 | // Don't transform signed compares of GEPs into index compares. Even if the |
| 922 | // GEP is inbounds, the final add of the base pointer can have signed overflow |
| 923 | // and would change the result of the icmp. |
| 924 | // 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] | 925 | // the maximum signed value for the pointer type. |
Benjamin Kramer | 6ee8690 | 2012-02-21 13:31:09 +0000 | [diff] [blame] | 926 | if (ICmpInst::isSigned(Cond)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 927 | return nullptr; |
Benjamin Kramer | 6ee8690 | 2012-02-21 13:31:09 +0000 | [diff] [blame] | 928 | |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 929 | // Look through bitcasts and addrspacecasts. We do not however want to remove |
| 930 | // 0 GEPs. |
| 931 | if (!isa<GetElementPtrInst>(RHS)) |
| 932 | RHS = RHS->stripPointerCasts(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 933 | |
| 934 | Value *PtrBase = GEPLHS->getOperand(0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 935 | if (PtrBase == RHS && GEPLHS->isInBounds()) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 936 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| 937 | // This transformation (ignoring the base and scales) is valid because we |
| 938 | // know pointers can't overflow since the gep is inbounds. See if we can |
| 939 | // output an optimized form. |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 940 | Value *Offset = evaluateGEPOffsetExpression(GEPLHS, *this, DL); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 942 | // If not, synthesize the offset the hard way. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 943 | if (!Offset) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 944 | Offset = EmitGEPOffset(GEPLHS); |
| 945 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset, |
| 946 | Constant::getNullValue(Offset->getType())); |
| 947 | } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) { |
| 948 | // If the base pointers are different, but the indices are the same, just |
| 949 | // compare the base pointer. |
| 950 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 951 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
| 952 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
| 953 | GEPRHS->getOperand(0)->getType(); |
| 954 | if (IndicesTheSame) |
| 955 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 956 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 957 | IndicesTheSame = false; |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | // If all indices are the same, just compare the base pointers. |
| 962 | if (IndicesTheSame) |
David Majnemer | 5953d37 | 2013-06-29 10:28:04 +0000 | [diff] [blame] | 963 | return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 964 | |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 965 | // If we're comparing GEPs with two base pointers that only differ in type |
| 966 | // and both GEPs have only constant indices or just one use, then fold |
| 967 | // the compare with the adjusted indices. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 968 | if (GEPLHS->isInBounds() && GEPRHS->isInBounds() && |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 969 | (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) && |
| 970 | (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) && |
| 971 | PtrBase->stripPointerCasts() == |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 972 | GEPRHS->getOperand(0)->stripPointerCasts()) { |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 973 | Value *LOffset = EmitGEPOffset(GEPLHS); |
| 974 | Value *ROffset = EmitGEPOffset(GEPRHS); |
| 975 | |
| 976 | // If we looked through an addrspacecast between different sized address |
| 977 | // spaces, the LHS and RHS pointers are different sized |
| 978 | // integers. Truncate to the smaller one. |
| 979 | Type *LHSIndexTy = LOffset->getType(); |
| 980 | Type *RHSIndexTy = ROffset->getType(); |
| 981 | if (LHSIndexTy != RHSIndexTy) { |
| 982 | if (LHSIndexTy->getPrimitiveSizeInBits() < |
| 983 | RHSIndexTy->getPrimitiveSizeInBits()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 984 | ROffset = Builder.CreateTrunc(ROffset, LHSIndexTy); |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 985 | } else |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 986 | LOffset = Builder.CreateTrunc(LOffset, RHSIndexTy); |
Matt Arsenault | 44f60d0 | 2014-06-09 19:20:29 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 989 | Value *Cmp = Builder.CreateICmp(ICmpInst::getSignedPredicate(Cond), |
| 990 | LOffset, ROffset); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 991 | return replaceInstUsesWith(I, Cmp); |
Benjamin Kramer | 7adb189 | 2012-02-20 15:07:47 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 994 | // Otherwise, the base pointers are different and the indices are |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 995 | // different. Try convert this to an indexed compare by looking through |
| 996 | // PHIs/casts. |
| 997 | return transformToIndexedCompare(GEPLHS, RHS, Cond, DL); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | // If one of the GEPs has all zero indices, recurse. |
Benjamin Kramer | d0993e0 | 2014-07-07 11:01:16 +0000 | [diff] [blame] | 1001 | if (GEPLHS->hasAllZeroIndices()) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 1002 | return foldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
David Majnemer | 92a8a7d | 2013-06-29 09:45:35 +0000 | [diff] [blame] | 1003 | ICmpInst::getSwappedPredicate(Cond), I); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1004 | |
| 1005 | // If the other GEP has all zero indices, recurse. |
Benjamin Kramer | d0993e0 | 2014-07-07 11:01:16 +0000 | [diff] [blame] | 1006 | if (GEPRHS->hasAllZeroIndices()) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 1007 | return foldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1008 | |
Stuart Hastings | 66a82b9 | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 1009 | bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1010 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 1011 | // If the GEPs only differ by one index, compare it. |
| 1012 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 1013 | unsigned DiffOperand = 0; // The operand that differs. |
| 1014 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 1015 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 1016 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 1017 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
| 1018 | // Irreconcilable differences. |
| 1019 | NumDifferences = 2; |
| 1020 | break; |
| 1021 | } else { |
| 1022 | if (NumDifferences++) break; |
| 1023 | DiffOperand = i; |
| 1024 | } |
| 1025 | } |
| 1026 | |
Rafael Espindola | a7bbc0b | 2013-06-06 17:03:05 +0000 | [diff] [blame] | 1027 | if (NumDifferences == 0) // SAME GEP? |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1028 | return replaceInstUsesWith(I, // No comparison is needed here. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1029 | Builder.getInt1(ICmpInst::isTrueWhenEqual(Cond))); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1030 | |
Stuart Hastings | 66a82b9 | 2011-05-14 05:55:10 +0000 | [diff] [blame] | 1031 | else if (NumDifferences == 1 && GEPsInBounds) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1032 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 1033 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
| 1034 | // Make sure we do a signed comparison here. |
| 1035 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // Only lower this if the icmp is the only user of the GEP or if we expect |
| 1040 | // the result to fold to a constant! |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1041 | if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1042 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 1043 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 1044 | Value *L = EmitGEPOffset(GEPLHS); |
| 1045 | Value *R = EmitGEPOffset(GEPRHS); |
| 1046 | return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R); |
| 1047 | } |
| 1048 | } |
Silviu Baranga | f29dfd3 | 2016-01-15 15:52:05 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Try convert this to an indexed compare by looking through PHIs/casts as a |
| 1051 | // last resort. |
| 1052 | return transformToIndexedCompare(GEPLHS, RHS, Cond, DL); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 1055 | Instruction *InstCombiner::foldAllocaCmp(ICmpInst &ICI, |
| 1056 | const AllocaInst *Alloca, |
| 1057 | const Value *Other) { |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1058 | assert(ICI.isEquality() && "Cannot fold non-equality comparison."); |
| 1059 | |
| 1060 | // It would be tempting to fold away comparisons between allocas and any |
| 1061 | // pointer not based on that alloca (e.g. an argument). However, even |
| 1062 | // though such pointers cannot alias, they can still compare equal. |
| 1063 | // |
| 1064 | // But LLVM doesn't specify where allocas get their memory, so if the alloca |
| 1065 | // doesn't escape we can argue that it's impossible to guess its value, and we |
| 1066 | // can therefore act as if any such guesses are wrong. |
| 1067 | // |
| 1068 | // The code below checks that the alloca doesn't escape, and that it's only |
| 1069 | // used in a comparison once (the current instruction). The |
| 1070 | // single-comparison-use condition ensures that we're trivially folding all |
| 1071 | // comparisons against the alloca consistently, and avoids the risk of |
| 1072 | // erroneously folding a comparison of the pointer with itself. |
| 1073 | |
| 1074 | unsigned MaxIter = 32; // Break cycles and bound to constant-time. |
| 1075 | |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 1076 | SmallVector<const Use *, 32> Worklist; |
| 1077 | for (const Use &U : Alloca->uses()) { |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1078 | if (Worklist.size() >= MaxIter) |
| 1079 | return nullptr; |
| 1080 | Worklist.push_back(&U); |
| 1081 | } |
| 1082 | |
| 1083 | unsigned NumCmps = 0; |
| 1084 | while (!Worklist.empty()) { |
| 1085 | assert(Worklist.size() <= MaxIter); |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 1086 | const Use *U = Worklist.pop_back_val(); |
| 1087 | const Value *V = U->getUser(); |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1088 | --MaxIter; |
| 1089 | |
| 1090 | if (isa<BitCastInst>(V) || isa<GetElementPtrInst>(V) || isa<PHINode>(V) || |
| 1091 | isa<SelectInst>(V)) { |
| 1092 | // Track the uses. |
| 1093 | } else if (isa<LoadInst>(V)) { |
| 1094 | // Loading from the pointer doesn't escape it. |
| 1095 | continue; |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 1096 | } else if (const auto *SI = dyn_cast<StoreInst>(V)) { |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1097 | // Storing *to* the pointer is fine, but storing the pointer escapes it. |
| 1098 | if (SI->getValueOperand() == U->get()) |
| 1099 | return nullptr; |
| 1100 | continue; |
| 1101 | } else if (isa<ICmpInst>(V)) { |
| 1102 | if (NumCmps++) |
| 1103 | return nullptr; // Found more than one cmp. |
| 1104 | continue; |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 1105 | } else if (const auto *Intrin = dyn_cast<IntrinsicInst>(V)) { |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1106 | switch (Intrin->getIntrinsicID()) { |
| 1107 | // These intrinsics don't escape or compare the pointer. Memset is safe |
| 1108 | // because we don't allow ptrtoint. Memcpy and memmove are safe because |
| 1109 | // we don't allow stores, so src cannot point to V. |
| 1110 | case Intrinsic::lifetime_start: case Intrinsic::lifetime_end: |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1111 | case Intrinsic::memcpy: case Intrinsic::memmove: case Intrinsic::memset: |
| 1112 | continue; |
| 1113 | default: |
| 1114 | return nullptr; |
| 1115 | } |
| 1116 | } else { |
| 1117 | return nullptr; |
| 1118 | } |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 1119 | for (const Use &U : V->uses()) { |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1120 | if (Worklist.size() >= MaxIter) |
| 1121 | return nullptr; |
| 1122 | Worklist.push_back(&U); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | Type *CmpTy = CmpInst::makeCmpResultType(Other->getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1127 | return replaceInstUsesWith( |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 1128 | ICI, |
| 1129 | ConstantInt::get(CmpTy, !CmpInst::isTrueWhenEqual(ICI.getPredicate()))); |
| 1130 | } |
| 1131 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 1132 | /// Fold "icmp pred (X+CI), X". |
Craig Topper | a85f862 | 2017-08-23 05:46:09 +0000 | [diff] [blame] | 1133 | Instruction *InstCombiner::foldICmpAddOpConst(Value *X, ConstantInt *CI, |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 1134 | ICmpInst::Predicate Pred) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1135 | // 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] | 1136 | // 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] | 1137 | // operators. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 8c92b57 | 2010-01-08 17:48:19 +0000 | [diff] [blame] | 1139 | // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255 |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1140 | // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253 |
| 1141 | // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0 |
| 1142 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1143 | Value *R = |
Chris Lattner | 8c92b57 | 2010-01-08 17:48:19 +0000 | [diff] [blame] | 1144 | ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1145 | return new ICmpInst(ICmpInst::ICMP_UGT, X, R); |
| 1146 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1148 | // (X+1) >u X --> X <u (0-1) --> X != 255 |
| 1149 | // (X+2) >u X --> X <u (0-2) --> X <u 254 |
| 1150 | // (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] | 1151 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1152 | return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1153 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1154 | unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits(); |
| 1155 | ConstantInt *SMax = ConstantInt::get(X->getContext(), |
| 1156 | APInt::getSignedMaxValue(BitWidth)); |
| 1157 | |
| 1158 | // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127 |
| 1159 | // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125 |
| 1160 | // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0 |
| 1161 | // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1 |
| 1162 | // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126 |
| 1163 | // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127 |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 1164 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1165 | return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI)); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1167 | // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127 |
| 1168 | // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126 |
| 1169 | // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1 |
| 1170 | // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2 |
| 1171 | // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126 |
| 1172 | // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128 |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1174 | assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1175 | Constant *C = Builder.getInt(CI->getValue() - 1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1176 | return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C)); |
| 1177 | } |
| 1178 | |
Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 1179 | /// Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" -> |
| 1180 | /// (icmp eq/ne A, Log2(AP2/AP1)) -> |
| 1181 | /// (icmp eq/ne A, Log2(AP2) - Log2(AP1)). |
| 1182 | Instruction *InstCombiner::foldICmpShrConstConst(ICmpInst &I, Value *A, |
| 1183 | const APInt &AP1, |
| 1184 | const APInt &AP2) { |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1185 | assert(I.isEquality() && "Cannot fold icmp gt/lt"); |
| 1186 | |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1187 | auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) { |
| 1188 | if (I.getPredicate() == I.ICMP_NE) |
| 1189 | Pred = CmpInst::getInversePredicate(Pred); |
| 1190 | return new ICmpInst(Pred, LHS, RHS); |
| 1191 | }; |
| 1192 | |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1193 | // Don't bother doing any work for cases which InstSimplify handles. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1194 | if (AP2.isNullValue()) |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1195 | return nullptr; |
Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 1196 | |
| 1197 | bool IsAShr = isa<AShrOperator>(I.getOperand(0)); |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1198 | if (IsAShr) { |
| 1199 | if (AP2.isAllOnesValue()) |
| 1200 | return nullptr; |
| 1201 | if (AP2.isNegative() != AP1.isNegative()) |
| 1202 | return nullptr; |
| 1203 | if (AP2.sgt(AP1)) |
| 1204 | return nullptr; |
| 1205 | } |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1206 | |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1207 | if (!AP1) |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1208 | // 'A' must be large enough to shift out the highest set bit. |
| 1209 | return getICmp(I.ICMP_UGT, A, |
| 1210 | ConstantInt::get(A->getType(), AP2.logBase2())); |
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 == AP2) |
| 1213 | return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType())); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1214 | |
Andrea Di Biagio | 5b92b49 | 2014-09-17 11:32:31 +0000 | [diff] [blame] | 1215 | int Shift; |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1216 | if (IsAShr && AP1.isNegative()) |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1217 | Shift = AP1.countLeadingOnes() - AP2.countLeadingOnes(); |
Andrea Di Biagio | 5b92b49 | 2014-09-17 11:32:31 +0000 | [diff] [blame] | 1218 | else |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1219 | Shift = AP1.countLeadingZeros() - AP2.countLeadingZeros(); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1220 | |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1221 | if (Shift > 0) { |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1222 | if (IsAShr && AP1 == AP2.ashr(Shift)) { |
| 1223 | // 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] | 1224 | // of the ashr is not a power of two. |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1225 | if (AP1.isAllOnesValue() && !AP2.isPowerOf2()) |
| 1226 | return getICmp(I.ICMP_UGE, A, ConstantInt::get(A->getType(), Shift)); |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1227 | return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); |
David Majnemer | e5977eb | 2015-09-19 00:48:26 +0000 | [diff] [blame] | 1228 | } else if (AP1 == AP2.lshr(Shift)) { |
| 1229 | return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); |
| 1230 | } |
David Majnemer | d205602 | 2014-10-21 19:51:55 +0000 | [diff] [blame] | 1231 | } |
Sanjay Patel | 524fcdf | 2016-09-15 19:04:55 +0000 | [diff] [blame] | 1232 | |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1233 | // Shifting const2 will never be equal to const1. |
Sanjay Patel | 524fcdf | 2016-09-15 19:04:55 +0000 | [diff] [blame] | 1234 | // FIXME: This should always be handled by InstSimplify? |
| 1235 | auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE); |
| 1236 | return replaceInstUsesWith(I, TorF); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 1237 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 1238 | |
Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 1239 | /// Handle "(icmp eq/ne (shl AP2, A), AP1)" -> |
| 1240 | /// (icmp eq/ne A, TrailingZeros(AP1) - TrailingZeros(AP2)). |
| 1241 | Instruction *InstCombiner::foldICmpShlConstConst(ICmpInst &I, Value *A, |
| 1242 | const APInt &AP1, |
| 1243 | const APInt &AP2) { |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1244 | assert(I.isEquality() && "Cannot fold icmp gt/lt"); |
| 1245 | |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1246 | auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) { |
| 1247 | if (I.getPredicate() == I.ICMP_NE) |
| 1248 | Pred = CmpInst::getInversePredicate(Pred); |
| 1249 | return new ICmpInst(Pred, LHS, RHS); |
| 1250 | }; |
| 1251 | |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1252 | // Don't bother doing any work for cases which InstSimplify handles. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1253 | if (AP2.isNullValue()) |
David Majnemer | 2abb818 | 2014-10-25 07:13:13 +0000 | [diff] [blame] | 1254 | return nullptr; |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1255 | |
| 1256 | unsigned AP2TrailingZeros = AP2.countTrailingZeros(); |
| 1257 | |
| 1258 | if (!AP1 && AP2TrailingZeros != 0) |
Sanjay Patel | af91d1f | 2016-09-15 21:35:30 +0000 | [diff] [blame] | 1259 | return getICmp( |
| 1260 | I.ICMP_UGE, A, |
| 1261 | ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros)); |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (AP1 == AP2) |
| 1264 | return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType())); |
| 1265 | |
| 1266 | // Get the distance between the lowest bits that are set. |
| 1267 | int Shift = AP1.countTrailingZeros() - AP2TrailingZeros; |
| 1268 | |
| 1269 | if (Shift > 0 && AP2.shl(Shift) == AP1) |
| 1270 | return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift)); |
| 1271 | |
| 1272 | // Shifting const2 will never be equal to const1. |
Sanjay Patel | 524fcdf | 2016-09-15 19:04:55 +0000 | [diff] [blame] | 1273 | // FIXME: This should always be handled by InstSimplify? |
| 1274 | auto *TorF = ConstantInt::get(I.getType(), I.getPredicate() == I.ICMP_NE); |
| 1275 | return replaceInstUsesWith(I, TorF); |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1278 | /// The caller has matched a pattern of the form: |
| 1279 | /// I = icmp ugt (add (add A, B), CI2), CI1 |
| 1280 | /// If this is of the form: |
| 1281 | /// sum = a + b |
| 1282 | /// if (sum+128 >u 255) |
| 1283 | /// Then replace it with llvm.sadd.with.overflow.i8. |
| 1284 | /// |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 1285 | static Instruction *processUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B, |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1286 | ConstantInt *CI2, ConstantInt *CI1, |
| 1287 | InstCombiner &IC) { |
| 1288 | // The transformation we're trying to do here is to transform this into an |
| 1289 | // llvm.sadd.with.overflow. To do this, we have to replace the original add |
| 1290 | // with a narrower add, and discard the add-with-constant that is part of the |
| 1291 | // range check (if we can't eliminate it, this isn't profitable). |
| 1292 | |
| 1293 | // In order to eliminate the add-with-constant, the compare can be its only |
| 1294 | // use. |
| 1295 | Instruction *AddWithCst = cast<Instruction>(I.getOperand(0)); |
| 1296 | if (!AddWithCst->hasOneUse()) |
| 1297 | return nullptr; |
| 1298 | |
| 1299 | // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow. |
| 1300 | if (!CI2->getValue().isPowerOf2()) |
| 1301 | return nullptr; |
| 1302 | unsigned NewWidth = CI2->getValue().countTrailingZeros(); |
| 1303 | if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) |
| 1304 | return nullptr; |
| 1305 | |
| 1306 | // The width of the new add formed is 1 more than the bias. |
| 1307 | ++NewWidth; |
| 1308 | |
| 1309 | // Check to see that CI1 is an all-ones value with NewWidth bits. |
| 1310 | if (CI1->getBitWidth() == NewWidth || |
| 1311 | CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth)) |
| 1312 | return nullptr; |
| 1313 | |
| 1314 | // This is only really a signed overflow check if the inputs have been |
| 1315 | // sign-extended; check for that condition. For example, if CI2 is 2^31 and |
| 1316 | // the operands of the add are 64 bits wide, we need at least 33 sign bits. |
| 1317 | unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1; |
| 1318 | if (IC.ComputeNumSignBits(A, 0, &I) < NeededSignBits || |
| 1319 | IC.ComputeNumSignBits(B, 0, &I) < NeededSignBits) |
| 1320 | return nullptr; |
| 1321 | |
| 1322 | // In order to replace the original add with a narrower |
| 1323 | // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant |
| 1324 | // and truncates that discard the high bits of the add. Verify that this is |
| 1325 | // the case. |
| 1326 | Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0)); |
| 1327 | for (User *U : OrigAdd->users()) { |
| 1328 | if (U == AddWithCst) |
| 1329 | continue; |
| 1330 | |
| 1331 | // Only accept truncates for now. We would really like a nice recursive |
| 1332 | // predicate like SimplifyDemandedBits, but which goes downwards the use-def |
| 1333 | // chain to see which bits of a value are actually demanded. If the |
| 1334 | // original add had another add which was then immediately truncated, we |
| 1335 | // could still do the transformation. |
| 1336 | TruncInst *TI = dyn_cast<TruncInst>(U); |
| 1337 | if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth) |
| 1338 | return nullptr; |
| 1339 | } |
| 1340 | |
| 1341 | // If the pattern matches, truncate the inputs to the narrower type and |
| 1342 | // use the sadd_with_overflow intrinsic to efficiently compute both the |
| 1343 | // result and the overflow bit. |
| 1344 | Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth); |
| 1345 | Value *F = Intrinsic::getDeclaration(I.getModule(), |
| 1346 | Intrinsic::sadd_with_overflow, NewType); |
| 1347 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1348 | InstCombiner::BuilderTy &Builder = IC.Builder; |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1349 | |
| 1350 | // Put the new code above the original add, in case there are any uses of the |
| 1351 | // add between the add and the compare. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1352 | Builder.SetInsertPoint(OrigAdd); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1353 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1354 | Value *TruncA = Builder.CreateTrunc(A, NewType, A->getName() + ".trunc"); |
| 1355 | Value *TruncB = Builder.CreateTrunc(B, NewType, B->getName() + ".trunc"); |
| 1356 | CallInst *Call = Builder.CreateCall(F, {TruncA, TruncB}, "sadd"); |
| 1357 | Value *Add = Builder.CreateExtractValue(Call, 0, "sadd.result"); |
| 1358 | Value *ZExt = Builder.CreateZExt(Add, OrigAdd->getType()); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1359 | |
| 1360 | // The inner add was the result of the narrow add, zero extended to the |
| 1361 | // wider type. Replace it with the result computed by the intrinsic. |
| 1362 | IC.replaceInstUsesWith(*OrigAdd, ZExt); |
| 1363 | |
| 1364 | // The original icmp gets replaced with the overflow value. |
| 1365 | return ExtractValueInst::Create(Call, 1, "sadd.overflow"); |
| 1366 | } |
| 1367 | |
| 1368 | // Fold icmp Pred X, C. |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1369 | Instruction *InstCombiner::foldICmpWithConstant(ICmpInst &Cmp) { |
| 1370 | CmpInst::Predicate Pred = Cmp.getPredicate(); |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1371 | Value *X = Cmp.getOperand(0); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1372 | |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1373 | const APInt *C; |
| 1374 | if (!match(Cmp.getOperand(1), m_APInt(C))) |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1375 | return nullptr; |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1376 | |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1377 | Value *A = nullptr, *B = nullptr; |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1378 | |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1379 | // Match the following pattern, which is a common idiom when writing |
| 1380 | // overflow-safe integer arithmetic functions. The source performs an addition |
| 1381 | // in wider type and explicitly checks for overflow using comparisons against |
| 1382 | // INT_MIN and INT_MAX. Simplify by using the sadd_with_overflow intrinsic. |
| 1383 | // |
| 1384 | // TODO: This could probably be generalized to handle other overflow-safe |
| 1385 | // operations if we worked out the formulas to compute the appropriate magic |
| 1386 | // constants. |
| 1387 | // |
| 1388 | // sum = a + b |
| 1389 | // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8 |
| 1390 | { |
| 1391 | ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI |
| 1392 | if (Pred == ICmpInst::ICMP_UGT && |
| 1393 | match(X, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2)))) |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 1394 | if (Instruction *Res = processUGT_ADDCST_ADD( |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1395 | Cmp, A, B, CI2, cast<ConstantInt>(Cmp.getOperand(1)), *this)) |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1396 | return Res; |
| 1397 | } |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1398 | |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1399 | // (icmp sgt smin(PosA, B) 0) -> (icmp sgt B 0) |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1400 | if (C->isNullValue() && Pred == ICmpInst::ICMP_SGT) { |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1401 | SelectPatternResult SPR = matchSelectPattern(X, A, B); |
| 1402 | if (SPR.Flavor == SPF_SMIN) { |
Craig Topper | d45185f | 2017-05-26 18:23:57 +0000 | [diff] [blame] | 1403 | if (isKnownPositive(A, DL, 0, &AC, &Cmp, &DT)) |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1404 | return new ICmpInst(Pred, B, Cmp.getOperand(1)); |
Craig Topper | d45185f | 2017-05-26 18:23:57 +0000 | [diff] [blame] | 1405 | if (isKnownPositive(B, DL, 0, &AC, &Cmp, &DT)) |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1406 | return new ICmpInst(Pred, A, Cmp.getOperand(1)); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1407 | } |
Sanjay Patel | 40c53ea | 2016-09-15 16:23:20 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // FIXME: Use m_APInt to allow folds for splat constants. |
| 1411 | ConstantInt *CI = dyn_cast<ConstantInt>(Cmp.getOperand(1)); |
| 1412 | if (!CI) |
| 1413 | return nullptr; |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1414 | |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1415 | // Canonicalize icmp instructions based on dominating conditions. |
| 1416 | BasicBlock *Parent = Cmp.getParent(); |
| 1417 | BasicBlock *Dom = Parent->getSinglePredecessor(); |
| 1418 | auto *BI = Dom ? dyn_cast<BranchInst>(Dom->getTerminator()) : nullptr; |
| 1419 | ICmpInst::Predicate Pred2; |
| 1420 | BasicBlock *TrueBB, *FalseBB; |
| 1421 | ConstantInt *CI2; |
| 1422 | if (BI && match(BI, m_Br(m_ICmp(Pred2, m_Specific(X), m_ConstantInt(CI2)), |
| 1423 | TrueBB, FalseBB)) && |
| 1424 | TrueBB != FalseBB) { |
| 1425 | ConstantRange CR = |
| 1426 | ConstantRange::makeAllowedICmpRegion(Pred, CI->getValue()); |
| 1427 | ConstantRange DominatingCR = |
| 1428 | (Parent == TrueBB) |
| 1429 | ? ConstantRange::makeExactICmpRegion(Pred2, CI2->getValue()) |
| 1430 | : ConstantRange::makeExactICmpRegion( |
| 1431 | CmpInst::getInversePredicate(Pred2), CI2->getValue()); |
| 1432 | ConstantRange Intersection = DominatingCR.intersectWith(CR); |
| 1433 | ConstantRange Difference = DominatingCR.difference(CR); |
| 1434 | if (Intersection.isEmptySet()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1435 | return replaceInstUsesWith(Cmp, Builder.getFalse()); |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1436 | if (Difference.isEmptySet()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1437 | return replaceInstUsesWith(Cmp, Builder.getTrue()); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1438 | |
Sanjay Patel | 9745983 | 2016-09-15 15:11:12 +0000 | [diff] [blame] | 1439 | // If this is a normal comparison, it demands all bits. If it is a sign |
| 1440 | // bit comparison, it only demands the sign bit. |
| 1441 | bool UnusedBit; |
| 1442 | bool IsSignBit = isSignBitCheck(Pred, CI->getValue(), UnusedBit); |
| 1443 | |
| 1444 | // Canonicalizing a sign bit comparison that gets used in a branch, |
| 1445 | // pessimizes codegen by generating branch on zero instruction instead |
| 1446 | // of a test and branch. So we avoid canonicalizing in such situations |
| 1447 | // because test and branch instruction has better branch displacement |
| 1448 | // than compare and branch instruction. |
Eric Christopher | a95aac3 | 2017-06-30 01:57:48 +0000 | [diff] [blame] | 1449 | if (Cmp.isEquality() || (IsSignBit && hasBranchUse(Cmp))) |
| 1450 | return nullptr; |
| 1451 | |
| 1452 | if (auto *AI = Intersection.getSingleElement()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1453 | return new ICmpInst(ICmpInst::ICMP_EQ, X, Builder.getInt(*AI)); |
Eric Christopher | a95aac3 | 2017-06-30 01:57:48 +0000 | [diff] [blame] | 1454 | if (auto *AD = Difference.getSingleElement()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1455 | return new ICmpInst(ICmpInst::ICMP_NE, X, Builder.getInt(*AD)); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | return nullptr; |
| 1459 | } |
| 1460 | |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1461 | /// Fold icmp (trunc X, Y), C. |
| 1462 | Instruction *InstCombiner::foldICmpTruncConstant(ICmpInst &Cmp, |
Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 1463 | TruncInst *Trunc, |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1464 | const APInt *C) { |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1465 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 1466 | Value *X = Trunc->getOperand(0); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1467 | if (C->isOneValue() && C->getBitWidth() > 1) { |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1468 | // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1 |
| 1469 | Value *V = nullptr; |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1470 | if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V)))) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1471 | return new ICmpInst(ICmpInst::ICMP_SLT, V, |
| 1472 | ConstantInt::get(V->getType(), 1)); |
| 1473 | } |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (Cmp.isEquality() && Trunc->hasOneUse()) { |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1476 | // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all |
| 1477 | // of the high bits truncated out of x are known. |
Sanjay Patel | 40e8ca4 | 2016-08-18 20:28:54 +0000 | [diff] [blame] | 1478 | unsigned DstBits = Trunc->getType()->getScalarSizeInBits(), |
| 1479 | SrcBits = X->getType()->getScalarSizeInBits(); |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 1480 | KnownBits Known = computeKnownBits(X, 0, &Cmp); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1481 | |
| 1482 | // If all the high bits are known, we can do this xform. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1483 | if ((Known.Zero | Known.One).countLeadingOnes() >= SrcBits - DstBits) { |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1484 | // Pull in the high bits from known-ones set. |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1485 | APInt NewRHS = C->zext(SrcBits); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1486 | NewRHS |= Known.One & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits); |
Sanjay Patel | 40e8ca4 | 2016-08-18 20:28:54 +0000 | [diff] [blame] | 1487 | return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), NewRHS)); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1488 | } |
| 1489 | } |
Sanjay Patel | 5f4ce4e | 2016-08-18 20:25:16 +0000 | [diff] [blame] | 1490 | |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1491 | return nullptr; |
| 1492 | } |
| 1493 | |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1494 | /// Fold icmp (xor X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 1495 | Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp, |
| 1496 | BinaryOperator *Xor, |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1497 | const APInt *C) { |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1498 | Value *X = Xor->getOperand(0); |
| 1499 | Value *Y = Xor->getOperand(1); |
Sanjay Patel | daffec91 | 2016-08-17 19:45:18 +0000 | [diff] [blame] | 1500 | const APInt *XorC; |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1501 | if (!match(Y, m_APInt(XorC))) |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1502 | return nullptr; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1503 | |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1504 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 1505 | // fold the xor. |
| 1506 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1507 | if ((Pred == ICmpInst::ICMP_SLT && C->isNullValue()) || |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1508 | (Pred == ICmpInst::ICMP_SGT && C->isAllOnesValue())) { |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1509 | |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1510 | // If the sign bit of the XorCst is not set, there is no change to |
| 1511 | // the operation, just stop using the Xor. |
Sanjay Patel | daffec91 | 2016-08-17 19:45:18 +0000 | [diff] [blame] | 1512 | if (!XorC->isNegative()) { |
| 1513 | Cmp.setOperand(0, X); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1514 | Worklist.Add(Xor); |
| 1515 | return &Cmp; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1518 | // Was the old condition true if the operand is positive? |
| 1519 | bool isTrueIfPositive = Pred == ICmpInst::ICMP_SGT; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1520 | |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1521 | // If so, the new one isn't. |
| 1522 | isTrueIfPositive ^= true; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1523 | |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1524 | Constant *CmpConstant = cast<Constant>(Cmp.getOperand(1)); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1525 | if (isTrueIfPositive) |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1526 | return new ICmpInst(ICmpInst::ICMP_SGT, X, SubOne(CmpConstant)); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1527 | else |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1528 | return new ICmpInst(ICmpInst::ICMP_SLT, X, AddOne(CmpConstant)); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1529 | } |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1530 | |
| 1531 | if (Xor->hasOneUse()) { |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1532 | // (icmp u/s (xor X SignMask), C) -> (icmp s/u X, (xor C SignMask)) |
| 1533 | if (!Cmp.isEquality() && XorC->isSignMask()) { |
Sanjay Patel | daffec91 | 2016-08-17 19:45:18 +0000 | [diff] [blame] | 1534 | Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate() |
| 1535 | : Cmp.getSignedPredicate(); |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1536 | return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), *C ^ *XorC)); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1539 | // (icmp u/s (xor X ~SignMask), C) -> (icmp s/u X, (xor C ~SignMask)) |
Sanjay Patel | daffec91 | 2016-08-17 19:45:18 +0000 | [diff] [blame] | 1540 | if (!Cmp.isEquality() && XorC->isMaxSignedValue()) { |
| 1541 | Pred = Cmp.isSigned() ? Cmp.getUnsignedPredicate() |
| 1542 | : Cmp.getSignedPredicate(); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1543 | Pred = Cmp.getSwappedPredicate(Pred); |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1544 | return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), *C ^ *XorC)); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | // (icmp ugt (xor X, C), ~C) -> (icmp ult X, C) |
| 1549 | // iff -C is a power of 2 |
Sanjay Patel | daffec91 | 2016-08-17 19:45:18 +0000 | [diff] [blame] | 1550 | if (Pred == ICmpInst::ICMP_UGT && *XorC == ~(*C) && (*C + 1).isPowerOf2()) |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1551 | return new ICmpInst(ICmpInst::ICMP_ULT, X, Y); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1552 | |
| 1553 | // (icmp ult (xor X, C), -C) -> (icmp uge X, C) |
| 1554 | // iff -C is a power of 2 |
Sanjay Patel | daffec91 | 2016-08-17 19:45:18 +0000 | [diff] [blame] | 1555 | if (Pred == ICmpInst::ICMP_ULT && *XorC == -(*C) && C->isPowerOf2()) |
Sanjay Patel | 4c5e60d | 2016-08-18 14:10:48 +0000 | [diff] [blame] | 1556 | return new ICmpInst(ICmpInst::ICMP_UGE, X, Y); |
Sanjay Patel | 6d5f448 | 2016-08-17 19:23:42 +0000 | [diff] [blame] | 1557 | |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1558 | return nullptr; |
| 1559 | } |
| 1560 | |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 1561 | /// Fold icmp (and (sh X, Y), C2), C1. |
| 1562 | Instruction *InstCombiner::foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1563 | const APInt *C1, const APInt *C2) { |
| 1564 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(And->getOperand(0)); |
| 1565 | if (!Shift || !Shift->isShift()) |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1566 | return nullptr; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1567 | |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1568 | // If this is: (X >> C3) & C2 != C1 (where any shift and any compare could |
| 1569 | // exist), turn it into (X & (C2 << C3)) != (C1 << C3). This happens a LOT in |
| 1570 | // code produced by the clang front-end, for bitfield access. |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1571 | // This seemingly simple opportunity to fold away a shift turns out to be |
| 1572 | // rather complicated. See PR17827 for details. |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1573 | unsigned ShiftOpcode = Shift->getOpcode(); |
| 1574 | bool IsShl = ShiftOpcode == Instruction::Shl; |
| 1575 | const APInt *C3; |
| 1576 | if (match(Shift->getOperand(1), m_APInt(C3))) { |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1577 | bool CanFold = false; |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1578 | if (ShiftOpcode == Instruction::AShr) { |
| 1579 | // There may be some constraints that make this possible, but nothing |
| 1580 | // simple has been discovered yet. |
| 1581 | CanFold = false; |
| 1582 | } else if (ShiftOpcode == Instruction::Shl) { |
| 1583 | // For a left shift, we can fold if the comparison is not signed. We can |
| 1584 | // also fold a signed comparison if the mask value and comparison value |
| 1585 | // are not negative. These constraints may not be obvious, but we can |
| 1586 | // prove that they are correct using an SMT solver. |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1587 | if (!Cmp.isSigned() || (!C2->isNegative() && !C1->isNegative())) |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1588 | CanFold = true; |
| 1589 | } else if (ShiftOpcode == Instruction::LShr) { |
| 1590 | // For a logical right shift, we can fold if the comparison is not signed. |
| 1591 | // We can also fold a signed comparison if the shifted mask value and the |
| 1592 | // shifted comparison value are not negative. These constraints may not be |
| 1593 | // obvious, but we can prove that they are correct using an SMT solver. |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1594 | if (!Cmp.isSigned() || |
| 1595 | (!C2->shl(*C3).isNegative() && !C1->shl(*C3).isNegative())) |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1596 | CanFold = true; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1599 | if (CanFold) { |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1600 | APInt NewCst = IsShl ? C1->lshr(*C3) : C1->shl(*C3); |
| 1601 | APInt SameAsC1 = IsShl ? NewCst.shl(*C3) : NewCst.lshr(*C3); |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1602 | // Check to see if we are shifting out any of the bits being compared. |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1603 | if (SameAsC1 != *C1) { |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1604 | // If we shifted bits out, the fold is not going to work out. As a |
| 1605 | // special case, check to see if this means that the result is always |
| 1606 | // true or false now. |
| 1607 | if (Cmp.getPredicate() == ICmpInst::ICMP_EQ) |
Sanjay Patel | 1c608f4 | 2016-09-08 16:54:02 +0000 | [diff] [blame] | 1608 | return replaceInstUsesWith(Cmp, ConstantInt::getFalse(Cmp.getType())); |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1609 | if (Cmp.getPredicate() == ICmpInst::ICMP_NE) |
Sanjay Patel | 1c608f4 | 2016-09-08 16:54:02 +0000 | [diff] [blame] | 1610 | return replaceInstUsesWith(Cmp, ConstantInt::getTrue(Cmp.getType())); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1611 | } else { |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1612 | Cmp.setOperand(1, ConstantInt::get(And->getType(), NewCst)); |
| 1613 | APInt NewAndCst = IsShl ? C2->lshr(*C3) : C2->shl(*C3); |
| 1614 | And->setOperand(1, ConstantInt::get(And->getType(), NewAndCst)); |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1615 | And->setOperand(0, Shift->getOperand(0)); |
| 1616 | Worklist.Add(Shift); // Shift is dead. |
| 1617 | return &Cmp; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1618 | } |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1619 | } |
| 1620 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1621 | |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1622 | // Turn ((X >> Y) & C2) == 0 into (X & (C2 << Y)) == 0. The latter is |
| 1623 | // preferable because it allows the C2 << Y expression to be hoisted out of a |
| 1624 | // loop if Y is invariant and X is not. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1625 | if (Shift->hasOneUse() && C1->isNullValue() && Cmp.isEquality() && |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1626 | !Shift->isArithmeticShift() && !isa<Constant>(Shift->getOperand(0))) { |
| 1627 | // Compute C2 << Y. |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1628 | Value *NewShift = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1629 | IsShl ? Builder.CreateLShr(And->getOperand(1), Shift->getOperand(1)) |
| 1630 | : Builder.CreateShl(And->getOperand(1), Shift->getOperand(1)); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1631 | |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1632 | // Compute X & (C2 << Y). |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1633 | Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift); |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1634 | Cmp.setOperand(0, NewAnd); |
| 1635 | return &Cmp; |
| 1636 | } |
| 1637 | |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 1638 | return nullptr; |
| 1639 | } |
| 1640 | |
| 1641 | /// Fold icmp (and X, C2), C1. |
| 1642 | Instruction *InstCombiner::foldICmpAndConstConst(ICmpInst &Cmp, |
| 1643 | BinaryOperator *And, |
| 1644 | const APInt *C1) { |
Sanjay Patel | 6b49097 | 2016-09-04 14:32:15 +0000 | [diff] [blame] | 1645 | const APInt *C2; |
| 1646 | if (!match(And->getOperand(1), m_APInt(C2))) |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 1647 | return nullptr; |
| 1648 | |
| 1649 | if (!And->hasOneUse() || !And->getOperand(0)->hasOneUse()) |
| 1650 | return nullptr; |
| 1651 | |
Sanjay Patel | 6b49097 | 2016-09-04 14:32:15 +0000 | [diff] [blame] | 1652 | // If the LHS is an 'and' of a truncate and we can widen the and/compare to |
| 1653 | // the input width without changing the value produced, eliminate the cast: |
| 1654 | // |
| 1655 | // icmp (and (trunc W), C2), C1 -> icmp (and W, C2'), C1' |
| 1656 | // |
| 1657 | // We can do this transformation if the constants do not have their sign bits |
| 1658 | // set or if it is an equality comparison. Extending a relational comparison |
| 1659 | // when we're checking the sign bit would not work. |
| 1660 | Value *W; |
| 1661 | if (match(And->getOperand(0), m_Trunc(m_Value(W))) && |
| 1662 | (Cmp.isEquality() || (!C1->isNegative() && !C2->isNegative()))) { |
| 1663 | // TODO: Is this a good transform for vectors? Wider types may reduce |
| 1664 | // throughput. Should this transform be limited (even for scalars) by using |
Sanjay Patel | 2217f75 | 2017-01-31 17:25:42 +0000 | [diff] [blame] | 1665 | // shouldChangeType()? |
Sanjay Patel | 6b49097 | 2016-09-04 14:32:15 +0000 | [diff] [blame] | 1666 | if (!Cmp.getType()->isVectorTy()) { |
| 1667 | Type *WideType = W->getType(); |
| 1668 | unsigned WideScalarBits = WideType->getScalarSizeInBits(); |
| 1669 | Constant *ZextC1 = ConstantInt::get(WideType, C1->zext(WideScalarBits)); |
| 1670 | Constant *ZextC2 = ConstantInt::get(WideType, C2->zext(WideScalarBits)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1671 | Value *NewAnd = Builder.CreateAnd(W, ZextC2, And->getName()); |
Sanjay Patel | 6b49097 | 2016-09-04 14:32:15 +0000 | [diff] [blame] | 1672 | return new ICmpInst(Cmp.getPredicate(), NewAnd, ZextC1); |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 1676 | if (Instruction *I = foldICmpAndShift(Cmp, And, C1, C2)) |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 1677 | return I; |
| 1678 | |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1679 | // (icmp pred (and (or (lshr A, B), A), 1), 0) --> |
Sanjay Patel | 6b49097 | 2016-09-04 14:32:15 +0000 | [diff] [blame] | 1680 | // (icmp pred (and A, (or (shl 1, B), 1), 0)) |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1681 | // |
| 1682 | // iff pred isn't signed |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1683 | if (!Cmp.isSigned() && C1->isNullValue() && |
| 1684 | match(And->getOperand(1), m_One())) { |
Sanjay Patel | def931e | 2016-09-07 20:50:44 +0000 | [diff] [blame] | 1685 | Constant *One = cast<Constant>(And->getOperand(1)); |
| 1686 | Value *Or = And->getOperand(0); |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1687 | Value *A, *B, *LShr; |
Sanjay Patel | def931e | 2016-09-07 20:50:44 +0000 | [diff] [blame] | 1688 | if (match(Or, m_Or(m_Value(LShr), m_Value(A))) && |
| 1689 | match(LShr, m_LShr(m_Specific(A), m_Value(B)))) { |
| 1690 | unsigned UsesRemoved = 0; |
| 1691 | if (And->hasOneUse()) |
| 1692 | ++UsesRemoved; |
| 1693 | if (Or->hasOneUse()) |
| 1694 | ++UsesRemoved; |
| 1695 | if (LShr->hasOneUse()) |
| 1696 | ++UsesRemoved; |
| 1697 | |
| 1698 | // Compute A & ((1 << B) | 1) |
| 1699 | Value *NewOr = nullptr; |
| 1700 | if (auto *C = dyn_cast<Constant>(B)) { |
| 1701 | if (UsesRemoved >= 1) |
| 1702 | NewOr = ConstantExpr::getOr(ConstantExpr::getNUWShl(One, C), One); |
| 1703 | } else { |
| 1704 | if (UsesRemoved >= 3) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1705 | NewOr = Builder.CreateOr(Builder.CreateShl(One, B, LShr->getName(), |
| 1706 | /*HasNUW=*/true), |
| 1707 | One, Or->getName()); |
Sanjay Patel | def931e | 2016-09-07 20:50:44 +0000 | [diff] [blame] | 1708 | } |
| 1709 | if (NewOr) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1710 | Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName()); |
Sanjay Patel | def931e | 2016-09-07 20:50:44 +0000 | [diff] [blame] | 1711 | Cmp.setOperand(0, NewAnd); |
| 1712 | return &Cmp; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1715 | } |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1716 | |
Sanjay Patel | def931e | 2016-09-07 20:50:44 +0000 | [diff] [blame] | 1717 | // (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a |
Craig Topper | 562bf99 | 2017-09-20 21:18:17 +0000 | [diff] [blame] | 1718 | // result greater than C1. Also handle (X & C2) < C1 --> (X & C2) == 0. |
| 1719 | if (!C2->isNullValue()) { |
| 1720 | unsigned NumTZ = C2->countTrailingZeros(); |
| 1721 | if (Cmp.getPredicate() == ICmpInst::ICMP_UGT && |
| 1722 | NumTZ >= C1->getActiveBits()) { |
| 1723 | Constant *Zero = Constant::getNullValue(And->getType()); |
| 1724 | return new ICmpInst(ICmpInst::ICMP_NE, And, Zero); |
| 1725 | } |
| 1726 | if (Cmp.getPredicate() == ICmpInst::ICMP_ULT && |
| 1727 | NumTZ >= C1->ceilLogBase2()) { |
| 1728 | Constant *Zero = Constant::getNullValue(And->getType()); |
| 1729 | return new ICmpInst(ICmpInst::ICMP_EQ, And, Zero); |
| 1730 | } |
Sanjay Patel | da9c562 | 2016-08-26 17:15:22 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Sanjay Patel | d3c7bb28 | 2016-08-26 16:42:33 +0000 | [diff] [blame] | 1733 | return nullptr; |
| 1734 | } |
| 1735 | |
| 1736 | /// Fold icmp (and X, Y), C. |
| 1737 | Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &Cmp, |
| 1738 | BinaryOperator *And, |
| 1739 | const APInt *C) { |
| 1740 | if (Instruction *I = foldICmpAndConstConst(Cmp, And, C)) |
| 1741 | return I; |
| 1742 | |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1743 | // 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] | 1744 | |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1745 | // Try to optimize things like "A[i] & 42 == 0" to index computations. |
| 1746 | Value *X = And->getOperand(0); |
| 1747 | Value *Y = And->getOperand(1); |
| 1748 | if (auto *LI = dyn_cast<LoadInst>(X)) |
| 1749 | if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) |
| 1750 | if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1751 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1752 | !LI->isVolatile() && isa<ConstantInt>(Y)) { |
| 1753 | ConstantInt *C2 = cast<ConstantInt>(Y); |
| 1754 | if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, Cmp, C2)) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1755 | return Res; |
| 1756 | } |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1757 | |
| 1758 | if (!Cmp.isEquality()) |
| 1759 | return nullptr; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1760 | |
| 1761 | // X & -C == -C -> X > u ~C |
| 1762 | // X & -C != -C -> X <= u ~C |
| 1763 | // iff C is a power of 2 |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1764 | if (Cmp.getOperand(1) == Y && (-(*C)).isPowerOf2()) { |
| 1765 | auto NewPred = Cmp.getPredicate() == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGT |
| 1766 | : CmpInst::ICMP_ULE; |
| 1767 | return new ICmpInst(NewPred, X, SubOne(cast<Constant>(Cmp.getOperand(1)))); |
| 1768 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1769 | |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1770 | // (X & C2) == 0 -> (trunc X) >= 0 |
| 1771 | // (X & C2) != 0 -> (trunc X) < 0 |
| 1772 | // iff C2 is a power of 2 and it masks the sign bit of a legal integer type. |
| 1773 | const APInt *C2; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1774 | if (And->hasOneUse() && C->isNullValue() && match(Y, m_APInt(C2))) { |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1775 | int32_t ExactLogBase2 = C2->exactLogBase2(); |
| 1776 | if (ExactLogBase2 != -1 && DL.isLegalInteger(ExactLogBase2 + 1)) { |
| 1777 | Type *NTy = IntegerType::get(Cmp.getContext(), ExactLogBase2 + 1); |
| 1778 | if (And->getType()->isVectorTy()) |
| 1779 | NTy = VectorType::get(NTy, And->getType()->getVectorNumElements()); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1780 | Value *Trunc = Builder.CreateTrunc(X, NTy); |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1781 | auto NewPred = Cmp.getPredicate() == CmpInst::ICMP_EQ ? CmpInst::ICMP_SGE |
| 1782 | : CmpInst::ICMP_SLT; |
| 1783 | return new ICmpInst(NewPred, Trunc, Constant::getNullValue(NTy)); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1784 | } |
| 1785 | } |
Sanjay Patel | 5c5311f | 2016-08-28 18:18:00 +0000 | [diff] [blame] | 1786 | |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1787 | return nullptr; |
| 1788 | } |
| 1789 | |
Sanjay Patel | 943e92e | 2016-08-17 16:30:43 +0000 | [diff] [blame] | 1790 | /// Fold icmp (or X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 1791 | Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, |
Sanjay Patel | 943e92e | 2016-08-17 16:30:43 +0000 | [diff] [blame] | 1792 | const APInt *C) { |
Sanjay Patel | 943e92e | 2016-08-17 16:30:43 +0000 | [diff] [blame] | 1793 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1794 | if (C->isOneValue()) { |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1795 | // icmp slt signum(V) 1 --> icmp slt V, 1 |
| 1796 | Value *V = nullptr; |
Sanjay Patel | 943e92e | 2016-08-17 16:30:43 +0000 | [diff] [blame] | 1797 | if (Pred == ICmpInst::ICMP_SLT && match(Or, m_Signum(m_Value(V)))) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1798 | return new ICmpInst(ICmpInst::ICMP_SLT, V, |
| 1799 | ConstantInt::get(V->getType(), 1)); |
| 1800 | } |
| 1801 | |
Sanjay Patel | 50c82c4 | 2017-04-05 17:57:05 +0000 | [diff] [blame] | 1802 | // X | C == C --> X <=u C |
| 1803 | // X | C != C --> X >u C |
| 1804 | // iff C+1 is a power of 2 (C is a bitmask of the low bits) |
| 1805 | if (Cmp.isEquality() && Cmp.getOperand(1) == Or->getOperand(1) && |
| 1806 | (*C + 1).isPowerOf2()) { |
| 1807 | Pred = (Pred == CmpInst::ICMP_EQ) ? CmpInst::ICMP_ULE : CmpInst::ICMP_UGT; |
| 1808 | return new ICmpInst(Pred, Or->getOperand(0), Or->getOperand(1)); |
| 1809 | } |
| 1810 | |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1811 | if (!Cmp.isEquality() || !C->isNullValue() || !Or->hasOneUse()) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1812 | return nullptr; |
| 1813 | |
| 1814 | Value *P, *Q; |
Sanjay Patel | 943e92e | 2016-08-17 16:30:43 +0000 | [diff] [blame] | 1815 | 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] | 1816 | // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0 |
| 1817 | // -> and (icmp eq P, null), (icmp eq Q, null). |
Reid Kleckner | a871d38 | 2016-08-19 16:53:18 +0000 | [diff] [blame] | 1818 | Value *CmpP = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1819 | Builder.CreateICmp(Pred, P, ConstantInt::getNullValue(P->getType())); |
Reid Kleckner | a871d38 | 2016-08-19 16:53:18 +0000 | [diff] [blame] | 1820 | Value *CmpQ = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1821 | Builder.CreateICmp(Pred, Q, ConstantInt::getNullValue(Q->getType())); |
Sanjay Patel | 3f4db3e | 2017-07-14 15:09:49 +0000 | [diff] [blame] | 1822 | auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; |
| 1823 | return BinaryOperator::Create(BOpc, CmpP, CmpQ); |
| 1824 | } |
| 1825 | |
| 1826 | // Are we using xors to bitwise check for a pair of (in)equalities? Convert to |
| 1827 | // a shorter form that has more potential to be folded even further. |
| 1828 | Value *X1, *X2, *X3, *X4; |
| 1829 | if (match(Or->getOperand(0), m_OneUse(m_Xor(m_Value(X1), m_Value(X2)))) && |
| 1830 | match(Or->getOperand(1), m_OneUse(m_Xor(m_Value(X3), m_Value(X4))))) { |
| 1831 | // ((X1 ^ X2) || (X3 ^ X4)) == 0 --> (X1 == X2) && (X3 == X4) |
| 1832 | // ((X1 ^ X2) || (X3 ^ X4)) != 0 --> (X1 != X2) || (X3 != X4) |
| 1833 | Value *Cmp12 = Builder.CreateICmp(Pred, X1, X2); |
| 1834 | Value *Cmp34 = Builder.CreateICmp(Pred, X3, X4); |
| 1835 | auto BOpc = Pred == CmpInst::ICMP_EQ ? Instruction::And : Instruction::Or; |
| 1836 | return BinaryOperator::Create(BOpc, Cmp12, Cmp34); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1837 | } |
Sanjay Patel | 943e92e | 2016-08-17 16:30:43 +0000 | [diff] [blame] | 1838 | |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1839 | return nullptr; |
| 1840 | } |
| 1841 | |
Sanjay Patel | 6347807 | 2016-08-18 15:44:44 +0000 | [diff] [blame] | 1842 | /// Fold icmp (mul X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 1843 | Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp, |
| 1844 | BinaryOperator *Mul, |
Sanjay Patel | 6347807 | 2016-08-18 15:44:44 +0000 | [diff] [blame] | 1845 | const APInt *C) { |
| 1846 | const APInt *MulC; |
| 1847 | if (!match(Mul->getOperand(1), m_APInt(MulC))) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1848 | return nullptr; |
| 1849 | |
Sanjay Patel | 6347807 | 2016-08-18 15:44:44 +0000 | [diff] [blame] | 1850 | // If this is a test of the sign bit and the multiply is sign-preserving with |
| 1851 | // a constant operand, use the multiply LHS operand instead. |
| 1852 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 1853 | if (isSignTest(Pred, *C) && Mul->hasNoSignedWrap()) { |
Sanjay Patel | 6347807 | 2016-08-18 15:44:44 +0000 | [diff] [blame] | 1854 | if (MulC->isNegative()) |
| 1855 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 1856 | return new ICmpInst(Pred, Mul->getOperand(0), |
| 1857 | Constant::getNullValue(Mul->getType())); |
| 1858 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1859 | |
| 1860 | return nullptr; |
| 1861 | } |
| 1862 | |
Sanjay Patel | 98cd99d | 2016-08-18 21:28:30 +0000 | [diff] [blame] | 1863 | /// Fold icmp (shl 1, Y), C. |
| 1864 | static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl, |
| 1865 | const APInt *C) { |
| 1866 | Value *Y; |
| 1867 | if (!match(Shl, m_Shl(m_One(), m_Value(Y)))) |
| 1868 | return nullptr; |
| 1869 | |
| 1870 | Type *ShiftType = Shl->getType(); |
| 1871 | uint32_t TypeBits = C->getBitWidth(); |
| 1872 | bool CIsPowerOf2 = C->isPowerOf2(); |
| 1873 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 1874 | if (Cmp.isUnsigned()) { |
| 1875 | // (1 << Y) pred C -> Y pred Log2(C) |
| 1876 | if (!CIsPowerOf2) { |
| 1877 | // (1 << Y) < 30 -> Y <= 4 |
| 1878 | // (1 << Y) <= 30 -> Y <= 4 |
| 1879 | // (1 << Y) >= 30 -> Y > 4 |
| 1880 | // (1 << Y) > 30 -> Y > 4 |
| 1881 | if (Pred == ICmpInst::ICMP_ULT) |
| 1882 | Pred = ICmpInst::ICMP_ULE; |
| 1883 | else if (Pred == ICmpInst::ICMP_UGE) |
| 1884 | Pred = ICmpInst::ICMP_UGT; |
| 1885 | } |
| 1886 | |
| 1887 | // (1 << Y) >= 2147483648 -> Y >= 31 -> Y == 31 |
| 1888 | // (1 << Y) < 2147483648 -> Y < 31 -> Y != 31 |
| 1889 | unsigned CLog2 = C->logBase2(); |
| 1890 | if (CLog2 == TypeBits - 1) { |
| 1891 | if (Pred == ICmpInst::ICMP_UGE) |
| 1892 | Pred = ICmpInst::ICMP_EQ; |
| 1893 | else if (Pred == ICmpInst::ICMP_ULT) |
| 1894 | Pred = ICmpInst::ICMP_NE; |
| 1895 | } |
| 1896 | return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, CLog2)); |
| 1897 | } else if (Cmp.isSigned()) { |
| 1898 | Constant *BitWidthMinusOne = ConstantInt::get(ShiftType, TypeBits - 1); |
| 1899 | if (C->isAllOnesValue()) { |
| 1900 | // (1 << Y) <= -1 -> Y == 31 |
| 1901 | if (Pred == ICmpInst::ICMP_SLE) |
| 1902 | return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne); |
| 1903 | |
| 1904 | // (1 << Y) > -1 -> Y != 31 |
| 1905 | if (Pred == ICmpInst::ICMP_SGT) |
| 1906 | return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne); |
| 1907 | } else if (!(*C)) { |
| 1908 | // (1 << Y) < 0 -> Y == 31 |
| 1909 | // (1 << Y) <= 0 -> Y == 31 |
| 1910 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
| 1911 | return new ICmpInst(ICmpInst::ICMP_EQ, Y, BitWidthMinusOne); |
| 1912 | |
| 1913 | // (1 << Y) >= 0 -> Y != 31 |
| 1914 | // (1 << Y) > 0 -> Y != 31 |
| 1915 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) |
| 1916 | return new ICmpInst(ICmpInst::ICMP_NE, Y, BitWidthMinusOne); |
| 1917 | } |
| 1918 | } else if (Cmp.isEquality() && CIsPowerOf2) { |
| 1919 | return new ICmpInst(Pred, Y, ConstantInt::get(ShiftType, C->logBase2())); |
| 1920 | } |
| 1921 | |
| 1922 | return nullptr; |
| 1923 | } |
| 1924 | |
Sanjay Patel | 38b7506 | 2016-08-19 17:20:37 +0000 | [diff] [blame] | 1925 | /// Fold icmp (shl X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 1926 | Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, |
| 1927 | BinaryOperator *Shl, |
Sanjay Patel | 38b7506 | 2016-08-19 17:20:37 +0000 | [diff] [blame] | 1928 | const APInt *C) { |
Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 1929 | const APInt *ShiftVal; |
| 1930 | if (Cmp.isEquality() && match(Shl->getOperand(0), m_APInt(ShiftVal))) |
| 1931 | return foldICmpShlConstConst(Cmp, Shl->getOperand(1), *C, *ShiftVal); |
| 1932 | |
Sanjay Patel | fa7de60 | 2016-08-19 22:33:26 +0000 | [diff] [blame] | 1933 | const APInt *ShiftAmt; |
| 1934 | if (!match(Shl->getOperand(1), m_APInt(ShiftAmt))) |
Sanjay Patel | 38b7506 | 2016-08-19 17:20:37 +0000 | [diff] [blame] | 1935 | return foldICmpShlOne(Cmp, Shl, C); |
Sanjay Patel | a867afe | 2016-08-19 16:12:16 +0000 | [diff] [blame] | 1936 | |
Sanjay Patel | 38b7506 | 2016-08-19 17:20:37 +0000 | [diff] [blame] | 1937 | // Check that the shift amount is in range. If not, don't perform undefined |
Sanjay Patel | 940c061 | 2017-01-09 16:27:56 +0000 | [diff] [blame] | 1938 | // shifts. When the shift is visited, it will be simplified. |
Sanjay Patel | 38b7506 | 2016-08-19 17:20:37 +0000 | [diff] [blame] | 1939 | unsigned TypeBits = C->getBitWidth(); |
Sanjay Patel | fa7de60 | 2016-08-19 22:33:26 +0000 | [diff] [blame] | 1940 | if (ShiftAmt->uge(TypeBits)) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 1941 | return nullptr; |
| 1942 | |
Sanjay Patel | e38e79c | 2016-08-19 17:34:05 +0000 | [diff] [blame] | 1943 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 1944 | Value *X = Shl->getOperand(0); |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 1945 | Type *ShType = Shl->getType(); |
| 1946 | |
Sanjay Patel | 291c3d8 | 2017-01-19 16:12:10 +0000 | [diff] [blame] | 1947 | // NSW guarantees that we are only shifting out sign bits from the high bits, |
| 1948 | // so we can ASHR the compare constant without needing a mask and eliminate |
| 1949 | // the shift. |
| 1950 | if (Shl->hasNoSignedWrap()) { |
| 1951 | if (Pred == ICmpInst::ICMP_SGT) { |
| 1952 | // icmp Pred (shl nsw X, ShiftAmt), C --> icmp Pred X, (C >>s ShiftAmt) |
| 1953 | APInt ShiftedC = C->ashr(*ShiftAmt); |
| 1954 | return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); |
| 1955 | } |
| 1956 | if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) { |
| 1957 | // This is the same code as the SGT case, but assert the pre-condition |
| 1958 | // that is needed for this to work with equality predicates. |
| 1959 | assert(C->ashr(*ShiftAmt).shl(*ShiftAmt) == *C && |
| 1960 | "Compare known true or false was not folded"); |
| 1961 | APInt ShiftedC = C->ashr(*ShiftAmt); |
| 1962 | return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); |
| 1963 | } |
| 1964 | if (Pred == ICmpInst::ICMP_SLT) { |
| 1965 | // SLE is the same as above, but SLE is canonicalized to SLT, so convert: |
| 1966 | // (X << S) <=s C is equiv to X <=s (C >> S) for all C |
| 1967 | // (X << S) <s (C + 1) is equiv to X <s (C >> S) + 1 if C <s SMAX |
| 1968 | // (X << S) <s C is equiv to X <s ((C - 1) >> S) + 1 if C >s SMIN |
| 1969 | assert(!C->isMinSignedValue() && "Unexpected icmp slt"); |
| 1970 | APInt ShiftedC = (*C - 1).ashr(*ShiftAmt) + 1; |
| 1971 | return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); |
| 1972 | } |
| 1973 | // If this is a signed comparison to 0 and the shift is sign preserving, |
| 1974 | // use the shift LHS operand instead; isSignTest may change 'Pred', so only |
| 1975 | // do that if we're sure to not continue on in this function. |
| 1976 | if (isSignTest(Pred, *C)) |
| 1977 | return new ICmpInst(Pred, X, Constant::getNullValue(ShType)); |
| 1978 | } |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 1979 | |
Sanjay Patel | 291c3d8 | 2017-01-19 16:12:10 +0000 | [diff] [blame] | 1980 | // NUW guarantees that we are only shifting out zero bits from the high bits, |
| 1981 | // so we can LSHR the compare constant without needing a mask and eliminate |
| 1982 | // the shift. |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 1983 | if (Shl->hasNoUnsignedWrap()) { |
Sanjay Patel | ae23d65 | 2017-01-18 21:16:12 +0000 | [diff] [blame] | 1984 | if (Pred == ICmpInst::ICMP_UGT) { |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 1985 | // icmp Pred (shl nuw X, ShiftAmt), C --> icmp Pred X, (C >>u ShiftAmt) |
| 1986 | APInt ShiftedC = C->lshr(*ShiftAmt); |
| 1987 | return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); |
| 1988 | } |
Sanjay Patel | ae23d65 | 2017-01-18 21:16:12 +0000 | [diff] [blame] | 1989 | if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) { |
| 1990 | // This is the same code as the UGT case, but assert the pre-condition |
| 1991 | // that is needed for this to work with equality predicates. |
| 1992 | assert(C->lshr(*ShiftAmt).shl(*ShiftAmt) == *C && |
| 1993 | "Compare known true or false was not folded"); |
| 1994 | APInt ShiftedC = C->lshr(*ShiftAmt); |
| 1995 | return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); |
| 1996 | } |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 1997 | if (Pred == ICmpInst::ICMP_ULT) { |
| 1998 | // ULE is the same as above, but ULE is canonicalized to ULT, so convert: |
| 1999 | // (X << S) <=u C is equiv to X <=u (C >> S) for all C |
| 2000 | // (X << S) <u (C + 1) is equiv to X <u (C >> S) + 1 if C <u ~0u |
| 2001 | // (X << S) <u C is equiv to X <u ((C - 1) >> S) + 1 if C >u 0 |
| 2002 | assert(C->ugt(0) && "ult 0 should have been eliminated"); |
| 2003 | APInt ShiftedC = (*C - 1).lshr(*ShiftAmt) + 1; |
| 2004 | return new ICmpInst(Pred, X, ConstantInt::get(ShType, ShiftedC)); |
| 2005 | } |
| 2006 | } |
| 2007 | |
Sanjay Patel | 291c3d8 | 2017-01-19 16:12:10 +0000 | [diff] [blame] | 2008 | if (Cmp.isEquality() && Shl->hasOneUse()) { |
| 2009 | // Strength-reduce the shift into an 'and'. |
| 2010 | Constant *Mask = ConstantInt::get( |
| 2011 | ShType, |
| 2012 | APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt->getZExtValue())); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2013 | Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask"); |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 2014 | Constant *LShrC = ConstantInt::get(ShType, C->lshr(*ShiftAmt)); |
Sanjay Patel | 291c3d8 | 2017-01-19 16:12:10 +0000 | [diff] [blame] | 2015 | return new ICmpInst(Pred, And, LShrC); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2018 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 2019 | bool TrueIfSigned = false; |
Sanjay Patel | 7926366 | 2016-08-21 15:07:45 +0000 | [diff] [blame] | 2020 | if (Shl->hasOneUse() && isSignBitCheck(Pred, *C, TrueIfSigned)) { |
Sanjay Patel | 7ffcde7 | 2016-08-21 16:35:34 +0000 | [diff] [blame] | 2021 | // (X << 31) <s 0 --> (X & 1) != 0 |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2022 | Constant *Mask = ConstantInt::get( |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 2023 | ShType, |
Sanjay Patel | fa7de60 | 2016-08-19 22:33:26 +0000 | [diff] [blame] | 2024 | APInt::getOneBitSet(TypeBits, TypeBits - ShiftAmt->getZExtValue() - 1)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2025 | Value *And = Builder.CreateAnd(X, Mask, Shl->getName() + ".mask"); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2026 | return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 2027 | And, Constant::getNullValue(ShType)); |
Sanjay Patel | c0339c7 | 2016-11-01 19:19:29 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Sanjay Patel | 643d21a | 2016-08-21 17:10:07 +0000 | [diff] [blame] | 2030 | // Transform (icmp pred iM (shl iM %v, N), C) |
| 2031 | // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (C>>N)) |
| 2032 | // Transform the shl to a trunc if (trunc (C>>N)) has no loss and M-N. |
Sanjay Patel | 940c061 | 2017-01-09 16:27:56 +0000 | [diff] [blame] | 2033 | // This enables us to get rid of the shift in favor of a trunc that may be |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2034 | // free on the target. It has the additional benefit of comparing to a |
Sanjay Patel | 940c061 | 2017-01-09 16:27:56 +0000 | [diff] [blame] | 2035 | // smaller constant that may be more target-friendly. |
Sanjay Patel | fa7de60 | 2016-08-19 22:33:26 +0000 | [diff] [blame] | 2036 | unsigned Amt = ShiftAmt->getLimitedValue(TypeBits - 1); |
Sanjay Patel | f3dda13 | 2016-10-25 20:11:47 +0000 | [diff] [blame] | 2037 | if (Shl->hasOneUse() && Amt != 0 && C->countTrailingZeros() >= Amt && |
| 2038 | DL.isLegalInteger(TypeBits - Amt)) { |
Sanjay Patel | 643d21a | 2016-08-21 17:10:07 +0000 | [diff] [blame] | 2039 | Type *TruncTy = IntegerType::get(Cmp.getContext(), TypeBits - Amt); |
Sanjay Patel | 14715b3 | 2017-01-17 21:25:16 +0000 | [diff] [blame] | 2040 | if (ShType->isVectorTy()) |
| 2041 | TruncTy = VectorType::get(TruncTy, ShType->getVectorNumElements()); |
Sanjay Patel | 643d21a | 2016-08-21 17:10:07 +0000 | [diff] [blame] | 2042 | Constant *NewC = |
| 2043 | ConstantInt::get(TruncTy, C->ashr(*ShiftAmt).trunc(TypeBits - Amt)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2044 | return new ICmpInst(Pred, Builder.CreateTrunc(X, TruncTy), NewC); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | return nullptr; |
| 2048 | } |
| 2049 | |
Sanjay Patel | a392049 | 2016-08-22 20:45:06 +0000 | [diff] [blame] | 2050 | /// Fold icmp ({al}shr X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2051 | Instruction *InstCombiner::foldICmpShrConstant(ICmpInst &Cmp, |
| 2052 | BinaryOperator *Shr, |
| 2053 | const APInt *C) { |
Sanjay Patel | a392049 | 2016-08-22 20:45:06 +0000 | [diff] [blame] | 2054 | // An exact shr only shifts out zero bits, so: |
| 2055 | // icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0 |
Sanjay Patel | d64e988 | 2016-08-23 22:05:55 +0000 | [diff] [blame] | 2056 | Value *X = Shr->getOperand(0); |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2057 | CmpInst::Predicate Pred = Cmp.getPredicate(); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2058 | if (Cmp.isEquality() && Shr->isExact() && Shr->hasOneUse() && |
| 2059 | C->isNullValue()) |
Sanjay Patel | d64e988 | 2016-08-23 22:05:55 +0000 | [diff] [blame] | 2060 | return new ICmpInst(Pred, X, Cmp.getOperand(1)); |
Sanjay Patel | a392049 | 2016-08-22 20:45:06 +0000 | [diff] [blame] | 2061 | |
Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 2062 | const APInt *ShiftVal; |
| 2063 | if (Cmp.isEquality() && match(Shr->getOperand(0), m_APInt(ShiftVal))) |
| 2064 | return foldICmpShrConstConst(Cmp, Shr->getOperand(1), *C, *ShiftVal); |
| 2065 | |
Sanjay Patel | d398d4a | 2016-08-24 22:22:06 +0000 | [diff] [blame] | 2066 | const APInt *ShiftAmt; |
| 2067 | if (!match(Shr->getOperand(1), m_APInt(ShiftAmt))) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2068 | return nullptr; |
| 2069 | |
Sanjay Patel | d398d4a | 2016-08-24 22:22:06 +0000 | [diff] [blame] | 2070 | // Check that the shift amount is in range. If not, don't perform undefined |
| 2071 | // shifts. When the shift is visited it will be simplified. |
| 2072 | unsigned TypeBits = C->getBitWidth(); |
| 2073 | unsigned ShAmtVal = ShiftAmt->getLimitedValue(TypeBits); |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2074 | if (ShAmtVal >= TypeBits || ShAmtVal == 0) |
| 2075 | return nullptr; |
| 2076 | |
Sanjay Patel | d64e988 | 2016-08-23 22:05:55 +0000 | [diff] [blame] | 2077 | bool IsAShr = Shr->getOpcode() == Instruction::AShr; |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2078 | if (!Cmp.isEquality()) { |
| 2079 | // If we have an unsigned comparison and an ashr, we can't simplify this. |
| 2080 | // Similarly for signed comparisons with lshr. |
Sanjay Patel | d64e988 | 2016-08-23 22:05:55 +0000 | [diff] [blame] | 2081 | if (Cmp.isSigned() != IsAShr) |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2082 | return nullptr; |
| 2083 | |
| 2084 | // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv |
| 2085 | // by a power of 2. Since we already have logic to simplify these, |
| 2086 | // transform to div and then simplify the resultant comparison. |
Sanjay Patel | d64e988 | 2016-08-23 22:05:55 +0000 | [diff] [blame] | 2087 | if (IsAShr && (!Shr->isExact() || ShAmtVal == TypeBits - 1)) |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2088 | return nullptr; |
| 2089 | |
| 2090 | // Revisit the shift (to delete it). |
| 2091 | Worklist.Add(Shr); |
| 2092 | |
| 2093 | Constant *DivCst = ConstantInt::get( |
| 2094 | Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal)); |
| 2095 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2096 | Value *Tmp = IsAShr ? Builder.CreateSDiv(X, DivCst, "", Shr->isExact()) |
| 2097 | : Builder.CreateUDiv(X, DivCst, "", Shr->isExact()); |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2098 | |
| 2099 | Cmp.setOperand(0, Tmp); |
| 2100 | |
| 2101 | // If the builder folded the binop, just return it. |
| 2102 | BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp); |
| 2103 | if (!TheDiv) |
| 2104 | return &Cmp; |
| 2105 | |
| 2106 | // Otherwise, fold this div/compare. |
| 2107 | assert(TheDiv->getOpcode() == Instruction::SDiv || |
| 2108 | TheDiv->getOpcode() == Instruction::UDiv); |
| 2109 | |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2110 | Instruction *Res = foldICmpDivConstant(Cmp, TheDiv, C); |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2111 | assert(Res && "This div/cst should have folded!"); |
Sanjay Patel | a392049 | 2016-08-22 20:45:06 +0000 | [diff] [blame] | 2112 | return Res; |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Sanjay Patel | d398d4a | 2016-08-24 22:22:06 +0000 | [diff] [blame] | 2115 | // Handle equality comparisons of shift-by-constant. |
| 2116 | |
Sanjay Patel | 8e29774 | 2016-08-24 13:55:55 +0000 | [diff] [blame] | 2117 | // If the comparison constant changes with the shift, the comparison cannot |
| 2118 | // succeed (bits of the comparison constant cannot match the shifted value). |
| 2119 | // This should be known by InstSimplify and already be folded to true/false. |
| 2120 | assert(((IsAShr && C->shl(ShAmtVal).ashr(ShAmtVal) == *C) || |
| 2121 | (!IsAShr && C->shl(ShAmtVal).lshr(ShAmtVal) == *C)) && |
| 2122 | "Expected icmp+shr simplify did not occur."); |
| 2123 | |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2124 | // Check if the bits shifted out are known to be zero. If so, we can compare |
| 2125 | // against the unshifted value: |
| 2126 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
Sanjay Patel | d398d4a | 2016-08-24 22:22:06 +0000 | [diff] [blame] | 2127 | Constant *ShiftedCmpRHS = ConstantInt::get(Shr->getType(), *C << ShAmtVal); |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2128 | if (Shr->hasOneUse()) { |
Sanjay Patel | d398d4a | 2016-08-24 22:22:06 +0000 | [diff] [blame] | 2129 | if (Shr->isExact()) |
| 2130 | return new ICmpInst(Pred, X, ShiftedCmpRHS); |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2131 | |
Sanjay Patel | d398d4a | 2016-08-24 22:22:06 +0000 | [diff] [blame] | 2132 | // Otherwise strength reduce the shift into an 'and'. |
| 2133 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| 2134 | Constant *Mask = ConstantInt::get(Shr->getType(), Val); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2135 | Value *And = Builder.CreateAnd(X, Mask, Shr->getName() + ".mask"); |
Sanjay Patel | dcac0df | 2016-08-23 21:25:13 +0000 | [diff] [blame] | 2136 | return new ICmpInst(Pred, And, ShiftedCmpRHS); |
| 2137 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2138 | |
| 2139 | return nullptr; |
| 2140 | } |
| 2141 | |
Sanjay Patel | 12a4105 | 2016-08-18 17:37:26 +0000 | [diff] [blame] | 2142 | /// Fold icmp (udiv X, Y), C. |
| 2143 | Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp, |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2144 | BinaryOperator *UDiv, |
Sanjay Patel | 12a4105 | 2016-08-18 17:37:26 +0000 | [diff] [blame] | 2145 | const APInt *C) { |
Sanjay Patel | fa5ca2b | 2016-08-18 17:55:59 +0000 | [diff] [blame] | 2146 | const APInt *C2; |
| 2147 | if (!match(UDiv->getOperand(0), m_APInt(C2))) |
| 2148 | return nullptr; |
| 2149 | |
Craig Topper | 29c282e | 2017-06-07 07:40:29 +0000 | [diff] [blame] | 2150 | assert(*C2 != 0 && "udiv 0, X should have been simplified already."); |
Sanjay Patel | fa5ca2b | 2016-08-18 17:55:59 +0000 | [diff] [blame] | 2151 | |
| 2152 | // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1)) |
| 2153 | Value *Y = UDiv->getOperand(1); |
| 2154 | if (Cmp.getPredicate() == ICmpInst::ICMP_UGT) { |
| 2155 | assert(!C->isMaxValue() && |
| 2156 | "icmp ugt X, UINT_MAX should have been simplified already."); |
| 2157 | return new ICmpInst(ICmpInst::ICMP_ULE, Y, |
| 2158 | ConstantInt::get(Y->getType(), C2->udiv(*C + 1))); |
| 2159 | } |
| 2160 | |
| 2161 | // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C) |
| 2162 | if (Cmp.getPredicate() == ICmpInst::ICMP_ULT) { |
Craig Topper | 29c282e | 2017-06-07 07:40:29 +0000 | [diff] [blame] | 2163 | assert(*C != 0 && "icmp ult X, 0 should have been simplified already."); |
Sanjay Patel | fa5ca2b | 2016-08-18 17:55:59 +0000 | [diff] [blame] | 2164 | return new ICmpInst(ICmpInst::ICMP_UGT, Y, |
| 2165 | ConstantInt::get(Y->getType(), C2->udiv(*C))); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | return nullptr; |
| 2169 | } |
| 2170 | |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2171 | /// Fold icmp ({su}div X, Y), C. |
| 2172 | Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &Cmp, |
| 2173 | BinaryOperator *Div, |
| 2174 | const APInt *C) { |
Sanjay Patel | a7cb477 | 2016-08-30 17:10:49 +0000 | [diff] [blame] | 2175 | // Fold: icmp pred ([us]div X, C2), C -> range test |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2176 | // Fold this div into the comparison, producing a range check. |
| 2177 | // Determine, based on the divide type, what the range is being |
| 2178 | // checked. If there is an overflow on the low or high side, remember |
| 2179 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 2180 | // See: InsertRangeTest above for the kinds of replacements possible. |
Sanjay Patel | a7cb477 | 2016-08-30 17:10:49 +0000 | [diff] [blame] | 2181 | const APInt *C2; |
| 2182 | if (!match(Div->getOperand(1), m_APInt(C2))) |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2183 | return nullptr; |
| 2184 | |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2185 | // FIXME: If the operand types don't match the type of the divide |
| 2186 | // then don't attempt this transform. The code below doesn't have the |
| 2187 | // logic to deal with a signed divide and an unsigned compare (and |
Sanjay Patel | a7cb477 | 2016-08-30 17:10:49 +0000 | [diff] [blame] | 2188 | // vice versa). This is because (x /s C2) <s C produces different |
| 2189 | // results than (x /s C2) <u C or (x /u C2) <s C or even |
| 2190 | // (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] | 2191 | // work. :( The if statement below tests that condition and bails |
| 2192 | // if it finds it. |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2193 | bool DivIsSigned = Div->getOpcode() == Instruction::SDiv; |
| 2194 | if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned()) |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2195 | return nullptr; |
Sanjay Patel | a7cb477 | 2016-08-30 17:10:49 +0000 | [diff] [blame] | 2196 | |
Sanjay Patel | eea2ef7 | 2016-09-05 23:38:22 +0000 | [diff] [blame] | 2197 | // The ProdOV computation fails on divide by 0 and divide by -1. Cases with |
| 2198 | // INT_MIN will also fail if the divisor is 1. Although folds of all these |
| 2199 | // division-by-constant cases should be present, we can not assert that they |
| 2200 | // have happened before we reach this icmp instruction. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2201 | if (C2->isNullValue() || C2->isOneValue() || |
| 2202 | (DivIsSigned && C2->isAllOnesValue())) |
Sanjay Patel | eea2ef7 | 2016-09-05 23:38:22 +0000 | [diff] [blame] | 2203 | return nullptr; |
Sanjay Patel | b371457 | 2016-08-30 17:31:34 +0000 | [diff] [blame] | 2204 | |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2205 | // TODO: We could do all of the computations below using APInt. |
| 2206 | Constant *CmpRHS = cast<Constant>(Cmp.getOperand(1)); |
| 2207 | Constant *DivRHS = cast<Constant>(Div->getOperand(1)); |
Sanjay Patel | b371457 | 2016-08-30 17:31:34 +0000 | [diff] [blame] | 2208 | |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2209 | // Compute Prod = CmpRHS * DivRHS. We are essentially solving an equation of |
| 2210 | // form X / C2 = C. We solve for X by multiplying C2 (DivRHS) and C (CmpRHS). |
| 2211 | // By solving for X, we can turn this into a range check instead of computing |
| 2212 | // a divide. |
| 2213 | Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2214 | |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2215 | // Determine if the product overflows by seeing if the product is not equal to |
| 2216 | // the divide. Make sure we do the same kind of divide as in the LHS |
| 2217 | // instruction that we're folding. |
| 2218 | bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) |
| 2219 | : ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS; |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2220 | |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2221 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2222 | |
| 2223 | // If the division is known to be exact, then there is no remainder from the |
| 2224 | // 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] | 2225 | Constant *RangeSize = |
| 2226 | Div->isExact() ? ConstantInt::get(Div->getType(), 1) : DivRHS; |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2227 | |
| 2228 | // Figure out the interval that is being checked. For example, a comparison |
| 2229 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 2230 | // Compute this interval based on the constants involved and the signedness of |
| 2231 | // the compare/divide. This computes a half-open interval, keeping track of |
| 2232 | // whether either value in the interval overflows. After analysis each |
| 2233 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 2234 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 2235 | int LoOverflow = 0, HiOverflow = 0; |
| 2236 | Constant *LoBound = nullptr, *HiBound = nullptr; |
| 2237 | |
| 2238 | if (!DivIsSigned) { // udiv |
| 2239 | // e.g. X/5 op 3 --> [15, 20) |
| 2240 | LoBound = Prod; |
| 2241 | HiOverflow = LoOverflow = ProdOV; |
| 2242 | if (!HiOverflow) { |
| 2243 | // If this is not an exact divide, then many values in the range collapse |
| 2244 | // to the same result value. |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 2245 | HiOverflow = addWithOverflow(HiBound, LoBound, RangeSize, false); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2246 | } |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2247 | } else if (C2->isStrictlyPositive()) { // Divisor is > 0. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2248 | if (C->isNullValue()) { // (X / pos) op 0 |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2249 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
| 2250 | LoBound = ConstantExpr::getNeg(SubOne(RangeSize)); |
| 2251 | HiBound = RangeSize; |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2252 | } else if (C->isStrictlyPositive()) { // (X / pos) op pos |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2253 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 2254 | HiOverflow = LoOverflow = ProdOV; |
| 2255 | if (!HiOverflow) |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 2256 | HiOverflow = addWithOverflow(HiBound, Prod, RangeSize, true); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2257 | } else { // (X / pos) op neg |
| 2258 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
| 2259 | HiBound = AddOne(Prod); |
| 2260 | LoOverflow = HiOverflow = ProdOV ? -1 : 0; |
| 2261 | if (!LoOverflow) { |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2262 | Constant *DivNeg = ConstantExpr::getNeg(RangeSize); |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 2263 | LoOverflow = addWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0; |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2266 | } else if (C2->isNegative()) { // Divisor is < 0. |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2267 | if (Div->isExact()) |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2268 | RangeSize = ConstantExpr::getNeg(RangeSize); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2269 | if (C->isNullValue()) { // (X / neg) op 0 |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2270 | // e.g. X/-5 op 0 --> [-4, 5) |
| 2271 | LoBound = AddOne(RangeSize); |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2272 | HiBound = ConstantExpr::getNeg(RangeSize); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2273 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 2274 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 2275 | HiBound = nullptr; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 2276 | } |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2277 | } else if (C->isStrictlyPositive()) { // (X / neg) op pos |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2278 | // e.g. X/-5 op 3 --> [-19, -14) |
| 2279 | HiBound = AddOne(Prod); |
| 2280 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
| 2281 | if (!LoOverflow) |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 2282 | LoOverflow = addWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0; |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2283 | } else { // (X / neg) op neg |
| 2284 | LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20) |
| 2285 | LoOverflow = HiOverflow = ProdOV; |
| 2286 | if (!HiOverflow) |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 2287 | HiOverflow = subWithOverflow(HiBound, Prod, RangeSize, true); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | // Dividing by a negative swaps the condition. LT <-> GT |
| 2291 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 2292 | } |
| 2293 | |
Sanjay Patel | f7ba089 | 2016-08-26 15:53:01 +0000 | [diff] [blame] | 2294 | Value *X = Div->getOperand(0); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2295 | switch (Pred) { |
| 2296 | default: llvm_unreachable("Unhandled icmp opcode!"); |
| 2297 | case ICmpInst::ICMP_EQ: |
| 2298 | if (LoOverflow && HiOverflow) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2299 | return replaceInstUsesWith(Cmp, Builder.getFalse()); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2300 | if (HiOverflow) |
| 2301 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 2302 | ICmpInst::ICMP_UGE, X, LoBound); |
| 2303 | if (LoOverflow) |
| 2304 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 2305 | ICmpInst::ICMP_ULT, X, HiBound); |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 2306 | return replaceInstUsesWith( |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2307 | Cmp, insertRangeTest(X, LoBound->getUniqueInteger(), |
| 2308 | HiBound->getUniqueInteger(), DivIsSigned, true)); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2309 | case ICmpInst::ICMP_NE: |
| 2310 | if (LoOverflow && HiOverflow) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2311 | return replaceInstUsesWith(Cmp, Builder.getTrue()); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2312 | if (HiOverflow) |
| 2313 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT : |
| 2314 | ICmpInst::ICMP_ULT, X, LoBound); |
| 2315 | if (LoOverflow) |
| 2316 | return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE : |
| 2317 | ICmpInst::ICMP_UGE, X, HiBound); |
Sanjay Patel | 541aef4 | 2016-08-31 21:57:21 +0000 | [diff] [blame] | 2318 | return replaceInstUsesWith(Cmp, |
| 2319 | insertRangeTest(X, LoBound->getUniqueInteger(), |
| 2320 | HiBound->getUniqueInteger(), |
| 2321 | DivIsSigned, false)); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2322 | case ICmpInst::ICMP_ULT: |
| 2323 | case ICmpInst::ICMP_SLT: |
| 2324 | if (LoOverflow == +1) // Low bound is greater than input range. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2325 | return replaceInstUsesWith(Cmp, Builder.getTrue()); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2326 | if (LoOverflow == -1) // Low bound is less than input range. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2327 | return replaceInstUsesWith(Cmp, Builder.getFalse()); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2328 | return new ICmpInst(Pred, X, LoBound); |
| 2329 | case ICmpInst::ICMP_UGT: |
| 2330 | case ICmpInst::ICMP_SGT: |
| 2331 | if (HiOverflow == +1) // High bound greater than input range. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2332 | return replaceInstUsesWith(Cmp, Builder.getFalse()); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2333 | if (HiOverflow == -1) // High bound less than input range. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2334 | return replaceInstUsesWith(Cmp, Builder.getTrue()); |
Sanjay Patel | 1655414 | 2016-08-24 23:03:36 +0000 | [diff] [blame] | 2335 | if (Pred == ICmpInst::ICMP_UGT) |
| 2336 | return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound); |
| 2337 | return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound); |
| 2338 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2339 | |
| 2340 | return nullptr; |
| 2341 | } |
| 2342 | |
Sanjay Patel | b9aa67b | 2016-08-16 21:26:10 +0000 | [diff] [blame] | 2343 | /// Fold icmp (sub X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2344 | Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, |
| 2345 | BinaryOperator *Sub, |
Sanjay Patel | b9aa67b | 2016-08-16 21:26:10 +0000 | [diff] [blame] | 2346 | const APInt *C) { |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2347 | Value *X = Sub->getOperand(0), *Y = Sub->getOperand(1); |
| 2348 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 2349 | |
| 2350 | // The following transforms are only worth it if the only user of the subtract |
| 2351 | // is the icmp. |
| 2352 | if (!Sub->hasOneUse()) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2353 | return nullptr; |
| 2354 | |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2355 | if (Sub->hasNoSignedWrap()) { |
| 2356 | // (icmp sgt (sub nsw X, Y), -1) -> (icmp sge X, Y) |
| 2357 | if (Pred == ICmpInst::ICMP_SGT && C->isAllOnesValue()) |
| 2358 | return new ICmpInst(ICmpInst::ICMP_SGE, X, Y); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2359 | |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2360 | // (icmp sgt (sub nsw X, Y), 0) -> (icmp sgt X, Y) |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2361 | if (Pred == ICmpInst::ICMP_SGT && C->isNullValue()) |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2362 | return new ICmpInst(ICmpInst::ICMP_SGT, X, Y); |
| 2363 | |
| 2364 | // (icmp slt (sub nsw X, Y), 0) -> (icmp slt X, Y) |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2365 | if (Pred == ICmpInst::ICMP_SLT && C->isNullValue()) |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2366 | return new ICmpInst(ICmpInst::ICMP_SLT, X, Y); |
| 2367 | |
| 2368 | // (icmp slt (sub nsw X, Y), 1) -> (icmp sle X, Y) |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2369 | if (Pred == ICmpInst::ICMP_SLT && C->isOneValue()) |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2370 | return new ICmpInst(ICmpInst::ICMP_SLE, X, Y); |
| 2371 | } |
| 2372 | |
| 2373 | const APInt *C2; |
| 2374 | if (!match(X, m_APInt(C2))) |
| 2375 | return nullptr; |
| 2376 | |
| 2377 | // C2 - Y <u C -> (Y | (C - 1)) == C2 |
| 2378 | // iff (C2 & (C - 1)) == C - 1 and C is a power of 2 |
| 2379 | if (Pred == ICmpInst::ICMP_ULT && C->isPowerOf2() && |
| 2380 | (*C2 & (*C - 1)) == (*C - 1)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2381 | return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateOr(Y, *C - 1), X); |
Sanjay Patel | 886a542 | 2016-09-15 18:05:17 +0000 | [diff] [blame] | 2382 | |
| 2383 | // C2 - Y >u C -> (Y | C) != C2 |
| 2384 | // iff C2 & C == C and C + 1 is a power of 2 |
| 2385 | if (Pred == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() && (*C2 & *C) == *C) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2386 | return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateOr(Y, *C), X); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2387 | |
| 2388 | return nullptr; |
| 2389 | } |
| 2390 | |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2391 | /// Fold icmp (add X, Y), C. |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2392 | Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp, |
| 2393 | BinaryOperator *Add, |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2394 | const APInt *C) { |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2395 | Value *Y = Add->getOperand(1); |
| 2396 | const APInt *C2; |
| 2397 | if (Cmp.isEquality() || !match(Y, m_APInt(C2))) |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2398 | return nullptr; |
| 2399 | |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2400 | // Fold icmp pred (add X, C2), C. |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2401 | Value *X = Add->getOperand(0); |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2402 | Type *Ty = Add->getType(); |
Sanjay Patel | 6dd2eae | 2017-02-08 16:19:36 +0000 | [diff] [blame] | 2403 | CmpInst::Predicate Pred = Cmp.getPredicate(); |
Sanjay Patel | 45b7e69 | 2017-02-12 16:40:30 +0000 | [diff] [blame] | 2404 | |
| 2405 | // If the add does not wrap, we can always adjust the compare by subtracting |
| 2406 | // the constants. Equality comparisons are handled elsewhere. SGE/SLE are |
| 2407 | // canonicalized to SGT/SLT. |
| 2408 | if (Add->hasNoSignedWrap() && |
| 2409 | (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT)) { |
| 2410 | bool Overflow; |
| 2411 | APInt NewC = C->ssub_ov(*C2, Overflow); |
| 2412 | // If there is overflow, the result must be true or false. |
| 2413 | // TODO: Can we assert there is no overflow because InstSimplify always |
| 2414 | // handles those cases? |
| 2415 | if (!Overflow) |
| 2416 | // icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2) |
| 2417 | return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC)); |
| 2418 | } |
| 2419 | |
Sanjay Patel | 6dd2eae | 2017-02-08 16:19:36 +0000 | [diff] [blame] | 2420 | auto CR = ConstantRange::makeExactICmpRegion(Pred, *C).subtract(*C2); |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2421 | const APInt &Upper = CR.getUpper(); |
| 2422 | const APInt &Lower = CR.getLower(); |
| 2423 | if (Cmp.isSigned()) { |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 2424 | if (Lower.isSignMask()) |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2425 | return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::get(Ty, Upper)); |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 2426 | if (Upper.isSignMask()) |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2427 | return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::get(Ty, Lower)); |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2428 | } else { |
| 2429 | if (Lower.isMinValue()) |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2430 | return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantInt::get(Ty, Upper)); |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2431 | if (Upper.isMinValue()) |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2432 | return new ICmpInst(ICmpInst::ICMP_UGE, X, ConstantInt::get(Ty, Lower)); |
Sanjay Patel | 60ea1b4 | 2016-08-16 22:34:42 +0000 | [diff] [blame] | 2433 | } |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2434 | |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2435 | if (!Add->hasOneUse()) |
| 2436 | return nullptr; |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2437 | |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2438 | // X+C <u C2 -> (X & -C2) == C |
| 2439 | // iff C & (C2-1) == 0 |
| 2440 | // C2 is a power of 2 |
Sanjay Patel | 6dd2eae | 2017-02-08 16:19:36 +0000 | [diff] [blame] | 2441 | if (Pred == ICmpInst::ICMP_ULT && C->isPowerOf2() && (*C2 & (*C - 1)) == 0) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2442 | return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(X, -(*C)), |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2443 | ConstantExpr::getNeg(cast<Constant>(Y))); |
| 2444 | |
| 2445 | // X+C >u C2 -> (X & ~C2) != C |
| 2446 | // iff C & C2 == 0 |
| 2447 | // C2+1 is a power of 2 |
Sanjay Patel | 6dd2eae | 2017-02-08 16:19:36 +0000 | [diff] [blame] | 2448 | if (Pred == ICmpInst::ICMP_UGT && (*C + 1).isPowerOf2() && (*C2 & *C) == 0) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2449 | return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, ~(*C)), |
Sanjay Patel | 4f7eb2a | 2016-08-17 15:24:30 +0000 | [diff] [blame] | 2450 | ConstantExpr::getNeg(cast<Constant>(Y))); |
| 2451 | |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 2452 | return nullptr; |
| 2453 | } |
| 2454 | |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2455 | bool InstCombiner::matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, |
| 2456 | Value *&RHS, ConstantInt *&Less, |
| 2457 | ConstantInt *&Equal, |
| 2458 | ConstantInt *&Greater) { |
| 2459 | // TODO: Generalize this to work with other comparison idioms or ensure |
| 2460 | // they get canonicalized into this form. |
| 2461 | |
| 2462 | // select i1 (a == b), i32 Equal, i32 (select i1 (a < b), i32 Less, i32 |
| 2463 | // Greater), where Equal, Less and Greater are placeholders for any three |
| 2464 | // constants. |
| 2465 | ICmpInst::Predicate PredA, PredB; |
| 2466 | if (match(SI->getTrueValue(), m_ConstantInt(Equal)) && |
| 2467 | match(SI->getCondition(), m_ICmp(PredA, m_Value(LHS), m_Value(RHS))) && |
| 2468 | PredA == ICmpInst::ICMP_EQ && |
| 2469 | match(SI->getFalseValue(), |
| 2470 | m_Select(m_ICmp(PredB, m_Specific(LHS), m_Specific(RHS)), |
| 2471 | m_ConstantInt(Less), m_ConstantInt(Greater))) && |
| 2472 | PredB == ICmpInst::ICMP_SLT) { |
| 2473 | return true; |
| 2474 | } |
| 2475 | return false; |
| 2476 | } |
| 2477 | |
| 2478 | Instruction *InstCombiner::foldICmpSelectConstant(ICmpInst &Cmp, |
Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 2479 | SelectInst *Select, |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2480 | ConstantInt *C) { |
| 2481 | |
| 2482 | assert(C && "Cmp RHS should be a constant int!"); |
| 2483 | // If we're testing a constant value against the result of a three way |
| 2484 | // comparison, the result can be expressed directly in terms of the |
| 2485 | // original values being compared. Note: We could possibly be more |
| 2486 | // aggressive here and remove the hasOneUse test. The original select is |
| 2487 | // really likely to simplify or sink when we remove a test of the result. |
| 2488 | Value *OrigLHS, *OrigRHS; |
| 2489 | ConstantInt *C1LessThan, *C2Equal, *C3GreaterThan; |
| 2490 | if (Cmp.hasOneUse() && |
Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 2491 | matchThreeWayIntCompare(Select, OrigLHS, OrigRHS, C1LessThan, C2Equal, |
| 2492 | C3GreaterThan)) { |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2493 | assert(C1LessThan && C2Equal && C3GreaterThan); |
| 2494 | |
| 2495 | bool TrueWhenLessThan = |
| 2496 | ConstantExpr::getCompare(Cmp.getPredicate(), C1LessThan, C) |
| 2497 | ->isAllOnesValue(); |
| 2498 | bool TrueWhenEqual = |
| 2499 | ConstantExpr::getCompare(Cmp.getPredicate(), C2Equal, C) |
| 2500 | ->isAllOnesValue(); |
| 2501 | bool TrueWhenGreaterThan = |
| 2502 | ConstantExpr::getCompare(Cmp.getPredicate(), C3GreaterThan, C) |
| 2503 | ->isAllOnesValue(); |
| 2504 | |
| 2505 | // This generates the new instruction that will replace the original Cmp |
| 2506 | // Instruction. Instead of enumerating the various combinations when |
| 2507 | // TrueWhenLessThan, TrueWhenEqual and TrueWhenGreaterThan are true versus |
| 2508 | // false, we rely on chaining of ORs and future passes of InstCombine to |
| 2509 | // simplify the OR further (i.e. a s< b || a == b becomes a s<= b). |
| 2510 | |
| 2511 | // When none of the three constants satisfy the predicate for the RHS (C), |
| 2512 | // the entire original Cmp can be simplified to a false. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2513 | Value *Cond = Builder.getFalse(); |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2514 | if (TrueWhenLessThan) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2515 | Cond = Builder.CreateOr(Cond, Builder.CreateICmp(ICmpInst::ICMP_SLT, OrigLHS, OrigRHS)); |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2516 | if (TrueWhenEqual) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2517 | Cond = Builder.CreateOr(Cond, Builder.CreateICmp(ICmpInst::ICMP_EQ, OrigLHS, OrigRHS)); |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2518 | if (TrueWhenGreaterThan) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2519 | Cond = Builder.CreateOr(Cond, Builder.CreateICmp(ICmpInst::ICMP_SGT, OrigLHS, OrigRHS)); |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2520 | |
| 2521 | return replaceInstUsesWith(Cmp, Cond); |
| 2522 | } |
| 2523 | return nullptr; |
| 2524 | } |
| 2525 | |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 2526 | /// Try to fold integer comparisons with a constant operand: icmp Pred X, C |
| 2527 | /// where X is some kind of instruction. |
| 2528 | Instruction *InstCombiner::foldICmpInstWithConstant(ICmpInst &Cmp) { |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2529 | const APInt *C; |
| 2530 | if (!match(Cmp.getOperand(1), m_APInt(C))) |
Sanjay Patel | 1e5b2d1 | 2016-08-16 16:08:11 +0000 | [diff] [blame] | 2531 | return nullptr; |
| 2532 | |
Craig Topper | a94069f | 2017-08-23 05:46:08 +0000 | [diff] [blame] | 2533 | if (auto *BO = dyn_cast<BinaryOperator>(Cmp.getOperand(0))) { |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2534 | switch (BO->getOpcode()) { |
| 2535 | case Instruction::Xor: |
| 2536 | if (Instruction *I = foldICmpXorConstant(Cmp, BO, C)) |
| 2537 | return I; |
| 2538 | break; |
| 2539 | case Instruction::And: |
| 2540 | if (Instruction *I = foldICmpAndConstant(Cmp, BO, C)) |
| 2541 | return I; |
| 2542 | break; |
| 2543 | case Instruction::Or: |
| 2544 | if (Instruction *I = foldICmpOrConstant(Cmp, BO, C)) |
| 2545 | return I; |
| 2546 | break; |
| 2547 | case Instruction::Mul: |
| 2548 | if (Instruction *I = foldICmpMulConstant(Cmp, BO, C)) |
| 2549 | return I; |
| 2550 | break; |
| 2551 | case Instruction::Shl: |
| 2552 | if (Instruction *I = foldICmpShlConstant(Cmp, BO, C)) |
| 2553 | return I; |
| 2554 | break; |
| 2555 | case Instruction::LShr: |
| 2556 | case Instruction::AShr: |
| 2557 | if (Instruction *I = foldICmpShrConstant(Cmp, BO, C)) |
| 2558 | return I; |
| 2559 | break; |
| 2560 | case Instruction::UDiv: |
| 2561 | if (Instruction *I = foldICmpUDivConstant(Cmp, BO, C)) |
| 2562 | return I; |
| 2563 | LLVM_FALLTHROUGH; |
| 2564 | case Instruction::SDiv: |
| 2565 | if (Instruction *I = foldICmpDivConstant(Cmp, BO, C)) |
| 2566 | return I; |
| 2567 | break; |
| 2568 | case Instruction::Sub: |
| 2569 | if (Instruction *I = foldICmpSubConstant(Cmp, BO, C)) |
| 2570 | return I; |
| 2571 | break; |
| 2572 | case Instruction::Add: |
| 2573 | if (Instruction *I = foldICmpAddConstant(Cmp, BO, C)) |
| 2574 | return I; |
| 2575 | break; |
| 2576 | default: |
| 2577 | break; |
| 2578 | } |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 2579 | // TODO: These folds could be refactored to be part of the above calls. |
| 2580 | if (Instruction *I = foldICmpBinOpEqualityWithConstant(Cmp, BO, C)) |
| 2581 | return I; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2582 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2583 | |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2584 | // Match against CmpInst LHS being instructions other than binary operators. |
Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (auto *SI = dyn_cast<SelectInst>(Cmp.getOperand(0))) { |
| 2587 | // For now, we only support constant integers while folding the |
| 2588 | // ICMP(SELECT)) pattern. We can extend this to support vector of integers |
| 2589 | // similar to the cases handled by binary ops above. |
| 2590 | if (ConstantInt *ConstRHS = dyn_cast<ConstantInt>(Cmp.getOperand(1))) |
| 2591 | if (Instruction *I = foldICmpSelectConstant(Cmp, SI, ConstRHS)) |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2592 | return I; |
Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | if (auto *TI = dyn_cast<TruncInst>(Cmp.getOperand(0))) { |
| 2596 | if (Instruction *I = foldICmpTruncConstant(Cmp, TI, C)) |
| 2597 | return I; |
Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 2598 | } |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 2599 | |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 2600 | if (Instruction *I = foldICmpIntrinsicWithConstant(Cmp, C)) |
| 2601 | return I; |
| 2602 | |
Sanjay Patel | 1710e7c | 2016-07-21 17:15:49 +0000 | [diff] [blame] | 2603 | return nullptr; |
| 2604 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 2605 | |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2606 | /// Fold an icmp equality instruction with binary operator LHS and constant RHS: |
| 2607 | /// icmp eq/ne BO, C. |
| 2608 | Instruction *InstCombiner::foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, |
| 2609 | BinaryOperator *BO, |
| 2610 | const APInt *C) { |
| 2611 | // TODO: Some of these folds could work with arbitrary constants, but this |
| 2612 | // function is limited to scalar and vector splat constants. |
| 2613 | if (!Cmp.isEquality()) |
Sanjay Patel | 1710e7c | 2016-07-21 17:15:49 +0000 | [diff] [blame] | 2614 | return nullptr; |
| 2615 | |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2616 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 2617 | bool isICMP_NE = Pred == ICmpInst::ICMP_NE; |
| 2618 | Constant *RHS = cast<Constant>(Cmp.getOperand(1)); |
Sanjay Patel | 51a767c | 2016-08-03 17:23:08 +0000 | [diff] [blame] | 2619 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
Sanjay Patel | 1710e7c | 2016-07-21 17:15:49 +0000 | [diff] [blame] | 2620 | |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2621 | switch (BO->getOpcode()) { |
| 2622 | case Instruction::SRem: |
| 2623 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2624 | if (C->isNullValue() && BO->hasOneUse()) { |
Sanjay Patel | 2e9675f | 2016-08-03 19:48:40 +0000 | [diff] [blame] | 2625 | const APInt *BOC; |
| 2626 | if (match(BOp1, m_APInt(BOC)) && BOC->sgt(1) && BOC->isPowerOf2()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2627 | Value *NewRem = Builder.CreateURem(BOp0, BOp1, BO->getName()); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2628 | return new ICmpInst(Pred, NewRem, |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2629 | Constant::getNullValue(BO->getType())); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2630 | } |
Sanjay Patel | 1710e7c | 2016-07-21 17:15:49 +0000 | [diff] [blame] | 2631 | } |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2632 | break; |
Sanjay Patel | 00a324e | 2016-08-03 22:08:44 +0000 | [diff] [blame] | 2633 | case Instruction::Add: { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2634 | // 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] | 2635 | const APInt *BOC; |
| 2636 | if (match(BOp1, m_APInt(BOC))) { |
| 2637 | if (BO->hasOneUse()) { |
| 2638 | Constant *SubC = ConstantExpr::getSub(RHS, cast<Constant>(BOp1)); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2639 | return new ICmpInst(Pred, BOp0, SubC); |
Sanjay Patel | 00a324e | 2016-08-03 22:08:44 +0000 | [diff] [blame] | 2640 | } |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2641 | } else if (C->isNullValue()) { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2642 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 2643 | // efficiently invertible, or if the add has just this one use. |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2644 | if (Value *NegVal = dyn_castNegVal(BOp1)) |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2645 | return new ICmpInst(Pred, BOp0, NegVal); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2646 | if (Value *NegVal = dyn_castNegVal(BOp0)) |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2647 | return new ICmpInst(Pred, NegVal, BOp1); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2648 | if (BO->hasOneUse()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2649 | Value *Neg = Builder.CreateNeg(BOp1); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2650 | Neg->takeName(BO); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2651 | return new ICmpInst(Pred, BOp0, Neg); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | break; |
Sanjay Patel | 00a324e | 2016-08-03 22:08:44 +0000 | [diff] [blame] | 2655 | } |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2656 | case Instruction::Xor: |
| 2657 | if (BO->hasOneUse()) { |
Sanjay Patel | 51a767c | 2016-08-03 17:23:08 +0000 | [diff] [blame] | 2658 | if (Constant *BOC = dyn_cast<Constant>(BOp1)) { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2659 | // For the xor case, we can xor two constants together, eliminating |
| 2660 | // the explicit xor. |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2661 | return new ICmpInst(Pred, BOp0, ConstantExpr::getXor(RHS, BOC)); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2662 | } else if (C->isNullValue()) { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2663 | // Replace ((xor A, B) != 0) with (A != B) |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2664 | return new ICmpInst(Pred, BOp0, BOp1); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | break; |
| 2668 | case Instruction::Sub: |
| 2669 | if (BO->hasOneUse()) { |
Sanjay Patel | 9d591d1 | 2016-08-04 15:19:25 +0000 | [diff] [blame] | 2670 | const APInt *BOC; |
| 2671 | if (match(BOp0, m_APInt(BOC))) { |
Sanjay Patel | 362ff5c | 2016-09-15 17:01:17 +0000 | [diff] [blame] | 2672 | // Replace ((sub BOC, B) != C) with (B != BOC-C). |
Sanjay Patel | 9d591d1 | 2016-08-04 15:19:25 +0000 | [diff] [blame] | 2673 | Constant *SubC = ConstantExpr::getSub(cast<Constant>(BOp0), RHS); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2674 | return new ICmpInst(Pred, BOp1, SubC); |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2675 | } else if (C->isNullValue()) { |
Sanjay Patel | 362ff5c | 2016-09-15 17:01:17 +0000 | [diff] [blame] | 2676 | // Replace ((sub A, B) != 0) with (A != B). |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2677 | return new ICmpInst(Pred, BOp0, BOp1); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | break; |
Sanjay Patel | b3de75d | 2016-08-04 19:12:12 +0000 | [diff] [blame] | 2681 | case Instruction::Or: { |
| 2682 | const APInt *BOC; |
| 2683 | if (match(BOp1, m_APInt(BOC)) && BO->hasOneUse() && RHS->isAllOnesValue()) { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2684 | // Comparing if all bits outside of a constant mask are set? |
| 2685 | // Replace (X | C) == -1 with (X & ~C) == ~C. |
| 2686 | // This removes the -1 constant. |
Sanjay Patel | b3de75d | 2016-08-04 19:12:12 +0000 | [diff] [blame] | 2687 | Constant *NotBOC = ConstantExpr::getNot(cast<Constant>(BOp1)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2688 | Value *And = Builder.CreateAnd(BOp0, NotBOC); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2689 | return new ICmpInst(Pred, And, NotBOC); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2690 | } |
| 2691 | break; |
Sanjay Patel | b3de75d | 2016-08-04 19:12:12 +0000 | [diff] [blame] | 2692 | } |
Sanjay Patel | d938e88 | 2016-08-04 20:05:02 +0000 | [diff] [blame] | 2693 | case Instruction::And: { |
| 2694 | const APInt *BOC; |
| 2695 | if (match(BOp1, m_APInt(BOC))) { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2696 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2697 | if (C == BOC && C->isPowerOf2()) |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2698 | return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE, |
Sanjay Patel | ab50a93 | 2016-08-02 22:38:33 +0000 | [diff] [blame] | 2699 | BO, Constant::getNullValue(RHS->getType())); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2700 | |
| 2701 | // Don't perform the following transforms if the AND has multiple uses |
| 2702 | if (!BO->hasOneUse()) |
| 2703 | break; |
| 2704 | |
| 2705 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 2706 | if (BOC->isSignMask()) { |
Sanjay Patel | 51a767c | 2016-08-03 17:23:08 +0000 | [diff] [blame] | 2707 | Constant *Zero = Constant::getNullValue(BOp0->getType()); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2708 | auto NewPred = isICMP_NE ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| 2709 | return new ICmpInst(NewPred, BOp0, Zero); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | // ((X & ~7) == 0) --> X < 8 |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2713 | if (C->isNullValue() && (~(*BOC) + 1).isPowerOf2()) { |
Sanjay Patel | d938e88 | 2016-08-04 20:05:02 +0000 | [diff] [blame] | 2714 | Constant *NegBOC = ConstantExpr::getNeg(cast<Constant>(BOp1)); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2715 | auto NewPred = isICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| 2716 | return new ICmpInst(NewPred, BOp0, NegBOC); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | break; |
Sanjay Patel | d938e88 | 2016-08-04 20:05:02 +0000 | [diff] [blame] | 2720 | } |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2721 | case Instruction::Mul: |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2722 | if (C->isNullValue() && BO->hasNoSignedWrap()) { |
Sanjay Patel | 3bade13 | 2016-08-04 22:19:27 +0000 | [diff] [blame] | 2723 | const APInt *BOC; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2724 | if (match(BOp1, m_APInt(BOC)) && !BOC->isNullValue()) { |
Sanjay Patel | 3bade13 | 2016-08-04 22:19:27 +0000 | [diff] [blame] | 2725 | // The trivial case (mul X, 0) is handled by InstSimplify. |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2726 | // General case : (mul X, C) != 0 iff X != 0 |
| 2727 | // (mul X, C) == 0 iff X == 0 |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2728 | return new ICmpInst(Pred, BOp0, Constant::getNullValue(RHS->getType())); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | break; |
Sanjay Patel | 6ebd585 | 2016-07-23 00:28:39 +0000 | [diff] [blame] | 2732 | case Instruction::UDiv: |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2733 | if (C->isNullValue()) { |
Sanjay Patel | 6ebd585 | 2016-07-23 00:28:39 +0000 | [diff] [blame] | 2734 | // (icmp eq/ne (udiv A, B), 0) -> (icmp ugt/ule i32 B, A) |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2735 | auto NewPred = isICMP_NE ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_UGT; |
| 2736 | return new ICmpInst(NewPred, BOp1, BOp0); |
Sanjay Patel | 6ebd585 | 2016-07-23 00:28:39 +0000 | [diff] [blame] | 2737 | } |
| 2738 | break; |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2739 | default: |
| 2740 | break; |
| 2741 | } |
| 2742 | return nullptr; |
| 2743 | } |
| 2744 | |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2745 | /// Fold an icmp with LLVM intrinsic and constant operand: icmp Pred II, C. |
| 2746 | Instruction *InstCombiner::foldICmpIntrinsicWithConstant(ICmpInst &Cmp, |
| 2747 | const APInt *C) { |
| 2748 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(Cmp.getOperand(0)); |
| 2749 | if (!II || !Cmp.isEquality()) |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2750 | return nullptr; |
| 2751 | |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2752 | // Handle icmp {eq|ne} <intrinsic>, Constant. |
| 2753 | Type *Ty = II->getType(); |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2754 | switch (II->getIntrinsicID()) { |
| 2755 | case Intrinsic::bswap: |
| 2756 | Worklist.Add(II); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2757 | Cmp.setOperand(0, II->getArgOperand(0)); |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2758 | Cmp.setOperand(1, ConstantInt::get(Ty, C->byteSwap())); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2759 | return &Cmp; |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2760 | |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2761 | case Intrinsic::ctlz: |
| 2762 | case Intrinsic::cttz: |
Amaury Sechet | 6bea674 | 2016-08-04 05:27:20 +0000 | [diff] [blame] | 2763 | // ctz(A) == bitwidth(A) -> A == 0 and likewise for != |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2764 | if (*C == C->getBitWidth()) { |
Sanjay Patel | 1710e7c | 2016-07-21 17:15:49 +0000 | [diff] [blame] | 2765 | Worklist.Add(II); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2766 | Cmp.setOperand(0, II->getArgOperand(0)); |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2767 | Cmp.setOperand(1, ConstantInt::getNullValue(Ty)); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2768 | return &Cmp; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2769 | } |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2770 | break; |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2771 | |
Amaury Sechet | 6bea674 | 2016-08-04 05:27:20 +0000 | [diff] [blame] | 2772 | case Intrinsic::ctpop: { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2773 | // popcount(A) == 0 -> A == 0 and likewise for != |
Amaury Sechet | 6bea674 | 2016-08-04 05:27:20 +0000 | [diff] [blame] | 2774 | // popcount(A) == bitwidth(A) -> A == -1 and likewise for != |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 2775 | bool IsZero = C->isNullValue(); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2776 | if (IsZero || *C == C->getBitWidth()) { |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2777 | Worklist.Add(II); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2778 | Cmp.setOperand(0, II->getArgOperand(0)); |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2779 | auto *NewOp = |
| 2780 | IsZero ? Constant::getNullValue(Ty) : Constant::getAllOnesValue(Ty); |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2781 | Cmp.setOperand(1, NewOp); |
| 2782 | return &Cmp; |
Amaury Sechet | 6bea674 | 2016-08-04 05:27:20 +0000 | [diff] [blame] | 2783 | } |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2784 | break; |
Sanjay Patel | 0a3d72b | 2016-09-10 15:33:39 +0000 | [diff] [blame] | 2785 | } |
Sanjay Patel | 18fa9d3 | 2016-07-21 23:27:36 +0000 | [diff] [blame] | 2786 | default: |
| 2787 | break; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2788 | } |
Sanjay Patel | b51e072 | 2017-07-02 16:05:11 +0000 | [diff] [blame] | 2789 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2790 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2793 | /// Handle icmp with constant (but not simple integer constant) RHS. |
| 2794 | Instruction *InstCombiner::foldICmpInstWithConstantNotInt(ICmpInst &I) { |
| 2795 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2796 | Constant *RHSC = dyn_cast<Constant>(Op1); |
| 2797 | Instruction *LHSI = dyn_cast<Instruction>(Op0); |
| 2798 | if (!RHSC || !LHSI) |
| 2799 | return nullptr; |
| 2800 | |
| 2801 | switch (LHSI->getOpcode()) { |
| 2802 | case Instruction::GetElementPtr: |
| 2803 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
| 2804 | if (RHSC->isNullValue() && |
| 2805 | cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices()) |
| 2806 | return new ICmpInst( |
| 2807 | I.getPredicate(), LHSI->getOperand(0), |
| 2808 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 2809 | break; |
| 2810 | case Instruction::PHI: |
| 2811 | // Only fold icmp into the PHI if the phi and icmp are in the same |
| 2812 | // block. If in the same block, we're encouraging jump threading. If |
| 2813 | // not, we are just pessimizing the code by making an i1 phi. |
| 2814 | if (LHSI->getParent() == I.getParent()) |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 2815 | if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI))) |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2816 | return NV; |
| 2817 | break; |
| 2818 | case Instruction::Select: { |
| 2819 | // If either operand of the select is a constant, we can fold the |
| 2820 | // comparison into the select arms, which will cause one to be |
| 2821 | // constant folded and the select turned into a bitwise or. |
| 2822 | Value *Op1 = nullptr, *Op2 = nullptr; |
| 2823 | ConstantInt *CI = nullptr; |
| 2824 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 2825 | Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 2826 | CI = dyn_cast<ConstantInt>(Op1); |
| 2827 | } |
| 2828 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 2829 | Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC); |
| 2830 | CI = dyn_cast<ConstantInt>(Op2); |
| 2831 | } |
| 2832 | |
| 2833 | // We only want to perform this transformation if it will not lead to |
| 2834 | // additional code. This is true if either both sides of the select |
| 2835 | // fold to a constant (in which case the icmp is replaced with a select |
| 2836 | // which will usually simplify) or this is the only user of the |
| 2837 | // select (in which case we are trading a select+icmp for a simpler |
| 2838 | // select+icmp) or all uses of the select can be replaced based on |
| 2839 | // dominance information ("Global cases"). |
| 2840 | bool Transform = false; |
| 2841 | if (Op1 && Op2) |
| 2842 | Transform = true; |
| 2843 | else if (Op1 || Op2) { |
| 2844 | // Local case |
| 2845 | if (LHSI->hasOneUse()) |
| 2846 | Transform = true; |
| 2847 | // Global cases |
| 2848 | else if (CI && !CI->isZero()) |
| 2849 | // When Op1 is constant try replacing select with second operand. |
| 2850 | // Otherwise Op2 is constant and try replacing select with first |
| 2851 | // operand. |
| 2852 | Transform = |
| 2853 | replacedSelectWithOperand(cast<SelectInst>(LHSI), &I, Op1 ? 2 : 1); |
| 2854 | } |
| 2855 | if (Transform) { |
| 2856 | if (!Op1) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2857 | Op1 = Builder.CreateICmp(I.getPredicate(), LHSI->getOperand(1), RHSC, |
| 2858 | I.getName()); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2859 | if (!Op2) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2860 | Op2 = Builder.CreateICmp(I.getPredicate(), LHSI->getOperand(2), RHSC, |
| 2861 | I.getName()); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2862 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
| 2863 | } |
| 2864 | break; |
| 2865 | } |
| 2866 | case Instruction::IntToPtr: |
| 2867 | // icmp pred inttoptr(X), null -> icmp pred X, 0 |
| 2868 | if (RHSC->isNullValue() && |
| 2869 | DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType()) |
| 2870 | return new ICmpInst( |
| 2871 | I.getPredicate(), LHSI->getOperand(0), |
| 2872 | Constant::getNullValue(LHSI->getOperand(0)->getType())); |
| 2873 | break; |
| 2874 | |
| 2875 | case Instruction::Load: |
| 2876 | // Try to optimize things like "A[i] > 4" to index computations. |
| 2877 | if (GetElementPtrInst *GEP = |
| 2878 | dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) { |
| 2879 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 2880 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 2881 | !cast<LoadInst>(LHSI)->isVolatile()) |
| 2882 | if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I)) |
| 2883 | return Res; |
| 2884 | } |
| 2885 | break; |
| 2886 | } |
| 2887 | |
| 2888 | return nullptr; |
| 2889 | } |
| 2890 | |
| 2891 | /// Try to fold icmp (binop), X or icmp X, (binop). |
Sanjay Patel | 2df38a8 | 2017-05-08 16:21:55 +0000 | [diff] [blame] | 2892 | /// TODO: A large part of this logic is duplicated in InstSimplify's |
| 2893 | /// simplifyICmpWithBinOp(). We should be able to share that and avoid the code |
| 2894 | /// duplication. |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2895 | Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) { |
| 2896 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2897 | |
| 2898 | // Special logic for binary operators. |
| 2899 | BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0); |
| 2900 | BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1); |
| 2901 | if (!BO0 && !BO1) |
| 2902 | return nullptr; |
| 2903 | |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 2904 | const CmpInst::Predicate Pred = I.getPredicate(); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2905 | bool NoOp0WrapProblem = false, NoOp1WrapProblem = false; |
| 2906 | if (BO0 && isa<OverflowingBinaryOperator>(BO0)) |
| 2907 | NoOp0WrapProblem = |
| 2908 | ICmpInst::isEquality(Pred) || |
| 2909 | (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) || |
| 2910 | (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap()); |
| 2911 | if (BO1 && isa<OverflowingBinaryOperator>(BO1)) |
| 2912 | NoOp1WrapProblem = |
| 2913 | ICmpInst::isEquality(Pred) || |
| 2914 | (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) || |
| 2915 | (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap()); |
| 2916 | |
| 2917 | // Analyze the case when either Op0 or Op1 is an add instruction. |
| 2918 | // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null). |
| 2919 | Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr; |
| 2920 | if (BO0 && BO0->getOpcode() == Instruction::Add) { |
| 2921 | A = BO0->getOperand(0); |
| 2922 | B = BO0->getOperand(1); |
| 2923 | } |
| 2924 | if (BO1 && BO1->getOpcode() == Instruction::Add) { |
| 2925 | C = BO1->getOperand(0); |
| 2926 | D = BO1->getOperand(1); |
| 2927 | } |
| 2928 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 2929 | // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. |
| 2930 | if ((A == Op1 || B == Op1) && NoOp0WrapProblem) |
| 2931 | return new ICmpInst(Pred, A == Op1 ? B : A, |
| 2932 | Constant::getNullValue(Op1->getType())); |
| 2933 | |
| 2934 | // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. |
| 2935 | if ((C == Op0 || D == Op0) && NoOp1WrapProblem) |
| 2936 | return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()), |
| 2937 | C == Op0 ? D : C); |
| 2938 | |
| 2939 | // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow. |
| 2940 | if (A && C && (A == C || A == D || B == C || B == D) && NoOp0WrapProblem && |
| 2941 | NoOp1WrapProblem && |
| 2942 | // Try not to increase register pressure. |
| 2943 | BO0->hasOneUse() && BO1->hasOneUse()) { |
| 2944 | // Determine Y and Z in the form icmp (X+Y), (X+Z). |
| 2945 | Value *Y, *Z; |
| 2946 | if (A == C) { |
| 2947 | // C + B == C + D -> B == D |
| 2948 | Y = B; |
| 2949 | Z = D; |
| 2950 | } else if (A == D) { |
| 2951 | // D + B == C + D -> B == C |
| 2952 | Y = B; |
| 2953 | Z = C; |
| 2954 | } else if (B == C) { |
| 2955 | // A + C == C + D -> A == D |
| 2956 | Y = A; |
| 2957 | Z = D; |
| 2958 | } else { |
| 2959 | assert(B == D); |
| 2960 | // A + D == C + D -> A == C |
| 2961 | Y = A; |
| 2962 | Z = C; |
| 2963 | } |
| 2964 | return new ICmpInst(Pred, Y, Z); |
| 2965 | } |
| 2966 | |
| 2967 | // icmp slt (X + -1), Y -> icmp sle X, Y |
| 2968 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT && |
| 2969 | match(B, m_AllOnes())) |
| 2970 | return new ICmpInst(CmpInst::ICMP_SLE, A, Op1); |
| 2971 | |
| 2972 | // icmp sge (X + -1), Y -> icmp sgt X, Y |
| 2973 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE && |
| 2974 | match(B, m_AllOnes())) |
| 2975 | return new ICmpInst(CmpInst::ICMP_SGT, A, Op1); |
| 2976 | |
| 2977 | // icmp sle (X + 1), Y -> icmp slt X, Y |
| 2978 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE && match(B, m_One())) |
| 2979 | return new ICmpInst(CmpInst::ICMP_SLT, A, Op1); |
| 2980 | |
| 2981 | // icmp sgt (X + 1), Y -> icmp sge X, Y |
| 2982 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT && match(B, m_One())) |
| 2983 | return new ICmpInst(CmpInst::ICMP_SGE, A, Op1); |
| 2984 | |
| 2985 | // icmp sgt X, (Y + -1) -> icmp sge X, Y |
| 2986 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGT && |
| 2987 | match(D, m_AllOnes())) |
| 2988 | return new ICmpInst(CmpInst::ICMP_SGE, Op0, C); |
| 2989 | |
| 2990 | // icmp sle X, (Y + -1) -> icmp slt X, Y |
| 2991 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLE && |
| 2992 | match(D, m_AllOnes())) |
| 2993 | return new ICmpInst(CmpInst::ICMP_SLT, Op0, C); |
| 2994 | |
| 2995 | // icmp sge X, (Y + 1) -> icmp sgt X, Y |
| 2996 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SGE && match(D, m_One())) |
| 2997 | return new ICmpInst(CmpInst::ICMP_SGT, Op0, C); |
| 2998 | |
| 2999 | // icmp slt X, (Y + 1) -> icmp sle X, Y |
| 3000 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_SLT && match(D, m_One())) |
| 3001 | return new ICmpInst(CmpInst::ICMP_SLE, Op0, C); |
| 3002 | |
Sanjay Patel | 40f4017 | 2017-01-13 23:25:46 +0000 | [diff] [blame] | 3003 | // TODO: The subtraction-related identities shown below also hold, but |
| 3004 | // canonicalization from (X -nuw 1) to (X + -1) means that the combinations |
| 3005 | // wouldn't happen even if they were implemented. |
| 3006 | // |
| 3007 | // icmp ult (X - 1), Y -> icmp ule X, Y |
| 3008 | // icmp uge (X - 1), Y -> icmp ugt X, Y |
| 3009 | // icmp ugt X, (Y - 1) -> icmp uge X, Y |
| 3010 | // icmp ule X, (Y - 1) -> icmp ult X, Y |
| 3011 | |
| 3012 | // icmp ule (X + 1), Y -> icmp ult X, Y |
| 3013 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_ULE && match(B, m_One())) |
| 3014 | return new ICmpInst(CmpInst::ICMP_ULT, A, Op1); |
| 3015 | |
| 3016 | // icmp ugt (X + 1), Y -> icmp uge X, Y |
| 3017 | if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_UGT && match(B, m_One())) |
| 3018 | return new ICmpInst(CmpInst::ICMP_UGE, A, Op1); |
| 3019 | |
| 3020 | // icmp uge X, (Y + 1) -> icmp ugt X, Y |
| 3021 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_UGE && match(D, m_One())) |
| 3022 | return new ICmpInst(CmpInst::ICMP_UGT, Op0, C); |
| 3023 | |
| 3024 | // icmp ult X, (Y + 1) -> icmp ule X, Y |
| 3025 | if (C && NoOp1WrapProblem && Pred == CmpInst::ICMP_ULT && match(D, m_One())) |
| 3026 | return new ICmpInst(CmpInst::ICMP_ULE, Op0, C); |
| 3027 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3028 | // if C1 has greater magnitude than C2: |
| 3029 | // icmp (X + C1), (Y + C2) -> icmp (X + C3), Y |
| 3030 | // s.t. C3 = C1 - C2 |
| 3031 | // |
| 3032 | // if C2 has greater magnitude than C1: |
| 3033 | // icmp (X + C1), (Y + C2) -> icmp X, (Y + C3) |
| 3034 | // s.t. C3 = C2 - C1 |
| 3035 | if (A && C && NoOp0WrapProblem && NoOp1WrapProblem && |
| 3036 | (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned()) |
| 3037 | if (ConstantInt *C1 = dyn_cast<ConstantInt>(B)) |
| 3038 | if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) { |
| 3039 | const APInt &AP1 = C1->getValue(); |
| 3040 | const APInt &AP2 = C2->getValue(); |
| 3041 | if (AP1.isNegative() == AP2.isNegative()) { |
| 3042 | APInt AP1Abs = C1->getValue().abs(); |
| 3043 | APInt AP2Abs = C2->getValue().abs(); |
| 3044 | if (AP1Abs.uge(AP2Abs)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3045 | ConstantInt *C3 = Builder.getInt(AP1 - AP2); |
| 3046 | Value *NewAdd = Builder.CreateNSWAdd(A, C3); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3047 | return new ICmpInst(Pred, NewAdd, C); |
| 3048 | } else { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3049 | ConstantInt *C3 = Builder.getInt(AP2 - AP1); |
| 3050 | Value *NewAdd = Builder.CreateNSWAdd(C, C3); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3051 | return new ICmpInst(Pred, A, NewAdd); |
| 3052 | } |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | // Analyze the case when either Op0 or Op1 is a sub instruction. |
| 3057 | // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null). |
| 3058 | A = nullptr; |
| 3059 | B = nullptr; |
| 3060 | C = nullptr; |
| 3061 | D = nullptr; |
| 3062 | if (BO0 && BO0->getOpcode() == Instruction::Sub) { |
| 3063 | A = BO0->getOperand(0); |
| 3064 | B = BO0->getOperand(1); |
| 3065 | } |
| 3066 | if (BO1 && BO1->getOpcode() == Instruction::Sub) { |
| 3067 | C = BO1->getOperand(0); |
| 3068 | D = BO1->getOperand(1); |
| 3069 | } |
| 3070 | |
| 3071 | // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow. |
| 3072 | if (A == Op1 && NoOp0WrapProblem) |
| 3073 | return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B); |
| 3074 | |
| 3075 | // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow. |
| 3076 | if (C == Op0 && NoOp1WrapProblem) |
| 3077 | return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType())); |
| 3078 | |
| 3079 | // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow. |
| 3080 | if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem && |
| 3081 | // Try not to increase register pressure. |
| 3082 | BO0->hasOneUse() && BO1->hasOneUse()) |
| 3083 | return new ICmpInst(Pred, A, C); |
| 3084 | |
| 3085 | // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow. |
| 3086 | if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem && |
| 3087 | // Try not to increase register pressure. |
| 3088 | BO0->hasOneUse() && BO1->hasOneUse()) |
| 3089 | return new ICmpInst(Pred, D, B); |
| 3090 | |
| 3091 | // icmp (0-X) < cst --> x > -cst |
| 3092 | if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) { |
| 3093 | Value *X; |
| 3094 | if (match(BO0, m_Neg(m_Value(X)))) |
| 3095 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1)) |
| 3096 | if (!RHSC->isMinValue(/*isSigned=*/true)) |
| 3097 | return new ICmpInst(I.getSwappedPredicate(), X, |
| 3098 | ConstantExpr::getNeg(RHSC)); |
| 3099 | } |
| 3100 | |
| 3101 | BinaryOperator *SRem = nullptr; |
| 3102 | // icmp (srem X, Y), Y |
| 3103 | if (BO0 && BO0->getOpcode() == Instruction::SRem && Op1 == BO0->getOperand(1)) |
| 3104 | SRem = BO0; |
| 3105 | // icmp Y, (srem X, Y) |
| 3106 | else if (BO1 && BO1->getOpcode() == Instruction::SRem && |
| 3107 | Op0 == BO1->getOperand(1)) |
| 3108 | SRem = BO1; |
| 3109 | if (SRem) { |
| 3110 | // We don't check hasOneUse to avoid increasing register pressure because |
| 3111 | // the value we use is the same value this instruction was already using. |
| 3112 | switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) { |
| 3113 | default: |
| 3114 | break; |
| 3115 | case ICmpInst::ICMP_EQ: |
| 3116 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 3117 | case ICmpInst::ICMP_NE: |
| 3118 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 3119 | case ICmpInst::ICMP_SGT: |
| 3120 | case ICmpInst::ICMP_SGE: |
| 3121 | return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1), |
| 3122 | Constant::getAllOnesValue(SRem->getType())); |
| 3123 | case ICmpInst::ICMP_SLT: |
| 3124 | case ICmpInst::ICMP_SLE: |
| 3125 | return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1), |
| 3126 | Constant::getNullValue(SRem->getType())); |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() && BO0->hasOneUse() && |
| 3131 | BO1->hasOneUse() && BO0->getOperand(1) == BO1->getOperand(1)) { |
| 3132 | switch (BO0->getOpcode()) { |
| 3133 | default: |
| 3134 | break; |
| 3135 | case Instruction::Add: |
| 3136 | case Instruction::Sub: |
Sanjay Patel | d3106ad | 2017-05-23 17:29:58 +0000 | [diff] [blame] | 3137 | case Instruction::Xor: { |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3138 | if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3139 | return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); |
Sanjay Patel | d3106ad | 2017-05-23 17:29:58 +0000 | [diff] [blame] | 3140 | |
| 3141 | const APInt *C; |
| 3142 | if (match(BO0->getOperand(1), m_APInt(C))) { |
| 3143 | // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b |
| 3144 | if (C->isSignMask()) { |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3145 | ICmpInst::Predicate NewPred = |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3146 | I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate(); |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3147 | return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0)); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Sanjay Patel | d3106ad | 2017-05-23 17:29:58 +0000 | [diff] [blame] | 3150 | // icmp u/s (a ^ maxsignval), (b ^ maxsignval) --> icmp s/u' a, b |
| 3151 | if (BO0->getOpcode() == Instruction::Xor && C->isMaxSignedValue()) { |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3152 | ICmpInst::Predicate NewPred = |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3153 | I.isSigned() ? I.getUnsignedPredicate() : I.getSignedPredicate(); |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3154 | NewPred = I.getSwappedPredicate(NewPred); |
| 3155 | return new ICmpInst(NewPred, BO0->getOperand(0), BO1->getOperand(0)); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3156 | } |
| 3157 | } |
| 3158 | break; |
Sanjay Patel | d3106ad | 2017-05-23 17:29:58 +0000 | [diff] [blame] | 3159 | } |
Sanjay Patel | 07b1ba5 | 2017-05-24 22:58:17 +0000 | [diff] [blame] | 3160 | case Instruction::Mul: { |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3161 | if (!I.isEquality()) |
| 3162 | break; |
| 3163 | |
Sanjay Patel | 07b1ba5 | 2017-05-24 22:58:17 +0000 | [diff] [blame] | 3164 | const APInt *C; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 3165 | if (match(BO0->getOperand(1), m_APInt(C)) && !C->isNullValue() && |
| 3166 | !C->isOneValue()) { |
Sanjay Patel | 07b1ba5 | 2017-05-24 22:58:17 +0000 | [diff] [blame] | 3167 | // icmp eq/ne (X * C), (Y * C) --> icmp (X & Mask), (Y & Mask) |
| 3168 | // Mask = -1 >> count-trailing-zeros(C). |
Sanjay Patel | 5150612 | 2017-05-25 14:13:57 +0000 | [diff] [blame] | 3169 | if (unsigned TZs = C->countTrailingZeros()) { |
Sanjay Patel | 07b1ba5 | 2017-05-24 22:58:17 +0000 | [diff] [blame] | 3170 | Constant *Mask = ConstantInt::get( |
| 3171 | BO0->getType(), |
Sanjay Patel | 5150612 | 2017-05-25 14:13:57 +0000 | [diff] [blame] | 3172 | APInt::getLowBitsSet(C->getBitWidth(), C->getBitWidth() - TZs)); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3173 | Value *And1 = Builder.CreateAnd(BO0->getOperand(0), Mask); |
| 3174 | Value *And2 = Builder.CreateAnd(BO1->getOperand(0), Mask); |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3175 | return new ICmpInst(Pred, And1, And2); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3176 | } |
Sanjay Patel | 5150612 | 2017-05-25 14:13:57 +0000 | [diff] [blame] | 3177 | // If there are no trailing zeros in the multiplier, just eliminate |
| 3178 | // the multiplies (no masking is needed): |
| 3179 | // icmp eq/ne (X * C), (Y * C) --> icmp eq/ne X, Y |
| 3180 | return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3181 | } |
| 3182 | break; |
Sanjay Patel | 07b1ba5 | 2017-05-24 22:58:17 +0000 | [diff] [blame] | 3183 | } |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3184 | case Instruction::UDiv: |
| 3185 | case Instruction::LShr: |
Sanjay Patel | 878715f | 2017-05-15 19:27:53 +0000 | [diff] [blame] | 3186 | if (I.isSigned() || !BO0->isExact() || !BO1->isExact()) |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3187 | break; |
Sanjay Patel | 878715f | 2017-05-15 19:27:53 +0000 | [diff] [blame] | 3188 | return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); |
| 3189 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3190 | case Instruction::SDiv: |
Sanjay Patel | 878715f | 2017-05-15 19:27:53 +0000 | [diff] [blame] | 3191 | if (!I.isEquality() || !BO0->isExact() || !BO1->isExact()) |
| 3192 | break; |
| 3193 | return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); |
| 3194 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3195 | case Instruction::AShr: |
| 3196 | if (!BO0->isExact() || !BO1->isExact()) |
| 3197 | break; |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3198 | return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); |
Sanjay Patel | 878715f | 2017-05-15 19:27:53 +0000 | [diff] [blame] | 3199 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3200 | case Instruction::Shl: { |
| 3201 | bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap(); |
| 3202 | bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap(); |
| 3203 | if (!NUW && !NSW) |
| 3204 | break; |
| 3205 | if (!NSW && I.isSigned()) |
| 3206 | break; |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3207 | return new ICmpInst(Pred, BO0->getOperand(0), BO1->getOperand(0)); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | if (BO0) { |
| 3213 | // Transform A & (L - 1) `ult` L --> L != 0 |
| 3214 | auto LSubOne = m_Add(m_Specific(Op1), m_AllOnes()); |
Craig Topper | 72ee694 | 2017-06-24 06:24:01 +0000 | [diff] [blame] | 3215 | auto BitwiseAnd = m_c_And(m_Value(), LSubOne); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3216 | |
Sanjay Patel | 2a06263 | 2017-05-08 16:33:42 +0000 | [diff] [blame] | 3217 | if (match(BO0, BitwiseAnd) && Pred == ICmpInst::ICMP_ULT) { |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3218 | auto *Zero = Constant::getNullValue(BO0->getType()); |
| 3219 | return new ICmpInst(ICmpInst::ICMP_NE, Op1, Zero); |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | return nullptr; |
| 3224 | } |
| 3225 | |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3226 | /// Fold icmp Pred min|max(X, Y), X. |
| 3227 | static Instruction *foldICmpWithMinMax(ICmpInst &Cmp) { |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3228 | ICmpInst::Predicate Pred = Cmp.getPredicate(); |
| 3229 | Value *Op0 = Cmp.getOperand(0); |
| 3230 | Value *X = Cmp.getOperand(1); |
| 3231 | |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3232 | // Canonicalize minimum or maximum operand to LHS of the icmp. |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3233 | if (match(X, m_c_SMin(m_Specific(Op0), m_Value())) || |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3234 | match(X, m_c_SMax(m_Specific(Op0), m_Value())) || |
| 3235 | match(X, m_c_UMin(m_Specific(Op0), m_Value())) || |
| 3236 | match(X, m_c_UMax(m_Specific(Op0), m_Value()))) { |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3237 | std::swap(Op0, X); |
| 3238 | Pred = Cmp.getSwappedPredicate(); |
| 3239 | } |
| 3240 | |
| 3241 | Value *Y; |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3242 | if (match(Op0, m_c_SMin(m_Specific(X), m_Value(Y)))) { |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3243 | // smin(X, Y) == X --> X s<= Y |
| 3244 | // smin(X, Y) s>= X --> X s<= Y |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3245 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_SGE) |
| 3246 | return new ICmpInst(ICmpInst::ICMP_SLE, X, Y); |
| 3247 | |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3248 | // smin(X, Y) != X --> X s> Y |
| 3249 | // smin(X, Y) s< X --> X s> Y |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3250 | if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_SLT) |
| 3251 | return new ICmpInst(ICmpInst::ICMP_SGT, X, Y); |
| 3252 | |
| 3253 | // These cases should be handled in InstSimplify: |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3254 | // smin(X, Y) s<= X --> true |
| 3255 | // smin(X, Y) s> X --> false |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3256 | return nullptr; |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3257 | } |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3258 | |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3259 | if (match(Op0, m_c_SMax(m_Specific(X), m_Value(Y)))) { |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3260 | // smax(X, Y) == X --> X s>= Y |
| 3261 | // smax(X, Y) s<= X --> X s>= Y |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3262 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_SLE) |
| 3263 | return new ICmpInst(ICmpInst::ICMP_SGE, X, Y); |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3264 | |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3265 | // smax(X, Y) != X --> X s< Y |
| 3266 | // smax(X, Y) s> X --> X s< Y |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3267 | if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_SGT) |
| 3268 | return new ICmpInst(ICmpInst::ICMP_SLT, X, Y); |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3269 | |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3270 | // These cases should be handled in InstSimplify: |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 3271 | // smax(X, Y) s>= X --> true |
| 3272 | // smax(X, Y) s< X --> false |
| 3273 | return nullptr; |
| 3274 | } |
| 3275 | |
| 3276 | if (match(Op0, m_c_UMin(m_Specific(X), m_Value(Y)))) { |
| 3277 | // umin(X, Y) == X --> X u<= Y |
| 3278 | // umin(X, Y) u>= X --> X u<= Y |
| 3279 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_UGE) |
| 3280 | return new ICmpInst(ICmpInst::ICMP_ULE, X, Y); |
| 3281 | |
| 3282 | // umin(X, Y) != X --> X u> Y |
| 3283 | // umin(X, Y) u< X --> X u> Y |
| 3284 | if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_ULT) |
| 3285 | return new ICmpInst(ICmpInst::ICMP_UGT, X, Y); |
| 3286 | |
| 3287 | // These cases should be handled in InstSimplify: |
| 3288 | // umin(X, Y) u<= X --> true |
| 3289 | // umin(X, Y) u> X --> false |
| 3290 | return nullptr; |
| 3291 | } |
| 3292 | |
| 3293 | if (match(Op0, m_c_UMax(m_Specific(X), m_Value(Y)))) { |
| 3294 | // umax(X, Y) == X --> X u>= Y |
| 3295 | // umax(X, Y) u<= X --> X u>= Y |
| 3296 | if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_ULE) |
| 3297 | return new ICmpInst(ICmpInst::ICMP_UGE, X, Y); |
| 3298 | |
| 3299 | // umax(X, Y) != X --> X u< Y |
| 3300 | // umax(X, Y) u> X --> X u< Y |
| 3301 | if (Pred == CmpInst::ICMP_NE || Pred == CmpInst::ICMP_UGT) |
| 3302 | return new ICmpInst(ICmpInst::ICMP_ULT, X, Y); |
| 3303 | |
| 3304 | // These cases should be handled in InstSimplify: |
| 3305 | // umax(X, Y) u>= X --> true |
| 3306 | // umax(X, Y) u< X --> false |
Sanjay Patel | 8296c6c | 2016-12-19 16:28:53 +0000 | [diff] [blame] | 3307 | return nullptr; |
| 3308 | } |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3309 | |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 3310 | return nullptr; |
| 3311 | } |
| 3312 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3313 | Instruction *InstCombiner::foldICmpEquality(ICmpInst &I) { |
| 3314 | if (!I.isEquality()) |
| 3315 | return nullptr; |
| 3316 | |
| 3317 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3318 | const CmpInst::Predicate Pred = I.getPredicate(); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3319 | Value *A, *B, *C, *D; |
| 3320 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { |
| 3321 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 3322 | Value *OtherVal = A == Op1 ? B : A; |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3323 | return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType())); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { |
| 3327 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| 3328 | ConstantInt *C1, *C2; |
| 3329 | if (match(B, m_ConstantInt(C1)) && match(D, m_ConstantInt(C2)) && |
| 3330 | Op1->hasOneUse()) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3331 | Constant *NC = Builder.getInt(C1->getValue() ^ C2->getValue()); |
| 3332 | Value *Xor = Builder.CreateXor(C, NC); |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3333 | return new ICmpInst(Pred, A, Xor); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | // A^B == A^D -> B == D |
| 3337 | if (A == C) |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3338 | return new ICmpInst(Pred, B, D); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3339 | if (A == D) |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3340 | return new ICmpInst(Pred, B, C); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3341 | if (B == C) |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3342 | return new ICmpInst(Pred, A, D); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3343 | if (B == D) |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3344 | return new ICmpInst(Pred, A, C); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && (A == Op0 || B == Op0)) { |
| 3349 | // A == (A^B) -> B == 0 |
| 3350 | Value *OtherVal = A == Op0 ? B : A; |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3351 | return new ICmpInst(Pred, OtherVal, Constant::getNullValue(A->getType())); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 3355 | if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) && |
| 3356 | match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) { |
| 3357 | Value *X = nullptr, *Y = nullptr, *Z = nullptr; |
| 3358 | |
| 3359 | if (A == C) { |
| 3360 | X = B; |
| 3361 | Y = D; |
| 3362 | Z = A; |
| 3363 | } else if (A == D) { |
| 3364 | X = B; |
| 3365 | Y = C; |
| 3366 | Z = A; |
| 3367 | } else if (B == C) { |
| 3368 | X = A; |
| 3369 | Y = D; |
| 3370 | Z = B; |
| 3371 | } else if (B == D) { |
| 3372 | X = A; |
| 3373 | Y = C; |
| 3374 | Z = B; |
| 3375 | } |
| 3376 | |
| 3377 | if (X) { // Build (X^Y) & Z |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3378 | Op1 = Builder.CreateXor(X, Y); |
| 3379 | Op1 = Builder.CreateAnd(Op1, Z); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3380 | I.setOperand(0, Op1); |
| 3381 | I.setOperand(1, Constant::getNullValue(Op1->getType())); |
| 3382 | return &I; |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B) |
| 3387 | // and (B & (1<<X)-1) == (zext A) --> A == (trunc B) |
| 3388 | ConstantInt *Cst1; |
| 3389 | if ((Op0->hasOneUse() && match(Op0, m_ZExt(m_Value(A))) && |
| 3390 | match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) || |
| 3391 | (Op1->hasOneUse() && match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) && |
| 3392 | match(Op1, m_ZExt(m_Value(A))))) { |
| 3393 | APInt Pow2 = Cst1->getValue() + 1; |
| 3394 | if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) && |
| 3395 | Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3396 | return new ICmpInst(Pred, A, Builder.CreateTrunc(B, A->getType())); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | // (A >> C) == (B >> C) --> (A^B) u< (1 << C) |
| 3400 | // For lshr and ashr pairs. |
| 3401 | if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_ConstantInt(Cst1)))) && |
| 3402 | match(Op1, m_OneUse(m_LShr(m_Value(B), m_Specific(Cst1))))) || |
| 3403 | (match(Op0, m_OneUse(m_AShr(m_Value(A), m_ConstantInt(Cst1)))) && |
| 3404 | match(Op1, m_OneUse(m_AShr(m_Value(B), m_Specific(Cst1)))))) { |
| 3405 | unsigned TypeBits = Cst1->getBitWidth(); |
| 3406 | unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits); |
| 3407 | if (ShAmt < TypeBits && ShAmt != 0) { |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3408 | ICmpInst::Predicate NewPred = |
| 3409 | Pred == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3410 | Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted"); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3411 | APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3412 | return new ICmpInst(NewPred, Xor, Builder.getInt(CmpVal)); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0 |
| 3417 | if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) && |
| 3418 | match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) { |
| 3419 | unsigned TypeBits = Cst1->getBitWidth(); |
| 3420 | unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits); |
| 3421 | if (ShAmt < TypeBits && ShAmt != 0) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3422 | Value *Xor = Builder.CreateXor(A, B, I.getName() + ".unshifted"); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3423 | APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3424 | Value *And = Builder.CreateAnd(Xor, Builder.getInt(AndVal), |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3425 | I.getName() + ".mask"); |
Sanjay Patel | 4e96f19 | 2017-06-28 16:39:06 +0000 | [diff] [blame] | 3426 | return new ICmpInst(Pred, And, Constant::getNullValue(Cst1->getType())); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to |
| 3431 | // "icmp (and X, mask), cst" |
| 3432 | uint64_t ShAmt = 0; |
| 3433 | if (Op0->hasOneUse() && |
| 3434 | match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A), m_ConstantInt(ShAmt))))) && |
| 3435 | match(Op1, m_ConstantInt(Cst1)) && |
| 3436 | // Only do this when A has multiple uses. This is most important to do |
| 3437 | // when it exposes other optimizations. |
| 3438 | !A->hasOneUse()) { |
| 3439 | unsigned ASize = cast<IntegerType>(A->getType())->getPrimitiveSizeInBits(); |
| 3440 | |
| 3441 | if (ShAmt < ASize) { |
| 3442 | APInt MaskV = |
| 3443 | APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits()); |
| 3444 | MaskV <<= ShAmt; |
| 3445 | |
| 3446 | APInt CmpV = Cst1->getValue().zext(ASize); |
| 3447 | CmpV <<= ShAmt; |
| 3448 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3449 | Value *Mask = Builder.CreateAnd(A, Builder.getInt(MaskV)); |
| 3450 | return new ICmpInst(Pred, Mask, Builder.getInt(CmpV)); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3451 | } |
| 3452 | } |
| 3453 | |
Sanjay Patel | c3d5cf0 | 2017-07-02 14:34:50 +0000 | [diff] [blame] | 3454 | // If both operands are byte-swapped or bit-reversed, just compare the |
| 3455 | // original values. |
| 3456 | // TODO: Move this to a function similar to foldICmpIntrinsicWithConstant() |
| 3457 | // and handle more intrinsics. |
| 3458 | if ((match(Op0, m_BSwap(m_Value(A))) && match(Op1, m_BSwap(m_Value(B)))) || |
Simon Pilgrim | df2657a | 2017-07-02 16:31:16 +0000 | [diff] [blame] | 3459 | (match(Op0, m_BitReverse(m_Value(A))) && |
| 3460 | match(Op1, m_BitReverse(m_Value(B))))) |
Sanjay Patel | c3d5cf0 | 2017-07-02 14:34:50 +0000 | [diff] [blame] | 3461 | return new ICmpInst(Pred, A, B); |
| 3462 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 3463 | return nullptr; |
| 3464 | } |
| 3465 | |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3466 | /// Handle icmp (cast x to y), (cast/cst). We only handle extending casts so |
| 3467 | /// far. |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 3468 | Instruction *InstCombiner::foldICmpWithCastAndCast(ICmpInst &ICmp) { |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3469 | const CastInst *LHSCI = cast<CastInst>(ICmp.getOperand(0)); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3470 | Value *LHSCIOp = LHSCI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3471 | Type *SrcTy = LHSCIOp->getType(); |
| 3472 | Type *DestTy = LHSCI->getType(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3473 | Value *RHSCIOp; |
| 3474 | |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3475 | // 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] | 3476 | // integer type is the same size as the pointer type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3477 | if (LHSCI->getOpcode() == Instruction::PtrToInt && |
| 3478 | DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3479 | Value *RHSOp = nullptr; |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3480 | if (auto *RHSC = dyn_cast<PtrToIntOperator>(ICmp.getOperand(1))) { |
Michael Liao | d266b92 | 2015-02-13 04:51:26 +0000 | [diff] [blame] | 3481 | Value *RHSCIOp = RHSC->getOperand(0); |
| 3482 | if (RHSCIOp->getType()->getPointerAddressSpace() == |
| 3483 | LHSCIOp->getType()->getPointerAddressSpace()) { |
| 3484 | RHSOp = RHSC->getOperand(0); |
| 3485 | // If the pointer types don't match, insert a bitcast. |
| 3486 | if (LHSCIOp->getType() != RHSOp->getType()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3487 | RHSOp = Builder.CreateBitCast(RHSOp, LHSCIOp->getType()); |
Michael Liao | d266b92 | 2015-02-13 04:51:26 +0000 | [diff] [blame] | 3488 | } |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3489 | } else if (auto *RHSC = dyn_cast<Constant>(ICmp.getOperand(1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3490 | RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy); |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3491 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3492 | |
| 3493 | if (RHSOp) |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3494 | return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSOp); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3495 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3496 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3497 | // The code below only handles extension cast instructions, so far. |
| 3498 | // Enforce this. |
| 3499 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 3500 | LHSCI->getOpcode() != Instruction::SExt) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3501 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3502 | |
| 3503 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3504 | bool isSignedCmp = ICmp.isSigned(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3505 | |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3506 | if (auto *CI = dyn_cast<CastInst>(ICmp.getOperand(1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3507 | // Not an extension from the same type? |
| 3508 | RHSCIOp = CI->getOperand(0); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3509 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3510 | return nullptr; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3511 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3512 | // If the signedness of the two casts doesn't agree (i.e. one is a sext |
| 3513 | // and the other is a zext), then we can't handle this. |
| 3514 | if (CI->getOpcode() != LHSCI->getOpcode()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3515 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Deal with equality cases early. |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3518 | if (ICmp.isEquality()) |
| 3519 | return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSCIOp); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3520 | |
| 3521 | // A signed comparison of sign extended values simplifies into a |
| 3522 | // signed comparison. |
| 3523 | if (isSignedCmp && isSignedExt) |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3524 | return new ICmpInst(ICmp.getPredicate(), LHSCIOp, RHSCIOp); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3525 | |
| 3526 | // The other three cases all fold into an unsigned comparison. |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3527 | return new ICmpInst(ICmp.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
Sanjay Patel | 4c20423 | 2016-06-04 20:39:22 +0000 | [diff] [blame] | 3530 | // 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] | 3531 | auto *C = dyn_cast<Constant>(ICmp.getOperand(1)); |
| 3532 | if (!C) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3533 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Compute the constant that would happen if we truncated to SrcTy then |
Sanjay Patel | c774f8c | 2016-06-04 21:20:44 +0000 | [diff] [blame] | 3536 | // re-extended to DestTy. |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3537 | Constant *Res1 = ConstantExpr::getTrunc(C, SrcTy); |
Sanjay Patel | c774f8c | 2016-06-04 21:20:44 +0000 | [diff] [blame] | 3538 | Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(), Res1, DestTy); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3539 | |
| 3540 | // If the re-extended constant didn't change... |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3541 | if (Res2 == C) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3542 | // Deal with equality cases early. |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3543 | if (ICmp.isEquality()) |
| 3544 | return new ICmpInst(ICmp.getPredicate(), LHSCIOp, Res1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3545 | |
| 3546 | // A signed comparison of sign extended values simplifies into a |
| 3547 | // signed comparison. |
| 3548 | if (isSignedExt && isSignedCmp) |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3549 | return new ICmpInst(ICmp.getPredicate(), LHSCIOp, Res1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3550 | |
| 3551 | // The other three cases all fold into an unsigned comparison. |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3552 | return new ICmpInst(ICmp.getUnsignedPredicate(), LHSCIOp, Res1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
Sanjay Patel | 6a333c3 | 2016-06-06 16:56:57 +0000 | [diff] [blame] | 3555 | // The re-extended constant changed, partly changed (in the case of a vector), |
| 3556 | // or could not be determined to be equal (in the case of a constant |
| 3557 | // expression), so the constant cannot be represented in the shorter type. |
| 3558 | // Consequently, we cannot emit a simple comparison. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3559 | // All the cases that fold to true or false will have already been handled |
| 3560 | // by SimplifyICmpInst, so only deal with the tricky case. |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3561 | |
Sanjay Patel | 6a333c3 | 2016-06-06 16:56:57 +0000 | [diff] [blame] | 3562 | if (isSignedCmp || !isSignedExt || !isa<ConstantInt>(C)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3563 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3564 | |
| 3565 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 3566 | // should have been folded away previously and not enter in here. |
Duncan Sands | 8fb2c38 | 2011-01-20 13:21:55 +0000 | [diff] [blame] | 3567 | |
| 3568 | // We're performing an unsigned comp with a sign extended value. |
| 3569 | // This is true if the input is >= 0. [aka >s -1] |
| 3570 | Constant *NegOne = Constant::getAllOnesValue(SrcTy); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3571 | Value *Result = Builder.CreateICmpSGT(LHSCIOp, NegOne, ICmp.getName()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3572 | |
| 3573 | // Finally, return the value computed. |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3574 | if (ICmp.getPredicate() == ICmpInst::ICMP_ULT) |
| 3575 | return replaceInstUsesWith(ICmp, Result); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3576 | |
Sanjay Patel | 6f8f47b | 2016-06-05 00:12:32 +0000 | [diff] [blame] | 3577 | assert(ICmp.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!"); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3578 | return BinaryOperator::CreateNot(Result); |
| 3579 | } |
| 3580 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3581 | bool InstCombiner::OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, |
| 3582 | Value *RHS, Instruction &OrigI, |
| 3583 | Value *&Result, Constant *&Overflow) { |
Sanjoy Das | 827529e | 2015-08-11 21:33:55 +0000 | [diff] [blame] | 3584 | if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS)) |
| 3585 | std::swap(LHS, RHS); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3586 | |
| 3587 | auto SetResult = [&](Value *OpResult, Constant *OverflowVal, bool ReuseName) { |
| 3588 | Result = OpResult; |
| 3589 | Overflow = OverflowVal; |
| 3590 | if (ReuseName) |
| 3591 | Result->takeName(&OrigI); |
| 3592 | return true; |
| 3593 | }; |
| 3594 | |
Sanjoy Das | 6f5dca7 | 2015-08-28 19:09:31 +0000 | [diff] [blame] | 3595 | // If the overflow check was an add followed by a compare, the insertion point |
| 3596 | // may be pointing to the compare. We want to insert the new instructions |
| 3597 | // before the add in case there are uses of the add between the add and the |
| 3598 | // compare. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3599 | Builder.SetInsertPoint(&OrigI); |
Sanjoy Das | 6f5dca7 | 2015-08-28 19:09:31 +0000 | [diff] [blame] | 3600 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3601 | switch (OCF) { |
| 3602 | case OCF_INVALID: |
| 3603 | llvm_unreachable("bad overflow check kind!"); |
| 3604 | |
| 3605 | case OCF_UNSIGNED_ADD: { |
| 3606 | OverflowResult OR = computeOverflowForUnsignedAdd(LHS, RHS, &OrigI); |
| 3607 | if (OR == OverflowResult::NeverOverflows) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3608 | return SetResult(Builder.CreateNUWAdd(LHS, RHS), Builder.getFalse(), |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3609 | true); |
| 3610 | |
| 3611 | if (OR == OverflowResult::AlwaysOverflows) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3612 | return SetResult(Builder.CreateAdd(LHS, RHS), Builder.getTrue(), true); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 3613 | |
| 3614 | // Fall through uadd into sadd |
| 3615 | LLVM_FALLTHROUGH; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3616 | } |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3617 | case OCF_SIGNED_ADD: { |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 3618 | // X + 0 -> {X, false} |
| 3619 | if (match(RHS, m_Zero())) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3620 | return SetResult(LHS, Builder.getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3621 | |
| 3622 | // We can strength reduce this signed add into a regular add if we can prove |
| 3623 | // that it will never overflow. |
| 3624 | if (OCF == OCF_SIGNED_ADD) |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 3625 | if (willNotOverflowSignedAdd(LHS, RHS, OrigI)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3626 | return SetResult(Builder.CreateNSWAdd(LHS, RHS), Builder.getFalse(), |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3627 | true); |
Sanjoy Das | 72cb5e1 | 2015-06-05 18:04:42 +0000 | [diff] [blame] | 3628 | break; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | case OCF_UNSIGNED_SUB: |
| 3632 | case OCF_SIGNED_SUB: { |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 3633 | // X - 0 -> {X, false} |
| 3634 | if (match(RHS, m_Zero())) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3635 | return SetResult(LHS, Builder.getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3636 | |
| 3637 | if (OCF == OCF_SIGNED_SUB) { |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 3638 | if (willNotOverflowSignedSub(LHS, RHS, OrigI)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3639 | return SetResult(Builder.CreateNSWSub(LHS, RHS), Builder.getFalse(), |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3640 | true); |
| 3641 | } else { |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 3642 | if (willNotOverflowUnsignedSub(LHS, RHS, OrigI)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3643 | return SetResult(Builder.CreateNUWSub(LHS, RHS), Builder.getFalse(), |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3644 | true); |
| 3645 | } |
| 3646 | break; |
| 3647 | } |
| 3648 | |
| 3649 | case OCF_UNSIGNED_MUL: { |
| 3650 | OverflowResult OR = computeOverflowForUnsignedMul(LHS, RHS, &OrigI); |
| 3651 | if (OR == OverflowResult::NeverOverflows) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3652 | return SetResult(Builder.CreateNUWMul(LHS, RHS), Builder.getFalse(), |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3653 | true); |
| 3654 | if (OR == OverflowResult::AlwaysOverflows) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3655 | return SetResult(Builder.CreateMul(LHS, RHS), Builder.getTrue(), true); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 3656 | LLVM_FALLTHROUGH; |
| 3657 | } |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3658 | case OCF_SIGNED_MUL: |
| 3659 | // X * undef -> undef |
| 3660 | if (isa<UndefValue>(RHS)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3661 | return SetResult(RHS, UndefValue::get(Builder.getInt1Ty()), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3662 | |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 3663 | // X * 0 -> {0, false} |
| 3664 | if (match(RHS, m_Zero())) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3665 | return SetResult(RHS, Builder.getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3666 | |
David Majnemer | 27e89ba | 2015-05-21 23:04:21 +0000 | [diff] [blame] | 3667 | // X * 1 -> {X, false} |
| 3668 | if (match(RHS, m_One())) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3669 | return SetResult(LHS, Builder.getFalse(), false); |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3670 | |
| 3671 | if (OCF == OCF_SIGNED_MUL) |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 3672 | if (willNotOverflowSignedMul(LHS, RHS, OrigI)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3673 | return SetResult(Builder.CreateNSWMul(LHS, RHS), Builder.getFalse(), |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3674 | true); |
Sanjoy Das | c80dad6 | 2015-06-05 18:04:46 +0000 | [diff] [blame] | 3675 | break; |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | return false; |
| 3679 | } |
| 3680 | |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3681 | /// \brief Recognize and process idiom involving test for multiplication |
| 3682 | /// overflow. |
| 3683 | /// |
| 3684 | /// The caller has matched a pattern of the form: |
| 3685 | /// I = cmp u (mul(zext A, zext B), V |
| 3686 | /// The function checks if this is a test for overflow and if so replaces |
| 3687 | /// multiplication with call to 'mul.with.overflow' intrinsic. |
| 3688 | /// |
| 3689 | /// \param I Compare instruction. |
| 3690 | /// \param MulVal Result of 'mult' instruction. It is one of the arguments of |
| 3691 | /// the compare instruction. Must be of integer type. |
| 3692 | /// \param OtherVal The other argument of compare instruction. |
| 3693 | /// \returns Instruction which must replace the compare instruction, NULL if no |
| 3694 | /// replacement required. |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 3695 | static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal, |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3696 | Value *OtherVal, InstCombiner &IC) { |
Benjamin Kramer | c96a7f8 | 2014-06-24 10:47:52 +0000 | [diff] [blame] | 3697 | // Don't bother doing this transformation for pointers, don't do it for |
| 3698 | // vectors. |
| 3699 | if (!isa<IntegerType>(MulVal->getType())) |
| 3700 | return nullptr; |
| 3701 | |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3702 | assert(I.getOperand(0) == MulVal || I.getOperand(1) == MulVal); |
| 3703 | assert(I.getOperand(0) == OtherVal || I.getOperand(1) == OtherVal); |
David Majnemer | daa24b9 | 2015-09-05 20:44:56 +0000 | [diff] [blame] | 3704 | auto *MulInstr = dyn_cast<Instruction>(MulVal); |
| 3705 | if (!MulInstr) |
| 3706 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3707 | assert(MulInstr->getOpcode() == Instruction::Mul); |
| 3708 | |
David Majnemer | 634ca23 | 2014-11-01 23:46:05 +0000 | [diff] [blame] | 3709 | auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)), |
| 3710 | *RHS = cast<ZExtOperator>(MulInstr->getOperand(1)); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3711 | assert(LHS->getOpcode() == Instruction::ZExt); |
| 3712 | assert(RHS->getOpcode() == Instruction::ZExt); |
| 3713 | Value *A = LHS->getOperand(0), *B = RHS->getOperand(0); |
| 3714 | |
| 3715 | // Calculate type and width of the result produced by mul.with.overflow. |
| 3716 | Type *TyA = A->getType(), *TyB = B->getType(); |
| 3717 | unsigned WidthA = TyA->getPrimitiveSizeInBits(), |
| 3718 | WidthB = TyB->getPrimitiveSizeInBits(); |
| 3719 | unsigned MulWidth; |
| 3720 | Type *MulType; |
| 3721 | if (WidthB > WidthA) { |
| 3722 | MulWidth = WidthB; |
| 3723 | MulType = TyB; |
| 3724 | } else { |
| 3725 | MulWidth = WidthA; |
| 3726 | MulType = TyA; |
| 3727 | } |
| 3728 | |
| 3729 | // In order to replace the original mul with a narrower mul.with.overflow, |
| 3730 | // all uses must ignore upper bits of the product. The number of used low |
| 3731 | // bits must be not greater than the width of mul.with.overflow. |
| 3732 | if (MulVal->hasNUsesOrMore(2)) |
| 3733 | for (User *U : MulVal->users()) { |
| 3734 | if (U == &I) |
| 3735 | continue; |
| 3736 | if (TruncInst *TI = dyn_cast<TruncInst>(U)) { |
| 3737 | // Check if truncation ignores bits above MulWidth. |
| 3738 | unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits(); |
| 3739 | if (TruncWidth > MulWidth) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3740 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3741 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) { |
| 3742 | // Check if AND ignores bits above MulWidth. |
| 3743 | if (BO->getOpcode() != Instruction::And) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3744 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3745 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 3746 | const APInt &CVal = CI->getValue(); |
| 3747 | if (CVal.getBitWidth() - CVal.countLeadingZeros() > MulWidth) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3748 | return nullptr; |
Davide Italiano | 579064e | 2017-07-16 18:56:30 +0000 | [diff] [blame] | 3749 | } else { |
| 3750 | // In this case we could have the operand of the binary operation |
| 3751 | // being defined in another block, and performing the replacement |
| 3752 | // could break the dominance relation. |
| 3753 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3754 | } |
| 3755 | } else { |
| 3756 | // Other uses prohibit this transformation. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3757 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | // Recognize patterns |
| 3762 | switch (I.getPredicate()) { |
| 3763 | case ICmpInst::ICMP_EQ: |
| 3764 | case ICmpInst::ICMP_NE: |
| 3765 | // Recognize pattern: |
| 3766 | // mulval = mul(zext A, zext B) |
| 3767 | // cmp eq/neq mulval, zext trunc mulval |
| 3768 | if (ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal)) |
| 3769 | if (Zext->hasOneUse()) { |
| 3770 | Value *ZextArg = Zext->getOperand(0); |
| 3771 | if (TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg)) |
| 3772 | if (Trunc->getType()->getPrimitiveSizeInBits() == MulWidth) |
| 3773 | break; //Recognized |
| 3774 | } |
| 3775 | |
| 3776 | // Recognize pattern: |
| 3777 | // mulval = mul(zext A, zext B) |
| 3778 | // cmp eq/neq mulval, and(mulval, mask), mask selects low MulWidth bits. |
| 3779 | ConstantInt *CI; |
| 3780 | Value *ValToMask; |
| 3781 | if (match(OtherVal, m_And(m_Value(ValToMask), m_ConstantInt(CI)))) { |
| 3782 | if (ValToMask != MulVal) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3783 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3784 | const APInt &CVal = CI->getValue() + 1; |
| 3785 | if (CVal.isPowerOf2()) { |
| 3786 | unsigned MaskWidth = CVal.logBase2(); |
| 3787 | if (MaskWidth == MulWidth) |
| 3788 | break; // Recognized |
| 3789 | } |
| 3790 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3791 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3792 | |
| 3793 | case ICmpInst::ICMP_UGT: |
| 3794 | // Recognize pattern: |
| 3795 | // mulval = mul(zext A, zext B) |
| 3796 | // cmp ugt mulval, max |
| 3797 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
| 3798 | APInt MaxVal = APInt::getMaxValue(MulWidth); |
| 3799 | MaxVal = MaxVal.zext(CI->getBitWidth()); |
| 3800 | if (MaxVal.eq(CI->getValue())) |
| 3801 | break; // Recognized |
| 3802 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3803 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3804 | |
| 3805 | case ICmpInst::ICMP_UGE: |
| 3806 | // Recognize pattern: |
| 3807 | // mulval = mul(zext A, zext B) |
| 3808 | // cmp uge mulval, max+1 |
| 3809 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
| 3810 | APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth); |
| 3811 | if (MaxVal.eq(CI->getValue())) |
| 3812 | break; // Recognized |
| 3813 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3814 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3815 | |
| 3816 | case ICmpInst::ICMP_ULE: |
| 3817 | // Recognize pattern: |
| 3818 | // mulval = mul(zext A, zext B) |
| 3819 | // cmp ule mulval, max |
| 3820 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
| 3821 | APInt MaxVal = APInt::getMaxValue(MulWidth); |
| 3822 | MaxVal = MaxVal.zext(CI->getBitWidth()); |
| 3823 | if (MaxVal.eq(CI->getValue())) |
| 3824 | break; // Recognized |
| 3825 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3826 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3827 | |
| 3828 | case ICmpInst::ICMP_ULT: |
| 3829 | // Recognize pattern: |
| 3830 | // mulval = mul(zext A, zext B) |
| 3831 | // cmp ule mulval, max + 1 |
| 3832 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) { |
Serge Pavlov | b5f3ddc | 2014-04-14 02:20:19 +0000 | [diff] [blame] | 3833 | APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3834 | if (MaxVal.eq(CI->getValue())) |
| 3835 | break; // Recognized |
| 3836 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3837 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3838 | |
| 3839 | default: |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3840 | return nullptr; |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3843 | InstCombiner::BuilderTy &Builder = IC.Builder; |
| 3844 | Builder.SetInsertPoint(MulInstr); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3845 | |
| 3846 | // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B) |
| 3847 | Value *MulA = A, *MulB = B; |
| 3848 | if (WidthA < MulWidth) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3849 | MulA = Builder.CreateZExt(A, MulType); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3850 | if (WidthB < MulWidth) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3851 | MulB = Builder.CreateZExt(B, MulType); |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 3852 | Value *F = Intrinsic::getDeclaration(I.getModule(), |
| 3853 | Intrinsic::umul_with_overflow, MulType); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3854 | CallInst *Call = Builder.CreateCall(F, {MulA, MulB}, "umul"); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3855 | IC.Worklist.Add(MulInstr); |
| 3856 | |
| 3857 | // If there are uses of mul result other than the comparison, we know that |
| 3858 | // they are truncation or binary AND. Change them to use result of |
Serge Pavlov | b5f3ddc | 2014-04-14 02:20:19 +0000 | [diff] [blame] | 3859 | // mul.with.overflow and adjust properly mask/size. |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3860 | if (MulVal->hasNUsesOrMore(2)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3861 | Value *Mul = Builder.CreateExtractValue(Call, 0, "umul.value"); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3862 | for (User *U : MulVal->users()) { |
| 3863 | if (U == &I || U == OtherVal) |
| 3864 | continue; |
| 3865 | if (TruncInst *TI = dyn_cast<TruncInst>(U)) { |
| 3866 | if (TI->getType()->getPrimitiveSizeInBits() == MulWidth) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3867 | IC.replaceInstUsesWith(*TI, Mul); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3868 | else |
| 3869 | TI->setOperand(0, Mul); |
| 3870 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) { |
| 3871 | assert(BO->getOpcode() == Instruction::And); |
| 3872 | // Replace (mul & mask) --> zext (mul.with.overflow & short_mask) |
Davide Italiano | 579064e | 2017-07-16 18:56:30 +0000 | [diff] [blame] | 3873 | ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1)); |
| 3874 | APInt ShortMask = CI->getValue().trunc(MulWidth); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3875 | Value *ShortAnd = Builder.CreateAnd(Mul, ShortMask); |
Davide Italiano | 579064e | 2017-07-16 18:56:30 +0000 | [diff] [blame] | 3876 | Instruction *Zext = |
| 3877 | cast<Instruction>(Builder.CreateZExt(ShortAnd, BO->getType())); |
| 3878 | IC.Worklist.Add(Zext); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 3879 | IC.replaceInstUsesWith(*BO, Zext); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3880 | } else { |
| 3881 | llvm_unreachable("Unexpected Binary operation"); |
| 3882 | } |
Davide Italiano | 579064e | 2017-07-16 18:56:30 +0000 | [diff] [blame] | 3883 | IC.Worklist.Add(cast<Instruction>(U)); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | if (isa<Instruction>(OtherVal)) |
| 3887 | IC.Worklist.Add(cast<Instruction>(OtherVal)); |
| 3888 | |
| 3889 | // The original icmp gets replaced with the overflow value, maybe inverted |
| 3890 | // depending on predicate. |
| 3891 | bool Inverse = false; |
| 3892 | switch (I.getPredicate()) { |
| 3893 | case ICmpInst::ICMP_NE: |
| 3894 | break; |
| 3895 | case ICmpInst::ICMP_EQ: |
| 3896 | Inverse = true; |
| 3897 | break; |
| 3898 | case ICmpInst::ICMP_UGT: |
| 3899 | case ICmpInst::ICMP_UGE: |
| 3900 | if (I.getOperand(0) == MulVal) |
| 3901 | break; |
| 3902 | Inverse = true; |
| 3903 | break; |
| 3904 | case ICmpInst::ICMP_ULT: |
| 3905 | case ICmpInst::ICMP_ULE: |
| 3906 | if (I.getOperand(1) == MulVal) |
| 3907 | break; |
| 3908 | Inverse = true; |
| 3909 | break; |
| 3910 | default: |
| 3911 | llvm_unreachable("Unexpected predicate"); |
| 3912 | } |
| 3913 | if (Inverse) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 3914 | Value *Res = Builder.CreateExtractValue(Call, 1); |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 3915 | return BinaryOperator::CreateNot(Res); |
| 3916 | } |
| 3917 | |
| 3918 | return ExtractValueInst::Create(Call, 1); |
| 3919 | } |
| 3920 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 3921 | /// When performing a comparison against a constant, it is possible that not all |
| 3922 | /// the bits in the LHS are demanded. This helper method computes the mask that |
| 3923 | /// IS demanded. |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 3924 | static APInt getDemandedBitsLHSMask(ICmpInst &I, unsigned BitWidth, |
| 3925 | bool isSignCheck) { |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3926 | if (isSignCheck) |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 3927 | return APInt::getSignMask(BitWidth); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3928 | |
Craig Topper | 18887bf | 2017-09-20 23:48:58 +0000 | [diff] [blame^] | 3929 | const APInt *RHS; |
| 3930 | if (!match(I.getOperand(1), m_APInt(RHS))) |
| 3931 | return APInt::getAllOnesValue(BitWidth); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3932 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3933 | switch (I.getPredicate()) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3934 | // For a UGT comparison, we don't care about any bits that |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3935 | // correspond to the trailing ones of the comparand. The value of these |
| 3936 | // bits doesn't impact the outcome of the comparison, because any value |
| 3937 | // greater than the RHS must differ in a bit higher than these due to carry. |
Craig Topper | 18887bf | 2017-09-20 23:48:58 +0000 | [diff] [blame^] | 3938 | case ICmpInst::ICMP_UGT: |
| 3939 | return APInt::getBitsSetFrom(BitWidth, RHS->countTrailingOnes()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3940 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3941 | // Similarly, for a ULT comparison, we don't care about the trailing zeros. |
| 3942 | // Any value less than the RHS must differ in a higher bit because of carries. |
Craig Topper | 18887bf | 2017-09-20 23:48:58 +0000 | [diff] [blame^] | 3943 | case ICmpInst::ICMP_ULT: |
| 3944 | return APInt::getBitsSetFrom(BitWidth, RHS->countTrailingZeros()); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 3945 | |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3946 | default: |
| 3947 | return APInt::getAllOnesValue(BitWidth); |
| 3948 | } |
Owen Anderson | d490c2d | 2011-01-11 00:36:45 +0000 | [diff] [blame] | 3949 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 3950 | |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 3951 | /// \brief Check if the order of \p Op0 and \p Op1 as operand in an ICmpInst |
| 3952 | /// should be swapped. |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3953 | /// 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] | 3954 | /// as subtract operands and their positions in those instructions. |
| 3955 | /// The rational is that several architectures use the same instruction for |
| 3956 | /// both subtract and cmp, thus it is better if the order of those operands |
| 3957 | /// match. |
| 3958 | /// \return true if Op0 and Op1 should be swapped. |
| 3959 | static bool swapMayExposeCSEOpportunities(const Value * Op0, |
| 3960 | const Value * Op1) { |
| 3961 | // Filter out pointer value as those cannot appears directly in subtract. |
| 3962 | // FIXME: we may want to go through inttoptrs or bitcasts. |
| 3963 | if (Op0->getType()->isPointerTy()) |
| 3964 | return false; |
| 3965 | // Count every uses of both Op0 and Op1 in a subtract. |
| 3966 | // Each time Op0 is the first operand, count -1: swapping is bad, the |
| 3967 | // subtract has already the same layout as the compare. |
| 3968 | // 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] | 3969 | // subtract has a different layout as the compare. |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 3970 | // At the end, if the benefit is greater than 0, Op0 should come second to |
| 3971 | // expose more CSE opportunities. |
| 3972 | int GlobalSwapBenefits = 0; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3973 | for (const User *U : Op0->users()) { |
| 3974 | const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(U); |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 3975 | if (!BinOp || BinOp->getOpcode() != Instruction::Sub) |
| 3976 | continue; |
| 3977 | // If Op0 is the first argument, this is not beneficial to swap the |
| 3978 | // arguments. |
| 3979 | int LocalSwapBenefits = -1; |
| 3980 | unsigned Op1Idx = 1; |
| 3981 | if (BinOp->getOperand(Op1Idx) == Op0) { |
| 3982 | Op1Idx = 0; |
| 3983 | LocalSwapBenefits = 1; |
| 3984 | } |
| 3985 | if (BinOp->getOperand(Op1Idx) != Op1) |
| 3986 | continue; |
| 3987 | GlobalSwapBenefits += LocalSwapBenefits; |
| 3988 | } |
| 3989 | return GlobalSwapBenefits > 0; |
| 3990 | } |
| 3991 | |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3992 | /// \brief Check that one use is in the same block as the definition and all |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 3993 | /// other uses are in blocks dominated by a given block. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 3994 | /// |
| 3995 | /// \param DI Definition |
| 3996 | /// \param UI Use |
| 3997 | /// \param DB Block that must dominate all uses of \p DI outside |
| 3998 | /// the parent block |
| 3999 | /// \return true when \p UI is the only use of \p DI in the parent block |
| 4000 | /// and all other uses of \p DI are in blocks dominated by \p DB. |
| 4001 | /// |
| 4002 | bool InstCombiner::dominatesAllUses(const Instruction *DI, |
| 4003 | const Instruction *UI, |
| 4004 | const BasicBlock *DB) const { |
| 4005 | assert(DI && UI && "Instruction not defined\n"); |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 4006 | // Ignore incomplete definitions. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4007 | if (!DI->getParent()) |
| 4008 | return false; |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 4009 | // DI and UI must be in the same block. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4010 | if (DI->getParent() != UI->getParent()) |
| 4011 | return false; |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 4012 | // Protect from self-referencing blocks. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4013 | if (DI->getParent() == DB) |
| 4014 | return false; |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4015 | for (const User *U : DI->users()) { |
| 4016 | auto *Usr = cast<Instruction>(U); |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 4017 | if (Usr != UI && !DT.dominates(DB, Usr->getParent())) |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4018 | return false; |
| 4019 | } |
| 4020 | return true; |
| 4021 | } |
| 4022 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 4023 | /// 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] | 4024 | static bool isChainSelectCmpBranch(const SelectInst *SI) { |
| 4025 | const BasicBlock *BB = SI->getParent(); |
| 4026 | if (!BB) |
| 4027 | return false; |
| 4028 | auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator()); |
| 4029 | if (!BI || BI->getNumSuccessors() != 2) |
| 4030 | return false; |
| 4031 | auto *IC = dyn_cast<ICmpInst>(BI->getCondition()); |
| 4032 | if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI)) |
| 4033 | return false; |
| 4034 | return true; |
| 4035 | } |
| 4036 | |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4037 | /// \brief True when a select result is replaced by one of its operands |
| 4038 | /// in select-icmp sequence. This will eventually result in the elimination |
| 4039 | /// of the select. |
| 4040 | /// |
| 4041 | /// \param SI Select instruction |
| 4042 | /// \param Icmp Compare instruction |
| 4043 | /// \param SIOpd Operand that replaces the select |
| 4044 | /// |
| 4045 | /// Notes: |
| 4046 | /// - The replacement is global and requires dominator information |
| 4047 | /// - The caller is responsible for the actual replacement |
| 4048 | /// |
| 4049 | /// Example: |
| 4050 | /// |
| 4051 | /// entry: |
| 4052 | /// %4 = select i1 %3, %C* %0, %C* null |
| 4053 | /// %5 = icmp eq %C* %4, null |
| 4054 | /// br i1 %5, label %9, label %7 |
| 4055 | /// ... |
| 4056 | /// ; <label>:7 ; preds = %entry |
| 4057 | /// %8 = getelementptr inbounds %C* %4, i64 0, i32 0 |
| 4058 | /// ... |
| 4059 | /// |
| 4060 | /// can be transformed to |
| 4061 | /// |
| 4062 | /// %5 = icmp eq %C* %0, null |
| 4063 | /// %6 = select i1 %3, i1 %5, i1 true |
| 4064 | /// br i1 %6, label %9, label %7 |
| 4065 | /// ... |
| 4066 | /// ; <label>:7 ; preds = %entry |
| 4067 | /// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0! |
| 4068 | /// |
| 4069 | /// Similar when the first operand of the select is a constant or/and |
| 4070 | /// the compare is for not equal rather than equal. |
| 4071 | /// |
| 4072 | /// NOTE: The function is only called when the select and compare constants |
| 4073 | /// are equal, the optimization can work only for EQ predicates. This is not a |
| 4074 | /// major restriction since a NE compare should be 'normalized' to an equal |
| 4075 | /// compare, which usually happens in the combiner and test case |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 4076 | /// select-cmp-br.ll checks for it. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4077 | bool InstCombiner::replacedSelectWithOperand(SelectInst *SI, |
| 4078 | const ICmpInst *Icmp, |
| 4079 | const unsigned SIOpd) { |
David Majnemer | 83484fd | 2014-11-22 06:09:28 +0000 | [diff] [blame] | 4080 | assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!"); |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4081 | if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) { |
| 4082 | BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1); |
Bjorn Pettersson | e5027cf | 2017-03-02 15:18:58 +0000 | [diff] [blame] | 4083 | // The check for the single predecessor is not the best that can be |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 4084 | // done. But it protects efficiently against cases like when SI's |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4085 | // home block has two successors, Succ and Succ1, and Succ1 predecessor |
| 4086 | // of Succ. Then SI can't be replaced by SIOpd because the use that gets |
| 4087 | // replaced can be reached on either path. So the uniqueness check |
| 4088 | // guarantees that the path all uses of SI (outside SI's parent) are on |
| 4089 | // is disjoint from all other paths out of SI. But that information |
| 4090 | // is more expensive to compute, and the trade-off here is in favor |
Bjorn Pettersson | e5027cf | 2017-03-02 15:18:58 +0000 | [diff] [blame] | 4091 | // of compile-time. It should also be noticed that we check for a single |
| 4092 | // predecessor and not only uniqueness. This to handle the situation when |
| 4093 | // Succ and Succ1 points to the same basic block. |
| 4094 | if (Succ->getSinglePredecessor() && dominatesAllUses(SI, Icmp, Succ)) { |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 4095 | NumSel++; |
| 4096 | SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent()); |
| 4097 | return true; |
| 4098 | } |
| 4099 | } |
| 4100 | return false; |
| 4101 | } |
| 4102 | |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4103 | /// Try to fold the comparison based on range information we can get by checking |
| 4104 | /// whether bits are known to be zero or one in the inputs. |
| 4105 | Instruction *InstCombiner::foldICmpUsingKnownBits(ICmpInst &I) { |
| 4106 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4107 | Type *Ty = Op0->getType(); |
Sanjay Patel | 0531f0a | 2016-09-12 15:52:28 +0000 | [diff] [blame] | 4108 | ICmpInst::Predicate Pred = I.getPredicate(); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4109 | |
| 4110 | // Get scalar or pointer size. |
| 4111 | unsigned BitWidth = Ty->isIntOrIntVectorTy() |
| 4112 | ? Ty->getScalarSizeInBits() |
| 4113 | : DL.getTypeSizeInBits(Ty->getScalarType()); |
| 4114 | |
| 4115 | if (!BitWidth) |
| 4116 | return nullptr; |
| 4117 | |
| 4118 | // If this is a normal comparison, it demands all bits. If it is a sign bit |
| 4119 | // comparison, it only demands the sign bit. |
| 4120 | bool IsSignBit = false; |
Sanjay Patel | f5887f1 | 2016-09-12 16:25:41 +0000 | [diff] [blame] | 4121 | const APInt *CmpC; |
| 4122 | if (match(Op1, m_APInt(CmpC))) { |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4123 | bool UnusedBit; |
Sanjay Patel | f5887f1 | 2016-09-12 16:25:41 +0000 | [diff] [blame] | 4124 | IsSignBit = isSignBitCheck(Pred, *CmpC, UnusedBit); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4127 | KnownBits Op0Known(BitWidth); |
| 4128 | KnownBits Op1Known(BitWidth); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4129 | |
Craig Topper | 47596dd | 2017-03-25 06:52:52 +0000 | [diff] [blame] | 4130 | if (SimplifyDemandedBits(&I, 0, |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 4131 | getDemandedBitsLHSMask(I, BitWidth, IsSignBit), |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4132 | Op0Known, 0)) |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4133 | return &I; |
| 4134 | |
Craig Topper | 47596dd | 2017-03-25 06:52:52 +0000 | [diff] [blame] | 4135 | if (SimplifyDemandedBits(&I, 1, APInt::getAllOnesValue(BitWidth), |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4136 | Op1Known, 0)) |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4137 | return &I; |
| 4138 | |
| 4139 | // Given the known and unknown bits, compute a range that the LHS could be |
| 4140 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
| 4141 | // EQ and NE we use unsigned values. |
| 4142 | APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0); |
| 4143 | APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0); |
| 4144 | if (I.isSigned()) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4145 | computeSignedMinMaxValuesFromKnownBits(Op0Known, Op0Min, Op0Max); |
| 4146 | computeSignedMinMaxValuesFromKnownBits(Op1Known, Op1Min, Op1Max); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4147 | } else { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4148 | computeUnsignedMinMaxValuesFromKnownBits(Op0Known, Op0Min, Op0Max); |
| 4149 | computeUnsignedMinMaxValuesFromKnownBits(Op1Known, Op1Min, Op1Max); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
Sanjay Patel | 0531f0a | 2016-09-12 15:52:28 +0000 | [diff] [blame] | 4153 | // figured out that the LHS is a constant. Constant fold this now, so that |
| 4154 | // code below can assume that Min != Max. |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4155 | if (!isa<Constant>(Op0) && Op0Min == Op0Max) |
Sanjay Patel | 0531f0a | 2016-09-12 15:52:28 +0000 | [diff] [blame] | 4156 | return new ICmpInst(Pred, ConstantInt::get(Op0->getType(), Op0Min), Op1); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4157 | if (!isa<Constant>(Op1) && Op1Min == Op1Max) |
Sanjay Patel | 0531f0a | 2016-09-12 15:52:28 +0000 | [diff] [blame] | 4158 | return new ICmpInst(Pred, Op0, ConstantInt::get(Op1->getType(), Op1Min)); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4159 | |
| 4160 | // Based on the range information we know about the LHS, see if we can |
| 4161 | // simplify this comparison. For example, (x&4) < 8 is always true. |
Sanjay Patel | 0531f0a | 2016-09-12 15:52:28 +0000 | [diff] [blame] | 4162 | switch (Pred) { |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4163 | default: |
| 4164 | llvm_unreachable("Unknown icmp opcode!"); |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4165 | case ICmpInst::ICMP_EQ: |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4166 | case ICmpInst::ICMP_NE: { |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4167 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) { |
| 4168 | return Pred == CmpInst::ICMP_EQ |
| 4169 | ? replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())) |
| 4170 | : replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4171 | } |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4172 | |
Sanjay Patel | 0531f0a | 2016-09-12 15:52:28 +0000 | [diff] [blame] | 4173 | // If all bits are known zero except for one, then we know at most one bit |
| 4174 | // is set. If the comparison is against zero, then this is a check to see if |
| 4175 | // *that* bit is set. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4176 | APInt Op0KnownZeroInverted = ~Op0Known.Zero; |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 4177 | if (Op1Known.isZero()) { |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4178 | // If the LHS is an AND with the same constant, look through it. |
| 4179 | Value *LHS = nullptr; |
Sanjay Patel | 7577a3d | 2016-09-15 14:15:47 +0000 | [diff] [blame] | 4180 | const APInt *LHSC; |
| 4181 | if (!match(Op0, m_And(m_Value(LHS), m_APInt(LHSC))) || |
| 4182 | *LHSC != Op0KnownZeroInverted) |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4183 | LHS = Op0; |
| 4184 | |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4185 | Value *X; |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4186 | if (match(LHS, m_Shl(m_One(), m_Value(X)))) { |
| 4187 | APInt ValToCheck = Op0KnownZeroInverted; |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4188 | Type *XTy = X->getType(); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4189 | if (ValToCheck.isPowerOf2()) { |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4190 | // ((1 << X) & 8) == 0 -> X != 3 |
| 4191 | // ((1 << X) & 8) != 0 -> X == 3 |
| 4192 | auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros()); |
| 4193 | auto NewPred = ICmpInst::getInversePredicate(Pred); |
| 4194 | return new ICmpInst(NewPred, X, CmpC); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4195 | } else if ((++ValToCheck).isPowerOf2()) { |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4196 | // ((1 << X) & 7) == 0 -> X >= 3 |
| 4197 | // ((1 << X) & 7) != 0 -> X < 3 |
| 4198 | auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros()); |
| 4199 | auto NewPred = |
| 4200 | Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT; |
| 4201 | return new ICmpInst(NewPred, X, CmpC); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4202 | } |
| 4203 | } |
| 4204 | |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4205 | // Check if the LHS is 8 >>u x and the result is a power of 2 like 1. |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4206 | const APInt *CI; |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 4207 | if (Op0KnownZeroInverted.isOneValue() && |
Sanjay Patel | 9efb1bd | 2016-09-14 23:38:56 +0000 | [diff] [blame] | 4208 | match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) { |
| 4209 | // ((8 >>u X) & 1) == 0 -> X != 3 |
| 4210 | // ((8 >>u X) & 1) != 0 -> X == 3 |
| 4211 | unsigned CmpVal = CI->countTrailingZeros(); |
| 4212 | auto NewPred = ICmpInst::getInversePredicate(Pred); |
| 4213 | return new ICmpInst(NewPred, X, ConstantInt::get(X->getType(), CmpVal)); |
| 4214 | } |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4215 | } |
| 4216 | break; |
| 4217 | } |
| 4218 | case ICmpInst::ICMP_ULT: { |
| 4219 | if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B) |
| 4220 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4221 | if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B) |
| 4222 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4223 | if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B) |
| 4224 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4225 | |
| 4226 | const APInt *CmpC; |
| 4227 | if (match(Op1, m_APInt(CmpC))) { |
| 4228 | // A <u C -> A == C-1 if min(A)+1 == C |
| 4229 | if (Op1Max == Op0Min + 1) { |
| 4230 | Constant *CMinus1 = ConstantInt::get(Op0->getType(), *CmpC - 1); |
| 4231 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, CMinus1); |
| 4232 | } |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4233 | } |
| 4234 | break; |
| 4235 | } |
| 4236 | case ICmpInst::ICMP_UGT: { |
| 4237 | if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B) |
| 4238 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4239 | |
| 4240 | if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B) |
| 4241 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4242 | |
| 4243 | if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B) |
| 4244 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4245 | |
| 4246 | const APInt *CmpC; |
| 4247 | if (match(Op1, m_APInt(CmpC))) { |
| 4248 | // A >u C -> A == C+1 if max(a)-1 == C |
| 4249 | if (*CmpC == Op0Max - 1) |
| 4250 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
| 4251 | ConstantInt::get(Op1->getType(), *CmpC + 1)); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4252 | } |
| 4253 | break; |
| 4254 | } |
| 4255 | case ICmpInst::ICMP_SLT: |
| 4256 | if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C) |
| 4257 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4258 | if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C) |
| 4259 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4260 | if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B) |
| 4261 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4262 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 4263 | if (Op1Max == Op0Min + 1) // A <s C -> A == C-1 if min(A)+1 == C |
| 4264 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4265 | Builder.getInt(CI->getValue() - 1)); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4266 | } |
| 4267 | break; |
| 4268 | case ICmpInst::ICMP_SGT: |
| 4269 | if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B) |
| 4270 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4271 | if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B) |
| 4272 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4273 | |
| 4274 | if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B) |
| 4275 | return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1); |
| 4276 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 4277 | if (Op1Min == Op0Max - 1) // A >s C -> A == C+1 if max(A)-1 == C |
| 4278 | return new ICmpInst(ICmpInst::ICMP_EQ, Op0, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4279 | Builder.getInt(CI->getValue() + 1)); |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4280 | } |
| 4281 | break; |
| 4282 | case ICmpInst::ICMP_SGE: |
| 4283 | assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!"); |
| 4284 | if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B) |
| 4285 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4286 | if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B) |
| 4287 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4288 | break; |
| 4289 | case ICmpInst::ICMP_SLE: |
| 4290 | assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!"); |
| 4291 | if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B) |
| 4292 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4293 | if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B) |
| 4294 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4295 | break; |
| 4296 | case ICmpInst::ICMP_UGE: |
| 4297 | assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!"); |
| 4298 | if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B) |
| 4299 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4300 | if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B) |
| 4301 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4302 | break; |
| 4303 | case ICmpInst::ICMP_ULE: |
| 4304 | assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!"); |
| 4305 | if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B) |
| 4306 | return replaceInstUsesWith(I, ConstantInt::getTrue(I.getType())); |
| 4307 | if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B) |
| 4308 | return replaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); |
| 4309 | break; |
| 4310 | } |
| 4311 | |
| 4312 | // Turn a signed comparison into an unsigned one if both operands are known to |
| 4313 | // have the same sign. |
| 4314 | if (I.isSigned() && |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 4315 | ((Op0Known.Zero.isNegative() && Op1Known.Zero.isNegative()) || |
| 4316 | (Op0Known.One.isNegative() && Op1Known.One.isNegative()))) |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4317 | return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1); |
| 4318 | |
| 4319 | return nullptr; |
| 4320 | } |
| 4321 | |
Sanjay Patel | d5b0e54 | 2016-04-29 16:22:25 +0000 | [diff] [blame] | 4322 | /// If we have an icmp le or icmp ge instruction with a constant operand, turn |
| 4323 | /// it into the appropriate icmp lt or icmp gt instruction. This transform |
| 4324 | /// allows them to be folded in visitICmpInst. |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4325 | static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) { |
| 4326 | ICmpInst::Predicate Pred = I.getPredicate(); |
| 4327 | if (Pred != ICmpInst::ICMP_SLE && Pred != ICmpInst::ICMP_SGE && |
| 4328 | Pred != ICmpInst::ICMP_ULE && Pred != ICmpInst::ICMP_UGE) |
| 4329 | return nullptr; |
| 4330 | |
Sanjay Patel | d5b0e54 | 2016-04-29 16:22:25 +0000 | [diff] [blame] | 4331 | Value *Op0 = I.getOperand(0); |
| 4332 | Value *Op1 = I.getOperand(1); |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4333 | auto *Op1C = dyn_cast<Constant>(Op1); |
| 4334 | if (!Op1C) |
| 4335 | return nullptr; |
Sanjay Patel | d5b0e54 | 2016-04-29 16:22:25 +0000 | [diff] [blame] | 4336 | |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4337 | // Check if the constant operand can be safely incremented/decremented without |
| 4338 | // overflowing/underflowing. For scalars, SimplifyICmpInst has already handled |
| 4339 | // the edge cases for us, so we just assert on them. For vectors, we must |
| 4340 | // handle the edge cases. |
| 4341 | Type *Op1Type = Op1->getType(); |
| 4342 | bool IsSigned = I.isSigned(); |
| 4343 | bool IsLE = (Pred == ICmpInst::ICMP_SLE || Pred == ICmpInst::ICMP_ULE); |
Sanjay Patel | 1825493 | 2016-05-17 01:12:31 +0000 | [diff] [blame] | 4344 | auto *CI = dyn_cast<ConstantInt>(Op1C); |
| 4345 | if (CI) { |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4346 | // A <= MAX -> TRUE ; A >= MIN -> TRUE |
| 4347 | assert(IsLE ? !CI->isMaxValue(IsSigned) : !CI->isMinValue(IsSigned)); |
| 4348 | } else if (Op1Type->isVectorTy()) { |
Sanjay Patel | b79ab27 | 2016-05-13 15:10:46 +0000 | [diff] [blame] | 4349 | // 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] | 4350 | // are for scalar, we could remove the min/max checks. However, to do that, |
| 4351 | // we would have to use insertelement/shufflevector to replace edge values. |
| 4352 | unsigned NumElts = Op1Type->getVectorNumElements(); |
| 4353 | for (unsigned i = 0; i != NumElts; ++i) { |
| 4354 | Constant *Elt = Op1C->getAggregateElement(i); |
Benjamin Kramer | ca9a0fe | 2016-05-17 12:08:55 +0000 | [diff] [blame] | 4355 | if (!Elt) |
| 4356 | return nullptr; |
| 4357 | |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4358 | if (isa<UndefValue>(Elt)) |
| 4359 | continue; |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 4360 | |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4361 | // Bail out if we can't determine if this constant is min/max or if we |
| 4362 | // know that this constant is min/max. |
| 4363 | auto *CI = dyn_cast<ConstantInt>(Elt); |
| 4364 | if (!CI || (IsLE ? CI->isMaxValue(IsSigned) : CI->isMinValue(IsSigned))) |
| 4365 | return nullptr; |
Sanjay Patel | b79ab27 | 2016-05-13 15:10:46 +0000 | [diff] [blame] | 4366 | } |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4367 | } else { |
| 4368 | // ConstantExpr? |
| 4369 | return nullptr; |
Sanjay Patel | b79ab27 | 2016-05-13 15:10:46 +0000 | [diff] [blame] | 4370 | } |
Sanjay Patel | d5b0e54 | 2016-04-29 16:22:25 +0000 | [diff] [blame] | 4371 | |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4372 | // Increment or decrement the constant and set the new comparison predicate: |
| 4373 | // ULE -> ULT ; UGE -> UGT ; SLE -> SLT ; SGE -> SGT |
Sanjay Patel | 22b01fe | 2016-05-17 20:20:40 +0000 | [diff] [blame] | 4374 | Constant *OneOrNegOne = ConstantInt::get(Op1Type, IsLE ? 1 : -1, true); |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4375 | CmpInst::Predicate NewPred = IsLE ? ICmpInst::ICMP_ULT: ICmpInst::ICMP_UGT; |
| 4376 | NewPred = IsSigned ? ICmpInst::getSignedPredicate(NewPred) : NewPred; |
| 4377 | return new ICmpInst(NewPred, Op0, ConstantExpr::getAdd(Op1C, OneOrNegOne)); |
Sanjay Patel | d5b0e54 | 2016-04-29 16:22:25 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Sanjay Patel | e5747e3 | 2017-05-17 22:15:07 +0000 | [diff] [blame] | 4380 | /// Integer compare with boolean values can always be turned into bitwise ops. |
| 4381 | static Instruction *canonicalizeICmpBool(ICmpInst &I, |
| 4382 | InstCombiner::BuilderTy &Builder) { |
| 4383 | Value *A = I.getOperand(0), *B = I.getOperand(1); |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 4384 | assert(A->getType()->isIntOrIntVectorTy(1) && "Bools only"); |
Sanjay Patel | e5747e3 | 2017-05-17 22:15:07 +0000 | [diff] [blame] | 4385 | |
Sanjay Patel | ba212c2 | 2017-05-17 22:29:40 +0000 | [diff] [blame] | 4386 | // A boolean compared to true/false can be simplified to Op0/true/false in |
| 4387 | // 14 out of the 20 (10 predicates * 2 constants) possible combinations. |
| 4388 | // Cases not handled by InstSimplify are always 'not' of Op0. |
| 4389 | if (match(B, m_Zero())) { |
| 4390 | switch (I.getPredicate()) { |
| 4391 | case CmpInst::ICMP_EQ: // A == 0 -> !A |
| 4392 | case CmpInst::ICMP_ULE: // A <=u 0 -> !A |
| 4393 | case CmpInst::ICMP_SGE: // A >=s 0 -> !A |
| 4394 | return BinaryOperator::CreateNot(A); |
| 4395 | default: |
| 4396 | llvm_unreachable("ICmp i1 X, C not simplified as expected."); |
| 4397 | } |
| 4398 | } else if (match(B, m_One())) { |
| 4399 | switch (I.getPredicate()) { |
| 4400 | case CmpInst::ICMP_NE: // A != 1 -> !A |
| 4401 | case CmpInst::ICMP_ULT: // A <u 1 -> !A |
| 4402 | case CmpInst::ICMP_SGT: // A >s -1 -> !A |
| 4403 | return BinaryOperator::CreateNot(A); |
| 4404 | default: |
| 4405 | llvm_unreachable("ICmp i1 X, C not simplified as expected."); |
| 4406 | } |
| 4407 | } |
| 4408 | |
Sanjay Patel | e5747e3 | 2017-05-17 22:15:07 +0000 | [diff] [blame] | 4409 | switch (I.getPredicate()) { |
| 4410 | default: |
| 4411 | llvm_unreachable("Invalid icmp instruction!"); |
| 4412 | case ICmpInst::ICMP_EQ: |
| 4413 | // icmp eq i1 A, B -> ~(A ^ B) |
| 4414 | return BinaryOperator::CreateNot(Builder.CreateXor(A, B)); |
| 4415 | |
| 4416 | case ICmpInst::ICMP_NE: |
| 4417 | // icmp ne i1 A, B -> A ^ B |
| 4418 | return BinaryOperator::CreateXor(A, B); |
| 4419 | |
| 4420 | case ICmpInst::ICMP_UGT: |
| 4421 | // icmp ugt -> icmp ult |
| 4422 | std::swap(A, B); |
| 4423 | LLVM_FALLTHROUGH; |
| 4424 | case ICmpInst::ICMP_ULT: |
| 4425 | // icmp ult i1 A, B -> ~A & B |
| 4426 | return BinaryOperator::CreateAnd(Builder.CreateNot(A), B); |
| 4427 | |
| 4428 | case ICmpInst::ICMP_SGT: |
| 4429 | // icmp sgt -> icmp slt |
| 4430 | std::swap(A, B); |
| 4431 | LLVM_FALLTHROUGH; |
| 4432 | case ICmpInst::ICMP_SLT: |
| 4433 | // icmp slt i1 A, B -> A & ~B |
| 4434 | return BinaryOperator::CreateAnd(Builder.CreateNot(B), A); |
| 4435 | |
| 4436 | case ICmpInst::ICMP_UGE: |
| 4437 | // icmp uge -> icmp ule |
| 4438 | std::swap(A, B); |
| 4439 | LLVM_FALLTHROUGH; |
| 4440 | case ICmpInst::ICMP_ULE: |
| 4441 | // icmp ule i1 A, B -> ~A | B |
| 4442 | return BinaryOperator::CreateOr(Builder.CreateNot(A), B); |
| 4443 | |
| 4444 | case ICmpInst::ICMP_SGE: |
| 4445 | // icmp sge -> icmp sle |
| 4446 | std::swap(A, B); |
| 4447 | LLVM_FALLTHROUGH; |
| 4448 | case ICmpInst::ICMP_SLE: |
| 4449 | // icmp sle i1 A, B -> A | ~B |
| 4450 | return BinaryOperator::CreateOr(Builder.CreateNot(B), A); |
| 4451 | } |
| 4452 | } |
| 4453 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4454 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 4455 | bool Changed = false; |
Chris Lattner | 9306ffa | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 4456 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 4457 | unsigned Op0Cplxity = getComplexity(Op0); |
| 4458 | unsigned Op1Cplxity = getComplexity(Op1); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4459 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4460 | /// Orders the operands of the compare so that they are listed from most |
| 4461 | /// complex to least complex. This puts constants before unary operators, |
| 4462 | /// before binary operators. |
Quentin Colombet | 5ab5555 | 2013-09-09 20:56:48 +0000 | [diff] [blame] | 4463 | if (Op0Cplxity < Op1Cplxity || |
Sanjay Patel | 4c20423 | 2016-06-04 20:39:22 +0000 | [diff] [blame] | 4464 | (Op0Cplxity == Op1Cplxity && swapMayExposeCSEOpportunities(Op0, Op1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4465 | I.swapOperands(); |
Chris Lattner | 9306ffa | 2010-02-01 19:54:45 +0000 | [diff] [blame] | 4466 | std::swap(Op0, Op1); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4467 | Changed = true; |
| 4468 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4469 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 4470 | if (Value *V = SimplifyICmpInst(I.getPredicate(), Op0, Op1, |
| 4471 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4472 | return replaceInstUsesWith(I, V); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4473 | |
Uriel Korach | 1897223 | 2017-09-10 08:31:22 +0000 | [diff] [blame] | 4474 | // 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] | 4475 | // ie, abs(val) != 0 -> val != 0 |
Sanjay Patel | 4c20423 | 2016-06-04 20:39:22 +0000 | [diff] [blame] | 4476 | if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero())) { |
Pete Cooper | fdddc27 | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 4477 | Value *Cond, *SelectTrue, *SelectFalse; |
| 4478 | if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue), |
Pete Cooper | bc5c524 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 4479 | m_Value(SelectFalse)))) { |
Pete Cooper | fdddc27 | 2011-12-01 19:13:26 +0000 | [diff] [blame] | 4480 | if (Value *V = dyn_castNegVal(SelectTrue)) { |
| 4481 | if (V == SelectFalse) |
| 4482 | return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); |
| 4483 | } |
| 4484 | else if (Value *V = dyn_castNegVal(SelectFalse)) { |
| 4485 | if (V == SelectTrue) |
| 4486 | return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1); |
Pete Cooper | bc5c524 | 2011-12-01 03:58:40 +0000 | [diff] [blame] | 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 4491 | if (Op0->getType()->isIntOrIntVectorTy(1)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4492 | if (Instruction *Res = canonicalizeICmpBool(I, Builder)) |
Sanjay Patel | e5747e3 | 2017-05-17 22:15:07 +0000 | [diff] [blame] | 4493 | return Res; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4494 | |
Sanjay Patel | e9b2c32 | 2016-05-17 00:57:57 +0000 | [diff] [blame] | 4495 | if (ICmpInst *NewICmp = canonicalizeCmpWithConstant(I)) |
Sanjay Patel | d5b0e54 | 2016-04-29 16:22:25 +0000 | [diff] [blame] | 4496 | return NewICmp; |
| 4497 | |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 4498 | if (Instruction *Res = foldICmpWithConstant(I)) |
| 4499 | return Res; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4500 | |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 4501 | if (Instruction *Res = foldICmpUsingKnownBits(I)) |
| 4502 | return Res; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4503 | |
| 4504 | // Test if the ICmpInst instruction is used exclusively by a select as |
| 4505 | // part of a minimum or maximum operation. If so, refrain from doing |
| 4506 | // any other folding. This helps out other analyses which understand |
| 4507 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 4508 | // and CodeGen. And in this case, at least one of the comparison |
| 4509 | // operands has at least one user besides the compare (the select), |
| 4510 | // which would often largely negate the benefit of folding anyway. |
| 4511 | if (I.hasOneUse()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 4512 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin())) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4513 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 4514 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4515 | return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4516 | |
Sanjay Patel | febcb9c | 2017-01-27 23:26:27 +0000 | [diff] [blame] | 4517 | // FIXME: We only do this after checking for min/max to prevent infinite |
| 4518 | // looping caused by a reverse canonicalization of these patterns for min/max. |
| 4519 | // FIXME: The organization of folds is a mess. These would naturally go into |
| 4520 | // canonicalizeCmpWithConstant(), but we can't move all of the above folds |
| 4521 | // down here after the min/max restriction. |
| 4522 | ICmpInst::Predicate Pred = I.getPredicate(); |
| 4523 | const APInt *C; |
| 4524 | if (match(Op1, m_APInt(C))) { |
| 4525 | // For i32: x >u 2147483647 -> x <s 0 -> true if sign bit set |
| 4526 | if (Pred == ICmpInst::ICMP_UGT && C->isMaxSignedValue()) { |
| 4527 | Constant *Zero = Constant::getNullValue(Op0->getType()); |
| 4528 | return new ICmpInst(ICmpInst::ICMP_SLT, Op0, Zero); |
| 4529 | } |
| 4530 | |
| 4531 | // For i32: x <u 2147483648 -> x >s -1 -> true if sign bit clear |
| 4532 | if (Pred == ICmpInst::ICMP_ULT && C->isMinSignedValue()) { |
| 4533 | Constant *AllOnes = Constant::getAllOnesValue(Op0->getType()); |
| 4534 | return new ICmpInst(ICmpInst::ICMP_SGT, Op0, AllOnes); |
| 4535 | } |
| 4536 | } |
| 4537 | |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 4538 | if (Instruction *Res = foldICmpInstWithConstant(I)) |
Sanjay Patel | 1271bf9 | 2016-07-23 13:06:49 +0000 | [diff] [blame] | 4539 | return Res; |
| 4540 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 4541 | if (Instruction *Res = foldICmpInstWithConstantNotInt(I)) |
| 4542 | return Res; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4543 | |
| 4544 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
| 4545 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0)) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 4546 | if (Instruction *NI = foldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4547 | return NI; |
| 4548 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1)) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 4549 | if (Instruction *NI = foldGEPICmp(GEP, Op0, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4550 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
| 4551 | return NI; |
| 4552 | |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 4553 | // Try to optimize equality comparisons against alloca-based pointers. |
| 4554 | if (Op0->getType()->isPointerTy() && I.isEquality()) { |
| 4555 | assert(Op1->getType()->isPointerTy() && "Comparing pointer with non-pointer?"); |
| 4556 | if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op0, DL))) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 4557 | if (Instruction *New = foldAllocaCmp(I, Alloca, Op1)) |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 4558 | return New; |
| 4559 | if (auto *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Op1, DL))) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 4560 | if (Instruction *New = foldAllocaCmp(I, Alloca, Op0)) |
Hans Wennborg | f1f3651 | 2015-10-07 00:20:07 +0000 | [diff] [blame] | 4561 | return New; |
| 4562 | } |
| 4563 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4564 | // Test to see if the operands of the icmp are casted versions of other |
| 4565 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 4566 | // now. |
| 4567 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4568 | if (Op0->getType()->isPointerTy() && |
| 4569 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4570 | // We keep moving the cast from the left operand over to the right |
| 4571 | // operand, where it can often be eliminated completely. |
| 4572 | Op0 = CI->getOperand(0); |
| 4573 | |
| 4574 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 4575 | // so eliminate it as well. |
| 4576 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 4577 | Op1 = CI2->getOperand(0); |
| 4578 | |
| 4579 | // If Op1 is a constant, we can fold the cast into the constant. |
| 4580 | if (Op0->getType() != Op1->getType()) { |
| 4581 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| 4582 | Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType()); |
| 4583 | } else { |
| 4584 | // Otherwise, cast the RHS right before the icmp |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4585 | Op1 = Builder.CreateBitCast(Op1, Op0->getType()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4586 | } |
| 4587 | } |
| 4588 | return new ICmpInst(I.getPredicate(), Op0, Op1); |
| 4589 | } |
| 4590 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4591 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4592 | if (isa<CastInst>(Op0)) { |
| 4593 | // Handle the special case of: icmp (cast bool to X), <cst> |
| 4594 | // This comes up when you have code like |
| 4595 | // int X = A < B; |
| 4596 | // if (X) ... |
| 4597 | // For generality, we handle any zero-extension of any operand comparison |
| 4598 | // with a constant or another cast from the same type. |
| 4599 | if (isa<Constant>(Op1) || isa<CastInst>(Op1)) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 4600 | if (Instruction *R = foldICmpWithCastAndCast(I)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4601 | return R; |
| 4602 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4603 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 4604 | if (Instruction *Res = foldICmpBinOp(I)) |
| 4605 | return Res; |
Duncan Sands | e522001 | 2011-02-17 07:46:37 +0000 | [diff] [blame] | 4606 | |
Sanjay Patel | dd46b52 | 2016-12-19 17:32:37 +0000 | [diff] [blame] | 4607 | if (Instruction *Res = foldICmpWithMinMax(I)) |
Sanjay Patel | d640641 | 2016-12-15 19:13:37 +0000 | [diff] [blame] | 4608 | return Res; |
| 4609 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 4610 | { |
| 4611 | Value *A, *B; |
David Majnemer | 1a08acc | 2013-04-12 17:25:07 +0000 | [diff] [blame] | 4612 | // Transform (A & ~B) == 0 --> (A & B) != 0 |
| 4613 | // and (A & ~B) != 0 --> (A & B) == 0 |
| 4614 | // if A is a power of 2. |
| 4615 | 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] | 4616 | match(Op1, m_Zero()) && |
Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 4617 | isKnownToBeAPowerOfTwo(A, false, 0, &I) && I.isEquality()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4618 | return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(A, B), |
David Majnemer | 1a08acc | 2013-04-12 17:25:07 +0000 | [diff] [blame] | 4619 | Op1); |
| 4620 | |
Sanjay Patel | 4dc85eb | 2017-06-02 16:11:14 +0000 | [diff] [blame] | 4621 | // ~X < ~Y --> Y < X |
| 4622 | // ~X < C --> X > ~C |
Chris Lattner | f3c4eef | 2011-01-15 05:41:33 +0000 | [diff] [blame] | 4623 | if (match(Op0, m_Not(m_Value(A)))) { |
| 4624 | if (match(Op1, m_Not(m_Value(B)))) |
| 4625 | return new ICmpInst(I.getPredicate(), B, A); |
Sanjay Patel | 4dc85eb | 2017-06-02 16:11:14 +0000 | [diff] [blame] | 4626 | |
Sanjay Patel | ce241f4 | 2017-06-02 16:29:41 +0000 | [diff] [blame] | 4627 | const APInt *C; |
| 4628 | if (match(Op1, m_APInt(C))) |
Sanjay Patel | 4dc85eb | 2017-06-02 16:11:14 +0000 | [diff] [blame] | 4629 | return new ICmpInst(I.getSwappedPredicate(), A, |
Sanjay Patel | ce241f4 | 2017-06-02 16:29:41 +0000 | [diff] [blame] | 4630 | ConstantInt::get(Op1->getType(), ~(*C))); |
Chris Lattner | f3c4eef | 2011-01-15 05:41:33 +0000 | [diff] [blame] | 4631 | } |
Chris Lattner | 5e0c0c7 | 2010-12-19 19:37:52 +0000 | [diff] [blame] | 4632 | |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 4633 | Instruction *AddI = nullptr; |
| 4634 | if (match(&I, m_UAddWithOverflow(m_Value(A), m_Value(B), |
| 4635 | m_Instruction(AddI))) && |
| 4636 | isa<IntegerType>(A->getType())) { |
| 4637 | Value *Result; |
| 4638 | Constant *Overflow; |
| 4639 | if (OptimizeOverflowCheck(OCF_UNSIGNED_ADD, A, B, *AddI, Result, |
| 4640 | Overflow)) { |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4641 | replaceInstUsesWith(*AddI, Result); |
| 4642 | return replaceInstUsesWith(I, Overflow); |
Sanjoy Das | b6c5914 | 2015-04-10 21:07:09 +0000 | [diff] [blame] | 4643 | } |
| 4644 | } |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 4645 | |
| 4646 | // (zext a) * (zext b) --> llvm.umul.with.overflow. |
| 4647 | if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 4648 | if (Instruction *R = processUMulZExtIdiom(I, Op0, Op1, *this)) |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 4649 | return R; |
| 4650 | } |
| 4651 | if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) { |
Sanjay Patel | d93c4c0 | 2016-09-15 18:22:25 +0000 | [diff] [blame] | 4652 | if (Instruction *R = processUMulZExtIdiom(I, Op1, Op0, *this)) |
Serge Pavlov | 4bb54d5 | 2014-04-13 18:23:41 +0000 | [diff] [blame] | 4653 | return R; |
| 4654 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4655 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4656 | |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 4657 | if (Instruction *Res = foldICmpEquality(I)) |
| 4658 | return Res; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4659 | |
David Majnemer | c1eca5a | 2014-11-06 23:23:30 +0000 | [diff] [blame] | 4660 | // The 'cmpxchg' instruction returns an aggregate containing the old value and |
| 4661 | // an i1 which indicates whether or not we successfully did the swap. |
| 4662 | // |
| 4663 | // Replace comparisons between the old value and the expected value with the |
| 4664 | // indicator that 'cmpxchg' returns. |
| 4665 | // |
| 4666 | // N.B. This transform is only valid when the 'cmpxchg' is not permitted to |
| 4667 | // spuriously fail. In those cases, the old value may equal the expected |
| 4668 | // value but it is possible for the swap to not occur. |
| 4669 | if (I.getPredicate() == ICmpInst::ICMP_EQ) |
| 4670 | if (auto *EVI = dyn_cast<ExtractValueInst>(Op0)) |
| 4671 | if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand())) |
| 4672 | if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 && |
| 4673 | !ACXI->isWeak()) |
| 4674 | return ExtractValueInst::Create(ACXI, 1); |
| 4675 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4676 | { |
| 4677 | Value *X; ConstantInt *Cst; |
| 4678 | // icmp X+Cst, X |
| 4679 | if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X) |
Craig Topper | a85f862 | 2017-08-23 05:46:09 +0000 | [diff] [blame] | 4680 | return foldICmpAddOpConst(X, Cst, I.getPredicate()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4681 | |
| 4682 | // icmp X, X+Cst |
| 4683 | if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X) |
Craig Topper | a85f862 | 2017-08-23 05:46:09 +0000 | [diff] [blame] | 4684 | return foldICmpAddOpConst(X, Cst, I.getSwappedPredicate()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4685 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4686 | return Changed ? &I : nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Sanjay Patel | 5f0217f | 2016-06-05 16:46:18 +0000 | [diff] [blame] | 4689 | /// Fold fcmp ([us]itofp x, cst) if possible. |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 4690 | Instruction *InstCombiner::foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4691 | Constant *RHSC) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4692 | if (!isa<ConstantFP>(RHSC)) return nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4693 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4694 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4695 | // Get the width of the mantissa. We don't want to hack on conversions that |
| 4696 | // might lose information from the integer, e.g. "i64 -> float" |
| 4697 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4698 | if (MantissaWidth == -1) return nullptr; // Unknown. |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4699 | |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4700 | IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); |
| 4701 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4702 | bool LHSUnsigned = isa<UIToFPInst>(LHSI); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4703 | |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4704 | if (I.isEquality()) { |
| 4705 | FCmpInst::Predicate P = I.getPredicate(); |
| 4706 | bool IsExact = false; |
| 4707 | APSInt RHSCvt(IntTy->getBitWidth(), LHSUnsigned); |
| 4708 | RHS.convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact); |
| 4709 | |
| 4710 | // If the floating point constant isn't an integer value, we know if we will |
| 4711 | // ever compare equal / not equal to it. |
| 4712 | if (!IsExact) { |
| 4713 | // TODO: Can never be -0.0 and other non-representable values |
| 4714 | APFloat RHSRoundInt(RHS); |
| 4715 | RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven); |
| 4716 | if (RHS.compare(RHSRoundInt) != APFloat::cmpEqual) { |
| 4717 | if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4718 | return replaceInstUsesWith(I, Builder.getFalse()); |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4719 | |
| 4720 | assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4721 | return replaceInstUsesWith(I, Builder.getTrue()); |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
| 4725 | // TODO: If the constant is exactly representable, is it always OK to do |
| 4726 | // equality compares as integer? |
| 4727 | } |
| 4728 | |
Arch D. Robison | 8ed0854 | 2015-09-15 17:51:59 +0000 | [diff] [blame] | 4729 | // Check to see that the input is converted from an integer type that is small |
| 4730 | // enough that preserves all bits. TODO: check here for "known" sign bits. |
| 4731 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. |
| 4732 | unsigned InputSize = IntTy->getScalarSizeInBits(); |
Matt Arsenault | 55e7312 | 2015-01-06 15:50:59 +0000 | [diff] [blame] | 4733 | |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 4734 | // Following test does NOT adjust InputSize downwards for signed inputs, |
| 4735 | // because the most negative value still requires all the mantissa bits |
Arch D. Robison | 8ed0854 | 2015-09-15 17:51:59 +0000 | [diff] [blame] | 4736 | // to distinguish it from one less than that value. |
| 4737 | if ((int)InputSize > MantissaWidth) { |
| 4738 | // Conversion would lose accuracy. Check if loss can impact comparison. |
| 4739 | int Exp = ilogb(RHS); |
| 4740 | if (Exp == APFloat::IEK_Inf) { |
| 4741 | int MaxExponent = ilogb(APFloat::getLargest(RHS.getSemantics())); |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 4742 | if (MaxExponent < (int)InputSize - !LHSUnsigned) |
Arch D. Robison | 8ed0854 | 2015-09-15 17:51:59 +0000 | [diff] [blame] | 4743 | // Conversion could create infinity. |
| 4744 | return nullptr; |
| 4745 | } else { |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 4746 | // 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] | 4747 | // and first condition is trivially false. |
Justin Bogner | c7e4fbe | 2016-08-05 01:09:48 +0000 | [diff] [blame] | 4748 | if (MantissaWidth <= Exp && Exp <= (int)InputSize - !LHSUnsigned) |
Arch D. Robison | 8ed0854 | 2015-09-15 17:51:59 +0000 | [diff] [blame] | 4749 | // Conversion could affect comparison. |
| 4750 | return nullptr; |
| 4751 | } |
| 4752 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4753 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4754 | // Otherwise, we can potentially simplify the comparison. We know that it |
| 4755 | // will always come through as an integer value and we know the constant is |
| 4756 | // not a NAN (it would have been previously simplified). |
| 4757 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4758 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4759 | ICmpInst::Predicate Pred; |
| 4760 | switch (I.getPredicate()) { |
| 4761 | default: llvm_unreachable("Unexpected predicate!"); |
| 4762 | case FCmpInst::FCMP_UEQ: |
| 4763 | case FCmpInst::FCMP_OEQ: |
| 4764 | Pred = ICmpInst::ICMP_EQ; |
| 4765 | break; |
| 4766 | case FCmpInst::FCMP_UGT: |
| 4767 | case FCmpInst::FCMP_OGT: |
| 4768 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT; |
| 4769 | break; |
| 4770 | case FCmpInst::FCMP_UGE: |
| 4771 | case FCmpInst::FCMP_OGE: |
| 4772 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE; |
| 4773 | break; |
| 4774 | case FCmpInst::FCMP_ULT: |
| 4775 | case FCmpInst::FCMP_OLT: |
| 4776 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT; |
| 4777 | break; |
| 4778 | case FCmpInst::FCMP_ULE: |
| 4779 | case FCmpInst::FCMP_OLE: |
| 4780 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE; |
| 4781 | break; |
| 4782 | case FCmpInst::FCMP_UNE: |
| 4783 | case FCmpInst::FCMP_ONE: |
| 4784 | Pred = ICmpInst::ICMP_NE; |
| 4785 | break; |
| 4786 | case FCmpInst::FCMP_ORD: |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4787 | return replaceInstUsesWith(I, Builder.getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4788 | case FCmpInst::FCMP_UNO: |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4789 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4790 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4791 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4792 | // 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] | 4793 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4794 | // See if the FP constant is too large for the integer. For example, |
| 4795 | // comparing an i8 to 300.0. |
| 4796 | unsigned IntWidth = IntTy->getScalarSizeInBits(); |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4797 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4798 | if (!LHSUnsigned) { |
| 4799 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF |
| 4800 | // and large values. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4801 | APFloat SMax(RHS.getSemantics()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4802 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, |
| 4803 | APFloat::rmNearestTiesToEven); |
| 4804 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 |
| 4805 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
| 4806 | Pred == ICmpInst::ICMP_SLE) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4807 | return replaceInstUsesWith(I, Builder.getTrue()); |
| 4808 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4809 | } |
| 4810 | } else { |
| 4811 | // If the RHS value is > UnsignedMax, fold the comparison. This handles |
| 4812 | // +INF and large values. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4813 | APFloat UMax(RHS.getSemantics()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4814 | UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false, |
| 4815 | APFloat::rmNearestTiesToEven); |
| 4816 | if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 |
| 4817 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || |
| 4818 | Pred == ICmpInst::ICMP_ULE) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4819 | return replaceInstUsesWith(I, Builder.getTrue()); |
| 4820 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4821 | } |
| 4822 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4823 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4824 | if (!LHSUnsigned) { |
| 4825 | // See if the RHS value is < SignedMin. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4826 | APFloat SMin(RHS.getSemantics()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4827 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, |
| 4828 | APFloat::rmNearestTiesToEven); |
| 4829 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 |
| 4830 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
| 4831 | Pred == ICmpInst::ICMP_SGE) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4832 | return replaceInstUsesWith(I, Builder.getTrue()); |
| 4833 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4834 | } |
Devang Patel | 698452b | 2012-02-13 23:05:18 +0000 | [diff] [blame] | 4835 | } else { |
| 4836 | // See if the RHS value is < UnsignedMin. |
Michael Gottesman | 79b0967 | 2013-06-27 21:58:19 +0000 | [diff] [blame] | 4837 | APFloat SMin(RHS.getSemantics()); |
Devang Patel | 698452b | 2012-02-13 23:05:18 +0000 | [diff] [blame] | 4838 | SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true, |
| 4839 | APFloat::rmNearestTiesToEven); |
| 4840 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0 |
| 4841 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT || |
| 4842 | Pred == ICmpInst::ICMP_UGE) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4843 | return replaceInstUsesWith(I, Builder.getTrue()); |
| 4844 | return replaceInstUsesWith(I, Builder.getFalse()); |
Devang Patel | 698452b | 2012-02-13 23:05:18 +0000 | [diff] [blame] | 4845 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or |
| 4849 | // [0, UMAX], but it may still be fractional. See if it is fractional by |
| 4850 | // casting the FP value to the integer value and back, checking for equality. |
| 4851 | // Don't do this for zero, because -0.0 is not fractional. |
| 4852 | Constant *RHSInt = LHSUnsigned |
| 4853 | ? ConstantExpr::getFPToUI(RHSC, IntTy) |
| 4854 | : ConstantExpr::getFPToSI(RHSC, IntTy); |
| 4855 | if (!RHS.isZero()) { |
| 4856 | bool Equal = LHSUnsigned |
| 4857 | ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC |
| 4858 | : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC; |
| 4859 | if (!Equal) { |
| 4860 | // If we had a comparison against a fractional value, we have to adjust |
| 4861 | // the compare predicate and sometimes the value. RHSC is rounded towards |
| 4862 | // zero at this point. |
| 4863 | switch (Pred) { |
| 4864 | default: llvm_unreachable("Unexpected integer comparison!"); |
| 4865 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4866 | return replaceInstUsesWith(I, Builder.getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4867 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4868 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4869 | case ICmpInst::ICMP_ULE: |
| 4870 | // (float)int <= 4.4 --> int <= 4 |
| 4871 | // (float)int <= -4.4 --> false |
| 4872 | if (RHS.isNegative()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4873 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4874 | break; |
| 4875 | case ICmpInst::ICMP_SLE: |
| 4876 | // (float)int <= 4.4 --> int <= 4 |
| 4877 | // (float)int <= -4.4 --> int < -4 |
| 4878 | if (RHS.isNegative()) |
| 4879 | Pred = ICmpInst::ICMP_SLT; |
| 4880 | break; |
| 4881 | case ICmpInst::ICMP_ULT: |
| 4882 | // (float)int < -4.4 --> false |
| 4883 | // (float)int < 4.4 --> int <= 4 |
| 4884 | if (RHS.isNegative()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4885 | return replaceInstUsesWith(I, Builder.getFalse()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4886 | Pred = ICmpInst::ICMP_ULE; |
| 4887 | break; |
| 4888 | case ICmpInst::ICMP_SLT: |
| 4889 | // (float)int < -4.4 --> int < -4 |
| 4890 | // (float)int < 4.4 --> int <= 4 |
| 4891 | if (!RHS.isNegative()) |
| 4892 | Pred = ICmpInst::ICMP_SLE; |
| 4893 | break; |
| 4894 | case ICmpInst::ICMP_UGT: |
| 4895 | // (float)int > 4.4 --> int > 4 |
| 4896 | // (float)int > -4.4 --> true |
| 4897 | if (RHS.isNegative()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4898 | return replaceInstUsesWith(I, Builder.getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4899 | break; |
| 4900 | case ICmpInst::ICMP_SGT: |
| 4901 | // (float)int > 4.4 --> int > 4 |
| 4902 | // (float)int > -4.4 --> int >= -4 |
| 4903 | if (RHS.isNegative()) |
| 4904 | Pred = ICmpInst::ICMP_SGE; |
| 4905 | break; |
| 4906 | case ICmpInst::ICMP_UGE: |
| 4907 | // (float)int >= -4.4 --> true |
| 4908 | // (float)int >= 4.4 --> int > 4 |
Bob Wilson | 61f3ad5 | 2012-08-07 22:35:16 +0000 | [diff] [blame] | 4909 | if (RHS.isNegative()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 4910 | return replaceInstUsesWith(I, Builder.getTrue()); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4911 | Pred = ICmpInst::ICMP_UGT; |
| 4912 | break; |
| 4913 | case ICmpInst::ICMP_SGE: |
| 4914 | // (float)int >= -4.4 --> int >= -4 |
| 4915 | // (float)int >= 4.4 --> int > 4 |
| 4916 | if (!RHS.isNegative()) |
| 4917 | Pred = ICmpInst::ICMP_SGT; |
| 4918 | break; |
| 4919 | } |
| 4920 | } |
| 4921 | } |
| 4922 | |
| 4923 | // Lower this FP comparison into an appropriate integer version of the |
| 4924 | // comparison. |
| 4925 | return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt); |
| 4926 | } |
| 4927 | |
| 4928 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 4929 | bool Changed = false; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4930 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4931 | /// Orders the operands of the compare so that they are listed from most |
| 4932 | /// complex to least complex. This puts constants before unary operators, |
| 4933 | /// before binary operators. |
| 4934 | if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) { |
| 4935 | I.swapOperands(); |
| 4936 | Changed = true; |
| 4937 | } |
| 4938 | |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 4939 | const CmpInst::Predicate Pred = I.getPredicate(); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4940 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 4941 | if (Value *V = SimplifyFCmpInst(Pred, Op0, Op1, I.getFastMathFlags(), |
| 4942 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 4943 | return replaceInstUsesWith(I, V); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4944 | |
| 4945 | // Simplify 'fcmp pred X, X' |
| 4946 | if (Op0 == Op1) { |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 4947 | switch (Pred) { |
| 4948 | default: break; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4949 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 4950 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 4951 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 4952 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 4953 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 4954 | I.setPredicate(FCmpInst::FCMP_UNO); |
| 4955 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4956 | return &I; |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4957 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4958 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 4959 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 4960 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 4961 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 4962 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 4963 | I.setPredicate(FCmpInst::FCMP_ORD); |
| 4964 | I.setOperand(1, Constant::getNullValue(Op0->getType())); |
| 4965 | return &I; |
| 4966 | } |
| 4967 | } |
Jim Grosbach | 129c52a | 2011-09-30 18:09:53 +0000 | [diff] [blame] | 4968 | |
Sanjay Patel | 6840c5f | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 4969 | // If we're just checking for a NaN (ORD/UNO) and have a non-NaN operand, |
| 4970 | // then canonicalize the operand to 0.0. |
| 4971 | if (Pred == CmpInst::FCMP_ORD || Pred == CmpInst::FCMP_UNO) { |
| 4972 | if (!match(Op0, m_Zero()) && isKnownNeverNaN(Op0)) { |
| 4973 | I.setOperand(0, ConstantFP::getNullValue(Op0->getType())); |
| 4974 | return &I; |
| 4975 | } |
| 4976 | if (!match(Op1, m_Zero()) && isKnownNeverNaN(Op1)) { |
| 4977 | I.setOperand(1, ConstantFP::getNullValue(Op0->getType())); |
| 4978 | return &I; |
| 4979 | } |
| 4980 | } |
| 4981 | |
James Molloy | 2b21a7c | 2015-05-20 18:41:25 +0000 | [diff] [blame] | 4982 | // Test if the FCmpInst instruction is used exclusively by a select as |
| 4983 | // part of a minimum or maximum operation. If so, refrain from doing |
| 4984 | // any other folding. This helps out other analyses which understand |
| 4985 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 4986 | // and CodeGen. And in this case, at least one of the comparison |
| 4987 | // operands has at least one user besides the compare (the select), |
| 4988 | // which would often largely negate the benefit of folding anyway. |
| 4989 | if (I.hasOneUse()) |
| 4990 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin())) |
| 4991 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 4992 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
| 4993 | return nullptr; |
| 4994 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 4995 | // Handle fcmp with constant RHS |
| 4996 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 4997 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 4998 | switch (LHSI->getOpcode()) { |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 4999 | case Instruction::FPExt: { |
| 5000 | // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless |
| 5001 | FPExtInst *LHSExt = cast<FPExtInst>(LHSI); |
| 5002 | ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC); |
| 5003 | if (!RHSF) |
| 5004 | break; |
| 5005 | |
| 5006 | const fltSemantics *Sem; |
| 5007 | // FIXME: This shouldn't be here. |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 5008 | if (LHSExt->getSrcTy()->isHalfTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 5009 | Sem = &APFloat::IEEEhalf(); |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 5010 | else if (LHSExt->getSrcTy()->isFloatTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 5011 | Sem = &APFloat::IEEEsingle(); |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 5012 | else if (LHSExt->getSrcTy()->isDoubleTy()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 5013 | Sem = &APFloat::IEEEdouble(); |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 5014 | else if (LHSExt->getSrcTy()->isFP128Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 5015 | Sem = &APFloat::IEEEquad(); |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 5016 | else if (LHSExt->getSrcTy()->isX86_FP80Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 5017 | Sem = &APFloat::x87DoubleExtended(); |
Ulrich Weigand | 6a9bb51 | 2012-10-30 12:33:18 +0000 | [diff] [blame] | 5018 | else if (LHSExt->getSrcTy()->isPPC_FP128Ty()) |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 5019 | Sem = &APFloat::PPCDoubleDouble(); |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 5020 | else |
| 5021 | break; |
| 5022 | |
| 5023 | bool Lossy; |
| 5024 | APFloat F = RHSF->getValueAPF(); |
| 5025 | F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy); |
| 5026 | |
Jim Grosbach | 24ff834 | 2011-09-30 18:45:50 +0000 | [diff] [blame] | 5027 | // Avoid lossy conversions and denormals. Zero is a special case |
| 5028 | // that's OK to convert. |
Jim Grosbach | 011dafb | 2011-09-30 19:58:46 +0000 | [diff] [blame] | 5029 | APFloat Fabs = F; |
| 5030 | Fabs.clearSign(); |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 5031 | if (!Lossy && |
Jim Grosbach | 011dafb | 2011-09-30 19:58:46 +0000 | [diff] [blame] | 5032 | ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) != |
| 5033 | APFloat::cmpLessThan) || Fabs.isZero())) |
Jim Grosbach | 24ff834 | 2011-09-30 18:45:50 +0000 | [diff] [blame] | 5034 | |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 5035 | return new FCmpInst(Pred, LHSExt->getOperand(0), |
Benjamin Kramer | cbb18e9 | 2011-03-31 10:12:07 +0000 | [diff] [blame] | 5036 | ConstantFP::get(RHSC->getContext(), F)); |
| 5037 | break; |
| 5038 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 5039 | case Instruction::PHI: |
| 5040 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
| 5041 | // block. If in the same block, we're encouraging jump threading. If |
| 5042 | // not, we are just pessimizing the code by making an i1 phi. |
| 5043 | if (LHSI->getParent() == I.getParent()) |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 5044 | if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI))) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 5045 | return NV; |
| 5046 | break; |
| 5047 | case Instruction::SIToFP: |
| 5048 | case Instruction::UIToFP: |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 5049 | if (Instruction *NV = foldFCmpIntToFPConst(I, LHSI, RHSC)) |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 5050 | return NV; |
| 5051 | break; |
Benjamin Kramer | a8c5d08 | 2011-03-31 10:12:15 +0000 | [diff] [blame] | 5052 | case Instruction::FSub: { |
| 5053 | // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C |
| 5054 | Value *Op; |
| 5055 | if (match(LHSI, m_FNeg(m_Value(Op)))) |
| 5056 | return new FCmpInst(I.getSwappedPredicate(), Op, |
| 5057 | ConstantExpr::getFNeg(RHSC)); |
| 5058 | break; |
| 5059 | } |
Dan Gohman | 9473202 | 2010-02-24 06:46:09 +0000 | [diff] [blame] | 5060 | case Instruction::Load: |
| 5061 | if (GetElementPtrInst *GEP = |
| 5062 | dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) { |
| 5063 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) |
| 5064 | if (GV->isConstant() && GV->hasDefinitiveInitializer() && |
| 5065 | !cast<LoadInst>(LHSI)->isVolatile()) |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 5066 | if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I)) |
Dan Gohman | 9473202 | 2010-02-24 06:46:09 +0000 | [diff] [blame] | 5067 | return Res; |
| 5068 | } |
| 5069 | break; |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 5070 | case Instruction::Call: { |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 5071 | if (!RHSC->isNullValue()) |
| 5072 | break; |
| 5073 | |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 5074 | CallInst *CI = cast<CallInst>(LHSI); |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 5075 | Intrinsic::ID IID = getIntrinsicForCallSite(CI, &TLI); |
David Majnemer | 2e02ba7 | 2016-04-15 17:21:03 +0000 | [diff] [blame] | 5076 | if (IID != Intrinsic::fabs) |
Matt Arsenault | b935d9d | 2015-01-08 20:09:34 +0000 | [diff] [blame] | 5077 | break; |
| 5078 | |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 5079 | // Various optimization for fabs compared with zero. |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 5080 | switch (Pred) { |
David Majnemer | 2e02ba7 | 2016-04-15 17:21:03 +0000 | [diff] [blame] | 5081 | default: |
| 5082 | break; |
| 5083 | // fabs(x) < 0 --> false |
| 5084 | case FCmpInst::FCMP_OLT: |
| 5085 | llvm_unreachable("handled by SimplifyFCmpInst"); |
| 5086 | // fabs(x) > 0 --> x != 0 |
| 5087 | case FCmpInst::FCMP_OGT: |
| 5088 | return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0), RHSC); |
| 5089 | // fabs(x) <= 0 --> x == 0 |
| 5090 | case FCmpInst::FCMP_OLE: |
| 5091 | return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0), RHSC); |
| 5092 | // fabs(x) >= 0 --> !isnan(x) |
| 5093 | case FCmpInst::FCMP_OGE: |
| 5094 | return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0), RHSC); |
| 5095 | // fabs(x) == 0 --> x == 0 |
| 5096 | // fabs(x) != 0 --> x != 0 |
| 5097 | case FCmpInst::FCMP_OEQ: |
| 5098 | case FCmpInst::FCMP_UEQ: |
| 5099 | case FCmpInst::FCMP_ONE: |
| 5100 | case FCmpInst::FCMP_UNE: |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 5101 | return new FCmpInst(Pred, CI->getArgOperand(0), RHSC); |
Benjamin Kramer | 8c2a733 | 2012-08-18 20:06:47 +0000 | [diff] [blame] | 5102 | } |
| 5103 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 5104 | } |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Benjamin Kramer | be209ab | 2011-03-31 10:46:03 +0000 | [diff] [blame] | 5107 | // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y |
Benjamin Kramer | d159d94 | 2011-03-31 10:12:22 +0000 | [diff] [blame] | 5108 | Value *X, *Y; |
| 5109 | 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] | 5110 | return new FCmpInst(I.getSwappedPredicate(), X, Y); |
Benjamin Kramer | d159d94 | 2011-03-31 10:12:22 +0000 | [diff] [blame] | 5111 | |
Benjamin Kramer | 2ccfbc8 | 2011-03-31 10:11:58 +0000 | [diff] [blame] | 5112 | // fcmp (fpext x), (fpext y) -> fcmp x, y |
| 5113 | if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0)) |
| 5114 | if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1)) |
| 5115 | if (LHSExt->getSrcTy() == RHSExt->getSrcTy()) |
Sanjay Patel | 6b13946 | 2017-09-02 15:11:55 +0000 | [diff] [blame] | 5116 | return new FCmpInst(Pred, LHSExt->getOperand(0), RHSExt->getOperand(0)); |
Benjamin Kramer | 2ccfbc8 | 2011-03-31 10:11:58 +0000 | [diff] [blame] | 5117 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 5118 | return Changed ? &I : nullptr; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 5119 | } |