blob: 7e10407fc31cf4b566f21eff154dec73bc1d53d1 [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"
21#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 {
45 /// \brief The function prototype of the callee.
46 const FunctionProtoType *CalleeProtoTy;
47 /// \brief The function declaration of the callee.
48 const Decl *CalleeDecl;
John McCallb92ab1a2016-10-26 23:46:34 +000049
Reid Klecknerde864822017-03-21 16:57:30 +000050public:
51 explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl(nullptr) {}
52 CGCalleeInfo(const FunctionProtoType *calleeProtoTy, const Decl *calleeDecl)
53 : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
54 CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
55 : CalleeProtoTy(calleeProtoTy), CalleeDecl(nullptr) {}
56 CGCalleeInfo(const Decl *calleeDecl)
57 : 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 }
62 const Decl *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,
71
72 Last = PseudoDestructor
73 };
74
John McCallaaae3022016-11-07 21:13:27 +000075 struct BuiltinInfoStorage {
76 const FunctionDecl *Decl;
77 unsigned ID;
78 };
79 struct PseudoDestructorInfoStorage {
80 const CXXPseudoDestructorExpr *Expr;
81 };
82
John McCallb92ab1a2016-10-26 23:46:34 +000083 SpecialKind KindOrFunctionPointer;
84 union {
85 CGCalleeInfo AbstractInfo;
John McCallaaae3022016-11-07 21:13:27 +000086 BuiltinInfoStorage BuiltinInfo;
87 PseudoDestructorInfoStorage PseudoDestructorInfo;
John McCallb92ab1a2016-10-26 23:46:34 +000088 };
89
90 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
91
92 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
93 : KindOrFunctionPointer(SpecialKind::Builtin) {
94 BuiltinInfo.Decl = builtinDecl;
95 BuiltinInfo.ID = builtinID;
96 }
97
98 public:
99 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
100
101 /// Construct a callee. Call this constructor directly when this
102 /// isn't a direct call.
103 CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
104 : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
105 AbstractInfo = abstractInfo;
106 assert(functionPtr && "configuring callee without function pointer");
107 assert(functionPtr->getType()->isPointerTy());
108 assert(functionPtr->getType()->getPointerElementType()->isFunctionTy());
109 }
110
111 static CGCallee forBuiltin(unsigned builtinID,
112 const FunctionDecl *builtinDecl) {
113 CGCallee result(SpecialKind::Builtin);
114 result.BuiltinInfo.Decl = builtinDecl;
115 result.BuiltinInfo.ID = builtinID;
116 return result;
117 }
118
119 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
120 CGCallee result(SpecialKind::PseudoDestructor);
121 result.PseudoDestructorInfo.Expr = E;
122 return result;
123 }
124
125 static CGCallee forDirect(llvm::Constant *functionPtr,
126 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
127 return CGCallee(abstractInfo, functionPtr);
128 }
129
130 bool isBuiltin() const {
131 return KindOrFunctionPointer == SpecialKind::Builtin;
132 }
133 const FunctionDecl *getBuiltinDecl() const {
134 assert(isBuiltin());
135 return BuiltinInfo.Decl;
136 }
137 unsigned getBuiltinID() const {
138 assert(isBuiltin());
139 return BuiltinInfo.ID;
140 }
141
142 bool isPseudoDestructor() const {
143 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
144 }
145 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
146 assert(isPseudoDestructor());
147 return PseudoDestructorInfo.Expr;
148 }
149
150 bool isOrdinary() const {
151 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
152 }
153 const CGCalleeInfo &getAbstractInfo() const {
154 assert(isOrdinary());
155 return AbstractInfo;
156 }
157 llvm::Value *getFunctionPointer() const {
158 assert(isOrdinary());
159 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
160 }
161 llvm::FunctionType *getFunctionType() const {
162 return cast<llvm::FunctionType>(
163 getFunctionPointer()->getType()->getPointerElementType());
164 }
165 void setFunctionPointer(llvm::Value *functionPtr) {
166 assert(isOrdinary());
167 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
168 }
169 };
170
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000171 struct CallArg {
172 RValue RV;
173 QualType Ty;
Eli Friedmandf968192011-05-26 00:10:27 +0000174 bool NeedsCopy;
175 CallArg(RValue rv, QualType ty, bool needscopy)
176 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanf4258eb2011-05-02 18:05:27 +0000177 { }
178 };
179
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000180 /// CallArgList - Type for representing both the value and type of
181 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +0000182 class CallArgList :
Chris Lattner01cf8db2011-07-20 06:58:45 +0000183 public SmallVector<CallArg, 16> {
John McCall32ea9692011-03-11 20:59:21 +0000184 public:
Reid Kleckner7c2f9e82015-10-08 00:17:45 +0000185 CallArgList() : StackBase(nullptr) {}
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000186
John McCall31168b02011-06-15 23:02:42 +0000187 struct Writeback {
John McCalleff18842013-03-23 02:35:54 +0000188 /// The original argument. Note that the argument l-value
189 /// is potentially null.
190 LValue Source;
John McCall31168b02011-06-15 23:02:42 +0000191
192 /// The temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +0000193 Address Temporary;
John McCalleff18842013-03-23 02:35:54 +0000194
195 /// A value to "use" after the writeback, or null.
196 llvm::Value *ToUse;
John McCall31168b02011-06-15 23:02:42 +0000197 };
198
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000199 struct CallArgCleanup {
200 EHScopeStack::stable_iterator Cleanup;
201
202 /// The "is active" insertion point. This instruction is temporary and
203 /// will be removed after insertion.
204 llvm::Instruction *IsActiveIP;
205 };
206
Eli Friedmandf968192011-05-26 00:10:27 +0000207 void add(RValue rvalue, QualType type, bool needscopy = false) {
208 push_back(CallArg(rvalue, type, needscopy));
John McCall32ea9692011-03-11 20:59:21 +0000209 }
John McCall31168b02011-06-15 23:02:42 +0000210
Richard Smith762672a2016-09-28 19:09:10 +0000211 /// Add all the arguments from another CallArgList to this one. After doing
212 /// this, the old CallArgList retains its list of arguments, but must not
213 /// be used to emit a call.
John McCall31168b02011-06-15 23:02:42 +0000214 void addFrom(const CallArgList &other) {
215 insert(end(), other.begin(), other.end());
216 Writebacks.insert(Writebacks.end(),
217 other.Writebacks.begin(), other.Writebacks.end());
Richard Smith762672a2016-09-28 19:09:10 +0000218 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
219 other.CleanupsToDeactivate.begin(),
220 other.CleanupsToDeactivate.end());
221 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
222 if (!StackBase)
223 StackBase = other.StackBase;
John McCall31168b02011-06-15 23:02:42 +0000224 }
225
John McCall7f416cc2015-09-08 08:05:57 +0000226 void addWriteback(LValue srcLV, Address temporary,
John McCalleff18842013-03-23 02:35:54 +0000227 llvm::Value *toUse) {
John McCall7f416cc2015-09-08 08:05:57 +0000228 Writeback writeback = { srcLV, temporary, toUse };
John McCall31168b02011-06-15 23:02:42 +0000229 Writebacks.push_back(writeback);
230 }
231
232 bool hasWritebacks() const { return !Writebacks.empty(); }
233
Aaron Ballman36a7fa82014-03-17 17:22:27 +0000234 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
235 writeback_const_range;
236
237 writeback_const_range writebacks() const {
238 return writeback_const_range(Writebacks.begin(), Writebacks.end());
239 }
John McCall31168b02011-06-15 23:02:42 +0000240
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000241 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
242 llvm::Instruction *IsActiveIP) {
243 CallArgCleanup ArgCleanup;
244 ArgCleanup.Cleanup = Cleanup;
245 ArgCleanup.IsActiveIP = IsActiveIP;
246 CleanupsToDeactivate.push_back(ArgCleanup);
247 }
248
249 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
250 return CleanupsToDeactivate;
251 }
252
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000253 void allocateArgumentMemory(CodeGenFunction &CGF);
254 llvm::Instruction *getStackBase() const { return StackBase; }
Nico Weber8cdb3f92015-08-25 18:43:32 +0000255 void freeArgumentMemory(CodeGenFunction &CGF) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000256
257 /// \brief Returns if we're using an inalloca struct to pass arguments in
258 /// memory.
259 bool isUsingInAlloca() const { return StackBase; }
260
John McCall31168b02011-06-15 23:02:42 +0000261 private:
Chris Lattner01cf8db2011-07-20 06:58:45 +0000262 SmallVector<Writeback, 1> Writebacks;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000263
264 /// Deactivate these cleanups immediately before making the call. This
265 /// is used to cleanup objects that are owned by the callee once the call
266 /// occurs.
267 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000268
269 /// The stacksave call. It dominates all of the argument evaluation.
270 llvm::CallInst *StackBase;
John McCalla738c252011-03-09 04:27:21 +0000271 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000272
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000273 /// FunctionArgList - Type for representing both the decl and type
274 /// of parameters to a function. The decl must be either a
275 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000276 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalla738c252011-03-09 04:27:21 +0000277 };
Mike Stump11289f42009-09-09 15:08:12 +0000278
Anders Carlsson17490832009-12-24 20:40:36 +0000279 /// ReturnValueSlot - Contains the address where the return value of a
280 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000281 class ReturnValueSlot {
Leny Kholodov6aab1112015-06-08 10:23:49 +0000282 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
John McCall7f416cc2015-09-08 08:05:57 +0000283 CharUnits Alignment;
Leny Kholodov6aab1112015-06-08 10:23:49 +0000284
285 // Return value slot flags
286 enum Flags {
287 IS_VOLATILE = 0x1,
288 IS_UNUSED = 0x2,
289 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000290
291 public:
292 ReturnValueSlot() {}
John McCall7f416cc2015-09-08 08:05:57 +0000293 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
294 : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
295 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
296 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
Anders Carlsson0435ed52009-12-24 19:08:58 +0000297
John McCall7f416cc2015-09-08 08:05:57 +0000298 bool isNull() const { return !getValue().isValid(); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000299
300 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
John McCall7f416cc2015-09-08 08:05:57 +0000301 Address getValue() const { return Address(Value.getPointer(), Alignment); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000302 bool isUnused() const { return Value.getInt() & IS_UNUSED; }
Anders Carlsson0435ed52009-12-24 19:08:58 +0000303 };
304
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000305} // end namespace CodeGen
306} // end namespace clang
307
308#endif