1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
18 #include "EHScopeStack.h"
19 #include "clang/AST/ASTFwd.h"
20 #include "clang/AST/CanonicalType.h"
21 #include "clang/AST/GlobalDecl.h"
22 #include "clang/AST/Type.h"
23 #include "llvm/IR/Value.h"
25 // FIXME: Restructure so we don't have to expose so much stuff.
44 /// Abstract information about a function or function prototype.
46 /// The function prototype of the callee.
47 const FunctionProtoType *CalleeProtoTy;
48 /// The function declaration of the callee.
49 GlobalDecl CalleeDecl;
52 explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl() {}
53 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
54 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
55 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
56 : CalleeProtoTy(calleeProtoTy), CalleeDecl() {}
57 CGCalleeInfo(GlobalDecl calleeDecl)
58 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
60 const FunctionProtoType *getCalleeFunctionProtoType() const {
63 const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
66 /// All available information about a concrete callee.
68 enum class SpecialKind : uintptr_t {
77 struct BuiltinInfoStorage {
78 const FunctionDecl *Decl;
81 struct PseudoDestructorInfoStorage {
82 const CXXPseudoDestructorExpr *Expr;
84 struct VirtualInfoStorage {
88 llvm::FunctionType *FTy;
91 SpecialKind KindOrFunctionPointer;
93 CGCalleeInfo AbstractInfo;
94 BuiltinInfoStorage BuiltinInfo;
95 PseudoDestructorInfoStorage PseudoDestructorInfo;
96 VirtualInfoStorage VirtualInfo;
99 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
101 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
102 : KindOrFunctionPointer(SpecialKind::Builtin) {
103 BuiltinInfo.Decl = builtinDecl;
104 BuiltinInfo.ID = builtinID;
108 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
110 /// Construct a callee. Call this constructor directly when this
111 /// isn't a direct call.
112 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
113 : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
114 AbstractInfo = abstractInfo;
115 assert(functionPtr && "configuring callee without function pointer");
116 assert(functionPtr->getType()->isPointerTy());
117 assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
120 static CGCallee forBuiltin(unsigned builtinID,
121 const FunctionDecl *builtinDecl) {
122 CGCallee result(SpecialKind::Builtin);
123 result.BuiltinInfo.Decl = builtinDecl;
124 result.BuiltinInfo.ID = builtinID;
128 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
129 CGCallee result(SpecialKind::PseudoDestructor);
130 result.PseudoDestructorInfo.Expr = E;
134 static CGCallee forDirect(llvm::Constant *functionPtr,
135 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
136 return CGCallee(abstractInfo, functionPtr);
139 static CGCallee forDirect(llvm::FunctionCallee functionPtr,
140 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
141 return CGCallee(abstractInfo, functionPtr.getCallee());
144 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
145 llvm::FunctionType *FTy) {
146 CGCallee result(SpecialKind::Virtual);
147 result.VirtualInfo.CE = CE;
148 result.VirtualInfo.MD = MD;
149 result.VirtualInfo.Addr = Addr;
150 result.VirtualInfo.FTy = FTy;
154 bool isBuiltin() const {
155 return KindOrFunctionPointer == SpecialKind::Builtin;
157 const FunctionDecl *getBuiltinDecl() const {
159 return BuiltinInfo.Decl;
161 unsigned getBuiltinID() const {
163 return BuiltinInfo.ID;
166 bool isPseudoDestructor() const {
167 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
169 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
170 assert(isPseudoDestructor());
171 return PseudoDestructorInfo.Expr;
174 bool isOrdinary() const {
175 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
177 CGCalleeInfo getAbstractInfo() const {
179 return VirtualInfo.MD;
180 assert(isOrdinary());
183 llvm::Value *getFunctionPointer() const {
184 assert(isOrdinary());
185 return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
187 void setFunctionPointer(llvm::Value *functionPtr) {
188 assert(isOrdinary());
189 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
192 bool isVirtual() const {
193 return KindOrFunctionPointer == SpecialKind::Virtual;
195 const CallExpr *getVirtualCallExpr() const {
197 return VirtualInfo.CE;
199 GlobalDecl getVirtualMethodDecl() const {
201 return VirtualInfo.MD;
203 Address getThisAddress() const {
205 return VirtualInfo.Addr;
207 llvm::FunctionType *getVirtualFunctionType() const {
209 return VirtualInfo.FTy;
212 /// If this is a delayed callee computation of some sort, prepare
213 /// a concrete callee.
214 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
221 LValue LV; /// The argument is semantically a load from this l-value.
225 /// A data-flow flag to make sure getRValue and/or copyInto are not
226 /// called twice for duplicated IR emission.
231 CallArg(RValue rv, QualType ty)
232 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
233 CallArg(LValue lv, QualType ty)
234 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
235 bool hasLValue() const { return HasLV; }
236 QualType getType() const { return Ty; }
238 /// \returns an independent RValue. If the CallArg contains an LValue,
239 /// a temporary copy is returned.
240 RValue getRValue(CodeGenFunction &CGF) const;
242 LValue getKnownLValue() const {
243 assert(HasLV && !IsUsed);
246 RValue getKnownRValue() const {
247 assert(!HasLV && !IsUsed);
250 void setRValue(RValue _RV) {
255 bool isAggregate() const { return HasLV || RV.isAggregate(); }
257 void copyInto(CodeGenFunction &CGF, Address A) const;
260 /// CallArgList - Type for representing both the value and type of
261 /// arguments in a call.
262 class CallArgList : public SmallVector<CallArg, 8> {
264 CallArgList() : StackBase(nullptr) {}
267 /// The original argument. Note that the argument l-value
268 /// is potentially null.
271 /// The temporary alloca.
274 /// A value to "use" after the writeback, or null.
278 struct CallArgCleanup {
279 EHScopeStack::stable_iterator Cleanup;
281 /// The "is active" insertion point. This instruction is temporary and
282 /// will be removed after insertion.
283 llvm::Instruction *IsActiveIP;
286 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
288 void addUncopiedAggregate(LValue LV, QualType type) {
289 push_back(CallArg(LV, type));
292 /// Add all the arguments from another CallArgList to this one. After doing
293 /// this, the old CallArgList retains its list of arguments, but must not
294 /// be used to emit a call.
295 void addFrom(const CallArgList &other) {
296 insert(end(), other.begin(), other.end());
297 Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
298 other.Writebacks.end());
299 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
300 other.CleanupsToDeactivate.begin(),
301 other.CleanupsToDeactivate.end());
302 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
304 StackBase = other.StackBase;
307 void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
308 Writeback writeback = {srcLV, temporary, toUse};
309 Writebacks.push_back(writeback);
312 bool hasWritebacks() const { return !Writebacks.empty(); }
314 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
315 writeback_const_range;
317 writeback_const_range writebacks() const {
318 return writeback_const_range(Writebacks.begin(), Writebacks.end());
321 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
322 llvm::Instruction *IsActiveIP) {
323 CallArgCleanup ArgCleanup;
324 ArgCleanup.Cleanup = Cleanup;
325 ArgCleanup.IsActiveIP = IsActiveIP;
326 CleanupsToDeactivate.push_back(ArgCleanup);
329 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
330 return CleanupsToDeactivate;
333 void allocateArgumentMemory(CodeGenFunction &CGF);
334 llvm::Instruction *getStackBase() const { return StackBase; }
335 void freeArgumentMemory(CodeGenFunction &CGF) const;
337 /// Returns if we're using an inalloca struct to pass arguments in
339 bool isUsingInAlloca() const { return StackBase; }
342 SmallVector<Writeback, 1> Writebacks;
344 /// Deactivate these cleanups immediately before making the call. This
345 /// is used to cleanup objects that are owned by the callee once the call
347 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
349 /// The stacksave call. It dominates all of the argument evaluation.
350 llvm::CallInst *StackBase;
353 /// FunctionArgList - Type for representing both the decl and type
354 /// of parameters to a function. The decl must be either a
355 /// ParmVarDecl or ImplicitParamDecl.
356 class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
358 /// ReturnValueSlot - Contains the address where the return value of a
359 /// function can be stored, and whether the address is volatile or not.
360 class ReturnValueSlot {
361 Address Addr = Address::invalid();
363 // Return value slot flags
364 unsigned IsVolatile : 1;
365 unsigned IsUnused : 1;
366 unsigned IsExternallyDestructed : 1;
370 : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
371 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
372 bool IsExternallyDestructed = false)
373 : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
374 IsExternallyDestructed(IsExternallyDestructed) {}
376 bool isNull() const { return !Addr.isValid(); }
377 bool isVolatile() const { return IsVolatile; }
378 Address getValue() const { return Addr; }
379 bool isUnused() const { return IsUnused; }
380 bool isExternallyDestructed() const { return IsExternallyDestructed; }
383 } // end namespace CodeGen
384 } // end namespace clang