This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize i32 range checks
AbandonedPublic

Authored by dtcxzyw on May 3 2023, 10:10 PM.

Details

Reviewers
craig.topper
Summary

This patch improves instruction selection for i32 range check patterns like INT_MIN <= x && x <= INT_MAX and its alternatives.
On rv32: GCC: https://godbolt.org/z/1zKjfMqax Alive2: https://alive2.llvm.org/ce/z/FPc4ss
On rv64: GCC: https://godbolt.org/z/ezTsv8cEo Alive2: https://alive2.llvm.org/ce/z/ZkF7Er

I am unsure whether it is suitable to do the pattern match in ISel. I also have an alternative that does pattern match in RISCVCodeGenPrepare.

Diff Detail

Event Timeline

dtcxzyw created this revision.May 3 2023, 10:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 10:10 PM
dtcxzyw requested review of this revision.May 3 2023, 10:10 PM
craig.topper added a comment.EditedMay 4 2023, 4:25 PM

My suggested patch lifted from AArch64/X86 with i8/i16 removed.

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index c020b27edf3e..f8fe17a9cfe3 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -550,6 +550,23 @@ public:
     return ISD::SIGN_EXTEND;
   }
 
+  bool shouldTransformSignedTruncationCheck(EVT XVT,
+                                            unsigned KeptBits) const override {
+    // For vectors, we don't have a preference..
+    if (XVT.isVector())
+      return false;
+
+    auto VTIsOk = [](EVT VT) -> bool {
+      return VT == MVT::i32 || VT == MVT::i64;
+    };
+
+    // We are ok with KeptBitsVT being byte/word/dword, what sext.w supports.
+    // XVT will be larger than KeptBitsVT.
+    // FIXME: Support Zbb?
+    MVT KeptBitsVT = MVT::getIntegerVT(KeptBits);
+    return VTIsOk(XVT) && VTIsOk(KeptBitsVT);
+  }
+
   TargetLowering::ShiftLegalizationStrategy
   preferredShiftLegalizationStrategy(SelectionDAG &DAG, SDNode *N,
                                      unsigned ExpansionFactor) const override {

My suggested patch lifted from AArch64/X86 with i8/i16 removed.

diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index c020b27edf3e..f8fe17a9cfe3 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -550,6 +550,23 @@ public:
     return ISD::SIGN_EXTEND;
   }
 
+  bool shouldTransformSignedTruncationCheck(EVT XVT,
+                                            unsigned KeptBits) const override {
+    // For vectors, we don't have a preference..
+    if (XVT.isVector())
+      return false;
+
+    auto VTIsOk = [](EVT VT) -> bool {
+      return VT == MVT::i32 || VT == MVT::i64;
+    };
+
+    // We are ok with KeptBitsVT being byte/word/dword, what sext.w supports.
+    // XVT will be larger than KeptBitsVT.
+    // FIXME: Support Zbb?
+    MVT KeptBitsVT = MVT::getIntegerVT(KeptBits);
+    return VTIsOk(XVT) && VTIsOk(KeptBitsVT);
+  }
+
   TargetLowering::ShiftLegalizationStrategy
   preferredShiftLegalizationStrategy(SelectionDAG &DAG, SDNode *N,
                                      unsigned ExpansionFactor) const override {

Posted as https://reviews.llvm.org/D149977

dtcxzyw abandoned this revision.May 8 2023, 9:50 PM