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. Note that this may be NULL if this instance
96 /// does not correspond directly to an LLVM basic block.
98 const BasicBlock *getBasicBlock() const { return BB; }
100 /// getName - Return the name of the corresponding LLVM basic block, or
102 StringRef getName() const;
104 /// hasAddressTaken - Test whether this block is potentially the target
105 /// of an indirect branch.
106 bool hasAddressTaken() const { return AddressTaken; }
108 /// setHasAddressTaken - Set this block to reflect that it potentially
109 /// is the target of an indirect branch.
110 void setHasAddressTaken() { AddressTaken = true; }
112 /// getParent - Return the MachineFunction containing this basic block.
114 const MachineFunction *getParent() const { return xParent; }
115 MachineFunction *getParent() { return xParent; }
117 typedef Instructions::iterator iterator;
118 typedef Instructions::const_iterator const_iterator;
119 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
120 typedef std::reverse_iterator<iterator> reverse_iterator;
122 unsigned size() const { return (unsigned)Insts.size(); }
123 bool empty() const { return Insts.empty(); }
125 MachineInstr& front() { return Insts.front(); }
126 MachineInstr& back() { return Insts.back(); }
127 const MachineInstr& front() const { return Insts.front(); }
128 const MachineInstr& back() const { return Insts.back(); }
130 iterator begin() { return Insts.begin(); }
131 const_iterator begin() const { return Insts.begin(); }
132 iterator end() { return Insts.end(); }
133 const_iterator end() const { return Insts.end(); }
134 reverse_iterator rbegin() { return Insts.rbegin(); }
135 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
136 reverse_iterator rend () { return Insts.rend(); }
137 const_reverse_iterator rend () const { return Insts.rend(); }
139 // Machine-CFG iterators
140 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
141 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
142 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
143 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
144 typedef std::vector<MachineBasicBlock *>::reverse_iterator
145 pred_reverse_iterator;
146 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
147 const_pred_reverse_iterator;
148 typedef std::vector<MachineBasicBlock *>::reverse_iterator
149 succ_reverse_iterator;
150 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
151 const_succ_reverse_iterator;
153 pred_iterator pred_begin() { return Predecessors.begin(); }
154 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
155 pred_iterator pred_end() { return Predecessors.end(); }
156 const_pred_iterator pred_end() const { return Predecessors.end(); }
157 pred_reverse_iterator pred_rbegin()
158 { return Predecessors.rbegin();}
159 const_pred_reverse_iterator pred_rbegin() const
160 { return Predecessors.rbegin();}
161 pred_reverse_iterator pred_rend()
162 { return Predecessors.rend(); }
163 const_pred_reverse_iterator pred_rend() const
164 { return Predecessors.rend(); }
165 unsigned pred_size() const {
166 return (unsigned)Predecessors.size();
168 bool pred_empty() const { return Predecessors.empty(); }
169 succ_iterator succ_begin() { return Successors.begin(); }
170 const_succ_iterator succ_begin() const { return Successors.begin(); }
171 succ_iterator succ_end() { return Successors.end(); }
172 const_succ_iterator succ_end() const { return Successors.end(); }
173 succ_reverse_iterator succ_rbegin()
174 { return Successors.rbegin(); }
175 const_succ_reverse_iterator succ_rbegin() const
176 { return Successors.rbegin(); }
177 succ_reverse_iterator succ_rend()
178 { return Successors.rend(); }
179 const_succ_reverse_iterator succ_rend() const
180 { return Successors.rend(); }
181 unsigned succ_size() const {
182 return (unsigned)Successors.size();
184 bool succ_empty() const { return Successors.empty(); }
186 // LiveIn management methods.
188 /// addLiveIn - Add the specified register as a live in. Note that it
189 /// is an error to add the same register to the same set more than once.
190 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
192 /// removeLiveIn - Remove the specified register from the live in set.
194 void removeLiveIn(unsigned Reg);
196 /// isLiveIn - Return true if the specified register is in the live in set.
198 bool isLiveIn(unsigned Reg) const;
200 // Iteration support for live in sets. These sets are kept in sorted
201 // order by their register number.
202 typedef std::vector<unsigned>::iterator livein_iterator;
203 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
204 livein_iterator livein_begin() { return LiveIns.begin(); }
205 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
206 livein_iterator livein_end() { return LiveIns.end(); }
207 const_livein_iterator livein_end() const { return LiveIns.end(); }
208 bool livein_empty() const { return LiveIns.empty(); }
210 /// getAlignment - Return alignment of the basic block.
212 unsigned getAlignment() const { return Alignment; }
214 /// setAlignment - Set alignment of the basic block.
216 void setAlignment(unsigned Align) { Alignment = Align; }
218 /// isLandingPad - Returns true if the block is a landing pad. That is
219 /// this basic block is entered via an exception handler.
220 bool isLandingPad() const { return IsLandingPad; }
222 /// setIsLandingPad - Indicates the block is a landing pad. That is
223 /// this basic block is entered via an exception handler.
224 void setIsLandingPad() { IsLandingPad = true; }
226 // Code Layout methods.
228 /// moveBefore/moveAfter - move 'this' block before or after the specified
229 /// block. This only moves the block, it does not modify the CFG or adjust
230 /// potential fall-throughs at the end of the block.
231 void moveBefore(MachineBasicBlock *NewAfter);
232 void moveAfter(MachineBasicBlock *NewBefore);
234 /// updateTerminator - Update the terminator instructions in block to account
235 /// for changes to the layout. If the block previously used a fallthrough,
236 /// it may now need a branch, and if it previously used branching it may now
237 /// be able to use a fallthrough.
238 void updateTerminator();
240 // Machine-CFG mutators
242 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
243 /// The Predecessors list of succ is automatically updated.
245 void addSuccessor(MachineBasicBlock *succ);
247 /// removeSuccessor - Remove successor from the successors list of this
248 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
250 void removeSuccessor(MachineBasicBlock *succ);
252 /// removeSuccessor - Remove specified successor from the successors list of
253 /// this MachineBasicBlock. The Predecessors list of succ is automatically
254 /// updated. Return the iterator to the element after the one removed.
256 succ_iterator removeSuccessor(succ_iterator I);
258 /// transferSuccessors - Transfers all the successors from MBB to this
259 /// machine basic block (i.e., copies all the successors fromMBB and
260 /// remove all the successors from fromMBB).
261 void transferSuccessors(MachineBasicBlock *fromMBB);
263 /// isSuccessor - Return true if the specified MBB is a successor of this
265 bool isSuccessor(const MachineBasicBlock *MBB) const;
267 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
268 /// immediately after this block, such that if this block exits by
269 /// falling through, control will transfer to the specified MBB. Note
270 /// that MBB need not be a successor at all, for example if this block
271 /// ends with an unconditional branch to some other block.
272 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
274 /// canFallThrough - Return true if the block can implicitly transfer
275 /// control to the block after it by falling off the end of it. This should
276 /// return false if it can reach the block after it, but it uses an explicit
277 /// branch to do so (e.g., a table jump). True is a conservative answer.
278 bool canFallThrough();
280 /// getFirstTerminator - returns an iterator to the first terminator
281 /// instruction of this basic block. If a terminator does not exist,
283 iterator getFirstTerminator();
285 /// isOnlyReachableViaFallthough - Return true if this basic block has
286 /// exactly one predecessor and the control transfer mechanism between
287 /// the predecessor and this block is a fall-through.
288 bool isOnlyReachableByFallthrough() const;
290 void pop_front() { Insts.pop_front(); }
291 void pop_back() { Insts.pop_back(); }
292 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
293 template<typename IT>
294 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
295 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
297 // erase - Remove the specified element or range from the instruction list.
298 // These functions delete any instructions removed.
300 iterator erase(iterator I) { return Insts.erase(I); }
301 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
302 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
303 void clear() { Insts.clear(); }
305 /// splice - Take an instruction from MBB 'Other' at the position From,
306 /// and insert it into this MBB right before 'where'.
307 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
308 Insts.splice(where, Other->Insts, From);
311 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
312 /// To), and insert them into this MBB right before 'where'.
313 void splice(iterator where, MachineBasicBlock *Other, iterator From,
315 Insts.splice(where, Other->Insts, From, To);
318 /// removeFromParent - This method unlinks 'this' from the containing
319 /// function, and returns it, but does not delete it.
320 MachineBasicBlock *removeFromParent();
322 /// eraseFromParent - This method unlinks 'this' from the containing
323 /// function and deletes it.
324 void eraseFromParent();
326 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
327 /// 'Old', change the code and CFG so that it branches to 'New' instead.
328 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
330 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
331 /// the CFG to be inserted. If we have proven that MBB can only branch to
332 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
333 /// DestB can be null. Besides DestA and DestB, retain other edges leading
334 /// to LandingPads (currently there can be only one; we don't check or require
335 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
336 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
337 MachineBasicBlock *DestB,
340 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
341 /// any DEBUG_VALUE instructions. Return UnknownLoc if there is none.
342 DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
344 // Debugging methods.
346 void print(raw_ostream &OS) const;
348 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
349 /// level, unless they're not in a MachineFunction yet, in which case this
352 int getNumber() const { return Number; }
353 void setNumber(int N) { Number = N; }
355 private: // Methods used to maintain doubly linked list of blocks...
356 friend struct ilist_traits<MachineBasicBlock>;
358 // Machine-CFG mutators
360 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
361 /// Don't do this unless you know what you're doing, because it doesn't
362 /// update pred's successors list. Use pred->addSuccessor instead.
364 void addPredecessor(MachineBasicBlock *pred);
366 /// removePredecessor - Remove pred as a predecessor of this
367 /// MachineBasicBlock. Don't do this unless you know what you're
368 /// doing, because it doesn't update pred's successors list. Use
369 /// pred->removeSuccessor instead.
371 void removePredecessor(MachineBasicBlock *pred);
374 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
376 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
378 //===--------------------------------------------------------------------===//
379 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
380 //===--------------------------------------------------------------------===//
382 // Provide specializations of GraphTraits to be able to treat a
383 // MachineFunction as a graph of MachineBasicBlocks...
386 template <> struct GraphTraits<MachineBasicBlock *> {
387 typedef MachineBasicBlock NodeType;
388 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
390 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
391 static inline ChildIteratorType child_begin(NodeType *N) {
392 return N->succ_begin();
394 static inline ChildIteratorType child_end(NodeType *N) {
395 return N->succ_end();
399 template <> struct GraphTraits<const MachineBasicBlock *> {
400 typedef const MachineBasicBlock NodeType;
401 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
403 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
404 static inline ChildIteratorType child_begin(NodeType *N) {
405 return N->succ_begin();
407 static inline ChildIteratorType child_end(NodeType *N) {
408 return N->succ_end();
412 // Provide specializations of GraphTraits to be able to treat a
413 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
414 // in inverse order. Inverse order for a function is considered
415 // to be when traversing the predecessor edges of a MBB
416 // instead of the successor edges.
418 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
419 typedef MachineBasicBlock NodeType;
420 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
421 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
424 static inline ChildIteratorType child_begin(NodeType *N) {
425 return N->pred_begin();
427 static inline ChildIteratorType child_end(NodeType *N) {
428 return N->pred_end();
432 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
433 typedef const MachineBasicBlock NodeType;
434 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
435 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
438 static inline ChildIteratorType child_begin(NodeType *N) {
439 return N->pred_begin();
441 static inline ChildIteratorType child_end(NodeType *N) {
442 return N->pred_end();
446 } // End llvm namespace