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