blob: 3c79b944f50de29a85f8faabd0ac7cbd8f50e65e [file] [log] [blame]
Chris Lattner9c6342d2002-10-28 01:27:51 +00001//===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
Misha Brukmanea61c352005-04-21 20:39:54 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// 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 Brukmanea61c352005-04-21 20:39:54 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Misha Brukmanea61c352005-04-21 20:39:54 +00009//
Chris Lattnercd0d1d12002-12-28 20:04:31 +000010// 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 Brukmanea61c352005-04-21 20:39:54 +000015//
Chris Lattnerf2868ce2002-02-03 07:54:50 +000016//===----------------------------------------------------------------------===//
17
Misha Brukmanfce11432002-10-28 00:28:31 +000018#ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
19#define LLVM_CODEGEN_MACHINEFUNCTION_H
Chris Lattnerf2868ce2002-02-03 07:54:50 +000020
Jim Laskey44c3b9f2007-01-26 21:22:28 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner0551f542002-10-28 02:01:06 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Annotation.h"
Evan Cheng505e5512007-04-25 22:10:09 +000024#include "llvm/ADT/BitVector.h"
Chris Lattner8e7ae982002-10-28 02:08:43 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Chris Lattner8bdf87d2004-08-18 18:13:16 +000028class Function;
29class TargetMachine;
30class SSARegMap;
31class MachineFrameInfo;
32class MachineConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +000033class MachineJumpTableInfo;
Chris Lattner8bdf87d2004-08-18 18:13:16 +000034
Tanya Lattner792699c2004-05-24 06:11:51 +000035// ilist_traits
36template <>
Chris Lattner1fca5ff2004-10-27 16:14:51 +000037struct ilist_traits<MachineBasicBlock> {
Tanya Lattner792699c2004-05-24 06:11:51 +000038 // this is only set by the MachineFunction owning the ilist
39 friend class MachineFunction;
Tanya Lattner17fb34b2004-05-24 07:14:35 +000040 MachineFunction* Parent;
Misha Brukmanea61c352005-04-21 20:39:54 +000041
Tanya Lattner792699c2004-05-24 06:11:51 +000042public:
Tanya Lattner17fb34b2004-05-24 07:14:35 +000043 ilist_traits<MachineBasicBlock>() : Parent(0) { }
Misha Brukmanea61c352005-04-21 20:39:54 +000044
Tanya Lattner792699c2004-05-24 06:11:51 +000045 static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; }
46 static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; }
Misha Brukmanea61c352005-04-21 20:39:54 +000047
Tanya Lattner792699c2004-05-24 06:11:51 +000048 static const MachineBasicBlock*
49 getPrev(const MachineBasicBlock* N) { return N->Prev; }
Misha Brukmanea61c352005-04-21 20:39:54 +000050
Tanya Lattner792699c2004-05-24 06:11:51 +000051 static const MachineBasicBlock*
52 getNext(const MachineBasicBlock* N) { return N->Next; }
Misha Brukmanea61c352005-04-21 20:39:54 +000053
Chris Lattner2fcd4512004-08-16 22:35:26 +000054 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 Brukman274ba032004-08-17 17:52:36 +000060
Chris Lattnerbbec41d2005-01-30 00:13:34 +000061 static MachineBasicBlock* createSentinel();
62 static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; }
Tanya Lattner792699c2004-05-24 06:11:51 +000063 void addNodeToList(MachineBasicBlock* N);
64 void removeNodeFromList(MachineBasicBlock* N);
Chris Lattner2fcd4512004-08-16 22:35:26 +000065 void transferNodesFromList(iplist<MachineBasicBlock,
66 ilist_traits<MachineBasicBlock> > &toList,
Misha Brukman00876a22005-04-22 03:46:24 +000067 ilist_iterator<MachineBasicBlock> first,
68 ilist_iterator<MachineBasicBlock> last);
Tanya Lattner792699c2004-05-24 06:11:51 +000069};
Tanya Lattner792699c2004-05-24 06:11:51 +000070
Chris Lattner8bdf87d2004-08-18 18:13:16 +000071/// 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.
75struct MachineFunctionInfo {
Dan Gohman81975f62007-08-27 14:50:10 +000076 virtual ~MachineFunctionInfo() {}
Chris Lattner8bdf87d2004-08-18 18:13:16 +000077};
Chris Lattnerf2868ce2002-02-03 07:54:50 +000078
Misha Brukmanfce11432002-10-28 00:28:31 +000079class MachineFunction : private Annotation {
Chris Lattner335d5c32002-10-28 05:58:46 +000080 const Function *Fn;
81 const TargetMachine &Target;
Chris Lattner8e7ae982002-10-28 02:08:43 +000082
83 // List of machine basic blocks in function
Tanya Lattner17fb34b2004-05-24 07:14:35 +000084 ilist<MachineBasicBlock> BasicBlocks;
Chris Lattner9c6342d2002-10-28 01:27:51 +000085
Chris Lattner03ab7af2002-12-25 05:00:16 +000086 // Keeping track of mapping from SSA values to registers
87 SSARegMap *SSARegMapping;
88
Chris Lattner8bdf87d2004-08-18 18:13:16 +000089 // Used to keep track of target-specific per-machine function information for
90 // the target implementation.
91 MachineFunctionInfo *MFInfo;
Chris Lattnercd0d1d12002-12-28 20:04:31 +000092
93 // Keep track of objects allocated on the stack.
Chris Lattneraa09b752002-12-28 21:08:28 +000094 MachineFrameInfo *FrameInfo;
Misha Brukman1617e6c2002-11-20 00:53:10 +000095
Chris Lattner40a75572003-01-13 00:16:10 +000096 // Keep track of constants which are spilled to memory
97 MachineConstantPool *ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +000098
99 // Keep track of jump tables for switch instructions
100 MachineJumpTableInfo *JumpTableInfo;
Chris Lattner40a75572003-01-13 00:16:10 +0000101
Chris Lattner25d80392004-07-01 06:01:36 +0000102 // 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 Gaekef460f162004-05-12 21:35:21 +0000106
Evan Cheng505e5512007-04-25 22:10:09 +0000107 /// 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 Lattnerd6b210c2005-01-23 22:13:36 +0000110 /// so that the code generator knows which callee save registers to save and
111 /// for other target specific uses.
Evan Cheng505e5512007-04-25 22:10:09 +0000112 BitVector UsedPhysRegs;
Chris Lattnerd6b210c2005-01-23 22:13:36 +0000113
Chris Lattnerdde3a9a2005-04-09 15:22:53 +0000114 /// 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 Lattner712ad0c2005-05-13 07:08:07 +0000117 /// 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 Laskeyf5395ce2005-12-16 22:45:29 +0000121
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000122public:
Chris Lattner40a75572003-01-13 00:16:10 +0000123 MachineFunction(const Function *Fn, const TargetMachine &TM);
Chris Lattner03ab7af2002-12-25 05:00:16 +0000124 ~MachineFunction();
Chris Lattner335d5c32002-10-28 05:58:46 +0000125
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 Lattnerdea73832002-10-30 00:46:31 +0000133
Chris Lattnercd0d1d12002-12-28 20:04:31 +0000134 /// 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 Lattneraa09b752002-12-28 21:08:28 +0000144 MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
Chris Lattnercd0d1d12002-12-28 20:04:31 +0000145
Nate Begeman37efe672006-04-22 18:53:45 +0000146 /// 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 Lattner40a75572003-01-13 00:16:10 +0000152 /// getConstantPool - Return the constant pool object for the current
153 /// function.
Misha Brukman274ba032004-08-17 17:52:36 +0000154 ///
Chris Lattner40a75572003-01-13 00:16:10 +0000155 MachineConstantPool *getConstantPool() const { return ConstantPool; }
156
Chris Lattnercd0d1d12002-12-28 20:04:31 +0000157 /// MachineFunctionInfo - Keep track of various per-function pieces of
Chris Lattner67d25622005-08-31 22:49:51 +0000158 /// information for backends that would like to do so.
Chris Lattnercd0d1d12002-12-28 20:04:31 +0000159 ///
Chris Lattner8bdf87d2004-08-18 18:13:16 +0000160 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 Lattnercd0d1d12002-12-28 20:04:31 +0000168
Chris Lattner5ea64fd2006-08-17 22:00:08 +0000169 template<typename Ty>
170 const Ty *getInfo() const {
171 return const_cast<MachineFunction*>(this)->getInfo<Ty>();
172 }
173
Chris Lattnerd6b210c2005-01-23 22:13:36 +0000174 /// isPhysRegUsed - Return true if the specified register is used in this
175 /// function. This only works after register allocation.
Evan Cheng3cd4c892007-02-15 02:55:51 +0000176 bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; }
Chris Lattnerd6b210c2005-01-23 22:13:36 +0000177
Evan Cheng505e5512007-04-25 22:10:09 +0000178 /// 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 Lattnerd6b210c2005-01-23 22:13:36 +0000181
Evan Cheng505e5512007-04-25 22:10:09 +0000182 /// 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 Lattnerdde3a9a2005-04-09 15:22:53 +0000185
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 Lattner712ad0c2005-05-13 07:08:07 +0000190 void addLiveIn(unsigned Reg, unsigned vreg = 0) {
191 LiveIns.push_back(std::make_pair(Reg, vreg));
192 }
Chris Lattnerdde3a9a2005-04-09 15:22:53 +0000193 void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
Misha Brukmanea61c352005-04-21 20:39:54 +0000194
Chris Lattnerdde3a9a2005-04-09 15:22:53 +0000195 // Iteration support for live in/out sets. These sets are kept in sorted
196 // order by their register number.
Chris Lattner712ad0c2005-05-13 07:08:07 +0000197 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 Lattner347d9d62006-04-11 01:09:25 +0000202 bool livein_empty() const { return LiveIns.empty(); }
Chris Lattner712ad0c2005-05-13 07:08:07 +0000203 liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
204 liveout_iterator liveout_end() const { return LiveOuts.end(); }
Chris Lattner347d9d62006-04-11 01:09:25 +0000205 bool liveout_empty() const { return LiveOuts.empty(); }
Chris Lattnerdde3a9a2005-04-09 15:22:53 +0000206
Chris Lattner25d80392004-07-01 06:01:36 +0000207 /// 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 Brukman274ba032004-08-17 17:52:36 +0000211 ///
Chris Lattner25d80392004-07-01 06:01:36 +0000212 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 Lattnercd0d1d12002-12-28 20:04:31 +0000217
Chris Lattner25408132006-09-14 06:40:48 +0000218 /// getNumBlockIDs - Return the number of MBB ID's allocated.
Chris Lattnere70cab02006-10-03 19:18:57 +0000219 ///
Chris Lattner25408132006-09-14 06:40:48 +0000220 unsigned getNumBlockIDs() const { return MBBNumbering.size(); }
Jim Laskeyf5395ce2005-12-16 22:45:29 +0000221
Chris Lattnere70cab02006-10-03 19:18:57 +0000222 /// 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 Lattnerdea73832002-10-30 00:46:31 +0000229 /// 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 Wendling5c7e3262006-12-17 05:15:13 +0000233 void print(std::ostream *OS) const { if (OS) print(*OS); }
Chris Lattnerdea73832002-10-30 00:46:31 +0000234
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000235 /// 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 Brukmanea61c352005-04-21 20:39:54 +0000242
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000243 /// 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 Lattnerdea73832002-10-30 00:46:31 +0000250 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
251 ///
252 void dump() const;
253
Misha Brukmana7afa372004-06-04 14:51:25 +0000254 /// construct - Allocate and initialize a MachineFunction for a given Function
255 /// and Target
256 ///
Chris Lattner40a75572003-01-13 00:16:10 +0000257 static MachineFunction& construct(const Function *F, const TargetMachine &TM);
Misha Brukmana7afa372004-06-04 14:51:25 +0000258
259 /// destruct - Destroy the MachineFunction corresponding to a given Function
260 ///
Chris Lattnere7506a32002-03-23 22:51:58 +0000261 static void destruct(const Function *F);
Misha Brukmana7afa372004-06-04 14:51:25 +0000262
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 Lattnerd0aa0cd2002-10-28 05:30:46 +0000267 static MachineFunction& get(const Function *F);
Chris Lattner9c6342d2002-10-28 01:27:51 +0000268
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000269 // Provide accessors for the MachineBasicBlock list...
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000270 typedef ilist<MachineBasicBlock> BasicBlockListType;
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000271 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 Brukmanea61c352005-04-21 20:39:54 +0000279
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000280 //===--------------------------------------------------------------------===//
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 Brukmand5806ff2002-10-28 19:58:38 +0000297 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
298 MachineBasicBlock & back() { return BasicBlocks.back(); }
Chris Lattner25d80392004-07-01 06:01:36 +0000299
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 Lattnerf2868ce2002-02-03 07:54:50 +0000319};
320
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000321//===--------------------------------------------------------------------===//
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//
330template <> 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};
341template <> 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 Brukmanea61c352005-04-21 20:39:54 +0000354// Provide specializations of GraphTraits to be able to treat a function as a
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000355// 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//
359template <> struct GraphTraits<Inverse<MachineFunction*> > :
360 public GraphTraits<Inverse<MachineBasicBlock*> > {
361 static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
362 return &G.Graph->front();
363 }
364};
365template <> 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 Gaeked0fde302003-11-11 22:41:34 +0000372} // End llvm namespace
373
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000374#endif