1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/ADT/GraphTraits.h"
23 class MachineFunction;
29 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
31 mutable ilist_half_node<MachineInstr> Sentinel;
33 // this is only set by the MachineBasicBlock owning the LiveList
34 friend class MachineBasicBlock;
35 MachineBasicBlock* Parent;
38 MachineInstr *createSentinel() const {
39 return static_cast<MachineInstr*>(&Sentinel);
41 void destroySentinel(MachineInstr *) const {}
43 MachineInstr *provideInitialHead() const { return createSentinel(); }
44 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
45 static void noteHead(MachineInstr*, MachineInstr*) {}
47 void addNodeToList(MachineInstr* N);
48 void removeNodeFromList(MachineInstr* N);
49 void transferNodesFromList(ilist_traits &SrcTraits,
50 ilist_iterator<MachineInstr> first,
51 ilist_iterator<MachineInstr> last);
52 void deleteNode(MachineInstr *N);
54 void createNode(const MachineInstr &);
57 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
58 typedef ilist<MachineInstr> Instructions;
62 MachineFunction *xParent;
64 /// Predecessors/Successors - Keep track of the predecessor / successor
66 std::vector<MachineBasicBlock *> Predecessors;
67 std::vector<MachineBasicBlock *> Successors;
69 /// LiveIns - Keep track of the physical registers that are livein of
71 std::vector<unsigned> LiveIns;
73 /// Alignment - Alignment of the basic block. Zero if the basic block does
74 /// not need to be aligned.
77 /// IsLandingPad - Indicate that this basic block is entered via an
78 /// exception handler.
81 /// AddressTaken - Indicate that this basic block is potentially the
82 /// target of an indirect branch.
85 // Intrusive list support
86 MachineBasicBlock() {}
88 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
92 // MachineBasicBlocks are allocated and owned by MachineFunction.
93 friend class MachineFunction;
96 /// getBasicBlock - Return the LLVM basic block that this instance
97 /// corresponded to originally. Note that this may be NULL if this instance
98 /// does not correspond directly to an LLVM basic block.
100 const BasicBlock *getBasicBlock() const { return BB; }
102 /// getName - Return the name of the corresponding LLVM basic block, or
104 StringRef getName() const;
106 /// hasAddressTaken - Test whether this block is potentially the target
107 /// of an indirect branch.
108 bool hasAddressTaken() const { return AddressTaken; }
110 /// setHasAddressTaken - Set this block to reflect that it potentially
111 /// is the target of an indirect branch.
112 void setHasAddressTaken() { AddressTaken = true; }
114 /// getParent - Return the MachineFunction containing this basic block.
116 const MachineFunction *getParent() const { return xParent; }
117 MachineFunction *getParent() { return xParent; }
119 typedef Instructions::iterator iterator;
120 typedef Instructions::const_iterator const_iterator;
121 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
122 typedef std::reverse_iterator<iterator> reverse_iterator;
124 unsigned size() const { return (unsigned)Insts.size(); }
125 bool empty() const { return Insts.empty(); }
127 MachineInstr& front() { return Insts.front(); }
128 MachineInstr& back() { return Insts.back(); }
129 const MachineInstr& front() const { return Insts.front(); }
130 const MachineInstr& back() const { return Insts.back(); }
132 iterator begin() { return Insts.begin(); }
133 const_iterator begin() const { return Insts.begin(); }
134 iterator end() { return Insts.end(); }
135 const_iterator end() const { return Insts.end(); }
136 reverse_iterator rbegin() { return Insts.rbegin(); }
137 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
138 reverse_iterator rend () { return Insts.rend(); }
139 const_reverse_iterator rend () const { return Insts.rend(); }
141 // Machine-CFG iterators
142 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
143 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
144 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
145 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
146 typedef std::vector<MachineBasicBlock *>::reverse_iterator
147 pred_reverse_iterator;
148 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
149 const_pred_reverse_iterator;
150 typedef std::vector<MachineBasicBlock *>::reverse_iterator
151 succ_reverse_iterator;
152 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
153 const_succ_reverse_iterator;
155 pred_iterator pred_begin() { return Predecessors.begin(); }
156 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
157 pred_iterator pred_end() { return Predecessors.end(); }
158 const_pred_iterator pred_end() const { return Predecessors.end(); }
159 pred_reverse_iterator pred_rbegin()
160 { return Predecessors.rbegin();}
161 const_pred_reverse_iterator pred_rbegin() const
162 { return Predecessors.rbegin();}
163 pred_reverse_iterator pred_rend()
164 { return Predecessors.rend(); }
165 const_pred_reverse_iterator pred_rend() const
166 { return Predecessors.rend(); }
167 unsigned pred_size() const {
168 return (unsigned)Predecessors.size();
170 bool pred_empty() const { return Predecessors.empty(); }
171 succ_iterator succ_begin() { return Successors.begin(); }
172 const_succ_iterator succ_begin() const { return Successors.begin(); }
173 succ_iterator succ_end() { return Successors.end(); }
174 const_succ_iterator succ_end() const { return Successors.end(); }
175 succ_reverse_iterator succ_rbegin()
176 { return Successors.rbegin(); }
177 const_succ_reverse_iterator succ_rbegin() const
178 { return Successors.rbegin(); }
179 succ_reverse_iterator succ_rend()
180 { return Successors.rend(); }
181 const_succ_reverse_iterator succ_rend() const
182 { return Successors.rend(); }
183 unsigned succ_size() const {
184 return (unsigned)Successors.size();
186 bool succ_empty() const { return Successors.empty(); }
188 // LiveIn management methods.
190 /// addLiveIn - Add the specified register as a live in. Note that it
191 /// is an error to add the same register to the same set more than once.
192 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
194 /// removeLiveIn - Remove the specified register from the live in set.
196 void removeLiveIn(unsigned Reg);
198 /// isLiveIn - Return true if the specified register is in the live in set.
200 bool isLiveIn(unsigned Reg) const;
202 // Iteration support for live in sets. These sets are kept in sorted
203 // order by their register number.
204 typedef std::vector<unsigned>::iterator livein_iterator;
205 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
206 livein_iterator livein_begin() { return LiveIns.begin(); }
207 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
208 livein_iterator livein_end() { return LiveIns.end(); }
209 const_livein_iterator livein_end() const { return LiveIns.end(); }
210 bool livein_empty() const { return LiveIns.empty(); }
212 /// getAlignment - Return alignment of the basic block.
214 unsigned getAlignment() const { return Alignment; }
216 /// setAlignment - Set alignment of the basic block.
218 void setAlignment(unsigned Align) { Alignment = Align; }
220 /// isLandingPad - Returns true if the block is a landing pad. That is
221 /// this basic block is entered via an exception handler.
222 bool isLandingPad() const { return IsLandingPad; }
224 /// setIsLandingPad - Indicates the block is a landing pad. That is
225 /// this basic block is entered via an exception handler.
226 void setIsLandingPad() { IsLandingPad = true; }
228 // Code Layout methods.
230 /// moveBefore/moveAfter - move 'this' block before or after the specified
231 /// block. This only moves the block, it does not modify the CFG or adjust
232 /// potential fall-throughs at the end of the block.
233 void moveBefore(MachineBasicBlock *NewAfter);
234 void moveAfter(MachineBasicBlock *NewBefore);
236 /// updateTerminator - Update the terminator instructions in block to account
237 /// for changes to the layout. If the block previously used a fallthrough,
238 /// it may now need a branch, and if it previously used branching it may now
239 /// be able to use a fallthrough.
240 void updateTerminator();
242 // Machine-CFG mutators
244 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
245 /// The Predecessors list of succ is automatically updated.
247 void addSuccessor(MachineBasicBlock *succ);
249 /// removeSuccessor - Remove successor from the successors list of this
250 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
252 void removeSuccessor(MachineBasicBlock *succ);
254 /// removeSuccessor - Remove specified successor from the successors list of
255 /// this MachineBasicBlock. The Predecessors list of succ is automatically
256 /// updated. Return the iterator to the element after the one removed.
258 succ_iterator removeSuccessor(succ_iterator I);
260 /// transferSuccessors - Transfers all the successors from MBB to this
261 /// machine basic block (i.e., copies all the successors fromMBB and
262 /// remove all the successors from fromMBB).
263 void transferSuccessors(MachineBasicBlock *fromMBB);
265 /// isSuccessor - Return true if the specified MBB is a successor of this
267 bool isSuccessor(const MachineBasicBlock *MBB) const;
269 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
270 /// immediately after this block, such that if this block exits by
271 /// falling through, control will transfer to the specified MBB. Note
272 /// that MBB need not be a successor at all, for example if this block
273 /// ends with an unconditional branch to some other block.
274 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
276 /// canFallThrough - Return true if the block can implicitly transfer
277 /// control to the block after it by falling off the end of it. This should
278 /// return false if it can reach the block after it, but it uses an explicit
279 /// branch to do so (e.g., a table jump). True is a conservative answer.
280 bool canFallThrough();
282 /// getFirstTerminator - returns an iterator to the first terminator
283 /// instruction of this basic block. If a terminator does not exist,
285 iterator getFirstTerminator();
287 void pop_front() { Insts.pop_front(); }
288 void pop_back() { Insts.pop_back(); }
289 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
290 template<typename IT>
291 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
292 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
294 // erase - Remove the specified element or range from the instruction list.
295 // These functions delete any instructions removed.
297 iterator erase(iterator I) { return Insts.erase(I); }
298 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
299 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
300 void clear() { Insts.clear(); }
302 /// splice - Take an instruction from MBB 'Other' at the position From,
303 /// and insert it into this MBB right before 'where'.
304 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
305 Insts.splice(where, Other->Insts, From);
308 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
309 /// To), and insert them into this MBB right before 'where'.
310 void splice(iterator where, MachineBasicBlock *Other, iterator From,
312 Insts.splice(where, Other->Insts, From, To);
315 /// removeFromParent - This method unlinks 'this' from the containing
316 /// function, and returns it, but does not delete it.
317 MachineBasicBlock *removeFromParent();
319 /// eraseFromParent - This method unlinks 'this' from the containing
320 /// function and deletes it.
321 void eraseFromParent();
323 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
324 /// 'Old', change the code and CFG so that it branches to 'New' instead.
325 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
327 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
328 /// the CFG to be inserted. If we have proven that MBB can only branch to
329 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
330 /// DestB can be null. Besides DestA and DestB, retain other edges leading
331 /// to LandingPads (currently there can be only one; we don't check or require
332 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
333 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
334 MachineBasicBlock *DestB,
337 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
338 /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
339 DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
341 // Debugging methods.
343 void print(raw_ostream &OS) const;
345 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
346 /// level, unless they're not in a MachineFunction yet, in which case this
349 int getNumber() const { return Number; }
350 void setNumber(int N) { Number = N; }
352 /// getSymbol - Return the MCSymbol for this basic block.
354 MCSymbol *getSymbol() const;
356 private: // Methods used to maintain doubly linked list of blocks...
357 friend struct ilist_traits<MachineBasicBlock>;
359 // Machine-CFG mutators
361 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
362 /// Don't do this unless you know what you're doing, because it doesn't
363 /// update pred's successors list. Use pred->addSuccessor instead.
365 void addPredecessor(MachineBasicBlock *pred);
367 /// removePredecessor - Remove pred as a predecessor of this
368 /// MachineBasicBlock. Don't do this unless you know what you're
369 /// doing, because it doesn't update pred's successors list. Use
370 /// pred->removeSuccessor instead.
372 void removePredecessor(MachineBasicBlock *pred);
375 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
377 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
379 //===--------------------------------------------------------------------===//
380 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
381 //===--------------------------------------------------------------------===//
383 // Provide specializations of GraphTraits to be able to treat a
384 // MachineFunction as a graph of MachineBasicBlocks...
387 template <> struct GraphTraits<MachineBasicBlock *> {
388 typedef MachineBasicBlock NodeType;
389 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
391 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
392 static inline ChildIteratorType child_begin(NodeType *N) {
393 return N->succ_begin();
395 static inline ChildIteratorType child_end(NodeType *N) {
396 return N->succ_end();
400 template <> struct GraphTraits<const MachineBasicBlock *> {
401 typedef const MachineBasicBlock NodeType;
402 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
404 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
405 static inline ChildIteratorType child_begin(NodeType *N) {
406 return N->succ_begin();
408 static inline ChildIteratorType child_end(NodeType *N) {
409 return N->succ_end();
413 // Provide specializations of GraphTraits to be able to treat a
414 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
415 // in inverse order. Inverse order for a function is considered
416 // to be when traversing the predecessor edges of a MBB
417 // instead of the successor edges.
419 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
420 typedef MachineBasicBlock NodeType;
421 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
422 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
425 static inline ChildIteratorType child_begin(NodeType *N) {
426 return N->pred_begin();
428 static inline ChildIteratorType child_end(NodeType *N) {
429 return N->pred_end();
433 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
434 typedef const MachineBasicBlock NodeType;
435 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
436 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
439 static inline ChildIteratorType child_begin(NodeType *N) {
440 return N->pred_begin();
442 static inline ChildIteratorType child_end(NodeType *N) {
443 return N->pred_end();
447 } // End llvm namespace