2003-10-13 03:32:08 +00:00
|
|
|
//===-- SchedGraph.h - Scheduling Graph -------------------------*- C++ -*-===//
|
2003-10-21 15:17:13 +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-08-09 20:08:03 +00:00
|
|
|
//
|
2003-10-13 03:32:08 +00:00
|
|
|
// This is a scheduling graph based on SSA graph plus extra dependence edges
|
|
|
|
// capturing dependences due to machine resources (machine registers, CC
|
|
|
|
// registers, and any others).
|
2002-08-09 20:08:03 +00:00
|
|
|
//
|
2003-10-13 03:32:08 +00:00
|
|
|
// This graph tries to leverage the SSA graph as much as possible, but captures
|
|
|
|
// the extra dependences through a common interface.
|
2002-08-09 20:08:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-08-28 23:06:02 +00:00
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_SCHEDGRAPH_H
|
|
|
|
#define LLVM_CODEGEN_SCHEDGRAPH_H
|
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
#include "llvm/CodeGen/SchedGraphCommon.h"
|
2001-11-08 05:20:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2003-08-25 22:42:20 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2003-08-27 02:42:58 +00:00
|
|
|
#include "Support/hash_map"
|
|
|
|
#include "Support/GraphTraits.h"
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-09-30 23:37:26 +00:00
|
|
|
class RegToRefVecMap;
|
2001-11-05 04:04:23 +00:00
|
|
|
class ValueToDefVecMap;
|
|
|
|
class RefVec;
|
2001-10-28 21:45:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
class SchedGraphNode : public SchedGraphNodeCommon {
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
MachineBasicBlock *MBB;
|
|
|
|
const MachineInstr *MI;
|
2001-08-28 23:06:02 +00:00
|
|
|
|
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
SchedGraphNode(unsigned nodeId, MachineBasicBlock *mbb, int indexInBB,
|
|
|
|
const TargetMachine& Target);
|
|
|
|
~SchedGraphNode();
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
friend class SchedGraph; // give access for ctor and dtor
|
|
|
|
friend class SchedGraphEdge; // give access for adding edges
|
2003-06-02 22:45:07 +00:00
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
public:
|
2003-08-27 02:42:58 +00:00
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
// Accessor methods
|
2003-08-27 02:42:58 +00:00
|
|
|
const MachineInstr* getMachineInstr() const { return MI; }
|
|
|
|
const MachineOpCode getOpCode() const { return MI->getOpCode(); }
|
|
|
|
bool isDummyNode() const { return (MI == NULL); }
|
|
|
|
MachineBasicBlock &getMachineBasicBlock() const { return *MBB; }
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void print(std::ostream &os) const;
|
2003-08-25 22:42:20 +00:00
|
|
|
};
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
class SchedGraph : public SchedGraphCommon {
|
|
|
|
MachineBasicBlock &MBB;
|
|
|
|
hash_map<const MachineInstr*, SchedGraphNode*> GraphMap;
|
2001-08-28 23:06:02 +00:00
|
|
|
|
|
|
|
public:
|
2003-08-25 22:42:20 +00:00
|
|
|
typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator iterator;
|
|
|
|
typedef hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator const_iterator;
|
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
MachineBasicBlock& getBasicBlock() const{return MBB;}
|
|
|
|
const unsigned int getNumNodes() const { return GraphMap.size()+2; }
|
2003-08-25 22:42:20 +00:00
|
|
|
SchedGraphNode* getGraphNodeForInstr(const MachineInstr* MI) const {
|
|
|
|
const_iterator onePair = find(MI);
|
|
|
|
return (onePair != end())? onePair->second : NULL;
|
2001-08-28 23:06:02 +00:00
|
|
|
}
|
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
// Debugging support
|
2003-08-27 02:42:58 +00:00
|
|
|
void dump() const;
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
protected:
|
|
|
|
SchedGraph(MachineBasicBlock& mbb, const TargetMachine& TM);
|
|
|
|
~SchedGraph();
|
2002-01-20 22:54:45 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
// Unordered iterators.
|
2001-08-28 23:06:02 +00:00
|
|
|
// Return values is pair<const MachineIntr*,SchedGraphNode*>.
|
|
|
|
//
|
2003-08-25 22:42:20 +00:00
|
|
|
hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator begin() const {
|
|
|
|
return GraphMap.begin();
|
|
|
|
}
|
|
|
|
hash_map<const MachineInstr*, SchedGraphNode*>::const_iterator end() const {
|
|
|
|
return GraphMap.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned size() { return GraphMap.size(); }
|
|
|
|
iterator find(const MachineInstr *MI) const { return GraphMap.find(MI); }
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
SchedGraphNode *&operator[](const MachineInstr *MI) {
|
|
|
|
return GraphMap[MI];
|
|
|
|
}
|
2001-08-28 23:06:02 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class SchedGraphSet; // give access to ctor
|
2003-08-27 02:42:58 +00:00
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
inline void noteGraphNodeForInstr (const MachineInstr* minstr,
|
2003-08-27 02:42:58 +00:00
|
|
|
SchedGraphNode* node) {
|
2001-08-28 23:06:02 +00:00
|
|
|
assert((*this)[minstr] == NULL);
|
|
|
|
(*this)[minstr] = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Graph builder
|
|
|
|
//
|
2003-08-27 02:42:58 +00:00
|
|
|
void buildGraph(const TargetMachine& target);
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void buildNodesForBB(const TargetMachine& target,MachineBasicBlock &MBB,
|
|
|
|
std::vector<SchedGraphNode*>& memNV,
|
|
|
|
std::vector<SchedGraphNode*>& callNV,
|
|
|
|
RegToRefVecMap& regToRefVecMap,
|
|
|
|
ValueToDefVecMap& valueToDefVecMap);
|
2003-08-25 22:42:20 +00:00
|
|
|
|
2001-11-05 04:04:23 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void findDefUseInfoAtInstr(const TargetMachine& target, SchedGraphNode* node,
|
|
|
|
std::vector<SchedGraphNode*>& memNV,
|
|
|
|
std::vector<SchedGraphNode*>& callNV,
|
|
|
|
RegToRefVecMap& regToRefVecMap,
|
|
|
|
ValueToDefVecMap& valueToDefVecMap);
|
2001-11-05 04:04:23 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void addEdgesForInstruction(const MachineInstr& minstr,
|
|
|
|
const ValueToDefVecMap& valueToDefVecMap,
|
|
|
|
const TargetMachine& target);
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void addCDEdges(const TerminatorInst* term, const TargetMachine& target);
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void addMemEdges(const std::vector<SchedGraphNode*>& memNod,
|
|
|
|
const TargetMachine& target);
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void addCallCCEdges(const std::vector<SchedGraphNode*>& memNod,
|
|
|
|
MachineBasicBlock& bbMvec,
|
|
|
|
const TargetMachine& target);
|
|
|
|
|
|
|
|
void addCallDepEdges(const std::vector<SchedGraphNode*>& callNV,
|
|
|
|
const TargetMachine& target);
|
2003-08-25 22:42:20 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
|
|
|
|
const TargetMachine& target);
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
void addEdgesForValue(SchedGraphNode* refNode, const RefVec& defVec,
|
|
|
|
const Value* defValue, bool refNodeIsDef,
|
|
|
|
bool refNodeIsDefAndUse,
|
|
|
|
const TargetMachine& target);
|
|
|
|
|
|
|
|
void addDummyEdges();
|
2003-08-25 22:42:20 +00:00
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-06-02 22:45:07 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
class SchedGraphSet {
|
|
|
|
const Function* function;
|
|
|
|
std::vector<SchedGraph*> Graphs;
|
|
|
|
|
|
|
|
// Graph builder
|
2003-08-27 02:42:58 +00:00
|
|
|
void buildGraphsForMethod(const Function *F, const TargetMachine& target);
|
2003-08-25 22:42:20 +00:00
|
|
|
|
|
|
|
inline void addGraph(SchedGraph* graph) {
|
|
|
|
assert(graph != NULL);
|
|
|
|
Graphs.push_back(graph);
|
|
|
|
}
|
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
public:
|
2003-08-27 02:42:58 +00:00
|
|
|
SchedGraphSet(const Function *function, const TargetMachine& target);
|
2003-06-02 22:45:07 +00:00
|
|
|
~SchedGraphSet();
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
//iterators
|
|
|
|
typedef std::vector<SchedGraph*>::const_iterator iterator;
|
|
|
|
typedef std::vector<SchedGraph*>::const_iterator const_iterator;
|
|
|
|
|
|
|
|
std::vector<SchedGraph*>::const_iterator begin() const { return Graphs.begin(); }
|
|
|
|
std::vector<SchedGraph*>::const_iterator end() const { return Graphs.end(); }
|
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
// Debugging support
|
2003-06-02 22:45:07 +00:00
|
|
|
void dump() const;
|
2001-08-28 23:06:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
//********************** Sched Graph Iterators *****************************/
|
|
|
|
|
|
|
|
// Ok to make it a template because it shd get instantiated at most twice:
|
|
|
|
// for <SchedGraphNode, SchedGraphNode::iterator> and
|
|
|
|
// for <const SchedGraphNode, SchedGraphNode::const_iterator>.
|
|
|
|
//
|
|
|
|
template <class _NodeType, class _EdgeType, class _EdgeIter>
|
2002-07-25 06:17:51 +00:00
|
|
|
class SGPredIterator: public bidirectional_iterator<_NodeType, ptrdiff_t> {
|
2001-08-28 23:06:02 +00:00
|
|
|
protected:
|
|
|
|
_EdgeIter oi;
|
|
|
|
public:
|
2002-02-12 22:39:50 +00:00
|
|
|
typedef SGPredIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2002-02-12 22:39:50 +00:00
|
|
|
inline SGPredIterator(_EdgeIter startEdge) : oi(startEdge) {}
|
2001-08-28 23:06:02 +00:00
|
|
|
|
|
|
|
inline bool operator==(const _Self& x) const { return oi == x.oi; }
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
|
|
|
// operator*() differs for pred or succ iterator
|
2003-08-25 22:42:20 +00:00
|
|
|
inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSrc(); }
|
2003-08-27 02:42:58 +00:00
|
|
|
inline _NodeType* operator->() const { return operator*(); }
|
2001-08-28 23:06:02 +00:00
|
|
|
|
|
|
|
inline _EdgeType* getEdge() const { return *(oi); }
|
|
|
|
|
2001-09-07 21:07:10 +00:00
|
|
|
inline _Self &operator++() { ++oi; return *this; } // Preincrement
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
|
|
|
_Self tmp(*this); ++*this; return tmp;
|
|
|
|
}
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2001-09-07 21:07:10 +00:00
|
|
|
inline _Self &operator--() { --oi; return *this; } // Predecrement
|
|
|
|
inline _Self operator--(int) { // Postdecrement
|
|
|
|
_Self tmp = *this; --*this; return tmp;
|
|
|
|
}
|
2001-08-28 23:06:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _NodeType, class _EdgeType, class _EdgeIter>
|
2002-07-25 06:17:51 +00:00
|
|
|
class SGSuccIterator : public bidirectional_iterator<_NodeType, ptrdiff_t> {
|
2001-09-07 21:07:10 +00:00
|
|
|
protected:
|
|
|
|
_EdgeIter oi;
|
2001-08-28 23:06:02 +00:00
|
|
|
public:
|
2002-02-12 22:39:50 +00:00
|
|
|
typedef SGSuccIterator<_NodeType, _EdgeType, _EdgeIter> _Self;
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2002-02-12 22:39:50 +00:00
|
|
|
inline SGSuccIterator(_EdgeIter startEdge) : oi(startEdge) {}
|
2001-09-07 21:07:10 +00:00
|
|
|
|
|
|
|
inline bool operator==(const _Self& x) const { return oi == x.oi; }
|
|
|
|
inline bool operator!=(const _Self& x) const { return !operator==(x); }
|
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
inline _NodeType* operator*() const { return (_NodeType*)(*oi)->getSink(); }
|
2003-08-27 02:42:58 +00:00
|
|
|
inline _NodeType* operator->() const { return operator*(); }
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2001-09-07 21:07:10 +00:00
|
|
|
inline _EdgeType* getEdge() const { return *(oi); }
|
|
|
|
|
|
|
|
inline _Self &operator++() { ++oi; return *this; } // Preincrement
|
|
|
|
inline _Self operator++(int) { // Postincrement
|
|
|
|
_Self tmp(*this); ++*this; return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _Self &operator--() { --oi; return *this; } // Predecrement
|
|
|
|
inline _Self operator--(int) { // Postdecrement
|
|
|
|
_Self tmp = *this; --*this; return tmp;
|
|
|
|
}
|
2001-08-28 23:06:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// sg_pred_iterator
|
|
|
|
// sg_pred_const_iterator
|
|
|
|
//
|
2002-02-12 22:39:50 +00:00
|
|
|
typedef SGPredIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
|
2001-08-28 23:06:02 +00:00
|
|
|
sg_pred_iterator;
|
2002-02-12 22:39:50 +00:00
|
|
|
typedef SGPredIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
|
2001-08-28 23:06:02 +00:00
|
|
|
sg_pred_const_iterator;
|
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
inline sg_pred_iterator pred_begin(SchedGraphNode *N) {
|
2001-08-28 23:06:02 +00:00
|
|
|
return sg_pred_iterator(N->beginInEdges());
|
|
|
|
}
|
2003-08-27 02:42:58 +00:00
|
|
|
inline sg_pred_iterator pred_end(SchedGraphNode *N) {
|
2001-08-28 23:06:02 +00:00
|
|
|
return sg_pred_iterator(N->endInEdges());
|
|
|
|
}
|
|
|
|
inline sg_pred_const_iterator pred_begin(const SchedGraphNode *N) {
|
|
|
|
return sg_pred_const_iterator(N->beginInEdges());
|
|
|
|
}
|
2003-08-27 02:42:58 +00:00
|
|
|
inline sg_pred_const_iterator pred_end(const SchedGraphNode *N) {
|
2001-08-28 23:06:02 +00:00
|
|
|
return sg_pred_const_iterator(N->endInEdges());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// sg_succ_iterator
|
|
|
|
// sg_succ_const_iterator
|
|
|
|
//
|
2002-02-12 22:39:50 +00:00
|
|
|
typedef SGSuccIterator<SchedGraphNode, SchedGraphEdge, SchedGraphNode::iterator>
|
2001-08-28 23:06:02 +00:00
|
|
|
sg_succ_iterator;
|
2002-02-12 22:39:50 +00:00
|
|
|
typedef SGSuccIterator<const SchedGraphNode, const SchedGraphEdge,SchedGraphNode::const_iterator>
|
2001-08-28 23:06:02 +00:00
|
|
|
sg_succ_const_iterator;
|
|
|
|
|
2003-08-27 02:42:58 +00:00
|
|
|
inline sg_succ_iterator succ_begin(SchedGraphNode *N) {
|
2001-08-28 23:06:02 +00:00
|
|
|
return sg_succ_iterator(N->beginOutEdges());
|
|
|
|
}
|
2003-08-27 02:42:58 +00:00
|
|
|
inline sg_succ_iterator succ_end(SchedGraphNode *N) {
|
2001-08-28 23:06:02 +00:00
|
|
|
return sg_succ_iterator(N->endOutEdges());
|
|
|
|
}
|
|
|
|
inline sg_succ_const_iterator succ_begin(const SchedGraphNode *N) {
|
|
|
|
return sg_succ_const_iterator(N->beginOutEdges());
|
|
|
|
}
|
2003-08-27 02:42:58 +00:00
|
|
|
inline sg_succ_const_iterator succ_end(const SchedGraphNode *N) {
|
2001-08-28 23:06:02 +00:00
|
|
|
return sg_succ_const_iterator(N->endOutEdges());
|
|
|
|
}
|
|
|
|
|
2001-09-28 22:56:31 +00:00
|
|
|
// Provide specializations of GraphTraits to be able to use graph iterators on
|
|
|
|
// the scheduling graph!
|
2001-08-28 23:06:02 +00:00
|
|
|
//
|
2001-09-28 22:56:31 +00:00
|
|
|
template <> struct GraphTraits<SchedGraph*> {
|
|
|
|
typedef SchedGraphNode NodeType;
|
|
|
|
typedef sg_succ_iterator ChildIteratorType;
|
|
|
|
|
2003-08-25 22:42:20 +00:00
|
|
|
static inline NodeType *getEntryNode(SchedGraph *SG) { return (NodeType*)SG->getRoot(); }
|
2001-09-28 22:56:31 +00:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
|
|
|
return succ_begin(N);
|
|
|
|
}
|
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
|
|
|
return succ_end(N);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct GraphTraits<const SchedGraph*> {
|
|
|
|
typedef const SchedGraphNode NodeType;
|
|
|
|
typedef sg_succ_const_iterator ChildIteratorType;
|
|
|
|
|
|
|
|
static inline NodeType *getEntryNode(const SchedGraph *SG) {
|
2003-08-25 22:42:20 +00:00
|
|
|
return (NodeType*)SG->getRoot();
|
2001-09-28 22:56:31 +00:00
|
|
|
}
|
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
|
|
|
return succ_begin(N);
|
|
|
|
}
|
|
|
|
static inline ChildIteratorType child_end(NodeType *N) {
|
|
|
|
return succ_end(N);
|
|
|
|
}
|
|
|
|
};
|
2001-08-28 23:06:02 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-08-28 23:06:02 +00:00
|
|
|
#endif
|