[PATCH] D108966: [RISCV] Teach instruction selection to elide sext.w in some cases.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 2 07:54:48 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb5fd6b46f59e: [RISCV] Teach instruction selection to elide sext.w in some cases. (authored by craig.topper).

Changed prior to commit:
  https://reviews.llvm.org/D108966?vs=369611&id=370272#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108966/new/

https://reviews.llvm.org/D108966

Files:
  llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
  llvm/lib/Target/RISCV/RISCVInstrInfo.td
  llvm/test/CodeGen/RISCV/rv64zba.ll


Index: llvm/test/CodeGen/RISCV/rv64zba.ll
===================================================================
--- llvm/test/CodeGen/RISCV/rv64zba.ll
+++ llvm/test/CodeGen/RISCV/rv64zba.ll
@@ -376,7 +376,6 @@
 ; to remove the sext_inreg because it has multiple uses. The ashr will use the
 ; sext_inreg to become sraiw. This leaves the sext_inreg only used by the shl.
 ; If the shl is selected as sllw, we don't need the sext_inreg.
-; FIXME: We should not emit a sext.w.
 define i64 @sh2add_extra_sext(i32 %x, i32 %y, i32 %z) {
 ; RV64I-LABEL: sh2add_extra_sext:
 ; RV64I:       # %bb.0:
@@ -390,8 +389,7 @@
 ; RV64B-LABEL: sh2add_extra_sext:
 ; RV64B:       # %bb.0:
 ; RV64B-NEXT:    sh2add a0, a0, a1
-; RV64B-NEXT:    sext.w a1, a0
-; RV64B-NEXT:    sllw a1, a2, a1
+; RV64B-NEXT:    sllw a1, a2, a0
 ; RV64B-NEXT:    sraiw a0, a0, 2
 ; RV64B-NEXT:    mul a0, a1, a0
 ; RV64B-NEXT:    ret
@@ -399,8 +397,7 @@
 ; RV64ZBA-LABEL: sh2add_extra_sext:
 ; RV64ZBA:       # %bb.0:
 ; RV64ZBA-NEXT:    sh2add a0, a0, a1
-; RV64ZBA-NEXT:    sext.w a1, a0
-; RV64ZBA-NEXT:    sllw a1, a2, a1
+; RV64ZBA-NEXT:    sllw a1, a2, a0
 ; RV64ZBA-NEXT:    sraiw a0, a0, 2
 ; RV64ZBA-NEXT:    mul a0, a1, a0
 ; RV64ZBA-NEXT:    ret
Index: llvm/lib/Target/RISCV/RISCVInstrInfo.td
===================================================================
--- llvm/lib/Target/RISCV/RISCVInstrInfo.td
+++ llvm/lib/Target/RISCV/RISCVInstrInfo.td
@@ -1262,10 +1262,18 @@
   return hasAllWUsers(Node);
 }]>;
 
+def sexti32_allwusers : PatFrag<(ops node:$src),
+                                (sext_inreg node:$src, i32), [{
+  return hasAllWUsers(Node);
+}]>;
+
 let Predicates = [IsRV64] in {
 
 /// sext and zext
 
+// Sign extend is not needed if all users are W instructions.
+def : Pat<(sexti32_allwusers GPR:$rs1), (XLenVT GPR:$rs1)>;
+
 def : Pat<(sext_inreg GPR:$rs1, i32), (ADDIW GPR:$rs1, 0)>;
 
 /// ALU operations
Index: llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
+++ llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
@@ -1536,6 +1536,7 @@
 bool RISCVDAGToDAGISel::hasAllNBitUsers(SDNode *Node, unsigned Bits) const {
   assert((Node->getOpcode() == ISD::ADD || Node->getOpcode() == ISD::SUB ||
           Node->getOpcode() == ISD::MUL || Node->getOpcode() == ISD::SHL ||
+          Node->getOpcode() == ISD::SIGN_EXTEND_INREG ||
           isa<ConstantSDNode>(Node)) &&
          "Unexpected opcode");
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108966.370272.patch
Type: text/x-patch
Size: 2501 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210902/a9311cb5/attachment.bin>


More information about the llvm-commits mailing list