LLVM API Documentation

InstCombineShifts.cpp
Go to the documentation of this file.
00001 //===- InstCombineShifts.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 visitShl, visitLShr, and visitAShr functions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "InstCombineInternal.h"
00015 #include "llvm/Analysis/ConstantFolding.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 Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
00025   assert(I.getOperand(1)->getType() == I.getOperand(0)->getType());
00026   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00027 
00028   // See if we can fold away this shift.
00029   if (SimplifyDemandedInstructionBits(I))
00030     return &I;
00031 
00032   // Try to fold constant and into select arguments.
00033   if (isa<Constant>(Op0))
00034     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
00035       if (Instruction *R = FoldOpIntoSelect(I, SI))
00036         return R;
00037 
00038   if (Constant *CUI = dyn_cast<Constant>(Op1))
00039     if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
00040       return Res;
00041 
00042   // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2.
00043   // Because shifts by negative values (which could occur if A were negative)
00044   // are undefined.
00045   Value *A; const APInt *B;
00046   if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) {
00047     // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
00048     // demand the sign bit (and many others) here??
00049     Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1),
00050                                     Op1->getName());
00051     I.setOperand(1, Rem);
00052     return &I;
00053   }
00054 
00055   return nullptr;
00056 }
00057 
00058 /// CanEvaluateShifted - See if we can compute the specified value, but shifted
00059 /// logically to the left or right by some number of bits.  This should return
00060 /// true if the expression can be computed for the same cost as the current
00061 /// expression tree.  This is used to eliminate extraneous shifting from things
00062 /// like:
00063 ///      %C = shl i128 %A, 64
00064 ///      %D = shl i128 %B, 96
00065 ///      %E = or i128 %C, %D
00066 ///      %F = lshr i128 %E, 64
00067 /// where the client will ask if E can be computed shifted right by 64-bits.  If
00068 /// this succeeds, the GetShiftedValue function will be called to produce the
00069 /// value.
00070 static bool CanEvaluateShifted(Value *V, unsigned NumBits, bool isLeftShift,
00071                                InstCombiner &IC, Instruction *CxtI) {
00072   // We can always evaluate constants shifted.
00073   if (isa<Constant>(V))
00074     return true;
00075 
00076   Instruction *I = dyn_cast<Instruction>(V);
00077   if (!I) return false;
00078 
00079   // If this is the opposite shift, we can directly reuse the input of the shift
00080   // if the needed bits are already zero in the input.  This allows us to reuse
00081   // the value which means that we don't care if the shift has multiple uses.
00082   //  TODO:  Handle opposite shift by exact value.
00083   ConstantInt *CI = nullptr;
00084   if ((isLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) ||
00085       (!isLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) {
00086     if (CI->getZExtValue() == NumBits) {
00087       // TODO: Check that the input bits are already zero with MaskedValueIsZero
00088 #if 0
00089       // If this is a truncate of a logical shr, we can truncate it to a smaller
00090       // lshr iff we know that the bits we would otherwise be shifting in are
00091       // already zeros.
00092       uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
00093       uint32_t BitWidth = Ty->getScalarSizeInBits();
00094       if (MaskedValueIsZero(I->getOperand(0),
00095             APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) &&
00096           CI->getLimitedValue(BitWidth) < BitWidth) {
00097         return CanEvaluateTruncated(I->getOperand(0), Ty);
00098       }
00099 #endif
00100 
00101     }
00102   }
00103 
00104   // We can't mutate something that has multiple uses: doing so would
00105   // require duplicating the instruction in general, which isn't profitable.
00106   if (!I->hasOneUse()) return false;
00107 
00108   switch (I->getOpcode()) {
00109   default: return false;
00110   case Instruction::And:
00111   case Instruction::Or:
00112   case Instruction::Xor:
00113     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
00114     return CanEvaluateShifted(I->getOperand(0), NumBits, isLeftShift, IC, I) &&
00115            CanEvaluateShifted(I->getOperand(1), NumBits, isLeftShift, IC, I);
00116 
00117   case Instruction::Shl: {
00118     // We can often fold the shift into shifts-by-a-constant.
00119     CI = dyn_cast<ConstantInt>(I->getOperand(1));
00120     if (!CI) return false;
00121 
00122     // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
00123     if (isLeftShift) return true;
00124 
00125     // We can always turn shl(c)+shr(c) -> and(c2).
00126     if (CI->getValue() == NumBits) return true;
00127 
00128     unsigned TypeWidth = I->getType()->getScalarSizeInBits();
00129 
00130     // We can turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but it isn't
00131     // profitable unless we know the and'd out bits are already zero.
00132     if (CI->getZExtValue() > NumBits) {
00133       unsigned LowBits = TypeWidth - CI->getZExtValue();
00134       if (IC.MaskedValueIsZero(I->getOperand(0),
00135                        APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits,
00136                        0, CxtI))
00137         return true;
00138     }
00139 
00140     return false;
00141   }
00142   case Instruction::LShr: {
00143     // We can often fold the shift into shifts-by-a-constant.
00144     CI = dyn_cast<ConstantInt>(I->getOperand(1));
00145     if (!CI) return false;
00146 
00147     // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
00148     if (!isLeftShift) return true;
00149 
00150     // We can always turn lshr(c)+shl(c) -> and(c2).
00151     if (CI->getValue() == NumBits) return true;
00152 
00153     unsigned TypeWidth = I->getType()->getScalarSizeInBits();
00154 
00155     // We can always turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but it isn't
00156     // profitable unless we know the and'd out bits are already zero.
00157     if (CI->getValue().ult(TypeWidth) && CI->getZExtValue() > NumBits) {
00158       unsigned LowBits = CI->getZExtValue() - NumBits;
00159       if (IC.MaskedValueIsZero(I->getOperand(0),
00160                           APInt::getLowBitsSet(TypeWidth, NumBits) << LowBits,
00161                           0, CxtI))
00162         return true;
00163     }
00164 
00165     return false;
00166   }
00167   case Instruction::Select: {
00168     SelectInst *SI = cast<SelectInst>(I);
00169     return CanEvaluateShifted(SI->getTrueValue(), NumBits, isLeftShift,
00170                               IC, SI) &&
00171            CanEvaluateShifted(SI->getFalseValue(), NumBits, isLeftShift, IC, SI);
00172   }
00173   case Instruction::PHI: {
00174     // We can change a phi if we can change all operands.  Note that we never
00175     // get into trouble with cyclic PHIs here because we only consider
00176     // instructions with a single use.
00177     PHINode *PN = cast<PHINode>(I);
00178     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00179       if (!CanEvaluateShifted(PN->getIncomingValue(i), NumBits, isLeftShift,
00180                               IC, PN))
00181         return false;
00182     return true;
00183   }
00184   }
00185 }
00186 
00187 /// GetShiftedValue - When CanEvaluateShifted returned true for an expression,
00188 /// this value inserts the new computation that produces the shifted value.
00189 static Value *GetShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
00190                               InstCombiner &IC) {
00191   // We can always evaluate constants shifted.
00192   if (Constant *C = dyn_cast<Constant>(V)) {
00193     if (isLeftShift)
00194       V = IC.Builder->CreateShl(C, NumBits);
00195     else
00196       V = IC.Builder->CreateLShr(C, NumBits);
00197     // If we got a constantexpr back, try to simplify it with TD info.
00198     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00199       V = ConstantFoldConstantExpression(CE, IC.getDataLayout(),
00200                                          IC.getTargetLibraryInfo());
00201     return V;
00202   }
00203 
00204   Instruction *I = cast<Instruction>(V);
00205   IC.Worklist.Add(I);
00206 
00207   switch (I->getOpcode()) {
00208   default: llvm_unreachable("Inconsistency with CanEvaluateShifted");
00209   case Instruction::And:
00210   case Instruction::Or:
00211   case Instruction::Xor:
00212     // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
00213     I->setOperand(0, GetShiftedValue(I->getOperand(0), NumBits,isLeftShift,IC));
00214     I->setOperand(1, GetShiftedValue(I->getOperand(1), NumBits,isLeftShift,IC));
00215     return I;
00216 
00217   case Instruction::Shl: {
00218     BinaryOperator *BO = cast<BinaryOperator>(I);
00219     unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
00220 
00221     // We only accept shifts-by-a-constant in CanEvaluateShifted.
00222     ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
00223 
00224     // We can always fold shl(c1)+shl(c2) -> shl(c1+c2).
00225     if (isLeftShift) {
00226       // If this is oversized composite shift, then unsigned shifts get 0.
00227       unsigned NewShAmt = NumBits+CI->getZExtValue();
00228       if (NewShAmt >= TypeWidth)
00229         return Constant::getNullValue(I->getType());
00230 
00231       BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
00232       BO->setHasNoUnsignedWrap(false);
00233       BO->setHasNoSignedWrap(false);
00234       return I;
00235     }
00236 
00237     // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have
00238     // zeros.
00239     if (CI->getValue() == NumBits) {
00240       APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits));
00241       V = IC.Builder->CreateAnd(BO->getOperand(0),
00242                                 ConstantInt::get(BO->getContext(), Mask));
00243       if (Instruction *VI = dyn_cast<Instruction>(V)) {
00244         VI->moveBefore(BO);
00245         VI->takeName(BO);
00246       }
00247       return V;
00248     }
00249 
00250     // We turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but only when we know that
00251     // the and won't be needed.
00252     assert(CI->getZExtValue() > NumBits);
00253     BO->setOperand(1, ConstantInt::get(BO->getType(),
00254                                        CI->getZExtValue() - NumBits));
00255     BO->setHasNoUnsignedWrap(false);
00256     BO->setHasNoSignedWrap(false);
00257     return BO;
00258   }
00259   case Instruction::LShr: {
00260     BinaryOperator *BO = cast<BinaryOperator>(I);
00261     unsigned TypeWidth = BO->getType()->getScalarSizeInBits();
00262     // We only accept shifts-by-a-constant in CanEvaluateShifted.
00263     ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
00264 
00265     // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2).
00266     if (!isLeftShift) {
00267       // If this is oversized composite shift, then unsigned shifts get 0.
00268       unsigned NewShAmt = NumBits+CI->getZExtValue();
00269       if (NewShAmt >= TypeWidth)
00270         return Constant::getNullValue(BO->getType());
00271 
00272       BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt));
00273       BO->setIsExact(false);
00274       return I;
00275     }
00276 
00277     // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have
00278     // zeros.
00279     if (CI->getValue() == NumBits) {
00280       APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits));
00281       V = IC.Builder->CreateAnd(I->getOperand(0),
00282                                 ConstantInt::get(BO->getContext(), Mask));
00283       if (Instruction *VI = dyn_cast<Instruction>(V)) {
00284         VI->moveBefore(I);
00285         VI->takeName(I);
00286       }
00287       return V;
00288     }
00289 
00290     // We turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but only when we know that
00291     // the and won't be needed.
00292     assert(CI->getZExtValue() > NumBits);
00293     BO->setOperand(1, ConstantInt::get(BO->getType(),
00294                                        CI->getZExtValue() - NumBits));
00295     BO->setIsExact(false);
00296     return BO;
00297   }
00298 
00299   case Instruction::Select:
00300     I->setOperand(1, GetShiftedValue(I->getOperand(1), NumBits,isLeftShift,IC));
00301     I->setOperand(2, GetShiftedValue(I->getOperand(2), NumBits,isLeftShift,IC));
00302     return I;
00303   case Instruction::PHI: {
00304     // We can change a phi if we can change all operands.  Note that we never
00305     // get into trouble with cyclic PHIs here because we only consider
00306     // instructions with a single use.
00307     PHINode *PN = cast<PHINode>(I);
00308     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00309       PN->setIncomingValue(i, GetShiftedValue(PN->getIncomingValue(i),
00310                                               NumBits, isLeftShift, IC));
00311     return PN;
00312   }
00313   }
00314 }
00315 
00316 
00317 
00318 Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1,
00319                                                BinaryOperator &I) {
00320   bool isLeftShift = I.getOpcode() == Instruction::Shl;
00321 
00322   ConstantInt *COp1 = nullptr;
00323   if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1))
00324     COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
00325   else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1))
00326     COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue());
00327   else
00328     COp1 = dyn_cast<ConstantInt>(Op1);
00329 
00330   if (!COp1)
00331     return nullptr;
00332 
00333   // See if we can propagate this shift into the input, this covers the trivial
00334   // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
00335   if (I.getOpcode() != Instruction::AShr &&
00336       CanEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this, &I)) {
00337     DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression"
00338               " to eliminate shift:\n  IN: " << *Op0 << "\n  SH: " << I <<"\n");
00339 
00340     return ReplaceInstUsesWith(I,
00341                  GetShiftedValue(Op0, COp1->getZExtValue(), isLeftShift, *this));
00342   }
00343 
00344   // See if we can simplify any instructions used by the instruction whose sole
00345   // purpose is to compute bits we don't care about.
00346   uint32_t TypeBits = Op0->getType()->getScalarSizeInBits();
00347 
00348   assert(!COp1->uge(TypeBits) &&
00349          "Shift over the type width should have been removed already");
00350 
00351   // ((X*C1) << C2) == (X * (C1 << C2))
00352   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
00353     if (BO->getOpcode() == Instruction::Mul && isLeftShift)
00354       if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
00355         return BinaryOperator::CreateMul(BO->getOperand(0),
00356                                         ConstantExpr::getShl(BOOp, Op1));
00357 
00358   // Try to fold constant and into select arguments.
00359   if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
00360     if (Instruction *R = FoldOpIntoSelect(I, SI))
00361       return R;
00362   if (isa<PHINode>(Op0))
00363     if (Instruction *NV = FoldOpIntoPhi(I))
00364       return NV;
00365 
00366   // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
00367   if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
00368     Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
00369     // If 'shift2' is an ashr, we would have to get the sign bit into a funny
00370     // place.  Don't try to do this transformation in this case.  Also, we
00371     // require that the input operand is a shift-by-constant so that we have
00372     // confidence that the shifts will get folded together.  We could do this
00373     // xform in more cases, but it is unlikely to be profitable.
00374     if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
00375         isa<ConstantInt>(TrOp->getOperand(1))) {
00376       // Okay, we'll do this xform.  Make the shift of shift.
00377       Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType());
00378       // (shift2 (shift1 & 0x00FF), c2)
00379       Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName());
00380 
00381       // For logical shifts, the truncation has the effect of making the high
00382       // part of the register be zeros.  Emulate this by inserting an AND to
00383       // clear the top bits as needed.  This 'and' will usually be zapped by
00384       // other xforms later if dead.
00385       unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
00386       unsigned DstSize = TI->getType()->getScalarSizeInBits();
00387       APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
00388 
00389       // The mask we constructed says what the trunc would do if occurring
00390       // between the shifts.  We want to know the effect *after* the second
00391       // shift.  We know that it is a logical shift by a constant, so adjust the
00392       // mask as appropriate.
00393       if (I.getOpcode() == Instruction::Shl)
00394         MaskV <<= COp1->getZExtValue();
00395       else {
00396         assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift");
00397         MaskV = MaskV.lshr(COp1->getZExtValue());
00398       }
00399 
00400       // shift1 & 0x00FF
00401       Value *And = Builder->CreateAnd(NSh,
00402                                       ConstantInt::get(I.getContext(), MaskV),
00403                                       TI->getName());
00404 
00405       // Return the value truncated to the interesting size.
00406       return new TruncInst(And, I.getType());
00407     }
00408   }
00409 
00410   if (Op0->hasOneUse()) {
00411     if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
00412       // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
00413       Value *V1, *V2;
00414       ConstantInt *CC;
00415       switch (Op0BO->getOpcode()) {
00416       default: break;
00417       case Instruction::Add:
00418       case Instruction::And:
00419       case Instruction::Or:
00420       case Instruction::Xor: {
00421         // These operators commute.
00422         // Turn (Y + (X >> C)) << C  ->  (X + (Y << C)) & (~0 << C)
00423         if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
00424             match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
00425                   m_Specific(Op1)))) {
00426           Value *YS =         // (Y << C)
00427             Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
00428           // (X + (Y << C))
00429           Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1,
00430                                           Op0BO->getOperand(1)->getName());
00431           uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
00432 
00433           APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
00434           Constant *Mask = ConstantInt::get(I.getContext(), Bits);
00435           if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
00436             Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
00437           return BinaryOperator::CreateAnd(X, Mask);
00438         }
00439 
00440         // Turn (Y + ((X >> C) & CC)) << C  ->  ((X & (CC << C)) + (Y << C))
00441         Value *Op0BOOp1 = Op0BO->getOperand(1);
00442         if (isLeftShift && Op0BOOp1->hasOneUse() &&
00443             match(Op0BOOp1,
00444                   m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
00445                         m_ConstantInt(CC)))) {
00446           Value *YS =   // (Y << C)
00447             Builder->CreateShl(Op0BO->getOperand(0), Op1,
00448                                          Op0BO->getName());
00449           // X & (CC << C)
00450           Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
00451                                          V1->getName()+".mask");
00452           return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
00453         }
00454       }
00455 
00456       // FALL THROUGH.
00457       case Instruction::Sub: {
00458         // Turn ((X >> C) + Y) << C  ->  (X + (Y << C)) & (~0 << C)
00459         if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
00460             match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
00461                   m_Specific(Op1)))) {
00462           Value *YS =  // (Y << C)
00463             Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
00464           // (X + (Y << C))
00465           Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS,
00466                                           Op0BO->getOperand(0)->getName());
00467           uint32_t Op1Val = COp1->getLimitedValue(TypeBits);
00468 
00469           APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
00470           Constant *Mask = ConstantInt::get(I.getContext(), Bits);
00471           if (VectorType *VT = dyn_cast<VectorType>(X->getType()))
00472             Mask = ConstantVector::getSplat(VT->getNumElements(), Mask);
00473           return BinaryOperator::CreateAnd(X, Mask);
00474         }
00475 
00476         // Turn (((X >> C)&CC) + Y) << C  ->  (X + (Y << C)) & (CC << C)
00477         if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
00478             match(Op0BO->getOperand(0),
00479                   m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))),
00480                         m_ConstantInt(CC))) && V2 == Op1) {
00481           Value *YS = // (Y << C)
00482             Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
00483           // X & (CC << C)
00484           Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
00485                                          V1->getName()+".mask");
00486 
00487           return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
00488         }
00489 
00490         break;
00491       }
00492       }
00493 
00494 
00495       // If the operand is a bitwise operator with a constant RHS, and the
00496       // shift is the only use, we can pull it out of the shift.
00497       if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
00498         bool isValid = true;     // Valid only for And, Or, Xor
00499         bool highBitSet = false; // Transform if high bit of constant set?
00500 
00501         switch (Op0BO->getOpcode()) {
00502         default: isValid = false; break;   // Do not perform transform!
00503         case Instruction::Add:
00504           isValid = isLeftShift;
00505           break;
00506         case Instruction::Or:
00507         case Instruction::Xor:
00508           highBitSet = false;
00509           break;
00510         case Instruction::And:
00511           highBitSet = true;
00512           break;
00513         }
00514 
00515         // If this is a signed shift right, and the high bit is modified
00516         // by the logical operation, do not perform the transformation.
00517         // The highBitSet boolean indicates the value of the high bit of
00518         // the constant which would cause it to be modified for this
00519         // operation.
00520         //
00521         if (isValid && I.getOpcode() == Instruction::AShr)
00522           isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
00523 
00524         if (isValid) {
00525           Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
00526 
00527           Value *NewShift =
00528             Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
00529           NewShift->takeName(Op0BO);
00530 
00531           return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
00532                                         NewRHS);
00533         }
00534       }
00535     }
00536   }
00537 
00538   // Find out if this is a shift of a shift by a constant.
00539   BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0);
00540   if (ShiftOp && !ShiftOp->isShift())
00541     ShiftOp = nullptr;
00542 
00543   if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) {
00544 
00545     // This is a constant shift of a constant shift. Be careful about hiding
00546     // shl instructions behind bit masks. They are used to represent multiplies
00547     // by a constant, and it is important that simple arithmetic expressions
00548     // are still recognizable by scalar evolution.
00549     //
00550     // The transforms applied to shl are very similar to the transforms applied
00551     // to mul by constant. We can be more aggressive about optimizing right
00552     // shifts.
00553     //
00554     // Combinations of right and left shifts will still be optimized in
00555     // DAGCombine where scalar evolution no longer applies.
00556 
00557     ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1));
00558     uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits);
00559     uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits);
00560     assert(ShiftAmt2 != 0 && "Should have been simplified earlier");
00561     if (ShiftAmt1 == 0) return nullptr;  // Will be simplified in the future.
00562     Value *X = ShiftOp->getOperand(0);
00563 
00564     IntegerType *Ty = cast<IntegerType>(I.getType());
00565 
00566     // Check for (X << c1) << c2  and  (X >> c1) >> c2
00567     if (I.getOpcode() == ShiftOp->getOpcode()) {
00568       uint32_t AmtSum = ShiftAmt1+ShiftAmt2;   // Fold into one big shift.
00569       // If this is oversized composite shift, then unsigned shifts get 0, ashr
00570       // saturates.
00571       if (AmtSum >= TypeBits) {
00572         if (I.getOpcode() != Instruction::AShr)
00573           return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
00574         AmtSum = TypeBits-1;  // Saturate to 31 for i32 ashr.
00575       }
00576 
00577       return BinaryOperator::Create(I.getOpcode(), X,
00578                                     ConstantInt::get(Ty, AmtSum));
00579     }
00580 
00581     if (ShiftAmt1 == ShiftAmt2) {
00582       // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
00583       if (I.getOpcode() == Instruction::LShr &&
00584           ShiftOp->getOpcode() == Instruction::Shl) {
00585         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
00586         return BinaryOperator::CreateAnd(X,
00587                                         ConstantInt::get(I.getContext(), Mask));
00588       }
00589     } else if (ShiftAmt1 < ShiftAmt2) {
00590       uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1;
00591 
00592       // (X >>?,exact C1) << C2 --> X << (C2-C1)
00593       // The inexact version is deferred to DAGCombine so we don't hide shl
00594       // behind a bit mask.
00595       if (I.getOpcode() == Instruction::Shl &&
00596           ShiftOp->getOpcode() != Instruction::Shl &&
00597           ShiftOp->isExact()) {
00598         assert(ShiftOp->getOpcode() == Instruction::LShr ||
00599                ShiftOp->getOpcode() == Instruction::AShr);
00600         ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
00601         BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
00602                                                         X, ShiftDiffCst);
00603         NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
00604         NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
00605         return NewShl;
00606       }
00607 
00608       // (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
00609       if (I.getOpcode() == Instruction::LShr &&
00610           ShiftOp->getOpcode() == Instruction::Shl) {
00611         ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
00612         // (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
00613         if (ShiftOp->hasNoUnsignedWrap()) {
00614           BinaryOperator *NewLShr = BinaryOperator::Create(Instruction::LShr,
00615                                                            X, ShiftDiffCst);
00616           NewLShr->setIsExact(I.isExact());
00617           return NewLShr;
00618         }
00619         Value *Shift = Builder->CreateLShr(X, ShiftDiffCst);
00620 
00621         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
00622         return BinaryOperator::CreateAnd(Shift,
00623                                          ConstantInt::get(I.getContext(),Mask));
00624       }
00625 
00626       // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
00627       // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
00628       if (I.getOpcode() == Instruction::AShr &&
00629           ShiftOp->getOpcode() == Instruction::Shl) {
00630         if (ShiftOp->hasNoSignedWrap()) {
00631           // (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
00632           ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
00633           BinaryOperator *NewAShr = BinaryOperator::Create(Instruction::AShr,
00634                                                            X, ShiftDiffCst);
00635           NewAShr->setIsExact(I.isExact());
00636           return NewAShr;
00637         }
00638       }
00639     } else {
00640       assert(ShiftAmt2 < ShiftAmt1);
00641       uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2;
00642 
00643       // (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
00644       // The inexact version is deferred to DAGCombine so we don't hide shl
00645       // behind a bit mask.
00646       if (I.getOpcode() == Instruction::Shl &&
00647           ShiftOp->getOpcode() != Instruction::Shl &&
00648           ShiftOp->isExact()) {
00649         ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
00650         BinaryOperator *NewShr = BinaryOperator::Create(ShiftOp->getOpcode(),
00651                                                         X, ShiftDiffCst);
00652         NewShr->setIsExact(true);
00653         return NewShr;
00654       }
00655 
00656       // (X << C1) >>u C2  --> X << (C1-C2) & (-1 >> C2)
00657       if (I.getOpcode() == Instruction::LShr &&
00658           ShiftOp->getOpcode() == Instruction::Shl) {
00659         ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
00660         if (ShiftOp->hasNoUnsignedWrap()) {
00661           // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2)
00662           BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
00663                                                           X, ShiftDiffCst);
00664           NewShl->setHasNoUnsignedWrap(true);
00665           return NewShl;
00666         }
00667         Value *Shift = Builder->CreateShl(X, ShiftDiffCst);
00668 
00669         APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
00670         return BinaryOperator::CreateAnd(Shift,
00671                                          ConstantInt::get(I.getContext(),Mask));
00672       }
00673 
00674       // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However,
00675       // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
00676       if (I.getOpcode() == Instruction::AShr &&
00677           ShiftOp->getOpcode() == Instruction::Shl) {
00678         if (ShiftOp->hasNoSignedWrap()) {
00679           // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2)
00680           ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff);
00681           BinaryOperator *NewShl = BinaryOperator::Create(Instruction::Shl,
00682                                                           X, ShiftDiffCst);
00683           NewShl->setHasNoSignedWrap(true);
00684           return NewShl;
00685         }
00686       }
00687     }
00688   }
00689   return nullptr;
00690 }
00691 
00692 Instruction *InstCombiner::visitShl(BinaryOperator &I) {
00693   if (Value *V = SimplifyVectorOp(I))
00694     return ReplaceInstUsesWith(I, V);
00695 
00696   if (Value *V =
00697           SimplifyShlInst(I.getOperand(0), I.getOperand(1), I.hasNoSignedWrap(),
00698                           I.hasNoUnsignedWrap(), DL, TLI, DT, AC))
00699     return ReplaceInstUsesWith(I, V);
00700 
00701   if (Instruction *V = commonShiftTransforms(I))
00702     return V;
00703 
00704   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(I.getOperand(1))) {
00705     unsigned ShAmt = Op1C->getZExtValue();
00706 
00707     // If the shifted-out value is known-zero, then this is a NUW shift.
00708     if (!I.hasNoUnsignedWrap() &&
00709         MaskedValueIsZero(I.getOperand(0),
00710                           APInt::getHighBitsSet(Op1C->getBitWidth(), ShAmt),
00711                           0, &I)) {
00712           I.setHasNoUnsignedWrap();
00713           return &I;
00714         }
00715 
00716     // If the shifted out value is all signbits, this is a NSW shift.
00717     if (!I.hasNoSignedWrap() &&
00718         ComputeNumSignBits(I.getOperand(0), 0, &I) > ShAmt) {
00719       I.setHasNoSignedWrap();
00720       return &I;
00721     }
00722   }
00723 
00724   // (C1 << A) << C2 -> (C1 << C2) << A
00725   Constant *C1, *C2;
00726   Value *A;
00727   if (match(I.getOperand(0), m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) &&
00728       match(I.getOperand(1), m_Constant(C2)))
00729     return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A);
00730 
00731   return nullptr;
00732 }
00733 
00734 Instruction *InstCombiner::visitLShr(BinaryOperator &I) {
00735   if (Value *V = SimplifyVectorOp(I))
00736     return ReplaceInstUsesWith(I, V);
00737 
00738   if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
00739                                   DL, TLI, DT, AC))
00740     return ReplaceInstUsesWith(I, V);
00741 
00742   if (Instruction *R = commonShiftTransforms(I))
00743     return R;
00744 
00745   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00746 
00747   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
00748     unsigned ShAmt = Op1C->getZExtValue();
00749 
00750     if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
00751       unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
00752       // ctlz.i32(x)>>5  --> zext(x == 0)
00753       // cttz.i32(x)>>5  --> zext(x == 0)
00754       // ctpop.i32(x)>>5 --> zext(x == -1)
00755       if ((II->getIntrinsicID() == Intrinsic::ctlz ||
00756            II->getIntrinsicID() == Intrinsic::cttz ||
00757            II->getIntrinsicID() == Intrinsic::ctpop) &&
00758           isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt) {
00759         bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
00760         Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
00761         Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS);
00762         return new ZExtInst(Cmp, II->getType());
00763       }
00764     }
00765 
00766     // If the shifted-out value is known-zero, then this is an exact shift.
00767     if (!I.isExact() &&
00768         MaskedValueIsZero(Op0, APInt::getLowBitsSet(Op1C->getBitWidth(), ShAmt),
00769                           0, &I)){
00770       I.setIsExact();
00771       return &I;
00772     }
00773   }
00774 
00775   return nullptr;
00776 }
00777 
00778 Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
00779   if (Value *V = SimplifyVectorOp(I))
00780     return ReplaceInstUsesWith(I, V);
00781 
00782   if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
00783                                   DL, TLI, DT, AC))
00784     return ReplaceInstUsesWith(I, V);
00785 
00786   if (Instruction *R = commonShiftTransforms(I))
00787     return R;
00788 
00789   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
00790 
00791   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
00792     unsigned ShAmt = Op1C->getZExtValue();
00793 
00794     // If the input is a SHL by the same constant (ashr (shl X, C), C), then we
00795     // have a sign-extend idiom.
00796     Value *X;
00797     if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1)))) {
00798       // If the input is an extension from the shifted amount value, e.g.
00799       //   %x = zext i8 %A to i32
00800       //   %y = shl i32 %x, 24
00801       //   %z = ashr %y, 24
00802       // then turn this into "z = sext i8 A to i32".
00803       if (ZExtInst *ZI = dyn_cast<ZExtInst>(X)) {
00804         uint32_t SrcBits = ZI->getOperand(0)->getType()->getScalarSizeInBits();
00805         uint32_t DestBits = ZI->getType()->getScalarSizeInBits();
00806         if (Op1C->getZExtValue() == DestBits-SrcBits)
00807           return new SExtInst(ZI->getOperand(0), ZI->getType());
00808       }
00809     }
00810 
00811     // If the shifted-out value is known-zero, then this is an exact shift.
00812     if (!I.isExact() &&
00813         MaskedValueIsZero(Op0,APInt::getLowBitsSet(Op1C->getBitWidth(),ShAmt),
00814                           0, &I)){
00815       I.setIsExact();
00816       return &I;
00817     }
00818   }
00819 
00820   // See if we can turn a signed shr into an unsigned shr.
00821   if (MaskedValueIsZero(Op0,
00822                         APInt::getSignBit(I.getType()->getScalarSizeInBits()),
00823                         0, &I))
00824     return BinaryOperator::CreateLShr(Op0, Op1);
00825 
00826   return nullptr;
00827 }