From 76456bc40c79fcae4da52d34f96c079d9759257c Mon Sep 17 00:00:00 2001 From: Brian Gaeke Date: Wed, 28 Apr 2004 02:16:33 +0000 Subject: [PATCH] Add machine-code CFG support: MachineBasicBlocks may now have their own predecessors and successors git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13208 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/MachineBasicBlock.h | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/include/llvm/CodeGen/MachineBasicBlock.h b/include/llvm/CodeGen/MachineBasicBlock.h index 25148d2df22..01bbc11519d 100644 --- a/include/llvm/CodeGen/MachineBasicBlock.h +++ b/include/llvm/CodeGen/MachineBasicBlock.h @@ -60,6 +60,9 @@ public: Instructions Insts; MachineBasicBlock *Prev, *Next; const BasicBlock *BB; + std::vector Predecessors; + std::vector Successors; + public: MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) { Insts.parent = this; @@ -95,6 +98,70 @@ public: reverse_iterator rend () { return Insts.rend(); } const_reverse_iterator rend () const { return Insts.rend(); } + // Machine-CFG iterators + typedef std::vector::iterator pred_iterator; + typedef std::vector::const_iterator const_pred_iterator; + typedef std::vector::iterator succ_iterator; + typedef std::vector::const_iterator const_succ_iterator; + + pred_iterator pred_begin() { return Predecessors.begin (); } + const_pred_iterator pred_begin() const { return Predecessors.begin (); } + pred_iterator pred_end() { return Predecessors.end (); } + const_pred_iterator pred_end() const { return Predecessors.end (); } + succ_iterator succ_begin() { return Successors.begin (); } + const_succ_iterator succ_begin() const { return Successors.begin (); } + succ_iterator succ_end() { return Successors.end (); } + const_succ_iterator succ_end() const { return Successors.end (); } + + // Machine-CFG mutators + + /// addSuccessor - Add succ as a successor of this MachineBasicBlock. + /// The Predecessors list of succ is automatically updated. + /// + void addSuccessor (MachineBasicBlock *succ) { + Successors.push_back (succ); + assert (std::find (Successors.begin (), Successors.end (), succ) + == Successors.end () + && "Trying to addSuccessor a MBB which is already my successor"); + succ->addPredecessor (this); + } + + /// removeSuccessor - Remove succ from the successors list of this + /// MachineBasicBlock. The Predecessors list of succ is automatically updated. + /// + void removeSuccessor (MachineBasicBlock *succ) { + succ->removePredecessor (this); + std::vector::iterator goner = + std::find (Successors.begin(), Successors.end (), succ); + assert (goner != Successors.end () + && "Trying to removeSuccessor a MBB which isn't my successor"); + Successors.erase (goner); + } + + /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock. + /// Don't do this unless you know what you're doing, because it doesn't + /// update pred's successors list. Use pred->addSuccessor instead. + /// + void addPredecessor (MachineBasicBlock *pred) { + Predecessors.push_back (pred); + assert(std::find (Predecessors.begin (), Predecessors.end (), pred) + == Predecessors.end () + && "Trying to addPredecessor a MBB which is already my predecessor"); + } + + /// removePredecessor - Remove pred as a predecessor of this + /// MachineBasicBlock. Don't do this unless you know what you're + /// doing, because it doesn't update pred's successors list. Use + /// pred->removeSuccessor instead. + /// + void removePredecessor (MachineBasicBlock *pred) { + std::vector::iterator goner = + std::find (Predecessors.begin(), Predecessors.end (), pred); + assert (goner != Predecessors.end () + && "Trying to removePredecessor a MBB which isn't my predecessor"); + Predecessors.erase (goner); + } + /// getFirstTerminator - returns an iterator to the first terminator /// instruction of this basic block. If a terminator does not exist, /// it returns end()