[clang] ec23e55 - [clang][Interp][NFC] Avoid unnecessary work in compileFunc()

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 07:08:33 PST 2022


Author: Timm Bäder
Date: 2022-11-30T16:07:29+01:00
New Revision: ec23e5584de0b7212db64f5e2aff355e8279537e

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

LOG: [clang][Interp][NFC] Avoid unnecessary work in compileFunc()

We don't need to create the paramter descriptors etc. if we've already
done that in the past.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeEmitter.cpp b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
index 1d72a904556eb..b67e6969c37e1 100644
--- a/clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -26,50 +26,52 @@ ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
   // will (maybe) happen later.
   bool HasBody = FuncDecl->hasBody(FuncDecl);
 
-  // Set up argument indices.
-  unsigned ParamOffset = 0;
-  SmallVector<PrimType, 8> ParamTypes;
-  llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
-
-  // If the return is not a primitive, a pointer to the storage where the value
-  // is initialized in is passed as the first argument.
-  // See 'RVO' elsewhere in the code.
-  QualType Ty = FuncDecl->getReturnType();
-  bool HasRVO = false;
-  if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
-    HasRVO = true;
-    ParamTypes.push_back(PT_Ptr);
-    ParamOffset += align(primSize(PT_Ptr));
-  }
+  // Create a handle over the emitted code.
+  Function *Func = P.getFunction(FuncDecl);
+  if (!Func) {
+    // Set up argument indices.
+    unsigned ParamOffset = 0;
+    SmallVector<PrimType, 8> ParamTypes;
+    llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
+
+    // If the return is not a primitive, a pointer to the storage where the
+    // value is initialized in is passed as the first argument. See 'RVO'
+    // elsewhere in the code.
+    QualType Ty = FuncDecl->getReturnType();
+    bool HasRVO = false;
+    if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
+      HasRVO = true;
+      ParamTypes.push_back(PT_Ptr);
+      ParamOffset += align(primSize(PT_Ptr));
+    }
 
-  // If the function decl is a member decl, the next parameter is
-  // the 'this' pointer. This parameter is pop()ed from the
-  // InterpStack when calling the function.
-  bool HasThisPointer = false;
-  if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
-      MD && MD->isInstance()) {
-    HasThisPointer = true;
-    ParamTypes.push_back(PT_Ptr);
-    ParamOffset += align(primSize(PT_Ptr));
-  }
+    // If the function decl is a member decl, the next parameter is
+    // the 'this' pointer. This parameter is pop()ed from the
+    // InterpStack when calling the function.
+    bool HasThisPointer = false;
+    if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
+        MD && MD->isInstance()) {
+      HasThisPointer = true;
+      ParamTypes.push_back(PT_Ptr);
+      ParamOffset += align(primSize(PT_Ptr));
+    }
 
-  // Assign descriptors to all parameters.
-  // Composite objects are lowered to pointers.
-  for (const ParmVarDecl *PD : FuncDecl->parameters()) {
-    PrimType Ty = Ctx.classify(PD->getType()).value_or(PT_Ptr);
-    Descriptor *Desc = P.createDescriptor(PD, Ty);
-    ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
-    Params.insert({PD, ParamOffset});
-    ParamOffset += align(primSize(Ty));
-    ParamTypes.push_back(Ty);
-  }
+    // Assign descriptors to all parameters.
+    // Composite objects are lowered to pointers.
+    for (const ParmVarDecl *PD : FuncDecl->parameters()) {
+      PrimType Ty = Ctx.classify(PD->getType()).value_or(PT_Ptr);
+      Descriptor *Desc = P.createDescriptor(PD, Ty);
+      ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
+      Params.insert({PD, ParamOffset});
+      ParamOffset += align(primSize(Ty));
+      ParamTypes.push_back(Ty);
+    }
 
-  // Create a handle over the emitted code.
-  Function *Func = P.getFunction(FuncDecl);
-  if (!Func)
     Func =
         P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
                          std::move(ParamDescriptors), HasThisPointer, HasRVO);
+  }
+
   assert(Func);
   if (!HasBody)
     return Func;


        


More information about the cfe-commits mailing list