Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InstCombine] Resolve TODO: Remove one-time check if other logic operand (Y) is constant #77973

Merged
merged 2 commits into from
Jan 23, 2024

Conversation

AtariDreams
Copy link
Contributor

@AtariDreams AtariDreams commented Jan 12, 2024

By using match(W, m_ImmConstant()), we do not need to worry about one-time use anymore.

@AtariDreams AtariDreams changed the title [Transforms] Remove one-time check if other logic operand (Y) is constant. [Transforms] Remove one-time check if other logic operand (Y) is constant Jan 12, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 12, 2024

@llvm/pr-subscribers-llvm-transforms

Author: AtariDreams (AtariDreams)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/77973.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp (+8-4)
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

@nikic nikic changed the title [Transforms] Remove one-time check if other logic operand (Y) is constant [InstCombine] Remove one-time check if other logic operand (Y) is constant Jan 12, 2024
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests?

@AtariDreams AtariDreams changed the title [InstCombine] Remove one-time check if other logic operand (Y) is constant [InstCombine] Resolve FIXME: Remove one-time check if other logic operand (Y) is constant Jan 13, 2024
@AtariDreams AtariDreams changed the title [InstCombine] Resolve FIXME: Remove one-time check if other logic operand (Y) is constant [InstCombine] Resolve TODO: Remove one-time check if other logic operand (Y) is constant Jan 13, 2024
@AtariDreams
Copy link
Contributor Author

Tests?

Sure. I added a whole bunch.

@AtariDreams
Copy link
Contributor Author

@nikic Alright. I think I got this right. Though I am concerned about the shl undef tests that seem to fold things into poison values. I do not know if this is what we want.

@AtariDreams
Copy link
Contributor Author

@nikic Can I use the other tests that I removed but is affected by the transform then?

@nikic
Copy link
Contributor

nikic commented Jan 21, 2024

You need the simplest possible test (no vectors...) that does not fold before your changes and folds after your changes. Please find this test on your own.

@AtariDreams
Copy link
Contributor Author

You need the simplest possible test (no vectors...) that does not fold before your changes and folds after your changes. Please find this test on your own.

Did. @nikic Is this good?

@@ -433,9 +433,9 @@ define i8 @shl_sub(i8 %x, i8 %y) {
define i8 @shl_sub_multiuse(i8 %x) {
; CHECK-LABEL: @shl_sub_multiuse(
; CHECK-NEXT: [[SH0:%.*]] = shl i8 [[X:%.*]], 3
; CHECK-NEXT: [[R:%.*]] = sub i8 [[SH0]], 42 ; constant operand on the 'sub'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, did update_llc_test_checks.py really add that comment?...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It removed the comment, not added it @nikic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It added the comment as it was in the original IR, but when it was transformed llc removed it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but why was it there in the first place. The previous commit added it, but that suggests the first commit is wrong.

And please do not resolve conversations off the bat like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I put it there originally.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well don't then?

And for the second time, stop resolving conversations like that.

@AtariDreams
Copy link
Contributor Author

Alright @nikic Can we please merge this?

@AtariDreams
Copy link
Contributor Author

@nikic Done!

…tant

By using match(W, m_ImmConstant()), we do not need to worry about one-time use anymore.
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic nikic merged commit 96adf69 into llvm:main Jan 23, 2024
4 checks passed
@AtariDreams AtariDreams deleted the puts branch January 23, 2024 13:05
@LLVMCoCCommittee
Copy link

@AtariDreams We have reached out to request your feedback via the email attached to your GitHub account. Please respond to conduct@llvm.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants