Skip to content

Commit f3f717b

Browse files
authored
[RISCV] Add computeKnownBitsForTargetNode for RISCVISD::SRAW. (#156191)
This node reads the lower 32 bits, shifts it right arithmetically then sign extends to i64. If we know some of the lower 32 bits we can propagate that information. For the test case I had to find something that didn't get optimized before type legalizaton and didn't get type legalized to a sign extended value. The bswap gets type legalized to (lshr (bswap), 32).
1 parent 31c9198 commit f3f717b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21379,6 +21379,15 @@ void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
2137921379
Known = Known.sext(BitWidth);
2138021380
break;
2138121381
}
21382+
case RISCVISD::SRAW: {
21383+
KnownBits Known2;
21384+
Known = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
21385+
Known2 = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
21386+
Known = KnownBits::ashr(Known.trunc(32), Known2.trunc(5).zext(32));
21387+
// Restore the original width by sign extending.
21388+
Known = Known.sext(BitWidth);
21389+
break;
21390+
}
2138221391
case RISCVISD::CTZW: {
2138321392
KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1);
2138421393
unsigned PossibleTZ = Known2.trunc(32).countMaxTrailingZeros();

llvm/test/CodeGen/RISCV/rv64i-shift-sext.ll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,28 @@ define signext i32 @test15(i32 signext %x, i32 signext %y) {
235235
%c = shl i32 %b, 1
236236
ret i32 %c
237237
}
238+
239+
define signext i32 @test16(i32 signext %x, i32 signext %y) {
240+
; RV64I-LABEL: test16:
241+
; RV64I: # %bb.0:
242+
; RV64I-NEXT: ori a2, a0, 192
243+
; RV64I-NEXT: srli a3, a0, 8
244+
; RV64I-NEXT: lui a4, 16
245+
; RV64I-NEXT: srliw a0, a0, 24
246+
; RV64I-NEXT: addi a4, a4, -256
247+
; RV64I-NEXT: and a3, a3, a4
248+
; RV64I-NEXT: and a4, a2, a4
249+
; RV64I-NEXT: or a0, a3, a0
250+
; RV64I-NEXT: slli a4, a4, 8
251+
; RV64I-NEXT: slli a2, a2, 24
252+
; RV64I-NEXT: or a2, a2, a4
253+
; RV64I-NEXT: or a0, a2, a0
254+
; RV64I-NEXT: sraw a0, a0, a1
255+
; RV64I-NEXT: slli a0, a0, 1
256+
; RV64I-NEXT: ret
257+
%a = or i32 %x, 192
258+
%d = call i32 @llvm.bswap.i32(i32 %a)
259+
%b = ashr i32 %d, %y
260+
%c = shl i32 %b, 1
261+
ret i32 %c
262+
}

0 commit comments

Comments
 (0)