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"
24 class MachineFunction;
31 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
33 mutable ilist_half_node<MachineInstr> Sentinel;
35 // this is only set by the MachineBasicBlock owning the LiveList
36 friend class MachineBasicBlock;
37 MachineBasicBlock* Parent;
40 MachineInstr *createSentinel() const {
41 return static_cast<MachineInstr*>(&Sentinel);
43 void destroySentinel(MachineInstr *) const {}
45 MachineInstr *provideInitialHead() const { return createSentinel(); }
46 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
47 static void noteHead(MachineInstr*, MachineInstr*) {}
49 void addNodeToList(MachineInstr* N);
50 void removeNodeFromList(MachineInstr* N);
51 void transferNodesFromList(ilist_traits &SrcTraits,
52 ilist_iterator<MachineInstr> first,
53 ilist_iterator<MachineInstr> last);
54 void deleteNode(MachineInstr *N);
56 void createNode(const MachineInstr &);
59 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
60 typedef ilist<MachineInstr> Instructions;
64 MachineFunction *xParent;
66 /// Predecessors/Successors - Keep track of the predecessor / successor
68 std::vector<MachineBasicBlock *> Predecessors;
69 std::vector<MachineBasicBlock *> Successors;
71 /// LiveIns - Keep track of the physical registers that are livein of
73 std::vector<unsigned> LiveIns;
75 /// Alignment - Alignment of the basic block. Zero if the basic block does
76 /// not need to be aligned.
79 /// IsLandingPad - Indicate that this basic block is entered via an
80 /// exception handler.
83 /// AddressTaken - Indicate that this basic block is potentially the
84 /// target of an indirect branch.
87 // Intrusive list support
88 MachineBasicBlock() {}
90 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
94 // MachineBasicBlocks are allocated and owned by MachineFunction.
95 friend class MachineFunction;
98 /// getBasicBlock - Return the LLVM basic block that this instance
99 /// corresponded to originally. Note that this may be NULL if this instance
100 /// does not correspond directly to an LLVM basic block.
102 const BasicBlock *getBasicBlock() const { return BB; }
104 /// getName - Return the name of the corresponding LLVM basic block, or
106 StringRef getName() const;
108 /// hasAddressTaken - Test whether this block is potentially the target
109 /// of an indirect branch.
110 bool hasAddressTaken() const { return AddressTaken; }
112 /// setHasAddressTaken - Set this block to reflect that it potentially
113 /// is the target of an indirect branch.
114 void setHasAddressTaken() { AddressTaken = true; }
116 /// getParent - Return the MachineFunction containing this basic block.
118 const MachineFunction *getParent() const { return xParent; }
119 MachineFunction *getParent() { return xParent; }
121 typedef Instructions::iterator iterator;
122 typedef Instructions::const_iterator const_iterator;
123 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
124 typedef std::reverse_iterator<iterator> reverse_iterator;
126 unsigned size() const { return (unsigned)Insts.size(); }
127 bool empty() const { return Insts.empty(); }
129 MachineInstr& front() { return Insts.front(); }
130 MachineInstr& back() { return Insts.back(); }
131 const MachineInstr& front() const { return Insts.front(); }
132 const MachineInstr& back() const { return Insts.back(); }
134 iterator begin() { return Insts.begin(); }
135 const_iterator begin() const { return Insts.begin(); }
136 iterator end() { return Insts.end(); }
137 const_iterator end() const { return Insts.end(); }
138 reverse_iterator rbegin() { return Insts.rbegin(); }
139 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
140 reverse_iterator rend () { return Insts.rend(); }
141 const_reverse_iterator rend () const { return Insts.rend(); }
143 // Machine-CFG iterators
144 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
145 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
146 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
147 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
148 typedef std::vector<MachineBasicBlock *>::reverse_iterator
149 pred_reverse_iterator;
150 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
151 const_pred_reverse_iterator;
152 typedef std::vector<MachineBasicBlock *>::reverse_iterator
153 succ_reverse_iterator;
154 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
155 const_succ_reverse_iterator;
157 pred_iterator pred_begin() { return Predecessors.begin(); }
158 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
159 pred_iterator pred_end() { return Predecessors.end(); }
160 const_pred_iterator pred_end() const { return Predecessors.end(); }
161 pred_reverse_iterator pred_rbegin()
162 { return Predecessors.rbegin();}
163 const_pred_reverse_iterator pred_rbegin() const
164 { return Predecessors.rbegin();}
165 pred_reverse_iterator pred_rend()
166 { return Predecessors.rend(); }
167 const_pred_reverse_iterator pred_rend() const
168 { return Predecessors.rend(); }
169 unsigned pred_size() const {
170 return (unsigned)Predecessors.size();
172 bool pred_empty() const { return Predecessors.empty(); }
173 succ_iterator succ_begin() { return Successors.begin(); }
174 const_succ_iterator succ_begin() const { return Successors.begin(); }
175 succ_iterator succ_end() { return Successors.end(); }
176 const_succ_iterator succ_end() const { return Successors.end(); }
177 succ_reverse_iterator succ_rbegin()
178 { return Successors.rbegin(); }
179 const_succ_reverse_iterator succ_rbegin() const
180 { return Successors.rbegin(); }
181 succ_reverse_iterator succ_rend()
182 { return Successors.rend(); }
183 const_succ_reverse_iterator succ_rend() const
184 { return Successors.rend(); }
185 unsigned succ_size() const {
186 return (unsigned)Successors.size();
188 bool succ_empty() const { return Successors.empty(); }
190 // LiveIn management methods.
192 /// addLiveIn - Add the specified register as a live in. Note that it
193 /// is an error to add the same register to the same set more than once.
194 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
196 /// removeLiveIn - Remove the specified register from the live in set.
198 void removeLiveIn(unsigned Reg);
200 /// isLiveIn - Return true if the specified register is in the live in set.
202 bool isLiveIn(unsigned Reg) const;
204 // Iteration support for live in sets. These sets are kept in sorted
205 // order by their register number.
206 typedef std::vector<unsigned>::const_iterator livein_iterator;
207 livein_iterator livein_begin() const { return LiveIns.begin(); }
208 livein_iterator livein_end() const { return LiveIns.end(); }
209 bool livein_empty() const { return LiveIns.empty(); }
211 /// getAlignment - Return alignment of the basic block.
213 unsigned getAlignment() const { return Alignment; }
215 /// setAlignment - Set alignment of the basic block.
217 void setAlignment(unsigned Align) { Alignment = Align; }
219 /// isLandingPad - Returns true if the block is a landing pad. That is
220 /// this basic block is entered via an exception handler.
221 bool isLandingPad() const { return IsLandingPad; }
223 /// setIsLandingPad - Indicates the block is a landing pad. That is
224 /// this basic block is entered via an exception handler.
225 void setIsLandingPad() { IsLandingPad = true; }
227 // Code Layout methods.
229 /// moveBefore/moveAfter - move 'this' block before or after the specified
230 /// block. This only moves the block, it does not modify the CFG or adjust
231 /// potential fall-throughs at the end of the block.
232 void moveBefore(MachineBasicBlock *NewAfter);
233 void moveAfter(MachineBasicBlock *NewBefore);
235 /// updateTerminator - Update the terminator instructions in block to account
236 /// for changes to the layout. If the block previously used a fallthrough,
237 /// it may now need a branch, and if it previously used branching it may now
238 /// be able to use a fallthrough.
239 void updateTerminator();
241 // Machine-CFG mutators
243 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
244 /// The Predecessors list of succ is automatically updated.
246 void addSuccessor(MachineBasicBlock *succ);
248 /// removeSuccessor - Remove successor from the successors list of this
249 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
251 void removeSuccessor(MachineBasicBlock *succ);
253 /// removeSuccessor - Remove specified successor from the successors list of
254 /// this MachineBasicBlock. The Predecessors list of succ is automatically
255 /// updated. Return the iterator to the element after the one removed.
257 succ_iterator removeSuccessor(succ_iterator I);
259 /// transferSuccessors - Transfers all the successors from MBB to this
260 /// machine basic block (i.e., copies all the successors fromMBB and
261 /// remove all the successors from fromMBB).
262 void transferSuccessors(MachineBasicBlock *fromMBB);
264 /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
265 /// in transferSuccessors, and update PHI operands in the successor blocks
266 /// which refer to fromMBB to refer to this.
267 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
269 /// isSuccessor - Return true if the specified MBB is a successor of this
271 bool isSuccessor(const MachineBasicBlock *MBB) const;
273 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
274 /// immediately after this block, such that if this block exits by
275 /// falling through, control will transfer to the specified MBB. Note
276 /// that MBB need not be a successor at all, for example if this block
277 /// ends with an unconditional branch to some other block.
278 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
280 /// canFallThrough - Return true if the block can implicitly transfer
281 /// control to the block after it by falling off the end of it. This should
282 /// return false if it can reach the block after it, but it uses an explicit
283 /// branch to do so (e.g., a table jump). True is a conservative answer.
284 bool canFallThrough();
286 /// Returns a pointer to the first instructon in this block that is not a
287 /// PHINode instruction. When adding instruction to the beginning of the
288 /// basic block, they should be added before the returned value, not before
289 /// the first instruction, which might be PHI.
290 /// Returns end() is there's no non-PHI instruction.
291 iterator getFirstNonPHI();
293 /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
294 /// not a PHI or a label. This is the correct point to insert copies at the
295 /// beginning of a basic block.
296 iterator SkipPHIsAndLabels(iterator I);
298 /// getFirstTerminator - returns an iterator to the first terminator
299 /// instruction of this basic block. If a terminator does not exist,
301 iterator getFirstTerminator();
303 /// SplitCriticalEdge - Split the critical edge from this block to the
304 /// given successor block, and return the newly created block, or null
305 /// if splitting is not possible.
307 /// This function updates LiveVariables, MachineDominatorTree, and
308 /// MachineLoopInfo, as applicable.
309 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
311 void pop_front() { Insts.pop_front(); }
312 void pop_back() { Insts.pop_back(); }
313 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
314 template<typename IT>
315 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
316 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
317 iterator insertAfter(iterator I, MachineInstr *M) {
318 return Insts.insertAfter(I, M);
321 // erase - Remove the specified element or range from the instruction list.
322 // These functions delete any instructions removed.
324 iterator erase(iterator I) { return Insts.erase(I); }
325 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
326 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
327 void clear() { Insts.clear(); }
329 /// splice - Take an instruction from MBB 'Other' at the position From,
330 /// and insert it into this MBB right before 'where'.
331 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
332 Insts.splice(where, Other->Insts, From);
335 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
336 /// To), and insert them into this MBB right before 'where'.
337 void splice(iterator where, MachineBasicBlock *Other, iterator From,
339 Insts.splice(where, Other->Insts, From, To);
342 /// removeFromParent - This method unlinks 'this' from the containing
343 /// function, and returns it, but does not delete it.
344 MachineBasicBlock *removeFromParent();
346 /// eraseFromParent - This method unlinks 'this' from the containing
347 /// function and deletes it.
348 void eraseFromParent();
350 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
351 /// 'Old', change the code and CFG so that it branches to 'New' instead.
352 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
354 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
355 /// the CFG to be inserted. If we have proven that MBB can only branch to
356 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
357 /// DestB can be null. Besides DestA and DestB, retain other edges leading
358 /// to LandingPads (currently there can be only one; we don't check or require
359 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
360 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
361 MachineBasicBlock *DestB,
364 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
365 /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
366 DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
368 // Debugging methods.
370 void print(raw_ostream &OS, SlotIndexes* = 0) const;
372 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
373 /// level, unless they're not in a MachineFunction yet, in which case this
376 int getNumber() const { return Number; }
377 void setNumber(int N) { Number = N; }
379 /// getSymbol - Return the MCSymbol for this basic block.
381 MCSymbol *getSymbol() const;
383 private: // Methods used to maintain doubly linked list of blocks...
384 friend struct ilist_traits<MachineBasicBlock>;
386 // Machine-CFG mutators
388 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
389 /// Don't do this unless you know what you're doing, because it doesn't
390 /// update pred's successors list. Use pred->addSuccessor instead.
392 void addPredecessor(MachineBasicBlock *pred);
394 /// removePredecessor - Remove pred as a predecessor of this
395 /// MachineBasicBlock. Don't do this unless you know what you're
396 /// doing, because it doesn't update pred's successors list. Use
397 /// pred->removeSuccessor instead.
399 void removePredecessor(MachineBasicBlock *pred);
402 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
404 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
406 //===--------------------------------------------------------------------===//
407 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
408 //===--------------------------------------------------------------------===//
410 // Provide specializations of GraphTraits to be able to treat a
411 // MachineFunction as a graph of MachineBasicBlocks...
414 template <> struct GraphTraits<MachineBasicBlock *> {
415 typedef MachineBasicBlock NodeType;
416 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
418 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
419 static inline ChildIteratorType child_begin(NodeType *N) {
420 return N->succ_begin();
422 static inline ChildIteratorType child_end(NodeType *N) {
423 return N->succ_end();
427 template <> struct GraphTraits<const MachineBasicBlock *> {
428 typedef const MachineBasicBlock NodeType;
429 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
431 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
432 static inline ChildIteratorType child_begin(NodeType *N) {
433 return N->succ_begin();
435 static inline ChildIteratorType child_end(NodeType *N) {
436 return N->succ_end();
440 // Provide specializations of GraphTraits to be able to treat a
441 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
442 // in inverse order. Inverse order for a function is considered
443 // to be when traversing the predecessor edges of a MBB
444 // instead of the successor edges.
446 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
447 typedef MachineBasicBlock NodeType;
448 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
449 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
452 static inline ChildIteratorType child_begin(NodeType *N) {
453 return N->pred_begin();
455 static inline ChildIteratorType child_end(NodeType *N) {
456 return N->pred_end();
460 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
461 typedef const MachineBasicBlock NodeType;
462 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
463 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
466 static inline ChildIteratorType child_begin(NodeType *N) {
467 return N->pred_begin();
469 static inline ChildIteratorType child_end(NodeType *N) {
470 return N->pred_end();
474 } // End llvm namespace