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