CGCall.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // These classes wrap the information about a call or function
  10. // definition used to handle ABI compliancy.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_CODEGEN_CGCALL_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CGCALL_H
  15. #include "CGValue.h"
  16. #include "EHScopeStack.h"
  17. #include "clang/AST/ASTFwd.h"
  18. #include "clang/AST/CanonicalType.h"
  19. #include "clang/AST/GlobalDecl.h"
  20. #include "clang/AST/Type.h"
  21. #include "llvm/IR/Value.h"
  22. // FIXME: Restructure so we don't have to expose so much stuff.
  23. #include "ABIInfo.h"
  24. namespace llvm {
  25. class Type;
  26. class Value;
  27. } // namespace llvm
  28. namespace clang {
  29. class Decl;
  30. class FunctionDecl;
  31. class VarDecl;
  32. namespace CodeGen {
  33. /// Abstract information about a function or function prototype.
  34. class CGCalleeInfo {
  35. /// The function prototype of the callee.
  36. const FunctionProtoType *CalleeProtoTy;
  37. /// The function declaration of the callee.
  38. GlobalDecl CalleeDecl;
  39. public:
  40. explicit CGCalleeInfo() : CalleeProtoTy(nullptr) {}
  41. CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
  42. : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
  43. CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
  44. : CalleeProtoTy(calleeProtoTy) {}
  45. CGCalleeInfo(GlobalDecl calleeDecl)
  46. : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
  47. const FunctionProtoType *getCalleeFunctionProtoType() const {
  48. return CalleeProtoTy;
  49. }
  50. const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
  51. };
  52. /// All available information about a concrete callee.
  53. class CGCallee {
  54. enum class SpecialKind : uintptr_t {
  55. Invalid,
  56. Builtin,
  57. PseudoDestructor,
  58. Virtual,
  59. Last = Virtual
  60. };
  61. struct BuiltinInfoStorage {
  62. const FunctionDecl *Decl;
  63. unsigned ID;
  64. };
  65. struct PseudoDestructorInfoStorage {
  66. const CXXPseudoDestructorExpr *Expr;
  67. };
  68. struct VirtualInfoStorage {
  69. const CallExpr *CE;
  70. GlobalDecl MD;
  71. Address Addr;
  72. llvm::FunctionType *FTy;
  73. };
  74. SpecialKind KindOrFunctionPointer;
  75. union {
  76. CGCalleeInfo AbstractInfo;
  77. BuiltinInfoStorage BuiltinInfo;
  78. PseudoDestructorInfoStorage PseudoDestructorInfo;
  79. VirtualInfoStorage VirtualInfo;
  80. };
  81. explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
  82. CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
  83. : KindOrFunctionPointer(SpecialKind::Builtin) {
  84. BuiltinInfo.Decl = builtinDecl;
  85. BuiltinInfo.ID = builtinID;
  86. }
  87. public:
  88. CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
  89. /// Construct a callee. Call this constructor directly when this
  90. /// isn't a direct call.
  91. CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
  92. : KindOrFunctionPointer(
  93. SpecialKind(reinterpret_cast<uintptr_t>(functionPtr))) {
  94. AbstractInfo = abstractInfo;
  95. assert(functionPtr && "configuring callee without function pointer");
  96. assert(functionPtr->getType()->isPointerTy());
  97. assert(functionPtr->getType()->isOpaquePointerTy() ||
  98. functionPtr->getType()->getNonOpaquePointerElementType()
  99. ->isFunctionTy());
  100. }
  101. static CGCallee forBuiltin(unsigned builtinID,
  102. const FunctionDecl *builtinDecl) {
  103. CGCallee result(SpecialKind::Builtin);
  104. result.BuiltinInfo.Decl = builtinDecl;
  105. result.BuiltinInfo.ID = builtinID;
  106. return result;
  107. }
  108. static CGCallee forPseudoDestructor(const CXXPseudoDestructorExpr *E) {
  109. CGCallee result(SpecialKind::PseudoDestructor);
  110. result.PseudoDestructorInfo.Expr = E;
  111. return result;
  112. }
  113. static CGCallee forDirect(llvm::Constant *functionPtr,
  114. const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
  115. return CGCallee(abstractInfo, functionPtr);
  116. }
  117. static CGCallee forDirect(llvm::FunctionCallee functionPtr,
  118. const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
  119. return CGCallee(abstractInfo, functionPtr.getCallee());
  120. }
  121. static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
  122. llvm::FunctionType *FTy) {
  123. CGCallee result(SpecialKind::Virtual);
  124. result.VirtualInfo.CE = CE;
  125. result.VirtualInfo.MD = MD;
  126. result.VirtualInfo.Addr = Addr;
  127. result.VirtualInfo.FTy = FTy;
  128. return result;
  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. bool isPseudoDestructor() const {
  142. return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
  143. }
  144. const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
  145. assert(isPseudoDestructor());
  146. return PseudoDestructorInfo.Expr;
  147. }
  148. bool isOrdinary() const {
  149. return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
  150. }
  151. CGCalleeInfo getAbstractInfo() const {
  152. if (isVirtual())
  153. return VirtualInfo.MD;
  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. void setFunctionPointer(llvm::Value *functionPtr) {
  162. assert(isOrdinary());
  163. KindOrFunctionPointer =
  164. SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
  165. }
  166. bool isVirtual() const {
  167. return KindOrFunctionPointer == SpecialKind::Virtual;
  168. }
  169. const CallExpr *getVirtualCallExpr() const {
  170. assert(isVirtual());
  171. return VirtualInfo.CE;
  172. }
  173. GlobalDecl getVirtualMethodDecl() const {
  174. assert(isVirtual());
  175. return VirtualInfo.MD;
  176. }
  177. Address getThisAddress() const {
  178. assert(isVirtual());
  179. return VirtualInfo.Addr;
  180. }
  181. llvm::FunctionType *getVirtualFunctionType() const {
  182. assert(isVirtual());
  183. return VirtualInfo.FTy;
  184. }
  185. /// If this is a delayed callee computation of some sort, prepare
  186. /// a concrete callee.
  187. CGCallee prepareConcreteCallee(CodeGenFunction &CGF) const;
  188. };
  189. struct CallArg {
  190. private:
  191. union {
  192. RValue RV;
  193. LValue LV; /// The argument is semantically a load from this l-value.
  194. };
  195. bool HasLV;
  196. /// A data-flow flag to make sure getRValue and/or copyInto are not
  197. /// called twice for duplicated IR emission.
  198. mutable bool IsUsed;
  199. public:
  200. QualType Ty;
  201. CallArg(RValue rv, QualType ty)
  202. : RV(rv), HasLV(false), IsUsed(false), Ty(ty) {}
  203. CallArg(LValue lv, QualType ty)
  204. : LV(lv), HasLV(true), IsUsed(false), Ty(ty) {}
  205. bool hasLValue() const { return HasLV; }
  206. QualType getType() const { return Ty; }
  207. /// \returns an independent RValue. If the CallArg contains an LValue,
  208. /// a temporary copy is returned.
  209. RValue getRValue(CodeGenFunction &CGF) const;
  210. LValue getKnownLValue() const {
  211. assert(HasLV && !IsUsed);
  212. return LV;
  213. }
  214. RValue getKnownRValue() const {
  215. assert(!HasLV && !IsUsed);
  216. return RV;
  217. }
  218. void setRValue(RValue _RV) {
  219. assert(!HasLV);
  220. RV = _RV;
  221. }
  222. bool isAggregate() const { return HasLV || RV.isAggregate(); }
  223. void copyInto(CodeGenFunction &CGF, Address A) const;
  224. };
  225. /// CallArgList - Type for representing both the value and type of
  226. /// arguments in a call.
  227. class CallArgList : public SmallVector<CallArg, 8> {
  228. public:
  229. CallArgList() : StackBase(nullptr) {}
  230. struct Writeback {
  231. /// The original argument. Note that the argument l-value
  232. /// is potentially null.
  233. LValue Source;
  234. /// The temporary alloca.
  235. Address Temporary;
  236. /// A value to "use" after the writeback, or null.
  237. llvm::Value *ToUse;
  238. };
  239. struct CallArgCleanup {
  240. EHScopeStack::stable_iterator Cleanup;
  241. /// The "is active" insertion point. This instruction is temporary and
  242. /// will be removed after insertion.
  243. llvm::Instruction *IsActiveIP;
  244. };
  245. void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
  246. void addUncopiedAggregate(LValue LV, QualType type) {
  247. push_back(CallArg(LV, type));
  248. }
  249. /// Add all the arguments from another CallArgList to this one. After doing
  250. /// this, the old CallArgList retains its list of arguments, but must not
  251. /// be used to emit a call.
  252. void addFrom(const CallArgList &other) {
  253. insert(end(), other.begin(), other.end());
  254. Writebacks.insert(Writebacks.end(), other.Writebacks.begin(),
  255. other.Writebacks.end());
  256. CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
  257. other.CleanupsToDeactivate.begin(),
  258. other.CleanupsToDeactivate.end());
  259. assert(!(StackBase && other.StackBase) && "can't merge stackbases");
  260. if (!StackBase)
  261. StackBase = other.StackBase;
  262. }
  263. void addWriteback(LValue srcLV, Address temporary, llvm::Value *toUse) {
  264. Writeback writeback = {srcLV, temporary, toUse};
  265. Writebacks.push_back(writeback);
  266. }
  267. bool hasWritebacks() const { return !Writebacks.empty(); }
  268. typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
  269. writeback_const_range;
  270. writeback_const_range writebacks() const {
  271. return writeback_const_range(Writebacks.begin(), Writebacks.end());
  272. }
  273. void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
  274. llvm::Instruction *IsActiveIP) {
  275. CallArgCleanup ArgCleanup;
  276. ArgCleanup.Cleanup = Cleanup;
  277. ArgCleanup.IsActiveIP = IsActiveIP;
  278. CleanupsToDeactivate.push_back(ArgCleanup);
  279. }
  280. ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
  281. return CleanupsToDeactivate;
  282. }
  283. void allocateArgumentMemory(CodeGenFunction &CGF);
  284. llvm::Instruction *getStackBase() const { return StackBase; }
  285. void freeArgumentMemory(CodeGenFunction &CGF) const;
  286. /// Returns if we're using an inalloca struct to pass arguments in
  287. /// memory.
  288. bool isUsingInAlloca() const { return StackBase; }
  289. private:
  290. SmallVector<Writeback, 1> Writebacks;
  291. /// Deactivate these cleanups immediately before making the call. This
  292. /// is used to cleanup objects that are owned by the callee once the call
  293. /// occurs.
  294. SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
  295. /// The stacksave call. It dominates all of the argument evaluation.
  296. llvm::CallInst *StackBase;
  297. };
  298. /// FunctionArgList - Type for representing both the decl and type
  299. /// of parameters to a function. The decl must be either a
  300. /// ParmVarDecl or ImplicitParamDecl.
  301. class FunctionArgList : public SmallVector<const VarDecl *, 16> {};
  302. /// ReturnValueSlot - Contains the address where the return value of a
  303. /// function can be stored, and whether the address is volatile or not.
  304. class ReturnValueSlot {
  305. Address Addr = Address::invalid();
  306. // Return value slot flags
  307. unsigned IsVolatile : 1;
  308. unsigned IsUnused : 1;
  309. unsigned IsExternallyDestructed : 1;
  310. public:
  311. ReturnValueSlot()
  312. : IsVolatile(false), IsUnused(false), IsExternallyDestructed(false) {}
  313. ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false,
  314. bool IsExternallyDestructed = false)
  315. : Addr(Addr), IsVolatile(IsVolatile), IsUnused(IsUnused),
  316. IsExternallyDestructed(IsExternallyDestructed) {}
  317. bool isNull() const { return !Addr.isValid(); }
  318. bool isVolatile() const { return IsVolatile; }
  319. Address getValue() const { return Addr; }
  320. bool isUnused() const { return IsUnused; }
  321. bool isExternallyDestructed() const { return IsExternallyDestructed; }
  322. };
  323. } // end namespace CodeGen
  324. } // end namespace clang
  325. #endif