[lld][WebAssembly] Add `--table-base` setting
[llvm-project.git] / clang / lib / CodeGen / CGCall.h
blob75c4dcc400caf0c7b0dbba592b3dafbc313eb3c7
1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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
17 #include "CGValue.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 namespace llvm {
26 class Type;
27 class Value;
28 } // namespace llvm
30 namespace clang {
31 class Decl;
32 class FunctionDecl;
33 class TargetOptions;
34 class VarDecl;
36 namespace CodeGen {
38 /// Abstract information about a function or function prototype.
39 class CGCalleeInfo {
40 /// The function prototype of the callee.
41 const FunctionProtoType *CalleeProtoTy;
42 /// The function declaration of the callee.
43 GlobalDecl CalleeDecl;
45 public:
46 explicit CGCalleeInfo() : CalleeProtoTy(nullptr) {}
47 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
48 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
49 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
50 : CalleeProtoTy(calleeProtoTy) {}
51 CGCalleeInfo(GlobalDecl calleeDecl)
52 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
54 const FunctionProtoType *getCalleeFunctionProtoType() const {
55 return CalleeProtoTy;
57 const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
60 /// All available information about a concrete callee.
61 class CGCallee {
62 enum class SpecialKind : uintptr_t {
63 Invalid,
64 Builtin,
65 PseudoDestructor,
66 Virtual,
68 Last = Virtual
71 struct BuiltinInfoStorage {
72 const FunctionDecl *Decl;
73 unsigned ID;
75 struct PseudoDestructorInfoStorage {
76 const CXXPseudoDestructorExpr *Expr;
78 struct VirtualInfoStorage {
79 const CallExpr *CE;
80 GlobalDecl MD;
81 Address Addr;
82 llvm::FunctionType *FTy;
85 SpecialKind KindOrFunctionPointer;
86 union {
87 CGCalleeInfo AbstractInfo;
88 BuiltinInfoStorage BuiltinInfo;
89 PseudoDestructorInfoStorage PseudoDestructorInfo;
90 VirtualInfoStorage VirtualInfo;
93 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
95 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
96 : KindOrFunctionPointer(SpecialKind::Builtin) {
97 BuiltinInfo.Decl = builtinDecl;
98 BuiltinInfo.ID = builtinID;
101 public:
102 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
104 /// Construct a callee. Call this constructor directly when this
105 /// isn't a direct call.
106 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
107 : KindOrFunctionPointer(
108 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
109 AbstractInfo = abstractInfo;
110 assert(functionPtr && "configuring callee without function pointer");
111 assert(functionPtr->getType()->isPointerTy());
114 static CGCallee forBuiltin(unsigned builtinID,
115 const FunctionDecl *builtinDecl) {
116 CGCallee result(SpecialKind::Builtin);
117 result.BuiltinInfo.Decl = builtinDecl;
118 result.BuiltinInfo.ID = builtinID;
119 return result;
122 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
123 CGCallee result(SpecialKind::PseudoDestructor);
124 result.PseudoDestructorInfo.Expr = E;
125 return result;
128 static CGCallee forDirect(llvm::Constant *functionPtr,
129 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
130 return CGCallee(abstractInfo, functionPtr);
133 static CGCallee forDirect(llvm::FunctionCallee functionPtr,
134 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
135 return CGCallee(abstractInfo, functionPtr.getCallee());
138 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
139 llvm::FunctionType *FTy) {
140 CGCallee result(SpecialKind::Virtual);
141 result.VirtualInfo.CE = CE;
142 result.VirtualInfo.MD = MD;
143 result.VirtualInfo.Addr = Addr;
144 result.VirtualInfo.FTy = FTy;
145 return result;
148 bool isBuiltin() const {
149 return KindOrFunctionPointer == SpecialKind::Builtin;
151 const FunctionDecl *getBuiltinDecl() const {
152 assert(isBuiltin());
153 return BuiltinInfo.Decl;
155 unsigned getBuiltinID() const {
156 assert(isBuiltin());
157 return BuiltinInfo.ID;
160 bool isPseudoDestructor() const {
161 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
163 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
164 assert(isPseudoDestructor());
165 return PseudoDestructorInfo.Expr;
168 bool isOrdinary() const {
169 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
171 CGCalleeInfo getAbstractInfo() const {
172 if (isVirtual())
173 return VirtualInfo.MD;
174 assert(isOrdinary());
175 return AbstractInfo;
177 llvm::Value *getFunctionPointer() const {
178 assert(isOrdinary());
179 return reinterpret_cast<llvm::Value *>(uintptr_t(KindOrFunctionPointer));
181 void setFunctionPointer(llvm::Value *functionPtr) {
182 assert(isOrdinary());
183 KindOrFunctionPointer =
184 SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
187 bool isVirtual() const {
188 return KindOrFunctionPointer == SpecialKind::Virtual;
190 const CallExpr *getVirtualCallExpr() const {
191 assert(isVirtual());
192 return VirtualInfo.CE;
194 GlobalDecl getVirtualMethodDecl() const {
195 assert(isVirtual());
196 return VirtualInfo.MD;
198 Address getThisAddress() const {
199 assert(isVirtual());
200 return VirtualInfo.Addr;
202 llvm::FunctionType *getVirtualFunctionType() const {
203 assert(isVirtual());
204 return VirtualInfo.FTy;
207 /// If this is a delayed callee computation of some sort, prepare
208 /// a concrete callee.
209 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
212 struct CallArg {
213 private:
214 union {
215 RValue RV;
216 LValue LV; /// The argument is semantically a load from this l-value.
218 bool HasLV;
220 /// A data-flow flag to make sure getRValue and/or copyInto are not
221 /// called twice for duplicated IR emission.
222 mutable bool IsUsed;
224 public:
225 QualType Ty;
226 CallArg(RValue rv, QualType ty)
227 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
228 CallArg(LValue lv, QualType ty)
229 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
230 bool hasLValue() const { return HasLV; }
231 QualType getType() const { return Ty; }
233 /// \returns an independent RValue. If the CallArg contains an LValue,
234 /// a temporary copy is returned.
235 RValue getRValue(CodeGenFunction &CGF) const;
237 LValue getKnownLValue() const {
238 assert(HasLV && !IsUsed);
239 return LV;
241 RValue getKnownRValue() const {
242 assert(!HasLV && !IsUsed);
243 return RV;
245 void setRValue(RValue _RV) {
246 assert(!HasLV);
247 RV = _RV;
250 bool isAggregate() const { return HasLV || RV.isAggregate(); }
252 void copyInto(CodeGenFunction &CGF, Address A) const;
255 /// CallArgList - Type for representing both the value and type of
256 /// arguments in a call.
257 class CallArgList : public SmallVector<CallArg, 8> {
258 public:
259 CallArgList() = default;
261 struct Writeback {
262 /// The original argument. Note that the argument l-value
263 /// is potentially null.
264 LValue Source;
266 /// The temporary alloca.
267 Address Temporary;
269 /// A value to "use" after the writeback, or null.
270 llvm::Value *ToUse;
273 struct CallArgCleanup {
274 EHScopeStack::stable_iterator Cleanup;
276 /// The "is active" insertion point. This instruction is temporary and
277 /// will be removed after insertion.
278 llvm::Instruction *IsActiveIP;
281 struct EndLifetimeInfo {
282 llvm::Value *Addr;
283 llvm::Value *Size;
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 LifetimeCleanups.insert(LifetimeCleanups.end(),
303 other.LifetimeCleanups.begin(),
304 other.LifetimeCleanups.end());
305 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
306 if (!StackBase)
307 StackBase = other.StackBase;
310 void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
311 Writeback writeback = {srcLV, temporary, toUse};
312 Writebacks.push_back(writeback);
315 bool hasWritebacks() const { return !Writebacks.empty(); }
317 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
318 writeback_const_range;
320 writeback_const_range writebacks() const {
321 return writeback_const_range(Writebacks.begin(), Writebacks.end());
324 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
325 llvm::Instruction *IsActiveIP) {
326 CallArgCleanup ArgCleanup;
327 ArgCleanup.Cleanup = Cleanup;
328 ArgCleanup.IsActiveIP = IsActiveIP;
329 CleanupsToDeactivate.push_back(ArgCleanup);
332 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
333 return CleanupsToDeactivate;
336 void allocateArgumentMemory(CodeGenFunction &CGF);
337 llvm::Instruction *getStackBase() const { return StackBase; }
338 void freeArgumentMemory(CodeGenFunction &CGF) const;
340 /// Returns if we're using an inalloca struct to pass arguments in
341 /// memory.
342 bool isUsingInAlloca() const { return StackBase; }
344 void addLifetimeCleanup(EndLifetimeInfo Info) {
345 LifetimeCleanups.push_back(Info);
348 ArrayRef<EndLifetimeInfo> getLifetimeCleanups() const {
349 return LifetimeCleanups;
352 private:
353 SmallVector<Writeback, 1> Writebacks;
355 /// Deactivate these cleanups immediately before making the call. This
356 /// is used to cleanup objects that are owned by the callee once the call
357 /// occurs.
358 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
360 /// Lifetime information needed to call llvm.lifetime.end for any temporary
361 /// argument allocas.
362 SmallVector<EndLifetimeInfo, 2> LifetimeCleanups;
364 /// The stacksave call. It dominates all of the argument evaluation.
365 llvm::CallInst *StackBase = nullptr;
368 /// FunctionArgList - Type for representing both the decl and type
369 /// of parameters to a function. The decl must be either a
370 /// ParmVarDecl or ImplicitParamDecl.
371 class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
373 /// ReturnValueSlot - Contains the address where the return value of a
374 /// function can be stored, and whether the address is volatile or not.
375 class ReturnValueSlot {
376 Address Addr = Address::invalid();
378 // Return value slot flags
379 unsigned IsVolatile : 1;
380 unsigned IsUnused : 1;
381 unsigned IsExternallyDestructed : 1;
383 public:
384 ReturnValueSlot()
385 : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
386 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
387 bool IsExternallyDestructed = false)
388 : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
389 IsExternallyDestructed(IsExternallyDestructed) {}
391 bool isNull() const { return !Addr.isValid(); }
392 bool isVolatile() const { return IsVolatile; }
393 Address getValue() const { return Addr; }
394 bool isUnused() const { return IsUnused; }
395 bool isExternallyDestructed() const { return IsExternallyDestructed; }
398 /// Helper to add attributes to \p F according to the CodeGenOptions and
399 /// LangOptions without requiring a CodeGenModule to be constructed.
400 void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F,
401 const CodeGenOptions CodeGenOpts,
402 const LangOptions &LangOpts,
403 const TargetOptions &TargetOpts,
404 bool WillInternalize);
406 enum class FnInfoOpts {
407 None = 0,
408 IsInstanceMethod = 1 << 0,
409 IsChainCall = 1 << 1,
410 IsDelegateCall = 1 << 2,
413 inline FnInfoOpts operator|(FnInfoOpts A, FnInfoOpts B) {
414 return static_cast<FnInfoOpts>(
415 static_cast<std::underlying_type_t<FnInfoOpts>>(A) |
416 static_cast<std::underlying_type_t<FnInfoOpts>>(B));
419 inline FnInfoOpts operator&(FnInfoOpts A, FnInfoOpts B) {
420 return static_cast<FnInfoOpts>(
421 static_cast<std::underlying_type_t<FnInfoOpts>>(A) &
422 static_cast<std::underlying_type_t<FnInfoOpts>>(B));
425 inline FnInfoOpts operator|=(FnInfoOpts A, FnInfoOpts B) {
426 A = A | B;
427 return A;
430 inline FnInfoOpts operator&=(FnInfoOpts A, FnInfoOpts B) {
431 A = A & B;
432 return A;
435 } // end namespace CodeGen
436 } // end namespace clang
438 #endif