blob: 2ca865a74d38ee43e621e6f22e3864f767a991fa [file] [log] [blame]
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +00006//
7//===----------------------------------------------------------------------===//
8//
9// These classes wrap the information about a call or function
10// definition used to handle ABI compliancy.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
15#define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000016
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "CGValue.h"
Reid Kleckner23f4c4b2013-06-21 12:45:15 +000018#include "EHScopeStack.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/CanonicalType.h"
Peter Collingbourneea211002018-02-05 23:09:13 +000020#include "clang/AST/GlobalDecl.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/Type.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000022#include "llvm/IR/Value.h"
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000023
Daniel Dunbar313321e2009-02-03 05:31:23 +000024// FIXME: Restructure so we don't have to expose so much stuff.
25#include "ABIInfo.h"
26
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000027namespace llvm {
Reid Klecknerde864822017-03-21 16:57:30 +000028class AttributeList;
29class Function;
30class Type;
31class Value;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000032}
33
34namespace clang {
35 class ASTContext;
36 class Decl;
37 class FunctionDecl;
38 class ObjCMethodDecl;
Daniel Dunbarbc915f42008-09-09 23:14:03 +000039 class VarDecl;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000040
41namespace CodeGen {
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000042
Reid Klecknerde864822017-03-21 16:57:30 +000043/// Abstract information about a function or function prototype.
44class CGCalleeInfo {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000045 /// The function prototype of the callee.
Reid Klecknerde864822017-03-21 16:57:30 +000046 const FunctionProtoType *CalleeProtoTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000047 /// The function declaration of the callee.
Erich Keanede6480a32018-11-13 15:48:08 +000048 GlobalDecl CalleeDecl;
John McCallb92ab1a2016-10-26 23:46:34 +000049
Reid Klecknerde864822017-03-21 16:57:30 +000050public:
Erich Keanede6480a32018-11-13 15:48:08 +000051 explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl() {}
52 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
Reid Klecknerde864822017-03-21 16:57:30 +000053 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
54 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
Erich Keanede6480a32018-11-13 15:48:08 +000055 : CalleeProtoTy(calleeProtoTy), CalleeDecl() {}
56 CGCalleeInfo(GlobalDecl calleeDecl)
Reid Klecknerde864822017-03-21 16:57:30 +000057 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
John McCallb92ab1a2016-10-26 23:46:34 +000058
Reid Klecknerde864822017-03-21 16:57:30 +000059 const FunctionProtoType *getCalleeFunctionProtoType() const {
60 return CalleeProtoTy;
61 }
Erich Keanede6480a32018-11-13 15:48:08 +000062 const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
John McCallb92ab1a2016-10-26 23:46:34 +000063 };
64
65 /// All available information about a concrete callee.
66 class CGCallee {
67 enum class SpecialKind : uintptr_t {
68 Invalid,
69 Builtin,
70 PseudoDestructor,
Peter Collingbourneea211002018-02-05 23:09:13 +000071 Virtual,
John McCallb92ab1a2016-10-26 23:46:34 +000072
Peter Collingbourneea211002018-02-05 23:09:13 +000073 Last = Virtual
John McCallb92ab1a2016-10-26 23:46:34 +000074 };
75
John McCallaaae3022016-11-07 21:13:27 +000076 struct BuiltinInfoStorage {
77 const FunctionDecl *Decl;
78 unsigned ID;
79 };
80 struct PseudoDestructorInfoStorage {
81 const CXXPseudoDestructorExpr *Expr;
82 };
Peter Collingbourneea211002018-02-05 23:09:13 +000083 struct VirtualInfoStorage {
84 const CallExpr *CE;
85 GlobalDecl MD;
86 Address Addr;
87 llvm::FunctionType *FTy;
88 };
John McCallaaae3022016-11-07 21:13:27 +000089
John McCallb92ab1a2016-10-26 23:46:34 +000090 SpecialKind KindOrFunctionPointer;
91 union {
92 CGCalleeInfo AbstractInfo;
John McCallaaae3022016-11-07 21:13:27 +000093 BuiltinInfoStorage BuiltinInfo;
94 PseudoDestructorInfoStorage PseudoDestructorInfo;
Peter Collingbourneea211002018-02-05 23:09:13 +000095 VirtualInfoStorage VirtualInfo;
John McCallb92ab1a2016-10-26 23:46:34 +000096 };
97
98 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
99
100 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
101 : KindOrFunctionPointer(SpecialKind::Builtin) {
102 BuiltinInfo.Decl = builtinDecl;
103 BuiltinInfo.ID = builtinID;
104 }
105
106 public:
107 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
108
109 /// Construct a callee. Call this constructor directly when this
110 /// isn't a direct call.
111 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
112 : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
113 AbstractInfo = abstractInfo;
114 assert(functionPtr && "configuring callee without function pointer");
115 assert(functionPtr->getType()->isPointerTy());
116 assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
117 }
118
119 static CGCallee forBuiltin(unsigned builtinID,
120 const FunctionDecl *builtinDecl) {
121 CGCallee result(SpecialKind::Builtin);
122 result.BuiltinInfo.Decl = builtinDecl;
123 result.BuiltinInfo.ID = builtinID;
124 return result;
125 }
126
127 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
128 CGCallee result(SpecialKind::PseudoDestructor);
129 result.PseudoDestructorInfo.Expr = E;
130 return result;
131 }
132
133 static CGCallee forDirect(llvm::Constant *functionPtr,
134 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
135 return CGCallee(abstractInfo, functionPtr);
136 }
137
Peter Collingbourneea211002018-02-05 23:09:13 +0000138 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;
146 }
147
John McCallb92ab1a2016-10-26 23:46:34 +0000148 bool isBuiltin() const {
149 return KindOrFunctionPointer == SpecialKind::Builtin;
150 }
151 const FunctionDecl *getBuiltinDecl() const {
152 assert(isBuiltin());
153 return BuiltinInfo.Decl;
154 }
155 unsigned getBuiltinID() const {
156 assert(isBuiltin());
157 return BuiltinInfo.ID;
158 }
159
160 bool isPseudoDestructor() const {
161 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
162 }
163 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
164 assert(isPseudoDestructor());
165 return PseudoDestructorInfo.Expr;
166 }
167
168 bool isOrdinary() const {
169 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
170 }
Peter Collingbourneea211002018-02-05 23:09:13 +0000171 CGCalleeInfo getAbstractInfo() const {
172 if (isVirtual())
Erich Keanede6480a32018-11-13 15:48:08 +0000173 return VirtualInfo.MD;
John McCallb92ab1a2016-10-26 23:46:34 +0000174 assert(isOrdinary());
175 return AbstractInfo;
176 }
177 llvm::Value *getFunctionPointer() const {
178 assert(isOrdinary());
179 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
180 }
John McCallb92ab1a2016-10-26 23:46:34 +0000181 void setFunctionPointer(llvm::Value *functionPtr) {
182 assert(isOrdinary());
183 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
184 }
Peter Collingbourneea211002018-02-05 23:09:13 +0000185
186 bool isVirtual() const {
187 return KindOrFunctionPointer == SpecialKind::Virtual;
188 }
189 const CallExpr *getVirtualCallExpr() const {
190 assert(isVirtual());
191 return VirtualInfo.CE;
192 }
193 GlobalDecl getVirtualMethodDecl() const {
194 assert(isVirtual());
195 return VirtualInfo.MD;
196 }
197 Address getThisAddress() const {
198 assert(isVirtual());
199 return VirtualInfo.Addr;
200 }
201
202 llvm::FunctionType *getFunctionType() const {
203 if (isVirtual())
204 return VirtualInfo.FTy;
205 return cast<llvm::FunctionType>(
206 getFunctionPointer()->getType()->getPointerElementType());
207 }
John McCall9831b842018-02-06 18:52:44 +0000208
209 /// If this is a delayed callee computation of some sort, prepare
210 /// a concrete callee.
211 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
John McCallb92ab1a2016-10-26 23:46:34 +0000212 };
213
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000214 struct CallArg {
Yaxun Liu5b330e82018-03-15 15:25:19 +0000215 private:
216 union {
217 RValue RV;
218 LValue LV; /// The argument is semantically a load from this l-value.
219 };
220 bool HasLV;
221
222 /// A data-flow flag to make sure getRValue and/or copyInto are not
223 /// called twice for duplicated IR emission.
224 mutable bool IsUsed;
225
226 public:
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000227 QualType Ty;
Yaxun Liu5b330e82018-03-15 15:25:19 +0000228 CallArg(RValue rv, QualType ty)
229 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
230 CallArg(LValue lv, QualType ty)
231 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
232 bool hasLValue() const { return HasLV; }
233 QualType getType() const { return Ty; }
234
235 /// \returns an independent RValue. If the CallArg contains an LValue,
236 /// a temporary copy is returned.
237 RValue getRValue(CodeGenFunction &CGF) const;
238
239 LValue getKnownLValue() const {
240 assert(HasLV && !IsUsed);
241 return LV;
242 }
243 RValue getKnownRValue() const {
244 assert(!HasLV && !IsUsed);
245 return RV;
246 }
247 void setRValue(RValue _RV) {
248 assert(!HasLV);
249 RV = _RV;
250 }
251
252 bool isAggregate() const { return HasLV || RV.isAggregate(); }
253
254 void copyInto(CodeGenFunction &CGF, Address A) const;
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000255 };
256
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000257 /// CallArgList - Type for representing both the value and type of
258 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +0000259 class CallArgList :
Yaxun Liud9389822018-03-14 15:02:28 +0000260 public SmallVector<CallArg, 8> {
John McCall32ea9692011-03-11 20:59:21 +0000261 public:
Reid Kleckner7c2f9e82015-10-08 00:17:45 +0000262 CallArgList() : StackBase(nullptr) {}
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000263
John McCall31168b02011-06-15 23:02:42 +0000264 struct Writeback {
John McCalleff18842013-03-23 02:35:54 +0000265 /// The original argument. Note that the argument l-value
266 /// is potentially null.
267 LValue Source;
John McCall31168b02011-06-15 23:02:42 +0000268
269 /// The temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +0000270 Address Temporary;
John McCalleff18842013-03-23 02:35:54 +0000271
272 /// A value to "use" after the writeback, or null.
273 llvm::Value *ToUse;
John McCall31168b02011-06-15 23:02:42 +0000274 };
275
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000276 struct CallArgCleanup {
277 EHScopeStack::stable_iterator Cleanup;
278
279 /// The "is active" insertion point. This instruction is temporary and
280 /// will be removed after insertion.
281 llvm::Instruction *IsActiveIP;
282 };
283
Yaxun Liu5b330e82018-03-15 15:25:19 +0000284 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
285
286 void addUncopiedAggregate(LValue LV, QualType type) {
287 push_back(CallArg(LV, type));
John McCall32ea9692011-03-11 20:59:21 +0000288 }
John McCall31168b02011-06-15 23:02:42 +0000289
Richard Smith762672a2016-09-28 19:09:10 +0000290 /// Add all the arguments from another CallArgList to this one. After doing
291 /// this, the old CallArgList retains its list of arguments, but must not
292 /// be used to emit a call.
John McCall31168b02011-06-15 23:02:42 +0000293 void addFrom(const CallArgList &other) {
294 insert(end(), other.begin(), other.end());
295 Writebacks.insert(Writebacks.end(),
296 other.Writebacks.begin(), other.Writebacks.end());
Richard Smith762672a2016-09-28 19:09:10 +0000297 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
298 other.CleanupsToDeactivate.begin(),
299 other.CleanupsToDeactivate.end());
300 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
301 if (!StackBase)
302 StackBase = other.StackBase;
John McCall31168b02011-06-15 23:02:42 +0000303 }
304
John McCall7f416cc2015-09-08 08:05:57 +0000305 void addWriteback(LValue srcLV, Address temporary,
John McCalleff18842013-03-23 02:35:54 +0000306 llvm::Value *toUse) {
John McCall7f416cc2015-09-08 08:05:57 +0000307 Writeback writeback = { srcLV, temporary, toUse };
John McCall31168b02011-06-15 23:02:42 +0000308 Writebacks.push_back(writeback);
309 }
310
311 bool hasWritebacks() const { return !Writebacks.empty(); }
312
Aaron Ballman36a7fa82014-03-17 17:22:27 +0000313 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
314 writeback_const_range;
315
316 writeback_const_range writebacks() const {
317 return writeback_const_range(Writebacks.begin(), Writebacks.end());
318 }
John McCall31168b02011-06-15 23:02:42 +0000319
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000320 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
321 llvm::Instruction *IsActiveIP) {
322 CallArgCleanup ArgCleanup;
323 ArgCleanup.Cleanup = Cleanup;
324 ArgCleanup.IsActiveIP = IsActiveIP;
325 CleanupsToDeactivate.push_back(ArgCleanup);
326 }
327
328 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
329 return CleanupsToDeactivate;
330 }
331
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000332 void allocateArgumentMemory(CodeGenFunction &CGF);
333 llvm::Instruction *getStackBase() const { return StackBase; }
Nico Weber8cdb3f92015-08-25 18:43:32 +0000334 void freeArgumentMemory(CodeGenFunction &CGF) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000335
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000336 /// Returns if we're using an inalloca struct to pass arguments in
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000337 /// memory.
338 bool isUsingInAlloca() const { return StackBase; }
339
John McCall31168b02011-06-15 23:02:42 +0000340 private:
Chris Lattner01cf8db2011-07-20 06:58:45 +0000341 SmallVector<Writeback, 1> Writebacks;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000342
343 /// Deactivate these cleanups immediately before making the call. This
344 /// is used to cleanup objects that are owned by the callee once the call
345 /// occurs.
346 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000347
348 /// The stacksave call. It dominates all of the argument evaluation.
349 llvm::CallInst *StackBase;
John McCalla738c252011-03-09 04:27:21 +0000350 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000351
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000352 /// FunctionArgList - Type for representing both the decl and type
353 /// of parameters to a function. The decl must be either a
354 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000355 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalla738c252011-03-09 04:27:21 +0000356 };
Mike Stump11289f42009-09-09 15:08:12 +0000357
Fangrui Song6907ce22018-07-30 19:24:48 +0000358 /// ReturnValueSlot - Contains the address where the return value of a
Anders Carlsson17490832009-12-24 20:40:36 +0000359 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000360 class ReturnValueSlot {
Leny Kholodov6aab1112015-06-08 10:23:49 +0000361 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
John McCall7f416cc2015-09-08 08:05:57 +0000362 CharUnits Alignment;
Leny Kholodov6aab1112015-06-08 10:23:49 +0000363
364 // Return value slot flags
365 enum Flags {
366 IS_VOLATILE = 0x1,
367 IS_UNUSED = 0x2,
368 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000369
370 public:
371 ReturnValueSlot() {}
John McCall7f416cc2015-09-08 08:05:57 +0000372 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
373 : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
374 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
375 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
Anders Carlsson0435ed52009-12-24 19:08:58 +0000376
John McCall7f416cc2015-09-08 08:05:57 +0000377 bool isNull() const { return !getValue().isValid(); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000378
379 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
John McCall7f416cc2015-09-08 08:05:57 +0000380 Address getValue() const { return Address(Value.getPointer(), Alignment); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000381 bool isUnused() const { return Value.getInt() & IS_UNUSED; }
Anders Carlsson0435ed52009-12-24 19:08:58 +0000382 };
Fangrui Song6907ce22018-07-30 19:24:48 +0000383
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000384} // end namespace CodeGen
385} // end namespace clang
386
387#endif