1 //===-- llvm/BasicBlock.h - Represent a basic block in the VM ----*- C++ -*--=//
3 // This file contains the declaration of the BasicBlock class, which represents
4 // a single basic block in the VM.
6 // Note that basic blocks themselves are Def's, because they are referenced
7 // by instructions like branches and can go in switch tables and stuff...
9 // This may see wierd at first, but it's really pretty cool. :)
11 //===----------------------------------------------------------------------===//
13 // Note that well formed basic blocks are formed of a list of instructions
14 // followed by a single TerminatorInst instruction. TerminatorInst's may not
15 // occur in the middle of basic blocks, and must terminate the blocks.
17 // This code allows malformed basic blocks to occur, because it may be useful
18 // in the intermediate stage of analysis or modification of a program.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_BASICBLOCK_H
23 #define LLVM_BASICBLOCK_H
25 #include "llvm/Value.h" // Get the definition of Value
26 #include "llvm/ValueHolder.h"
27 #include "llvm/CFGdecls.h"
34 typedef UseTy<BasicBlock> BasicBlockUse;
36 class BasicBlock : public Value { // Basic blocks are data objects also
38 typedef ValueHolder<Instruction, BasicBlock> InstListType;
40 InstListType InstList;
42 friend class ValueHolder<BasicBlock,Method>;
43 void setParent(Method *parent);
46 // Instruction iterators...
47 typedef InstListType::iterator iterator;
48 typedef InstListType::const_iterator const_iterator;
49 typedef reverse_iterator<const_iterator> const_reverse_iterator;
50 typedef reverse_iterator<iterator> reverse_iterator;
52 typedef cfg::succ_iterator succ_iterator; // Include CFG.h to use these
53 typedef cfg::pred_iterator pred_iterator;
54 typedef cfg::succ_const_iterator succ_const_iterator;
55 typedef cfg::pred_const_iterator pred_const_iterator;
57 BasicBlock(const string &Name = "", Method *Parent = 0);
60 // Specialize setName to take care of symbol table majik
61 virtual void setName(const string &name);
63 const Method *getParent() const { return (const Method*)InstList.getParent();}
64 Method *getParent() { return (Method*)InstList.getParent(); }
66 // getTerminator() - If this is a well formed basic block, then this returns
67 // a pointer to the terminator instruction. If it is not, then you get a null
70 TerminatorInst *getTerminator();
71 const TerminatorInst *const getTerminator() const;
73 //===--------------------------------------------------------------------===//
74 // Instruction iterator methods
75 inline iterator begin() { return InstList.begin(); }
76 inline const_iterator begin() const { return InstList.begin(); }
77 inline iterator end () { return InstList.end(); }
78 inline const_iterator end () const { return InstList.end(); }
80 inline reverse_iterator rbegin() { return InstList.rbegin(); }
81 inline const_reverse_iterator rbegin() const { return InstList.rbegin(); }
82 inline reverse_iterator rend () { return InstList.rend(); }
83 inline const_reverse_iterator rend () const { return InstList.rend(); }
85 inline unsigned size() const { return InstList.size(); }
86 inline bool empty() const { return InstList.empty(); }
87 inline const Instruction *front() const { return InstList.front(); }
88 inline Instruction *front() { return InstList.front(); }
89 inline const Instruction *back() const { return InstList.back(); }
90 inline Instruction *back() { return InstList.back(); }
92 // getInstList() - Return the underlying instruction list container. You need
93 // to access it directly if you want to modify it currently.
95 const InstListType &getInstList() const { return InstList; }
96 InstListType &getInstList() { return InstList; }
98 // hasConstantPoolReferences() - This predicate is true if there is a
99 // reference to this basic block in the constant pool for this method. For
100 // example, if a block is reached through a switch table, that table resides
101 // in the constant pool, and the basic block is reference from it.
103 bool hasConstantPoolReferences() const;
105 // dropAllReferences() - This function causes all the subinstructions to "let
106 // go" of all references that they are maintaining. This allows one to
107 // 'delete' a whole class at a time, even though there may be circular
108 // references... first all references are dropped, and all use counts go to
109 // zero. Then everything is delete'd for real. Note that no operations are
110 // valid on an object that has "dropped all references", except operator
113 void dropAllReferences();
115 // removePredecessor - This method is used to notify a BasicBlock that the
116 // specified Predecessor of the block is no longer able to reach it. This is
117 // actually not used to update the Predecessor list, but is actually used to
118 // update the PHI nodes that reside in the block. Note that this should be
119 // called while the predecessor still refers to this block.
121 void removePredecessor(BasicBlock *Pred);
123 // splitBasicBlock - This splits a basic block into two at the specified
124 // instruction. Note that all instructions BEFORE the specified iterator stay
125 // as part of the original basic block, an unconditional branch is added to
126 // the new BB, and the rest of the instructions in the BB are moved to the new
127 // BB, including the old terminator. The newly formed BasicBlock is returned.
128 // This function invalidates the specified iterator.
130 // Note that this only works on well formed basic blocks (must have a
131 // terminator), and 'I' must not be the end of instruction list (which would
132 // cause a degenerate basic block to be formed, having a terminator inside of
135 BasicBlock *splitBasicBlock(iterator I);