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.

470 lines
17 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_IR_FUNCTION_H
  18. #define LLVM_IR_FUNCTION_H
  19. #include "llvm/IR/Argument.h"
  20. #include "llvm/IR/Attributes.h"
  21. #include "llvm/IR/BasicBlock.h"
  22. #include "llvm/IR/CallingConv.h"
  23. #include "llvm/IR/GlobalValue.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. AttributeSet AttributeSets; ///< 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. /// Do the actual lookup of an intrinsic ID when the query could not be
  94. /// answered from the cache.
  95. unsigned lookupIntrinsicID() const LLVM_READONLY;
  96. /// Function ctor - If the (optional) Module argument is specified, the
  97. /// function is automatically inserted into the end of the function list for
  98. /// the module.
  99. ///
  100. Function(FunctionType *Ty, LinkageTypes Linkage,
  101. const Twine &N = "", Module *M = 0);
  102. public:
  103. static Function *Create(FunctionType *Ty, LinkageTypes Linkage,
  104. const Twine &N = "", Module *M = 0) {
  105. return new(0) Function(Ty, Linkage, N, M);
  106. }
  107. ~Function();
  108. Type *getReturnType() const; // Return the type of the ret val
  109. FunctionType *getFunctionType() const; // Return the FunctionType for me
  110. /// getContext - Return a pointer to the LLVMContext associated with this
  111. /// function, or NULL if this function is not bound to a context yet.
  112. LLVMContext &getContext() const;
  113. /// isVarArg - Return true if this function takes a variable number of
  114. /// arguments.
  115. bool isVarArg() const;
  116. /// getIntrinsicID - This method returns the ID number of the specified
  117. /// function, or Intrinsic::not_intrinsic if the function is not an
  118. /// intrinsic, or if the pointer is null. This value is always defined to be
  119. /// zero to allow easy checking for whether a function is intrinsic or not.
  120. /// The particular intrinsic functions which correspond to this value are
  121. /// defined in llvm/Intrinsics.h. Results are cached in the LLVM context,
  122. /// subsequent requests for the same ID return results much faster from the
  123. /// cache.
  124. ///
  125. unsigned getIntrinsicID() const LLVM_READONLY;
  126. bool isIntrinsic() const { return getName().startswith("llvm."); }
  127. /// getCallingConv()/setCallingConv(CC) - These method get and set the
  128. /// calling convention of this function. The enum values for the known
  129. /// calling conventions are defined in CallingConv.h.
  130. CallingConv::ID getCallingConv() const {
  131. return static_cast<CallingConv::ID>(getSubclassDataFromValue() >> 1);
  132. }
  133. void setCallingConv(CallingConv::ID CC) {
  134. setValueSubclassData((getSubclassDataFromValue() & 1) |
  135. (static_cast<unsigned>(CC) << 1));
  136. }
  137. /// getAttributes - Return the attribute list for this Function.
  138. ///
  139. AttributeSet getAttributes() const { return AttributeSets; }
  140. /// setAttributes - Set the attribute list for this Function.
  141. ///
  142. void setAttributes(AttributeSet attrs) { AttributeSets = attrs; }
  143. /// addFnAttr - Add function attributes to this function.
  144. ///
  145. void addFnAttr(Attribute::AttrKind N) {
  146. setAttributes(AttributeSets.addAttribute(getContext(),
  147. AttributeSet::FunctionIndex, N));
  148. }
  149. /// addFnAttr - Add function attributes to this function.
  150. ///
  151. void addFnAttr(StringRef Kind) {
  152. setAttributes(
  153. AttributeSets.addAttribute(getContext(),
  154. AttributeSet::FunctionIndex, Kind));
  155. }
  156. /// \brief Return true if the function has the attribute.
  157. bool hasFnAttribute(Attribute::AttrKind Kind) const {
  158. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind);
  159. }
  160. bool hasFnAttribute(StringRef Kind) const {
  161. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind);
  162. }
  163. /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
  164. /// to use during code generation.
  165. bool hasGC() const;
  166. const char *getGC() const;
  167. void setGC(const char *Str);
  168. void clearGC();
  169. /// @brief adds the attribute to the list of attributes.
  170. void addAttribute(unsigned i, Attribute::AttrKind attr);
  171. /// @brief adds the attributes to the list of attributes.
  172. void addAttributes(unsigned i, AttributeSet attrs);
  173. /// @brief removes the attributes from the list of attributes.
  174. void removeAttributes(unsigned i, AttributeSet attr);
  175. /// @brief Extract the alignment for a call or parameter (0=unknown).
  176. unsigned getParamAlignment(unsigned i) const {
  177. return AttributeSets.getParamAlignment(i);
  178. }
  179. /// @brief Determine if the function does not access memory.
  180. bool doesNotAccessMemory() const {
  181. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
  182. Attribute::ReadNone);
  183. }
  184. void setDoesNotAccessMemory() {
  185. addFnAttr(Attribute::ReadNone);
  186. }
  187. /// @brief Determine if the function does not access or only reads memory.
  188. bool onlyReadsMemory() const {
  189. return doesNotAccessMemory() ||
  190. AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
  191. Attribute::ReadOnly);
  192. }
  193. void setOnlyReadsMemory() {
  194. addFnAttr(Attribute::ReadOnly);
  195. }
  196. /// @brief Determine if the function cannot return.
  197. bool doesNotReturn() const {
  198. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
  199. Attribute::NoReturn);
  200. }
  201. void setDoesNotReturn() {
  202. addFnAttr(Attribute::NoReturn);
  203. }
  204. /// @brief Determine if the function cannot unwind.
  205. bool doesNotThrow() const {
  206. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
  207. Attribute::NoUnwind);
  208. }
  209. void setDoesNotThrow() {
  210. addFnAttr(Attribute::NoUnwind);
  211. }
  212. /// @brief Determine if the call cannot be duplicated.
  213. bool cannotDuplicate() const {
  214. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
  215. Attribute::NoDuplicate);
  216. }
  217. void setCannotDuplicate() {
  218. addFnAttr(Attribute::NoDuplicate);
  219. }
  220. /// @brief True if the ABI mandates (or the user requested) that this
  221. /// function be in a unwind table.
  222. bool hasUWTable() const {
  223. return AttributeSets.hasAttribute(AttributeSet::FunctionIndex,
  224. Attribute::UWTable);
  225. }
  226. void setHasUWTable() {
  227. addFnAttr(Attribute::UWTable);
  228. }
  229. /// @brief True if this function needs an unwind table.
  230. bool needsUnwindTableEntry() const {
  231. return hasUWTable() || !doesNotThrow();
  232. }
  233. /// @brief Determine if the function returns a structure through first
  234. /// pointer argument.
  235. bool hasStructRetAttr() const {
  236. return AttributeSets.hasAttribute(1, Attribute::StructRet);
  237. }
  238. /// @brief Determine if the parameter does not alias other parameters.
  239. /// @param n The parameter to check. 1 is the first parameter, 0 is the return
  240. bool doesNotAlias(unsigned n) const {
  241. return AttributeSets.hasAttribute(n, Attribute::NoAlias);
  242. }
  243. void setDoesNotAlias(unsigned n) {
  244. addAttribute(n, Attribute::NoAlias);
  245. }
  246. /// @brief Determine if the parameter can be captured.
  247. /// @param n The parameter to check. 1 is the first parameter, 0 is the return
  248. bool doesNotCapture(unsigned n) const {
  249. return AttributeSets.hasAttribute(n, Attribute::NoCapture);
  250. }
  251. void setDoesNotCapture(unsigned n) {
  252. addAttribute(n, Attribute::NoCapture);
  253. }
  254. /// copyAttributesFrom - copy all additional attributes (those not needed to
  255. /// create a Function) from the Function Src to this one.
  256. void copyAttributesFrom(const GlobalValue *Src);
  257. /// deleteBody - This method deletes the body of the function, and converts
  258. /// the linkage to external.
  259. ///
  260. void deleteBody() {
  261. dropAllReferences();
  262. setLinkage(ExternalLinkage);
  263. }
  264. /// removeFromParent - This method unlinks 'this' from the containing module,
  265. /// but does not delete it.
  266. ///
  267. virtual void removeFromParent();
  268. /// eraseFromParent - This method unlinks 'this' from the containing module
  269. /// and deletes it.
  270. ///
  271. virtual void eraseFromParent();
  272. /// Get the underlying elements of the Function... the basic block list is
  273. /// empty for external functions.
  274. ///
  275. const ArgumentListType &getArgumentList() const {
  276. CheckLazyArguments();
  277. return ArgumentList;
  278. }
  279. ArgumentListType &getArgumentList() {
  280. CheckLazyArguments();
  281. return ArgumentList;
  282. }
  283. static iplist<Argument> Function::*getSublistAccess(Argument*) {
  284. return &Function::ArgumentList;
  285. }
  286. const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
  287. BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
  288. static iplist<BasicBlock> Function::*getSublistAccess(BasicBlock*) {
  289. return &Function::BasicBlocks;
  290. }
  291. const BasicBlock &getEntryBlock() const { return front(); }
  292. BasicBlock &getEntryBlock() { return front(); }
  293. //===--------------------------------------------------------------------===//
  294. // Symbol Table Accessing functions...
  295. /// getSymbolTable() - Return the symbol table...
  296. ///
  297. inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
  298. inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
  299. //===--------------------------------------------------------------------===//
  300. // BasicBlock iterator forwarding functions
  301. //
  302. iterator begin() { return BasicBlocks.begin(); }
  303. const_iterator begin() const { return BasicBlocks.begin(); }
  304. iterator end () { return BasicBlocks.end(); }
  305. const_iterator end () const { return BasicBlocks.end(); }
  306. size_t size() const { return BasicBlocks.size(); }
  307. bool empty() const { return BasicBlocks.empty(); }
  308. const BasicBlock &front() const { return BasicBlocks.front(); }
  309. BasicBlock &front() { return BasicBlocks.front(); }
  310. const BasicBlock &back() const { return BasicBlocks.back(); }
  311. BasicBlock &back() { return BasicBlocks.back(); }
  312. //===--------------------------------------------------------------------===//
  313. // Argument iterator forwarding functions
  314. //
  315. arg_iterator arg_begin() {
  316. CheckLazyArguments();
  317. return ArgumentList.begin();
  318. }
  319. const_arg_iterator arg_begin() const {
  320. CheckLazyArguments();
  321. return ArgumentList.begin();
  322. }
  323. arg_iterator arg_end() {
  324. CheckLazyArguments();
  325. return ArgumentList.end();
  326. }
  327. const_arg_iterator arg_end() const {
  328. CheckLazyArguments();
  329. return ArgumentList.end();
  330. }
  331. size_t arg_size() const;
  332. bool arg_empty() const;
  333. /// viewCFG - This function is meant for use from the debugger. You can just
  334. /// say 'call F->viewCFG()' and a ghostview window should pop up from the
  335. /// program, displaying the CFG of the current function with the code for each
  336. /// basic block inside. This depends on there being a 'dot' and 'gv' program
  337. /// in your path.
  338. ///
  339. void viewCFG() const;
  340. /// viewCFGOnly - This function is meant for use from the debugger. It works
  341. /// just like viewCFG, but it does not include the contents of basic blocks
  342. /// into the nodes, just the label. If you are only interested in the CFG
  343. /// this can make the graph smaller.
  344. ///
  345. void viewCFGOnly() const;
  346. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  347. static inline bool classof(const Value *V) {
  348. return V->getValueID() == Value::FunctionVal;
  349. }
  350. /// dropAllReferences() - This method causes all the subinstructions to "let
  351. /// go" of all references that they are maintaining. This allows one to
  352. /// 'delete' a whole module at a time, even though there may be circular
  353. /// references... first all references are dropped, and all use counts go to
  354. /// zero. Then everything is deleted for real. Note that no operations are
  355. /// valid on an object that has "dropped all references", except operator
  356. /// delete.
  357. ///
  358. /// Since no other object in the module can have references into the body of a
  359. /// function, dropping all references deletes the entire body of the function,
  360. /// including any contained basic blocks.
  361. ///
  362. void dropAllReferences();
  363. /// hasAddressTaken - returns true if there are any uses of this function
  364. /// other than direct calls or invokes to it, or blockaddress expressions.
  365. /// Optionally passes back an offending user for diagnostic purposes.
  366. ///
  367. bool hasAddressTaken(const User** = 0) const;
  368. /// isDefTriviallyDead - Return true if it is trivially safe to remove
  369. /// this function definition from the module (because it isn't externally
  370. /// visible, does not have its address taken, and has no callers). To make
  371. /// this more accurate, call removeDeadConstantUsers first.
  372. bool isDefTriviallyDead() const;
  373. /// callsFunctionThatReturnsTwice - Return true if the function has a call to
  374. /// setjmp or other function that gcc recognizes as "returning twice".
  375. bool callsFunctionThatReturnsTwice() const;
  376. private:
  377. // Shadow Value::setValueSubclassData with a private forwarding method so that
  378. // subclasses cannot accidentally use it.
  379. void setValueSubclassData(unsigned short D) {
  380. Value::setValueSubclassData(D);
  381. }
  382. };
  383. inline ValueSymbolTable *
  384. ilist_traits<BasicBlock>::getSymTab(Function *F) {
  385. return F ? &F->getValueSymbolTable() : 0;
  386. }
  387. inline ValueSymbolTable *
  388. ilist_traits<Argument>::getSymTab(Function *F) {
  389. return F ? &F->getValueSymbolTable() : 0;
  390. }
  391. } // End llvm namespace
  392. #endif