[llvm] [X86] Remove redundant TEST after shifts when count is non-zero (PR #169069)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 21 10:04:42 PST 2025


================
@@ -3698,11 +3699,30 @@ void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
     if (const PossiblyExactOperator *ExactOp =
             dyn_cast<const PossiblyExactOperator>(&I))
       exact = ExactOp->isExact();
+
+    const Value *ShiftAmt = I.getOperand(1);
+
+    // Look through zext as computeConstantRange does not do this.
+    const Value *InnerVal = ShiftAmt;
+    if (auto *ZExt = dyn_cast<ZExtInst>(ShiftAmt)) {
+      InnerVal = ZExt->getOperand(0);
+    }
+
+    // Get the constant range and check it excludes 0
+    ConstantRange CR = llvm::computeConstantRange(
+        InnerVal, true, true, AC, dyn_cast<Instruction>(&I), nullptr);
----------------
arsenm wrote:

I don't really like when the DAG builder tries to be clever about doing optimizations instead of straightforwardly translating what was in the original IR. Can't you perform this check directly on the DAG operations? The known bits analysis is always going to be worse but it should be enough for your simple examples? 

https://github.com/llvm/llvm-project/pull/169069


More information about the llvm-commits mailing list