2002-10-28 01:21:55 +00:00
|
|
|
//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
|
2002-07-08 22:40:34 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2002-10-27 20:49:47 +00:00
|
|
|
// Collect the sequence of machine instructions for a basic block.
|
|
|
|
//
|
2002-10-28 01:21:55 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-08 22:40:34 +00:00
|
|
|
|
2002-10-28 00:28:31 +00:00
|
|
|
#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
|
|
|
|
#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
|
2002-07-08 22:40:34 +00:00
|
|
|
|
2004-02-12 02:27:10 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2004-05-01 21:05:34 +00:00
|
|
|
#include "Support/GraphTraits.h"
|
2004-02-12 19:12:03 +00:00
|
|
|
#include "Support/ilist"
|
2004-02-13 04:40:15 +00:00
|
|
|
#include <iosfwd>
|
2003-11-11 22:41:34 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2004-02-19 16:13:54 +00:00
|
|
|
class MachineFunction;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-02-12 19:12:03 +00:00
|
|
|
// ilist_traits
|
|
|
|
template <>
|
2004-02-19 16:13:54 +00:00
|
|
|
class ilist_traits<MachineInstr> {
|
2004-02-12 19:12:03 +00:00
|
|
|
// this is only set by the MachineBasicBlock owning the ilist
|
|
|
|
friend class MachineBasicBlock;
|
|
|
|
MachineBasicBlock* parent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ilist_traits<MachineInstr>() : parent(0) { }
|
|
|
|
|
|
|
|
static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
|
|
|
|
static MachineInstr* getNext(MachineInstr* N) { return N->next; }
|
|
|
|
|
|
|
|
static const MachineInstr*
|
|
|
|
getPrev(const MachineInstr* N) { return N->prev; }
|
|
|
|
|
|
|
|
static const MachineInstr*
|
|
|
|
getNext(const MachineInstr* N) { return N->next; }
|
|
|
|
|
|
|
|
static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
|
|
|
|
static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
|
|
|
|
|
2004-02-16 07:17:43 +00:00
|
|
|
static MachineInstr* createNode();
|
|
|
|
void addNodeToList(MachineInstr* N);
|
|
|
|
void removeNodeFromList(MachineInstr* N);
|
|
|
|
void transferNodesFromList(
|
|
|
|
iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
|
|
|
|
ilist_iterator<MachineInstr> first,
|
|
|
|
ilist_iterator<MachineInstr> last);
|
2004-02-12 19:12:03 +00:00
|
|
|
};
|
|
|
|
|
2002-10-28 01:53:00 +00:00
|
|
|
class BasicBlock;
|
2002-07-08 22:40:34 +00:00
|
|
|
|
2002-10-28 05:30:46 +00:00
|
|
|
class MachineBasicBlock {
|
2004-02-12 02:27:10 +00:00
|
|
|
public:
|
|
|
|
typedef ilist<MachineInstr> Instructions;
|
|
|
|
Instructions Insts;
|
2002-10-28 02:08:43 +00:00
|
|
|
MachineBasicBlock *Prev, *Next;
|
2003-07-26 23:30:37 +00:00
|
|
|
const BasicBlock *BB;
|
2004-04-28 02:16:33 +00:00
|
|
|
std::vector<MachineBasicBlock *> Predecessors;
|
|
|
|
std::vector<MachineBasicBlock *> Successors;
|
2004-05-12 21:35:20 +00:00
|
|
|
int Number;
|
2004-05-24 06:11:51 +00:00
|
|
|
MachineFunction *Parent;
|
2004-04-28 02:16:33 +00:00
|
|
|
|
2002-07-08 22:40:34 +00:00
|
|
|
public:
|
2004-05-12 21:35:20 +00:00
|
|
|
MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb),
|
2004-05-24 06:11:51 +00:00
|
|
|
Number(-1), Parent(0) {
|
2004-02-12 18:49:07 +00:00
|
|
|
Insts.parent = this;
|
|
|
|
}
|
2004-05-24 07:14:35 +00:00
|
|
|
|
|
|
|
~MachineBasicBlock();
|
2002-07-08 22:40:34 +00:00
|
|
|
|
2002-10-28 05:30:46 +00:00
|
|
|
/// getBasicBlock - Return the LLVM basic block that this instance
|
|
|
|
/// corresponded to originally.
|
|
|
|
///
|
2003-07-26 23:30:37 +00:00
|
|
|
const BasicBlock *getBasicBlock() const { return BB; }
|
2004-02-19 16:13:54 +00:00
|
|
|
|
|
|
|
/// getParent - Return the MachineFunction containing this basic block.
|
|
|
|
///
|
2004-05-24 06:11:51 +00:00
|
|
|
const MachineFunction *getParent() const { return Parent; }
|
|
|
|
MachineFunction *getParent() { return Parent; }
|
2004-02-19 16:13:54 +00:00
|
|
|
|
2004-02-12 02:27:10 +00:00
|
|
|
typedef ilist<MachineInstr>::iterator iterator;
|
|
|
|
typedef ilist<MachineInstr>::const_iterator const_iterator;
|
2002-07-08 22:40:34 +00:00
|
|
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
|
|
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
|
|
|
|
|
|
|
unsigned size() const { return Insts.size(); }
|
|
|
|
bool empty() const { return Insts.empty(); }
|
|
|
|
|
2004-02-12 02:27:10 +00:00
|
|
|
MachineInstr& front() { return Insts.front(); }
|
|
|
|
MachineInstr& back() { return Insts.back(); }
|
2002-07-08 22:40:34 +00:00
|
|
|
|
|
|
|
iterator begin() { return Insts.begin(); }
|
|
|
|
const_iterator begin() const { return Insts.begin(); }
|
|
|
|
iterator end() { return Insts.end(); }
|
|
|
|
const_iterator end() const { return Insts.end(); }
|
|
|
|
reverse_iterator rbegin() { return Insts.rbegin(); }
|
|
|
|
const_reverse_iterator rbegin() const { return Insts.rbegin(); }
|
|
|
|
reverse_iterator rend () { return Insts.rend(); }
|
|
|
|
const_reverse_iterator rend () const { return Insts.rend(); }
|
|
|
|
|
2004-04-28 02:16:33 +00:00
|
|
|
// Machine-CFG iterators
|
|
|
|
typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
|
|
|
|
typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
|
|
|
|
typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
|
|
|
|
typedef std::vector<MachineBasicBlock *>::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 (); }
|
2004-04-28 04:46:35 +00:00
|
|
|
unsigned pred_size() const { return Predecessors.size (); }
|
2004-04-28 02:16:33 +00:00
|
|
|
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 (); }
|
2004-04-28 04:46:35 +00:00
|
|
|
unsigned succ_size() const { return Successors.size (); }
|
2004-04-28 02:16:33 +00:00
|
|
|
|
|
|
|
// Machine-CFG mutators
|
|
|
|
|
|
|
|
/// addSuccessor - Add succ as a successor of this MachineBasicBlock.
|
|
|
|
/// The Predecessors list of succ is automatically updated.
|
|
|
|
///
|
|
|
|
void addSuccessor (MachineBasicBlock *succ) {
|
2004-04-28 03:59:48 +00:00
|
|
|
Successors.push_back (succ);
|
2004-04-28 02:16:33 +00:00
|
|
|
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<MachineBasicBlock *>::iterator goner =
|
|
|
|
std::find (Successors.begin(), Successors.end (), succ);
|
|
|
|
Successors.erase (goner);
|
|
|
|
}
|
|
|
|
|
2004-02-23 18:14:48 +00:00
|
|
|
/// getFirstTerminator - returns an iterator to the first terminator
|
|
|
|
/// instruction of this basic block. If a terminator does not exist,
|
|
|
|
/// it returns end()
|
|
|
|
iterator getFirstTerminator();
|
|
|
|
|
2004-06-08 18:52:47 +00:00
|
|
|
void pop_front() { Insts.pop_front(); }
|
2002-07-08 22:40:34 +00:00
|
|
|
void push_back(MachineInstr *MI) { Insts.push_back(MI); }
|
|
|
|
template<typename IT>
|
|
|
|
void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
|
|
|
|
iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
|
|
|
|
|
2002-09-20 00:55:57 +00:00
|
|
|
// erase - Remove the specified element or range from the instruction list.
|
2004-02-12 02:27:10 +00:00
|
|
|
// These functions delete any instructions removed.
|
2002-07-08 22:40:34 +00:00
|
|
|
//
|
2002-09-20 00:55:57 +00:00
|
|
|
iterator erase(iterator I) { return Insts.erase(I); }
|
2002-07-08 22:40:34 +00:00
|
|
|
iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
|
2004-03-31 21:58:50 +00:00
|
|
|
MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
|
2004-03-31 22:43:12 +00:00
|
|
|
void clear() { Insts.clear(); }
|
2002-10-28 02:08:43 +00:00
|
|
|
|
2004-02-13 04:40:15 +00:00
|
|
|
// Debugging methods.
|
|
|
|
void dump() const;
|
|
|
|
void print(std::ostream &OS) const;
|
|
|
|
|
2004-05-12 21:57:23 +00:00
|
|
|
/// getNumber - MachineBasicBlocks are uniquely numbered at the function
|
|
|
|
/// level, unless they're not in a MachineFunction yet, in which case this
|
|
|
|
/// will return -1.
|
|
|
|
///
|
2004-05-12 21:35:20 +00:00
|
|
|
int getNumber() const { return Number; }
|
|
|
|
|
2002-10-28 02:08:43 +00:00
|
|
|
private: // Methods used to maintain doubly linked list of blocks...
|
|
|
|
friend class ilist_traits<MachineBasicBlock>;
|
|
|
|
|
|
|
|
MachineBasicBlock *getPrev() const { return Prev; }
|
|
|
|
MachineBasicBlock *getNext() const { return Next; }
|
|
|
|
void setPrev(MachineBasicBlock *P) { Prev = P; }
|
|
|
|
void setNext(MachineBasicBlock *N) { Next = N; }
|
2004-04-28 04:15:06 +00:00
|
|
|
|
|
|
|
// Machine-CFG mutators
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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<MachineBasicBlock *>::iterator goner =
|
|
|
|
std::find (Predecessors.begin(), Predecessors.end (), pred);
|
|
|
|
Predecessors.erase (goner);
|
|
|
|
}
|
2002-07-08 22:40:34 +00:00
|
|
|
};
|
|
|
|
|
2004-05-01 21:05:34 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// GraphTraits specializations for machine basic block graphs (machine-CFGs)
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Provide specializations of GraphTraits to be able to treat a
|
|
|
|
// MachineFunction as a graph of MachineBasicBlocks...
|
|
|
|
//
|
|
|
|
|
|
|
|
template <> struct GraphTraits<MachineBasicBlock *> {
|
|
|
|
typedef MachineBasicBlock NodeType;
|
|
|
|
typedef MachineBasicBlock::succ_iterator ChildIteratorType;
|
|
|
|
|
|
|
|
static NodeType *getEntryNode(MachineBasicBlock *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 MachineBasicBlock *> {
|
|
|
|
typedef const MachineBasicBlock NodeType;
|
|
|
|
typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
|
|
|
|
|
|
|
|
static NodeType *getEntryNode(const MachineBasicBlock *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
|
|
|
|
// MachineFunction as a graph of MachineBasicBlocks... and to walk it
|
|
|
|
// in inverse order. Inverse order for a function is considered
|
|
|
|
// to be when traversing the predecessor edges of a MBB
|
|
|
|
// instead of the successor edges.
|
|
|
|
//
|
|
|
|
template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
|
|
|
|
typedef MachineBasicBlock NodeType;
|
|
|
|
typedef MachineBasicBlock::pred_iterator ChildIteratorType;
|
|
|
|
static NodeType *getEntryNode(Inverse<MachineBasicBlock *> 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 MachineBasicBlock*> > {
|
|
|
|
typedef const MachineBasicBlock NodeType;
|
|
|
|
typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
|
|
|
|
static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> 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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
2002-07-08 22:40:34 +00:00
|
|
|
|
|
|
|
#endif
|