1 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
16 #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
19 #include "EHScopeStack.h"
20 #include "clang/AST/CanonicalType.h"
21 #include "clang/AST/GlobalDecl.h"
22 #include "clang/AST/Type.h"
23 #include "llvm/IR/Value.h"
25 // FIXME: Restructure so we don't have to expose so much stuff.
44 /// Abstract information about a function or function prototype.
46 /// The function prototype of the callee.
47 const FunctionProtoType *CalleeProtoTy;
48 /// The function declaration of the callee.
49 const Decl *CalleeDecl;
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) {}
60 const FunctionProtoType *getCalleeFunctionProtoType() const {
63 const Decl *getCalleeDecl() const { return CalleeDecl; }
66 /// All available information about a concrete callee.
68 enum class SpecialKind : uintptr_t {
77 struct BuiltinInfoStorage {
78 const FunctionDecl *Decl;
81 struct PseudoDestructorInfoStorage {
82 const CXXPseudoDestructorExpr *Expr;
84 struct VirtualInfoStorage {
88 llvm::FunctionType *FTy;
91 SpecialKind KindOrFunctionPointer;
93 CGCalleeInfo AbstractInfo;
94 BuiltinInfoStorage BuiltinInfo;
95 PseudoDestructorInfoStorage PseudoDestructorInfo;
96 VirtualInfoStorage VirtualInfo;
99 explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
101 CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
102 : KindOrFunctionPointer(SpecialKind::Builtin) {
103 BuiltinInfo.Decl = builtinDecl;
104 BuiltinInfo.ID = builtinID;
108 CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
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());
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;
128 static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
129 CGCallee result(SpecialKind::PseudoDestructor);
130 result.PseudoDestructorInfo.Expr = E;
134 static CGCallee forDirect(llvm::Constant *functionPtr,
135 const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
136 return CGCallee(abstractInfo, functionPtr);
139 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;
149 bool isBuiltin() const {
150 return KindOrFunctionPointer == SpecialKind::Builtin;
152 const FunctionDecl *getBuiltinDecl() const {
154 return BuiltinInfo.Decl;
156 unsigned getBuiltinID() const {
158 return BuiltinInfo.ID;
161 bool isPseudoDestructor() const {
162 return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
164 const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
165 assert(isPseudoDestructor());
166 return PseudoDestructorInfo.Expr;
169 bool isOrdinary() const {
170 return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
172 CGCalleeInfo getAbstractInfo() const {
174 return VirtualInfo.MD.getDecl();
175 assert(isOrdinary());
178 llvm::Value *getFunctionPointer() const {
179 assert(isOrdinary());
180 return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
182 void setFunctionPointer(llvm::Value *functionPtr) {
183 assert(isOrdinary());
184 KindOrFunctionPointer = SpecialKind(uintptr_t(functionPtr));
187 bool isVirtual() const {
188 return KindOrFunctionPointer == SpecialKind::Virtual;
190 const CallExpr *getVirtualCallExpr() const {
192 return VirtualInfo.CE;
194 GlobalDecl getVirtualMethodDecl() const {
196 return VirtualInfo.MD;
198 Address getThisAddress() const {
200 return VirtualInfo.Addr;
203 llvm::FunctionType *getFunctionType() const {
205 return VirtualInfo.FTy;
206 return cast<llvm::FunctionType>(
207 getFunctionPointer()->getType()->getPointerElementType());
210 /// If this is a delayed callee computation of some sort, prepare
211 /// a concrete callee.
212 CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
219 LValue LV; /// The argument is semantically a load from this l-value.
223 /// A data-flow flag to make sure getRValue and/or copyInto are not
224 /// called twice for duplicated IR emission.
229 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; }
236 /// \returns an independent RValue. If the CallArg contains an LValue,
237 /// a temporary copy is returned.
238 RValue getRValue(CodeGenFunction &CGF) const;
240 LValue getKnownLValue() const {
241 assert(HasLV && !IsUsed);
244 RValue getKnownRValue() const {
245 assert(!HasLV && !IsUsed);
248 void setRValue(RValue _RV) {
253 bool isAggregate() const { return HasLV || RV.isAggregate(); }
255 void copyInto(CodeGenFunction &CGF, Address A) const;
258 /// CallArgList - Type for representing both the value and type of
259 /// arguments in a call.
261 public SmallVector<CallArg, 8> {
263 CallArgList() : StackBase(nullptr) {}
266 /// The original argument. Note that the argument l-value
267 /// is potentially null.
270 /// The temporary alloca.
273 /// A value to "use" after the writeback, or null.
277 struct CallArgCleanup {
278 EHScopeStack::stable_iterator Cleanup;
280 /// The "is active" insertion point. This instruction is temporary and
281 /// will be removed after insertion.
282 llvm::Instruction *IsActiveIP;
285 void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
287 void addUncopiedAggregate(LValue LV, QualType type) {
288 push_back(CallArg(LV, type));
291 /// 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.
294 void addFrom(const CallArgList &other) {
295 insert(end(), other.begin(), other.end());
296 Writebacks.insert(Writebacks.end(),
297 other.Writebacks.begin(), other.Writebacks.end());
298 CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
299 other.CleanupsToDeactivate.begin(),
300 other.CleanupsToDeactivate.end());
301 assert(!(StackBase && other.StackBase) && "can't merge stackbases");
303 StackBase = other.StackBase;
306 void addWriteback(LValue srcLV, Address temporary,
307 llvm::Value *toUse) {
308 Writeback writeback = { srcLV, temporary, toUse };
309 Writebacks.push_back(writeback);
312 bool hasWritebacks() const { return !Writebacks.empty(); }
314 typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
315 writeback_const_range;
317 writeback_const_range writebacks() const {
318 return writeback_const_range(Writebacks.begin(), Writebacks.end());
321 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);
329 ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
330 return CleanupsToDeactivate;
333 void allocateArgumentMemory(CodeGenFunction &CGF);
334 llvm::Instruction *getStackBase() const { return StackBase; }
335 void freeArgumentMemory(CodeGenFunction &CGF) const;
337 /// Returns if we're using an inalloca struct to pass arguments in
339 bool isUsingInAlloca() const { return StackBase; }
342 SmallVector<Writeback, 1> Writebacks;
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
347 SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
349 /// The stacksave call. It dominates all of the argument evaluation.
350 llvm::CallInst *StackBase;
353 /// 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.
356 class FunctionArgList : public SmallVector<const VarDecl*, 16> {
359 /// ReturnValueSlot - Contains the address where the return value of a
360 /// function can be stored, and whether the address is volatile or not.
361 class ReturnValueSlot {
362 llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
365 // Return value slot flags
373 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()) {}
378 bool isNull() const { return !getValue().isValid(); }
380 bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
381 Address getValue() const { return Address(Value.getPointer(), Alignment); }
382 bool isUnused() const { return Value.getInt() & IS_UNUSED; }
385 } // end namespace CodeGen
386 } // end namespace clang