- // splitBasicBlock - This splits a basic block into two at the specified
- // instruction. Note that all instructions BEFORE the specified iterator stay
- // as part of the original basic block, an unconditional branch is added to
- // the new BB, and the rest of the instructions in the BB are moved to the new
- // BB, including the old terminator. The newly formed BasicBlock is returned.
- // This function invalidates the specified iterator.
- //
- // Note that this only works on well formed basic blocks (must have a
- // terminator), and 'I' must not be the end of instruction list (which would
- // cause a degenerate basic block to be formed, having a terminator inside of
- // the basic block).
- //
- BasicBlock *splitBasicBlock(iterator I);
-
-
- //===--------------------------------------------------------------------===//
- // Predecessor and Successor Iterators
- //
- template <class _Ptr, class _USE_iterator> // Predecessor Iterator
- class PredIterator : public std::bidirectional_iterator<_Ptr, ptrdiff_t> {
- _Ptr *BB;
- _USE_iterator It;
- public:
- typedef PredIterator<_Ptr,_USE_iterator> _Self;
-
- inline void advancePastConstants() {
- // TODO: This is bad
- // Loop to ignore constant pool references
- while (It != BB->use_end() && !isa<TerminatorInst>(*It))
- ++It;
- }
-
- inline PredIterator(_Ptr *bb) : BB(bb), It(bb->use_begin()) {
- advancePastConstants();
- }
- inline PredIterator(_Ptr *bb, bool) : BB(bb), It(bb->use_end()) {}
-
- inline bool operator==(const _Self& x) const { return It == x.It; }
- inline bool operator!=(const _Self& x) const { return !operator==(x); }
-
- inline pointer operator*() const {
- return cast<Instruction>(*It)->getParent();
- }
- inline pointer *operator->() const { return &(operator*()); }
-
- inline _Self& operator++() { // Preincrement
- ++It; advancePastConstants();
- return *this;
- }
-
- inline _Self operator++(int) { // Postincrement
- _Self tmp = *this; ++*this; return tmp;
- }
-
- inline _Self& operator--() { --It; return *this; } // Predecrement
- inline _Self operator--(int) { // Postdecrement
- _Self tmp = *this; --*this; return tmp;
- }
- };
-
- inline pred_iterator pred_begin() { return pred_iterator(this); }
- inline pred_const_iterator pred_begin() const {
- return pred_const_iterator(this);
- }
- inline pred_iterator pred_end() { return pred_iterator(this, true); }
- inline pred_const_iterator pred_end() const {
- return pred_const_iterator(this, true);
- }
-
- template <class _Term, class _BB> // Successor Iterator
- class SuccIterator : public std::bidirectional_iterator<_BB, ptrdiff_t> {
- const _Term Term;
- unsigned idx;
- public:
- typedef SuccIterator<_Term, _BB> _Self;
- // TODO: This can be random access iterator, need operator+ and stuff tho
-
- inline SuccIterator(_Term T) : Term(T), idx(0) { // begin iterator
- assert(T && "getTerminator returned null!");
- }
- inline SuccIterator(_Term T, bool) // end iterator
- : Term(T), idx(Term->getNumSuccessors()) {
- assert(T && "getTerminator returned null!");
- }
-
- inline bool operator==(const _Self& x) const { return idx == x.idx; }
- inline bool operator!=(const _Self& x) const { return !operator==(x); }
-
- inline pointer operator*() const { return Term->getSuccessor(idx); }
- inline pointer operator->() const { return operator*(); }
-
- inline _Self& operator++() { ++idx; return *this; } // Preincrement
- inline _Self operator++(int) { // Postincrement
- _Self tmp = *this; ++*this; return tmp;
- }
-
- inline _Self& operator--() { --idx; return *this; } // Predecrement
- inline _Self operator--(int) { // Postdecrement
- _Self tmp = *this; --*this; return tmp;
- }
- };
-
- inline succ_iterator succ_begin() { return succ_iterator(getTerminator()); }
- inline succ_const_iterator succ_begin() const {
- return succ_const_iterator(getTerminator());
- }
- inline succ_iterator succ_end() {return succ_iterator(getTerminator(), true);}
- inline succ_const_iterator succ_end() const {
- return succ_const_iterator(getTerminator(), true);
- }
-};
-
-
-//===--------------------------------------------------------------------===//
-// GraphTraits specializations for basic block graphs (CFGs)
-//===--------------------------------------------------------------------===//
-
-// Provide specializations of GraphTraits to be able to treat a method as a
-// graph of basic blocks...
-
-template <> struct GraphTraits<BasicBlock*> {
- typedef BasicBlock NodeType;
- typedef BasicBlock::succ_iterator ChildIteratorType;
-
- static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
- static inline ChildIteratorType child_begin(NodeType *N) {
- return N->succ_begin();
- }
- static inline ChildIteratorType child_end(NodeType *N) {
- return N->succ_end();
- }
-};
-
-template <> struct GraphTraits<const BasicBlock*> {
- typedef const BasicBlock NodeType;
- typedef BasicBlock::succ_const_iterator ChildIteratorType;
-
- static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
-
- static inline ChildIteratorType child_begin(NodeType *N) {
- return N->succ_begin();
- }
- static inline ChildIteratorType child_end(NodeType *N) {
- return N->succ_end();
- }
-};
-
-// Provide specializations of GraphTraits to be able to treat a method as a
-// graph of basic blocks... and to walk it in inverse order. Inverse order for
-// a method is considered to be when traversing the predecessor edges of a BB
-// instead of the successor edges.
-//
-template <> struct GraphTraits<Inverse<BasicBlock*> > {
- typedef BasicBlock NodeType;
- typedef BasicBlock::pred_iterator ChildIteratorType;
- static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
- static inline ChildIteratorType child_begin(NodeType *N) {
- return N->pred_begin();
- }
- static inline ChildIteratorType child_end(NodeType *N) {
- return N->pred_end();
- }
-};
-
-template <> struct GraphTraits<Inverse<const BasicBlock*> > {
- typedef const BasicBlock NodeType;
- typedef BasicBlock::pred_const_iterator ChildIteratorType;
- static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
- return G.Graph;
- }
- static inline ChildIteratorType child_begin(NodeType *N) {
- return N->pred_begin();
- }
- static inline ChildIteratorType child_end(NodeType *N) {
- return N->pred_end();
- }