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