1 //===-- llvm/Module.h - C++ class to represent a VM module -------*- C++ -*--=//
3 // This file contains the declarations for the Module class that is used to
4 // maintain all the information related to a VM module.
6 // A module also maintains a GlobalValRefMap object that is used to hold all
7 // constant references to global variables in the module. When a global
8 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
10 //===----------------------------------------------------------------------===//
15 #include "llvm/Function.h"
16 #include "llvm/GlobalVariable.h"
18 class GlobalValueRefMap; // Used by ConstantVals.cpp
19 class ConstantPointerRef;
23 template<> struct ilist_traits<Function>
24 : public SymbolTableListTraits<Function, Module, Module> {
25 // createNode is used to create a node that marks the end of the list...
26 static Function *createNode();
27 static iplist<Function> &getList(Module *M);
29 template<> struct ilist_traits<GlobalVariable>
30 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
31 // createNode is used to create a node that marks the end of the list...
32 static GlobalVariable *createNode();
33 static iplist<GlobalVariable> &getList(Module *M);
36 class Module : public Annotable {
38 typedef iplist<GlobalVariable> GlobalListType;
39 typedef iplist<Function> FunctionListType;
41 // Global Variable iterators...
42 typedef GlobalListType::iterator giterator;
43 typedef GlobalListType::const_iterator const_giterator;
44 typedef std::reverse_iterator<giterator> reverse_giterator;
45 typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
47 // Function iterators...
48 typedef FunctionListType::iterator iterator;
49 typedef FunctionListType::const_iterator const_iterator;
50 typedef std::reverse_iterator<iterator> reverse_iterator;
51 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
53 enum Endianness { LittleEndian, BigEndian };
54 enum PointerSize { Pointer32, Pointer64 };
57 GlobalListType GlobalList; // The Global Variables in the module
58 FunctionListType FunctionList; // The Functions in the module
59 GlobalValueRefMap *GVRefMap; // Keep track of GlobalValueRef's
60 SymbolTable *SymTab; // Symbol Table for the module
61 std::string ModuleID; // Human readable identifier for the module
63 // These flags are probably not the right long-term way to handle this kind of
64 // target information, but it is sufficient for now.
65 Endianness Endian; // True if target is little endian
66 PointerSize PtrSize; // True if target has 32-bit pointers (false = 64-bit)
68 // Accessor for the underlying GVRefMap... only through the Constant class...
69 friend class Constant;
70 friend class ConstantPointerRef;
71 void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
72 ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
73 void destroyConstantPointerRef(ConstantPointerRef *CPR);
76 Module(const std::string &ModuleID);
79 const std::string &getModuleIdentifier() const { return ModuleID; }
81 /// Target endian information...
82 bool isLittleEndian() const { return Endian == LittleEndian; }
83 bool isBigEndian() const { return Endian == BigEndian; }
84 Endianness getEndianness() const { return Endian; }
85 void setEndianness(Endianness E) { Endian = E; }
87 /// Target Pointer Size information...
88 bool has32BitPointers() const { return PtrSize == Pointer32; }
89 bool has64BitPointers() const { return PtrSize == Pointer64; }
90 PointerSize getPointerSize() const { return PtrSize; }
91 void setPointerSize(PointerSize PS) { PtrSize = PS; }
93 /// getOrInsertFunction - Look up the specified function in the module symbol
94 /// table. If it does not exist, add a prototype for the function and return
96 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
98 /// getFunction - Look up the specified function in the module symbol table.
99 /// If it does not exist, return null.
101 Function *getFunction(const std::string &Name, const FunctionType *Ty);
103 /// getMainFunction - This function looks up main efficiently. This is such a
104 /// common case, that it is a method in Module. If main cannot be found, a
105 /// null pointer is returned.
107 Function *getMainFunction();
109 /// getNamedFunction - Return the first function in the module with the
110 /// specified name, of arbitrary type. This method returns null if a function
111 /// with the specified name is not found.
113 Function *getNamedFunction(const std::string &Name);
115 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
116 /// there is already an entry for this name, true is returned and the symbol
117 /// table is not modified.
119 bool addTypeName(const std::string &Name, const Type *Ty);
121 /// getTypeName - If there is at least one entry in the symbol table for the
122 /// specified type, return it.
124 std::string getTypeName(const Type *Ty);
126 /// Get the underlying elements of the Module...
127 inline const GlobalListType &getGlobalList() const { return GlobalList; }
128 inline GlobalListType &getGlobalList() { return GlobalList; }
129 inline const FunctionListType &getFunctionList() const { return FunctionList;}
130 inline FunctionListType &getFunctionList() { return FunctionList;}
133 //===--------------------------------------------------------------------===//
134 // Symbol table support functions...
136 /// getSymbolTable() - Get access to the symbol table for the module, where
137 /// global variables and functions are identified.
139 inline SymbolTable &getSymbolTable() { return *SymTab; }
140 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
143 //===--------------------------------------------------------------------===//
144 // Module iterator forwarding functions
146 inline giterator gbegin() { return GlobalList.begin(); }
147 inline const_giterator gbegin() const { return GlobalList.begin(); }
148 inline giterator gend () { return GlobalList.end(); }
149 inline const_giterator gend () const { return GlobalList.end(); }
151 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
152 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
153 inline reverse_giterator grend () { return GlobalList.rend(); }
154 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
156 inline unsigned gsize() const { return GlobalList.size(); }
157 inline bool gempty() const { return GlobalList.empty(); }
158 inline const GlobalVariable &gfront() const { return GlobalList.front(); }
159 inline GlobalVariable &gfront() { return GlobalList.front(); }
160 inline const GlobalVariable &gback() const { return GlobalList.back(); }
161 inline GlobalVariable &gback() { return GlobalList.back(); }
165 inline iterator begin() { return FunctionList.begin(); }
166 inline const_iterator begin() const { return FunctionList.begin(); }
167 inline iterator end () { return FunctionList.end(); }
168 inline const_iterator end () const { return FunctionList.end(); }
170 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
171 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
172 inline reverse_iterator rend () { return FunctionList.rend(); }
173 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
175 inline unsigned size() const { return FunctionList.size(); }
176 inline bool empty() const { return FunctionList.empty(); }
177 inline const Function &front() const { return FunctionList.front(); }
178 inline Function &front() { return FunctionList.front(); }
179 inline const Function &back() const { return FunctionList.back(); }
180 inline Function &back() { return FunctionList.back(); }
182 void print(std::ostream &OS) const;
185 /// dropAllReferences() - This function causes all the subinstructions to "let
186 /// go" of all references that they are maintaining. This allows one to
187 /// 'delete' a whole class at a time, even though there may be circular
188 /// references... first all references are dropped, and all use counts go to
189 /// zero. Then everything is delete'd for real. Note that no operations are
190 /// valid on an object that has "dropped all references", except operator
193 void dropAllReferences();
196 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
201 inline std::ostream &operator<<(std::ostream &O, const Module &M) {