Function.h revision f3841fcbd587c31aa9842b3f33bd57de40c9f443
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/Support/Annotation.h"
25#include "llvm/Attributes.h"
26
27namespace llvm {
28
29class FunctionType;
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 iplist<BasicBlock> &getList(Function *F);
47  static ValueSymbolTable *getSymTab(Function *ItemParent);
48  static int getListOffset();
49private:
50  mutable ilist_node<BasicBlock> Sentinel;
51};
52
53template<> struct ilist_traits<Argument>
54  : public SymbolTableListTraits<Argument, Function> {
55
56  Argument *createSentinel() const {
57    return static_cast<Argument*>(&Sentinel);
58  }
59  static void destroySentinel(Argument*) {}
60
61  Argument *provideInitialHead() const { return createSentinel(); }
62  Argument *ensureHead(Argument*) const { return createSentinel(); }
63  static void noteHead(Argument*, Argument*) {}
64
65  static iplist<Argument> &getList(Function *F);
66  static ValueSymbolTable *getSymTab(Function *ItemParent);
67  static int getListOffset();
68private:
69  mutable ilist_node<Argument> Sentinel;
70};
71
72class Function : public GlobalValue, public Annotable,
73                 public ilist_node<Function> {
74public:
75  typedef iplist<Argument> ArgumentListType;
76  typedef iplist<BasicBlock> BasicBlockListType;
77
78  // BasicBlock iterators...
79  typedef BasicBlockListType::iterator iterator;
80  typedef BasicBlockListType::const_iterator const_iterator;
81
82  typedef ArgumentListType::iterator arg_iterator;
83  typedef ArgumentListType::const_iterator const_arg_iterator;
84
85private:
86  // Important things that make up a function!
87  BasicBlockListType  BasicBlocks;        ///< The basic blocks
88  mutable ArgumentListType ArgumentList;  ///< The formal arguments
89  ValueSymbolTable *SymTab;               ///< Symbol table of args/instructions
90  AttrListPtr AttributeList;              ///< Parameter attributes
91
92  // The Calling Convention is stored in Value::SubclassData.
93  /*unsigned CallingConvention;*/
94
95  friend class SymbolTableListTraits<Function, Module>;
96
97  void setParent(Module *parent);
98
99  /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
100  /// built on demand, so that the list isn't allocated until the first client
101  /// needs it.  The hasLazyArguments predicate returns true if the arg list
102  /// hasn't been set up yet.
103  bool hasLazyArguments() const {
104    return SubclassData & 1;
105  }
106  void CheckLazyArguments() const {
107    if (hasLazyArguments())
108      BuildLazyArguments();
109  }
110  void BuildLazyArguments() const;
111
112  Function(const Function&); // DO NOT IMPLEMENT
113  void operator=(const Function&); // DO NOT IMPLEMENT
114
115  /// Function ctor - If the (optional) Module argument is specified, the
116  /// function is automatically inserted into the end of the function list for
117  /// the module.
118  ///
119  Function(const FunctionType *Ty, LinkageTypes Linkage,
120           const std::string &N = "", Module *M = 0);
121
122public:
123  static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
124                          const std::string &N = "", Module *M = 0) {
125    return new(0) Function(Ty, Linkage, N, M);
126  }
127
128  ~Function();
129
130  const Type *getReturnType() const;           // Return the type of the ret val
131  const FunctionType *getFunctionType() const; // Return the FunctionType for me
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
309  const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
310        BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
311
312  const BasicBlock       &getEntryBlock() const   { return front(); }
313        BasicBlock       &getEntryBlock()         { return front(); }
314
315  //===--------------------------------------------------------------------===//
316  // Symbol Table Accessing functions...
317
318  /// getSymbolTable() - Return the symbol table...
319  ///
320  inline       ValueSymbolTable &getValueSymbolTable()       { return *SymTab; }
321  inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
322
323
324  //===--------------------------------------------------------------------===//
325  // BasicBlock iterator forwarding functions
326  //
327  iterator                begin()       { return BasicBlocks.begin(); }
328  const_iterator          begin() const { return BasicBlocks.begin(); }
329  iterator                end  ()       { return BasicBlocks.end();   }
330  const_iterator          end  () const { return BasicBlocks.end();   }
331
332  size_t                   size() const { return BasicBlocks.size();  }
333  bool                    empty() const { return BasicBlocks.empty(); }
334  const BasicBlock       &front() const { return BasicBlocks.front(); }
335        BasicBlock       &front()       { return BasicBlocks.front(); }
336  const BasicBlock        &back() const { return BasicBlocks.back();  }
337        BasicBlock        &back()       { return BasicBlocks.back();  }
338
339  //===--------------------------------------------------------------------===//
340  // Argument iterator forwarding functions
341  //
342  arg_iterator arg_begin() {
343    CheckLazyArguments();
344    return ArgumentList.begin();
345  }
346  const_arg_iterator arg_begin() const {
347    CheckLazyArguments();
348    return ArgumentList.begin();
349  }
350  arg_iterator arg_end() {
351    CheckLazyArguments();
352    return ArgumentList.end();
353  }
354  const_arg_iterator arg_end() const {
355    CheckLazyArguments();
356    return ArgumentList.end();
357  }
358
359  size_t arg_size() const;
360  bool arg_empty() const;
361
362  /// viewCFG - This function is meant for use from the debugger.  You can just
363  /// say 'call F->viewCFG()' and a ghostview window should pop up from the
364  /// program, displaying the CFG of the current function with the code for each
365  /// basic block inside.  This depends on there being a 'dot' and 'gv' program
366  /// in your path.
367  ///
368  void viewCFG() const;
369
370  /// viewCFGOnly - This function is meant for use from the debugger.  It works
371  /// just like viewCFG, but it does not include the contents of basic blocks
372  /// into the nodes, just the label.  If you are only interested in the CFG
373  /// this can make the graph smaller.
374  ///
375  void viewCFGOnly() const;
376
377  /// Methods for support type inquiry through isa, cast, and dyn_cast:
378  static inline bool classof(const Function *) { return true; }
379  static inline bool classof(const Value *V) {
380    return V->getValueID() == Value::FunctionVal;
381  }
382
383  /// dropAllReferences() - This method causes all the subinstructions to "let
384  /// go" of all references that they are maintaining.  This allows one to
385  /// 'delete' a whole module at a time, even though there may be circular
386  /// references... first all references are dropped, and all use counts go to
387  /// zero.  Then everything is deleted for real.  Note that no operations are
388  /// valid on an object that has "dropped all references", except operator
389  /// delete.
390  ///
391  /// Since no other object in the module can have references into the body of a
392  /// function, dropping all references deletes the entire body of the function,
393  /// including any contained basic blocks.
394  ///
395  void dropAllReferences();
396
397  static unsigned getBasicBlockListOffset() {
398    Function *Obj = 0;
399    return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
400  }
401  static unsigned getArgumentListOffset() {
402    Function *Obj = 0;
403    return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
404  }
405};
406
407inline ValueSymbolTable *
408ilist_traits<BasicBlock>::getSymTab(Function *F) {
409  return F ? &F->getValueSymbolTable() : 0;
410}
411
412inline ValueSymbolTable *
413ilist_traits<Argument>::getSymTab(Function *F) {
414  return F ? &F->getValueSymbolTable() : 0;
415}
416
417inline int
418ilist_traits<BasicBlock>::getListOffset() {
419  return Function::getBasicBlockListOffset();
420}
421
422inline int
423ilist_traits<Argument>::getListOffset() {
424  return Function::getArgumentListOffset();
425}
426
427
428} // End llvm namespace
429
430#endif
431