Skip to content

[GlobalISel][KnownBits] Use KnownBits::urem for G_UREM#193455

Merged
arsenm merged 11 commits into
llvm:mainfrom
tejasgaikwad04:fix-knownbits-urem
Apr 30, 2026
Merged

[GlobalISel][KnownBits] Use KnownBits::urem for G_UREM#193455
arsenm merged 11 commits into
llvm:mainfrom
tejasgaikwad04:fix-knownbits-urem

Conversation

@tejasgaikwad04

Copy link
Copy Markdown
Contributor

This updates the implementation of G_UREM in GlobalISel to use KnownBits::urem instead of reimplementing the logic.
Supersedes #189087.

@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 commented Apr 22, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-backend-aarch64

Author: Tejas Gaikwad (tejasgaikwad04)

Changes

This updates the implementation of G_UREM in GlobalISel to use KnownBits::urem instead of reimplementing the logic.
Supersedes #189087.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp (+16)
  • (added) llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir (+20)
diff --git a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
index f0b455fbdc7d0..decca334cf2ee 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
@@ -285,6 +285,22 @@ void GISelValueTracking::computeKnownBitsImpl(Register R, KnownBits &Known,
     }
     break;
   }
+
+  case TargetOpcode::G_UREM: {
+    KnownBits LHSKnown(Known.getBitWidth());
+    KnownBits RHSKnown(Known.getBitWidth());
+
+    computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
+                         Depth + 1);
+    computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
+                         Depth + 1);
+
+    KnownBits Res = KnownBits::urem(LHSKnown, RHSKnown);
+
+    Known = Res;
+    break;
+  }
+
   case TargetOpcode::G_CONSTANT: {
     Known = KnownBits::makeConstant(MI.getOperand(1).getCImm()->getValue());
     break;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
new file mode 100644
index 0000000000000..b5c70f46a35cc
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
@@ -0,0 +1,20 @@
+# NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple aarch64 -passes="print<gisel-value-tracking>" %s -filetype=null 2>&1 | FileCheck %s
+
+name:            urem_pow2
+
+body:             |
+  bb.1:
+
+  ; CHECK-LABEL: name: @urem_pow2
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:00000000000000000000000000001000 SignBits:28
+  ; CHECK-NEXT: %2:_ KnownBits:00000000000000000000000000000??? SignBits:29
+
+    liveins: $w0
+
+    %0:_(s32) = COPY $w0
+    %1:_(s32) = G_CONSTANT i32 8
+    %2:_(s32) = G_UREM %0, %1
+    $w0 = COPY %2(s32)
+    RET_ReallyLR implicit $w0

@llvmbot

llvmbot commented Apr 22, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-llvm-globalisel

Author: Tejas Gaikwad (tejasgaikwad04)

Changes

This updates the implementation of G_UREM in GlobalISel to use KnownBits::urem instead of reimplementing the logic.
Supersedes #189087.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp (+16)
  • (added) llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir (+20)
diff --git a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
index f0b455fbdc7d0..decca334cf2ee 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp
@@ -285,6 +285,22 @@ void GISelValueTracking::computeKnownBitsImpl(Register R, KnownBits &Known,
     }
     break;
   }
+
+  case TargetOpcode::G_UREM: {
+    KnownBits LHSKnown(Known.getBitWidth());
+    KnownBits RHSKnown(Known.getBitWidth());
+
+    computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
+                         Depth + 1);
+    computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
+                         Depth + 1);
+
+    KnownBits Res = KnownBits::urem(LHSKnown, RHSKnown);
+
+    Known = Res;
+    break;
+  }
+
   case TargetOpcode::G_CONSTANT: {
     Known = KnownBits::makeConstant(MI.getOperand(1).getCImm()->getValue());
     break;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
new file mode 100644
index 0000000000000..b5c70f46a35cc
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
@@ -0,0 +1,20 @@
+# NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple aarch64 -passes="print<gisel-value-tracking>" %s -filetype=null 2>&1 | FileCheck %s
+
+name:            urem_pow2
+
+body:             |
+  bb.1:
+
+  ; CHECK-LABEL: name: @urem_pow2
+  ; CHECK-NEXT: %0:_ KnownBits:???????????????????????????????? SignBits:1
+  ; CHECK-NEXT: %1:_ KnownBits:00000000000000000000000000001000 SignBits:28
+  ; CHECK-NEXT: %2:_ KnownBits:00000000000000000000000000000??? SignBits:29
+
+    liveins: $w0
+
+    %0:_(s32) = COPY $w0
+    %1:_(s32) = G_CONSTANT i32 8
+    %2:_(s32) = G_UREM %0, %1
+    $w0 = COPY %2(s32)
+    RET_ReallyLR implicit $w0

Comment thread llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp Outdated
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
Comment thread llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp Outdated
Comment thread llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp Outdated
Comment thread llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp Outdated
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
Comment thread llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
Comment thread llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
Comment thread llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir
body: |
bb.1:

; CHECK-LABEL: name: @urem_pow2

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.

Test some other cases too, like non-power-of-2 and G_IMPLICIT_DEF

tejasgaikwad04 and others added 3 commits April 28, 2026 00:08
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
@tejasgaikwad04

Copy link
Copy Markdown
Contributor Author

@arsenm, I have added additional testcases for testing the UREM feature.

Comment thread llvm/test/CodeGen/AArch64/GlobalISel/knownbits-urem.mir Outdated
@arsenm arsenm enabled auto-merge (squash) April 30, 2026 11:56
@arsenm arsenm merged commit 4975ad9 into llvm:main Apr 30, 2026
9 of 10 checks passed
@github-actions

Copy link
Copy Markdown

@tejasgaikwad04 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!

enferex pushed a commit to enferex/llvm-project that referenced this pull request May 5, 2026
This updates the implementation of G_UREM in GlobalISel to use
KnownBits::urem instead of reimplementing the logic.
Supersedes llvm#189087.
moar55 pushed a commit to moar55/llvm-project that referenced this pull request May 12, 2026
This updates the implementation of G_UREM in GlobalISel to use
KnownBits::urem instead of reimplementing the logic.
Supersedes llvm#189087.
arsenm pushed a commit that referenced this pull request May 21, 2026
This PR also move case statement for or `G_UREM `that is being
introduced by #193455 So that
`G_[U|S][DIV|REM] ` being grouped together, just like in
`SelectionDAG.cpp`

Related: #150515

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
cpullvm-upstream-sync Bot pushed a commit to navaneethshan/cpullvm-toolchain-1 that referenced this pull request May 21, 2026
This PR also move case statement for or `G_UREM `that is being
introduced by llvm/llvm-project#193455 So that
`G_[U|S][DIV|REM] ` being grouped together, just like in
`SelectionDAG.cpp`

Related: llvm/llvm-project#150515

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
llvm-sync Bot pushed a commit to arm/arm-toolchain that referenced this pull request May 21, 2026
This PR also move case statement for or `G_UREM `that is being
introduced by llvm/llvm-project#193455 So that
`G_[U|S][DIV|REM] ` being grouped together, just like in
`SelectionDAG.cpp`

Related: llvm/llvm-project#150515

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
llvm-upstreamsync Bot pushed a commit to qualcomm/cpullvm-toolchain that referenced this pull request May 21, 2026
This PR also move case statement for or `G_UREM `that is being
introduced by llvm/llvm-project#193455 So that
`G_[U|S][DIV|REM] ` being grouped together, just like in
`SelectionDAG.cpp`

Related: llvm/llvm-project#150515

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
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.

3 participants