1 //===- InstCombineMulDivRem.cpp -------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
12 //===----------------------------------------------------------------------===//
14 #include "InstCombineInternal.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Analysis/InstructionSimplify.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/InstrTypes.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/PatternMatch.h"
29 #include "llvm/IR/Type.h"
30 #include "llvm/IR/Value.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Transforms/InstCombine/InstCombiner.h"
34 #include "llvm/Transforms/Utils/BuildLibCalls.h"
37 #define DEBUG_TYPE "instcombine"
38 #include "llvm/Transforms/Utils/InstructionWorklist.h"
41 using namespace PatternMatch
;
43 /// The specific integer value is used in a context where it is known to be
44 /// non-zero. If this allows us to simplify the computation, do so and return
45 /// the new operand, otherwise return null.
46 static Value
*simplifyValueKnownNonZero(Value
*V
, InstCombinerImpl
&IC
,
48 // If V has multiple uses, then we would have to do more analysis to determine
49 // if this is safe. For example, the use could be in dynamically unreached
51 if (!V
->hasOneUse()) return nullptr;
53 bool MadeChange
= false;
55 // ((1 << A) >>u B) --> (1 << (A-B))
56 // Because V cannot be zero, we know that B is less than A.
57 Value
*A
= nullptr, *B
= nullptr, *One
= nullptr;
58 if (match(V
, m_LShr(m_OneUse(m_Shl(m_Value(One
), m_Value(A
))), m_Value(B
))) &&
59 match(One
, m_One())) {
60 A
= IC
.Builder
.CreateSub(A
, B
);
61 return IC
.Builder
.CreateShl(One
, A
);
64 // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
65 // inexact. Similarly for <<.
66 BinaryOperator
*I
= dyn_cast
<BinaryOperator
>(V
);
67 if (I
&& I
->isLogicalShift() &&
68 IC
.isKnownToBeAPowerOfTwo(I
->getOperand(0), false, 0, &CxtI
)) {
69 // We know that this is an exact/nuw shift and that the input is a
70 // non-zero context as well.
71 if (Value
*V2
= simplifyValueKnownNonZero(I
->getOperand(0), IC
, CxtI
)) {
72 IC
.replaceOperand(*I
, 0, V2
);
76 if (I
->getOpcode() == Instruction::LShr
&& !I
->isExact()) {
81 if (I
->getOpcode() == Instruction::Shl
&& !I
->hasNoUnsignedWrap()) {
82 I
->setHasNoUnsignedWrap();
87 // TODO: Lots more we could do here:
88 // If V is a phi node, we can call this on each of its operands.
89 // "select cond, X, 0" can simplify to "X".
91 return MadeChange
? V
: nullptr;
94 // TODO: This is a specific form of a much more general pattern.
95 // We could detect a select with any binop identity constant, or we
96 // could use SimplifyBinOp to see if either arm of the select reduces.
97 // But that needs to be done carefully and/or while removing potential
98 // reverse canonicalizations as in InstCombiner::foldSelectIntoOp().
99 static Value
*foldMulSelectToNegate(BinaryOperator
&I
,
100 InstCombiner::BuilderTy
&Builder
) {
101 Value
*Cond
, *OtherOp
;
103 // mul (select Cond, 1, -1), OtherOp --> select Cond, OtherOp, -OtherOp
104 // mul OtherOp, (select Cond, 1, -1) --> select Cond, OtherOp, -OtherOp
105 if (match(&I
, m_c_Mul(m_OneUse(m_Select(m_Value(Cond
), m_One(), m_AllOnes())),
106 m_Value(OtherOp
)))) {
107 bool HasAnyNoWrap
= I
.hasNoSignedWrap() || I
.hasNoUnsignedWrap();
108 Value
*Neg
= Builder
.CreateNeg(OtherOp
, "", false, HasAnyNoWrap
);
109 return Builder
.CreateSelect(Cond
, OtherOp
, Neg
);
111 // mul (select Cond, -1, 1), OtherOp --> select Cond, -OtherOp, OtherOp
112 // mul OtherOp, (select Cond, -1, 1) --> select Cond, -OtherOp, OtherOp
113 if (match(&I
, m_c_Mul(m_OneUse(m_Select(m_Value(Cond
), m_AllOnes(), m_One())),
114 m_Value(OtherOp
)))) {
115 bool HasAnyNoWrap
= I
.hasNoSignedWrap() || I
.hasNoUnsignedWrap();
116 Value
*Neg
= Builder
.CreateNeg(OtherOp
, "", false, HasAnyNoWrap
);
117 return Builder
.CreateSelect(Cond
, Neg
, OtherOp
);
120 // fmul (select Cond, 1.0, -1.0), OtherOp --> select Cond, OtherOp, -OtherOp
121 // fmul OtherOp, (select Cond, 1.0, -1.0) --> select Cond, OtherOp, -OtherOp
122 if (match(&I
, m_c_FMul(m_OneUse(m_Select(m_Value(Cond
), m_SpecificFP(1.0),
123 m_SpecificFP(-1.0))),
124 m_Value(OtherOp
)))) {
125 IRBuilder
<>::FastMathFlagGuard
FMFGuard(Builder
);
126 Builder
.setFastMathFlags(I
.getFastMathFlags());
127 return Builder
.CreateSelect(Cond
, OtherOp
, Builder
.CreateFNeg(OtherOp
));
130 // fmul (select Cond, -1.0, 1.0), OtherOp --> select Cond, -OtherOp, OtherOp
131 // fmul OtherOp, (select Cond, -1.0, 1.0) --> select Cond, -OtherOp, OtherOp
132 if (match(&I
, m_c_FMul(m_OneUse(m_Select(m_Value(Cond
), m_SpecificFP(-1.0),
134 m_Value(OtherOp
)))) {
135 IRBuilder
<>::FastMathFlagGuard
FMFGuard(Builder
);
136 Builder
.setFastMathFlags(I
.getFastMathFlags());
137 return Builder
.CreateSelect(Cond
, Builder
.CreateFNeg(OtherOp
), OtherOp
);
143 /// Reduce integer multiplication patterns that contain a (+/-1 << Z) factor.
144 /// Callers are expected to call this twice to handle commuted patterns.
145 static Value
*foldMulShl1(BinaryOperator
&Mul
, bool CommuteOperands
,
146 InstCombiner::BuilderTy
&Builder
) {
147 Value
*X
= Mul
.getOperand(0), *Y
= Mul
.getOperand(1);
151 const bool HasNSW
= Mul
.hasNoSignedWrap();
152 const bool HasNUW
= Mul
.hasNoUnsignedWrap();
154 // X * (1 << Z) --> X << Z
156 if (match(Y
, m_Shl(m_One(), m_Value(Z
)))) {
157 bool PropagateNSW
= HasNSW
&& cast
<ShlOperator
>(Y
)->hasNoSignedWrap();
158 return Builder
.CreateShl(X
, Z
, Mul
.getName(), HasNUW
, PropagateNSW
);
161 // Similar to above, but an increment of the shifted value becomes an add:
162 // X * ((1 << Z) + 1) --> (X * (1 << Z)) + X --> (X << Z) + X
163 // This increases uses of X, so it may require a freeze, but that is still
164 // expected to be an improvement because it removes the multiply.
165 BinaryOperator
*Shift
;
166 if (match(Y
, m_OneUse(m_Add(m_BinOp(Shift
), m_One()))) &&
167 match(Shift
, m_OneUse(m_Shl(m_One(), m_Value(Z
))))) {
168 bool PropagateNSW
= HasNSW
&& Shift
->hasNoSignedWrap();
169 Value
*FrX
= Builder
.CreateFreeze(X
, X
->getName() + ".fr");
170 Value
*Shl
= Builder
.CreateShl(FrX
, Z
, "mulshl", HasNUW
, PropagateNSW
);
171 return Builder
.CreateAdd(Shl
, FrX
, Mul
.getName(), HasNUW
, PropagateNSW
);
174 // Similar to above, but a decrement of the shifted value is disguised as
175 // 'not' and becomes a sub:
176 // X * (~(-1 << Z)) --> X * ((1 << Z) - 1) --> (X << Z) - X
177 // This increases uses of X, so it may require a freeze, but that is still
178 // expected to be an improvement because it removes the multiply.
179 if (match(Y
, m_OneUse(m_Not(m_OneUse(m_Shl(m_AllOnes(), m_Value(Z
))))))) {
180 Value
*FrX
= Builder
.CreateFreeze(X
, X
->getName() + ".fr");
181 Value
*Shl
= Builder
.CreateShl(FrX
, Z
, "mulshl");
182 return Builder
.CreateSub(Shl
, FrX
, Mul
.getName());
188 static Value
*takeLog2(IRBuilderBase
&Builder
, Value
*Op
, unsigned Depth
,
189 bool AssumeNonZero
, bool DoFold
);
191 Instruction
*InstCombinerImpl::visitMul(BinaryOperator
&I
) {
192 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
194 simplifyMulInst(Op0
, Op1
, I
.hasNoSignedWrap(), I
.hasNoUnsignedWrap(),
195 SQ
.getWithInstruction(&I
)))
196 return replaceInstUsesWith(I
, V
);
198 if (SimplifyAssociativeOrCommutative(I
))
201 if (Instruction
*X
= foldVectorBinop(I
))
204 if (Instruction
*Phi
= foldBinopWithPhiOperands(I
))
207 if (Value
*V
= foldUsingDistributiveLaws(I
))
208 return replaceInstUsesWith(I
, V
);
210 Type
*Ty
= I
.getType();
211 const unsigned BitWidth
= Ty
->getScalarSizeInBits();
212 const bool HasNSW
= I
.hasNoSignedWrap();
213 const bool HasNUW
= I
.hasNoUnsignedWrap();
216 if (match(Op1
, m_AllOnes())) {
217 return HasNSW
? BinaryOperator::CreateNSWNeg(Op0
)
218 : BinaryOperator::CreateNeg(Op0
);
221 // Also allow combining multiply instructions on vectors.
226 if (match(&I
, m_Mul(m_Shl(m_Value(NewOp
), m_Constant(C2
)),
228 match(C1
, m_APInt(IVal
))) {
229 // ((X << C2)*C1) == (X * (C1 << C2))
230 Constant
*Shl
= ConstantExpr::getShl(C1
, C2
);
231 BinaryOperator
*Mul
= cast
<BinaryOperator
>(I
.getOperand(0));
232 BinaryOperator
*BO
= BinaryOperator::CreateMul(NewOp
, Shl
);
233 if (HasNUW
&& Mul
->hasNoUnsignedWrap())
234 BO
->setHasNoUnsignedWrap();
235 if (HasNSW
&& Mul
->hasNoSignedWrap() && Shl
->isNotMinSignedValue())
236 BO
->setHasNoSignedWrap();
240 if (match(&I
, m_Mul(m_Value(NewOp
), m_Constant(C1
)))) {
241 // Replace X*(2^C) with X << C, where C is either a scalar or a vector.
242 if (Constant
*NewCst
= ConstantExpr::getExactLogBase2(C1
)) {
243 BinaryOperator
*Shl
= BinaryOperator::CreateShl(NewOp
, NewCst
);
246 Shl
->setHasNoUnsignedWrap();
249 if (match(NewCst
, m_APInt(V
)) && *V
!= V
->getBitWidth() - 1)
250 Shl
->setHasNoSignedWrap();
258 if (Op0
->hasOneUse() && match(Op1
, m_NegatedPower2())) {
259 // Interpret X * (-1<<C) as (-X) * (1<<C) and try to sink the negation.
260 // The "* (1<<C)" thus becomes a potential shifting opportunity.
262 Negator::Negate(/*IsNegation*/ true, HasNSW
, Op0
, *this)) {
263 auto *Op1C
= cast
<Constant
>(Op1
);
264 return replaceInstUsesWith(
265 I
, Builder
.CreateMul(NegOp0
, ConstantExpr::getNeg(Op1C
), "",
267 HasNSW
&& Op1C
->isNotMinSignedValue()));
270 // Try to convert multiply of extended operand to narrow negate and shift
271 // for better analysis.
272 // This is valid if the shift amount (trailing zeros in the multiplier
273 // constant) clears more high bits than the bitwidth difference between
274 // source and destination types:
275 // ({z/s}ext X) * (-1<<C) --> (zext (-X)) << C
276 const APInt
*NegPow2C
;
278 if (match(Op0
, m_ZExtOrSExt(m_Value(X
))) &&
279 match(Op1
, m_APIntAllowUndef(NegPow2C
))) {
280 unsigned SrcWidth
= X
->getType()->getScalarSizeInBits();
281 unsigned ShiftAmt
= NegPow2C
->countr_zero();
282 if (ShiftAmt
>= BitWidth
- SrcWidth
) {
283 Value
*N
= Builder
.CreateNeg(X
, X
->getName() + ".neg");
284 Value
*Z
= Builder
.CreateZExt(N
, Ty
, N
->getName() + ".z");
285 return BinaryOperator::CreateShl(Z
, ConstantInt::get(Ty
, ShiftAmt
));
290 if (Instruction
*FoldedMul
= foldBinOpIntoSelectOrPhi(I
))
293 if (Value
*FoldedMul
= foldMulSelectToNegate(I
, Builder
))
294 return replaceInstUsesWith(I
, FoldedMul
);
296 // Simplify mul instructions with a constant RHS.
298 if (match(Op1
, m_ImmConstant(MulC
))) {
299 // Canonicalize (X+C1)*MulC -> X*MulC+C1*MulC.
300 // Canonicalize (X|C1)*MulC -> X*MulC+C1*MulC.
303 if (match(Op0
, m_OneUse(m_AddLike(m_Value(X
), m_ImmConstant(C1
))))) {
304 // C1*MulC simplifies to a tidier constant.
305 Value
*NewC
= Builder
.CreateMul(C1
, MulC
);
306 auto *BOp0
= cast
<BinaryOperator
>(Op0
);
308 (BOp0
->getOpcode() == Instruction::Or
|| BOp0
->hasNoUnsignedWrap());
309 Value
*NewMul
= Builder
.CreateMul(X
, MulC
);
310 auto *BO
= BinaryOperator::CreateAdd(NewMul
, NewC
);
311 if (HasNUW
&& Op0NUW
) {
312 // If NewMulBO is constant we also can set BO to nuw.
313 if (auto *NewMulBO
= dyn_cast
<BinaryOperator
>(NewMul
))
314 NewMulBO
->setHasNoUnsignedWrap();
315 BO
->setHasNoUnsignedWrap();
321 // abs(X) * abs(X) -> X * X
322 // nabs(X) * nabs(X) -> X * X
325 SelectPatternFlavor SPF
= matchSelectPattern(Op0
, X
, Y
).Flavor
;
326 if (SPF
== SPF_ABS
|| SPF
== SPF_NABS
)
327 return BinaryOperator::CreateMul(X
, X
);
329 if (match(Op0
, m_Intrinsic
<Intrinsic::abs
>(m_Value(X
))))
330 return BinaryOperator::CreateMul(X
, X
);
335 // abs(X) * abs(Y) -> abs(X * Y)
336 if (I
.hasNoSignedWrap() &&
338 m_OneUse(m_Intrinsic
<Intrinsic::abs
>(m_Value(X
), m_One()))) &&
339 match(Op1
, m_OneUse(m_Intrinsic
<Intrinsic::abs
>(m_Value(Y
), m_One()))))
340 return replaceInstUsesWith(
341 I
, Builder
.CreateBinaryIntrinsic(Intrinsic::abs
,
342 Builder
.CreateNSWMul(X
, Y
),
349 if (match(Op0
, m_Neg(m_Value(X
))) && match(Op1
, m_Constant(Op1C
)))
350 return BinaryOperator::CreateMul(X
, ConstantExpr::getNeg(Op1C
));
353 if (match(Op0
, m_Neg(m_Value(X
))) && match(Op1
, m_Neg(m_Value(Y
)))) {
354 auto *NewMul
= BinaryOperator::CreateMul(X
, Y
);
355 if (HasNSW
&& cast
<OverflowingBinaryOperator
>(Op0
)->hasNoSignedWrap() &&
356 cast
<OverflowingBinaryOperator
>(Op1
)->hasNoSignedWrap())
357 NewMul
->setHasNoSignedWrap();
361 // -X * Y --> -(X * Y)
362 // X * -Y --> -(X * Y)
363 if (match(&I
, m_c_Mul(m_OneUse(m_Neg(m_Value(X
))), m_Value(Y
))))
364 return BinaryOperator::CreateNeg(Builder
.CreateMul(X
, Y
));
366 // (-X * Y) * -X --> (X * Y) * X
367 // (-X << Y) * -X --> (X << Y) * X
368 if (match(Op1
, m_Neg(m_Value(X
)))) {
369 if (Value
*NegOp0
= Negator::Negate(false, /*IsNSW*/ false, Op0
, *this))
370 return BinaryOperator::CreateMul(NegOp0
, X
);
373 // (X / Y) * Y = X - (X % Y)
374 // (X / Y) * -Y = (X % Y) - X
377 BinaryOperator
*Div
= dyn_cast
<BinaryOperator
>(Op0
);
378 if (!Div
|| (Div
->getOpcode() != Instruction::UDiv
&&
379 Div
->getOpcode() != Instruction::SDiv
)) {
381 Div
= dyn_cast
<BinaryOperator
>(Op1
);
383 Value
*Neg
= dyn_castNegVal(Y
);
384 if (Div
&& Div
->hasOneUse() &&
385 (Div
->getOperand(1) == Y
|| Div
->getOperand(1) == Neg
) &&
386 (Div
->getOpcode() == Instruction::UDiv
||
387 Div
->getOpcode() == Instruction::SDiv
)) {
388 Value
*X
= Div
->getOperand(0), *DivOp1
= Div
->getOperand(1);
390 // If the division is exact, X % Y is zero, so we end up with X or -X.
391 if (Div
->isExact()) {
393 return replaceInstUsesWith(I
, X
);
394 return BinaryOperator::CreateNeg(X
);
397 auto RemOpc
= Div
->getOpcode() == Instruction::UDiv
? Instruction::URem
399 // X must be frozen because we are increasing its number of uses.
400 Value
*XFreeze
= Builder
.CreateFreeze(X
, X
->getName() + ".fr");
401 Value
*Rem
= Builder
.CreateBinOp(RemOpc
, XFreeze
, DivOp1
);
403 return BinaryOperator::CreateSub(XFreeze
, Rem
);
404 return BinaryOperator::CreateSub(Rem
, XFreeze
);
408 // Fold the following two scenarios:
409 // 1) i1 mul -> i1 and.
410 // 2) X * Y --> X & Y, iff X, Y can be only {0,1}.
411 // Note: We could use known bits to generalize this and related patterns with
413 if (Ty
->isIntOrIntVectorTy(1) ||
414 (match(Op0
, m_And(m_Value(), m_One())) &&
415 match(Op1
, m_And(m_Value(), m_One()))))
416 return BinaryOperator::CreateAnd(Op0
, Op1
);
418 if (Value
*R
= foldMulShl1(I
, /* CommuteOperands */ false, Builder
))
419 return replaceInstUsesWith(I
, R
);
420 if (Value
*R
= foldMulShl1(I
, /* CommuteOperands */ true, Builder
))
421 return replaceInstUsesWith(I
, R
);
423 // (zext bool X) * (zext bool Y) --> zext (and X, Y)
424 // (sext bool X) * (sext bool Y) --> zext (and X, Y)
425 // Note: -1 * -1 == 1 * 1 == 1 (if the extends match, the result is the same)
426 if (((match(Op0
, m_ZExt(m_Value(X
))) && match(Op1
, m_ZExt(m_Value(Y
)))) ||
427 (match(Op0
, m_SExt(m_Value(X
))) && match(Op1
, m_SExt(m_Value(Y
))))) &&
428 X
->getType()->isIntOrIntVectorTy(1) && X
->getType() == Y
->getType() &&
429 (Op0
->hasOneUse() || Op1
->hasOneUse() || X
== Y
)) {
430 Value
*And
= Builder
.CreateAnd(X
, Y
, "mulbool");
431 return CastInst::Create(Instruction::ZExt
, And
, Ty
);
433 // (sext bool X) * (zext bool Y) --> sext (and X, Y)
434 // (zext bool X) * (sext bool Y) --> sext (and X, Y)
435 // Note: -1 * 1 == 1 * -1 == -1
436 if (((match(Op0
, m_SExt(m_Value(X
))) && match(Op1
, m_ZExt(m_Value(Y
)))) ||
437 (match(Op0
, m_ZExt(m_Value(X
))) && match(Op1
, m_SExt(m_Value(Y
))))) &&
438 X
->getType()->isIntOrIntVectorTy(1) && X
->getType() == Y
->getType() &&
439 (Op0
->hasOneUse() || Op1
->hasOneUse())) {
440 Value
*And
= Builder
.CreateAnd(X
, Y
, "mulbool");
441 return CastInst::Create(Instruction::SExt
, And
, Ty
);
444 // (zext bool X) * Y --> X ? Y : 0
445 // Y * (zext bool X) --> X ? Y : 0
446 if (match(Op0
, m_ZExt(m_Value(X
))) && X
->getType()->isIntOrIntVectorTy(1))
447 return SelectInst::Create(X
, Op1
, ConstantInt::getNullValue(Ty
));
448 if (match(Op1
, m_ZExt(m_Value(X
))) && X
->getType()->isIntOrIntVectorTy(1))
449 return SelectInst::Create(X
, Op0
, ConstantInt::getNullValue(Ty
));
452 if (match(Op1
, m_ImmConstant(ImmC
))) {
453 // (sext bool X) * C --> X ? -C : 0
454 if (match(Op0
, m_SExt(m_Value(X
))) && X
->getType()->isIntOrIntVectorTy(1)) {
455 Constant
*NegC
= ConstantExpr::getNeg(ImmC
);
456 return SelectInst::Create(X
, NegC
, ConstantInt::getNullValue(Ty
));
459 // (ashr i32 X, 31) * C --> (X < 0) ? -C : 0
461 if (match(Op0
, m_OneUse(m_AShr(m_Value(X
), m_APInt(C
)))) &&
462 *C
== C
->getBitWidth() - 1) {
463 Constant
*NegC
= ConstantExpr::getNeg(ImmC
);
464 Value
*IsNeg
= Builder
.CreateIsNeg(X
, "isneg");
465 return SelectInst::Create(IsNeg
, NegC
, ConstantInt::getNullValue(Ty
));
469 // (lshr X, 31) * Y --> (X < 0) ? Y : 0
470 // TODO: We are not checking one-use because the elimination of the multiply
471 // is better for analysis?
473 if (match(&I
, m_c_BinOp(m_LShr(m_Value(X
), m_APInt(C
)), m_Value(Y
))) &&
474 *C
== C
->getBitWidth() - 1) {
475 Value
*IsNeg
= Builder
.CreateIsNeg(X
, "isneg");
476 return SelectInst::Create(IsNeg
, Y
, ConstantInt::getNullValue(Ty
));
479 // (and X, 1) * Y --> (trunc X) ? Y : 0
480 if (match(&I
, m_c_BinOp(m_OneUse(m_And(m_Value(X
), m_One())), m_Value(Y
)))) {
481 Value
*Tr
= Builder
.CreateTrunc(X
, CmpInst::makeCmpResultType(Ty
));
482 return SelectInst::Create(Tr
, Y
, ConstantInt::getNullValue(Ty
));
485 // ((ashr X, 31) | 1) * X --> abs(X)
486 // X * ((ashr X, 31) | 1) --> abs(X)
487 if (match(&I
, m_c_BinOp(m_Or(m_AShr(m_Value(X
),
488 m_SpecificIntAllowUndef(BitWidth
- 1)),
491 Value
*Abs
= Builder
.CreateBinaryIntrinsic(
492 Intrinsic::abs
, X
, ConstantInt::getBool(I
.getContext(), HasNSW
));
494 return replaceInstUsesWith(I
, Abs
);
497 if (Instruction
*Ext
= narrowMathIfNoOverflow(I
))
500 if (Instruction
*Res
= foldBinOpOfSelectAndCastOfSelectCondition(I
))
504 // if Log2(Op0) folds away ->
505 // (shl Op1, Log2(Op0))
506 // if Log2(Op1) folds away ->
507 // (shl Op0, Log2(Op1))
508 if (takeLog2(Builder
, Op0
, /*Depth*/ 0, /*AssumeNonZero*/ false,
510 Value
*Res
= takeLog2(Builder
, Op0
, /*Depth*/ 0, /*AssumeNonZero*/ false,
512 BinaryOperator
*Shl
= BinaryOperator::CreateShl(Op1
, Res
);
513 // We can only propegate nuw flag.
514 Shl
->setHasNoUnsignedWrap(HasNUW
);
517 if (takeLog2(Builder
, Op1
, /*Depth*/ 0, /*AssumeNonZero*/ false,
519 Value
*Res
= takeLog2(Builder
, Op1
, /*Depth*/ 0, /*AssumeNonZero*/ false,
521 BinaryOperator
*Shl
= BinaryOperator::CreateShl(Op0
, Res
);
522 // We can only propegate nuw flag.
523 Shl
->setHasNoUnsignedWrap(HasNUW
);
527 bool Changed
= false;
528 if (!HasNSW
&& willNotOverflowSignedMul(Op0
, Op1
, I
)) {
530 I
.setHasNoSignedWrap(true);
533 if (!HasNUW
&& willNotOverflowUnsignedMul(Op0
, Op1
, I
)) {
535 I
.setHasNoUnsignedWrap(true);
538 return Changed
? &I
: nullptr;
541 Instruction
*InstCombinerImpl::foldFPSignBitOps(BinaryOperator
&I
) {
542 BinaryOperator::BinaryOps Opcode
= I
.getOpcode();
543 assert((Opcode
== Instruction::FMul
|| Opcode
== Instruction::FDiv
) &&
544 "Expected fmul or fdiv");
546 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
551 if (match(Op0
, m_FNeg(m_Value(X
))) && match(Op1
, m_FNeg(m_Value(Y
))))
552 return BinaryOperator::CreateWithCopiedFlags(Opcode
, X
, Y
, &I
);
554 // fabs(X) * fabs(X) -> X * X
555 // fabs(X) / fabs(X) -> X / X
556 if (Op0
== Op1
&& match(Op0
, m_FAbs(m_Value(X
))))
557 return BinaryOperator::CreateWithCopiedFlags(Opcode
, X
, X
, &I
);
559 // fabs(X) * fabs(Y) --> fabs(X * Y)
560 // fabs(X) / fabs(Y) --> fabs(X / Y)
561 if (match(Op0
, m_FAbs(m_Value(X
))) && match(Op1
, m_FAbs(m_Value(Y
))) &&
562 (Op0
->hasOneUse() || Op1
->hasOneUse())) {
563 IRBuilder
<>::FastMathFlagGuard
FMFGuard(Builder
);
564 Builder
.setFastMathFlags(I
.getFastMathFlags());
565 Value
*XY
= Builder
.CreateBinOp(Opcode
, X
, Y
);
566 Value
*Fabs
= Builder
.CreateUnaryIntrinsic(Intrinsic::fabs
, XY
);
568 return replaceInstUsesWith(I
, Fabs
);
574 Instruction
*InstCombinerImpl::foldFMulReassoc(BinaryOperator
&I
) {
575 Value
*Op0
= I
.getOperand(0);
576 Value
*Op1
= I
.getOperand(1);
580 // Reassociate constant RHS with another constant to form constant
582 if (match(Op1
, m_Constant(C
)) && C
->isFiniteNonZeroFP()) {
584 if (match(Op0
, m_OneUse(m_FDiv(m_Constant(C1
), m_Value(X
))))) {
585 // (C1 / X) * C --> (C * C1) / X
587 ConstantFoldBinaryOpOperands(Instruction::FMul
, C
, C1
, DL
);
588 if (CC1
&& CC1
->isNormalFP())
589 return BinaryOperator::CreateFDivFMF(CC1
, X
, &I
);
591 if (match(Op0
, m_FDiv(m_Value(X
), m_Constant(C1
)))) {
592 // (X / C1) * C --> X * (C / C1)
594 ConstantFoldBinaryOpOperands(Instruction::FDiv
, C
, C1
, DL
);
595 if (CDivC1
&& CDivC1
->isNormalFP())
596 return BinaryOperator::CreateFMulFMF(X
, CDivC1
, &I
);
598 // If the constant was a denormal, try reassociating differently.
599 // (X / C1) * C --> X / (C1 / C)
601 ConstantFoldBinaryOpOperands(Instruction::FDiv
, C1
, C
, DL
);
602 if (C1DivC
&& Op0
->hasOneUse() && C1DivC
->isNormalFP())
603 return BinaryOperator::CreateFDivFMF(X
, C1DivC
, &I
);
606 // We do not need to match 'fadd C, X' and 'fsub X, C' because they are
607 // canonicalized to 'fadd X, C'. Distributing the multiply may allow
608 // further folds and (X * C) + C2 is 'fma'.
609 if (match(Op0
, m_OneUse(m_FAdd(m_Value(X
), m_Constant(C1
))))) {
610 // (X + C1) * C --> (X * C) + (C * C1)
612 ConstantFoldBinaryOpOperands(Instruction::FMul
, C
, C1
, DL
)) {
613 Value
*XC
= Builder
.CreateFMulFMF(X
, C
, &I
);
614 return BinaryOperator::CreateFAddFMF(XC
, CC1
, &I
);
617 if (match(Op0
, m_OneUse(m_FSub(m_Constant(C1
), m_Value(X
))))) {
618 // (C1 - X) * C --> (C * C1) - (X * C)
620 ConstantFoldBinaryOpOperands(Instruction::FMul
, C
, C1
, DL
)) {
621 Value
*XC
= Builder
.CreateFMulFMF(X
, C
, &I
);
622 return BinaryOperator::CreateFSubFMF(CC1
, XC
, &I
);
629 m_c_FMul(m_OneUse(m_FDiv(m_Value(X
), m_Value(Y
))), m_Value(Z
)))) {
630 // Sink division: (X / Y) * Z --> (X * Z) / Y
631 Value
*NewFMul
= Builder
.CreateFMulFMF(X
, Z
, &I
);
632 return BinaryOperator::CreateFDivFMF(NewFMul
, Y
, &I
);
635 // sqrt(X) * sqrt(Y) -> sqrt(X * Y)
636 // nnan disallows the possibility of returning a number if both operands are
637 // negative (in that case, we should return NaN).
638 if (I
.hasNoNaNs() && match(Op0
, m_OneUse(m_Sqrt(m_Value(X
)))) &&
639 match(Op1
, m_OneUse(m_Sqrt(m_Value(Y
))))) {
640 Value
*XY
= Builder
.CreateFMulFMF(X
, Y
, &I
);
641 Value
*Sqrt
= Builder
.CreateUnaryIntrinsic(Intrinsic::sqrt
, XY
, &I
);
642 return replaceInstUsesWith(I
, Sqrt
);
645 // The following transforms are done irrespective of the number of uses
646 // for the expression "1.0/sqrt(X)".
647 // 1) 1.0/sqrt(X) * X -> X/sqrt(X)
648 // 2) X * 1.0/sqrt(X) -> X/sqrt(X)
649 // We always expect the backend to reduce X/sqrt(X) to sqrt(X), if it
650 // has the necessary (reassoc) fast-math-flags.
651 if (I
.hasNoSignedZeros() &&
652 match(Op0
, (m_FDiv(m_SpecificFP(1.0), m_Value(Y
)))) &&
653 match(Y
, m_Sqrt(m_Value(X
))) && Op1
== X
)
654 return BinaryOperator::CreateFDivFMF(X
, Y
, &I
);
655 if (I
.hasNoSignedZeros() &&
656 match(Op1
, (m_FDiv(m_SpecificFP(1.0), m_Value(Y
)))) &&
657 match(Y
, m_Sqrt(m_Value(X
))) && Op0
== X
)
658 return BinaryOperator::CreateFDivFMF(X
, Y
, &I
);
660 // Like the similar transform in instsimplify, this requires 'nsz' because
661 // sqrt(-0.0) = -0.0, and -0.0 * -0.0 does not simplify to -0.0.
662 if (I
.hasNoNaNs() && I
.hasNoSignedZeros() && Op0
== Op1
&& Op0
->hasNUses(2)) {
663 // Peek through fdiv to find squaring of square root:
664 // (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y
665 if (match(Op0
, m_FDiv(m_Value(X
), m_Sqrt(m_Value(Y
))))) {
666 Value
*XX
= Builder
.CreateFMulFMF(X
, X
, &I
);
667 return BinaryOperator::CreateFDivFMF(XX
, Y
, &I
);
669 // (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X)
670 if (match(Op0
, m_FDiv(m_Sqrt(m_Value(Y
)), m_Value(X
)))) {
671 Value
*XX
= Builder
.CreateFMulFMF(X
, X
, &I
);
672 return BinaryOperator::CreateFDivFMF(Y
, XX
, &I
);
676 // pow(X, Y) * X --> pow(X, Y+1)
677 // X * pow(X, Y) --> pow(X, Y+1)
678 if (match(&I
, m_c_FMul(m_OneUse(m_Intrinsic
<Intrinsic::pow
>(m_Value(X
),
681 Value
*Y1
= Builder
.CreateFAddFMF(Y
, ConstantFP::get(I
.getType(), 1.0), &I
);
682 Value
*Pow
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, X
, Y1
, &I
);
683 return replaceInstUsesWith(I
, Pow
);
686 if (I
.isOnlyUserOfAnyOperand()) {
687 // pow(X, Y) * pow(X, Z) -> pow(X, Y + Z)
688 if (match(Op0
, m_Intrinsic
<Intrinsic::pow
>(m_Value(X
), m_Value(Y
))) &&
689 match(Op1
, m_Intrinsic
<Intrinsic::pow
>(m_Specific(X
), m_Value(Z
)))) {
690 auto *YZ
= Builder
.CreateFAddFMF(Y
, Z
, &I
);
691 auto *NewPow
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, X
, YZ
, &I
);
692 return replaceInstUsesWith(I
, NewPow
);
694 // pow(X, Y) * pow(Z, Y) -> pow(X * Z, Y)
695 if (match(Op0
, m_Intrinsic
<Intrinsic::pow
>(m_Value(X
), m_Value(Y
))) &&
696 match(Op1
, m_Intrinsic
<Intrinsic::pow
>(m_Value(Z
), m_Specific(Y
)))) {
697 auto *XZ
= Builder
.CreateFMulFMF(X
, Z
, &I
);
698 auto *NewPow
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, XZ
, Y
, &I
);
699 return replaceInstUsesWith(I
, NewPow
);
702 // powi(x, y) * powi(x, z) -> powi(x, y + z)
703 if (match(Op0
, m_Intrinsic
<Intrinsic::powi
>(m_Value(X
), m_Value(Y
))) &&
704 match(Op1
, m_Intrinsic
<Intrinsic::powi
>(m_Specific(X
), m_Value(Z
))) &&
705 Y
->getType() == Z
->getType()) {
706 auto *YZ
= Builder
.CreateAdd(Y
, Z
);
707 auto *NewPow
= Builder
.CreateIntrinsic(
708 Intrinsic::powi
, {X
->getType(), YZ
->getType()}, {X
, YZ
}, &I
);
709 return replaceInstUsesWith(I
, NewPow
);
712 // exp(X) * exp(Y) -> exp(X + Y)
713 if (match(Op0
, m_Intrinsic
<Intrinsic::exp
>(m_Value(X
))) &&
714 match(Op1
, m_Intrinsic
<Intrinsic::exp
>(m_Value(Y
)))) {
715 Value
*XY
= Builder
.CreateFAddFMF(X
, Y
, &I
);
716 Value
*Exp
= Builder
.CreateUnaryIntrinsic(Intrinsic::exp
, XY
, &I
);
717 return replaceInstUsesWith(I
, Exp
);
720 // exp2(X) * exp2(Y) -> exp2(X + Y)
721 if (match(Op0
, m_Intrinsic
<Intrinsic::exp2
>(m_Value(X
))) &&
722 match(Op1
, m_Intrinsic
<Intrinsic::exp2
>(m_Value(Y
)))) {
723 Value
*XY
= Builder
.CreateFAddFMF(X
, Y
, &I
);
724 Value
*Exp2
= Builder
.CreateUnaryIntrinsic(Intrinsic::exp2
, XY
, &I
);
725 return replaceInstUsesWith(I
, Exp2
);
729 // (X*Y) * X => (X*X) * Y where Y != X
730 // The purpose is two-fold:
731 // 1) to form a power expression (of X).
732 // 2) potentially shorten the critical path: After transformation, the
733 // latency of the instruction Y is amortized by the expression of X*X,
734 // and therefore Y is in a "less critical" position compared to what it
735 // was before the transformation.
736 if (match(Op0
, m_OneUse(m_c_FMul(m_Specific(Op1
), m_Value(Y
)))) && Op1
!= Y
) {
737 Value
*XX
= Builder
.CreateFMulFMF(Op1
, Op1
, &I
);
738 return BinaryOperator::CreateFMulFMF(XX
, Y
, &I
);
740 if (match(Op1
, m_OneUse(m_c_FMul(m_Specific(Op0
), m_Value(Y
)))) && Op0
!= Y
) {
741 Value
*XX
= Builder
.CreateFMulFMF(Op0
, Op0
, &I
);
742 return BinaryOperator::CreateFMulFMF(XX
, Y
, &I
);
748 Instruction
*InstCombinerImpl::visitFMul(BinaryOperator
&I
) {
749 if (Value
*V
= simplifyFMulInst(I
.getOperand(0), I
.getOperand(1),
750 I
.getFastMathFlags(),
751 SQ
.getWithInstruction(&I
)))
752 return replaceInstUsesWith(I
, V
);
754 if (SimplifyAssociativeOrCommutative(I
))
757 if (Instruction
*X
= foldVectorBinop(I
))
760 if (Instruction
*Phi
= foldBinopWithPhiOperands(I
))
763 if (Instruction
*FoldedMul
= foldBinOpIntoSelectOrPhi(I
))
766 if (Value
*FoldedMul
= foldMulSelectToNegate(I
, Builder
))
767 return replaceInstUsesWith(I
, FoldedMul
);
769 if (Instruction
*R
= foldFPSignBitOps(I
))
773 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
774 if (match(Op1
, m_SpecificFP(-1.0)))
775 return UnaryOperator::CreateFNegFMF(Op0
, &I
);
777 // With no-nans: X * 0.0 --> copysign(0.0, X)
778 if (I
.hasNoNaNs() && match(Op1
, m_PosZeroFP())) {
779 CallInst
*CopySign
= Builder
.CreateIntrinsic(Intrinsic::copysign
,
780 {I
.getType()}, {Op1
, Op0
}, &I
);
781 return replaceInstUsesWith(I
, CopySign
);
787 if (match(Op0
, m_FNeg(m_Value(X
))) && match(Op1
, m_Constant(C
)))
788 if (Constant
*NegC
= ConstantFoldUnaryOpOperand(Instruction::FNeg
, C
, DL
))
789 return BinaryOperator::CreateFMulFMF(X
, NegC
, &I
);
791 // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E)
792 if (Value
*V
= SimplifySelectsFeedingBinaryOp(I
, Op0
, Op1
))
793 return replaceInstUsesWith(I
, V
);
795 if (I
.hasAllowReassoc())
796 if (Instruction
*FoldedMul
= foldFMulReassoc(I
))
799 // log2(X * 0.5) * Y = log2(X) * Y - Y
801 IntrinsicInst
*Log2
= nullptr;
802 if (match(Op0
, m_OneUse(m_Intrinsic
<Intrinsic::log2
>(
803 m_OneUse(m_FMul(m_Value(X
), m_SpecificFP(0.5))))))) {
804 Log2
= cast
<IntrinsicInst
>(Op0
);
807 if (match(Op1
, m_OneUse(m_Intrinsic
<Intrinsic::log2
>(
808 m_OneUse(m_FMul(m_Value(X
), m_SpecificFP(0.5))))))) {
809 Log2
= cast
<IntrinsicInst
>(Op1
);
813 Value
*Log2
= Builder
.CreateUnaryIntrinsic(Intrinsic::log2
, X
, &I
);
814 Value
*LogXTimesY
= Builder
.CreateFMulFMF(Log2
, Y
, &I
);
815 return BinaryOperator::CreateFSubFMF(LogXTimesY
, Y
, &I
);
819 // Simplify FMUL recurrences starting with 0.0 to 0.0 if nnan and nsz are set.
820 // Given a phi node with entry value as 0 and it used in fmul operation,
821 // we can replace fmul with 0 safely and eleminate loop operation.
822 PHINode
*PN
= nullptr;
823 Value
*Start
= nullptr, *Step
= nullptr;
824 if (matchSimpleRecurrence(&I
, PN
, Start
, Step
) && I
.hasNoNaNs() &&
825 I
.hasNoSignedZeros() && match(Start
, m_Zero()))
826 return replaceInstUsesWith(I
, Start
);
828 // minimum(X, Y) * maximum(X, Y) => X * Y.
830 m_c_FMul(m_Intrinsic
<Intrinsic::maximum
>(m_Value(X
), m_Value(Y
)),
831 m_c_Intrinsic
<Intrinsic::minimum
>(m_Deferred(X
),
833 BinaryOperator
*Result
= BinaryOperator::CreateFMulFMF(X
, Y
, &I
);
834 // We cannot preserve ninf if nnan flag is not set.
835 // If X is NaN and Y is Inf then in original program we had NaN * NaN,
836 // while in optimized version NaN * Inf and this is a poison with ninf flag.
837 if (!Result
->hasNoNaNs())
838 Result
->setHasNoInfs(false);
845 /// Fold a divide or remainder with a select instruction divisor when one of the
846 /// select operands is zero. In that case, we can use the other select operand
847 /// because div/rem by zero is undefined.
848 bool InstCombinerImpl::simplifyDivRemOfSelectWithZeroOp(BinaryOperator
&I
) {
849 SelectInst
*SI
= dyn_cast
<SelectInst
>(I
.getOperand(1));
854 if (match(SI
->getTrueValue(), m_Zero()))
855 // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
857 else if (match(SI
->getFalseValue(), m_Zero()))
858 // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
863 // Change the div/rem to use 'Y' instead of the select.
864 replaceOperand(I
, 1, SI
->getOperand(NonNullOperand
));
866 // Okay, we know we replace the operand of the div/rem with 'Y' with no
867 // problem. However, the select, or the condition of the select may have
868 // multiple uses. Based on our knowledge that the operand must be non-zero,
869 // propagate the known value for the select into other uses of it, and
870 // propagate a known value of the condition into its other users.
872 // If the select and condition only have a single use, don't bother with this,
874 Value
*SelectCond
= SI
->getCondition();
875 if (SI
->use_empty() && SelectCond
->hasOneUse())
878 // Scan the current block backward, looking for other uses of SI.
879 BasicBlock::iterator BBI
= I
.getIterator(), BBFront
= I
.getParent()->begin();
880 Type
*CondTy
= SelectCond
->getType();
881 while (BBI
!= BBFront
) {
883 // If we found an instruction that we can't assume will return, so
884 // information from below it cannot be propagated above it.
885 if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI
))
888 // Replace uses of the select or its condition with the known values.
889 for (Use
&Op
: BBI
->operands()) {
891 replaceUse(Op
, SI
->getOperand(NonNullOperand
));
892 Worklist
.push(&*BBI
);
893 } else if (Op
== SelectCond
) {
894 replaceUse(Op
, NonNullOperand
== 1 ? ConstantInt::getTrue(CondTy
)
895 : ConstantInt::getFalse(CondTy
));
896 Worklist
.push(&*BBI
);
900 // If we past the instruction, quit looking for it.
903 if (&*BBI
== SelectCond
)
904 SelectCond
= nullptr;
906 // If we ran out of things to eliminate, break out of the loop.
907 if (!SelectCond
&& !SI
)
914 /// True if the multiply can not be expressed in an int this size.
915 static bool multiplyOverflows(const APInt
&C1
, const APInt
&C2
, APInt
&Product
,
918 Product
= IsSigned
? C1
.smul_ov(C2
, Overflow
) : C1
.umul_ov(C2
, Overflow
);
922 /// True if C1 is a multiple of C2. Quotient contains C1/C2.
923 static bool isMultiple(const APInt
&C1
, const APInt
&C2
, APInt
&Quotient
,
925 assert(C1
.getBitWidth() == C2
.getBitWidth() && "Constant widths not equal");
927 // Bail if we will divide by zero.
931 // Bail if we would divide INT_MIN by -1.
932 if (IsSigned
&& C1
.isMinSignedValue() && C2
.isAllOnes())
935 APInt
Remainder(C1
.getBitWidth(), /*val=*/0ULL, IsSigned
);
937 APInt::sdivrem(C1
, C2
, Quotient
, Remainder
);
939 APInt::udivrem(C1
, C2
, Quotient
, Remainder
);
941 return Remainder
.isMinValue();
944 static Value
*foldIDivShl(BinaryOperator
&I
, InstCombiner::BuilderTy
&Builder
) {
945 assert((I
.getOpcode() == Instruction::SDiv
||
946 I
.getOpcode() == Instruction::UDiv
) &&
947 "Expected integer divide");
949 bool IsSigned
= I
.getOpcode() == Instruction::SDiv
;
950 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
951 Type
*Ty
= I
.getType();
955 // With appropriate no-wrap constraints, remove a common factor in the
956 // dividend and divisor that is disguised as a left-shifted value.
957 if (match(Op1
, m_Shl(m_Value(X
), m_Value(Z
))) &&
958 match(Op0
, m_c_Mul(m_Specific(X
), m_Value(Y
)))) {
959 // Both operands must have the matching no-wrap for this kind of division.
960 auto *Mul
= cast
<OverflowingBinaryOperator
>(Op0
);
961 auto *Shl
= cast
<OverflowingBinaryOperator
>(Op1
);
962 bool HasNUW
= Mul
->hasNoUnsignedWrap() && Shl
->hasNoUnsignedWrap();
963 bool HasNSW
= Mul
->hasNoSignedWrap() && Shl
->hasNoSignedWrap();
965 // (X * Y) u/ (X << Z) --> Y u>> Z
966 if (!IsSigned
&& HasNUW
)
967 return Builder
.CreateLShr(Y
, Z
, "", I
.isExact());
969 // (X * Y) s/ (X << Z) --> Y s/ (1 << Z)
970 if (IsSigned
&& HasNSW
&& (Op0
->hasOneUse() || Op1
->hasOneUse())) {
971 Value
*Shl
= Builder
.CreateShl(ConstantInt::get(Ty
, 1), Z
);
972 return Builder
.CreateSDiv(Y
, Shl
, "", I
.isExact());
976 // With appropriate no-wrap constraints, remove a common factor in the
977 // dividend and divisor that is disguised as a left-shift amount.
978 if (match(Op0
, m_Shl(m_Value(X
), m_Value(Z
))) &&
979 match(Op1
, m_Shl(m_Value(Y
), m_Specific(Z
)))) {
980 auto *Shl0
= cast
<OverflowingBinaryOperator
>(Op0
);
981 auto *Shl1
= cast
<OverflowingBinaryOperator
>(Op1
);
983 // For unsigned div, we need 'nuw' on both shifts or
984 // 'nsw' on both shifts + 'nuw' on the dividend.
985 // (X << Z) / (Y << Z) --> X / Y
987 ((Shl0
->hasNoUnsignedWrap() && Shl1
->hasNoUnsignedWrap()) ||
988 (Shl0
->hasNoUnsignedWrap() && Shl0
->hasNoSignedWrap() &&
989 Shl1
->hasNoSignedWrap())))
990 return Builder
.CreateUDiv(X
, Y
, "", I
.isExact());
992 // For signed div, we need 'nsw' on both shifts + 'nuw' on the divisor.
993 // (X << Z) / (Y << Z) --> X / Y
994 if (IsSigned
&& Shl0
->hasNoSignedWrap() && Shl1
->hasNoSignedWrap() &&
995 Shl1
->hasNoUnsignedWrap())
996 return Builder
.CreateSDiv(X
, Y
, "", I
.isExact());
999 // If X << Y and X << Z does not overflow, then:
1000 // (X << Y) / (X << Z) -> (1 << Y) / (1 << Z) -> 1 << Y >> Z
1001 if (match(Op0
, m_Shl(m_Value(X
), m_Value(Y
))) &&
1002 match(Op1
, m_Shl(m_Specific(X
), m_Value(Z
)))) {
1003 auto *Shl0
= cast
<OverflowingBinaryOperator
>(Op0
);
1004 auto *Shl1
= cast
<OverflowingBinaryOperator
>(Op1
);
1006 if (IsSigned
? (Shl0
->hasNoSignedWrap() && Shl1
->hasNoSignedWrap())
1007 : (Shl0
->hasNoUnsignedWrap() && Shl1
->hasNoUnsignedWrap())) {
1008 Constant
*One
= ConstantInt::get(X
->getType(), 1);
1009 // Only preserve the nsw flag if dividend has nsw
1010 // or divisor has nsw and operator is sdiv.
1011 Value
*Dividend
= Builder
.CreateShl(
1012 One
, Y
, "shl.dividend",
1015 IsSigned
? (Shl0
->hasNoUnsignedWrap() || Shl1
->hasNoUnsignedWrap())
1016 : Shl0
->hasNoSignedWrap());
1017 return Builder
.CreateLShr(Dividend
, Z
, "", I
.isExact());
1024 /// This function implements the transforms common to both integer division
1025 /// instructions (udiv and sdiv). It is called by the visitors to those integer
1026 /// division instructions.
1027 /// Common integer divide transforms
1028 Instruction
*InstCombinerImpl::commonIDivTransforms(BinaryOperator
&I
) {
1029 if (Instruction
*Phi
= foldBinopWithPhiOperands(I
))
1032 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
1033 bool IsSigned
= I
.getOpcode() == Instruction::SDiv
;
1034 Type
*Ty
= I
.getType();
1036 // The RHS is known non-zero.
1037 if (Value
*V
= simplifyValueKnownNonZero(I
.getOperand(1), *this, I
))
1038 return replaceOperand(I
, 1, V
);
1040 // Handle cases involving: [su]div X, (select Cond, Y, Z)
1041 // This does not apply for fdiv.
1042 if (simplifyDivRemOfSelectWithZeroOp(I
))
1045 // If the divisor is a select-of-constants, try to constant fold all div ops:
1046 // C / (select Cond, TrueC, FalseC) --> select Cond, (C / TrueC), (C / FalseC)
1047 // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
1048 if (match(Op0
, m_ImmConstant()) &&
1049 match(Op1
, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
1050 if (Instruction
*R
= FoldOpIntoSelect(I
, cast
<SelectInst
>(Op1
),
1051 /*FoldWithMultiUse*/ true))
1056 if (match(Op1
, m_APInt(C2
))) {
1060 // (X / C1) / C2 -> X / (C1*C2)
1061 if ((IsSigned
&& match(Op0
, m_SDiv(m_Value(X
), m_APInt(C1
)))) ||
1062 (!IsSigned
&& match(Op0
, m_UDiv(m_Value(X
), m_APInt(C1
))))) {
1063 APInt
Product(C1
->getBitWidth(), /*val=*/0ULL, IsSigned
);
1064 if (!multiplyOverflows(*C1
, *C2
, Product
, IsSigned
))
1065 return BinaryOperator::Create(I
.getOpcode(), X
,
1066 ConstantInt::get(Ty
, Product
));
1069 APInt
Quotient(C2
->getBitWidth(), /*val=*/0ULL, IsSigned
);
1070 if ((IsSigned
&& match(Op0
, m_NSWMul(m_Value(X
), m_APInt(C1
)))) ||
1071 (!IsSigned
&& match(Op0
, m_NUWMul(m_Value(X
), m_APInt(C1
))))) {
1073 // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
1074 if (isMultiple(*C2
, *C1
, Quotient
, IsSigned
)) {
1075 auto *NewDiv
= BinaryOperator::Create(I
.getOpcode(), X
,
1076 ConstantInt::get(Ty
, Quotient
));
1077 NewDiv
->setIsExact(I
.isExact());
1081 // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
1082 if (isMultiple(*C1
, *C2
, Quotient
, IsSigned
)) {
1083 auto *Mul
= BinaryOperator::Create(Instruction::Mul
, X
,
1084 ConstantInt::get(Ty
, Quotient
));
1085 auto *OBO
= cast
<OverflowingBinaryOperator
>(Op0
);
1086 Mul
->setHasNoUnsignedWrap(!IsSigned
&& OBO
->hasNoUnsignedWrap());
1087 Mul
->setHasNoSignedWrap(OBO
->hasNoSignedWrap());
1092 if ((IsSigned
&& match(Op0
, m_NSWShl(m_Value(X
), m_APInt(C1
))) &&
1093 C1
->ult(C1
->getBitWidth() - 1)) ||
1094 (!IsSigned
&& match(Op0
, m_NUWShl(m_Value(X
), m_APInt(C1
))) &&
1095 C1
->ult(C1
->getBitWidth()))) {
1096 APInt C1Shifted
= APInt::getOneBitSet(
1097 C1
->getBitWidth(), static_cast<unsigned>(C1
->getZExtValue()));
1099 // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1.
1100 if (isMultiple(*C2
, C1Shifted
, Quotient
, IsSigned
)) {
1101 auto *BO
= BinaryOperator::Create(I
.getOpcode(), X
,
1102 ConstantInt::get(Ty
, Quotient
));
1103 BO
->setIsExact(I
.isExact());
1107 // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2.
1108 if (isMultiple(C1Shifted
, *C2
, Quotient
, IsSigned
)) {
1109 auto *Mul
= BinaryOperator::Create(Instruction::Mul
, X
,
1110 ConstantInt::get(Ty
, Quotient
));
1111 auto *OBO
= cast
<OverflowingBinaryOperator
>(Op0
);
1112 Mul
->setHasNoUnsignedWrap(!IsSigned
&& OBO
->hasNoUnsignedWrap());
1113 Mul
->setHasNoSignedWrap(OBO
->hasNoSignedWrap());
1118 // Distribute div over add to eliminate a matching div/mul pair:
1119 // ((X * C2) + C1) / C2 --> X + C1/C2
1120 // We need a multiple of the divisor for a signed add constant, but
1121 // unsigned is fine with any constant pair.
1123 match(Op0
, m_NSWAdd(m_NSWMul(m_Value(X
), m_SpecificInt(*C2
)),
1125 isMultiple(*C1
, *C2
, Quotient
, IsSigned
)) {
1126 return BinaryOperator::CreateNSWAdd(X
, ConstantInt::get(Ty
, Quotient
));
1129 match(Op0
, m_NUWAdd(m_NUWMul(m_Value(X
), m_SpecificInt(*C2
)),
1131 return BinaryOperator::CreateNUWAdd(X
,
1132 ConstantInt::get(Ty
, C1
->udiv(*C2
)));
1135 if (!C2
->isZero()) // avoid X udiv 0
1136 if (Instruction
*FoldedDiv
= foldBinOpIntoSelectOrPhi(I
))
1140 if (match(Op0
, m_One())) {
1141 assert(!Ty
->isIntOrIntVectorTy(1) && "i1 divide not removed?");
1143 // 1 / 0 --> undef ; 1 / 1 --> 1 ; 1 / -1 --> -1 ; 1 / anything else --> 0
1144 // (Op1 + 1) u< 3 ? Op1 : 0
1145 // Op1 must be frozen because we are increasing its number of uses.
1146 Value
*F1
= Builder
.CreateFreeze(Op1
, Op1
->getName() + ".fr");
1147 Value
*Inc
= Builder
.CreateAdd(F1
, Op0
);
1148 Value
*Cmp
= Builder
.CreateICmpULT(Inc
, ConstantInt::get(Ty
, 3));
1149 return SelectInst::Create(Cmp
, F1
, ConstantInt::get(Ty
, 0));
1151 // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
1152 // result is one, otherwise it's zero.
1153 return new ZExtInst(Builder
.CreateICmpEQ(Op1
, Op0
), Ty
);
1157 // See if we can fold away this div instruction.
1158 if (SimplifyDemandedInstructionBits(I
))
1161 // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
1163 if (match(Op0
, m_Sub(m_Value(X
), m_Value(Z
)))) // (X - Z) / Y; Y = Op1
1164 if ((IsSigned
&& match(Z
, m_SRem(m_Specific(X
), m_Specific(Op1
)))) ||
1165 (!IsSigned
&& match(Z
, m_URem(m_Specific(X
), m_Specific(Op1
)))))
1166 return BinaryOperator::Create(I
.getOpcode(), X
, Op1
);
1168 // (X << Y) / X -> 1 << Y
1170 if (IsSigned
&& match(Op0
, m_NSWShl(m_Specific(Op1
), m_Value(Y
))))
1171 return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty
, 1), Y
);
1172 if (!IsSigned
&& match(Op0
, m_NUWShl(m_Specific(Op1
), m_Value(Y
))))
1173 return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty
, 1), Y
);
1175 // X / (X * Y) -> 1 / Y if the multiplication does not overflow.
1176 if (match(Op1
, m_c_Mul(m_Specific(Op0
), m_Value(Y
)))) {
1177 bool HasNSW
= cast
<OverflowingBinaryOperator
>(Op1
)->hasNoSignedWrap();
1178 bool HasNUW
= cast
<OverflowingBinaryOperator
>(Op1
)->hasNoUnsignedWrap();
1179 if ((IsSigned
&& HasNSW
) || (!IsSigned
&& HasNUW
)) {
1180 replaceOperand(I
, 0, ConstantInt::get(Ty
, 1));
1181 replaceOperand(I
, 1, Y
);
1186 // (X << Z) / (X * Y) -> (1 << Z) / Y
1187 // TODO: Handle sdiv.
1188 if (!IsSigned
&& Op1
->hasOneUse() &&
1189 match(Op0
, m_NUWShl(m_Value(X
), m_Value(Z
))) &&
1190 match(Op1
, m_c_Mul(m_Specific(X
), m_Value(Y
))))
1191 if (cast
<OverflowingBinaryOperator
>(Op1
)->hasNoUnsignedWrap()) {
1192 Instruction
*NewDiv
= BinaryOperator::CreateUDiv(
1193 Builder
.CreateShl(ConstantInt::get(Ty
, 1), Z
, "", /*NUW*/ true), Y
);
1194 NewDiv
->setIsExact(I
.isExact());
1198 if (Value
*R
= foldIDivShl(I
, Builder
))
1199 return replaceInstUsesWith(I
, R
);
1201 // With the appropriate no-wrap constraint, remove a multiply by the divisor
1202 // after peeking through another divide:
1203 // ((Op1 * X) / Y) / Op1 --> X / Y
1204 if (match(Op0
, m_BinOp(I
.getOpcode(), m_c_Mul(m_Specific(Op1
), m_Value(X
)),
1206 auto *InnerDiv
= cast
<PossiblyExactOperator
>(Op0
);
1207 auto *Mul
= cast
<OverflowingBinaryOperator
>(InnerDiv
->getOperand(0));
1208 Instruction
*NewDiv
= nullptr;
1209 if (!IsSigned
&& Mul
->hasNoUnsignedWrap())
1210 NewDiv
= BinaryOperator::CreateUDiv(X
, Y
);
1211 else if (IsSigned
&& Mul
->hasNoSignedWrap())
1212 NewDiv
= BinaryOperator::CreateSDiv(X
, Y
);
1214 // Exact propagates only if both of the original divides are exact.
1216 NewDiv
->setIsExact(I
.isExact() && InnerDiv
->isExact());
1221 // (X * Y) / (X * Z) --> Y / Z (and commuted variants)
1222 if (match(Op0
, m_Mul(m_Value(X
), m_Value(Y
)))) {
1223 auto OB0HasNSW
= cast
<OverflowingBinaryOperator
>(Op0
)->hasNoSignedWrap();
1224 auto OB0HasNUW
= cast
<OverflowingBinaryOperator
>(Op0
)->hasNoUnsignedWrap();
1226 auto CreateDivOrNull
= [&](Value
*A
, Value
*B
) -> Instruction
* {
1227 auto OB1HasNSW
= cast
<OverflowingBinaryOperator
>(Op1
)->hasNoSignedWrap();
1229 cast
<OverflowingBinaryOperator
>(Op1
)->hasNoUnsignedWrap();
1230 const APInt
*C1
, *C2
;
1231 if (IsSigned
&& OB0HasNSW
) {
1232 if (OB1HasNSW
&& match(B
, m_APInt(C1
)) && !C1
->isAllOnes())
1233 return BinaryOperator::CreateSDiv(A
, B
);
1235 if (!IsSigned
&& OB0HasNUW
) {
1237 return BinaryOperator::CreateUDiv(A
, B
);
1238 if (match(A
, m_APInt(C1
)) && match(B
, m_APInt(C2
)) && C2
->ule(*C1
))
1239 return BinaryOperator::CreateUDiv(A
, B
);
1244 if (match(Op1
, m_c_Mul(m_Specific(X
), m_Value(Z
)))) {
1245 if (auto *Val
= CreateDivOrNull(Y
, Z
))
1248 if (match(Op1
, m_c_Mul(m_Specific(Y
), m_Value(Z
)))) {
1249 if (auto *Val
= CreateDivOrNull(X
, Z
))
1256 static const unsigned MaxDepth
= 6;
1258 // Take the exact integer log2 of the value. If DoFold is true, create the
1259 // actual instructions, otherwise return a non-null dummy value. Return nullptr
1261 static Value
*takeLog2(IRBuilderBase
&Builder
, Value
*Op
, unsigned Depth
,
1262 bool AssumeNonZero
, bool DoFold
) {
1263 auto IfFold
= [DoFold
](function_ref
<Value
*()> Fn
) {
1265 return reinterpret_cast<Value
*>(-1);
1269 // FIXME: assert that Op1 isn't/doesn't contain undef.
1272 if (match(Op
, m_Power2()))
1273 return IfFold([&]() {
1274 Constant
*C
= ConstantExpr::getExactLogBase2(cast
<Constant
>(Op
));
1276 llvm_unreachable("Failed to constant fold udiv -> logbase2");
1280 // The remaining tests are all recursive, so bail out if we hit the limit.
1281 if (Depth
++ == MaxDepth
)
1284 // log2(zext X) -> zext log2(X)
1285 // FIXME: Require one use?
1287 if (match(Op
, m_ZExt(m_Value(X
))))
1288 if (Value
*LogX
= takeLog2(Builder
, X
, Depth
, AssumeNonZero
, DoFold
))
1289 return IfFold([&]() { return Builder
.CreateZExt(LogX
, Op
->getType()); });
1291 // log2(X << Y) -> log2(X) + Y
1292 // FIXME: Require one use unless X is 1?
1293 if (match(Op
, m_Shl(m_Value(X
), m_Value(Y
)))) {
1294 auto *BO
= cast
<OverflowingBinaryOperator
>(Op
);
1295 // nuw will be set if the `shl` is trivially non-zero.
1296 if (AssumeNonZero
|| BO
->hasNoUnsignedWrap() || BO
->hasNoSignedWrap())
1297 if (Value
*LogX
= takeLog2(Builder
, X
, Depth
, AssumeNonZero
, DoFold
))
1298 return IfFold([&]() { return Builder
.CreateAdd(LogX
, Y
); });
1301 // log2(Cond ? X : Y) -> Cond ? log2(X) : log2(Y)
1302 // FIXME: missed optimization: if one of the hands of select is/contains
1303 // undef, just directly pick the other one.
1304 // FIXME: can both hands contain undef?
1305 // FIXME: Require one use?
1306 if (SelectInst
*SI
= dyn_cast
<SelectInst
>(Op
))
1307 if (Value
*LogX
= takeLog2(Builder
, SI
->getOperand(1), Depth
,
1308 AssumeNonZero
, DoFold
))
1309 if (Value
*LogY
= takeLog2(Builder
, SI
->getOperand(2), Depth
,
1310 AssumeNonZero
, DoFold
))
1311 return IfFold([&]() {
1312 return Builder
.CreateSelect(SI
->getOperand(0), LogX
, LogY
);
1315 // log2(umin(X, Y)) -> umin(log2(X), log2(Y))
1316 // log2(umax(X, Y)) -> umax(log2(X), log2(Y))
1317 auto *MinMax
= dyn_cast
<MinMaxIntrinsic
>(Op
);
1318 if (MinMax
&& MinMax
->hasOneUse() && !MinMax
->isSigned()) {
1319 // Use AssumeNonZero as false here. Otherwise we can hit case where
1320 // log2(umax(X, Y)) != umax(log2(X), log2(Y)) (because overflow).
1321 if (Value
*LogX
= takeLog2(Builder
, MinMax
->getLHS(), Depth
,
1322 /*AssumeNonZero*/ false, DoFold
))
1323 if (Value
*LogY
= takeLog2(Builder
, MinMax
->getRHS(), Depth
,
1324 /*AssumeNonZero*/ false, DoFold
))
1325 return IfFold([&]() {
1326 return Builder
.CreateBinaryIntrinsic(MinMax
->getIntrinsicID(), LogX
,
1334 /// If we have zero-extended operands of an unsigned div or rem, we may be able
1335 /// to narrow the operation (sink the zext below the math).
1336 static Instruction
*narrowUDivURem(BinaryOperator
&I
,
1337 InstCombinerImpl
&IC
) {
1338 Instruction::BinaryOps Opcode
= I
.getOpcode();
1339 Value
*N
= I
.getOperand(0);
1340 Value
*D
= I
.getOperand(1);
1341 Type
*Ty
= I
.getType();
1343 if (match(N
, m_ZExt(m_Value(X
))) && match(D
, m_ZExt(m_Value(Y
))) &&
1344 X
->getType() == Y
->getType() && (N
->hasOneUse() || D
->hasOneUse())) {
1345 // udiv (zext X), (zext Y) --> zext (udiv X, Y)
1346 // urem (zext X), (zext Y) --> zext (urem X, Y)
1347 Value
*NarrowOp
= IC
.Builder
.CreateBinOp(Opcode
, X
, Y
);
1348 return new ZExtInst(NarrowOp
, Ty
);
1352 if (isa
<Instruction
>(N
) && match(N
, m_OneUse(m_ZExt(m_Value(X
)))) &&
1353 match(D
, m_Constant(C
))) {
1354 // If the constant is the same in the smaller type, use the narrow version.
1355 Constant
*TruncC
= IC
.getLosslessUnsignedTrunc(C
, X
->getType());
1359 // udiv (zext X), C --> zext (udiv X, C')
1360 // urem (zext X), C --> zext (urem X, C')
1361 return new ZExtInst(IC
.Builder
.CreateBinOp(Opcode
, X
, TruncC
), Ty
);
1363 if (isa
<Instruction
>(D
) && match(D
, m_OneUse(m_ZExt(m_Value(X
)))) &&
1364 match(N
, m_Constant(C
))) {
1365 // If the constant is the same in the smaller type, use the narrow version.
1366 Constant
*TruncC
= IC
.getLosslessUnsignedTrunc(C
, X
->getType());
1370 // udiv C, (zext X) --> zext (udiv C', X)
1371 // urem C, (zext X) --> zext (urem C', X)
1372 return new ZExtInst(IC
.Builder
.CreateBinOp(Opcode
, TruncC
, X
), Ty
);
1378 Instruction
*InstCombinerImpl::visitUDiv(BinaryOperator
&I
) {
1379 if (Value
*V
= simplifyUDivInst(I
.getOperand(0), I
.getOperand(1), I
.isExact(),
1380 SQ
.getWithInstruction(&I
)))
1381 return replaceInstUsesWith(I
, V
);
1383 if (Instruction
*X
= foldVectorBinop(I
))
1386 // Handle the integer div common cases
1387 if (Instruction
*Common
= commonIDivTransforms(I
))
1390 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
1392 const APInt
*C1
, *C2
;
1393 if (match(Op0
, m_LShr(m_Value(X
), m_APInt(C1
))) && match(Op1
, m_APInt(C2
))) {
1394 // (X lshr C1) udiv C2 --> X udiv (C2 << C1)
1396 APInt C2ShlC1
= C2
->ushl_ov(*C1
, Overflow
);
1398 bool IsExact
= I
.isExact() && match(Op0
, m_Exact(m_Value()));
1399 BinaryOperator
*BO
= BinaryOperator::CreateUDiv(
1400 X
, ConstantInt::get(X
->getType(), C2ShlC1
));
1407 // Op0 / C where C is large (negative) --> zext (Op0 >= C)
1408 // TODO: Could use isKnownNegative() to handle non-constant values.
1409 Type
*Ty
= I
.getType();
1410 if (match(Op1
, m_Negative())) {
1411 Value
*Cmp
= Builder
.CreateICmpUGE(Op0
, Op1
);
1412 return CastInst::CreateZExtOrBitCast(Cmp
, Ty
);
1414 // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined)
1415 if (match(Op1
, m_SExt(m_Value(X
))) && X
->getType()->isIntOrIntVectorTy(1)) {
1416 Value
*Cmp
= Builder
.CreateICmpEQ(Op0
, ConstantInt::getAllOnesValue(Ty
));
1417 return CastInst::CreateZExtOrBitCast(Cmp
, Ty
);
1420 if (Instruction
*NarrowDiv
= narrowUDivURem(I
, *this))
1425 // Look through a right-shift to find the common factor:
1426 // ((Op1 *nuw A) >> B) / Op1 --> A >> B
1427 if (match(Op0
, m_LShr(m_NUWMul(m_Specific(Op1
), m_Value(A
)), m_Value(B
))) ||
1428 match(Op0
, m_LShr(m_NUWMul(m_Value(A
), m_Specific(Op1
)), m_Value(B
)))) {
1429 Instruction
*Lshr
= BinaryOperator::CreateLShr(A
, B
);
1430 if (I
.isExact() && cast
<PossiblyExactOperator
>(Op0
)->isExact())
1435 // Op1 udiv Op2 -> Op1 lshr log2(Op2), if log2() folds away.
1436 if (takeLog2(Builder
, Op1
, /*Depth*/ 0, /*AssumeNonZero*/ true,
1437 /*DoFold*/ false)) {
1438 Value
*Res
= takeLog2(Builder
, Op1
, /*Depth*/ 0,
1439 /*AssumeNonZero*/ true, /*DoFold*/ true);
1440 return replaceInstUsesWith(
1441 I
, Builder
.CreateLShr(Op0
, Res
, I
.getName(), I
.isExact()));
1447 Instruction
*InstCombinerImpl::visitSDiv(BinaryOperator
&I
) {
1448 if (Value
*V
= simplifySDivInst(I
.getOperand(0), I
.getOperand(1), I
.isExact(),
1449 SQ
.getWithInstruction(&I
)))
1450 return replaceInstUsesWith(I
, V
);
1452 if (Instruction
*X
= foldVectorBinop(I
))
1455 // Handle the integer div common cases
1456 if (Instruction
*Common
= commonIDivTransforms(I
))
1459 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
1460 Type
*Ty
= I
.getType();
1462 // sdiv Op0, -1 --> -Op0
1463 // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
1464 if (match(Op1
, m_AllOnes()) ||
1465 (match(Op1
, m_SExt(m_Value(X
))) && X
->getType()->isIntOrIntVectorTy(1)))
1466 return BinaryOperator::CreateNSWNeg(Op0
);
1468 // X / INT_MIN --> X == INT_MIN
1469 if (match(Op1
, m_SignMask()))
1470 return new ZExtInst(Builder
.CreateICmpEQ(Op0
, Op1
), Ty
);
1473 // sdiv exact X, 1<<C --> ashr exact X, C iff 1<<C is non-negative
1474 if (match(Op1
, m_Power2()) && match(Op1
, m_NonNegative())) {
1475 Constant
*C
= ConstantExpr::getExactLogBase2(cast
<Constant
>(Op1
));
1476 return BinaryOperator::CreateExactAShr(Op0
, C
);
1479 // sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative)
1481 if (match(Op1
, m_NSWShl(m_One(), m_Value(ShAmt
))))
1482 return BinaryOperator::CreateExactAShr(Op0
, ShAmt
);
1484 // sdiv exact X, -1<<C --> -(ashr exact X, C)
1485 if (match(Op1
, m_NegatedPower2())) {
1486 Constant
*NegPow2C
= ConstantExpr::getNeg(cast
<Constant
>(Op1
));
1487 Constant
*C
= ConstantExpr::getExactLogBase2(NegPow2C
);
1488 Value
*Ashr
= Builder
.CreateAShr(Op0
, C
, I
.getName() + ".neg", true);
1489 return BinaryOperator::CreateNSWNeg(Ashr
);
1494 if (match(Op1
, m_APInt(Op1C
))) {
1495 // If the dividend is sign-extended and the constant divisor is small enough
1496 // to fit in the source type, shrink the division to the narrower type:
1497 // (sext X) sdiv C --> sext (X sdiv C)
1499 if (match(Op0
, m_OneUse(m_SExt(m_Value(Op0Src
)))) &&
1500 Op0Src
->getType()->getScalarSizeInBits() >=
1501 Op1C
->getSignificantBits()) {
1503 // In the general case, we need to make sure that the dividend is not the
1504 // minimum signed value because dividing that by -1 is UB. But here, we
1505 // know that the -1 divisor case is already handled above.
1507 Constant
*NarrowDivisor
=
1508 ConstantExpr::getTrunc(cast
<Constant
>(Op1
), Op0Src
->getType());
1509 Value
*NarrowOp
= Builder
.CreateSDiv(Op0Src
, NarrowDivisor
);
1510 return new SExtInst(NarrowOp
, Ty
);
1513 // -X / C --> X / -C (if the negation doesn't overflow).
1514 // TODO: This could be enhanced to handle arbitrary vector constants by
1515 // checking if all elements are not the min-signed-val.
1516 if (!Op1C
->isMinSignedValue() &&
1517 match(Op0
, m_NSWSub(m_Zero(), m_Value(X
)))) {
1518 Constant
*NegC
= ConstantInt::get(Ty
, -(*Op1C
));
1519 Instruction
*BO
= BinaryOperator::CreateSDiv(X
, NegC
);
1520 BO
->setIsExact(I
.isExact());
1525 // -X / Y --> -(X / Y)
1527 if (match(&I
, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X
))), m_Value(Y
))))
1528 return BinaryOperator::CreateNSWNeg(
1529 Builder
.CreateSDiv(X
, Y
, I
.getName(), I
.isExact()));
1531 // abs(X) / X --> X > -1 ? 1 : -1
1532 // X / abs(X) --> X > -1 ? 1 : -1
1533 if (match(&I
, m_c_BinOp(
1534 m_OneUse(m_Intrinsic
<Intrinsic::abs
>(m_Value(X
), m_One())),
1536 Value
*Cond
= Builder
.CreateIsNotNeg(X
);
1537 return SelectInst::Create(Cond
, ConstantInt::get(Ty
, 1),
1538 ConstantInt::getAllOnesValue(Ty
));
1541 KnownBits KnownDividend
= computeKnownBits(Op0
, 0, &I
);
1543 (match(Op1
, m_Power2(Op1C
)) || match(Op1
, m_NegatedPower2(Op1C
))) &&
1544 KnownDividend
.countMinTrailingZeros() >= Op1C
->countr_zero()) {
1549 if (KnownDividend
.isNonNegative()) {
1550 // If both operands are unsigned, turn this into a udiv.
1551 if (isKnownNonNegative(Op1
, SQ
.getWithInstruction(&I
))) {
1552 auto *BO
= BinaryOperator::CreateUDiv(Op0
, Op1
, I
.getName());
1553 BO
->setIsExact(I
.isExact());
1557 if (match(Op1
, m_NegatedPower2())) {
1558 // X sdiv (-(1 << C)) -> -(X sdiv (1 << C)) ->
1559 // -> -(X udiv (1 << C)) -> -(X u>> C)
1560 Constant
*CNegLog2
= ConstantExpr::getExactLogBase2(
1561 ConstantExpr::getNeg(cast
<Constant
>(Op1
)));
1562 Value
*Shr
= Builder
.CreateLShr(Op0
, CNegLog2
, I
.getName(), I
.isExact());
1563 return BinaryOperator::CreateNeg(Shr
);
1566 if (isKnownToBeAPowerOfTwo(Op1
, /*OrZero*/ true, 0, &I
)) {
1567 // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1568 // Safe because the only negative value (1 << Y) can take on is
1569 // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1570 // the sign bit set.
1571 auto *BO
= BinaryOperator::CreateUDiv(Op0
, Op1
, I
.getName());
1572 BO
->setIsExact(I
.isExact());
1577 // -X / X --> X == INT_MIN ? 1 : -1
1578 if (isKnownNegation(Op0
, Op1
)) {
1579 APInt MinVal
= APInt::getSignedMinValue(Ty
->getScalarSizeInBits());
1580 Value
*Cond
= Builder
.CreateICmpEQ(Op0
, ConstantInt::get(Ty
, MinVal
));
1581 return SelectInst::Create(Cond
, ConstantInt::get(Ty
, 1),
1582 ConstantInt::getAllOnesValue(Ty
));
1587 /// Remove negation and try to convert division into multiplication.
1588 Instruction
*InstCombinerImpl::foldFDivConstantDivisor(BinaryOperator
&I
) {
1590 if (!match(I
.getOperand(1), m_Constant(C
)))
1593 // -X / C --> X / -C
1595 const DataLayout
&DL
= I
.getModule()->getDataLayout();
1596 if (match(I
.getOperand(0), m_FNeg(m_Value(X
))))
1597 if (Constant
*NegC
= ConstantFoldUnaryOpOperand(Instruction::FNeg
, C
, DL
))
1598 return BinaryOperator::CreateFDivFMF(X
, NegC
, &I
);
1600 // nnan X / +0.0 -> copysign(inf, X)
1601 if (I
.hasNoNaNs() && match(I
.getOperand(1), m_Zero())) {
1603 // TODO: nnan nsz X / -0.0 -> copysign(inf, X)
1604 CallInst
*CopySign
= B
.CreateIntrinsic(
1605 Intrinsic::copysign
, {C
->getType()},
1606 {ConstantFP::getInfinity(I
.getType()), I
.getOperand(0)}, &I
);
1607 CopySign
->takeName(&I
);
1608 return replaceInstUsesWith(I
, CopySign
);
1611 // If the constant divisor has an exact inverse, this is always safe. If not,
1612 // then we can still create a reciprocal if fast-math-flags allow it and the
1613 // constant is a regular number (not zero, infinite, or denormal).
1614 if (!(C
->hasExactInverseFP() || (I
.hasAllowReciprocal() && C
->isNormalFP())))
1617 // Disallow denormal constants because we don't know what would happen
1619 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1620 // denorms are flushed?
1621 auto *RecipC
= ConstantFoldBinaryOpOperands(
1622 Instruction::FDiv
, ConstantFP::get(I
.getType(), 1.0), C
, DL
);
1623 if (!RecipC
|| !RecipC
->isNormalFP())
1626 // X / C --> X * (1 / C)
1627 return BinaryOperator::CreateFMulFMF(I
.getOperand(0), RecipC
, &I
);
1630 /// Remove negation and try to reassociate constant math.
1631 static Instruction
*foldFDivConstantDividend(BinaryOperator
&I
) {
1633 if (!match(I
.getOperand(0), m_Constant(C
)))
1636 // C / -X --> -C / X
1638 const DataLayout
&DL
= I
.getModule()->getDataLayout();
1639 if (match(I
.getOperand(1), m_FNeg(m_Value(X
))))
1640 if (Constant
*NegC
= ConstantFoldUnaryOpOperand(Instruction::FNeg
, C
, DL
))
1641 return BinaryOperator::CreateFDivFMF(NegC
, X
, &I
);
1643 if (!I
.hasAllowReassoc() || !I
.hasAllowReciprocal())
1646 // Try to reassociate C / X expressions where X includes another constant.
1647 Constant
*C2
, *NewC
= nullptr;
1648 if (match(I
.getOperand(1), m_FMul(m_Value(X
), m_Constant(C2
)))) {
1649 // C / (X * C2) --> (C / C2) / X
1650 NewC
= ConstantFoldBinaryOpOperands(Instruction::FDiv
, C
, C2
, DL
);
1651 } else if (match(I
.getOperand(1), m_FDiv(m_Value(X
), m_Constant(C2
)))) {
1652 // C / (X / C2) --> (C * C2) / X
1653 NewC
= ConstantFoldBinaryOpOperands(Instruction::FMul
, C
, C2
, DL
);
1655 // Disallow denormal constants because we don't know what would happen
1657 // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1658 // denorms are flushed?
1659 if (!NewC
|| !NewC
->isNormalFP())
1662 return BinaryOperator::CreateFDivFMF(NewC
, X
, &I
);
1665 /// Negate the exponent of pow/exp to fold division-by-pow() into multiply.
1666 static Instruction
*foldFDivPowDivisor(BinaryOperator
&I
,
1667 InstCombiner::BuilderTy
&Builder
) {
1668 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
1669 auto *II
= dyn_cast
<IntrinsicInst
>(Op1
);
1670 if (!II
|| !II
->hasOneUse() || !I
.hasAllowReassoc() ||
1671 !I
.hasAllowReciprocal())
1674 // Z / pow(X, Y) --> Z * pow(X, -Y)
1675 // Z / exp{2}(Y) --> Z * exp{2}(-Y)
1676 // In the general case, this creates an extra instruction, but fmul allows
1677 // for better canonicalization and optimization than fdiv.
1678 Intrinsic::ID IID
= II
->getIntrinsicID();
1679 SmallVector
<Value
*> Args
;
1681 case Intrinsic::pow
:
1682 Args
.push_back(II
->getArgOperand(0));
1683 Args
.push_back(Builder
.CreateFNegFMF(II
->getArgOperand(1), &I
));
1685 case Intrinsic::powi
: {
1686 // Require 'ninf' assuming that makes powi(X, -INT_MIN) acceptable.
1687 // That is, X ** (huge negative number) is 0.0, ~1.0, or INF and so
1688 // dividing by that is INF, ~1.0, or 0.0. Code that uses powi allows
1689 // non-standard results, so this corner case should be acceptable if the
1690 // code rules out INF values.
1693 Args
.push_back(II
->getArgOperand(0));
1694 Args
.push_back(Builder
.CreateNeg(II
->getArgOperand(1)));
1695 Type
*Tys
[] = {I
.getType(), II
->getArgOperand(1)->getType()};
1696 Value
*Pow
= Builder
.CreateIntrinsic(IID
, Tys
, Args
, &I
);
1697 return BinaryOperator::CreateFMulFMF(Op0
, Pow
, &I
);
1699 case Intrinsic::exp
:
1700 case Intrinsic::exp2
:
1701 Args
.push_back(Builder
.CreateFNegFMF(II
->getArgOperand(0), &I
));
1706 Value
*Pow
= Builder
.CreateIntrinsic(IID
, I
.getType(), Args
, &I
);
1707 return BinaryOperator::CreateFMulFMF(Op0
, Pow
, &I
);
1710 Instruction
*InstCombinerImpl::visitFDiv(BinaryOperator
&I
) {
1711 Module
*M
= I
.getModule();
1713 if (Value
*V
= simplifyFDivInst(I
.getOperand(0), I
.getOperand(1),
1714 I
.getFastMathFlags(),
1715 SQ
.getWithInstruction(&I
)))
1716 return replaceInstUsesWith(I
, V
);
1718 if (Instruction
*X
= foldVectorBinop(I
))
1721 if (Instruction
*Phi
= foldBinopWithPhiOperands(I
))
1724 if (Instruction
*R
= foldFDivConstantDivisor(I
))
1727 if (Instruction
*R
= foldFDivConstantDividend(I
))
1730 if (Instruction
*R
= foldFPSignBitOps(I
))
1733 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
1734 if (isa
<Constant
>(Op0
))
1735 if (SelectInst
*SI
= dyn_cast
<SelectInst
>(Op1
))
1736 if (Instruction
*R
= FoldOpIntoSelect(I
, SI
))
1739 if (isa
<Constant
>(Op1
))
1740 if (SelectInst
*SI
= dyn_cast
<SelectInst
>(Op0
))
1741 if (Instruction
*R
= FoldOpIntoSelect(I
, SI
))
1744 if (I
.hasAllowReassoc() && I
.hasAllowReciprocal()) {
1746 if (match(Op0
, m_OneUse(m_FDiv(m_Value(X
), m_Value(Y
)))) &&
1747 (!isa
<Constant
>(Y
) || !isa
<Constant
>(Op1
))) {
1748 // (X / Y) / Z => X / (Y * Z)
1749 Value
*YZ
= Builder
.CreateFMulFMF(Y
, Op1
, &I
);
1750 return BinaryOperator::CreateFDivFMF(X
, YZ
, &I
);
1752 if (match(Op1
, m_OneUse(m_FDiv(m_Value(X
), m_Value(Y
)))) &&
1753 (!isa
<Constant
>(Y
) || !isa
<Constant
>(Op0
))) {
1754 // Z / (X / Y) => (Y * Z) / X
1755 Value
*YZ
= Builder
.CreateFMulFMF(Y
, Op0
, &I
);
1756 return BinaryOperator::CreateFDivFMF(YZ
, X
, &I
);
1758 // Z / (1.0 / Y) => (Y * Z)
1760 // This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0. The
1761 // m_OneUse check is avoided because even in the case of the multiple uses
1762 // for 1.0/Y, the number of instructions remain the same and a division is
1763 // replaced by a multiplication.
1764 if (match(Op1
, m_FDiv(m_SpecificFP(1.0), m_Value(Y
))))
1765 return BinaryOperator::CreateFMulFMF(Y
, Op0
, &I
);
1768 if (I
.hasAllowReassoc() && Op0
->hasOneUse() && Op1
->hasOneUse()) {
1769 // sin(X) / cos(X) -> tan(X)
1770 // cos(X) / sin(X) -> 1/tan(X) (cotangent)
1772 bool IsTan
= match(Op0
, m_Intrinsic
<Intrinsic::sin
>(m_Value(X
))) &&
1773 match(Op1
, m_Intrinsic
<Intrinsic::cos
>(m_Specific(X
)));
1775 !IsTan
&& match(Op0
, m_Intrinsic
<Intrinsic::cos
>(m_Value(X
))) &&
1776 match(Op1
, m_Intrinsic
<Intrinsic::sin
>(m_Specific(X
)));
1778 if ((IsTan
|| IsCot
) && hasFloatFn(M
, &TLI
, I
.getType(), LibFunc_tan
,
1779 LibFunc_tanf
, LibFunc_tanl
)) {
1781 IRBuilder
<>::FastMathFlagGuard
FMFGuard(B
);
1782 B
.setFastMathFlags(I
.getFastMathFlags());
1783 AttributeList Attrs
=
1784 cast
<CallBase
>(Op0
)->getCalledFunction()->getAttributes();
1785 Value
*Res
= emitUnaryFloatFnCall(X
, &TLI
, LibFunc_tan
, LibFunc_tanf
,
1786 LibFunc_tanl
, B
, Attrs
);
1788 Res
= B
.CreateFDiv(ConstantFP::get(I
.getType(), 1.0), Res
);
1789 return replaceInstUsesWith(I
, Res
);
1793 // X / (X * Y) --> 1.0 / Y
1794 // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
1795 // We can ignore the possibility that X is infinity because INF/INF is NaN.
1797 if (I
.hasNoNaNs() && I
.hasAllowReassoc() &&
1798 match(Op1
, m_c_FMul(m_Specific(Op0
), m_Value(Y
)))) {
1799 replaceOperand(I
, 0, ConstantFP::get(I
.getType(), 1.0));
1800 replaceOperand(I
, 1, Y
);
1804 // X / fabs(X) -> copysign(1.0, X)
1805 // fabs(X) / X -> copysign(1.0, X)
1806 if (I
.hasNoNaNs() && I
.hasNoInfs() &&
1807 (match(&I
, m_FDiv(m_Value(X
), m_FAbs(m_Deferred(X
)))) ||
1808 match(&I
, m_FDiv(m_FAbs(m_Value(X
)), m_Deferred(X
))))) {
1809 Value
*V
= Builder
.CreateBinaryIntrinsic(
1810 Intrinsic::copysign
, ConstantFP::get(I
.getType(), 1.0), X
, &I
);
1811 return replaceInstUsesWith(I
, V
);
1814 if (Instruction
*Mul
= foldFDivPowDivisor(I
, Builder
))
1817 // pow(X, Y) / X --> pow(X, Y-1)
1818 if (I
.hasAllowReassoc() &&
1819 match(Op0
, m_OneUse(m_Intrinsic
<Intrinsic::pow
>(m_Specific(Op1
),
1822 Builder
.CreateFAddFMF(Y
, ConstantFP::get(I
.getType(), -1.0), &I
);
1823 Value
*Pow
= Builder
.CreateBinaryIntrinsic(Intrinsic::pow
, Op1
, Y1
, &I
);
1824 return replaceInstUsesWith(I
, Pow
);
1827 // powi(X, Y) / X --> powi(X, Y-1)
1828 // This is legal when (Y - 1) can't wraparound, in which case reassoc and nnan
1830 // TODO: Multi-use may be also better off creating Powi(x,y-1)
1831 if (I
.hasAllowReassoc() && I
.hasNoNaNs() &&
1832 match(Op0
, m_OneUse(m_Intrinsic
<Intrinsic::powi
>(m_Specific(Op1
),
1834 willNotOverflowSignedSub(Y
, ConstantInt::get(Y
->getType(), 1), I
)) {
1835 Constant
*NegOne
= ConstantInt::getAllOnesValue(Y
->getType());
1836 Value
*Y1
= Builder
.CreateAdd(Y
, NegOne
);
1837 Type
*Types
[] = {Op1
->getType(), Y1
->getType()};
1838 Value
*Pow
= Builder
.CreateIntrinsic(Intrinsic::powi
, Types
, {Op1
, Y1
}, &I
);
1839 return replaceInstUsesWith(I
, Pow
);
1845 // Variety of transform for:
1846 // (urem/srem (mul X, Y), (mul X, Z))
1847 // (urem/srem (shl X, Y), (shl X, Z))
1848 // (urem/srem (shl Y, X), (shl Z, X))
1849 // NB: The shift cases are really just extensions of the mul case. We treat
1850 // shift as Val * (1 << Amt).
1851 static Instruction
*simplifyIRemMulShl(BinaryOperator
&I
,
1852 InstCombinerImpl
&IC
) {
1853 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1), *X
= nullptr;
1855 bool ShiftByX
= false;
1857 // If V is not nullptr, it will be matched using m_Specific.
1858 auto MatchShiftOrMulXC
= [](Value
*Op
, Value
*&V
, APInt
&C
) -> bool {
1859 const APInt
*Tmp
= nullptr;
1860 if ((!V
&& match(Op
, m_Mul(m_Value(V
), m_APInt(Tmp
)))) ||
1861 (V
&& match(Op
, m_Mul(m_Specific(V
), m_APInt(Tmp
)))))
1863 else if ((!V
&& match(Op
, m_Shl(m_Value(V
), m_APInt(Tmp
)))) ||
1864 (V
&& match(Op
, m_Shl(m_Specific(V
), m_APInt(Tmp
)))))
1865 C
= APInt(Tmp
->getBitWidth(), 1) << *Tmp
;
1869 // Reset `V` so we don't start with specific value on next match attempt.
1874 auto MatchShiftCX
= [](Value
*Op
, APInt
&C
, Value
*&V
) -> bool {
1875 const APInt
*Tmp
= nullptr;
1876 if ((!V
&& match(Op
, m_Shl(m_APInt(Tmp
), m_Value(V
)))) ||
1877 (V
&& match(Op
, m_Shl(m_APInt(Tmp
), m_Specific(V
))))) {
1882 // Reset `V` so we don't start with specific value on next match attempt.
1887 if (MatchShiftOrMulXC(Op0
, X
, Y
) && MatchShiftOrMulXC(Op1
, X
, Z
)) {
1889 } else if (MatchShiftCX(Op0
, Y
, X
) && MatchShiftCX(Op1
, Z
, X
)) {
1895 bool IsSRem
= I
.getOpcode() == Instruction::SRem
;
1897 OverflowingBinaryOperator
*BO0
= cast
<OverflowingBinaryOperator
>(Op0
);
1898 // TODO: We may be able to deduce more about nsw/nuw of BO0/BO1 based on Y >=
1900 bool BO0HasNSW
= BO0
->hasNoSignedWrap();
1901 bool BO0HasNUW
= BO0
->hasNoUnsignedWrap();
1902 bool BO0NoWrap
= IsSRem
? BO0HasNSW
: BO0HasNUW
;
1904 APInt RemYZ
= IsSRem
? Y
.srem(Z
) : Y
.urem(Z
);
1905 // (rem (mul nuw/nsw X, Y), (mul X, Z))
1906 // if (rem Y, Z) == 0
1908 if (RemYZ
.isZero() && BO0NoWrap
)
1909 return IC
.replaceInstUsesWith(I
, ConstantInt::getNullValue(I
.getType()));
1911 // Helper function to emit either (RemSimplificationC << X) or
1912 // (RemSimplificationC * X) depending on whether we matched Op0/Op1 as
1913 // (shl V, X) or (mul V, X) respectively.
1914 auto CreateMulOrShift
=
1915 [&](const APInt
&RemSimplificationC
) -> BinaryOperator
* {
1916 Value
*RemSimplification
=
1917 ConstantInt::get(I
.getType(), RemSimplificationC
);
1918 return ShiftByX
? BinaryOperator::CreateShl(RemSimplification
, X
)
1919 : BinaryOperator::CreateMul(X
, RemSimplification
);
1922 OverflowingBinaryOperator
*BO1
= cast
<OverflowingBinaryOperator
>(Op1
);
1923 bool BO1HasNSW
= BO1
->hasNoSignedWrap();
1924 bool BO1HasNUW
= BO1
->hasNoUnsignedWrap();
1925 bool BO1NoWrap
= IsSRem
? BO1HasNSW
: BO1HasNUW
;
1926 // (rem (mul X, Y), (mul nuw/nsw X, Z))
1927 // if (rem Y, Z) == Y
1928 // -> (mul nuw/nsw X, Y)
1929 if (RemYZ
== Y
&& BO1NoWrap
) {
1930 BinaryOperator
*BO
= CreateMulOrShift(Y
);
1931 // Copy any overflow flags from Op0.
1932 BO
->setHasNoSignedWrap(IsSRem
|| BO0HasNSW
);
1933 BO
->setHasNoUnsignedWrap(!IsSRem
|| BO0HasNUW
);
1937 // (rem (mul nuw/nsw X, Y), (mul {nsw} X, Z))
1939 // -> (mul {nuw} nsw X, (rem Y, Z))
1940 if (Y
.uge(Z
) && (IsSRem
? (BO0HasNSW
&& BO1HasNSW
) : BO0HasNUW
)) {
1941 BinaryOperator
*BO
= CreateMulOrShift(RemYZ
);
1942 BO
->setHasNoSignedWrap();
1943 BO
->setHasNoUnsignedWrap(BO0HasNUW
);
1950 /// This function implements the transforms common to both integer remainder
1951 /// instructions (urem and srem). It is called by the visitors to those integer
1952 /// remainder instructions.
1953 /// Common integer remainder transforms
1954 Instruction
*InstCombinerImpl::commonIRemTransforms(BinaryOperator
&I
) {
1955 if (Instruction
*Phi
= foldBinopWithPhiOperands(I
))
1958 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
1960 // The RHS is known non-zero.
1961 if (Value
*V
= simplifyValueKnownNonZero(I
.getOperand(1), *this, I
))
1962 return replaceOperand(I
, 1, V
);
1964 // Handle cases involving: rem X, (select Cond, Y, Z)
1965 if (simplifyDivRemOfSelectWithZeroOp(I
))
1968 // If the divisor is a select-of-constants, try to constant fold all rem ops:
1969 // C % (select Cond, TrueC, FalseC) --> select Cond, (C % TrueC), (C % FalseC)
1970 // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
1971 if (match(Op0
, m_ImmConstant()) &&
1972 match(Op1
, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
1973 if (Instruction
*R
= FoldOpIntoSelect(I
, cast
<SelectInst
>(Op1
),
1974 /*FoldWithMultiUse*/ true))
1978 if (isa
<Constant
>(Op1
)) {
1979 if (Instruction
*Op0I
= dyn_cast
<Instruction
>(Op0
)) {
1980 if (SelectInst
*SI
= dyn_cast
<SelectInst
>(Op0I
)) {
1981 if (Instruction
*R
= FoldOpIntoSelect(I
, SI
))
1983 } else if (auto *PN
= dyn_cast
<PHINode
>(Op0I
)) {
1984 const APInt
*Op1Int
;
1985 if (match(Op1
, m_APInt(Op1Int
)) && !Op1Int
->isMinValue() &&
1986 (I
.getOpcode() == Instruction::URem
||
1987 !Op1Int
->isMinSignedValue())) {
1988 // foldOpIntoPhi will speculate instructions to the end of the PHI's
1989 // predecessor blocks, so do this only if we know the srem or urem
1991 if (Instruction
*NV
= foldOpIntoPhi(I
, PN
))
1996 // See if we can fold away this rem instruction.
1997 if (SimplifyDemandedInstructionBits(I
))
2002 if (Instruction
*R
= simplifyIRemMulShl(I
, *this))
2008 Instruction
*InstCombinerImpl::visitURem(BinaryOperator
&I
) {
2009 if (Value
*V
= simplifyURemInst(I
.getOperand(0), I
.getOperand(1),
2010 SQ
.getWithInstruction(&I
)))
2011 return replaceInstUsesWith(I
, V
);
2013 if (Instruction
*X
= foldVectorBinop(I
))
2016 if (Instruction
*common
= commonIRemTransforms(I
))
2019 if (Instruction
*NarrowRem
= narrowUDivURem(I
, *this))
2022 // X urem Y -> X and Y-1, where Y is a power of 2,
2023 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
2024 Type
*Ty
= I
.getType();
2025 if (isKnownToBeAPowerOfTwo(Op1
, /*OrZero*/ true, 0, &I
)) {
2026 // This may increase instruction count, we don't enforce that Y is a
2028 Constant
*N1
= Constant::getAllOnesValue(Ty
);
2029 Value
*Add
= Builder
.CreateAdd(Op1
, N1
);
2030 return BinaryOperator::CreateAnd(Op0
, Add
);
2033 // 1 urem X -> zext(X != 1)
2034 if (match(Op0
, m_One())) {
2035 Value
*Cmp
= Builder
.CreateICmpNE(Op1
, ConstantInt::get(Ty
, 1));
2036 return CastInst::CreateZExtOrBitCast(Cmp
, Ty
);
2039 // Op0 urem C -> Op0 < C ? Op0 : Op0 - C, where C >= signbit.
2040 // Op0 must be frozen because we are increasing its number of uses.
2041 if (match(Op1
, m_Negative())) {
2042 Value
*F0
= Builder
.CreateFreeze(Op0
, Op0
->getName() + ".fr");
2043 Value
*Cmp
= Builder
.CreateICmpULT(F0
, Op1
);
2044 Value
*Sub
= Builder
.CreateSub(F0
, Op1
);
2045 return SelectInst::Create(Cmp
, F0
, Sub
);
2048 // If the divisor is a sext of a boolean, then the divisor must be max
2049 // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also
2050 // max unsigned value. In that case, the remainder is 0:
2051 // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0
2053 if (match(Op1
, m_SExt(m_Value(X
))) && X
->getType()->isIntOrIntVectorTy(1)) {
2054 Value
*FrozenOp0
= Builder
.CreateFreeze(Op0
, Op0
->getName() + ".frozen");
2056 Builder
.CreateICmpEQ(FrozenOp0
, ConstantInt::getAllOnesValue(Ty
));
2057 return SelectInst::Create(Cmp
, ConstantInt::getNullValue(Ty
), FrozenOp0
);
2060 // For "(X + 1) % Op1" and if (X u< Op1) => (X + 1) == Op1 ? 0 : X + 1 .
2061 if (match(Op0
, m_Add(m_Value(X
), m_One()))) {
2063 simplifyICmpInst(ICmpInst::ICMP_ULT
, X
, Op1
, SQ
.getWithInstruction(&I
));
2064 if (Val
&& match(Val
, m_One())) {
2065 Value
*FrozenOp0
= Builder
.CreateFreeze(Op0
, Op0
->getName() + ".frozen");
2066 Value
*Cmp
= Builder
.CreateICmpEQ(FrozenOp0
, Op1
);
2067 return SelectInst::Create(Cmp
, ConstantInt::getNullValue(Ty
), FrozenOp0
);
2074 Instruction
*InstCombinerImpl::visitSRem(BinaryOperator
&I
) {
2075 if (Value
*V
= simplifySRemInst(I
.getOperand(0), I
.getOperand(1),
2076 SQ
.getWithInstruction(&I
)))
2077 return replaceInstUsesWith(I
, V
);
2079 if (Instruction
*X
= foldVectorBinop(I
))
2082 // Handle the integer rem common cases
2083 if (Instruction
*Common
= commonIRemTransforms(I
))
2086 Value
*Op0
= I
.getOperand(0), *Op1
= I
.getOperand(1);
2090 if (match(Op1
, m_Negative(Y
)) && !Y
->isMinSignedValue())
2091 return replaceOperand(I
, 1, ConstantInt::get(I
.getType(), -*Y
));
2094 // -X srem Y --> -(X srem Y)
2096 if (match(&I
, m_SRem(m_OneUse(m_NSWSub(m_Zero(), m_Value(X
))), m_Value(Y
))))
2097 return BinaryOperator::CreateNSWNeg(Builder
.CreateSRem(X
, Y
));
2099 // If the sign bits of both operands are zero (i.e. we can prove they are
2100 // unsigned inputs), turn this into a urem.
2101 APInt
Mask(APInt::getSignMask(I
.getType()->getScalarSizeInBits()));
2102 if (MaskedValueIsZero(Op1
, Mask
, 0, &I
) &&
2103 MaskedValueIsZero(Op0
, Mask
, 0, &I
)) {
2104 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
2105 return BinaryOperator::CreateURem(Op0
, Op1
, I
.getName());
2108 // If it's a constant vector, flip any negative values positive.
2109 if (isa
<ConstantVector
>(Op1
) || isa
<ConstantDataVector
>(Op1
)) {
2110 Constant
*C
= cast
<Constant
>(Op1
);
2111 unsigned VWidth
= cast
<FixedVectorType
>(C
->getType())->getNumElements();
2113 bool hasNegative
= false;
2114 bool hasMissing
= false;
2115 for (unsigned i
= 0; i
!= VWidth
; ++i
) {
2116 Constant
*Elt
= C
->getAggregateElement(i
);
2122 if (ConstantInt
*RHS
= dyn_cast
<ConstantInt
>(Elt
))
2123 if (RHS
->isNegative())
2127 if (hasNegative
&& !hasMissing
) {
2128 SmallVector
<Constant
*, 16> Elts(VWidth
);
2129 for (unsigned i
= 0; i
!= VWidth
; ++i
) {
2130 Elts
[i
] = C
->getAggregateElement(i
); // Handle undef, etc.
2131 if (ConstantInt
*RHS
= dyn_cast
<ConstantInt
>(Elts
[i
])) {
2132 if (RHS
->isNegative())
2133 Elts
[i
] = cast
<ConstantInt
>(ConstantExpr::getNeg(RHS
));
2137 Constant
*NewRHSV
= ConstantVector::get(Elts
);
2138 if (NewRHSV
!= C
) // Don't loop on -MININT
2139 return replaceOperand(I
, 1, NewRHSV
);
2146 Instruction
*InstCombinerImpl::visitFRem(BinaryOperator
&I
) {
2147 if (Value
*V
= simplifyFRemInst(I
.getOperand(0), I
.getOperand(1),
2148 I
.getFastMathFlags(),
2149 SQ
.getWithInstruction(&I
)))
2150 return replaceInstUsesWith(I
, V
);
2152 if (Instruction
*X
= foldVectorBinop(I
))
2155 if (Instruction
*Phi
= foldBinopWithPhiOperands(I
))