Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit a896e9a

Browse files
committed
Refactor call emission to package the function pointer together with
abstract information about the callee. NFC. The goal here is to make it easier to recognize indirect calls and trigger additional logic in certain cases. That logic will come in a later patch; in the meantime, I felt that this was a significant improvement to the code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@285258 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9b09301 commit a896e9a

26 files changed

+634
-355
lines changed

Diff for: include/clang/AST/Expr.h

+5
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ class Expr : public Stmt {
445445
return const_cast<Expr*>(this)->getSourceBitField();
446446
}
447447

448+
Decl *getReferencedDeclOfCallee();
449+
const Decl *getReferencedDeclOfCallee() const {
450+
return const_cast<Expr*>(this)->getReferencedDeclOfCallee();
451+
}
452+
448453
/// \brief If this expression is an l-value for an Objective C
449454
/// property, find the underlying property reference expression.
450455
const ObjCPropertyRefExpr *getObjCProperty() const;

Diff for: include/clang/CodeGen/CGFunctionInfo.h

-23
Original file line numberDiff line numberDiff line change
@@ -664,29 +664,6 @@ class CGFunctionInfo final
664664
}
665665
};
666666

667-
/// CGCalleeInfo - Class to encapsulate the information about a callee to be
668-
/// used during the generation of call/invoke instructions.
669-
class CGCalleeInfo {
670-
/// \brief The function proto type of the callee.
671-
const FunctionProtoType *CalleeProtoTy;
672-
/// \brief The function declaration of the callee.
673-
const Decl *CalleeDecl;
674-
675-
public:
676-
explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
677-
CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
678-
: CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
679-
CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
680-
: CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
681-
CGCalleeInfo(const Decl *calleeDecl)
682-
: CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
683-
684-
const FunctionProtoType *getCalleeFunctionProtoType() {
685-
return CalleeProtoTy;
686-
}
687-
const Decl *getCalleeDecl() { return CalleeDecl; }
688-
};
689-
690667
} // end namespace CodeGen
691668
} // end namespace clang
692669

Diff for: lib/AST/Expr.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,16 @@ void CallExpr::updateDependenciesFromArg(Expr *Arg) {
11811181
ExprBits.ContainsUnexpandedParameterPack = true;
11821182
}
11831183

1184+
FunctionDecl *CallExpr::getDirectCallee() {
1185+
return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
1186+
}
1187+
11841188
Decl *CallExpr::getCalleeDecl() {
1185-
Expr *CEE = getCallee()->IgnoreParenImpCasts();
1189+
return getCallee()->getReferencedDeclOfCallee();
1190+
}
1191+
1192+
Decl *Expr::getReferencedDeclOfCallee() {
1193+
Expr *CEE = IgnoreParenImpCasts();
11861194

11871195
while (SubstNonTypeTemplateParmExpr *NTTP
11881196
= dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
@@ -1205,10 +1213,6 @@ Decl *CallExpr::getCalleeDecl() {
12051213
return nullptr;
12061214
}
12071215

1208-
FunctionDecl *CallExpr::getDirectCallee() {
1209-
return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
1210-
}
1211-
12121216
/// setNumArgs - This changes the number of arguments present in this call.
12131217
/// Any orphaned expressions are deleted by this, and any new operands are set
12141218
/// to null.

Diff for: lib/CodeGen/CGAtomic.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ static RValue emitAtomicLibcall(CodeGenFunction &CGF,
307307
CGF.CGM.getTypes().arrangeBuiltinFunctionCall(resultType, args);
308308
llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
309309
llvm::Constant *fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
310-
return CGF.EmitCall(fnInfo, fn, ReturnValueSlot(), args);
310+
auto callee = CGCallee::forDirect(fn);
311+
return CGF.EmitCall(fnInfo, callee, ReturnValueSlot(), args);
311312
}
312313

313314
/// Does a store of the given IR type modify the full expected width?

Diff for: lib/CodeGen/CGBlocks.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -975,25 +975,24 @@ RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
975975
const BlockPointerType *BPT =
976976
E->getCallee()->getType()->getAs<BlockPointerType>();
977977

978-
llvm::Value *Callee = EmitScalarExpr(E->getCallee());
978+
llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee());
979979

980980
// Get a pointer to the generic block literal.
981981
llvm::Type *BlockLiteralTy =
982982
llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
983983

984984
// Bitcast the callee to a block literal.
985-
llvm::Value *BlockLiteral =
986-
Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
985+
BlockPtr = Builder.CreateBitCast(BlockPtr, BlockLiteralTy, "block.literal");
987986

988987
// Get the function pointer from the literal.
989988
llvm::Value *FuncPtr =
990-
Builder.CreateStructGEP(CGM.getGenericBlockLiteralType(), BlockLiteral, 3);
989+
Builder.CreateStructGEP(CGM.getGenericBlockLiteralType(), BlockPtr, 3);
991990

992-
BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy);
991+
BlockPtr = Builder.CreateBitCast(BlockPtr, VoidPtrTy);
993992

994993
// Add the block literal.
995994
CallArgList Args;
996-
Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy);
995+
Args.add(RValue::get(BlockPtr), getContext().VoidPtrTy);
997996

998997
QualType FnType = BPT->getPointeeType();
999998

@@ -1013,8 +1012,11 @@ RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
10131012
llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
10141013
Func = Builder.CreateBitCast(Func, BlockFTyPtr);
10151014

1015+
// Prepare the callee.
1016+
CGCallee Callee(CGCalleeInfo(), Func);
1017+
10161018
// And call the block.
1017-
return EmitCall(FnInfo, Func, ReturnValue, Args);
1019+
return EmitCall(FnInfo, Callee, ReturnValue, Args);
10181020
}
10191021

10201022
Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,

Diff for: lib/CodeGen/CGBuiltin.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ using namespace llvm;
3737

3838
/// getBuiltinLibFunction - Given a builtin id for a function like
3939
/// "__builtin_fabsf", return a Function* for "fabsf".
40-
llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
41-
unsigned BuiltinID) {
40+
llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
41+
unsigned BuiltinID) {
4242
assert(Context.BuiltinInfo.isLibFunction(BuiltinID));
4343

4444
// Get the name, skip over the __builtin_ prefix (if necessary).
@@ -304,10 +304,10 @@ static Value *EmitSignBit(CodeGenFunction &CGF, Value *V) {
304304
return CGF.Builder.CreateICmpSLT(V, Zero);
305305
}
306306

307-
static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *Fn,
308-
const CallExpr *E, llvm::Value *calleeValue) {
309-
return CGF.EmitCall(E->getCallee()->getType(), calleeValue, E,
310-
ReturnValueSlot(), Fn);
307+
static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
308+
const CallExpr *E, llvm::Constant *calleeValue) {
309+
CGCallee callee = CGCallee::forDirect(calleeValue, FD);
310+
return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
311311
}
312312

313313
/// \brief Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
@@ -1570,7 +1570,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
15701570
CGM.getTypes().arrangeBuiltinFunctionCall(E->getType(), Args);
15711571
llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
15721572
llvm::Constant *Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
1573-
return EmitCall(FuncInfo, Func, ReturnValueSlot(), Args);
1573+
return EmitCall(FuncInfo, CGCallee::forDirect(Func),
1574+
ReturnValueSlot(), Args);
15741575
}
15751576

15761577
case Builtin::BI__atomic_test_and_set: {
@@ -2085,8 +2086,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
20852086
const CallExpr *Call = cast<CallExpr>(E->getArg(0));
20862087
const Expr *Chain = E->getArg(1);
20872088
return EmitCall(Call->getCallee()->getType(),
2088-
EmitScalarExpr(Call->getCallee()), Call, ReturnValue,
2089-
Call->getCalleeDecl(), EmitScalarExpr(Chain));
2089+
EmitCallee(Call->getCallee()), Call, ReturnValue,
2090+
EmitScalarExpr(Chain));
20902091
}
20912092
case Builtin::BI_InterlockedExchange8:
20922093
case Builtin::BI_InterlockedExchange16:
@@ -2692,7 +2693,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
26922693
// If this is a predefined lib function (e.g. malloc), emit the call
26932694
// using exactly the normal call path.
26942695
if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
2695-
return emitLibraryCall(*this, FD, E, EmitScalarExpr(E->getCallee()));
2696+
return emitLibraryCall(*this, FD, E,
2697+
cast<llvm::Constant>(EmitScalarExpr(E->getCallee())));
26962698

26972699
// Check that a call to a target specific builtin has the correct target
26982700
// features.

Diff for: lib/CodeGen/CGCUDARuntime.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,7 @@ RValue CGCUDARuntime::EmitCUDAKernelCallExpr(CodeGenFunction &CGF,
3636

3737
eval.begin(CGF);
3838
CGF.EmitBlock(ConfigOKBlock);
39-
40-
const Decl *TargetDecl = nullptr;
41-
if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
42-
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
43-
TargetDecl = DRE->getDecl();
44-
}
45-
}
46-
47-
llvm::Value *Callee = CGF.EmitScalarExpr(E->getCallee());
48-
CGF.EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue, TargetDecl);
39+
CGF.EmitSimpleCallExpr(E, ReturnValue);
4940
CGF.EmitBranch(ContBlock);
5041

5142
CGF.EmitBlock(ContBlock);

Diff for: lib/CodeGen/CGCXX.cpp

+18-21
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ llvm::Constant *CodeGenModule::getAddrOfCXXStructor(
258258
/*isThunk=*/false, /*ExtraAttrs=*/llvm::AttributeSet(), IsForDefinition);
259259
}
260260

261-
static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
262-
GlobalDecl GD,
263-
llvm::Type *Ty,
264-
const CXXRecordDecl *RD) {
261+
static CGCallee BuildAppleKextVirtualCall(CodeGenFunction &CGF,
262+
GlobalDecl GD,
263+
llvm::Type *Ty,
264+
const CXXRecordDecl *RD) {
265265
assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
266266
"No kext in Microsoft ABI");
267267
GD = GD.getCanonicalDecl();
@@ -277,16 +277,19 @@ static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
277277
VTableIndex += AddressPoint;
278278
llvm::Value *VFuncPtr =
279279
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
280-
return CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.PointerAlignInBytes);
280+
llvm::Value *VFunc =
281+
CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.PointerAlignInBytes);
282+
CGCallee Callee(GD.getDecl(), VFunc);
283+
return Callee;
281284
}
282285

283286
/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
284287
/// indirect call to virtual functions. It makes the call through indexing
285288
/// into the vtable.
286-
llvm::Value *
289+
CGCallee
287290
CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
288-
NestedNameSpecifier *Qual,
289-
llvm::Type *Ty) {
291+
NestedNameSpecifier *Qual,
292+
llvm::Type *Ty) {
290293
assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
291294
"BuildAppleKextVirtualCall - bad Qual kind");
292295

@@ -304,21 +307,15 @@ CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
304307

305308
/// BuildVirtualCall - This routine makes indirect vtable call for
306309
/// call to virtual destructors. It returns 0 if it could not do it.
307-
llvm::Value *
310+
CGCallee
308311
CodeGenFunction::BuildAppleKextVirtualDestructorCall(
309312
const CXXDestructorDecl *DD,
310313
CXXDtorType Type,
311314
const CXXRecordDecl *RD) {
312-
const auto *MD = cast<CXXMethodDecl>(DD);
313-
// FIXME. Dtor_Base dtor is always direct!!
314-
// It need be somehow inline expanded into the caller.
315-
// -O does that. But need to support -O0 as well.
316-
if (MD->isVirtual() && Type != Dtor_Base) {
317-
// Compute the function type we're calling.
318-
const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration(
319-
DD, StructorType::Complete);
320-
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
321-
return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
322-
}
323-
return nullptr;
315+
assert(DD->isVirtual() && Type != Dtor_Base);
316+
// Compute the function type we're calling.
317+
const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration(
318+
DD, StructorType::Complete);
319+
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
320+
return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
324321
}

Diff for: lib/CodeGen/CGCXXABI.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ CGCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
7373
return CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
7474
}
7575

76-
llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(
76+
CGCallee CGCXXABI::EmitLoadOfMemberFunctionPointer(
7777
CodeGenFunction &CGF, const Expr *E, Address This,
7878
llvm::Value *&ThisPtrForCall,
7979
llvm::Value *MemPtr, const MemberPointerType *MPT) {
@@ -86,7 +86,8 @@ llvm::Value *CGCXXABI::EmitLoadOfMemberFunctionPointer(
8686
cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
8787
llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(
8888
CGM.getTypes().arrangeCXXMethodType(RD, FPT, /*FD=*/nullptr));
89-
return llvm::Constant::getNullValue(FTy->getPointerTo());
89+
llvm::Constant *FnPtr = llvm::Constant::getNullValue(FTy->getPointerTo());
90+
return CGCallee::forDirect(FnPtr, FPT);
9091
}
9192

9293
llvm::Value *

Diff for: lib/CodeGen/CGCXXABI.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class FieldDecl;
3535
class MangleContext;
3636

3737
namespace CodeGen {
38+
class CGCallee;
3839
class CodeGenFunction;
3940
class CodeGenModule;
4041
struct CatchTypeInfo;
@@ -154,7 +155,7 @@ class CGCXXABI {
154155
/// Load a member function from an object and a member function
155156
/// pointer. Apply the this-adjustment and set 'This' to the
156157
/// adjusted value.
157-
virtual llvm::Value *EmitLoadOfMemberFunctionPointer(
158+
virtual CGCallee EmitLoadOfMemberFunctionPointer(
158159
CodeGenFunction &CGF, const Expr *E, Address This,
159160
llvm::Value *&ThisPtrForCall, llvm::Value *MemPtr,
160161
const MemberPointerType *MPT);
@@ -403,11 +404,11 @@ class CGCXXABI {
403404
CharUnits VPtrOffset) = 0;
404405

405406
/// Build a virtual function pointer in the ABI-specific way.
406-
virtual llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF,
407-
GlobalDecl GD,
408-
Address This,
409-
llvm::Type *Ty,
410-
SourceLocation Loc) = 0;
407+
virtual CGCallee getVirtualFunctionPointer(CodeGenFunction &CGF,
408+
GlobalDecl GD,
409+
Address This,
410+
llvm::Type *Ty,
411+
SourceLocation Loc) = 0;
411412

412413
/// Emit the ABI-specific virtual destructor call.
413414
virtual llvm::Value *

0 commit comments

Comments
 (0)