[llvm] 9692dff - [TableGen][NFC] Use early exit to simplify large block in emitAction. (#138220)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 8 10:12:18 PDT 2025


Author: Jason Eckhardt
Date: 2025-05-08T12:12:15-05:00
New Revision: 9692dff7b7624208f482007c3b76c838b12fda63

URL: https://github.com/llvm/llvm-project/commit/9692dff7b7624208f482007c3b76c838b12fda63
DIFF: https://github.com/llvm/llvm-project/commit/9692dff7b7624208f482007c3b76c838b12fda63.diff

LOG: [TableGen][NFC] Use early exit to simplify large block in emitAction. (#138220)

Most of the processing in emitAction is in an unneeded else-block--
reduce indentation by exiting after the recursive call.

`XXXGenCallingConv.inc` are identical before and after this patch for
all targets.

Added: 
    

Modified: 
    llvm/test/TableGen/cc-assign-to-reg-tuple.td
    llvm/utils/TableGen/CallingConvEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/TableGen/cc-assign-to-reg-tuple.td b/llvm/test/TableGen/cc-assign-to-reg-tuple.td
index 624aa1d5f5f0b..abc3402ea9038 100644
--- a/llvm/test/TableGen/cc-assign-to-reg-tuple.td
+++ b/llvm/test/TableGen/cc-assign-to-reg-tuple.td
@@ -1,6 +1,6 @@
-// RUN: llvm-tblgen --gen-callingconv -I %p/../../include -I %p/Common %s 2>&1 | FileCheck %s
-// RUN: not llvm-tblgen -DERROR1 --gen-callingconv -I %p/../../include -I %p/Common %s 2>&1 | FileCheck --check-prefix=CHECK-ERROR1 %s
-// RUN: not llvm-tblgen -DERROR2 --gen-callingconv -I %p/../../include -I %p/Common %s 2>&1 | FileCheck --check-prefix=CHECK-ERROR2 %s
+// RUN: llvm-tblgen -gen-callingconv -I %p/../../include -I %p/Common %s | FileCheck %s
+// RUN: not llvm-tblgen -gen-callingconv -DERROR1 -I %p/../../include -I %p/Common %s 2>&1 | FileCheck --check-prefix=CHECK-ERROR1 %s
+// RUN: not llvm-tblgen -gen-callingconv -DERROR2 -I %p/../../include -I %p/Common %s 2>&1 | FileCheck --check-prefix=CHECK-ERROR2 %s
 
 include "reg-with-subregs-common.td"
 

diff  --git a/llvm/utils/TableGen/CallingConvEmitter.cpp b/llvm/utils/TableGen/CallingConvEmitter.cpp
index 20dc1ff0ffd14..6a2a2442a6044 100644
--- a/llvm/utils/TableGen/CallingConvEmitter.cpp
+++ b/llvm/utils/TableGen/CallingConvEmitter.cpp
@@ -221,120 +221,119 @@ void CallingConvEmitter::emitAction(const Record *Action, indent Indent,
     O << ") {\n";
     emitAction(Action->getValueAsDef("SubAction"), Indent + 2, O);
     O << Indent << "}\n";
-  } else {
-    if (Action->isSubClassOf("CCDelegateTo")) {
-      const Record *CC = Action->getValueAsDef("CC");
-      O << Indent << "if (!" << CC->getName()
-        << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
-        << Indent + 2 << "return false;\n";
-      DelegateToMap[CurrentAction].insert(CC->getName().str());
-    } else if (Action->isSubClassOf("CCAssignToReg") ||
-               Action->isSubClassOf("CCAssignToRegTuple") ||
-               Action->isSubClassOf("CCAssignToRegAndStack")) {
-      const ListInit *RegList = Action->getValueAsListInit("RegList");
-      for (unsigned I = 0, E = RegList->size(); I != E; ++I) {
-        std::string Name = getQualifiedRegisterName(RegList->getElement(I));
-        if (SwiftAction)
-          AssignedSwiftRegsMap[CurrentAction].insert(std::move(Name));
-        else
-          AssignedRegsMap[CurrentAction].insert(std::move(Name));
-      }
-      EmitAllocateReg({RegList}, {"RegList"});
-
-      if (Action->isSubClassOf("CCAssignToRegAndStack"))
-        EmitAllocateStack();
-
-      O << Indent << "  return false;\n";
-      O << Indent << "}\n";
-    } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
-      const ListInit *RegList = Action->getValueAsListInit("RegList");
-      const ListInit *ShadowRegList =
-          Action->getValueAsListInit("ShadowRegList");
-      if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
-        PrintFatalError(Action->getLoc(),
-                        "Invalid length of list of shadowed registers");
-
-      EmitAllocateReg({RegList, ShadowRegList}, {"RegList", "RegList"});
-
-      O << Indent << "  return false;\n";
-      O << Indent << "}\n";
-    } else if (Action->isSubClassOf("CCAssignToStack")) {
-      EmitAllocateStack(/*EmitOffset=*/true);
-      O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
-        << Counter << ", LocVT, LocInfo));\n";
-      O << Indent << "return false;\n";
-    } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
-      int Size = Action->getValueAsInt("Size");
-      int Align = Action->getValueAsInt("Align");
-      const ListInit *ShadowRegList =
-          Action->getValueAsListInit("ShadowRegList");
-
-      unsigned ShadowRegListNumber = ++Counter;
-      EmitRegList(ShadowRegList, "ShadowRegList" + utostr(ShadowRegListNumber));
-
-      O << Indent << "int64_t Offset" << ++Counter << " = State.AllocateStack("
-        << Size << ", Align(" << Align << "), "
-        << "ShadowRegList" << ShadowRegListNumber << ");\n";
-      O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
-        << Counter << ", LocVT, LocInfo));\n";
-      O << Indent << "return false;\n";
-    } else if (Action->isSubClassOf("CCPromoteToType")) {
-      const Record *DestTy = Action->getValueAsDef("DestTy");
-      MVT::SimpleValueType DestVT = getValueType(DestTy);
-      O << Indent << "LocVT = " << getEnumName(DestVT) << ";\n";
-      if (MVT(DestVT).isFloatingPoint()) {
-        O << Indent << "LocInfo = CCValAssign::FPExt;\n";
-      } else {
-        O << Indent << "if (ArgFlags.isSExt())\n"
-          << Indent << "  LocInfo = CCValAssign::SExt;\n"
-          << Indent << "else if (ArgFlags.isZExt())\n"
-          << Indent << "  LocInfo = CCValAssign::ZExt;\n"
-          << Indent << "else\n"
-          << Indent << "  LocInfo = CCValAssign::AExt;\n";
-      }
-    } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
-      const Record *DestTy = Action->getValueAsDef("DestTy");
-      MVT::SimpleValueType DestVT = getValueType(DestTy);
-      O << Indent << "LocVT = " << getEnumName(DestVT) << ";\n";
-      if (MVT(DestVT).isFloatingPoint()) {
-        PrintFatalError(Action->getLoc(),
-                        "CCPromoteToUpperBitsInType does not handle floating "
-                        "point");
-      } else {
-        O << Indent << "if (ArgFlags.isSExt())\n"
-          << Indent << "  LocInfo = CCValAssign::SExtUpper;\n"
-          << Indent << "else if (ArgFlags.isZExt())\n"
-          << Indent << "  LocInfo = CCValAssign::ZExtUpper;\n"
-          << Indent << "else\n"
-          << Indent << "  LocInfo = CCValAssign::AExtUpper;\n";
-      }
-    } else if (Action->isSubClassOf("CCBitConvertToType")) {
-      const Record *DestTy = Action->getValueAsDef("DestTy");
-      O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
-      O << Indent << "LocInfo = CCValAssign::BCvt;\n";
-    } else if (Action->isSubClassOf("CCTruncToType")) {
-      const Record *DestTy = Action->getValueAsDef("DestTy");
-      O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
-      O << Indent << "LocInfo = CCValAssign::Trunc;\n";
-    } else if (Action->isSubClassOf("CCPassIndirect")) {
-      const Record *DestTy = Action->getValueAsDef("DestTy");
-      O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
-      O << Indent << "LocInfo = CCValAssign::Indirect;\n";
-    } else if (Action->isSubClassOf("CCPassByVal")) {
-      int Size = Action->getValueAsInt("Size");
-      int Align = Action->getValueAsInt("Align");
-      O << Indent << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " << Size
-        << ", Align(" << Align << "), ArgFlags);\n";
-      O << Indent << "return false;\n";
-    } else if (Action->isSubClassOf("CCCustom")) {
-      O << Indent << "if (" << Action->getValueAsString("FuncName")
-        << "(ValNo, ValVT, "
-        << "LocVT, LocInfo, ArgFlags, State))\n";
-      O << Indent << "  return false;\n";
+    return;
+  }
+
+  if (Action->isSubClassOf("CCDelegateTo")) {
+    const Record *CC = Action->getValueAsDef("CC");
+    O << Indent << "if (!" << CC->getName()
+      << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n"
+      << Indent + 2 << "return false;\n";
+    DelegateToMap[CurrentAction].insert(CC->getName().str());
+  } else if (Action->isSubClassOf("CCAssignToReg") ||
+             Action->isSubClassOf("CCAssignToRegTuple") ||
+             Action->isSubClassOf("CCAssignToRegAndStack")) {
+    const ListInit *RegList = Action->getValueAsListInit("RegList");
+    for (unsigned I = 0, E = RegList->size(); I != E; ++I) {
+      std::string Name = getQualifiedRegisterName(RegList->getElement(I));
+      if (SwiftAction)
+        AssignedSwiftRegsMap[CurrentAction].insert(std::move(Name));
+      else
+        AssignedRegsMap[CurrentAction].insert(std::move(Name));
+    }
+    EmitAllocateReg({RegList}, {"RegList"});
+
+    if (Action->isSubClassOf("CCAssignToRegAndStack"))
+      EmitAllocateStack();
+
+    O << Indent << "  return false;\n";
+    O << Indent << "}\n";
+  } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) {
+    const ListInit *RegList = Action->getValueAsListInit("RegList");
+    const ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
+    if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size())
+      PrintFatalError(Action->getLoc(),
+                      "Invalid length of list of shadowed registers");
+
+    EmitAllocateReg({RegList, ShadowRegList}, {"RegList", "RegList"});
+
+    O << Indent << "  return false;\n";
+    O << Indent << "}\n";
+  } else if (Action->isSubClassOf("CCAssignToStack")) {
+    EmitAllocateStack(/*EmitOffset=*/true);
+    O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
+      << Counter << ", LocVT, LocInfo));\n";
+    O << Indent << "return false;\n";
+  } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) {
+    int Size = Action->getValueAsInt("Size");
+    int Align = Action->getValueAsInt("Align");
+    const ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList");
+
+    unsigned ShadowRegListNumber = ++Counter;
+    EmitRegList(ShadowRegList, "ShadowRegList" + utostr(ShadowRegListNumber));
+
+    O << Indent << "int64_t Offset" << ++Counter << " = State.AllocateStack("
+      << Size << ", Align(" << Align << "), "
+      << "ShadowRegList" << ShadowRegListNumber << ");\n";
+    O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset"
+      << Counter << ", LocVT, LocInfo));\n";
+    O << Indent << "return false;\n";
+  } else if (Action->isSubClassOf("CCPromoteToType")) {
+    const Record *DestTy = Action->getValueAsDef("DestTy");
+    MVT::SimpleValueType DestVT = getValueType(DestTy);
+    O << Indent << "LocVT = " << getEnumName(DestVT) << ";\n";
+    if (MVT(DestVT).isFloatingPoint()) {
+      O << Indent << "LocInfo = CCValAssign::FPExt;\n";
     } else {
-      errs() << *Action;
-      PrintFatalError(Action->getLoc(), "Unknown CCAction!");
+      O << Indent << "if (ArgFlags.isSExt())\n"
+        << Indent << "  LocInfo = CCValAssign::SExt;\n"
+        << Indent << "else if (ArgFlags.isZExt())\n"
+        << Indent << "  LocInfo = CCValAssign::ZExt;\n"
+        << Indent << "else\n"
+        << Indent << "  LocInfo = CCValAssign::AExt;\n";
+    }
+  } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) {
+    const Record *DestTy = Action->getValueAsDef("DestTy");
+    MVT::SimpleValueType DestVT = getValueType(DestTy);
+    O << Indent << "LocVT = " << getEnumName(DestVT) << ";\n";
+    if (MVT(DestVT).isFloatingPoint()) {
+      PrintFatalError(Action->getLoc(),
+                      "CCPromoteToUpperBitsInType does not handle floating "
+                      "point");
+    } else {
+      O << Indent << "if (ArgFlags.isSExt())\n"
+        << Indent << "  LocInfo = CCValAssign::SExtUpper;\n"
+        << Indent << "else if (ArgFlags.isZExt())\n"
+        << Indent << "  LocInfo = CCValAssign::ZExtUpper;\n"
+        << Indent << "else\n"
+        << Indent << "  LocInfo = CCValAssign::AExtUpper;\n";
     }
+  } else if (Action->isSubClassOf("CCBitConvertToType")) {
+    const Record *DestTy = Action->getValueAsDef("DestTy");
+    O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
+    O << Indent << "LocInfo = CCValAssign::BCvt;\n";
+  } else if (Action->isSubClassOf("CCTruncToType")) {
+    const Record *DestTy = Action->getValueAsDef("DestTy");
+    O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
+    O << Indent << "LocInfo = CCValAssign::Trunc;\n";
+  } else if (Action->isSubClassOf("CCPassIndirect")) {
+    const Record *DestTy = Action->getValueAsDef("DestTy");
+    O << Indent << "LocVT = " << getEnumName(getValueType(DestTy)) << ";\n";
+    O << Indent << "LocInfo = CCValAssign::Indirect;\n";
+  } else if (Action->isSubClassOf("CCPassByVal")) {
+    int Size = Action->getValueAsInt("Size");
+    int Align = Action->getValueAsInt("Align");
+    O << Indent << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " << Size
+      << ", Align(" << Align << "), ArgFlags);\n";
+    O << Indent << "return false;\n";
+  } else if (Action->isSubClassOf("CCCustom")) {
+    O << Indent << "if (" << Action->getValueAsString("FuncName")
+      << "(ValNo, ValVT, "
+      << "LocVT, LocInfo, ArgFlags, State))\n";
+    O << Indent << "  return false;\n";
+  } else {
+    errs() << *Action;
+    PrintFatalError(Action->getLoc(), "Unknown CCAction!");
   }
 }
 


        


More information about the llvm-commits mailing list