[llvm] c85f450 - [InstCombine] refactor to reduce indent; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 6 08:29:36 PDT 2021


Author: Sanjay Patel
Date: 2021-09-06T11:13:39-04:00
New Revision: c85f450619f74c9befc519b748d0cf0649f07518

URL: https://github.com/llvm/llvm-project/commit/c85f450619f74c9befc519b748d0cf0649f07518
DIFF: https://github.com/llvm/llvm-project/commit/c85f450619f74c9befc519b748d0cf0649f07518.diff

LOG: [InstCombine] refactor to reduce indent; NFC

This transform should be updated to use better
variable names and code comments. It could
also create the shift-of-shift directly instead
of relying on another combine for that.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 544115c57a19..9d5e5b7dd34e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -694,41 +694,39 @@ Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *Op1,
     return nullptr;
 
   // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
-  if (auto *TI = dyn_cast<TruncInst>(Op0)) {
-    // If 'shift2' is an ashr, we would have to get the sign bit into a funny
-    // place.  Don't try to do this transformation in this case.  Also, we
-    // require that the input operand is a shift-by-constant so that we have
-    // confidence that the shifts will get folded together.  We could do this
-    // xform in more cases, but it is unlikely to be profitable.
-    const APInt *TrShiftAmt;
-    if (I.isLogicalShift() &&
-        match(TI->getOperand(0), m_Shift(m_Value(), m_APInt(TrShiftAmt)))) {
-      auto *TrOp = cast<Instruction>(TI->getOperand(0));
-      Type *SrcTy = TrOp->getType();
-
-      // Okay, we'll do this xform.  Make the shift of shift.
-      Constant *ShAmt = ConstantExpr::getZExt(Op1, SrcTy);
-      // (shift2 (shift1 & 0x00FF), c2)
-      Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
-
-      // For logical shifts, the truncation has the effect of making the high
-      // part of the register be zeros.  Emulate this by inserting an AND to
-      // clear the top bits as needed.  This 'and' will usually be zapped by
-      // other xforms later if dead.
-      unsigned SrcSize = SrcTy->getScalarSizeInBits();
-      Constant *MaskV =
-          ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcSize, TypeBits));
-
-      // The mask we constructed says what the trunc would do if occurring
-      // between the shifts.  We want to know the effect *after* the second
-      // shift.  We know that it is a logical shift by a constant, so adjust the
-      // mask as appropriate.
-      MaskV = ConstantExpr::get(I.getOpcode(), MaskV, ShAmt);
-      // shift1 & 0x00FF
-      Value *And = Builder.CreateAnd(NSh, MaskV, TI->getName());
-      // Return the value truncated to the interesting size.
-      return new TruncInst(And, Ty);
-    }
+  // If 'shift2' is an ashr, we would have to get the sign bit into a funny
+  // place.  Don't try to do this transformation in this case.  Also, we
+  // require that the input operand is a shift-by-constant so that we have
+  // confidence that the shifts will get folded together.  We could do this
+  // xform in more cases, but it is unlikely to be profitable.
+  Instruction *TrOp;
+  const APInt *TrShiftAmt;
+  if (I.isLogicalShift() && match(Op0, m_Trunc(m_Instruction(TrOp))) &&
+      match(TrOp, m_Shift(m_Value(), m_APInt(TrShiftAmt)))) {
+    Type *SrcTy = TrOp->getType();
+
+    // Okay, we'll do this xform.  Make the shift of shift.
+    Constant *ShAmt = ConstantExpr::getZExt(Op1, SrcTy);
+    // (shift2 (shift1 & 0x00FF), c2)
+    Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
+
+    // For logical shifts, the truncation has the effect of making the high
+    // part of the register be zeros.  Emulate this by inserting an AND to
+    // clear the top bits as needed.  This 'and' will usually be zapped by
+    // other xforms later if dead.
+    unsigned SrcSize = SrcTy->getScalarSizeInBits();
+    Constant *MaskV =
+        ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcSize, TypeBits));
+
+    // The mask we constructed says what the trunc would do if occurring
+    // between the shifts.  We want to know the effect *after* the second
+    // shift.  We know that it is a logical shift by a constant, so adjust the
+    // mask as appropriate.
+    MaskV = ConstantExpr::get(I.getOpcode(), MaskV, ShAmt);
+    // shift1 & 0x00FF
+    Value *And = Builder.CreateAnd(NSh, MaskV, Op0->getName());
+    // Return the value truncated to the interesting size.
+    return new TruncInst(And, Ty);
   }
 
   if (auto *Op0BO = dyn_cast<BinaryOperator>(Op0)) {


        


More information about the llvm-commits mailing list