1 //===- InstCombineMulDivRem.cpp -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
13 //===----------------------------------------------------------------------===//
15 #include "InstCombine.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/IR/IntrinsicInst.h"
18 #include "llvm/IR/PatternMatch.h"
20 using namespace PatternMatch;
22 #define DEBUG_TYPE "instcombine"
25 /// simplifyValueKnownNonZero - The specific integer value is used in a context
26 /// where it is known to be non-zero. If this allows us to simplify the
27 /// computation, do so and return the new operand, otherwise return null.
28 static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
30 // If V has multiple uses, then we would have to do more analysis to determine
31 // if this is safe. For example, the use could be in dynamically unreached
33 if (!V->hasOneUse()) return nullptr;
35 bool MadeChange = false;
37 // ((1 << A) >>u B) --> (1 << (A-B))
38 // Because V cannot be zero, we know that B is less than A.
39 Value *A = nullptr, *B = nullptr, *PowerOf2 = nullptr;
40 if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))),
42 // The "1" can be any value known to be a power of 2.
43 isKnownToBeAPowerOfTwo(PowerOf2, false, 0, IC.getAssumptionTracker(),
44 CxtI, IC.getDominatorTree())) {
45 A = IC.Builder->CreateSub(A, B);
46 return IC.Builder->CreateShl(PowerOf2, A);
49 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
50 // inexact. Similarly for <<.
51 if (BinaryOperator *I = dyn_cast<BinaryOperator>(V))
52 if (I->isLogicalShift() && isKnownToBeAPowerOfTwo(I->getOperand(0), false,
53 0, IC.getAssumptionTracker(),
55 IC.getDominatorTree())) {
56 // We know that this is an exact/nuw shift and that the input is a
57 // non-zero context as well.
58 if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
63 if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
68 if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
69 I->setHasNoUnsignedWrap();
74 // TODO: Lots more we could do here:
75 // If V is a phi node, we can call this on each of its operands.
76 // "select cond, X, 0" can simplify to "X".
78 return MadeChange ? V : nullptr;
82 /// MultiplyOverflows - True if the multiply can not be expressed in an int
84 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
85 uint32_t W = C1->getBitWidth();
86 APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
88 LHSExt = LHSExt.sext(W * 2);
89 RHSExt = RHSExt.sext(W * 2);
91 LHSExt = LHSExt.zext(W * 2);
92 RHSExt = RHSExt.zext(W * 2);
95 APInt MulExt = LHSExt * RHSExt;
98 return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
100 APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
101 APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
102 return MulExt.slt(Min) || MulExt.sgt(Max);
105 /// \brief True if C2 is a multiple of C1. Quotient contains C2/C1.
106 static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
108 assert(C1.getBitWidth() == C2.getBitWidth() &&
109 "Inconsistent width of constants!");
111 APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned);
113 APInt::sdivrem(C1, C2, Quotient, Remainder);
115 APInt::udivrem(C1, C2, Quotient, Remainder);
117 return Remainder.isMinValue();
120 /// \brief A helper routine of InstCombiner::visitMul().
122 /// If C is a vector of known powers of 2, then this function returns
123 /// a new vector obtained from C replacing each element with its logBase2.
124 /// Return a null pointer otherwise.
125 static Constant *getLogBase2Vector(ConstantDataVector *CV) {
127 SmallVector<Constant *, 4> Elts;
129 for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
130 Constant *Elt = CV->getElementAsConstant(I);
131 if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
133 Elts.push_back(ConstantInt::get(Elt->getType(), IVal->logBase2()));
136 return ConstantVector::get(Elts);
139 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
140 bool Changed = SimplifyAssociativeOrCommutative(I);
141 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
143 if (Value *V = SimplifyVectorOp(I))
144 return ReplaceInstUsesWith(I, V);
146 if (Value *V = SimplifyMulInst(Op0, Op1, DL, TLI, DT, AT))
147 return ReplaceInstUsesWith(I, V);
149 if (Value *V = SimplifyUsingDistributiveLaws(I))
150 return ReplaceInstUsesWith(I, V);
152 if (match(Op1, m_AllOnes())) // X * -1 == 0 - X
153 return BinaryOperator::CreateNeg(Op0, I.getName());
155 // Also allow combining multiply instructions on vectors.
160 if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
162 match(C1, m_APInt(IVal)))
163 // ((X << C1)*C2) == (X * (C2 << C1))
164 return BinaryOperator::CreateMul(NewOp, ConstantExpr::getShl(C1, C2));
166 if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
167 Constant *NewCst = nullptr;
168 if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2())
169 // Replace X*(2^C) with X << C, where C is either a scalar or a splat.
170 NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2());
171 else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1))
172 // Replace X*(2^C) with X << C, where C is a vector of known
173 // constant powers of 2.
174 NewCst = getLogBase2Vector(CV);
177 BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
179 if (I.hasNoSignedWrap())
180 Shl->setHasNoSignedWrap();
181 if (I.hasNoUnsignedWrap())
182 Shl->setHasNoUnsignedWrap();
189 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
190 // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
191 // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
192 // The "* (2**n)" thus becomes a potential shifting opportunity.
194 const APInt & Val = CI->getValue();
195 const APInt &PosVal = Val.abs();
196 if (Val.isNegative() && PosVal.isPowerOf2()) {
197 Value *X = nullptr, *Y = nullptr;
198 if (Op0->hasOneUse()) {
200 Value *Sub = nullptr;
201 if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
202 Sub = Builder->CreateSub(X, Y, "suba");
203 else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
204 Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc");
207 BinaryOperator::CreateMul(Sub,
208 ConstantInt::get(Y->getType(), PosVal));
214 // Simplify mul instructions with a constant RHS.
215 if (isa<Constant>(Op1)) {
216 // Try to fold constant mul into select arguments.
217 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
218 if (Instruction *R = FoldOpIntoSelect(I, SI))
221 if (isa<PHINode>(Op0))
222 if (Instruction *NV = FoldOpIntoPhi(I))
225 // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
229 if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
230 Value *Mul = Builder->CreateMul(C1, Op1);
231 // Only go forward with the transform if C1*CI simplifies to a tidier
233 if (!match(Mul, m_Mul(m_Value(), m_Value())))
234 return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul);
239 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
240 if (Value *Op1v = dyn_castNegVal(Op1))
241 return BinaryOperator::CreateMul(Op0v, Op1v);
243 // (X / Y) * Y = X - (X % Y)
244 // (X / Y) * -Y = (X % Y) - X
247 BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
249 (BO->getOpcode() != Instruction::UDiv &&
250 BO->getOpcode() != Instruction::SDiv)) {
252 BO = dyn_cast<BinaryOperator>(Op1);
254 Value *Neg = dyn_castNegVal(Op1C);
255 if (BO && BO->hasOneUse() &&
256 (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) &&
257 (BO->getOpcode() == Instruction::UDiv ||
258 BO->getOpcode() == Instruction::SDiv)) {
259 Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
261 // If the division is exact, X % Y is zero, so we end up with X or -X.
262 if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO))
263 if (SDiv->isExact()) {
265 return ReplaceInstUsesWith(I, Op0BO);
266 return BinaryOperator::CreateNeg(Op0BO);
270 if (BO->getOpcode() == Instruction::UDiv)
271 Rem = Builder->CreateURem(Op0BO, Op1BO);
273 Rem = Builder->CreateSRem(Op0BO, Op1BO);
277 return BinaryOperator::CreateSub(Op0BO, Rem);
278 return BinaryOperator::CreateSub(Rem, Op0BO);
282 /// i1 mul -> i1 and.
283 if (I.getType()->getScalarType()->isIntegerTy(1))
284 return BinaryOperator::CreateAnd(Op0, Op1);
286 // X*(1 << Y) --> X << Y
287 // (1 << Y)*X --> X << Y
290 if (match(Op0, m_Shl(m_One(), m_Value(Y))))
291 return BinaryOperator::CreateShl(Op1, Y);
292 if (match(Op1, m_Shl(m_One(), m_Value(Y))))
293 return BinaryOperator::CreateShl(Op0, Y);
296 // If one of the operands of the multiply is a cast from a boolean value, then
297 // we know the bool is either zero or one, so this is a 'masking' multiply.
298 // X * Y (where Y is 0 or 1) -> X & (0-Y)
299 if (!I.getType()->isVectorTy()) {
300 // -2 is "-1 << 1" so it is all bits set except the low one.
301 APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true);
303 Value *BoolCast = nullptr, *OtherOp = nullptr;
304 if (MaskedValueIsZero(Op0, Negative2, 0, &I))
305 BoolCast = Op0, OtherOp = Op1;
306 else if (MaskedValueIsZero(Op1, Negative2, 0, &I))
307 BoolCast = Op1, OtherOp = Op0;
310 Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()),
312 return BinaryOperator::CreateAnd(V, OtherOp);
316 return Changed ? &I : nullptr;
324 // And check for corresponding fast math flags
327 static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) {
329 if (!Op->hasOneUse())
332 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op);
335 if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra())
339 Value *OpLog2Of = II->getArgOperand(0);
340 if (!OpLog2Of->hasOneUse())
343 Instruction *I = dyn_cast<Instruction>(OpLog2Of);
346 if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra())
349 if (match(I->getOperand(0), m_SpecificFP(0.5)))
350 Y = I->getOperand(1);
351 else if (match(I->getOperand(1), m_SpecificFP(0.5)))
352 Y = I->getOperand(0);
355 static bool isFiniteNonZeroFp(Constant *C) {
356 if (C->getType()->isVectorTy()) {
357 for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
359 ConstantFP *CFP = dyn_cast<ConstantFP>(C->getAggregateElement(I));
360 if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
366 return isa<ConstantFP>(C) &&
367 cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero();
370 static bool isNormalFp(Constant *C) {
371 if (C->getType()->isVectorTy()) {
372 for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
374 ConstantFP *CFP = dyn_cast<ConstantFP>(C->getAggregateElement(I));
375 if (!CFP || !CFP->getValueAPF().isNormal())
381 return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal();
384 /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns
385 /// true iff the given value is FMul or FDiv with one and only one operand
386 /// being a normal constant (i.e. not Zero/NaN/Infinity).
387 static bool isFMulOrFDivWithConstant(Value *V) {
388 Instruction *I = dyn_cast<Instruction>(V);
389 if (!I || (I->getOpcode() != Instruction::FMul &&
390 I->getOpcode() != Instruction::FDiv))
393 Constant *C0 = dyn_cast<Constant>(I->getOperand(0));
394 Constant *C1 = dyn_cast<Constant>(I->getOperand(1));
399 return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1));
402 /// foldFMulConst() is a helper routine of InstCombiner::visitFMul().
403 /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand
404 /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true).
405 /// This function is to simplify "FMulOrDiv * C" and returns the
406 /// resulting expression. Note that this function could return NULL in
407 /// case the constants cannot be folded into a normal floating-point.
409 Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C,
410 Instruction *InsertBefore) {
411 assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid");
413 Value *Opnd0 = FMulOrDiv->getOperand(0);
414 Value *Opnd1 = FMulOrDiv->getOperand(1);
416 Constant *C0 = dyn_cast<Constant>(Opnd0);
417 Constant *C1 = dyn_cast<Constant>(Opnd1);
419 BinaryOperator *R = nullptr;
421 // (X * C0) * C => X * (C0*C)
422 if (FMulOrDiv->getOpcode() == Instruction::FMul) {
423 Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C);
425 R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
428 // (C0 / X) * C => (C0 * C) / X
429 if (FMulOrDiv->hasOneUse()) {
430 // It would otherwise introduce another div.
431 Constant *F = ConstantExpr::getFMul(C0, C);
433 R = BinaryOperator::CreateFDiv(F, Opnd1);
436 // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal
437 Constant *F = ConstantExpr::getFDiv(C, C1);
439 R = BinaryOperator::CreateFMul(Opnd0, F);
441 // (X / C1) * C => X / (C1/C)
442 Constant *F = ConstantExpr::getFDiv(C1, C);
444 R = BinaryOperator::CreateFDiv(Opnd0, F);
450 R->setHasUnsafeAlgebra(true);
451 InsertNewInstWith(R, *InsertBefore);
457 Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
458 bool Changed = SimplifyAssociativeOrCommutative(I);
459 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
461 if (Value *V = SimplifyVectorOp(I))
462 return ReplaceInstUsesWith(I, V);
464 if (isa<Constant>(Op0))
467 if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), DL, TLI,
469 return ReplaceInstUsesWith(I, V);
471 bool AllowReassociate = I.hasUnsafeAlgebra();
473 // Simplify mul instructions with a constant RHS.
474 if (isa<Constant>(Op1)) {
475 // Try to fold constant mul into select arguments.
476 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
477 if (Instruction *R = FoldOpIntoSelect(I, SI))
480 if (isa<PHINode>(Op0))
481 if (Instruction *NV = FoldOpIntoPhi(I))
484 // (fmul X, -1.0) --> (fsub -0.0, X)
485 if (match(Op1, m_SpecificFP(-1.0))) {
486 Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType());
487 Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
488 RI->copyFastMathFlags(&I);
492 Constant *C = cast<Constant>(Op1);
493 if (AllowReassociate && isFiniteNonZeroFp(C)) {
494 // Let MDC denote an expression in one of these forms:
495 // X * C, C/X, X/C, where C is a constant.
497 // Try to simplify "MDC * Constant"
498 if (isFMulOrFDivWithConstant(Op0))
499 if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
500 return ReplaceInstUsesWith(I, V);
502 // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C)
503 Instruction *FAddSub = dyn_cast<Instruction>(Op0);
505 (FAddSub->getOpcode() == Instruction::FAdd ||
506 FAddSub->getOpcode() == Instruction::FSub)) {
507 Value *Opnd0 = FAddSub->getOperand(0);
508 Value *Opnd1 = FAddSub->getOperand(1);
509 Constant *C0 = dyn_cast<Constant>(Opnd0);
510 Constant *C1 = dyn_cast<Constant>(Opnd1);
514 std::swap(Opnd0, Opnd1);
518 if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) {
519 Value *M1 = ConstantExpr::getFMul(C1, C);
520 Value *M0 = isNormalFp(cast<Constant>(M1)) ?
521 foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
524 if (Swap && FAddSub->getOpcode() == Instruction::FSub)
527 Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd)
528 ? BinaryOperator::CreateFAdd(M0, M1)
529 : BinaryOperator::CreateFSub(M0, M1);
530 RI->copyFastMathFlags(&I);
538 // sqrt(X) * sqrt(X) -> X
539 if (AllowReassociate && (Op0 == Op1))
540 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0))
541 if (II->getIntrinsicID() == Intrinsic::sqrt)
542 return ReplaceInstUsesWith(I, II->getOperand(0));
544 // Under unsafe algebra do:
545 // X * log2(0.5*Y) = X*log2(Y) - X
546 if (AllowReassociate) {
547 Value *OpX = nullptr;
548 Value *OpY = nullptr;
550 detectLog2OfHalf(Op0, OpY, Log2);
554 detectLog2OfHalf(Op1, OpY, Log2);
559 // if pattern detected emit alternate sequence
561 BuilderTy::FastMathFlagGuard Guard(*Builder);
562 Builder->SetFastMathFlags(Log2->getFastMathFlags());
563 Log2->setArgOperand(0, OpY);
564 Value *FMulVal = Builder->CreateFMul(OpX, Log2);
565 Value *FSub = Builder->CreateFSub(FMulVal, OpX);
567 return ReplaceInstUsesWith(I, FSub);
571 // Handle symmetric situation in a 2-iteration loop
574 for (int i = 0; i < 2; i++) {
575 bool IgnoreZeroSign = I.hasNoSignedZeros();
576 if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) {
577 BuilderTy::FastMathFlagGuard Guard(*Builder);
578 Builder->SetFastMathFlags(I.getFastMathFlags());
580 Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
581 Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
585 Value *FMul = Builder->CreateFMul(N0, N1);
587 return ReplaceInstUsesWith(I, FMul);
590 if (Opnd0->hasOneUse()) {
591 // -X * Y => -(X*Y) (Promote negation as high as possible)
592 Value *T = Builder->CreateFMul(N0, Opnd1);
593 Value *Neg = Builder->CreateFNeg(T);
595 return ReplaceInstUsesWith(I, Neg);
599 // (X*Y) * X => (X*X) * Y where Y != X
600 // The purpose is two-fold:
601 // 1) to form a power expression (of X).
602 // 2) potentially shorten the critical path: After transformation, the
603 // latency of the instruction Y is amortized by the expression of X*X,
604 // and therefore Y is in a "less critical" position compared to what it
605 // was before the transformation.
607 if (AllowReassociate) {
608 Value *Opnd0_0, *Opnd0_1;
609 if (Opnd0->hasOneUse() &&
610 match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) {
612 if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
614 else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
618 BuilderTy::FastMathFlagGuard Guard(*Builder);
619 Builder->SetFastMathFlags(I.getFastMathFlags());
620 Value *T = Builder->CreateFMul(Opnd1, Opnd1);
622 Value *R = Builder->CreateFMul(T, Y);
624 return ReplaceInstUsesWith(I, R);
629 if (!isa<Constant>(Op1))
630 std::swap(Opnd0, Opnd1);
635 return Changed ? &I : nullptr;
638 /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
640 bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
641 SelectInst *SI = cast<SelectInst>(I.getOperand(1));
643 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
644 int NonNullOperand = -1;
645 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
646 if (ST->isNullValue())
648 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
649 if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
650 if (ST->isNullValue())
653 if (NonNullOperand == -1)
656 Value *SelectCond = SI->getOperand(0);
658 // Change the div/rem to use 'Y' instead of the select.
659 I.setOperand(1, SI->getOperand(NonNullOperand));
661 // Okay, we know we replace the operand of the div/rem with 'Y' with no
662 // problem. However, the select, or the condition of the select may have
663 // multiple uses. Based on our knowledge that the operand must be non-zero,
664 // propagate the known value for the select into other uses of it, and
665 // propagate a known value of the condition into its other users.
667 // If the select and condition only have a single use, don't bother with this,
669 if (SI->use_empty() && SelectCond->hasOneUse())
672 // Scan the current block backward, looking for other uses of SI.
673 BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
675 while (BBI != BBFront) {
677 // If we found a call to a function, we can't assume it will return, so
678 // information from below it cannot be propagated above it.
679 if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
682 // Replace uses of the select or its condition with the known values.
683 for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
686 *I = SI->getOperand(NonNullOperand);
688 } else if (*I == SelectCond) {
689 *I = Builder->getInt1(NonNullOperand == 1);
694 // If we past the instruction, quit looking for it.
697 if (&*BBI == SelectCond)
698 SelectCond = nullptr;
700 // If we ran out of things to eliminate, break out of the loop.
701 if (!SelectCond && !SI)
709 /// This function implements the transforms common to both integer division
710 /// instructions (udiv and sdiv). It is called by the visitors to those integer
711 /// division instructions.
712 /// @brief Common integer divide transforms
713 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
714 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
716 // The RHS is known non-zero.
717 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, &I)) {
722 // Handle cases involving: [su]div X, (select Cond, Y, Z)
723 // This does not apply for fdiv.
724 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
727 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
728 if (Instruction *LHS = dyn_cast<Instruction>(Op0)) {
729 // (X / C1) / C2 -> X / (C1*C2)
730 if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
731 if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
732 if (MultiplyOverflows(RHS, LHSRHS,
733 I.getOpcode() == Instruction::SDiv))
734 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
735 return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
736 ConstantExpr::getMul(RHS, LHSRHS));
740 const APInt *C1, *C2;
741 if (match(RHS, m_APInt(C2))) {
742 bool IsSigned = I.getOpcode() == Instruction::SDiv;
743 if ((IsSigned && match(LHS, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
744 (!IsSigned && match(LHS, m_NUWMul(m_Value(X), m_APInt(C1))))) {
745 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
747 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
748 if (IsMultiple(*C2, *C1, Quotient, IsSigned)) {
749 BinaryOperator *BO = BinaryOperator::Create(
750 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
751 BO->setIsExact(I.isExact());
755 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
756 if (IsMultiple(*C1, *C2, Quotient, IsSigned)) {
757 BinaryOperator *BO = BinaryOperator::Create(
758 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
759 BO->setHasNoUnsignedWrap(
761 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
762 BO->setHasNoSignedWrap(
763 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
768 if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1)))) ||
769 (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) {
770 APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
771 APInt C1Shifted = APInt::getOneBitSet(
772 C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue()));
774 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1.
775 if (IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
776 BinaryOperator *BO = BinaryOperator::Create(
777 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
778 BO->setIsExact(I.isExact());
782 // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2.
783 if (IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
784 BinaryOperator *BO = BinaryOperator::Create(
785 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
786 BO->setHasNoUnsignedWrap(
788 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
789 BO->setHasNoSignedWrap(
790 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
797 if (!RHS->isZero()) { // avoid X udiv 0
798 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
799 if (Instruction *R = FoldOpIntoSelect(I, SI))
801 if (isa<PHINode>(Op0))
802 if (Instruction *NV = FoldOpIntoPhi(I))
807 if (ConstantInt *One = dyn_cast<ConstantInt>(Op0)) {
808 if (One->isOne() && !I.getType()->isIntegerTy(1)) {
809 bool isSigned = I.getOpcode() == Instruction::SDiv;
811 // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
812 // result is one, if Op1 is -1 then the result is minus one, otherwise
814 Value *Inc = Builder->CreateAdd(Op1, One);
815 Value *Cmp = Builder->CreateICmpULT(
816 Inc, ConstantInt::get(I.getType(), 3));
817 return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0));
819 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
820 // result is one, otherwise it's zero.
821 return new ZExtInst(Builder->CreateICmpEQ(Op1, One), I.getType());
826 // See if we can fold away this div instruction.
827 if (SimplifyDemandedInstructionBits(I))
830 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
831 Value *X = nullptr, *Z = nullptr;
832 if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1
833 bool isSigned = I.getOpcode() == Instruction::SDiv;
834 if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
835 (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
836 return BinaryOperator::Create(I.getOpcode(), X, Op1);
842 /// dyn_castZExtVal - Checks if V is a zext or constant that can
843 /// be truncated to Ty without losing bits.
844 static Value *dyn_castZExtVal(Value *V, Type *Ty) {
845 if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) {
846 if (Z->getSrcTy() == Ty)
847 return Z->getOperand(0);
848 } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) {
849 if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth())
850 return ConstantExpr::getTrunc(C, Ty);
856 const unsigned MaxDepth = 6;
857 typedef Instruction *(*FoldUDivOperandCb)(Value *Op0, Value *Op1,
858 const BinaryOperator &I,
861 /// \brief Used to maintain state for visitUDivOperand().
862 struct UDivFoldAction {
863 FoldUDivOperandCb FoldAction; ///< Informs visitUDiv() how to fold this
864 ///< operand. This can be zero if this action
865 ///< joins two actions together.
867 Value *OperandToFold; ///< Which operand to fold.
869 Instruction *FoldResult; ///< The instruction returned when FoldAction is
872 size_t SelectLHSIdx; ///< Stores the LHS action index if this action
873 ///< joins two actions together.
876 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
877 : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
878 UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
879 : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
883 // X udiv 2^C -> X >> C
884 static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
885 const BinaryOperator &I, InstCombiner &IC) {
886 const APInt &C = cast<Constant>(Op1)->getUniqueInteger();
887 BinaryOperator *LShr = BinaryOperator::CreateLShr(
888 Op0, ConstantInt::get(Op0->getType(), C.logBase2()));
894 // X udiv C, where C >= signbit
895 static Instruction *foldUDivNegCst(Value *Op0, Value *Op1,
896 const BinaryOperator &I, InstCombiner &IC) {
897 Value *ICI = IC.Builder->CreateICmpULT(Op0, cast<ConstantInt>(Op1));
899 return SelectInst::Create(ICI, Constant::getNullValue(I.getType()),
900 ConstantInt::get(I.getType(), 1));
903 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
904 static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
906 Instruction *ShiftLeft = cast<Instruction>(Op1);
907 if (isa<ZExtInst>(ShiftLeft))
908 ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0));
911 cast<Constant>(ShiftLeft->getOperand(0))->getUniqueInteger();
912 Value *N = ShiftLeft->getOperand(1);
914 N = IC.Builder->CreateAdd(N, ConstantInt::get(N->getType(), CI.logBase2()));
915 if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1))
916 N = IC.Builder->CreateZExt(N, Z->getDestTy());
917 BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
923 // \brief Recursively visits the possible right hand operands of a udiv
924 // instruction, seeing through select instructions, to determine if we can
925 // replace the udiv with something simpler. If we find that an operand is not
926 // able to simplify the udiv, we abort the entire transformation.
927 static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
928 SmallVectorImpl<UDivFoldAction> &Actions,
929 unsigned Depth = 0) {
930 // Check to see if this is an unsigned division with an exact power of 2,
931 // if so, convert to a right shift.
932 if (match(Op1, m_Power2())) {
933 Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
934 return Actions.size();
937 if (ConstantInt *C = dyn_cast<ConstantInt>(Op1))
938 // X udiv C, where C >= signbit
939 if (C->getValue().isNegative()) {
940 Actions.push_back(UDivFoldAction(foldUDivNegCst, C));
941 return Actions.size();
944 // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
945 if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
946 match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
947 Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
948 return Actions.size();
951 // The remaining tests are all recursive, so bail out if we hit the limit.
952 if (Depth++ == MaxDepth)
955 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
957 visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
958 if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
959 Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
960 return Actions.size();
966 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
967 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
969 if (Value *V = SimplifyVectorOp(I))
970 return ReplaceInstUsesWith(I, V);
972 if (Value *V = SimplifyUDivInst(Op0, Op1, DL, TLI, DT, AT))
973 return ReplaceInstUsesWith(I, V);
975 // Handle the integer div common cases
976 if (Instruction *Common = commonIDivTransforms(I))
979 // (x lshr C1) udiv C2 --> x udiv (C2 << C1)
980 if (Constant *C2 = dyn_cast<Constant>(Op1)) {
983 if (match(Op0, m_LShr(m_Value(X), m_Constant(C1))))
984 return BinaryOperator::CreateUDiv(X, ConstantExpr::getShl(C2, C1));
987 // (zext A) udiv (zext B) --> zext (A udiv B)
988 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
989 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
991 Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", I.isExact()),
994 // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
995 SmallVector<UDivFoldAction, 6> UDivActions;
996 if (visitUDivOperand(Op0, Op1, I, UDivActions))
997 for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
998 FoldUDivOperandCb Action = UDivActions[i].FoldAction;
999 Value *ActionOp1 = UDivActions[i].OperandToFold;
1002 Inst = Action(Op0, ActionOp1, I, *this);
1004 // This action joins two actions together. The RHS of this action is
1005 // simply the last action we processed, we saved the LHS action index in
1006 // the joining action.
1007 size_t SelectRHSIdx = i - 1;
1008 Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
1009 size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
1010 Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
1011 Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
1012 SelectLHS, SelectRHS);
1015 // If this is the last action to process, return it to the InstCombiner.
1016 // Otherwise, we insert it before the UDiv and record it so that we may
1017 // use it as part of a joining action (i.e., a SelectInst).
1019 Inst->insertBefore(&I);
1020 UDivActions[i].FoldResult = Inst;
1028 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
1029 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1031 if (Value *V = SimplifyVectorOp(I))
1032 return ReplaceInstUsesWith(I, V);
1034 if (Value *V = SimplifySDivInst(Op0, Op1, DL, TLI, DT, AT))
1035 return ReplaceInstUsesWith(I, V);
1037 // Handle the integer div common cases
1038 if (Instruction *Common = commonIDivTransforms(I))
1042 if (match(Op1, m_AllOnes()))
1043 return BinaryOperator::CreateNeg(Op0);
1045 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
1046 // sdiv X, C --> ashr exact X, log2(C)
1047 if (I.isExact() && RHS->getValue().isNonNegative() &&
1048 RHS->getValue().isPowerOf2()) {
1049 Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
1050 RHS->getValue().exactLogBase2());
1051 return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
1055 if (Constant *RHS = dyn_cast<Constant>(Op1)) {
1056 // X/INT_MIN -> X == INT_MIN
1057 if (RHS->isMinSignedValue())
1058 return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType());
1060 // -X/C --> X/-C provided the negation doesn't overflow.
1061 if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
1062 if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap())
1063 return BinaryOperator::CreateSDiv(Sub->getOperand(1),
1064 ConstantExpr::getNeg(RHS));
1067 // If the sign bits of both operands are zero (i.e. we can prove they are
1068 // unsigned inputs), turn this into a udiv.
1069 if (I.getType()->isIntegerTy()) {
1070 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
1071 if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
1072 if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
1073 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
1074 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1077 if (match(Op1, m_Shl(m_Power2(), m_Value()))) {
1078 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1079 // Safe because the only negative value (1 << Y) can take on is
1080 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1081 // the sign bit set.
1082 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1090 /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special
1092 /// 1) 1/C is exact, or
1093 /// 2) reciprocal is allowed.
1094 /// If the conversion was successful, the simplified expression "X * 1/C" is
1095 /// returned; otherwise, NULL is returned.
1097 static Instruction *CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor,
1098 bool AllowReciprocal) {
1099 if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors.
1102 const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
1103 APFloat Reciprocal(FpVal.getSemantics());
1104 bool Cvt = FpVal.getExactInverse(&Reciprocal);
1106 if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) {
1107 Reciprocal = APFloat(FpVal.getSemantics(), 1.0f);
1108 (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven);
1109 Cvt = !Reciprocal.isDenormal();
1116 R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal);
1117 return BinaryOperator::CreateFMul(Dividend, R);
1120 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
1121 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1123 if (Value *V = SimplifyVectorOp(I))
1124 return ReplaceInstUsesWith(I, V);
1126 if (Value *V = SimplifyFDivInst(Op0, Op1, DL, TLI, DT, AT))
1127 return ReplaceInstUsesWith(I, V);
1129 if (isa<Constant>(Op0))
1130 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1131 if (Instruction *R = FoldOpIntoSelect(I, SI))
1134 bool AllowReassociate = I.hasUnsafeAlgebra();
1135 bool AllowReciprocal = I.hasAllowReciprocal();
1137 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
1138 if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1139 if (Instruction *R = FoldOpIntoSelect(I, SI))
1142 if (AllowReassociate) {
1143 Constant *C1 = nullptr;
1144 Constant *C2 = Op1C;
1146 Instruction *Res = nullptr;
1148 if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) {
1149 // (X*C1)/C2 => X * (C1/C2)
1151 Constant *C = ConstantExpr::getFDiv(C1, C2);
1153 Res = BinaryOperator::CreateFMul(X, C);
1154 } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
1155 // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed]
1157 Constant *C = ConstantExpr::getFMul(C1, C2);
1158 if (isNormalFp(C)) {
1159 Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal);
1161 Res = BinaryOperator::CreateFDiv(X, C);
1166 Res->setFastMathFlags(I.getFastMathFlags());
1172 if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) {
1173 T->copyFastMathFlags(&I);
1180 if (AllowReassociate && isa<Constant>(Op0)) {
1181 Constant *C1 = cast<Constant>(Op0), *C2;
1182 Constant *Fold = nullptr;
1184 bool CreateDiv = true;
1186 // C1 / (X*C2) => (C1/C2) / X
1187 if (match(Op1, m_FMul(m_Value(X), m_Constant(C2))))
1188 Fold = ConstantExpr::getFDiv(C1, C2);
1189 else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) {
1190 // C1 / (X/C2) => (C1*C2) / X
1191 Fold = ConstantExpr::getFMul(C1, C2);
1192 } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) {
1193 // C1 / (C2/X) => (C1/C2) * X
1194 Fold = ConstantExpr::getFDiv(C1, C2);
1198 if (Fold && isNormalFp(Fold)) {
1199 Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
1200 : BinaryOperator::CreateFMul(X, Fold);
1201 R->setFastMathFlags(I.getFastMathFlags());
1207 if (AllowReassociate) {
1209 Value *NewInst = nullptr;
1210 Instruction *SimpR = nullptr;
1212 if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) {
1213 // (X/Y) / Z => X / (Y*Z)
1215 if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
1216 NewInst = Builder->CreateFMul(Y, Op1);
1217 if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1218 FastMathFlags Flags = I.getFastMathFlags();
1219 Flags &= cast<Instruction>(Op0)->getFastMathFlags();
1220 RI->setFastMathFlags(Flags);
1222 SimpR = BinaryOperator::CreateFDiv(X, NewInst);
1224 } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) {
1225 // Z / (X/Y) => Z*Y / X
1227 if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
1228 NewInst = Builder->CreateFMul(Op0, Y);
1229 if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
1230 FastMathFlags Flags = I.getFastMathFlags();
1231 Flags &= cast<Instruction>(Op1)->getFastMathFlags();
1232 RI->setFastMathFlags(Flags);
1234 SimpR = BinaryOperator::CreateFDiv(NewInst, X);
1239 if (Instruction *T = dyn_cast<Instruction>(NewInst))
1240 T->setDebugLoc(I.getDebugLoc());
1241 SimpR->setFastMathFlags(I.getFastMathFlags());
1249 /// This function implements the transforms common to both integer remainder
1250 /// instructions (urem and srem). It is called by the visitors to those integer
1251 /// remainder instructions.
1252 /// @brief Common integer remainder transforms
1253 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
1254 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1256 // The RHS is known non-zero.
1257 if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, &I)) {
1262 // Handle cases involving: rem X, (select Cond, Y, Z)
1263 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
1266 if (isa<Constant>(Op1)) {
1267 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1268 if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1269 if (Instruction *R = FoldOpIntoSelect(I, SI))
1271 } else if (isa<PHINode>(Op0I)) {
1272 if (Instruction *NV = FoldOpIntoPhi(I))
1276 // See if we can fold away this rem instruction.
1277 if (SimplifyDemandedInstructionBits(I))
1285 Instruction *InstCombiner::visitURem(BinaryOperator &I) {
1286 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1288 if (Value *V = SimplifyVectorOp(I))
1289 return ReplaceInstUsesWith(I, V);
1291 if (Value *V = SimplifyURemInst(Op0, Op1, DL, TLI, DT, AT))
1292 return ReplaceInstUsesWith(I, V);
1294 if (Instruction *common = commonIRemTransforms(I))
1297 // (zext A) urem (zext B) --> zext (A urem B)
1298 if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
1299 if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
1300 return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
1303 // X urem Y -> X and Y-1, where Y is a power of 2,
1304 if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true, 0, AT, &I, DT)) {
1305 Constant *N1 = Constant::getAllOnesValue(I.getType());
1306 Value *Add = Builder->CreateAdd(Op1, N1);
1307 return BinaryOperator::CreateAnd(Op0, Add);
1310 // 1 urem X -> zext(X != 1)
1311 if (match(Op0, m_One())) {
1312 Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
1313 Value *Ext = Builder->CreateZExt(Cmp, I.getType());
1314 return ReplaceInstUsesWith(I, Ext);
1320 Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
1321 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1323 if (Value *V = SimplifyVectorOp(I))
1324 return ReplaceInstUsesWith(I, V);
1326 if (Value *V = SimplifySRemInst(Op0, Op1, DL, TLI, DT, AT))
1327 return ReplaceInstUsesWith(I, V);
1329 // Handle the integer rem common cases
1330 if (Instruction *Common = commonIRemTransforms(I))
1333 if (Value *RHSNeg = dyn_castNegVal(Op1))
1334 if (!isa<Constant>(RHSNeg) ||
1335 (isa<ConstantInt>(RHSNeg) &&
1336 cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
1338 Worklist.AddValue(I.getOperand(1));
1339 I.setOperand(1, RHSNeg);
1343 // If the sign bits of both operands are zero (i.e. we can prove they are
1344 // unsigned inputs), turn this into a urem.
1345 if (I.getType()->isIntegerTy()) {
1346 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
1347 if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
1348 MaskedValueIsZero(Op0, Mask, 0, &I)) {
1349 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
1350 return BinaryOperator::CreateURem(Op0, Op1, I.getName());
1354 // If it's a constant vector, flip any negative values positive.
1355 if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
1356 Constant *C = cast<Constant>(Op1);
1357 unsigned VWidth = C->getType()->getVectorNumElements();
1359 bool hasNegative = false;
1360 bool hasMissing = false;
1361 for (unsigned i = 0; i != VWidth; ++i) {
1362 Constant *Elt = C->getAggregateElement(i);
1368 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
1369 if (RHS->isNegative())
1373 if (hasNegative && !hasMissing) {
1374 SmallVector<Constant *, 16> Elts(VWidth);
1375 for (unsigned i = 0; i != VWidth; ++i) {
1376 Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
1377 if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
1378 if (RHS->isNegative())
1379 Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
1383 Constant *NewRHSV = ConstantVector::get(Elts);
1384 if (NewRHSV != C) { // Don't loop on -MININT
1385 Worklist.AddValue(I.getOperand(1));
1386 I.setOperand(1, NewRHSV);
1395 Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
1396 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1398 if (Value *V = SimplifyVectorOp(I))
1399 return ReplaceInstUsesWith(I, V);
1401 if (Value *V = SimplifyFRemInst(Op0, Op1, DL, TLI, DT, AT))
1402 return ReplaceInstUsesWith(I, V);
1404 // Handle cases involving: rem X, (select Cond, Y, Z)
1405 if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))