2 //***************************************************************************
7 // Scheduling graph based on SSA graph plus extra dependence edges
8 // capturing dependences due to machine resources (machine registers,
9 // CC registers, and any others).
12 // 7/20/01 - Vikram Adve - Created
13 //**************************************************************************/
15 #include "SchedGraph.h"
16 #include "llvm/InstrTypes.h"
17 #include "llvm/Instruction.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Method.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/InstrSelection.h"
22 #include "llvm/Target/MachineInstrInfo.h"
23 #include "llvm/Target/MachineRegInfo.h"
24 #include "llvm/Support/StringExtras.h"
25 #include "llvm/iOther.h"
29 //*********************** Internal Data Structures *************************/
31 typedef vector< pair<SchedGraphNode*, unsigned int> > RefVec;
33 // The following needs to be a class, not a typedef, so we can use
34 // an opaque declaration in SchedGraph.h
35 struct RegToRefVecMap: public hash_map<int, RefVec> {
36 typedef hash_map<int, RefVec>:: iterator iterator;
37 typedef hash_map<int, RefVec>::const_iterator const_iterator;
41 // class SchedGraphEdge
45 SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
46 SchedGraphNode* _sink,
47 SchedGraphEdgeDepType _depType,
48 unsigned int _depOrderType,
53 depOrderType(_depOrderType),
54 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
57 src->addOutEdge(this);
58 sink->addInEdge(this);
63 SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
64 SchedGraphNode* _sink,
66 unsigned int _depOrderType,
71 depOrderType(_depOrderType),
72 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
75 src->addOutEdge(this);
76 sink->addInEdge(this);
81 SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
82 SchedGraphNode* _sink,
84 unsigned int _depOrderType,
88 depType(MachineRegister),
89 depOrderType(_depOrderType),
90 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
91 machineRegNum(_regNum)
93 src->addOutEdge(this);
94 sink->addInEdge(this);
99 SchedGraphEdge::SchedGraphEdge(SchedGraphNode* _src,
100 SchedGraphNode* _sink,
101 ResourceId _resourceId,
105 depType(MachineResource),
106 depOrderType(NonDataDep),
107 minDelay((_minDelay >= 0)? _minDelay : _src->getLatency()),
108 resourceId(_resourceId)
110 src->addOutEdge(this);
111 sink->addInEdge(this);
115 SchedGraphEdge::~SchedGraphEdge()
119 void SchedGraphEdge::dump(int indent=0) const {
120 printIndent(indent); cout << *this;
125 // class SchedGraphNode
129 SchedGraphNode::SchedGraphNode(unsigned int _nodeId,
130 const Instruction* _instr,
131 const MachineInstr* _minstr,
132 const TargetMachine& target)
140 MachineOpCode mopCode = minstr->getOpCode();
141 latency = target.getInstrInfo().hasResultInterlock(mopCode)
142 ? target.getInstrInfo().minLatency(mopCode)
143 : target.getInstrInfo().maxLatency(mopCode);
149 SchedGraphNode::~SchedGraphNode()
153 void SchedGraphNode::dump(int indent=0) const {
154 printIndent(indent); cout << *this;
159 SchedGraphNode::addInEdge(SchedGraphEdge* edge)
161 inEdges.push_back(edge);
166 SchedGraphNode::addOutEdge(SchedGraphEdge* edge)
168 outEdges.push_back(edge);
172 SchedGraphNode::removeInEdge(const SchedGraphEdge* edge)
174 assert(edge->getSink() == this);
176 for (iterator I = beginInEdges(); I != endInEdges(); ++I)
185 SchedGraphNode::removeOutEdge(const SchedGraphEdge* edge)
187 assert(edge->getSrc() == this);
189 for (iterator I = beginOutEdges(); I != endOutEdges(); ++I)
204 SchedGraph::SchedGraph(const BasicBlock* bb,
205 const TargetMachine& target)
208 this->buildGraph(target);
213 SchedGraph::~SchedGraph()
215 for (iterator I=begin(); I != end(); ++I)
217 SchedGraphNode* node = (*I).second;
219 // for each node, delete its out-edges
220 for (SchedGraphNode::iterator I = node->beginOutEdges();
221 I != node->endOutEdges(); ++I)
224 // then delete the node itself.
231 SchedGraph::dump() const
233 cout << " Sched Graph for Basic Blocks: ";
234 for (unsigned i=0, N=bbVec.size(); i < N; i++)
236 cout << (bbVec[i]->hasName()? bbVec[i]->getName() : "block")
237 << " (" << bbVec[i] << ")"
238 << ((i == N-1)? "" : ", ");
241 cout << endl << endl << " Actual Root nodes : ";
242 for (unsigned i=0, N=graphRoot->outEdges.size(); i < N; i++)
243 cout << graphRoot->outEdges[i]->getSink()->getNodeId()
244 << ((i == N-1)? "" : ", ");
246 cout << endl << " Graph Nodes:" << endl;
247 for (const_iterator I=begin(); I != end(); ++I)
248 cout << endl << * (*I).second;
255 SchedGraph::eraseIncomingEdges(SchedGraphNode* node, bool addDummyEdges)
257 // Delete and disconnect all in-edges for the node
258 for (SchedGraphNode::iterator I = node->beginInEdges();
259 I != node->endInEdges(); ++I)
261 SchedGraphNode* srcNode = (*I)->getSrc();
262 srcNode->removeOutEdge(*I);
266 srcNode != getRoot() &&
267 srcNode->beginOutEdges() == srcNode->endOutEdges())
268 { // srcNode has no more out edges, so add an edge to dummy EXIT node
269 assert(node != getLeaf() && "Adding edge that was just removed?");
270 (void) new SchedGraphEdge(srcNode, getLeaf(),
271 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
275 node->inEdges.clear();
279 SchedGraph::eraseOutgoingEdges(SchedGraphNode* node, bool addDummyEdges)
281 // Delete and disconnect all out-edges for the node
282 for (SchedGraphNode::iterator I = node->beginOutEdges();
283 I != node->endOutEdges(); ++I)
285 SchedGraphNode* sinkNode = (*I)->getSink();
286 sinkNode->removeInEdge(*I);
290 sinkNode != getLeaf() &&
291 sinkNode->beginInEdges() == sinkNode->endInEdges())
292 { //sinkNode has no more in edges, so add an edge from dummy ENTRY node
293 assert(node != getRoot() && "Adding edge that was just removed?");
294 (void) new SchedGraphEdge(getRoot(), sinkNode,
295 SchedGraphEdge::CtrlDep, SchedGraphEdge::NonDataDep, 0);
299 node->outEdges.clear();
303 SchedGraph::eraseIncidentEdges(SchedGraphNode* node, bool addDummyEdges)
305 this->eraseIncomingEdges(node, addDummyEdges);
306 this->eraseOutgoingEdges(node, addDummyEdges);
311 SchedGraph::addDummyEdges()
313 assert(graphRoot->outEdges.size() == 0);
315 for (const_iterator I=begin(); I != end(); ++I)
317 SchedGraphNode* node = (*I).second;
318 assert(node != graphRoot && node != graphLeaf);
319 if (node->beginInEdges() == node->endInEdges())
320 (void) new SchedGraphEdge(graphRoot, node, SchedGraphEdge::CtrlDep,
321 SchedGraphEdge::NonDataDep, 0);
322 if (node->beginOutEdges() == node->endOutEdges())
323 (void) new SchedGraphEdge(node, graphLeaf, SchedGraphEdge::CtrlDep,
324 SchedGraphEdge::NonDataDep, 0);
330 SchedGraph::addCDEdges(const TerminatorInst* term,
331 const TargetMachine& target)
333 const MachineInstrInfo& mii = target.getInstrInfo();
334 MachineCodeForVMInstr& termMvec = term->getMachineInstrVec();
336 // Find the first branch instr in the sequence of machine instrs for term
339 while (! mii.isBranch(termMvec[first]->getOpCode()))
341 assert(first < termMvec.size() &&
342 "No branch instructions for BR? Ok, but weird! Delete assertion.");
343 if (first == termMvec.size())
346 SchedGraphNode* firstBrNode = this->getGraphNodeForInstr(termMvec[first]);
348 // Add CD edges from each instruction in the sequence to the
349 // *last preceding* branch instr. in the sequence
350 // Use a latency of 0 because we only need to prevent out-of-order issue.
352 for (int i = (int) termMvec.size()-1; i > (int) first; i--)
354 SchedGraphNode* toNode = this->getGraphNodeForInstr(termMvec[i]);
355 assert(toNode && "No node for instr generated for branch?");
357 for (int j = i-1; j >= 0; j--)
358 if (mii.isBranch(termMvec[j]->getOpCode()))
360 SchedGraphNode* brNode = this->getGraphNodeForInstr(termMvec[j]);
361 assert(brNode && "No node for instr generated for branch?");
362 (void) new SchedGraphEdge(brNode, toNode, SchedGraphEdge::CtrlDep,
363 SchedGraphEdge::NonDataDep, 0);
364 break; // only one incoming edge is enough
368 // Add CD edges from each instruction preceding the first branch
369 // to the first branch. Use a latency of 0 as above.
371 for (int i = first-1; i >= 0; i--)
373 SchedGraphNode* fromNode = this->getGraphNodeForInstr(termMvec[i]);
374 assert(fromNode && "No node for instr generated for branch?");
375 (void) new SchedGraphEdge(fromNode, firstBrNode, SchedGraphEdge::CtrlDep,
376 SchedGraphEdge::NonDataDep, 0);
379 // Now add CD edges to the first branch instruction in the sequence from
380 // all preceding instructions in the basic block. Use 0 latency again.
382 const BasicBlock* bb = term->getParent();
383 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
385 if ((*II) == (const Instruction*) term) // special case, handled above
388 assert(! (*II)->isTerminator() && "Two terminators in basic block?");
390 const MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
391 for (unsigned i=0, N=mvec.size(); i < N; i++)
393 SchedGraphNode* fromNode = this->getGraphNodeForInstr(mvec[i]);
394 if (fromNode == NULL)
395 continue; // dummy instruction, e.g., PHI
397 (void) new SchedGraphEdge(fromNode, firstBrNode,
398 SchedGraphEdge::CtrlDep,
399 SchedGraphEdge::NonDataDep, 0);
401 // If we find any other machine instructions (other than due to
402 // the terminator) that also have delay slots, add an outgoing edge
403 // from the instruction to the instructions in the delay slots.
405 unsigned d = mii.getNumDelaySlots(mvec[i]->getOpCode());
406 assert(i+d < N && "Insufficient delay slots for instruction?");
408 for (unsigned j=1; j <= d; j++)
410 SchedGraphNode* toNode = this->getGraphNodeForInstr(mvec[i+j]);
411 assert(toNode && "No node for machine instr in delay slot?");
412 (void) new SchedGraphEdge(fromNode, toNode,
413 SchedGraphEdge::CtrlDep,
414 SchedGraphEdge::NonDataDep, 0);
420 static const int SG_LOAD_REF = 0;
421 static const int SG_STORE_REF = 1;
422 static const int SG_CALL_REF = 2;
424 static const unsigned int SG_DepOrderArray[][3] = {
425 { SchedGraphEdge::NonDataDep,
426 SchedGraphEdge::AntiDep,
427 SchedGraphEdge::AntiDep },
428 { SchedGraphEdge::TrueDep,
429 SchedGraphEdge::OutputDep,
430 SchedGraphEdge::TrueDep | SchedGraphEdge::OutputDep },
431 { SchedGraphEdge::TrueDep,
432 SchedGraphEdge::AntiDep | SchedGraphEdge::OutputDep,
433 SchedGraphEdge::TrueDep | SchedGraphEdge::AntiDep
434 | SchedGraphEdge::OutputDep }
439 SchedGraph::addMemEdges(const vector<const Instruction*>& memVec,
440 const TargetMachine& target)
442 const MachineInstrInfo& mii = target.getInstrInfo();
444 for (unsigned im=0, NM=memVec.size(); im < NM; im++)
446 const Instruction* fromInstr = memVec[im];
447 int fromType = (fromInstr->getOpcode() == Instruction::Call
449 : (fromInstr->getOpcode() == Instruction::Load
453 for (unsigned jm=im+1; jm < NM; jm++)
455 const Instruction* toInstr = memVec[jm];
456 int toType = (fromInstr->getOpcode() == Instruction::Call? 2
457 : ((fromInstr->getOpcode()==Instruction::Load)? 0:1));
459 if (fromType == SG_LOAD_REF && toType == SG_LOAD_REF)
462 unsigned int depOrderType = SG_DepOrderArray[fromType][toType];
464 MachineCodeForVMInstr& fromInstrMvec=fromInstr->getMachineInstrVec();
465 MachineCodeForVMInstr& toInstrMvec = toInstr->getMachineInstrVec();
467 // We have two VM memory instructions, and at least one is a store
468 // or a call. Add edges between all machine load/store/call instrs.
469 // Use a latency of 1 to ensure that memory operations are ordered;
470 // latency does not otherwise matter (true dependences enforce that).
472 for (unsigned i=0, N=fromInstrMvec.size(); i < N; i++)
474 MachineOpCode fromOpCode = fromInstrMvec[i]->getOpCode();
476 if (mii.isLoad(fromOpCode) || mii.isStore(fromOpCode) ||
477 mii.isCall(fromOpCode))
479 SchedGraphNode* fromNode =
480 this->getGraphNodeForInstr(fromInstrMvec[i]);
481 assert(fromNode && "No node for memory instr?");
483 for (unsigned j=0, M=toInstrMvec.size(); j < M; j++)
485 MachineOpCode toOpCode = toInstrMvec[j]->getOpCode();
486 if (mii.isLoad(toOpCode) || mii.isStore(toOpCode)
487 || mii.isCall(fromOpCode))
489 SchedGraphNode* toNode =
490 this->getGraphNodeForInstr(toInstrMvec[j]);
491 assert(toNode && "No node for memory instr?");
493 (void) new SchedGraphEdge(fromNode, toNode,
494 SchedGraphEdge::MemoryDep,
506 SchedGraph::addCallCCEdges(const vector<const Instruction*>& memVec,
507 MachineCodeForBasicBlock& bbMvec,
508 const TargetMachine& target)
510 const MachineInstrInfo& mii = target.getInstrInfo();
511 vector<SchedGraphNode*> callNodeVec;
513 // Find the call machine instructions and put them in a vector.
514 // By using memVec, we avoid searching the entire machine code of the BB.
516 for (unsigned im=0, NM=memVec.size(); im < NM; im++)
517 if (memVec[im]->getOpcode() == Instruction::Call)
519 MachineCodeForVMInstr& callMvec=memVec[im]->getMachineInstrVec();
520 for (unsigned i=0; i < callMvec.size(); ++i)
521 if (mii.isCall(callMvec[i]->getOpCode()))
522 callNodeVec.push_back(this->getGraphNodeForInstr(callMvec[i]));
525 // Now add additional edges from/to CC reg instrs to/from call instrs.
526 // Essentially this prevents anything that sets or uses a CC reg from being
527 // reordered w.r.t. a call.
528 // Use a latency of 0 because we only need to prevent out-of-order issue,
529 // like with control dependences.
531 int lastCallNodeIdx = -1;
532 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
533 if (mii.isCall(bbMvec[i]->getOpCode()))
536 for ( ; lastCallNodeIdx < (int)callNodeVec.size(); ++lastCallNodeIdx)
537 if (callNodeVec[lastCallNodeIdx]->getMachineInstr() == bbMvec[i])
539 assert(lastCallNodeIdx < (int)callNodeVec.size() && "Missed Call?");
541 else if (mii.isCCInstr(bbMvec[i]->getOpCode()))
542 { // Add incoming/outgoing edges from/to preceding/later calls
543 SchedGraphNode* ccNode = this->getGraphNodeForInstr(bbMvec[i]);
545 for ( ; j <= lastCallNodeIdx; j++)
546 (void) new SchedGraphEdge(callNodeVec[j], ccNode,
547 MachineCCRegsRID, 0);
548 for ( ; j < (int) callNodeVec.size(); j++)
549 (void) new SchedGraphEdge(ccNode, callNodeVec[j],
550 MachineCCRegsRID, 0);
556 SchedGraph::addMachineRegEdges(RegToRefVecMap& regToRefVecMap,
557 const TargetMachine& target)
559 assert(bbVec.size() == 1 && "Only handling a single basic block here");
561 // This assumes that such hardwired registers are never allocated
562 // to any LLVM value (since register allocation happens later), i.e.,
563 // any uses or defs of this register have been made explicit!
564 // Also assumes that two registers with different numbers are
567 for (RegToRefVecMap::iterator I = regToRefVecMap.begin();
568 I != regToRefVecMap.end(); ++I)
570 int regNum = (*I).first;
571 RefVec& regRefVec = (*I).second;
573 // regRefVec is ordered by control flow order in the basic block
574 for (unsigned i=0; i < regRefVec.size(); ++i)
576 SchedGraphNode* node = regRefVec[i].first;
577 unsigned int opNum = regRefVec[i].second;
578 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
580 for (unsigned p=0; p < i; ++p)
582 SchedGraphNode* prevNode = regRefVec[p].first;
583 if (prevNode != node)
585 unsigned int prevOpNum = regRefVec[p].second;
587 prevNode->getMachineInstr()->operandIsDefined(prevOpNum);
590 new SchedGraphEdge(prevNode, node, regNum,
591 (prevIsDef)? SchedGraphEdge::OutputDep
592 : SchedGraphEdge::AntiDep);
594 new SchedGraphEdge(prevNode, node, regNum,
595 SchedGraphEdge::TrueDep);
604 CreateSSAEdge(SchedGraph* graph,
605 MachineInstr* defInstr,
606 SchedGraphNode* node,
609 // this instruction does define value `val'.
610 // if there is a node for it in the same graph, add an edge.
611 SchedGraphNode* defNode = graph->getGraphNodeForInstr(defInstr);
612 if (defNode != NULL && defNode != node)
613 (void) new SchedGraphEdge(defNode, node, val);
618 SchedGraph::addSSAEdge(SchedGraphNode* destNode,
619 const Instruction* defVMInstr,
620 const Value* defValue,
621 const TargetMachine& target)
623 // Phi instructions are the only ones that produce a value but don't get
624 // any non-dummy machine instructions. Return here as an optimization.
626 if (isa<PHINode>(defVMInstr))
629 // Now add the graph edge for the appropriate machine instruction(s).
630 // Note that multiple machine instructions generated for the
631 // def VM instruction may modify the register for the def value.
633 MachineCodeForVMInstr& defMvec = defVMInstr->getMachineInstrVec();
634 const MachineInstrInfo& mii = target.getInstrInfo();
636 for (unsigned i=0, N=defMvec.size(); i < N; i++)
638 bool edgeAddedForInstr = false;
640 // First check the explicit operands
641 for (int o=0, N=mii.getNumOperands(defMvec[i]->getOpCode()); o < N; o++)
643 const MachineOperand& defOp = defMvec[i]->getOperand(o);
646 && (defOp.getOperandType() == MachineOperand::MO_VirtualRegister
647 || defOp.getOperandType() == MachineOperand::MO_CCRegister)
648 && (defOp.getVRegValue() == defValue))
650 CreateSSAEdge(this, defMvec[i], destNode, defValue);
651 edgeAddedForInstr = true;
656 // Then check the implicit operands
657 if (! edgeAddedForInstr)
659 for (unsigned o=0, N=defMvec[i]->getNumImplicitRefs(); o < N; ++o)
660 if (defMvec[i]->implicitRefIsDefined(o) &&
661 defMvec[i]->getImplicitRef(o) == defValue)
663 CreateSSAEdge(this, defMvec[i], destNode, defValue);
664 edgeAddedForInstr = true;
673 SchedGraph::addEdgesForInstruction(const MachineInstr& minstr,
674 RegToRefVecMap& regToRefVecMap,
675 const TargetMachine& target)
677 SchedGraphNode* node = this->getGraphNodeForInstr(&minstr);
681 assert(node->getInstr() && "Should be no dummy nodes here!");
682 const Instruction* instr = node->getInstr();
684 // Add edges for all operands of the machine instruction.
685 // Also, record all machine register references to add reg. deps. later.
687 for (unsigned i=0, numOps=minstr.getNumOperands(); i < numOps; i++)
689 const MachineOperand& mop = minstr.getOperand(i);
691 // if this writes to a machine register other than the hardwired
692 // "zero" register, record the reference.
693 if (mop.getOperandType() == MachineOperand::MO_MachineRegister
694 && (mop.getMachineRegNum()
695 != (unsigned) target.getRegInfo().getZeroRegNum()))
697 regToRefVecMap[mop.getMachineRegNum()].push_back(make_pair(node, i));
700 // ignore all other def operands
701 if (minstr.operandIsDefined(i))
704 switch(mop.getOperandType())
706 case MachineOperand::MO_VirtualRegister:
707 case MachineOperand::MO_CCRegister:
708 if (const Instruction* srcI =
709 dyn_cast_or_null<Instruction>(mop.getVRegValue()))
711 if (srcI->getOpcode() == TMP_INSTRUCTION_OPCODE)
713 addSSAEdge(node, srcI, mop.getVRegValue(), target);
717 case MachineOperand::MO_MachineRegister:
720 case MachineOperand::MO_SignExtendedImmed:
721 case MachineOperand::MO_UnextendedImmed:
722 case MachineOperand::MO_PCRelativeDisp:
723 break; // nothing to do for immediate fields
726 assert(0 && "Unknown machine operand type in SchedGraph builder");
731 // Add edges for values implicitly used by the machine instruction.
732 // Examples include function arguments to a Call instructions or the return
733 // value of a Ret instruction.
735 for (unsigned i=0, N=minstr.getNumImplicitRefs(); i < N; ++i)
736 if (! minstr.implicitRefIsDefined(i))
737 if (const Instruction* srcI =
738 dyn_cast_or_null<Instruction>(minstr.getImplicitRef(i)))
740 if (srcI->getOpcode() == TMP_INSTRUCTION_OPCODE)
742 addSSAEdge(node, srcI, minstr.getImplicitRef(i), target);
748 SchedGraph::addNonSSAEdgesForValue(const Instruction* instr,
749 const TargetMachine& target)
751 if (isa<PHINode>(instr))
754 MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
755 const MachineInstrInfo& mii = target.getInstrInfo();
758 for (unsigned i=0, N=mvec.size(); i < N; i++)
759 for (int o=0, N = mii.getNumOperands(mvec[i]->getOpCode()); o < N; o++)
761 const MachineOperand& op = mvec[i]->getOperand(o);
763 if ((op.getOperandType() == MachineOperand::MO_VirtualRegister ||
764 op.getOperandType() == MachineOperand::MO_CCRegister)
765 && op.getVRegValue() == (Value*) instr)
767 // this operand is a definition or use of value `instr'
768 SchedGraphNode* node = this->getGraphNodeForInstr(mvec[i]);
769 assert(node && "No node for machine instruction in this BB?");
770 refVec.push_back(make_pair(node, o));
774 // refVec is ordered by control flow order of the machine instructions
775 for (unsigned i=0; i < refVec.size(); ++i)
777 SchedGraphNode* node = refVec[i].first;
778 unsigned int opNum = refVec[i].second;
779 bool isDef = node->getMachineInstr()->operandIsDefined(opNum);
782 // add output and/or anti deps to this definition
783 for (unsigned p=0; p < i; ++p)
785 SchedGraphNode* prevNode = refVec[p].first;
786 if (prevNode != node)
788 bool prevIsDef = prevNode->getMachineInstr()->
789 operandIsDefined(refVec[p].second);
790 new SchedGraphEdge(prevNode, node, SchedGraphEdge::DefUseDep,
791 (prevIsDef)? SchedGraphEdge::OutputDep
792 : SchedGraphEdge::AntiDep);
800 SchedGraph::buildNodesforVMInstr(const TargetMachine& target,
801 const Instruction* instr)
803 const MachineInstrInfo& mii = target.getInstrInfo();
804 const MachineCodeForVMInstr& mvec = instr->getMachineInstrVec();
805 for (unsigned i=0; i < mvec.size(); i++)
806 if (! mii.isDummyPhiInstr(mvec[i]->getOpCode()))
808 SchedGraphNode* node = new SchedGraphNode(getNumNodes(),
809 instr, mvec[i], target);
810 this->noteGraphNodeForInstr(mvec[i], node);
816 SchedGraph::buildGraph(const TargetMachine& target)
818 const MachineInstrInfo& mii = target.getInstrInfo();
819 const BasicBlock* bb = bbVec[0];
821 assert(bbVec.size() == 1 && "Only handling a single basic block here");
823 // Use these data structures to note all LLVM memory instructions.
824 // We use this to add memory dependence edges without a second full walk.
826 vector<const Instruction*> memVec;
828 // Use this data structures to note any uses or definitions of
829 // machine registers so we can add edges for those later without
830 // extra passes over the nodes.
831 // The vector holds an ordered list of references to the machine reg,
832 // ordered according to control-flow order. This only works for a
833 // single basic block, hence the assertion. Each reference is identified
834 // by the pair: <node, operand-number>.
836 RegToRefVecMap regToRefVecMap;
838 // Make a dummy root node. We'll add edges to the real roots later.
839 graphRoot = new SchedGraphNode(0, NULL, NULL, target);
840 graphLeaf = new SchedGraphNode(1, NULL, NULL, target);
842 //----------------------------------------------------------------
843 // First add nodes for all the machine instructions in the basic block
844 // because this greatly simplifies identifying which edges to add.
845 // Do this one VM instruction at a time since the SchedGraphNode needs that.
846 // Also, remember the load/store instructions to add memory deps later.
847 //----------------------------------------------------------------
849 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
851 const Instruction *instr = *II;
853 // Build graph nodes for this VM instruction
854 buildNodesforVMInstr(target, instr);
856 // Remember the load/store/call instructions to add memory deps later.
857 if (instr->getOpcode() == Instruction::Load ||
858 instr->getOpcode() == Instruction::Store ||
859 instr->getOpcode() == Instruction::Call)
860 memVec.push_back(instr);
863 //----------------------------------------------------------------
864 // Now add edges for the following (all are incoming edges except (4)):
865 // (1) operands of the machine instruction, including hidden operands
866 // (2) machine register dependences
867 // (3) memory load/store dependences
868 // (3) other resource dependences for the machine instruction, if any
869 // (4) output dependences when multiple machine instructions define the
870 // same value; all must have been generated from a single VM instrn
871 // (5) control dependences to branch instructions generated for the
872 // terminator instruction of the BB. Because of delay slots and
873 // 2-way conditional branches, multiple CD edges are needed
874 // (see addCDEdges for details).
875 // Also, note any uses or defs of machine registers.
877 //----------------------------------------------------------------
879 MachineCodeForBasicBlock& bbMvec = bb->getMachineInstrVec();
881 // First, add edges to the terminator instruction of the basic block.
882 this->addCDEdges(bb->getTerminator(), target);
884 // Then add memory dep edges: store->load, load->store, and store->store.
885 // Call instructions are treated as both load and store.
886 this->addMemEdges(memVec, target);
888 // Then add edges between call instructions and CC set/use instructions
889 this->addCallCCEdges(memVec, bbMvec, target);
891 // Then add other edges for all instructions in the block.
892 // Do this in machine code order and find all references to machine regs.
893 for (unsigned i=0, N=bbMvec.size(); i < N; i++)
894 addEdgesForInstruction(*bbMvec[i], regToRefVecMap, target);
896 // Since the code is no longer in SSA form, add output dep. edges
897 // between machine instructions that define the same Value, and anti-dep.
898 // edges from those to other machine instructions for the same VM instr.
899 // We assume that all machine instructions that define a value are
900 // generated from the VM instruction corresponding to that value.
902 for (BasicBlock::const_iterator II = bb->begin(); II != bb->end(); ++II)
904 const Instruction *instr = *II;
905 this->addNonSSAEdgesForValue(instr, target);
908 // Then add edges for dependences on machine registers
909 this->addMachineRegEdges(regToRefVecMap, target);
911 // Finally, add edges from the dummy root and to dummy leaf
912 this->addDummyEdges();
917 // class SchedGraphSet
921 SchedGraphSet::SchedGraphSet(const Method* _method,
922 const TargetMachine& target) :
925 buildGraphsForMethod(method, target);
930 SchedGraphSet::~SchedGraphSet()
932 // delete all the graphs
933 for (iterator I=begin(); I != end(); ++I)
939 SchedGraphSet::dump() const
941 cout << "======== Sched graphs for method `"
942 << (method->hasName()? method->getName() : "???")
943 << "' ========" << endl << endl;
945 for (const_iterator I=begin(); I != end(); ++I)
948 cout << endl << "====== End graphs for method `"
949 << (method->hasName()? method->getName() : "")
950 << "' ========" << endl << endl;
955 SchedGraphSet::buildGraphsForMethod(const Method *method,
956 const TargetMachine& target)
958 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
960 SchedGraph* graph = new SchedGraph(*BI, target);
961 this->noteGraphForBlock(*BI, graph);
968 operator<<(ostream& os, const SchedGraphEdge& edge)
970 os << "edge [" << edge.src->getNodeId() << "] -> ["
971 << edge.sink->getNodeId() << "] : ";
973 switch(edge.depType) {
974 case SchedGraphEdge::CtrlDep: os<< "Control Dep"; break;
975 case SchedGraphEdge::DefUseDep: os<< "Reg Value " << edge.val; break;
976 case SchedGraphEdge::MemoryDep: os<< "Mem Value " << edge.val; break;
977 case SchedGraphEdge::MachineRegister: os<< "Reg " <<edge.machineRegNum;break;
978 case SchedGraphEdge::MachineResource: os<<"Resource "<<edge.resourceId;break;
979 default: assert(0); break;
982 os << " : delay = " << edge.minDelay << endl;
988 operator<<(ostream& os, const SchedGraphNode& node)
991 os << "Node " << node.nodeId << " : "
992 << "latency = " << node.latency << endl;
996 if (node.getMachineInstr() == NULL)
997 os << "(Dummy node)" << endl;
1000 os << *node.getMachineInstr() << endl;
1003 os << node.inEdges.size() << " Incoming Edges:" << endl;
1004 for (unsigned i=0, N=node.inEdges.size(); i < N; i++)
1007 os << * node.inEdges[i];
1011 os << node.outEdges.size() << " Outgoing Edges:" << endl;
1012 for (unsigned i=0, N=node.outEdges.size(); i < N; i++)
1015 os << * node.outEdges[i];