2002-09-06 20:47:31 +00:00
|
|
|
//===-- llvm/Function.h - Class to represent a single function --*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2005-04-21 20:19:05 +00:00
|
|
|
// This file contains the declaration of the Function class, which represents a
|
2002-09-06 20:47:31 +00:00
|
|
|
// single function/procedure in LLVM.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2002-09-06 20:47:31 +00:00
|
|
|
// A function basically consists of a list of basic blocks, a list of arguments,
|
|
|
|
// and a symbol table.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-03-23 22:47:28 +00:00
|
|
|
#ifndef LLVM_FUNCTION_H
|
|
|
|
#define LLVM_FUNCTION_H
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
#include "llvm/GlobalValue.h"
|
2002-06-25 16:12:52 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Argument.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Annotation.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-03-29 03:30:59 +00:00
|
|
|
class FunctionType;
|
2007-04-09 15:01:12 +00:00
|
|
|
class ParamAttrsList;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-06-25 16:12:52 +00:00
|
|
|
// Traits for intrusive list of instructions...
|
|
|
|
template<> struct ilist_traits<BasicBlock>
|
2007-04-17 03:26:42 +00:00
|
|
|
: public SymbolTableListTraits<BasicBlock, Function> {
|
2002-06-25 16:12:52 +00:00
|
|
|
|
2005-01-30 00:09:23 +00:00
|
|
|
// createSentinel is used to create a node that marks the end of the list...
|
|
|
|
static BasicBlock *createSentinel();
|
|
|
|
static void destroySentinel(BasicBlock *BB) { delete BB; }
|
2002-06-25 16:12:52 +00:00
|
|
|
static iplist<BasicBlock> &getList(Function *F);
|
2007-04-17 03:26:42 +00:00
|
|
|
static ValueSymbolTable *getSymTab(Function *ItemParent);
|
2007-04-17 04:04:14 +00:00
|
|
|
static int getListOffset();
|
2002-06-25 16:12:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<> struct ilist_traits<Argument>
|
2007-04-17 03:26:42 +00:00
|
|
|
: public SymbolTableListTraits<Argument, Function> {
|
2002-06-25 16:12:52 +00:00
|
|
|
|
2005-01-30 00:09:23 +00:00
|
|
|
// createSentinel is used to create a node that marks the end of the list...
|
|
|
|
static Argument *createSentinel();
|
|
|
|
static void destroySentinel(Argument *A) { delete A; }
|
2002-06-25 16:12:52 +00:00
|
|
|
static iplist<Argument> &getList(Function *F);
|
2007-04-17 03:26:42 +00:00
|
|
|
static ValueSymbolTable *getSymTab(Function *ItemParent);
|
2007-04-17 04:04:14 +00:00
|
|
|
static int getListOffset();
|
2002-06-25 16:12:52 +00:00
|
|
|
};
|
|
|
|
|
2004-02-26 08:08:38 +00:00
|
|
|
class Function : public GlobalValue, public Annotable {
|
2001-06-06 20:29:01 +00:00
|
|
|
public:
|
2002-06-25 16:12:52 +00:00
|
|
|
typedef iplist<Argument> ArgumentListType;
|
|
|
|
typedef iplist<BasicBlock> BasicBlockListType;
|
2001-06-27 23:26:41 +00:00
|
|
|
|
|
|
|
// BasicBlock iterators...
|
2002-06-25 16:12:52 +00:00
|
|
|
typedef BasicBlockListType::iterator iterator;
|
|
|
|
typedef BasicBlockListType::const_iterator const_iterator;
|
2001-06-27 23:26:41 +00:00
|
|
|
|
2005-03-14 23:49:40 +00:00
|
|
|
typedef ArgumentListType::iterator arg_iterator;
|
|
|
|
typedef ArgumentListType::const_iterator const_arg_iterator;
|
2002-06-25 16:12:52 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
private:
|
2002-04-28 04:44:40 +00:00
|
|
|
// Important things that make up a function!
|
2007-04-09 15:01:12 +00:00
|
|
|
BasicBlockListType BasicBlocks; ///< The basic blocks
|
|
|
|
ArgumentListType ArgumentList; ///< The formal arguments
|
|
|
|
ValueSymbolTable *SymTab; ///< Symbol table of args/instructions
|
|
|
|
ParamAttrsList *ParamAttrs; ///< Parameter attributes
|
2007-04-17 04:31:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
// The Calling Convention is stored in Value::SubclassData.
|
|
|
|
/*unsigned CallingConvention;*/
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2007-04-17 03:26:42 +00:00
|
|
|
friend class SymbolTableListTraits<Function, Module>;
|
2002-06-25 16:12:52 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
void setParent(Module *parent);
|
2002-06-25 16:12:52 +00:00
|
|
|
Function *Prev, *Next;
|
|
|
|
void setNext(Function *N) { Next = N; }
|
|
|
|
void setPrev(Function *N) { Prev = N; }
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2007-04-28 13:45:00 +00:00
|
|
|
// getNext/Prev - Return the next or previous function in the list. These
|
|
|
|
// methods should never be used directly, and are only used to implement the
|
|
|
|
// function list as part of the module.
|
|
|
|
//
|
|
|
|
Function *getNext() { return Next; }
|
|
|
|
const Function *getNext() const { return Next; }
|
|
|
|
Function *getPrev() { return Prev; }
|
|
|
|
const Function *getPrev() const { return Prev; }
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
public:
|
2002-09-06 20:47:31 +00:00
|
|
|
/// Function ctor - If the (optional) Module argument is specified, the
|
|
|
|
/// function is automatically inserted into the end of the function list for
|
|
|
|
/// the module.
|
|
|
|
///
|
2003-04-16 20:28:45 +00:00
|
|
|
Function(const FunctionType *Ty, LinkageTypes Linkage,
|
|
|
|
const std::string &N = "", Module *M = 0);
|
2002-03-23 22:47:28 +00:00
|
|
|
~Function();
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-03-29 03:30:59 +00:00
|
|
|
const Type *getReturnType() const; // Return the type of the ret val
|
|
|
|
const FunctionType *getFunctionType() const; // Return the FunctionType for me
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2005-01-07 07:40:32 +00:00
|
|
|
/// isVarArg - Return true if this function takes a variable number of
|
|
|
|
/// arguments.
|
|
|
|
bool isVarArg() const;
|
|
|
|
|
2007-01-30 20:08:39 +00:00
|
|
|
/// isDeclaration - Is the body of this function unknown? (The basic block
|
|
|
|
/// list is empty if so.) This is true for function declarations, but not
|
|
|
|
/// true for function definitions.
|
2002-08-25 22:54:55 +00:00
|
|
|
///
|
2007-01-30 20:08:39 +00:00
|
|
|
virtual bool isDeclaration() const { return BasicBlocks.empty(); }
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-05-08 03:34:12 +00:00
|
|
|
/// getIntrinsicID - This method returns the ID number of the specified
|
2003-11-11 22:41:34 +00:00
|
|
|
/// function, or Intrinsic::not_intrinsic if the function is not an
|
2003-05-08 03:34:12 +00:00
|
|
|
/// instrinsic, or if the pointer is null. This value is always defined to be
|
|
|
|
/// zero to allow easy checking for whether a function is intrinsic or not.
|
|
|
|
/// The particular intrinsic functions which correspond to this value are
|
|
|
|
/// defined in llvm/Intrinsics.h.
|
|
|
|
///
|
2007-04-16 06:54:34 +00:00
|
|
|
unsigned getIntrinsicID(bool noAssert = false) const;
|
2003-05-08 03:34:12 +00:00
|
|
|
bool isIntrinsic() const { return getIntrinsicID() != 0; }
|
|
|
|
|
2005-05-06 20:26:26 +00:00
|
|
|
/// getCallingConv()/setCallingConv(uint) - These method get and set the
|
|
|
|
/// calling convention of this function. The enum values for the known
|
|
|
|
/// calling conventions are defined in CallingConv.h.
|
2007-04-17 04:31:29 +00:00
|
|
|
unsigned getCallingConv() const { return SubclassData; }
|
|
|
|
void setCallingConv(unsigned CC) { SubclassData = CC; }
|
2005-05-06 20:26:26 +00:00
|
|
|
|
2007-04-09 15:01:12 +00:00
|
|
|
/// Obtains a constant pointer to the ParamAttrsList object which holds the
|
|
|
|
/// parameter attributes information, if any.
|
|
|
|
/// @returns 0 if no parameter attributes have been set.
|
|
|
|
/// @brief Get the parameter attributes.
|
|
|
|
const ParamAttrsList *getParamAttrs() const { return ParamAttrs; }
|
|
|
|
|
|
|
|
/// Sets the parameter attributes for this Function. To construct a
|
|
|
|
/// ParamAttrsList, see ParameterAttributes.h
|
|
|
|
/// @brief Set the parameter attributes.
|
2007-04-22 17:28:03 +00:00
|
|
|
void setParamAttrs(ParamAttrsList *attrs);
|
2007-04-09 15:01:12 +00:00
|
|
|
|
2003-09-17 04:58:43 +00:00
|
|
|
/// deleteBody - This method deletes the body of the function, and converts
|
|
|
|
/// the linkage to external.
|
2004-03-01 18:31:19 +00:00
|
|
|
///
|
2003-09-17 04:58:43 +00:00
|
|
|
void deleteBody() {
|
|
|
|
dropAllReferences();
|
|
|
|
setLinkage(ExternalLinkage);
|
|
|
|
}
|
|
|
|
|
2004-10-11 22:21:13 +00:00
|
|
|
/// removeFromParent - This method unlinks 'this' from the containing module,
|
|
|
|
/// but does not delete it.
|
|
|
|
///
|
2007-04-29 18:02:48 +00:00
|
|
|
void removeFromParent();
|
2004-10-11 22:21:13 +00:00
|
|
|
|
|
|
|
/// eraseFromParent - This method unlinks 'this' from the containing module
|
|
|
|
/// and deletes it.
|
|
|
|
///
|
2007-04-29 18:02:48 +00:00
|
|
|
void eraseFromParent();
|
2004-10-11 22:21:13 +00:00
|
|
|
|
|
|
|
|
2003-05-09 22:16:18 +00:00
|
|
|
/// Get the underlying elements of the Function... the basic block list is
|
|
|
|
/// empty for external functions.
|
2002-08-25 22:54:55 +00:00
|
|
|
///
|
2002-06-25 16:12:52 +00:00
|
|
|
const ArgumentListType &getArgumentList() const { return ArgumentList; }
|
|
|
|
ArgumentListType &getArgumentList() { return ArgumentList; }
|
2001-06-27 23:26:41 +00:00
|
|
|
|
2002-06-25 16:12:52 +00:00
|
|
|
const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
|
|
|
|
BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-20 14:36:49 +00:00
|
|
|
const BasicBlock &getEntryBlock() const { return front(); }
|
|
|
|
BasicBlock &getEntryBlock() { return front(); }
|
2002-04-28 04:44:40 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Symbol Table Accessing functions...
|
|
|
|
|
2002-11-20 18:07:48 +00:00
|
|
|
/// getSymbolTable() - Return the symbol table...
|
2002-08-25 22:54:55 +00:00
|
|
|
///
|
2007-02-05 20:47:22 +00:00
|
|
|
inline ValueSymbolTable &getValueSymbolTable() { return *SymTab; }
|
|
|
|
inline const ValueSymbolTable &getValueSymbolTable() const { return *SymTab; }
|
2002-04-28 04:44:40 +00:00
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2001-06-27 23:26:41 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// BasicBlock iterator forwarding functions
|
|
|
|
//
|
2002-06-25 16:12:52 +00:00
|
|
|
iterator begin() { return BasicBlocks.begin(); }
|
|
|
|
const_iterator begin() const { return BasicBlocks.begin(); }
|
|
|
|
iterator end () { return BasicBlocks.end(); }
|
|
|
|
const_iterator end () const { return BasicBlocks.end(); }
|
|
|
|
|
2004-11-15 19:02:35 +00:00
|
|
|
size_t size() const { return BasicBlocks.size(); }
|
2002-06-25 16:12:52 +00:00
|
|
|
bool empty() const { return BasicBlocks.empty(); }
|
|
|
|
const BasicBlock &front() const { return BasicBlocks.front(); }
|
|
|
|
BasicBlock &front() { return BasicBlocks.front(); }
|
2004-10-19 05:50:34 +00:00
|
|
|
const BasicBlock &back() const { return BasicBlocks.back(); }
|
|
|
|
BasicBlock &back() { return BasicBlocks.back(); }
|
2002-06-25 16:12:52 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Argument iterator forwarding functions
|
|
|
|
//
|
2005-03-14 23:49:40 +00:00
|
|
|
arg_iterator arg_begin() { return ArgumentList.begin(); }
|
|
|
|
const_arg_iterator arg_begin() const { return ArgumentList.begin(); }
|
|
|
|
arg_iterator arg_end () { return ArgumentList.end(); }
|
|
|
|
const_arg_iterator arg_end () const { return ArgumentList.end(); }
|
|
|
|
|
|
|
|
size_t arg_size () const { return ArgumentList.size(); }
|
|
|
|
bool arg_empty() const { return ArgumentList.empty(); }
|
|
|
|
|
2003-10-30 23:41:19 +00:00
|
|
|
virtual void print(std::ostream &OS) const { print(OS, 0); }
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS) const { if (OS) print(*OS); }
|
2003-10-30 23:41:19 +00:00
|
|
|
void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-22 16:03:20 +00:00
|
|
|
/// viewCFG - This function is meant for use from the debugger. You can just
|
|
|
|
/// say 'call F->viewCFG()' and a ghostview window should pop up from the
|
|
|
|
/// program, displaying the CFG of the current function with the code for each
|
|
|
|
/// basic block inside. This depends on there being a 'dot' and 'gv' program
|
|
|
|
/// in your path.
|
|
|
|
///
|
|
|
|
void viewCFG() const;
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2003-10-22 16:03:20 +00:00
|
|
|
/// viewCFGOnly - This function is meant for use from the debugger. It works
|
|
|
|
/// just like viewCFG, but it does not include the contents of basic blocks
|
2004-03-01 18:31:19 +00:00
|
|
|
/// into the nodes, just the label. If you are only interested in the CFG
|
|
|
|
/// this can make the graph smaller.
|
2003-10-22 16:03:20 +00:00
|
|
|
///
|
|
|
|
void viewCFGOnly() const;
|
|
|
|
|
2002-08-25 22:54:55 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
2002-03-23 22:47:28 +00:00
|
|
|
static inline bool classof(const Function *) { return true; }
|
2001-10-02 03:41:24 +00:00
|
|
|
static inline bool classof(const Value *V) {
|
2007-04-13 18:12:09 +00:00
|
|
|
return V->getValueID() == Value::FunctionVal;
|
2001-10-01 16:18:37 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-17 04:58:43 +00:00
|
|
|
/// dropAllReferences() - This method causes all the subinstructions to "let
|
2002-08-25 22:54:55 +00:00
|
|
|
/// go" of all references that they are maintaining. This allows one to
|
2003-09-17 04:58:43 +00:00
|
|
|
/// 'delete' a whole module at a time, even though there may be circular
|
2002-08-25 22:54:55 +00:00
|
|
|
/// references... first all references are dropped, and all use counts go to
|
2004-03-01 18:31:19 +00:00
|
|
|
/// zero. Then everything is deleted for real. Note that no operations are
|
2005-04-21 20:19:05 +00:00
|
|
|
/// valid on an object that has "dropped all references", except operator
|
2002-08-25 22:54:55 +00:00
|
|
|
/// delete.
|
|
|
|
///
|
2003-09-17 04:58:43 +00:00
|
|
|
/// Since no other object in the module can have references into the body of a
|
|
|
|
/// function, dropping all references deletes the entire body of the function,
|
|
|
|
/// including any contained basic blocks.
|
|
|
|
///
|
2001-06-06 20:29:01 +00:00
|
|
|
void dropAllReferences();
|
2007-04-17 04:04:14 +00:00
|
|
|
|
|
|
|
static unsigned getBasicBlockListOffset() {
|
|
|
|
Function *Obj = 0;
|
2007-04-17 05:33:04 +00:00
|
|
|
return unsigned(reinterpret_cast<uintptr_t>(&Obj->BasicBlocks));
|
2007-04-17 04:04:14 +00:00
|
|
|
}
|
|
|
|
static unsigned getArgumentListOffset() {
|
|
|
|
Function *Obj = 0;
|
2007-04-17 05:33:04 +00:00
|
|
|
return unsigned(reinterpret_cast<uintptr_t>(&Obj->ArgumentList));
|
2007-04-17 04:04:14 +00:00
|
|
|
}
|
2001-09-28 22:56:31 +00:00
|
|
|
};
|
|
|
|
|
2007-04-17 03:26:42 +00:00
|
|
|
inline ValueSymbolTable *
|
|
|
|
ilist_traits<BasicBlock>::getSymTab(Function *F) {
|
|
|
|
return F ? &F->getValueSymbolTable() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ValueSymbolTable *
|
|
|
|
ilist_traits<Argument>::getSymTab(Function *F) {
|
|
|
|
return F ? &F->getValueSymbolTable() : 0;
|
|
|
|
}
|
|
|
|
|
2007-04-17 04:04:14 +00:00
|
|
|
inline int
|
|
|
|
ilist_traits<BasicBlock>::getListOffset() {
|
|
|
|
return Function::getBasicBlockListOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int
|
|
|
|
ilist_traits<Argument>::getListOffset() {
|
|
|
|
return Function::getArgumentListOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|