Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SelectionDAG] Add space-optimized forms of OPC_EmitCopyToReg #73293

Merged
merged 3 commits into from
Dec 12, 2023

Conversation

wangpc-pp
Copy link
Contributor

@wangpc-pp wangpc-pp commented Nov 24, 2023

These new opcodes implicitly indicate the RecNo.

The old OPC_EmitCopyToReg2 is renamed to OPC_EmitCopyToRegTwoByte.

Overall this reduces the llc binary size with all in-tree targets by
about 33K (most are from RISCV target).

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Nov 24, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 24, 2023

@llvm/pr-subscribers-llvm-selectiondag

Author: Wang Pengcheng (wangpc-pp)

Changes

These new opcodes implicitly indicate the RecNo.

The old OPC_EmitCopyToReg2 is renamed to OPC_EmitCopyToRegHalf.

Overall this reduces the llc binary size with all in-tree targets by
about 33K (most are from RISCV target).


Full diff: https://github.com/llvm/llvm-project/pull/73293.diff

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/SelectionDAGISel.h (+8)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp (+14-3)
  • (modified) llvm/utils/TableGen/DAGISelMatcherEmitter.cpp (+10-4)
diff --git a/llvm/include/llvm/CodeGen/SelectionDAGISel.h b/llvm/include/llvm/CodeGen/SelectionDAGISel.h
index e6513eb6abc8749..a0c590fd5269b5d 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAGISel.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAGISel.h
@@ -192,7 +192,15 @@ class SelectionDAGISel : public MachineFunctionPass {
     OPC_EmitMergeInputChains1_1,
     OPC_EmitMergeInputChains1_2,
     OPC_EmitCopyToReg,
+    OPC_EmitCopyToReg0,
+    OPC_EmitCopyToReg1,
     OPC_EmitCopyToReg2,
+    OPC_EmitCopyToReg3,
+    OPC_EmitCopyToReg4,
+    OPC_EmitCopyToReg5,
+    OPC_EmitCopyToReg6,
+    OPC_EmitCopyToReg7,
+    OPC_EmitCopyToRegHalf,
     OPC_EmitNodeXForm,
     OPC_EmitNode,
     // Space-optimized forms that implicitly encode number of result VTs.
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 7d9bebdca127224..126f90a632ff180 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -3584,11 +3584,22 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
     }
 
     case OPC_EmitCopyToReg:
-    case OPC_EmitCopyToReg2: {
-      unsigned RecNo = MatcherTable[MatcherIndex++];
+    case OPC_EmitCopyToReg0:
+    case OPC_EmitCopyToReg1:
+    case OPC_EmitCopyToReg2:
+    case OPC_EmitCopyToReg3:
+    case OPC_EmitCopyToReg4:
+    case OPC_EmitCopyToReg5:
+    case OPC_EmitCopyToReg6:
+    case OPC_EmitCopyToReg7:
+    case OPC_EmitCopyToRegHalf: {
+      unsigned RecNo =
+          Opcode >= OPC_EmitCopyToReg0 && Opcode <= OPC_EmitCopyToReg7
+              ? Opcode - OPC_EmitCopyToReg0
+              : MatcherTable[MatcherIndex++];
       assert(RecNo < RecordedNodes.size() && "Invalid EmitCopyToReg");
       unsigned DestPhysReg = MatcherTable[MatcherIndex++];
-      if (Opcode == OPC_EmitCopyToReg2)
+      if (Opcode == OPC_EmitCopyToRegHalf)
         DestPhysReg |= MatcherTable[MatcherIndex++] << 8;
 
       if (!InputChain.getNode())
diff --git a/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp b/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
index 4a11991036efc11..e62119c1f6c3e0f 100644
--- a/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
@@ -732,14 +732,20 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
     const auto *C2RMatcher = cast<EmitCopyToRegMatcher>(N);
     int Bytes = 3;
     const CodeGenRegister *Reg = C2RMatcher->getDestPhysReg();
+    unsigned Slot = C2RMatcher->getSrcSlot();
     if (Reg->EnumValue > 255) {
       assert(isUInt<16>(Reg->EnumValue) && "not handled");
-      OS << "OPC_EmitCopyToReg2, " << C2RMatcher->getSrcSlot() << ", "
-         << "TARGET_VAL(" << getQualifiedName(Reg->TheDef) << "),\n";
+      OS << "OPC_EmitCopyToRegHalf, " << Slot << ", " << "TARGET_VAL("
+         << getQualifiedName(Reg->TheDef) << "),\n";
       ++Bytes;
     } else {
-      OS << "OPC_EmitCopyToReg, " << C2RMatcher->getSrcSlot() << ", "
-         << getQualifiedName(Reg->TheDef) << ",\n";
+      if (Slot < 8) {
+        OS << "OPC_EmitCopyToReg" << Slot << ", "
+           << getQualifiedName(Reg->TheDef) << ",\n";
+        --Bytes;
+      } else
+        OS << "OPC_EmitCopyToReg, " << Slot << ", "
+           << getQualifiedName(Reg->TheDef) << ",\n";
     }
 
     return Bytes;

Copy link

github-actions bot commented Nov 24, 2023

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

@wangpc-pp wangpc-pp force-pushed the main-matcher-table-emit-copy-to-reg branch from 31bfa75 to aa436c1 Compare November 24, 2023 07:47
@wangpc-pp
Copy link
Contributor Author

Ping.

These new opcodes implicitly indicate the RecNo.

The old `OPC_EmitCopyToReg2` is renamed to `OPC_EmitCopyToRegHalf`.

Overall this reduces the llc binary size with all in-tree targets by
about 33K (most are from RISCV target).
@wangpc-pp wangpc-pp force-pushed the main-matcher-table-emit-copy-to-reg branch from d64fc64 to 0e9e102 Compare December 11, 2023 12:33
Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wangpc-pp wangpc-pp merged commit cbf1d58 into llvm:main Dec 12, 2023
4 checks passed
@wangpc-pp wangpc-pp deleted the main-matcher-table-emit-copy-to-reg branch December 12, 2023 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants