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