Skip to content

Commit ec23e55

Browse files
committed
[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.
1 parent 9221bed commit ec23e55

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

clang/lib/AST/Interp/ByteCodeEmitter.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,52 @@ ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
2626
// will (maybe) happen later.
2727
bool HasBody = FuncDecl->hasBody(FuncDecl);
2828

29-
// Set up argument indices.
30-
unsigned ParamOffset = 0;
31-
SmallVector<PrimType, 8> ParamTypes;
32-
llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
33-
34-
// If the return is not a primitive, a pointer to the storage where the value
35-
// is initialized in is passed as the first argument.
36-
// See 'RVO' elsewhere in the code.
37-
QualType Ty = FuncDecl->getReturnType();
38-
bool HasRVO = false;
39-
if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
40-
HasRVO = true;
41-
ParamTypes.push_back(PT_Ptr);
42-
ParamOffset += align(primSize(PT_Ptr));
43-
}
29+
// Create a handle over the emitted code.
30+
Function *Func = P.getFunction(FuncDecl);
31+
if (!Func) {
32+
// Set up argument indices.
33+
unsigned ParamOffset = 0;
34+
SmallVector<PrimType, 8> ParamTypes;
35+
llvm::DenseMap<unsigned, Function::ParamDescriptor> ParamDescriptors;
36+
37+
// If the return is not a primitive, a pointer to the storage where the
38+
// value is initialized in is passed as the first argument. See 'RVO'
39+
// elsewhere in the code.
40+
QualType Ty = FuncDecl->getReturnType();
41+
bool HasRVO = false;
42+
if (!Ty->isVoidType() && !Ctx.classify(Ty)) {
43+
HasRVO = true;
44+
ParamTypes.push_back(PT_Ptr);
45+
ParamOffset += align(primSize(PT_Ptr));
46+
}
4447

45-
// If the function decl is a member decl, the next parameter is
46-
// the 'this' pointer. This parameter is pop()ed from the
47-
// InterpStack when calling the function.
48-
bool HasThisPointer = false;
49-
if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
50-
MD && MD->isInstance()) {
51-
HasThisPointer = true;
52-
ParamTypes.push_back(PT_Ptr);
53-
ParamOffset += align(primSize(PT_Ptr));
54-
}
48+
// If the function decl is a member decl, the next parameter is
49+
// the 'this' pointer. This parameter is pop()ed from the
50+
// InterpStack when calling the function.
51+
bool HasThisPointer = false;
52+
if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl);
53+
MD && MD->isInstance()) {
54+
HasThisPointer = true;
55+
ParamTypes.push_back(PT_Ptr);
56+
ParamOffset += align(primSize(PT_Ptr));
57+
}
5558

56-
// Assign descriptors to all parameters.
57-
// Composite objects are lowered to pointers.
58-
for (const ParmVarDecl *PD : FuncDecl->parameters()) {
59-
PrimType Ty = Ctx.classify(PD->getType()).value_or(PT_Ptr);
60-
Descriptor *Desc = P.createDescriptor(PD, Ty);
61-
ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
62-
Params.insert({PD, ParamOffset});
63-
ParamOffset += align(primSize(Ty));
64-
ParamTypes.push_back(Ty);
65-
}
59+
// Assign descriptors to all parameters.
60+
// Composite objects are lowered to pointers.
61+
for (const ParmVarDecl *PD : FuncDecl->parameters()) {
62+
PrimType Ty = Ctx.classify(PD->getType()).value_or(PT_Ptr);
63+
Descriptor *Desc = P.createDescriptor(PD, Ty);
64+
ParamDescriptors.insert({ParamOffset, {Ty, Desc}});
65+
Params.insert({PD, ParamOffset});
66+
ParamOffset += align(primSize(Ty));
67+
ParamTypes.push_back(Ty);
68+
}
6669

67-
// Create a handle over the emitted code.
68-
Function *Func = P.getFunction(FuncDecl);
69-
if (!Func)
7070
Func =
7171
P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes),
7272
std::move(ParamDescriptors), HasThisPointer, HasRVO);
73+
}
74+
7375
assert(Func);
7476
if (!HasBody)
7577
return Func;

0 commit comments

Comments
 (0)