Chris Lattner | 9c6342d | 2002-10-28 01:27:51 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===// |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +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. |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 7 | // |
John Criswell | 6fbcc26 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 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 | ea61c35 | 2005-04-21 20:39:54 +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 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 0551f54 | 2002-10-28 02:01:06 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Annotation.h" |
Evan Cheng | 505e551 | 2007-04-25 22:10:09 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/BitVector.h" |
Chris Lattner | 8e7ae98 | 2002-10-28 02:08:43 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 28 | class Function; |
| 29 | class TargetMachine; |
| 30 | class SSARegMap; |
| 31 | class MachineFrameInfo; |
| 32 | class MachineConstantPool; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 33 | class MachineJumpTableInfo; |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 34 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 35 | // ilist_traits |
| 36 | template <> |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 37 | struct ilist_traits<MachineBasicBlock> { |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 38 | // this is only set by the MachineFunction owning the ilist |
| 39 | friend class MachineFunction; |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 40 | MachineFunction* Parent; |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 41 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 42 | public: |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 43 | ilist_traits<MachineBasicBlock>() : Parent(0) { } |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 44 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 45 | static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; } |
| 46 | static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; } |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 47 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 48 | static const MachineBasicBlock* |
| 49 | getPrev(const MachineBasicBlock* N) { return N->Prev; } |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 50 | |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 51 | static const MachineBasicBlock* |
| 52 | getNext(const MachineBasicBlock* N) { return N->Next; } |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 2fcd451 | 2004-08-16 22:35:26 +0000 | [diff] [blame] | 54 | static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) { |
| 55 | N->Prev = prev; |
| 56 | } |
| 57 | static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) { |
| 58 | N->Next = next; |
| 59 | } |
Misha Brukman | 274ba03 | 2004-08-17 17:52:36 +0000 | [diff] [blame] | 60 | |
Chris Lattner | bbec41d | 2005-01-30 00:13:34 +0000 | [diff] [blame] | 61 | static MachineBasicBlock* createSentinel(); |
| 62 | static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; } |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 63 | void addNodeToList(MachineBasicBlock* N); |
| 64 | void removeNodeFromList(MachineBasicBlock* N); |
Chris Lattner | 2fcd451 | 2004-08-16 22:35:26 +0000 | [diff] [blame] | 65 | void transferNodesFromList(iplist<MachineBasicBlock, |
| 66 | ilist_traits<MachineBasicBlock> > &toList, |
Misha Brukman | 00876a2 | 2005-04-22 03:46:24 +0000 | [diff] [blame] | 67 | ilist_iterator<MachineBasicBlock> first, |
| 68 | ilist_iterator<MachineBasicBlock> last); |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 69 | }; |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 71 | /// MachineFunctionInfo - This class can be derived from and used by targets to |
| 72 | /// hold private target-specific information for each MachineFunction. Objects |
| 73 | /// of type are accessed/created with MF::getInfo and destroyed when the |
| 74 | /// MachineFunction is destroyed. |
| 75 | struct MachineFunctionInfo { |
Dan Gohman | 81975f6 | 2007-08-27 14:50:10 +0000 | [diff] [blame^] | 76 | virtual ~MachineFunctionInfo() {} |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 77 | }; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 78 | |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 79 | class MachineFunction : private Annotation { |
Chris Lattner | 335d5c3 | 2002-10-28 05:58:46 +0000 | [diff] [blame] | 80 | const Function *Fn; |
| 81 | const TargetMachine &Target; |
Chris Lattner | 8e7ae98 | 2002-10-28 02:08:43 +0000 | [diff] [blame] | 82 | |
| 83 | // List of machine basic blocks in function |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 84 | ilist<MachineBasicBlock> BasicBlocks; |
Chris Lattner | 9c6342d | 2002-10-28 01:27:51 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 03ab7af | 2002-12-25 05:00:16 +0000 | [diff] [blame] | 86 | // Keeping track of mapping from SSA values to registers |
| 87 | SSARegMap *SSARegMapping; |
| 88 | |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 89 | // Used to keep track of target-specific per-machine function information for |
| 90 | // the target implementation. |
| 91 | MachineFunctionInfo *MFInfo; |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 92 | |
| 93 | // Keep track of objects allocated on the stack. |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 94 | MachineFrameInfo *FrameInfo; |
Misha Brukman | 1617e6c | 2002-11-20 00:53:10 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 96 | // Keep track of constants which are spilled to memory |
| 97 | MachineConstantPool *ConstantPool; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 98 | |
| 99 | // Keep track of jump tables for switch instructions |
| 100 | MachineJumpTableInfo *JumpTableInfo; |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 102 | // Function-level unique numbering for MachineBasicBlocks. When a |
| 103 | // MachineBasicBlock is inserted into a MachineFunction is it automatically |
| 104 | // numbered and this vector keeps track of the mapping from ID's to MBB's. |
| 105 | std::vector<MachineBasicBlock*> MBBNumbering; |
Brian Gaeke | f460f16 | 2004-05-12 21:35:21 +0000 | [diff] [blame] | 106 | |
Evan Cheng | 505e551 | 2007-04-25 22:10:09 +0000 | [diff] [blame] | 107 | /// UsedPhysRegs - This is a bit vector that is computed and set by the |
| 108 | /// register allocator, and must be kept up to date by passes that run after |
| 109 | /// register allocation (though most don't modify this). This is used |
Chris Lattner | d6b210c | 2005-01-23 22:13:36 +0000 | [diff] [blame] | 110 | /// so that the code generator knows which callee save registers to save and |
| 111 | /// for other target specific uses. |
Evan Cheng | 505e551 | 2007-04-25 22:10:09 +0000 | [diff] [blame] | 112 | BitVector UsedPhysRegs; |
Chris Lattner | d6b210c | 2005-01-23 22:13:36 +0000 | [diff] [blame] | 113 | |
Chris Lattner | dde3a9a | 2005-04-09 15:22:53 +0000 | [diff] [blame] | 114 | /// LiveIns/LiveOuts - Keep track of the physical registers that are |
| 115 | /// livein/liveout of the function. Live in values are typically arguments in |
| 116 | /// registers, live out values are typically return values in registers. |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame] | 117 | /// LiveIn values are allowed to have virtual registers associated with them, |
| 118 | /// stored in the second element. |
| 119 | std::vector<std::pair<unsigned, unsigned> > LiveIns; |
| 120 | std::vector<unsigned> LiveOuts; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 121 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 122 | public: |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 123 | MachineFunction(const Function *Fn, const TargetMachine &TM); |
Chris Lattner | 03ab7af | 2002-12-25 05:00:16 +0000 | [diff] [blame] | 124 | ~MachineFunction(); |
Chris Lattner | 335d5c3 | 2002-10-28 05:58:46 +0000 | [diff] [blame] | 125 | |
| 126 | /// getFunction - Return the LLVM function that this machine code represents |
| 127 | /// |
| 128 | const Function *getFunction() const { return Fn; } |
| 129 | |
| 130 | /// getTarget - Return the target machine this machine code is compiled with |
| 131 | /// |
| 132 | const TargetMachine &getTarget() const { return Target; } |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 133 | |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 134 | /// SSARegMap Interface... Keep track of information about each SSA virtual |
| 135 | /// register, such as which register class it belongs to. |
| 136 | /// |
| 137 | SSARegMap *getSSARegMap() const { return SSARegMapping; } |
| 138 | void clearSSARegMap(); |
| 139 | |
| 140 | /// getFrameInfo - Return the frame info object for the current function. |
| 141 | /// This object contains information about objects allocated on the stack |
| 142 | /// frame of the current function in an abstract way. |
| 143 | /// |
Chris Lattner | aa09b75 | 2002-12-28 21:08:28 +0000 | [diff] [blame] | 144 | MachineFrameInfo *getFrameInfo() const { return FrameInfo; } |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 145 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 146 | /// getJumpTableInfo - Return the jump table info object for the current |
| 147 | /// function. This object contains information about jump tables for switch |
| 148 | /// instructions in the current function. |
| 149 | /// |
| 150 | MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; } |
| 151 | |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 152 | /// getConstantPool - Return the constant pool object for the current |
| 153 | /// function. |
Misha Brukman | 274ba03 | 2004-08-17 17:52:36 +0000 | [diff] [blame] | 154 | /// |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 155 | MachineConstantPool *getConstantPool() const { return ConstantPool; } |
| 156 | |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 157 | /// MachineFunctionInfo - Keep track of various per-function pieces of |
Chris Lattner | 67d2562 | 2005-08-31 22:49:51 +0000 | [diff] [blame] | 158 | /// information for backends that would like to do so. |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 159 | /// |
Chris Lattner | 8bdf87d | 2004-08-18 18:13:16 +0000 | [diff] [blame] | 160 | template<typename Ty> |
| 161 | Ty *getInfo() { |
| 162 | if (!MFInfo) MFInfo = new Ty(*this); |
| 163 | |
| 164 | assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo && |
| 165 | "Invalid concrete type or multiple inheritence for getInfo"); |
| 166 | return static_cast<Ty*>(MFInfo); |
| 167 | } |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 5ea64fd | 2006-08-17 22:00:08 +0000 | [diff] [blame] | 169 | template<typename Ty> |
| 170 | const Ty *getInfo() const { |
| 171 | return const_cast<MachineFunction*>(this)->getInfo<Ty>(); |
| 172 | } |
| 173 | |
Chris Lattner | d6b210c | 2005-01-23 22:13:36 +0000 | [diff] [blame] | 174 | /// isPhysRegUsed - Return true if the specified register is used in this |
| 175 | /// function. This only works after register allocation. |
Evan Cheng | 3cd4c89 | 2007-02-15 02:55:51 +0000 | [diff] [blame] | 176 | bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; } |
Chris Lattner | d6b210c | 2005-01-23 22:13:36 +0000 | [diff] [blame] | 177 | |
Evan Cheng | 505e551 | 2007-04-25 22:10:09 +0000 | [diff] [blame] | 178 | /// setPhysRegUsed - Mark the specified register used in this function. |
| 179 | /// This should only be called during and after register allocation. |
| 180 | void setPhysRegUsed(unsigned Reg) { UsedPhysRegs[Reg] = true; } |
Chris Lattner | d6b210c | 2005-01-23 22:13:36 +0000 | [diff] [blame] | 181 | |
Evan Cheng | 505e551 | 2007-04-25 22:10:09 +0000 | [diff] [blame] | 182 | /// setPhysRegUnused - Mark the specified register unused in this function. |
| 183 | /// This should only be called during and after register allocation. |
| 184 | void setPhysRegUnused(unsigned Reg) { UsedPhysRegs[Reg] = false; } |
Chris Lattner | dde3a9a | 2005-04-09 15:22:53 +0000 | [diff] [blame] | 185 | |
| 186 | // LiveIn/LiveOut management methods. |
| 187 | |
| 188 | /// addLiveIn/Out - Add the specified register as a live in/out. Note that it |
| 189 | /// is an error to add the same register to the same set more than once. |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame] | 190 | void addLiveIn(unsigned Reg, unsigned vreg = 0) { |
| 191 | LiveIns.push_back(std::make_pair(Reg, vreg)); |
| 192 | } |
Chris Lattner | dde3a9a | 2005-04-09 15:22:53 +0000 | [diff] [blame] | 193 | void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); } |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 194 | |
Chris Lattner | dde3a9a | 2005-04-09 15:22:53 +0000 | [diff] [blame] | 195 | // Iteration support for live in/out sets. These sets are kept in sorted |
| 196 | // order by their register number. |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame] | 197 | typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator |
| 198 | livein_iterator; |
| 199 | typedef std::vector<unsigned>::const_iterator liveout_iterator; |
| 200 | livein_iterator livein_begin() const { return LiveIns.begin(); } |
| 201 | livein_iterator livein_end() const { return LiveIns.end(); } |
Chris Lattner | 347d9d6 | 2006-04-11 01:09:25 +0000 | [diff] [blame] | 202 | bool livein_empty() const { return LiveIns.empty(); } |
Chris Lattner | 712ad0c | 2005-05-13 07:08:07 +0000 | [diff] [blame] | 203 | liveout_iterator liveout_begin() const { return LiveOuts.begin(); } |
| 204 | liveout_iterator liveout_end() const { return LiveOuts.end(); } |
Chris Lattner | 347d9d6 | 2006-04-11 01:09:25 +0000 | [diff] [blame] | 205 | bool liveout_empty() const { return LiveOuts.empty(); } |
Chris Lattner | dde3a9a | 2005-04-09 15:22:53 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 207 | /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they |
| 208 | /// are inserted into the machine function. The block number for a machine |
| 209 | /// basic block can be found by using the MBB::getBlockNumber method, this |
| 210 | /// method provides the inverse mapping. |
Misha Brukman | 274ba03 | 2004-08-17 17:52:36 +0000 | [diff] [blame] | 211 | /// |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 212 | MachineBasicBlock *getBlockNumbered(unsigned N) { |
| 213 | assert(N < MBBNumbering.size() && "Illegal block number"); |
| 214 | assert(MBBNumbering[N] && "Block was removed from the machine function!"); |
| 215 | return MBBNumbering[N]; |
| 216 | } |
Chris Lattner | cd0d1d1 | 2002-12-28 20:04:31 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 2540813 | 2006-09-14 06:40:48 +0000 | [diff] [blame] | 218 | /// getNumBlockIDs - Return the number of MBB ID's allocated. |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 219 | /// |
Chris Lattner | 2540813 | 2006-09-14 06:40:48 +0000 | [diff] [blame] | 220 | unsigned getNumBlockIDs() const { return MBBNumbering.size(); } |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 221 | |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 222 | /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and |
| 223 | /// recomputes them. This guarantees that the MBB numbers are sequential, |
| 224 | /// dense, and match the ordering of the blocks within the function. If a |
| 225 | /// specific MachineBasicBlock is specified, only that block and those after |
| 226 | /// it are renumbered. |
| 227 | void RenumberBlocks(MachineBasicBlock *MBBFrom = 0); |
| 228 | |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 229 | /// print - Print out the MachineFunction in a format suitable for debugging |
| 230 | /// to the specified stream. |
| 231 | /// |
| 232 | void print(std::ostream &OS) const; |
Bill Wendling | 5c7e326 | 2006-12-17 05:15:13 +0000 | [diff] [blame] | 233 | void print(std::ostream *OS) const { if (OS) print(*OS); } |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 234 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 235 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 236 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 237 | /// program, displaying the CFG of the current function with the code for each |
| 238 | /// basic block inside. This depends on there being a 'dot' and 'gv' program |
| 239 | /// in your path. |
| 240 | /// |
| 241 | void viewCFG() const; |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 242 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 243 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 244 | /// just like viewCFG, but it does not include the contents of basic blocks |
| 245 | /// into the nodes, just the label. If you are only interested in the CFG |
| 246 | /// this can make the graph smaller. |
| 247 | /// |
| 248 | void viewCFGOnly() const; |
| 249 | |
Chris Lattner | dea7383 | 2002-10-30 00:46:31 +0000 | [diff] [blame] | 250 | /// dump - Print the current MachineFunction to cerr, useful for debugger use. |
| 251 | /// |
| 252 | void dump() const; |
| 253 | |
Misha Brukman | a7afa37 | 2004-06-04 14:51:25 +0000 | [diff] [blame] | 254 | /// construct - Allocate and initialize a MachineFunction for a given Function |
| 255 | /// and Target |
| 256 | /// |
Chris Lattner | 40a7557 | 2003-01-13 00:16:10 +0000 | [diff] [blame] | 257 | static MachineFunction& construct(const Function *F, const TargetMachine &TM); |
Misha Brukman | a7afa37 | 2004-06-04 14:51:25 +0000 | [diff] [blame] | 258 | |
| 259 | /// destruct - Destroy the MachineFunction corresponding to a given Function |
| 260 | /// |
Chris Lattner | e7506a3 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 261 | static void destruct(const Function *F); |
Misha Brukman | a7afa37 | 2004-06-04 14:51:25 +0000 | [diff] [blame] | 262 | |
| 263 | /// get - Return a handle to a MachineFunction corresponding to the given |
| 264 | /// Function. This should not be called before "construct()" for a given |
| 265 | /// Function. |
| 266 | /// |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 267 | static MachineFunction& get(const Function *F); |
Chris Lattner | 9c6342d | 2002-10-28 01:27:51 +0000 | [diff] [blame] | 268 | |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 269 | // Provide accessors for the MachineBasicBlock list... |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 270 | typedef ilist<MachineBasicBlock> BasicBlockListType; |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 271 | typedef BasicBlockListType::iterator iterator; |
| 272 | typedef BasicBlockListType::const_iterator const_iterator; |
| 273 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 274 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 275 | |
| 276 | // Provide accessors for basic blocks... |
| 277 | const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } |
| 278 | BasicBlockListType &getBasicBlockList() { return BasicBlocks; } |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 279 | |
Chris Lattner | d0aa0cd | 2002-10-28 05:30:46 +0000 | [diff] [blame] | 280 | //===--------------------------------------------------------------------===// |
| 281 | // BasicBlock iterator forwarding functions |
| 282 | // |
| 283 | iterator begin() { return BasicBlocks.begin(); } |
| 284 | const_iterator begin() const { return BasicBlocks.begin(); } |
| 285 | iterator end () { return BasicBlocks.end(); } |
| 286 | const_iterator end () const { return BasicBlocks.end(); } |
| 287 | |
| 288 | reverse_iterator rbegin() { return BasicBlocks.rbegin(); } |
| 289 | const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } |
| 290 | reverse_iterator rend () { return BasicBlocks.rend(); } |
| 291 | const_reverse_iterator rend () const { return BasicBlocks.rend(); } |
| 292 | |
| 293 | unsigned size() const { return BasicBlocks.size(); } |
| 294 | bool empty() const { return BasicBlocks.empty(); } |
| 295 | const MachineBasicBlock &front() const { return BasicBlocks.front(); } |
| 296 | MachineBasicBlock &front() { return BasicBlocks.front(); } |
Misha Brukman | d5806ff | 2002-10-28 19:58:38 +0000 | [diff] [blame] | 297 | const MachineBasicBlock & back() const { return BasicBlocks.back(); } |
| 298 | MachineBasicBlock & back() { return BasicBlocks.back(); } |
Chris Lattner | 25d8039 | 2004-07-01 06:01:36 +0000 | [diff] [blame] | 299 | |
| 300 | //===--------------------------------------------------------------------===// |
| 301 | // Internal functions used to automatically number MachineBasicBlocks |
| 302 | // |
| 303 | |
| 304 | /// getNextMBBNumber - Returns the next unique number to be assigned |
| 305 | /// to a MachineBasicBlock in this MachineFunction. |
| 306 | /// |
| 307 | unsigned addToMBBNumbering(MachineBasicBlock *MBB) { |
| 308 | MBBNumbering.push_back(MBB); |
| 309 | return MBBNumbering.size()-1; |
| 310 | } |
| 311 | |
| 312 | /// removeFromMBBNumbering - Remove the specific machine basic block from our |
| 313 | /// tracker, this is only really to be used by the MachineBasicBlock |
| 314 | /// implementation. |
| 315 | void removeFromMBBNumbering(unsigned N) { |
| 316 | assert(N < MBBNumbering.size() && "Illegal basic block #"); |
| 317 | MBBNumbering[N] = 0; |
| 318 | } |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 321 | //===--------------------------------------------------------------------===// |
| 322 | // GraphTraits specializations for function basic block graphs (CFGs) |
| 323 | //===--------------------------------------------------------------------===// |
| 324 | |
| 325 | // Provide specializations of GraphTraits to be able to treat a |
| 326 | // machine function as a graph of machine basic blocks... these are |
| 327 | // the same as the machine basic block iterators, except that the root |
| 328 | // node is implicitly the first node of the function. |
| 329 | // |
| 330 | template <> struct GraphTraits<MachineFunction*> : |
| 331 | public GraphTraits<MachineBasicBlock*> { |
| 332 | static NodeType *getEntryNode(MachineFunction *F) { |
| 333 | return &F->front(); |
| 334 | } |
| 335 | |
| 336 | // nodes_iterator/begin/end - Allow iteration over all nodes in the graph |
| 337 | typedef MachineFunction::iterator nodes_iterator; |
| 338 | static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); } |
| 339 | static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); } |
| 340 | }; |
| 341 | template <> struct GraphTraits<const MachineFunction*> : |
| 342 | public GraphTraits<const MachineBasicBlock*> { |
| 343 | static NodeType *getEntryNode(const MachineFunction *F) { |
| 344 | return &F->front(); |
| 345 | } |
| 346 | |
| 347 | // nodes_iterator/begin/end - Allow iteration over all nodes in the graph |
| 348 | typedef MachineFunction::const_iterator nodes_iterator; |
| 349 | static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); } |
| 350 | static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); } |
| 351 | }; |
| 352 | |
| 353 | |
Misha Brukman | ea61c35 | 2005-04-21 20:39:54 +0000 | [diff] [blame] | 354 | // Provide specializations of GraphTraits to be able to treat a function as a |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 355 | // graph of basic blocks... and to walk it in inverse order. Inverse order for |
| 356 | // a function is considered to be when traversing the predecessor edges of a BB |
| 357 | // instead of the successor edges. |
| 358 | // |
| 359 | template <> struct GraphTraits<Inverse<MachineFunction*> > : |
| 360 | public GraphTraits<Inverse<MachineBasicBlock*> > { |
| 361 | static NodeType *getEntryNode(Inverse<MachineFunction*> G) { |
| 362 | return &G.Graph->front(); |
| 363 | } |
| 364 | }; |
| 365 | template <> struct GraphTraits<Inverse<const MachineFunction*> > : |
| 366 | public GraphTraits<Inverse<const MachineBasicBlock*> > { |
| 367 | static NodeType *getEntryNode(Inverse<const MachineFunction *> G) { |
| 368 | return &G.Graph->front(); |
| 369 | } |
| 370 | }; |
| 371 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 372 | } // End llvm namespace |
| 373 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 374 | #endif |