This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Implement shouldTransformSignedTruncationCheck.
ClosedPublic

Authored by craig.topper on May 5 2023, 11:34 AM.

Details

Summary

This helps avoid constant materialization for the patterns
InstCombine emits for something like INT_MIN <= X && x <= INT_MAX.

See top of changed test files for more detailed explanation.

I've enabled this for i16 when Zbb is enabled. sext.b did not seem
to be a benefit due to the constants folding into addi/sltiu.

This an alternative to https://reviews.llvm.org/D149814

Diff Detail

Event Timeline

craig.topper created this revision.May 5 2023, 11:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2023, 11:34 AM
craig.topper requested review of this revision.May 5 2023, 11:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2023, 11:34 AM
dtcxzyw added inline comments.May 6 2023, 2:06 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
12172

We can enable this for i8 on rv32 when XVT is i64 and Zbb is enabled.
Before:

; RV32I-LABEL: add_ultcmp_i64_i8:
; RV32I:       # %bb.0:
; RV32I-NEXT:    addi a2, a0, -128
; RV32I-NEXT:    sltu a0, a2, a0
; RV32I-NEXT:    add a0, a1, a0
; RV32I-NEXT:    addi a0, a0, -1
; RV32I-NEXT:    li a1, -1
; RV32I-NEXT:    beq a0, a1, .LBB11_2
; RV32I-NEXT:  # %bb.1:
; RV32I-NEXT:    sltiu a0, a0, -1
; RV32I-NEXT:    ret
; RV32I-NEXT:  .LBB11_2:
; RV32I-NEXT:    sltiu a0, a2, -256
; RV32I-NEXT:    ret

After:

; RV32ZBB-LABEL: add_ultcmp_i64_i8:
; RV32ZBB:       # %bb.0:
; RV32ZBB-NEXT:    sext.b a2, a0
; RV32ZBB-NEXT:    xor a0, a2, a0
; RV32ZBB-NEXT:    srai a2, a2, 31
; RV32ZBB-NEXT:    xor a1, a2, a1
; RV32ZBB-NEXT:    or a0, a0, a1
; RV32ZBB-NEXT:    snez a0, a0
; RV32ZBB-NEXT:    ret
reames accepted this revision.May 8 2023, 8:13 AM

LGTM. The current form is an improvement. We can iterative improve in follow up reviews if desired.

This revision is now accepted and ready to land.May 8 2023, 8:13 AM