1 //===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the Function class, which represents a
11 // single function/procedure in LLVM.
13 // A function basically consists of a list of basic blocks, a list of arguments,
14 // and a symbol table.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_FUNCTION_H
19 #define LLVM_FUNCTION_H
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"
31 // Traits for intrusive list of instructions...
32 template<> struct ilist_traits<BasicBlock>
33 : public SymbolTableListTraits<BasicBlock, Function> {
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();
43 template<> struct ilist_traits<Argument>
44 : public SymbolTableListTraits<Argument, Function> {
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();
54 typedef unsigned FunctionNotes;
55 const FunctionNotes FN_NOTE_None = 0;
56 const FunctionNotes FN_NOTE_NoInline = 1<<0;
57 const FunctionNotes FN_NOTE_AlwaysInline = 1<<1;
58 const FunctionNotes FN_NOTE_OptimizeForSize = 1<<2;
60 class Function : public GlobalValue, public Annotable,
61 public ilist_node<Function> {
63 typedef iplist<Argument> ArgumentListType;
64 typedef iplist<BasicBlock> BasicBlockListType;
66 // BasicBlock iterators...
67 typedef BasicBlockListType::iterator iterator;
68 typedef BasicBlockListType::const_iterator const_iterator;
70 typedef ArgumentListType::iterator arg_iterator;
71 typedef ArgumentListType::const_iterator const_arg_iterator;
74 // Important things that make up a function!
75 BasicBlockListType BasicBlocks; ///< The basic blocks
76 mutable ArgumentListType ArgumentList; ///< The formal arguments
77 ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
78 PAListPtr ParamAttrs; ///< Parameter attributes
79 FunctionNotes Notes; ///< Function properties
81 // The Calling Convention is stored in Value::SubclassData.
82 /*unsigned CallingConvention;*/
84 friend class SymbolTableListTraits<Function, Module>;
86 void setParent(Module *parent);
88 /// hasLazyArguments/CheckLazyArguments - The argument list of a function is
89 /// built on demand, so that the list isn't allocated until the first client
90 /// needs it. The hasLazyArguments predicate returns true if the arg list
91 /// hasn't been set up yet.
92 bool hasLazyArguments() const {
93 return SubclassData & 1;
95 void CheckLazyArguments() const {
96 if (hasLazyArguments())
99 void BuildLazyArguments() const;
101 Function(const Function&); // DO NOT IMPLEMENT
102 void operator=(const Function&); // DO NOT IMPLEMENT
104 /// Function ctor - If the (optional) Module argument is specified, the
105 /// function is automatically inserted into the end of the function list for
108 Function(const FunctionType *Ty, LinkageTypes Linkage,
109 const std::string &N = "", Module *M = 0);
112 static Function *Create(const FunctionType *Ty, LinkageTypes Linkage,
113 const std::string &N = "", Module *M = 0) {
114 return new(0) Function(Ty, Linkage, N, M);
119 const Type *getReturnType() const; // Return the type of the ret val
120 const FunctionType *getFunctionType() const; // Return the FunctionType for me
122 /// isVarArg - Return true if this function takes a variable number of
124 bool isVarArg() const;
126 /// isDeclaration - Is the body of this function unknown? (The basic block
127 /// list is empty if so.) This is true for function declarations, but not
128 /// true for function definitions.
130 virtual bool isDeclaration() const { return BasicBlocks.empty(); }
132 /// getIntrinsicID - This method returns the ID number of the specified
133 /// function, or Intrinsic::not_intrinsic if the function is not an
134 /// instrinsic, or if the pointer is null. This value is always defined to be
135 /// zero to allow easy checking for whether a function is intrinsic or not.
136 /// The particular intrinsic functions which correspond to this value are
137 /// defined in llvm/Intrinsics.h.
139 unsigned getIntrinsicID(bool noAssert = false) const;
140 bool isIntrinsic() const { return getIntrinsicID() != 0; }
142 /// getCallingConv()/setCallingConv(uint) - These method get and set the
143 /// calling convention of this function. The enum values for the known
144 /// calling conventions are defined in CallingConv.h.
145 unsigned getCallingConv() const { return SubclassData >> 1; }
146 void setCallingConv(unsigned CC) {
147 SubclassData = (SubclassData & 1) | (CC << 1);
150 /// getParamAttrs - Return the parameter attributes for this Function.
152 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
154 /// setParamAttrs - Set the parameter attributes for this Function.
156 void setParamAttrs(const PAListPtr &attrs) { ParamAttrs = attrs; }
158 /// getNotes - Return function notes
160 const FunctionNotes &getNotes() const { return Notes; }
162 /// hasNote - Return true if this function has given note.
163 bool hasNote(FunctionNotes N) const {
164 return (!isDeclaration() && (Notes & N));
167 /// setNotes - Set notes for this function
169 void setNotes(const FunctionNotes P) { Notes = Notes | P;}
171 /// hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm
172 /// to use during code generation.
174 const char *getGC() const;
175 void setGC(const char *Str);
178 /// @brief Determine whether the function has the given attribute.
179 bool paramHasAttr(unsigned i, ParameterAttributes attr) const {
180 return ParamAttrs.paramHasAttr(i, attr);
183 /// addParamAttr - adds the attribute to the list of attributes.
184 void addParamAttr(unsigned i, ParameterAttributes attr);
186 /// removeParamAttr - removes the attribute from the list of attributes.
187 void removeParamAttr(unsigned i, ParameterAttributes attr);
189 /// @brief Extract the alignment for a call or parameter (0=unknown).
190 unsigned getParamAlignment(unsigned i) const {
191 return ParamAttrs.getParamAlignment(i);
194 /// @brief Determine if the function does not access memory.
195 bool doesNotAccessMemory() const {
196 return paramHasAttr(0, ParamAttr::ReadNone);
198 void setDoesNotAccessMemory(bool DoesNotAccessMemory = true) {
199 if (DoesNotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
200 else removeParamAttr(0, ParamAttr::ReadNone);
203 /// @brief Determine if the function does not access or only reads memory.
204 bool onlyReadsMemory() const {
205 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
207 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
208 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
209 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
212 /// @brief Determine if the function cannot return.
213 bool doesNotReturn() const {
214 return paramHasAttr(0, ParamAttr::NoReturn);
216 void setDoesNotReturn(bool DoesNotReturn = true) {
217 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
218 else removeParamAttr(0, ParamAttr::NoReturn);
221 /// @brief Determine if the function cannot unwind.
222 bool doesNotThrow() const {
223 return paramHasAttr(0, ParamAttr::NoUnwind);
225 void setDoesNotThrow(bool DoesNotThrow = true) {
226 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
227 else removeParamAttr(0, ParamAttr::NoUnwind);
230 /// @brief Determine if the function returns a structure through first
231 /// pointer argument.
232 bool hasStructRetAttr() const {
233 return paramHasAttr(1, ParamAttr::StructRet);
236 /// copyAttributesFrom - copy all additional attributes (those not needed to
237 /// create a Function) from the Function Src to this one.
238 void copyAttributesFrom(const GlobalValue *Src);
240 /// deleteBody - This method deletes the body of the function, and converts
241 /// the linkage to external.
245 setLinkage(ExternalLinkage);
248 /// removeFromParent - This method unlinks 'this' from the containing module,
249 /// but does not delete it.
251 virtual void removeFromParent();
253 /// eraseFromParent - This method unlinks 'this' from the containing module
256 virtual void eraseFromParent();
259 /// Get the underlying elements of the Function... the basic block list is
260 /// empty for external functions.
262 const ArgumentListType &getArgumentList() const {
263 CheckLazyArguments();
266 ArgumentListType &getArgumentList() {
267 CheckLazyArguments();
271 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
272 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
274 const BasicBlock &getEntryBlock() const { return front(); }
275 BasicBlock &getEntryBlock() { return front(); }
277 //===--------------------------------------------------------------------===//
278 // Symbol Table Accessing functions...
280 /// getSymbolTable() - Return the symbol table...
282 inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
283 inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
286 //===--------------------------------------------------------------------===//
287 // BasicBlock iterator forwarding functions
289 iterator begin() { return BasicBlocks.begin(); }
290 const_iterator begin() const { return BasicBlocks.begin(); }
291 iterator end () { return BasicBlocks.end(); }
292 const_iterator end () const { return BasicBlocks.end(); }
294 size_t size() const { return BasicBlocks.size(); }
295 bool empty() const { return BasicBlocks.empty(); }
296 const BasicBlock &front() const { return BasicBlocks.front(); }
297 BasicBlock &front() { return BasicBlocks.front(); }
298 const BasicBlock &back() const { return BasicBlocks.back(); }
299 BasicBlock &back() { return BasicBlocks.back(); }
301 //===--------------------------------------------------------------------===//
302 // Argument iterator forwarding functions
304 arg_iterator arg_begin() {
305 CheckLazyArguments();
306 return ArgumentList.begin();
308 const_arg_iterator arg_begin() const {
309 CheckLazyArguments();
310 return ArgumentList.begin();
312 arg_iterator arg_end() {
313 CheckLazyArguments();
314 return ArgumentList.end();
316 const_arg_iterator arg_end() const {
317 CheckLazyArguments();
318 return ArgumentList.end();
321 size_t arg_size() const;
322 bool arg_empty() const;
324 /// viewCFG - This function is meant for use from the debugger. You can just
325 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
326 /// program, displaying the CFG of the current function with the code for each
327 /// basic block inside. This depends on there being a 'dot' and 'gv' program
330 void viewCFG() const;
332 /// viewCFGOnly - This function is meant for use from the debugger. It works
333 /// just like viewCFG, but it does not include the contents of basic blocks
334 /// into the nodes, just the label. If you are only interested in the CFG
335 /// this can make the graph smaller.
337 void viewCFGOnly() const;
339 /// Methods for support type inquiry through isa, cast, and dyn_cast:
340 static inline bool classof(const Function *) { return true; }
341 static inline bool classof(const Value *V) {
342 return V->getValueID() == Value::FunctionVal;
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
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.
357 void dropAllReferences();
359 static unsigned getBasicBlockListOffset() {
361 return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
363 static unsigned getArgumentListOffset() {
365 return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
369 inline ValueSymbolTable *
370 ilist_traits<BasicBlock>::getSymTab(Function *F) {
371 return F ? &F->getValueSymbolTable() : 0;
374 inline ValueSymbolTable *
375 ilist_traits<Argument>::getSymTab(Function *F) {
376 return F ? &F->getValueSymbolTable() : 0;
380 ilist_traits<BasicBlock>::getListOffset() {
381 return Function::getBasicBlockListOffset();
385 ilist_traits<Argument>::getListOffset() {
386 return Function::getArgumentListOffset();
390 } // End llvm namespace