Skip to content

[DAG] Improved handling of ISD::ROTL and ISD::ROTR in isKnownToBeAPowerOfTwo - #182744

Merged
RKSimon merged 13 commits into
llvm:mainfrom
mirimmad:main
Mar 4, 2026
Merged

RKSimon merged 13 commits into
llvm:mainfrom
mirimmad:main

Conversation

@mirimmad

Copy link
Copy Markdown
Contributor

Fixes #181642

@github-actions

Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Feb 22, 2026
@llvmbot

llvmbot commented Feb 22, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-selectiondag

Author: Mir Immad (mirimmad)

Changes

Fixes #181642


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+1-2)
  • (modified) llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp (+49)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 581553d41cb6d..971c5f5674ecb 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4733,8 +4733,7 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
 
   case ISD::ROTL:
   case ISD::ROTR:
-    return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
-                                  Depth + 1);
+    return isKnownToBeAPowerOfTwo(Val.getOperand(0), false, Depth + 1);
 
   case ISD::SMIN:
   case ISD::SMAX:
diff --git a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
index b2b8dfb0c21fc..33a3540f6b562 100644
--- a/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
+++ b/llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp
@@ -1119,6 +1119,55 @@ TEST_F(AArch64SelectionDAGTest,
   EXPECT_EQ(SplatIdx, 0);
 }
 
+TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTL) {
+  SDLoc Loc;
+  auto IntVT = EVT::getIntegerVT(Context, 32);
+
+  // Power-of-two values
+  auto Pow2_4 = DAG->getConstant(4, Loc, IntVT);
+  auto Pow2_8 = DAG->getConstant(8, Loc, IntVT);
+
+  // Non-power-of-two values
+  auto NonPow2_5 = DAG->getConstant(5, Loc, IntVT);
+  auto NonPow2_0 = DAG->getConstant(0, Loc, IntVT);
+
+  auto RotAmount = DAG->getConstant(3, Loc, IntVT);
+
+  auto RotlPow2_4 = DAG->getNode(ISD::ROTL, Loc, IntVT, Pow2_4, RotAmount);
+  auto RotlPow2_8 = DAG->getNode(ISD::ROTL, Loc, IntVT, Pow2_8, RotAmount);
+  auto RotlNonPow2_5 =
+      DAG->getNode(ISD::ROTL, Loc, IntVT, NonPow2_5, RotAmount);
+  auto RotlZero = DAG->getNode(ISD::ROTL, Loc, IntVT, NonPow2_0, RotAmount);
+
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_4));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlPow2_8));
+
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlNonPow2_5));
+
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotlZero));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotlZero, /*OrZero=*/true));
+}
+
+TEST_F(AArch64SelectionDAGTest, KnownToBeAPowerOfTwo_ROTR) {
+  SDLoc Loc;
+  auto IntVT = EVT::getIntegerVT(Context, 32);
+
+  auto Pow2_16 = DAG->getConstant(16, Loc, IntVT);
+  auto NonPow2_6 = DAG->getConstant(6, Loc, IntVT);
+  auto Zero = DAG->getConstant(0, Loc, IntVT);
+
+  auto RotAmount = DAG->getConstant(5, Loc, IntVT);
+
+  auto RotrPow2 = DAG->getNode(ISD::ROTR, Loc, IntVT, Pow2_16, RotAmount);
+  auto RotrNonPow2 = DAG->getNode(ISD::ROTR, Loc, IntVT, NonPow2_6, RotAmount);
+  auto RotrZero = DAG->getNode(ISD::ROTR, Loc, IntVT, Zero, RotAmount);
+
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrPow2));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrNonPow2));
+  EXPECT_FALSE(DAG->isKnownToBeAPowerOfTwo(RotrZero));
+  EXPECT_TRUE(DAG->isKnownToBeAPowerOfTwo(RotrZero, /*OrZero=*/true));
+}
+
 TEST_F(AArch64SelectionDAGTest, getRepeatedSequence_Patterns) {
   TargetLowering TL(*TM, *STI);
 

@RKSimon
RKSimon self-requested a review February 22, 2026 15:48
Comment thread llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Outdated
@mirimmad

Copy link
Copy Markdown
Contributor Author

@RKSimon Made the changes, thanks.

Comment thread llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp Outdated
Comment thread llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp Outdated
@mirimmad

mirimmad commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

@arsenm There are already tests for pow2_rotr and pow2_rotl in known-pow2.ll. I have added tests for the vector case.

@mirimmad
mirimmad requested review from RKSimon and arsenm February 24, 2026 12:13
Comment thread llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp Outdated
@github-actions

github-actions Bot commented Feb 24, 2026

Copy link
Copy Markdown

✅ With the latest revision this PR passed the undef deprecator.

Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
@@ -11,6 +11,8 @@ declare i32 @llvm.smin.i32(i32, i32)
declare i32 @llvm.smax.i32(i32, i32)
declare i32 @llvm.fshl.i32(i32, i32, i32)
declare i32 @llvm.fshr.i32(i32, i32, i32)
declare <2 x i32> @llvm.fshl.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)
declare <2 x i32> @llvm.fshr.v2i32(<2 x i32>, <2 x i32>, <2 x i32>)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

don't bother adding these - we don't need to declare anymore

Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
@@ -889,3 +891,32 @@ define i1 @pow2_and_i128(i128 %num, i128 %shift) {
%bool = icmp eq i128 %bit, 0
ret i1 %bool
}

define <2 x i32> @pow2_rotl_vec() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Use the update_llc_test_checks script

Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please fix the bad merge (test files?)

@mirimmad
mirimmad requested a review from RKSimon March 2, 2026 10:07
@mirimmad
mirimmad force-pushed the main branch 2 times, most recently from 5b0ac03 to 97e2eb2 Compare March 2, 2026 10:57
Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
Comment thread llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp Outdated
@mirimmad
mirimmad requested a review from RKSimon March 3, 2026 06:54
Comment thread llvm/test/CodeGen/X86/known-pow2.ll
Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
Comment thread llvm/test/CodeGen/X86/known-pow2.ll
Comment thread llvm/test/CodeGen/X86/known-pow2.ll Outdated
@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown

🪟 Windows x64 Test Results

  • 131798 tests passed
  • 2974 tests skipped

✅ The build succeeded and all tests passed.

@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown

🐧 Linux x64 Test Results

  • 191565 tests passed
  • 4986 tests skipped

✅ The build succeeded and all tests passed.

@mirimmad
mirimmad requested a review from RKSimon March 4, 2026 05:49

@RKSimon RKSimon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - cheers

@RKSimon
RKSimon enabled auto-merge (squash) March 4, 2026 16:08
@RKSimon
RKSimon merged commit b0b5834 into llvm:main Mar 4, 2026
10 checks passed
@github-actions

github-actions Bot commented Mar 4, 2026

Copy link
Copy Markdown

@mirimmad Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

sujianIBM pushed a commit to sujianIBM/llvm-project that referenced this pull request Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DAG] isKnownToBeAPowerOfTwo - add DemandedElts + OrZero handling to ISD::ROTL/ROTR cases

6 participants