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;
27 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
29 mutable ilist_half_node<MachineInstr> Sentinel;
31 // this is only set by the MachineBasicBlock owning the LiveList
32 friend class MachineBasicBlock;
33 MachineBasicBlock* Parent;
36 MachineInstr *createSentinel() const {
37 return static_cast<MachineInstr*>(&Sentinel);
39 void destroySentinel(MachineInstr *) const {}
41 MachineInstr *provideInitialHead() const { return createSentinel(); }
42 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
43 static void noteHead(MachineInstr*, MachineInstr*) {}
45 void addNodeToList(MachineInstr* N);
46 void removeNodeFromList(MachineInstr* N);
47 void transferNodesFromList(ilist_traits &SrcTraits,
48 ilist_iterator<MachineInstr> first,
49 ilist_iterator<MachineInstr> last);
50 void deleteNode(MachineInstr *N);
52 void createNode(const MachineInstr &);
55 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
56 typedef ilist<MachineInstr> Instructions;
60 MachineFunction *xParent;
62 /// Predecessors/Successors - Keep track of the predecessor / successor
64 std::vector<MachineBasicBlock *> Predecessors;
65 std::vector<MachineBasicBlock *> Successors;
67 /// LiveIns - Keep track of the physical registers that are livein of
69 std::vector<unsigned> LiveIns;
71 /// Alignment - Alignment of the basic block. Zero if the basic block does
72 /// not need to be aligned.
75 /// IsLandingPad - Indicate that this basic block is entered via an
76 /// exception handler.
79 /// AddressTaken - Indicate that this basic block is potentially the
80 /// target of an indirect branch.
83 // Intrusive list support
84 MachineBasicBlock() {}
86 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
90 // MachineBasicBlocks are allocated and owned by MachineFunction.
91 friend class MachineFunction;
94 /// getBasicBlock - Return the LLVM basic block that this instance
95 /// corresponded to originally.
97 const BasicBlock *getBasicBlock() const { return BB; }
99 /// hasAddressTaken - Test whether this block is potentially the target
100 /// of an indirect branch.
101 bool hasAddressTaken() const { return AddressTaken; }
103 /// setHasAddressTaken - Set this block to reflect that it potentially
104 /// is the target of an indirect branch.
105 void setHasAddressTaken() { AddressTaken = true; }
107 /// getParent - Return the MachineFunction containing this basic block.
109 const MachineFunction *getParent() const { return xParent; }
110 MachineFunction *getParent() { return xParent; }
112 typedef Instructions::iterator iterator;
113 typedef Instructions::const_iterator const_iterator;
114 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
115 typedef std::reverse_iterator<iterator> reverse_iterator;
117 unsigned size() const { return (unsigned)Insts.size(); }
118 bool empty() const { return Insts.empty(); }
120 MachineInstr& front() { return Insts.front(); }
121 MachineInstr& back() { return Insts.back(); }
122 const MachineInstr& front() const { return Insts.front(); }
123 const MachineInstr& back() const { return Insts.back(); }
125 iterator begin() { return Insts.begin(); }
126 const_iterator begin() const { return Insts.begin(); }
127 iterator end() { return Insts.end(); }
128 const_iterator end() const { return Insts.end(); }
129 reverse_iterator rbegin() { return Insts.rbegin(); }
130 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
131 reverse_iterator rend () { return Insts.rend(); }
132 const_reverse_iterator rend () const { return Insts.rend(); }
134 // Machine-CFG iterators
135 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
136 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
137 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
138 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
139 typedef std::vector<MachineBasicBlock *>::reverse_iterator
140 pred_reverse_iterator;
141 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
142 const_pred_reverse_iterator;
143 typedef std::vector<MachineBasicBlock *>::reverse_iterator
144 succ_reverse_iterator;
145 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
146 const_succ_reverse_iterator;
148 pred_iterator pred_begin() { return Predecessors.begin(); }
149 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
150 pred_iterator pred_end() { return Predecessors.end(); }
151 const_pred_iterator pred_end() const { return Predecessors.end(); }
152 pred_reverse_iterator pred_rbegin()
153 { return Predecessors.rbegin();}
154 const_pred_reverse_iterator pred_rbegin() const
155 { return Predecessors.rbegin();}
156 pred_reverse_iterator pred_rend()
157 { return Predecessors.rend(); }
158 const_pred_reverse_iterator pred_rend() const
159 { return Predecessors.rend(); }
160 unsigned pred_size() const {
161 return (unsigned)Predecessors.size();
163 bool pred_empty() const { return Predecessors.empty(); }
164 succ_iterator succ_begin() { return Successors.begin(); }
165 const_succ_iterator succ_begin() const { return Successors.begin(); }
166 succ_iterator succ_end() { return Successors.end(); }
167 const_succ_iterator succ_end() const { return Successors.end(); }
168 succ_reverse_iterator succ_rbegin()
169 { return Successors.rbegin(); }
170 const_succ_reverse_iterator succ_rbegin() const
171 { return Successors.rbegin(); }
172 succ_reverse_iterator succ_rend()
173 { return Successors.rend(); }
174 const_succ_reverse_iterator succ_rend() const
175 { return Successors.rend(); }
176 unsigned succ_size() const {
177 return (unsigned)Successors.size();
179 bool succ_empty() const { return Successors.empty(); }
181 // LiveIn management methods.
183 /// addLiveIn - Add the specified register as a live in. Note that it
184 /// is an error to add the same register to the same set more than once.
185 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
187 /// removeLiveIn - Remove the specified register from the live in set.
189 void removeLiveIn(unsigned Reg);
191 /// isLiveIn - Return true if the specified register is in the live in set.
193 bool isLiveIn(unsigned Reg) const;
195 // Iteration support for live in sets. These sets are kept in sorted
196 // order by their register number.
197 typedef std::vector<unsigned>::iterator livein_iterator;
198 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
199 livein_iterator livein_begin() { return LiveIns.begin(); }
200 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
201 livein_iterator livein_end() { return LiveIns.end(); }
202 const_livein_iterator livein_end() const { return LiveIns.end(); }
203 bool livein_empty() const { return LiveIns.empty(); }
205 /// getAlignment - Return alignment of the basic block.
207 unsigned getAlignment() const { return Alignment; }
209 /// setAlignment - Set alignment of the basic block.
211 void setAlignment(unsigned Align) { Alignment = Align; }
213 /// isLandingPad - Returns true if the block is a landing pad. That is
214 /// this basic block is entered via an exception handler.
215 bool isLandingPad() const { return IsLandingPad; }
217 /// setIsLandingPad - Indicates the block is a landing pad. That is
218 /// this basic block is entered via an exception handler.
219 void setIsLandingPad() { IsLandingPad = true; }
221 // Code Layout methods.
223 /// moveBefore/moveAfter - move 'this' block before or after the specified
224 /// block. This only moves the block, it does not modify the CFG or adjust
225 /// potential fall-throughs at the end of the block.
226 void moveBefore(MachineBasicBlock *NewAfter);
227 void moveAfter(MachineBasicBlock *NewBefore);
229 /// updateTerminator - Update the terminator instructions in block to account
230 /// for changes to the layout. If the block previously used a fallthrough,
231 /// it may now need a branch, and if it previously used branching it may now
232 /// be able to use a fallthrough.
233 void updateTerminator();
235 // Machine-CFG mutators
237 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
238 /// The Predecessors list of succ is automatically updated.
240 void addSuccessor(MachineBasicBlock *succ);
242 /// removeSuccessor - Remove successor from the successors list of this
243 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
245 void removeSuccessor(MachineBasicBlock *succ);
247 /// removeSuccessor - Remove specified successor from the successors list of
248 /// this MachineBasicBlock. The Predecessors list of succ is automatically
249 /// updated. Return the iterator to the element after the one removed.
251 succ_iterator removeSuccessor(succ_iterator I);
253 /// transferSuccessors - Transfers all the successors from MBB to this
254 /// machine basic block (i.e., copies all the successors fromMBB and
255 /// remove all the successors from fromMBB).
256 void transferSuccessors(MachineBasicBlock *fromMBB);
258 /// isSuccessor - Return true if the specified MBB is a successor of this
260 bool isSuccessor(const MachineBasicBlock *MBB) const;
262 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
263 /// immediately after this block, such that if this block exits by
264 /// falling through, control will transfer to the specified MBB. Note
265 /// that MBB need not be a successor at all, for example if this block
266 /// ends with an unconditional branch to some other block.
267 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
269 /// getFirstTerminator - returns an iterator to the first terminator
270 /// instruction of this basic block. If a terminator does not exist,
272 iterator getFirstTerminator();
274 /// isOnlyReachableViaFallthough - Return true if this basic block has
275 /// exactly one predecessor and the control transfer mechanism between
276 /// the predecessor and this block is a fall-through.
277 bool isOnlyReachableByFallthrough() const;
279 void pop_front() { Insts.pop_front(); }
280 void pop_back() { Insts.pop_back(); }
281 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
282 template<typename IT>
283 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
284 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
286 // erase - Remove the specified element or range from the instruction list.
287 // These functions delete any instructions removed.
289 iterator erase(iterator I) { return Insts.erase(I); }
290 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
291 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
292 void clear() { Insts.clear(); }
294 /// splice - Take an instruction from MBB 'Other' at the position From,
295 /// and insert it into this MBB right before 'where'.
296 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
297 Insts.splice(where, Other->Insts, From);
300 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
301 /// To), and insert them into this MBB right before 'where'.
302 void splice(iterator where, MachineBasicBlock *Other, iterator From,
304 Insts.splice(where, Other->Insts, From, To);
307 /// removeFromParent - This method unlinks 'this' from the containing
308 /// function, and returns it, but does not delete it.
309 MachineBasicBlock *removeFromParent();
311 /// eraseFromParent - This method unlinks 'this' from the containing
312 /// function and deletes it.
313 void eraseFromParent();
315 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
316 /// 'Old', change the code and CFG so that it branches to 'New' instead.
317 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
319 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
320 /// the CFG to be inserted. If we have proven that MBB can only branch to
321 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
322 /// DestB can be null. Besides DestA and DestB, retain other edges leading
323 /// to LandingPads (currently there can be only one; we don't check or require
324 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
325 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
326 MachineBasicBlock *DestB,
329 // Debugging methods.
331 void print(raw_ostream &OS) const;
333 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
334 /// level, unless they're not in a MachineFunction yet, in which case this
337 int getNumber() const { return Number; }
338 void setNumber(int N) { Number = N; }
340 private: // Methods used to maintain doubly linked list of blocks...
341 friend struct ilist_traits<MachineBasicBlock>;
343 // Machine-CFG mutators
345 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
346 /// Don't do this unless you know what you're doing, because it doesn't
347 /// update pred's successors list. Use pred->addSuccessor instead.
349 void addPredecessor(MachineBasicBlock *pred);
351 /// removePredecessor - Remove pred as a predecessor of this
352 /// MachineBasicBlock. Don't do this unless you know what you're
353 /// doing, because it doesn't update pred's successors list. Use
354 /// pred->removeSuccessor instead.
356 void removePredecessor(MachineBasicBlock *pred);
359 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
361 //===--------------------------------------------------------------------===//
362 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
363 //===--------------------------------------------------------------------===//
365 // Provide specializations of GraphTraits to be able to treat a
366 // MachineFunction as a graph of MachineBasicBlocks...
369 template <> struct GraphTraits<MachineBasicBlock *> {
370 typedef MachineBasicBlock NodeType;
371 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
373 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
374 static inline ChildIteratorType child_begin(NodeType *N) {
375 return N->succ_begin();
377 static inline ChildIteratorType child_end(NodeType *N) {
378 return N->succ_end();
382 template <> struct GraphTraits<const MachineBasicBlock *> {
383 typedef const MachineBasicBlock NodeType;
384 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
386 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
387 static inline ChildIteratorType child_begin(NodeType *N) {
388 return N->succ_begin();
390 static inline ChildIteratorType child_end(NodeType *N) {
391 return N->succ_end();
395 // Provide specializations of GraphTraits to be able to treat a
396 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
397 // in inverse order. Inverse order for a function is considered
398 // to be when traversing the predecessor edges of a MBB
399 // instead of the successor edges.
401 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
402 typedef MachineBasicBlock NodeType;
403 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
404 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
407 static inline ChildIteratorType child_begin(NodeType *N) {
408 return N->pred_begin();
410 static inline ChildIteratorType child_end(NodeType *N) {
411 return N->pred_end();
415 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
416 typedef const MachineBasicBlock NodeType;
417 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
418 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
421 static inline ChildIteratorType child_begin(NodeType *N) {
422 return N->pred_begin();
424 static inline ChildIteratorType child_end(NodeType *N) {
425 return N->pred_end();
429 } // End llvm namespace