blob: fa42d393a3be3ee5d742c18cd94ac1fcdc533342 [file] [log] [blame]
Dan Gohmana629b482008-12-08 17:50:35 +00001//===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
Dan Gohman343f0c02008-11-19 23:18:57 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Dan Gohmana629b482008-12-08 17:50:35 +000010// This implements the ScheduleDAGInstrs class, which implements re-scheduling
11// of MachineInstrs.
Dan Gohman343f0c02008-11-19 23:18:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "sched-instrs"
16#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000017#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000018#include "llvm/Target/TargetMachine.h"
19#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetRegisterInfo.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000023#include <map>
Dan Gohman343f0c02008-11-19 23:18:57 +000024using namespace llvm;
25
26ScheduleDAGInstrs::ScheduleDAGInstrs(MachineBasicBlock *bb,
27 const TargetMachine &tm)
28 : ScheduleDAG(0, bb, tm) {}
29
30void ScheduleDAGInstrs::BuildSchedUnits() {
31 SUnits.clear();
32 SUnits.reserve(BB->size());
33
Dan Gohman6a9041e2008-12-04 01:35:46 +000034 // We build scheduling units by walking a block's instruction list from bottom
35 // to top.
36
37 // Remember where defs and uses of each physical register are as we procede.
Dan Gohman343f0c02008-11-19 23:18:57 +000038 SUnit *Defs[TargetRegisterInfo::FirstVirtualRegister] = {};
39 std::vector<SUnit *> Uses[TargetRegisterInfo::FirstVirtualRegister] = {};
Dan Gohman6a9041e2008-12-04 01:35:46 +000040
41 // Remember where unknown loads are after the most recent unknown store
42 // as we procede.
43 std::vector<SUnit *> PendingLoads;
44
45 // Remember where a generic side-effecting instruction is as we procede. If
46 // ChainMMO is null, this is assumed to have arbitrary side-effects. If
47 // ChainMMO is non-null, then Chain makes only a single memory reference.
48 SUnit *Chain = 0;
49 MachineMemOperand *ChainMMO = 0;
50
51 // Memory references to specific known memory locations are tracked so that
52 // they can be given more precise dependencies.
53 std::map<const Value *, SUnit *> MemDefs;
54 std::map<const Value *, std::vector<SUnit *> > MemUses;
55
56 // Terminators can perform control transfers, we we need to make sure that
57 // all the work of the block is done before the terminator.
58 SUnit *Terminator = 0;
Dan Gohman343f0c02008-11-19 23:18:57 +000059
60 for (MachineBasicBlock::iterator MII = BB->end(), MIE = BB->begin();
61 MII != MIE; --MII) {
62 MachineInstr *MI = prior(MII);
63 SUnit *SU = NewSUnit(MI);
64
Dan Gohman54e4c362008-12-09 22:54:47 +000065 // Assign the Latency field of SU using target-provided information.
66 ComputeLatency(SU);
67
Dan Gohman6a9041e2008-12-04 01:35:46 +000068 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +000069 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
70 const MachineOperand &MO = MI->getOperand(j);
71 if (!MO.isReg()) continue;
72 unsigned Reg = MO.getReg();
73 if (Reg == 0) continue;
74
75 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
76 std::vector<SUnit *> &UseList = Uses[Reg];
77 SUnit *&Def = Defs[Reg];
Dan Gohmanfc626b62008-11-21 19:16:58 +000078 // Optionally add output and anti dependencies.
Dan Gohman54e4c362008-12-09 22:54:47 +000079 // TODO: Using a latency of 1 here assumes there's no cost for
80 // reusing registers.
81 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
Dan Gohman343f0c02008-11-19 23:18:57 +000082 if (Def && Def != SU)
Dan Gohman54e4c362008-12-09 22:54:47 +000083 Def->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
Dan Gohman343f0c02008-11-19 23:18:57 +000084 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
85 SUnit *&Def = Defs[*Alias];
86 if (Def && Def != SU)
Dan Gohman54e4c362008-12-09 22:54:47 +000087 Def->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
Dan Gohman343f0c02008-11-19 23:18:57 +000088 }
89
90 if (MO.isDef()) {
91 // Add any data dependencies.
92 for (unsigned i = 0, e = UseList.size(); i != e; ++i)
93 if (UseList[i] != SU)
Dan Gohman54e4c362008-12-09 22:54:47 +000094 UseList[i]->addPred(SDep(SU, SDep::Data, SU->Latency, Reg));
Dan Gohman343f0c02008-11-19 23:18:57 +000095 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
96 std::vector<SUnit *> &UseList = Uses[*Alias];
97 for (unsigned i = 0, e = UseList.size(); i != e; ++i)
98 if (UseList[i] != SU)
Dan Gohman54e4c362008-12-09 22:54:47 +000099 UseList[i]->addPred(SDep(SU, SDep::Data, SU->Latency, *Alias));
Dan Gohman343f0c02008-11-19 23:18:57 +0000100 }
101
102 UseList.clear();
103 Def = SU;
104 } else {
105 UseList.push_back(SU);
106 }
107 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000108
109 // Add chain dependencies.
110 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
111 // after stack slots are lowered to actual addresses.
112 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
113 // produce more precise dependence information.
114 const TargetInstrDesc &TID = MI->getDesc();
115 if (TID.isCall() || TID.isReturn() || TID.isBranch() ||
116 TID.hasUnmodeledSideEffects()) {
117 new_chain:
Dan Gohmana629b482008-12-08 17:50:35 +0000118 // This is the conservative case. Add dependencies on all memory
119 // references.
Dan Gohman343f0c02008-11-19 23:18:57 +0000120 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000121 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000122 Chain = SU;
Dan Gohman343f0c02008-11-19 23:18:57 +0000123 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
Dan Gohman54e4c362008-12-09 22:54:47 +0000124 PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman343f0c02008-11-19 23:18:57 +0000125 PendingLoads.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000126 for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
127 E = MemDefs.end(); I != E; ++I) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000128 I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000129 I->second = SU;
130 }
131 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
132 MemUses.begin(), E = MemUses.end(); I != E; ++I) {
133 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000134 I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000135 I->second.clear();
136 }
137 // See if it is known to just have a single memory reference.
138 MachineInstr *ChainMI = Chain->getInstr();
139 const TargetInstrDesc &ChainTID = ChainMI->getDesc();
140 if (!ChainTID.isCall() && !ChainTID.isReturn() && !ChainTID.isBranch() &&
141 !ChainTID.hasUnmodeledSideEffects() &&
142 ChainMI->hasOneMemOperand() &&
143 !ChainMI->memoperands_begin()->isVolatile() &&
144 ChainMI->memoperands_begin()->getValue())
145 // We know that the Chain accesses one specific memory location.
146 ChainMMO = &*ChainMI->memoperands_begin();
147 else
148 // Unknown memory accesses. Assume the worst.
149 ChainMMO = 0;
150 } else if (TID.mayStore()) {
151 if (MI->hasOneMemOperand() &&
152 MI->memoperands_begin()->getValue() &&
153 !MI->memoperands_begin()->isVolatile() &&
154 isa<PseudoSourceValue>(MI->memoperands_begin()->getValue())) {
155 // A store to a specific PseudoSourceValue. Add precise dependencies.
156 const Value *V = MI->memoperands_begin()->getValue();
157 // Handle the def in MemDefs, if there is one.
158 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
159 if (I != MemDefs.end()) {
Dan Gohman54e4c362008-12-09 22:54:47 +0000160 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
161 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000162 I->second = SU;
163 } else {
164 MemDefs[V] = SU;
165 }
166 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000167 std::map<const Value *, std::vector<SUnit *> >::iterator J =
168 MemUses.find(V);
Dan Gohman6a9041e2008-12-04 01:35:46 +0000169 if (J != MemUses.end()) {
170 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
Dan Gohman54e4c362008-12-09 22:54:47 +0000171 J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
172 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000173 J->second.clear();
174 }
175 // Add a general dependence too, if needed.
176 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000177 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000178 } else
179 // Treat all other stores conservatively.
180 goto new_chain;
181 } else if (TID.mayLoad()) {
182 if (TII->isInvariantLoad(MI)) {
183 // Invariant load, no chain dependencies needed!
184 } else if (MI->hasOneMemOperand() &&
185 MI->memoperands_begin()->getValue() &&
186 !MI->memoperands_begin()->isVolatile() &&
187 isa<PseudoSourceValue>(MI->memoperands_begin()->getValue())) {
188 // A load from a specific PseudoSourceValue. Add precise dependencies.
189 const Value *V = MI->memoperands_begin()->getValue();
190 std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
191 if (I != MemDefs.end())
Dan Gohman54e4c362008-12-09 22:54:47 +0000192 I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
193 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000194 MemUses[V].push_back(SU);
195
196 // Add a general dependence too, if needed.
197 if (Chain && (!ChainMMO ||
198 (ChainMMO->isStore() || ChainMMO->isVolatile())))
Dan Gohman54e4c362008-12-09 22:54:47 +0000199 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000200 } else if (MI->hasVolatileMemoryRef()) {
201 // Treat volatile loads conservatively. Note that this includes
202 // cases where memoperand information is unavailable.
203 goto new_chain;
204 } else {
205 // A normal load. Just depend on the general chain.
206 if (Chain)
Dan Gohman54e4c362008-12-09 22:54:47 +0000207 Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000208 PendingLoads.push_back(SU);
209 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000210 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000211
Dan Gohmana629b482008-12-08 17:50:35 +0000212 // Add chain edges from the terminator to ensure that all the work of the
213 // block is completed before any control transfers.
Dan Gohman343f0c02008-11-19 23:18:57 +0000214 if (Terminator && SU->Succs.empty())
Dan Gohman54e4c362008-12-09 22:54:47 +0000215 Terminator->addPred(SDep(SU, SDep::Order, SU->Latency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000216 if (TID.isTerminator() || MI->isLabel())
Dan Gohman343f0c02008-11-19 23:18:57 +0000217 Terminator = SU;
218 }
219}
220
Dan Gohmanc8c28272008-11-21 00:12:10 +0000221void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
222 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
223
224 // Compute the latency for the node. We use the sum of the latencies for
225 // all nodes flagged together into this SUnit.
226 SU->Latency =
227 InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000228
229 // Simplistic target-independent heuristic: assume that loads take
230 // extra time.
231 if (InstrItins.isEmpty())
232 if (SU->getInstr()->getDesc().mayLoad())
233 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000234}
235
Dan Gohman343f0c02008-11-19 23:18:57 +0000236void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
237 SU->getInstr()->dump();
238}
239
240std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
241 std::string s;
242 raw_string_ostream oss(s);
243 SU->getInstr()->print(oss);
244 return oss.str();
245}
246
247// EmitSchedule - Emit the machine code in scheduled order.
248MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
249 // For MachineInstr-based scheduling, we're rescheduling the instructions in
250 // the block, so start by removing them from the block.
251 while (!BB->empty())
252 BB->remove(BB->begin());
253
254 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
255 SUnit *SU = Sequence[i];
256 if (!SU) {
257 // Null SUnit* is a noop.
258 EmitNoop();
259 continue;
260 }
261
262 BB->push_back(SU->getInstr());
263 }
264
265 return BB;
266}