blob: 04e600536e57f4a9a5f277700cb8951d84f28ce5 [file] [log] [blame]
Daniel Dunbar0dbe2272008-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
15#ifndef CLANG_CODEGEN_CGCALL_H
16#define CLANG_CODEGEN_CGCALL_H
17
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "CGValue.h"
Reid Kleckner9b601952013-06-21 12:45:15 +000019#include "EHScopeStack.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/CanonicalType.h"
21#include "clang/AST/Type.h"
Anders Carlsson31777a22009-12-24 19:08:58 +000022#include "llvm/ADT/FoldingSet.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000023#include "llvm/IR/Value.h"
Daniel Dunbar46f45b92008-09-09 01:06:48 +000024
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000025// FIXME: Restructure so we don't have to expose so much stuff.
26#include "ABIInfo.h"
27
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000028namespace llvm {
Bill Wendlingb263bdf2013-01-27 02:46:53 +000029 class AttributeSet;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +000030 class Function;
31 class Type;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000032 class Value;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000033}
34
35namespace clang {
36 class ASTContext;
37 class Decl;
38 class FunctionDecl;
39 class ObjCMethodDecl;
Daniel Dunbar7c086512008-09-09 23:14:03 +000040 class VarDecl;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000041
42namespace CodeGen {
Bill Wendlingb263bdf2013-01-27 02:46:53 +000043 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000044
Eli Friedmanc6d07822011-05-02 18:05:27 +000045 struct CallArg {
46 RValue RV;
47 QualType Ty;
Eli Friedman55d48482011-05-26 00:10:27 +000048 bool NeedsCopy;
49 CallArg(RValue rv, QualType ty, bool needscopy)
50 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanc6d07822011-05-02 18:05:27 +000051 { }
52 };
53
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000054 /// CallArgList - Type for representing both the value and type of
55 /// arguments in a call.
John McCalld26bc762011-03-09 04:27:21 +000056 class CallArgList :
Chris Lattner686775d2011-07-20 06:58:45 +000057 public SmallVector<CallArg, 16> {
John McCall413ebdb2011-03-11 20:59:21 +000058 public:
John McCallf85e1932011-06-15 23:02:42 +000059 struct Writeback {
John McCallb6a60792013-03-23 02:35:54 +000060 /// The original argument. Note that the argument l-value
61 /// is potentially null.
62 LValue Source;
John McCallf85e1932011-06-15 23:02:42 +000063
64 /// The temporary alloca.
65 llvm::Value *Temporary;
John McCallb6a60792013-03-23 02:35:54 +000066
67 /// A value to "use" after the writeback, or null.
68 llvm::Value *ToUse;
John McCallf85e1932011-06-15 23:02:42 +000069 };
70
Reid Kleckner9b601952013-06-21 12:45:15 +000071 struct CallArgCleanup {
72 EHScopeStack::stable_iterator Cleanup;
73
74 /// The "is active" insertion point. This instruction is temporary and
75 /// will be removed after insertion.
76 llvm::Instruction *IsActiveIP;
77 };
78
Eli Friedman55d48482011-05-26 00:10:27 +000079 void add(RValue rvalue, QualType type, bool needscopy = false) {
80 push_back(CallArg(rvalue, type, needscopy));
John McCall413ebdb2011-03-11 20:59:21 +000081 }
John McCallf85e1932011-06-15 23:02:42 +000082
83 void addFrom(const CallArgList &other) {
84 insert(end(), other.begin(), other.end());
85 Writebacks.insert(Writebacks.end(),
86 other.Writebacks.begin(), other.Writebacks.end());
87 }
88
John McCallb6a60792013-03-23 02:35:54 +000089 void addWriteback(LValue srcLV, llvm::Value *temporary,
90 llvm::Value *toUse) {
John McCallf85e1932011-06-15 23:02:42 +000091 Writeback writeback;
John McCallb6a60792013-03-23 02:35:54 +000092 writeback.Source = srcLV;
John McCallf85e1932011-06-15 23:02:42 +000093 writeback.Temporary = temporary;
John McCallb6a60792013-03-23 02:35:54 +000094 writeback.ToUse = toUse;
John McCallf85e1932011-06-15 23:02:42 +000095 Writebacks.push_back(writeback);
96 }
97
98 bool hasWritebacks() const { return !Writebacks.empty(); }
99
Chris Lattner686775d2011-07-20 06:58:45 +0000100 typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
John McCallf85e1932011-06-15 23:02:42 +0000101 writeback_iterator writeback_begin() const { return Writebacks.begin(); }
102 writeback_iterator writeback_end() const { return Writebacks.end(); }
103
Reid Kleckner9b601952013-06-21 12:45:15 +0000104 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
105 llvm::Instruction *IsActiveIP) {
106 CallArgCleanup ArgCleanup;
107 ArgCleanup.Cleanup = Cleanup;
108 ArgCleanup.IsActiveIP = IsActiveIP;
109 CleanupsToDeactivate.push_back(ArgCleanup);
110 }
111
112 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
113 return CleanupsToDeactivate;
114 }
115
John McCallf85e1932011-06-15 23:02:42 +0000116 private:
Chris Lattner686775d2011-07-20 06:58:45 +0000117 SmallVector<Writeback, 1> Writebacks;
Reid Kleckner9b601952013-06-21 12:45:15 +0000118
119 /// Deactivate these cleanups immediately before making the call. This
120 /// is used to cleanup objects that are owned by the callee once the call
121 /// occurs.
122 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
John McCalld26bc762011-03-09 04:27:21 +0000123 };
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000124
John McCallde5d3c72012-02-17 03:33:10 +0000125 /// A class for recording the number of arguments that a function
126 /// signature requires.
127 class RequiredArgs {
128 /// The number of required arguments, or ~0 if the signature does
129 /// not permit optional arguments.
130 unsigned NumRequired;
131 public:
132 enum All_t { All };
133
134 RequiredArgs(All_t _) : NumRequired(~0U) {}
135 explicit RequiredArgs(unsigned n) : NumRequired(n) {
136 assert(n != ~0U);
137 }
138
139 /// Compute the arguments required by the given formal prototype,
140 /// given that there may be some additional, non-formal arguments
141 /// in play.
142 static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
143 unsigned additional) {
144 if (!prototype->isVariadic()) return All;
145 return RequiredArgs(prototype->getNumArgs() + additional);
146 }
147
148 static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
149 return forPrototypePlus(prototype, 0);
150 }
151
152 static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
153 return forPrototype(prototype.getTypePtr());
154 }
155
156 static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
157 unsigned additional) {
158 return forPrototypePlus(prototype.getTypePtr(), additional);
159 }
160
161 bool allowsOptionalArgs() const { return NumRequired != ~0U; }
John McCalle56bb362012-12-07 07:03:17 +0000162 unsigned getNumRequiredArgs() const {
John McCallde5d3c72012-02-17 03:33:10 +0000163 assert(allowsOptionalArgs());
164 return NumRequired;
165 }
166
167 unsigned getOpaqueData() const { return NumRequired; }
168 static RequiredArgs getFromOpaqueData(unsigned value) {
169 if (value == ~0U) return All;
170 return RequiredArgs(value);
171 }
172 };
173
Daniel Dunbar7c086512008-09-09 23:14:03 +0000174 /// FunctionArgList - Type for representing both the decl and type
175 /// of parameters to a function. The decl must be either a
176 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner686775d2011-07-20 06:58:45 +0000177 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalld26bc762011-03-09 04:27:21 +0000178 };
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000180 /// CGFunctionInfo - Class to encapsulate the information about a
181 /// function definition.
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000182 class CGFunctionInfo : public llvm::FoldingSetNode {
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000183 struct ArgInfo {
John McCallead608a2010-02-26 00:48:12 +0000184 CanQualType type;
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000185 ABIArgInfo info;
186 };
187
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000188 /// The LLVM::CallingConv to use for this function (as specified by the
189 /// user).
John McCallde5d3c72012-02-17 03:33:10 +0000190 unsigned CallingConvention : 8;
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000191
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000192 /// The LLVM::CallingConv to actually use for this function, which may
193 /// depend on the ABI.
John McCallde5d3c72012-02-17 03:33:10 +0000194 unsigned EffectiveCallingConvention : 8;
195
196 /// The clang::CallingConv that this was originally created with.
197 unsigned ASTCallingConvention : 8;
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000198
John McCall04a67a62010-02-05 21:31:56 +0000199 /// Whether this function is noreturn.
John McCallde5d3c72012-02-17 03:33:10 +0000200 unsigned NoReturn : 1;
John McCall04a67a62010-02-05 21:31:56 +0000201
John McCallf85e1932011-06-15 23:02:42 +0000202 /// Whether this function is returns-retained.
John McCallde5d3c72012-02-17 03:33:10 +0000203 unsigned ReturnsRetained : 1;
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000204
Rafael Espindola425ef722010-03-30 22:15:11 +0000205 /// How many arguments to pass inreg.
John McCallde5d3c72012-02-17 03:33:10 +0000206 unsigned HasRegParm : 1;
207 unsigned RegParm : 4;
208
209 RequiredArgs Required;
210
211 unsigned NumArgs;
212 ArgInfo *getArgsBuffer() {
213 return reinterpret_cast<ArgInfo*>(this+1);
214 }
215 const ArgInfo *getArgsBuffer() const {
216 return reinterpret_cast<const ArgInfo*>(this + 1);
217 }
218
219 CGFunctionInfo() : Required(RequiredArgs::All) {}
Rafael Espindola425ef722010-03-30 22:15:11 +0000220
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000221 public:
John McCallde5d3c72012-02-17 03:33:10 +0000222 static CGFunctionInfo *create(unsigned llvmCC,
223 const FunctionType::ExtInfo &extInfo,
224 CanQualType resultType,
225 ArrayRef<CanQualType> argTypes,
226 RequiredArgs required);
227
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000228 typedef const ArgInfo *const_arg_iterator;
229 typedef ArgInfo *arg_iterator;
Daniel Dunbara0a99e02009-02-02 23:43:58 +0000230
John McCallde5d3c72012-02-17 03:33:10 +0000231 const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
232 const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
233 arg_iterator arg_begin() { return getArgsBuffer() + 1; }
234 arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000235
Daniel Dunbar4b5f0a42009-02-04 21:17:21 +0000236 unsigned arg_size() const { return NumArgs; }
237
John McCallde5d3c72012-02-17 03:33:10 +0000238 bool isVariadic() const { return Required.allowsOptionalArgs(); }
239 RequiredArgs getRequiredArgs() const { return Required; }
240
John McCall04a67a62010-02-05 21:31:56 +0000241 bool isNoReturn() const { return NoReturn; }
242
John McCallde5d3c72012-02-17 03:33:10 +0000243 /// In ARC, whether this function retains its return value. This
John McCallf85e1932011-06-15 23:02:42 +0000244 /// is not always reliable for call sites.
245 bool isReturnsRetained() const { return ReturnsRetained; }
246
John McCallde5d3c72012-02-17 03:33:10 +0000247 /// getASTCallingConvention() - Return the AST-specified calling
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000248 /// convention.
John McCallde5d3c72012-02-17 03:33:10 +0000249 CallingConv getASTCallingConvention() const {
250 return CallingConv(ASTCallingConvention);
251 }
252
253 /// getCallingConvention - Return the user specified calling
254 /// convention, which has been translated into an LLVM CC.
Daniel Dunbarbac7c252009-09-11 22:24:53 +0000255 unsigned getCallingConvention() const { return CallingConvention; }
256
Daniel Dunbarca6408c2009-09-12 00:59:20 +0000257 /// getEffectiveCallingConvention - Return the actual calling convention to
258 /// use, which may depend on the ABI.
259 unsigned getEffectiveCallingConvention() const {
260 return EffectiveCallingConvention;
261 }
262 void setEffectiveCallingConvention(unsigned Value) {
263 EffectiveCallingConvention = Value;
264 }
265
Eli Friedmana49218e2011-04-09 08:18:08 +0000266 bool getHasRegParm() const { return HasRegParm; }
Rafael Espindola425ef722010-03-30 22:15:11 +0000267 unsigned getRegParm() const { return RegParm; }
268
John McCallde5d3c72012-02-17 03:33:10 +0000269 FunctionType::ExtInfo getExtInfo() const {
270 return FunctionType::ExtInfo(isNoReturn(),
271 getHasRegParm(), getRegParm(),
272 getASTCallingConvention(),
273 isReturnsRetained());
274 }
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000275
John McCallde5d3c72012-02-17 03:33:10 +0000276 CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
277
278 ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
279 const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000280
281 void Profile(llvm::FoldingSetNodeID &ID) {
John McCallde5d3c72012-02-17 03:33:10 +0000282 ID.AddInteger(getASTCallingConvention());
John McCall04a67a62010-02-05 21:31:56 +0000283 ID.AddBoolean(NoReturn);
John McCallf85e1932011-06-15 23:02:42 +0000284 ID.AddBoolean(ReturnsRetained);
Eli Friedmana49218e2011-04-09 08:18:08 +0000285 ID.AddBoolean(HasRegParm);
Rafael Espindola425ef722010-03-30 22:15:11 +0000286 ID.AddInteger(RegParm);
John McCallde5d3c72012-02-17 03:33:10 +0000287 ID.AddInteger(Required.getOpaqueData());
Daniel Dunbar35e67d42009-02-05 00:00:23 +0000288 getReturnType().Profile(ID);
Daniel Dunbar88c2fa92009-02-03 05:31:23 +0000289 for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
290 it->type.Profile(ID);
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292 static void Profile(llvm::FoldingSetNodeID &ID,
John McCallde5d3c72012-02-17 03:33:10 +0000293 const FunctionType::ExtInfo &info,
294 RequiredArgs required,
295 CanQualType resultType,
296 ArrayRef<CanQualType> argTypes) {
297 ID.AddInteger(info.getCC());
298 ID.AddBoolean(info.getNoReturn());
299 ID.AddBoolean(info.getProducesResult());
300 ID.AddBoolean(info.getHasRegParm());
301 ID.AddInteger(info.getRegParm());
302 ID.AddInteger(required.getOpaqueData());
303 resultType.Profile(ID);
304 for (ArrayRef<CanQualType>::iterator
305 i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
306 i->Profile(ID);
John McCallead608a2010-02-26 00:48:12 +0000307 }
Daniel Dunbar40a6be62009-02-03 00:07:12 +0000308 }
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000309 };
Anders Carlsson31777a22009-12-24 19:08:58 +0000310
Anders Carlssond2490a92009-12-24 20:40:36 +0000311 /// ReturnValueSlot - Contains the address where the return value of a
312 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson31777a22009-12-24 19:08:58 +0000313 class ReturnValueSlot {
314 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
315
316 public:
317 ReturnValueSlot() {}
318 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
319 : Value(Value, IsVolatile) {}
320
321 bool isNull() const { return !getValue(); }
322
323 bool isVolatile() const { return Value.getInt(); }
324 llvm::Value *getValue() const { return Value.getPointer(); }
325 };
326
Daniel Dunbar0dbe2272008-09-08 21:33:45 +0000327} // end namespace CodeGen
328} // end namespace clang
329
330#endif