From 7cff8772838d776ccfd1e4eeaba9fd89488a97dd Mon Sep 17 00:00:00 2001
From: Alexey Samsonov <vonosmas@gmail.com>
Date: Mon, 29 Sep 2014 20:30:22 +0000
Subject: [PATCH] Speedup ClangToLLVMArgMapping construction. NFC.

Add a method to calculate the number of arguments given QualType
expnads to. Use this method in ClangToLLVMArgMapping calculation.
This number may be cached in CodeGenTypes for efficiency, if needed.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@218623 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/CodeGen/CGCall.cpp | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp
index 5f22d3929f7..e4da4e14113 100644
--- a/lib/CodeGen/CGCall.cpp
+++ b/lib/CodeGen/CGCall.cpp
@@ -612,6 +612,23 @@ getTypeExpansion(QualType Ty, const ASTContext &Context) {
   return llvm::make_unique<NoExpansion>();
 }
 
+static int getExpansionSize(QualType Ty, const ASTContext &Context) {
+  auto Exp = getTypeExpansion(Ty, Context);
+  if (auto CAExp = dyn_cast<ConstantArrayExpansion>(Exp.get())) {
+    return CAExp->NumElts * getExpansionSize(CAExp->EltTy, Context);
+  }
+  if (auto RExp = dyn_cast<RecordExpansion>(Exp.get())) {
+    int Res = 0;
+    for (auto FD : RExp->Fields)
+      Res += getExpansionSize(FD->getType(), Context);
+    return Res;
+  }
+  if (isa<ComplexExpansion>(Exp.get()))
+    return 2;
+  assert(isa<NoExpansion>(Exp.get()));
+  return 1;
+}
+
 void CodeGenTypes::GetExpandedTypes(QualType type,
                      SmallVectorImpl<llvm::Type*> &expandedTypes) {
   auto Exp = getTypeExpansion(type, Context);
@@ -1222,12 +1239,7 @@ void ClangToLLVMArgMapping::construct(CodeGenModule &CGM,
       IRArgs.NumberOfArgs = 0;
       break;
     case ABIArgInfo::Expand: {
-      SmallVector<llvm::Type*, 8> Types;
-      // FIXME: This is rather inefficient. Do we ever actually need to do
-      // anything here? The result should be just reconstructed on the other
-      // side, so extension should be a non-issue.
-      CGM.getTypes().GetExpandedTypes(ArgType, Types);
-      IRArgs.NumberOfArgs = Types.size();
+      IRArgs.NumberOfArgs = getExpansionSize(ArgType, CGM.getContext());
       break;
     }
     }
-- 
GitLab