LLVM API Documentation

InstCombineMulDivRem.cpp
Go to the documentation of this file.
00001 //===- InstCombineMulDivRem.cpp -------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
00011 // srem, urem, frem.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "InstCombine.h"
00016 #include "llvm/Analysis/InstructionSimplify.h"
00017 #include "llvm/IR/IntrinsicInst.h"
00018 #include "llvm/IR/PatternMatch.h"
00019 using namespace llvm;
00020 using namespace PatternMatch;
00021 
00022 #define DEBUG_TYPE "instcombine"
00023 
00024 
00025 /// simplifyValueKnownNonZero - The specific integer value is used in a context
00026 /// where it is known to be non-zero.  If this allows us to simplify the
00027 /// computation, do so and return the new operand, otherwise return null.
00028 static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
00029                                         Instruction *CxtI) {
00030   // If V has multiple uses, then we would have to do more analysis to determine
00031   // if this is safe.  For example, the use could be in dynamically unreached
00032   // code.
00033   if (!V->hasOneUse()) return nullptr;
00034 
00035   bool MadeChange = false;
00036 
00037   // ((1 << A) >>u B) --> (1 << (A-B))
00038   // Because V cannot be zero, we know that B is less than A.
00039   Value *A = nullptr, *B = nullptr, *PowerOf2 = nullptr;
00040   if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(PowerOf2), m_Value(A))),
00041                       m_Value(B))) &&
00042       // The "1" can be any value known to be a power of 2.
00043       isKnownToBeAPowerOfTwo(PowerOf2, false, 0, IC.getAssumptionTracker(),
00044                              CxtI, IC.getDominatorTree())) {
00045     A = IC.Builder->CreateSub(A, B);
00046     return IC.Builder->CreateShl(PowerOf2, A);
00047   }
00048 
00049   // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
00050   // inexact.  Similarly for <<.
00051   if (BinaryOperator *I = dyn_cast<BinaryOperator>(V))
00052     if (I->isLogicalShift() && isKnownToBeAPowerOfTwo(I->getOperand(0), false,
00053                                                       0, IC.getAssumptionTracker(),
00054                                                       CxtI,
00055                                                       IC.getDominatorTree())) {
00056       // We know that this is an exact/nuw shift and that the input is a
00057       // non-zero context as well.
00058       if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
00059         I->setOperand(0, V2);
00060         MadeChange = true;
00061       }
00062 
00063       if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
00064         I->setIsExact();
00065         MadeChange = true;
00066       }
00067 
00068       if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
00069         I->setHasNoUnsignedWrap();
00070         MadeChange = true;
00071       }
00072     }
00073 
00074   // TODO: Lots more we could do here:
00075   //    If V is a phi node, we can call this on each of its operands.
00076   //    "select cond, X, 0" can simplify to "X".
00077 
00078   return MadeChange ? V : nullptr;
00079 }
00080 
00081 
00082 /// MultiplyOverflows - True if the multiply can not be expressed in an int
00083 /// this size.
00084 static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign) {
00085   uint32_t W = C1->getBitWidth();
00086   APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
00087   if (sign) {
00088     LHSExt = LHSExt.sext(W * 2);
00089     RHSExt = RHSExt.sext(W * 2);
00090   } else {
00091     LHSExt = LHSExt.zext(W * 2);
00092     RHSExt = RHSExt.zext(W * 2);
00093   }
00094 
00095   APInt MulExt = LHSExt * RHSExt;
00096 
00097   if (!sign)
00098     return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
00099 
00100   APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
00101   APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
00102   return MulExt.slt(Min) || MulExt.sgt(Max);
00103 }
00104 
00105 /// \brief True if C2 is a multiple of C1. Quotient contains C2/C1.
00106 static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
00107                        bool IsSigned) {
00108   assert(C1.getBitWidth() == C2.getBitWidth() &&
00109          "Inconsistent width of constants!");
00110 
00111   APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned);
00112   if (IsSigned)
00113     APInt::sdivrem(C1, C2, Quotient, Remainder);
00114   else
00115     APInt::udivrem(C1, C2, Quotient, Remainder);
00116 
00117   return Remainder.isMinValue();
00118 }
00119 
00120 /// \brief A helper routine of InstCombiner::visitMul().
00121 ///
00122 /// If C is a vector of known powers of 2, then this function returns
00123 /// a new vector obtained from C replacing each element with its logBase2.
00124 /// Return a null pointer otherwise.
00125 static Constant *getLogBase2Vector(ConstantDataVector *CV) {
00126   const APInt *IVal;
00127   SmallVector<Constant *, 4> Elts;
00128 
00129   for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
00130     Constant *Elt = CV->getElementAsConstant(I);
00131     if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
00132       return nullptr;
00133     Elts.push_back(ConstantInt::get(Elt->getType(), IVal->logBase2()));
00134   }
00135 
00136   return ConstantVector::get(Elts);
00137 }
00138 
00139 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
00140   bool Changed = SimplifyAssociativeOrCommutative(I);
00141   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00142 
00143   if (Value *V = SimplifyVectorOp(I))
00144     return ReplaceInstUsesWith(I, V);
00145 
00146   if (Value *V = SimplifyMulInst(Op0, Op1, DL, TLI, DT, AT))
00147     return ReplaceInstUsesWith(I, V);
00148 
00149   if (Value *V = SimplifyUsingDistributiveLaws(I))
00150     return ReplaceInstUsesWith(I, V);
00151 
00152   if (match(Op1, m_AllOnes()))  // X * -1 == 0 - X
00153     return BinaryOperator::CreateNeg(Op0, I.getName());
00154 
00155   // Also allow combining multiply instructions on vectors.
00156   {
00157     Value *NewOp;
00158     Constant *C1, *C2;
00159     const APInt *IVal;
00160     if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
00161                         m_Constant(C1))) &&
00162         match(C1, m_APInt(IVal)))
00163       // ((X << C1)*C2) == (X * (C2 << C1))
00164       return BinaryOperator::CreateMul(NewOp, ConstantExpr::getShl(C1, C2));
00165 
00166     if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
00167       Constant *NewCst = nullptr;
00168       if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2())
00169         // Replace X*(2^C) with X << C, where C is either a scalar or a splat.
00170         NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2());
00171       else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1))
00172         // Replace X*(2^C) with X << C, where C is a vector of known
00173         // constant powers of 2.
00174         NewCst = getLogBase2Vector(CV);
00175 
00176       if (NewCst) {
00177         BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
00178         if (I.hasNoSignedWrap()) Shl->setHasNoSignedWrap();
00179         if (I.hasNoUnsignedWrap()) Shl->setHasNoUnsignedWrap();
00180         return Shl;
00181       }
00182     }
00183   }
00184 
00185   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
00186     // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
00187     // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
00188     // The "* (2**n)" thus becomes a potential shifting opportunity.
00189     {
00190       const APInt &   Val = CI->getValue();
00191       const APInt &PosVal = Val.abs();
00192       if (Val.isNegative() && PosVal.isPowerOf2()) {
00193         Value *X = nullptr, *Y = nullptr;
00194         if (Op0->hasOneUse()) {
00195           ConstantInt *C1;
00196           Value *Sub = nullptr;
00197           if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
00198             Sub = Builder->CreateSub(X, Y, "suba");
00199           else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
00200             Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc");
00201           if (Sub)
00202             return
00203               BinaryOperator::CreateMul(Sub,
00204                                         ConstantInt::get(Y->getType(), PosVal));
00205         }
00206       }
00207     }
00208   }
00209 
00210   // Simplify mul instructions with a constant RHS.
00211   if (isa<Constant>(Op1)) {
00212     // Try to fold constant mul into select arguments.
00213     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
00214       if (Instruction *R = FoldOpIntoSelect(I, SI))
00215         return R;
00216 
00217     if (isa<PHINode>(Op0))
00218       if (Instruction *NV = FoldOpIntoPhi(I))
00219         return NV;
00220 
00221     // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
00222     {
00223       Value *X;
00224       Constant *C1;
00225       if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
00226         Value *Mul = Builder->CreateMul(C1, Op1);
00227         // Only go forward with the transform if C1*CI simplifies to a tidier
00228         // constant.
00229         if (!match(Mul, m_Mul(m_Value(), m_Value())))
00230           return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul);
00231       }
00232     }
00233   }
00234 
00235   if (Value *Op0v = dyn_castNegVal(Op0))     // -X * -Y = X*Y
00236     if (Value *Op1v = dyn_castNegVal(Op1))
00237       return BinaryOperator::CreateMul(Op0v, Op1v);
00238 
00239   // (X / Y) *  Y = X - (X % Y)
00240   // (X / Y) * -Y = (X % Y) - X
00241   {
00242     Value *Op1C = Op1;
00243     BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
00244     if (!BO ||
00245         (BO->getOpcode() != Instruction::UDiv &&
00246          BO->getOpcode() != Instruction::SDiv)) {
00247       Op1C = Op0;
00248       BO = dyn_cast<BinaryOperator>(Op1);
00249     }
00250     Value *Neg = dyn_castNegVal(Op1C);
00251     if (BO && BO->hasOneUse() &&
00252         (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) &&
00253         (BO->getOpcode() == Instruction::UDiv ||
00254          BO->getOpcode() == Instruction::SDiv)) {
00255       Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
00256 
00257       // If the division is exact, X % Y is zero, so we end up with X or -X.
00258       if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO))
00259         if (SDiv->isExact()) {
00260           if (Op1BO == Op1C)
00261             return ReplaceInstUsesWith(I, Op0BO);
00262           return BinaryOperator::CreateNeg(Op0BO);
00263         }
00264 
00265       Value *Rem;
00266       if (BO->getOpcode() == Instruction::UDiv)
00267         Rem = Builder->CreateURem(Op0BO, Op1BO);
00268       else
00269         Rem = Builder->CreateSRem(Op0BO, Op1BO);
00270       Rem->takeName(BO);
00271 
00272       if (Op1BO == Op1C)
00273         return BinaryOperator::CreateSub(Op0BO, Rem);
00274       return BinaryOperator::CreateSub(Rem, Op0BO);
00275     }
00276   }
00277 
00278   /// i1 mul -> i1 and.
00279   if (I.getType()->getScalarType()->isIntegerTy(1))
00280     return BinaryOperator::CreateAnd(Op0, Op1);
00281 
00282   // X*(1 << Y) --> X << Y
00283   // (1 << Y)*X --> X << Y
00284   {
00285     Value *Y;
00286     if (match(Op0, m_Shl(m_One(), m_Value(Y))))
00287       return BinaryOperator::CreateShl(Op1, Y);
00288     if (match(Op1, m_Shl(m_One(), m_Value(Y))))
00289       return BinaryOperator::CreateShl(Op0, Y);
00290   }
00291 
00292   // If one of the operands of the multiply is a cast from a boolean value, then
00293   // we know the bool is either zero or one, so this is a 'masking' multiply.
00294   //   X * Y (where Y is 0 or 1) -> X & (0-Y)
00295   if (!I.getType()->isVectorTy()) {
00296     // -2 is "-1 << 1" so it is all bits set except the low one.
00297     APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true);
00298 
00299     Value *BoolCast = nullptr, *OtherOp = nullptr;
00300     if (MaskedValueIsZero(Op0, Negative2, 0, &I))
00301       BoolCast = Op0, OtherOp = Op1;
00302     else if (MaskedValueIsZero(Op1, Negative2, 0, &I))
00303       BoolCast = Op1, OtherOp = Op0;
00304 
00305     if (BoolCast) {
00306       Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()),
00307                                     BoolCast);
00308       return BinaryOperator::CreateAnd(V, OtherOp);
00309     }
00310   }
00311 
00312   return Changed ? &I : nullptr;
00313 }
00314 
00315 //
00316 // Detect pattern:
00317 //
00318 // log2(Y*0.5)
00319 //
00320 // And check for corresponding fast math flags
00321 //
00322 
00323 static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) {
00324 
00325    if (!Op->hasOneUse())
00326      return;
00327 
00328    IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op);
00329    if (!II)
00330      return;
00331    if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra())
00332      return;
00333    Log2 = II;
00334 
00335    Value *OpLog2Of = II->getArgOperand(0);
00336    if (!OpLog2Of->hasOneUse())
00337      return;
00338 
00339    Instruction *I = dyn_cast<Instruction>(OpLog2Of);
00340    if (!I)
00341      return;
00342    if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra())
00343      return;
00344 
00345    if (match(I->getOperand(0), m_SpecificFP(0.5)))
00346      Y = I->getOperand(1);
00347    else if (match(I->getOperand(1), m_SpecificFP(0.5)))
00348      Y = I->getOperand(0);
00349 }
00350 
00351 static bool isFiniteNonZeroFp(Constant *C) {
00352   if (C->getType()->isVectorTy()) {
00353     for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
00354          ++I) {
00355       ConstantFP *CFP = dyn_cast<ConstantFP>(C->getAggregateElement(I));
00356       if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
00357         return false;
00358     }
00359     return true;
00360   }
00361 
00362   return isa<ConstantFP>(C) &&
00363          cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero();
00364 }
00365 
00366 static bool isNormalFp(Constant *C) {
00367   if (C->getType()->isVectorTy()) {
00368     for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
00369          ++I) {
00370       ConstantFP *CFP = dyn_cast<ConstantFP>(C->getAggregateElement(I));
00371       if (!CFP || !CFP->getValueAPF().isNormal())
00372         return false;
00373     }
00374     return true;
00375   }
00376 
00377   return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal();
00378 }
00379 
00380 /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns
00381 /// true iff the given value is FMul or FDiv with one and only one operand
00382 /// being a normal constant (i.e. not Zero/NaN/Infinity).
00383 static bool isFMulOrFDivWithConstant(Value *V) {
00384   Instruction *I = dyn_cast<Instruction>(V);
00385   if (!I || (I->getOpcode() != Instruction::FMul &&
00386              I->getOpcode() != Instruction::FDiv))
00387     return false;
00388 
00389   Constant *C0 = dyn_cast<Constant>(I->getOperand(0));
00390   Constant *C1 = dyn_cast<Constant>(I->getOperand(1));
00391 
00392   if (C0 && C1)
00393     return false;
00394 
00395   return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1));
00396 }
00397 
00398 /// foldFMulConst() is a helper routine of InstCombiner::visitFMul().
00399 /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand
00400 /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true).
00401 /// This function is to simplify "FMulOrDiv * C" and returns the
00402 /// resulting expression. Note that this function could return NULL in
00403 /// case the constants cannot be folded into a normal floating-point.
00404 ///
00405 Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C,
00406                                    Instruction *InsertBefore) {
00407   assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid");
00408 
00409   Value *Opnd0 = FMulOrDiv->getOperand(0);
00410   Value *Opnd1 = FMulOrDiv->getOperand(1);
00411 
00412   Constant *C0 = dyn_cast<Constant>(Opnd0);
00413   Constant *C1 = dyn_cast<Constant>(Opnd1);
00414 
00415   BinaryOperator *R = nullptr;
00416 
00417   // (X * C0) * C => X * (C0*C)
00418   if (FMulOrDiv->getOpcode() == Instruction::FMul) {
00419     Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C);
00420     if (isNormalFp(F))
00421       R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
00422   } else {
00423     if (C0) {
00424       // (C0 / X) * C => (C0 * C) / X
00425       if (FMulOrDiv->hasOneUse()) {
00426         // It would otherwise introduce another div.
00427         Constant *F = ConstantExpr::getFMul(C0, C);
00428         if (isNormalFp(F))
00429           R = BinaryOperator::CreateFDiv(F, Opnd1);
00430       }
00431     } else {
00432       // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal
00433       Constant *F = ConstantExpr::getFDiv(C, C1);
00434       if (isNormalFp(F)) {
00435         R = BinaryOperator::CreateFMul(Opnd0, F);
00436       } else {
00437         // (X / C1) * C => X / (C1/C)
00438         Constant *F = ConstantExpr::getFDiv(C1, C);
00439         if (isNormalFp(F))
00440           R = BinaryOperator::CreateFDiv(Opnd0, F);
00441       }
00442     }
00443   }
00444 
00445   if (R) {
00446     R->setHasUnsafeAlgebra(true);
00447     InsertNewInstWith(R, *InsertBefore);
00448   }
00449 
00450   return R;
00451 }
00452 
00453 Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
00454   bool Changed = SimplifyAssociativeOrCommutative(I);
00455   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00456 
00457   if (Value *V = SimplifyVectorOp(I))
00458     return ReplaceInstUsesWith(I, V);
00459 
00460   if (isa<Constant>(Op0))
00461     std::swap(Op0, Op1);
00462 
00463   if (Value *V = SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), DL, TLI,
00464                                   DT, AT))
00465     return ReplaceInstUsesWith(I, V);
00466 
00467   bool AllowReassociate = I.hasUnsafeAlgebra();
00468 
00469   // Simplify mul instructions with a constant RHS.
00470   if (isa<Constant>(Op1)) {
00471     // Try to fold constant mul into select arguments.
00472     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
00473       if (Instruction *R = FoldOpIntoSelect(I, SI))
00474         return R;
00475 
00476     if (isa<PHINode>(Op0))
00477       if (Instruction *NV = FoldOpIntoPhi(I))
00478         return NV;
00479 
00480     // (fmul X, -1.0) --> (fsub -0.0, X)
00481     if (match(Op1, m_SpecificFP(-1.0))) {
00482       Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType());
00483       Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
00484       RI->copyFastMathFlags(&I);
00485       return RI;
00486     }
00487 
00488     Constant *C = cast<Constant>(Op1);
00489     if (AllowReassociate && isFiniteNonZeroFp(C)) {
00490       // Let MDC denote an expression in one of these forms:
00491       // X * C, C/X, X/C, where C is a constant.
00492       //
00493       // Try to simplify "MDC * Constant"
00494       if (isFMulOrFDivWithConstant(Op0))
00495         if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
00496           return ReplaceInstUsesWith(I, V);
00497 
00498       // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C)
00499       Instruction *FAddSub = dyn_cast<Instruction>(Op0);
00500       if (FAddSub &&
00501           (FAddSub->getOpcode() == Instruction::FAdd ||
00502            FAddSub->getOpcode() == Instruction::FSub)) {
00503         Value *Opnd0 = FAddSub->getOperand(0);
00504         Value *Opnd1 = FAddSub->getOperand(1);
00505         Constant *C0 = dyn_cast<Constant>(Opnd0);
00506         Constant *C1 = dyn_cast<Constant>(Opnd1);
00507         bool Swap = false;
00508         if (C0) {
00509           std::swap(C0, C1);
00510           std::swap(Opnd0, Opnd1);
00511           Swap = true;
00512         }
00513 
00514         if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) {
00515           Value *M1 = ConstantExpr::getFMul(C1, C);
00516           Value *M0 = isNormalFp(cast<Constant>(M1)) ?
00517                       foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
00518                       nullptr;
00519           if (M0 && M1) {
00520             if (Swap && FAddSub->getOpcode() == Instruction::FSub)
00521               std::swap(M0, M1);
00522 
00523             Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd)
00524                                   ? BinaryOperator::CreateFAdd(M0, M1)
00525                                   : BinaryOperator::CreateFSub(M0, M1);
00526             RI->copyFastMathFlags(&I);
00527             return RI;
00528           }
00529         }
00530       }
00531     }
00532   }
00533 
00534 
00535   // Under unsafe algebra do:
00536   // X * log2(0.5*Y) = X*log2(Y) - X
00537   if (I.hasUnsafeAlgebra()) {
00538     Value *OpX = nullptr;
00539     Value *OpY = nullptr;
00540     IntrinsicInst *Log2;
00541     detectLog2OfHalf(Op0, OpY, Log2);
00542     if (OpY) {
00543       OpX = Op1;
00544     } else {
00545       detectLog2OfHalf(Op1, OpY, Log2);
00546       if (OpY) {
00547         OpX = Op0;
00548       }
00549     }
00550     // if pattern detected emit alternate sequence
00551     if (OpX && OpY) {
00552       BuilderTy::FastMathFlagGuard Guard(*Builder);
00553       Builder->SetFastMathFlags(Log2->getFastMathFlags());
00554       Log2->setArgOperand(0, OpY);
00555       Value *FMulVal = Builder->CreateFMul(OpX, Log2);
00556       Value *FSub = Builder->CreateFSub(FMulVal, OpX);
00557       FSub->takeName(&I);
00558       return ReplaceInstUsesWith(I, FSub);
00559     }
00560   }
00561 
00562   // Handle symmetric situation in a 2-iteration loop
00563   Value *Opnd0 = Op0;
00564   Value *Opnd1 = Op1;
00565   for (int i = 0; i < 2; i++) {
00566     bool IgnoreZeroSign = I.hasNoSignedZeros();
00567     if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) {
00568       BuilderTy::FastMathFlagGuard Guard(*Builder);
00569       Builder->SetFastMathFlags(I.getFastMathFlags());
00570 
00571       Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
00572       Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
00573 
00574       // -X * -Y => X*Y
00575       if (N1) {
00576         Value *FMul = Builder->CreateFMul(N0, N1);
00577         FMul->takeName(&I);
00578         return ReplaceInstUsesWith(I, FMul);
00579       }
00580 
00581       if (Opnd0->hasOneUse()) {
00582         // -X * Y => -(X*Y) (Promote negation as high as possible)
00583         Value *T = Builder->CreateFMul(N0, Opnd1);
00584         Value *Neg = Builder->CreateFNeg(T);
00585         Neg->takeName(&I);
00586         return ReplaceInstUsesWith(I, Neg);
00587       }
00588     }
00589 
00590     // (X*Y) * X => (X*X) * Y where Y != X
00591     //  The purpose is two-fold:
00592     //   1) to form a power expression (of X).
00593     //   2) potentially shorten the critical path: After transformation, the
00594     //  latency of the instruction Y is amortized by the expression of X*X,
00595     //  and therefore Y is in a "less critical" position compared to what it
00596     //  was before the transformation.
00597     //
00598     if (AllowReassociate) {
00599       Value *Opnd0_0, *Opnd0_1;
00600       if (Opnd0->hasOneUse() &&
00601           match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) {
00602         Value *Y = nullptr;
00603         if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
00604           Y = Opnd0_1;
00605         else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
00606           Y = Opnd0_0;
00607 
00608         if (Y) {
00609           BuilderTy::FastMathFlagGuard Guard(*Builder);
00610           Builder->SetFastMathFlags(I.getFastMathFlags());
00611           Value *T = Builder->CreateFMul(Opnd1, Opnd1);
00612 
00613           Value *R = Builder->CreateFMul(T, Y);
00614           R->takeName(&I);
00615           return ReplaceInstUsesWith(I, R);
00616         }
00617       }
00618     }
00619 
00620     if (!isa<Constant>(Op1))
00621       std::swap(Opnd0, Opnd1);
00622     else
00623       break;
00624   }
00625 
00626   return Changed ? &I : nullptr;
00627 }
00628 
00629 /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
00630 /// instruction.
00631 bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
00632   SelectInst *SI = cast<SelectInst>(I.getOperand(1));
00633 
00634   // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
00635   int NonNullOperand = -1;
00636   if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
00637     if (ST->isNullValue())
00638       NonNullOperand = 2;
00639   // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
00640   if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
00641     if (ST->isNullValue())
00642       NonNullOperand = 1;
00643 
00644   if (NonNullOperand == -1)
00645     return false;
00646 
00647   Value *SelectCond = SI->getOperand(0);
00648 
00649   // Change the div/rem to use 'Y' instead of the select.
00650   I.setOperand(1, SI->getOperand(NonNullOperand));
00651 
00652   // Okay, we know we replace the operand of the div/rem with 'Y' with no
00653   // problem.  However, the select, or the condition of the select may have
00654   // multiple uses.  Based on our knowledge that the operand must be non-zero,
00655   // propagate the known value for the select into other uses of it, and
00656   // propagate a known value of the condition into its other users.
00657 
00658   // If the select and condition only have a single use, don't bother with this,
00659   // early exit.
00660   if (SI->use_empty() && SelectCond->hasOneUse())
00661     return true;
00662 
00663   // Scan the current block backward, looking for other uses of SI.
00664   BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
00665 
00666   while (BBI != BBFront) {
00667     --BBI;
00668     // If we found a call to a function, we can't assume it will return, so
00669     // information from below it cannot be propagated above it.
00670     if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
00671       break;
00672 
00673     // Replace uses of the select or its condition with the known values.
00674     for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
00675          I != E; ++I) {
00676       if (*I == SI) {
00677         *I = SI->getOperand(NonNullOperand);
00678         Worklist.Add(BBI);
00679       } else if (*I == SelectCond) {
00680         *I = Builder->getInt1(NonNullOperand == 1);
00681         Worklist.Add(BBI);
00682       }
00683     }
00684 
00685     // If we past the instruction, quit looking for it.
00686     if (&*BBI == SI)
00687       SI = nullptr;
00688     if (&*BBI == SelectCond)
00689       SelectCond = nullptr;
00690 
00691     // If we ran out of things to eliminate, break out of the loop.
00692     if (!SelectCond && !SI)
00693       break;
00694 
00695   }
00696   return true;
00697 }
00698 
00699 
00700 /// This function implements the transforms common to both integer division
00701 /// instructions (udiv and sdiv). It is called by the visitors to those integer
00702 /// division instructions.
00703 /// @brief Common integer divide transforms
00704 Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
00705   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00706 
00707   // The RHS is known non-zero.
00708   if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, &I)) {
00709     I.setOperand(1, V);
00710     return &I;
00711   }
00712 
00713   // Handle cases involving: [su]div X, (select Cond, Y, Z)
00714   // This does not apply for fdiv.
00715   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
00716     return &I;
00717 
00718   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
00719     if (Instruction *LHS = dyn_cast<Instruction>(Op0)) {
00720       // (X / C1) / C2  -> X / (C1*C2)
00721       if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode())
00722         if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
00723           if (MultiplyOverflows(RHS, LHSRHS,
00724                                 I.getOpcode() == Instruction::SDiv))
00725             return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
00726           return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
00727                                         ConstantExpr::getMul(RHS, LHSRHS));
00728         }
00729 
00730       Value *X;
00731       const APInt *C1, *C2;
00732       if (match(RHS, m_APInt(C2))) {
00733         bool IsSigned = I.getOpcode() == Instruction::SDiv;
00734         if ((IsSigned && match(LHS, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
00735             (!IsSigned && match(LHS, m_NUWMul(m_Value(X), m_APInt(C1))))) {
00736           APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
00737 
00738           // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
00739           if (IsMultiple(*C2, *C1, Quotient, IsSigned)) {
00740             BinaryOperator *BO = BinaryOperator::Create(
00741                 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
00742             BO->setIsExact(I.isExact());
00743             return BO;
00744           }
00745 
00746           // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
00747           if (IsMultiple(*C1, *C2, Quotient, IsSigned)) {
00748             BinaryOperator *BO = BinaryOperator::Create(
00749                 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
00750             BO->setHasNoUnsignedWrap(
00751                 !IsSigned &&
00752                 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
00753             BO->setHasNoSignedWrap(
00754                 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
00755             return BO;
00756           }
00757         }
00758 
00759         if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1)))) ||
00760             (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) {
00761           APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
00762           APInt C1Shifted = APInt::getOneBitSet(
00763               C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue()));
00764 
00765           // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1.
00766           if (IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
00767             BinaryOperator *BO = BinaryOperator::Create(
00768                 I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
00769             BO->setIsExact(I.isExact());
00770             return BO;
00771           }
00772 
00773           // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2.
00774           if (IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
00775             BinaryOperator *BO = BinaryOperator::Create(
00776                 Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
00777             BO->setHasNoUnsignedWrap(
00778                 !IsSigned &&
00779                 cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
00780             BO->setHasNoSignedWrap(
00781                 cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
00782             return BO;
00783           }
00784         }
00785       }
00786     }
00787 
00788     if (!RHS->isZero()) { // avoid X udiv 0
00789       if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
00790         if (Instruction *R = FoldOpIntoSelect(I, SI))
00791           return R;
00792       if (isa<PHINode>(Op0))
00793         if (Instruction *NV = FoldOpIntoPhi(I))
00794           return NV;
00795     }
00796   }
00797 
00798   if (ConstantInt *One = dyn_cast<ConstantInt>(Op0)) {
00799     if (One->isOne() && !I.getType()->isIntegerTy(1)) {
00800       bool isSigned = I.getOpcode() == Instruction::SDiv;
00801       if (isSigned) {
00802         // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
00803         // result is one, if Op1 is -1 then the result is minus one, otherwise
00804         // it's zero.
00805         Value *Inc = Builder->CreateAdd(Op1, One);
00806         Value *Cmp = Builder->CreateICmpULT(
00807                          Inc, ConstantInt::get(I.getType(), 3));
00808         return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0));
00809       } else {
00810         // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
00811         // result is one, otherwise it's zero.
00812         return new ZExtInst(Builder->CreateICmpEQ(Op1, One), I.getType());
00813       }
00814     }
00815   }
00816 
00817   // See if we can fold away this div instruction.
00818   if (SimplifyDemandedInstructionBits(I))
00819     return &I;
00820 
00821   // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
00822   Value *X = nullptr, *Z = nullptr;
00823   if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1
00824     bool isSigned = I.getOpcode() == Instruction::SDiv;
00825     if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
00826         (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
00827       return BinaryOperator::Create(I.getOpcode(), X, Op1);
00828   }
00829 
00830   return nullptr;
00831 }
00832 
00833 /// dyn_castZExtVal - Checks if V is a zext or constant that can
00834 /// be truncated to Ty without losing bits.
00835 static Value *dyn_castZExtVal(Value *V, Type *Ty) {
00836   if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) {
00837     if (Z->getSrcTy() == Ty)
00838       return Z->getOperand(0);
00839   } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) {
00840     if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth())
00841       return ConstantExpr::getTrunc(C, Ty);
00842   }
00843   return nullptr;
00844 }
00845 
00846 namespace {
00847 const unsigned MaxDepth = 6;
00848 typedef Instruction *(*FoldUDivOperandCb)(Value *Op0, Value *Op1,
00849                                           const BinaryOperator &I,
00850                                           InstCombiner &IC);
00851 
00852 /// \brief Used to maintain state for visitUDivOperand().
00853 struct UDivFoldAction {
00854   FoldUDivOperandCb FoldAction; ///< Informs visitUDiv() how to fold this
00855                                 ///< operand.  This can be zero if this action
00856                                 ///< joins two actions together.
00857 
00858   Value *OperandToFold;         ///< Which operand to fold.
00859   union {
00860     Instruction *FoldResult;    ///< The instruction returned when FoldAction is
00861                                 ///< invoked.
00862 
00863     size_t SelectLHSIdx;        ///< Stores the LHS action index if this action
00864                                 ///< joins two actions together.
00865   };
00866 
00867   UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
00868       : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
00869   UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
00870       : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
00871 };
00872 }
00873 
00874 // X udiv 2^C -> X >> C
00875 static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
00876                                     const BinaryOperator &I, InstCombiner &IC) {
00877   const APInt &C = cast<Constant>(Op1)->getUniqueInteger();
00878   BinaryOperator *LShr = BinaryOperator::CreateLShr(
00879       Op0, ConstantInt::get(Op0->getType(), C.logBase2()));
00880   if (I.isExact()) LShr->setIsExact();
00881   return LShr;
00882 }
00883 
00884 // X udiv C, where C >= signbit
00885 static Instruction *foldUDivNegCst(Value *Op0, Value *Op1,
00886                                    const BinaryOperator &I, InstCombiner &IC) {
00887   Value *ICI = IC.Builder->CreateICmpULT(Op0, cast<ConstantInt>(Op1));
00888 
00889   return SelectInst::Create(ICI, Constant::getNullValue(I.getType()),
00890                             ConstantInt::get(I.getType(), 1));
00891 }
00892 
00893 // X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2)
00894 static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
00895                                 InstCombiner &IC) {
00896   Instruction *ShiftLeft = cast<Instruction>(Op1);
00897   if (isa<ZExtInst>(ShiftLeft))
00898     ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0));
00899 
00900   const APInt &CI =
00901       cast<Constant>(ShiftLeft->getOperand(0))->getUniqueInteger();
00902   Value *N = ShiftLeft->getOperand(1);
00903   if (CI != 1)
00904     N = IC.Builder->CreateAdd(N, ConstantInt::get(N->getType(), CI.logBase2()));
00905   if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1))
00906     N = IC.Builder->CreateZExt(N, Z->getDestTy());
00907   BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
00908   if (I.isExact()) LShr->setIsExact();
00909   return LShr;
00910 }
00911 
00912 // \brief Recursively visits the possible right hand operands of a udiv
00913 // instruction, seeing through select instructions, to determine if we can
00914 // replace the udiv with something simpler.  If we find that an operand is not
00915 // able to simplify the udiv, we abort the entire transformation.
00916 static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
00917                                SmallVectorImpl<UDivFoldAction> &Actions,
00918                                unsigned Depth = 0) {
00919   // Check to see if this is an unsigned division with an exact power of 2,
00920   // if so, convert to a right shift.
00921   if (match(Op1, m_Power2())) {
00922     Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
00923     return Actions.size();
00924   }
00925 
00926   if (ConstantInt *C = dyn_cast<ConstantInt>(Op1))
00927     // X udiv C, where C >= signbit
00928     if (C->getValue().isNegative()) {
00929       Actions.push_back(UDivFoldAction(foldUDivNegCst, C));
00930       return Actions.size();
00931     }
00932 
00933   // X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2)
00934   if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
00935       match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
00936     Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
00937     return Actions.size();
00938   }
00939 
00940   // The remaining tests are all recursive, so bail out if we hit the limit.
00941   if (Depth++ == MaxDepth)
00942     return 0;
00943 
00944   if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
00945     if (size_t LHSIdx =
00946             visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
00947       if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
00948         Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
00949         return Actions.size();
00950       }
00951 
00952   return 0;
00953 }
00954 
00955 Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
00956   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00957 
00958   if (Value *V = SimplifyVectorOp(I))
00959     return ReplaceInstUsesWith(I, V);
00960 
00961   if (Value *V = SimplifyUDivInst(Op0, Op1, DL, TLI, DT, AT))
00962     return ReplaceInstUsesWith(I, V);
00963 
00964   // Handle the integer div common cases
00965   if (Instruction *Common = commonIDivTransforms(I))
00966     return Common;
00967 
00968   // (x lshr C1) udiv C2 --> x udiv (C2 << C1)
00969   if (Constant *C2 = dyn_cast<Constant>(Op1)) {
00970     Value *X;
00971     Constant *C1;
00972     if (match(Op0, m_LShr(m_Value(X), m_Constant(C1))))
00973       return BinaryOperator::CreateUDiv(X, ConstantExpr::getShl(C2, C1));
00974   }
00975 
00976   // (zext A) udiv (zext B) --> zext (A udiv B)
00977   if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
00978     if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
00979       return new ZExtInst(Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div",
00980                                               I.isExact()),
00981                           I.getType());
00982 
00983   // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
00984   SmallVector<UDivFoldAction, 6> UDivActions;
00985   if (visitUDivOperand(Op0, Op1, I, UDivActions))
00986     for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
00987       FoldUDivOperandCb Action = UDivActions[i].FoldAction;
00988       Value *ActionOp1 = UDivActions[i].OperandToFold;
00989       Instruction *Inst;
00990       if (Action)
00991         Inst = Action(Op0, ActionOp1, I, *this);
00992       else {
00993         // This action joins two actions together.  The RHS of this action is
00994         // simply the last action we processed, we saved the LHS action index in
00995         // the joining action.
00996         size_t SelectRHSIdx = i - 1;
00997         Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
00998         size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
00999         Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
01000         Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
01001                                   SelectLHS, SelectRHS);
01002       }
01003 
01004       // If this is the last action to process, return it to the InstCombiner.
01005       // Otherwise, we insert it before the UDiv and record it so that we may
01006       // use it as part of a joining action (i.e., a SelectInst).
01007       if (e - i != 1) {
01008         Inst->insertBefore(&I);
01009         UDivActions[i].FoldResult = Inst;
01010       } else
01011         return Inst;
01012     }
01013 
01014   return nullptr;
01015 }
01016 
01017 Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
01018   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01019 
01020   if (Value *V = SimplifyVectorOp(I))
01021     return ReplaceInstUsesWith(I, V);
01022 
01023   if (Value *V = SimplifySDivInst(Op0, Op1, DL, TLI, DT, AT))
01024     return ReplaceInstUsesWith(I, V);
01025 
01026   // Handle the integer div common cases
01027   if (Instruction *Common = commonIDivTransforms(I))
01028     return Common;
01029 
01030   // sdiv X, -1 == -X
01031   if (match(Op1, m_AllOnes()))
01032     return BinaryOperator::CreateNeg(Op0);
01033 
01034   if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
01035     // sdiv X, C  -->  ashr exact X, log2(C)
01036     if (I.isExact() && RHS->getValue().isNonNegative() &&
01037         RHS->getValue().isPowerOf2()) {
01038       Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
01039                                             RHS->getValue().exactLogBase2());
01040       return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
01041     }
01042   }
01043 
01044   if (Constant *RHS = dyn_cast<Constant>(Op1)) {
01045     // X/INT_MIN -> X == INT_MIN
01046     if (RHS->isMinSignedValue())
01047       return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType());
01048 
01049     // -X/C  -->  X/-C  provided the negation doesn't overflow.
01050     if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
01051       if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap())
01052         return BinaryOperator::CreateSDiv(Sub->getOperand(1),
01053                                           ConstantExpr::getNeg(RHS));
01054   }
01055 
01056   // If the sign bits of both operands are zero (i.e. we can prove they are
01057   // unsigned inputs), turn this into a udiv.
01058   if (I.getType()->isIntegerTy()) {
01059     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
01060     if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
01061       if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
01062         // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
01063         return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
01064       }
01065 
01066       if (match(Op1, m_Shl(m_Power2(), m_Value()))) {
01067         // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
01068         // Safe because the only negative value (1 << Y) can take on is
01069         // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
01070         // the sign bit set.
01071         return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
01072       }
01073     }
01074   }
01075 
01076   return nullptr;
01077 }
01078 
01079 /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special
01080 /// FP value and:
01081 ///    1) 1/C is exact, or
01082 ///    2) reciprocal is allowed.
01083 /// If the conversion was successful, the simplified expression "X * 1/C" is
01084 /// returned; otherwise, NULL is returned.
01085 ///
01086 static Instruction *CvtFDivConstToReciprocal(Value *Dividend,
01087                                              Constant *Divisor,
01088                                              bool AllowReciprocal) {
01089   if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors.
01090     return nullptr;
01091 
01092   const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
01093   APFloat Reciprocal(FpVal.getSemantics());
01094   bool Cvt = FpVal.getExactInverse(&Reciprocal);
01095 
01096   if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) {
01097     Reciprocal = APFloat(FpVal.getSemantics(), 1.0f);
01098     (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven);
01099     Cvt = !Reciprocal.isDenormal();
01100   }
01101 
01102   if (!Cvt)
01103     return nullptr;
01104 
01105   ConstantFP *R;
01106   R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal);
01107   return BinaryOperator::CreateFMul(Dividend, R);
01108 }
01109 
01110 Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
01111   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01112 
01113   if (Value *V = SimplifyVectorOp(I))
01114     return ReplaceInstUsesWith(I, V);
01115 
01116   if (Value *V = SimplifyFDivInst(Op0, Op1, DL, TLI, DT, AT))
01117     return ReplaceInstUsesWith(I, V);
01118 
01119   if (isa<Constant>(Op0))
01120     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
01121       if (Instruction *R = FoldOpIntoSelect(I, SI))
01122         return R;
01123 
01124   bool AllowReassociate = I.hasUnsafeAlgebra();
01125   bool AllowReciprocal = I.hasAllowReciprocal();
01126 
01127   if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
01128     if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
01129       if (Instruction *R = FoldOpIntoSelect(I, SI))
01130         return R;
01131 
01132     if (AllowReassociate) {
01133       Constant *C1 = nullptr;
01134       Constant *C2 = Op1C;
01135       Value *X;
01136       Instruction *Res = nullptr;
01137 
01138       if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) {
01139         // (X*C1)/C2 => X * (C1/C2)
01140         //
01141         Constant *C = ConstantExpr::getFDiv(C1, C2);
01142         if (isNormalFp(C))
01143           Res = BinaryOperator::CreateFMul(X, C);
01144       } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
01145         // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed]
01146         //
01147         Constant *C = ConstantExpr::getFMul(C1, C2);
01148         if (isNormalFp(C)) {
01149           Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal);
01150           if (!Res)
01151             Res = BinaryOperator::CreateFDiv(X, C);
01152         }
01153       }
01154 
01155       if (Res) {
01156         Res->setFastMathFlags(I.getFastMathFlags());
01157         return Res;
01158       }
01159     }
01160 
01161     // X / C => X * 1/C
01162     if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) {
01163       T->copyFastMathFlags(&I);
01164       return T;
01165     }
01166 
01167     return nullptr;
01168   }
01169 
01170   if (AllowReassociate && isa<Constant>(Op0)) {
01171     Constant *C1 = cast<Constant>(Op0), *C2;
01172     Constant *Fold = nullptr;
01173     Value *X;
01174     bool CreateDiv = true;
01175 
01176     // C1 / (X*C2) => (C1/C2) / X
01177     if (match(Op1, m_FMul(m_Value(X), m_Constant(C2))))
01178       Fold = ConstantExpr::getFDiv(C1, C2);
01179     else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) {
01180       // C1 / (X/C2) => (C1*C2) / X
01181       Fold = ConstantExpr::getFMul(C1, C2);
01182     } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) {
01183       // C1 / (C2/X) => (C1/C2) * X
01184       Fold = ConstantExpr::getFDiv(C1, C2);
01185       CreateDiv = false;
01186     }
01187 
01188     if (Fold && isNormalFp(Fold)) {
01189       Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
01190                                  : BinaryOperator::CreateFMul(X, Fold);
01191       R->setFastMathFlags(I.getFastMathFlags());
01192       return R;
01193     }
01194     return nullptr;
01195   }
01196 
01197   if (AllowReassociate) {
01198     Value *X, *Y;
01199     Value *NewInst = nullptr;
01200     Instruction *SimpR = nullptr;
01201 
01202     if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) {
01203       // (X/Y) / Z => X / (Y*Z)
01204       //
01205       if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
01206         NewInst = Builder->CreateFMul(Y, Op1);
01207         if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
01208           FastMathFlags Flags = I.getFastMathFlags();
01209           Flags &= cast<Instruction>(Op0)->getFastMathFlags();
01210           RI->setFastMathFlags(Flags);
01211         }
01212         SimpR = BinaryOperator::CreateFDiv(X, NewInst);
01213       }
01214     } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) {
01215       // Z / (X/Y) => Z*Y / X
01216       //
01217       if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
01218         NewInst = Builder->CreateFMul(Op0, Y);
01219         if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
01220           FastMathFlags Flags = I.getFastMathFlags();
01221           Flags &= cast<Instruction>(Op1)->getFastMathFlags();
01222           RI->setFastMathFlags(Flags);
01223         }
01224         SimpR = BinaryOperator::CreateFDiv(NewInst, X);
01225       }
01226     }
01227 
01228     if (NewInst) {
01229       if (Instruction *T = dyn_cast<Instruction>(NewInst))
01230         T->setDebugLoc(I.getDebugLoc());
01231       SimpR->setFastMathFlags(I.getFastMathFlags());
01232       return SimpR;
01233     }
01234   }
01235 
01236   return nullptr;
01237 }
01238 
01239 /// This function implements the transforms common to both integer remainder
01240 /// instructions (urem and srem). It is called by the visitors to those integer
01241 /// remainder instructions.
01242 /// @brief Common integer remainder transforms
01243 Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
01244   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01245 
01246   // The RHS is known non-zero.
01247   if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, &I)) {
01248     I.setOperand(1, V);
01249     return &I;
01250   }
01251 
01252   // Handle cases involving: rem X, (select Cond, Y, Z)
01253   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
01254     return &I;
01255 
01256   if (isa<Constant>(Op1)) {
01257     if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
01258       if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
01259         if (Instruction *R = FoldOpIntoSelect(I, SI))
01260           return R;
01261       } else if (isa<PHINode>(Op0I)) {
01262         if (Instruction *NV = FoldOpIntoPhi(I))
01263           return NV;
01264       }
01265 
01266       // See if we can fold away this rem instruction.
01267       if (SimplifyDemandedInstructionBits(I))
01268         return &I;
01269     }
01270   }
01271 
01272   return nullptr;
01273 }
01274 
01275 Instruction *InstCombiner::visitURem(BinaryOperator &I) {
01276   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01277 
01278   if (Value *V = SimplifyVectorOp(I))
01279     return ReplaceInstUsesWith(I, V);
01280 
01281   if (Value *V = SimplifyURemInst(Op0, Op1, DL, TLI, DT, AT))
01282     return ReplaceInstUsesWith(I, V);
01283 
01284   if (Instruction *common = commonIRemTransforms(I))
01285     return common;
01286 
01287   // (zext A) urem (zext B) --> zext (A urem B)
01288   if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
01289     if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
01290       return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
01291                           I.getType());
01292 
01293   // X urem Y -> X and Y-1, where Y is a power of 2,
01294   if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/true, 0, AT, &I, DT)) {
01295     Constant *N1 = Constant::getAllOnesValue(I.getType());
01296     Value *Add = Builder->CreateAdd(Op1, N1);
01297     return BinaryOperator::CreateAnd(Op0, Add);
01298   }
01299 
01300   // 1 urem X -> zext(X != 1)
01301   if (match(Op0, m_One())) {
01302     Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
01303     Value *Ext = Builder->CreateZExt(Cmp, I.getType());
01304     return ReplaceInstUsesWith(I, Ext);
01305   }
01306 
01307   return nullptr;
01308 }
01309 
01310 Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
01311   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01312 
01313   if (Value *V = SimplifyVectorOp(I))
01314     return ReplaceInstUsesWith(I, V);
01315 
01316   if (Value *V = SimplifySRemInst(Op0, Op1, DL, TLI, DT, AT))
01317     return ReplaceInstUsesWith(I, V);
01318 
01319   // Handle the integer rem common cases
01320   if (Instruction *Common = commonIRemTransforms(I))
01321     return Common;
01322 
01323   if (Value *RHSNeg = dyn_castNegVal(Op1))
01324     if (!isa<Constant>(RHSNeg) ||
01325         (isa<ConstantInt>(RHSNeg) &&
01326          cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) {
01327       // X % -Y -> X % Y
01328       Worklist.AddValue(I.getOperand(1));
01329       I.setOperand(1, RHSNeg);
01330       return &I;
01331     }
01332 
01333   // If the sign bits of both operands are zero (i.e. we can prove they are
01334   // unsigned inputs), turn this into a urem.
01335   if (I.getType()->isIntegerTy()) {
01336     APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
01337     if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
01338         MaskedValueIsZero(Op0, Mask, 0, &I)) {
01339       // X srem Y -> X urem Y, iff X and Y don't have sign bit set
01340       return BinaryOperator::CreateURem(Op0, Op1, I.getName());
01341     }
01342   }
01343 
01344   // If it's a constant vector, flip any negative values positive.
01345   if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
01346     Constant *C = cast<Constant>(Op1);
01347     unsigned VWidth = C->getType()->getVectorNumElements();
01348 
01349     bool hasNegative = false;
01350     bool hasMissing = false;
01351     for (unsigned i = 0; i != VWidth; ++i) {
01352       Constant *Elt = C->getAggregateElement(i);
01353       if (!Elt) {
01354         hasMissing = true;
01355         break;
01356       }
01357 
01358       if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
01359         if (RHS->isNegative())
01360           hasNegative = true;
01361     }
01362 
01363     if (hasNegative && !hasMissing) {
01364       SmallVector<Constant *, 16> Elts(VWidth);
01365       for (unsigned i = 0; i != VWidth; ++i) {
01366         Elts[i] = C->getAggregateElement(i);  // Handle undef, etc.
01367         if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
01368           if (RHS->isNegative())
01369             Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
01370         }
01371       }
01372 
01373       Constant *NewRHSV = ConstantVector::get(Elts);
01374       if (NewRHSV != C) {  // Don't loop on -MININT
01375         Worklist.AddValue(I.getOperand(1));
01376         I.setOperand(1, NewRHSV);
01377         return &I;
01378       }
01379     }
01380   }
01381 
01382   return nullptr;
01383 }
01384 
01385 Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
01386   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
01387 
01388   if (Value *V = SimplifyVectorOp(I))
01389     return ReplaceInstUsesWith(I, V);
01390 
01391   if (Value *V = SimplifyFRemInst(Op0, Op1, DL, TLI, DT, AT))
01392     return ReplaceInstUsesWith(I, V);
01393 
01394   // Handle cases involving: rem X, (select Cond, Y, Z)
01395   if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
01396     return &I;
01397 
01398   return nullptr;
01399 }