[llvm] [Transforms] Remove one-time check if other logic operand (Y) is constant. (PR #77973)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 12 12:17:34 PST 2024


https://github.com/AtariDreams created https://github.com/llvm/llvm-project/pull/77973

By using isa<Constant>(W), we do not need to worry about one-time use anymore.

>From 53cfb91e4f03fec12cb365229fc737896d827c17 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Fri, 12 Jan 2024 15:15:27 -0500
Subject: [PATCH] Remove one-time check if other logic operand (Y) is constant.

By using isa<Constant>(W), we do not need to worry about one-time use anymore.
---
 .../lib/Transforms/InstCombine/InstCombineShifts.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index b7958978c450c9..8614aad529eb4a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -368,10 +368,14 @@ static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
 
   // Find a matching one-use shift by constant. The fold is not valid if the sum
   // of the shift values equals or exceeds bitwidth.
-  // TODO: Remove the one-use check if the other logic operand (Y) is constant.
   Value *X, *Y;
-  auto matchFirstShift = [&](Value *V) {
+  auto matchFirstShift = [&](Value *V, Value *W) {
     APInt Threshold(Ty->getScalarSizeInBits(), Ty->getScalarSizeInBits());
+    if (isa<Constant>(W)) {
+      return match(V, (m_BinOp(ShiftOpcode, m_Value(X), m_Constant(C0)))) &&
+             match(ConstantExpr::getAdd(C0, C1),
+                   m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
+    }
     return match(V,
                  m_OneUse(m_BinOp(ShiftOpcode, m_Value(X), m_Constant(C0)))) &&
            match(ConstantExpr::getAdd(C0, C1),
@@ -382,9 +386,9 @@ static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I,
   // is not so we cannot reoder if we match operand(1) and need to keep the
   // operands in their original positions.
   bool FirstShiftIsOp1 = false;
-  if (matchFirstShift(BinInst->getOperand(0)))
+  if (matchFirstShift(BinInst->getOperand(0), BinInst->getOperand(1)))
     Y = BinInst->getOperand(1);
-  else if (matchFirstShift(BinInst->getOperand(1))) {
+  else if (matchFirstShift(BinInst->getOperand(1), BinInst->getOperand(0))) {
     Y = BinInst->getOperand(0);
     FirstShiftIsOp1 = BinInst->getOpcode() == Instruction::Sub;
   } else



More information about the llvm-commits mailing list