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