blob: 99a36e4e12f11c5d2159dfa37a0ea71f95f1d7f9 [file] [log] [blame]
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +00001//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16#define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000017
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "CGValue.h"
Reid Kleckner23f4c4b2013-06-21 12:45:15 +000019#include "EHScopeStack.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/CanonicalType.h"
Peter Collingbourneea211002018-02-05 23:09:13 +000021#include "clang/AST/GlobalDecl.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/AST/Type.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000023#include "llvm/IR/Value.h"
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000024
Daniel Dunbar313321e2009-02-03 05:31:23 +000025// FIXME: Restructure so we don't have to expose so much stuff.
26#include "ABIInfo.h"
27
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000028namespace llvm {
Reid Klecknerde864822017-03-21 16:57:30 +000029class AttributeList;
30class Function;
31class Type;
32class Value;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000033}
34
35namespace clang {
36 class ASTContext;
37 class Decl;
38 class FunctionDecl;
39 class ObjCMethodDecl;
Daniel Dunbarbc915f42008-09-09 23:14:03 +000040 class VarDecl;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000041
42namespace CodeGen {
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000043
Reid Klecknerde864822017-03-21 16:57:30 +000044/// Abstract information about a function or function prototype.
45class CGCalleeInfo {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 /// The function prototype of the callee.
Reid Klecknerde864822017-03-21 16:57:30 +000047 const FunctionProtoType *CalleeProtoTy;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000048 /// The function declaration of the callee.
Reid Klecknerde864822017-03-21 16:57:30 +000049 const Decl *CalleeDecl;
John McCallb92ab1a2016-10-26 23:46:34 +000050
Reid Klecknerde864822017-03-21 16:57:30 +000051public:
52 explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
53 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
54 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
55 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
56 : CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
57 CGCalleeInfo(const Decl *calleeDecl)
58 : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
John McCallb92ab1a2016-10-26 23:46:34 +000059
Reid Klecknerde864822017-03-21 16:57:30 +000060 const FunctionProtoType *getCalleeFunctionProtoType() const {
61 return CalleeProtoTy;
62 }
63 const Decl *getCalleeDecl() const { return CalleeDecl; }
John McCallb92ab1a2016-10-26 23:46:34 +000064 };
65
66 /// All available information about a concrete callee.
67 class CGCallee {
68 enum class SpecialKind : uintptr_t {
69 Invalid,
70 Builtin,
71 PseudoDestructor,
Peter Collingbourneea211002018-02-05 23:09:13 +000072 Virtual,
John McCallb92ab1a2016-10-26 23:46:34 +000073
Peter Collingbourneea211002018-02-05 23:09:13 +000074 Last = Virtual
John McCallb92ab1a2016-10-26 23:46:34 +000075 };
76
John McCallaaae3022016-11-07 21:13:27 +000077 struct BuiltinInfoStorage {
78 const FunctionDecl *Decl;
79 unsigned ID;
80 };
81 struct PseudoDestructorInfoStorage {
82 const CXXPseudoDestructorExpr *Expr;
83 };
Peter Collingbourneea211002018-02-05 23:09:13 +000084 struct VirtualInfoStorage {
85 const CallExpr *CE;
86 GlobalDecl MD;
87 Address Addr;
88 llvm::FunctionType *FTy;
89 };
John McCallaaae3022016-11-07 21:13:27 +000090
John McCallb92ab1a2016-10-26 23:46:34 +000091 SpecialKind KindOrFunctionPointer;
92 union {
93 CGCalleeInfo AbstractInfo;
John McCallaaae3022016-11-07 21:13:27 +000094 BuiltinInfoStorage BuiltinInfo;
95 PseudoDestructorInfoStorage PseudoDestructorInfo;
Peter Collingbourneea211002018-02-05 23:09:13 +000096 VirtualInfoStorage VirtualInfo;
John McCallb92ab1a2016-10-26 23:46:34 +000097 };
98
99 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
100
101 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
102 : KindOrFunctionPointer(SpecialKind::Builtin) {
103 BuiltinInfo.Decl = builtinDecl;
104 BuiltinInfo.ID = builtinID;
105 }
106
107 public:
108 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
109
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());
118 }
119
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;
125 return result;
126 }
127
128 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
129 CGCallee result(SpecialKind::PseudoDestructor);
130 result.PseudoDestructorInfo.Expr = E;
131 return result;
132 }
133
134 static CGCallee forDirect(llvm::Constant *functionPtr,
135 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
136 return CGCallee(abstractInfo, functionPtr);
137 }
138
Peter Collingbourneea211002018-02-05 23:09:13 +0000139 static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
140 llvm::FunctionType *FTy) {
141 CGCallee result(SpecialKind::Virtual);
142 result.VirtualInfo.CE = CE;
143 result.VirtualInfo.MD = MD;
144 result.VirtualInfo.Addr = Addr;
145 result.VirtualInfo.FTy = FTy;
146 return result;
147 }
148
John McCallb92ab1a2016-10-26 23:46:34 +0000149 bool isBuiltin() const {
150 return KindOrFunctionPointer == SpecialKind::Builtin;
151 }
152 const FunctionDecl *getBuiltinDecl() const {
153 assert(isBuiltin());
154 return BuiltinInfo.Decl;
155 }
156 unsigned getBuiltinID() const {
157 assert(isBuiltin());
158 return BuiltinInfo.ID;
159 }
160
161 bool isPseudoDestructor() const {
162 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
163 }
164 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
165 assert(isPseudoDestructor());
166 return PseudoDestructorInfo.Expr;
167 }
168
169 bool isOrdinary() const {
170 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
171 }
Peter Collingbourneea211002018-02-05 23:09:13 +0000172 CGCalleeInfo getAbstractInfo() const {
173 if (isVirtual())
174 return VirtualInfo.MD.getDecl();
John McCallb92ab1a2016-10-26 23:46:34 +0000175 assert(isOrdinary());
176 return AbstractInfo;
177 }
178 llvm::Value *getFunctionPointer() const {
179 assert(isOrdinary());
180 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
181 }
John McCallb92ab1a2016-10-26 23:46:34 +0000182 void setFunctionPointer(llvm::Value *functionPtr) {
183 assert(isOrdinary());
184 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
185 }
Peter Collingbourneea211002018-02-05 23:09:13 +0000186
187 bool isVirtual() const {
188 return KindOrFunctionPointer == SpecialKind::Virtual;
189 }
190 const CallExpr *getVirtualCallExpr() const {
191 assert(isVirtual());
192 return VirtualInfo.CE;
193 }
194 GlobalDecl getVirtualMethodDecl() const {
195 assert(isVirtual());
196 return VirtualInfo.MD;
197 }
198 Address getThisAddress() const {
199 assert(isVirtual());
200 return VirtualInfo.Addr;
201 }
202
203 llvm::FunctionType *getFunctionType() const {
204 if (isVirtual())
205 return VirtualInfo.FTy;
206 return cast<llvm::FunctionType>(
207 getFunctionPointer()->getType()->getPointerElementType());
208 }
John McCall9831b842018-02-06 18:52:44 +0000209
210 /// If this is a delayed callee computation of some sort, prepare
211 /// a concrete callee.
212 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
John McCallb92ab1a2016-10-26 23:46:34 +0000213 };
214
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000215 struct CallArg {
Yaxun Liu5b330e82018-03-15 15:25:19 +0000216 private:
217 union {
218 RValue RV;
219 LValue LV; /// The argument is semantically a load from this l-value.
220 };
221 bool HasLV;
222
223 /// A data-flow flag to make sure getRValue and/or copyInto are not
224 /// called twice for duplicated IR emission.
225 mutable bool IsUsed;
226
227 public:
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000228 QualType Ty;
Yaxun Liu5b330e82018-03-15 15:25:19 +0000229 CallArg(RValue rv, QualType ty)
230 : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
231 CallArg(LValue lv, QualType ty)
232 : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
233 bool hasLValue() const { return HasLV; }
234 QualType getType() const { return Ty; }
235
236 /// \returns an independent RValue. If the CallArg contains an LValue,
237 /// a temporary copy is returned.
238 RValue getRValue(CodeGenFunction &CGF) const;
239
240 LValue getKnownLValue() const {
241 assert(HasLV && !IsUsed);
242 return LV;
243 }
244 RValue getKnownRValue() const {
245 assert(!HasLV && !IsUsed);
246 return RV;
247 }
248 void setRValue(RValue _RV) {
249 assert(!HasLV);
250 RV = _RV;
251 }
252
253 bool isAggregate() const { return HasLV || RV.isAggregate(); }
254
255 void copyInto(CodeGenFunction &CGF, Address A) const;
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000256 };
257
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000258 /// CallArgList - Type for representing both the value and type of
259 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +0000260 class CallArgList :
Yaxun Liud9389822018-03-14 15:02:28 +0000261 public SmallVector<CallArg, 8> {
John McCall32ea9692011-03-11 20:59:21 +0000262 public:
Reid Kleckner7c2f9e82015-10-08 00:17:45 +0000263 CallArgList() : StackBase(nullptr) {}
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000264
John McCall31168b02011-06-15 23:02:42 +0000265 struct Writeback {
John McCalleff18842013-03-23 02:35:54 +0000266 /// The original argument. Note that the argument l-value
267 /// is potentially null.
268 LValue Source;
John McCall31168b02011-06-15 23:02:42 +0000269
270 /// The temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +0000271 Address Temporary;
John McCalleff18842013-03-23 02:35:54 +0000272
273 /// A value to "use" after the writeback, or null.
274 llvm::Value *ToUse;
John McCall31168b02011-06-15 23:02:42 +0000275 };
276
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000277 struct CallArgCleanup {
278 EHScopeStack::stable_iterator Cleanup;
279
280 /// The "is active" insertion point. This instruction is temporary and
281 /// will be removed after insertion.
282 llvm::Instruction *IsActiveIP;
283 };
284
Yaxun Liu5b330e82018-03-15 15:25:19 +0000285 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
286
287 void addUncopiedAggregate(LValue LV, QualType type) {
288 push_back(CallArg(LV, type));
John McCall32ea9692011-03-11 20:59:21 +0000289 }
John McCall31168b02011-06-15 23:02:42 +0000290
Richard Smith762672a2016-09-28 19:09:10 +0000291 /// Add all the arguments from another CallArgList to this one. After doing
292 /// this, the old CallArgList retains its list of arguments, but must not
293 /// be used to emit a call.
John McCall31168b02011-06-15 23:02:42 +0000294 void addFrom(const CallArgList &other) {
295 insert(end(), other.begin(), other.end());
296 Writebacks.insert(Writebacks.end(),
297 other.Writebacks.begin(), other.Writebacks.end());
Richard Smith762672a2016-09-28 19:09:10 +0000298 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
299 other.CleanupsToDeactivate.begin(),
300 other.CleanupsToDeactivate.end());
301 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
302 if (!StackBase)
303 StackBase = other.StackBase;
John McCall31168b02011-06-15 23:02:42 +0000304 }
305
John McCall7f416cc2015-09-08 08:05:57 +0000306 void addWriteback(LValue srcLV, Address temporary,
John McCalleff18842013-03-23 02:35:54 +0000307 llvm::Value *toUse) {
John McCall7f416cc2015-09-08 08:05:57 +0000308 Writeback writeback = { srcLV, temporary, toUse };
John McCall31168b02011-06-15 23:02:42 +0000309 Writebacks.push_back(writeback);
310 }
311
312 bool hasWritebacks() const { return !Writebacks.empty(); }
313
Aaron Ballman36a7fa82014-03-17 17:22:27 +0000314 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
315 writeback_const_range;
316
317 writeback_const_range writebacks() const {
318 return writeback_const_range(Writebacks.begin(), Writebacks.end());
319 }
John McCall31168b02011-06-15 23:02:42 +0000320
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000321 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);
327 }
328
329 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
330 return CleanupsToDeactivate;
331 }
332
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000333 void allocateArgumentMemory(CodeGenFunction &CGF);
334 llvm::Instruction *getStackBase() const { return StackBase; }
Nico Weber8cdb3f92015-08-25 18:43:32 +0000335 void freeArgumentMemory(CodeGenFunction &CGF) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000336
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000337 /// Returns if we're using an inalloca struct to pass arguments in
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000338 /// memory.
339 bool isUsingInAlloca() const { return StackBase; }
340
John McCall31168b02011-06-15 23:02:42 +0000341 private:
Chris Lattner01cf8db2011-07-20 06:58:45 +0000342 SmallVector<Writeback, 1> Writebacks;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000343
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
346 /// occurs.
347 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000348
349 /// The stacksave call. It dominates all of the argument evaluation.
350 llvm::CallInst *StackBase;
John McCalla738c252011-03-09 04:27:21 +0000351 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000352
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000353 /// 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.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000356 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalla738c252011-03-09 04:27:21 +0000357 };
Mike Stump11289f42009-09-09 15:08:12 +0000358
Fangrui Song6907ce22018-07-30 19:24:48 +0000359 /// ReturnValueSlot - Contains the address where the return value of a
Anders Carlsson17490832009-12-24 20:40:36 +0000360 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000361 class ReturnValueSlot {
Leny Kholodov6aab1112015-06-08 10:23:49 +0000362 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
John McCall7f416cc2015-09-08 08:05:57 +0000363 CharUnits Alignment;
Leny Kholodov6aab1112015-06-08 10:23:49 +0000364
365 // Return value slot flags
366 enum Flags {
367 IS_VOLATILE = 0x1,
368 IS_UNUSED = 0x2,
369 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000370
371 public:
372 ReturnValueSlot() {}
John McCall7f416cc2015-09-08 08:05:57 +0000373 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
374 : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
375 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
376 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
Anders Carlsson0435ed52009-12-24 19:08:58 +0000377
John McCall7f416cc2015-09-08 08:05:57 +0000378 bool isNull() const { return !getValue().isValid(); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000379
380 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
John McCall7f416cc2015-09-08 08:05:57 +0000381 Address getValue() const { return Address(Value.getPointer(), Alignment); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000382 bool isUnused() const { return Value.getInt() & IS_UNUSED; }
Anders Carlsson0435ed52009-12-24 19:08:58 +0000383 };
Fangrui Song6907ce22018-07-30 19:24:48 +0000384
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000385} // end namespace CodeGen
386} // end namespace clang
387
388#endif