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