blob: 6e62cb5136cbec1a2c8b1c3b22bc72e2fa953aff [file] [log] [blame]
Eugene Zelenko32a40562017-09-11 23:00:48 +00001//===- MachinePipeliner.cpp - Machine Software Pipeliner Pass -------------===//
Brendon Cahoon254f8892016-07-29 16:44:44 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Brendon Cahoon254f8892016-07-29 16:44:44 +00006//
7//===----------------------------------------------------------------------===//
8//
9// An implementation of the Swing Modulo Scheduling (SMS) software pipeliner.
10//
Brendon Cahoon254f8892016-07-29 16:44:44 +000011// This SMS implementation is a target-independent back-end pass. When enabled,
12// the pass runs just prior to the register allocation pass, while the machine
13// IR is in SSA form. If software pipelining is successful, then the original
14// loop is replaced by the optimized loop. The optimized loop contains one or
15// more prolog blocks, the pipelined kernel, and one or more epilog blocks. If
16// the instructions cannot be scheduled in a given MII, we increase the MII by
17// one and try again.
18//
19// The SMS implementation is an extension of the ScheduleDAGInstrs class. We
20// represent loop carried dependences in the DAG as order edges to the Phi
21// nodes. We also perform several passes over the DAG to eliminate unnecessary
22// edges that inhibit the ability to pipeline. The implementation uses the
23// DFAPacketizer class to compute the minimum initiation interval and the check
24// where an instruction may be inserted in the pipelined schedule.
25//
26// In order for the SMS pass to work, several target specific hooks need to be
27// implemented to get information about the loop structure and to rewrite
28// instructions.
29//
30//===----------------------------------------------------------------------===//
31
Eugene Zelenkocdc71612016-08-11 17:20:18 +000032#include "llvm/ADT/ArrayRef.h"
33#include "llvm/ADT/BitVector.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000034#include "llvm/ADT/DenseMap.h"
35#include "llvm/ADT/MapVector.h"
36#include "llvm/ADT/PriorityQueue.h"
37#include "llvm/ADT/SetVector.h"
38#include "llvm/ADT/SmallPtrSet.h"
39#include "llvm/ADT/SmallSet.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000040#include "llvm/ADT/SmallVector.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000041#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000042#include "llvm/ADT/iterator_range.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000043#include "llvm/Analysis/AliasAnalysis.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000044#include "llvm/Analysis/MemoryLocation.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000045#include "llvm/Analysis/ValueTracking.h"
46#include "llvm/CodeGen/DFAPacketizer.h"
Matthias Braunf8422972017-12-13 02:51:04 +000047#include "llvm/CodeGen/LiveIntervals.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000048#include "llvm/CodeGen/MachineBasicBlock.h"
49#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000050#include "llvm/CodeGen/MachineFunction.h"
51#include "llvm/CodeGen/MachineFunctionPass.h"
52#include "llvm/CodeGen/MachineInstr.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000053#include "llvm/CodeGen/MachineInstrBuilder.h"
54#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000055#include "llvm/CodeGen/MachineMemOperand.h"
56#include "llvm/CodeGen/MachineOperand.h"
Adrian Prantlfa2e3582019-01-14 17:24:11 +000057#include "llvm/CodeGen/MachinePipeliner.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000058#include "llvm/CodeGen/MachineRegisterInfo.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000059#include "llvm/CodeGen/RegisterPressure.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000060#include "llvm/CodeGen/ScheduleDAG.h"
Krzysztof Parzyszek88391242016-12-22 19:21:20 +000061#include "llvm/CodeGen/ScheduleDAGMutation.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000062#include "llvm/CodeGen/TargetOpcodes.h"
63#include "llvm/CodeGen/TargetRegisterInfo.h"
64#include "llvm/CodeGen/TargetSubtargetInfo.h"
Nico Weber432a3882018-04-30 14:59:11 +000065#include "llvm/Config/llvm-config.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000066#include "llvm/IR/Attributes.h"
67#include "llvm/IR/DebugLoc.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000068#include "llvm/IR/Function.h"
69#include "llvm/MC/LaneBitmask.h"
70#include "llvm/MC/MCInstrDesc.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000071#include "llvm/MC/MCInstrItineraries.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000072#include "llvm/MC/MCRegisterInfo.h"
73#include "llvm/Pass.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000074#include "llvm/Support/CommandLine.h"
Eugene Zelenko32a40562017-09-11 23:00:48 +000075#include "llvm/Support/Compiler.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000076#include "llvm/Support/Debug.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000077#include "llvm/Support/MathExtras.h"
Brendon Cahoon254f8892016-07-29 16:44:44 +000078#include "llvm/Support/raw_ostream.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000079#include <algorithm>
80#include <cassert>
Brendon Cahoon254f8892016-07-29 16:44:44 +000081#include <climits>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000082#include <cstdint>
Brendon Cahoon254f8892016-07-29 16:44:44 +000083#include <deque>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000084#include <functional>
85#include <iterator>
Brendon Cahoon254f8892016-07-29 16:44:44 +000086#include <map>
Eugene Zelenko32a40562017-09-11 23:00:48 +000087#include <memory>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000088#include <tuple>
89#include <utility>
90#include <vector>
Brendon Cahoon254f8892016-07-29 16:44:44 +000091
92using namespace llvm;
93
94#define DEBUG_TYPE "pipeliner"
95
96STATISTIC(NumTrytoPipeline, "Number of loops that we attempt to pipeline");
97STATISTIC(NumPipelined, "Number of loops software pipelined");
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +000098STATISTIC(NumNodeOrderIssues, "Number of node order issues found");
Brendon Cahoon254f8892016-07-29 16:44:44 +000099
100/// A command line option to turn software pipelining on or off.
Benjamin Kramerb7d33112016-08-06 11:13:10 +0000101static cl::opt<bool> EnableSWP("enable-pipeliner", cl::Hidden, cl::init(true),
102 cl::ZeroOrMore,
103 cl::desc("Enable Software Pipelining"));
Brendon Cahoon254f8892016-07-29 16:44:44 +0000104
105/// A command line option to enable SWP at -Os.
106static cl::opt<bool> EnableSWPOptSize("enable-pipeliner-opt-size",
107 cl::desc("Enable SWP at Os."), cl::Hidden,
108 cl::init(false));
109
110/// A command line argument to limit minimum initial interval for pipelining.
111static cl::opt<int> SwpMaxMii("pipeliner-max-mii",
Hiroshi Inoue8f976ba2018-01-17 12:29:38 +0000112 cl::desc("Size limit for the MII."),
Brendon Cahoon254f8892016-07-29 16:44:44 +0000113 cl::Hidden, cl::init(27));
114
115/// A command line argument to limit the number of stages in the pipeline.
116static cl::opt<int>
117 SwpMaxStages("pipeliner-max-stages",
118 cl::desc("Maximum stages allowed in the generated scheduled."),
119 cl::Hidden, cl::init(3));
120
121/// A command line option to disable the pruning of chain dependences due to
122/// an unrelated Phi.
123static cl::opt<bool>
124 SwpPruneDeps("pipeliner-prune-deps",
125 cl::desc("Prune dependences between unrelated Phi nodes."),
126 cl::Hidden, cl::init(true));
127
128/// A command line option to disable the pruning of loop carried order
129/// dependences.
130static cl::opt<bool>
131 SwpPruneLoopCarried("pipeliner-prune-loop-carried",
132 cl::desc("Prune loop carried order dependences."),
133 cl::Hidden, cl::init(true));
134
135#ifndef NDEBUG
136static cl::opt<int> SwpLoopLimit("pipeliner-max", cl::Hidden, cl::init(-1));
137#endif
138
139static cl::opt<bool> SwpIgnoreRecMII("pipeliner-ignore-recmii",
140 cl::ReallyHidden, cl::init(false),
141 cl::ZeroOrMore, cl::desc("Ignore RecMII"));
142
Adrian Prantlfa2e3582019-01-14 17:24:11 +0000143namespace llvm {
144
Sumanth Gundapaneni62ac69d2018-10-18 15:51:16 +0000145// A command line option to enable the CopyToPhi DAG mutation.
Adrian Prantlfa2e3582019-01-14 17:24:11 +0000146cl::opt<bool>
Aleksandr Urakov00d4c382018-10-23 14:27:45 +0000147 SwpEnableCopyToPhi("pipeliner-enable-copytophi", cl::ReallyHidden,
148 cl::init(true), cl::ZeroOrMore,
149 cl::desc("Enable CopyToPhi DAG Mutation"));
Sumanth Gundapaneni62ac69d2018-10-18 15:51:16 +0000150
Adrian Prantlfa2e3582019-01-14 17:24:11 +0000151} // end namespace llvm
Brendon Cahoon254f8892016-07-29 16:44:44 +0000152
153unsigned SwingSchedulerDAG::Circuits::MaxPaths = 5;
154char MachinePipeliner::ID = 0;
155#ifndef NDEBUG
156int MachinePipeliner::NumTries = 0;
157#endif
158char &llvm::MachinePipelinerID = MachinePipeliner::ID;
Eugene Zelenko32a40562017-09-11 23:00:48 +0000159
Matthias Braun1527baa2017-05-25 21:26:32 +0000160INITIALIZE_PASS_BEGIN(MachinePipeliner, DEBUG_TYPE,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000161 "Modulo Software Pipelining", false, false)
162INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
163INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
164INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
165INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
Matthias Braun1527baa2017-05-25 21:26:32 +0000166INITIALIZE_PASS_END(MachinePipeliner, DEBUG_TYPE,
Brendon Cahoon254f8892016-07-29 16:44:44 +0000167 "Modulo Software Pipelining", false, false)
168
169/// The "main" function for implementing Swing Modulo Scheduling.
170bool MachinePipeliner::runOnMachineFunction(MachineFunction &mf) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000171 if (skipFunction(mf.getFunction()))
Brendon Cahoon254f8892016-07-29 16:44:44 +0000172 return false;
173
174 if (!EnableSWP)
175 return false;
176
Matthias Braunf1caa282017-12-15 22:22:58 +0000177 if (mf.getFunction().getAttributes().hasAttribute(
Reid Klecknerb5180542017-03-21 16:57:19 +0000178 AttributeList::FunctionIndex, Attribute::OptimizeForSize) &&
Brendon Cahoon254f8892016-07-29 16:44:44 +0000179 !EnableSWPOptSize.getPosition())
180 return false;
181
182 MF = &mf;
183 MLI = &getAnalysis<MachineLoopInfo>();
184 MDT = &getAnalysis<MachineDominatorTree>();
185 TII = MF->getSubtarget().getInstrInfo();
186 RegClassInfo.runOnMachineFunction(*MF);
187
188 for (auto &L : *MLI)
189 scheduleLoop(*L);
190
191 return false;
192}
193
194/// Attempt to perform the SMS algorithm on the specified loop. This function is
195/// the main entry point for the algorithm. The function identifies candidate
196/// loops, calculates the minimum initiation interval, and attempts to schedule
197/// the loop.
198bool MachinePipeliner::scheduleLoop(MachineLoop &L) {
199 bool Changed = false;
200 for (auto &InnerLoop : L)
201 Changed |= scheduleLoop(*InnerLoop);
202
203#ifndef NDEBUG
204 // Stop trying after reaching the limit (if any).
205 int Limit = SwpLoopLimit;
206 if (Limit >= 0) {
207 if (NumTries >= SwpLoopLimit)
208 return Changed;
209 NumTries++;
210 }
211#endif
212
213 if (!canPipelineLoop(L))
214 return Changed;
215
216 ++NumTrytoPipeline;
217
218 Changed = swingModuloScheduler(L);
219
220 return Changed;
221}
222
223/// Return true if the loop can be software pipelined. The algorithm is
224/// restricted to loops with a single basic block. Make sure that the
225/// branch in the loop can be analyzed.
226bool MachinePipeliner::canPipelineLoop(MachineLoop &L) {
227 if (L.getNumBlocks() != 1)
228 return false;
229
230 // Check if the branch can't be understood because we can't do pipelining
231 // if that's the case.
232 LI.TBB = nullptr;
233 LI.FBB = nullptr;
234 LI.BrCond.clear();
235 if (TII->analyzeBranch(*L.getHeader(), LI.TBB, LI.FBB, LI.BrCond))
236 return false;
237
238 LI.LoopInductionVar = nullptr;
239 LI.LoopCompare = nullptr;
240 if (TII->analyzeLoop(L, LI.LoopInductionVar, LI.LoopCompare))
241 return false;
242
243 if (!L.getLoopPreheader())
244 return false;
245
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000246 // Remove any subregisters from inputs to phi nodes.
247 preprocessPhiNodes(*L.getHeader());
Brendon Cahoon254f8892016-07-29 16:44:44 +0000248 return true;
249}
250
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000251void MachinePipeliner::preprocessPhiNodes(MachineBasicBlock &B) {
252 MachineRegisterInfo &MRI = MF->getRegInfo();
253 SlotIndexes &Slots = *getAnalysis<LiveIntervals>().getSlotIndexes();
254
255 for (MachineInstr &PI : make_range(B.begin(), B.getFirstNonPHI())) {
256 MachineOperand &DefOp = PI.getOperand(0);
257 assert(DefOp.getSubReg() == 0);
258 auto *RC = MRI.getRegClass(DefOp.getReg());
259
260 for (unsigned i = 1, n = PI.getNumOperands(); i != n; i += 2) {
261 MachineOperand &RegOp = PI.getOperand(i);
262 if (RegOp.getSubReg() == 0)
263 continue;
264
265 // If the operand uses a subregister, replace it with a new register
266 // without subregisters, and generate a copy to the new register.
267 unsigned NewReg = MRI.createVirtualRegister(RC);
268 MachineBasicBlock &PredB = *PI.getOperand(i+1).getMBB();
269 MachineBasicBlock::iterator At = PredB.getFirstTerminator();
270 const DebugLoc &DL = PredB.findDebugLoc(At);
271 auto Copy = BuildMI(PredB, At, DL, TII->get(TargetOpcode::COPY), NewReg)
272 .addReg(RegOp.getReg(), getRegState(RegOp),
273 RegOp.getSubReg());
274 Slots.insertMachineInstrInMaps(*Copy);
275 RegOp.setReg(NewReg);
276 RegOp.setSubReg(0);
277 }
278 }
279}
280
Brendon Cahoon254f8892016-07-29 16:44:44 +0000281/// The SMS algorithm consists of the following main steps:
282/// 1. Computation and analysis of the dependence graph.
283/// 2. Ordering of the nodes (instructions).
284/// 3. Attempt to Schedule the loop.
285bool MachinePipeliner::swingModuloScheduler(MachineLoop &L) {
286 assert(L.getBlocks().size() == 1 && "SMS works on single blocks only.");
287
288 SwingSchedulerDAG SMS(*this, L, getAnalysis<LiveIntervals>(), RegClassInfo);
289
290 MachineBasicBlock *MBB = L.getHeader();
291 // The kernel should not include any terminator instructions. These
292 // will be added back later.
293 SMS.startBlock(MBB);
294
295 // Compute the number of 'real' instructions in the basic block by
296 // ignoring terminators.
297 unsigned size = MBB->size();
298 for (MachineBasicBlock::iterator I = MBB->getFirstTerminator(),
299 E = MBB->instr_end();
300 I != E; ++I, --size)
301 ;
302
303 SMS.enterRegion(MBB, MBB->begin(), MBB->getFirstTerminator(), size);
304 SMS.schedule();
305 SMS.exitRegion();
306
307 SMS.finishBlock();
308 return SMS.hasNewSchedule();
309}
310
311/// We override the schedule function in ScheduleDAGInstrs to implement the
312/// scheduling part of the Swing Modulo Scheduling algorithm.
313void SwingSchedulerDAG::schedule() {
314 AliasAnalysis *AA = &Pass.getAnalysis<AAResultsWrapperPass>().getAAResults();
315 buildSchedGraph(AA);
316 addLoopCarriedDependences(AA);
317 updatePhiDependences();
318 Topo.InitDAGTopologicalSorting();
319 changeDependences();
Sumanth Gundapaneni62ac69d2018-10-18 15:51:16 +0000320 postprocessDAG();
Matthias Braun726e12c2018-09-19 00:23:35 +0000321 LLVM_DEBUG(dump());
Brendon Cahoon254f8892016-07-29 16:44:44 +0000322
323 NodeSetType NodeSets;
324 findCircuits(NodeSets);
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +0000325 NodeSetType Circuits = NodeSets;
Brendon Cahoon254f8892016-07-29 16:44:44 +0000326
327 // Calculate the MII.
328 unsigned ResMII = calculateResMII();
329 unsigned RecMII = calculateRecMII(NodeSets);
330
331 fuseRecs(NodeSets);
332
333 // This flag is used for testing and can cause correctness problems.
334 if (SwpIgnoreRecMII)
335 RecMII = 0;
336
337 MII = std::max(ResMII, RecMII);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000338 LLVM_DEBUG(dbgs() << "MII = " << MII << " (rec=" << RecMII
339 << ", res=" << ResMII << ")\n");
Brendon Cahoon254f8892016-07-29 16:44:44 +0000340
341 // Can't schedule a loop without a valid MII.
342 if (MII == 0)
343 return;
344
345 // Don't pipeline large loops.
346 if (SwpMaxMii != -1 && (int)MII > SwpMaxMii)
347 return;
348
349 computeNodeFunctions(NodeSets);
350
351 registerPressureFilter(NodeSets);
352
353 colocateNodeSets(NodeSets);
354
355 checkNodeSets(NodeSets);
356
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000357 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +0000358 for (auto &I : NodeSets) {
359 dbgs() << " Rec NodeSet ";
360 I.dump();
361 }
362 });
363
Krzysztof Parzyszek6c2f8682018-04-12 15:11:11 +0000364 std::stable_sort(NodeSets.begin(), NodeSets.end(), std::greater<NodeSet>());
Brendon Cahoon254f8892016-07-29 16:44:44 +0000365
366 groupRemainingNodes(NodeSets);
367
368 removeDuplicateNodes(NodeSets);
369
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000370 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +0000371 for (auto &I : NodeSets) {
372 dbgs() << " NodeSet ";
373 I.dump();
374 }
375 });
376
377 computeNodeOrder(NodeSets);
378
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +0000379 // check for node order issues
380 checkValidNodeOrder(Circuits);
381
Brendon Cahoon254f8892016-07-29 16:44:44 +0000382 SMSchedule Schedule(Pass.MF);
383 Scheduled = schedulePipeline(Schedule);
384
385 if (!Scheduled)
386 return;
387
388 unsigned numStages = Schedule.getMaxStageCount();
389 // No need to generate pipeline if there are no overlapped iterations.
390 if (numStages == 0)
391 return;
392
393 // Check that the maximum stage count is less than user-defined limit.
394 if (SwpMaxStages > -1 && (int)numStages > SwpMaxStages)
395 return;
396
397 generatePipelinedLoop(Schedule);
398 ++NumPipelined;
399}
400
401/// Clean up after the software pipeliner runs.
402void SwingSchedulerDAG::finishBlock() {
403 for (MachineInstr *I : NewMIs)
404 MF.DeleteMachineInstr(I);
405 NewMIs.clear();
406
407 // Call the superclass.
408 ScheduleDAGInstrs::finishBlock();
409}
410
411/// Return the register values for the operands of a Phi instruction.
412/// This function assume the instruction is a Phi.
413static void getPhiRegs(MachineInstr &Phi, MachineBasicBlock *Loop,
414 unsigned &InitVal, unsigned &LoopVal) {
415 assert(Phi.isPHI() && "Expecting a Phi.");
416
417 InitVal = 0;
418 LoopVal = 0;
419 for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2)
420 if (Phi.getOperand(i + 1).getMBB() != Loop)
421 InitVal = Phi.getOperand(i).getReg();
Simon Pilgrimfbfb19b2017-03-16 19:52:00 +0000422 else
Brendon Cahoon254f8892016-07-29 16:44:44 +0000423 LoopVal = Phi.getOperand(i).getReg();
424
425 assert(InitVal != 0 && LoopVal != 0 && "Unexpected Phi structure.");
426}
427
428/// Return the Phi register value that comes from the incoming block.
429static unsigned getInitPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) {
430 for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2)
431 if (Phi.getOperand(i + 1).getMBB() != LoopBB)
432 return Phi.getOperand(i).getReg();
433 return 0;
434}
435
Hiroshi Inoue8f976ba2018-01-17 12:29:38 +0000436/// Return the Phi register value that comes the loop block.
Brendon Cahoon254f8892016-07-29 16:44:44 +0000437static unsigned getLoopPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) {
438 for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2)
439 if (Phi.getOperand(i + 1).getMBB() == LoopBB)
440 return Phi.getOperand(i).getReg();
441 return 0;
442}
443
444/// Return true if SUb can be reached from SUa following the chain edges.
445static bool isSuccOrder(SUnit *SUa, SUnit *SUb) {
446 SmallPtrSet<SUnit *, 8> Visited;
447 SmallVector<SUnit *, 8> Worklist;
448 Worklist.push_back(SUa);
449 while (!Worklist.empty()) {
450 const SUnit *SU = Worklist.pop_back_val();
451 for (auto &SI : SU->Succs) {
452 SUnit *SuccSU = SI.getSUnit();
453 if (SI.getKind() == SDep::Order) {
454 if (Visited.count(SuccSU))
455 continue;
456 if (SuccSU == SUb)
457 return true;
458 Worklist.push_back(SuccSU);
459 Visited.insert(SuccSU);
460 }
461 }
462 }
463 return false;
464}
465
466/// Return true if the instruction causes a chain between memory
467/// references before and after it.
468static bool isDependenceBarrier(MachineInstr &MI, AliasAnalysis *AA) {
469 return MI.isCall() || MI.hasUnmodeledSideEffects() ||
470 (MI.hasOrderedMemoryRef() &&
Justin Lebard98cf002016-09-10 01:03:20 +0000471 (!MI.mayLoad() || !MI.isDereferenceableInvariantLoad(AA)));
Brendon Cahoon254f8892016-07-29 16:44:44 +0000472}
473
474/// Return the underlying objects for the memory references of an instruction.
475/// This function calls the code in ValueTracking, but first checks that the
476/// instruction has a memory operand.
477static void getUnderlyingObjects(MachineInstr *MI,
478 SmallVectorImpl<Value *> &Objs,
479 const DataLayout &DL) {
480 if (!MI->hasOneMemOperand())
481 return;
482 MachineMemOperand *MM = *MI->memoperands_begin();
483 if (!MM->getValue())
484 return;
485 GetUnderlyingObjects(const_cast<Value *>(MM->getValue()), Objs, DL);
Krzysztof Parzyszek9f041b12018-03-26 16:50:11 +0000486 for (Value *V : Objs) {
487 if (!isIdentifiedObject(V)) {
488 Objs.clear();
489 return;
490 }
491 Objs.push_back(V);
492 }
Brendon Cahoon254f8892016-07-29 16:44:44 +0000493}
494
495/// Add a chain edge between a load and store if the store can be an
496/// alias of the load on a subsequent iteration, i.e., a loop carried
497/// dependence. This code is very similar to the code in ScheduleDAGInstrs
498/// but that code doesn't create loop carried dependences.
499void SwingSchedulerDAG::addLoopCarriedDependences(AliasAnalysis *AA) {
500 MapVector<Value *, SmallVector<SUnit *, 4>> PendingLoads;
Krzysztof Parzyszek9f041b12018-03-26 16:50:11 +0000501 Value *UnknownValue =
502 UndefValue::get(Type::getVoidTy(MF.getFunction().getContext()));
Brendon Cahoon254f8892016-07-29 16:44:44 +0000503 for (auto &SU : SUnits) {
504 MachineInstr &MI = *SU.getInstr();
505 if (isDependenceBarrier(MI, AA))
506 PendingLoads.clear();
507 else if (MI.mayLoad()) {
508 SmallVector<Value *, 4> Objs;
509 getUnderlyingObjects(&MI, Objs, MF.getDataLayout());
Krzysztof Parzyszek9f041b12018-03-26 16:50:11 +0000510 if (Objs.empty())
511 Objs.push_back(UnknownValue);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000512 for (auto V : Objs) {
513 SmallVector<SUnit *, 4> &SUs = PendingLoads[V];
514 SUs.push_back(&SU);
515 }
516 } else if (MI.mayStore()) {
517 SmallVector<Value *, 4> Objs;
518 getUnderlyingObjects(&MI, Objs, MF.getDataLayout());
Krzysztof Parzyszek9f041b12018-03-26 16:50:11 +0000519 if (Objs.empty())
520 Objs.push_back(UnknownValue);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000521 for (auto V : Objs) {
522 MapVector<Value *, SmallVector<SUnit *, 4>>::iterator I =
523 PendingLoads.find(V);
524 if (I == PendingLoads.end())
525 continue;
526 for (auto Load : I->second) {
527 if (isSuccOrder(Load, &SU))
528 continue;
529 MachineInstr &LdMI = *Load->getInstr();
530 // First, perform the cheaper check that compares the base register.
531 // If they are the same and the load offset is less than the store
532 // offset, then mark the dependence as loop carried potentially.
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +0000533 MachineOperand *BaseOp1, *BaseOp2;
Brendon Cahoon254f8892016-07-29 16:44:44 +0000534 int64_t Offset1, Offset2;
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +0000535 if (TII->getMemOperandWithOffset(LdMI, BaseOp1, Offset1, TRI) &&
536 TII->getMemOperandWithOffset(MI, BaseOp2, Offset2, TRI)) {
537 if (BaseOp1->isIdenticalTo(*BaseOp2) &&
538 (int)Offset1 < (int)Offset2) {
Krzysztof Parzyszek9f041b12018-03-26 16:50:11 +0000539 assert(TII->areMemAccessesTriviallyDisjoint(LdMI, MI, AA) &&
540 "What happened to the chain edge?");
541 SDep Dep(Load, SDep::Barrier);
542 Dep.setLatency(1);
543 SU.addPred(Dep);
544 continue;
545 }
Brendon Cahoon254f8892016-07-29 16:44:44 +0000546 }
547 // Second, the more expensive check that uses alias analysis on the
548 // base registers. If they alias, and the load offset is less than
549 // the store offset, the mark the dependence as loop carried.
550 if (!AA) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000551 SDep Dep(Load, SDep::Barrier);
552 Dep.setLatency(1);
553 SU.addPred(Dep);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000554 continue;
555 }
556 MachineMemOperand *MMO1 = *LdMI.memoperands_begin();
557 MachineMemOperand *MMO2 = *MI.memoperands_begin();
558 if (!MMO1->getValue() || !MMO2->getValue()) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000559 SDep Dep(Load, SDep::Barrier);
560 Dep.setLatency(1);
561 SU.addPred(Dep);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000562 continue;
563 }
564 if (MMO1->getValue() == MMO2->getValue() &&
565 MMO1->getOffset() <= MMO2->getOffset()) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000566 SDep Dep(Load, SDep::Barrier);
567 Dep.setLatency(1);
568 SU.addPred(Dep);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000569 continue;
570 }
571 AliasResult AAResult = AA->alias(
George Burgess IV6ef80022018-10-10 21:28:44 +0000572 MemoryLocation(MMO1->getValue(), LocationSize::unknown(),
Brendon Cahoon254f8892016-07-29 16:44:44 +0000573 MMO1->getAAInfo()),
George Burgess IV6ef80022018-10-10 21:28:44 +0000574 MemoryLocation(MMO2->getValue(), LocationSize::unknown(),
Brendon Cahoon254f8892016-07-29 16:44:44 +0000575 MMO2->getAAInfo()));
576
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000577 if (AAResult != NoAlias) {
578 SDep Dep(Load, SDep::Barrier);
579 Dep.setLatency(1);
580 SU.addPred(Dep);
581 }
Brendon Cahoon254f8892016-07-29 16:44:44 +0000582 }
583 }
584 }
585 }
586}
587
588/// Update the phi dependences to the DAG because ScheduleDAGInstrs no longer
589/// processes dependences for PHIs. This function adds true dependences
590/// from a PHI to a use, and a loop carried dependence from the use to the
591/// PHI. The loop carried dependence is represented as an anti dependence
592/// edge. This function also removes chain dependences between unrelated
593/// PHIs.
594void SwingSchedulerDAG::updatePhiDependences() {
595 SmallVector<SDep, 4> RemoveDeps;
596 const TargetSubtargetInfo &ST = MF.getSubtarget<TargetSubtargetInfo>();
597
598 // Iterate over each DAG node.
599 for (SUnit &I : SUnits) {
600 RemoveDeps.clear();
601 // Set to true if the instruction has an operand defined by a Phi.
602 unsigned HasPhiUse = 0;
603 unsigned HasPhiDef = 0;
604 MachineInstr *MI = I.getInstr();
605 // Iterate over each operand, and we process the definitions.
606 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
607 MOE = MI->operands_end();
608 MOI != MOE; ++MOI) {
609 if (!MOI->isReg())
610 continue;
611 unsigned Reg = MOI->getReg();
612 if (MOI->isDef()) {
613 // If the register is used by a Phi, then create an anti dependence.
614 for (MachineRegisterInfo::use_instr_iterator
615 UI = MRI.use_instr_begin(Reg),
616 UE = MRI.use_instr_end();
617 UI != UE; ++UI) {
618 MachineInstr *UseMI = &*UI;
619 SUnit *SU = getSUnit(UseMI);
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000620 if (SU != nullptr && UseMI->isPHI()) {
Brendon Cahoon254f8892016-07-29 16:44:44 +0000621 if (!MI->isPHI()) {
622 SDep Dep(SU, SDep::Anti, Reg);
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +0000623 Dep.setLatency(1);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000624 I.addPred(Dep);
625 } else {
626 HasPhiDef = Reg;
627 // Add a chain edge to a dependent Phi that isn't an existing
628 // predecessor.
629 if (SU->NodeNum < I.NodeNum && !I.isPred(SU))
630 I.addPred(SDep(SU, SDep::Barrier));
631 }
632 }
633 }
634 } else if (MOI->isUse()) {
635 // If the register is defined by a Phi, then create a true dependence.
636 MachineInstr *DefMI = MRI.getUniqueVRegDef(Reg);
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000637 if (DefMI == nullptr)
Brendon Cahoon254f8892016-07-29 16:44:44 +0000638 continue;
639 SUnit *SU = getSUnit(DefMI);
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000640 if (SU != nullptr && DefMI->isPHI()) {
Brendon Cahoon254f8892016-07-29 16:44:44 +0000641 if (!MI->isPHI()) {
642 SDep Dep(SU, SDep::Data, Reg);
643 Dep.setLatency(0);
644 ST.adjustSchedDependency(SU, &I, Dep);
645 I.addPred(Dep);
646 } else {
647 HasPhiUse = Reg;
648 // Add a chain edge to a dependent Phi that isn't an existing
649 // predecessor.
650 if (SU->NodeNum < I.NodeNum && !I.isPred(SU))
651 I.addPred(SDep(SU, SDep::Barrier));
652 }
653 }
654 }
655 }
656 // Remove order dependences from an unrelated Phi.
657 if (!SwpPruneDeps)
658 continue;
659 for (auto &PI : I.Preds) {
660 MachineInstr *PMI = PI.getSUnit()->getInstr();
661 if (PMI->isPHI() && PI.getKind() == SDep::Order) {
662 if (I.getInstr()->isPHI()) {
663 if (PMI->getOperand(0).getReg() == HasPhiUse)
664 continue;
665 if (getLoopPhiReg(*PMI, PMI->getParent()) == HasPhiDef)
666 continue;
667 }
668 RemoveDeps.push_back(PI);
669 }
670 }
671 for (int i = 0, e = RemoveDeps.size(); i != e; ++i)
672 I.removePred(RemoveDeps[i]);
673 }
674}
675
676/// Iterate over each DAG node and see if we can change any dependences
677/// in order to reduce the recurrence MII.
678void SwingSchedulerDAG::changeDependences() {
679 // See if an instruction can use a value from the previous iteration.
680 // If so, we update the base and offset of the instruction and change
681 // the dependences.
682 for (SUnit &I : SUnits) {
683 unsigned BasePos = 0, OffsetPos = 0, NewBase = 0;
684 int64_t NewOffset = 0;
685 if (!canUseLastOffsetValue(I.getInstr(), BasePos, OffsetPos, NewBase,
686 NewOffset))
687 continue;
688
689 // Get the MI and SUnit for the instruction that defines the original base.
690 unsigned OrigBase = I.getInstr()->getOperand(BasePos).getReg();
691 MachineInstr *DefMI = MRI.getUniqueVRegDef(OrigBase);
692 if (!DefMI)
693 continue;
694 SUnit *DefSU = getSUnit(DefMI);
695 if (!DefSU)
696 continue;
697 // Get the MI and SUnit for the instruction that defins the new base.
698 MachineInstr *LastMI = MRI.getUniqueVRegDef(NewBase);
699 if (!LastMI)
700 continue;
701 SUnit *LastSU = getSUnit(LastMI);
702 if (!LastSU)
703 continue;
704
705 if (Topo.IsReachable(&I, LastSU))
706 continue;
707
708 // Remove the dependence. The value now depends on a prior iteration.
709 SmallVector<SDep, 4> Deps;
710 for (SUnit::pred_iterator P = I.Preds.begin(), E = I.Preds.end(); P != E;
711 ++P)
712 if (P->getSUnit() == DefSU)
713 Deps.push_back(*P);
714 for (int i = 0, e = Deps.size(); i != e; i++) {
715 Topo.RemovePred(&I, Deps[i].getSUnit());
716 I.removePred(Deps[i]);
717 }
718 // Remove the chain dependence between the instructions.
719 Deps.clear();
720 for (auto &P : LastSU->Preds)
721 if (P.getSUnit() == &I && P.getKind() == SDep::Order)
722 Deps.push_back(P);
723 for (int i = 0, e = Deps.size(); i != e; i++) {
724 Topo.RemovePred(LastSU, Deps[i].getSUnit());
725 LastSU->removePred(Deps[i]);
726 }
727
728 // Add a dependence between the new instruction and the instruction
729 // that defines the new base.
730 SDep Dep(&I, SDep::Anti, NewBase);
Sumanth Gundapaneni8916e432018-10-11 19:42:46 +0000731 Topo.AddPred(LastSU, &I);
Brendon Cahoon254f8892016-07-29 16:44:44 +0000732 LastSU->addPred(Dep);
733
734 // Remember the base and offset information so that we can update the
735 // instruction during code generation.
736 InstrChanges[&I] = std::make_pair(NewBase, NewOffset);
737 }
738}
739
740namespace {
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000741
Brendon Cahoon254f8892016-07-29 16:44:44 +0000742// FuncUnitSorter - Comparison operator used to sort instructions by
743// the number of functional unit choices.
744struct FuncUnitSorter {
745 const InstrItineraryData *InstrItins;
746 DenseMap<unsigned, unsigned> Resources;
747
Eugene Zelenko32a40562017-09-11 23:00:48 +0000748 FuncUnitSorter(const InstrItineraryData *IID) : InstrItins(IID) {}
749
Brendon Cahoon254f8892016-07-29 16:44:44 +0000750 // Compute the number of functional unit alternatives needed
751 // at each stage, and take the minimum value. We prioritize the
752 // instructions by the least number of choices first.
753 unsigned minFuncUnits(const MachineInstr *Inst, unsigned &F) const {
754 unsigned schedClass = Inst->getDesc().getSchedClass();
755 unsigned min = UINT_MAX;
756 for (const InstrStage *IS = InstrItins->beginStage(schedClass),
757 *IE = InstrItins->endStage(schedClass);
758 IS != IE; ++IS) {
759 unsigned funcUnits = IS->getUnits();
760 unsigned numAlternatives = countPopulation(funcUnits);
761 if (numAlternatives < min) {
762 min = numAlternatives;
763 F = funcUnits;
764 }
765 }
766 return min;
767 }
768
769 // Compute the critical resources needed by the instruction. This
770 // function records the functional units needed by instructions that
771 // must use only one functional unit. We use this as a tie breaker
772 // for computing the resource MII. The instrutions that require
773 // the same, highly used, functional unit have high priority.
774 void calcCriticalResources(MachineInstr &MI) {
775 unsigned SchedClass = MI.getDesc().getSchedClass();
776 for (const InstrStage *IS = InstrItins->beginStage(SchedClass),
777 *IE = InstrItins->endStage(SchedClass);
778 IS != IE; ++IS) {
779 unsigned FuncUnits = IS->getUnits();
780 if (countPopulation(FuncUnits) == 1)
781 Resources[FuncUnits]++;
782 }
783 }
784
Brendon Cahoon254f8892016-07-29 16:44:44 +0000785 /// Return true if IS1 has less priority than IS2.
786 bool operator()(const MachineInstr *IS1, const MachineInstr *IS2) const {
787 unsigned F1 = 0, F2 = 0;
788 unsigned MFUs1 = minFuncUnits(IS1, F1);
789 unsigned MFUs2 = minFuncUnits(IS2, F2);
790 if (MFUs1 == 1 && MFUs2 == 1)
791 return Resources.lookup(F1) < Resources.lookup(F2);
792 return MFUs1 > MFUs2;
793 }
794};
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000795
796} // end anonymous namespace
Brendon Cahoon254f8892016-07-29 16:44:44 +0000797
798/// Calculate the resource constrained minimum initiation interval for the
799/// specified loop. We use the DFA to model the resources needed for
800/// each instruction, and we ignore dependences. A different DFA is created
801/// for each cycle that is required. When adding a new instruction, we attempt
802/// to add it to each existing DFA, until a legal space is found. If the
803/// instruction cannot be reserved in an existing DFA, we create a new one.
804unsigned SwingSchedulerDAG::calculateResMII() {
805 SmallVector<DFAPacketizer *, 8> Resources;
806 MachineBasicBlock *MBB = Loop.getHeader();
807 Resources.push_back(TII->CreateTargetScheduleState(MF.getSubtarget()));
808
809 // Sort the instructions by the number of available choices for scheduling,
810 // least to most. Use the number of critical resources as the tie breaker.
811 FuncUnitSorter FUS =
812 FuncUnitSorter(MF.getSubtarget().getInstrItineraryData());
813 for (MachineBasicBlock::iterator I = MBB->getFirstNonPHI(),
814 E = MBB->getFirstTerminator();
815 I != E; ++I)
816 FUS.calcCriticalResources(*I);
817 PriorityQueue<MachineInstr *, std::vector<MachineInstr *>, FuncUnitSorter>
818 FuncUnitOrder(FUS);
819
820 for (MachineBasicBlock::iterator I = MBB->getFirstNonPHI(),
821 E = MBB->getFirstTerminator();
822 I != E; ++I)
823 FuncUnitOrder.push(&*I);
824
825 while (!FuncUnitOrder.empty()) {
826 MachineInstr *MI = FuncUnitOrder.top();
827 FuncUnitOrder.pop();
828 if (TII->isZeroCost(MI->getOpcode()))
829 continue;
830 // Attempt to reserve the instruction in an existing DFA. At least one
831 // DFA is needed for each cycle.
832 unsigned NumCycles = getSUnit(MI)->Latency;
833 unsigned ReservedCycles = 0;
834 SmallVectorImpl<DFAPacketizer *>::iterator RI = Resources.begin();
835 SmallVectorImpl<DFAPacketizer *>::iterator RE = Resources.end();
836 for (unsigned C = 0; C < NumCycles; ++C)
837 while (RI != RE) {
838 if ((*RI++)->canReserveResources(*MI)) {
839 ++ReservedCycles;
840 break;
841 }
842 }
843 // Start reserving resources using existing DFAs.
844 for (unsigned C = 0; C < ReservedCycles; ++C) {
845 --RI;
846 (*RI)->reserveResources(*MI);
847 }
848 // Add new DFAs, if needed, to reserve resources.
849 for (unsigned C = ReservedCycles; C < NumCycles; ++C) {
850 DFAPacketizer *NewResource =
851 TII->CreateTargetScheduleState(MF.getSubtarget());
852 assert(NewResource->canReserveResources(*MI) && "Reserve error.");
853 NewResource->reserveResources(*MI);
854 Resources.push_back(NewResource);
855 }
856 }
857 int Resmii = Resources.size();
858 // Delete the memory for each of the DFAs that were created earlier.
859 for (DFAPacketizer *RI : Resources) {
860 DFAPacketizer *D = RI;
861 delete D;
862 }
863 Resources.clear();
864 return Resmii;
865}
866
867/// Calculate the recurrence-constrainted minimum initiation interval.
868/// Iterate over each circuit. Compute the delay(c) and distance(c)
869/// for each circuit. The II needs to satisfy the inequality
870/// delay(c) - II*distance(c) <= 0. For each circuit, choose the smallest
Hiroshi Inouec73b6d62018-06-20 05:29:26 +0000871/// II that satisfies the inequality, and the RecMII is the maximum
Brendon Cahoon254f8892016-07-29 16:44:44 +0000872/// of those values.
873unsigned SwingSchedulerDAG::calculateRecMII(NodeSetType &NodeSets) {
874 unsigned RecMII = 0;
875
876 for (NodeSet &Nodes : NodeSets) {
Eugene Zelenko32a40562017-09-11 23:00:48 +0000877 if (Nodes.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +0000878 continue;
879
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +0000880 unsigned Delay = Nodes.getLatency();
Brendon Cahoon254f8892016-07-29 16:44:44 +0000881 unsigned Distance = 1;
882
883 // ii = ceil(delay / distance)
884 unsigned CurMII = (Delay + Distance - 1) / Distance;
885 Nodes.setRecMII(CurMII);
886 if (CurMII > RecMII)
887 RecMII = CurMII;
888 }
889
890 return RecMII;
891}
892
893/// Swap all the anti dependences in the DAG. That means it is no longer a DAG,
894/// but we do this to find the circuits, and then change them back.
895static void swapAntiDependences(std::vector<SUnit> &SUnits) {
896 SmallVector<std::pair<SUnit *, SDep>, 8> DepsAdded;
897 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
898 SUnit *SU = &SUnits[i];
899 for (SUnit::pred_iterator IP = SU->Preds.begin(), EP = SU->Preds.end();
900 IP != EP; ++IP) {
901 if (IP->getKind() != SDep::Anti)
902 continue;
903 DepsAdded.push_back(std::make_pair(SU, *IP));
904 }
905 }
906 for (SmallVector<std::pair<SUnit *, SDep>, 8>::iterator I = DepsAdded.begin(),
907 E = DepsAdded.end();
908 I != E; ++I) {
909 // Remove this anti dependency and add one in the reverse direction.
910 SUnit *SU = I->first;
911 SDep &D = I->second;
912 SUnit *TargetSU = D.getSUnit();
913 unsigned Reg = D.getReg();
914 unsigned Lat = D.getLatency();
915 SU->removePred(D);
916 SDep Dep(SU, SDep::Anti, Reg);
917 Dep.setLatency(Lat);
918 TargetSU->addPred(Dep);
919 }
920}
921
922/// Create the adjacency structure of the nodes in the graph.
923void SwingSchedulerDAG::Circuits::createAdjacencyStructure(
924 SwingSchedulerDAG *DAG) {
925 BitVector Added(SUnits.size());
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +0000926 DenseMap<int, int> OutputDeps;
Brendon Cahoon254f8892016-07-29 16:44:44 +0000927 for (int i = 0, e = SUnits.size(); i != e; ++i) {
928 Added.reset();
929 // Add any successor to the adjacency matrix and exclude duplicates.
930 for (auto &SI : SUnits[i].Succs) {
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +0000931 // Only create a back-edge on the first and last nodes of a dependence
932 // chain. This records any chains and adds them later.
933 if (SI.getKind() == SDep::Output) {
934 int N = SI.getSUnit()->NodeNum;
935 int BackEdge = i;
936 auto Dep = OutputDeps.find(BackEdge);
937 if (Dep != OutputDeps.end()) {
938 BackEdge = Dep->second;
939 OutputDeps.erase(Dep);
940 }
941 OutputDeps[N] = BackEdge;
942 }
Sumanth Gundapaneniada0f512018-10-25 21:27:08 +0000943 // Do not process a boundary node, an artificial node.
944 // A back-edge is processed only if it goes to a Phi.
945 if (SI.getSUnit()->isBoundaryNode() || SI.isArtificial() ||
Brendon Cahoon254f8892016-07-29 16:44:44 +0000946 (SI.getKind() == SDep::Anti && !SI.getSUnit()->getInstr()->isPHI()))
947 continue;
948 int N = SI.getSUnit()->NodeNum;
949 if (!Added.test(N)) {
950 AdjK[i].push_back(N);
951 Added.set(N);
952 }
953 }
954 // A chain edge between a store and a load is treated as a back-edge in the
955 // adjacency matrix.
956 for (auto &PI : SUnits[i].Preds) {
957 if (!SUnits[i].getInstr()->mayStore() ||
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +0000958 !DAG->isLoopCarriedDep(&SUnits[i], PI, false))
Brendon Cahoon254f8892016-07-29 16:44:44 +0000959 continue;
960 if (PI.getKind() == SDep::Order && PI.getSUnit()->getInstr()->mayLoad()) {
961 int N = PI.getSUnit()->NodeNum;
962 if (!Added.test(N)) {
963 AdjK[i].push_back(N);
964 Added.set(N);
965 }
966 }
967 }
968 }
Hiroshi Inouedad8c6a2019-01-09 05:11:10 +0000969 // Add back-edges in the adjacency matrix for the output dependences.
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +0000970 for (auto &OD : OutputDeps)
971 if (!Added.test(OD.second)) {
972 AdjK[OD.first].push_back(OD.second);
973 Added.set(OD.second);
974 }
Brendon Cahoon254f8892016-07-29 16:44:44 +0000975}
976
977/// Identify an elementary circuit in the dependence graph starting at the
978/// specified node.
979bool SwingSchedulerDAG::Circuits::circuit(int V, int S, NodeSetType &NodeSets,
980 bool HasBackedge) {
981 SUnit *SV = &SUnits[V];
982 bool F = false;
983 Stack.insert(SV);
984 Blocked.set(V);
985
986 for (auto W : AdjK[V]) {
987 if (NumPaths > MaxPaths)
988 break;
989 if (W < S)
990 continue;
991 if (W == S) {
992 if (!HasBackedge)
993 NodeSets.push_back(NodeSet(Stack.begin(), Stack.end()));
994 F = true;
995 ++NumPaths;
996 break;
997 } else if (!Blocked.test(W)) {
Sumanth Gundapaneni77418a32018-10-11 19:45:07 +0000998 if (circuit(W, S, NodeSets,
999 Node2Idx->at(W) < Node2Idx->at(V) ? true : HasBackedge))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001000 F = true;
1001 }
1002 }
1003
1004 if (F)
1005 unblock(V);
1006 else {
1007 for (auto W : AdjK[V]) {
1008 if (W < S)
1009 continue;
1010 if (B[W].count(SV) == 0)
1011 B[W].insert(SV);
1012 }
1013 }
1014 Stack.pop_back();
1015 return F;
1016}
1017
1018/// Unblock a node in the circuit finding algorithm.
1019void SwingSchedulerDAG::Circuits::unblock(int U) {
1020 Blocked.reset(U);
1021 SmallPtrSet<SUnit *, 4> &BU = B[U];
1022 while (!BU.empty()) {
1023 SmallPtrSet<SUnit *, 4>::iterator SI = BU.begin();
1024 assert(SI != BU.end() && "Invalid B set.");
1025 SUnit *W = *SI;
1026 BU.erase(W);
1027 if (Blocked.test(W->NodeNum))
1028 unblock(W->NodeNum);
1029 }
1030}
1031
1032/// Identify all the elementary circuits in the dependence graph using
1033/// Johnson's circuit algorithm.
1034void SwingSchedulerDAG::findCircuits(NodeSetType &NodeSets) {
1035 // Swap all the anti dependences in the DAG. That means it is no longer a DAG,
1036 // but we do this to find the circuits, and then change them back.
1037 swapAntiDependences(SUnits);
1038
Sumanth Gundapaneni77418a32018-10-11 19:45:07 +00001039 Circuits Cir(SUnits, Topo);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001040 // Create the adjacency structure.
1041 Cir.createAdjacencyStructure(this);
1042 for (int i = 0, e = SUnits.size(); i != e; ++i) {
1043 Cir.reset();
1044 Cir.circuit(i, i, NodeSets);
1045 }
1046
1047 // Change the dependences back so that we've created a DAG again.
1048 swapAntiDependences(SUnits);
1049}
1050
Sumanth Gundapaneni62ac69d2018-10-18 15:51:16 +00001051// Create artificial dependencies between the source of COPY/REG_SEQUENCE that
1052// is loop-carried to the USE in next iteration. This will help pipeliner avoid
1053// additional copies that are needed across iterations. An artificial dependence
1054// edge is added from USE to SOURCE of COPY/REG_SEQUENCE.
1055
1056// PHI-------Anti-Dep-----> COPY/REG_SEQUENCE (loop-carried)
1057// SRCOfCopY------True-Dep---> COPY/REG_SEQUENCE
1058// PHI-------True-Dep------> USEOfPhi
1059
1060// The mutation creates
1061// USEOfPHI -------Artificial-Dep---> SRCOfCopy
1062
1063// This overall will ensure, the USEOfPHI is scheduled before SRCOfCopy
1064// (since USE is a predecessor), implies, the COPY/ REG_SEQUENCE is scheduled
1065// late to avoid additional copies across iterations. The possible scheduling
1066// order would be
1067// USEOfPHI --- SRCOfCopy--- COPY/REG_SEQUENCE.
1068
1069void SwingSchedulerDAG::CopyToPhiMutation::apply(ScheduleDAGInstrs *DAG) {
1070 for (SUnit &SU : DAG->SUnits) {
1071 // Find the COPY/REG_SEQUENCE instruction.
1072 if (!SU.getInstr()->isCopy() && !SU.getInstr()->isRegSequence())
1073 continue;
1074
1075 // Record the loop carried PHIs.
1076 SmallVector<SUnit *, 4> PHISUs;
1077 // Record the SrcSUs that feed the COPY/REG_SEQUENCE instructions.
1078 SmallVector<SUnit *, 4> SrcSUs;
1079
1080 for (auto &Dep : SU.Preds) {
1081 SUnit *TmpSU = Dep.getSUnit();
1082 MachineInstr *TmpMI = TmpSU->getInstr();
1083 SDep::Kind DepKind = Dep.getKind();
1084 // Save the loop carried PHI.
1085 if (DepKind == SDep::Anti && TmpMI->isPHI())
1086 PHISUs.push_back(TmpSU);
1087 // Save the source of COPY/REG_SEQUENCE.
1088 // If the source has no pre-decessors, we will end up creating cycles.
1089 else if (DepKind == SDep::Data && !TmpMI->isPHI() && TmpSU->NumPreds > 0)
1090 SrcSUs.push_back(TmpSU);
1091 }
1092
1093 if (PHISUs.size() == 0 || SrcSUs.size() == 0)
1094 continue;
1095
1096 // Find the USEs of PHI. If the use is a PHI or REG_SEQUENCE, push back this
1097 // SUnit to the container.
1098 SmallVector<SUnit *, 8> UseSUs;
1099 for (auto I = PHISUs.begin(); I != PHISUs.end(); ++I) {
1100 for (auto &Dep : (*I)->Succs) {
1101 if (Dep.getKind() != SDep::Data)
1102 continue;
1103
1104 SUnit *TmpSU = Dep.getSUnit();
1105 MachineInstr *TmpMI = TmpSU->getInstr();
1106 if (TmpMI->isPHI() || TmpMI->isRegSequence()) {
1107 PHISUs.push_back(TmpSU);
1108 continue;
1109 }
1110 UseSUs.push_back(TmpSU);
1111 }
1112 }
1113
1114 if (UseSUs.size() == 0)
1115 continue;
1116
1117 SwingSchedulerDAG *SDAG = cast<SwingSchedulerDAG>(DAG);
1118 // Add the artificial dependencies if it does not form a cycle.
1119 for (auto I : UseSUs) {
1120 for (auto Src : SrcSUs) {
1121 if (!SDAG->Topo.IsReachable(I, Src) && Src != I) {
1122 Src->addPred(SDep(I, SDep::Artificial));
1123 SDAG->Topo.AddPred(Src, I);
1124 }
1125 }
1126 }
1127 }
1128}
1129
Brendon Cahoon254f8892016-07-29 16:44:44 +00001130/// Return true for DAG nodes that we ignore when computing the cost functions.
Hiroshi Inouec73b6d62018-06-20 05:29:26 +00001131/// We ignore the back-edge recurrence in order to avoid unbounded recursion
Brendon Cahoon254f8892016-07-29 16:44:44 +00001132/// in the calculation of the ASAP, ALAP, etc functions.
1133static bool ignoreDependence(const SDep &D, bool isPred) {
1134 if (D.isArtificial())
1135 return true;
1136 return D.getKind() == SDep::Anti && isPred;
1137}
1138
1139/// Compute several functions need to order the nodes for scheduling.
1140/// ASAP - Earliest time to schedule a node.
1141/// ALAP - Latest time to schedule a node.
1142/// MOV - Mobility function, difference between ALAP and ASAP.
1143/// D - Depth of each node.
1144/// H - Height of each node.
1145void SwingSchedulerDAG::computeNodeFunctions(NodeSetType &NodeSets) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00001146 ScheduleInfo.resize(SUnits.size());
1147
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001148 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001149 for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(),
1150 E = Topo.end();
1151 I != E; ++I) {
Matthias Braun726e12c2018-09-19 00:23:35 +00001152 const SUnit &SU = SUnits[*I];
1153 dumpNode(SU);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001154 }
1155 });
1156
1157 int maxASAP = 0;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001158 // Compute ASAP and ZeroLatencyDepth.
Brendon Cahoon254f8892016-07-29 16:44:44 +00001159 for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(),
1160 E = Topo.end();
1161 I != E; ++I) {
1162 int asap = 0;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001163 int zeroLatencyDepth = 0;
Brendon Cahoon254f8892016-07-29 16:44:44 +00001164 SUnit *SU = &SUnits[*I];
1165 for (SUnit::const_pred_iterator IP = SU->Preds.begin(),
1166 EP = SU->Preds.end();
1167 IP != EP; ++IP) {
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001168 SUnit *pred = IP->getSUnit();
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00001169 if (IP->getLatency() == 0)
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001170 zeroLatencyDepth =
1171 std::max(zeroLatencyDepth, getZeroLatencyDepth(pred) + 1);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001172 if (ignoreDependence(*IP, true))
1173 continue;
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00001174 asap = std::max(asap, (int)(getASAP(pred) + IP->getLatency() -
Brendon Cahoon254f8892016-07-29 16:44:44 +00001175 getDistance(pred, SU, *IP) * MII));
1176 }
1177 maxASAP = std::max(maxASAP, asap);
1178 ScheduleInfo[*I].ASAP = asap;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001179 ScheduleInfo[*I].ZeroLatencyDepth = zeroLatencyDepth;
Brendon Cahoon254f8892016-07-29 16:44:44 +00001180 }
1181
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001182 // Compute ALAP, ZeroLatencyHeight, and MOV.
Brendon Cahoon254f8892016-07-29 16:44:44 +00001183 for (ScheduleDAGTopologicalSort::const_reverse_iterator I = Topo.rbegin(),
1184 E = Topo.rend();
1185 I != E; ++I) {
1186 int alap = maxASAP;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001187 int zeroLatencyHeight = 0;
Brendon Cahoon254f8892016-07-29 16:44:44 +00001188 SUnit *SU = &SUnits[*I];
1189 for (SUnit::const_succ_iterator IS = SU->Succs.begin(),
1190 ES = SU->Succs.end();
1191 IS != ES; ++IS) {
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001192 SUnit *succ = IS->getSUnit();
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00001193 if (IS->getLatency() == 0)
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001194 zeroLatencyHeight =
1195 std::max(zeroLatencyHeight, getZeroLatencyHeight(succ) + 1);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001196 if (ignoreDependence(*IS, true))
1197 continue;
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00001198 alap = std::min(alap, (int)(getALAP(succ) - IS->getLatency() +
Brendon Cahoon254f8892016-07-29 16:44:44 +00001199 getDistance(SU, succ, *IS) * MII));
1200 }
1201
1202 ScheduleInfo[*I].ALAP = alap;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001203 ScheduleInfo[*I].ZeroLatencyHeight = zeroLatencyHeight;
Brendon Cahoon254f8892016-07-29 16:44:44 +00001204 }
1205
1206 // After computing the node functions, compute the summary for each node set.
1207 for (NodeSet &I : NodeSets)
1208 I.computeNodeSetInfo(this);
1209
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001210 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001211 for (unsigned i = 0; i < SUnits.size(); i++) {
1212 dbgs() << "\tNode " << i << ":\n";
1213 dbgs() << "\t ASAP = " << getASAP(&SUnits[i]) << "\n";
1214 dbgs() << "\t ALAP = " << getALAP(&SUnits[i]) << "\n";
1215 dbgs() << "\t MOV = " << getMOV(&SUnits[i]) << "\n";
1216 dbgs() << "\t D = " << getDepth(&SUnits[i]) << "\n";
1217 dbgs() << "\t H = " << getHeight(&SUnits[i]) << "\n";
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001218 dbgs() << "\t ZLD = " << getZeroLatencyDepth(&SUnits[i]) << "\n";
1219 dbgs() << "\t ZLH = " << getZeroLatencyHeight(&SUnits[i]) << "\n";
Brendon Cahoon254f8892016-07-29 16:44:44 +00001220 }
1221 });
1222}
1223
1224/// Compute the Pred_L(O) set, as defined in the paper. The set is defined
1225/// as the predecessors of the elements of NodeOrder that are not also in
1226/// NodeOrder.
1227static bool pred_L(SetVector<SUnit *> &NodeOrder,
1228 SmallSetVector<SUnit *, 8> &Preds,
1229 const NodeSet *S = nullptr) {
1230 Preds.clear();
1231 for (SetVector<SUnit *>::iterator I = NodeOrder.begin(), E = NodeOrder.end();
1232 I != E; ++I) {
1233 for (SUnit::pred_iterator PI = (*I)->Preds.begin(), PE = (*I)->Preds.end();
1234 PI != PE; ++PI) {
1235 if (S && S->count(PI->getSUnit()) == 0)
1236 continue;
1237 if (ignoreDependence(*PI, true))
1238 continue;
1239 if (NodeOrder.count(PI->getSUnit()) == 0)
1240 Preds.insert(PI->getSUnit());
1241 }
1242 // Back-edges are predecessors with an anti-dependence.
1243 for (SUnit::const_succ_iterator IS = (*I)->Succs.begin(),
1244 ES = (*I)->Succs.end();
1245 IS != ES; ++IS) {
1246 if (IS->getKind() != SDep::Anti)
1247 continue;
1248 if (S && S->count(IS->getSUnit()) == 0)
1249 continue;
1250 if (NodeOrder.count(IS->getSUnit()) == 0)
1251 Preds.insert(IS->getSUnit());
1252 }
1253 }
Eugene Zelenko32a40562017-09-11 23:00:48 +00001254 return !Preds.empty();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001255}
1256
1257/// Compute the Succ_L(O) set, as defined in the paper. The set is defined
1258/// as the successors of the elements of NodeOrder that are not also in
1259/// NodeOrder.
1260static bool succ_L(SetVector<SUnit *> &NodeOrder,
1261 SmallSetVector<SUnit *, 8> &Succs,
1262 const NodeSet *S = nullptr) {
1263 Succs.clear();
1264 for (SetVector<SUnit *>::iterator I = NodeOrder.begin(), E = NodeOrder.end();
1265 I != E; ++I) {
1266 for (SUnit::succ_iterator SI = (*I)->Succs.begin(), SE = (*I)->Succs.end();
1267 SI != SE; ++SI) {
1268 if (S && S->count(SI->getSUnit()) == 0)
1269 continue;
1270 if (ignoreDependence(*SI, false))
1271 continue;
1272 if (NodeOrder.count(SI->getSUnit()) == 0)
1273 Succs.insert(SI->getSUnit());
1274 }
1275 for (SUnit::const_pred_iterator PI = (*I)->Preds.begin(),
1276 PE = (*I)->Preds.end();
1277 PI != PE; ++PI) {
1278 if (PI->getKind() != SDep::Anti)
1279 continue;
1280 if (S && S->count(PI->getSUnit()) == 0)
1281 continue;
1282 if (NodeOrder.count(PI->getSUnit()) == 0)
1283 Succs.insert(PI->getSUnit());
1284 }
1285 }
Eugene Zelenko32a40562017-09-11 23:00:48 +00001286 return !Succs.empty();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001287}
1288
1289/// Return true if there is a path from the specified node to any of the nodes
1290/// in DestNodes. Keep track and return the nodes in any path.
1291static bool computePath(SUnit *Cur, SetVector<SUnit *> &Path,
1292 SetVector<SUnit *> &DestNodes,
1293 SetVector<SUnit *> &Exclude,
1294 SmallPtrSet<SUnit *, 8> &Visited) {
1295 if (Cur->isBoundaryNode())
1296 return false;
1297 if (Exclude.count(Cur) != 0)
1298 return false;
1299 if (DestNodes.count(Cur) != 0)
1300 return true;
1301 if (!Visited.insert(Cur).second)
1302 return Path.count(Cur) != 0;
1303 bool FoundPath = false;
1304 for (auto &SI : Cur->Succs)
1305 FoundPath |= computePath(SI.getSUnit(), Path, DestNodes, Exclude, Visited);
1306 for (auto &PI : Cur->Preds)
1307 if (PI.getKind() == SDep::Anti)
1308 FoundPath |=
1309 computePath(PI.getSUnit(), Path, DestNodes, Exclude, Visited);
1310 if (FoundPath)
1311 Path.insert(Cur);
1312 return FoundPath;
1313}
1314
1315/// Return true if Set1 is a subset of Set2.
1316template <class S1Ty, class S2Ty> static bool isSubset(S1Ty &Set1, S2Ty &Set2) {
1317 for (typename S1Ty::iterator I = Set1.begin(), E = Set1.end(); I != E; ++I)
1318 if (Set2.count(*I) == 0)
1319 return false;
1320 return true;
1321}
1322
1323/// Compute the live-out registers for the instructions in a node-set.
1324/// The live-out registers are those that are defined in the node-set,
1325/// but not used. Except for use operands of Phis.
1326static void computeLiveOuts(MachineFunction &MF, RegPressureTracker &RPTracker,
1327 NodeSet &NS) {
1328 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1329 MachineRegisterInfo &MRI = MF.getRegInfo();
1330 SmallVector<RegisterMaskPair, 8> LiveOutRegs;
1331 SmallSet<unsigned, 4> Uses;
1332 for (SUnit *SU : NS) {
1333 const MachineInstr *MI = SU->getInstr();
1334 if (MI->isPHI())
1335 continue;
Matthias Braunfc371552016-10-24 21:36:43 +00001336 for (const MachineOperand &MO : MI->operands())
1337 if (MO.isReg() && MO.isUse()) {
1338 unsigned Reg = MO.getReg();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001339 if (TargetRegisterInfo::isVirtualRegister(Reg))
1340 Uses.insert(Reg);
1341 else if (MRI.isAllocatable(Reg))
1342 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
1343 Uses.insert(*Units);
1344 }
1345 }
1346 for (SUnit *SU : NS)
Matthias Braunfc371552016-10-24 21:36:43 +00001347 for (const MachineOperand &MO : SU->getInstr()->operands())
1348 if (MO.isReg() && MO.isDef() && !MO.isDead()) {
1349 unsigned Reg = MO.getReg();
Brendon Cahoon254f8892016-07-29 16:44:44 +00001350 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1351 if (!Uses.count(Reg))
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001352 LiveOutRegs.push_back(RegisterMaskPair(Reg,
1353 LaneBitmask::getNone()));
Brendon Cahoon254f8892016-07-29 16:44:44 +00001354 } else if (MRI.isAllocatable(Reg)) {
1355 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
1356 if (!Uses.count(*Units))
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001357 LiveOutRegs.push_back(RegisterMaskPair(*Units,
1358 LaneBitmask::getNone()));
Brendon Cahoon254f8892016-07-29 16:44:44 +00001359 }
1360 }
1361 RPTracker.addLiveRegs(LiveOutRegs);
1362}
1363
1364/// A heuristic to filter nodes in recurrent node-sets if the register
1365/// pressure of a set is too high.
1366void SwingSchedulerDAG::registerPressureFilter(NodeSetType &NodeSets) {
1367 for (auto &NS : NodeSets) {
1368 // Skip small node-sets since they won't cause register pressure problems.
1369 if (NS.size() <= 2)
1370 continue;
1371 IntervalPressure RecRegPressure;
1372 RegPressureTracker RecRPTracker(RecRegPressure);
1373 RecRPTracker.init(&MF, &RegClassInfo, &LIS, BB, BB->end(), false, true);
1374 computeLiveOuts(MF, RecRPTracker, NS);
1375 RecRPTracker.closeBottom();
1376
1377 std::vector<SUnit *> SUnits(NS.begin(), NS.end());
Fangrui Song0cac7262018-09-27 02:13:45 +00001378 llvm::sort(SUnits, [](const SUnit *A, const SUnit *B) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00001379 return A->NodeNum > B->NodeNum;
1380 });
1381
1382 for (auto &SU : SUnits) {
1383 // Since we're computing the register pressure for a subset of the
1384 // instructions in a block, we need to set the tracker for each
1385 // instruction in the node-set. The tracker is set to the instruction
1386 // just after the one we're interested in.
1387 MachineBasicBlock::const_iterator CurInstI = SU->getInstr();
1388 RecRPTracker.setPos(std::next(CurInstI));
1389
1390 RegPressureDelta RPDelta;
1391 ArrayRef<PressureChange> CriticalPSets;
1392 RecRPTracker.getMaxUpwardPressureDelta(SU->getInstr(), nullptr, RPDelta,
1393 CriticalPSets,
1394 RecRegPressure.MaxSetPressure);
1395 if (RPDelta.Excess.isValid()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001396 LLVM_DEBUG(
1397 dbgs() << "Excess register pressure: SU(" << SU->NodeNum << ") "
1398 << TRI->getRegPressureSetName(RPDelta.Excess.getPSet())
1399 << ":" << RPDelta.Excess.getUnitInc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00001400 NS.setExceedPressure(SU);
1401 break;
1402 }
1403 RecRPTracker.recede();
1404 }
1405 }
1406}
1407
1408/// A heuristic to colocate node sets that have the same set of
1409/// successors.
1410void SwingSchedulerDAG::colocateNodeSets(NodeSetType &NodeSets) {
1411 unsigned Colocate = 0;
1412 for (int i = 0, e = NodeSets.size(); i < e; ++i) {
1413 NodeSet &N1 = NodeSets[i];
1414 SmallSetVector<SUnit *, 8> S1;
1415 if (N1.empty() || !succ_L(N1, S1))
1416 continue;
1417 for (int j = i + 1; j < e; ++j) {
1418 NodeSet &N2 = NodeSets[j];
1419 if (N1.compareRecMII(N2) != 0)
1420 continue;
1421 SmallSetVector<SUnit *, 8> S2;
1422 if (N2.empty() || !succ_L(N2, S2))
1423 continue;
1424 if (isSubset(S1, S2) && S1.size() == S2.size()) {
1425 N1.setColocate(++Colocate);
1426 N2.setColocate(Colocate);
1427 break;
1428 }
1429 }
1430 }
1431}
1432
1433/// Check if the existing node-sets are profitable. If not, then ignore the
1434/// recurrent node-sets, and attempt to schedule all nodes together. This is
Krzysztof Parzyszek3ca23342018-03-26 17:07:41 +00001435/// a heuristic. If the MII is large and all the recurrent node-sets are small,
1436/// then it's best to try to schedule all instructions together instead of
1437/// starting with the recurrent node-sets.
Brendon Cahoon254f8892016-07-29 16:44:44 +00001438void SwingSchedulerDAG::checkNodeSets(NodeSetType &NodeSets) {
1439 // Look for loops with a large MII.
Krzysztof Parzyszek3ca23342018-03-26 17:07:41 +00001440 if (MII < 17)
Brendon Cahoon254f8892016-07-29 16:44:44 +00001441 return;
1442 // Check if the node-set contains only a simple add recurrence.
Krzysztof Parzyszek3ca23342018-03-26 17:07:41 +00001443 for (auto &NS : NodeSets) {
1444 if (NS.getRecMII() > 2)
Brendon Cahoon254f8892016-07-29 16:44:44 +00001445 return;
Krzysztof Parzyszek3ca23342018-03-26 17:07:41 +00001446 if (NS.getMaxDepth() > MII)
Brendon Cahoon254f8892016-07-29 16:44:44 +00001447 return;
Krzysztof Parzyszek3ca23342018-03-26 17:07:41 +00001448 }
1449 NodeSets.clear();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001450 LLVM_DEBUG(dbgs() << "Clear recurrence node-sets\n");
Krzysztof Parzyszek3ca23342018-03-26 17:07:41 +00001451 return;
Brendon Cahoon254f8892016-07-29 16:44:44 +00001452}
1453
1454/// Add the nodes that do not belong to a recurrence set into groups
1455/// based upon connected componenets.
1456void SwingSchedulerDAG::groupRemainingNodes(NodeSetType &NodeSets) {
1457 SetVector<SUnit *> NodesAdded;
1458 SmallPtrSet<SUnit *, 8> Visited;
1459 // Add the nodes that are on a path between the previous node sets and
1460 // the current node set.
1461 for (NodeSet &I : NodeSets) {
1462 SmallSetVector<SUnit *, 8> N;
1463 // Add the nodes from the current node set to the previous node set.
1464 if (succ_L(I, N)) {
1465 SetVector<SUnit *> Path;
1466 for (SUnit *NI : N) {
1467 Visited.clear();
1468 computePath(NI, Path, NodesAdded, I, Visited);
1469 }
Eugene Zelenko32a40562017-09-11 23:00:48 +00001470 if (!Path.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00001471 I.insert(Path.begin(), Path.end());
1472 }
1473 // Add the nodes from the previous node set to the current node set.
1474 N.clear();
1475 if (succ_L(NodesAdded, N)) {
1476 SetVector<SUnit *> Path;
1477 for (SUnit *NI : N) {
1478 Visited.clear();
1479 computePath(NI, Path, I, NodesAdded, Visited);
1480 }
Eugene Zelenko32a40562017-09-11 23:00:48 +00001481 if (!Path.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00001482 I.insert(Path.begin(), Path.end());
1483 }
1484 NodesAdded.insert(I.begin(), I.end());
1485 }
1486
1487 // Create a new node set with the connected nodes of any successor of a node
1488 // in a recurrent set.
1489 NodeSet NewSet;
1490 SmallSetVector<SUnit *, 8> N;
1491 if (succ_L(NodesAdded, N))
1492 for (SUnit *I : N)
1493 addConnectedNodes(I, NewSet, NodesAdded);
Eugene Zelenko32a40562017-09-11 23:00:48 +00001494 if (!NewSet.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00001495 NodeSets.push_back(NewSet);
1496
1497 // Create a new node set with the connected nodes of any predecessor of a node
1498 // in a recurrent set.
1499 NewSet.clear();
1500 if (pred_L(NodesAdded, N))
1501 for (SUnit *I : N)
1502 addConnectedNodes(I, NewSet, NodesAdded);
Eugene Zelenko32a40562017-09-11 23:00:48 +00001503 if (!NewSet.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00001504 NodeSets.push_back(NewSet);
1505
Hiroshi Inoue372ffa12018-04-13 11:37:06 +00001506 // Create new nodes sets with the connected nodes any remaining node that
Brendon Cahoon254f8892016-07-29 16:44:44 +00001507 // has no predecessor.
1508 for (unsigned i = 0; i < SUnits.size(); ++i) {
1509 SUnit *SU = &SUnits[i];
1510 if (NodesAdded.count(SU) == 0) {
1511 NewSet.clear();
1512 addConnectedNodes(SU, NewSet, NodesAdded);
Eugene Zelenko32a40562017-09-11 23:00:48 +00001513 if (!NewSet.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00001514 NodeSets.push_back(NewSet);
1515 }
1516 }
1517}
1518
1519/// Add the node to the set, and add all is its connected nodes to the set.
1520void SwingSchedulerDAG::addConnectedNodes(SUnit *SU, NodeSet &NewSet,
1521 SetVector<SUnit *> &NodesAdded) {
1522 NewSet.insert(SU);
1523 NodesAdded.insert(SU);
1524 for (auto &SI : SU->Succs) {
1525 SUnit *Successor = SI.getSUnit();
1526 if (!SI.isArtificial() && NodesAdded.count(Successor) == 0)
1527 addConnectedNodes(Successor, NewSet, NodesAdded);
1528 }
1529 for (auto &PI : SU->Preds) {
1530 SUnit *Predecessor = PI.getSUnit();
1531 if (!PI.isArtificial() && NodesAdded.count(Predecessor) == 0)
1532 addConnectedNodes(Predecessor, NewSet, NodesAdded);
1533 }
1534}
1535
1536/// Return true if Set1 contains elements in Set2. The elements in common
1537/// are returned in a different container.
1538static bool isIntersect(SmallSetVector<SUnit *, 8> &Set1, const NodeSet &Set2,
1539 SmallSetVector<SUnit *, 8> &Result) {
1540 Result.clear();
1541 for (unsigned i = 0, e = Set1.size(); i != e; ++i) {
1542 SUnit *SU = Set1[i];
1543 if (Set2.count(SU) != 0)
1544 Result.insert(SU);
1545 }
1546 return !Result.empty();
1547}
1548
1549/// Merge the recurrence node sets that have the same initial node.
1550void SwingSchedulerDAG::fuseRecs(NodeSetType &NodeSets) {
1551 for (NodeSetType::iterator I = NodeSets.begin(), E = NodeSets.end(); I != E;
1552 ++I) {
1553 NodeSet &NI = *I;
1554 for (NodeSetType::iterator J = I + 1; J != E;) {
1555 NodeSet &NJ = *J;
1556 if (NI.getNode(0)->NodeNum == NJ.getNode(0)->NodeNum) {
1557 if (NJ.compareRecMII(NI) > 0)
1558 NI.setRecMII(NJ.getRecMII());
1559 for (NodeSet::iterator NII = J->begin(), ENI = J->end(); NII != ENI;
1560 ++NII)
1561 I->insert(*NII);
1562 NodeSets.erase(J);
1563 E = NodeSets.end();
1564 } else {
1565 ++J;
1566 }
1567 }
1568 }
1569}
1570
1571/// Remove nodes that have been scheduled in previous NodeSets.
1572void SwingSchedulerDAG::removeDuplicateNodes(NodeSetType &NodeSets) {
1573 for (NodeSetType::iterator I = NodeSets.begin(), E = NodeSets.end(); I != E;
1574 ++I)
1575 for (NodeSetType::iterator J = I + 1; J != E;) {
1576 J->remove_if([&](SUnit *SUJ) { return I->count(SUJ); });
1577
Eugene Zelenko32a40562017-09-11 23:00:48 +00001578 if (J->empty()) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00001579 NodeSets.erase(J);
1580 E = NodeSets.end();
1581 } else {
1582 ++J;
1583 }
1584 }
1585}
1586
Brendon Cahoon254f8892016-07-29 16:44:44 +00001587/// Compute an ordered list of the dependence graph nodes, which
1588/// indicates the order that the nodes will be scheduled. This is a
1589/// two-level algorithm. First, a partial order is created, which
1590/// consists of a list of sets ordered from highest to lowest priority.
1591void SwingSchedulerDAG::computeNodeOrder(NodeSetType &NodeSets) {
1592 SmallSetVector<SUnit *, 8> R;
1593 NodeOrder.clear();
1594
1595 for (auto &Nodes : NodeSets) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001596 LLVM_DEBUG(dbgs() << "NodeSet size " << Nodes.size() << "\n");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001597 OrderKind Order;
1598 SmallSetVector<SUnit *, 8> N;
1599 if (pred_L(NodeOrder, N) && isSubset(N, Nodes)) {
1600 R.insert(N.begin(), N.end());
1601 Order = BottomUp;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001602 LLVM_DEBUG(dbgs() << " Bottom up (preds) ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001603 } else if (succ_L(NodeOrder, N) && isSubset(N, Nodes)) {
1604 R.insert(N.begin(), N.end());
1605 Order = TopDown;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001606 LLVM_DEBUG(dbgs() << " Top down (succs) ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001607 } else if (isIntersect(N, Nodes, R)) {
1608 // If some of the successors are in the existing node-set, then use the
1609 // top-down ordering.
1610 Order = TopDown;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001611 LLVM_DEBUG(dbgs() << " Top down (intersect) ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001612 } else if (NodeSets.size() == 1) {
1613 for (auto &N : Nodes)
1614 if (N->Succs.size() == 0)
1615 R.insert(N);
1616 Order = BottomUp;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001617 LLVM_DEBUG(dbgs() << " Bottom up (all) ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001618 } else {
1619 // Find the node with the highest ASAP.
1620 SUnit *maxASAP = nullptr;
1621 for (SUnit *SU : Nodes) {
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00001622 if (maxASAP == nullptr || getASAP(SU) > getASAP(maxASAP) ||
1623 (getASAP(SU) == getASAP(maxASAP) && SU->NodeNum > maxASAP->NodeNum))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001624 maxASAP = SU;
1625 }
1626 R.insert(maxASAP);
1627 Order = BottomUp;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001628 LLVM_DEBUG(dbgs() << " Bottom up (default) ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001629 }
1630
1631 while (!R.empty()) {
1632 if (Order == TopDown) {
1633 // Choose the node with the maximum height. If more than one, choose
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00001634 // the node wiTH the maximum ZeroLatencyHeight. If still more than one,
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001635 // choose the node with the lowest MOV.
Brendon Cahoon254f8892016-07-29 16:44:44 +00001636 while (!R.empty()) {
1637 SUnit *maxHeight = nullptr;
1638 for (SUnit *I : R) {
Eugene Zelenkocdc71612016-08-11 17:20:18 +00001639 if (maxHeight == nullptr || getHeight(I) > getHeight(maxHeight))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001640 maxHeight = I;
1641 else if (getHeight(I) == getHeight(maxHeight) &&
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001642 getZeroLatencyHeight(I) > getZeroLatencyHeight(maxHeight))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001643 maxHeight = I;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001644 else if (getHeight(I) == getHeight(maxHeight) &&
1645 getZeroLatencyHeight(I) ==
1646 getZeroLatencyHeight(maxHeight) &&
1647 getMOV(I) < getMOV(maxHeight))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001648 maxHeight = I;
1649 }
1650 NodeOrder.insert(maxHeight);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001651 LLVM_DEBUG(dbgs() << maxHeight->NodeNum << " ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001652 R.remove(maxHeight);
1653 for (const auto &I : maxHeight->Succs) {
1654 if (Nodes.count(I.getSUnit()) == 0)
1655 continue;
1656 if (NodeOrder.count(I.getSUnit()) != 0)
1657 continue;
1658 if (ignoreDependence(I, false))
1659 continue;
1660 R.insert(I.getSUnit());
1661 }
1662 // Back-edges are predecessors with an anti-dependence.
1663 for (const auto &I : maxHeight->Preds) {
1664 if (I.getKind() != SDep::Anti)
1665 continue;
1666 if (Nodes.count(I.getSUnit()) == 0)
1667 continue;
1668 if (NodeOrder.count(I.getSUnit()) != 0)
1669 continue;
1670 R.insert(I.getSUnit());
1671 }
1672 }
1673 Order = BottomUp;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001674 LLVM_DEBUG(dbgs() << "\n Switching order to bottom up ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001675 SmallSetVector<SUnit *, 8> N;
1676 if (pred_L(NodeOrder, N, &Nodes))
1677 R.insert(N.begin(), N.end());
1678 } else {
1679 // Choose the node with the maximum depth. If more than one, choose
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001680 // the node with the maximum ZeroLatencyDepth. If still more than one,
1681 // choose the node with the lowest MOV.
Brendon Cahoon254f8892016-07-29 16:44:44 +00001682 while (!R.empty()) {
1683 SUnit *maxDepth = nullptr;
1684 for (SUnit *I : R) {
Eugene Zelenkocdc71612016-08-11 17:20:18 +00001685 if (maxDepth == nullptr || getDepth(I) > getDepth(maxDepth))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001686 maxDepth = I;
1687 else if (getDepth(I) == getDepth(maxDepth) &&
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001688 getZeroLatencyDepth(I) > getZeroLatencyDepth(maxDepth))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001689 maxDepth = I;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00001690 else if (getDepth(I) == getDepth(maxDepth) &&
1691 getZeroLatencyDepth(I) == getZeroLatencyDepth(maxDepth) &&
1692 getMOV(I) < getMOV(maxDepth))
Brendon Cahoon254f8892016-07-29 16:44:44 +00001693 maxDepth = I;
1694 }
1695 NodeOrder.insert(maxDepth);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001696 LLVM_DEBUG(dbgs() << maxDepth->NodeNum << " ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001697 R.remove(maxDepth);
1698 if (Nodes.isExceedSU(maxDepth)) {
1699 Order = TopDown;
1700 R.clear();
1701 R.insert(Nodes.getNode(0));
1702 break;
1703 }
1704 for (const auto &I : maxDepth->Preds) {
1705 if (Nodes.count(I.getSUnit()) == 0)
1706 continue;
1707 if (NodeOrder.count(I.getSUnit()) != 0)
1708 continue;
Brendon Cahoon254f8892016-07-29 16:44:44 +00001709 R.insert(I.getSUnit());
1710 }
1711 // Back-edges are predecessors with an anti-dependence.
1712 for (const auto &I : maxDepth->Succs) {
1713 if (I.getKind() != SDep::Anti)
1714 continue;
1715 if (Nodes.count(I.getSUnit()) == 0)
1716 continue;
1717 if (NodeOrder.count(I.getSUnit()) != 0)
1718 continue;
1719 R.insert(I.getSUnit());
1720 }
1721 }
1722 Order = TopDown;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001723 LLVM_DEBUG(dbgs() << "\n Switching order to top down ");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001724 SmallSetVector<SUnit *, 8> N;
1725 if (succ_L(NodeOrder, N, &Nodes))
1726 R.insert(N.begin(), N.end());
1727 }
1728 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001729 LLVM_DEBUG(dbgs() << "\nDone with Nodeset\n");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001730 }
1731
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001732 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001733 dbgs() << "Node order: ";
1734 for (SUnit *I : NodeOrder)
1735 dbgs() << " " << I->NodeNum << " ";
1736 dbgs() << "\n";
1737 });
1738}
1739
1740/// Process the nodes in the computed order and create the pipelined schedule
1741/// of the instructions, if possible. Return true if a schedule is found.
1742bool SwingSchedulerDAG::schedulePipeline(SMSchedule &Schedule) {
Eugene Zelenko32a40562017-09-11 23:00:48 +00001743 if (NodeOrder.empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00001744 return false;
1745
1746 bool scheduleFound = false;
1747 // Keep increasing II until a valid schedule is found.
1748 for (unsigned II = MII; II < MII + 10 && !scheduleFound; ++II) {
1749 Schedule.reset();
1750 Schedule.setInitiationInterval(II);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001751 LLVM_DEBUG(dbgs() << "Try to schedule with " << II << "\n");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001752
1753 SetVector<SUnit *>::iterator NI = NodeOrder.begin();
1754 SetVector<SUnit *>::iterator NE = NodeOrder.end();
1755 do {
1756 SUnit *SU = *NI;
1757
1758 // Compute the schedule time for the instruction, which is based
1759 // upon the scheduled time for any predecessors/successors.
1760 int EarlyStart = INT_MIN;
1761 int LateStart = INT_MAX;
1762 // These values are set when the size of the schedule window is limited
1763 // due to chain dependences.
1764 int SchedEnd = INT_MAX;
1765 int SchedStart = INT_MIN;
1766 Schedule.computeStart(SU, &EarlyStart, &LateStart, &SchedEnd, &SchedStart,
1767 II, this);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001768 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001769 dbgs() << "Inst (" << SU->NodeNum << ") ";
1770 SU->getInstr()->dump();
1771 dbgs() << "\n";
1772 });
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001773 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001774 dbgs() << "\tes: " << EarlyStart << " ls: " << LateStart
1775 << " me: " << SchedEnd << " ms: " << SchedStart << "\n";
1776 });
1777
1778 if (EarlyStart > LateStart || SchedEnd < EarlyStart ||
1779 SchedStart > LateStart)
1780 scheduleFound = false;
1781 else if (EarlyStart != INT_MIN && LateStart == INT_MAX) {
1782 SchedEnd = std::min(SchedEnd, EarlyStart + (int)II - 1);
1783 scheduleFound = Schedule.insert(SU, EarlyStart, SchedEnd, II);
1784 } else if (EarlyStart == INT_MIN && LateStart != INT_MAX) {
1785 SchedStart = std::max(SchedStart, LateStart - (int)II + 1);
1786 scheduleFound = Schedule.insert(SU, LateStart, SchedStart, II);
1787 } else if (EarlyStart != INT_MIN && LateStart != INT_MAX) {
1788 SchedEnd =
1789 std::min(SchedEnd, std::min(LateStart, EarlyStart + (int)II - 1));
1790 // When scheduling a Phi it is better to start at the late cycle and go
1791 // backwards. The default order may insert the Phi too far away from
1792 // its first dependence.
1793 if (SU->getInstr()->isPHI())
1794 scheduleFound = Schedule.insert(SU, SchedEnd, EarlyStart, II);
1795 else
1796 scheduleFound = Schedule.insert(SU, EarlyStart, SchedEnd, II);
1797 } else {
1798 int FirstCycle = Schedule.getFirstCycle();
1799 scheduleFound = Schedule.insert(SU, FirstCycle + getASAP(SU),
1800 FirstCycle + getASAP(SU) + II - 1, II);
1801 }
1802 // Even if we find a schedule, make sure the schedule doesn't exceed the
1803 // allowable number of stages. We keep trying if this happens.
1804 if (scheduleFound)
1805 if (SwpMaxStages > -1 &&
1806 Schedule.getMaxStageCount() > (unsigned)SwpMaxStages)
1807 scheduleFound = false;
1808
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001809 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001810 if (!scheduleFound)
1811 dbgs() << "\tCan't schedule\n";
1812 });
1813 } while (++NI != NE && scheduleFound);
1814
1815 // If a schedule is found, check if it is a valid schedule too.
1816 if (scheduleFound)
1817 scheduleFound = Schedule.isValidSchedule(this);
1818 }
1819
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001820 LLVM_DEBUG(dbgs() << "Schedule Found? " << scheduleFound << "\n");
Brendon Cahoon254f8892016-07-29 16:44:44 +00001821
1822 if (scheduleFound)
1823 Schedule.finalizeSchedule(this);
1824 else
1825 Schedule.reset();
1826
1827 return scheduleFound && Schedule.getMaxStageCount() > 0;
1828}
1829
1830/// Given a schedule for the loop, generate a new version of the loop,
1831/// and replace the old version. This function generates a prolog
1832/// that contains the initial iterations in the pipeline, and kernel
1833/// loop, and the epilogue that contains the code for the final
1834/// iterations.
1835void SwingSchedulerDAG::generatePipelinedLoop(SMSchedule &Schedule) {
1836 // Create a new basic block for the kernel and add it to the CFG.
1837 MachineBasicBlock *KernelBB = MF.CreateMachineBasicBlock(BB->getBasicBlock());
1838
1839 unsigned MaxStageCount = Schedule.getMaxStageCount();
1840
1841 // Remember the registers that are used in different stages. The index is
1842 // the iteration, or stage, that the instruction is scheduled in. This is
Hiroshi Inouec73b6d62018-06-20 05:29:26 +00001843 // a map between register names in the original block and the names created
Brendon Cahoon254f8892016-07-29 16:44:44 +00001844 // in each stage of the pipelined loop.
1845 ValueMapTy *VRMap = new ValueMapTy[(MaxStageCount + 1) * 2];
1846 InstrMapTy InstrMap;
1847
1848 SmallVector<MachineBasicBlock *, 4> PrologBBs;
1849 // Generate the prolog instructions that set up the pipeline.
1850 generateProlog(Schedule, MaxStageCount, KernelBB, VRMap, PrologBBs);
1851 MF.insert(BB->getIterator(), KernelBB);
1852
1853 // Rearrange the instructions to generate the new, pipelined loop,
1854 // and update register names as needed.
1855 for (int Cycle = Schedule.getFirstCycle(),
1856 LastCycle = Schedule.getFinalCycle();
1857 Cycle <= LastCycle; ++Cycle) {
1858 std::deque<SUnit *> &CycleInstrs = Schedule.getInstructions(Cycle);
1859 // This inner loop schedules each instruction in the cycle.
1860 for (SUnit *CI : CycleInstrs) {
1861 if (CI->getInstr()->isPHI())
1862 continue;
1863 unsigned StageNum = Schedule.stageScheduled(getSUnit(CI->getInstr()));
1864 MachineInstr *NewMI = cloneInstr(CI->getInstr(), MaxStageCount, StageNum);
1865 updateInstruction(NewMI, false, MaxStageCount, StageNum, Schedule, VRMap);
1866 KernelBB->push_back(NewMI);
1867 InstrMap[NewMI] = CI->getInstr();
1868 }
1869 }
1870
1871 // Copy any terminator instructions to the new kernel, and update
1872 // names as needed.
1873 for (MachineBasicBlock::iterator I = BB->getFirstTerminator(),
1874 E = BB->instr_end();
1875 I != E; ++I) {
1876 MachineInstr *NewMI = MF.CloneMachineInstr(&*I);
1877 updateInstruction(NewMI, false, MaxStageCount, 0, Schedule, VRMap);
1878 KernelBB->push_back(NewMI);
1879 InstrMap[NewMI] = &*I;
1880 }
1881
1882 KernelBB->transferSuccessors(BB);
1883 KernelBB->replaceSuccessor(BB, KernelBB);
1884
1885 generateExistingPhis(KernelBB, PrologBBs.back(), KernelBB, KernelBB, Schedule,
1886 VRMap, InstrMap, MaxStageCount, MaxStageCount, false);
1887 generatePhis(KernelBB, PrologBBs.back(), KernelBB, KernelBB, Schedule, VRMap,
1888 InstrMap, MaxStageCount, MaxStageCount, false);
1889
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001890 LLVM_DEBUG(dbgs() << "New block\n"; KernelBB->dump(););
Brendon Cahoon254f8892016-07-29 16:44:44 +00001891
1892 SmallVector<MachineBasicBlock *, 4> EpilogBBs;
1893 // Generate the epilog instructions to complete the pipeline.
1894 generateEpilog(Schedule, MaxStageCount, KernelBB, VRMap, EpilogBBs,
1895 PrologBBs);
1896
1897 // We need this step because the register allocation doesn't handle some
1898 // situations well, so we insert copies to help out.
1899 splitLifetimes(KernelBB, EpilogBBs, Schedule);
1900
1901 // Remove dead instructions due to loop induction variables.
1902 removeDeadInstructions(KernelBB, EpilogBBs);
1903
1904 // Add branches between prolog and epilog blocks.
1905 addBranches(PrologBBs, KernelBB, EpilogBBs, Schedule, VRMap);
1906
1907 // Remove the original loop since it's no longer referenced.
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00001908 for (auto &I : *BB)
1909 LIS.RemoveMachineInstrFromMaps(I);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001910 BB->clear();
1911 BB->eraseFromParent();
1912
1913 delete[] VRMap;
1914}
1915
1916/// Generate the pipeline prolog code.
1917void SwingSchedulerDAG::generateProlog(SMSchedule &Schedule, unsigned LastStage,
1918 MachineBasicBlock *KernelBB,
1919 ValueMapTy *VRMap,
1920 MBBVectorTy &PrologBBs) {
1921 MachineBasicBlock *PreheaderBB = MLI->getLoopFor(BB)->getLoopPreheader();
Eugene Zelenko32a40562017-09-11 23:00:48 +00001922 assert(PreheaderBB != nullptr &&
Brendon Cahoon254f8892016-07-29 16:44:44 +00001923 "Need to add code to handle loops w/o preheader");
1924 MachineBasicBlock *PredBB = PreheaderBB;
1925 InstrMapTy InstrMap;
1926
1927 // Generate a basic block for each stage, not including the last stage,
1928 // which will be generated in the kernel. Each basic block may contain
1929 // instructions from multiple stages/iterations.
1930 for (unsigned i = 0; i < LastStage; ++i) {
1931 // Create and insert the prolog basic block prior to the original loop
1932 // basic block. The original loop is removed later.
1933 MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock(BB->getBasicBlock());
1934 PrologBBs.push_back(NewBB);
1935 MF.insert(BB->getIterator(), NewBB);
1936 NewBB->transferSuccessors(PredBB);
1937 PredBB->addSuccessor(NewBB);
1938 PredBB = NewBB;
1939
1940 // Generate instructions for each appropriate stage. Process instructions
1941 // in original program order.
1942 for (int StageNum = i; StageNum >= 0; --StageNum) {
1943 for (MachineBasicBlock::iterator BBI = BB->instr_begin(),
1944 BBE = BB->getFirstTerminator();
1945 BBI != BBE; ++BBI) {
1946 if (Schedule.isScheduledAtStage(getSUnit(&*BBI), (unsigned)StageNum)) {
1947 if (BBI->isPHI())
1948 continue;
1949 MachineInstr *NewMI =
1950 cloneAndChangeInstr(&*BBI, i, (unsigned)StageNum, Schedule);
1951 updateInstruction(NewMI, false, i, (unsigned)StageNum, Schedule,
1952 VRMap);
1953 NewBB->push_back(NewMI);
1954 InstrMap[NewMI] = &*BBI;
1955 }
1956 }
1957 }
1958 rewritePhiValues(NewBB, i, Schedule, VRMap, InstrMap);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001959 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00001960 dbgs() << "prolog:\n";
1961 NewBB->dump();
1962 });
1963 }
1964
1965 PredBB->replaceSuccessor(BB, KernelBB);
1966
1967 // Check if we need to remove the branch from the preheader to the original
1968 // loop, and replace it with a branch to the new loop.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00001969 unsigned numBranches = TII->removeBranch(*PreheaderBB);
Brendon Cahoon254f8892016-07-29 16:44:44 +00001970 if (numBranches) {
1971 SmallVector<MachineOperand, 0> Cond;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00001972 TII->insertBranch(*PreheaderBB, PrologBBs[0], nullptr, Cond, DebugLoc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00001973 }
1974}
1975
1976/// Generate the pipeline epilog code. The epilog code finishes the iterations
1977/// that were started in either the prolog or the kernel. We create a basic
1978/// block for each stage that needs to complete.
1979void SwingSchedulerDAG::generateEpilog(SMSchedule &Schedule, unsigned LastStage,
1980 MachineBasicBlock *KernelBB,
1981 ValueMapTy *VRMap,
1982 MBBVectorTy &EpilogBBs,
1983 MBBVectorTy &PrologBBs) {
1984 // We need to change the branch from the kernel to the first epilog block, so
1985 // this call to analyze branch uses the kernel rather than the original BB.
1986 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1987 SmallVector<MachineOperand, 4> Cond;
1988 bool checkBranch = TII->analyzeBranch(*KernelBB, TBB, FBB, Cond);
1989 assert(!checkBranch && "generateEpilog must be able to analyze the branch");
1990 if (checkBranch)
1991 return;
1992
1993 MachineBasicBlock::succ_iterator LoopExitI = KernelBB->succ_begin();
1994 if (*LoopExitI == KernelBB)
1995 ++LoopExitI;
1996 assert(LoopExitI != KernelBB->succ_end() && "Expecting a successor");
1997 MachineBasicBlock *LoopExitBB = *LoopExitI;
1998
1999 MachineBasicBlock *PredBB = KernelBB;
2000 MachineBasicBlock *EpilogStart = LoopExitBB;
2001 InstrMapTy InstrMap;
2002
2003 // Generate a basic block for each stage, not including the last stage,
2004 // which was generated for the kernel. Each basic block may contain
2005 // instructions from multiple stages/iterations.
2006 int EpilogStage = LastStage + 1;
2007 for (unsigned i = LastStage; i >= 1; --i, ++EpilogStage) {
2008 MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock();
2009 EpilogBBs.push_back(NewBB);
2010 MF.insert(BB->getIterator(), NewBB);
2011
2012 PredBB->replaceSuccessor(LoopExitBB, NewBB);
2013 NewBB->addSuccessor(LoopExitBB);
2014
2015 if (EpilogStart == LoopExitBB)
2016 EpilogStart = NewBB;
2017
2018 // Add instructions to the epilog depending on the current block.
2019 // Process instructions in original program order.
2020 for (unsigned StageNum = i; StageNum <= LastStage; ++StageNum) {
2021 for (auto &BBI : *BB) {
2022 if (BBI.isPHI())
2023 continue;
2024 MachineInstr *In = &BBI;
2025 if (Schedule.isScheduledAtStage(getSUnit(In), StageNum)) {
Krzysztof Parzyszek785b6ce2018-03-26 15:45:55 +00002026 // Instructions with memoperands in the epilog are updated with
2027 // conservative values.
2028 MachineInstr *NewMI = cloneInstr(In, UINT_MAX, 0);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002029 updateInstruction(NewMI, i == 1, EpilogStage, 0, Schedule, VRMap);
2030 NewBB->push_back(NewMI);
2031 InstrMap[NewMI] = In;
2032 }
2033 }
2034 }
2035 generateExistingPhis(NewBB, PrologBBs[i - 1], PredBB, KernelBB, Schedule,
2036 VRMap, InstrMap, LastStage, EpilogStage, i == 1);
2037 generatePhis(NewBB, PrologBBs[i - 1], PredBB, KernelBB, Schedule, VRMap,
2038 InstrMap, LastStage, EpilogStage, i == 1);
2039 PredBB = NewBB;
2040
Nicola Zaghend34e60c2018-05-14 12:53:11 +00002041 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00002042 dbgs() << "epilog:\n";
2043 NewBB->dump();
2044 });
2045 }
2046
2047 // Fix any Phi nodes in the loop exit block.
2048 for (MachineInstr &MI : *LoopExitBB) {
2049 if (!MI.isPHI())
2050 break;
2051 for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
2052 MachineOperand &MO = MI.getOperand(i);
2053 if (MO.getMBB() == BB)
2054 MO.setMBB(PredBB);
2055 }
2056 }
2057
2058 // Create a branch to the new epilog from the kernel.
2059 // Remove the original branch and add a new branch to the epilog.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +00002060 TII->removeBranch(*KernelBB);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00002061 TII->insertBranch(*KernelBB, KernelBB, EpilogStart, Cond, DebugLoc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00002062 // Add a branch to the loop exit.
2063 if (EpilogBBs.size() > 0) {
2064 MachineBasicBlock *LastEpilogBB = EpilogBBs.back();
2065 SmallVector<MachineOperand, 4> Cond1;
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00002066 TII->insertBranch(*LastEpilogBB, LoopExitBB, nullptr, Cond1, DebugLoc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00002067 }
2068}
2069
2070/// Replace all uses of FromReg that appear outside the specified
2071/// basic block with ToReg.
2072static void replaceRegUsesAfterLoop(unsigned FromReg, unsigned ToReg,
2073 MachineBasicBlock *MBB,
2074 MachineRegisterInfo &MRI,
2075 LiveIntervals &LIS) {
2076 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(FromReg),
2077 E = MRI.use_end();
2078 I != E;) {
2079 MachineOperand &O = *I;
2080 ++I;
2081 if (O.getParent()->getParent() != MBB)
2082 O.setReg(ToReg);
2083 }
2084 if (!LIS.hasInterval(ToReg))
2085 LIS.createEmptyInterval(ToReg);
2086}
2087
2088/// Return true if the register has a use that occurs outside the
2089/// specified loop.
2090static bool hasUseAfterLoop(unsigned Reg, MachineBasicBlock *BB,
2091 MachineRegisterInfo &MRI) {
2092 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(Reg),
2093 E = MRI.use_end();
2094 I != E; ++I)
2095 if (I->getParent()->getParent() != BB)
2096 return true;
2097 return false;
2098}
2099
2100/// Generate Phis for the specific block in the generated pipelined code.
2101/// This function looks at the Phis from the original code to guide the
2102/// creation of new Phis.
2103void SwingSchedulerDAG::generateExistingPhis(
2104 MachineBasicBlock *NewBB, MachineBasicBlock *BB1, MachineBasicBlock *BB2,
2105 MachineBasicBlock *KernelBB, SMSchedule &Schedule, ValueMapTy *VRMap,
2106 InstrMapTy &InstrMap, unsigned LastStageNum, unsigned CurStageNum,
2107 bool IsLast) {
Simon Pilgrim6bdc7552017-03-31 10:59:37 +00002108 // Compute the stage number for the initial value of the Phi, which
Brendon Cahoon254f8892016-07-29 16:44:44 +00002109 // comes from the prolog. The prolog to use depends on to which kernel/
2110 // epilog that we're adding the Phi.
2111 unsigned PrologStage = 0;
2112 unsigned PrevStage = 0;
2113 bool InKernel = (LastStageNum == CurStageNum);
2114 if (InKernel) {
2115 PrologStage = LastStageNum - 1;
2116 PrevStage = CurStageNum;
2117 } else {
2118 PrologStage = LastStageNum - (CurStageNum - LastStageNum);
2119 PrevStage = LastStageNum + (CurStageNum - LastStageNum) - 1;
2120 }
2121
2122 for (MachineBasicBlock::iterator BBI = BB->instr_begin(),
2123 BBE = BB->getFirstNonPHI();
2124 BBI != BBE; ++BBI) {
2125 unsigned Def = BBI->getOperand(0).getReg();
2126
2127 unsigned InitVal = 0;
2128 unsigned LoopVal = 0;
2129 getPhiRegs(*BBI, BB, InitVal, LoopVal);
2130
2131 unsigned PhiOp1 = 0;
2132 // The Phi value from the loop body typically is defined in the loop, but
2133 // not always. So, we need to check if the value is defined in the loop.
2134 unsigned PhiOp2 = LoopVal;
2135 if (VRMap[LastStageNum].count(LoopVal))
2136 PhiOp2 = VRMap[LastStageNum][LoopVal];
2137
2138 int StageScheduled = Schedule.stageScheduled(getSUnit(&*BBI));
2139 int LoopValStage =
2140 Schedule.stageScheduled(getSUnit(MRI.getVRegDef(LoopVal)));
2141 unsigned NumStages = Schedule.getStagesForReg(Def, CurStageNum);
2142 if (NumStages == 0) {
2143 // We don't need to generate a Phi anymore, but we need to rename any uses
2144 // of the Phi value.
2145 unsigned NewReg = VRMap[PrevStage][LoopVal];
2146 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, 0, &*BBI,
Krzysztof Parzyszek16e66f52018-03-26 16:41:36 +00002147 Def, InitVal, NewReg);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002148 if (VRMap[CurStageNum].count(LoopVal))
2149 VRMap[CurStageNum][Def] = VRMap[CurStageNum][LoopVal];
2150 }
2151 // Adjust the number of Phis needed depending on the number of prologs left,
Krzysztof Parzyszek3f72a6b2018-03-26 16:37:55 +00002152 // and the distance from where the Phi is first scheduled. The number of
2153 // Phis cannot exceed the number of prolog stages. Each stage can
2154 // potentially define two values.
2155 unsigned MaxPhis = PrologStage + 2;
2156 if (!InKernel && (int)PrologStage <= LoopValStage)
2157 MaxPhis = std::max((int)MaxPhis - (int)LoopValStage, 1);
2158 unsigned NumPhis = std::min(NumStages, MaxPhis);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002159
2160 unsigned NewReg = 0;
Brendon Cahoon254f8892016-07-29 16:44:44 +00002161 unsigned AccessStage = (LoopValStage != -1) ? LoopValStage : StageScheduled;
2162 // In the epilog, we may need to look back one stage to get the correct
2163 // Phi name because the epilog and prolog blocks execute the same stage.
2164 // The correct name is from the previous block only when the Phi has
2165 // been completely scheduled prior to the epilog, and Phi value is not
2166 // needed in multiple stages.
2167 int StageDiff = 0;
2168 if (!InKernel && StageScheduled >= LoopValStage && AccessStage == 0 &&
2169 NumPhis == 1)
2170 StageDiff = 1;
2171 // Adjust the computations below when the phi and the loop definition
2172 // are scheduled in different stages.
2173 if (InKernel && LoopValStage != -1 && StageScheduled > LoopValStage)
2174 StageDiff = StageScheduled - LoopValStage;
2175 for (unsigned np = 0; np < NumPhis; ++np) {
2176 // If the Phi hasn't been scheduled, then use the initial Phi operand
2177 // value. Otherwise, use the scheduled version of the instruction. This
2178 // is a little complicated when a Phi references another Phi.
2179 if (np > PrologStage || StageScheduled >= (int)LastStageNum)
2180 PhiOp1 = InitVal;
2181 // Check if the Phi has already been scheduled in a prolog stage.
2182 else if (PrologStage >= AccessStage + StageDiff + np &&
2183 VRMap[PrologStage - StageDiff - np].count(LoopVal) != 0)
2184 PhiOp1 = VRMap[PrologStage - StageDiff - np][LoopVal];
Hiroshi Inouedad8c6a2019-01-09 05:11:10 +00002185 // Check if the Phi has already been scheduled, but the loop instruction
Brendon Cahoon254f8892016-07-29 16:44:44 +00002186 // is either another Phi, or doesn't occur in the loop.
2187 else if (PrologStage >= AccessStage + StageDiff + np) {
2188 // If the Phi references another Phi, we need to examine the other
2189 // Phi to get the correct value.
2190 PhiOp1 = LoopVal;
2191 MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1);
2192 int Indirects = 1;
2193 while (InstOp1 && InstOp1->isPHI() && InstOp1->getParent() == BB) {
2194 int PhiStage = Schedule.stageScheduled(getSUnit(InstOp1));
2195 if ((int)(PrologStage - StageDiff - np) < PhiStage + Indirects)
2196 PhiOp1 = getInitPhiReg(*InstOp1, BB);
2197 else
2198 PhiOp1 = getLoopPhiReg(*InstOp1, BB);
2199 InstOp1 = MRI.getVRegDef(PhiOp1);
2200 int PhiOpStage = Schedule.stageScheduled(getSUnit(InstOp1));
2201 int StageAdj = (PhiOpStage != -1 ? PhiStage - PhiOpStage : 0);
2202 if (PhiOpStage != -1 && PrologStage - StageAdj >= Indirects + np &&
2203 VRMap[PrologStage - StageAdj - Indirects - np].count(PhiOp1)) {
2204 PhiOp1 = VRMap[PrologStage - StageAdj - Indirects - np][PhiOp1];
2205 break;
2206 }
2207 ++Indirects;
2208 }
2209 } else
2210 PhiOp1 = InitVal;
2211 // If this references a generated Phi in the kernel, get the Phi operand
2212 // from the incoming block.
2213 if (MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1))
2214 if (InstOp1->isPHI() && InstOp1->getParent() == KernelBB)
2215 PhiOp1 = getInitPhiReg(*InstOp1, KernelBB);
2216
2217 MachineInstr *PhiInst = MRI.getVRegDef(LoopVal);
2218 bool LoopDefIsPhi = PhiInst && PhiInst->isPHI();
2219 // In the epilog, a map lookup is needed to get the value from the kernel,
2220 // or previous epilog block. How is does this depends on if the
2221 // instruction is scheduled in the previous block.
2222 if (!InKernel) {
2223 int StageDiffAdj = 0;
2224 if (LoopValStage != -1 && StageScheduled > LoopValStage)
2225 StageDiffAdj = StageScheduled - LoopValStage;
2226 // Use the loop value defined in the kernel, unless the kernel
2227 // contains the last definition of the Phi.
2228 if (np == 0 && PrevStage == LastStageNum &&
2229 (StageScheduled != 0 || LoopValStage != 0) &&
2230 VRMap[PrevStage - StageDiffAdj].count(LoopVal))
2231 PhiOp2 = VRMap[PrevStage - StageDiffAdj][LoopVal];
2232 // Use the value defined by the Phi. We add one because we switch
2233 // from looking at the loop value to the Phi definition.
2234 else if (np > 0 && PrevStage == LastStageNum &&
2235 VRMap[PrevStage - np + 1].count(Def))
2236 PhiOp2 = VRMap[PrevStage - np + 1][Def];
2237 // Use the loop value defined in the kernel.
Brendon Cahoone3841eea2018-08-27 22:04:50 +00002238 else if (static_cast<unsigned>(LoopValStage) > PrologStage + 1 &&
Brendon Cahoon254f8892016-07-29 16:44:44 +00002239 VRMap[PrevStage - StageDiffAdj - np].count(LoopVal))
2240 PhiOp2 = VRMap[PrevStage - StageDiffAdj - np][LoopVal];
2241 // Use the value defined by the Phi, unless we're generating the first
2242 // epilog and the Phi refers to a Phi in a different stage.
2243 else if (VRMap[PrevStage - np].count(Def) &&
2244 (!LoopDefIsPhi || PrevStage != LastStageNum))
2245 PhiOp2 = VRMap[PrevStage - np][Def];
2246 }
2247
2248 // Check if we can reuse an existing Phi. This occurs when a Phi
2249 // references another Phi, and the other Phi is scheduled in an
2250 // earlier stage. We can try to reuse an existing Phi up until the last
2251 // stage of the current Phi.
Brendon Cahoone3841eea2018-08-27 22:04:50 +00002252 if (LoopDefIsPhi) {
2253 if (static_cast<int>(PrologStage - np) >= StageScheduled) {
2254 int LVNumStages = Schedule.getStagesForPhi(LoopVal);
2255 int StageDiff = (StageScheduled - LoopValStage);
2256 LVNumStages -= StageDiff;
2257 // Make sure the loop value Phi has been processed already.
2258 if (LVNumStages > (int)np && VRMap[CurStageNum].count(LoopVal)) {
2259 NewReg = PhiOp2;
2260 unsigned ReuseStage = CurStageNum;
2261 if (Schedule.isLoopCarried(this, *PhiInst))
2262 ReuseStage -= LVNumStages;
2263 // Check if the Phi to reuse has been generated yet. If not, then
2264 // there is nothing to reuse.
2265 if (VRMap[ReuseStage - np].count(LoopVal)) {
2266 NewReg = VRMap[ReuseStage - np][LoopVal];
Brendon Cahoon254f8892016-07-29 16:44:44 +00002267
Brendon Cahoone3841eea2018-08-27 22:04:50 +00002268 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2269 &*BBI, Def, NewReg);
2270 // Update the map with the new Phi name.
2271 VRMap[CurStageNum - np][Def] = NewReg;
2272 PhiOp2 = NewReg;
2273 if (VRMap[LastStageNum - np - 1].count(LoopVal))
2274 PhiOp2 = VRMap[LastStageNum - np - 1][LoopVal];
Brendon Cahoon254f8892016-07-29 16:44:44 +00002275
Brendon Cahoone3841eea2018-08-27 22:04:50 +00002276 if (IsLast && np == NumPhis - 1)
2277 replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS);
2278 continue;
2279 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00002280 }
Brendon Cahoone3841eea2018-08-27 22:04:50 +00002281 }
2282 if (InKernel && StageDiff > 0 &&
2283 VRMap[CurStageNum - StageDiff - np].count(LoopVal))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002284 PhiOp2 = VRMap[CurStageNum - StageDiff - np][LoopVal];
2285 }
2286
2287 const TargetRegisterClass *RC = MRI.getRegClass(Def);
2288 NewReg = MRI.createVirtualRegister(RC);
2289
2290 MachineInstrBuilder NewPhi =
2291 BuildMI(*NewBB, NewBB->getFirstNonPHI(), DebugLoc(),
2292 TII->get(TargetOpcode::PHI), NewReg);
2293 NewPhi.addReg(PhiOp1).addMBB(BB1);
2294 NewPhi.addReg(PhiOp2).addMBB(BB2);
2295 if (np == 0)
2296 InstrMap[NewPhi] = &*BBI;
2297
2298 // We define the Phis after creating the new pipelined code, so
2299 // we need to rename the Phi values in scheduled instructions.
2300
2301 unsigned PrevReg = 0;
2302 if (InKernel && VRMap[PrevStage - np].count(LoopVal))
2303 PrevReg = VRMap[PrevStage - np][LoopVal];
2304 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np, &*BBI,
2305 Def, NewReg, PrevReg);
2306 // If the Phi has been scheduled, use the new name for rewriting.
2307 if (VRMap[CurStageNum - np].count(Def)) {
2308 unsigned R = VRMap[CurStageNum - np][Def];
2309 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np, &*BBI,
2310 R, NewReg);
2311 }
2312
2313 // Check if we need to rename any uses that occurs after the loop. The
2314 // register to replace depends on whether the Phi is scheduled in the
2315 // epilog.
2316 if (IsLast && np == NumPhis - 1)
2317 replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS);
2318
2319 // In the kernel, a dependent Phi uses the value from this Phi.
2320 if (InKernel)
2321 PhiOp2 = NewReg;
2322
2323 // Update the map with the new Phi name.
2324 VRMap[CurStageNum - np][Def] = NewReg;
2325 }
2326
2327 while (NumPhis++ < NumStages) {
2328 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, NumPhis,
2329 &*BBI, Def, NewReg, 0);
2330 }
2331
2332 // Check if we need to rename a Phi that has been eliminated due to
2333 // scheduling.
2334 if (NumStages == 0 && IsLast && VRMap[CurStageNum].count(LoopVal))
2335 replaceRegUsesAfterLoop(Def, VRMap[CurStageNum][LoopVal], BB, MRI, LIS);
2336 }
2337}
2338
2339/// Generate Phis for the specified block in the generated pipelined code.
2340/// These are new Phis needed because the definition is scheduled after the
Hiroshi Inouec73b6d62018-06-20 05:29:26 +00002341/// use in the pipelined sequence.
Brendon Cahoon254f8892016-07-29 16:44:44 +00002342void SwingSchedulerDAG::generatePhis(
2343 MachineBasicBlock *NewBB, MachineBasicBlock *BB1, MachineBasicBlock *BB2,
2344 MachineBasicBlock *KernelBB, SMSchedule &Schedule, ValueMapTy *VRMap,
2345 InstrMapTy &InstrMap, unsigned LastStageNum, unsigned CurStageNum,
2346 bool IsLast) {
2347 // Compute the stage number that contains the initial Phi value, and
2348 // the Phi from the previous stage.
2349 unsigned PrologStage = 0;
2350 unsigned PrevStage = 0;
2351 unsigned StageDiff = CurStageNum - LastStageNum;
2352 bool InKernel = (StageDiff == 0);
2353 if (InKernel) {
2354 PrologStage = LastStageNum - 1;
2355 PrevStage = CurStageNum;
2356 } else {
2357 PrologStage = LastStageNum - StageDiff;
2358 PrevStage = LastStageNum + StageDiff - 1;
2359 }
2360
2361 for (MachineBasicBlock::iterator BBI = BB->getFirstNonPHI(),
2362 BBE = BB->instr_end();
2363 BBI != BBE; ++BBI) {
2364 for (unsigned i = 0, e = BBI->getNumOperands(); i != e; ++i) {
2365 MachineOperand &MO = BBI->getOperand(i);
2366 if (!MO.isReg() || !MO.isDef() ||
2367 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2368 continue;
2369
2370 int StageScheduled = Schedule.stageScheduled(getSUnit(&*BBI));
2371 assert(StageScheduled != -1 && "Expecting scheduled instruction.");
2372 unsigned Def = MO.getReg();
2373 unsigned NumPhis = Schedule.getStagesForReg(Def, CurStageNum);
2374 // An instruction scheduled in stage 0 and is used after the loop
2375 // requires a phi in the epilog for the last definition from either
2376 // the kernel or prolog.
2377 if (!InKernel && NumPhis == 0 && StageScheduled == 0 &&
2378 hasUseAfterLoop(Def, BB, MRI))
2379 NumPhis = 1;
2380 if (!InKernel && (unsigned)StageScheduled > PrologStage)
2381 continue;
2382
2383 unsigned PhiOp2 = VRMap[PrevStage][Def];
2384 if (MachineInstr *InstOp2 = MRI.getVRegDef(PhiOp2))
2385 if (InstOp2->isPHI() && InstOp2->getParent() == NewBB)
2386 PhiOp2 = getLoopPhiReg(*InstOp2, BB2);
2387 // The number of Phis can't exceed the number of prolog stages. The
2388 // prolog stage number is zero based.
2389 if (NumPhis > PrologStage + 1 - StageScheduled)
2390 NumPhis = PrologStage + 1 - StageScheduled;
2391 for (unsigned np = 0; np < NumPhis; ++np) {
2392 unsigned PhiOp1 = VRMap[PrologStage][Def];
2393 if (np <= PrologStage)
2394 PhiOp1 = VRMap[PrologStage - np][Def];
2395 if (MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1)) {
2396 if (InstOp1->isPHI() && InstOp1->getParent() == KernelBB)
2397 PhiOp1 = getInitPhiReg(*InstOp1, KernelBB);
2398 if (InstOp1->isPHI() && InstOp1->getParent() == NewBB)
2399 PhiOp1 = getInitPhiReg(*InstOp1, NewBB);
2400 }
2401 if (!InKernel)
2402 PhiOp2 = VRMap[PrevStage - np][Def];
2403
2404 const TargetRegisterClass *RC = MRI.getRegClass(Def);
2405 unsigned NewReg = MRI.createVirtualRegister(RC);
2406
2407 MachineInstrBuilder NewPhi =
2408 BuildMI(*NewBB, NewBB->getFirstNonPHI(), DebugLoc(),
2409 TII->get(TargetOpcode::PHI), NewReg);
2410 NewPhi.addReg(PhiOp1).addMBB(BB1);
2411 NewPhi.addReg(PhiOp2).addMBB(BB2);
2412 if (np == 0)
2413 InstrMap[NewPhi] = &*BBI;
2414
2415 // Rewrite uses and update the map. The actions depend upon whether
2416 // we generating code for the kernel or epilog blocks.
2417 if (InKernel) {
2418 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2419 &*BBI, PhiOp1, NewReg);
2420 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2421 &*BBI, PhiOp2, NewReg);
2422
2423 PhiOp2 = NewReg;
2424 VRMap[PrevStage - np - 1][Def] = NewReg;
2425 } else {
2426 VRMap[CurStageNum - np][Def] = NewReg;
2427 if (np == NumPhis - 1)
2428 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2429 &*BBI, Def, NewReg);
2430 }
2431 if (IsLast && np == NumPhis - 1)
2432 replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS);
2433 }
2434 }
2435 }
2436}
2437
2438/// Remove instructions that generate values with no uses.
2439/// Typically, these are induction variable operations that generate values
2440/// used in the loop itself. A dead instruction has a definition with
2441/// no uses, or uses that occur in the original loop only.
2442void SwingSchedulerDAG::removeDeadInstructions(MachineBasicBlock *KernelBB,
2443 MBBVectorTy &EpilogBBs) {
2444 // For each epilog block, check that the value defined by each instruction
2445 // is used. If not, delete it.
2446 for (MBBVectorTy::reverse_iterator MBB = EpilogBBs.rbegin(),
2447 MBE = EpilogBBs.rend();
2448 MBB != MBE; ++MBB)
2449 for (MachineBasicBlock::reverse_instr_iterator MI = (*MBB)->instr_rbegin(),
2450 ME = (*MBB)->instr_rend();
2451 MI != ME;) {
2452 // From DeadMachineInstructionElem. Don't delete inline assembly.
2453 if (MI->isInlineAsm()) {
2454 ++MI;
2455 continue;
2456 }
2457 bool SawStore = false;
2458 // Check if it's safe to remove the instruction due to side effects.
2459 // We can, and want to, remove Phis here.
2460 if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI()) {
2461 ++MI;
2462 continue;
2463 }
2464 bool used = true;
2465 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
2466 MOE = MI->operands_end();
2467 MOI != MOE; ++MOI) {
2468 if (!MOI->isReg() || !MOI->isDef())
2469 continue;
2470 unsigned reg = MOI->getReg();
Krzysztof Parzyszekb9b75b82018-03-26 15:53:23 +00002471 // Assume physical registers are used, unless they are marked dead.
2472 if (TargetRegisterInfo::isPhysicalRegister(reg)) {
2473 used = !MOI->isDead();
2474 if (used)
2475 break;
2476 continue;
2477 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00002478 unsigned realUses = 0;
2479 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(reg),
2480 EI = MRI.use_end();
2481 UI != EI; ++UI) {
2482 // Check if there are any uses that occur only in the original
2483 // loop. If so, that's not a real use.
2484 if (UI->getParent()->getParent() != BB) {
2485 realUses++;
2486 used = true;
2487 break;
2488 }
2489 }
2490 if (realUses > 0)
2491 break;
2492 used = false;
2493 }
2494 if (!used) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00002495 LIS.RemoveMachineInstrFromMaps(*MI);
Duncan P. N. Exon Smith5c001c32016-08-30 00:13:12 +00002496 MI++->eraseFromParent();
Brendon Cahoon254f8892016-07-29 16:44:44 +00002497 continue;
2498 }
2499 ++MI;
2500 }
2501 // In the kernel block, check if we can remove a Phi that generates a value
2502 // used in an instruction removed in the epilog block.
2503 for (MachineBasicBlock::iterator BBI = KernelBB->instr_begin(),
2504 BBE = KernelBB->getFirstNonPHI();
2505 BBI != BBE;) {
2506 MachineInstr *MI = &*BBI;
2507 ++BBI;
2508 unsigned reg = MI->getOperand(0).getReg();
2509 if (MRI.use_begin(reg) == MRI.use_end()) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00002510 LIS.RemoveMachineInstrFromMaps(*MI);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002511 MI->eraseFromParent();
2512 }
2513 }
2514}
2515
2516/// For loop carried definitions, we split the lifetime of a virtual register
2517/// that has uses past the definition in the next iteration. A copy with a new
2518/// virtual register is inserted before the definition, which helps with
2519/// generating a better register assignment.
2520///
2521/// v1 = phi(a, v2) v1 = phi(a, v2)
2522/// v2 = phi(b, v3) v2 = phi(b, v3)
2523/// v3 = .. v4 = copy v1
2524/// .. = V1 v3 = ..
2525/// .. = v4
2526void SwingSchedulerDAG::splitLifetimes(MachineBasicBlock *KernelBB,
2527 MBBVectorTy &EpilogBBs,
2528 SMSchedule &Schedule) {
2529 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Bob Wilson90ecac02018-01-04 02:58:15 +00002530 for (auto &PHI : KernelBB->phis()) {
2531 unsigned Def = PHI.getOperand(0).getReg();
Brendon Cahoon254f8892016-07-29 16:44:44 +00002532 // Check for any Phi definition that used as an operand of another Phi
2533 // in the same block.
2534 for (MachineRegisterInfo::use_instr_iterator I = MRI.use_instr_begin(Def),
2535 E = MRI.use_instr_end();
2536 I != E; ++I) {
2537 if (I->isPHI() && I->getParent() == KernelBB) {
2538 // Get the loop carried definition.
Bob Wilson90ecac02018-01-04 02:58:15 +00002539 unsigned LCDef = getLoopPhiReg(PHI, KernelBB);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002540 if (!LCDef)
2541 continue;
2542 MachineInstr *MI = MRI.getVRegDef(LCDef);
2543 if (!MI || MI->getParent() != KernelBB || MI->isPHI())
2544 continue;
2545 // Search through the rest of the block looking for uses of the Phi
2546 // definition. If one occurs, then split the lifetime.
2547 unsigned SplitReg = 0;
2548 for (auto &BBJ : make_range(MachineBasicBlock::instr_iterator(MI),
2549 KernelBB->instr_end()))
2550 if (BBJ.readsRegister(Def)) {
2551 // We split the lifetime when we find the first use.
2552 if (SplitReg == 0) {
2553 SplitReg = MRI.createVirtualRegister(MRI.getRegClass(Def));
2554 BuildMI(*KernelBB, MI, MI->getDebugLoc(),
2555 TII->get(TargetOpcode::COPY), SplitReg)
2556 .addReg(Def);
2557 }
2558 BBJ.substituteRegister(Def, SplitReg, 0, *TRI);
2559 }
2560 if (!SplitReg)
2561 continue;
2562 // Search through each of the epilog blocks for any uses to be renamed.
2563 for (auto &Epilog : EpilogBBs)
2564 for (auto &I : *Epilog)
2565 if (I.readsRegister(Def))
2566 I.substituteRegister(Def, SplitReg, 0, *TRI);
2567 break;
2568 }
2569 }
2570 }
2571}
2572
2573/// Remove the incoming block from the Phis in a basic block.
2574static void removePhis(MachineBasicBlock *BB, MachineBasicBlock *Incoming) {
2575 for (MachineInstr &MI : *BB) {
2576 if (!MI.isPHI())
2577 break;
2578 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2)
2579 if (MI.getOperand(i + 1).getMBB() == Incoming) {
2580 MI.RemoveOperand(i + 1);
2581 MI.RemoveOperand(i);
2582 break;
2583 }
2584 }
2585}
2586
2587/// Create branches from each prolog basic block to the appropriate epilog
2588/// block. These edges are needed if the loop ends before reaching the
2589/// kernel.
2590void SwingSchedulerDAG::addBranches(MBBVectorTy &PrologBBs,
2591 MachineBasicBlock *KernelBB,
2592 MBBVectorTy &EpilogBBs,
2593 SMSchedule &Schedule, ValueMapTy *VRMap) {
2594 assert(PrologBBs.size() == EpilogBBs.size() && "Prolog/Epilog mismatch");
2595 MachineInstr *IndVar = Pass.LI.LoopInductionVar;
2596 MachineInstr *Cmp = Pass.LI.LoopCompare;
2597 MachineBasicBlock *LastPro = KernelBB;
2598 MachineBasicBlock *LastEpi = KernelBB;
2599
2600 // Start from the blocks connected to the kernel and work "out"
2601 // to the first prolog and the last epilog blocks.
2602 SmallVector<MachineInstr *, 4> PrevInsts;
2603 unsigned MaxIter = PrologBBs.size() - 1;
2604 unsigned LC = UINT_MAX;
2605 unsigned LCMin = UINT_MAX;
2606 for (unsigned i = 0, j = MaxIter; i <= MaxIter; ++i, --j) {
2607 // Add branches to the prolog that go to the corresponding
2608 // epilog, and the fall-thru prolog/kernel block.
2609 MachineBasicBlock *Prolog = PrologBBs[j];
2610 MachineBasicBlock *Epilog = EpilogBBs[i];
2611 // We've executed one iteration, so decrement the loop count and check for
2612 // the loop end.
2613 SmallVector<MachineOperand, 4> Cond;
2614 // Check if the LOOP0 has already been removed. If so, then there is no need
2615 // to reduce the trip count.
2616 if (LC != 0)
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002617 LC = TII->reduceLoopCount(*Prolog, IndVar, *Cmp, Cond, PrevInsts, j,
Brendon Cahoon254f8892016-07-29 16:44:44 +00002618 MaxIter);
2619
2620 // Record the value of the first trip count, which is used to determine if
2621 // branches and blocks can be removed for constant trip counts.
2622 if (LCMin == UINT_MAX)
2623 LCMin = LC;
2624
2625 unsigned numAdded = 0;
2626 if (TargetRegisterInfo::isVirtualRegister(LC)) {
2627 Prolog->addSuccessor(Epilog);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00002628 numAdded = TII->insertBranch(*Prolog, Epilog, LastPro, Cond, DebugLoc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00002629 } else if (j >= LCMin) {
2630 Prolog->addSuccessor(Epilog);
2631 Prolog->removeSuccessor(LastPro);
2632 LastEpi->removeSuccessor(Epilog);
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00002633 numAdded = TII->insertBranch(*Prolog, Epilog, nullptr, Cond, DebugLoc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00002634 removePhis(Epilog, LastEpi);
2635 // Remove the blocks that are no longer referenced.
2636 if (LastPro != LastEpi) {
2637 LastEpi->clear();
2638 LastEpi->eraseFromParent();
2639 }
2640 LastPro->clear();
2641 LastPro->eraseFromParent();
2642 } else {
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +00002643 numAdded = TII->insertBranch(*Prolog, LastPro, nullptr, Cond, DebugLoc());
Brendon Cahoon254f8892016-07-29 16:44:44 +00002644 removePhis(Epilog, Prolog);
2645 }
2646 LastPro = Prolog;
2647 LastEpi = Epilog;
2648 for (MachineBasicBlock::reverse_instr_iterator I = Prolog->instr_rbegin(),
2649 E = Prolog->instr_rend();
2650 I != E && numAdded > 0; ++I, --numAdded)
2651 updateInstruction(&*I, false, j, 0, Schedule, VRMap);
2652 }
2653}
2654
2655/// Return true if we can compute the amount the instruction changes
2656/// during each iteration. Set Delta to the amount of the change.
2657bool SwingSchedulerDAG::computeDelta(MachineInstr &MI, unsigned &Delta) {
2658 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002659 MachineOperand *BaseOp;
Brendon Cahoon254f8892016-07-29 16:44:44 +00002660 int64_t Offset;
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002661 if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, TRI))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002662 return false;
2663
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00002664 if (!BaseOp->isReg())
2665 return false;
2666
2667 unsigned BaseReg = BaseOp->getReg();
2668
Brendon Cahoon254f8892016-07-29 16:44:44 +00002669 MachineRegisterInfo &MRI = MF.getRegInfo();
2670 // Check if there is a Phi. If so, get the definition in the loop.
2671 MachineInstr *BaseDef = MRI.getVRegDef(BaseReg);
2672 if (BaseDef && BaseDef->isPHI()) {
2673 BaseReg = getLoopPhiReg(*BaseDef, MI.getParent());
2674 BaseDef = MRI.getVRegDef(BaseReg);
2675 }
2676 if (!BaseDef)
2677 return false;
2678
2679 int D = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002680 if (!TII->getIncrementValue(*BaseDef, D) && D >= 0)
Brendon Cahoon254f8892016-07-29 16:44:44 +00002681 return false;
2682
2683 Delta = D;
2684 return true;
2685}
2686
2687/// Update the memory operand with a new offset when the pipeliner
Justin Lebarcf56e922016-08-12 23:58:19 +00002688/// generates a new copy of the instruction that refers to a
Brendon Cahoon254f8892016-07-29 16:44:44 +00002689/// different memory location.
2690void SwingSchedulerDAG::updateMemOperands(MachineInstr &NewMI,
2691 MachineInstr &OldMI, unsigned Num) {
2692 if (Num == 0)
2693 return;
2694 // If the instruction has memory operands, then adjust the offset
2695 // when the instruction appears in different stages.
Chandler Carruthc73c0302018-08-16 21:30:05 +00002696 if (NewMI.memoperands_empty())
Brendon Cahoon254f8892016-07-29 16:44:44 +00002697 return;
Chandler Carruthc73c0302018-08-16 21:30:05 +00002698 SmallVector<MachineMemOperand *, 2> NewMMOs;
Justin Lebar0a33a7a2016-08-23 17:18:07 +00002699 for (MachineMemOperand *MMO : NewMI.memoperands()) {
Justin Lebaradbf09e2016-09-11 01:38:58 +00002700 if (MMO->isVolatile() || (MMO->isInvariant() && MMO->isDereferenceable()) ||
2701 (!MMO->getValue())) {
Chandler Carruthc73c0302018-08-16 21:30:05 +00002702 NewMMOs.push_back(MMO);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002703 continue;
2704 }
2705 unsigned Delta;
Krzysztof Parzyszek785b6ce2018-03-26 15:45:55 +00002706 if (Num != UINT_MAX && computeDelta(OldMI, Delta)) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00002707 int64_t AdjOffset = Delta * Num;
Chandler Carruthc73c0302018-08-16 21:30:05 +00002708 NewMMOs.push_back(
2709 MF.getMachineMemOperand(MMO, AdjOffset, MMO->getSize()));
Krzysztof Parzyszek2d790172018-02-27 22:40:52 +00002710 } else {
Krzysztof Parzyszekcc3f6302018-08-20 20:37:57 +00002711 NewMMOs.push_back(
2712 MF.getMachineMemOperand(MMO, 0, MemoryLocation::UnknownSize));
Krzysztof Parzyszek2d790172018-02-27 22:40:52 +00002713 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00002714 }
Chandler Carruthc73c0302018-08-16 21:30:05 +00002715 NewMI.setMemRefs(MF, NewMMOs);
Brendon Cahoon254f8892016-07-29 16:44:44 +00002716}
2717
2718/// Clone the instruction for the new pipelined loop and update the
2719/// memory operands, if needed.
2720MachineInstr *SwingSchedulerDAG::cloneInstr(MachineInstr *OldMI,
2721 unsigned CurStageNum,
2722 unsigned InstStageNum) {
2723 MachineInstr *NewMI = MF.CloneMachineInstr(OldMI);
2724 // Check for tied operands in inline asm instructions. This should be handled
2725 // elsewhere, but I'm not sure of the best solution.
2726 if (OldMI->isInlineAsm())
2727 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
2728 const auto &MO = OldMI->getOperand(i);
2729 if (MO.isReg() && MO.isUse())
2730 break;
2731 unsigned UseIdx;
2732 if (OldMI->isRegTiedToUseOperand(i, &UseIdx))
2733 NewMI->tieOperands(i, UseIdx);
2734 }
2735 updateMemOperands(*NewMI, *OldMI, CurStageNum - InstStageNum);
2736 return NewMI;
2737}
2738
2739/// Clone the instruction for the new pipelined loop. If needed, this
2740/// function updates the instruction using the values saved in the
2741/// InstrChanges structure.
2742MachineInstr *SwingSchedulerDAG::cloneAndChangeInstr(MachineInstr *OldMI,
2743 unsigned CurStageNum,
2744 unsigned InstStageNum,
2745 SMSchedule &Schedule) {
2746 MachineInstr *NewMI = MF.CloneMachineInstr(OldMI);
2747 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
2748 InstrChanges.find(getSUnit(OldMI));
2749 if (It != InstrChanges.end()) {
2750 std::pair<unsigned, int64_t> RegAndOffset = It->second;
2751 unsigned BasePos, OffsetPos;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002752 if (!TII->getBaseAndOffsetPosition(*OldMI, BasePos, OffsetPos))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002753 return nullptr;
2754 int64_t NewOffset = OldMI->getOperand(OffsetPos).getImm();
2755 MachineInstr *LoopDef = findDefInLoop(RegAndOffset.first);
2756 if (Schedule.stageScheduled(getSUnit(LoopDef)) > (signed)InstStageNum)
2757 NewOffset += RegAndOffset.second * (CurStageNum - InstStageNum);
2758 NewMI->getOperand(OffsetPos).setImm(NewOffset);
2759 }
2760 updateMemOperands(*NewMI, *OldMI, CurStageNum - InstStageNum);
2761 return NewMI;
2762}
2763
2764/// Update the machine instruction with new virtual registers. This
2765/// function may change the defintions and/or uses.
2766void SwingSchedulerDAG::updateInstruction(MachineInstr *NewMI, bool LastDef,
2767 unsigned CurStageNum,
2768 unsigned InstrStageNum,
2769 SMSchedule &Schedule,
2770 ValueMapTy *VRMap) {
2771 for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
2772 MachineOperand &MO = NewMI->getOperand(i);
2773 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2774 continue;
2775 unsigned reg = MO.getReg();
2776 if (MO.isDef()) {
2777 // Create a new virtual register for the definition.
2778 const TargetRegisterClass *RC = MRI.getRegClass(reg);
2779 unsigned NewReg = MRI.createVirtualRegister(RC);
2780 MO.setReg(NewReg);
2781 VRMap[CurStageNum][reg] = NewReg;
2782 if (LastDef)
2783 replaceRegUsesAfterLoop(reg, NewReg, BB, MRI, LIS);
2784 } else if (MO.isUse()) {
2785 MachineInstr *Def = MRI.getVRegDef(reg);
2786 // Compute the stage that contains the last definition for instruction.
2787 int DefStageNum = Schedule.stageScheduled(getSUnit(Def));
2788 unsigned StageNum = CurStageNum;
2789 if (DefStageNum != -1 && (int)InstrStageNum > DefStageNum) {
2790 // Compute the difference in stages between the defintion and the use.
2791 unsigned StageDiff = (InstrStageNum - DefStageNum);
2792 // Make an adjustment to get the last definition.
2793 StageNum -= StageDiff;
2794 }
2795 if (VRMap[StageNum].count(reg))
2796 MO.setReg(VRMap[StageNum][reg]);
2797 }
2798 }
2799}
2800
2801/// Return the instruction in the loop that defines the register.
2802/// If the definition is a Phi, then follow the Phi operand to
2803/// the instruction in the loop.
2804MachineInstr *SwingSchedulerDAG::findDefInLoop(unsigned Reg) {
2805 SmallPtrSet<MachineInstr *, 8> Visited;
2806 MachineInstr *Def = MRI.getVRegDef(Reg);
2807 while (Def->isPHI()) {
2808 if (!Visited.insert(Def).second)
2809 break;
2810 for (unsigned i = 1, e = Def->getNumOperands(); i < e; i += 2)
2811 if (Def->getOperand(i + 1).getMBB() == BB) {
2812 Def = MRI.getVRegDef(Def->getOperand(i).getReg());
2813 break;
2814 }
2815 }
2816 return Def;
2817}
2818
2819/// Return the new name for the value from the previous stage.
2820unsigned SwingSchedulerDAG::getPrevMapVal(unsigned StageNum, unsigned PhiStage,
2821 unsigned LoopVal, unsigned LoopStage,
2822 ValueMapTy *VRMap,
2823 MachineBasicBlock *BB) {
2824 unsigned PrevVal = 0;
2825 if (StageNum > PhiStage) {
2826 MachineInstr *LoopInst = MRI.getVRegDef(LoopVal);
2827 if (PhiStage == LoopStage && VRMap[StageNum - 1].count(LoopVal))
2828 // The name is defined in the previous stage.
2829 PrevVal = VRMap[StageNum - 1][LoopVal];
2830 else if (VRMap[StageNum].count(LoopVal))
2831 // The previous name is defined in the current stage when the instruction
2832 // order is swapped.
2833 PrevVal = VRMap[StageNum][LoopVal];
Krzysztof Parzyszekdf24da22016-12-22 18:49:55 +00002834 else if (!LoopInst->isPHI() || LoopInst->getParent() != BB)
Brendon Cahoon254f8892016-07-29 16:44:44 +00002835 // The loop value hasn't yet been scheduled.
2836 PrevVal = LoopVal;
2837 else if (StageNum == PhiStage + 1)
2838 // The loop value is another phi, which has not been scheduled.
2839 PrevVal = getInitPhiReg(*LoopInst, BB);
2840 else if (StageNum > PhiStage + 1 && LoopInst->getParent() == BB)
2841 // The loop value is another phi, which has been scheduled.
2842 PrevVal =
2843 getPrevMapVal(StageNum - 1, PhiStage, getLoopPhiReg(*LoopInst, BB),
2844 LoopStage, VRMap, BB);
2845 }
2846 return PrevVal;
2847}
2848
2849/// Rewrite the Phi values in the specified block to use the mappings
2850/// from the initial operand. Once the Phi is scheduled, we switch
2851/// to using the loop value instead of the Phi value, so those names
2852/// do not need to be rewritten.
2853void SwingSchedulerDAG::rewritePhiValues(MachineBasicBlock *NewBB,
2854 unsigned StageNum,
2855 SMSchedule &Schedule,
2856 ValueMapTy *VRMap,
2857 InstrMapTy &InstrMap) {
Bob Wilson90ecac02018-01-04 02:58:15 +00002858 for (auto &PHI : BB->phis()) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00002859 unsigned InitVal = 0;
2860 unsigned LoopVal = 0;
Bob Wilson90ecac02018-01-04 02:58:15 +00002861 getPhiRegs(PHI, BB, InitVal, LoopVal);
2862 unsigned PhiDef = PHI.getOperand(0).getReg();
Brendon Cahoon254f8892016-07-29 16:44:44 +00002863
2864 unsigned PhiStage =
2865 (unsigned)Schedule.stageScheduled(getSUnit(MRI.getVRegDef(PhiDef)));
2866 unsigned LoopStage =
2867 (unsigned)Schedule.stageScheduled(getSUnit(MRI.getVRegDef(LoopVal)));
2868 unsigned NumPhis = Schedule.getStagesForPhi(PhiDef);
2869 if (NumPhis > StageNum)
2870 NumPhis = StageNum;
2871 for (unsigned np = 0; np <= NumPhis; ++np) {
2872 unsigned NewVal =
2873 getPrevMapVal(StageNum - np, PhiStage, LoopVal, LoopStage, VRMap, BB);
2874 if (!NewVal)
2875 NewVal = InitVal;
Bob Wilson90ecac02018-01-04 02:58:15 +00002876 rewriteScheduledInstr(NewBB, Schedule, InstrMap, StageNum - np, np, &PHI,
Brendon Cahoon254f8892016-07-29 16:44:44 +00002877 PhiDef, NewVal);
2878 }
2879 }
2880}
2881
2882/// Rewrite a previously scheduled instruction to use the register value
2883/// from the new instruction. Make sure the instruction occurs in the
2884/// basic block, and we don't change the uses in the new instruction.
2885void SwingSchedulerDAG::rewriteScheduledInstr(
2886 MachineBasicBlock *BB, SMSchedule &Schedule, InstrMapTy &InstrMap,
2887 unsigned CurStageNum, unsigned PhiNum, MachineInstr *Phi, unsigned OldReg,
2888 unsigned NewReg, unsigned PrevReg) {
2889 bool InProlog = (CurStageNum < Schedule.getMaxStageCount());
2890 int StagePhi = Schedule.stageScheduled(getSUnit(Phi)) + PhiNum;
2891 // Rewrite uses that have been scheduled already to use the new
2892 // Phi register.
2893 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(OldReg),
2894 EI = MRI.use_end();
2895 UI != EI;) {
2896 MachineOperand &UseOp = *UI;
2897 MachineInstr *UseMI = UseOp.getParent();
2898 ++UI;
2899 if (UseMI->getParent() != BB)
2900 continue;
2901 if (UseMI->isPHI()) {
2902 if (!Phi->isPHI() && UseMI->getOperand(0).getReg() == NewReg)
2903 continue;
2904 if (getLoopPhiReg(*UseMI, BB) != OldReg)
2905 continue;
2906 }
2907 InstrMapTy::iterator OrigInstr = InstrMap.find(UseMI);
2908 assert(OrigInstr != InstrMap.end() && "Instruction not scheduled.");
2909 SUnit *OrigMISU = getSUnit(OrigInstr->second);
2910 int StageSched = Schedule.stageScheduled(OrigMISU);
2911 int CycleSched = Schedule.cycleScheduled(OrigMISU);
2912 unsigned ReplaceReg = 0;
2913 // This is the stage for the scheduled instruction.
2914 if (StagePhi == StageSched && Phi->isPHI()) {
2915 int CyclePhi = Schedule.cycleScheduled(getSUnit(Phi));
2916 if (PrevReg && InProlog)
2917 ReplaceReg = PrevReg;
2918 else if (PrevReg && !Schedule.isLoopCarried(this, *Phi) &&
2919 (CyclePhi <= CycleSched || OrigMISU->getInstr()->isPHI()))
2920 ReplaceReg = PrevReg;
2921 else
2922 ReplaceReg = NewReg;
2923 }
2924 // The scheduled instruction occurs before the scheduled Phi, and the
2925 // Phi is not loop carried.
2926 if (!InProlog && StagePhi + 1 == StageSched &&
2927 !Schedule.isLoopCarried(this, *Phi))
2928 ReplaceReg = NewReg;
2929 if (StagePhi > StageSched && Phi->isPHI())
2930 ReplaceReg = NewReg;
2931 if (!InProlog && !Phi->isPHI() && StagePhi < StageSched)
2932 ReplaceReg = NewReg;
2933 if (ReplaceReg) {
2934 MRI.constrainRegClass(ReplaceReg, MRI.getRegClass(OldReg));
2935 UseOp.setReg(ReplaceReg);
2936 }
2937 }
2938}
2939
2940/// Check if we can change the instruction to use an offset value from the
2941/// previous iteration. If so, return true and set the base and offset values
2942/// so that we can rewrite the load, if necessary.
2943/// v1 = Phi(v0, v3)
2944/// v2 = load v1, 0
2945/// v3 = post_store v1, 4, x
2946/// This function enables the load to be rewritten as v2 = load v3, 4.
2947bool SwingSchedulerDAG::canUseLastOffsetValue(MachineInstr *MI,
2948 unsigned &BasePos,
2949 unsigned &OffsetPos,
2950 unsigned &NewBase,
2951 int64_t &Offset) {
2952 // Get the load instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002953 if (TII->isPostIncrement(*MI))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002954 return false;
2955 unsigned BasePosLd, OffsetPosLd;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002956 if (!TII->getBaseAndOffsetPosition(*MI, BasePosLd, OffsetPosLd))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002957 return false;
2958 unsigned BaseReg = MI->getOperand(BasePosLd).getReg();
2959
2960 // Look for the Phi instruction.
Justin Bognerfdf9bf42017-10-10 23:50:49 +00002961 MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
Brendon Cahoon254f8892016-07-29 16:44:44 +00002962 MachineInstr *Phi = MRI.getVRegDef(BaseReg);
2963 if (!Phi || !Phi->isPHI())
2964 return false;
2965 // Get the register defined in the loop block.
2966 unsigned PrevReg = getLoopPhiReg(*Phi, MI->getParent());
2967 if (!PrevReg)
2968 return false;
2969
2970 // Check for the post-increment load/store instruction.
2971 MachineInstr *PrevDef = MRI.getVRegDef(PrevReg);
2972 if (!PrevDef || PrevDef == MI)
2973 return false;
2974
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002975 if (!TII->isPostIncrement(*PrevDef))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002976 return false;
2977
2978 unsigned BasePos1 = 0, OffsetPos1 = 0;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00002979 if (!TII->getBaseAndOffsetPosition(*PrevDef, BasePos1, OffsetPos1))
Brendon Cahoon254f8892016-07-29 16:44:44 +00002980 return false;
2981
Krzysztof Parzyszek40df8a22018-03-26 16:17:06 +00002982 // Make sure that the instructions do not access the same memory location in
2983 // the next iteration.
Brendon Cahoon254f8892016-07-29 16:44:44 +00002984 int64_t LoadOffset = MI->getOperand(OffsetPosLd).getImm();
2985 int64_t StoreOffset = PrevDef->getOperand(OffsetPos1).getImm();
Krzysztof Parzyszek40df8a22018-03-26 16:17:06 +00002986 MachineInstr *NewMI = MF.CloneMachineInstr(MI);
2987 NewMI->getOperand(OffsetPosLd).setImm(LoadOffset + StoreOffset);
2988 bool Disjoint = TII->areMemAccessesTriviallyDisjoint(*NewMI, *PrevDef);
2989 MF.DeleteMachineInstr(NewMI);
2990 if (!Disjoint)
Brendon Cahoon254f8892016-07-29 16:44:44 +00002991 return false;
2992
2993 // Set the return value once we determine that we return true.
2994 BasePos = BasePosLd;
2995 OffsetPos = OffsetPosLd;
2996 NewBase = PrevReg;
2997 Offset = StoreOffset;
2998 return true;
2999}
3000
3001/// Apply changes to the instruction if needed. The changes are need
3002/// to improve the scheduling and depend up on the final schedule.
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003003void SwingSchedulerDAG::applyInstrChange(MachineInstr *MI,
3004 SMSchedule &Schedule) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00003005 SUnit *SU = getSUnit(MI);
3006 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
3007 InstrChanges.find(SU);
3008 if (It != InstrChanges.end()) {
3009 std::pair<unsigned, int64_t> RegAndOffset = It->second;
3010 unsigned BasePos, OffsetPos;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003011 if (!TII->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos))
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003012 return;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003013 unsigned BaseReg = MI->getOperand(BasePos).getReg();
3014 MachineInstr *LoopDef = findDefInLoop(BaseReg);
3015 int DefStageNum = Schedule.stageScheduled(getSUnit(LoopDef));
3016 int DefCycleNum = Schedule.cycleScheduled(getSUnit(LoopDef));
3017 int BaseStageNum = Schedule.stageScheduled(SU);
3018 int BaseCycleNum = Schedule.cycleScheduled(SU);
3019 if (BaseStageNum < DefStageNum) {
3020 MachineInstr *NewMI = MF.CloneMachineInstr(MI);
3021 int OffsetDiff = DefStageNum - BaseStageNum;
3022 if (DefCycleNum < BaseCycleNum) {
3023 NewMI->getOperand(BasePos).setReg(RegAndOffset.first);
3024 if (OffsetDiff > 0)
3025 --OffsetDiff;
3026 }
3027 int64_t NewOffset =
3028 MI->getOperand(OffsetPos).getImm() + RegAndOffset.second * OffsetDiff;
3029 NewMI->getOperand(OffsetPos).setImm(NewOffset);
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003030 SU->setInstr(NewMI);
3031 MISUnitMap[NewMI] = SU;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003032 NewMIs.insert(NewMI);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003033 }
3034 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00003035}
3036
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003037/// Return true for an order or output dependence that is loop carried
3038/// potentially. A dependence is loop carried if the destination defines a valu
3039/// that may be used or defined by the source in a subsequent iteration.
3040bool SwingSchedulerDAG::isLoopCarriedDep(SUnit *Source, const SDep &Dep,
3041 bool isSucc) {
3042 if ((Dep.getKind() != SDep::Order && Dep.getKind() != SDep::Output) ||
3043 Dep.isArtificial())
Brendon Cahoon254f8892016-07-29 16:44:44 +00003044 return false;
3045
3046 if (!SwpPruneLoopCarried)
3047 return true;
3048
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003049 if (Dep.getKind() == SDep::Output)
3050 return true;
3051
Brendon Cahoon254f8892016-07-29 16:44:44 +00003052 MachineInstr *SI = Source->getInstr();
3053 MachineInstr *DI = Dep.getSUnit()->getInstr();
3054 if (!isSucc)
3055 std::swap(SI, DI);
3056 assert(SI != nullptr && DI != nullptr && "Expecting SUnit with an MI.");
3057
3058 // Assume ordered loads and stores may have a loop carried dependence.
3059 if (SI->hasUnmodeledSideEffects() || DI->hasUnmodeledSideEffects() ||
3060 SI->hasOrderedMemoryRef() || DI->hasOrderedMemoryRef())
3061 return true;
3062
3063 // Only chain dependences between a load and store can be loop carried.
3064 if (!DI->mayStore() || !SI->mayLoad())
3065 return false;
3066
3067 unsigned DeltaS, DeltaD;
3068 if (!computeDelta(*SI, DeltaS) || !computeDelta(*DI, DeltaD))
3069 return true;
3070
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003071 MachineOperand *BaseOpS, *BaseOpD;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003072 int64_t OffsetS, OffsetD;
3073 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003074 if (!TII->getMemOperandWithOffset(*SI, BaseOpS, OffsetS, TRI) ||
3075 !TII->getMemOperandWithOffset(*DI, BaseOpD, OffsetD, TRI))
Brendon Cahoon254f8892016-07-29 16:44:44 +00003076 return true;
3077
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003078 if (!BaseOpS->isIdenticalTo(*BaseOpD))
Brendon Cahoon254f8892016-07-29 16:44:44 +00003079 return true;
3080
Krzysztof Parzyszek8c07d0c2018-03-26 16:58:40 +00003081 // Check that the base register is incremented by a constant value for each
3082 // iteration.
Francis Visoiu Mistrihd7eebd62018-11-28 12:00:20 +00003083 MachineInstr *Def = MRI.getVRegDef(BaseOpS->getReg());
Krzysztof Parzyszek8c07d0c2018-03-26 16:58:40 +00003084 if (!Def || !Def->isPHI())
3085 return true;
3086 unsigned InitVal = 0;
3087 unsigned LoopVal = 0;
3088 getPhiRegs(*Def, BB, InitVal, LoopVal);
3089 MachineInstr *LoopDef = MRI.getVRegDef(LoopVal);
3090 int D = 0;
3091 if (!LoopDef || !TII->getIncrementValue(*LoopDef, D))
3092 return true;
3093
Brendon Cahoon254f8892016-07-29 16:44:44 +00003094 uint64_t AccessSizeS = (*SI->memoperands_begin())->getSize();
3095 uint64_t AccessSizeD = (*DI->memoperands_begin())->getSize();
3096
3097 // This is the main test, which checks the offset values and the loop
3098 // increment value to determine if the accesses may be loop carried.
3099 if (OffsetS >= OffsetD)
3100 return OffsetS + AccessSizeS > DeltaS;
Simon Pilgrimfbfb19b2017-03-16 19:52:00 +00003101 else
Brendon Cahoon254f8892016-07-29 16:44:44 +00003102 return OffsetD + AccessSizeD > DeltaD;
3103
3104 return true;
3105}
3106
Krzysztof Parzyszek88391242016-12-22 19:21:20 +00003107void SwingSchedulerDAG::postprocessDAG() {
3108 for (auto &M : Mutations)
3109 M->apply(this);
3110}
3111
Brendon Cahoon254f8892016-07-29 16:44:44 +00003112/// Try to schedule the node at the specified StartCycle and continue
3113/// until the node is schedule or the EndCycle is reached. This function
3114/// returns true if the node is scheduled. This routine may search either
3115/// forward or backward for a place to insert the instruction based upon
3116/// the relative values of StartCycle and EndCycle.
3117bool SMSchedule::insert(SUnit *SU, int StartCycle, int EndCycle, int II) {
3118 bool forward = true;
3119 if (StartCycle > EndCycle)
3120 forward = false;
3121
3122 // The terminating condition depends on the direction.
3123 int termCycle = forward ? EndCycle + 1 : EndCycle - 1;
3124 for (int curCycle = StartCycle; curCycle != termCycle;
3125 forward ? ++curCycle : --curCycle) {
3126
3127 // Add the already scheduled instructions at the specified cycle to the DFA.
3128 Resources->clearResources();
3129 for (int checkCycle = FirstCycle + ((curCycle - FirstCycle) % II);
3130 checkCycle <= LastCycle; checkCycle += II) {
3131 std::deque<SUnit *> &cycleInstrs = ScheduledInstrs[checkCycle];
3132
3133 for (std::deque<SUnit *>::iterator I = cycleInstrs.begin(),
3134 E = cycleInstrs.end();
3135 I != E; ++I) {
3136 if (ST.getInstrInfo()->isZeroCost((*I)->getInstr()->getOpcode()))
3137 continue;
3138 assert(Resources->canReserveResources(*(*I)->getInstr()) &&
3139 "These instructions have already been scheduled.");
3140 Resources->reserveResources(*(*I)->getInstr());
3141 }
3142 }
3143 if (ST.getInstrInfo()->isZeroCost(SU->getInstr()->getOpcode()) ||
3144 Resources->canReserveResources(*SU->getInstr())) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003145 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00003146 dbgs() << "\tinsert at cycle " << curCycle << " ";
3147 SU->getInstr()->dump();
3148 });
3149
3150 ScheduledInstrs[curCycle].push_back(SU);
3151 InstrToCycle.insert(std::make_pair(SU, curCycle));
3152 if (curCycle > LastCycle)
3153 LastCycle = curCycle;
3154 if (curCycle < FirstCycle)
3155 FirstCycle = curCycle;
3156 return true;
3157 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003158 LLVM_DEBUG({
Brendon Cahoon254f8892016-07-29 16:44:44 +00003159 dbgs() << "\tfailed to insert at cycle " << curCycle << " ";
3160 SU->getInstr()->dump();
3161 });
3162 }
3163 return false;
3164}
3165
3166// Return the cycle of the earliest scheduled instruction in the chain.
3167int SMSchedule::earliestCycleInChain(const SDep &Dep) {
3168 SmallPtrSet<SUnit *, 8> Visited;
3169 SmallVector<SDep, 8> Worklist;
3170 Worklist.push_back(Dep);
3171 int EarlyCycle = INT_MAX;
3172 while (!Worklist.empty()) {
3173 const SDep &Cur = Worklist.pop_back_val();
3174 SUnit *PrevSU = Cur.getSUnit();
3175 if (Visited.count(PrevSU))
3176 continue;
3177 std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(PrevSU);
3178 if (it == InstrToCycle.end())
3179 continue;
3180 EarlyCycle = std::min(EarlyCycle, it->second);
3181 for (const auto &PI : PrevSU->Preds)
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003182 if (PI.getKind() == SDep::Order || Dep.getKind() == SDep::Output)
Brendon Cahoon254f8892016-07-29 16:44:44 +00003183 Worklist.push_back(PI);
3184 Visited.insert(PrevSU);
3185 }
3186 return EarlyCycle;
3187}
3188
3189// Return the cycle of the latest scheduled instruction in the chain.
3190int SMSchedule::latestCycleInChain(const SDep &Dep) {
3191 SmallPtrSet<SUnit *, 8> Visited;
3192 SmallVector<SDep, 8> Worklist;
3193 Worklist.push_back(Dep);
3194 int LateCycle = INT_MIN;
3195 while (!Worklist.empty()) {
3196 const SDep &Cur = Worklist.pop_back_val();
3197 SUnit *SuccSU = Cur.getSUnit();
3198 if (Visited.count(SuccSU))
3199 continue;
3200 std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SuccSU);
3201 if (it == InstrToCycle.end())
3202 continue;
3203 LateCycle = std::max(LateCycle, it->second);
3204 for (const auto &SI : SuccSU->Succs)
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003205 if (SI.getKind() == SDep::Order || Dep.getKind() == SDep::Output)
Brendon Cahoon254f8892016-07-29 16:44:44 +00003206 Worklist.push_back(SI);
3207 Visited.insert(SuccSU);
3208 }
3209 return LateCycle;
3210}
3211
3212/// If an instruction has a use that spans multiple iterations, then
3213/// return true. These instructions are characterized by having a back-ege
3214/// to a Phi, which contains a reference to another Phi.
3215static SUnit *multipleIterations(SUnit *SU, SwingSchedulerDAG *DAG) {
3216 for (auto &P : SU->Preds)
3217 if (DAG->isBackedge(SU, P) && P.getSUnit()->getInstr()->isPHI())
3218 for (auto &S : P.getSUnit()->Succs)
Krzysztof Parzyszekb9b75b82018-03-26 15:53:23 +00003219 if (S.getKind() == SDep::Data && S.getSUnit()->getInstr()->isPHI())
Brendon Cahoon254f8892016-07-29 16:44:44 +00003220 return P.getSUnit();
3221 return nullptr;
3222}
3223
3224/// Compute the scheduling start slot for the instruction. The start slot
3225/// depends on any predecessor or successor nodes scheduled already.
3226void SMSchedule::computeStart(SUnit *SU, int *MaxEarlyStart, int *MinLateStart,
3227 int *MinEnd, int *MaxStart, int II,
3228 SwingSchedulerDAG *DAG) {
3229 // Iterate over each instruction that has been scheduled already. The start
Hiroshi Inouec73b6d62018-06-20 05:29:26 +00003230 // slot computation depends on whether the previously scheduled instruction
Brendon Cahoon254f8892016-07-29 16:44:44 +00003231 // is a predecessor or successor of the specified instruction.
3232 for (int cycle = getFirstCycle(); cycle <= LastCycle; ++cycle) {
3233
3234 // Iterate over each instruction in the current cycle.
3235 for (SUnit *I : getInstructions(cycle)) {
3236 // Because we're processing a DAG for the dependences, we recognize
3237 // the back-edge in recurrences by anti dependences.
3238 for (unsigned i = 0, e = (unsigned)SU->Preds.size(); i != e; ++i) {
3239 const SDep &Dep = SU->Preds[i];
3240 if (Dep.getSUnit() == I) {
3241 if (!DAG->isBackedge(SU, Dep)) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00003242 int EarlyStart = cycle + Dep.getLatency() -
Brendon Cahoon254f8892016-07-29 16:44:44 +00003243 DAG->getDistance(Dep.getSUnit(), SU, Dep) * II;
3244 *MaxEarlyStart = std::max(*MaxEarlyStart, EarlyStart);
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003245 if (DAG->isLoopCarriedDep(SU, Dep, false)) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00003246 int End = earliestCycleInChain(Dep) + (II - 1);
3247 *MinEnd = std::min(*MinEnd, End);
3248 }
3249 } else {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00003250 int LateStart = cycle - Dep.getLatency() +
Brendon Cahoon254f8892016-07-29 16:44:44 +00003251 DAG->getDistance(SU, Dep.getSUnit(), Dep) * II;
3252 *MinLateStart = std::min(*MinLateStart, LateStart);
3253 }
3254 }
3255 // For instruction that requires multiple iterations, make sure that
3256 // the dependent instruction is not scheduled past the definition.
3257 SUnit *BE = multipleIterations(I, DAG);
3258 if (BE && Dep.getSUnit() == BE && !SU->getInstr()->isPHI() &&
3259 !SU->isPred(I))
3260 *MinLateStart = std::min(*MinLateStart, cycle);
3261 }
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00003262 for (unsigned i = 0, e = (unsigned)SU->Succs.size(); i != e; ++i) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00003263 if (SU->Succs[i].getSUnit() == I) {
3264 const SDep &Dep = SU->Succs[i];
3265 if (!DAG->isBackedge(SU, Dep)) {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00003266 int LateStart = cycle - Dep.getLatency() +
Brendon Cahoon254f8892016-07-29 16:44:44 +00003267 DAG->getDistance(SU, Dep.getSUnit(), Dep) * II;
3268 *MinLateStart = std::min(*MinLateStart, LateStart);
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003269 if (DAG->isLoopCarriedDep(SU, Dep)) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00003270 int Start = latestCycleInChain(Dep) + 1 - II;
3271 *MaxStart = std::max(*MaxStart, Start);
3272 }
3273 } else {
Krzysztof Parzyszekc715a5d2018-03-21 16:39:11 +00003274 int EarlyStart = cycle + Dep.getLatency() -
Brendon Cahoon254f8892016-07-29 16:44:44 +00003275 DAG->getDistance(Dep.getSUnit(), SU, Dep) * II;
3276 *MaxEarlyStart = std::max(*MaxEarlyStart, EarlyStart);
3277 }
3278 }
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +00003279 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00003280 }
3281 }
3282}
3283
3284/// Order the instructions within a cycle so that the definitions occur
3285/// before the uses. Returns true if the instruction is added to the start
3286/// of the list, or false if added to the end.
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003287void SMSchedule::orderDependence(SwingSchedulerDAG *SSD, SUnit *SU,
Brendon Cahoon254f8892016-07-29 16:44:44 +00003288 std::deque<SUnit *> &Insts) {
3289 MachineInstr *MI = SU->getInstr();
3290 bool OrderBeforeUse = false;
3291 bool OrderAfterDef = false;
3292 bool OrderBeforeDef = false;
3293 unsigned MoveDef = 0;
3294 unsigned MoveUse = 0;
3295 int StageInst1 = stageScheduled(SU);
3296
3297 unsigned Pos = 0;
3298 for (std::deque<SUnit *>::iterator I = Insts.begin(), E = Insts.end(); I != E;
3299 ++I, ++Pos) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00003300 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
3301 MachineOperand &MO = MI->getOperand(i);
3302 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
3303 continue;
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003304
Brendon Cahoon254f8892016-07-29 16:44:44 +00003305 unsigned Reg = MO.getReg();
3306 unsigned BasePos, OffsetPos;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +00003307 if (ST.getInstrInfo()->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos))
Brendon Cahoon254f8892016-07-29 16:44:44 +00003308 if (MI->getOperand(BasePos).getReg() == Reg)
3309 if (unsigned NewReg = SSD->getInstrBaseReg(SU))
3310 Reg = NewReg;
3311 bool Reads, Writes;
3312 std::tie(Reads, Writes) =
3313 (*I)->getInstr()->readsWritesVirtualRegister(Reg);
3314 if (MO.isDef() && Reads && stageScheduled(*I) <= StageInst1) {
3315 OrderBeforeUse = true;
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003316 if (MoveUse == 0)
3317 MoveUse = Pos;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003318 } else if (MO.isDef() && Reads && stageScheduled(*I) > StageInst1) {
3319 // Add the instruction after the scheduled instruction.
3320 OrderAfterDef = true;
3321 MoveDef = Pos;
3322 } else if (MO.isUse() && Writes && stageScheduled(*I) == StageInst1) {
3323 if (cycleScheduled(*I) == cycleScheduled(SU) && !(*I)->isSucc(SU)) {
3324 OrderBeforeUse = true;
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003325 if (MoveUse == 0)
3326 MoveUse = Pos;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003327 } else {
3328 OrderAfterDef = true;
3329 MoveDef = Pos;
3330 }
3331 } else if (MO.isUse() && Writes && stageScheduled(*I) > StageInst1) {
3332 OrderBeforeUse = true;
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003333 if (MoveUse == 0)
3334 MoveUse = Pos;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003335 if (MoveUse != 0) {
3336 OrderAfterDef = true;
3337 MoveDef = Pos - 1;
3338 }
3339 } else if (MO.isUse() && Writes && stageScheduled(*I) < StageInst1) {
3340 // Add the instruction before the scheduled instruction.
3341 OrderBeforeUse = true;
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003342 if (MoveUse == 0)
3343 MoveUse = Pos;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003344 } else if (MO.isUse() && stageScheduled(*I) == StageInst1 &&
3345 isLoopCarriedDefOfUse(SSD, (*I)->getInstr(), MO)) {
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003346 if (MoveUse == 0) {
3347 OrderBeforeDef = true;
3348 MoveUse = Pos;
3349 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00003350 }
3351 }
3352 // Check for order dependences between instructions. Make sure the source
3353 // is ordered before the destination.
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003354 for (auto &S : SU->Succs) {
3355 if (S.getSUnit() != *I)
3356 continue;
3357 if (S.getKind() == SDep::Order && stageScheduled(*I) == StageInst1) {
3358 OrderBeforeUse = true;
3359 if (Pos < MoveUse)
Brendon Cahoon254f8892016-07-29 16:44:44 +00003360 MoveUse = Pos;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003361 }
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003362 }
3363 for (auto &P : SU->Preds) {
3364 if (P.getSUnit() != *I)
3365 continue;
3366 if (P.getKind() == SDep::Order && stageScheduled(*I) == StageInst1) {
3367 OrderAfterDef = true;
3368 MoveDef = Pos;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003369 }
Krzysztof Parzyszek8e1363d2018-03-26 16:05:55 +00003370 }
Brendon Cahoon254f8892016-07-29 16:44:44 +00003371 }
3372
3373 // A circular dependence.
3374 if (OrderAfterDef && OrderBeforeUse && MoveUse == MoveDef)
3375 OrderBeforeUse = false;
3376
3377 // OrderAfterDef takes precedences over OrderBeforeDef. The latter is due
3378 // to a loop-carried dependence.
3379 if (OrderBeforeDef)
3380 OrderBeforeUse = !OrderAfterDef || (MoveUse > MoveDef);
3381
3382 // The uncommon case when the instruction order needs to be updated because
3383 // there is both a use and def.
3384 if (OrderBeforeUse && OrderAfterDef) {
3385 SUnit *UseSU = Insts.at(MoveUse);
3386 SUnit *DefSU = Insts.at(MoveDef);
3387 if (MoveUse > MoveDef) {
3388 Insts.erase(Insts.begin() + MoveUse);
3389 Insts.erase(Insts.begin() + MoveDef);
3390 } else {
3391 Insts.erase(Insts.begin() + MoveDef);
3392 Insts.erase(Insts.begin() + MoveUse);
3393 }
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003394 orderDependence(SSD, UseSU, Insts);
3395 orderDependence(SSD, SU, Insts);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003396 orderDependence(SSD, DefSU, Insts);
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003397 return;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003398 }
3399 // Put the new instruction first if there is a use in the list. Otherwise,
3400 // put it at the end of the list.
3401 if (OrderBeforeUse)
3402 Insts.push_front(SU);
3403 else
3404 Insts.push_back(SU);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003405}
3406
3407/// Return true if the scheduled Phi has a loop carried operand.
3408bool SMSchedule::isLoopCarried(SwingSchedulerDAG *SSD, MachineInstr &Phi) {
3409 if (!Phi.isPHI())
3410 return false;
Hiroshi Inouec73b6d62018-06-20 05:29:26 +00003411 assert(Phi.isPHI() && "Expecting a Phi.");
Brendon Cahoon254f8892016-07-29 16:44:44 +00003412 SUnit *DefSU = SSD->getSUnit(&Phi);
3413 unsigned DefCycle = cycleScheduled(DefSU);
3414 int DefStage = stageScheduled(DefSU);
3415
3416 unsigned InitVal = 0;
3417 unsigned LoopVal = 0;
3418 getPhiRegs(Phi, Phi.getParent(), InitVal, LoopVal);
3419 SUnit *UseSU = SSD->getSUnit(MRI.getVRegDef(LoopVal));
3420 if (!UseSU)
3421 return true;
3422 if (UseSU->getInstr()->isPHI())
3423 return true;
3424 unsigned LoopCycle = cycleScheduled(UseSU);
3425 int LoopStage = stageScheduled(UseSU);
Simon Pilgrim3d8482a2016-11-14 10:40:23 +00003426 return (LoopCycle > DefCycle) || (LoopStage <= DefStage);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003427}
3428
3429/// Return true if the instruction is a definition that is loop carried
3430/// and defines the use on the next iteration.
3431/// v1 = phi(v2, v3)
3432/// (Def) v3 = op v1
3433/// (MO) = v1
3434/// If MO appears before Def, then then v1 and v3 may get assigned to the same
3435/// register.
3436bool SMSchedule::isLoopCarriedDefOfUse(SwingSchedulerDAG *SSD,
3437 MachineInstr *Def, MachineOperand &MO) {
3438 if (!MO.isReg())
3439 return false;
3440 if (Def->isPHI())
3441 return false;
3442 MachineInstr *Phi = MRI.getVRegDef(MO.getReg());
3443 if (!Phi || !Phi->isPHI() || Phi->getParent() != Def->getParent())
3444 return false;
3445 if (!isLoopCarried(SSD, *Phi))
3446 return false;
3447 unsigned LoopReg = getLoopPhiReg(*Phi, Phi->getParent());
3448 for (unsigned i = 0, e = Def->getNumOperands(); i != e; ++i) {
3449 MachineOperand &DMO = Def->getOperand(i);
3450 if (!DMO.isReg() || !DMO.isDef())
3451 continue;
3452 if (DMO.getReg() == LoopReg)
3453 return true;
3454 }
3455 return false;
3456}
3457
3458// Check if the generated schedule is valid. This function checks if
3459// an instruction that uses a physical register is scheduled in a
3460// different stage than the definition. The pipeliner does not handle
3461// physical register values that may cross a basic block boundary.
3462bool SMSchedule::isValidSchedule(SwingSchedulerDAG *SSD) {
Brendon Cahoon254f8892016-07-29 16:44:44 +00003463 for (int i = 0, e = SSD->SUnits.size(); i < e; ++i) {
3464 SUnit &SU = SSD->SUnits[i];
3465 if (!SU.hasPhysRegDefs)
3466 continue;
3467 int StageDef = stageScheduled(&SU);
3468 assert(StageDef != -1 && "Instruction should have been scheduled.");
3469 for (auto &SI : SU.Succs)
3470 if (SI.isAssignedRegDep())
Simon Pilgrimb39236b2016-07-29 18:57:32 +00003471 if (ST.getRegisterInfo()->isPhysicalRegister(SI.getReg()))
Brendon Cahoon254f8892016-07-29 16:44:44 +00003472 if (stageScheduled(SI.getSUnit()) != StageDef)
3473 return false;
3474 }
3475 return true;
3476}
3477
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003478/// A property of the node order in swing-modulo-scheduling is
3479/// that for nodes outside circuits the following holds:
3480/// none of them is scheduled after both a successor and a
3481/// predecessor.
3482/// The method below checks whether the property is met.
3483/// If not, debug information is printed and statistics information updated.
3484/// Note that we do not use an assert statement.
3485/// The reason is that although an invalid node oder may prevent
3486/// the pipeliner from finding a pipelined schedule for arbitrary II,
3487/// it does not lead to the generation of incorrect code.
3488void SwingSchedulerDAG::checkValidNodeOrder(const NodeSetType &Circuits) const {
3489
3490 // a sorted vector that maps each SUnit to its index in the NodeOrder
3491 typedef std::pair<SUnit *, unsigned> UnitIndex;
3492 std::vector<UnitIndex> Indices(NodeOrder.size(), std::make_pair(nullptr, 0));
3493
3494 for (unsigned i = 0, s = NodeOrder.size(); i < s; ++i)
3495 Indices.push_back(std::make_pair(NodeOrder[i], i));
3496
3497 auto CompareKey = [](UnitIndex i1, UnitIndex i2) {
3498 return std::get<0>(i1) < std::get<0>(i2);
3499 };
3500
3501 // sort, so that we can perform a binary search
Fangrui Song0cac7262018-09-27 02:13:45 +00003502 llvm::sort(Indices, CompareKey);
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003503
3504 bool Valid = true;
David L Kreitzerfebf70a2018-03-16 21:21:23 +00003505 (void)Valid;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003506 // for each SUnit in the NodeOrder, check whether
3507 // it appears after both a successor and a predecessor
3508 // of the SUnit. If this is the case, and the SUnit
3509 // is not part of circuit, then the NodeOrder is not
3510 // valid.
3511 for (unsigned i = 0, s = NodeOrder.size(); i < s; ++i) {
3512 SUnit *SU = NodeOrder[i];
3513 unsigned Index = i;
3514
3515 bool PredBefore = false;
3516 bool SuccBefore = false;
3517
3518 SUnit *Succ;
3519 SUnit *Pred;
David L Kreitzerfebf70a2018-03-16 21:21:23 +00003520 (void)Succ;
3521 (void)Pred;
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003522
3523 for (SDep &PredEdge : SU->Preds) {
3524 SUnit *PredSU = PredEdge.getSUnit();
3525 unsigned PredIndex =
3526 std::get<1>(*std::lower_bound(Indices.begin(), Indices.end(),
3527 std::make_pair(PredSU, 0), CompareKey));
3528 if (!PredSU->getInstr()->isPHI() && PredIndex < Index) {
3529 PredBefore = true;
3530 Pred = PredSU;
3531 break;
3532 }
3533 }
3534
3535 for (SDep &SuccEdge : SU->Succs) {
3536 SUnit *SuccSU = SuccEdge.getSUnit();
3537 unsigned SuccIndex =
3538 std::get<1>(*std::lower_bound(Indices.begin(), Indices.end(),
3539 std::make_pair(SuccSU, 0), CompareKey));
3540 if (!SuccSU->getInstr()->isPHI() && SuccIndex < Index) {
3541 SuccBefore = true;
3542 Succ = SuccSU;
3543 break;
3544 }
3545 }
3546
3547 if (PredBefore && SuccBefore && !SU->getInstr()->isPHI()) {
3548 // instructions in circuits are allowed to be scheduled
3549 // after both a successor and predecessor.
3550 bool InCircuit = std::any_of(
3551 Circuits.begin(), Circuits.end(),
3552 [SU](const NodeSet &Circuit) { return Circuit.count(SU); });
3553 if (InCircuit)
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003554 LLVM_DEBUG(dbgs() << "In a circuit, predecessor ";);
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003555 else {
3556 Valid = false;
3557 NumNodeOrderIssues++;
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003558 LLVM_DEBUG(dbgs() << "Predecessor ";);
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003559 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003560 LLVM_DEBUG(dbgs() << Pred->NodeNum << " and successor " << Succ->NodeNum
3561 << " are scheduled before node " << SU->NodeNum
3562 << "\n";);
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003563 }
3564 }
3565
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003566 LLVM_DEBUG({
Roorda, Jan-Willem4b8bcf02018-03-07 18:53:36 +00003567 if (!Valid)
3568 dbgs() << "Invalid node order found!\n";
3569 });
3570}
3571
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003572/// Attempt to fix the degenerate cases when the instruction serialization
3573/// causes the register lifetimes to overlap. For example,
3574/// p' = store_pi(p, b)
3575/// = load p, offset
3576/// In this case p and p' overlap, which means that two registers are needed.
3577/// Instead, this function changes the load to use p' and updates the offset.
3578void SwingSchedulerDAG::fixupRegisterOverlaps(std::deque<SUnit *> &Instrs) {
3579 unsigned OverlapReg = 0;
3580 unsigned NewBaseReg = 0;
3581 for (SUnit *SU : Instrs) {
3582 MachineInstr *MI = SU->getInstr();
3583 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
3584 const MachineOperand &MO = MI->getOperand(i);
3585 // Look for an instruction that uses p. The instruction occurs in the
3586 // same cycle but occurs later in the serialized order.
3587 if (MO.isReg() && MO.isUse() && MO.getReg() == OverlapReg) {
3588 // Check that the instruction appears in the InstrChanges structure,
3589 // which contains instructions that can have the offset updated.
3590 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
3591 InstrChanges.find(SU);
3592 if (It != InstrChanges.end()) {
3593 unsigned BasePos, OffsetPos;
3594 // Update the base register and adjust the offset.
3595 if (TII->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos)) {
Krzysztof Parzyszek12bdcab2017-10-11 15:59:51 +00003596 MachineInstr *NewMI = MF.CloneMachineInstr(MI);
3597 NewMI->getOperand(BasePos).setReg(NewBaseReg);
3598 int64_t NewOffset =
3599 MI->getOperand(OffsetPos).getImm() - It->second.second;
3600 NewMI->getOperand(OffsetPos).setImm(NewOffset);
3601 SU->setInstr(NewMI);
3602 MISUnitMap[NewMI] = SU;
3603 NewMIs.insert(NewMI);
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003604 }
3605 }
3606 OverlapReg = 0;
3607 NewBaseReg = 0;
3608 break;
3609 }
3610 // Look for an instruction of the form p' = op(p), which uses and defines
3611 // two virtual registers that get allocated to the same physical register.
3612 unsigned TiedUseIdx = 0;
3613 if (MI->isRegTiedToUseOperand(i, &TiedUseIdx)) {
3614 // OverlapReg is p in the example above.
3615 OverlapReg = MI->getOperand(TiedUseIdx).getReg();
3616 // NewBaseReg is p' in the example above.
3617 NewBaseReg = MI->getOperand(i).getReg();
3618 break;
3619 }
3620 }
3621 }
3622}
3623
Brendon Cahoon254f8892016-07-29 16:44:44 +00003624/// After the schedule has been formed, call this function to combine
3625/// the instructions from the different stages/cycles. That is, this
3626/// function creates a schedule that represents a single iteration.
3627void SMSchedule::finalizeSchedule(SwingSchedulerDAG *SSD) {
3628 // Move all instructions to the first stage from later stages.
3629 for (int cycle = getFirstCycle(); cycle <= getFinalCycle(); ++cycle) {
3630 for (int stage = 1, lastStage = getMaxStageCount(); stage <= lastStage;
3631 ++stage) {
3632 std::deque<SUnit *> &cycleInstrs =
3633 ScheduledInstrs[cycle + (stage * InitiationInterval)];
3634 for (std::deque<SUnit *>::reverse_iterator I = cycleInstrs.rbegin(),
3635 E = cycleInstrs.rend();
3636 I != E; ++I)
3637 ScheduledInstrs[cycle].push_front(*I);
3638 }
3639 }
3640 // Iterate over the definitions in each instruction, and compute the
3641 // stage difference for each use. Keep the maximum value.
3642 for (auto &I : InstrToCycle) {
3643 int DefStage = stageScheduled(I.first);
3644 MachineInstr *MI = I.first->getInstr();
3645 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
3646 MachineOperand &Op = MI->getOperand(i);
3647 if (!Op.isReg() || !Op.isDef())
3648 continue;
3649
3650 unsigned Reg = Op.getReg();
3651 unsigned MaxDiff = 0;
3652 bool PhiIsSwapped = false;
3653 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(Reg),
3654 EI = MRI.use_end();
3655 UI != EI; ++UI) {
3656 MachineOperand &UseOp = *UI;
3657 MachineInstr *UseMI = UseOp.getParent();
3658 SUnit *SUnitUse = SSD->getSUnit(UseMI);
3659 int UseStage = stageScheduled(SUnitUse);
3660 unsigned Diff = 0;
3661 if (UseStage != -1 && UseStage >= DefStage)
3662 Diff = UseStage - DefStage;
3663 if (MI->isPHI()) {
3664 if (isLoopCarried(SSD, *MI))
3665 ++Diff;
3666 else
3667 PhiIsSwapped = true;
3668 }
3669 MaxDiff = std::max(Diff, MaxDiff);
3670 }
3671 RegToStageDiff[Reg] = std::make_pair(MaxDiff, PhiIsSwapped);
3672 }
3673 }
3674
3675 // Erase all the elements in the later stages. Only one iteration should
3676 // remain in the scheduled list, and it contains all the instructions.
3677 for (int cycle = getFinalCycle() + 1; cycle <= LastCycle; ++cycle)
3678 ScheduledInstrs.erase(cycle);
3679
3680 // Change the registers in instruction as specified in the InstrChanges
3681 // map. We need to use the new registers to create the correct order.
3682 for (int i = 0, e = SSD->SUnits.size(); i != e; ++i) {
3683 SUnit *SU = &SSD->SUnits[i];
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003684 SSD->applyInstrChange(SU->getInstr(), *this);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003685 }
3686
3687 // Reorder the instructions in each cycle to fix and improve the
3688 // generated code.
3689 for (int Cycle = getFirstCycle(), E = getFinalCycle(); Cycle <= E; ++Cycle) {
3690 std::deque<SUnit *> &cycleInstrs = ScheduledInstrs[Cycle];
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003691 std::deque<SUnit *> newOrderPhi;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003692 for (unsigned i = 0, e = cycleInstrs.size(); i < e; ++i) {
3693 SUnit *SU = cycleInstrs[i];
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003694 if (SU->getInstr()->isPHI())
3695 newOrderPhi.push_back(SU);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003696 }
3697 std::deque<SUnit *> newOrderI;
Brendon Cahoon254f8892016-07-29 16:44:44 +00003698 for (unsigned i = 0, e = cycleInstrs.size(); i < e; ++i) {
3699 SUnit *SU = cycleInstrs[i];
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003700 if (!SU->getInstr()->isPHI())
Brendon Cahoon254f8892016-07-29 16:44:44 +00003701 orderDependence(SSD, SU, newOrderI);
3702 }
3703 // Replace the old order with the new order.
Krzysztof Parzyszekf13bbf12018-03-26 16:23:29 +00003704 cycleInstrs.swap(newOrderPhi);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003705 cycleInstrs.insert(cycleInstrs.end(), newOrderI.begin(), newOrderI.end());
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00003706 SSD->fixupRegisterOverlaps(cycleInstrs);
Brendon Cahoon254f8892016-07-29 16:44:44 +00003707 }
3708
Nicola Zaghend34e60c2018-05-14 12:53:11 +00003709 LLVM_DEBUG(dump(););
Brendon Cahoon254f8892016-07-29 16:44:44 +00003710}
3711
Adrian Prantlfa2e3582019-01-14 17:24:11 +00003712void NodeSet::print(raw_ostream &os) const {
3713 os << "Num nodes " << size() << " rec " << RecMII << " mov " << MaxMOV
3714 << " depth " << MaxDepth << " col " << Colocate << "\n";
3715 for (const auto &I : Nodes)
3716 os << " SU(" << I->NodeNum << ") " << *(I->getInstr());
3717 os << "\n";
3718}
3719
Aaron Ballman615eb472017-10-15 14:32:27 +00003720#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Brendon Cahoon254f8892016-07-29 16:44:44 +00003721/// Print the schedule information to the given output.
3722void SMSchedule::print(raw_ostream &os) const {
3723 // Iterate over each cycle.
3724 for (int cycle = getFirstCycle(); cycle <= getFinalCycle(); ++cycle) {
3725 // Iterate over each instruction in the cycle.
3726 const_sched_iterator cycleInstrs = ScheduledInstrs.find(cycle);
3727 for (SUnit *CI : cycleInstrs->second) {
3728 os << "cycle " << cycle << " (" << stageScheduled(CI) << ") ";
3729 os << "(" << CI->NodeNum << ") ";
3730 CI->getInstr()->print(os);
3731 os << "\n";
3732 }
3733 }
3734}
3735
3736/// Utility function used for debugging to print the schedule.
Matthias Braun8c209aa2017-01-28 02:02:38 +00003737LLVM_DUMP_METHOD void SMSchedule::dump() const { print(dbgs()); }
Adrian Prantlfa2e3582019-01-14 17:24:11 +00003738LLVM_DUMP_METHOD void NodeSet::dump() const { print(dbgs()); }
3739
Matthias Braun8c209aa2017-01-28 02:02:38 +00003740#endif
Adrian Prantlfa2e3582019-01-14 17:24:11 +00003741
3742
3743