Skip to content

[RISCV] Fix Lsb > Msb case in (sra (sext_inreg X, _), C) for th.ext #136287

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
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ bool RISCVDAGToDAGISel::trySignedBitfieldExtract(SDNode *Node) {
return false;

const unsigned Msb = ExtSize - 1;
const unsigned Lsb = RightShAmt;
// If the shift-right amount is greater than Msb, it means that extracts
// the X[Msb] bit and sign-extend it.
const unsigned Lsb = RightShAmt > Msb ? Msb : RightShAmt;

SDNode *TH_EXT = BitfieldExtract(N0, Msb, Lsb, DL, VT);
ReplaceNode(Node, TH_EXT);
Expand Down
32 changes: 32 additions & 0 deletions llvm/test/CodeGen/RISCV/rv32xtheadbb.ll
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,38 @@ define i64 @no_sexth_i64(i64 %a) nounwind {
ret i64 %shr
}

define i32 @sext_sextinreg_sra(i16 %a) nounwind {
; RV32I-LABEL: sext_sextinreg_sra:
; RV32I: # %bb.0:
; RV32I-NEXT: slli a0, a0, 16
; RV32I-NEXT: srai a0, a0, 26
; RV32I-NEXT: ret
;
; RV32XTHEADBB-LABEL: sext_sextinreg_sra:
; RV32XTHEADBB: # %bb.0:
; RV32XTHEADBB-NEXT: th.ext a0, a0, 15, 10
; RV32XTHEADBB-NEXT: ret
%sext = sext i16 %a to i32
%shr = ashr exact i32 %sext, 10
ret i32 %shr
}

define i32 @sext_sextinreg_sra_2(i16 %a) nounwind {
; RV32I-LABEL: sext_sextinreg_sra_2:
; RV32I: # %bb.0:
; RV32I-NEXT: slli a0, a0, 16
; RV32I-NEXT: srai a0, a0, 31
; RV32I-NEXT: ret
;
; RV32XTHEADBB-LABEL: sext_sextinreg_sra_2:
; RV32XTHEADBB: # %bb.0:
; RV32XTHEADBB-NEXT: th.ext a0, a0, 15, 15
; RV32XTHEADBB-NEXT: ret
%sext = sext i16 %a to i32
%shr = ashr exact i32 %sext, 24
ret i32 %shr
}

define i32 @zexth_i32(i32 %a) nounwind {
; RV32I-LABEL: zexth_i32:
; RV32I: # %bb.0:
Expand Down
Loading