Skip to content

Conversation

badumbatish
Copy link
Contributor

Fixes #149285.

@llvmbot
Copy link
Member

llvmbot commented Aug 7, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Jasmine Tang (badumbatish)

Changes

Fixes #149285.


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

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+8)
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index adbfbeb4669e7..d86ec003459d3 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -21477,6 +21477,14 @@ bool RISCVTargetLowering::canCreateUndefOrPoisonForTargetNode(
 
   // TODO: Add more target nodes.
   switch (Op.getOpcode()) {
+  case RISCVISD::SLLW:
+  case RISCVISD::SRAW:
+  case RISCVISD::SRLW:
+  case RISCVISD::RORW:
+  case RISCVISD::ROLW:
+    // Only the lower 5 bits of RHS are read, guaranteeing the rotate/shift
+    // amount is bounds.
+    return false;
   case RISCVISD::SELECT_CC:
     // Integer select_cc cannot create poison.
     // TODO: What are the FP poison semantics?

@badumbatish badumbatish marked this pull request as draft August 7, 2025 23:00
@badumbatish
Copy link
Contributor Author

badumbatish commented Aug 7, 2025

The only place where I see this piece of code used is via visitFreeze in llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp.

But in visiting freeze (hoping to hit sllw), freeze is still wrapping sext and shl, not sllw yet.

Therefore I'm not sure if there's any other tests to be constructed. I'm also not sure what kind of visible effects can I test for here? i.e even if freeze unwraps itself for a sllw inside, how does that change the assembly?

Putting it as draft for now

Test case is

define i64 @shl(i32 %x, i32 %y) {
; CHECK64I-LABEL: shl:
; CHECK64I:       # %bb.0:
; CHECK64I-NEXT:    sllw a0, a0, a1
; CHECK64I-NEXT:    ret
  %m = and i32 %y, 31
  %s = shl i32 %x, %m
  %r = sext i32 %s to i64
  %hhhh = freeze i64 %r
  ret i64 %r
}

@badumbatish badumbatish requested review from lukel97 and topperc August 7, 2025 23:02
Co-authored-by: RKSimon <RKSimon@users.noreply.github.com>
@badumbatish badumbatish marked this pull request as ready for review August 8, 2025 05:24
@topperc topperc changed the title Added ROLW/RORW/SLLW/SRAW/SRLW for canCreateUndefOrPoisonForTargetNode [RISCV] Added ROLW/RORW/SLLW/SRAW/SRLW for canCreateUndefOrPoisonForTargetNode Aug 23, 2025
@RKSimon
Copy link
Collaborator

RKSimon commented Aug 26, 2025

@topperc Do you have any objections to this going in without tests?

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

@topperc
Copy link
Collaborator

topperc commented Aug 26, 2025

@topperc Do you have any objections to this going in without tests?

No objection. Writing a test is probably tricky since this node appears after type legalization. So the freeze would also need to be created after type legalization to avoid it getting optimized earlier.

@RKSimon RKSimon enabled auto-merge (squash) August 27, 2025 11:54
@RKSimon RKSimon disabled auto-merge August 27, 2025 11:54
@RKSimon RKSimon enabled auto-merge (squash) August 27, 2025 11:54
@RKSimon RKSimon merged commit 18f5e42 into llvm:main Aug 27, 2025
9 checks passed
@badumbatish badumbatish deleted the riscv_cancreateup branch August 27, 2025 17:13
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.

[RISCV] canCreateUndefOrPoisonForTargetNode - can ROLW/RORW + SLLW/SRAW/SRLW create undef/poison?
4 participants