[llvm] 3fcb00d - [InstCombine] restrict shift-trunc-shift fold to opposite direction shifts

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 30 12:06:22 PDT 2021


Author: Sanjay Patel
Date: 2021-09-30T15:06:13-04:00
New Revision: 3fcb00df5dbfa63844db148bd6877d865a930400

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

LOG: [InstCombine] restrict shift-trunc-shift fold to opposite direction shifts

This is NFCI because the pattern with 2 left-shifts should get
folded independently by smaller folds.

The motivation is to refine this block to avoid infinite loops
seen with D110170.

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 cf746a0323f52..df7fb01ca7fad 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -853,15 +853,13 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
         return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
     }
 
-    // Fold shl(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
-    // 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 non-poison shift-by-constant so that
+    // Fold shl(trunc(shr(x,c1)),c2) -> trunc(and(shl(shr(x,c1),c2),c2'))
+    // Require that the input operand is a non-poison shift-by-constant so that
     // we have confidence that the shifts will get folded together.
     Instruction *TrOp;
     const APInt *TrShiftAmt;
     if (match(Op0, m_OneUse(m_Trunc(m_Instruction(TrOp)))) &&
-        match(TrOp, m_OneUse(m_Shift(m_Value(), m_APInt(TrShiftAmt)))) &&
+        match(TrOp, m_OneUse(m_Shr(m_Value(), m_APInt(TrShiftAmt)))) &&
         TrShiftAmt->ult(TrOp->getType()->getScalarSizeInBits())) {
       Type *SrcTy = TrOp->getType();
 


        


More information about the llvm-commits mailing list