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/ADT/SmallSet.h"
19 #include "llvm/CodeGen/MachineDominators.h"
20 #include "llvm/CodeGen/MachineLoopInfo.h"
21 #include "llvm/CodeGen/ScheduleDAG.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Target/TargetRegisterInfo.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 VISIBILITY_HIDDEN 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
) {
52 MachineBasicBlock
*Header
= Loop
->getHeader();
53 SmallSet
<unsigned, 8> LoopLiveIns
;
54 for (MachineBasicBlock::livein_iterator LI
= Header
->livein_begin(),
55 LE
= Header
->livein_end(); LI
!= LE
; ++LI
)
56 LoopLiveIns
.insert(*LI
);
58 const MachineDomTreeNode
*Node
= MDT
.getNode(Header
);
59 const MachineBasicBlock
*MBB
= Node
->getBlock();
60 assert(Loop
->contains(MBB
) &&
61 "Loop does not contain header!");
62 VisitRegion(Node
, MBB
, Loop
, LoopLiveIns
);
66 void VisitRegion(const MachineDomTreeNode
*Node
,
67 const MachineBasicBlock
*MBB
,
68 const MachineLoop
*Loop
,
69 const SmallSet
<unsigned, 8> &LoopLiveIns
) {
71 for (MachineBasicBlock::const_iterator I
= MBB
->begin(), E
= MBB
->end();
72 I
!= E
; ++I
, ++Count
) {
73 const MachineInstr
*MI
= I
;
74 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
75 const MachineOperand
&MO
= MI
->getOperand(i
);
76 if (!MO
.isReg() || !MO
.isUse())
78 unsigned MOReg
= MO
.getReg();
79 if (LoopLiveIns
.count(MOReg
))
80 Deps
.insert(std::make_pair(MOReg
, std::make_pair(&MO
, Count
)));
84 const std::vector
<MachineDomTreeNode
*> &Children
= Node
->getChildren();
85 for (std::vector
<MachineDomTreeNode
*>::const_iterator I
=
86 Children
.begin(), E
= Children
.end(); I
!= E
; ++I
) {
87 const MachineDomTreeNode
*ChildNode
= *I
;
88 MachineBasicBlock
*ChildBlock
= ChildNode
->getBlock();
89 if (Loop
->contains(ChildBlock
))
90 VisitRegion(ChildNode
, ChildBlock
, Loop
, LoopLiveIns
);
95 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
97 class VISIBILITY_HIDDEN ScheduleDAGInstrs
: public ScheduleDAG
{
98 const MachineLoopInfo
&MLI
;
99 const MachineDominatorTree
&MDT
;
101 /// Defs, Uses - Remember where defs and uses of each physical register
102 /// are as we iterate upward through the instructions. This is allocated
103 /// here instead of inside BuildSchedGraph to avoid the need for it to be
104 /// initialized and destructed for each block.
105 std::vector
<SUnit
*> Defs
[TargetRegisterInfo::FirstVirtualRegister
];
106 std::vector
<SUnit
*> Uses
[TargetRegisterInfo::FirstVirtualRegister
];
108 /// PendingLoads - Remember where unknown loads are after the most recent
109 /// unknown store, as we iterate. As with Defs and Uses, this is here
110 /// to minimize construction/destruction.
111 std::vector
<SUnit
*> PendingLoads
;
113 /// LoopRegs - Track which registers are used for loop-carried dependencies.
115 LoopDependencies LoopRegs
;
117 /// LoopLiveInRegs - Track which regs are live into a loop, to help guide
118 /// back-edge-aware scheduling.
120 SmallSet
<unsigned, 8> LoopLiveInRegs
;
123 MachineBasicBlock
*BB
; // Current basic block
124 MachineBasicBlock::iterator Begin
; // The beginning of the range to
125 // be scheduled. The range extends
127 unsigned InsertPosIndex
; // The index in BB of InsertPos.
129 explicit ScheduleDAGInstrs(MachineFunction
&mf
,
130 const MachineLoopInfo
&mli
,
131 const MachineDominatorTree
&mdt
);
133 virtual ~ScheduleDAGInstrs() {}
135 /// NewSUnit - Creates a new SUnit and return a ptr to it.
137 SUnit
*NewSUnit(MachineInstr
*MI
) {
139 const SUnit
*Addr
= SUnits
.empty() ? 0 : &SUnits
[0];
141 SUnits
.push_back(SUnit(MI
, (unsigned)SUnits
.size()));
142 assert((Addr
== 0 || Addr
== &SUnits
[0]) &&
143 "SUnits std::vector reallocated on the fly!");
144 SUnits
.back().OrigNode
= &SUnits
.back();
145 return &SUnits
.back();
148 /// Run - perform scheduling.
150 void Run(MachineBasicBlock
*bb
,
151 MachineBasicBlock::iterator begin
,
152 MachineBasicBlock::iterator end
,
155 /// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
157 virtual void BuildSchedGraph();
159 /// ComputeLatency - Compute node latency.
161 virtual void ComputeLatency(SUnit
*SU
);
163 virtual MachineBasicBlock
*EmitSchedule();
165 /// StartBlock - Prepare to perform scheduling in the given block.
167 virtual void StartBlock(MachineBasicBlock
*BB
);
169 /// Schedule - Order nodes according to selected style, filling
170 /// in the Sequence member.
172 virtual void Schedule() = 0;
174 /// FinishBlock - Clean up after scheduling in the given block.
176 virtual void FinishBlock();
178 virtual void dumpNode(const SUnit
*SU
) const;
180 virtual std::string
getGraphNodeLabel(const SUnit
*SU
) const;