blob: 5e4bb5e6579ba915ae45e6815525cd7dfca5ba27 [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 {
Bill Wendling290d9522013-01-27 02:46:53 +000028 class AttributeSet;
Daniel Dunbar313321e2009-02-03 05:31:23 +000029 class Function;
30 class Type;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000031 class 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 {
Bill Wendling290d9522013-01-27 02:46:53 +000042 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000043
Eli Friedmanf4258eb2011-05-02 18:05:27 +000044 struct CallArg {
45 RValue RV;
46 QualType Ty;
Eli Friedmandf968192011-05-26 00:10:27 +000047 bool NeedsCopy;
48 CallArg(RValue rv, QualType ty, bool needscopy)
49 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanf4258eb2011-05-02 18:05:27 +000050 { }
51 };
52
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000053 /// CallArgList - Type for representing both the value and type of
54 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +000055 class CallArgList :
Chris Lattner01cf8db2011-07-20 06:58:45 +000056 public SmallVector<CallArg, 16> {
John McCall32ea9692011-03-11 20:59:21 +000057 public:
Reid Kleckner7c2f9e82015-10-08 00:17:45 +000058 CallArgList() : StackBase(nullptr) {}
Reid Kleckner314ef7b2014-02-01 00:04:45 +000059
John McCall31168b02011-06-15 23:02:42 +000060 struct Writeback {
John McCalleff18842013-03-23 02:35:54 +000061 /// The original argument. Note that the argument l-value
62 /// is potentially null.
63 LValue Source;
John McCall31168b02011-06-15 23:02:42 +000064
65 /// The temporary alloca.
John McCall7f416cc2015-09-08 08:05:57 +000066 Address Temporary;
John McCalleff18842013-03-23 02:35:54 +000067
68 /// A value to "use" after the writeback, or null.
69 llvm::Value *ToUse;
John McCall31168b02011-06-15 23:02:42 +000070 };
71
Reid Kleckner23f4c4b2013-06-21 12:45:15 +000072 struct CallArgCleanup {
73 EHScopeStack::stable_iterator Cleanup;
74
75 /// The "is active" insertion point. This instruction is temporary and
76 /// will be removed after insertion.
77 llvm::Instruction *IsActiveIP;
78 };
79
Eli Friedmandf968192011-05-26 00:10:27 +000080 void add(RValue rvalue, QualType type, bool needscopy = false) {
81 push_back(CallArg(rvalue, type, needscopy));
John McCall32ea9692011-03-11 20:59:21 +000082 }
John McCall31168b02011-06-15 23:02:42 +000083
Richard Smith762672a2016-09-28 19:09:10 +000084 /// Add all the arguments from another CallArgList to this one. After doing
85 /// this, the old CallArgList retains its list of arguments, but must not
86 /// be used to emit a call.
John McCall31168b02011-06-15 23:02:42 +000087 void addFrom(const CallArgList &other) {
88 insert(end(), other.begin(), other.end());
89 Writebacks.insert(Writebacks.end(),
90 other.Writebacks.begin(), other.Writebacks.end());
Richard Smith762672a2016-09-28 19:09:10 +000091 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
92 other.CleanupsToDeactivate.begin(),
93 other.CleanupsToDeactivate.end());
94 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
95 if (!StackBase)
96 StackBase = other.StackBase;
John McCall31168b02011-06-15 23:02:42 +000097 }
98
John McCall7f416cc2015-09-08 08:05:57 +000099 void addWriteback(LValue srcLV, Address temporary,
John McCalleff18842013-03-23 02:35:54 +0000100 llvm::Value *toUse) {
John McCall7f416cc2015-09-08 08:05:57 +0000101 Writeback writeback = { srcLV, temporary, toUse };
John McCall31168b02011-06-15 23:02:42 +0000102 Writebacks.push_back(writeback);
103 }
104
105 bool hasWritebacks() const { return !Writebacks.empty(); }
106
Aaron Ballman36a7fa82014-03-17 17:22:27 +0000107 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
108 writeback_const_range;
109
110 writeback_const_range writebacks() const {
111 return writeback_const_range(Writebacks.begin(), Writebacks.end());
112 }
John McCall31168b02011-06-15 23:02:42 +0000113
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000114 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
115 llvm::Instruction *IsActiveIP) {
116 CallArgCleanup ArgCleanup;
117 ArgCleanup.Cleanup = Cleanup;
118 ArgCleanup.IsActiveIP = IsActiveIP;
119 CleanupsToDeactivate.push_back(ArgCleanup);
120 }
121
122 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
123 return CleanupsToDeactivate;
124 }
125
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000126 void allocateArgumentMemory(CodeGenFunction &CGF);
127 llvm::Instruction *getStackBase() const { return StackBase; }
Nico Weber8cdb3f92015-08-25 18:43:32 +0000128 void freeArgumentMemory(CodeGenFunction &CGF) const;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000129
130 /// \brief Returns if we're using an inalloca struct to pass arguments in
131 /// memory.
132 bool isUsingInAlloca() const { return StackBase; }
133
John McCall31168b02011-06-15 23:02:42 +0000134 private:
Chris Lattner01cf8db2011-07-20 06:58:45 +0000135 SmallVector<Writeback, 1> Writebacks;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000136
137 /// Deactivate these cleanups immediately before making the call. This
138 /// is used to cleanup objects that are owned by the callee once the call
139 /// occurs.
140 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000141
142 /// The stacksave call. It dominates all of the argument evaluation.
143 llvm::CallInst *StackBase;
John McCalla738c252011-03-09 04:27:21 +0000144 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000145
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000146 /// FunctionArgList - Type for representing both the decl and type
147 /// of parameters to a function. The decl must be either a
148 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000149 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalla738c252011-03-09 04:27:21 +0000150 };
Mike Stump11289f42009-09-09 15:08:12 +0000151
Anders Carlsson17490832009-12-24 20:40:36 +0000152 /// ReturnValueSlot - Contains the address where the return value of a
153 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000154 class ReturnValueSlot {
Leny Kholodov6aab1112015-06-08 10:23:49 +0000155 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
John McCall7f416cc2015-09-08 08:05:57 +0000156 CharUnits Alignment;
Leny Kholodov6aab1112015-06-08 10:23:49 +0000157
158 // Return value slot flags
159 enum Flags {
160 IS_VOLATILE = 0x1,
161 IS_UNUSED = 0x2,
162 };
Anders Carlsson0435ed52009-12-24 19:08:58 +0000163
164 public:
165 ReturnValueSlot() {}
John McCall7f416cc2015-09-08 08:05:57 +0000166 ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
167 : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
168 (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
169 Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
Anders Carlsson0435ed52009-12-24 19:08:58 +0000170
John McCall7f416cc2015-09-08 08:05:57 +0000171 bool isNull() const { return !getValue().isValid(); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000172
173 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
John McCall7f416cc2015-09-08 08:05:57 +0000174 Address getValue() const { return Address(Value.getPointer(), Alignment); }
Leny Kholodov6aab1112015-06-08 10:23:49 +0000175 bool isUnused() const { return Value.getInt() & IS_UNUSED; }
Anders Carlsson0435ed52009-12-24 19:08:58 +0000176 };
177
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000178} // end namespace CodeGen
179} // end namespace clang
180
181#endif