Skip to content

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

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

Merged
merged 2 commits into from
Jan 23, 2024

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Jan 12, 2024

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

@AZero13 AZero13 requested a review from nikic as a code owner January 12, 2024 20:17
@AZero13 AZero13 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
Member

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?

@AZero13 AZero13 force-pushed the puts branch 3 times, most recently from cfc03de to 8f9d367 Compare January 13, 2024 03:13
@AZero13 AZero13 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
@AZero13 AZero13 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
@AZero13 AZero13 force-pushed the puts branch 2 times, most recently from 67361d8 to a9f84d5 Compare January 13, 2024 21:53
@AZero13
Copy link
Contributor Author

AZero13 commented Jan 13, 2024

Tests?

Sure. I added a whole bunch.

@AZero13 AZero13 requested a review from nikic January 13, 2024 21:54
@AZero13 AZero13 force-pushed the puts branch 4 times, most recently from 1726104 to 836c67d Compare January 15, 2024 00:30
@AZero13 AZero13 force-pushed the puts branch 2 times, most recently from 15bf60c to ea3ee13 Compare January 16, 2024 03:14
@AZero13 AZero13 force-pushed the puts branch 2 times, most recently from f68212c to 77f7092 Compare January 16, 2024 21:44
@AZero13
Copy link
Contributor Author

AZero13 commented Jan 16, 2024

@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.

@AZero13 AZero13 requested a review from nikic January 21, 2024 15:41
@AZero13
Copy link
Contributor Author

AZero13 commented Jan 21, 2024

@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.

@AZero13
Copy link
Contributor Author

AZero13 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.

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

@AZero13 AZero13 Jan 21, 2024

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.

@AZero13
Copy link
Contributor Author

AZero13 commented Jan 21, 2024

Alright @nikic Can we please merge this?

@AZero13 AZero13 force-pushed the puts branch 2 times, most recently from 90def1e to b235e03 Compare January 22, 2024 18:56
@AZero13 AZero13 requested a review from nikic January 22, 2024 18:56
@AZero13
Copy link
Contributor Author

AZero13 commented Jan 22, 2024

@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
@AZero13 AZero13 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 [email protected]

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.

6 participants