Skip to content

Commit cb71c3f

Browse files
committed
[RISCV] Fix Lsb > Msb case in (sra (sext_inreg X, _), C) for th.ext
According the spec https://github.com/XUANTIE-RV/thead-extension-spec/releases/tag/2.3.0, the operation of `th.ext rd, rs1, msb, lsb` is reg[rd] := sign_extend(reg[rs1][msb:lsb]) The spec doesn't specify if lsb is greater than msb. I don't think lsb can be greater than msb. So that If the shift-right amount is greater than msb, we can set lsb equal to msb to extract the bit rs1[msb] and sign-extend it.
1 parent bb6fe51 commit cb71c3f

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,9 @@ bool RISCVDAGToDAGISel::trySignedBitfieldExtract(SDNode *Node) {
653653
return false;
654654

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

658660
SDNode *TH_EXT = BitfieldExtract(N0, Msb, Lsb, DL, VT);
659661
ReplaceNode(Node, TH_EXT);

llvm/test/CodeGen/RISCV/rv32xtheadbb.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ define i32 @sext_sextinreg_sra_2(i16 %a) nounwind {
426426
;
427427
; RV32XTHEADBB-LABEL: sext_sextinreg_sra_2:
428428
; RV32XTHEADBB: # %bb.0:
429-
; RV32XTHEADBB-NEXT: th.ext a0, a0, 15, 24
429+
; RV32XTHEADBB-NEXT: th.ext a0, a0, 15, 15
430430
; RV32XTHEADBB-NEXT: ret
431431
%sext = sext i16 %a to i32
432432
%shr = ashr exact i32 %sext, 24

0 commit comments

Comments
 (0)