2002-10-28 01:21:55 +00:00
|
|
|
//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
|
2005-04-21 20:39:54 +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.
|
2005-04-21 20:39:54 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 20:39:54 +00:00
|
|
|
//
|
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-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
|
|
|
#include "llvm/ADT/ilist"
|
2006-11-28 22:21:29 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
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-10-27 16:14:51 +00:00
|
|
|
struct ilist_traits<MachineInstr> {
|
|
|
|
protected:
|
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; }
|
|
|
|
|
2005-01-30 00:13:34 +00:00
|
|
|
static MachineInstr* createSentinel();
|
|
|
|
static void destroySentinel(MachineInstr *MI) { delete MI; }
|
2004-02-16 07:17:43 +00:00
|
|
|
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-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
|
|
|
|
2007-02-10 02:38:19 +00:00
|
|
|
/// Predecessors/Successors - Keep track of the predecessor / successor
|
|
|
|
/// basicblocks.
|
|
|
|
std::vector<MachineBasicBlock *> Predecessors;
|
|
|
|
std::vector<MachineBasicBlock *> Successors;
|
|
|
|
|
|
|
|
/// LiveIns - Keep track of the physical registers that are livein of
|
|
|
|
/// the basicblock.
|
|
|
|
std::vector<unsigned> LiveIns;
|
2007-02-21 22:39:52 +00:00
|
|
|
|
|
|
|
/// IsLandingPad - Indicate that this basic block is entered via an
|
|
|
|
/// exception handler.
|
|
|
|
bool IsLandingPad;
|
2007-02-10 02:38:19 +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),
|
2007-02-21 22:39:52 +00:00
|
|
|
Number(-1), Parent(0),
|
|
|
|
IsLandingPad(false) {
|
2004-02-12 18:49:07 +00:00
|
|
|
Insts.parent = this;
|
|
|
|
}
|
2004-05-24 07:14:35 +00:00
|
|
|
|
|
|
|
~MachineBasicBlock();
|
2005-04-21 20:39:54 +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;
|
2005-04-21 20:39:54 +00:00
|
|
|
|
2006-10-21 06:50:05 +00:00
|
|
|
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(); }
|
|
|
|
unsigned pred_size() const { return Predecessors.size(); }
|
|
|
|
bool pred_empty() const { return Predecessors.empty(); }
|
|
|
|
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(); }
|
|
|
|
unsigned succ_size() const { return Successors.size(); }
|
|
|
|
bool succ_empty() const { return Successors.empty(); }
|
2004-04-28 02:16:33 +00:00
|
|
|
|
2007-02-10 02:38:19 +00:00
|
|
|
// LiveIn management methods.
|
|
|
|
|
|
|
|
/// addLiveIn - Add the specified register as a live in. Note that it
|
|
|
|
/// is an error to add the same register to the same set more than once.
|
|
|
|
void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
|
|
|
|
|
2007-02-19 21:49:54 +00:00
|
|
|
/// removeLiveIn - Remove the specified register from the live in set.
|
|
|
|
///
|
|
|
|
void removeLiveIn(unsigned Reg);
|
|
|
|
|
2007-02-10 02:38:19 +00:00
|
|
|
// Iteration support for live in sets. These sets are kept in sorted
|
|
|
|
// order by their register number.
|
2007-02-19 21:49:54 +00:00
|
|
|
typedef std::vector<unsigned>::iterator livein_iterator;
|
|
|
|
typedef std::vector<unsigned>::const_iterator const_livein_iterator;
|
|
|
|
livein_iterator livein_begin() { return LiveIns.begin(); }
|
|
|
|
const_livein_iterator livein_begin() const { return LiveIns.begin(); }
|
|
|
|
livein_iterator livein_end() { return LiveIns.end(); }
|
|
|
|
const_livein_iterator livein_end() const { return LiveIns.end(); }
|
2007-02-10 02:38:19 +00:00
|
|
|
bool livein_empty() const { return LiveIns.empty(); }
|
|
|
|
|
2007-02-21 22:39:52 +00:00
|
|
|
/// isLandingPad - Returns true if the block is a landing pad. That is
|
|
|
|
/// this basic block is entered via an exception handler.
|
|
|
|
bool isLandingPad() const { return IsLandingPad; }
|
|
|
|
|
|
|
|
/// setIsLandingPad - Indicates the block is a landing pad. That is
|
|
|
|
/// this basic block is entered via an exception handler.
|
|
|
|
void setIsLandingPad() { IsLandingPad = true; }
|
|
|
|
|
2006-10-23 23:35:35 +00:00
|
|
|
// Code Layout methods.
|
|
|
|
|
|
|
|
/// moveBefore/moveAfter - move 'this' block before or after the specified
|
|
|
|
/// block. This only moves the block, it does not modify the CFG or adjust
|
|
|
|
/// potential fall-throughs at the end of the block.
|
|
|
|
void moveBefore(MachineBasicBlock *NewAfter);
|
|
|
|
void moveAfter(MachineBasicBlock *NewBefore);
|
|
|
|
|
2004-04-28 02:16:33 +00:00
|
|
|
// Machine-CFG mutators
|
2006-10-23 23:35:35 +00:00
|
|
|
|
2004-04-28 02:16:33 +00:00
|
|
|
/// addSuccessor - Add succ as a successor of this MachineBasicBlock.
|
|
|
|
/// The Predecessors list of succ is automatically updated.
|
|
|
|
///
|
2004-10-26 15:41:13 +00:00
|
|
|
void addSuccessor(MachineBasicBlock *succ);
|
2004-04-28 02:16:33 +00:00
|
|
|
|
2004-07-31 01:59:11 +00:00
|
|
|
/// removeSuccessor - Remove successor from the successors list of this
|
2004-04-28 02:16:33 +00:00
|
|
|
/// MachineBasicBlock. The Predecessors list of succ is automatically updated.
|
|
|
|
///
|
2004-10-26 15:41:13 +00:00
|
|
|
void removeSuccessor(MachineBasicBlock *succ);
|
2004-07-31 01:59:11 +00:00
|
|
|
|
|
|
|
/// removeSuccessor - Remove specified successor from the successors list of
|
|
|
|
/// this MachineBasicBlock. The Predecessors list of succ is automatically
|
|
|
|
/// updated.
|
|
|
|
///
|
2004-10-26 15:41:13 +00:00
|
|
|
void removeSuccessor(succ_iterator I);
|
2006-10-21 06:50:05 +00:00
|
|
|
|
|
|
|
/// isSuccessor - Return true if the specified MBB is a successor of this
|
|
|
|
/// block.
|
|
|
|
bool isSuccessor(MachineBasicBlock *MBB) const {
|
|
|
|
for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
|
|
|
|
if (*I == MBB)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2004-04-28 02:16:33 +00:00
|
|
|
|
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(); }
|
2004-07-31 09:59:04 +00:00
|
|
|
void pop_back() { Insts.pop_back(); }
|
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(); }
|
2005-04-21 20:39:54 +00:00
|
|
|
|
2004-07-31 09:59:04 +00:00
|
|
|
/// splice - Take a block of instructions from MBB 'Other' in the range [From,
|
|
|
|
/// To), and insert them into this MBB right before 'where'.
|
|
|
|
void splice(iterator where, MachineBasicBlock *Other, iterator From,
|
|
|
|
iterator To) {
|
|
|
|
Insts.splice(where, Other->Insts, From, To);
|
|
|
|
}
|
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;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS) const { if (OS) print(*OS); }
|
2004-02-13 04:40:15 +00:00
|
|
|
|
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; }
|
2006-10-03 20:16:45 +00:00
|
|
|
void setNumber(int N) { Number = N; }
|
2004-05-12 21:35:20 +00:00
|
|
|
|
2002-10-28 02:08:43 +00:00
|
|
|
private: // Methods used to maintain doubly linked list of blocks...
|
2004-10-27 16:14:51 +00:00
|
|
|
friend struct ilist_traits<MachineBasicBlock>;
|
2002-10-28 02:08:43 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
///
|
2004-10-26 15:41:13 +00:00
|
|
|
void addPredecessor(MachineBasicBlock *pred);
|
2004-04-28 04:15:06 +00:00
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
2004-10-26 15:41:13 +00:00
|
|
|
void removePredecessor(MachineBasicBlock *pred);
|
2002-07-08 22:40:34 +00:00
|
|
|
};
|
|
|
|
|
2006-11-18 21:47:36 +00:00
|
|
|
std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
|
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; }
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
return N->succ_begin();
|
|
|
|
}
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
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; }
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
return N->succ_begin();
|
|
|
|
}
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
return N->pred_begin();
|
|
|
|
}
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
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) {
|
2005-04-21 20:39:54 +00:00
|
|
|
return G.Graph;
|
2004-05-01 21:05:34 +00:00
|
|
|
}
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
return N->pred_begin();
|
|
|
|
}
|
2005-04-21 20:39:54 +00:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
2004-05-01 21:05:34 +00:00
|
|
|
return N->pred_end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
2002-07-08 22:40:34 +00:00
|
|
|
|
|
|
|
#endif
|