2004-08-02 14:02:21 +00:00
|
|
|
//===-- MSchedGraph.cpp - Scheduling Graph ----------------------*- C++ -*-===//
|
2004-03-01 02:50:57 +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-03-23 01:47:20 +00:00
|
|
|
// A graph class for dependencies. This graph only contains true, anti, and
|
|
|
|
// output data dependencies for a given MachineBasicBlock. Dependencies
|
|
|
|
// across iterations are also computed. Unless data dependence analysis
|
2005-04-21 23:30:14 +00:00
|
|
|
// is provided, a conservative approach of adding dependencies between all
|
2005-03-23 01:47:20 +00:00
|
|
|
// loads and stores is taken.
|
2004-03-01 02:50:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define DEBUG_TYPE "ModuloSched"
|
|
|
|
|
|
|
|
#include "MSchedGraph.h"
|
2004-10-10 23:34:50 +00:00
|
|
|
#include "../SparcV9RegisterInfo.h"
|
2005-02-10 17:02:58 +00:00
|
|
|
#include "../MachineCodeForInstruction.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
2005-04-22 06:32:48 +00:00
|
|
|
#include "llvm/Constants.h"
|
2005-02-10 17:02:58 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2005-03-24 05:13:53 +00:00
|
|
|
#include "llvm/Type.h"
|
2004-03-01 02:50:57 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2004-08-02 14:02:21 +00:00
|
|
|
#include <cstdlib>
|
2004-09-28 14:42:44 +00:00
|
|
|
#include <algorithm>
|
2005-03-23 01:47:20 +00:00
|
|
|
#include <set>
|
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//MSchedGraphNode constructor
|
2005-04-21 23:30:14 +00:00
|
|
|
MSchedGraphNode::MSchedGraphNode(const MachineInstr* inst,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraph *graph, unsigned idx,
|
2005-07-27 06:12:32 +00:00
|
|
|
unsigned late, bool isBranch)
|
|
|
|
: Inst(inst), Parent(graph), index(idx), latency(late),
|
2005-04-22 06:32:48 +00:00
|
|
|
isBranchInstr(isBranch) {
|
2004-03-01 02:50:57 +00:00
|
|
|
|
|
|
|
//Add to the graph
|
|
|
|
graph->addNode(inst, this);
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//MSchedGraphNode copy constructor
|
2005-04-21 23:30:14 +00:00
|
|
|
MSchedGraphNode::MSchedGraphNode(const MSchedGraphNode &N)
|
2005-02-10 17:02:58 +00:00
|
|
|
: Predecessors(N.Predecessors), Successors(N.Successors) {
|
|
|
|
|
|
|
|
Inst = N.Inst;
|
|
|
|
Parent = N.Parent;
|
|
|
|
index = N.index;
|
|
|
|
latency = N.latency;
|
|
|
|
isBranchInstr = N.isBranchInstr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Print the node (instruction and latency)
|
2004-03-01 02:50:57 +00:00
|
|
|
void MSchedGraphNode::print(std::ostream &os) const {
|
2005-04-21 23:30:14 +00:00
|
|
|
os << "MSchedGraphNode: Inst=" << *Inst << ", latency= " << latency << "\n";
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
|
|
|
|
//Get the edge from a predecessor to this node
|
2004-03-01 02:50:57 +00:00
|
|
|
MSchedGraphEdge MSchedGraphNode::getInEdge(MSchedGraphNode *pred) {
|
|
|
|
//Loop over all the successors of our predecessor
|
|
|
|
//return the edge the corresponds to this in edge
|
2005-04-21 23:30:14 +00:00
|
|
|
for (MSchedGraphNode::succ_iterator I = pred->succ_begin(),
|
2004-08-02 14:02:21 +00:00
|
|
|
E = pred->succ_end(); I != E; ++I) {
|
|
|
|
if (*I == this)
|
2004-03-01 02:50:57 +00:00
|
|
|
return I.getEdge();
|
|
|
|
}
|
|
|
|
assert(0 && "Should have found edge between this node and its predecessor!");
|
2004-08-02 14:02:21 +00:00
|
|
|
abort();
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Get the iteration difference for the edge from this node to its successor
|
2005-02-10 17:02:58 +00:00
|
|
|
unsigned MSchedGraphNode::getIteDiff(MSchedGraphNode *succ) {
|
2005-07-27 06:12:32 +00:00
|
|
|
for(std::vector<MSchedGraphEdge>::iterator I = Successors.begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
E = Successors.end();
|
2005-02-10 17:02:58 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if(I->getDest() == succ)
|
|
|
|
return I->getIteDiff();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Get the index into the vector of edges for the edge from pred to this node
|
2004-05-08 16:12:10 +00:00
|
|
|
unsigned MSchedGraphNode::getInEdgeNum(MSchedGraphNode *pred) {
|
|
|
|
//Loop over all the successors of our predecessor
|
|
|
|
//return the edge the corresponds to this in edge
|
|
|
|
int count = 0;
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraphNode::succ_iterator I = pred->succ_begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
E = pred->succ_end();
|
2004-05-08 16:12:10 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if(*I == this)
|
|
|
|
return count;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
assert(0 && "Should have found edge between this node and its predecessor!");
|
|
|
|
abort();
|
|
|
|
}
|
2005-03-23 01:47:20 +00:00
|
|
|
|
|
|
|
//Determine if succ is a successor of this node
|
2004-05-08 16:12:10 +00:00
|
|
|
bool MSchedGraphNode::isSuccessor(MSchedGraphNode *succ) {
|
|
|
|
for(succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
|
|
|
|
if(*I == succ)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Dtermine if pred is a predecessor of this node
|
2004-05-08 16:12:10 +00:00
|
|
|
bool MSchedGraphNode::isPredecessor(MSchedGraphNode *pred) {
|
2005-07-27 06:12:32 +00:00
|
|
|
if(std::find( Predecessors.begin(), Predecessors.end(),
|
2005-07-27 05:53:44 +00:00
|
|
|
pred) != Predecessors.end())
|
2004-05-08 16:12:10 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Add a node to the graph
|
2004-03-01 02:50:57 +00:00
|
|
|
void MSchedGraph::addNode(const MachineInstr *MI,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphNode *node) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
|
|
|
//Make sure node does not already exist
|
|
|
|
assert(GraphMap.find(MI) == GraphMap.end()
|
2005-07-27 05:53:44 +00:00
|
|
|
&& "New MSchedGraphNode already exists for this instruction");
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
GraphMap[MI] = node;
|
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Delete a node to the graph
|
2005-02-10 17:02:58 +00:00
|
|
|
void MSchedGraph::deleteNode(MSchedGraphNode *node) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
//Delete the edge to this node from all predecessors
|
2005-02-16 04:00:59 +00:00
|
|
|
while(node->pred_size() > 0) {
|
2005-04-21 23:30:14 +00:00
|
|
|
//DEBUG(std::cerr << "Delete edge from: " << **P << " to " << *node << "\n");
|
2005-02-16 04:00:59 +00:00
|
|
|
MSchedGraphNode *pred = *(node->pred_begin());
|
|
|
|
pred->deleteSuccessor(node);
|
2005-02-10 17:02:58 +00:00
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
//Remove this node from the graph
|
|
|
|
GraphMap.erase(node->getInst());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
|
|
|
|
//Create a graph for a machine block. The ignoreInstrs map is so that
|
|
|
|
//we ignore instructions associated to the index variable since this
|
|
|
|
//is a special case in Modulo Scheduling. We only want to deal with
|
|
|
|
//the body of the loop.
|
2005-07-27 06:12:32 +00:00
|
|
|
MSchedGraph::MSchedGraph(const MachineBasicBlock *bb,
|
|
|
|
const TargetMachine &targ,
|
|
|
|
std::map<const MachineInstr*, unsigned> &ignoreInstrs,
|
|
|
|
DependenceAnalyzer &DA,
|
2005-07-27 05:53:44 +00:00
|
|
|
std::map<MachineInstr*, Instruction*> &machineTollvm)
|
2005-04-30 23:07:59 +00:00
|
|
|
: Target(targ) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
|
|
|
//Make sure BB is not null,
|
2005-04-30 23:07:59 +00:00
|
|
|
assert(bb != NULL && "Basic Block is null");
|
2004-03-01 02:50:57 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
BBs.push_back(bb);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Create nodes and edges for this BB
|
2005-04-05 16:36:44 +00:00
|
|
|
buildNodesAndEdges(ignoreInstrs, DA, machineTollvm);
|
2005-03-23 01:47:20 +00:00
|
|
|
|
|
|
|
//Experimental!
|
|
|
|
//addBranchEdges();
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
|
2005-05-01 01:25:53 +00:00
|
|
|
//Create a graph for a machine block. The ignoreInstrs map is so that
|
|
|
|
//we ignore instructions associated to the index variable since this
|
|
|
|
//is a special case in Modulo Scheduling. We only want to deal with
|
|
|
|
//the body of the loop.
|
2005-07-27 06:12:32 +00:00
|
|
|
MSchedGraph::MSchedGraph(std::vector<const MachineBasicBlock*> &bbs,
|
|
|
|
const TargetMachine &targ,
|
|
|
|
std::map<const MachineInstr*, unsigned> &ignoreInstrs,
|
|
|
|
DependenceAnalyzer &DA,
|
2005-07-27 05:53:44 +00:00
|
|
|
std::map<MachineInstr*, Instruction*> &machineTollvm)
|
2005-05-01 01:25:53 +00:00
|
|
|
: BBs(bbs), Target(targ) {
|
|
|
|
|
|
|
|
//Make sure there is at least one BB and it is not null,
|
|
|
|
assert(((bbs.size() >= 1) && bbs[1] != NULL) && "Basic Block is null");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-05-01 01:25:53 +00:00
|
|
|
//Create nodes and edges for this BB
|
|
|
|
buildNodesAndEdges(ignoreInstrs, DA, machineTollvm);
|
|
|
|
|
|
|
|
//Experimental!
|
|
|
|
//addBranchEdges();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Copies the graph and keeps a map from old to new nodes
|
2005-07-27 06:12:32 +00:00
|
|
|
MSchedGraph::MSchedGraph(const MSchedGraph &G,
|
|
|
|
std::map<MSchedGraphNode*, MSchedGraphNode*> &newNodes)
|
2005-04-30 23:07:59 +00:00
|
|
|
: Target(G.Target) {
|
|
|
|
|
|
|
|
BBs = G.BBs;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
std::map<MSchedGraphNode*, MSchedGraphNode*> oldToNew;
|
|
|
|
//Copy all nodes
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::const_iterator N = G.GraphMap.begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
NE = G.GraphMap.end(); N != NE; ++N) {
|
2005-04-22 06:32:48 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
MSchedGraphNode *newNode = new MSchedGraphNode(*(N->second));
|
|
|
|
oldToNew[&*(N->second)] = newNode;
|
|
|
|
newNodes[newNode] = &*(N->second);
|
|
|
|
GraphMap[&*(N->first)] = newNode;
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
//Loop over nodes and update edges to point to new nodes
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::iterator N = GraphMap.begin(), NE = GraphMap.end();
|
2005-04-22 06:32:48 +00:00
|
|
|
N != NE; ++N) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
//Get the node we are dealing with
|
|
|
|
MSchedGraphNode *node = &*(N->second);
|
|
|
|
|
|
|
|
node->setParent(this);
|
|
|
|
|
|
|
|
//Loop over nodes successors and predecessors and update to the new nodes
|
|
|
|
for(unsigned i = 0; i < node->pred_size(); ++i) {
|
|
|
|
node->setPredecessor(i, oldToNew[node->getPredecessor(i)]);
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
for(unsigned i = 0; i < node->succ_size(); ++i) {
|
|
|
|
MSchedGraphEdge *edge = node->getSuccessor(i);
|
|
|
|
MSchedGraphNode *oldDest = edge->getDest();
|
|
|
|
edge->setDest(oldToNew[oldDest]);
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
}
|
2005-02-10 17:02:58 +00:00
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Deconstructor, deletes all nodes in the graph
|
2004-03-01 02:50:57 +00:00
|
|
|
MSchedGraph::~MSchedGraph () {
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end();
|
2005-04-22 06:32:48 +00:00
|
|
|
I != E; ++I)
|
2004-03-01 02:50:57 +00:00
|
|
|
delete I->second;
|
|
|
|
}
|
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//Print out graph
|
|
|
|
void MSchedGraph::print(std::ostream &os) const {
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::const_iterator N = GraphMap.begin(), NE = GraphMap.end();
|
2005-04-22 06:32:48 +00:00
|
|
|
N != NE; ++N) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//Get the node we are dealing with
|
|
|
|
MSchedGraphNode *node = &*(N->second);
|
2005-03-23 01:47:20 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
os << "Node Start\n";
|
|
|
|
node->print(os);
|
|
|
|
os << "Successors:\n";
|
|
|
|
//print successors
|
|
|
|
for(unsigned i = 0; i < node->succ_size(); ++i) {
|
|
|
|
MSchedGraphEdge *edge = node->getSuccessor(i);
|
|
|
|
MSchedGraphNode *oldDest = edge->getDest();
|
|
|
|
oldDest->print(os);
|
|
|
|
}
|
|
|
|
os << "Node End\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Calculate total delay
|
|
|
|
int MSchedGraph::totalDelay() {
|
|
|
|
int sum = 0;
|
|
|
|
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::const_iterator N = GraphMap.begin(), NE = GraphMap.end();
|
2005-04-22 06:32:48 +00:00
|
|
|
N != NE; ++N) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//Get the node we are dealing with
|
|
|
|
MSchedGraphNode *node = &*(N->second);
|
|
|
|
sum += node->getLatency();
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
2005-03-23 01:47:20 +00:00
|
|
|
//Experimental code to add edges from the branch to all nodes dependent upon it.
|
2005-07-27 06:12:32 +00:00
|
|
|
void hasPath(MSchedGraphNode *node, std::set<MSchedGraphNode*> &visited,
|
2005-07-27 05:53:44 +00:00
|
|
|
std::set<MSchedGraphNode*> &branches, MSchedGraphNode *startNode,
|
|
|
|
std::set<std::pair<MSchedGraphNode*,MSchedGraphNode*> > &newEdges ) {
|
2005-03-23 01:47:20 +00:00
|
|
|
|
|
|
|
visited.insert(node);
|
|
|
|
DEBUG(std::cerr << "Visiting: " << *node << "\n");
|
|
|
|
//Loop over successors
|
|
|
|
for(unsigned i = 0; i < node->succ_size(); ++i) {
|
|
|
|
MSchedGraphEdge *edge = node->getSuccessor(i);
|
|
|
|
MSchedGraphNode *dest = edge->getDest();
|
|
|
|
if(branches.count(dest))
|
|
|
|
newEdges.insert(std::make_pair(dest, startNode));
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//only visit if we have not already
|
|
|
|
else if(!visited.count(dest)) {
|
|
|
|
if(edge->getIteDiff() == 0)
|
2005-07-27 05:53:44 +00:00
|
|
|
hasPath(dest, visited, branches, startNode, newEdges);}
|
2005-03-23 01:47:20 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Experimental code to add edges from the branch to all nodes dependent upon it.
|
|
|
|
void MSchedGraph::addBranchEdges() {
|
|
|
|
std::set<MSchedGraphNode*> branches;
|
|
|
|
std::set<MSchedGraphNode*> nodes;
|
|
|
|
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end();
|
2005-04-22 06:32:48 +00:00
|
|
|
I != E; ++I) {
|
2005-03-23 01:47:20 +00:00
|
|
|
if(I->second->isBranch())
|
|
|
|
if(I->second->hasPredecessors())
|
2005-07-27 05:53:44 +00:00
|
|
|
branches.insert(I->second);
|
2005-03-23 01:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//See if there is a path first instruction to the branches, if so, add an
|
|
|
|
//iteration dependence between that node and the branch
|
|
|
|
std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> > newEdges;
|
2005-07-27 06:12:32 +00:00
|
|
|
for(MSchedGraph::iterator I = GraphMap.begin(), E = GraphMap.end();
|
2005-04-22 06:32:48 +00:00
|
|
|
I != E; ++I) {
|
2005-03-23 01:47:20 +00:00
|
|
|
std::set<MSchedGraphNode*> visited;
|
|
|
|
hasPath((I->second), visited, branches, (I->second), newEdges);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Spit out all edges we are going to add
|
|
|
|
unsigned min = GraphMap.size();
|
|
|
|
if(newEdges.size() == 1) {
|
2005-04-21 23:30:14 +00:00
|
|
|
((newEdges.begin())->first)->addOutEdge(((newEdges.begin())->second),
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::BranchDep,
|
|
|
|
MSchedGraphEdge::NonDataDep, 1);
|
2005-03-23 01:47:20 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
unsigned count = 0;
|
|
|
|
MSchedGraphNode *start;
|
|
|
|
MSchedGraphNode *end;
|
|
|
|
for(std::set<std::pair<MSchedGraphNode*, MSchedGraphNode*> >::iterator I = newEdges.begin(), E = newEdges.end(); I != E; ++I) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
DEBUG(std::cerr << "Branch Edge from: " << *(I->first) << " to " << *(I->second) << "\n");
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
// if(I->second->getIndex() <= min) {
|
2005-07-27 05:53:44 +00:00
|
|
|
start = I->first;
|
|
|
|
end = I->second;
|
|
|
|
//min = I->second->getIndex();
|
|
|
|
//}
|
|
|
|
start->addOutEdge(end,
|
|
|
|
MSchedGraphEdge::BranchDep,
|
|
|
|
MSchedGraphEdge::NonDataDep, 1);
|
2005-03-23 01:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Add edges between the nodes
|
2005-04-05 16:36:44 +00:00
|
|
|
void MSchedGraph::buildNodesAndEdges(std::map<const MachineInstr*, unsigned> &ignoreInstrs,
|
2005-07-27 05:53:44 +00:00
|
|
|
DependenceAnalyzer &DA,
|
|
|
|
std::map<MachineInstr*, Instruction*> &machineTollvm) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get Machine target information for calculating latency
|
2004-07-30 23:36:10 +00:00
|
|
|
const TargetInstrInfo *MTI = Target.getInstrInfo();
|
2004-03-01 02:50:57 +00:00
|
|
|
|
|
|
|
std::vector<MSchedGraphNode*> memInstructions;
|
|
|
|
std::map<int, std::vector<OpIndexNodePair> > regNumtoNodeMap;
|
|
|
|
std::map<const Value*, std::vector<OpIndexNodePair> > valuetoNodeMap;
|
|
|
|
|
|
|
|
//Save PHI instructions to deal with later
|
|
|
|
std::vector<const MachineInstr*> phiInstrs;
|
2004-11-28 23:36:15 +00:00
|
|
|
unsigned index = 0;
|
2005-02-10 17:02:58 +00:00
|
|
|
|
2005-07-27 06:12:32 +00:00
|
|
|
for(std::vector<const MachineBasicBlock*>::iterator B = BBs.begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
BE = BBs.end(); B != BE; ++B) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
const MachineBasicBlock *BB = *B;
|
|
|
|
|
|
|
|
//Loop over instructions in MBB and add nodes and edges
|
2005-07-27 06:12:32 +00:00
|
|
|
for (MachineBasicBlock::const_iterator MI = BB->begin(), e = BB->end();
|
2005-07-27 05:53:44 +00:00
|
|
|
MI != e; ++MI) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Ignore indvar instructions
|
|
|
|
if(ignoreInstrs.count(MI)) {
|
2005-07-27 05:53:44 +00:00
|
|
|
++index;
|
|
|
|
continue;
|
2005-04-30 23:07:59 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Get each instruction of machine basic block, get the delay
|
|
|
|
//using the op code, create a new node for it, and add to the
|
|
|
|
//graph.
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
MachineOpCode opCode = MI->getOpcode();
|
|
|
|
int delay;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2004-05-08 16:12:10 +00:00
|
|
|
#if 0 // FIXME: LOOK INTO THIS
|
2005-04-30 23:07:59 +00:00
|
|
|
//Check if subsequent instructions can be issued before
|
|
|
|
//the result is ready, if so use min delay.
|
|
|
|
if(MTI->hasResultInterlock(MIopCode))
|
2005-07-27 05:53:44 +00:00
|
|
|
delay = MTI->minLatency(MIopCode);
|
2005-04-30 23:07:59 +00:00
|
|
|
else
|
2004-05-08 16:12:10 +00:00
|
|
|
#endif
|
2005-07-27 05:53:44 +00:00
|
|
|
//Get delay
|
|
|
|
delay = MTI->maxLatency(opCode);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Create new node for this machine instruction and add to the graph.
|
|
|
|
//Create only if not a nop
|
|
|
|
if(MTI->isNop(opCode))
|
2005-07-27 05:53:44 +00:00
|
|
|
continue;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Sparc BE does not use PHI opcode, so assert on this case
|
|
|
|
assert(opCode != TargetInstrInfo::PHI && "Did not expect PHI opcode");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
bool isBranch = false;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//We want to flag the branch node to treat it special
|
|
|
|
if(MTI->isBranch(opCode))
|
2005-07-27 05:53:44 +00:00
|
|
|
isBranch = true;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Node is created and added to the graph automatically
|
2005-07-27 06:12:32 +00:00
|
|
|
MSchedGraphNode *node = new MSchedGraphNode(MI, this, index, delay,
|
2005-07-27 05:53:44 +00:00
|
|
|
isBranch);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
DEBUG(std::cerr << "Created Node: " << *node << "\n");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Check OpCode to keep track of memory operations to add memory
|
|
|
|
//dependencies later.
|
|
|
|
if(MTI->isLoad(opCode) || MTI->isStore(opCode))
|
2005-07-27 05:53:44 +00:00
|
|
|
memInstructions.push_back(node);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Loop over all operands, and put them into the register number to
|
|
|
|
//graph node map for determining dependencies
|
|
|
|
//If an operands is a use/def, we have an anti dependence to itself
|
|
|
|
for(unsigned i=0; i < MI->getNumOperands(); ++i) {
|
2005-07-27 05:53:44 +00:00
|
|
|
//Get Operand
|
|
|
|
const MachineOperand &mOp = MI->getOperand(i);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Check if it has an allocated register
|
|
|
|
if(mOp.hasAllocatedReg()) {
|
|
|
|
int regNum = mOp.getReg();
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
if(regNum != SparcV9::g0) {
|
|
|
|
//Put into our map
|
|
|
|
regNumtoNodeMap[regNum].push_back(std::make_pair(i, node));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Add virtual registers dependencies
|
|
|
|
//Check if any exist in the value map already and create dependencies
|
|
|
|
//between them.
|
2005-07-27 06:12:32 +00:00
|
|
|
if(mOp.getType() == MachineOperand::MO_VirtualRegister
|
2005-07-27 05:53:44 +00:00
|
|
|
|| mOp.getType() == MachineOperand::MO_CCRegister) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Make sure virtual register value is not null
|
|
|
|
assert((mOp.getVRegValue() != NULL) && "Null value is defined");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Check if this is a read operation in a phi node, if so DO NOT PROCESS
|
|
|
|
if(mOp.isUse() && (opCode == TargetInstrInfo::PHI)) {
|
|
|
|
DEBUG(std::cerr << "Read Operation in a PHI node\n");
|
|
|
|
continue;
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
if (const Value* srcI = mOp.getVRegValue()) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Find value in the map
|
|
|
|
std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
|
|
|
|
= valuetoNodeMap.find(srcI);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//If there is something in the map already, add edges from
|
|
|
|
//those instructions
|
|
|
|
//to this one we are processing
|
|
|
|
if(V != valuetoNodeMap.end()) {
|
|
|
|
addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(), phiInstrs);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Add to value map
|
|
|
|
V->second.push_back(std::make_pair(i,node));
|
|
|
|
}
|
|
|
|
//Otherwise put it in the map
|
|
|
|
else
|
|
|
|
//Put into value map
|
|
|
|
valuetoNodeMap[mOp.getVRegValue()].push_back(std::make_pair(i, node));
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
}
|
2005-04-30 23:07:59 +00:00
|
|
|
++index;
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Loop over LLVM BB, examine phi instructions, and add them to our
|
|
|
|
//phiInstr list to process
|
|
|
|
const BasicBlock *llvm_bb = BB->getBasicBlock();
|
2005-07-27 06:12:32 +00:00
|
|
|
for(BasicBlock::const_iterator I = llvm_bb->begin(), E = llvm_bb->end();
|
2005-07-27 05:53:44 +00:00
|
|
|
I != E; ++I) {
|
2005-04-30 23:07:59 +00:00
|
|
|
if(const PHINode *PN = dyn_cast<PHINode>(I)) {
|
2005-07-27 05:53:44 +00:00
|
|
|
MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(PN);
|
|
|
|
for (unsigned j = 0; j < tempMvec.size(); j++) {
|
|
|
|
if(!ignoreInstrs.count(tempMvec[j])) {
|
|
|
|
DEBUG(std::cerr << "Inserting phi instr into map: " << *tempMvec[j] << "\n");
|
|
|
|
phiInstrs.push_back((MachineInstr*) tempMvec[j]);
|
|
|
|
}
|
|
|
|
}
|
2005-04-30 23:07:59 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-02-10 17:02:58 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
addMemEdges(memInstructions, DA, machineTollvm);
|
|
|
|
addMachRegEdges(regNumtoNodeMap);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Finally deal with PHI Nodes and Value*
|
2005-07-27 06:12:32 +00:00
|
|
|
for(std::vector<const MachineInstr*>::iterator I = phiInstrs.begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
E = phiInstrs.end(); I != E; ++I) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Get Node for this instruction
|
|
|
|
std::map<const MachineInstr*, MSchedGraphNode*>::iterator X;
|
|
|
|
X = find(*I);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
if(X == GraphMap.end())
|
2005-07-27 05:53:44 +00:00
|
|
|
continue;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
MSchedGraphNode *node = X->second;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
DEBUG(std::cerr << "Adding ite diff edges for node: " << *node << "\n");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-30 23:07:59 +00:00
|
|
|
//Loop over operands for this instruction and add value edges
|
|
|
|
for(unsigned i=0; i < (*I)->getNumOperands(); ++i) {
|
2005-07-27 05:53:44 +00:00
|
|
|
//Get Operand
|
|
|
|
const MachineOperand &mOp = (*I)->getOperand(i);
|
2005-07-27 06:12:32 +00:00
|
|
|
if((mOp.getType() == MachineOperand::MO_VirtualRegister
|
2005-07-27 05:53:44 +00:00
|
|
|
|| mOp.getType() == MachineOperand::MO_CCRegister) && mOp.isUse()) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//find the value in the map
|
|
|
|
if (const Value* srcI = mOp.getVRegValue()) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Find value in the map
|
|
|
|
std::map<const Value*, std::vector<OpIndexNodePair> >::iterator V
|
|
|
|
= valuetoNodeMap.find(srcI);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//If there is something in the map already, add edges from
|
|
|
|
//those instructions
|
|
|
|
//to this one we are processing
|
|
|
|
if(V != valuetoNodeMap.end()) {
|
2005-07-27 06:12:32 +00:00
|
|
|
addValueEdges(V->second, node, mOp.isUse(), mOp.isDef(),
|
2005-07-27 05:53:44 +00:00
|
|
|
phiInstrs, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
}
|
2005-03-23 01:47:20 +00:00
|
|
|
}
|
|
|
|
//Add dependencies for Value*s
|
2004-03-01 02:50:57 +00:00
|
|
|
void MSchedGraph::addValueEdges(std::vector<OpIndexNodePair> &NodesInMap,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphNode *destNode, bool nodeIsUse,
|
|
|
|
bool nodeIsDef, std::vector<const MachineInstr*> &phiInstrs, int diff) {
|
2004-03-01 02:50:57 +00:00
|
|
|
|
2005-04-21 23:30:14 +00:00
|
|
|
for(std::vector<OpIndexNodePair>::iterator I = NodesInMap.begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
E = NodesInMap.end(); I != E; ++I) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get node in vectors machine operand that is the same value as node
|
|
|
|
MSchedGraphNode *srcNode = I->second;
|
|
|
|
MachineOperand mOp = srcNode->getInst()->getOperand(I->first);
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
if(diff > 0)
|
|
|
|
if(std::find(phiInstrs.begin(), phiInstrs.end(), srcNode->getInst()) == phiInstrs.end())
|
2005-07-27 05:53:44 +00:00
|
|
|
continue;
|
2005-03-23 01:47:20 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Node is a Def, so add output dep.
|
|
|
|
if(nodeIsDef) {
|
2005-02-10 17:02:58 +00:00
|
|
|
if(mOp.isUse()) {
|
2005-07-27 05:53:44 +00:00
|
|
|
DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=anti)\n");
|
|
|
|
srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
|
|
|
|
MSchedGraphEdge::AntiDep, diff);
|
2005-02-10 17:02:58 +00:00
|
|
|
}
|
|
|
|
if(mOp.isDef()) {
|
2005-07-27 05:53:44 +00:00
|
|
|
DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=output)\n");
|
|
|
|
srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
|
|
|
|
MSchedGraphEdge::OutputDep, diff);
|
2005-02-10 17:02:58 +00:00
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
if(nodeIsUse) {
|
2005-03-23 01:47:20 +00:00
|
|
|
if(mOp.isDef()) {
|
2005-07-27 05:53:44 +00:00
|
|
|
DEBUG(std::cerr << "Edge from " << *srcNode << " to " << *destNode << " (itediff=" << diff << ", type=true)\n");
|
|
|
|
srcNode->addOutEdge(destNode, MSchedGraphEdge::ValueDep,
|
|
|
|
MSchedGraphEdge::TrueDep, diff);
|
2005-03-23 01:47:20 +00:00
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Add dependencies for machine registers across iterations
|
2004-03-01 02:50:57 +00:00
|
|
|
void MSchedGraph::addMachRegEdges(std::map<int, std::vector<OpIndexNodePair> >& regNumtoNodeMap) {
|
|
|
|
//Loop over all machine registers in the map, and add dependencies
|
|
|
|
//between the instructions that use it
|
|
|
|
typedef std::map<int, std::vector<OpIndexNodePair> > regNodeMap;
|
2005-07-27 06:12:32 +00:00
|
|
|
for(regNodeMap::iterator I = regNumtoNodeMap.begin();
|
2005-04-22 06:32:48 +00:00
|
|
|
I != regNumtoNodeMap.end(); ++I) {
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get the register number
|
|
|
|
int regNum = (*I).first;
|
|
|
|
|
|
|
|
//Get Vector of nodes that use this register
|
|
|
|
std::vector<OpIndexNodePair> Nodes = (*I).second;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Loop over nodes and determine the dependence between the other
|
|
|
|
//nodes in the vector
|
|
|
|
for(unsigned i =0; i < Nodes.size(); ++i) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get src node operator index that uses this machine register
|
|
|
|
int srcOpIndex = Nodes[i].first;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get the actual src Node
|
|
|
|
MSchedGraphNode *srcNode = Nodes[i].second;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get Operand
|
|
|
|
const MachineOperand &srcMOp = srcNode->getInst()->getOperand(srcOpIndex);
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
bool srcIsUseandDef = srcMOp.isDef() && srcMOp.isUse();
|
|
|
|
bool srcIsUse = srcMOp.isUse() && !srcMOp.isDef();
|
2005-04-21 23:30:14 +00:00
|
|
|
|
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Look at all instructions after this in execution order
|
|
|
|
for(unsigned j=i+1; j < Nodes.size(); ++j) {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Sink node is a write
|
|
|
|
if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
|
|
|
|
//Src only uses the register (read)
|
2004-03-01 02:50:57 +00:00
|
|
|
if(srcIsUse)
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::AntiDep);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
else if(srcIsUseandDef) {
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::AntiDep);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::OutputDep);
|
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
else
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::OutputDep);
|
|
|
|
}
|
|
|
|
//Dest node is a read
|
|
|
|
else {
|
|
|
|
if(!srcIsUse || srcIsUseandDef)
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::TrueDep);
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Look at all the instructions before this one since machine registers
|
|
|
|
//could live across iterations.
|
|
|
|
for(unsigned j = 0; j < i; ++j) {
|
2005-07-27 05:53:44 +00:00
|
|
|
//Sink node is a write
|
|
|
|
if(Nodes[j].second->getInst()->getOperand(Nodes[j].first).isDef()) {
|
|
|
|
//Src only uses the register (read)
|
2004-03-01 02:50:57 +00:00
|
|
|
if(srcIsUse)
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::AntiDep, 1);
|
2004-03-01 02:50:57 +00:00
|
|
|
else if(srcIsUseandDef) {
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::AntiDep, 1);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::OutputDep, 1);
|
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
else
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::OutputDep, 1);
|
|
|
|
}
|
|
|
|
//Dest node is a read
|
|
|
|
else {
|
|
|
|
if(!srcIsUse || srcIsUseandDef)
|
2005-07-27 06:12:32 +00:00
|
|
|
srcNode->addOutEdge(Nodes[j].second,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::MachineRegister,
|
|
|
|
MSchedGraphEdge::TrueDep,1 );
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
//Add edges between all loads and stores
|
|
|
|
//Can be less strict with alias analysis and data dependence analysis.
|
2005-07-27 06:12:32 +00:00
|
|
|
void MSchedGraph::addMemEdges(const std::vector<MSchedGraphNode*>& memInst,
|
|
|
|
DependenceAnalyzer &DA,
|
2005-07-27 05:53:44 +00:00
|
|
|
std::map<MachineInstr*, Instruction*> &machineTollvm) {
|
2004-03-01 02:50:57 +00:00
|
|
|
|
|
|
|
//Get Target machine instruction info
|
2004-07-30 23:36:10 +00:00
|
|
|
const TargetInstrInfo *TMI = Target.getInstrInfo();
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Loop over all memory instructions in the vector
|
|
|
|
//Knowing that they are in execution, add true, anti, and output dependencies
|
|
|
|
for (unsigned srcIndex = 0; srcIndex < memInst.size(); ++srcIndex) {
|
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
MachineInstr *srcInst = (MachineInstr*) memInst[srcIndex]->getInst();
|
|
|
|
|
2004-03-01 02:50:57 +00:00
|
|
|
//Get the machine opCode to determine type of memory instruction
|
2005-03-23 01:47:20 +00:00
|
|
|
MachineOpCode srcNodeOpCode = srcInst->getOpcode();
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//All instructions after this one in execution order have an
|
|
|
|
//iteration delay of 0
|
|
|
|
for(unsigned destIndex = 0; destIndex < memInst.size(); ++destIndex) {
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//No self loops
|
|
|
|
if(destIndex == srcIndex)
|
2005-07-27 05:53:44 +00:00
|
|
|
continue;
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-03-23 01:47:20 +00:00
|
|
|
MachineInstr *destInst = (MachineInstr*) memInst[destIndex]->getInst();
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2005-03-29 20:35:10 +00:00
|
|
|
DEBUG(std::cerr << "MInst1: " << *srcInst << "\n");
|
|
|
|
DEBUG(std::cerr << "MInst2: " << *destInst << "\n");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//Assuming instructions without corresponding llvm instructions
|
|
|
|
//are from constant pools.
|
|
|
|
if (!machineTollvm.count(srcInst) || !machineTollvm.count(destInst))
|
2005-07-27 05:53:44 +00:00
|
|
|
continue;
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
bool useDepAnalyzer = true;
|
|
|
|
|
|
|
|
//Some machine loads and stores are generated by casts, so be
|
|
|
|
//conservative and always add deps
|
|
|
|
Instruction *srcLLVM = machineTollvm[srcInst];
|
|
|
|
Instruction *destLLVM = machineTollvm[destInst];
|
2005-07-27 06:12:32 +00:00
|
|
|
if(!isa<LoadInst>(srcLLVM)
|
2005-07-27 05:53:44 +00:00
|
|
|
&& !isa<StoreInst>(srcLLVM)) {
|
|
|
|
if(isa<BinaryOperator>(srcLLVM)) {
|
|
|
|
if(isa<ConstantFP>(srcLLVM->getOperand(0)) || isa<ConstantFP>(srcLLVM->getOperand(1)))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
useDepAnalyzer = false;
|
2005-04-22 06:32:48 +00:00
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
if(!isa<LoadInst>(destLLVM)
|
2005-07-27 05:53:44 +00:00
|
|
|
&& !isa<StoreInst>(destLLVM)) {
|
|
|
|
if(isa<BinaryOperator>(destLLVM)) {
|
|
|
|
if(isa<ConstantFP>(destLLVM->getOperand(0)) || isa<ConstantFP>(destLLVM->getOperand(1)))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
useDepAnalyzer = false;
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
2005-04-05 16:36:44 +00:00
|
|
|
|
2005-04-22 06:32:48 +00:00
|
|
|
//Use dep analysis when we have corresponding llvm loads/stores
|
|
|
|
if(useDepAnalyzer) {
|
2005-07-27 05:53:44 +00:00
|
|
|
bool srcBeforeDest = true;
|
|
|
|
if(destIndex < srcIndex)
|
|
|
|
srcBeforeDest = false;
|
|
|
|
|
2005-07-27 06:12:32 +00:00
|
|
|
DependenceResult dr = DA.getDependenceInfo(machineTollvm[srcInst],
|
|
|
|
machineTollvm[destInst],
|
2005-07-27 05:53:44 +00:00
|
|
|
srcBeforeDest);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
|
|
|
for(std::vector<Dependence>::iterator d = dr.dependences.begin(),
|
2005-07-27 05:53:44 +00:00
|
|
|
de = dr.dependences.end(); d != de; ++d) {
|
|
|
|
//Add edge from load to store
|
2005-07-27 06:12:32 +00:00
|
|
|
memInst[srcIndex]->addOutEdge(memInst[destIndex],
|
|
|
|
MSchedGraphEdge::MemoryDep,
|
2005-07-27 05:53:44 +00:00
|
|
|
d->getDepType(), d->getIteDiff());
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
}
|
2005-03-23 01:47:20 +00:00
|
|
|
}
|
2005-04-22 06:32:48 +00:00
|
|
|
//Otherwise, we can not do any further analysis and must make a dependence
|
|
|
|
else {
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
//Get the machine opCode to determine type of memory instruction
|
|
|
|
MachineOpCode destNodeOpCode = destInst->getOpcode();
|
|
|
|
|
|
|
|
//Get the Value* that we are reading from the load, always the first op
|
|
|
|
const MachineOperand &mOp = srcInst->getOperand(0);
|
|
|
|
const MachineOperand &mOp2 = destInst->getOperand(0);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-07-27 05:53:44 +00:00
|
|
|
if(mOp.hasAllocatedReg())
|
|
|
|
if(mOp.getReg() == SparcV9::g0)
|
|
|
|
continue;
|
|
|
|
if(mOp2.hasAllocatedReg())
|
|
|
|
if(mOp2.getReg() == SparcV9::g0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(std::cerr << "Adding dependence for machine instructions\n");
|
|
|
|
//Load-Store deps
|
|
|
|
if(TMI->isLoad(srcNodeOpCode)) {
|
|
|
|
|
|
|
|
if(TMI->isStore(destNodeOpCode))
|
2005-07-27 06:12:32 +00:00
|
|
|
memInst[srcIndex]->addOutEdge(memInst[destIndex],
|
|
|
|
MSchedGraphEdge::MemoryDep,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::AntiDep, 0);
|
|
|
|
}
|
|
|
|
else if(TMI->isStore(srcNodeOpCode)) {
|
|
|
|
if(TMI->isStore(destNodeOpCode))
|
2005-07-27 06:12:32 +00:00
|
|
|
memInst[srcIndex]->addOutEdge(memInst[destIndex],
|
|
|
|
MSchedGraphEdge::MemoryDep,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::OutputDep, 0);
|
|
|
|
|
|
|
|
else
|
2005-07-27 06:12:32 +00:00
|
|
|
memInst[srcIndex]->addOutEdge(memInst[destIndex],
|
|
|
|
MSchedGraphEdge::MemoryDep,
|
2005-07-27 05:53:44 +00:00
|
|
|
MSchedGraphEdge::TrueDep, 0);
|
|
|
|
}
|
2004-03-01 02:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|