blob: 5bafc24dedf76657047b8cd7db95a5a4147dce0f [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 Gohman5034dd32010-12-15 20:02:24 +000019#include "llvm/Analysis/ValueTracking.h"
Dan Gohman3f237442008-12-16 03:25:46 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanc76909a2009-09-25 20:36:54 +000021#include "llvm/CodeGen/MachineMemOperand.h"
Dan Gohman3f237442008-12-16 03:25:46 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman6a9041e2008-12-04 01:35:46 +000023#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman3f237442008-12-16 03:25:46 +000027#include "llvm/Target/TargetSubtarget.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/raw_ostream.h"
Dan Gohman3f237442008-12-16 03:25:46 +000030#include "llvm/ADT/SmallSet.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000031using namespace llvm;
32
Dan Gohman79ce2762009-01-15 19:20:50 +000033ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
Dan Gohman3f237442008-12-16 03:25:46 +000034 const MachineLoopInfo &mli,
35 const MachineDominatorTree &mdt)
Evan Cheng3ef1c872010-09-10 01:29:16 +000036 : ScheduleDAG(mf), MLI(mli), MDT(mdt), MFI(mf.getFrameInfo()),
37 InstrItins(mf.getTarget().getInstrItineraryData()),
38 Defs(TRI->getNumRegs()), Uses(TRI->getNumRegs()), LoopRegs(MLI, MDT) {
Dale Johannesenbfdf7f32010-03-10 22:13:47 +000039 DbgValueVec.clear();
Evan Cheng38bdfc62009-10-18 19:58:47 +000040}
Dan Gohman343f0c02008-11-19 23:18:57 +000041
Dan Gohman47ac0f02009-02-11 04:27:20 +000042/// Run - perform scheduling.
43///
44void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
45 MachineBasicBlock::iterator begin,
46 MachineBasicBlock::iterator end,
47 unsigned endcount) {
48 BB = bb;
49 Begin = begin;
50 InsertPosIndex = endcount;
51
52 ScheduleDAG::Run(bb, end);
53}
54
Dan Gohman3311a1f2009-01-30 02:49:14 +000055/// getUnderlyingObjectFromInt - This is the function that does the work of
56/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
57static const Value *getUnderlyingObjectFromInt(const Value *V) {
58 do {
Dan Gohman8906f952009-07-17 20:58:59 +000059 if (const Operator *U = dyn_cast<Operator>(V)) {
Dan Gohman3311a1f2009-01-30 02:49:14 +000060 // If we find a ptrtoint, we can transfer control back to the
61 // regular getUnderlyingObjectFromInt.
Dan Gohman8906f952009-07-17 20:58:59 +000062 if (U->getOpcode() == Instruction::PtrToInt)
Dan Gohman3311a1f2009-01-30 02:49:14 +000063 return U->getOperand(0);
64 // If we find an add of a constant or a multiplied value, it's
65 // likely that the other operand will lead us to the base
66 // object. We don't have to worry about the case where the
Dan Gohman748f98f2009-08-07 01:26:06 +000067 // object address is somehow being computed by the multiply,
Dan Gohman3311a1f2009-01-30 02:49:14 +000068 // because our callers only care when the result is an
69 // identifibale object.
Dan Gohman8906f952009-07-17 20:58:59 +000070 if (U->getOpcode() != Instruction::Add ||
Dan Gohman3311a1f2009-01-30 02:49:14 +000071 (!isa<ConstantInt>(U->getOperand(1)) &&
Dan Gohman8906f952009-07-17 20:58:59 +000072 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
Dan Gohman3311a1f2009-01-30 02:49:14 +000073 return V;
74 V = U->getOperand(0);
75 } else {
76 return V;
77 }
Duncan Sands1df98592010-02-16 11:11:14 +000078 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
Dan Gohman3311a1f2009-01-30 02:49:14 +000079 } while (1);
80}
81
Dan Gohman5034dd32010-12-15 20:02:24 +000082/// getUnderlyingObject - This is a wrapper around GetUnderlyingObject
Dan Gohman3311a1f2009-01-30 02:49:14 +000083/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
84static const Value *getUnderlyingObject(const Value *V) {
85 // First just call Value::getUnderlyingObject to let it do what it does.
86 do {
Dan Gohman5034dd32010-12-15 20:02:24 +000087 V = GetUnderlyingObject(V);
Dan Gohman3311a1f2009-01-30 02:49:14 +000088 // If it found an inttoptr, use special code to continue climing.
Dan Gohman8906f952009-07-17 20:58:59 +000089 if (Operator::getOpcode(V) != Instruction::IntToPtr)
Dan Gohman3311a1f2009-01-30 02:49:14 +000090 break;
91 const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
92 // If that succeeded in finding a pointer, continue the search.
Duncan Sands1df98592010-02-16 11:11:14 +000093 if (!O->getType()->isPointerTy())
Dan Gohman3311a1f2009-01-30 02:49:14 +000094 break;
95 V = O;
96 } while (1);
97 return V;
98}
99
100/// getUnderlyingObjectForInstr - If this machine instr has memory reference
101/// information and it can be tracked to a normal reference to a known
102/// object, return the Value for that object. Otherwise return null.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000103static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI,
David Goodwina9e61072009-11-03 20:15:00 +0000104 const MachineFrameInfo *MFI,
105 bool &MayAlias) {
106 MayAlias = true;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000107 if (!MI->hasOneMemOperand() ||
Dan Gohmanc76909a2009-09-25 20:36:54 +0000108 !(*MI->memoperands_begin())->getValue() ||
109 (*MI->memoperands_begin())->isVolatile())
Dan Gohman3311a1f2009-01-30 02:49:14 +0000110 return 0;
111
Dan Gohmanc76909a2009-09-25 20:36:54 +0000112 const Value *V = (*MI->memoperands_begin())->getValue();
Dan Gohman3311a1f2009-01-30 02:49:14 +0000113 if (!V)
114 return 0;
115
116 V = getUnderlyingObject(V);
Evan Chengff89dcb2009-10-18 18:16:27 +0000117 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) {
118 // For now, ignore PseudoSourceValues which may alias LLVM IR values
119 // because the code that uses this function has no way to cope with
120 // such aliases.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000121 if (PSV->isAliased(MFI))
Evan Chengff89dcb2009-10-18 18:16:27 +0000122 return 0;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000123
David Goodwin980d4942009-11-09 19:22:17 +0000124 MayAlias = PSV->mayAlias(MFI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000125 return V;
126 }
Dan Gohman3311a1f2009-01-30 02:49:14 +0000127
Evan Chengff89dcb2009-10-18 18:16:27 +0000128 if (isIdentifiedObject(V))
129 return V;
130
131 return 0;
Dan Gohman3311a1f2009-01-30 02:49:14 +0000132}
133
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000134void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
135 if (MachineLoop *ML = MLI.getLoopFor(BB))
136 if (BB == ML->getLoopLatch()) {
137 MachineBasicBlock *Header = ML->getHeader();
138 for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
139 E = Header->livein_end(); I != E; ++I)
140 LoopLiveInRegs.insert(*I);
141 LoopRegs.VisitLoop(ML);
142 }
143}
144
Evan Chengec6906b2010-10-23 02:10:46 +0000145/// AddSchedBarrierDeps - Add dependencies from instructions in the current
146/// list of instructions being scheduled to scheduling barrier by adding
147/// the exit SU to the register defs and use list. This is because we want to
148/// make sure instructions which define registers that are either used by
149/// the terminator or are live-out are properly scheduled. This is
150/// especially important when the definition latency of the return value(s)
151/// are too high to be hidden by the branch or when the liveout registers
152/// used by instructions in the fallthrough block.
153void ScheduleDAGInstrs::AddSchedBarrierDeps() {
154 MachineInstr *ExitMI = InsertPos != BB->end() ? &*InsertPos : 0;
155 ExitSU.setInstr(ExitMI);
156 bool AllDepKnown = ExitMI &&
157 (ExitMI->getDesc().isCall() || ExitMI->getDesc().isBarrier());
158 if (ExitMI && AllDepKnown) {
159 // If it's a call or a barrier, add dependencies on the defs and uses of
160 // instruction.
161 for (unsigned i = 0, e = ExitMI->getNumOperands(); i != e; ++i) {
162 const MachineOperand &MO = ExitMI->getOperand(i);
163 if (!MO.isReg() || MO.isDef()) continue;
164 unsigned Reg = MO.getReg();
165 if (Reg == 0) continue;
166
167 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
168 Uses[Reg].push_back(&ExitSU);
169 }
170 } else {
171 // For others, e.g. fallthrough, conditional branch, assume the exit
Evan Chengde5fa932010-10-27 23:17:17 +0000172 // uses all the registers that are livein to the successor blocks.
173 SmallSet<unsigned, 8> Seen;
174 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
175 SE = BB->succ_end(); SI != SE; ++SI)
176 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
Andrew Trickf405b1a2011-05-05 19:24:06 +0000177 E = (*SI)->livein_end(); I != E; ++I) {
Evan Chengde5fa932010-10-27 23:17:17 +0000178 unsigned Reg = *I;
179 if (Seen.insert(Reg))
180 Uses[Reg].push_back(&ExitSU);
181 }
Evan Chengec6906b2010-10-23 02:10:46 +0000182 }
183}
184
Dan Gohmana70dca12009-10-09 23:27:56 +0000185void ScheduleDAGInstrs::BuildSchedGraph(AliasAnalysis *AA) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000186 // We'll be allocating one SUnit for each instruction, plus one for
187 // the region exit node.
Dan Gohman343f0c02008-11-19 23:18:57 +0000188 SUnits.reserve(BB->size());
189
Dan Gohman6a9041e2008-12-04 01:35:46 +0000190 // We build scheduling units by walking a block's instruction list from bottom
191 // to top.
192
David Goodwin980d4942009-11-09 19:22:17 +0000193 // Remember where a generic side-effecting instruction is as we procede.
194 SUnit *BarrierChain = 0, *AliasChain = 0;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000195
David Goodwin980d4942009-11-09 19:22:17 +0000196 // Memory references to specific known memory locations are tracked
197 // so that they can be given more precise dependencies. We track
198 // separately the known memory locations that may alias and those
199 // that are known not to alias
200 std::map<const Value *, SUnit *> AliasMemDefs, NonAliasMemDefs;
201 std::map<const Value *, std::vector<SUnit *> > AliasMemUses, NonAliasMemUses;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000202
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000203 // Keep track of dangling debug references to registers.
Bill Wendling87ea2942010-07-15 20:04:36 +0000204 std::vector<std::pair<MachineInstr*, unsigned> >
205 DanglingDebugValue(TRI->getNumRegs(),
206 std::make_pair(static_cast<MachineInstr*>(0), 0));
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000207
Dan Gohman3f237442008-12-16 03:25:46 +0000208 // Check to see if the scheduler cares about latencies.
209 bool UnitLatencies = ForceUnitLatencies();
210
Dan Gohman8749b612008-12-16 03:35:01 +0000211 // Ask the target if address-backscheduling is desirable, and if so how much.
David Goodwin71046162009-08-13 16:05:04 +0000212 const TargetSubtarget &ST = TM.getSubtarget<TargetSubtarget>();
213 unsigned SpecialAddressLatency = ST.getSpecialAddressLatency();
Dan Gohman8749b612008-12-16 03:35:01 +0000214
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000215 // Remove any stale debug info; sometimes BuildSchedGraph is called again
216 // without emitting the info from the previous call.
217 DbgValueVec.clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000218
Evan Chengec6906b2010-10-23 02:10:46 +0000219 // Model data dependencies between instructions being scheduled and the
220 // ExitSU.
221 AddSchedBarrierDeps();
222
Andrew Trick9b668532011-05-06 21:52:52 +0000223 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
224 assert(Defs[i].empty() && "Only BuildGraph should push/pop Defs");
225 }
226
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000227 // Walk the list of instructions, from bottom moving up.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000228 for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
Dan Gohman343f0c02008-11-19 23:18:57 +0000229 MII != MIE; --MII) {
230 MachineInstr *MI = prior(MII);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000231 // DBG_VALUE does not have SUnit's built, so just remember these for later
232 // reinsertion.
233 if (MI->isDebugValue()) {
234 if (MI->getNumOperands()==3 && MI->getOperand(0).isReg() &&
235 MI->getOperand(0).getReg())
236 DanglingDebugValue[MI->getOperand(0).getReg()] =
237 std::make_pair(MI, DbgValueVec.size());
238 DbgValueVec.push_back(MI);
239 continue;
240 }
Dan Gohman3f237442008-12-16 03:25:46 +0000241 const TargetInstrDesc &TID = MI->getDesc();
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000242 assert(!TID.isTerminator() && !MI->isLabel() &&
243 "Cannot schedule terminators or labels!");
244 // Create the SUnit for this MI.
Dan Gohman343f0c02008-11-19 23:18:57 +0000245 SUnit *SU = NewSUnit(MI);
Evan Cheng8239daf2010-11-03 00:45:17 +0000246 SU->isCall = TID.isCall();
247 SU->isCommutable = TID.isCommutable();
Dan Gohman343f0c02008-11-19 23:18:57 +0000248
Dan Gohman54e4c362008-12-09 22:54:47 +0000249 // Assign the Latency field of SU using target-provided information.
Dan Gohman3f237442008-12-16 03:25:46 +0000250 if (UnitLatencies)
251 SU->Latency = 1;
252 else
253 ComputeLatency(SU);
Dan Gohman54e4c362008-12-09 22:54:47 +0000254
Dan Gohman6a9041e2008-12-04 01:35:46 +0000255 // Add register-based dependencies (data, anti, and output).
Dan Gohman343f0c02008-11-19 23:18:57 +0000256 for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
257 const MachineOperand &MO = MI->getOperand(j);
258 if (!MO.isReg()) continue;
259 unsigned Reg = MO.getReg();
260 if (Reg == 0) continue;
261
262 assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000263
264 if (MO.isDef() && DanglingDebugValue[Reg].first!=0) {
Jim Grosbach309d20c2010-05-19 22:57:06 +0000265 SU->DbgInstrList.push_back(DanglingDebugValue[Reg].first);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000266 DbgValueVec[DanglingDebugValue[Reg].second] = 0;
267 DanglingDebugValue[Reg] = std::make_pair((MachineInstr*)0, 0);
268 }
269
Dan Gohman343f0c02008-11-19 23:18:57 +0000270 std::vector<SUnit *> &UseList = Uses[Reg];
Andrew Trick9b668532011-05-06 21:52:52 +0000271 // Defs are push in the order they are visited and never reordered.
Dan Gohman3f237442008-12-16 03:25:46 +0000272 std::vector<SUnit *> &DefList = Defs[Reg];
David Goodwind94a4e52009-08-10 15:55:25 +0000273 // Optionally add output and anti dependencies. For anti
274 // dependencies we use a latency of 0 because for a multi-issue
275 // target we want to allow the defining instruction to issue
276 // in the same cycle as the using instruction.
277 // TODO: Using a latency of 1 here for output dependencies assumes
278 // there's no cost for reusing registers.
Dan Gohman54e4c362008-12-09 22:54:47 +0000279 SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
David Goodwind94a4e52009-08-10 15:55:25 +0000280 unsigned AOLatency = (Kind == SDep::Anti) ? 0 : 1;
Dan Gohman3f237442008-12-16 03:25:46 +0000281 for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
282 SUnit *DefSU = DefList[i];
Evan Chengec6906b2010-10-23 02:10:46 +0000283 if (DefSU == &ExitSU)
284 continue;
Dan Gohman3f237442008-12-16 03:25:46 +0000285 if (DefSU != SU &&
286 (Kind != SDep::Output || !MO.isDead() ||
287 !DefSU->getInstr()->registerDefIsDead(Reg)))
David Goodwind94a4e52009-08-10 15:55:25 +0000288 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/Reg));
Dan Gohman3f237442008-12-16 03:25:46 +0000289 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000290 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Andrew Trick9b668532011-05-06 21:52:52 +0000291 std::vector<SUnit *> &MemDefList = Defs[*Alias];
292 for (unsigned i = 0, e = MemDefList.size(); i != e; ++i) {
293 SUnit *DefSU = MemDefList[i];
Evan Chengec6906b2010-10-23 02:10:46 +0000294 if (DefSU == &ExitSU)
295 continue;
Dan Gohman3f237442008-12-16 03:25:46 +0000296 if (DefSU != SU &&
297 (Kind != SDep::Output || !MO.isDead() ||
Dan Gohman91203cf2009-10-26 18:26:18 +0000298 !DefSU->getInstr()->registerDefIsDead(*Alias)))
David Goodwind94a4e52009-08-10 15:55:25 +0000299 DefSU->addPred(SDep(SU, Kind, AOLatency, /*Reg=*/ *Alias));
Dan Gohman3f237442008-12-16 03:25:46 +0000300 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000301 }
302
303 if (MO.isDef()) {
304 // Add any data dependencies.
Dan Gohman3f237442008-12-16 03:25:46 +0000305 unsigned DataLatency = SU->Latency;
306 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
307 SUnit *UseSU = UseList[i];
Evan Chenga69ec092010-03-22 21:24:33 +0000308 if (UseSU == SU)
309 continue;
310 unsigned LDataLatency = DataLatency;
311 // Optionally add in a special extra latency for nodes that
312 // feed addresses.
313 // TODO: Do this for register aliases too.
314 // TODO: Perhaps we should get rid of
315 // SpecialAddressLatency and just move this into
316 // adjustSchedDependency for the targets that care about it.
Evan Chengec6906b2010-10-23 02:10:46 +0000317 if (SpecialAddressLatency != 0 && !UnitLatencies &&
318 UseSU != &ExitSU) {
Evan Chenga69ec092010-03-22 21:24:33 +0000319 MachineInstr *UseMI = UseSU->getInstr();
320 const TargetInstrDesc &UseTID = UseMI->getDesc();
321 int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
322 assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
Evan Chengec6906b2010-10-23 02:10:46 +0000323 if (RegUseIndex >= 0 &&
324 (UseTID.mayLoad() || UseTID.mayStore()) &&
Evan Chenga69ec092010-03-22 21:24:33 +0000325 (unsigned)RegUseIndex < UseTID.getNumOperands() &&
326 UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
327 LDataLatency += SpecialAddressLatency;
Dan Gohman3f237442008-12-16 03:25:46 +0000328 }
Evan Chenga69ec092010-03-22 21:24:33 +0000329 // Adjust the dependence latency using operand def/use
330 // information (if any), and then allow the target to
331 // perform its own adjustments.
332 const SDep& dep = SDep(SU, SDep::Data, LDataLatency, Reg);
333 if (!UnitLatencies) {
Dan Gohman3fb150a2010-04-17 17:42:52 +0000334 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
335 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
Evan Chenga69ec092010-03-22 21:24:33 +0000336 }
337 UseSU->addPred(dep);
Dan Gohman3f237442008-12-16 03:25:46 +0000338 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000339 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
340 std::vector<SUnit *> &UseList = Uses[*Alias];
Dan Gohman3f237442008-12-16 03:25:46 +0000341 for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
342 SUnit *UseSU = UseList[i];
Evan Chenga69ec092010-03-22 21:24:33 +0000343 if (UseSU == SU)
344 continue;
345 const SDep& dep = SDep(SU, SDep::Data, DataLatency, *Alias);
346 if (!UnitLatencies) {
Dan Gohman3fb150a2010-04-17 17:42:52 +0000347 ComputeOperandLatency(SU, UseSU, const_cast<SDep &>(dep));
348 ST.adjustSchedDependency(SU, UseSU, const_cast<SDep &>(dep));
David Goodwin71046162009-08-13 16:05:04 +0000349 }
Evan Chenga69ec092010-03-22 21:24:33 +0000350 UseSU->addPred(dep);
Dan Gohman3f237442008-12-16 03:25:46 +0000351 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000352 }
353
Dan Gohman8749b612008-12-16 03:35:01 +0000354 // If a def is going to wrap back around to the top of the loop,
355 // backschedule it.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000356 if (!UnitLatencies && DefList.empty()) {
Dan Gohman8749b612008-12-16 03:35:01 +0000357 LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
358 if (I != LoopRegs.Deps.end()) {
359 const MachineOperand *UseMO = I->second.first;
360 unsigned Count = I->second.second;
361 const MachineInstr *UseMI = UseMO->getParent();
362 unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
363 const TargetInstrDesc &UseTID = UseMI->getDesc();
364 // TODO: If we knew the total depth of the region here, we could
365 // handle the case where the whole loop is inside the region but
366 // is large enough that the isScheduleHigh trick isn't needed.
367 if (UseMOIdx < UseTID.getNumOperands()) {
368 // Currently, we only support scheduling regions consisting of
369 // single basic blocks. Check to see if the instruction is in
370 // the same region by checking to see if it has the same parent.
371 if (UseMI->getParent() != MI->getParent()) {
372 unsigned Latency = SU->Latency;
373 if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
374 Latency += SpecialAddressLatency;
375 // This is a wild guess as to the portion of the latency which
376 // will be overlapped by work done outside the current
377 // scheduling region.
378 Latency -= std::min(Latency, Count);
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000379 // Add the artificial edge.
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000380 ExitSU.addPred(SDep(SU, SDep::Order, Latency,
381 /*Reg=*/0, /*isNormalMemory=*/false,
382 /*isMustAlias=*/false,
383 /*isArtificial=*/true));
Dan Gohman8749b612008-12-16 03:35:01 +0000384 } else if (SpecialAddressLatency > 0 &&
385 UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
386 // The entire loop body is within the current scheduling region
387 // and the latency of this operation is assumed to be greater
388 // than the latency of the loop.
389 // TODO: Recursively mark data-edge predecessors as
390 // isScheduleHigh too.
391 SU->isScheduleHigh = true;
392 }
393 }
394 LoopRegs.Deps.erase(I);
395 }
396 }
397
Dan Gohman343f0c02008-11-19 23:18:57 +0000398 UseList.clear();
Dan Gohman3f237442008-12-16 03:25:46 +0000399 if (!MO.isDead())
400 DefList.clear();
Andrew Trickee109152011-05-05 19:32:21 +0000401
402 // Calls will not be reordered because of chain dependencies (see
403 // below). Since call operands are dead, calls may continue to be added
404 // to the DefList making dependence checking quadratic in the size of
405 // the block. Instead, we leave only one call at the back of the
406 // DefList.
Andrew Trickee109152011-05-05 19:32:21 +0000407 if (SU->isCall) {
408 while (!DefList.empty() && DefList.back()->isCall)
409 DefList.pop_back();
410 }
Dan Gohman3f237442008-12-16 03:25:46 +0000411 DefList.push_back(SU);
Dan Gohman343f0c02008-11-19 23:18:57 +0000412 } else {
413 UseList.push_back(SU);
414 }
415 }
Dan Gohman6a9041e2008-12-04 01:35:46 +0000416
417 // Add chain dependencies.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000418 // Chain dependencies used to enforce memory order should have
419 // latency of 0 (except for true dependency of Store followed by
420 // aliased Load... we estimate that with a single cycle of latency
421 // assuming the hardware will bypass)
Dan Gohman6a9041e2008-12-04 01:35:46 +0000422 // Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
423 // after stack slots are lowered to actual addresses.
424 // TODO: Use an AliasAnalysis and do real alias-analysis queries, and
425 // produce more precise dependence information.
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000426#define STORE_LOAD_LATENCY 1
427 unsigned TrueMemOrderLatency = 0;
Evan Chengc36b7062011-01-07 23:50:32 +0000428 if (TID.isCall() || MI->hasUnmodeledSideEffects() ||
Andrew Trickf405b1a2011-05-05 19:24:06 +0000429 (MI->hasVolatileMemoryRef() &&
David Goodwin980d4942009-11-09 19:22:17 +0000430 (!TID.mayLoad() || !MI->isInvariantLoad(AA)))) {
431 // Be conservative with these and add dependencies on all memory
432 // references, even those that are known to not alias.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000433 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000434 NonAliasMemDefs.begin(), E = NonAliasMemDefs.end(); I != E; ++I) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000435 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000436 }
437 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000438 NonAliasMemUses.begin(), E = NonAliasMemUses.end(); I != E; ++I) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000439 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000440 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000441 }
David Goodwin980d4942009-11-09 19:22:17 +0000442 NonAliasMemDefs.clear();
443 NonAliasMemUses.clear();
444 // Add SU to the barrier chain.
445 if (BarrierChain)
446 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
447 BarrierChain = SU;
448
449 // fall-through
450 new_alias_chain:
451 // Chain all possibly aliasing memory references though SU.
452 if (AliasChain)
453 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
454 AliasChain = SU;
455 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
456 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
457 for (std::map<const Value *, SUnit *>::iterator I = AliasMemDefs.begin(),
458 E = AliasMemDefs.end(); I != E; ++I) {
459 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
460 }
461 for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
462 AliasMemUses.begin(), E = AliasMemUses.end(); I != E; ++I) {
463 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
464 I->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
465 }
466 PendingLoads.clear();
467 AliasMemDefs.clear();
468 AliasMemUses.clear();
Dan Gohman6a9041e2008-12-04 01:35:46 +0000469 } else if (TID.mayStore()) {
David Goodwina9e61072009-11-03 20:15:00 +0000470 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000471 TrueMemOrderLatency = STORE_LOAD_LATENCY;
David Goodwina9e61072009-11-03 20:15:00 +0000472 if (const Value *V = getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000473 // A store to a specific PseudoSourceValue. Add precise dependencies.
David Goodwin980d4942009-11-09 19:22:17 +0000474 // Record the def in MemDefs, first adding a dep if there is
475 // an existing def.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000476 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000477 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000478 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000479 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
480 if (I != IE) {
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000481 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
Dan Gohman54e4c362008-12-09 22:54:47 +0000482 /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000483 I->second = SU;
484 } else {
David Goodwin980d4942009-11-09 19:22:17 +0000485 if (MayAlias)
486 AliasMemDefs[V] = SU;
487 else
488 NonAliasMemDefs[V] = SU;
Dan Gohman6a9041e2008-12-04 01:35:46 +0000489 }
490 // Handle the uses in MemUses, if there are any.
Dan Gohmana629b482008-12-08 17:50:35 +0000491 std::map<const Value *, std::vector<SUnit *> >::iterator J =
David Goodwin980d4942009-11-09 19:22:17 +0000492 ((MayAlias) ? AliasMemUses.find(V) : NonAliasMemUses.find(V));
493 std::map<const Value *, std::vector<SUnit *> >::iterator JE =
494 ((MayAlias) ? AliasMemUses.end() : NonAliasMemUses.end());
495 if (J != JE) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000496 for (unsigned i = 0, e = J->second.size(); i != e; ++i)
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000497 J->second[i]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency,
498 /*Reg=*/0, /*isNormalMemory=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000499 J->second.clear();
500 }
David Goodwina9e61072009-11-03 20:15:00 +0000501 if (MayAlias) {
David Goodwin980d4942009-11-09 19:22:17 +0000502 // Add dependencies from all the PendingLoads, i.e. loads
503 // with no underlying object.
David Goodwina9e61072009-11-03 20:15:00 +0000504 for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
505 PendingLoads[k]->addPred(SDep(SU, SDep::Order, TrueMemOrderLatency));
David Goodwin980d4942009-11-09 19:22:17 +0000506 // Add dependence on alias chain, if needed.
507 if (AliasChain)
508 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwina9e61072009-11-03 20:15:00 +0000509 }
David Goodwin980d4942009-11-09 19:22:17 +0000510 // Add dependence on barrier chain, if needed.
511 if (BarrierChain)
512 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
David Goodwin5be870a2009-11-05 00:16:44 +0000513 } else {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000514 // Treat all other stores conservatively.
David Goodwin980d4942009-11-09 19:22:17 +0000515 goto new_alias_chain;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000516 }
Evan Chengec6906b2010-10-23 02:10:46 +0000517
518 if (!ExitSU.isPred(SU))
519 // Push store's up a bit to avoid them getting in between cmp
520 // and branches.
521 ExitSU.addPred(SDep(SU, SDep::Order, 0,
522 /*Reg=*/0, /*isNormalMemory=*/false,
523 /*isMustAlias=*/false,
524 /*isArtificial=*/true));
Dan Gohman6a9041e2008-12-04 01:35:46 +0000525 } else if (TID.mayLoad()) {
David Goodwina9e61072009-11-03 20:15:00 +0000526 bool MayAlias = true;
David Goodwin7c9b1ac2009-11-02 17:06:28 +0000527 TrueMemOrderLatency = 0;
Dan Gohmana70dca12009-10-09 23:27:56 +0000528 if (MI->isInvariantLoad(AA)) {
Dan Gohman6a9041e2008-12-04 01:35:46 +0000529 // Invariant load, no chain dependencies needed!
David Goodwin5be870a2009-11-05 00:16:44 +0000530 } else {
Andrew Trickf405b1a2011-05-05 19:24:06 +0000531 if (const Value *V =
David Goodwin980d4942009-11-09 19:22:17 +0000532 getUnderlyingObjectForInstr(MI, MFI, MayAlias)) {
533 // A load from a specific PseudoSourceValue. Add precise dependencies.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000534 std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000535 ((MayAlias) ? AliasMemDefs.find(V) : NonAliasMemDefs.find(V));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000536 std::map<const Value *, SUnit *>::iterator IE =
David Goodwin980d4942009-11-09 19:22:17 +0000537 ((MayAlias) ? AliasMemDefs.end() : NonAliasMemDefs.end());
538 if (I != IE)
539 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0, /*Reg=*/0,
540 /*isNormalMemory=*/true));
541 if (MayAlias)
542 AliasMemUses[V].push_back(SU);
Andrew Trickf405b1a2011-05-05 19:24:06 +0000543 else
David Goodwin980d4942009-11-09 19:22:17 +0000544 NonAliasMemUses[V].push_back(SU);
545 } else {
546 // A load with no underlying object. Depend on all
547 // potentially aliasing stores.
Andrew Trickf405b1a2011-05-05 19:24:06 +0000548 for (std::map<const Value *, SUnit *>::iterator I =
David Goodwin980d4942009-11-09 19:22:17 +0000549 AliasMemDefs.begin(), E = AliasMemDefs.end(); I != E; ++I)
550 I->second->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000551
David Goodwin980d4942009-11-09 19:22:17 +0000552 PendingLoads.push_back(SU);
553 MayAlias = true;
David Goodwina9e61072009-11-03 20:15:00 +0000554 }
Andrew Trickf405b1a2011-05-05 19:24:06 +0000555
David Goodwin980d4942009-11-09 19:22:17 +0000556 // Add dependencies on alias and barrier chains, if needed.
557 if (MayAlias && AliasChain)
558 AliasChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
559 if (BarrierChain)
560 BarrierChain->addPred(SDep(SU, SDep::Order, /*Latency=*/0));
Andrew Trickf405b1a2011-05-05 19:24:06 +0000561 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000562 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000563 }
Dan Gohman79ce2762009-01-15 19:20:50 +0000564
565 for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
566 Defs[i].clear();
567 Uses[i].clear();
568 }
569 PendingLoads.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000570}
571
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000572void ScheduleDAGInstrs::FinishBlock() {
573 // Nothing to do.
574}
575
Dan Gohmanc8c28272008-11-21 00:12:10 +0000576void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
David Goodwind94a4e52009-08-10 15:55:25 +0000577 // Compute the latency for the node.
Evan Cheng3ef1c872010-09-10 01:29:16 +0000578 if (!InstrItins || InstrItins->isEmpty()) {
579 SU->Latency = 1;
Dan Gohman4ea8e852008-12-16 02:38:22 +0000580
Evan Cheng3ef1c872010-09-10 01:29:16 +0000581 // Simplistic target-independent heuristic: assume that loads take
582 // extra time.
Dan Gohman4ea8e852008-12-16 02:38:22 +0000583 if (SU->getInstr()->getDesc().mayLoad())
584 SU->Latency += 2;
Evan Cheng8239daf2010-11-03 00:45:17 +0000585 } else {
586 SU->Latency = TII->getInstrLatency(InstrItins, SU->getInstr());
587 }
Dan Gohmanc8c28272008-11-21 00:12:10 +0000588}
589
Andrew Trickf405b1a2011-05-05 19:24:06 +0000590void ScheduleDAGInstrs::ComputeOperandLatency(SUnit *Def, SUnit *Use,
David Goodwindc4bdcd2009-08-19 16:08:58 +0000591 SDep& dep) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000592 if (!InstrItins || InstrItins->isEmpty())
David Goodwindc4bdcd2009-08-19 16:08:58 +0000593 return;
Andrew Trickf405b1a2011-05-05 19:24:06 +0000594
David Goodwindc4bdcd2009-08-19 16:08:58 +0000595 // For a data dependency with a known register...
596 if ((dep.getKind() != SDep::Data) || (dep.getReg() == 0))
597 return;
598
599 const unsigned Reg = dep.getReg();
600
601 // ... find the definition of the register in the defining
602 // instruction
603 MachineInstr *DefMI = Def->getInstr();
604 int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
605 if (DefIdx != -1) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000606 const MachineOperand &MO = DefMI->getOperand(DefIdx);
607 if (MO.isReg() && MO.isImplicit() &&
Evan Chengd82de832010-10-08 23:01:57 +0000608 DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
Evan Cheng1aca5bc2010-10-08 18:42:25 +0000609 // This is an implicit def, getOperandLatency() won't return the correct
610 // latency. e.g.
611 // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
612 // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
613 // What we want is to compute latency between def of %D6/%D7 and use of
614 // %Q3 instead.
615 DefIdx = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
616 }
Evan Chenga0792de2010-10-06 06:27:31 +0000617 MachineInstr *UseMI = Use->getInstr();
Evan Cheng3881cb72010-09-29 22:42:35 +0000618 // For all uses of the register, calculate the maxmimum latency
619 int Latency = -1;
Evan Chengec6906b2010-10-23 02:10:46 +0000620 if (UseMI) {
621 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
622 const MachineOperand &MO = UseMI->getOperand(i);
623 if (!MO.isReg() || !MO.isUse())
624 continue;
625 unsigned MOReg = MO.getReg();
626 if (MOReg != Reg)
627 continue;
David Goodwindc4bdcd2009-08-19 16:08:58 +0000628
Evan Chengec6906b2010-10-23 02:10:46 +0000629 int UseCycle = TII->getOperandLatency(InstrItins, DefMI, DefIdx,
630 UseMI, i);
631 Latency = std::max(Latency, UseCycle);
632 }
633 } else {
634 // UseMI is null, then it must be a scheduling barrier.
635 if (!InstrItins || InstrItins->isEmpty())
636 return;
637 unsigned DefClass = DefMI->getDesc().getSchedClass();
638 Latency = InstrItins->getOperandCycle(DefClass, DefIdx);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000639 }
Evan Chengec6906b2010-10-23 02:10:46 +0000640
641 // If we found a latency, then replace the existing dependence latency.
642 if (Latency >= 0)
643 dep.setLatency(Latency);
David Goodwindc4bdcd2009-08-19 16:08:58 +0000644 }
645}
646
Dan Gohman343f0c02008-11-19 23:18:57 +0000647void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
648 SU->getInstr()->dump();
649}
650
651std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
652 std::string s;
653 raw_string_ostream oss(s);
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000654 if (SU == &EntrySU)
655 oss << "<entry>";
656 else if (SU == &ExitSU)
657 oss << "<exit>";
658 else
659 SU->getInstr()->print(oss);
Dan Gohman343f0c02008-11-19 23:18:57 +0000660 return oss.str();
661}
662
663// EmitSchedule - Emit the machine code in scheduled order.
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000664MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
Dan Gohman343f0c02008-11-19 23:18:57 +0000665 // For MachineInstr-based scheduling, we're rescheduling the instructions in
666 // the block, so start by removing them from the block.
Dan Gohman47ac0f02009-02-11 04:27:20 +0000667 while (Begin != InsertPos) {
Dan Gohmanf7119392009-01-16 22:10:20 +0000668 MachineBasicBlock::iterator I = Begin;
669 ++Begin;
670 BB->remove(I);
671 }
Dan Gohman343f0c02008-11-19 23:18:57 +0000672
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000673 // First reinsert any remaining debug_values; these are either constants,
674 // or refer to live-in registers. The beginning of the block is the right
675 // place for the latter. The former might reasonably be placed elsewhere
676 // using some kind of ordering algorithm, but right now it doesn't matter.
677 for (int i = DbgValueVec.size()-1; i>=0; --i)
678 if (DbgValueVec[i])
679 BB->insert(InsertPos, DbgValueVec[i]);
680
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000681 // Then re-insert them according to the given schedule.
Dan Gohman343f0c02008-11-19 23:18:57 +0000682 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
683 SUnit *SU = Sequence[i];
684 if (!SU) {
685 // Null SUnit* is a noop.
686 EmitNoop();
687 continue;
688 }
689
Dan Gohman47ac0f02009-02-11 04:27:20 +0000690 BB->insert(InsertPos, SU->getInstr());
Jim Grosbach309d20c2010-05-19 22:57:06 +0000691 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i)
692 BB->insert(InsertPos, SU->DbgInstrList[i]);
Dan Gohman343f0c02008-11-19 23:18:57 +0000693 }
694
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000695 // Update the Begin iterator, as the first instruction in the block
696 // may have been scheduled later.
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000697 if (!DbgValueVec.empty()) {
698 for (int i = DbgValueVec.size()-1; i>=0; --i)
699 if (DbgValueVec[i]!=0) {
700 Begin = DbgValueVec[DbgValueVec.size()-1];
701 break;
702 }
703 } else if (!Sequence.empty())
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000704 Begin = Sequence[0]->getInstr();
705
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000706 DbgValueVec.clear();
Dan Gohman343f0c02008-11-19 23:18:57 +0000707 return BB;
708}