blob: b228733fb8ce56785e6430e58ac8878efac8f4e0 [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"
Anders Carlsson0435ed52009-12-24 19:08:58 +000022#include "llvm/ADT/FoldingSet.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 {
Bill Wendling290d9522013-01-27 02:46:53 +000029 class AttributeSet;
Daniel Dunbar313321e2009-02-03 05:31:23 +000030 class Function;
31 class Type;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000032 class 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 {
Bill Wendling290d9522013-01-27 02:46:53 +000043 typedef SmallVector<llvm::AttributeSet, 8> AttributeListType;
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000044
Eli Friedmanf4258eb2011-05-02 18:05:27 +000045 struct CallArg {
46 RValue RV;
47 QualType Ty;
Eli Friedmandf968192011-05-26 00:10:27 +000048 bool NeedsCopy;
49 CallArg(RValue rv, QualType ty, bool needscopy)
50 : RV(rv), Ty(ty), NeedsCopy(needscopy)
Eli Friedmanf4258eb2011-05-02 18:05:27 +000051 { }
52 };
53
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000054 /// CallArgList - Type for representing both the value and type of
55 /// arguments in a call.
John McCalla738c252011-03-09 04:27:21 +000056 class CallArgList :
Chris Lattner01cf8db2011-07-20 06:58:45 +000057 public SmallVector<CallArg, 16> {
John McCall32ea9692011-03-11 20:59:21 +000058 public:
Craig Topper8a13c412014-05-21 05:09:00 +000059 CallArgList() : StackBase(nullptr), StackBaseMem(nullptr) {}
Reid Kleckner314ef7b2014-02-01 00:04:45 +000060
John McCall31168b02011-06-15 23:02:42 +000061 struct Writeback {
John McCalleff18842013-03-23 02:35:54 +000062 /// The original argument. Note that the argument l-value
63 /// is potentially null.
64 LValue Source;
John McCall31168b02011-06-15 23:02:42 +000065
66 /// The temporary alloca.
67 llvm::Value *Temporary;
John McCalleff18842013-03-23 02:35:54 +000068
69 /// A value to "use" after the writeback, or null.
70 llvm::Value *ToUse;
John McCall31168b02011-06-15 23:02:42 +000071 };
72
Reid Kleckner23f4c4b2013-06-21 12:45:15 +000073 struct CallArgCleanup {
74 EHScopeStack::stable_iterator Cleanup;
75
76 /// The "is active" insertion point. This instruction is temporary and
77 /// will be removed after insertion.
78 llvm::Instruction *IsActiveIP;
79 };
80
Eli Friedmandf968192011-05-26 00:10:27 +000081 void add(RValue rvalue, QualType type, bool needscopy = false) {
82 push_back(CallArg(rvalue, type, needscopy));
John McCall32ea9692011-03-11 20:59:21 +000083 }
John McCall31168b02011-06-15 23:02:42 +000084
85 void addFrom(const CallArgList &other) {
86 insert(end(), other.begin(), other.end());
87 Writebacks.insert(Writebacks.end(),
88 other.Writebacks.begin(), other.Writebacks.end());
89 }
90
John McCalleff18842013-03-23 02:35:54 +000091 void addWriteback(LValue srcLV, llvm::Value *temporary,
92 llvm::Value *toUse) {
John McCall31168b02011-06-15 23:02:42 +000093 Writeback writeback;
John McCalleff18842013-03-23 02:35:54 +000094 writeback.Source = srcLV;
John McCall31168b02011-06-15 23:02:42 +000095 writeback.Temporary = temporary;
John McCalleff18842013-03-23 02:35:54 +000096 writeback.ToUse = toUse;
John McCall31168b02011-06-15 23:02:42 +000097 Writebacks.push_back(writeback);
98 }
99
100 bool hasWritebacks() const { return !Writebacks.empty(); }
101
Aaron Ballman36a7fa82014-03-17 17:22:27 +0000102 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
103 writeback_const_range;
104
105 writeback_const_range writebacks() const {
106 return writeback_const_range(Writebacks.begin(), Writebacks.end());
107 }
John McCall31168b02011-06-15 23:02:42 +0000108
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000109 void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
110 llvm::Instruction *IsActiveIP) {
111 CallArgCleanup ArgCleanup;
112 ArgCleanup.Cleanup = Cleanup;
113 ArgCleanup.IsActiveIP = IsActiveIP;
114 CleanupsToDeactivate.push_back(ArgCleanup);
115 }
116
117 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
118 return CleanupsToDeactivate;
119 }
120
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000121 void allocateArgumentMemory(CodeGenFunction &CGF);
122 llvm::Instruction *getStackBase() const { return StackBase; }
123 void freeArgumentMemory(CodeGenFunction &CGF) const;
124
125 /// \brief Returns if we're using an inalloca struct to pass arguments in
126 /// memory.
127 bool isUsingInAlloca() const { return StackBase; }
128
John McCall31168b02011-06-15 23:02:42 +0000129 private:
Chris Lattner01cf8db2011-07-20 06:58:45 +0000130 SmallVector<Writeback, 1> Writebacks;
Reid Kleckner23f4c4b2013-06-21 12:45:15 +0000131
132 /// Deactivate these cleanups immediately before making the call. This
133 /// is used to cleanup objects that are owned by the callee once the call
134 /// occurs.
135 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
Reid Kleckner314ef7b2014-02-01 00:04:45 +0000136
137 /// The stacksave call. It dominates all of the argument evaluation.
138 llvm::CallInst *StackBase;
139
140 /// The alloca holding the stackbase. We need it to maintain SSA form.
141 llvm::AllocaInst *StackBaseMem;
142
143 /// The iterator pointing to the stack restore cleanup. We manually run and
144 /// deactivate this cleanup after the call in the unexceptional case because
145 /// it doesn't run in the normal order.
146 EHScopeStack::stable_iterator StackCleanup;
John McCalla738c252011-03-09 04:27:21 +0000147 };
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000148
Daniel Dunbarbc915f42008-09-09 23:14:03 +0000149 /// FunctionArgList - Type for representing both the decl and type
150 /// of parameters to a function. The decl must be either a
151 /// ParmVarDecl or ImplicitParamDecl.
Chris Lattner01cf8db2011-07-20 06:58:45 +0000152 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
John McCalla738c252011-03-09 04:27:21 +0000153 };
Mike Stump11289f42009-09-09 15:08:12 +0000154
Anders Carlsson17490832009-12-24 20:40:36 +0000155 /// ReturnValueSlot - Contains the address where the return value of a
156 /// function can be stored, and whether the address is volatile or not.
Anders Carlsson0435ed52009-12-24 19:08:58 +0000157 class ReturnValueSlot {
158 llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
159
160 public:
161 ReturnValueSlot() {}
162 ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
163 : Value(Value, IsVolatile) {}
164
165 bool isNull() const { return !getValue(); }
166
167 bool isVolatile() const { return Value.getInt(); }
168 llvm::Value *getValue() const { return Value.getPointer(); }
169 };
170
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +0000171} // end namespace CodeGen
172} // end namespace clang
173
174#endif