blob: e532ade5f683fa5d6e7db3ffd4fc01c56a870058 [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"
Dan Gohman6dc75fe2009-02-06 17:12:10 +000016#include "ScheduleDAGInstrs.h"
Dan Gohman8906f952009-07-17 20:58:59 +000017#include "llvm/Operator.h"
Dan Gohman3311a1f2009-01-30 02:49:14 +000018#include "llvm/Analysis/AliasAnalysis.h"
Dan Gohman3f237442008-12-16 03:25:46 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000020#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohman3f237442008-12-16 03:25:46 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000022#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman3f237442008-12-16 03:25:46 +000026#include "llvm/Target/TargetSubtarget.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000029#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000030using namespace llvm;
31
Dan Gohman79ce2762009-01-15 19:20:50 +000032ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000033 const MachineLoopInfo &mli,
34 const MachineDominatorTree &mdt)
Evan Cheng38bdfc62009-10-18 19:58:47 +000035 : ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {
36 MFI = mf.getFrameInfo();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +000037 DbgValueVec.clear();
Evan Cheng38bdfc62009-10-18 19:58:47 +000038}
Dan Gohman343f0c02008-11-19 23:18:57 +000039
Dan Gohman47ac0f02009-02-11 04:27:20 +000040/// Run - perform scheduling.
41///
42void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
43 MachineBasicBlock::iterator begin,
44 MachineBasicBlock::iterator end,
45 unsigned endcount) {
46 BB = bb;
47 Begin = begin;
48 InsertPosIndex = endcount;
49
50 ScheduleDAG::Run(bb, end);
51}
52
Dan Gohman3311a1f2009-01-30 02:49:14 +000053/// getUnderlyingObjectFromInt - This is the function that does the work of
54/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
55static const Value *getUnderlyingObjectFromInt(const Value *V) {
56 do {
Dan Gohman8906f952009-07-17 20:58:59 +000057 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000058 // If we find a ptrtoint, we can transfer control back to the
59 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000060 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000061 return U->getOperand(0);
62 // If we find an add of a constant or a multiplied value, it's
63 // likely that the other operand will lead us to the base
64 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000065 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000066 // because our callers only care when the result is an
67 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000068 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000069 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000070 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000071 return V;
72 V = U->getOperand(0);
73 } else {
74 return V;
75 }
Duncan Sands1df98592010-02-16 11:11:14 +000076 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000077 } while (1);
78}
79
80/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
81/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
82static const Value *getUnderlyingObject(const Value *V) {
83 // First just call Value::getUnderlyingObject to let it do what it does.
84 do {
85 V = V->getUnderlyingObject();
86 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000087 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000088 break;
89 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
90 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +000091 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +000092 break;
93 V = O;
94 } while (1);
95 return V;
96}
97
98/// getUnderlyingObjectForInstr - If this machine instr has memory reference
99/// information and it can be tracked to a normal reference to a known
100/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000101static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000102 const MachineFrameInfo *MFI,
103 bool &MayAlias) {
104 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000105 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000106 !(*MI->memoperands_begin())->getValue() ||
107 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000108 return 0;
109
Dan Gohmanc76909a2009-09-25 20:36:54 +0000110 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000111 if (!V)
112 return 0;
113
114 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000115 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
116 // For now, ignore PseudoSourceValues which may alias LLVM IR values
117 // because the code that uses this function has no way to cope with
118 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000119 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000120 return 0;
David Goodwin980d4942009-11-09 19:22:17 +0000121
122 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000123 return V;
124 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000125
Evan Chengff89dcb2009-10-18 18:16:27 +0000126 if (isIdentifiedObject(V))
127 return V;
128
129 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000130}
131
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000132void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
133 if (MachineLoop *ML = MLI.getLoopFor(BB))
134 if (BB == ML->getLoopLatch()) {
135 MachineBasicBlock *Header = ML->getHeader();
136 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
137 E = Header->livein_end(); I != E; ++I)
138 LoopLiveInRegs.insert(*I);
139 LoopRegs.VisitLoop(ML);
140 }
141}
142
Dan Gohmana70dca12009-10-09 23:27:56 +0000143void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000144 // We'll be allocating one SUnit for each instruction, plus one for
145 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000146 SUnits.reserve(BB->size());
147
Dan Gohman6a9041e2008-12-04 01:35:46 +0000148 // We build scheduling units by walking a block's instruction list from bottom
149 // to top.
150
David Goodwin980d4942009-11-09 19:22:17 +0000151 // Remember where a generic side-effecting instruction is as we procede.
152 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000153
David Goodwin980d4942009-11-09 19:22:17 +0000154 // Memory references to specific known memory locations are tracked
155 // so that they can be given more precise dependencies. We track
156 // separately the known memory locations that may alias and those
157 // that are known not to alias
158 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
159 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000160
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000161 // Keep track of dangling debug references to registers.
162 std::pair<MachineInstr*, unsigned>
163 DanglingDebugValue[TargetRegisterInfo::FirstVirtualRegister];
164
Dan Gohman3f237442008-12-16 03:25:46 +0000165 // Check to see if the scheduler cares about latencies.
166 bool UnitLatencies = ForceUnitLatencies();
167
Dan Gohman8749b612008-12-16 03:35:01 +0000168 // Ask the target if address-backscheduling is desirable, and if so how much.
David Goodwin71046162009-08-13 16:05:04 +0000169 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
170 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Dan Gohman8749b612008-12-16 03:35:01 +0000171
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000172 // Remove any stale debug info; sometimes BuildSchedGraph is called again
173 // without emitting the info from the previous call.
174 DbgValueVec.clear();
175 std::memset(DanglingDebugValue, 0, sizeof(DanglingDebugValue));
176
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000177 // Walk the list of instructions, from bottom moving up.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000178 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000179 MII != MIE; --MII) {
180 MachineInstr *MI = prior(MII);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000181 // DBG_VALUE does not have SUnit's built, so just remember these for later
182 // reinsertion.
183 if (MI->isDebugValue()) {
184 if (MI->getNumOperands()==3 && MI->getOperand(0).isReg() &&
185 MI->getOperand(0).getReg())
186 DanglingDebugValue[MI->getOperand(0).getReg()] =
187 std::make_pair(MI, DbgValueVec.size());
188 DbgValueVec.push_back(MI);
189 continue;
190 }
Dan Gohman3f237442008-12-16 03:25:46 +0000191 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000192 assert(!TID.isTerminator() && !MI->isLabel() &&
193 "Cannot schedule terminators or labels!");
194 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000195 SUnit *SU = NewSUnit(MI);
196
Dan Gohman54e4c362008-12-09 22:54:47 +0000197 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000198 if (UnitLatencies)
199 SU->Latency = 1;
200 else
201 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000202
Dan Gohman6a9041e2008-12-04 01:35:46 +0000203 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000204 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
205 const MachineOperand &MO = MI->getOperand(j);
206 if (!MO.isReg()) continue;
207 unsigned Reg = MO.getReg();
208 if (Reg == 0) continue;
209
210 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000211
212 if (MO.isDef() && DanglingDebugValue[Reg].first!=0) {
213 SU->setDbgInstr(DanglingDebugValue[Reg].first);
214 DbgValueVec[DanglingDebugValue[Reg].second] = 0;
215 DanglingDebugValue[Reg] = std::make_pair((MachineInstr*)0, 0);
216 }
217
Dan Gohman343f0c02008-11-19 23:18:57 +0000218 std::vector<SUnit *> &UseList = Uses[Reg];
Dan Gohman3f237442008-12-16 03:25:46 +0000219 std::vector<SUnit *> &DefList = Defs[Reg];
David Goodwind94a4e52009-08-10 15:55:25 +0000220 // Optionally add output and anti dependencies. For anti
221 // dependencies we use a latency of 0 because for a multi-issue
222 // target we want to allow the defining instruction to issue
223 // in the same cycle as the using instruction.
224 // TODO: Using a latency of 1 here for output dependencies assumes
225 // there's no cost for reusing registers.
Dan Gohman54e4c362008-12-09 22:54:47 +0000226 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
David Goodwind94a4e52009-08-10 15:55:25 +0000227 unsigned AOLatency = (Kind == SDep::Anti) ? 0 : 1;
Dan Gohman3f237442008-12-16 03:25:46 +0000228 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
229 SUnit *DefSU = DefList[i];
230 if (DefSU != SU &&
231 (Kind != SDep::Output || !MO.isDead() ||
232 !DefSU->getInstr()->registerDefIsDead(Reg)))
David Goodwind94a4e52009-08-10 15:55:25 +0000233 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000234 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000235 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Dan Gohman3f237442008-12-16 03:25:46 +0000236 std::vector<SUnit *> &DefList = Defs[*Alias];
237 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
238 SUnit *DefSU = DefList[i];
239 if (DefSU != SU &&
240 (Kind != SDep::Output || !MO.isDead() ||
Dan Gohman91203cf2009-10-26 18:26:18 +0000241 !DefSU->getInstr()->registerDefIsDead(*Alias)))
David Goodwind94a4e52009-08-10 15:55:25 +0000242 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/ *Alias));
Dan Gohman3f237442008-12-16 03:25:46 +0000243 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000244 }
245
246 if (MO.isDef()) {
247 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000248 unsigned DataLatency = SU->Latency;
249 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
250 SUnit *UseSU = UseList[i];
251 if (UseSU != SU) {
Dan Gohman8749b612008-12-16 03:35:01 +0000252 unsigned LDataLatency = DataLatency;
253 // Optionally add in a special extra latency for nodes that
254 // feed addresses.
255 // TODO: Do this for register aliases too.
David Goodwindc4bdcd2009-08-19 16:08:58 +0000256 // TODO: Perhaps we should get rid of
257 // SpecialAddressLatency and just move this into
258 // adjustSchedDependency for the targets that care about
259 // it.
Dan Gohman8749b612008-12-16 03:35:01 +0000260 if (SpecialAddressLatency != 0 && !UnitLatencies) {
261 MachineInstr *UseMI = UseSU->getInstr();
262 const TargetInstrDesc &UseTID = UseMI->getDesc();
263 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
264 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
265 if ((UseTID.mayLoad() || UseTID.mayStore()) &&
266 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
267 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
268 LDataLatency += SpecialAddressLatency;
269 }
David Goodwindc4bdcd2009-08-19 16:08:58 +0000270 // Adjust the dependence latency using operand def/use
271 // information (if any), and then allow the target to
272 // perform its own adjustments.
David Goodwin71046162009-08-13 16:05:04 +0000273 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000274 if (!UnitLatencies) {
275 ComputeOperandLatency(SU, UseSU, (SDep &)dep);
276 ST.adjustSchedDependency(SU, UseSU, (SDep &)dep);
277 }
David Goodwin71046162009-08-13 16:05:04 +0000278 UseSU->addPred(dep);
Dan Gohman3f237442008-12-16 03:25:46 +0000279 }
280 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000281 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
282 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000283 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
284 SUnit *UseSU = UseList[i];
David Goodwin71046162009-08-13 16:05:04 +0000285 if (UseSU != SU) {
286 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000287 if (!UnitLatencies) {
288 ComputeOperandLatency(SU, UseSU, (SDep &)dep);
289 ST.adjustSchedDependency(SU, UseSU, (SDep &)dep);
290 }
David Goodwin71046162009-08-13 16:05:04 +0000291 UseSU->addPred(dep);
292 }
Dan Gohman3f237442008-12-16 03:25:46 +0000293 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000294 }
295
Dan Gohman8749b612008-12-16 03:35:01 +0000296 // If a def is going to wrap back around to the top of the loop,
297 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000298 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000299 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
300 if (I != LoopRegs.Deps.end()) {
301 const MachineOperand *UseMO = I->second.first;
302 unsigned Count = I->second.second;
303 const MachineInstr *UseMI = UseMO->getParent();
304 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
305 const TargetInstrDesc &UseTID = UseMI->getDesc();
306 // TODO: If we knew the total depth of the region here, we could
307 // handle the case where the whole loop is inside the region but
308 // is large enough that the isScheduleHigh trick isn't needed.
309 if (UseMOIdx < UseTID.getNumOperands()) {
310 // Currently, we only support scheduling regions consisting of
311 // single basic blocks. Check to see if the instruction is in
312 // the same region by checking to see if it has the same parent.
313 if (UseMI->getParent() != MI->getParent()) {
314 unsigned Latency = SU->Latency;
315 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
316 Latency += SpecialAddressLatency;
317 // This is a wild guess as to the portion of the latency which
318 // will be overlapped by work done outside the current
319 // scheduling region.
320 Latency -= std::min(Latency, Count);
321 // Add the artifical edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000322 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
323 /*Reg=*/0, /*isNormalMemory=*/false,
324 /*isMustAlias=*/false,
325 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000326 } else if (SpecialAddressLatency > 0 &&
327 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
328 // The entire loop body is within the current scheduling region
329 // and the latency of this operation is assumed to be greater
330 // than the latency of the loop.
331 // TODO: Recursively mark data-edge predecessors as
332 // isScheduleHigh too.
333 SU->isScheduleHigh = true;
334 }
335 }
336 LoopRegs.Deps.erase(I);
337 }
338 }
339
Dan Gohman343f0c02008-11-19 23:18:57 +0000340 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000341 if (!MO.isDead())
342 DefList.clear();
343 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000344 } else {
345 UseList.push_back(SU);
346 }
347 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000348
349 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000350 // Chain dependencies used to enforce memory order should have
351 // latency of 0 (except for true dependency of Store followed by
352 // aliased Load... we estimate that with a single cycle of latency
353 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000354 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
355 // after stack slots are lowered to actual addresses.
356 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
357 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000358#define STORE_LOAD_LATENCY 1
359 unsigned TrueMemOrderLatency = 0;
David Goodwin980d4942009-11-09 19:22:17 +0000360 if (TID.isCall() || TID.hasUnmodeledSideEffects() ||
361 (MI->hasVolatileMemoryRef() &&
362 (!TID.mayLoad() || !MI->isInvariantLoad(AA)))) {
363 // Be conservative with these and add dependencies on all memory
364 // references, even those that are known to not alias.
365 for (std::map<const Value *, SUnit *>::iterator I =
366 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000367 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000368 }
369 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000370 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000371 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000372 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000373 }
David Goodwin980d4942009-11-09 19:22:17 +0000374 NonAliasMemDefs.clear();
375 NonAliasMemUses.clear();
376 // Add SU to the barrier chain.
377 if (BarrierChain)
378 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
379 BarrierChain = SU;
380
381 // fall-through
382 new_alias_chain:
383 // Chain all possibly aliasing memory references though SU.
384 if (AliasChain)
385 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
386 AliasChain = SU;
387 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
388 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
389 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
390 E = AliasMemDefs.end(); I != E; ++I) {
391 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
392 }
393 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
394 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
395 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
396 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
397 }
398 PendingLoads.clear();
399 AliasMemDefs.clear();
400 AliasMemUses.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000401 } else if (TID.mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000402 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000403 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000404 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000405 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000406 // Record the def in MemDefs, first adding a dep if there is
407 // an existing def.
408 std::map<const Value *, SUnit *>::iterator I =
409 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
410 std::map<const Value *, SUnit *>::iterator IE =
411 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
412 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000413 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000414 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000415 I->second = SU;
416 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000417 if (MayAlias)
418 AliasMemDefs[V] = SU;
419 else
420 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000421 }
422 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000423 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000424 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
425 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
426 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
427 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000428 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000429 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
430 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000431 J->second.clear();
432 }
David Goodwina9e61072009-11-03 20:15:00 +0000433 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000434 // Add dependencies from all the PendingLoads, i.e. loads
435 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000436 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
437 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000438 // Add dependence on alias chain, if needed.
439 if (AliasChain)
440 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000441 }
David Goodwin980d4942009-11-09 19:22:17 +0000442 // Add dependence on barrier chain, if needed.
443 if (BarrierChain)
444 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000445 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000446 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000447 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000448 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000449 } else if (TID.mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000450 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000451 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000452 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000453 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000454 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000455 if (const Value *V =
456 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
457 // A load from a specific PseudoSourceValue. Add precise dependencies.
458 std::map<const Value *, SUnit *>::iterator I =
459 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
460 std::map<const Value *, SUnit *>::iterator IE =
461 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
462 if (I != IE)
463 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
464 /*isNormalMemory=*/true));
465 if (MayAlias)
466 AliasMemUses[V].push_back(SU);
467 else
468 NonAliasMemUses[V].push_back(SU);
469 } else {
470 // A load with no underlying object. Depend on all
471 // potentially aliasing stores.
472 for (std::map<const Value *, SUnit *>::iterator I =
473 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
474 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
475
476 PendingLoads.push_back(SU);
477 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000478 }
David Goodwin980d4942009-11-09 19:22:17 +0000479
480 // Add dependencies on alias and barrier chains, if needed.
481 if (MayAlias && AliasChain)
482 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
483 if (BarrierChain)
484 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
485 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000486 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000487 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000488
489 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
490 Defs[i].clear();
491 Uses[i].clear();
492 }
493 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000494}
495
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000496void ScheduleDAGInstrs::FinishBlock() {
497 // Nothing to do.
498}
499
Dan Gohmanc8c28272008-11-21 00:12:10 +0000500void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
501 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
502
David Goodwind94a4e52009-08-10 15:55:25 +0000503 // Compute the latency for the node.
Dan Gohmanc8c28272008-11-21 00:12:10 +0000504 SU->Latency =
David Goodwindc4bdcd2009-08-19 16:08:58 +0000505 InstrItins.getStageLatency(SU->getInstr()->getDesc().getSchedClass());
Dan Gohman4ea8e852008-12-16 02:38:22 +0000506
507 // Simplistic target-independent heuristic: assume that loads take
508 // extra time.
509 if (InstrItins.isEmpty())
510 if (SU->getInstr()->getDesc().mayLoad())
511 SU->Latency += 2;
Dan Gohmanc8c28272008-11-21 00:12:10 +0000512}
513
David Goodwindc4bdcd2009-08-19 16:08:58 +0000514void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
515 SDep& dep) const {
516 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
517 if (InstrItins.isEmpty())
518 return;
519
520 // For a data dependency with a known register...
521 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
522 return;
523
524 const unsigned Reg = dep.getReg();
525
526 // ... find the definition of the register in the defining
527 // instruction
528 MachineInstr *DefMI = Def->getInstr();
529 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
530 if (DefIdx != -1) {
531 int DefCycle = InstrItins.getOperandCycle(DefMI->getDesc().getSchedClass(), DefIdx);
532 if (DefCycle >= 0) {
533 MachineInstr *UseMI = Use->getInstr();
534 const unsigned UseClass = UseMI->getDesc().getSchedClass();
535
536 // For all uses of the register, calculate the maxmimum latency
537 int Latency = -1;
538 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
539 const MachineOperand &MO = UseMI->getOperand(i);
540 if (!MO.isReg() || !MO.isUse())
541 continue;
542 unsigned MOReg = MO.getReg();
543 if (MOReg != Reg)
544 continue;
545
546 int UseCycle = InstrItins.getOperandCycle(UseClass, i);
547 if (UseCycle >= 0)
548 Latency = std::max(Latency, DefCycle - UseCycle + 1);
549 }
550
551 // If we found a latency, then replace the existing dependence latency.
552 if (Latency >= 0)
553 dep.setLatency(Latency);
554 }
555 }
556}
557
Dan Gohman343f0c02008-11-19 23:18:57 +0000558void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
559 SU->getInstr()->dump();
560}
561
562std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
563 std::string s;
564 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000565 if (SU == &EntrySU)
566 oss << "<entry>";
567 else if (SU == &ExitSU)
568 oss << "<exit>";
569 else
570 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000571 return oss.str();
572}
573
574// EmitSchedule - Emit the machine code in scheduled order.
Evan Chengfb2e7522009-09-18 21:02:19 +0000575MachineBasicBlock *ScheduleDAGInstrs::
576EmitSchedule(DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) {
Dan Gohman343f0c02008-11-19 23:18:57 +0000577 // For MachineInstr-based scheduling, we're rescheduling the instructions in
578 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000579 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000580 MachineBasicBlock::iterator I = Begin;
581 ++Begin;
582 BB->remove(I);
583 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000584
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000585 // First reinsert any remaining debug_values; these are either constants,
586 // or refer to live-in registers. The beginning of the block is the right
587 // place for the latter. The former might reasonably be placed elsewhere
588 // using some kind of ordering algorithm, but right now it doesn't matter.
589 for (int i = DbgValueVec.size()-1; i>=0; --i)
590 if (DbgValueVec[i])
591 BB->insert(InsertPos, DbgValueVec[i]);
592
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000593 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000594 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
595 SUnit *SU = Sequence[i];
596 if (!SU) {
597 // Null SUnit* is a noop.
598 EmitNoop();
599 continue;
600 }
601
Dan Gohman47ac0f02009-02-11 04:27:20 +0000602 BB->insert(InsertPos, SU->getInstr());
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000603 if (SU->getDbgInstr())
604 BB->insert(InsertPos, SU->getDbgInstr());
Dan Gohman343f0c02008-11-19 23:18:57 +0000605 }
606
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000607 // Update the Begin iterator, as the first instruction in the block
608 // may have been scheduled later.
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000609 if (!DbgValueVec.empty()) {
610 for (int i = DbgValueVec.size()-1; i>=0; --i)
611 if (DbgValueVec[i]!=0) {
612 Begin = DbgValueVec[DbgValueVec.size()-1];
613 break;
614 }
615 } else if (!Sequence.empty())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000616 Begin = Sequence[0]->getInstr();
617
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000618 DbgValueVec.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000619 return BB;
620}