xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===- InstCombineMulDivRem.cpp -------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
10 // srem, urem, frem.
11 //
12 //===----------------------------------------------------------------------===//
13 
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"
35 #include <cassert>
36 
37 #define DEBUG_TYPE "instcombine"
38 #include "llvm/Transforms/Utils/InstructionWorklist.h"
39 
40 using namespace llvm;
41 using namespace PatternMatch;
42 
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,
47                                         Instruction &CxtI) {
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
50   // code.
51   if (!V->hasOneUse()) return nullptr;
52 
53   bool MadeChange = false;
54 
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);
62   }
63 
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);
73       MadeChange = true;
74     }
75 
76     if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
77       I->setIsExact();
78       MadeChange = true;
79     }
80 
81     if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
82       I->setHasNoUnsignedWrap();
83       MadeChange = true;
84     }
85   }
86 
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".
90 
91   return MadeChange ? V : nullptr;
92 }
93 
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;
102 
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);
110   }
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);
118   }
119 
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));
128   }
129 
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),
133                                            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);
138   }
139 
140   return nullptr;
141 }
142 
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);
148   if (CommuteOperands)
149     std::swap(X, Y);
150 
151   const bool HasNSW = Mul.hasNoSignedWrap();
152   const bool HasNUW = Mul.hasNoUnsignedWrap();
153 
154   // X * (1 << Z) --> X << Z
155   Value *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);
159   }
160 
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);
172   }
173 
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());
183   }
184 
185   return nullptr;
186 }
187 
188 static Value *takeLog2(IRBuilderBase &Builder, Value *Op, unsigned Depth,
189                        bool AssumeNonZero, bool DoFold);
190 
191 Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
192   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
193   if (Value *V =
194           simplifyMulInst(Op0, Op1, I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
195                           SQ.getWithInstruction(&I)))
196     return replaceInstUsesWith(I, V);
197 
198   if (SimplifyAssociativeOrCommutative(I))
199     return &I;
200 
201   if (Instruction *X = foldVectorBinop(I))
202     return X;
203 
204   if (Instruction *Phi = foldBinopWithPhiOperands(I))
205     return Phi;
206 
207   if (Value *V = foldUsingDistributiveLaws(I))
208     return replaceInstUsesWith(I, V);
209 
210   Type *Ty = I.getType();
211   const unsigned BitWidth = Ty->getScalarSizeInBits();
212   const bool HasNSW = I.hasNoSignedWrap();
213   const bool HasNUW = I.hasNoUnsignedWrap();
214 
215   // X * -1 --> 0 - X
216   if (match(Op1, m_AllOnes())) {
217     return HasNSW ? BinaryOperator::CreateNSWNeg(Op0)
218                   : BinaryOperator::CreateNeg(Op0);
219   }
220 
221   // Also allow combining multiply instructions on vectors.
222   {
223     Value *NewOp;
224     Constant *C1, *C2;
225     const APInt *IVal;
226     if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
227                         m_Constant(C1))) &&
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();
237       return BO;
238     }
239 
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);
244 
245         if (HasNUW)
246           Shl->setHasNoUnsignedWrap();
247         if (HasNSW) {
248           const APInt *V;
249           if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1)
250             Shl->setHasNoSignedWrap();
251         }
252 
253         return Shl;
254       }
255     }
256   }
257 
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.
261     if (Value *NegOp0 =
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), "",
266                                /* HasNUW */ false,
267                                HasNSW && Op1C->isNotMinSignedValue()));
268     }
269 
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;
277     Value *X;
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));
286       }
287     }
288   }
289 
290   if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
291     return FoldedMul;
292 
293   if (Value *FoldedMul = foldMulSelectToNegate(I, Builder))
294     return replaceInstUsesWith(I, FoldedMul);
295 
296   // Simplify mul instructions with a constant RHS.
297   Constant *MulC;
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.
301     Value *X;
302     Constant *C1;
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);
307       bool Op0NUW =
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();
316       }
317       return BO;
318     }
319   }
320 
321   // abs(X) * abs(X) -> X * X
322   // nabs(X) * nabs(X) -> X * X
323   if (Op0 == Op1) {
324     Value *X, *Y;
325     SelectPatternFlavor SPF = matchSelectPattern(Op0, X, Y).Flavor;
326     if (SPF == SPF_ABS || SPF == SPF_NABS)
327       return BinaryOperator::CreateMul(X, X);
328 
329     if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X))))
330       return BinaryOperator::CreateMul(X, X);
331   }
332 
333   // -X * C --> X * -C
334   Value *X, *Y;
335   Constant *Op1C;
336   if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C)))
337     return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C));
338 
339   // -X * -Y --> X * Y
340   if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) {
341     auto *NewMul = BinaryOperator::CreateMul(X, Y);
342     if (HasNSW && cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() &&
343         cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap())
344       NewMul->setHasNoSignedWrap();
345     return NewMul;
346   }
347 
348   // -X * Y --> -(X * Y)
349   // X * -Y --> -(X * Y)
350   if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
351     return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
352 
353   // (-X * Y) * -X --> (X * Y) * X
354   // (-X << Y) * -X --> (X << Y) * X
355   if (match(Op1, m_Neg(m_Value(X)))) {
356     if (Value *NegOp0 = Negator::Negate(false, /*IsNSW*/ false, Op0, *this))
357       return BinaryOperator::CreateMul(NegOp0, X);
358   }
359 
360   // (X / Y) *  Y = X - (X % Y)
361   // (X / Y) * -Y = (X % Y) - X
362   {
363     Value *Y = Op1;
364     BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0);
365     if (!Div || (Div->getOpcode() != Instruction::UDiv &&
366                  Div->getOpcode() != Instruction::SDiv)) {
367       Y = Op0;
368       Div = dyn_cast<BinaryOperator>(Op1);
369     }
370     Value *Neg = dyn_castNegVal(Y);
371     if (Div && Div->hasOneUse() &&
372         (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) &&
373         (Div->getOpcode() == Instruction::UDiv ||
374          Div->getOpcode() == Instruction::SDiv)) {
375       Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1);
376 
377       // If the division is exact, X % Y is zero, so we end up with X or -X.
378       if (Div->isExact()) {
379         if (DivOp1 == Y)
380           return replaceInstUsesWith(I, X);
381         return BinaryOperator::CreateNeg(X);
382       }
383 
384       auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem
385                                                           : Instruction::SRem;
386       // X must be frozen because we are increasing its number of uses.
387       Value *XFreeze = Builder.CreateFreeze(X, X->getName() + ".fr");
388       Value *Rem = Builder.CreateBinOp(RemOpc, XFreeze, DivOp1);
389       if (DivOp1 == Y)
390         return BinaryOperator::CreateSub(XFreeze, Rem);
391       return BinaryOperator::CreateSub(Rem, XFreeze);
392     }
393   }
394 
395   // Fold the following two scenarios:
396   //   1) i1 mul -> i1 and.
397   //   2) X * Y --> X & Y, iff X, Y can be only {0,1}.
398   // Note: We could use known bits to generalize this and related patterns with
399   // shifts/truncs
400   if (Ty->isIntOrIntVectorTy(1) ||
401       (match(Op0, m_And(m_Value(), m_One())) &&
402        match(Op1, m_And(m_Value(), m_One()))))
403     return BinaryOperator::CreateAnd(Op0, Op1);
404 
405   if (Value *R = foldMulShl1(I, /* CommuteOperands */ false, Builder))
406     return replaceInstUsesWith(I, R);
407   if (Value *R = foldMulShl1(I, /* CommuteOperands */ true, Builder))
408     return replaceInstUsesWith(I, R);
409 
410   // (zext bool X) * (zext bool Y) --> zext (and X, Y)
411   // (sext bool X) * (sext bool Y) --> zext (and X, Y)
412   // Note: -1 * -1 == 1 * 1 == 1 (if the extends match, the result is the same)
413   if (((match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) ||
414        (match(Op0, m_SExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) &&
415       X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
416       (Op0->hasOneUse() || Op1->hasOneUse() || X == Y)) {
417     Value *And = Builder.CreateAnd(X, Y, "mulbool");
418     return CastInst::Create(Instruction::ZExt, And, Ty);
419   }
420   // (sext bool X) * (zext bool Y) --> sext (and X, Y)
421   // (zext bool X) * (sext bool Y) --> sext (and X, Y)
422   // Note: -1 * 1 == 1 * -1  == -1
423   if (((match(Op0, m_SExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) ||
424        (match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) &&
425       X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
426       (Op0->hasOneUse() || Op1->hasOneUse())) {
427     Value *And = Builder.CreateAnd(X, Y, "mulbool");
428     return CastInst::Create(Instruction::SExt, And, Ty);
429   }
430 
431   // (zext bool X) * Y --> X ? Y : 0
432   // Y * (zext bool X) --> X ? Y : 0
433   if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
434     return SelectInst::Create(X, Op1, ConstantInt::getNullValue(Ty));
435   if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
436     return SelectInst::Create(X, Op0, ConstantInt::getNullValue(Ty));
437 
438   Constant *ImmC;
439   if (match(Op1, m_ImmConstant(ImmC))) {
440     // (sext bool X) * C --> X ? -C : 0
441     if (match(Op0, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
442       Constant *NegC = ConstantExpr::getNeg(ImmC);
443       return SelectInst::Create(X, NegC, ConstantInt::getNullValue(Ty));
444     }
445 
446     // (ashr i32 X, 31) * C --> (X < 0) ? -C : 0
447     const APInt *C;
448     if (match(Op0, m_OneUse(m_AShr(m_Value(X), m_APInt(C)))) &&
449         *C == C->getBitWidth() - 1) {
450       Constant *NegC = ConstantExpr::getNeg(ImmC);
451       Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
452       return SelectInst::Create(IsNeg, NegC, ConstantInt::getNullValue(Ty));
453     }
454   }
455 
456   // (lshr X, 31) * Y --> (X < 0) ? Y : 0
457   // TODO: We are not checking one-use because the elimination of the multiply
458   //       is better for analysis?
459   const APInt *C;
460   if (match(&I, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C)), m_Value(Y))) &&
461       *C == C->getBitWidth() - 1) {
462     Value *IsNeg = Builder.CreateIsNeg(X, "isneg");
463     return SelectInst::Create(IsNeg, Y, ConstantInt::getNullValue(Ty));
464   }
465 
466   // (and X, 1) * Y --> (trunc X) ? Y : 0
467   if (match(&I, m_c_BinOp(m_OneUse(m_And(m_Value(X), m_One())), m_Value(Y)))) {
468     Value *Tr = Builder.CreateTrunc(X, CmpInst::makeCmpResultType(Ty));
469     return SelectInst::Create(Tr, Y, ConstantInt::getNullValue(Ty));
470   }
471 
472   // ((ashr X, 31) | 1) * X --> abs(X)
473   // X * ((ashr X, 31) | 1) --> abs(X)
474   if (match(&I, m_c_BinOp(m_Or(m_AShr(m_Value(X),
475                                       m_SpecificIntAllowUndef(BitWidth - 1)),
476                                m_One()),
477                           m_Deferred(X)))) {
478     Value *Abs = Builder.CreateBinaryIntrinsic(
479         Intrinsic::abs, X, ConstantInt::getBool(I.getContext(), HasNSW));
480     Abs->takeName(&I);
481     return replaceInstUsesWith(I, Abs);
482   }
483 
484   if (Instruction *Ext = narrowMathIfNoOverflow(I))
485     return Ext;
486 
487   if (Instruction *Res = foldBinOpOfSelectAndCastOfSelectCondition(I))
488     return Res;
489 
490   // (mul Op0 Op1):
491   //    if Log2(Op0) folds away ->
492   //        (shl Op1, Log2(Op0))
493   //    if Log2(Op1) folds away ->
494   //        (shl Op0, Log2(Op1))
495   if (takeLog2(Builder, Op0, /*Depth*/ 0, /*AssumeNonZero*/ false,
496                /*DoFold*/ false)) {
497     Value *Res = takeLog2(Builder, Op0, /*Depth*/ 0, /*AssumeNonZero*/ false,
498                           /*DoFold*/ true);
499     BinaryOperator *Shl = BinaryOperator::CreateShl(Op1, Res);
500     // We can only propegate nuw flag.
501     Shl->setHasNoUnsignedWrap(HasNUW);
502     return Shl;
503   }
504   if (takeLog2(Builder, Op1, /*Depth*/ 0, /*AssumeNonZero*/ false,
505                /*DoFold*/ false)) {
506     Value *Res = takeLog2(Builder, Op1, /*Depth*/ 0, /*AssumeNonZero*/ false,
507                           /*DoFold*/ true);
508     BinaryOperator *Shl = BinaryOperator::CreateShl(Op0, Res);
509     // We can only propegate nuw flag.
510     Shl->setHasNoUnsignedWrap(HasNUW);
511     return Shl;
512   }
513 
514   bool Changed = false;
515   if (!HasNSW && willNotOverflowSignedMul(Op0, Op1, I)) {
516     Changed = true;
517     I.setHasNoSignedWrap(true);
518   }
519 
520   if (!HasNUW && willNotOverflowUnsignedMul(Op0, Op1, I)) {
521     Changed = true;
522     I.setHasNoUnsignedWrap(true);
523   }
524 
525   return Changed ? &I : nullptr;
526 }
527 
528 Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) {
529   BinaryOperator::BinaryOps Opcode = I.getOpcode();
530   assert((Opcode == Instruction::FMul || Opcode == Instruction::FDiv) &&
531          "Expected fmul or fdiv");
532 
533   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
534   Value *X, *Y;
535 
536   // -X * -Y --> X * Y
537   // -X / -Y --> X / Y
538   if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
539     return BinaryOperator::CreateWithCopiedFlags(Opcode, X, Y, &I);
540 
541   // fabs(X) * fabs(X) -> X * X
542   // fabs(X) / fabs(X) -> X / X
543   if (Op0 == Op1 && match(Op0, m_FAbs(m_Value(X))))
544     return BinaryOperator::CreateWithCopiedFlags(Opcode, X, X, &I);
545 
546   // fabs(X) * fabs(Y) --> fabs(X * Y)
547   // fabs(X) / fabs(Y) --> fabs(X / Y)
548   if (match(Op0, m_FAbs(m_Value(X))) && match(Op1, m_FAbs(m_Value(Y))) &&
549       (Op0->hasOneUse() || Op1->hasOneUse())) {
550     IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
551     Builder.setFastMathFlags(I.getFastMathFlags());
552     Value *XY = Builder.CreateBinOp(Opcode, X, Y);
553     Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, XY);
554     Fabs->takeName(&I);
555     return replaceInstUsesWith(I, Fabs);
556   }
557 
558   return nullptr;
559 }
560 
561 Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
562   Value *Op0 = I.getOperand(0);
563   Value *Op1 = I.getOperand(1);
564   Value *X, *Y;
565   Constant *C;
566 
567   // Reassociate constant RHS with another constant to form constant
568   // expression.
569   if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) {
570     Constant *C1;
571     if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) {
572       // (C1 / X) * C --> (C * C1) / X
573       Constant *CC1 =
574           ConstantFoldBinaryOpOperands(Instruction::FMul, C, C1, DL);
575       if (CC1 && CC1->isNormalFP())
576         return BinaryOperator::CreateFDivFMF(CC1, X, &I);
577     }
578     if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
579       // (X / C1) * C --> X * (C / C1)
580       Constant *CDivC1 =
581           ConstantFoldBinaryOpOperands(Instruction::FDiv, C, C1, DL);
582       if (CDivC1 && CDivC1->isNormalFP())
583         return BinaryOperator::CreateFMulFMF(X, CDivC1, &I);
584 
585       // If the constant was a denormal, try reassociating differently.
586       // (X / C1) * C --> X / (C1 / C)
587       Constant *C1DivC =
588           ConstantFoldBinaryOpOperands(Instruction::FDiv, C1, C, DL);
589       if (C1DivC && Op0->hasOneUse() && C1DivC->isNormalFP())
590         return BinaryOperator::CreateFDivFMF(X, C1DivC, &I);
591     }
592 
593     // We do not need to match 'fadd C, X' and 'fsub X, C' because they are
594     // canonicalized to 'fadd X, C'. Distributing the multiply may allow
595     // further folds and (X * C) + C2 is 'fma'.
596     if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) {
597       // (X + C1) * C --> (X * C) + (C * C1)
598       if (Constant *CC1 =
599               ConstantFoldBinaryOpOperands(Instruction::FMul, C, C1, DL)) {
600         Value *XC = Builder.CreateFMulFMF(X, C, &I);
601         return BinaryOperator::CreateFAddFMF(XC, CC1, &I);
602       }
603     }
604     if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) {
605       // (C1 - X) * C --> (C * C1) - (X * C)
606       if (Constant *CC1 =
607               ConstantFoldBinaryOpOperands(Instruction::FMul, C, C1, DL)) {
608         Value *XC = Builder.CreateFMulFMF(X, C, &I);
609         return BinaryOperator::CreateFSubFMF(CC1, XC, &I);
610       }
611     }
612   }
613 
614   Value *Z;
615   if (match(&I,
616             m_c_FMul(m_OneUse(m_FDiv(m_Value(X), m_Value(Y))), m_Value(Z)))) {
617     // Sink division: (X / Y) * Z --> (X * Z) / Y
618     Value *NewFMul = Builder.CreateFMulFMF(X, Z, &I);
619     return BinaryOperator::CreateFDivFMF(NewFMul, Y, &I);
620   }
621 
622   // sqrt(X) * sqrt(Y) -> sqrt(X * Y)
623   // nnan disallows the possibility of returning a number if both operands are
624   // negative (in that case, we should return NaN).
625   if (I.hasNoNaNs() && match(Op0, m_OneUse(m_Sqrt(m_Value(X)))) &&
626       match(Op1, m_OneUse(m_Sqrt(m_Value(Y))))) {
627     Value *XY = Builder.CreateFMulFMF(X, Y, &I);
628     Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I);
629     return replaceInstUsesWith(I, Sqrt);
630   }
631 
632   // The following transforms are done irrespective of the number of uses
633   // for the expression "1.0/sqrt(X)".
634   //  1) 1.0/sqrt(X) * X -> X/sqrt(X)
635   //  2) X * 1.0/sqrt(X) -> X/sqrt(X)
636   // We always expect the backend to reduce X/sqrt(X) to sqrt(X), if it
637   // has the necessary (reassoc) fast-math-flags.
638   if (I.hasNoSignedZeros() &&
639       match(Op0, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) &&
640       match(Y, m_Sqrt(m_Value(X))) && Op1 == X)
641     return BinaryOperator::CreateFDivFMF(X, Y, &I);
642   if (I.hasNoSignedZeros() &&
643       match(Op1, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) &&
644       match(Y, m_Sqrt(m_Value(X))) && Op0 == X)
645     return BinaryOperator::CreateFDivFMF(X, Y, &I);
646 
647   // Like the similar transform in instsimplify, this requires 'nsz' because
648   // sqrt(-0.0) = -0.0, and -0.0 * -0.0 does not simplify to -0.0.
649   if (I.hasNoNaNs() && I.hasNoSignedZeros() && Op0 == Op1 && Op0->hasNUses(2)) {
650     // Peek through fdiv to find squaring of square root:
651     // (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y
652     if (match(Op0, m_FDiv(m_Value(X), m_Sqrt(m_Value(Y))))) {
653       Value *XX = Builder.CreateFMulFMF(X, X, &I);
654       return BinaryOperator::CreateFDivFMF(XX, Y, &I);
655     }
656     // (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X)
657     if (match(Op0, m_FDiv(m_Sqrt(m_Value(Y)), m_Value(X)))) {
658       Value *XX = Builder.CreateFMulFMF(X, X, &I);
659       return BinaryOperator::CreateFDivFMF(Y, XX, &I);
660     }
661   }
662 
663   // pow(X, Y) * X --> pow(X, Y+1)
664   // X * pow(X, Y) --> pow(X, Y+1)
665   if (match(&I, m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::pow>(m_Value(X),
666                                                               m_Value(Y))),
667                          m_Deferred(X)))) {
668     Value *Y1 = Builder.CreateFAddFMF(Y, ConstantFP::get(I.getType(), 1.0), &I);
669     Value *Pow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, Y1, &I);
670     return replaceInstUsesWith(I, Pow);
671   }
672 
673   if (I.isOnlyUserOfAnyOperand()) {
674     // pow(X, Y) * pow(X, Z) -> pow(X, Y + Z)
675     if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) &&
676         match(Op1, m_Intrinsic<Intrinsic::pow>(m_Specific(X), m_Value(Z)))) {
677       auto *YZ = Builder.CreateFAddFMF(Y, Z, &I);
678       auto *NewPow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, YZ, &I);
679       return replaceInstUsesWith(I, NewPow);
680     }
681     // pow(X, Y) * pow(Z, Y) -> pow(X * Z, Y)
682     if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) &&
683         match(Op1, m_Intrinsic<Intrinsic::pow>(m_Value(Z), m_Specific(Y)))) {
684       auto *XZ = Builder.CreateFMulFMF(X, Z, &I);
685       auto *NewPow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, XZ, Y, &I);
686       return replaceInstUsesWith(I, NewPow);
687     }
688 
689     // powi(x, y) * powi(x, z) -> powi(x, y + z)
690     if (match(Op0, m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y))) &&
691         match(Op1, m_Intrinsic<Intrinsic::powi>(m_Specific(X), m_Value(Z))) &&
692         Y->getType() == Z->getType()) {
693       auto *YZ = Builder.CreateAdd(Y, Z);
694       auto *NewPow = Builder.CreateIntrinsic(
695           Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
696       return replaceInstUsesWith(I, NewPow);
697     }
698 
699     // exp(X) * exp(Y) -> exp(X + Y)
700     if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) &&
701         match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y)))) {
702       Value *XY = Builder.CreateFAddFMF(X, Y, &I);
703       Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I);
704       return replaceInstUsesWith(I, Exp);
705     }
706 
707     // exp2(X) * exp2(Y) -> exp2(X + Y)
708     if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) &&
709         match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y)))) {
710       Value *XY = Builder.CreateFAddFMF(X, Y, &I);
711       Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I);
712       return replaceInstUsesWith(I, Exp2);
713     }
714   }
715 
716   // (X*Y) * X => (X*X) * Y where Y != X
717   //  The purpose is two-fold:
718   //   1) to form a power expression (of X).
719   //   2) potentially shorten the critical path: After transformation, the
720   //  latency of the instruction Y is amortized by the expression of X*X,
721   //  and therefore Y is in a "less critical" position compared to what it
722   //  was before the transformation.
723   if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) && Op1 != Y) {
724     Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I);
725     return BinaryOperator::CreateFMulFMF(XX, Y, &I);
726   }
727   if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) && Op0 != Y) {
728     Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I);
729     return BinaryOperator::CreateFMulFMF(XX, Y, &I);
730   }
731 
732   return nullptr;
733 }
734 
735 Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
736   if (Value *V = simplifyFMulInst(I.getOperand(0), I.getOperand(1),
737                                   I.getFastMathFlags(),
738                                   SQ.getWithInstruction(&I)))
739     return replaceInstUsesWith(I, V);
740 
741   if (SimplifyAssociativeOrCommutative(I))
742     return &I;
743 
744   if (Instruction *X = foldVectorBinop(I))
745     return X;
746 
747   if (Instruction *Phi = foldBinopWithPhiOperands(I))
748     return Phi;
749 
750   if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
751     return FoldedMul;
752 
753   if (Value *FoldedMul = foldMulSelectToNegate(I, Builder))
754     return replaceInstUsesWith(I, FoldedMul);
755 
756   if (Instruction *R = foldFPSignBitOps(I))
757     return R;
758 
759   // X * -1.0 --> -X
760   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
761   if (match(Op1, m_SpecificFP(-1.0)))
762     return UnaryOperator::CreateFNegFMF(Op0, &I);
763 
764   // With no-nans: X * 0.0 --> copysign(0.0, X)
765   if (I.hasNoNaNs() && match(Op1, m_PosZeroFP())) {
766     CallInst *CopySign = Builder.CreateIntrinsic(Intrinsic::copysign,
767                                                  {I.getType()}, {Op1, Op0}, &I);
768     return replaceInstUsesWith(I, CopySign);
769   }
770 
771   // -X * C --> X * -C
772   Value *X, *Y;
773   Constant *C;
774   if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C)))
775     if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
776       return BinaryOperator::CreateFMulFMF(X, NegC, &I);
777 
778   // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E)
779   if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1))
780     return replaceInstUsesWith(I, V);
781 
782   if (I.hasAllowReassoc())
783     if (Instruction *FoldedMul = foldFMulReassoc(I))
784       return FoldedMul;
785 
786   // log2(X * 0.5) * Y = log2(X) * Y - Y
787   if (I.isFast()) {
788     IntrinsicInst *Log2 = nullptr;
789     if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>(
790             m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
791       Log2 = cast<IntrinsicInst>(Op0);
792       Y = Op1;
793     }
794     if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>(
795             m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
796       Log2 = cast<IntrinsicInst>(Op1);
797       Y = Op0;
798     }
799     if (Log2) {
800       Value *Log2 = Builder.CreateUnaryIntrinsic(Intrinsic::log2, X, &I);
801       Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I);
802       return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I);
803     }
804   }
805 
806   // Simplify FMUL recurrences starting with 0.0 to 0.0 if nnan and nsz are set.
807   // Given a phi node with entry value as 0 and it used in fmul operation,
808   // we can replace fmul with 0 safely and eleminate loop operation.
809   PHINode *PN = nullptr;
810   Value *Start = nullptr, *Step = nullptr;
811   if (matchSimpleRecurrence(&I, PN, Start, Step) && I.hasNoNaNs() &&
812       I.hasNoSignedZeros() && match(Start, m_Zero()))
813     return replaceInstUsesWith(I, Start);
814 
815   // minimum(X, Y) * maximum(X, Y) => X * Y.
816   if (match(&I,
817             m_c_FMul(m_Intrinsic<Intrinsic::maximum>(m_Value(X), m_Value(Y)),
818                      m_c_Intrinsic<Intrinsic::minimum>(m_Deferred(X),
819                                                        m_Deferred(Y))))) {
820     BinaryOperator *Result = BinaryOperator::CreateFMulFMF(X, Y, &I);
821     // We cannot preserve ninf if nnan flag is not set.
822     // If X is NaN and Y is Inf then in original program we had NaN * NaN,
823     // while in optimized version NaN * Inf and this is a poison with ninf flag.
824     if (!Result->hasNoNaNs())
825       Result->setHasNoInfs(false);
826     return Result;
827   }
828 
829   return nullptr;
830 }
831 
832 /// Fold a divide or remainder with a select instruction divisor when one of the
833 /// select operands is zero. In that case, we can use the other select operand
834 /// because div/rem by zero is undefined.
835 bool InstCombinerImpl::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) {
836   SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1));
837   if (!SI)
838     return false;
839 
840   int NonNullOperand;
841   if (match(SI->getTrueValue(), m_Zero()))
842     // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
843     NonNullOperand = 2;
844   else if (match(SI->getFalseValue(), m_Zero()))
845     // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
846     NonNullOperand = 1;
847   else
848     return false;
849 
850   // Change the div/rem to use 'Y' instead of the select.
851   replaceOperand(I, 1, SI->getOperand(NonNullOperand));
852 
853   // Okay, we know we replace the operand of the div/rem with 'Y' with no
854   // problem.  However, the select, or the condition of the select may have
855   // multiple uses.  Based on our knowledge that the operand must be non-zero,
856   // propagate the known value for the select into other uses of it, and
857   // propagate a known value of the condition into its other users.
858 
859   // If the select and condition only have a single use, don't bother with this,
860   // early exit.
861   Value *SelectCond = SI->getCondition();
862   if (SI->use_empty() && SelectCond->hasOneUse())
863     return true;
864 
865   // Scan the current block backward, looking for other uses of SI.
866   BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin();
867   Type *CondTy = SelectCond->getType();
868   while (BBI != BBFront) {
869     --BBI;
870     // If we found an instruction that we can't assume will return, so
871     // information from below it cannot be propagated above it.
872     if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI))
873       break;
874 
875     // Replace uses of the select or its condition with the known values.
876     for (Use &Op : BBI->operands()) {
877       if (Op == SI) {
878         replaceUse(Op, SI->getOperand(NonNullOperand));
879         Worklist.push(&*BBI);
880       } else if (Op == SelectCond) {
881         replaceUse(Op, NonNullOperand == 1 ? ConstantInt::getTrue(CondTy)
882                                            : ConstantInt::getFalse(CondTy));
883         Worklist.push(&*BBI);
884       }
885     }
886 
887     // If we past the instruction, quit looking for it.
888     if (&*BBI == SI)
889       SI = nullptr;
890     if (&*BBI == SelectCond)
891       SelectCond = nullptr;
892 
893     // If we ran out of things to eliminate, break out of the loop.
894     if (!SelectCond && !SI)
895       break;
896 
897   }
898   return true;
899 }
900 
901 /// True if the multiply can not be expressed in an int this size.
902 static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product,
903                               bool IsSigned) {
904   bool Overflow;
905   Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow);
906   return Overflow;
907 }
908 
909 /// True if C1 is a multiple of C2. Quotient contains C1/C2.
910 static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
911                        bool IsSigned) {
912   assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal");
913 
914   // Bail if we will divide by zero.
915   if (C2.isZero())
916     return false;
917 
918   // Bail if we would divide INT_MIN by -1.
919   if (IsSigned && C1.isMinSignedValue() && C2.isAllOnes())
920     return false;
921 
922   APInt Remainder(C1.getBitWidth(), /*val=*/0ULL, IsSigned);
923   if (IsSigned)
924     APInt::sdivrem(C1, C2, Quotient, Remainder);
925   else
926     APInt::udivrem(C1, C2, Quotient, Remainder);
927 
928   return Remainder.isMinValue();
929 }
930 
931 static Value *foldIDivShl(BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
932   assert((I.getOpcode() == Instruction::SDiv ||
933           I.getOpcode() == Instruction::UDiv) &&
934          "Expected integer divide");
935 
936   bool IsSigned = I.getOpcode() == Instruction::SDiv;
937   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
938   Type *Ty = I.getType();
939 
940   Value *X, *Y, *Z;
941 
942   // With appropriate no-wrap constraints, remove a common factor in the
943   // dividend and divisor that is disguised as a left-shifted value.
944   if (match(Op1, m_Shl(m_Value(X), m_Value(Z))) &&
945       match(Op0, m_c_Mul(m_Specific(X), m_Value(Y)))) {
946     // Both operands must have the matching no-wrap for this kind of division.
947     auto *Mul = cast<OverflowingBinaryOperator>(Op0);
948     auto *Shl = cast<OverflowingBinaryOperator>(Op1);
949     bool HasNUW = Mul->hasNoUnsignedWrap() && Shl->hasNoUnsignedWrap();
950     bool HasNSW = Mul->hasNoSignedWrap() && Shl->hasNoSignedWrap();
951 
952     // (X * Y) u/ (X << Z) --> Y u>> Z
953     if (!IsSigned && HasNUW)
954       return Builder.CreateLShr(Y, Z, "", I.isExact());
955 
956     // (X * Y) s/ (X << Z) --> Y s/ (1 << Z)
957     if (IsSigned && HasNSW && (Op0->hasOneUse() || Op1->hasOneUse())) {
958       Value *Shl = Builder.CreateShl(ConstantInt::get(Ty, 1), Z);
959       return Builder.CreateSDiv(Y, Shl, "", I.isExact());
960     }
961   }
962 
963   // With appropriate no-wrap constraints, remove a common factor in the
964   // dividend and divisor that is disguised as a left-shift amount.
965   if (match(Op0, m_Shl(m_Value(X), m_Value(Z))) &&
966       match(Op1, m_Shl(m_Value(Y), m_Specific(Z)))) {
967     auto *Shl0 = cast<OverflowingBinaryOperator>(Op0);
968     auto *Shl1 = cast<OverflowingBinaryOperator>(Op1);
969 
970     // For unsigned div, we need 'nuw' on both shifts or
971     // 'nsw' on both shifts + 'nuw' on the dividend.
972     // (X << Z) / (Y << Z) --> X / Y
973     if (!IsSigned &&
974         ((Shl0->hasNoUnsignedWrap() && Shl1->hasNoUnsignedWrap()) ||
975          (Shl0->hasNoUnsignedWrap() && Shl0->hasNoSignedWrap() &&
976           Shl1->hasNoSignedWrap())))
977       return Builder.CreateUDiv(X, Y, "", I.isExact());
978 
979     // For signed div, we need 'nsw' on both shifts + 'nuw' on the divisor.
980     // (X << Z) / (Y << Z) --> X / Y
981     if (IsSigned && Shl0->hasNoSignedWrap() && Shl1->hasNoSignedWrap() &&
982         Shl1->hasNoUnsignedWrap())
983       return Builder.CreateSDiv(X, Y, "", I.isExact());
984   }
985 
986   // If X << Y and X << Z does not overflow, then:
987   // (X << Y) / (X << Z) -> (1 << Y) / (1 << Z) -> 1 << Y >> Z
988   if (match(Op0, m_Shl(m_Value(X), m_Value(Y))) &&
989       match(Op1, m_Shl(m_Specific(X), m_Value(Z)))) {
990     auto *Shl0 = cast<OverflowingBinaryOperator>(Op0);
991     auto *Shl1 = cast<OverflowingBinaryOperator>(Op1);
992 
993     if (IsSigned ? (Shl0->hasNoSignedWrap() && Shl1->hasNoSignedWrap())
994                  : (Shl0->hasNoUnsignedWrap() && Shl1->hasNoUnsignedWrap())) {
995       Constant *One = ConstantInt::get(X->getType(), 1);
996       // Only preserve the nsw flag if dividend has nsw
997       // or divisor has nsw and operator is sdiv.
998       Value *Dividend = Builder.CreateShl(
999           One, Y, "shl.dividend",
1000           /*HasNUW*/ true,
1001           /*HasNSW*/
1002           IsSigned ? (Shl0->hasNoUnsignedWrap() || Shl1->hasNoUnsignedWrap())
1003                    : Shl0->hasNoSignedWrap());
1004       return Builder.CreateLShr(Dividend, Z, "", I.isExact());
1005     }
1006   }
1007 
1008   return nullptr;
1009 }
1010 
1011 /// This function implements the transforms common to both integer division
1012 /// instructions (udiv and sdiv). It is called by the visitors to those integer
1013 /// division instructions.
1014 /// Common integer divide transforms
1015 Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
1016   if (Instruction *Phi = foldBinopWithPhiOperands(I))
1017     return Phi;
1018 
1019   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1020   bool IsSigned = I.getOpcode() == Instruction::SDiv;
1021   Type *Ty = I.getType();
1022 
1023   // The RHS is known non-zero.
1024   if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I))
1025     return replaceOperand(I, 1, V);
1026 
1027   // Handle cases involving: [su]div X, (select Cond, Y, Z)
1028   // This does not apply for fdiv.
1029   if (simplifyDivRemOfSelectWithZeroOp(I))
1030     return &I;
1031 
1032   // If the divisor is a select-of-constants, try to constant fold all div ops:
1033   // C / (select Cond, TrueC, FalseC) --> select Cond, (C / TrueC), (C / FalseC)
1034   // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
1035   if (match(Op0, m_ImmConstant()) &&
1036       match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
1037     if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1),
1038                                           /*FoldWithMultiUse*/ true))
1039       return R;
1040   }
1041 
1042   const APInt *C2;
1043   if (match(Op1, m_APInt(C2))) {
1044     Value *X;
1045     const APInt *C1;
1046 
1047     // (X / C1) / C2  -> X / (C1*C2)
1048     if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) ||
1049         (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) {
1050       APInt Product(C1->getBitWidth(), /*val=*/0ULL, IsSigned);
1051       if (!multiplyOverflows(*C1, *C2, Product, IsSigned))
1052         return BinaryOperator::Create(I.getOpcode(), X,
1053                                       ConstantInt::get(Ty, Product));
1054     }
1055 
1056     APInt Quotient(C2->getBitWidth(), /*val=*/0ULL, IsSigned);
1057     if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
1058         (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) {
1059 
1060       // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
1061       if (isMultiple(*C2, *C1, Quotient, IsSigned)) {
1062         auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X,
1063                                               ConstantInt::get(Ty, Quotient));
1064         NewDiv->setIsExact(I.isExact());
1065         return NewDiv;
1066       }
1067 
1068       // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
1069       if (isMultiple(*C1, *C2, Quotient, IsSigned)) {
1070         auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
1071                                            ConstantInt::get(Ty, Quotient));
1072         auto *OBO = cast<OverflowingBinaryOperator>(Op0);
1073         Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
1074         Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
1075         return Mul;
1076       }
1077     }
1078 
1079     if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) &&
1080          C1->ult(C1->getBitWidth() - 1)) ||
1081         (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))) &&
1082          C1->ult(C1->getBitWidth()))) {
1083       APInt C1Shifted = APInt::getOneBitSet(
1084           C1->getBitWidth(), static_cast<unsigned>(C1->getZExtValue()));
1085 
1086       // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1.
1087       if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
1088         auto *BO = BinaryOperator::Create(I.getOpcode(), X,
1089                                           ConstantInt::get(Ty, Quotient));
1090         BO->setIsExact(I.isExact());
1091         return BO;
1092       }
1093 
1094       // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2.
1095       if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
1096         auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
1097                                            ConstantInt::get(Ty, Quotient));
1098         auto *OBO = cast<OverflowingBinaryOperator>(Op0);
1099         Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
1100         Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
1101         return Mul;
1102       }
1103     }
1104 
1105     // Distribute div over add to eliminate a matching div/mul pair:
1106     // ((X * C2) + C1) / C2 --> X + C1/C2
1107     // We need a multiple of the divisor for a signed add constant, but
1108     // unsigned is fine with any constant pair.
1109     if (IsSigned &&
1110         match(Op0, m_NSWAdd(m_NSWMul(m_Value(X), m_SpecificInt(*C2)),
1111                             m_APInt(C1))) &&
1112         isMultiple(*C1, *C2, Quotient, IsSigned)) {
1113       return BinaryOperator::CreateNSWAdd(X, ConstantInt::get(Ty, Quotient));
1114     }
1115     if (!IsSigned &&
1116         match(Op0, m_NUWAdd(m_NUWMul(m_Value(X), m_SpecificInt(*C2)),
1117                             m_APInt(C1)))) {
1118       return BinaryOperator::CreateNUWAdd(X,
1119                                           ConstantInt::get(Ty, C1->udiv(*C2)));
1120     }
1121 
1122     if (!C2->isZero()) // avoid X udiv 0
1123       if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I))
1124         return FoldedDiv;
1125   }
1126 
1127   if (match(Op0, m_One())) {
1128     assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?");
1129     if (IsSigned) {
1130       // 1 / 0 --> undef ; 1 / 1 --> 1 ; 1 / -1 --> -1 ; 1 / anything else --> 0
1131       // (Op1 + 1) u< 3 ? Op1 : 0
1132       // Op1 must be frozen because we are increasing its number of uses.
1133       Value *F1 = Builder.CreateFreeze(Op1, Op1->getName() + ".fr");
1134       Value *Inc = Builder.CreateAdd(F1, Op0);
1135       Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3));
1136       return SelectInst::Create(Cmp, F1, ConstantInt::get(Ty, 0));
1137     } else {
1138       // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
1139       // result is one, otherwise it's zero.
1140       return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty);
1141     }
1142   }
1143 
1144   // See if we can fold away this div instruction.
1145   if (SimplifyDemandedInstructionBits(I))
1146     return &I;
1147 
1148   // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
1149   Value *X, *Z;
1150   if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1
1151     if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
1152         (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
1153       return BinaryOperator::Create(I.getOpcode(), X, Op1);
1154 
1155   // (X << Y) / X -> 1 << Y
1156   Value *Y;
1157   if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y))))
1158     return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y);
1159   if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y))))
1160     return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y);
1161 
1162   // X / (X * Y) -> 1 / Y if the multiplication does not overflow.
1163   if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) {
1164     bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
1165     bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
1166     if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) {
1167       replaceOperand(I, 0, ConstantInt::get(Ty, 1));
1168       replaceOperand(I, 1, Y);
1169       return &I;
1170     }
1171   }
1172 
1173   // (X << Z) / (X * Y) -> (1 << Z) / Y
1174   // TODO: Handle sdiv.
1175   if (!IsSigned && Op1->hasOneUse() &&
1176       match(Op0, m_NUWShl(m_Value(X), m_Value(Z))) &&
1177       match(Op1, m_c_Mul(m_Specific(X), m_Value(Y))))
1178     if (cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap()) {
1179       Instruction *NewDiv = BinaryOperator::CreateUDiv(
1180           Builder.CreateShl(ConstantInt::get(Ty, 1), Z, "", /*NUW*/ true), Y);
1181       NewDiv->setIsExact(I.isExact());
1182       return NewDiv;
1183     }
1184 
1185   if (Value *R = foldIDivShl(I, Builder))
1186     return replaceInstUsesWith(I, R);
1187 
1188   // With the appropriate no-wrap constraint, remove a multiply by the divisor
1189   // after peeking through another divide:
1190   // ((Op1 * X) / Y) / Op1 --> X / Y
1191   if (match(Op0, m_BinOp(I.getOpcode(), m_c_Mul(m_Specific(Op1), m_Value(X)),
1192                          m_Value(Y)))) {
1193     auto *InnerDiv = cast<PossiblyExactOperator>(Op0);
1194     auto *Mul = cast<OverflowingBinaryOperator>(InnerDiv->getOperand(0));
1195     Instruction *NewDiv = nullptr;
1196     if (!IsSigned && Mul->hasNoUnsignedWrap())
1197       NewDiv = BinaryOperator::CreateUDiv(X, Y);
1198     else if (IsSigned && Mul->hasNoSignedWrap())
1199       NewDiv = BinaryOperator::CreateSDiv(X, Y);
1200 
1201     // Exact propagates only if both of the original divides are exact.
1202     if (NewDiv) {
1203       NewDiv->setIsExact(I.isExact() && InnerDiv->isExact());
1204       return NewDiv;
1205     }
1206   }
1207 
1208   // (X * Y) / (X * Z) --> Y / Z (and commuted variants)
1209   if (match(Op0, m_Mul(m_Value(X), m_Value(Y)))) {
1210     auto OB0HasNSW = cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap();
1211     auto OB0HasNUW = cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap();
1212 
1213     auto CreateDivOrNull = [&](Value *A, Value *B) -> Instruction * {
1214       auto OB1HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
1215       auto OB1HasNUW =
1216           cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
1217       const APInt *C1, *C2;
1218       if (IsSigned && OB0HasNSW) {
1219         if (OB1HasNSW && match(B, m_APInt(C1)) && !C1->isAllOnes())
1220           return BinaryOperator::CreateSDiv(A, B);
1221       }
1222       if (!IsSigned && OB0HasNUW) {
1223         if (OB1HasNUW)
1224           return BinaryOperator::CreateUDiv(A, B);
1225         if (match(A, m_APInt(C1)) && match(B, m_APInt(C2)) && C2->ule(*C1))
1226           return BinaryOperator::CreateUDiv(A, B);
1227       }
1228       return nullptr;
1229     };
1230 
1231     if (match(Op1, m_c_Mul(m_Specific(X), m_Value(Z)))) {
1232       if (auto *Val = CreateDivOrNull(Y, Z))
1233         return Val;
1234     }
1235     if (match(Op1, m_c_Mul(m_Specific(Y), m_Value(Z)))) {
1236       if (auto *Val = CreateDivOrNull(X, Z))
1237         return Val;
1238     }
1239   }
1240   return nullptr;
1241 }
1242 
1243 static const unsigned MaxDepth = 6;
1244 
1245 // Take the exact integer log2 of the value. If DoFold is true, create the
1246 // actual instructions, otherwise return a non-null dummy value. Return nullptr
1247 // on failure.
1248 static Value *takeLog2(IRBuilderBase &Builder, Value *Op, unsigned Depth,
1249                        bool AssumeNonZero, bool DoFold) {
1250   auto IfFold = [DoFold](function_ref<Value *()> Fn) {
1251     if (!DoFold)
1252       return reinterpret_cast<Value *>(-1);
1253     return Fn();
1254   };
1255 
1256   // FIXME: assert that Op1 isn't/doesn't contain undef.
1257 
1258   // log2(2^C) -> C
1259   if (match(Op, m_Power2()))
1260     return IfFold([&]() {
1261       Constant *C = ConstantExpr::getExactLogBase2(cast<Constant>(Op));
1262       if (!C)
1263         llvm_unreachable("Failed to constant fold udiv -> logbase2");
1264       return C;
1265     });
1266 
1267   // The remaining tests are all recursive, so bail out if we hit the limit.
1268   if (Depth++ == MaxDepth)
1269     return nullptr;
1270 
1271   // log2(zext X) -> zext log2(X)
1272   // FIXME: Require one use?
1273   Value *X, *Y;
1274   if (match(Op, m_ZExt(m_Value(X))))
1275     if (Value *LogX = takeLog2(Builder, X, Depth, AssumeNonZero, DoFold))
1276       return IfFold([&]() { return Builder.CreateZExt(LogX, Op->getType()); });
1277 
1278   // log2(X << Y) -> log2(X) + Y
1279   // FIXME: Require one use unless X is 1?
1280   if (match(Op, m_Shl(m_Value(X), m_Value(Y)))) {
1281     auto *BO = cast<OverflowingBinaryOperator>(Op);
1282     // nuw will be set if the `shl` is trivially non-zero.
1283     if (AssumeNonZero || BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap())
1284       if (Value *LogX = takeLog2(Builder, X, Depth, AssumeNonZero, DoFold))
1285         return IfFold([&]() { return Builder.CreateAdd(LogX, Y); });
1286   }
1287 
1288   // log2(Cond ? X : Y) -> Cond ? log2(X) : log2(Y)
1289   // FIXME: missed optimization: if one of the hands of select is/contains
1290   //        undef, just directly pick the other one.
1291   // FIXME: can both hands contain undef?
1292   // FIXME: Require one use?
1293   if (SelectInst *SI = dyn_cast<SelectInst>(Op))
1294     if (Value *LogX = takeLog2(Builder, SI->getOperand(1), Depth,
1295                                AssumeNonZero, DoFold))
1296       if (Value *LogY = takeLog2(Builder, SI->getOperand(2), Depth,
1297                                  AssumeNonZero, DoFold))
1298         return IfFold([&]() {
1299           return Builder.CreateSelect(SI->getOperand(0), LogX, LogY);
1300         });
1301 
1302   // log2(umin(X, Y)) -> umin(log2(X), log2(Y))
1303   // log2(umax(X, Y)) -> umax(log2(X), log2(Y))
1304   auto *MinMax = dyn_cast<MinMaxIntrinsic>(Op);
1305   if (MinMax && MinMax->hasOneUse() && !MinMax->isSigned()) {
1306     // Use AssumeNonZero as false here. Otherwise we can hit case where
1307     // log2(umax(X, Y)) != umax(log2(X), log2(Y)) (because overflow).
1308     if (Value *LogX = takeLog2(Builder, MinMax->getLHS(), Depth,
1309                                /*AssumeNonZero*/ false, DoFold))
1310       if (Value *LogY = takeLog2(Builder, MinMax->getRHS(), Depth,
1311                                  /*AssumeNonZero*/ false, DoFold))
1312         return IfFold([&]() {
1313           return Builder.CreateBinaryIntrinsic(MinMax->getIntrinsicID(), LogX,
1314                                                LogY);
1315         });
1316   }
1317 
1318   return nullptr;
1319 }
1320 
1321 /// If we have zero-extended operands of an unsigned div or rem, we may be able
1322 /// to narrow the operation (sink the zext below the math).
1323 static Instruction *narrowUDivURem(BinaryOperator &I,
1324                                    InstCombinerImpl &IC) {
1325   Instruction::BinaryOps Opcode = I.getOpcode();
1326   Value *N = I.getOperand(0);
1327   Value *D = I.getOperand(1);
1328   Type *Ty = I.getType();
1329   Value *X, *Y;
1330   if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) &&
1331       X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) {
1332     // udiv (zext X), (zext Y) --> zext (udiv X, Y)
1333     // urem (zext X), (zext Y) --> zext (urem X, Y)
1334     Value *NarrowOp = IC.Builder.CreateBinOp(Opcode, X, Y);
1335     return new ZExtInst(NarrowOp, Ty);
1336   }
1337 
1338   Constant *C;
1339   if (isa<Instruction>(N) && match(N, m_OneUse(m_ZExt(m_Value(X)))) &&
1340       match(D, m_Constant(C))) {
1341     // If the constant is the same in the smaller type, use the narrow version.
1342     Constant *TruncC = IC.getLosslessUnsignedTrunc(C, X->getType());
1343     if (!TruncC)
1344       return nullptr;
1345 
1346     // udiv (zext X), C --> zext (udiv X, C')
1347     // urem (zext X), C --> zext (urem X, C')
1348     return new ZExtInst(IC.Builder.CreateBinOp(Opcode, X, TruncC), Ty);
1349   }
1350   if (isa<Instruction>(D) && match(D, m_OneUse(m_ZExt(m_Value(X)))) &&
1351       match(N, m_Constant(C))) {
1352     // If the constant is the same in the smaller type, use the narrow version.
1353     Constant *TruncC = IC.getLosslessUnsignedTrunc(C, X->getType());
1354     if (!TruncC)
1355       return nullptr;
1356 
1357     // udiv C, (zext X) --> zext (udiv C', X)
1358     // urem C, (zext X) --> zext (urem C', X)
1359     return new ZExtInst(IC.Builder.CreateBinOp(Opcode, TruncC, X), Ty);
1360   }
1361 
1362   return nullptr;
1363 }
1364 
1365 Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) {
1366   if (Value *V = simplifyUDivInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1367                                   SQ.getWithInstruction(&I)))
1368     return replaceInstUsesWith(I, V);
1369 
1370   if (Instruction *X = foldVectorBinop(I))
1371     return X;
1372 
1373   // Handle the integer div common cases
1374   if (Instruction *Common = commonIDivTransforms(I))
1375     return Common;
1376 
1377   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1378   Value *X;
1379   const APInt *C1, *C2;
1380   if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) {
1381     // (X lshr C1) udiv C2 --> X udiv (C2 << C1)
1382     bool Overflow;
1383     APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow);
1384     if (!Overflow) {
1385       bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value()));
1386       BinaryOperator *BO = BinaryOperator::CreateUDiv(
1387           X, ConstantInt::get(X->getType(), C2ShlC1));
1388       if (IsExact)
1389         BO->setIsExact();
1390       return BO;
1391     }
1392   }
1393 
1394   // Op0 / C where C is large (negative) --> zext (Op0 >= C)
1395   // TODO: Could use isKnownNegative() to handle non-constant values.
1396   Type *Ty = I.getType();
1397   if (match(Op1, m_Negative())) {
1398     Value *Cmp = Builder.CreateICmpUGE(Op0, Op1);
1399     return CastInst::CreateZExtOrBitCast(Cmp, Ty);
1400   }
1401   // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined)
1402   if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1403     Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
1404     return CastInst::CreateZExtOrBitCast(Cmp, Ty);
1405   }
1406 
1407   if (Instruction *NarrowDiv = narrowUDivURem(I, *this))
1408     return NarrowDiv;
1409 
1410   Value *A, *B;
1411 
1412   // Look through a right-shift to find the common factor:
1413   // ((Op1 *nuw A) >> B) / Op1 --> A >> B
1414   if (match(Op0, m_LShr(m_NUWMul(m_Specific(Op1), m_Value(A)), m_Value(B))) ||
1415       match(Op0, m_LShr(m_NUWMul(m_Value(A), m_Specific(Op1)), m_Value(B)))) {
1416     Instruction *Lshr = BinaryOperator::CreateLShr(A, B);
1417     if (I.isExact() && cast<PossiblyExactOperator>(Op0)->isExact())
1418       Lshr->setIsExact();
1419     return Lshr;
1420   }
1421 
1422   // Op1 udiv Op2 -> Op1 lshr log2(Op2), if log2() folds away.
1423   if (takeLog2(Builder, Op1, /*Depth*/ 0, /*AssumeNonZero*/ true,
1424                /*DoFold*/ false)) {
1425     Value *Res = takeLog2(Builder, Op1, /*Depth*/ 0,
1426                           /*AssumeNonZero*/ true, /*DoFold*/ true);
1427     return replaceInstUsesWith(
1428         I, Builder.CreateLShr(Op0, Res, I.getName(), I.isExact()));
1429   }
1430 
1431   return nullptr;
1432 }
1433 
1434 Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
1435   if (Value *V = simplifySDivInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1436                                   SQ.getWithInstruction(&I)))
1437     return replaceInstUsesWith(I, V);
1438 
1439   if (Instruction *X = foldVectorBinop(I))
1440     return X;
1441 
1442   // Handle the integer div common cases
1443   if (Instruction *Common = commonIDivTransforms(I))
1444     return Common;
1445 
1446   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1447   Type *Ty = I.getType();
1448   Value *X;
1449   // sdiv Op0, -1 --> -Op0
1450   // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
1451   if (match(Op1, m_AllOnes()) ||
1452       (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
1453     return BinaryOperator::CreateNSWNeg(Op0);
1454 
1455   // X / INT_MIN --> X == INT_MIN
1456   if (match(Op1, m_SignMask()))
1457     return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), Ty);
1458 
1459   if (I.isExact()) {
1460     // sdiv exact X, 1<<C --> ashr exact X, C   iff  1<<C  is non-negative
1461     if (match(Op1, m_Power2()) && match(Op1, m_NonNegative())) {
1462       Constant *C = ConstantExpr::getExactLogBase2(cast<Constant>(Op1));
1463       return BinaryOperator::CreateExactAShr(Op0, C);
1464     }
1465 
1466     // sdiv exact X, (1<<ShAmt) --> ashr exact X, ShAmt (if shl is non-negative)
1467     Value *ShAmt;
1468     if (match(Op1, m_NSWShl(m_One(), m_Value(ShAmt))))
1469       return BinaryOperator::CreateExactAShr(Op0, ShAmt);
1470 
1471     // sdiv exact X, -1<<C --> -(ashr exact X, C)
1472     if (match(Op1, m_NegatedPower2())) {
1473       Constant *NegPow2C = ConstantExpr::getNeg(cast<Constant>(Op1));
1474       Constant *C = ConstantExpr::getExactLogBase2(NegPow2C);
1475       Value *Ashr = Builder.CreateAShr(Op0, C, I.getName() + ".neg", true);
1476       return BinaryOperator::CreateNSWNeg(Ashr);
1477     }
1478   }
1479 
1480   const APInt *Op1C;
1481   if (match(Op1, m_APInt(Op1C))) {
1482     // If the dividend is sign-extended and the constant divisor is small enough
1483     // to fit in the source type, shrink the division to the narrower type:
1484     // (sext X) sdiv C --> sext (X sdiv C)
1485     Value *Op0Src;
1486     if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) &&
1487         Op0Src->getType()->getScalarSizeInBits() >=
1488             Op1C->getSignificantBits()) {
1489 
1490       // In the general case, we need to make sure that the dividend is not the
1491       // minimum signed value because dividing that by -1 is UB. But here, we
1492       // know that the -1 divisor case is already handled above.
1493 
1494       Constant *NarrowDivisor =
1495           ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType());
1496       Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor);
1497       return new SExtInst(NarrowOp, Ty);
1498     }
1499 
1500     // -X / C --> X / -C (if the negation doesn't overflow).
1501     // TODO: This could be enhanced to handle arbitrary vector constants by
1502     //       checking if all elements are not the min-signed-val.
1503     if (!Op1C->isMinSignedValue() &&
1504         match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
1505       Constant *NegC = ConstantInt::get(Ty, -(*Op1C));
1506       Instruction *BO = BinaryOperator::CreateSDiv(X, NegC);
1507       BO->setIsExact(I.isExact());
1508       return BO;
1509     }
1510   }
1511 
1512   // -X / Y --> -(X / Y)
1513   Value *Y;
1514   if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
1515     return BinaryOperator::CreateNSWNeg(
1516         Builder.CreateSDiv(X, Y, I.getName(), I.isExact()));
1517 
1518   // abs(X) / X --> X > -1 ? 1 : -1
1519   // X / abs(X) --> X > -1 ? 1 : -1
1520   if (match(&I, m_c_BinOp(
1521                     m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(X), m_One())),
1522                     m_Deferred(X)))) {
1523     Value *Cond = Builder.CreateIsNotNeg(X);
1524     return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
1525                               ConstantInt::getAllOnesValue(Ty));
1526   }
1527 
1528   KnownBits KnownDividend = computeKnownBits(Op0, 0, &I);
1529   if (!I.isExact() &&
1530       (match(Op1, m_Power2(Op1C)) || match(Op1, m_NegatedPower2(Op1C))) &&
1531       KnownDividend.countMinTrailingZeros() >= Op1C->countr_zero()) {
1532     I.setIsExact();
1533     return &I;
1534   }
1535 
1536   if (KnownDividend.isNonNegative()) {
1537     // If both operands are unsigned, turn this into a udiv.
1538     if (isKnownNonNegative(Op1, SQ.getWithInstruction(&I))) {
1539       auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1540       BO->setIsExact(I.isExact());
1541       return BO;
1542     }
1543 
1544     if (match(Op1, m_NegatedPower2())) {
1545       // X sdiv (-(1 << C)) -> -(X sdiv (1 << C)) ->
1546       //                    -> -(X udiv (1 << C)) -> -(X u>> C)
1547       Constant *CNegLog2 = ConstantExpr::getExactLogBase2(
1548           ConstantExpr::getNeg(cast<Constant>(Op1)));
1549       Value *Shr = Builder.CreateLShr(Op0, CNegLog2, I.getName(), I.isExact());
1550       return BinaryOperator::CreateNeg(Shr);
1551     }
1552 
1553     if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
1554       // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
1555       // Safe because the only negative value (1 << Y) can take on is
1556       // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
1557       // the sign bit set.
1558       auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
1559       BO->setIsExact(I.isExact());
1560       return BO;
1561     }
1562   }
1563 
1564   // -X / X --> X == INT_MIN ? 1 : -1
1565   if (isKnownNegation(Op0, Op1)) {
1566     APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
1567     Value *Cond = Builder.CreateICmpEQ(Op0, ConstantInt::get(Ty, MinVal));
1568     return SelectInst::Create(Cond, ConstantInt::get(Ty, 1),
1569                               ConstantInt::getAllOnesValue(Ty));
1570   }
1571   return nullptr;
1572 }
1573 
1574 /// Remove negation and try to convert division into multiplication.
1575 Instruction *InstCombinerImpl::foldFDivConstantDivisor(BinaryOperator &I) {
1576   Constant *C;
1577   if (!match(I.getOperand(1), m_Constant(C)))
1578     return nullptr;
1579 
1580   // -X / C --> X / -C
1581   Value *X;
1582   const DataLayout &DL = I.getModule()->getDataLayout();
1583   if (match(I.getOperand(0), m_FNeg(m_Value(X))))
1584     if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
1585       return BinaryOperator::CreateFDivFMF(X, NegC, &I);
1586 
1587   // nnan X / +0.0 -> copysign(inf, X)
1588   if (I.hasNoNaNs() && match(I.getOperand(1), m_Zero())) {
1589     IRBuilder<> B(&I);
1590     // TODO: nnan nsz X / -0.0 -> copysign(inf, X)
1591     CallInst *CopySign = B.CreateIntrinsic(
1592         Intrinsic::copysign, {C->getType()},
1593         {ConstantFP::getInfinity(I.getType()), I.getOperand(0)}, &I);
1594     CopySign->takeName(&I);
1595     return replaceInstUsesWith(I, CopySign);
1596   }
1597 
1598   // If the constant divisor has an exact inverse, this is always safe. If not,
1599   // then we can still create a reciprocal if fast-math-flags allow it and the
1600   // constant is a regular number (not zero, infinite, or denormal).
1601   if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP())))
1602     return nullptr;
1603 
1604   // Disallow denormal constants because we don't know what would happen
1605   // on all targets.
1606   // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1607   // denorms are flushed?
1608   auto *RecipC = ConstantFoldBinaryOpOperands(
1609       Instruction::FDiv, ConstantFP::get(I.getType(), 1.0), C, DL);
1610   if (!RecipC || !RecipC->isNormalFP())
1611     return nullptr;
1612 
1613   // X / C --> X * (1 / C)
1614   return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I);
1615 }
1616 
1617 /// Remove negation and try to reassociate constant math.
1618 static Instruction *foldFDivConstantDividend(BinaryOperator &I) {
1619   Constant *C;
1620   if (!match(I.getOperand(0), m_Constant(C)))
1621     return nullptr;
1622 
1623   // C / -X --> -C / X
1624   Value *X;
1625   const DataLayout &DL = I.getModule()->getDataLayout();
1626   if (match(I.getOperand(1), m_FNeg(m_Value(X))))
1627     if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
1628       return BinaryOperator::CreateFDivFMF(NegC, X, &I);
1629 
1630   if (!I.hasAllowReassoc() || !I.hasAllowReciprocal())
1631     return nullptr;
1632 
1633   // Try to reassociate C / X expressions where X includes another constant.
1634   Constant *C2, *NewC = nullptr;
1635   if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) {
1636     // C / (X * C2) --> (C / C2) / X
1637     NewC = ConstantFoldBinaryOpOperands(Instruction::FDiv, C, C2, DL);
1638   } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) {
1639     // C / (X / C2) --> (C * C2) / X
1640     NewC = ConstantFoldBinaryOpOperands(Instruction::FMul, C, C2, DL);
1641   }
1642   // Disallow denormal constants because we don't know what would happen
1643   // on all targets.
1644   // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
1645   // denorms are flushed?
1646   if (!NewC || !NewC->isNormalFP())
1647     return nullptr;
1648 
1649   return BinaryOperator::CreateFDivFMF(NewC, X, &I);
1650 }
1651 
1652 /// Negate the exponent of pow/exp to fold division-by-pow() into multiply.
1653 static Instruction *foldFDivPowDivisor(BinaryOperator &I,
1654                                        InstCombiner::BuilderTy &Builder) {
1655   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1656   auto *II = dyn_cast<IntrinsicInst>(Op1);
1657   if (!II || !II->hasOneUse() || !I.hasAllowReassoc() ||
1658       !I.hasAllowReciprocal())
1659     return nullptr;
1660 
1661   // Z / pow(X, Y) --> Z * pow(X, -Y)
1662   // Z / exp{2}(Y) --> Z * exp{2}(-Y)
1663   // In the general case, this creates an extra instruction, but fmul allows
1664   // for better canonicalization and optimization than fdiv.
1665   Intrinsic::ID IID = II->getIntrinsicID();
1666   SmallVector<Value *> Args;
1667   switch (IID) {
1668   case Intrinsic::pow:
1669     Args.push_back(II->getArgOperand(0));
1670     Args.push_back(Builder.CreateFNegFMF(II->getArgOperand(1), &I));
1671     break;
1672   case Intrinsic::powi: {
1673     // Require 'ninf' assuming that makes powi(X, -INT_MIN) acceptable.
1674     // That is, X ** (huge negative number) is 0.0, ~1.0, or INF and so
1675     // dividing by that is INF, ~1.0, or 0.0. Code that uses powi allows
1676     // non-standard results, so this corner case should be acceptable if the
1677     // code rules out INF values.
1678     if (!I.hasNoInfs())
1679       return nullptr;
1680     Args.push_back(II->getArgOperand(0));
1681     Args.push_back(Builder.CreateNeg(II->getArgOperand(1)));
1682     Type *Tys[] = {I.getType(), II->getArgOperand(1)->getType()};
1683     Value *Pow = Builder.CreateIntrinsic(IID, Tys, Args, &I);
1684     return BinaryOperator::CreateFMulFMF(Op0, Pow, &I);
1685   }
1686   case Intrinsic::exp:
1687   case Intrinsic::exp2:
1688     Args.push_back(Builder.CreateFNegFMF(II->getArgOperand(0), &I));
1689     break;
1690   default:
1691     return nullptr;
1692   }
1693   Value *Pow = Builder.CreateIntrinsic(IID, I.getType(), Args, &I);
1694   return BinaryOperator::CreateFMulFMF(Op0, Pow, &I);
1695 }
1696 
1697 Instruction *InstCombinerImpl::visitFDiv(BinaryOperator &I) {
1698   Module *M = I.getModule();
1699 
1700   if (Value *V = simplifyFDivInst(I.getOperand(0), I.getOperand(1),
1701                                   I.getFastMathFlags(),
1702                                   SQ.getWithInstruction(&I)))
1703     return replaceInstUsesWith(I, V);
1704 
1705   if (Instruction *X = foldVectorBinop(I))
1706     return X;
1707 
1708   if (Instruction *Phi = foldBinopWithPhiOperands(I))
1709     return Phi;
1710 
1711   if (Instruction *R = foldFDivConstantDivisor(I))
1712     return R;
1713 
1714   if (Instruction *R = foldFDivConstantDividend(I))
1715     return R;
1716 
1717   if (Instruction *R = foldFPSignBitOps(I))
1718     return R;
1719 
1720   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1721   if (isa<Constant>(Op0))
1722     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
1723       if (Instruction *R = FoldOpIntoSelect(I, SI))
1724         return R;
1725 
1726   if (isa<Constant>(Op1))
1727     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
1728       if (Instruction *R = FoldOpIntoSelect(I, SI))
1729         return R;
1730 
1731   if (I.hasAllowReassoc() && I.hasAllowReciprocal()) {
1732     Value *X, *Y;
1733     if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
1734         (!isa<Constant>(Y) || !isa<Constant>(Op1))) {
1735       // (X / Y) / Z => X / (Y * Z)
1736       Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I);
1737       return BinaryOperator::CreateFDivFMF(X, YZ, &I);
1738     }
1739     if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
1740         (!isa<Constant>(Y) || !isa<Constant>(Op0))) {
1741       // Z / (X / Y) => (Y * Z) / X
1742       Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I);
1743       return BinaryOperator::CreateFDivFMF(YZ, X, &I);
1744     }
1745     // Z / (1.0 / Y) => (Y * Z)
1746     //
1747     // This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0. The
1748     // m_OneUse check is avoided because even in the case of the multiple uses
1749     // for 1.0/Y, the number of instructions remain the same and a division is
1750     // replaced by a multiplication.
1751     if (match(Op1, m_FDiv(m_SpecificFP(1.0), m_Value(Y))))
1752       return BinaryOperator::CreateFMulFMF(Y, Op0, &I);
1753   }
1754 
1755   if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
1756     // sin(X) / cos(X) -> tan(X)
1757     // cos(X) / sin(X) -> 1/tan(X) (cotangent)
1758     Value *X;
1759     bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) &&
1760                  match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X)));
1761     bool IsCot =
1762         !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) &&
1763                   match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X)));
1764 
1765     if ((IsTan || IsCot) && hasFloatFn(M, &TLI, I.getType(), LibFunc_tan,
1766                                        LibFunc_tanf, LibFunc_tanl)) {
1767       IRBuilder<> B(&I);
1768       IRBuilder<>::FastMathFlagGuard FMFGuard(B);
1769       B.setFastMathFlags(I.getFastMathFlags());
1770       AttributeList Attrs =
1771           cast<CallBase>(Op0)->getCalledFunction()->getAttributes();
1772       Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf,
1773                                         LibFunc_tanl, B, Attrs);
1774       if (IsCot)
1775         Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res);
1776       return replaceInstUsesWith(I, Res);
1777     }
1778   }
1779 
1780   // X / (X * Y) --> 1.0 / Y
1781   // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
1782   // We can ignore the possibility that X is infinity because INF/INF is NaN.
1783   Value *X, *Y;
1784   if (I.hasNoNaNs() && I.hasAllowReassoc() &&
1785       match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) {
1786     replaceOperand(I, 0, ConstantFP::get(I.getType(), 1.0));
1787     replaceOperand(I, 1, Y);
1788     return &I;
1789   }
1790 
1791   // X / fabs(X) -> copysign(1.0, X)
1792   // fabs(X) / X -> copysign(1.0, X)
1793   if (I.hasNoNaNs() && I.hasNoInfs() &&
1794       (match(&I, m_FDiv(m_Value(X), m_FAbs(m_Deferred(X)))) ||
1795        match(&I, m_FDiv(m_FAbs(m_Value(X)), m_Deferred(X))))) {
1796     Value *V = Builder.CreateBinaryIntrinsic(
1797         Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), X, &I);
1798     return replaceInstUsesWith(I, V);
1799   }
1800 
1801   if (Instruction *Mul = foldFDivPowDivisor(I, Builder))
1802     return Mul;
1803 
1804   // pow(X, Y) / X --> pow(X, Y-1)
1805   if (I.hasAllowReassoc() &&
1806       match(Op0, m_OneUse(m_Intrinsic<Intrinsic::pow>(m_Specific(Op1),
1807                                                       m_Value(Y))))) {
1808     Value *Y1 =
1809         Builder.CreateFAddFMF(Y, ConstantFP::get(I.getType(), -1.0), &I);
1810     Value *Pow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, Op1, Y1, &I);
1811     return replaceInstUsesWith(I, Pow);
1812   }
1813 
1814   // powi(X, Y) / X --> powi(X, Y-1)
1815   // This is legal when (Y - 1) can't wraparound, in which case reassoc and nnan
1816   // are required.
1817   // TODO: Multi-use may be also better off creating Powi(x,y-1)
1818   if (I.hasAllowReassoc() && I.hasNoNaNs() &&
1819       match(Op0, m_OneUse(m_Intrinsic<Intrinsic::powi>(m_Specific(Op1),
1820                                                        m_Value(Y)))) &&
1821       willNotOverflowSignedSub(Y, ConstantInt::get(Y->getType(), 1), I)) {
1822     Constant *NegOne = ConstantInt::getAllOnesValue(Y->getType());
1823     Value *Y1 = Builder.CreateAdd(Y, NegOne);
1824     Type *Types[] = {Op1->getType(), Y1->getType()};
1825     Value *Pow = Builder.CreateIntrinsic(Intrinsic::powi, Types, {Op1, Y1}, &I);
1826     return replaceInstUsesWith(I, Pow);
1827   }
1828 
1829   return nullptr;
1830 }
1831 
1832 // Variety of transform for:
1833 //  (urem/srem (mul X, Y), (mul X, Z))
1834 //  (urem/srem (shl X, Y), (shl X, Z))
1835 //  (urem/srem (shl Y, X), (shl Z, X))
1836 // NB: The shift cases are really just extensions of the mul case. We treat
1837 // shift as Val * (1 << Amt).
1838 static Instruction *simplifyIRemMulShl(BinaryOperator &I,
1839                                        InstCombinerImpl &IC) {
1840   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *X = nullptr;
1841   APInt Y, Z;
1842   bool ShiftByX = false;
1843 
1844   // If V is not nullptr, it will be matched using m_Specific.
1845   auto MatchShiftOrMulXC = [](Value *Op, Value *&V, APInt &C) -> bool {
1846     const APInt *Tmp = nullptr;
1847     if ((!V && match(Op, m_Mul(m_Value(V), m_APInt(Tmp)))) ||
1848         (V && match(Op, m_Mul(m_Specific(V), m_APInt(Tmp)))))
1849       C = *Tmp;
1850     else if ((!V && match(Op, m_Shl(m_Value(V), m_APInt(Tmp)))) ||
1851              (V && match(Op, m_Shl(m_Specific(V), m_APInt(Tmp)))))
1852       C = APInt(Tmp->getBitWidth(), 1) << *Tmp;
1853     if (Tmp != nullptr)
1854       return true;
1855 
1856     // Reset `V` so we don't start with specific value on next match attempt.
1857     V = nullptr;
1858     return false;
1859   };
1860 
1861   auto MatchShiftCX = [](Value *Op, APInt &C, Value *&V) -> bool {
1862     const APInt *Tmp = nullptr;
1863     if ((!V && match(Op, m_Shl(m_APInt(Tmp), m_Value(V)))) ||
1864         (V && match(Op, m_Shl(m_APInt(Tmp), m_Specific(V))))) {
1865       C = *Tmp;
1866       return true;
1867     }
1868 
1869     // Reset `V` so we don't start with specific value on next match attempt.
1870     V = nullptr;
1871     return false;
1872   };
1873 
1874   if (MatchShiftOrMulXC(Op0, X, Y) && MatchShiftOrMulXC(Op1, X, Z)) {
1875     // pass
1876   } else if (MatchShiftCX(Op0, Y, X) && MatchShiftCX(Op1, Z, X)) {
1877     ShiftByX = true;
1878   } else {
1879     return nullptr;
1880   }
1881 
1882   bool IsSRem = I.getOpcode() == Instruction::SRem;
1883 
1884   OverflowingBinaryOperator *BO0 = cast<OverflowingBinaryOperator>(Op0);
1885   // TODO: We may be able to deduce more about nsw/nuw of BO0/BO1 based on Y >=
1886   // Z or Z >= Y.
1887   bool BO0HasNSW = BO0->hasNoSignedWrap();
1888   bool BO0HasNUW = BO0->hasNoUnsignedWrap();
1889   bool BO0NoWrap = IsSRem ? BO0HasNSW : BO0HasNUW;
1890 
1891   APInt RemYZ = IsSRem ? Y.srem(Z) : Y.urem(Z);
1892   // (rem (mul nuw/nsw X, Y), (mul X, Z))
1893   //      if (rem Y, Z) == 0
1894   //          -> 0
1895   if (RemYZ.isZero() && BO0NoWrap)
1896     return IC.replaceInstUsesWith(I, ConstantInt::getNullValue(I.getType()));
1897 
1898   // Helper function to emit either (RemSimplificationC << X) or
1899   // (RemSimplificationC * X) depending on whether we matched Op0/Op1 as
1900   // (shl V, X) or (mul V, X) respectively.
1901   auto CreateMulOrShift =
1902       [&](const APInt &RemSimplificationC) -> BinaryOperator * {
1903     Value *RemSimplification =
1904         ConstantInt::get(I.getType(), RemSimplificationC);
1905     return ShiftByX ? BinaryOperator::CreateShl(RemSimplification, X)
1906                     : BinaryOperator::CreateMul(X, RemSimplification);
1907   };
1908 
1909   OverflowingBinaryOperator *BO1 = cast<OverflowingBinaryOperator>(Op1);
1910   bool BO1HasNSW = BO1->hasNoSignedWrap();
1911   bool BO1HasNUW = BO1->hasNoUnsignedWrap();
1912   bool BO1NoWrap = IsSRem ? BO1HasNSW : BO1HasNUW;
1913   // (rem (mul X, Y), (mul nuw/nsw X, Z))
1914   //      if (rem Y, Z) == Y
1915   //          -> (mul nuw/nsw X, Y)
1916   if (RemYZ == Y && BO1NoWrap) {
1917     BinaryOperator *BO = CreateMulOrShift(Y);
1918     // Copy any overflow flags from Op0.
1919     BO->setHasNoSignedWrap(IsSRem || BO0HasNSW);
1920     BO->setHasNoUnsignedWrap(!IsSRem || BO0HasNUW);
1921     return BO;
1922   }
1923 
1924   // (rem (mul nuw/nsw X, Y), (mul {nsw} X, Z))
1925   //      if Y >= Z
1926   //          -> (mul {nuw} nsw X, (rem Y, Z))
1927   if (Y.uge(Z) && (IsSRem ? (BO0HasNSW && BO1HasNSW) : BO0HasNUW)) {
1928     BinaryOperator *BO = CreateMulOrShift(RemYZ);
1929     BO->setHasNoSignedWrap();
1930     BO->setHasNoUnsignedWrap(BO0HasNUW);
1931     return BO;
1932   }
1933 
1934   return nullptr;
1935 }
1936 
1937 /// This function implements the transforms common to both integer remainder
1938 /// instructions (urem and srem). It is called by the visitors to those integer
1939 /// remainder instructions.
1940 /// Common integer remainder transforms
1941 Instruction *InstCombinerImpl::commonIRemTransforms(BinaryOperator &I) {
1942   if (Instruction *Phi = foldBinopWithPhiOperands(I))
1943     return Phi;
1944 
1945   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1946 
1947   // The RHS is known non-zero.
1948   if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I))
1949     return replaceOperand(I, 1, V);
1950 
1951   // Handle cases involving: rem X, (select Cond, Y, Z)
1952   if (simplifyDivRemOfSelectWithZeroOp(I))
1953     return &I;
1954 
1955   // If the divisor is a select-of-constants, try to constant fold all rem ops:
1956   // C % (select Cond, TrueC, FalseC) --> select Cond, (C % TrueC), (C % FalseC)
1957   // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
1958   if (match(Op0, m_ImmConstant()) &&
1959       match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
1960     if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1),
1961                                           /*FoldWithMultiUse*/ true))
1962       return R;
1963   }
1964 
1965   if (isa<Constant>(Op1)) {
1966     if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
1967       if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
1968         if (Instruction *R = FoldOpIntoSelect(I, SI))
1969           return R;
1970       } else if (auto *PN = dyn_cast<PHINode>(Op0I)) {
1971         const APInt *Op1Int;
1972         if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() &&
1973             (I.getOpcode() == Instruction::URem ||
1974              !Op1Int->isMinSignedValue())) {
1975           // foldOpIntoPhi will speculate instructions to the end of the PHI's
1976           // predecessor blocks, so do this only if we know the srem or urem
1977           // will not fault.
1978           if (Instruction *NV = foldOpIntoPhi(I, PN))
1979             return NV;
1980         }
1981       }
1982 
1983       // See if we can fold away this rem instruction.
1984       if (SimplifyDemandedInstructionBits(I))
1985         return &I;
1986     }
1987   }
1988 
1989   if (Instruction *R = simplifyIRemMulShl(I, *this))
1990     return R;
1991 
1992   return nullptr;
1993 }
1994 
1995 Instruction *InstCombinerImpl::visitURem(BinaryOperator &I) {
1996   if (Value *V = simplifyURemInst(I.getOperand(0), I.getOperand(1),
1997                                   SQ.getWithInstruction(&I)))
1998     return replaceInstUsesWith(I, V);
1999 
2000   if (Instruction *X = foldVectorBinop(I))
2001     return X;
2002 
2003   if (Instruction *common = commonIRemTransforms(I))
2004     return common;
2005 
2006   if (Instruction *NarrowRem = narrowUDivURem(I, *this))
2007     return NarrowRem;
2008 
2009   // X urem Y -> X and Y-1, where Y is a power of 2,
2010   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2011   Type *Ty = I.getType();
2012   if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
2013     // This may increase instruction count, we don't enforce that Y is a
2014     // constant.
2015     Constant *N1 = Constant::getAllOnesValue(Ty);
2016     Value *Add = Builder.CreateAdd(Op1, N1);
2017     return BinaryOperator::CreateAnd(Op0, Add);
2018   }
2019 
2020   // 1 urem X -> zext(X != 1)
2021   if (match(Op0, m_One())) {
2022     Value *Cmp = Builder.CreateICmpNE(Op1, ConstantInt::get(Ty, 1));
2023     return CastInst::CreateZExtOrBitCast(Cmp, Ty);
2024   }
2025 
2026   // Op0 urem C -> Op0 < C ? Op0 : Op0 - C, where C >= signbit.
2027   // Op0 must be frozen because we are increasing its number of uses.
2028   if (match(Op1, m_Negative())) {
2029     Value *F0 = Builder.CreateFreeze(Op0, Op0->getName() + ".fr");
2030     Value *Cmp = Builder.CreateICmpULT(F0, Op1);
2031     Value *Sub = Builder.CreateSub(F0, Op1);
2032     return SelectInst::Create(Cmp, F0, Sub);
2033   }
2034 
2035   // If the divisor is a sext of a boolean, then the divisor must be max
2036   // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also
2037   // max unsigned value. In that case, the remainder is 0:
2038   // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0
2039   Value *X;
2040   if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
2041     Value *FrozenOp0 = Builder.CreateFreeze(Op0, Op0->getName() + ".frozen");
2042     Value *Cmp =
2043         Builder.CreateICmpEQ(FrozenOp0, ConstantInt::getAllOnesValue(Ty));
2044     return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), FrozenOp0);
2045   }
2046 
2047   // For "(X + 1) % Op1" and if (X u< Op1) => (X + 1) == Op1 ? 0 : X + 1 .
2048   if (match(Op0, m_Add(m_Value(X), m_One()))) {
2049     Value *Val =
2050         simplifyICmpInst(ICmpInst::ICMP_ULT, X, Op1, SQ.getWithInstruction(&I));
2051     if (Val && match(Val, m_One())) {
2052       Value *FrozenOp0 = Builder.CreateFreeze(Op0, Op0->getName() + ".frozen");
2053       Value *Cmp = Builder.CreateICmpEQ(FrozenOp0, Op1);
2054       return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), FrozenOp0);
2055     }
2056   }
2057 
2058   return nullptr;
2059 }
2060 
2061 Instruction *InstCombinerImpl::visitSRem(BinaryOperator &I) {
2062   if (Value *V = simplifySRemInst(I.getOperand(0), I.getOperand(1),
2063                                   SQ.getWithInstruction(&I)))
2064     return replaceInstUsesWith(I, V);
2065 
2066   if (Instruction *X = foldVectorBinop(I))
2067     return X;
2068 
2069   // Handle the integer rem common cases
2070   if (Instruction *Common = commonIRemTransforms(I))
2071     return Common;
2072 
2073   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2074   {
2075     const APInt *Y;
2076     // X % -Y -> X % Y
2077     if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue())
2078       return replaceOperand(I, 1, ConstantInt::get(I.getType(), -*Y));
2079   }
2080 
2081   // -X srem Y --> -(X srem Y)
2082   Value *X, *Y;
2083   if (match(&I, m_SRem(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
2084     return BinaryOperator::CreateNSWNeg(Builder.CreateSRem(X, Y));
2085 
2086   // If the sign bits of both operands are zero (i.e. we can prove they are
2087   // unsigned inputs), turn this into a urem.
2088   APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
2089   if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
2090       MaskedValueIsZero(Op0, Mask, 0, &I)) {
2091     // X srem Y -> X urem Y, iff X and Y don't have sign bit set
2092     return BinaryOperator::CreateURem(Op0, Op1, I.getName());
2093   }
2094 
2095   // If it's a constant vector, flip any negative values positive.
2096   if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
2097     Constant *C = cast<Constant>(Op1);
2098     unsigned VWidth = cast<FixedVectorType>(C->getType())->getNumElements();
2099 
2100     bool hasNegative = false;
2101     bool hasMissing = false;
2102     for (unsigned i = 0; i != VWidth; ++i) {
2103       Constant *Elt = C->getAggregateElement(i);
2104       if (!Elt) {
2105         hasMissing = true;
2106         break;
2107       }
2108 
2109       if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
2110         if (RHS->isNegative())
2111           hasNegative = true;
2112     }
2113 
2114     if (hasNegative && !hasMissing) {
2115       SmallVector<Constant *, 16> Elts(VWidth);
2116       for (unsigned i = 0; i != VWidth; ++i) {
2117         Elts[i] = C->getAggregateElement(i);  // Handle undef, etc.
2118         if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
2119           if (RHS->isNegative())
2120             Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
2121         }
2122       }
2123 
2124       Constant *NewRHSV = ConstantVector::get(Elts);
2125       if (NewRHSV != C)  // Don't loop on -MININT
2126         return replaceOperand(I, 1, NewRHSV);
2127     }
2128   }
2129 
2130   return nullptr;
2131 }
2132 
2133 Instruction *InstCombinerImpl::visitFRem(BinaryOperator &I) {
2134   if (Value *V = simplifyFRemInst(I.getOperand(0), I.getOperand(1),
2135                                   I.getFastMathFlags(),
2136                                   SQ.getWithInstruction(&I)))
2137     return replaceInstUsesWith(I, V);
2138 
2139   if (Instruction *X = foldVectorBinop(I))
2140     return X;
2141 
2142   if (Instruction *Phi = foldBinopWithPhiOperands(I))
2143     return Phi;
2144 
2145   return nullptr;
2146 }
2147