Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
16 KiB

  1. //===-- llvm/Function.h - Class to represent a single function --*- 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. // This file contains the declaration of the Function class, which represents a
  11. // single function/procedure in LLVM.
  12. //
  13. // A function basically consists of a list of basic blocks, a list of arguments,
  14. // and a symbol table.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_FUNCTION_H
  18. #define LLVM_FUNCTION_H
  19. #include "llvm/GlobalValue.h"
  20. #include "llvm/CallingConv.h"
  21. #include "llvm/BasicBlock.h"
  22. #include "llvm/Argument.h"
  23. #include "llvm/Attributes.h"
  24. #include "llvm/Support/Compiler.h"
  25. namespace llvm {
  26. class FunctionType;
  27. class LLVMContext;
  28. // Traits for intrusive list of basic blocks...
  29. template<> struct ilist_traits<BasicBlock>
  30. : public SymbolTableListTraits<BasicBlock, Function> {
  31. // createSentinel is used to get hold of the node that marks the end of the
  32. // list... (same trick used here as in ilist_traits<Instruction>)
  33. BasicBlock *createSentinel() const {
  34. return static_cast<BasicBlock*>(&Sentinel);
  35. }
  36. static void destroySentinel(BasicBlock*) {}
  37. BasicBlock *provideInitialHead() const { return createSentinel(); }
  38. BasicBlock *ensureHead(BasicBlock*) const { return createSentinel(); }
  39. static void noteHead(BasicBlock*, BasicBlock*) {}
  40. static ValueSymbolTable *getSymTab(Function *ItemParent);
  41. private:
  42. mutable ilist_half_node<BasicBlock> Sentinel;
  43. };
  44. template<> struct ilist_traits<Argument>
  45. : public SymbolTableListTraits<Argument, Function> {
  46. Argument *createSentinel() const {
  47. return static_cast<Argument*>(&Sentinel);
  48. }
  49. static void destroySentinel(Argument*) {}
  50. Argument *provideInitialHead() const { return createSentinel(); }
  51. Argument *ensureHead(Argument*) const { return createSentinel(); }
  52. static void noteHead(Argument*, Argument*) {}
  53. static ValueSymbolTable *getSymTab(Function *ItemParent);
  54. private:
  55. mutable ilist_half_node<Argument> Sentinel;
  56. };
  57. class Function : public GlobalValue,
  58. public ilist_node<Function> {
  59. public:
  60. typedef iplist<Argument> ArgumentListType;
  61. typedef iplist<BasicBlock> BasicBlockListType;
  62. // BasicBlock iterators...
  63. typedef BasicBlockListType::iterator iterator;
  64. typedef BasicBlockListType::const_iterator const_iterator;
  65. typedef ArgumentListType::iterator arg_iterator;
  66. typedef ArgumentListType::const_iterator const_arg_iterator;
  67. private:
  68. // Important things that make up a function!
  69. BasicBlockListType BasicBlocks; ///< The basic blocks
  70. mutable ArgumentListType ArgumentList; ///< The formal arguments
  71. ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
  72. AttrListPtr AttributeList; ///< Parameter attributes
  73. // HasLazyArguments is stored in Value::SubclassData.
  74. /*bool HasLazyArguments;*/
  75. // The Calling Convention is stored in Value::SubclassData.
  76. /*CallingConv::ID CallingConvention;*/
  77. friend class SymbolTableListTraits<Function, Module>;
  78. void setParent(Module *parent);
  79. /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
  80. /// built on demand, so that the list isn't allocated until the first client
  81. /// needs it. The hasLazyArguments predicate returns true if the arg list
  82. /// hasn't been set up yet.
  83. bool hasLazyArguments() const {
  84. return getSubclassDataFromValue() & 1;
  85. }
  86. void CheckLazyArguments() const {
  87. if (hasLazyArguments())
  88. BuildLazyArguments();
  89. }
  90. void BuildLazyArguments() const;
  91. Function(const Function&) LLVM_DELETED_FUNCTION;
  92. void operator=(const Function&) LLVM_DELETED_FUNCTION;
  93. /// Function ctor - If the (optional) Module argument is specified, the
  94. /// function is automatically inserted into the end of the function list for
  95. /// the module.
  96. ///
  97. Function(FunctionType *Ty, LinkageTypes Linkage,
  98. const Twine &N = "", Module *M = 0);
  99. public:
  100. static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
  101. const Twine &N = "", Module *M = 0) {
  102. return new(0) Function(Ty, Linkage, N, M);
  103. }
  104. ~Function();
  105. Type *getReturnType() const; // Return the type of the ret val
  106. FunctionType *getFunctionType() const; // Return the FunctionType for me
  107. /// getContext - Return a pointer to the LLVMContext associated with this
  108. /// function, or NULL if this function is not bound to a context yet.
  109. LLVMContext &getContext() const;
  110. /// isVarArg - Return true if this function takes a variable number of
  111. /// arguments.
  112. bool isVarArg() const;
  113. /// getIntrinsicID - This method returns the ID number of the specified
  114. /// function, or Intrinsic::not_intrinsic if the function is not an
  115. /// instrinsic, or if the pointer is null. This value is always defined to be
  116. /// zero to allow easy checking for whether a function is intrinsic or not.
  117. /// The particular intrinsic functions which correspond to this value are
  118. /// defined in llvm/Intrinsics.h.
  119. ///
  120. unsigned getIntrinsicID() const LLVM_READONLY;
  121. bool isIntrinsic() const { return getIntrinsicID() != 0; }
  122. /// getCallingConv()/setCallingConv(CC) - These method get and set the
  123. /// calling convention of this function. The enum values for the known
  124. /// calling conventions are defined in CallingConv.h.
  125. CallingConv::ID getCallingConv() const {
  126. return static_cast<CallingConv::ID>(getSubclassDataFromValue() >> 1);
  127. }
  128. void setCallingConv(CallingConv::ID CC) {
  129. setValueSubclassData((getSubclassDataFromValue() & 1) |
  130. (static_cast<unsigned>(CC) << 1));
  131. }
  132. /// getAttributes - Return the attribute list for this Function.
  133. ///
  134. const AttrListPtr &getAttributes() const { return AttributeList; }
  135. /// setAttributes - Set the attribute list for this Function.
  136. ///
  137. void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
  138. /// getFnAttributes - Return the function attributes for querying.
  139. ///
  140. Attributes getFnAttributes() const {
  141. return AttributeList.getFnAttributes();
  142. }
  143. /// addFnAttr - Add function attributes to this function.
  144. ///
  145. void addFnAttr(Attributes N) {
  146. // Function Attributes are stored at ~0 index
  147. addAttribute(~0U, N);
  148. }
  149. /// removeFnAttr - Remove function attributes from this function.
  150. ///
  151. void removeFnAttr(Attributes N) {
  152. // Function Attributes are stored at ~0 index
  153. removeAttribute(~0U, N);
  154. }
  155. /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
  156. /// to use during code generation.
  157. bool hasGC() const;
  158. const char *getGC() const;
  159. void setGC(const char *Str);
  160. void clearGC();
  161. /// getRetAttributes - Return the return attributes for querying.
  162. Attributes getRetAttributes() const {
  163. return AttributeList.getRetAttributes();
  164. }
  165. /// getParamAttributes - Return the parameter attributes for querying.
  166. Attributes getParamAttributes(unsigned Idx) const {
  167. return AttributeList.getParamAttributes(Idx);
  168. }
  169. /// addAttribute - adds the attribute to the list of attributes.
  170. void addAttribute(unsigned i, Attributes attr);
  171. /// removeAttribute - removes the attribute from the list of attributes.
  172. void removeAttribute(unsigned i, Attributes attr);
  173. /// @brief Extract the alignment for a call or parameter (0=unknown).
  174. unsigned getParamAlignment(unsigned i) const {
  175. return AttributeList.getParamAlignment(i);
  176. }
  177. /// @brief Determine if the function does not access memory.
  178. bool doesNotAccessMemory() const {
  179. return getFnAttributes().hasAttribute(Attributes::ReadNone);
  180. }
  181. void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
  182. if (DoesNotAccessMemory) addFnAttr(Attribute::ReadNone);
  183. else removeFnAttr(Attribute::ReadNone);
  184. }
  185. /// @brief Determine if the function does not access or only reads memory.
  186. bool onlyReadsMemory() const {
  187. return doesNotAccessMemory() ||
  188. getFnAttributes().hasAttribute(Attributes::ReadOnly);
  189. }
  190. void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
  191. if (OnlyReadsMemory) addFnAttr(Attribute::ReadOnly);
  192. else removeFnAttr(Attribute::ReadOnly | Attribute::ReadNone);
  193. }
  194. /// @brief Determine if the function cannot return.
  195. bool doesNotReturn() const {
  196. return getFnAttributes().hasAttribute(Attributes::NoReturn);
  197. }
  198. void setDoesNotReturn(bool DoesNotReturn = true) {
  199. if (DoesNotReturn) addFnAttr(Attribute::NoReturn);
  200. else removeFnAttr(Attribute::NoReturn);
  201. }
  202. /// @brief Determine if the function cannot unwind.
  203. bool doesNotThrow() const {
  204. return getFnAttributes().hasAttribute(Attributes::NoUnwind);
  205. }
  206. void setDoesNotThrow(bool DoesNotThrow = true) {
  207. if (DoesNotThrow) addFnAttr(Attribute::NoUnwind);
  208. else removeFnAttr(Attribute::NoUnwind);
  209. }
  210. /// @brief True if the ABI mandates (or the user requested) that this
  211. /// function be in a unwind table.
  212. bool hasUWTable() const {
  213. return getFnAttributes().hasAttribute(Attributes::UWTable);
  214. }
  215. void setHasUWTable(bool HasUWTable = true) {
  216. if (HasUWTable)
  217. addFnAttr(Attribute::UWTable);
  218. else
  219. removeFnAttr(Attribute::UWTable);
  220. }
  221. /// @brief True if this function needs an unwind table.
  222. bool needsUnwindTableEntry() const {
  223. return hasUWTable() || !doesNotThrow();
  224. }
  225. /// @brief Determine if the function returns a structure through first
  226. /// pointer argument.
  227. bool hasStructRetAttr() const {
  228. return getParamAttributes(1).hasAttribute(Attributes::StructRet);
  229. }
  230. /// @brief Determine if the parameter does not alias other parameters.
  231. /// @param n The parameter to check. 1 is the first parameter, 0 is the return
  232. bool doesNotAlias(unsigned n) const {
  233. return getParamAttributes(n).hasAttribute(Attributes::NoAlias);
  234. }
  235. void setDoesNotAlias(unsigned n, bool DoesNotAlias = true) {
  236. if (DoesNotAlias) addAttribute(n, Attribute::NoAlias);
  237. else removeAttribute(n, Attribute::NoAlias);
  238. }
  239. /// @brief Determine if the parameter can be captured.
  240. /// @param n The parameter to check. 1 is the first parameter, 0 is the return
  241. bool doesNotCapture(unsigned n) const {
  242. return getParamAttributes(n).hasAttribute(Attributes::NoCapture);
  243. }
  244. void setDoesNotCapture(unsigned n, bool DoesNotCapture = true) {
  245. if (DoesNotCapture) addAttribute(n, Attribute::NoCapture);
  246. else removeAttribute(n, Attribute::NoCapture);
  247. }
  248. /// copyAttributesFrom - copy all additional attributes (those not needed to
  249. /// create a Function) from the Function Src to this one.
  250. void copyAttributesFrom(const GlobalValue *Src);
  251. /// deleteBody - This method deletes the body of the function, and converts
  252. /// the linkage to external.
  253. ///
  254. void deleteBody() {
  255. dropAllReferences();
  256. setLinkage(ExternalLinkage);
  257. }
  258. /// removeFromParent - This method unlinks 'this' from the containing module,
  259. /// but does not delete it.
  260. ///
  261. virtual void removeFromParent();
  262. /// eraseFromParent - This method unlinks 'this' from the containing module
  263. /// and deletes it.
  264. ///
  265. virtual void eraseFromParent();
  266. /// Get the underlying elements of the Function... the basic block list is
  267. /// empty for external functions.
  268. ///
  269. const ArgumentListType &getArgumentList() const {
  270. CheckLazyArguments();
  271. return ArgumentList;
  272. }
  273. ArgumentListType &getArgumentList() {
  274. CheckLazyArguments();
  275. return ArgumentList;
  276. }
  277. static iplist<Argument> Function::*getSublistAccess(Argument*) {
  278. return &Function::ArgumentList;
  279. }
  280. const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
  281. BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
  282. static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
  283. return &Function::BasicBlocks;
  284. }
  285. const BasicBlock &getEntryBlock() const { return front(); }
  286. BasicBlock &getEntryBlock() { return front(); }
  287. //===--------------------------------------------------------------------===//
  288. // Symbol Table Accessing functions...
  289. /// getSymbolTable() - Return the symbol table...
  290. ///
  291. inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
  292. inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
  293. //===--------------------------------------------------------------------===//
  294. // BasicBlock iterator forwarding functions
  295. //
  296. iterator begin() { return BasicBlocks.begin(); }
  297. const_iterator begin() const { return BasicBlocks.begin(); }
  298. iterator end () { return BasicBlocks.end(); }
  299. const_iterator end () const { return BasicBlocks.end(); }
  300. size_t size() const { return BasicBlocks.size(); }
  301. bool empty() const { return BasicBlocks.empty(); }
  302. const BasicBlock &front() const { return BasicBlocks.front(); }
  303. BasicBlock &front() { return BasicBlocks.front(); }
  304. const BasicBlock &back() const { return BasicBlocks.back(); }
  305. BasicBlock &back() { return BasicBlocks.back(); }
  306. //===--------------------------------------------------------------------===//
  307. // Argument iterator forwarding functions
  308. //
  309. arg_iterator arg_begin() {
  310. CheckLazyArguments();
  311. return ArgumentList.begin();
  312. }
  313. const_arg_iterator arg_begin() const {
  314. CheckLazyArguments();
  315. return ArgumentList.begin();
  316. }
  317. arg_iterator arg_end() {
  318. CheckLazyArguments();
  319. return ArgumentList.end();
  320. }
  321. const_arg_iterator arg_end() const {
  322. CheckLazyArguments();
  323. return ArgumentList.end();
  324. }
  325. size_t arg_size() const;
  326. bool arg_empty() const;
  327. /// viewCFG - This function is meant for use from the debugger. You can just
  328. /// say 'call F->viewCFG()' and a ghostview window should pop up from the
  329. /// program, displaying the CFG of the current function with the code for each
  330. /// basic block inside. This depends on there being a 'dot' and 'gv' program
  331. /// in your path.
  332. ///
  333. void viewCFG() const;
  334. /// viewCFGOnly - This function is meant for use from the debugger. It works
  335. /// just like viewCFG, but it does not include the contents of basic blocks
  336. /// into the nodes, just the label. If you are only interested in the CFG
  337. /// this can make the graph smaller.
  338. ///
  339. void viewCFGOnly() const;
  340. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  341. static inline bool classof(const Function *) { return true; }
  342. static inline bool classof(const Value *V) {
  343. return V->getValueID() == Value::FunctionVal;
  344. }
  345. /// dropAllReferences() - This method causes all the subinstructions to "let
  346. /// go" of all references that they are maintaining. This allows one to
  347. /// 'delete' a whole module at a time, even though there may be circular
  348. /// references... first all references are dropped, and all use counts go to
  349. /// zero. Then everything is deleted for real. Note that no operations are
  350. /// valid on an object that has "dropped all references", except operator
  351. /// delete.
  352. ///
  353. /// Since no other object in the module can have references into the body of a
  354. /// function, dropping all references deletes the entire body of the function,
  355. /// including any contained basic blocks.
  356. ///
  357. void dropAllReferences();
  358. /// hasAddressTaken - returns true if there are any uses of this function
  359. /// other than direct calls or invokes to it, or blockaddress expressions.
  360. /// Optionally passes back an offending user for diagnostic purposes.
  361. ///
  362. bool hasAddressTaken(const User** = 0) const;
  363. /// isDefTriviallyDead - Return true if it is trivially safe to remove
  364. /// this function definition from the module (because it isn't externally
  365. /// visible, does not have its address taken, and has no callers). To make
  366. /// this more accurate, call removeDeadConstantUsers first.
  367. bool isDefTriviallyDead() const;
  368. /// callsFunctionThatReturnsTwice - Return true if the function has a call to
  369. /// setjmp or other function that gcc recognizes as "returning twice".
  370. bool callsFunctionThatReturnsTwice() const;
  371. private:
  372. // Shadow Value::setValueSubclassData with a private forwarding method so that
  373. // subclasses cannot accidentally use it.
  374. void setValueSubclassData(unsigned short D) {
  375. Value::setValueSubclassData(D);
  376. }
  377. };
  378. inline ValueSymbolTable *
  379. ilist_traits<BasicBlock>::getSymTab(Function *F) {
  380. return F ? &F->getValueSymbolTable() : 0;
  381. }
  382. inline ValueSymbolTable *
  383. ilist_traits<Argument>::getSymTab(Function *F) {
  384. return F ? &F->getValueSymbolTable() : 0;
  385. }
  386. } // End llvm namespace
  387. #endif