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 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 254 | // Simplify mul instructions with a constant RHS. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 255 | if (isa<Constant>(Op1)) { |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 256 | if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I)) |
| 257 | return FoldedMul; |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 258 | |
| 259 | // Canonicalize (X+C1)*CI -> X*CI+C1*CI. |
| 260 | { |
| 261 | Value *X; |
| 262 | Constant *C1; |
| 263 | if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 264 | Value *Mul = Builder.CreateMul(C1, Op1); |
David Majnemer | 6cf6c05 | 2014-06-19 07:14:33 +0000 | [diff] [blame] | 265 | // Only go forward with the transform if C1*CI simplifies to a tidier |
| 266 | // constant. |
| 267 | if (!match(Mul, m_Mul(m_Value(), m_Value()))) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 268 | return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul); |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 271 | } |
| 272 | |
David Majnemer | 8279a750 | 2014-11-22 07:25:19 +0000 | [diff] [blame] | 273 | if (Value *Op0v = dyn_castNegVal(Op0)) { // -X * -Y = X*Y |
| 274 | if (Value *Op1v = dyn_castNegVal(Op1)) { |
| 275 | BinaryOperator *BO = BinaryOperator::CreateMul(Op0v, Op1v); |
| 276 | if (I.hasNoSignedWrap() && |
| 277 | match(Op0, m_NSWSub(m_Value(), m_Value())) && |
| 278 | match(Op1, m_NSWSub(m_Value(), m_Value()))) |
| 279 | BO->setHasNoSignedWrap(); |
| 280 | return BO; |
| 281 | } |
| 282 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 283 | |
| 284 | // (X / Y) * Y = X - (X % Y) |
| 285 | // (X / Y) * -Y = (X % Y) - X |
| 286 | { |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 287 | Value *Y = Op1; |
| 288 | BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0); |
| 289 | if (!Div || (Div->getOpcode() != Instruction::UDiv && |
| 290 | Div->getOpcode() != Instruction::SDiv)) { |
| 291 | Y = Op0; |
| 292 | Div = dyn_cast<BinaryOperator>(Op1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 293 | } |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 294 | Value *Neg = dyn_castNegVal(Y); |
| 295 | if (Div && Div->hasOneUse() && |
| 296 | (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) && |
| 297 | (Div->getOpcode() == Instruction::UDiv || |
| 298 | Div->getOpcode() == Instruction::SDiv)) { |
| 299 | Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 301 | // 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] | 302 | if (Div->isExact()) { |
| 303 | if (DivOp1 == Y) |
| 304 | return replaceInstUsesWith(I, X); |
| 305 | return BinaryOperator::CreateNeg(X); |
| 306 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 307 | |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 308 | auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem |
| 309 | : Instruction::SRem; |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 310 | Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1); |
Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 311 | if (DivOp1 == Y) |
| 312 | return BinaryOperator::CreateSub(X, Rem); |
| 313 | return BinaryOperator::CreateSub(Rem, X); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | /// i1 mul -> i1 and. |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 318 | if (I.getType()->isIntOrIntVectorTy(1)) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 319 | return BinaryOperator::CreateAnd(Op0, Op1); |
| 320 | |
| 321 | // X*(1 << Y) --> X << Y |
| 322 | // (1 << Y)*X --> X << Y |
| 323 | { |
| 324 | Value *Y; |
David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 325 | BinaryOperator *BO = nullptr; |
| 326 | bool ShlNSW = false; |
| 327 | if (match(Op0, m_Shl(m_One(), m_Value(Y)))) { |
| 328 | BO = BinaryOperator::CreateShl(Op1, Y); |
David Majnemer | 087dc8b | 2015-01-04 07:36:02 +0000 | [diff] [blame] | 329 | ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap(); |
David Majnemer | 8e6f6a9 | 2014-11-24 16:41:13 +0000 | [diff] [blame] | 330 | } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) { |
David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 331 | BO = BinaryOperator::CreateShl(Op0, Y); |
David Majnemer | 087dc8b | 2015-01-04 07:36:02 +0000 | [diff] [blame] | 332 | ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap(); |
David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 333 | } |
| 334 | if (BO) { |
| 335 | if (I.hasNoUnsignedWrap()) |
| 336 | BO->setHasNoUnsignedWrap(); |
| 337 | if (I.hasNoSignedWrap() && ShlNSW) |
| 338 | BO->setHasNoSignedWrap(); |
| 339 | return BO; |
| 340 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 341 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 342 | |
Sanjay Patel | cb8ac00 | 2018-02-13 20:41:22 +0000 | [diff] [blame] | 343 | // (bool X) * Y --> X ? Y : 0 |
Sanjay Patel | 7558d86 | 2018-02-13 22:24:37 +0000 | [diff] [blame^] | 344 | // Y * (bool X) --> X ? Y : 0 |
| 345 | Value *X; |
Sanjay Patel | cb8ac00 | 2018-02-13 20:41:22 +0000 | [diff] [blame] | 346 | if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) |
| 347 | return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0)); |
Sanjay Patel | cb8ac00 | 2018-02-13 20:41:22 +0000 | [diff] [blame] | 348 | if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) |
| 349 | return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0)); |
| 350 | |
Sanjay Patel | 7558d86 | 2018-02-13 22:24:37 +0000 | [diff] [blame^] | 351 | // (lshr X, 31) * Y --> (ashr X, 31) & Y |
| 352 | // Y * (lshr X, 31) --> (ashr X, 31) & Y |
| 353 | // TODO: We are not checking one-use because the elimination of the multiply |
| 354 | // is better for analysis? |
| 355 | // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be |
| 356 | // more similar to what we're doing above. |
| 357 | const APInt *C; |
| 358 | if (match(Op0, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1) |
| 359 | return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op1); |
| 360 | if (match(Op1, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1) |
| 361 | return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op0); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 362 | |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 363 | // Check for (mul (sext x), y), see if we can merge this into an |
| 364 | // integer mul followed by a sext. |
| 365 | if (SExtInst *Op0Conv = dyn_cast<SExtInst>(Op0)) { |
| 366 | // (mul (sext x), cst) --> (sext (mul x, cst')) |
| 367 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 368 | if (Op0Conv->hasOneUse()) { |
| 369 | Constant *CI = |
| 370 | ConstantExpr::getTrunc(Op1C, Op0Conv->getOperand(0)->getType()); |
| 371 | if (ConstantExpr::getSExt(CI, I.getType()) == Op1C && |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 372 | willNotOverflowSignedMul(Op0Conv->getOperand(0), CI, I)) { |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 373 | // Insert the new, smaller mul. |
| 374 | Value *NewMul = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 375 | Builder.CreateNSWMul(Op0Conv->getOperand(0), CI, "mulconv"); |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 376 | return new SExtInst(NewMul, I.getType()); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // (mul (sext x), (sext y)) --> (sext (mul int x, y)) |
| 382 | if (SExtInst *Op1Conv = dyn_cast<SExtInst>(Op1)) { |
| 383 | // Only do this if x/y have the same type, if at last one of them has a |
| 384 | // single use (so we don't increase the number of sexts), and if the |
| 385 | // integer mul will not overflow. |
| 386 | if (Op0Conv->getOperand(0)->getType() == |
| 387 | Op1Conv->getOperand(0)->getType() && |
| 388 | (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) && |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 389 | willNotOverflowSignedMul(Op0Conv->getOperand(0), |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 390 | Op1Conv->getOperand(0), I)) { |
| 391 | // Insert the new integer mul. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 392 | Value *NewMul = Builder.CreateNSWMul( |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 393 | Op0Conv->getOperand(0), Op1Conv->getOperand(0), "mulconv"); |
| 394 | return new SExtInst(NewMul, I.getType()); |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Check for (mul (zext x), y), see if we can merge this into an |
| 400 | // integer mul followed by a zext. |
| 401 | if (auto *Op0Conv = dyn_cast<ZExtInst>(Op0)) { |
| 402 | // (mul (zext x), cst) --> (zext (mul x, cst')) |
| 403 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 404 | if (Op0Conv->hasOneUse()) { |
| 405 | Constant *CI = |
| 406 | ConstantExpr::getTrunc(Op1C, Op0Conv->getOperand(0)->getType()); |
| 407 | if (ConstantExpr::getZExt(CI, I.getType()) == Op1C && |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 408 | willNotOverflowUnsignedMul(Op0Conv->getOperand(0), CI, I)) { |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 409 | // Insert the new, smaller mul. |
| 410 | Value *NewMul = |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 411 | Builder.CreateNUWMul(Op0Conv->getOperand(0), CI, "mulconv"); |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 412 | return new ZExtInst(NewMul, I.getType()); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // (mul (zext x), (zext y)) --> (zext (mul int x, y)) |
| 418 | if (auto *Op1Conv = dyn_cast<ZExtInst>(Op1)) { |
| 419 | // Only do this if x/y have the same type, if at last one of them has a |
| 420 | // single use (so we don't increase the number of zexts), and if the |
| 421 | // integer mul will not overflow. |
| 422 | if (Op0Conv->getOperand(0)->getType() == |
| 423 | Op1Conv->getOperand(0)->getType() && |
| 424 | (Op0Conv->hasOneUse() || Op1Conv->hasOneUse()) && |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 425 | willNotOverflowUnsignedMul(Op0Conv->getOperand(0), |
| 426 | Op1Conv->getOperand(0), I)) { |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 427 | // Insert the new integer mul. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 428 | Value *NewMul = Builder.CreateNUWMul( |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 429 | Op0Conv->getOperand(0), Op1Conv->getOperand(0), "mulconv"); |
| 430 | return new ZExtInst(NewMul, I.getType()); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 435 | if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) { |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 436 | Changed = true; |
| 437 | I.setHasNoSignedWrap(true); |
| 438 | } |
| 439 | |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 440 | if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) { |
David Majnemer | b1296ec | 2014-12-26 09:50:35 +0000 | [diff] [blame] | 441 | Changed = true; |
| 442 | I.setHasNoUnsignedWrap(true); |
| 443 | } |
| 444 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 445 | return Changed ? &I : nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 448 | /// Detect pattern log2(Y * 0.5) with corresponding fast math flags. |
Pedro Artigas | 993acd0 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 449 | static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) { |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 450 | if (!Op->hasOneUse()) |
| 451 | return; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 452 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 453 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op); |
| 454 | if (!II) |
| 455 | return; |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 456 | if (II->getIntrinsicID() != Intrinsic::log2 || !II->isFast()) |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 457 | return; |
| 458 | Log2 = II; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 459 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 460 | Value *OpLog2Of = II->getArgOperand(0); |
| 461 | if (!OpLog2Of->hasOneUse()) |
| 462 | return; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 463 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 464 | Instruction *I = dyn_cast<Instruction>(OpLog2Of); |
| 465 | if (!I) |
| 466 | return; |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 467 | |
| 468 | if (I->getOpcode() != Instruction::FMul || !I->isFast()) |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 469 | return; |
Pedro Artigas | 00b83c9 | 2012-11-30 22:47:15 +0000 | [diff] [blame] | 470 | |
Sanjay Patel | 17045f7 | 2014-10-14 00:33:23 +0000 | [diff] [blame] | 471 | if (match(I->getOperand(0), m_SpecificFP(0.5))) |
| 472 | Y = I->getOperand(1); |
| 473 | else if (match(I->getOperand(1), m_SpecificFP(0.5))) |
| 474 | Y = I->getOperand(0); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 475 | } |
Pedro Artigas | 993acd0 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 476 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 477 | static bool isFiniteNonZeroFp(Constant *C) { |
| 478 | if (C->getType()->isVectorTy()) { |
| 479 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; |
| 480 | ++I) { |
Michael Kuperstein | bcb26d6 | 2015-03-05 08:38:57 +0000 | [diff] [blame] | 481 | ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I)); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 482 | if (!CFP || !CFP->getValueAPF().isFiniteNonZero()) |
| 483 | return false; |
| 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | return isa<ConstantFP>(C) && |
| 489 | cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero(); |
| 490 | } |
| 491 | |
| 492 | static bool isNormalFp(Constant *C) { |
| 493 | if (C->getType()->isVectorTy()) { |
| 494 | for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E; |
| 495 | ++I) { |
Michael Kuperstein | bcb26d6 | 2015-03-05 08:38:57 +0000 | [diff] [blame] | 496 | ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I)); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 497 | if (!CFP || !CFP->getValueAPF().isNormal()) |
| 498 | return false; |
| 499 | } |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal(); |
| 504 | } |
| 505 | |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 506 | /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns |
| 507 | /// true iff the given value is FMul or FDiv with one and only one operand |
| 508 | /// being a normal constant (i.e. not Zero/NaN/Infinity). |
| 509 | static bool isFMulOrFDivWithConstant(Value *V) { |
| 510 | Instruction *I = dyn_cast<Instruction>(V); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 511 | if (!I || (I->getOpcode() != Instruction::FMul && |
Shuxin Yang | 8013866 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 512 | I->getOpcode() != Instruction::FDiv)) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 513 | return false; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 514 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 515 | Constant *C0 = dyn_cast<Constant>(I->getOperand(0)); |
| 516 | Constant *C1 = dyn_cast<Constant>(I->getOperand(1)); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 517 | |
| 518 | if (C0 && C1) |
| 519 | return false; |
| 520 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 521 | return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1)); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /// foldFMulConst() is a helper routine of InstCombiner::visitFMul(). |
| 525 | /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand |
| 526 | /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true). |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 527 | /// This function is to simplify "FMulOrDiv * C" and returns the |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 528 | /// resulting expression. Note that this function could return NULL in |
| 529 | /// case the constants cannot be folded into a normal floating-point. |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 530 | Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C, |
Shuxin Yang | 8013866 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 531 | Instruction *InsertBefore) { |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 532 | assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid"); |
| 533 | |
| 534 | Value *Opnd0 = FMulOrDiv->getOperand(0); |
| 535 | Value *Opnd1 = FMulOrDiv->getOperand(1); |
| 536 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 537 | Constant *C0 = dyn_cast<Constant>(Opnd0); |
| 538 | Constant *C1 = dyn_cast<Constant>(Opnd1); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 539 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 540 | BinaryOperator *R = nullptr; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 541 | |
| 542 | // (X * C0) * C => X * (C0*C) |
| 543 | if (FMulOrDiv->getOpcode() == Instruction::FMul) { |
| 544 | Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 545 | if (isNormalFp(F)) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 546 | R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F); |
| 547 | } else { |
| 548 | if (C0) { |
| 549 | // (C0 / X) * C => (C0 * C) / X |
Shuxin Yang | 3a7ca6e | 2013-09-19 21:13:46 +0000 | [diff] [blame] | 550 | if (FMulOrDiv->hasOneUse()) { |
| 551 | // It would otherwise introduce another div. |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 552 | Constant *F = ConstantExpr::getFMul(C0, C); |
Shuxin Yang | 3a7ca6e | 2013-09-19 21:13:46 +0000 | [diff] [blame] | 553 | if (isNormalFp(F)) |
| 554 | R = BinaryOperator::CreateFDiv(F, Opnd1); |
| 555 | } |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 556 | } else { |
| 557 | // (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] | 558 | Constant *F = ConstantExpr::getFDiv(C, C1); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 559 | if (isNormalFp(F)) { |
| 560 | R = BinaryOperator::CreateFMul(Opnd0, F); |
| 561 | } else { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 562 | // (X / C1) * C => X / (C1/C) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 563 | Constant *F = ConstantExpr::getFDiv(C1, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 564 | if (isNormalFp(F)) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 565 | R = BinaryOperator::CreateFDiv(Opnd0, F); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (R) { |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 571 | R->setFast(true); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 572 | InsertNewInstWith(R, *InsertBefore); |
| 573 | } |
| 574 | |
| 575 | return R; |
| 576 | } |
| 577 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 578 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 579 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 580 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 581 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 582 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 583 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 584 | |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 585 | if (isa<Constant>(Op0)) |
| 586 | std::swap(Op0, Op1); |
| 587 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 588 | if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), |
| 589 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 590 | return replaceInstUsesWith(I, V); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 591 | |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 592 | bool AllowReassociate = I.isFast(); |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 593 | |
Michael Ilseman | d5787be | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 594 | // Simplify mul instructions with a constant RHS. |
| 595 | if (isa<Constant>(Op1)) { |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 596 | if (Instruction *FoldedMul = foldOpWithConstantIntoOperand(I)) |
| 597 | return FoldedMul; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 598 | |
Owen Anderson | f74cfe0 | 2014-01-16 20:36:42 +0000 | [diff] [blame] | 599 | // (fmul X, -1.0) --> (fsub -0.0, X) |
Benjamin Kramer | fea9ac9 | 2014-01-18 16:43:14 +0000 | [diff] [blame] | 600 | if (match(Op1, m_SpecificFP(-1.0))) { |
| 601 | Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType()); |
| 602 | Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0); |
Owen Anderson | f74cfe0 | 2014-01-16 20:36:42 +0000 | [diff] [blame] | 603 | RI->copyFastMathFlags(&I); |
| 604 | return RI; |
| 605 | } |
| 606 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 607 | Constant *C = cast<Constant>(Op1); |
| 608 | if (AllowReassociate && isFiniteNonZeroFp(C)) { |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 609 | // Let MDC denote an expression in one of these forms: |
| 610 | // X * C, C/X, X/C, where C is a constant. |
| 611 | // |
| 612 | // Try to simplify "MDC * Constant" |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 613 | if (isFMulOrFDivWithConstant(Op0)) |
| 614 | if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 615 | return replaceInstUsesWith(I, V); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 616 | |
Quentin Colombet | e684a6d | 2013-02-28 21:12:40 +0000 | [diff] [blame] | 617 | // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C) |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 618 | Instruction *FAddSub = dyn_cast<Instruction>(Op0); |
| 619 | if (FAddSub && |
| 620 | (FAddSub->getOpcode() == Instruction::FAdd || |
| 621 | FAddSub->getOpcode() == Instruction::FSub)) { |
| 622 | Value *Opnd0 = FAddSub->getOperand(0); |
| 623 | Value *Opnd1 = FAddSub->getOperand(1); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 624 | Constant *C0 = dyn_cast<Constant>(Opnd0); |
| 625 | Constant *C1 = dyn_cast<Constant>(Opnd1); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 626 | bool Swap = false; |
| 627 | if (C0) { |
Shuxin Yang | 8013866 | 2013-01-07 22:41:28 +0000 | [diff] [blame] | 628 | std::swap(C0, C1); |
| 629 | std::swap(Opnd0, Opnd1); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 630 | Swap = true; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 633 | if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) { |
Quentin Colombet | e684a6d | 2013-02-28 21:12:40 +0000 | [diff] [blame] | 634 | Value *M1 = ConstantExpr::getFMul(C1, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 635 | Value *M0 = isNormalFp(cast<Constant>(M1)) ? |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 636 | foldFMulConst(cast<Instruction>(Opnd0), C, &I) : |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 637 | nullptr; |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 638 | if (M0 && M1) { |
| 639 | if (Swap && FAddSub->getOpcode() == Instruction::FSub) |
| 640 | std::swap(M0, M1); |
| 641 | |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 642 | Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd) |
| 643 | ? BinaryOperator::CreateFAdd(M0, M1) |
| 644 | : BinaryOperator::CreateFSub(M0, M1); |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 645 | RI->copyFastMathFlags(&I); |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 646 | return RI; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Matt Arsenault | 56c079f | 2016-01-30 05:02:00 +0000 | [diff] [blame] | 653 | if (Op0 == Op1) { |
| 654 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) { |
| 655 | // sqrt(X) * sqrt(X) -> X |
| 656 | if (AllowReassociate && II->getIntrinsicID() == Intrinsic::sqrt) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 657 | return replaceInstUsesWith(I, II->getOperand(0)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 658 | |
Matt Arsenault | 56c079f | 2016-01-30 05:02:00 +0000 | [diff] [blame] | 659 | // fabs(X) * fabs(X) -> X * X |
| 660 | if (II->getIntrinsicID() == Intrinsic::fabs) { |
| 661 | Instruction *FMulVal = BinaryOperator::CreateFMul(II->getOperand(0), |
| 662 | II->getOperand(0), |
| 663 | I.getName()); |
| 664 | FMulVal->copyFastMathFlags(&I); |
| 665 | return FMulVal; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 670 | // Under unsafe algebra do: |
| 671 | // X * log2(0.5*Y) = X*log2(Y) - X |
Sanjay Patel | b41d461 | 2014-10-02 15:20:45 +0000 | [diff] [blame] | 672 | if (AllowReassociate) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 673 | Value *OpX = nullptr; |
| 674 | Value *OpY = nullptr; |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 675 | IntrinsicInst *Log2; |
Pedro Artigas | 993acd0 | 2012-11-30 22:07:05 +0000 | [diff] [blame] | 676 | detectLog2OfHalf(Op0, OpY, Log2); |
| 677 | if (OpY) { |
| 678 | OpX = Op1; |
| 679 | } else { |
| 680 | detectLog2OfHalf(Op1, OpY, Log2); |
| 681 | if (OpY) { |
| 682 | OpX = Op0; |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | // if pattern detected emit alternate sequence |
| 686 | if (OpX && OpY) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 687 | BuilderTy::FastMathFlagGuard Guard(Builder); |
| 688 | Builder.setFastMathFlags(Log2->getFastMathFlags()); |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 689 | Log2->setArgOperand(0, OpY); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 690 | Value *FMulVal = Builder.CreateFMul(OpX, Log2); |
| 691 | Value *FSub = Builder.CreateFSub(FMulVal, OpX); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 692 | FSub->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 693 | return replaceInstUsesWith(I, FSub); |
Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Dmitry Venikov | a58d8de | 2018-01-02 05:58:11 +0000 | [diff] [blame] | 697 | // sqrt(a) * sqrt(b) -> sqrt(a * b) |
Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 698 | if (AllowReassociate && Op0->hasOneUse() && Op1->hasOneUse()) { |
Dmitry Venikov | a58d8de | 2018-01-02 05:58:11 +0000 | [diff] [blame] | 699 | Value *Opnd0 = nullptr; |
| 700 | Value *Opnd1 = nullptr; |
| 701 | if (match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(Opnd0))) && |
| 702 | match(Op1, m_Intrinsic<Intrinsic::sqrt>(m_Value(Opnd1)))) { |
| 703 | BuilderTy::FastMathFlagGuard Guard(Builder); |
| 704 | Builder.setFastMathFlags(I.getFastMathFlags()); |
| 705 | Value *FMulVal = Builder.CreateFMul(Opnd0, Opnd1); |
| 706 | Value *Sqrt = Intrinsic::getDeclaration(I.getModule(), |
| 707 | Intrinsic::sqrt, I.getType()); |
| 708 | Value *SqrtCall = Builder.CreateCall(Sqrt, FMulVal); |
| 709 | return replaceInstUsesWith(I, SqrtCall); |
| 710 | } |
| 711 | } |
| 712 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 713 | // Handle symmetric situation in a 2-iteration loop |
| 714 | Value *Opnd0 = Op0; |
| 715 | Value *Opnd1 = Op1; |
| 716 | for (int i = 0; i < 2; i++) { |
| 717 | bool IgnoreZeroSign = I.hasNoSignedZeros(); |
| 718 | if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 719 | BuilderTy::FastMathFlagGuard Guard(Builder); |
| 720 | Builder.setFastMathFlags(I.getFastMathFlags()); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 721 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 722 | Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign); |
| 723 | Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign); |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 724 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 725 | // -X * -Y => X*Y |
Owen Anderson | e8537fc | 2014-01-16 20:59:41 +0000 | [diff] [blame] | 726 | if (N1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 727 | Value *FMul = Builder.CreateFMul(N0, N1); |
Owen Anderson | e8537fc | 2014-01-16 20:59:41 +0000 | [diff] [blame] | 728 | FMul->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 729 | return replaceInstUsesWith(I, FMul); |
Owen Anderson | e8537fc | 2014-01-16 20:59:41 +0000 | [diff] [blame] | 730 | } |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 731 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 732 | if (Opnd0->hasOneUse()) { |
| 733 | // -X * Y => -(X*Y) (Promote negation as high as possible) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 734 | Value *T = Builder.CreateFMul(N0, Opnd1); |
| 735 | Value *Neg = Builder.CreateFNeg(T); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 736 | Neg->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 737 | return replaceInstUsesWith(I, Neg); |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 740 | |
Quentin Colombet | aa103b3 | 2017-09-20 17:32:16 +0000 | [diff] [blame] | 741 | // Handle specials cases for FMul with selects feeding the operation |
| 742 | if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1)) |
| 743 | return replaceInstUsesWith(I, V); |
| 744 | |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 745 | // (X*Y) * X => (X*X) * Y where Y != X |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 746 | // The purpose is two-fold: |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 747 | // 1) to form a power expression (of X). |
| 748 | // 2) potentially shorten the critical path: After transformation, the |
| 749 | // latency of the instruction Y is amortized by the expression of X*X, |
| 750 | // and therefore Y is in a "less critical" position compared to what it |
| 751 | // was before the transformation. |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 752 | if (AllowReassociate) { |
| 753 | Value *Opnd0_0, *Opnd0_1; |
| 754 | if (Opnd0->hasOneUse() && |
| 755 | match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 756 | Value *Y = nullptr; |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 757 | if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1) |
| 758 | Y = Opnd0_1; |
| 759 | else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1) |
| 760 | Y = Opnd0_0; |
| 761 | |
| 762 | if (Y) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 763 | BuilderTy::FastMathFlagGuard Guard(Builder); |
| 764 | Builder.setFastMathFlags(I.getFastMathFlags()); |
| 765 | Value *T = Builder.CreateFMul(Opnd1, Opnd1); |
| 766 | Value *R = Builder.CreateFMul(T, Y); |
Benjamin Kramer | 6748576 | 2013-09-30 15:39:59 +0000 | [diff] [blame] | 767 | R->takeName(&I); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 768 | return replaceInstUsesWith(I, R); |
Shuxin Yang | e822745 | 2013-01-15 21:09:32 +0000 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | if (!isa<Constant>(Op1)) |
| 774 | std::swap(Opnd0, Opnd1); |
| 775 | else |
| 776 | break; |
Shuxin Yang | f8e9a5a | 2012-12-14 18:46:06 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 779 | return Changed ? &I : nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 782 | /// Fold a divide or remainder with a select instruction divisor when one of the |
| 783 | /// select operands is zero. In that case, we can use the other select operand |
| 784 | /// because div/rem by zero is undefined. |
| 785 | bool InstCombiner::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) { |
| 786 | SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1)); |
| 787 | if (!SI) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 788 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 789 | |
Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 790 | int NonNullOperand; |
| 791 | if (match(SI->getTrueValue(), m_Zero())) |
| 792 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 793 | NonNullOperand = 2; |
| 794 | else if (match(SI->getFalseValue(), m_Zero())) |
| 795 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 796 | NonNullOperand = 1; |
| 797 | else |
| 798 | return false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 799 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 800 | // Change the div/rem to use 'Y' instead of the select. |
| 801 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 802 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 803 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 804 | // problem. However, the select, or the condition of the select may have |
| 805 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 806 | // propagate the known value for the select into other uses of it, and |
| 807 | // propagate a known value of the condition into its other users. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 808 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 809 | // If the select and condition only have a single use, don't bother with this, |
| 810 | // early exit. |
Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 811 | Value *SelectCond = SI->getCondition(); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 812 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 813 | return true; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 814 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 815 | // 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] | 816 | BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin(); |
Sanjay Patel | 72d339a | 2017-10-06 23:43:06 +0000 | [diff] [blame] | 817 | Type *CondTy = SelectCond->getType(); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 818 | while (BBI != BBFront) { |
| 819 | --BBI; |
| 820 | // If we found a call to a function, we can't assume it will return, so |
| 821 | // information from below it cannot be propagated above it. |
| 822 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 823 | break; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 824 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 825 | // Replace uses of the select or its condition with the known values. |
| 826 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 827 | I != E; ++I) { |
| 828 | if (*I == SI) { |
| 829 | *I = SI->getOperand(NonNullOperand); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 830 | Worklist.Add(&*BBI); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 831 | } else if (*I == SelectCond) { |
Sanjay Patel | 72d339a | 2017-10-06 23:43:06 +0000 | [diff] [blame] | 832 | *I = NonNullOperand == 1 ? ConstantInt::getTrue(CondTy) |
| 833 | : ConstantInt::getFalse(CondTy); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 834 | Worklist.Add(&*BBI); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 837 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 838 | // If we past the instruction, quit looking for it. |
| 839 | if (&*BBI == SI) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 840 | SI = nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 841 | if (&*BBI == SelectCond) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 842 | SelectCond = nullptr; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 843 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 844 | // 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] | 845 | if (!SelectCond && !SI) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 846 | break; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 847 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 848 | } |
| 849 | return true; |
| 850 | } |
| 851 | |
Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 852 | /// True if the multiply can not be expressed in an int this size. |
| 853 | static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, |
| 854 | bool IsSigned) { |
| 855 | bool Overflow; |
| 856 | Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow); |
| 857 | return Overflow; |
| 858 | } |
| 859 | |
| 860 | /// True if C2 is a multiple of C1. Quotient contains C2/C1. |
| 861 | static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, |
| 862 | bool IsSigned) { |
| 863 | assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal"); |
| 864 | |
| 865 | // Bail if we will divide by zero. |
| 866 | if (C2.isNullValue()) |
| 867 | return false; |
| 868 | |
| 869 | // Bail if we would divide INT_MIN by -1. |
| 870 | if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue()) |
| 871 | return false; |
| 872 | |
| 873 | APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 874 | if (IsSigned) |
| 875 | APInt::sdivrem(C1, C2, Quotient, Remainder); |
| 876 | else |
| 877 | APInt::udivrem(C1, C2, Quotient, Remainder); |
| 878 | |
| 879 | return Remainder.isMinValue(); |
| 880 | } |
| 881 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 882 | /// This function implements the transforms common to both integer division |
| 883 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 884 | /// division instructions. |
| 885 | /// @brief Common integer divide transforms |
| 886 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 887 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 888 | bool IsSigned = I.getOpcode() == Instruction::SDiv; |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 889 | Type *Ty = I.getType(); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 891 | // The RHS is known non-zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 892 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 893 | I.setOperand(1, V); |
| 894 | return &I; |
| 895 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 896 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 897 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 898 | // This does not apply for fdiv. |
Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 899 | if (simplifyDivRemOfSelectWithZeroOp(I)) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 900 | return &I; |
| 901 | |
Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 902 | const APInt *C2; |
| 903 | if (match(Op1, m_APInt(C2))) { |
| 904 | Value *X; |
| 905 | const APInt *C1; |
David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 906 | |
Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 907 | // (X / C1) / C2 -> X / (C1*C2) |
| 908 | if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) || |
| 909 | (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) { |
| 910 | APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 911 | if (!multiplyOverflows(*C1, *C2, Product, IsSigned)) |
| 912 | return BinaryOperator::Create(I.getOpcode(), X, |
| 913 | ConstantInt::get(Ty, Product)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 914 | } |
Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 915 | |
| 916 | if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) || |
| 917 | (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) { |
| 918 | APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 919 | |
| 920 | // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1. |
| 921 | if (isMultiple(*C2, *C1, Quotient, IsSigned)) { |
| 922 | auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X, |
| 923 | ConstantInt::get(Ty, Quotient)); |
| 924 | NewDiv->setIsExact(I.isExact()); |
| 925 | return NewDiv; |
| 926 | } |
| 927 | |
| 928 | // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2. |
| 929 | if (isMultiple(*C1, *C2, Quotient, IsSigned)) { |
| 930 | auto *Mul = BinaryOperator::Create(Instruction::Mul, X, |
| 931 | ConstantInt::get(Ty, Quotient)); |
| 932 | auto *OBO = cast<OverflowingBinaryOperator>(Op0); |
| 933 | Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); |
| 934 | Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); |
| 935 | return Mul; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) && |
| 940 | *C1 != C1->getBitWidth() - 1) || |
| 941 | (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))))) { |
| 942 | APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 943 | APInt C1Shifted = APInt::getOneBitSet( |
| 944 | C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue())); |
| 945 | |
| 946 | // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1. |
| 947 | if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) { |
| 948 | auto *BO = BinaryOperator::Create(I.getOpcode(), X, |
| 949 | ConstantInt::get(Ty, Quotient)); |
| 950 | BO->setIsExact(I.isExact()); |
| 951 | return BO; |
| 952 | } |
| 953 | |
| 954 | // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2. |
| 955 | if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) { |
| 956 | auto *Mul = BinaryOperator::Create(Instruction::Mul, X, |
| 957 | ConstantInt::get(Ty, Quotient)); |
| 958 | auto *OBO = cast<OverflowingBinaryOperator>(Op0); |
| 959 | Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); |
| 960 | Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); |
| 961 | return Mul; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | if (!C2->isNullValue()) // avoid X udiv 0 |
| 966 | if (Instruction *FoldedDiv = foldOpWithConstantIntoOperand(I)) |
| 967 | return FoldedDiv; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 970 | if (match(Op0, m_One())) { |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 971 | assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?"); |
| 972 | if (IsSigned) { |
Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 973 | // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the |
| 974 | // result is one, if Op1 is -1 then the result is minus one, otherwise |
| 975 | // it's zero. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 976 | Value *Inc = Builder.CreateAdd(Op1, Op0); |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 977 | Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3)); |
| 978 | return SelectInst::Create(Cmp, Op1, ConstantInt::get(Ty, 0)); |
Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 979 | } else { |
| 980 | // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the |
| 981 | // result is one, otherwise it's zero. |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 982 | return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty); |
Nick Lewycky | f0cf8fa | 2014-05-14 03:03:05 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
Benjamin Kramer | 57b3df5 | 2011-04-30 18:16:00 +0000 | [diff] [blame] | 986 | // See if we can fold away this div instruction. |
| 987 | if (SimplifyDemandedInstructionBits(I)) |
| 988 | return &I; |
| 989 | |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 990 | // (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] | 991 | Value *X, *Z; |
| 992 | if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1 |
| 993 | if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || |
| 994 | (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 995 | return BinaryOperator::Create(I.getOpcode(), X, Op1); |
Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 996 | |
| 997 | // (X << Y) / X -> 1 << Y |
| 998 | Value *Y; |
| 999 | if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y)))) |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 1000 | return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y); |
Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 1001 | if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y)))) |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 1002 | return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1003 | |
Sanjay Patel | 510d647 | 2018-02-11 17:20:32 +0000 | [diff] [blame] | 1004 | // X / (X * Y) -> 1 / Y if the multiplication does not overflow. |
| 1005 | if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) { |
| 1006 | bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap(); |
| 1007 | bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap(); |
| 1008 | if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) { |
Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 1009 | I.setOperand(0, ConstantInt::get(Ty, 1)); |
Sanjay Patel | 510d647 | 2018-02-11 17:20:32 +0000 | [diff] [blame] | 1010 | I.setOperand(1, Y); |
| 1011 | return &I; |
| 1012 | } |
| 1013 | } |
| 1014 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1015 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1018 | static const unsigned MaxDepth = 6; |
| 1019 | |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1020 | namespace { |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1021 | |
| 1022 | using FoldUDivOperandCb = Instruction *(*)(Value *Op0, Value *Op1, |
| 1023 | const BinaryOperator &I, |
| 1024 | InstCombiner &IC); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1025 | |
| 1026 | /// \brief Used to maintain state for visitUDivOperand(). |
| 1027 | struct UDivFoldAction { |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1028 | /// Informs visitUDiv() how to fold this operand. This can be zero if this |
| 1029 | /// action joins two actions together. |
| 1030 | FoldUDivOperandCb FoldAction; |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1031 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1032 | /// Which operand to fold. |
| 1033 | Value *OperandToFold; |
| 1034 | |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1035 | union { |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1036 | /// The instruction returned when FoldAction is invoked. |
| 1037 | Instruction *FoldResult; |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1038 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1039 | /// Stores the LHS action index if this action joins two actions together. |
| 1040 | size_t SelectLHSIdx; |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1041 | }; |
| 1042 | |
| 1043 | UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1044 | : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {} |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1045 | UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS) |
| 1046 | : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {} |
| 1047 | }; |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1048 | |
| 1049 | } // end anonymous namespace |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1050 | |
| 1051 | // X udiv 2^C -> X >> C |
| 1052 | static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1, |
| 1053 | const BinaryOperator &I, InstCombiner &IC) { |
Simon Pilgrim | 94cc89d | 2018-02-08 14:46:10 +0000 | [diff] [blame] | 1054 | Constant *C1 = getLogBase2(Op0->getType(), cast<Constant>(Op1)); |
| 1055 | if (!C1) |
| 1056 | llvm_unreachable("Failed to constant fold udiv -> logbase2"); |
| 1057 | BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, C1); |
Suyog Sarda | 65f5ae9 | 2014-10-07 12:04:07 +0000 | [diff] [blame] | 1058 | if (I.isExact()) |
| 1059 | LShr->setIsExact(); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1060 | return LShr; |
| 1061 | } |
| 1062 | |
| 1063 | // X udiv C, where C >= signbit |
| 1064 | static Instruction *foldUDivNegCst(Value *Op0, Value *Op1, |
| 1065 | const BinaryOperator &I, InstCombiner &IC) { |
Simon Pilgrim | 9620f4b | 2018-02-09 10:43:59 +0000 | [diff] [blame] | 1066 | Value *ICI = IC.Builder.CreateICmpULT(Op0, cast<Constant>(Op1)); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1067 | return SelectInst::Create(ICI, Constant::getNullValue(I.getType()), |
| 1068 | ConstantInt::get(I.getType(), 1)); |
| 1069 | } |
| 1070 | |
| 1071 | // 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] | 1072 | // 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] | 1073 | static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I, |
| 1074 | InstCombiner &IC) { |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1075 | Value *ShiftLeft; |
| 1076 | if (!match(Op1, m_ZExt(m_Value(ShiftLeft)))) |
| 1077 | ShiftLeft = Op1; |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1078 | |
Simon Pilgrim | 2a90acd | 2018-02-08 15:19:38 +0000 | [diff] [blame] | 1079 | Constant *CI; |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1080 | Value *N; |
Simon Pilgrim | 2a90acd | 2018-02-08 15:19:38 +0000 | [diff] [blame] | 1081 | if (!match(ShiftLeft, m_Shl(m_Constant(CI), m_Value(N)))) |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1082 | llvm_unreachable("match should never fail here!"); |
Simon Pilgrim | 2a90acd | 2018-02-08 15:19:38 +0000 | [diff] [blame] | 1083 | Constant *Log2Base = getLogBase2(N->getType(), CI); |
| 1084 | if (!Log2Base) |
| 1085 | llvm_unreachable("getLogBase2 should never fail here!"); |
| 1086 | N = IC.Builder.CreateAdd(N, Log2Base); |
Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 1087 | if (Op1 != ShiftLeft) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1088 | N = IC.Builder.CreateZExt(N, Op1->getType()); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1089 | BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N); |
Suyog Sarda | 65f5ae9 | 2014-10-07 12:04:07 +0000 | [diff] [blame] | 1090 | if (I.isExact()) |
| 1091 | LShr->setIsExact(); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1092 | return LShr; |
| 1093 | } |
| 1094 | |
| 1095 | // \brief Recursively visits the possible right hand operands of a udiv |
| 1096 | // instruction, seeing through select instructions, to determine if we can |
| 1097 | // replace the udiv with something simpler. If we find that an operand is not |
| 1098 | // able to simplify the udiv, we abort the entire transformation. |
| 1099 | static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, |
| 1100 | SmallVectorImpl<UDivFoldAction> &Actions, |
| 1101 | unsigned Depth = 0) { |
| 1102 | // Check to see if this is an unsigned division with an exact power of 2, |
| 1103 | // if so, convert to a right shift. |
| 1104 | if (match(Op1, m_Power2())) { |
| 1105 | Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1)); |
| 1106 | return Actions.size(); |
| 1107 | } |
| 1108 | |
Simon Pilgrim | 9620f4b | 2018-02-09 10:43:59 +0000 | [diff] [blame] | 1109 | // X udiv C, where C >= signbit |
| 1110 | if (match(Op1, m_Negative())) { |
| 1111 | Actions.push_back(UDivFoldAction(foldUDivNegCst, Op1)); |
| 1112 | return Actions.size(); |
| 1113 | } |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1114 | |
| 1115 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
| 1116 | if (match(Op1, m_Shl(m_Power2(), m_Value())) || |
| 1117 | match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) { |
| 1118 | Actions.push_back(UDivFoldAction(foldUDivShl, Op1)); |
| 1119 | return Actions.size(); |
| 1120 | } |
| 1121 | |
| 1122 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 1123 | if (Depth++ == MaxDepth) |
| 1124 | return 0; |
| 1125 | |
| 1126 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
David Majnemer | 492e612 | 2014-08-30 09:19:05 +0000 | [diff] [blame] | 1127 | if (size_t LHSIdx = |
| 1128 | visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth)) |
| 1129 | if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) { |
| 1130 | Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1)); |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1131 | return Actions.size(); |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Sanjay Patel | bb78938 | 2017-08-24 22:54:01 +0000 | [diff] [blame] | 1137 | /// If we have zero-extended operands of an unsigned div or rem, we may be able |
| 1138 | /// to narrow the operation (sink the zext below the math). |
| 1139 | static Instruction *narrowUDivURem(BinaryOperator &I, |
| 1140 | InstCombiner::BuilderTy &Builder) { |
| 1141 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 1142 | Value *N = I.getOperand(0); |
| 1143 | Value *D = I.getOperand(1); |
| 1144 | Type *Ty = I.getType(); |
| 1145 | Value *X, *Y; |
| 1146 | if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) && |
| 1147 | X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) { |
| 1148 | // udiv (zext X), (zext Y) --> zext (udiv X, Y) |
| 1149 | // urem (zext X), (zext Y) --> zext (urem X, Y) |
| 1150 | Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y); |
| 1151 | return new ZExtInst(NarrowOp, Ty); |
| 1152 | } |
| 1153 | |
| 1154 | Constant *C; |
| 1155 | if ((match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) || |
| 1156 | (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C)))) { |
| 1157 | // If the constant is the same in the smaller type, use the narrow version. |
| 1158 | Constant *TruncC = ConstantExpr::getTrunc(C, X->getType()); |
| 1159 | if (ConstantExpr::getZExt(TruncC, Ty) != C) |
| 1160 | return nullptr; |
| 1161 | |
| 1162 | // udiv (zext X), C --> zext (udiv X, C') |
| 1163 | // urem (zext X), C --> zext (urem X, C') |
| 1164 | // udiv C, (zext X) --> zext (udiv C', X) |
| 1165 | // urem C, (zext X) --> zext (urem C', X) |
| 1166 | Value *NarrowOp = isa<Constant>(D) ? Builder.CreateBinOp(Opcode, X, TruncC) |
| 1167 | : Builder.CreateBinOp(Opcode, TruncC, X); |
| 1168 | return new ZExtInst(NarrowOp, Ty); |
| 1169 | } |
| 1170 | |
| 1171 | return nullptr; |
| 1172 | } |
| 1173 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1174 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 1175 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1176 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1177 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1178 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1179 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1180 | if (Value *V = SimplifyUDivInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1181 | return replaceInstUsesWith(I, V); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1183 | // Handle the integer div common cases |
| 1184 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1185 | return Common; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1186 | |
Benjamin Kramer | d4a6471 | 2012-08-30 15:07:40 +0000 | [diff] [blame] | 1187 | // (x lshr C1) udiv C2 --> x udiv (C2 << C1) |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1188 | { |
Benjamin Kramer | 9c0a807 | 2012-08-28 13:08:13 +0000 | [diff] [blame] | 1189 | Value *X; |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1190 | const APInt *C1, *C2; |
| 1191 | if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && |
| 1192 | match(Op1, m_APInt(C2))) { |
| 1193 | bool Overflow; |
| 1194 | APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow); |
David Majnemer | a3aeb15 | 2014-11-22 18:16:54 +0000 | [diff] [blame] | 1195 | if (!Overflow) { |
| 1196 | bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value())); |
| 1197 | BinaryOperator *BO = BinaryOperator::CreateUDiv( |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1198 | X, ConstantInt::get(X->getType(), C2ShlC1)); |
David Majnemer | a3aeb15 | 2014-11-22 18:16:54 +0000 | [diff] [blame] | 1199 | if (IsExact) |
| 1200 | BO->setIsExact(); |
| 1201 | return BO; |
| 1202 | } |
David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 1203 | } |
Nadav Rotem | 11935b2 | 2012-08-28 10:01:43 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Sanjay Patel | bb78938 | 2017-08-24 22:54:01 +0000 | [diff] [blame] | 1206 | if (Instruction *NarrowDiv = narrowUDivURem(I, Builder)) |
| 1207 | return NarrowDiv; |
Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 1208 | |
David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 1209 | // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...)))) |
| 1210 | SmallVector<UDivFoldAction, 6> UDivActions; |
| 1211 | if (visitUDivOperand(Op0, Op1, I, UDivActions)) |
| 1212 | for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) { |
| 1213 | FoldUDivOperandCb Action = UDivActions[i].FoldAction; |
| 1214 | Value *ActionOp1 = UDivActions[i].OperandToFold; |
| 1215 | Instruction *Inst; |
| 1216 | if (Action) |
| 1217 | Inst = Action(Op0, ActionOp1, I, *this); |
| 1218 | else { |
| 1219 | // This action joins two actions together. The RHS of this action is |
| 1220 | // simply the last action we processed, we saved the LHS action index in |
| 1221 | // the joining action. |
| 1222 | size_t SelectRHSIdx = i - 1; |
| 1223 | Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult; |
| 1224 | size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx; |
| 1225 | Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult; |
| 1226 | Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(), |
| 1227 | SelectLHS, SelectRHS); |
| 1228 | } |
| 1229 | |
| 1230 | // If this is the last action to process, return it to the InstCombiner. |
| 1231 | // Otherwise, we insert it before the UDiv and record it so that we may |
| 1232 | // use it as part of a joining action (i.e., a SelectInst). |
| 1233 | if (e - i != 1) { |
| 1234 | Inst->insertBefore(&I); |
| 1235 | UDivActions[i].FoldResult = Inst; |
| 1236 | } else |
| 1237 | return Inst; |
| 1238 | } |
| 1239 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1240 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 1244 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1245 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1246 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1247 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1248 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1249 | if (Value *V = SimplifySDivInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1250 | return replaceInstUsesWith(I, V); |
Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1251 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1252 | // Handle the integer div common cases |
| 1253 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1254 | return Common; |
| 1255 | |
Sanjay Patel | c6ada53 | 2016-06-27 17:25:57 +0000 | [diff] [blame] | 1256 | const APInt *Op1C; |
Sanjay Patel | bedd1f9 | 2016-06-27 18:38:40 +0000 | [diff] [blame] | 1257 | if (match(Op1, m_APInt(Op1C))) { |
| 1258 | // sdiv X, -1 == -X |
| 1259 | if (Op1C->isAllOnesValue()) |
| 1260 | return BinaryOperator::CreateNeg(Op0); |
| 1261 | |
| 1262 | // sdiv exact X, C --> ashr exact X, log2(C) |
| 1263 | if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) { |
| 1264 | Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2()); |
| 1265 | return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); |
| 1266 | } |
Sanjay Patel | 59ed2ff | 2016-06-27 22:27:11 +0000 | [diff] [blame] | 1267 | |
| 1268 | // If the dividend is sign-extended and the constant divisor is small enough |
| 1269 | // to fit in the source type, shrink the division to the narrower type: |
| 1270 | // (sext X) sdiv C --> sext (X sdiv C) |
| 1271 | Value *Op0Src; |
| 1272 | if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) && |
| 1273 | Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) { |
| 1274 | |
| 1275 | // In the general case, we need to make sure that the dividend is not the |
| 1276 | // minimum signed value because dividing that by -1 is UB. But here, we |
| 1277 | // know that the -1 divisor case is already handled above. |
| 1278 | |
| 1279 | Constant *NarrowDivisor = |
| 1280 | ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType()); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1281 | Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor); |
Sanjay Patel | 59ed2ff | 2016-06-27 22:27:11 +0000 | [diff] [blame] | 1282 | return new SExtInst(NarrowOp, Op0->getType()); |
| 1283 | } |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1284 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1285 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1286 | if (Constant *RHS = dyn_cast<Constant>(Op1)) { |
David Majnemer | f28e2a4 | 2014-07-02 06:42:13 +0000 | [diff] [blame] | 1287 | // X/INT_MIN -> X == INT_MIN |
| 1288 | if (RHS->isMinSignedValue()) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1289 | return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), I.getType()); |
David Majnemer | f28e2a4 | 2014-07-02 06:42:13 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1291 | // -X/C --> X/-C provided the negation doesn't overflow. |
David Majnemer | fa4699e | 2014-11-22 20:00:34 +0000 | [diff] [blame] | 1292 | Value *X; |
| 1293 | if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) { |
| 1294 | auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS)); |
| 1295 | BO->setIsExact(I.isExact()); |
| 1296 | return BO; |
| 1297 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1301 | // unsigned inputs), turn this into a udiv. |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1302 | APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); |
Craig Topper | f248468 | 2017-04-17 01:51:19 +0000 | [diff] [blame] | 1303 | if (MaskedValueIsZero(Op0, Mask, 0, &I)) { |
| 1304 | if (MaskedValueIsZero(Op1, Mask, 0, &I)) { |
| 1305 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
| 1306 | auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1307 | BO->setIsExact(I.isExact()); |
| 1308 | return BO; |
| 1309 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1310 | |
Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 1311 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { |
Craig Topper | f248468 | 2017-04-17 01:51:19 +0000 | [diff] [blame] | 1312 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 1313 | // Safe because the only negative value (1 << Y) can take on is |
| 1314 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 1315 | // the sign bit set. |
| 1316 | auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1317 | BO->setIsExact(I.isExact()); |
| 1318 | return BO; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1319 | } |
| 1320 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1321 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1322 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1325 | /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special |
| 1326 | /// FP value and: |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1327 | /// 1) 1/C is exact, or |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1328 | /// 2) reciprocal is allowed. |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 1329 | /// If the conversion was successful, the simplified expression "X * 1/C" is |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1330 | /// returned; otherwise, nullptr is returned. |
Suyog Sarda | ea20551 | 2014-10-07 11:56:06 +0000 | [diff] [blame] | 1331 | static Instruction *CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor, |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1332 | bool AllowReciprocal) { |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1333 | if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1334 | return nullptr; |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1335 | |
| 1336 | const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF(); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1337 | APFloat Reciprocal(FpVal.getSemantics()); |
| 1338 | bool Cvt = FpVal.getExactInverse(&Reciprocal); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1339 | |
Michael Gottesman | 3cb77ab | 2013-06-19 21:23:18 +0000 | [diff] [blame] | 1340 | if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1341 | Reciprocal = APFloat(FpVal.getSemantics(), 1.0f); |
| 1342 | (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven); |
| 1343 | Cvt = !Reciprocal.isDenormal(); |
| 1344 | } |
| 1345 | |
| 1346 | if (!Cvt) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1347 | return nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1348 | |
| 1349 | ConstantFP *R; |
| 1350 | R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal); |
| 1351 | return BinaryOperator::CreateFMul(Dividend, R); |
| 1352 | } |
| 1353 | |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1354 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 1355 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1356 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1357 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1358 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1359 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1360 | if (Value *V = SimplifyFDivInst(Op0, Op1, I.getFastMathFlags(), |
| 1361 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1362 | return replaceInstUsesWith(I, V); |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1363 | |
Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1364 | if (isa<Constant>(Op0)) |
| 1365 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1366 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1367 | return R; |
| 1368 | |
Sanjay Patel | 629c411 | 2017-11-06 16:27:15 +0000 | [diff] [blame] | 1369 | bool AllowReassociate = I.isFast(); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1370 | bool AllowReciprocal = I.hasAllowReciprocal(); |
Benjamin Kramer | 8564e0d | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 1371 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1372 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1373 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1374 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1375 | return R; |
| 1376 | |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1377 | if (AllowReassociate) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1378 | Constant *C1 = nullptr; |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1379 | Constant *C2 = Op1C; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1380 | Value *X; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1381 | Instruction *Res = nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1382 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1383 | if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1384 | // (X*C1)/C2 => X * (C1/C2) |
| 1385 | // |
| 1386 | Constant *C = ConstantExpr::getFDiv(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1387 | if (isNormalFp(C)) |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1388 | Res = BinaryOperator::CreateFMul(X, C); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1389 | } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1390 | // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed] |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1391 | Constant *C = ConstantExpr::getFMul(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1392 | if (isNormalFp(C)) { |
| 1393 | Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1394 | if (!Res) |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1395 | Res = BinaryOperator::CreateFDiv(X, C); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if (Res) { |
| 1400 | Res->setFastMathFlags(I.getFastMathFlags()); |
| 1401 | return Res; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | // X / C => X * 1/C |
Owen Anderson | 4557a15 | 2014-01-16 21:07:52 +0000 | [diff] [blame] | 1406 | if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) { |
| 1407 | T->copyFastMathFlags(&I); |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1408 | return T; |
Owen Anderson | 4557a15 | 2014-01-16 21:07:52 +0000 | [diff] [blame] | 1409 | } |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1410 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1411 | return nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1414 | if (AllowReassociate && isa<Constant>(Op0)) { |
| 1415 | Constant *C1 = cast<Constant>(Op0), *C2; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1416 | Constant *Fold = nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1417 | Value *X; |
| 1418 | bool CreateDiv = true; |
| 1419 | |
| 1420 | // C1 / (X*C2) => (C1/C2) / X |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1421 | if (match(Op1, m_FMul(m_Value(X), m_Constant(C2)))) |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1422 | Fold = ConstantExpr::getFDiv(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1423 | else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1424 | // C1 / (X/C2) => (C1*C2) / X |
| 1425 | Fold = ConstantExpr::getFMul(C1, C2); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1426 | } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) { |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1427 | // C1 / (C2/X) => (C1/C2) * X |
| 1428 | Fold = ConstantExpr::getFDiv(C1, C2); |
| 1429 | CreateDiv = false; |
| 1430 | } |
| 1431 | |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1432 | if (Fold && isNormalFp(Fold)) { |
| 1433 | Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X) |
| 1434 | : BinaryOperator::CreateFMul(X, Fold); |
| 1435 | R->setFastMathFlags(I.getFastMathFlags()); |
| 1436 | return R; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1437 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1438 | return nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | if (AllowReassociate) { |
| 1442 | Value *X, *Y; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1443 | Value *NewInst = nullptr; |
| 1444 | Instruction *SimpR = nullptr; |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) { |
| 1447 | // (X/Y) / Z => X / (Y*Z) |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1448 | if (!isa<Constant>(Y) || !isa<Constant>(Op1)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1449 | NewInst = Builder.CreateFMul(Y, Op1); |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 1450 | if (Instruction *RI = dyn_cast<Instruction>(NewInst)) { |
| 1451 | FastMathFlags Flags = I.getFastMathFlags(); |
| 1452 | Flags &= cast<Instruction>(Op0)->getFastMathFlags(); |
| 1453 | RI->setFastMathFlags(Flags); |
| 1454 | } |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1455 | SimpR = BinaryOperator::CreateFDiv(X, NewInst); |
| 1456 | } |
| 1457 | } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) { |
| 1458 | // Z / (X/Y) => Z*Y / X |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1459 | if (!isa<Constant>(Y) || !isa<Constant>(Op0)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1460 | NewInst = Builder.CreateFMul(Op0, Y); |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 1461 | if (Instruction *RI = dyn_cast<Instruction>(NewInst)) { |
| 1462 | FastMathFlags Flags = I.getFastMathFlags(); |
| 1463 | Flags &= cast<Instruction>(Op1)->getFastMathFlags(); |
| 1464 | RI->setFastMathFlags(Flags); |
| 1465 | } |
Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1466 | SimpR = BinaryOperator::CreateFDiv(NewInst, X); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | if (NewInst) { |
| 1471 | if (Instruction *T = dyn_cast<Instruction>(NewInst)) |
| 1472 | T->setDebugLoc(I.getDebugLoc()); |
| 1473 | SimpR->setFastMathFlags(I.getFastMathFlags()); |
| 1474 | return SimpR; |
Benjamin Kramer | 8564e0d | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | |
Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 1478 | if (AllowReassociate && |
| 1479 | Op0->hasOneUse() && Op1->hasOneUse()) { |
| 1480 | Value *A; |
| 1481 | // sin(a) / cos(a) -> tan(a) |
| 1482 | if (match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(A))) && |
| 1483 | match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(A)))) { |
| 1484 | if (hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan, |
| 1485 | LibFunc_tanf, LibFunc_tanl)) { |
| 1486 | IRBuilder<> B(&I); |
| 1487 | IRBuilder<>::FastMathFlagGuard Guard(B); |
| 1488 | B.setFastMathFlags(I.getFastMathFlags()); |
Benjamin Kramer | 738e6e7 | 2018-01-11 15:33:21 +0000 | [diff] [blame] | 1489 | Value *Tan = emitUnaryFloatFnCall( |
| 1490 | A, TLI.getName(LibFunc_tan), B, |
| 1491 | CallSite(Op0).getCalledFunction()->getAttributes()); |
Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 1492 | return replaceInstUsesWith(I, Tan); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | // cos(a) / sin(a) -> 1/tan(a) |
| 1497 | if (match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(A))) && |
| 1498 | match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(A)))) { |
| 1499 | if (hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan, |
| 1500 | LibFunc_tanf, LibFunc_tanl)) { |
| 1501 | IRBuilder<> B(&I); |
| 1502 | IRBuilder<>::FastMathFlagGuard Guard(B); |
| 1503 | B.setFastMathFlags(I.getFastMathFlags()); |
Benjamin Kramer | 44993ed | 2018-01-11 15:19:02 +0000 | [diff] [blame] | 1504 | Value *Tan = emitUnaryFloatFnCall( |
| 1505 | A, TLI.getName(LibFunc_tan), B, |
| 1506 | CallSite(Op0).getCalledFunction()->getAttributes()); |
Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 1507 | Value *One = ConstantFP::get(Tan->getType(), 1.0); |
| 1508 | Value *Div = B.CreateFDiv(One, Tan); |
| 1509 | return replaceInstUsesWith(I, Div); |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 1514 | // -X / -Y -> X / Y |
| 1515 | Value *X, *Y; |
| 1516 | if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) { |
| 1517 | I.setOperand(0, X); |
| 1518 | I.setOperand(1, Y); |
Matt Arsenault | fdb78f8 | 2017-01-10 23:08:54 +0000 | [diff] [blame] | 1519 | return &I; |
| 1520 | } |
| 1521 | |
Sanjay Patel | 4a4f35f | 2018-02-12 19:39:21 +0000 | [diff] [blame] | 1522 | // X / (X * Y) --> 1.0 / Y |
| 1523 | // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed. |
| 1524 | // We can ignore the possibility that X is infinity because INF/INF is NaN. |
| 1525 | if (I.hasNoNaNs() && I.hasAllowReassoc() && |
| 1526 | match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) { |
| 1527 | I.setOperand(0, ConstantFP::get(I.getType(), 1.0)); |
| 1528 | I.setOperand(1, Y); |
| 1529 | return &I; |
| 1530 | } |
| 1531 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1532 | return nullptr; |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1535 | /// This function implements the transforms common to both integer remainder |
| 1536 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 1537 | /// remainder instructions. |
| 1538 | /// @brief Common integer remainder transforms |
| 1539 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 1540 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1541 | |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 1542 | // The RHS is known non-zero. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1543 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { |
Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 1544 | I.setOperand(1, V); |
| 1545 | return &I; |
| 1546 | } |
| 1547 | |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1548 | // Handle cases involving: rem X, (select Cond, Y, Z) |
Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 1549 | if (simplifyDivRemOfSelectWithZeroOp(I)) |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1550 | return &I; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1551 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1552 | if (isa<Constant>(Op1)) { |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1553 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1554 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 1555 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1556 | return R; |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1557 | } else if (auto *PN = dyn_cast<PHINode>(Op0I)) { |
Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1558 | const APInt *Op1Int; |
| 1559 | if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() && |
| 1560 | (I.getOpcode() == Instruction::URem || |
| 1561 | !Op1Int->isMinSignedValue())) { |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1562 | // foldOpIntoPhi will speculate instructions to the end of the PHI's |
Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1563 | // predecessor blocks, so do this only if we know the srem or urem |
| 1564 | // will not fault. |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1565 | if (Instruction *NV = foldOpIntoPhi(I, PN)) |
Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1566 | return NV; |
| 1567 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | // See if we can fold away this rem instruction. |
| 1571 | if (SimplifyDemandedInstructionBits(I)) |
| 1572 | return &I; |
| 1573 | } |
| 1574 | } |
| 1575 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1576 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 1580 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1581 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1582 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1583 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1584 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1585 | if (Value *V = SimplifyURemInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1586 | return replaceInstUsesWith(I, V); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1587 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1588 | if (Instruction *common = commonIRemTransforms(I)) |
| 1589 | return common; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1590 | |
Sanjay Patel | bb78938 | 2017-08-24 22:54:01 +0000 | [diff] [blame] | 1591 | if (Instruction *NarrowRem = narrowUDivURem(I, Builder)) |
| 1592 | return NarrowRem; |
David Majnemer | 6c30f49 | 2013-05-12 00:07:05 +0000 | [diff] [blame] | 1593 | |
David Majnemer | 470b077 | 2013-05-11 09:01:28 +0000 | [diff] [blame] | 1594 | // 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] | 1595 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 1596 | Constant *N1 = Constant::getAllOnesValue(I.getType()); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1597 | Value *Add = Builder.CreateAdd(Op1, N1); |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 1598 | return BinaryOperator::CreateAnd(Op0, Add); |
| 1599 | } |
| 1600 | |
Nick Lewycky | 7459be6 | 2013-07-13 01:16:47 +0000 | [diff] [blame] | 1601 | // 1 urem X -> zext(X != 1) |
| 1602 | if (match(Op0, m_One())) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1603 | Value *Cmp = Builder.CreateICmpNE(Op1, Op0); |
| 1604 | Value *Ext = Builder.CreateZExt(Cmp, I.getType()); |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1605 | return replaceInstUsesWith(I, Ext); |
Nick Lewycky | 7459be6 | 2013-07-13 01:16:47 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Sanjay Patel | 30ef70b | 2016-09-22 22:36:26 +0000 | [diff] [blame] | 1608 | // X urem C -> X < C ? X : X - C, where C >= signbit. |
Simon Pilgrim | 1889f26 | 2018-02-08 18:36:01 +0000 | [diff] [blame] | 1609 | if (match(Op1, m_Negative())) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1610 | Value *Cmp = Builder.CreateICmpULT(Op0, Op1); |
| 1611 | Value *Sub = Builder.CreateSub(Op0, Op1); |
Sanjay Patel | 30ef70b | 2016-09-22 22:36:26 +0000 | [diff] [blame] | 1612 | return SelectInst::Create(Cmp, Op0, Sub); |
| 1613 | } |
| 1614 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1615 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 1619 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1620 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1621 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1622 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1623 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1624 | if (Value *V = SimplifySRemInst(Op0, Op1, SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1625 | return replaceInstUsesWith(I, V); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1626 | |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1627 | // Handle the integer rem common cases |
| 1628 | if (Instruction *Common = commonIRemTransforms(I)) |
| 1629 | return Common; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1630 | |
David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1631 | { |
| 1632 | const APInt *Y; |
| 1633 | // X % -Y -> X % Y |
Simon Pilgrim | a54e8e4 | 2018-02-08 19:00:45 +0000 | [diff] [blame] | 1634 | if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) { |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1635 | Worklist.AddValue(I.getOperand(1)); |
David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1636 | I.setOperand(1, ConstantInt::get(I.getType(), -*Y)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1637 | return &I; |
| 1638 | } |
David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1639 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1640 | |
| 1641 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1642 | // unsigned inputs), turn this into a urem. |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1643 | APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); |
Craig Topper | 1a18a7c | 2017-04-17 01:51:24 +0000 | [diff] [blame] | 1644 | if (MaskedValueIsZero(Op1, Mask, 0, &I) && |
| 1645 | MaskedValueIsZero(Op0, Mask, 0, &I)) { |
| 1646 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 1647 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | // If it's a constant vector, flip any negative values positive. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1651 | if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { |
| 1652 | Constant *C = cast<Constant>(Op1); |
| 1653 | unsigned VWidth = C->getType()->getVectorNumElements(); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1654 | |
| 1655 | bool hasNegative = false; |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1656 | bool hasMissing = false; |
| 1657 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1658 | Constant *Elt = C->getAggregateElement(i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1659 | if (!Elt) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1660 | hasMissing = true; |
| 1661 | break; |
| 1662 | } |
| 1663 | |
| 1664 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1665 | if (RHS->isNegative()) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1666 | hasNegative = true; |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1667 | } |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1669 | if (hasNegative && !hasMissing) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1670 | SmallVector<Constant *, 16> Elts(VWidth); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1671 | for (unsigned i = 0; i != VWidth; ++i) { |
Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1672 | Elts[i] = C->getAggregateElement(i); // Handle undef, etc. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1673 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { |
Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1674 | if (RHS->isNegative()) |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1675 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | Constant *NewRHSV = ConstantVector::get(Elts); |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1680 | if (NewRHSV != C) { // Don't loop on -MININT |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1681 | Worklist.AddValue(I.getOperand(1)); |
| 1682 | I.setOperand(1, NewRHSV); |
| 1683 | return &I; |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1688 | return nullptr; |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1692 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1693 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1694 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1695 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1696 | |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1697 | if (Value *V = SimplifyFRemInst(Op0, Op1, I.getFastMathFlags(), |
| 1698 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1699 | return replaceInstUsesWith(I, V); |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1700 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1701 | return nullptr; |
Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1702 | } |