1 //==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ScheduleDAGInstrs class, which implements
11 // scheduling for a MachineInstr-based dependency graph.
13 //===----------------------------------------------------------------------===//
15 #ifndef SCHEDULEDAGINSTRS_H
16 #define SCHEDULEDAGINSTRS_H
18 #include "llvm/CodeGen/MachineDominators.h"
19 #include "llvm/CodeGen/MachineLoopInfo.h"
20 #include "llvm/CodeGen/ScheduleDAG.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include "llvm/ADT/SmallSet.h"
27 class MachineLoopInfo;
28 class MachineDominatorTree;
30 /// LoopDependencies - This class analyzes loop-oriented register
31 /// dependencies, which are used to guide scheduling decisions.
32 /// For example, loop induction variable increments should be
33 /// scheduled as soon as possible after the variable's last use.
35 class LLVM_LIBRARY_VISIBILITY LoopDependencies {
36 const MachineLoopInfo &MLI;
37 const MachineDominatorTree &MDT;
40 typedef std::map<unsigned, std::pair<const MachineOperand *, unsigned> >
44 LoopDependencies(const MachineLoopInfo &mli,
45 const MachineDominatorTree &mdt) :
48 /// VisitLoop - Clear out any previous state and analyze the given loop.
50 void VisitLoop(const MachineLoop *Loop) {
51 assert(Deps.empty() && "stale loop dependencies");
53 MachineBasicBlock *Header = Loop->getHeader();
54 SmallSet<unsigned, 8> LoopLiveIns;
55 for (MachineBasicBlock::livein_iterator LI = Header->livein_begin(),
56 LE = Header->livein_end(); LI != LE; ++LI)
57 LoopLiveIns.insert(*LI);
59 const MachineDomTreeNode *Node = MDT.getNode(Header);
60 const MachineBasicBlock *MBB = Node->getBlock();
61 assert(Loop->contains(MBB) &&
62 "Loop does not contain header!");
63 VisitRegion(Node, MBB, Loop, LoopLiveIns);
67 void VisitRegion(const MachineDomTreeNode *Node,
68 const MachineBasicBlock *MBB,
69 const MachineLoop *Loop,
70 const SmallSet<unsigned, 8> &LoopLiveIns) {
72 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
74 const MachineInstr *MI = I;
75 if (MI->isDebugValue())
77 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
78 const MachineOperand &MO = MI->getOperand(i);
79 if (!MO.isReg() || !MO.isUse())
81 unsigned MOReg = MO.getReg();
82 if (LoopLiveIns.count(MOReg))
83 Deps.insert(std::make_pair(MOReg, std::make_pair(&MO, Count)));
85 ++Count; // Not every iteration due to dbg_value above.
88 const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
89 for (std::vector<MachineDomTreeNode*>::const_iterator I =
90 Children.begin(), E = Children.end(); I != E; ++I) {
91 const MachineDomTreeNode *ChildNode = *I;
92 MachineBasicBlock *ChildBlock = ChildNode->getBlock();
93 if (Loop->contains(ChildBlock))
94 VisitRegion(ChildNode, ChildBlock, Loop, LoopLiveIns);
99 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
101 class LLVM_LIBRARY_VISIBILITY ScheduleDAGInstrs : public ScheduleDAG {
102 const MachineLoopInfo &MLI;
103 const MachineDominatorTree &MDT;
104 const MachineFrameInfo *MFI;
105 const InstrItineraryData *InstrItins;
107 /// Defs, Uses - Remember where defs and uses of each physical register
108 /// are as we iterate upward through the instructions. This is allocated
109 /// here instead of inside BuildSchedGraph to avoid the need for it to be
110 /// initialized and destructed for each block.
111 std::vector<std::vector<SUnit *> > Defs;
112 std::vector<std::vector<SUnit *> > Uses;
114 /// PendingLoads - Remember where unknown loads are after the most recent
115 /// unknown store, as we iterate. As with Defs and Uses, this is here
116 /// to minimize construction/destruction.
117 std::vector<SUnit *> PendingLoads;
119 /// LoopRegs - Track which registers are used for loop-carried dependencies.
121 LoopDependencies LoopRegs;
125 /// DbgValues - Remember instruction that preceeds DBG_VALUE.
126 typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
128 DbgValueVector DbgValues;
129 MachineInstr *FirstDbgValue;
132 MachineBasicBlock::iterator Begin; // The beginning of the range to
133 // be scheduled. The range extends
135 unsigned InsertPosIndex; // The index in BB of InsertPos.
137 explicit ScheduleDAGInstrs(MachineFunction &mf,
138 const MachineLoopInfo &mli,
139 const MachineDominatorTree &mdt);
141 virtual ~ScheduleDAGInstrs() {}
143 /// NewSUnit - Creates a new SUnit and return a ptr to it.
145 SUnit *NewSUnit(MachineInstr *MI) {
147 const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
149 SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
150 assert((Addr == 0 || Addr == &SUnits[0]) &&
151 "SUnits std::vector reallocated on the fly!");
152 SUnits.back().OrigNode = &SUnits.back();
153 return &SUnits.back();
156 /// Run - perform scheduling.
158 void Run(MachineBasicBlock *bb,
159 MachineBasicBlock::iterator begin,
160 MachineBasicBlock::iterator end,
163 /// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
165 virtual void BuildSchedGraph(AliasAnalysis *AA);
167 /// AddSchedBarrierDeps - Add dependencies from instructions in the current
168 /// list of instructions being scheduled to scheduling barrier. We want to
169 /// make sure instructions which define registers that are either used by
170 /// the terminator or are live-out are properly scheduled. This is
171 /// especially important when the definition latency of the return value(s)
172 /// are too high to be hidden by the branch or when the liveout registers
173 /// used by instructions in the fallthrough block.
174 void AddSchedBarrierDeps();
176 /// ComputeLatency - Compute node latency.
178 virtual void ComputeLatency(SUnit *SU);
180 /// ComputeOperandLatency - Override dependence edge latency using
181 /// operand use/def information
183 virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
186 virtual MachineBasicBlock *EmitSchedule();
188 /// StartBlock - Prepare to perform scheduling in the given block.
190 virtual void StartBlock(MachineBasicBlock *BB);
192 /// Schedule - Order nodes according to selected style, filling
193 /// in the Sequence member.
195 virtual void Schedule() = 0;
197 /// FinishBlock - Clean up after scheduling in the given block.
199 virtual void FinishBlock();
201 virtual void dumpNode(const SUnit *SU) const;
203 virtual std::string getGraphNodeLabel(const SUnit *SU) const;