]> granicus.if.org Git - llvm/commitdiff
[NFC][InstCombine] dropRedundantMaskingOfLeftShiftInput(): some NFC diff shaving
authorRoman Lebedev <lebedev.ri@gmail.com>
Tue, 17 Sep 2019 19:32:26 +0000 (19:32 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Tue, 17 Sep 2019 19:32:26 +0000 (19:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372171 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineShifts.cpp

index b451beebd0f35d208f73dab98c944425bead3792..cda83544a4d1747695d3ab6382f42d2a7550ab98 100644 (file)
@@ -131,7 +131,8 @@ reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0,
 //   c,d,e,f) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
 static Instruction *
 dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
-                                     const SimplifyQuery &SQ) {
+                                     const SimplifyQuery &SQ,
+                                     InstCombiner::BuilderTy &Builder) {
   assert(OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&
          "The input must be 'shl'!");
 
@@ -153,17 +154,19 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
   Value *X;
   if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) {
     // Can we simplify (MaskShAmt+ShiftShAmt) ?
-    Value *SumOfShAmts =
+    auto *SumOfShAmts = dyn_cast_or_null<Constant>(
         SimplifyAddInst(MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false,
-                        SQ.getWithInstruction(OuterShift));
+                        SQ.getWithInstruction(OuterShift)));
     if (!SumOfShAmts)
       return nullptr; // Did not simplify.
-    // Is the total shift amount *not* smaller than the bit width?
-    // FIXME: could also rely on ConstantRange.
-    unsigned BitWidth = X->getType()->getScalarSizeInBits();
+    Type *Ty = X->getType();
+    unsigned BitWidth = Ty->getScalarSizeInBits();
+    // In this pattern SumOfShAmts correlates with the number of low bits that
+    // shall remain in the root value (OuterShift). If SumOfShAmts is less than
+    // bitwidth, we'll need to also produce a mask to keep SumOfShAmts low bits.
     if (!match(SumOfShAmts, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_UGE,
                                                APInt(BitWidth, BitWidth))))
-      return nullptr;
+      return nullptr; // FIXME.
     // All good, we can do this fold.
   } else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
              match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
@@ -751,7 +754,7 @@ Instruction *InstCombiner::visitShl(BinaryOperator &I) {
   if (Instruction *V = commonShiftTransforms(I))
     return V;
 
-  if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, SQ))
+  if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, SQ, Builder))
     return V;
 
   Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);