Chris Lattner | 9c6342d | 2002-10-28 01:27:51 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===// |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 2 | // |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 10 | // Collect native machine code for a function. This class contains a list of |
| 11 | // MachineBasicBlock instances that make up the current compiled function. |
| 12 | // |
| 13 | // This class also contains pointers to various classes which hold |
| 14 | // target-specific information about the generated code. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 15 | // |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 18 | #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H |
| 19 | #define LLVM_CODEGEN_MACHINEFUNCTION_H |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 0551f54 | 2002-10-28 02:01:06 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Annotation.h" |
Chris Lattner | 8e7ae98 | 2002-10-28 02:08:43 +0000 | [diff] [blame] | 23 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 26 | class Function; |
| 27 | class TargetMachine; |
| 28 | class SSARegMap; |
| 29 | class MachineFrameInfo; |
| 30 | class MachineConstantPool; |
| 31 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 32 | // ilist_traits |
| 33 | template <> |
| 34 | class ilist_traits<MachineBasicBlock> { |
| 35 | // this is only set by the MachineFunction owning the ilist |
| 36 | friend class MachineFunction; |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 37 | MachineFunction* Parent; |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 40 | ilist_traits<MachineBasicBlock>() : Parent(0) { } |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 41 | |
| 42 | static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; } |
| 43 | static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; } |
| 44 | |
| 45 | static const MachineBasicBlock* |
| 46 | getPrev(const MachineBasicBlock* N) { return N->Prev; } |
| 47 | |
| 48 | static const MachineBasicBlock* |
| 49 | getNext(const MachineBasicBlock* N) { return N->Next; } |
| 50 | |
Chris Lattner | 2fcd451 | 2004-08-16 22:35:26 +0000 | [diff] [blame] | 51 | static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) { |
| 52 | N->Prev = prev; |
| 53 | } |
| 54 | static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) { |
| 55 | N->Next = next; |
| 56 | } |
Misha Brukman | 274ba03 | 2004-08-17 17:52:36 +0000 | [diff] [blame] | 57 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 58 | static MachineBasicBlock* createNode(); |
| 59 | void addNodeToList(MachineBasicBlock* N); |
| 60 | void removeNodeFromList(MachineBasicBlock* N); |
Chris Lattner | 2fcd451 | 2004-08-16 22:35:26 +0000 | [diff] [blame] | 61 | void transferNodesFromList(iplist<MachineBasicBlock, |
| 62 | ilist_traits<MachineBasicBlock> > &toList, |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 63 | ilist_iterator<MachineBasicBlock> first, |
| 64 | ilist_iterator<MachineBasicBlock> last); |
| 65 | }; |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 67 | /// MachineFunctionInfo - This class can be derived from and used by targets to |
| 68 | /// hold private target-specific information for each MachineFunction. Objects |
| 69 | /// of type are accessed/created with MF::getInfo and destroyed when the |
| 70 | /// MachineFunction is destroyed. |
| 71 | struct MachineFunctionInfo { |
| 72 | virtual ~MachineFunctionInfo() {}; |
| 73 | }; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 74 | |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 75 | class MachineFunction : private Annotation { |
Chris Lattner | 335d5c3 | 2002-10-28 05:58:46 +0000 | [diff] [blame] | 76 | const Function *Fn; |
| 77 | const TargetMachine &Target; |
Chris Lattner | 8e7ae98 | 2002-10-28 02:08:43 +0000 | [diff] [blame] | 78 | |
| 79 | // List of machine basic blocks in function |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 80 | ilist<MachineBasicBlock> BasicBlocks; |
Chris Lattner | 9c6342d | 2002-10-28 01:27:51 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 03ab7af | 2002-12-25 05:00:16 +0000 | [diff] [blame] | 82 | // Keeping track of mapping from SSA values to registers |
| 83 | SSARegMap *SSARegMapping; |
| 84 | |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 85 | // Used to keep track of target-specific per-machine function information for |
| 86 | // the target implementation. |
| 87 | MachineFunctionInfo *MFInfo; |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 88 | |
| 89 | // Keep track of objects allocated on the stack. |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 90 | MachineFrameInfo *FrameInfo; |
Misha Brukman | 1617e6c | 2002-11-20 00:53:10 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 92 | // Keep track of constants which are spilled to memory |
| 93 | MachineConstantPool *ConstantPool; |
| 94 | |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 95 | // Function-level unique numbering for MachineBasicBlocks. When a |
| 96 | // MachineBasicBlock is inserted into a MachineFunction is it automatically |
| 97 | // numbered and this vector keeps track of the mapping from ID's to MBB's. |
| 98 | std::vector<MachineBasicBlock*> MBBNumbering; |
Brian Gaeke | f460f16 | 2004-05-12 21:35:21 +0000 | [diff] [blame] | 99 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 100 | public: |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 101 | MachineFunction(const Function *Fn, const TargetMachine &TM); |
Chris Lattner | 03ab7af | 2002-12-25 05:00:16 +0000 | [diff] [blame] | 102 | ~MachineFunction(); |
Chris Lattner | 335d5c3 | 2002-10-28 05:58:46 +0000 | [diff] [blame] | 103 | |
| 104 | /// getFunction - Return the LLVM function that this machine code represents |
| 105 | /// |
| 106 | const Function *getFunction() const { return Fn; } |
| 107 | |
| 108 | /// getTarget - Return the target machine this machine code is compiled with |
| 109 | /// |
| 110 | const TargetMachine &getTarget() const { return Target; } |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 111 | |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 112 | /// SSARegMap Interface... Keep track of information about each SSA virtual |
| 113 | /// register, such as which register class it belongs to. |
| 114 | /// |
| 115 | SSARegMap *getSSARegMap() const { return SSARegMapping; } |
| 116 | void clearSSARegMap(); |
| 117 | |
| 118 | /// getFrameInfo - Return the frame info object for the current function. |
| 119 | /// This object contains information about objects allocated on the stack |
| 120 | /// frame of the current function in an abstract way. |
| 121 | /// |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 122 | MachineFrameInfo *getFrameInfo() const { return FrameInfo; } |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 124 | /// getConstantPool - Return the constant pool object for the current |
| 125 | /// function. |
Misha Brukman | 274ba03 | 2004-08-17 17:52:36 +0000 | [diff] [blame] | 126 | /// |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 127 | MachineConstantPool *getConstantPool() const { return ConstantPool; } |
| 128 | |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 129 | /// MachineFunctionInfo - Keep track of various per-function pieces of |
| 130 | /// information for the sparc backend. |
| 131 | /// |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 132 | template<typename Ty> |
| 133 | Ty *getInfo() { |
| 134 | if (!MFInfo) MFInfo = new Ty(*this); |
| 135 | |
| 136 | assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo && |
| 137 | "Invalid concrete type or multiple inheritence for getInfo"); |
| 138 | return static_cast<Ty*>(MFInfo); |
| 139 | } |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 141 | /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they |
| 142 | /// are inserted into the machine function. The block number for a machine |
| 143 | /// basic block can be found by using the MBB::getBlockNumber method, this |
| 144 | /// method provides the inverse mapping. |
Misha Brukman | 274ba03 | 2004-08-17 17:52:36 +0000 | [diff] [blame] | 145 | /// |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 146 | MachineBasicBlock *getBlockNumbered(unsigned N) { |
| 147 | assert(N < MBBNumbering.size() && "Illegal block number"); |
| 148 | assert(MBBNumbering[N] && "Block was removed from the machine function!"); |
| 149 | return MBBNumbering[N]; |
| 150 | } |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 151 | |
Alkis Evlogimenos | fec656c | 2004-08-27 04:02:35 +0000 | [diff] [blame] | 152 | /// getLastBlock - Returns the MachineBasicBlock with the greatest number |
| 153 | MachineBasicBlock *getLastBlock() { |
| 154 | return MBBNumbering.back(); |
| 155 | } |
Alkis Evlogimenos | 4b97f23 | 2004-09-30 21:42:02 +0000 | [diff] [blame^] | 156 | const MachineBasicBlock *getLastBlock() const { |
| 157 | return MBBNumbering.back(); |
| 158 | } |
Alkis Evlogimenos | fec656c | 2004-08-27 04:02:35 +0000 | [diff] [blame] | 159 | |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 160 | /// print - Print out the MachineFunction in a format suitable for debugging |
| 161 | /// to the specified stream. |
| 162 | /// |
| 163 | void print(std::ostream &OS) const; |
| 164 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 165 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 166 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 167 | /// program, displaying the CFG of the current function with the code for each |
| 168 | /// basic block inside. This depends on there being a 'dot' and 'gv' program |
| 169 | /// in your path. |
| 170 | /// |
| 171 | void viewCFG() const; |
| 172 | |
| 173 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 174 | /// just like viewCFG, but it does not include the contents of basic blocks |
| 175 | /// into the nodes, just the label. If you are only interested in the CFG |
| 176 | /// this can make the graph smaller. |
| 177 | /// |
| 178 | void viewCFGOnly() const; |
| 179 | |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 180 | /// dump - Print the current MachineFunction to cerr, useful for debugger use. |
| 181 | /// |
| 182 | void dump() const; |
| 183 | |
Misha Brukman | a7afa37 | 2004-06-04 14:51:25 +0000 | [diff] [blame] | 184 | /// construct - Allocate and initialize a MachineFunction for a given Function |
| 185 | /// and Target |
| 186 | /// |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 187 | static MachineFunction& construct(const Function *F, const TargetMachine &TM); |
Misha Brukman | a7afa37 | 2004-06-04 14:51:25 +0000 | [diff] [blame] | 188 | |
| 189 | /// destruct - Destroy the MachineFunction corresponding to a given Function |
| 190 | /// |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 191 | static void destruct(const Function *F); |
Misha Brukman | a7afa37 | 2004-06-04 14:51:25 +0000 | [diff] [blame] | 192 | |
| 193 | /// get - Return a handle to a MachineFunction corresponding to the given |
| 194 | /// Function. This should not be called before "construct()" for a given |
| 195 | /// Function. |
| 196 | /// |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 197 | static MachineFunction& get(const Function *F); |
Chris Lattner | 9c6342d | 2002-10-28 01:27:51 +0000 | [diff] [blame] | 198 | |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 199 | // Provide accessors for the MachineBasicBlock list... |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 200 | typedef ilist<MachineBasicBlock> BasicBlockListType; |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 201 | typedef BasicBlockListType::iterator iterator; |
| 202 | typedef BasicBlockListType::const_iterator const_iterator; |
| 203 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 204 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 205 | |
| 206 | // Provide accessors for basic blocks... |
| 207 | const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } |
| 208 | BasicBlockListType &getBasicBlockList() { return BasicBlocks; } |
| 209 | |
| 210 | //===--------------------------------------------------------------------===// |
| 211 | // BasicBlock iterator forwarding functions |
| 212 | // |
| 213 | iterator begin() { return BasicBlocks.begin(); } |
| 214 | const_iterator begin() const { return BasicBlocks.begin(); } |
| 215 | iterator end () { return BasicBlocks.end(); } |
| 216 | const_iterator end () const { return BasicBlocks.end(); } |
| 217 | |
| 218 | reverse_iterator rbegin() { return BasicBlocks.rbegin(); } |
| 219 | const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } |
| 220 | reverse_iterator rend () { return BasicBlocks.rend(); } |
| 221 | const_reverse_iterator rend () const { return BasicBlocks.rend(); } |
| 222 | |
| 223 | unsigned size() const { return BasicBlocks.size(); } |
| 224 | bool empty() const { return BasicBlocks.empty(); } |
| 225 | const MachineBasicBlock &front() const { return BasicBlocks.front(); } |
| 226 | MachineBasicBlock &front() { return BasicBlocks.front(); } |
Misha Brukman | d5806ff | 2002-10-28 19:58:38 +0000 | [diff] [blame] | 227 | const MachineBasicBlock & back() const { return BasicBlocks.back(); } |
| 228 | MachineBasicBlock & back() { return BasicBlocks.back(); } |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 229 | |
| 230 | //===--------------------------------------------------------------------===// |
| 231 | // Internal functions used to automatically number MachineBasicBlocks |
| 232 | // |
| 233 | |
| 234 | /// getNextMBBNumber - Returns the next unique number to be assigned |
| 235 | /// to a MachineBasicBlock in this MachineFunction. |
| 236 | /// |
| 237 | unsigned addToMBBNumbering(MachineBasicBlock *MBB) { |
| 238 | MBBNumbering.push_back(MBB); |
| 239 | return MBBNumbering.size()-1; |
| 240 | } |
| 241 | |
| 242 | /// removeFromMBBNumbering - Remove the specific machine basic block from our |
| 243 | /// tracker, this is only really to be used by the MachineBasicBlock |
| 244 | /// implementation. |
| 245 | void removeFromMBBNumbering(unsigned N) { |
| 246 | assert(N < MBBNumbering.size() && "Illegal basic block #"); |
| 247 | MBBNumbering[N] = 0; |
| 248 | } |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 251 | //===--------------------------------------------------------------------===// |
| 252 | // GraphTraits specializations for function basic block graphs (CFGs) |
| 253 | //===--------------------------------------------------------------------===// |
| 254 | |
| 255 | // Provide specializations of GraphTraits to be able to treat a |
| 256 | // machine function as a graph of machine basic blocks... these are |
| 257 | // the same as the machine basic block iterators, except that the root |
| 258 | // node is implicitly the first node of the function. |
| 259 | // |
| 260 | template <> struct GraphTraits<MachineFunction*> : |
| 261 | public GraphTraits<MachineBasicBlock*> { |
| 262 | static NodeType *getEntryNode(MachineFunction *F) { |
| 263 | return &F->front(); |
| 264 | } |
| 265 | |
| 266 | // nodes_iterator/begin/end - Allow iteration over all nodes in the graph |
| 267 | typedef MachineFunction::iterator nodes_iterator; |
| 268 | static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); } |
| 269 | static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); } |
| 270 | }; |
| 271 | template <> struct GraphTraits<const MachineFunction*> : |
| 272 | public GraphTraits<const MachineBasicBlock*> { |
| 273 | static NodeType *getEntryNode(const MachineFunction *F) { |
| 274 | return &F->front(); |
| 275 | } |
| 276 | |
| 277 | // nodes_iterator/begin/end - Allow iteration over all nodes in the graph |
| 278 | typedef MachineFunction::const_iterator nodes_iterator; |
| 279 | static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); } |
| 280 | static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); } |
| 281 | }; |
| 282 | |
| 283 | |
| 284 | // Provide specializations of GraphTraits to be able to treat a function as a |
| 285 | // graph of basic blocks... and to walk it in inverse order. Inverse order for |
| 286 | // a function is considered to be when traversing the predecessor edges of a BB |
| 287 | // instead of the successor edges. |
| 288 | // |
| 289 | template <> struct GraphTraits<Inverse<MachineFunction*> > : |
| 290 | public GraphTraits<Inverse<MachineBasicBlock*> > { |
| 291 | static NodeType *getEntryNode(Inverse<MachineFunction*> G) { |
| 292 | return &G.Graph->front(); |
| 293 | } |
| 294 | }; |
| 295 | template <> struct GraphTraits<Inverse<const MachineFunction*> > : |
| 296 | public GraphTraits<Inverse<const MachineBasicBlock*> > { |
| 297 | static NodeType *getEntryNode(Inverse<const MachineFunction *> G) { |
| 298 | return &G.Graph->front(); |
| 299 | } |
| 300 | }; |
| 301 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 302 | } // End llvm namespace |
| 303 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 304 | #endif |