Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1 | //===- InstCombineMulDivRem.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 visit functions for mul, fmul, sdiv, udiv, fdiv, |
| 11 | // srem, urem, frem. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 15 | #include "InstCombineInternal.h" |
Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace PatternMatch; |
| 21 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "instcombine" |
| 23 | |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 24 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 25 | /// The specific integer value is used in a context where it is known to be |
| 26 | /// non-zero. If this allows us to simplify the computation, do so and return |
| 27 | /// the new operand, otherwise return null. |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 28 | static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 29 | Instruction &CxtI) { |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 30 | // If V has multiple uses, then we would have to do more analysis to determine |
| 31 | // if this is safe. For example, the use could be in dynamically unreached |
| 32 | // code. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 33 | if (!V->hasOneUse()) return nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 388cb8a | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 35 | bool MadeChange = false; |
| 36 | |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 37 | // ((1 << A) >>u B) --> (1 << (A-B)) |
| 38 | // Because V cannot be zero, we know that B is less than A. |
David Majnemer | dad2103 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 39 | Value *A = nullptr, *B = nullptr, *One = nullptr; |
| 40 | if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) && |
| 41 | match(One, m_One())) { |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 42 | A = IC.Builder->CreateSub(A, B); |
David Majnemer | dad2103 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 43 | return IC.Builder->CreateShl(One, A); |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 44 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 388cb8a | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 46 | // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it |
| 47 | // inexact. Similarly for <<. |
Sanjay Patel | a8ef4a5 | 2016-05-22 17:08:52 +0000 | [diff] [blame] | 48 | BinaryOperator *I = dyn_cast<BinaryOperator>(V); |
| 49 | if (I && I->isLogicalShift() && |
Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 50 | IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) { |
Sanjay Patel | a8ef4a5 | 2016-05-22 17:08:52 +0000 | [diff] [blame] | 51 | // We know that this is an exact/nuw shift and that the input is a |
| 52 | // non-zero context as well. |
| 53 | if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) { |
| 54 | I->setOperand(0, V2); |
| 55 | MadeChange = true; |
Chris Lattner | 388cb8a | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Sanjay Patel | a8ef4a5 | 2016-05-22 17:08:52 +0000 | [diff] [blame] | 58 | if (I->getOpcode() == Instruction::LShr && !I->isExact()) { |
| 59 | I->setIsExact(); |
| 60 | MadeChange = true; |
| 61 | } |
| 62 | |
| 63 | if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { |
| 64 | I->setHasNoUnsignedWrap(); |
| 65 | MadeChange = true; |
| 66 | } |
| 67 | } |
| 68 | |
Chris Lattner | 162dfc3 | 2011-05-22 18:26:48 +0000 | [diff] [blame] | 69 | // TODO: Lots more we could do here: |
Chris Lattner | 162dfc3 | 2011-05-22 18:26:48 +0000 | [diff] [blame] | 70 | // If V is a phi node, we can call this on each of its operands. |
| 71 | // "select cond, X, 0" can simplify to "X". |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 72 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 73 | return MadeChange ? V : nullptr; |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 77 | /// True if the multiply can not be expressed in an int this size. |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 78 | static bool MultiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, |
| 79 | bool IsSigned) { |
| 80 | bool Overflow; |
| 81 | if (IsSigned) |
| 82 | Product = C1.smul_ov(C2, Overflow); |
| 83 | else |
| 84 | Product = C1.umul_ov(C2, Overflow); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 85 | |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 86 | return Overflow; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 87 | } |
| 88 | |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 89 | /// \brief True if C2 is a multiple of C1. Quotient contains C2/C1. |
| 90 | static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, |
| 91 | bool IsSigned) { |
| 92 | assert(C1.getBitWidth() == C2.getBitWidth() && |
| 93 | "Inconsistent width of constants!"); |
| 94 | |
David Majnemer | 135ca40 | 2015-09-06 06:49:59 +0000 | [diff] [blame] | 95 | // Bail if we will divide by zero. |
| 96 | if (C2.isMinValue()) |
| 97 | return false; |
| 98 | |
| 99 | // Bail if we would divide INT_MIN by -1. |
| 100 | if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue()) |
| 101 | return false; |
| 102 | |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 103 | APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 104 | if (IsSigned) |
| 105 | APInt::sdivrem(C1, C2, Quotient, Remainder); |
| 106 | else |
| 107 | APInt::udivrem(C1, C2, Quotient, Remainder); |
| 108 | |
| 109 | return Remainder.isMinValue(); |
| 110 | } |
| 111 | |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 112 | /// \brief A helper routine of InstCombiner::visitMul(). |
| 113 | /// |
| 114 | /// If C is a vector of known powers of 2, then this function returns |
| 115 | /// a new vector obtained from C replacing each element with its logBase2. |
| 116 | /// Return a null pointer otherwise. |
| 117 | static Constant *getLogBase2Vector(ConstantDataVector *CV) { |
| 118 | const APInt *IVal; |
| 119 | SmallVector<Constant *, 4> Elts; |
| 120 | |
| 121 | for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) { |
| 122 | Constant *Elt = CV->getElementAsConstant(I); |
| 123 | if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 124 | return nullptr; |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 125 | Elts.push_back(ConstantInt::get(Elt->getType(), IVal->logBase2())); |
| 126 | } |
| 127 | |
| 128 | return ConstantVector::get(Elts); |
| 129 | } |
| 130 | |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 131 | /// \brief Return true if we can prove that: |
| 132 | /// (mul LHS, RHS) === (mul nsw LHS, RHS) |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 133 | bool InstCombiner::willNotOverflowSignedMul(const Value *LHS, |
| 134 | const Value *RHS, |
| 135 | const Instruction &CxtI) const { |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 136 | // Multiplying n * m significant bits yields a result of n + m significant |
| 137 | // bits. If the total number of significant bits does not exceed the |
| 138 | // result bit width (minus 1), there is no overflow. |
| 139 | // This means if we have enough leading sign bits in the operands |
| 140 | // we can guarantee that the result does not overflow. |
| 141 | // Ref: "Hacker's Delight" by Henry Warren |
| 142 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
| 143 | |
| 144 | // Note that underestimating the number of sign bits gives a more |
| 145 | // conservative answer. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 146 | unsigned SignBits = |
| 147 | ComputeNumSignBits(LHS, 0, &CxtI) + ComputeNumSignBits(RHS, 0, &CxtI); |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 148 | |
| 149 | // First handle the easy case: if we have enough sign bits there's |
| 150 | // definitely no overflow. |
| 151 | if (SignBits > BitWidth + 1) |
| 152 | return true; |
| 153 | |
| 154 | // There are two ambiguous cases where there can be no overflow: |
| 155 | // SignBits == BitWidth + 1 and |
| 156 | // SignBits == BitWidth |
| 157 | // The second case is difficult to check, therefore we only handle the |
| 158 | // first case. |
| 159 | if (SignBits == BitWidth + 1) { |
| 160 | // It overflows only when both arguments are negative and the true |
| 161 | // product is exactly the minimum negative number. |
| 162 | // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 |
| 163 | // For simplicity we just check if at least one side is not negative. |
Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 164 | KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, &CxtI); |
| 165 | KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, &CxtI); |
| 166 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 172 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 173 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 174 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 175 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 176 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 177 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 178 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 179 | if (Value *V = SimplifyMulInst(Op0, Op1, SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 180 | return replaceInstUsesWith(I, V); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 181 | |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 182 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 183 | return replaceInstUsesWith(I, V); |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 184 | |
David Majnemer | 027bc80 | 2014-11-22 04:52:38 +0000 | [diff] [blame] | 185 | // X * -1 == 0 - X |
| 186 | if (match(Op1, m_AllOnes())) { |
| 187 | BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName()); |
| 188 | if (I.hasNoSignedWrap()) |
| 189 | BO->setHasNoSignedWrap(); |
| 190 | return BO; |
| 191 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 192 | |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 193 | // Also allow combining multiply instructions on vectors. |
| 194 | { |
| 195 | Value *NewOp; |
| 196 | Constant *C1, *C2; |
| 197 | const APInt *IVal; |
| 198 | if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)), |
| 199 | m_Constant(C1))) && |
David Majnemer | fd4a6d2 | 2014-11-22 04:52:52 +0000 | [diff] [blame] | 200 | match(C1, m_APInt(IVal))) { |
| 201 | // ((X << C2)*C1) == (X * (C1 << C2)) |
| 202 | Constant *Shl = ConstantExpr::getShl(C1, C2); |
| 203 | BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0)); |
| 204 | BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl); |
| 205 | if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap()) |
| 206 | BO->setHasNoUnsignedWrap(); |
| 207 | if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() && |
| 208 | Shl->isNotMinSignedValue()) |
| 209 | BO->setHasNoSignedWrap(); |
| 210 | return BO; |
| 211 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 212 | |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 213 | if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 214 | Constant *NewCst = nullptr; |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 215 | if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2()) |
| 216 | // Replace X*(2^C) with X << C, where C is either a scalar or a splat. |
| 217 | NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2()); |
| 218 | else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1)) |
| 219 | // Replace X*(2^C) with X << C, where C is a vector of known |
| 220 | // constant powers of 2. |
| 221 | NewCst = getLogBase2Vector(CV); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 222 | |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 223 | if (NewCst) { |
David Majnemer | 45951a6 | 2015-04-18 04:41:30 +0000 | [diff] [blame] | 224 | unsigned Width = NewCst->getType()->getPrimitiveSizeInBits(); |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 225 | BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst); |
Tilmann Scheller | 2bc5cb6 | 2014-10-07 10:19:34 +0000 | [diff] [blame] | 226 | |
Tilmann Scheller | 2bc5cb6 | 2014-10-07 10:19:34 +0000 | [diff] [blame] | 227 | if (I.hasNoUnsignedWrap()) |
| 228 | Shl->setHasNoUnsignedWrap(); |
David Majnemer | 45951a6 | 2015-04-18 04:41:30 +0000 | [diff] [blame] | 229 | if (I.hasNoSignedWrap()) { |
| 230 | uint64_t V; |
| 231 | if (match(NewCst, m_ConstantInt(V)) && V != Width - 1) |
| 232 | Shl->setHasNoSignedWrap(); |
| 233 | } |
Tilmann Scheller | 2bc5cb6 | 2014-10-07 10:19:34 +0000 | [diff] [blame] | 234 | |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 235 | return Shl; |
| 236 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 237 | } |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 238 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 239 | |
Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 240 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 241 | // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n |
| 242 | // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n |
| 243 | // The "* (2**n)" thus becomes a potential shifting opportunity. |
Stuart Hastings | 8284374 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 244 | { |
| 245 | const APInt & Val = CI->getValue(); |
| 246 | const APInt &PosVal = Val.abs(); |
| 247 | if (Val.isNegative() && PosVal.isPowerOf2()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 248 | Value *X = nullptr, *Y = nullptr; |
Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 249 | if (Op0->hasOneUse()) { |
| 250 | ConstantInt *C1; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 251 | Value *Sub = nullptr; |
Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 252 | if (match(Op0, m_Sub(m_Value(Y), m_Value(X)))) |
| 253 | Sub = Builder->CreateSub(X, Y, "suba"); |
| 254 | else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1)))) |
| 255 | Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc"); |
| 256 | if (Sub) |
| 257 | return |
| 258 | BinaryOperator::CreateMul(Sub, |
| 259 | ConstantInt::get(Y->getType(), PosVal)); |
Stuart Hastings | 8284374 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 263 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 265 | // Simplify mul instructions with a constant RHS. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 266 | if (isa<Constant>(Op1)) { |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 267 | if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I)) |
| 268 | return FoldedMul; |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 269 | |
| 270 | // Canonicalize (X+C1)*CI -> X*CI+C1*CI. |
| 271 | { |
| 272 | Value *X; |
| 273 | Constant *C1; |
| 274 | if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { |
David Majnemer | 6cf6c05 | 2014-06-19 07:14:33 +0000 | [diff] [blame] | 275 | Value *Mul = Builder->CreateMul(C1, Op1); |
| 276 | // Only go forward with the transform if C1*CI simplifies to a tidier |
| 277 | // constant. |
| 278 | if (!match(Mul, m_Mul(m_Value(), m_Value()))) |
| 279 | return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul); |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 282 | } |
| 283 | |
David Majnemer | 8279a750 | 2014-11-22 07:25:19 +0000 | [diff] [blame] | 284 | if (Value *Op0v = dyn_castNegVal(Op0)) { // -X * -Y = X*Y |
| 285 | if (Value *Op1v = dyn_castNegVal(Op1)) { |
| 286 | BinaryOperator *BO = BinaryOperator::CreateMul(Op0v, Op1v); |
| 287 | if (I.hasNoSignedWrap() && |
| 288 | match(Op0, m_NSWSub(m_Value(), m_Value())) && |
| 289 | match(Op1, m_NSWSub(m_Value(), m_Value()))) |
| 290 | BO->setHasNoSignedWrap(); |
| 291 | return BO; |
| 292 | } |
| 293 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 294 | |
| 295 | // (X / Y) * Y = X - (X % Y) |
| 296 | // (X / Y) * -Y = (X % Y) - X |
| 297 | { |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 298 | Value *Y = Op1; |
| 299 | BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0); |
| 300 | if (!Div || (Div->getOpcode() != Instruction::UDiv && |
| 301 | Div->getOpcode() != Instruction::SDiv)) { |
| 302 | Y = Op0; |
| 303 | Div = dyn_cast<BinaryOperator>(Op1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 304 | } |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 305 | Value *Neg = dyn_castNegVal(Y); |
| 306 | if (Div && Div->hasOneUse() && |
| 307 | (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) && |
| 308 | (Div->getOpcode() == Instruction::UDiv || |
| 309 | Div->getOpcode() == Instruction::SDiv)) { |
| 310 | Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 312 | // If the division is exact, X % Y is zero, so we end up with X or -X. |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 313 | if (Div->isExact()) { |
| 314 | if (DivOp1 == Y) |
| 315 | return replaceInstUsesWith(I, X); |
| 316 | return BinaryOperator::CreateNeg(X); |
| 317 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 318 | |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 319 | auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem |
| 320 | : Instruction::SRem; |
| 321 | Value *Rem = Builder->CreateBinOp(RemOpc, X, DivOp1); |
| 322 | if (DivOp1 == Y) |
| 323 | return BinaryOperator::CreateSub(X, Rem); |
| 324 | return BinaryOperator::CreateSub(Rem, X); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | /// i1 mul -> i1 and. |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 329 | if (I.getType()->getScalarType()->isIntegerTy(1)) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 330 | return BinaryOperator::CreateAnd(Op0, Op1); |
| 331 | |
| 332 | // X*(1 << Y) --> X << Y |
| 333 | // (1 << Y)*X --> X << Y |
| 334 | { |
| 335 | Value *Y; |
David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 336 | BinaryOperator *BO = nullptr; |
| 337 | bool ShlNSW = false; |
| 338 | if (match(Op0, m_Shl(m_One(), m_Value(Y)))) { |
| 339 | BO = BinaryOperator::CreateShl(Op1, Y); |
David Majnemer | 087dc8b | 2015-01-04 07:36:02 +0000 | [diff] [blame] | 340 | ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap(); |
David Majnemer | 8e6f6a9 | 2014-11-24 16:41:13 +0000 | [diff] [blame] | 341 | } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) { |
David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 342 | BO = BinaryOperator::CreateShl(Op0, Y); |
David Majnemer | 087dc8b | 2015-01-04 07:36:02 +0000 | [diff] [blame] | 343 | ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap(); |
David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 344 | } |
| 345 | if (BO) { |
| 346 | if (I.hasNoUnsignedWrap()) |
| 347 | BO->setHasNoUnsignedWrap(); |
| 348 | if (I.hasNoSignedWrap() && ShlNSW) |
| 349 | BO->setHasNoSignedWrap(); |
| 350 | return BO; |
| 351 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 352 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 353 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 354 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 355 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 356 | // X * Y (where Y is 0 or 1) -> X & (0-Y) |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 357 | if (!I.getType()->isVectorTy()) { |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 358 | // -2 is "-1 << 1" so it is all bits set except the low one. |
| 359 | APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 360 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 361 | Value *BoolCast = nullptr, *OtherOp = nullptr; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 362 | if (MaskedValueIsZero(Op0, Negative2, 0, &I)) { |
| 363 | BoolCast = Op0; |
| 364 | OtherOp = Op1; |
| 365 | } else if (MaskedValueIsZero(Op1, Negative2, 0, &I)) { |
| 366 | BoolCast = Op1; |
| 367 | OtherOp = Op0; |
| 368 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 369 | |
| 370 | if (BoolCast) { |
| 371 | Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()), |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 372 | BoolCast); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 373 | return BinaryOperator::CreateAnd(V, OtherOp); |
| 374 | } |
| 375 | } |
| 376 | |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 377 | // Check for (mul (sext x), y), see if we can merge this into an |
| 378 | // integer mul followed by a sext. |
| 379 | if (SExtInst *Op0Conv = dyn_cast<SExtInst>(Op0)) { |
| 380 | // (mul (sext x), cst) --> (sext (mul x, cst')) |
| 381 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 382 | if (Op0Conv->hasOneUse()) { |
| 383 | Constant *CI = |
| 384 | ConstantExpr::getTrunc(Op1C, Op0Conv->getOperand(0)->getType()); |
| 385 | if (ConstantExpr::getSExt(CI, I.getType()) == Op1C && |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 386 | willNotOverflowSignedMul(Op0Conv->getOperand(0), CI, I)) { |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 387 | // Insert the new, smaller mul. |
| 388 | Value *NewMul = |
| 389 | Builder->CreateNSWMul(Op0Conv->getOperand(0), CI, "mulconv"); |
| 390 | return new SExtInst(NewMul, I.getType()); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // (mul (sext x), (sext y)) --> (sext (mul int x, y)) |
| 396 | if (SExtInst *Op1Conv = dyn_cast<SExtInst>(Op1)) { |
| 397 | // Only do this if x/y have the same type, if at last one of them has a |
| 398 | // single use (so we don't increase the number of sexts), and if the |
| 399 | // integer mul will not overflow. |
| 400 | if (Op0Conv->getOperand(0)->getType() == |
| 401 | Op1Conv->getOperand(0)->getType() && |
| 402 | (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) && |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 403 | willNotOverflowSignedMul(Op0Conv->getOperand(0), |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 404 | Op1Conv->getOperand(0), I)) { |
| 405 | // Insert the new integer mul. |
| 406 | Value *NewMul = Builder->CreateNSWMul( |
| 407 | Op0Conv->getOperand(0), Op1Conv->getOperand(0), "mulconv"); |
| 408 | return new SExtInst(NewMul, I.getType()); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Check for (mul (zext x), y), see if we can merge this into an |
| 414 | // integer mul followed by a zext. |
| 415 | if (auto *Op0Conv = dyn_cast<ZExtInst>(Op0)) { |
| 416 | // (mul (zext x), cst) --> (zext (mul x, cst')) |
| 417 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 418 | if (Op0Conv->hasOneUse()) { |
| 419 | Constant *CI = |
| 420 | ConstantExpr::getTrunc(Op1C, Op0Conv->getOperand(0)->getType()); |
| 421 | if (ConstantExpr::getZExt(CI, I.getType()) == Op1C && |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 422 | willNotOverflowUnsignedMul(Op0Conv->getOperand(0), CI, I)) { |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 423 | // Insert the new, smaller mul. |
| 424 | Value *NewMul = |
| 425 | Builder->CreateNUWMul(Op0Conv->getOperand(0), CI, "mulconv"); |
| 426 | return new ZExtInst(NewMul, I.getType()); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // (mul (zext x), (zext y)) --> (zext (mul int x, y)) |
| 432 | if (auto *Op1Conv = dyn_cast<ZExtInst>(Op1)) { |
| 433 | // Only do this if x/y have the same type, if at last one of them has a |
| 434 | // single use (so we don't increase the number of zexts), and if the |
| 435 | // integer mul will not overflow. |
| 436 | if (Op0Conv->getOperand(0)->getType() == |
| 437 | Op1Conv->getOperand(0)->getType() && |
| 438 | (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) && |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 439 | willNotOverflowUnsignedMul(Op0Conv->getOperand(0), |
| 440 | Op1Conv->getOperand(0), I)) { |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 441 | // Insert the new integer mul. |
| 442 | Value *NewMul = Builder->CreateNUWMul( |
| 443 | Op0Conv->getOperand(0), Op1Conv->getOperand(0), "mulconv"); |
| 444 | return new ZExtInst(NewMul, I.getType()); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 449 | if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) { |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 450 | Changed = true; |
| 451 | I.setHasNoSignedWrap(true); |
| 452 | } |
| 453 | |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 454 | if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) { |
David Majnemer | b1296ec | 2014-12-26 09:50:35 +0000 | [diff] [blame] | 455 | Changed = true; |
| 456 | I.setHasNoUnsignedWrap(true); |
| 457 | } |
| 458 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 459 | return Changed ? &I : nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 462 | /// Detect pattern log2(Y * 0.5) with corresponding fast math flags. |
Pedro Artigas | 993acd0 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 463 | static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) { |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 464 | if (!Op->hasOneUse()) |
| 465 | return; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 466 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 467 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op); |
| 468 | if (!II) |
| 469 | return; |
| 470 | if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra()) |
| 471 | return; |
| 472 | Log2 = II; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 473 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 474 | Value *OpLog2Of = II->getArgOperand(0); |
| 475 | if (!OpLog2Of->hasOneUse()) |
| 476 | return; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 477 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 478 | Instruction *I = dyn_cast<Instruction>(OpLog2Of); |
| 479 | if (!I) |
| 480 | return; |
| 481 | if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra()) |
| 482 | return; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 483 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 484 | if (match(I->getOperand(0), m_SpecificFP(0.5))) |
| 485 | Y = I->getOperand(1); |
| 486 | else if (match(I->getOperand(1), m_SpecificFP(0.5))) |
| 487 | Y = I->getOperand(0); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 488 | } |
Pedro Artigas | 993acd0 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 489 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 490 | static bool isFiniteNonZeroFp(Constant *C) { |
| 491 | if (C->getType()->isVectorTy()) { |
| 492 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; |
| 493 | ++I) { |
Michael Kuperstein | bcb26d6 | 2015-03-05 08:38:57 +0000 | [diff] [blame] | 494 | ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I)); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 495 | if (!CFP || !CFP->getValueAPF().isFiniteNonZero()) |
| 496 | return false; |
| 497 | } |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | return isa<ConstantFP>(C) && |
| 502 | cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero(); |
| 503 | } |
| 504 | |
| 505 | static bool isNormalFp(Constant *C) { |
| 506 | if (C->getType()->isVectorTy()) { |
| 507 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; |
| 508 | ++I) { |
Michael Kuperstein | bcb26d6 | 2015-03-05 08:38:57 +0000 | [diff] [blame] | 509 | ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I)); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 510 | if (!CFP || !CFP->getValueAPF().isNormal()) |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal(); |
| 517 | } |
| 518 | |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 519 | /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns |
| 520 | /// true iff the given value is FMul or FDiv with one and only one operand |
| 521 | /// being a normal constant (i.e. not Zero/NaN/Infinity). |
| 522 | static bool isFMulOrFDivWithConstant(Value *V) { |
| 523 | Instruction *I = dyn_cast<Instruction>(V); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 524 | if (!I || (I->getOpcode() != Instruction::FMul && |
Shuxin Yang | 8013866 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 525 | I->getOpcode() != Instruction::FDiv)) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 526 | return false; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 527 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 528 | Constant *C0 = dyn_cast<Constant>(I->getOperand(0)); |
| 529 | Constant *C1 = dyn_cast<Constant>(I->getOperand(1)); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 530 | |
| 531 | if (C0 && C1) |
| 532 | return false; |
| 533 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 534 | return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1)); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /// foldFMulConst() is a helper routine of InstCombiner::visitFMul(). |
| 538 | /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand |
| 539 | /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true). |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 540 | /// This function is to simplify "FMulOrDiv * C" and returns the |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 541 | /// resulting expression. Note that this function could return NULL in |
| 542 | /// case the constants cannot be folded into a normal floating-point. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 543 | /// |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 544 | Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C, |
Shuxin Yang | 8013866 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 545 | Instruction *InsertBefore) { |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 546 | assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid"); |
| 547 | |
| 548 | Value *Opnd0 = FMulOrDiv->getOperand(0); |
| 549 | Value *Opnd1 = FMulOrDiv->getOperand(1); |
| 550 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 551 | Constant *C0 = dyn_cast<Constant>(Opnd0); |
| 552 | Constant *C1 = dyn_cast<Constant>(Opnd1); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 553 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 554 | BinaryOperator *R = nullptr; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 555 | |
| 556 | // (X * C0) * C => X * (C0*C) |
| 557 | if (FMulOrDiv->getOpcode() == Instruction::FMul) { |
| 558 | Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 559 | if (isNormalFp(F)) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 560 | R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F); |
| 561 | } else { |
| 562 | if (C0) { |
| 563 | // (C0 / X) * C => (C0 * C) / X |
Shuxin Yang | 3a7ca6e | 2013-09-19 21:13:46 +0000 | [diff] [blame] | 564 | if (FMulOrDiv->hasOneUse()) { |
| 565 | // It would otherwise introduce another div. |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 566 | Constant *F = ConstantExpr::getFMul(C0, C); |
Shuxin Yang | 3a7ca6e | 2013-09-19 21:13:46 +0000 | [diff] [blame] | 567 | if (isNormalFp(F)) |
| 568 | R = BinaryOperator::CreateFDiv(F, Opnd1); |
| 569 | } |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 570 | } else { |
| 571 | // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 572 | Constant *F = ConstantExpr::getFDiv(C, C1); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 573 | if (isNormalFp(F)) { |
| 574 | R = BinaryOperator::CreateFMul(Opnd0, F); |
| 575 | } else { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 576 | // (X / C1) * C => X / (C1/C) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 577 | Constant *F = ConstantExpr::getFDiv(C1, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 578 | if (isNormalFp(F)) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 579 | R = BinaryOperator::CreateFDiv(Opnd0, F); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | if (R) { |
| 585 | R->setHasUnsafeAlgebra(true); |
| 586 | InsertNewInstWith(R, *InsertBefore); |
| 587 | } |
| 588 | |
| 589 | return R; |
| 590 | } |
| 591 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 592 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 593 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 594 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 595 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 596 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 597 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 598 | |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 599 | if (isa<Constant>(Op0)) |
| 600 | std::swap(Op0, Op1); |
| 601 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 602 | if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 603 | return replaceInstUsesWith(I, V); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 604 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 605 | bool AllowReassociate = I.hasUnsafeAlgebra(); |
| 606 | |
Michael Ilseman | d5787be | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 607 | // Simplify mul instructions with a constant RHS. |
| 608 | if (isa<Constant>(Op1)) { |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 609 | if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I)) |
| 610 | return FoldedMul; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 611 | |
Owen Anderson | f74cfe0 | 2014-01-16 20:36:42 +0000 | [diff] [blame] | 612 | // (fmul X, -1.0) --> (fsub -0.0, X) |
Benjamin Kramer | fea9ac9 | 2014-01-18 16:43:14 +0000 | [diff] [blame] | 613 | if (match(Op1, m_SpecificFP(-1.0))) { |
| 614 | Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType()); |
| 615 | Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0); |
Owen Anderson | f74cfe0 | 2014-01-16 20:36:42 +0000 | [diff] [blame] | 616 | RI->copyFastMathFlags(&I); |
| 617 | return RI; |
| 618 | } |
| 619 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 620 | Constant *C = cast<Constant>(Op1); |
| 621 | if (AllowReassociate && isFiniteNonZeroFp(C)) { |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 622 | // Let MDC denote an expression in one of these forms: |
| 623 | // X * C, C/X, X/C, where C is a constant. |
| 624 | // |
| 625 | // Try to simplify "MDC * Constant" |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 626 | if (isFMulOrFDivWithConstant(Op0)) |
| 627 | if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 628 | return replaceInstUsesWith(I, V); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 629 | |
Quentin Colombet | e684a6d | 2013-02-28 21:12:40 +0000 | [diff] [blame] | 630 | // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 631 | Instruction *FAddSub = dyn_cast<Instruction>(Op0); |
| 632 | if (FAddSub && |
| 633 | (FAddSub->getOpcode() == Instruction::FAdd || |
| 634 | FAddSub->getOpcode() == Instruction::FSub)) { |
| 635 | Value *Opnd0 = FAddSub->getOperand(0); |
| 636 | Value *Opnd1 = FAddSub->getOperand(1); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 637 | Constant *C0 = dyn_cast<Constant>(Opnd0); |
| 638 | Constant *C1 = dyn_cast<Constant>(Opnd1); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 639 | bool Swap = false; |
| 640 | if (C0) { |
Shuxin Yang | 8013866 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 641 | std::swap(C0, C1); |
| 642 | std::swap(Opnd0, Opnd1); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 643 | Swap = true; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 646 | if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) { |
Quentin Colombet | e684a6d | 2013-02-28 21:12:40 +0000 | [diff] [blame] | 647 | Value *M1 = ConstantExpr::getFMul(C1, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 648 | Value *M0 = isNormalFp(cast<Constant>(M1)) ? |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 649 | foldFMulConst(cast<Instruction>(Opnd0), C, &I) : |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 650 | nullptr; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 651 | if (M0 && M1) { |
| 652 | if (Swap && FAddSub->getOpcode() == Instruction::FSub) |
| 653 | std::swap(M0, M1); |
| 654 | |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 655 | Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd) |
| 656 | ? BinaryOperator::CreateFAdd(M0, M1) |
| 657 | : BinaryOperator::CreateFSub(M0, M1); |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 658 | RI->copyFastMathFlags(&I); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 659 | return RI; |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Matt Arsenault | 56c079f | 2016-01-30 05:02:00 +0000 | [diff] [blame] | 666 | if (Op0 == Op1) { |
| 667 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) { |
| 668 | // sqrt(X) * sqrt(X) -> X |
| 669 | if (AllowReassociate && II->getIntrinsicID() == Intrinsic::sqrt) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 670 | return replaceInstUsesWith(I, II->getOperand(0)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 671 | |
Matt Arsenault | 56c079f | 2016-01-30 05:02:00 +0000 | [diff] [blame] | 672 | // fabs(X) * fabs(X) -> X * X |
| 673 | if (II->getIntrinsicID() == Intrinsic::fabs) { |
| 674 | Instruction *FMulVal = BinaryOperator::CreateFMul(II->getOperand(0), |
| 675 | II->getOperand(0), |
| 676 | I.getName()); |
| 677 | FMulVal->copyFastMathFlags(&I); |
| 678 | return FMulVal; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 683 | // Under unsafe algebra do: |
| 684 | // X * log2(0.5*Y) = X*log2(Y) - X |
Sanjay Patel | b41d461 | 2014-10-02 15:20:45 +0000 | [diff] [blame] | 685 | if (AllowReassociate) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 686 | Value *OpX = nullptr; |
| 687 | Value *OpY = nullptr; |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 688 | IntrinsicInst *Log2; |
Pedro Artigas | 993acd0 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 689 | detectLog2OfHalf(Op0, OpY, Log2); |
| 690 | if (OpY) { |
| 691 | OpX = Op1; |
| 692 | } else { |
| 693 | detectLog2OfHalf(Op1, OpY, Log2); |
| 694 | if (OpY) { |
| 695 | OpX = Op0; |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | // if pattern detected emit alternate sequence |
| 699 | if (OpX && OpY) { |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 700 | BuilderTy::FastMathFlagGuard Guard(*Builder); |
Sanjay Patel | a252815 | 2016-01-12 18:03:37 +0000 | [diff] [blame] | 701 | Builder->setFastMathFlags(Log2->getFastMathFlags()); |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 702 | Log2->setArgOperand(0, OpY); |
| 703 | Value *FMulVal = Builder->CreateFMul(OpX, Log2); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 704 | Value *FSub = Builder->CreateFSub(FMulVal, OpX); |
| 705 | FSub->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 706 | return replaceInstUsesWith(I, FSub); |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 710 | // Handle symmetric situation in a 2-iteration loop |
| 711 | Value *Opnd0 = Op0; |
| 712 | Value *Opnd1 = Op1; |
| 713 | for (int i = 0; i < 2; i++) { |
| 714 | bool IgnoreZeroSign = I.hasNoSignedZeros(); |
| 715 | if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) { |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 716 | BuilderTy::FastMathFlagGuard Guard(*Builder); |
Sanjay Patel | a252815 | 2016-01-12 18:03:37 +0000 | [diff] [blame] | 717 | Builder->setFastMathFlags(I.getFastMathFlags()); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 718 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 719 | Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign); |
| 720 | Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign); |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 721 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 722 | // -X * -Y => X*Y |
Owen Anderson | e8537fc | 2014-01-16 20:59:41 +0000 | [diff] [blame] | 723 | if (N1) { |
| 724 | Value *FMul = Builder->CreateFMul(N0, N1); |
| 725 | FMul->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 726 | return replaceInstUsesWith(I, FMul); |
Owen Anderson | e8537fc | 2014-01-16 20:59:41 +0000 | [diff] [blame] | 727 | } |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 728 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 729 | if (Opnd0->hasOneUse()) { |
| 730 | // -X * Y => -(X*Y) (Promote negation as high as possible) |
| 731 | Value *T = Builder->CreateFMul(N0, Opnd1); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 732 | Value *Neg = Builder->CreateFNeg(T); |
| 733 | Neg->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 734 | return replaceInstUsesWith(I, Neg); |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 737 | |
| 738 | // (X*Y) * X => (X*X) * Y where Y != X |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 739 | // The purpose is two-fold: |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 740 | // 1) to form a power expression (of X). |
| 741 | // 2) potentially shorten the critical path: After transformation, the |
| 742 | // latency of the instruction Y is amortized by the expression of X*X, |
| 743 | // and therefore Y is in a "less critical" position compared to what it |
| 744 | // was before the transformation. |
| 745 | // |
| 746 | if (AllowReassociate) { |
| 747 | Value *Opnd0_0, *Opnd0_1; |
| 748 | if (Opnd0->hasOneUse() && |
| 749 | match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 750 | Value *Y = nullptr; |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 751 | if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1) |
| 752 | Y = Opnd0_1; |
| 753 | else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1) |
| 754 | Y = Opnd0_0; |
| 755 | |
| 756 | if (Y) { |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 757 | BuilderTy::FastMathFlagGuard Guard(*Builder); |
Sanjay Patel | a252815 | 2016-01-12 18:03:37 +0000 | [diff] [blame] | 758 | Builder->setFastMathFlags(I.getFastMathFlags()); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 759 | Value *T = Builder->CreateFMul(Opnd1, Opnd1); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 760 | Value *R = Builder->CreateFMul(T, Y); |
| 761 | R->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 762 | return replaceInstUsesWith(I, R); |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | if (!isa<Constant>(Op1)) |
| 768 | std::swap(Opnd0, Opnd1); |
| 769 | else |
| 770 | break; |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 773 | return Changed ? &I : nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 776 | /// Try to fold a divide or remainder of a select instruction. |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 777 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { |
| 778 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 779 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 780 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 781 | int NonNullOperand = -1; |
| 782 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 783 | if (ST->isNullValue()) |
| 784 | NonNullOperand = 2; |
| 785 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 786 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 787 | if (ST->isNullValue()) |
| 788 | NonNullOperand = 1; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 789 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 790 | if (NonNullOperand == -1) |
| 791 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 792 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 793 | Value *SelectCond = SI->getOperand(0); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 794 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 795 | // Change the div/rem to use 'Y' instead of the select. |
| 796 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 797 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 798 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 799 | // problem. However, the select, or the condition of the select may have |
| 800 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 801 | // propagate the known value for the select into other uses of it, and |
| 802 | // propagate a known value of the condition into its other users. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 803 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 804 | // If the select and condition only have a single use, don't bother with this, |
| 805 | // early exit. |
| 806 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 807 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 808 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 809 | // Scan the current block backward, looking for other uses of SI. |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 810 | BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 811 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 812 | while (BBI != BBFront) { |
| 813 | --BBI; |
| 814 | // If we found a call to a function, we can't assume it will return, so |
| 815 | // information from below it cannot be propagated above it. |
| 816 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 817 | break; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 818 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 819 | // Replace uses of the select or its condition with the known values. |
| 820 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 821 | I != E; ++I) { |
| 822 | if (*I == SI) { |
| 823 | *I = SI->getOperand(NonNullOperand); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 824 | Worklist.Add(&*BBI); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 825 | } else if (*I == SelectCond) { |
Jakub Staszak | 96ff4d6 | 2013-06-06 23:34:59 +0000 | [diff] [blame] | 826 | *I = Builder->getInt1(NonNullOperand == 1); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 827 | Worklist.Add(&*BBI); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 830 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 831 | // If we past the instruction, quit looking for it. |
| 832 | if (&*BBI == SI) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 833 | SI = nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 834 | if (&*BBI == SelectCond) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 835 | SelectCond = nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 836 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 837 | // If we ran out of things to eliminate, break out of the loop. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 838 | if (!SelectCond && !SI) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 839 | break; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 840 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 841 | } |
| 842 | return true; |
| 843 | } |
| 844 | |
| 845 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 846 | /// This function implements the transforms common to both integer division |
| 847 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 848 | /// division instructions. |
| 849 | /// @brief Common integer divide transforms |
| 850 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 851 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 852 | |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 853 | // The RHS is known non-zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 854 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 855 | I.setOperand(1, V); |
| 856 | return &I; |
| 857 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 858 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 859 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 860 | // This does not apply for fdiv. |
| 861 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 862 | return &I; |
| 863 | |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 864 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) { |
| 865 | const APInt *C2; |
| 866 | if (match(Op1, m_APInt(C2))) { |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 867 | Value *X; |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 868 | const APInt *C1; |
| 869 | bool IsSigned = I.getOpcode() == Instruction::SDiv; |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 870 | |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 871 | // (X / C1) / C2 -> X / (C1*C2) |
| 872 | if ((IsSigned && match(LHS, m_SDiv(m_Value(X), m_APInt(C1)))) || |
| 873 | (!IsSigned && match(LHS, m_UDiv(m_Value(X), m_APInt(C1))))) { |
| 874 | APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 875 | if (!MultiplyOverflows(*C1, *C2, Product, IsSigned)) |
| 876 | return BinaryOperator::Create(I.getOpcode(), X, |
| 877 | ConstantInt::get(I.getType(), Product)); |
| 878 | } |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 879 | |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 880 | if ((IsSigned && match(LHS, m_NSWMul(m_Value(X), m_APInt(C1)))) || |
| 881 | (!IsSigned && match(LHS, m_NUWMul(m_Value(X), m_APInt(C1))))) { |
| 882 | APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 883 | |
| 884 | // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1. |
| 885 | if (IsMultiple(*C2, *C1, Quotient, IsSigned)) { |
| 886 | BinaryOperator *BO = BinaryOperator::Create( |
| 887 | I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient)); |
| 888 | BO->setIsExact(I.isExact()); |
| 889 | return BO; |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 890 | } |
| 891 | |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 892 | // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2. |
| 893 | if (IsMultiple(*C1, *C2, Quotient, IsSigned)) { |
| 894 | BinaryOperator *BO = BinaryOperator::Create( |
| 895 | Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient)); |
| 896 | BO->setHasNoUnsignedWrap( |
| 897 | !IsSigned && |
| 898 | cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap()); |
| 899 | BO->setHasNoSignedWrap( |
| 900 | cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap()); |
| 901 | return BO; |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 904 | |
David Majnemer | 27adb12 | 2014-10-12 08:34:24 +0000 | [diff] [blame] | 905 | if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1))) && |
| 906 | *C1 != C1->getBitWidth() - 1) || |
| 907 | (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) { |
| 908 | APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 909 | APInt C1Shifted = APInt::getOneBitSet( |
| 910 | C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue())); |
| 911 | |
| 912 | // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1. |
| 913 | if (IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) { |
| 914 | BinaryOperator *BO = BinaryOperator::Create( |
| 915 | I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient)); |
| 916 | BO->setIsExact(I.isExact()); |
| 917 | return BO; |
| 918 | } |
| 919 | |
| 920 | // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2. |
| 921 | if (IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) { |
| 922 | BinaryOperator *BO = BinaryOperator::Create( |
| 923 | Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient)); |
| 924 | BO->setHasNoUnsignedWrap( |
| 925 | !IsSigned && |
| 926 | cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap()); |
| 927 | BO->setHasNoSignedWrap( |
| 928 | cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap()); |
| 929 | return BO; |
| 930 | } |
| 931 | } |
| 932 | |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 933 | if (*C2 != 0) // avoid X udiv 0 |
| 934 | if (Instruction *FoldedDiv = foldOpWithConstantIntoOperand(I)) |
| 935 | return FoldedDiv; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 939 | if (match(Op0, m_One())) { |
| 940 | assert(!I.getType()->getScalarType()->isIntegerTy(1) && |
| 941 | "i1 divide not removed?"); |
| 942 | if (I.getOpcode() == Instruction::SDiv) { |
| 943 | // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the |
| 944 | // result is one, if Op1 is -1 then the result is minus one, otherwise |
| 945 | // it's zero. |
| 946 | Value *Inc = Builder->CreateAdd(Op1, Op0); |
| 947 | Value *Cmp = Builder->CreateICmpULT( |
| 948 | Inc, ConstantInt::get(I.getType(), 3)); |
| 949 | return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0)); |
| 950 | } else { |
| 951 | // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the |
| 952 | // result is one, otherwise it's zero. |
| 953 | return new ZExtInst(Builder->CreateICmpEQ(Op1, Op0), I.getType()); |
Nick Lewycky | f0cf8fa | 2014-05-14 03:03:05 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Benjamin Kramer | 57b3df5 | 2011-04-30 18:16:00 +0000 | [diff] [blame] | 957 | // See if we can fold away this div instruction. |
| 958 | if (SimplifyDemandedInstructionBits(I)) |
| 959 | return &I; |
| 960 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 961 | // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 962 | Value *X = nullptr, *Z = nullptr; |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 963 | if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1 |
| 964 | bool isSigned = I.getOpcode() == Instruction::SDiv; |
| 965 | if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || |
| 966 | (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) |
| 967 | return BinaryOperator::Create(I.getOpcode(), X, Op1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 970 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 973 | /// dyn_castZExtVal - Checks if V is a zext or constant that can |
| 974 | /// be truncated to Ty without losing bits. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 975 | static Value *dyn_castZExtVal(Value *V, Type *Ty) { |
Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 976 | if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) { |
| 977 | if (Z->getSrcTy() == Ty) |
| 978 | return Z->getOperand(0); |
| 979 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) { |
| 980 | if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth()) |
| 981 | return ConstantExpr::getTrunc(C, Ty); |
| 982 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 983 | return nullptr; |
Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 984 | } |
| 985 | |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 986 | namespace { |
| 987 | const unsigned MaxDepth = 6; |
| 988 | typedef Instruction *(*FoldUDivOperandCb)(Value *Op0, Value *Op1, |
| 989 | const BinaryOperator &I, |
| 990 | InstCombiner &IC); |
| 991 | |
| 992 | /// \brief Used to maintain state for visitUDivOperand(). |
| 993 | struct UDivFoldAction { |
| 994 | FoldUDivOperandCb FoldAction; ///< Informs visitUDiv() how to fold this |
| 995 | ///< operand. This can be zero if this action |
| 996 | ///< joins two actions together. |
| 997 | |
| 998 | Value *OperandToFold; ///< Which operand to fold. |
| 999 | union { |
| 1000 | Instruction *FoldResult; ///< The instruction returned when FoldAction is |
| 1001 | ///< invoked. |
| 1002 | |
| 1003 | size_t SelectLHSIdx; ///< Stores the LHS action index if this action |
| 1004 | ///< joins two actions together. |
| 1005 | }; |
| 1006 | |
| 1007 | UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1008 | : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {} |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1009 | UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS) |
| 1010 | : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {} |
| 1011 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 1012 | } |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1013 | |
| 1014 | // X udiv 2^C -> X >> C |
| 1015 | static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1, |
| 1016 | const BinaryOperator &I, InstCombiner &IC) { |
| 1017 | const APInt &C = cast<Constant>(Op1)->getUniqueInteger(); |
| 1018 | BinaryOperator *LShr = BinaryOperator::CreateLShr( |
| 1019 | Op0, ConstantInt::get(Op0->getType(), C.logBase2())); |
Suyog Sarda | 65f5ae9 | 2014-10-07 12:04:07 +0000 | [diff] [blame] | 1020 | if (I.isExact()) |
| 1021 | LShr->setIsExact(); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1022 | return LShr; |
| 1023 | } |
| 1024 | |
| 1025 | // X udiv C, where C >= signbit |
| 1026 | static Instruction *foldUDivNegCst(Value *Op0, Value *Op1, |
| 1027 | const BinaryOperator &I, InstCombiner &IC) { |
| 1028 | Value *ICI = IC.Builder->CreateICmpULT(Op0, cast<ConstantInt>(Op1)); |
| 1029 | |
| 1030 | return SelectInst::Create(ICI, Constant::getNullValue(I.getType()), |
| 1031 | ConstantInt::get(I.getType(), 1)); |
| 1032 | } |
| 1033 | |
| 1034 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1035 | // X udiv (zext (C1 << N)), where C1 is "1<<C2" --> X >> (N+C2) |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1036 | static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I, |
| 1037 | InstCombiner &IC) { |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1038 | Value *ShiftLeft; |
| 1039 | if (!match(Op1, m_ZExt(m_Value(ShiftLeft)))) |
| 1040 | ShiftLeft = Op1; |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1041 | |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1042 | const APInt *CI; |
| 1043 | Value *N; |
| 1044 | if (!match(ShiftLeft, m_Shl(m_APInt(CI), m_Value(N)))) |
| 1045 | llvm_unreachable("match should never fail here!"); |
| 1046 | if (*CI != 1) |
| 1047 | N = IC.Builder->CreateAdd(N, |
| 1048 | ConstantInt::get(N->getType(), CI->logBase2())); |
| 1049 | if (Op1 != ShiftLeft) |
| 1050 | N = IC.Builder->CreateZExt(N, Op1->getType()); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1051 | BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N); |
Suyog Sarda | 65f5ae9 | 2014-10-07 12:04:07 +0000 | [diff] [blame] | 1052 | if (I.isExact()) |
| 1053 | LShr->setIsExact(); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1054 | return LShr; |
| 1055 | } |
| 1056 | |
| 1057 | // \brief Recursively visits the possible right hand operands of a udiv |
| 1058 | // instruction, seeing through select instructions, to determine if we can |
| 1059 | // replace the udiv with something simpler. If we find that an operand is not |
| 1060 | // able to simplify the udiv, we abort the entire transformation. |
| 1061 | static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, |
| 1062 | SmallVectorImpl<UDivFoldAction> &Actions, |
| 1063 | unsigned Depth = 0) { |
| 1064 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1065 | // if so, convert to a right shift. |
| 1066 | if (match(Op1, m_Power2())) { |
| 1067 | Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1)); |
| 1068 | return Actions.size(); |
| 1069 | } |
| 1070 | |
| 1071 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) |
| 1072 | // X udiv C, where C >= signbit |
| 1073 | if (C->getValue().isNegative()) { |
| 1074 | Actions.push_back(UDivFoldAction(foldUDivNegCst, C)); |
| 1075 | return Actions.size(); |
| 1076 | } |
| 1077 | |
| 1078 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
| 1079 | if (match(Op1, m_Shl(m_Power2(), m_Value())) || |
| 1080 | match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) { |
| 1081 | Actions.push_back(UDivFoldAction(foldUDivShl, Op1)); |
| 1082 | return Actions.size(); |
| 1083 | } |
| 1084 | |
| 1085 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 1086 | if (Depth++ == MaxDepth) |
| 1087 | return 0; |
| 1088 | |
| 1089 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
David Majnemer | 492e612 | 2014-08-30 09:19:05 +0000 | [diff] [blame] | 1090 | if (size_t LHSIdx = |
| 1091 | visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth)) |
| 1092 | if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) { |
| 1093 | Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1)); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1094 | return Actions.size(); |
| 1095 | } |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1100 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 1101 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1102 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1103 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1104 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1105 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 1106 | if (Value *V = SimplifyUDivInst(Op0, Op1, SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1107 | return replaceInstUsesWith(I, V); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1109 | // Handle the integer div common cases |
| 1110 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1111 | return Common; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1112 | |
Benjamin Kramer | d4a6471 | 2012-08-30 15:07:40 +0000 | [diff] [blame] | 1113 | // (x lshr C1) udiv C2 --> x udiv (C2 << C1) |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1114 | { |
Benjamin Kramer | 9c0a807 | 2012-08-28 13:08:13 +0000 | [diff] [blame] | 1115 | Value *X; |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1116 | const APInt *C1, *C2; |
| 1117 | if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && |
| 1118 | match(Op1, m_APInt(C2))) { |
| 1119 | bool Overflow; |
| 1120 | APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow); |
David Majnemer | a3aeb15 | 2014-11-22 18:16:54 +0000 | [diff] [blame] | 1121 | if (!Overflow) { |
| 1122 | bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value())); |
| 1123 | BinaryOperator *BO = BinaryOperator::CreateUDiv( |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1124 | X, ConstantInt::get(X->getType(), C2ShlC1)); |
David Majnemer | a3aeb15 | 2014-11-22 18:16:54 +0000 | [diff] [blame] | 1125 | if (IsExact) |
| 1126 | BO->setIsExact(); |
| 1127 | return BO; |
| 1128 | } |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1129 | } |
Nadav Rotem | 11935b2 | 2012-08-28 10:01:43 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 1132 | // (zext A) udiv (zext B) --> zext (A udiv B) |
| 1133 | if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) |
| 1134 | if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) |
Suyog Sarda | ea20551 | 2014-10-07 11:56:06 +0000 | [diff] [blame] | 1135 | return new ZExtInst( |
| 1136 | Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", I.isExact()), |
| 1137 | I.getType()); |
Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 1138 | |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1139 | // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...)))) |
| 1140 | SmallVector<UDivFoldAction, 6> UDivActions; |
| 1141 | if (visitUDivOperand(Op0, Op1, I, UDivActions)) |
| 1142 | for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) { |
| 1143 | FoldUDivOperandCb Action = UDivActions[i].FoldAction; |
| 1144 | Value *ActionOp1 = UDivActions[i].OperandToFold; |
| 1145 | Instruction *Inst; |
| 1146 | if (Action) |
| 1147 | Inst = Action(Op0, ActionOp1, I, *this); |
| 1148 | else { |
| 1149 | // This action joins two actions together. The RHS of this action is |
| 1150 | // simply the last action we processed, we saved the LHS action index in |
| 1151 | // the joining action. |
| 1152 | size_t SelectRHSIdx = i - 1; |
| 1153 | Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult; |
| 1154 | size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx; |
| 1155 | Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult; |
| 1156 | Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(), |
| 1157 | SelectLHS, SelectRHS); |
| 1158 | } |
| 1159 | |
| 1160 | // If this is the last action to process, return it to the InstCombiner. |
| 1161 | // Otherwise, we insert it before the UDiv and record it so that we may |
| 1162 | // use it as part of a joining action (i.e., a SelectInst). |
| 1163 | if (e - i != 1) { |
| 1164 | Inst->insertBefore(&I); |
| 1165 | UDivActions[i].FoldResult = Inst; |
| 1166 | } else |
| 1167 | return Inst; |
| 1168 | } |
| 1169 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1170 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 1174 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1175 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1176 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1177 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1178 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 1179 | if (Value *V = SimplifySDivInst(Op0, Op1, SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1180 | return replaceInstUsesWith(I, V); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1182 | // Handle the integer div common cases |
| 1183 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1184 | return Common; |
| 1185 | |
Sanjay Patel | c6ada53 | 2016-06-27 17:25:57 +0000 | [diff] [blame] | 1186 | const APInt *Op1C; |
Sanjay Patel | bedd1f9 | 2016-06-27 18:38:40 +0000 | [diff] [blame] | 1187 | if (match(Op1, m_APInt(Op1C))) { |
| 1188 | // sdiv X, -1 == -X |
| 1189 | if (Op1C->isAllOnesValue()) |
| 1190 | return BinaryOperator::CreateNeg(Op0); |
| 1191 | |
| 1192 | // sdiv exact X, C --> ashr exact X, log2(C) |
| 1193 | if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) { |
| 1194 | Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2()); |
| 1195 | return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); |
| 1196 | } |
Sanjay Patel | 59ed2ff | 2016-06-27 22:27:11 +0000 | [diff] [blame] | 1197 | |
| 1198 | // If the dividend is sign-extended and the constant divisor is small enough |
| 1199 | // to fit in the source type, shrink the division to the narrower type: |
| 1200 | // (sext X) sdiv C --> sext (X sdiv C) |
| 1201 | Value *Op0Src; |
| 1202 | if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) && |
| 1203 | Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) { |
| 1204 | |
| 1205 | // In the general case, we need to make sure that the dividend is not the |
| 1206 | // minimum signed value because dividing that by -1 is UB. But here, we |
| 1207 | // know that the -1 divisor case is already handled above. |
| 1208 | |
| 1209 | Constant *NarrowDivisor = |
| 1210 | ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType()); |
| 1211 | Value *NarrowOp = Builder->CreateSDiv(Op0Src, NarrowDivisor); |
| 1212 | return new SExtInst(NarrowOp, Op0->getType()); |
| 1213 | } |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1214 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1215 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1216 | if (Constant *RHS = dyn_cast<Constant>(Op1)) { |
David Majnemer | f28e2a4 | 2014-07-02 06:42:13 +0000 | [diff] [blame] | 1217 | // X/INT_MIN -> X == INT_MIN |
| 1218 | if (RHS->isMinSignedValue()) |
| 1219 | return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType()); |
| 1220 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1221 | // -X/C --> X/-C provided the negation doesn't overflow. |
David Majnemer | fa4699e | 2014-11-22 20:00:34 +0000 | [diff] [blame] | 1222 | Value *X; |
| 1223 | if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) { |
| 1224 | auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS)); |
| 1225 | BO->setIsExact(I.isExact()); |
| 1226 | return BO; |
| 1227 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1231 | // unsigned inputs), turn this into a udiv. |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1232 | APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); |
Craig Topper | f248468 | 2017-04-17 01:51:19 +0000 | [diff] [blame] | 1233 | if (MaskedValueIsZero(Op0, Mask, 0, &I)) { |
| 1234 | if (MaskedValueIsZero(Op1, Mask, 0, &I)) { |
| 1235 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
| 1236 | auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1237 | BO->setIsExact(I.isExact()); |
| 1238 | return BO; |
| 1239 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1240 | |
Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 1241 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { |
Craig Topper | f248468 | 2017-04-17 01:51:19 +0000 | [diff] [blame] | 1242 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 1243 | // Safe because the only negative value (1 << Y) can take on is |
| 1244 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 1245 | // the sign bit set. |
| 1246 | auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1247 | BO->setIsExact(I.isExact()); |
| 1248 | return BO; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1251 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1252 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1255 | /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special |
| 1256 | /// FP value and: |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1257 | /// 1) 1/C is exact, or |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1258 | /// 2) reciprocal is allowed. |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 1259 | /// If the conversion was successful, the simplified expression "X * 1/C" is |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1260 | /// returned; otherwise, NULL is returned. |
| 1261 | /// |
Suyog Sarda | ea20551 | 2014-10-07 11:56:06 +0000 | [diff] [blame] | 1262 | static Instruction *CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor, |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1263 | bool AllowReciprocal) { |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1264 | if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1265 | return nullptr; |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1266 | |
| 1267 | const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF(); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1268 | APFloat Reciprocal(FpVal.getSemantics()); |
| 1269 | bool Cvt = FpVal.getExactInverse(&Reciprocal); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1270 | |
Michael Gottesman | 3cb77ab | 2013-06-19 21:23:18 +0000 | [diff] [blame] | 1271 | if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1272 | Reciprocal = APFloat(FpVal.getSemantics(), 1.0f); |
| 1273 | (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven); |
| 1274 | Cvt = !Reciprocal.isDenormal(); |
| 1275 | } |
| 1276 | |
| 1277 | if (!Cvt) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1278 | return nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1279 | |
| 1280 | ConstantFP *R; |
| 1281 | R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal); |
| 1282 | return BinaryOperator::CreateFMul(Dividend, R); |
| 1283 | } |
| 1284 | |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1285 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 1286 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1287 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1288 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1289 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1290 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 1291 | if (Value *V = SimplifyFDivInst(Op0, Op1, I.getFastMathFlags(), SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1292 | return replaceInstUsesWith(I, V); |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1293 | |
Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1294 | if (isa<Constant>(Op0)) |
| 1295 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1296 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1297 | return R; |
| 1298 | |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1299 | bool AllowReassociate = I.hasUnsafeAlgebra(); |
| 1300 | bool AllowReciprocal = I.hasAllowReciprocal(); |
Benjamin Kramer | 8564e0d | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 1301 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1302 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1303 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1304 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1305 | return R; |
| 1306 | |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1307 | if (AllowReassociate) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1308 | Constant *C1 = nullptr; |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1309 | Constant *C2 = Op1C; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1310 | Value *X; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1311 | Instruction *Res = nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1312 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1313 | if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1314 | // (X*C1)/C2 => X * (C1/C2) |
| 1315 | // |
| 1316 | Constant *C = ConstantExpr::getFDiv(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1317 | if (isNormalFp(C)) |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1318 | Res = BinaryOperator::CreateFMul(X, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1319 | } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1320 | // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed] |
| 1321 | // |
| 1322 | Constant *C = ConstantExpr::getFMul(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1323 | if (isNormalFp(C)) { |
| 1324 | Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1325 | if (!Res) |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1326 | Res = BinaryOperator::CreateFDiv(X, C); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | if (Res) { |
| 1331 | Res->setFastMathFlags(I.getFastMathFlags()); |
| 1332 | return Res; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | // X / C => X * 1/C |
Owen Anderson | 4557a15 | 2014-01-16 21:07:52 +0000 | [diff] [blame] | 1337 | if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) { |
| 1338 | T->copyFastMathFlags(&I); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1339 | return T; |
Owen Anderson | 4557a15 | 2014-01-16 21:07:52 +0000 | [diff] [blame] | 1340 | } |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1341 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1342 | return nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1345 | if (AllowReassociate && isa<Constant>(Op0)) { |
| 1346 | Constant *C1 = cast<Constant>(Op0), *C2; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1347 | Constant *Fold = nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1348 | Value *X; |
| 1349 | bool CreateDiv = true; |
| 1350 | |
| 1351 | // C1 / (X*C2) => (C1/C2) / X |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1352 | if (match(Op1, m_FMul(m_Value(X), m_Constant(C2)))) |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1353 | Fold = ConstantExpr::getFDiv(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1354 | else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1355 | // C1 / (X/C2) => (C1*C2) / X |
| 1356 | Fold = ConstantExpr::getFMul(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1357 | } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1358 | // C1 / (C2/X) => (C1/C2) * X |
| 1359 | Fold = ConstantExpr::getFDiv(C1, C2); |
| 1360 | CreateDiv = false; |
| 1361 | } |
| 1362 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1363 | if (Fold && isNormalFp(Fold)) { |
| 1364 | Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X) |
| 1365 | : BinaryOperator::CreateFMul(X, Fold); |
| 1366 | R->setFastMathFlags(I.getFastMathFlags()); |
| 1367 | return R; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1368 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1369 | return nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | if (AllowReassociate) { |
| 1373 | Value *X, *Y; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1374 | Value *NewInst = nullptr; |
| 1375 | Instruction *SimpR = nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1376 | |
| 1377 | if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) { |
| 1378 | // (X/Y) / Z => X / (Y*Z) |
| 1379 | // |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1380 | if (!isa<Constant>(Y) || !isa<Constant>(Op1)) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1381 | NewInst = Builder->CreateFMul(Y, Op1); |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 1382 | if (Instruction *RI = dyn_cast<Instruction>(NewInst)) { |
| 1383 | FastMathFlags Flags = I.getFastMathFlags(); |
| 1384 | Flags &= cast<Instruction>(Op0)->getFastMathFlags(); |
| 1385 | RI->setFastMathFlags(Flags); |
| 1386 | } |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1387 | SimpR = BinaryOperator::CreateFDiv(X, NewInst); |
| 1388 | } |
| 1389 | } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) { |
| 1390 | // Z / (X/Y) => Z*Y / X |
| 1391 | // |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1392 | if (!isa<Constant>(Y) || !isa<Constant>(Op0)) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1393 | NewInst = Builder->CreateFMul(Op0, Y); |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 1394 | if (Instruction *RI = dyn_cast<Instruction>(NewInst)) { |
| 1395 | FastMathFlags Flags = I.getFastMathFlags(); |
| 1396 | Flags &= cast<Instruction>(Op1)->getFastMathFlags(); |
| 1397 | RI->setFastMathFlags(Flags); |
| 1398 | } |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1399 | SimpR = BinaryOperator::CreateFDiv(NewInst, X); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | if (NewInst) { |
| 1404 | if (Instruction *T = dyn_cast<Instruction>(NewInst)) |
| 1405 | T->setDebugLoc(I.getDebugLoc()); |
| 1406 | SimpR->setFastMathFlags(I.getFastMathFlags()); |
| 1407 | return SimpR; |
Benjamin Kramer | 8564e0d | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
Matt Arsenault | fdb78f8 | 2017-01-10 23:08:54 +0000 | [diff] [blame] | 1411 | Value *LHS; |
| 1412 | Value *RHS; |
| 1413 | |
| 1414 | // -x / -y -> x / y |
| 1415 | if (match(Op0, m_FNeg(m_Value(LHS))) && match(Op1, m_FNeg(m_Value(RHS)))) { |
| 1416 | I.setOperand(0, LHS); |
| 1417 | I.setOperand(1, RHS); |
| 1418 | return &I; |
| 1419 | } |
| 1420 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1421 | return nullptr; |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1424 | /// This function implements the transforms common to both integer remainder |
| 1425 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 1426 | /// remainder instructions. |
| 1427 | /// @brief Common integer remainder transforms |
| 1428 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 1429 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1430 | |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 1431 | // The RHS is known non-zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1432 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 1433 | I.setOperand(1, V); |
| 1434 | return &I; |
| 1435 | } |
| 1436 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1437 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 1438 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 1439 | return &I; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1440 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1441 | if (isa<Constant>(Op1)) { |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1442 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1443 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 1444 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1445 | return R; |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1446 | } else if (auto *PN = dyn_cast<PHINode>(Op0I)) { |
Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1447 | using namespace llvm::PatternMatch; |
| 1448 | const APInt *Op1Int; |
| 1449 | if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() && |
| 1450 | (I.getOpcode() == Instruction::URem || |
| 1451 | !Op1Int->isMinSignedValue())) { |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1452 | // foldOpIntoPhi will speculate instructions to the end of the PHI's |
Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1453 | // predecessor blocks, so do this only if we know the srem or urem |
| 1454 | // will not fault. |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1455 | if (Instruction *NV = foldOpIntoPhi(I, PN)) |
Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1456 | return NV; |
| 1457 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | // See if we can fold away this rem instruction. |
| 1461 | if (SimplifyDemandedInstructionBits(I)) |
| 1462 | return &I; |
| 1463 | } |
| 1464 | } |
| 1465 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1466 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 1470 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1471 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1472 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1473 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1474 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 1475 | if (Value *V = SimplifyURemInst(Op0, Op1, SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1476 | return replaceInstUsesWith(I, V); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1478 | if (Instruction *common = commonIRemTransforms(I)) |
| 1479 | return common; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1480 | |
David Majnemer | 6c30f49 | 2013-05-12 00:07:05 +0000 | [diff] [blame] | 1481 | // (zext A) urem (zext B) --> zext (A urem B) |
| 1482 | if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0)) |
| 1483 | if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy())) |
| 1484 | return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1), |
| 1485 | I.getType()); |
| 1486 | |
David Majnemer | 470b077 | 2013-05-11 09:01:28 +0000 | [diff] [blame] | 1487 | // X urem Y -> X and Y-1, where Y is a power of 2, |
Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 1488 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 1489 | Constant *N1 = Constant::getAllOnesValue(I.getType()); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1490 | Value *Add = Builder->CreateAdd(Op1, N1); |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 1491 | return BinaryOperator::CreateAnd(Op0, Add); |
| 1492 | } |
| 1493 | |
Nick Lewycky | 7459be6 | 2013-07-13 01:16:47 +0000 | [diff] [blame] | 1494 | // 1 urem X -> zext(X != 1) |
| 1495 | if (match(Op0, m_One())) { |
| 1496 | Value *Cmp = Builder->CreateICmpNE(Op1, Op0); |
| 1497 | Value *Ext = Builder->CreateZExt(Cmp, I.getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1498 | return replaceInstUsesWith(I, Ext); |
Nick Lewycky | 7459be6 | 2013-07-13 01:16:47 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Sanjay Patel | 30ef70b | 2016-09-22 22:36:26 +0000 | [diff] [blame] | 1501 | // X urem C -> X < C ? X : X - C, where C >= signbit. |
| 1502 | const APInt *DivisorC; |
| 1503 | if (match(Op1, m_APInt(DivisorC)) && DivisorC->isNegative()) { |
| 1504 | Value *Cmp = Builder->CreateICmpULT(Op0, Op1); |
| 1505 | Value *Sub = Builder->CreateSub(Op0, Op1); |
| 1506 | return SelectInst::Create(Cmp, Op0, Sub); |
| 1507 | } |
| 1508 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1509 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 1513 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1514 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1515 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1516 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1517 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 1518 | if (Value *V = SimplifySRemInst(Op0, Op1, SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1519 | return replaceInstUsesWith(I, V); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1521 | // Handle the integer rem common cases |
| 1522 | if (Instruction *Common = commonIRemTransforms(I)) |
| 1523 | return Common; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1524 | |
David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1525 | { |
| 1526 | const APInt *Y; |
| 1527 | // X % -Y -> X % Y |
| 1528 | if (match(Op1, m_APInt(Y)) && Y->isNegative() && !Y->isMinSignedValue()) { |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1529 | Worklist.AddValue(I.getOperand(1)); |
David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1530 | I.setOperand(1, ConstantInt::get(I.getType(), -*Y)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1531 | return &I; |
| 1532 | } |
David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1533 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1534 | |
| 1535 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1536 | // unsigned inputs), turn this into a urem. |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1537 | APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); |
Craig Topper | 1a18a7c | 2017-04-17 01:51:24 +0000 | [diff] [blame] | 1538 | if (MaskedValueIsZero(Op1, Mask, 0, &I) && |
| 1539 | MaskedValueIsZero(Op0, Mask, 0, &I)) { |
| 1540 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 1541 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | // If it's a constant vector, flip any negative values positive. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1545 | if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { |
| 1546 | Constant *C = cast<Constant>(Op1); |
| 1547 | unsigned VWidth = C->getType()->getVectorNumElements(); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1548 | |
| 1549 | bool hasNegative = false; |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1550 | bool hasMissing = false; |
| 1551 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1552 | Constant *Elt = C->getAggregateElement(i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1553 | if (!Elt) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1554 | hasMissing = true; |
| 1555 | break; |
| 1556 | } |
| 1557 | |
| 1558 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1559 | if (RHS->isNegative()) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1560 | hasNegative = true; |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1561 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1562 | |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1563 | if (hasNegative && !hasMissing) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1564 | SmallVector<Constant *, 16> Elts(VWidth); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1565 | for (unsigned i = 0; i != VWidth; ++i) { |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1566 | Elts[i] = C->getAggregateElement(i); // Handle undef, etc. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1567 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1568 | if (RHS->isNegative()) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1569 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | Constant *NewRHSV = ConstantVector::get(Elts); |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1574 | if (NewRHSV != C) { // Don't loop on -MININT |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1575 | Worklist.AddValue(I.getOperand(1)); |
| 1576 | I.setOperand(1, NewRHSV); |
| 1577 | return &I; |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1582 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1586 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1587 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1588 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1589 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1590 | |
Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 1591 | if (Value *V = SimplifyFRemInst(Op0, Op1, I.getFastMathFlags(), SQ)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1592 | return replaceInstUsesWith(I, V); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| 1595 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 1596 | return &I; |
| 1597 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1598 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1599 | } |