MachineBasicBlock.h revision e81561909d128c6e2d8033cb5465a49b2596b26a
1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===// 24e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 34e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// The LLVM Compiler Infrastructure 44e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// This file was developed by the LLVM research group and is distributed under 64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// the University of Illinois Open Source License. See LICENSE.TXT for details. 74e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 84e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//===----------------------------------------------------------------------===// 94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Collect the sequence of machine instructions for a basic block. 114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//===----------------------------------------------------------------------===// 134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define LLVM_CODEGEN_MACHINEBASICBLOCK_H 164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "llvm/CodeGen/MachineInstr.h" 184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "llvm/ADT/GraphTraits.h" 194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "llvm/ADT/ilist" 204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "llvm/Support/Streams.h" 214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace llvm { 234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) class MachineFunction; 244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// ilist_traits 264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)template <> 274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)struct ilist_traits<MachineInstr> { 284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)protected: 294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // this is only set by the MachineBasicBlock owning the ilist 304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) friend class MachineBasicBlock; 314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) MachineBasicBlock* parent; 324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)public: 344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ilist_traits<MachineInstr>() : parent(0) { } 35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static MachineInstr* getPrev(MachineInstr* N) { return N->prev; } 374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static MachineInstr* getNext(MachineInstr* N) { return N->next; } 384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static const MachineInstr* 404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) getPrev(const MachineInstr* N) { return N->prev; } 414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch static const MachineInstr* 434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) getNext(const MachineInstr* N) { return N->next; } 444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; } 46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; } 474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static MachineInstr* createSentinel(); 494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static void destroySentinel(MachineInstr *MI) { delete MI; } 504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void addNodeToList(MachineInstr* N); 514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void removeNodeFromList(MachineInstr* N); 52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch void transferNodesFromList( 534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ilist_iterator<MachineInstr> first, 55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch ilist_iterator<MachineInstr> last); 564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass BasicBlock; 594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class MachineBasicBlock { 61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochpublic: 62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch typedef ilist<MachineInstr> Instructions; 63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch Instructions Insts; 64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch MachineBasicBlock *Prev, *Next; 65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const BasicBlock *BB; 66e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch std::vector<MachineBasicBlock *> Predecessors; 67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch std::vector<MachineBasicBlock *> Successors; 68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch int Number; 69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch MachineFunction *Parent; 70e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 71e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochpublic: 72e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb), 73e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch Number(-1), Parent(0) { 74e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch Insts.parent = this; 75e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch } 76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 77e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch ~MachineBasicBlock(); 78e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 79e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// getBasicBlock - Return the LLVM basic block that this instance 80e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// corresponded to originally. 81e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// 82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const BasicBlock *getBasicBlock() const { return BB; } 83e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 84e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// getParent - Return the MachineFunction containing this basic block. 85e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// 86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const MachineFunction *getParent() const { return Parent; } 87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch MachineFunction *getParent() { return Parent; } 88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef ilist<MachineInstr>::iterator iterator; 904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef ilist<MachineInstr>::const_iterator const_iterator; 914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch typedef std::reverse_iterator<iterator> reverse_iterator; 93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) unsigned size() const { return Insts.size(); } 954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) bool empty() const { return Insts.empty(); } 96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch MachineInstr& front() { return Insts.front(); } 98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch MachineInstr& back() { return Insts.back(); } 99e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 100e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch iterator begin() { return Insts.begin(); } 101e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const_iterator begin() const { return Insts.begin(); } 102e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch iterator end() { return Insts.end(); } 103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const_iterator end() const { return Insts.end(); } 104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch reverse_iterator rbegin() { return Insts.rbegin(); } 105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const_reverse_iterator rbegin() const { return Insts.rbegin(); } 106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch reverse_iterator rend () { return Insts.rend(); } 107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const_reverse_iterator rend () const { return Insts.rend(); } 108e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 1094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Machine-CFG iterators 1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef std::vector<MachineBasicBlock *>::iterator pred_iterator; 1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator; 1124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef std::vector<MachineBasicBlock *>::iterator succ_iterator; 113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator; 114e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pred_iterator pred_begin() { return Predecessors.begin(); } 1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const_pred_iterator pred_begin() const { return Predecessors.begin(); } 117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) pred_iterator pred_end() { return Predecessors.end(); } 118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const_pred_iterator pred_end() const { return Predecessors.end(); } 119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) unsigned pred_size() const { return Predecessors.size(); } 120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bool pred_empty() const { return Predecessors.empty(); } 1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) succ_iterator succ_begin() { return Successors.begin(); } 1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const_succ_iterator succ_begin() const { return Successors.begin(); } 1234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) succ_iterator succ_end() { return Successors.end(); } 1244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const_succ_iterator succ_end() const { return Successors.end(); } 1254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) unsigned succ_size() const { return Successors.size(); } 1264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) bool succ_empty() const { return Successors.empty(); } 1274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Code Layout methods. 1294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// moveBefore/moveAfter - move 'this' block before or after the specified 1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// block. This only moves the block, it does not modify the CFG or adjust 1324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// potential fall-throughs at the end of the block. 1334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void moveBefore(MachineBasicBlock *NewAfter); 134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void moveAfter(MachineBasicBlock *NewBefore); 1354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Machine-CFG mutators 1374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// addSuccessor - Add succ as a successor of this MachineBasicBlock. 1394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// The Predecessors list of succ is automatically updated. 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// 141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void addSuccessor(MachineBasicBlock *succ); 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// removeSuccessor - Remove successor from the successors list of this 1444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// MachineBasicBlock. The Predecessors list of succ is automatically updated. 145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// 146e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch void removeSuccessor(MachineBasicBlock *succ); 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// removeSuccessor - Remove specified successor from the successors list of 149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// this MachineBasicBlock. The Predecessors list of succ is automatically 1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// updated. 1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// 152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void removeSuccessor(succ_iterator I); 153e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 1544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// isSuccessor - Return true if the specified MBB is a successor of this 1554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// block. 1564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) bool isSuccessor(MachineBasicBlock *MBB) const { 1574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 1584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if (*I == MBB) 1594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return true; 1604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return false; 1614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// getFirstTerminator - returns an iterator to the first terminator 164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// instruction of this basic block. If a terminator does not exist, 165e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// it returns end() 166a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) iterator getFirstTerminator(); 167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void pop_front() { Insts.pop_front(); } 169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void pop_back() { Insts.pop_back(); } 170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void push_back(MachineInstr *MI) { Insts.push_back(MI); } 171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) template<typename IT> 172a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); } 173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); } 174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // erase - Remove the specified element or range from the instruction list. 176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // These functions delete any instructions removed. 177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // 178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) iterator erase(iterator I) { return Insts.erase(I); } 179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) iterator erase(iterator I, iterator E) { return Insts.erase(I, E); } 180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); } 181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void clear() { Insts.clear(); } 182e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 183e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// splice - Take a block of instructions from MBB 'Other' in the range [From, 184e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch /// To), and insert them into this MBB right before 'where'. 185e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch void splice(iterator where, MachineBasicBlock *Other, iterator From, 186e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch iterator To) { 1874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) Insts.splice(where, Other->Insts, From, To); 188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Debugging methods. 1914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void dump() const; 1924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void print(OStream &OS) const { 193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (OS.stream()) print(*OS.stream()); 1944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 1954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void print(std::ostream &OS) const; 196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 1974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// getNumber - MachineBasicBlocks are uniquely numbered at the function 198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// level, unless they're not in a MachineFunction yet, in which case this 199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// will return -1. 2004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// 2014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) int getNumber() const { return Number; } 2024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void setNumber(int N) { Number = N; } 203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)private: // Methods used to maintain doubly linked list of blocks... 205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) friend struct ilist_traits<MachineBasicBlock>; 206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MachineBasicBlock *getPrev() const { return Prev; } 208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MachineBasicBlock *getNext() const { return Next; } 209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void setPrev(MachineBasicBlock *P) { Prev = P; } 210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void setNext(MachineBasicBlock *N) { Next = N; } 211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Machine-CFG mutators 213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. 215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// Don't do this unless you know what you're doing, because it doesn't 216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// update pred's successors list. Use pred->addSuccessor instead. 217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// 218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void addPredecessor(MachineBasicBlock *pred); 219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// removePredecessor - Remove pred as a predecessor of this 221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// MachineBasicBlock. Don't do this unless you know what you're 222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// doing, because it doesn't update pred's successors list. Use 223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// pred->removeSuccessor instead. 224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// 225a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void removePredecessor(MachineBasicBlock *pred); 226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 228a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB); 229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)inline OStream& operator<<(OStream &OS, const MachineBasicBlock &MBB){ 230a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (OS.stream()) *OS.stream() << MBB; 231a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return OS; 232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===--------------------------------------------------------------------===// 235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// GraphTraits specializations for machine basic block graphs (machine-CFGs) 236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//===--------------------------------------------------------------------===// 237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Provide specializations of GraphTraits to be able to treat a 239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// MachineFunction as a graph of MachineBasicBlocks... 240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// 241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <> struct GraphTraits<MachineBasicBlock *> { 243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef MachineBasicBlock NodeType; 244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) typedef MachineBasicBlock::succ_iterator ChildIteratorType; 245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; } 247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static inline ChildIteratorType child_begin(NodeType *N) { 248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return N->succ_begin(); 249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 2504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_end(NodeType *N) { 2514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->succ_end(); 2524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 2544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)template <> struct GraphTraits<const MachineBasicBlock *> { 2564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef const MachineBasicBlock NodeType; 2574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef MachineBasicBlock::const_succ_iterator ChildIteratorType; 2584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; } 2604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_begin(NodeType *N) { 2614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->succ_begin(); 2624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_end(NodeType *N) { 2644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->succ_end(); 2654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 2674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Provide specializations of GraphTraits to be able to treat a 2694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// MachineFunction as a graph of MachineBasicBlocks... and to walk it 2704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// in inverse order. Inverse order for a function is considered 2714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// to be when traversing the predecessor edges of a MBB 2724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// instead of the successor edges. 2734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// 2744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)template <> struct GraphTraits<Inverse<MachineBasicBlock*> > { 2754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef MachineBasicBlock NodeType; 2764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef MachineBasicBlock::pred_iterator ChildIteratorType; 2774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) { 2784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return G.Graph; 2794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_begin(NodeType *N) { 2814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->pred_begin(); 2824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_end(NodeType *N) { 2844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->pred_end(); 2854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 2874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 2884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > { 2894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef const MachineBasicBlock NodeType; 2904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) typedef MachineBasicBlock::const_pred_iterator ChildIteratorType; 2914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) { 2924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return G.Graph; 2934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_begin(NodeType *N) { 2954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->pred_begin(); 2964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 2974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static inline ChildIteratorType child_end(NodeType *N) { 2984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return N->pred_end(); 2994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 3004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 3014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 3024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} // End llvm namespace 3034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 3044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif 3054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)