CGCall.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/CanonicalType.h"
  18. #include "clang/AST/GlobalDecl.h"
  19. #include "clang/AST/Type.h"
  20. #include "llvm/IR/Value.h"
  21. // FIXME: Restructure so we don't have to expose so much stuff.
  22. #include "ABIInfo.h"
  23. namespace llvm {
  24. class AttributeList;
  25. class Function;
  26. class Type;
  27. class Value;
  28. }
  29. namespace clang {
  30. class ASTContext;
  31. class Decl;
  32. class FunctionDecl;
  33. class ObjCMethodDecl;
  34. class VarDecl;
  35. namespace CodeGen {
  36. /// Abstract information about a function or function prototype.
  37. class CGCalleeInfo {
  38. /// The function prototype of the callee.
  39. const FunctionProtoType *CalleeProtoTy;
  40. /// The function declaration of the callee.
  41. GlobalDecl CalleeDecl;
  42. public:
  43. explicit CGCalleeInfo() : CalleeProtoTy(nullptr), CalleeDecl() {}
  44. CGCalleeInfo(const FunctionProtoType *calleeProtoTy, GlobalDecl calleeDecl)
  45. : CalleeProtoTy(calleeProtoTy), CalleeDecl(calleeDecl) {}
  46. CGCalleeInfo(const FunctionProtoType *calleeProtoTy)
  47. : CalleeProtoTy(calleeProtoTy), CalleeDecl() {}
  48. CGCalleeInfo(GlobalDecl calleeDecl)
  49. : CalleeProtoTy(nullptr), CalleeDecl(calleeDecl) {}
  50. const FunctionProtoType *getCalleeFunctionProtoType() const {
  51. return CalleeProtoTy;
  52. }
  53. const GlobalDecl getCalleeDecl() const { return CalleeDecl; }
  54. };
  55. /// All available information about a concrete callee.
  56. class CGCallee {
  57. enum class SpecialKind : uintptr_t {
  58. Invalid,
  59. Builtin,
  60. PseudoDestructor,
  61. Virtual,
  62. Last = Virtual
  63. };
  64. struct BuiltinInfoStorage {
  65. const FunctionDecl *Decl;
  66. unsigned ID;
  67. };
  68. struct PseudoDestructorInfoStorage {
  69. const CXXPseudoDestructorExpr *Expr;
  70. };
  71. struct VirtualInfoStorage {
  72. const CallExpr *CE;
  73. GlobalDecl MD;
  74. Address Addr;
  75. llvm::FunctionType *FTy;
  76. };
  77. SpecialKind KindOrFunctionPointer;
  78. union {
  79. CGCalleeInfo AbstractInfo;
  80. BuiltinInfoStorage BuiltinInfo;
  81. PseudoDestructorInfoStorage PseudoDestructorInfo;
  82. VirtualInfoStorage VirtualInfo;
  83. };
  84. explicit CGCallee(SpecialKind kind) : KindOrFunctionPointer(kind) {}
  85. CGCallee(const FunctionDecl *builtinDecl, unsigned builtinID)
  86. : KindOrFunctionPointer(SpecialKind::Builtin) {
  87. BuiltinInfo.Decl = builtinDecl;
  88. BuiltinInfo.ID = builtinID;
  89. }
  90. public:
  91. CGCallee() : KindOrFunctionPointer(SpecialKind::Invalid) {}
  92. /// Construct a callee. Call this constructor directly when this
  93. /// isn't a direct call.
  94. CGCallee(const CGCalleeInfo &abstractInfo, llvm::Value *functionPtr)
  95. : KindOrFunctionPointer(SpecialKind(uintptr_t(functionPtr))) {
  96. AbstractInfo = abstractInfo;
  97. assert(functionPtr && "configuring callee without function pointer");
  98. assert(functionPtr->getType()->isPointerTy());
  99. assert(functionPtr->getType()->getPointerElementType()->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
  118. forDirect(llvm::FunctionCallee functionPtr,
  119. const CGCalleeInfo &abstractInfo = CGCalleeInfo()) {
  120. return CGCallee(abstractInfo, functionPtr.getCallee());
  121. }
  122. static CGCallee forVirtual(const CallExpr *CE, GlobalDecl MD, Address Addr,
  123. llvm::FunctionType *FTy) {
  124. CGCallee result(SpecialKind::Virtual);
  125. result.VirtualInfo.CE = CE;
  126. result.VirtualInfo.MD = MD;
  127. result.VirtualInfo.Addr = Addr;
  128. result.VirtualInfo.FTy = FTy;
  129. return result;
  130. }
  131. bool isBuiltin() const {
  132. return KindOrFunctionPointer == SpecialKind::Builtin;
  133. }
  134. const FunctionDecl *getBuiltinDecl() const {
  135. assert(isBuiltin());
  136. return BuiltinInfo.Decl;
  137. }
  138. unsigned getBuiltinID() const {
  139. assert(isBuiltin());
  140. return BuiltinInfo.ID;
  141. }
  142. bool isPseudoDestructor() const {
  143. return KindOrFunctionPointer == SpecialKind::PseudoDestructor;
  144. }
  145. const CXXPseudoDestructorExpr *getPseudoDestructorExpr() const {
  146. assert(isPseudoDestructor());
  147. return PseudoDestructorInfo.Expr;
  148. }
  149. bool isOrdinary() const {
  150. return uintptr_t(KindOrFunctionPointer) > uintptr_t(SpecialKind::Last);
  151. }
  152. CGCalleeInfo getAbstractInfo() const {
  153. if (isVirtual())
  154. return VirtualInfo.MD;
  155. assert(isOrdinary());
  156. return AbstractInfo;
  157. }
  158. llvm::Value *getFunctionPointer() const {
  159. assert(isOrdinary());
  160. return reinterpret_cast<llvm::Value*>(uintptr_t(KindOrFunctionPointer));
  161. }
  162. void setFunctionPointer(llvm::Value *functionPtr) {
  163. assert(isOrdinary());
  164. KindOrFunctionPointer = SpecialKind(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 :
  228. public SmallVector<CallArg, 8> {
  229. public:
  230. CallArgList() : StackBase(nullptr) {}
  231. struct Writeback {
  232. /// The original argument. Note that the argument l-value
  233. /// is potentially null.
  234. LValue Source;
  235. /// The temporary alloca.
  236. Address Temporary;
  237. /// A value to "use" after the writeback, or null.
  238. llvm::Value *ToUse;
  239. };
  240. struct CallArgCleanup {
  241. EHScopeStack::stable_iterator Cleanup;
  242. /// The "is active" insertion point. This instruction is temporary and
  243. /// will be removed after insertion.
  244. llvm::Instruction *IsActiveIP;
  245. };
  246. void add(RValue rvalue, QualType type) { push_back(CallArg(rvalue, type)); }
  247. void addUncopiedAggregate(LValue LV, QualType type) {
  248. push_back(CallArg(LV, type));
  249. }
  250. /// Add all the arguments from another CallArgList to this one. After doing
  251. /// this, the old CallArgList retains its list of arguments, but must not
  252. /// be used to emit a call.
  253. void addFrom(const CallArgList &other) {
  254. insert(end(), other.begin(), other.end());
  255. Writebacks.insert(Writebacks.end(),
  256. other.Writebacks.begin(), other.Writebacks.end());
  257. CleanupsToDeactivate.insert(CleanupsToDeactivate.end(),
  258. other.CleanupsToDeactivate.begin(),
  259. other.CleanupsToDeactivate.end());
  260. assert(!(StackBase && other.StackBase) && "can't merge stackbases");
  261. if (!StackBase)
  262. StackBase = other.StackBase;
  263. }
  264. void addWriteback(LValue srcLV, Address temporary,
  265. llvm::Value *toUse) {
  266. Writeback writeback = { srcLV, temporary, toUse };
  267. Writebacks.push_back(writeback);
  268. }
  269. bool hasWritebacks() const { return !Writebacks.empty(); }
  270. typedef llvm::iterator_range<SmallVectorImpl<Writeback>::const_iterator>
  271. writeback_const_range;
  272. writeback_const_range writebacks() const {
  273. return writeback_const_range(Writebacks.begin(), Writebacks.end());
  274. }
  275. void addArgCleanupDeactivation(EHScopeStack::stable_iterator Cleanup,
  276. llvm::Instruction *IsActiveIP) {
  277. CallArgCleanup ArgCleanup;
  278. ArgCleanup.Cleanup = Cleanup;
  279. ArgCleanup.IsActiveIP = IsActiveIP;
  280. CleanupsToDeactivate.push_back(ArgCleanup);
  281. }
  282. ArrayRef<CallArgCleanup> getCleanupsToDeactivate() const {
  283. return CleanupsToDeactivate;
  284. }
  285. void allocateArgumentMemory(CodeGenFunction &CGF);
  286. llvm::Instruction *getStackBase() const { return StackBase; }
  287. void freeArgumentMemory(CodeGenFunction &CGF) const;
  288. /// Returns if we're using an inalloca struct to pass arguments in
  289. /// memory.
  290. bool isUsingInAlloca() const { return StackBase; }
  291. private:
  292. SmallVector<Writeback, 1> Writebacks;
  293. /// Deactivate these cleanups immediately before making the call. This
  294. /// is used to cleanup objects that are owned by the callee once the call
  295. /// occurs.
  296. SmallVector<CallArgCleanup, 1> CleanupsToDeactivate;
  297. /// The stacksave call. It dominates all of the argument evaluation.
  298. llvm::CallInst *StackBase;
  299. };
  300. /// FunctionArgList - Type for representing both the decl and type
  301. /// of parameters to a function. The decl must be either a
  302. /// ParmVarDecl or ImplicitParamDecl.
  303. class FunctionArgList : public SmallVector<const VarDecl*, 16> {
  304. };
  305. /// ReturnValueSlot - Contains the address where the return value of a
  306. /// function can be stored, and whether the address is volatile or not.
  307. class ReturnValueSlot {
  308. llvm::PointerIntPair<llvm::Value *, 2, unsigned int> Value;
  309. CharUnits Alignment;
  310. // Return value slot flags
  311. enum Flags {
  312. IS_VOLATILE = 0x1,
  313. IS_UNUSED = 0x2,
  314. };
  315. public:
  316. ReturnValueSlot() {}
  317. ReturnValueSlot(Address Addr, bool IsVolatile, bool IsUnused = false)
  318. : Value(Addr.isValid() ? Addr.getPointer() : nullptr,
  319. (IsVolatile ? IS_VOLATILE : 0) | (IsUnused ? IS_UNUSED : 0)),
  320. Alignment(Addr.isValid() ? Addr.getAlignment() : CharUnits::Zero()) {}
  321. bool isNull() const { return !getValue().isValid(); }
  322. bool isVolatile() const { return Value.getInt() & IS_VOLATILE; }
  323. Address getValue() const { return Address(Value.getPointer(), Alignment); }
  324. bool isUnused() const { return Value.getInt() & IS_UNUSED; }
  325. };
  326. } // end namespace CodeGen
  327. } // end namespace clang
  328. #endif