1 //===- MachinePipeliner.cpp - Machine Software Pipeliner Pass -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // An implementation of the Swing Modulo Scheduling (SMS) software pipeliner.
12 // Software pipelining (SWP) is an instruction scheduling technique for loops
13 // that overlap loop iterations and explioits ILP via a compiler transformation.
15 // Swing Modulo Scheduling is an implementation of software pipelining
16 // that generates schedules that are near optimal in terms of initiation
17 // interval, register requirements, and stage count. See the papers:
19 // "Swing Modulo Scheduling: A Lifetime-Sensitive Approach", by J. Llosa,
20 // A. Gonzalez, E. Ayguade, and M. Valero. In PACT '96 Processings of the 1996
21 // Conference on Parallel Architectures and Compilation Techiniques.
23 // "Lifetime-Sensitive Modulo Scheduling in a Production Environment", by J.
24 // Llosa, E. Ayguade, A. Gonzalez, M. Valero, and J. Eckhardt. In IEEE
25 // Transactions on Computers, Vol. 50, No. 3, 2001.
27 // "An Implementation of Swing Modulo Scheduling With Extensions for
28 // Superblocks", by T. Lattner, Master's Thesis, University of Illinois at
29 // Urbana-Chambpain, 2005.
32 // The SMS algorithm consists of three main steps after computing the minimal
33 // initiation interval (MII).
34 // 1) Analyze the dependence graph and compute information about each
35 // instruction in the graph.
36 // 2) Order the nodes (instructions) by priority based upon the heuristics
37 // described in the algorithm.
38 // 3) Attempt to schedule the nodes in the specified order using the MII.
40 // This SMS implementation is a target-independent back-end pass. When enabled,
41 // the pass runs just prior to the register allocation pass, while the machine
42 // IR is in SSA form. If software pipelining is successful, then the original
43 // loop is replaced by the optimized loop. The optimized loop contains one or
44 // more prolog blocks, the pipelined kernel, and one or more epilog blocks. If
45 // the instructions cannot be scheduled in a given MII, we increase the MII by
48 // The SMS implementation is an extension of the ScheduleDAGInstrs class. We
49 // represent loop carried dependences in the DAG as order edges to the Phi
50 // nodes. We also perform several passes over the DAG to eliminate unnecessary
51 // edges that inhibit the ability to pipeline. The implementation uses the
52 // DFAPacketizer class to compute the minimum initiation interval and the check
53 // where an instruction may be inserted in the pipelined schedule.
55 // In order for the SMS pass to work, several target specific hooks need to be
56 // implemented to get information about the loop structure and to rewrite
59 //===----------------------------------------------------------------------===//
61 #include "llvm/ADT/ArrayRef.h"
62 #include "llvm/ADT/BitVector.h"
63 #include "llvm/ADT/DenseMap.h"
64 #include "llvm/ADT/MapVector.h"
65 #include "llvm/ADT/PriorityQueue.h"
66 #include "llvm/ADT/SetVector.h"
67 #include "llvm/ADT/SmallPtrSet.h"
68 #include "llvm/ADT/SmallSet.h"
69 #include "llvm/ADT/SmallVector.h"
70 #include "llvm/ADT/Statistic.h"
71 #include "llvm/ADT/iterator_range.h"
72 #include "llvm/Analysis/AliasAnalysis.h"
73 #include "llvm/Analysis/MemoryLocation.h"
74 #include "llvm/Analysis/ValueTracking.h"
75 #include "llvm/CodeGen/DFAPacketizer.h"
76 #include "llvm/CodeGen/LiveIntervals.h"
77 #include "llvm/CodeGen/MachineBasicBlock.h"
78 #include "llvm/CodeGen/MachineDominators.h"
79 #include "llvm/CodeGen/MachineFunction.h"
80 #include "llvm/CodeGen/MachineFunctionPass.h"
81 #include "llvm/CodeGen/MachineInstr.h"
82 #include "llvm/CodeGen/MachineInstrBuilder.h"
83 #include "llvm/CodeGen/MachineLoopInfo.h"
84 #include "llvm/CodeGen/MachineMemOperand.h"
85 #include "llvm/CodeGen/MachineOperand.h"
86 #include "llvm/CodeGen/MachineRegisterInfo.h"
87 #include "llvm/CodeGen/RegisterClassInfo.h"
88 #include "llvm/CodeGen/RegisterPressure.h"
89 #include "llvm/CodeGen/ScheduleDAG.h"
90 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
91 #include "llvm/CodeGen/ScheduleDAGMutation.h"
92 #include "llvm/CodeGen/TargetInstrInfo.h"
93 #include "llvm/CodeGen/TargetOpcodes.h"
94 #include "llvm/CodeGen/TargetRegisterInfo.h"
95 #include "llvm/CodeGen/TargetSubtargetInfo.h"
96 #include "llvm/IR/Attributes.h"
97 #include "llvm/IR/DebugLoc.h"
98 #include "llvm/IR/Function.h"
99 #include "llvm/MC/LaneBitmask.h"
100 #include "llvm/MC/MCInstrDesc.h"
101 #include "llvm/MC/MCInstrItineraries.h"
102 #include "llvm/MC/MCRegisterInfo.h"
103 #include "llvm/Pass.h"
104 #include "llvm/Support/CommandLine.h"
105 #include "llvm/Support/Compiler.h"
106 #include "llvm/Support/Debug.h"
107 #include "llvm/Support/MathExtras.h"
108 #include "llvm/Support/raw_ostream.h"
114 #include <functional>
122 using namespace llvm;
124 #define DEBUG_TYPE "pipeliner"
126 STATISTIC(NumTrytoPipeline, "Number of loops that we attempt to pipeline");
127 STATISTIC(NumPipelined, "Number of loops software pipelined");
129 /// A command line option to turn software pipelining on or off.
130 static cl::opt<bool> EnableSWP("enable-pipeliner", cl::Hidden, cl::init(true),
132 cl::desc("Enable Software Pipelining"));
134 /// A command line option to enable SWP at -Os.
135 static cl::opt<bool> EnableSWPOptSize("enable-pipeliner-opt-size",
136 cl::desc("Enable SWP at Os."), cl::Hidden,
139 /// A command line argument to limit minimum initial interval for pipelining.
140 static cl::opt<int> SwpMaxMii("pipeliner-max-mii",
141 cl::desc("Size limit for the the MII."),
142 cl::Hidden, cl::init(27));
144 /// A command line argument to limit the number of stages in the pipeline.
146 SwpMaxStages("pipeliner-max-stages",
147 cl::desc("Maximum stages allowed in the generated scheduled."),
148 cl::Hidden, cl::init(3));
150 /// A command line option to disable the pruning of chain dependences due to
151 /// an unrelated Phi.
153 SwpPruneDeps("pipeliner-prune-deps",
154 cl::desc("Prune dependences between unrelated Phi nodes."),
155 cl::Hidden, cl::init(true));
157 /// A command line option to disable the pruning of loop carried order
160 SwpPruneLoopCarried("pipeliner-prune-loop-carried",
161 cl::desc("Prune loop carried order dependences."),
162 cl::Hidden, cl::init(true));
165 static cl::opt<int> SwpLoopLimit("pipeliner-max", cl::Hidden, cl::init(-1));
168 static cl::opt<bool> SwpIgnoreRecMII("pipeliner-ignore-recmii",
169 cl::ReallyHidden, cl::init(false),
170 cl::ZeroOrMore, cl::desc("Ignore RecMII"));
177 /// The main class in the implementation of the target independent
178 /// software pipeliner pass.
179 class MachinePipeliner : public MachineFunctionPass {
181 MachineFunction *MF = nullptr;
182 const MachineLoopInfo *MLI = nullptr;
183 const MachineDominatorTree *MDT = nullptr;
184 const InstrItineraryData *InstrItins;
185 const TargetInstrInfo *TII = nullptr;
186 RegisterClassInfo RegClassInfo;
192 /// Cache the target analysis information about the loop.
194 MachineBasicBlock *TBB = nullptr;
195 MachineBasicBlock *FBB = nullptr;
196 SmallVector<MachineOperand, 4> BrCond;
197 MachineInstr *LoopInductionVar = nullptr;
198 MachineInstr *LoopCompare = nullptr;
204 MachinePipeliner() : MachineFunctionPass(ID) {
205 initializeMachinePipelinerPass(*PassRegistry::getPassRegistry());
208 bool runOnMachineFunction(MachineFunction &MF) override;
210 void getAnalysisUsage(AnalysisUsage &AU) const override {
211 AU.addRequired<AAResultsWrapperPass>();
212 AU.addPreserved<AAResultsWrapperPass>();
213 AU.addRequired<MachineLoopInfo>();
214 AU.addRequired<MachineDominatorTree>();
215 AU.addRequired<LiveIntervals>();
216 MachineFunctionPass::getAnalysisUsage(AU);
220 bool canPipelineLoop(MachineLoop &L);
221 bool scheduleLoop(MachineLoop &L);
222 bool swingModuloScheduler(MachineLoop &L);
225 /// This class builds the dependence graph for the instructions in a loop,
226 /// and attempts to schedule the instructions using the SMS algorithm.
227 class SwingSchedulerDAG : public ScheduleDAGInstrs {
228 MachinePipeliner &Pass;
229 /// The minimum initiation interval between iterations for this schedule.
231 /// Set to true if a valid pipelined schedule is found for the loop.
232 bool Scheduled = false;
235 const RegisterClassInfo &RegClassInfo;
237 /// A toplogical ordering of the SUnits, which is needed for changing
238 /// dependences and iterating over the SUnits.
239 ScheduleDAGTopologicalSort Topo;
245 NodeInfo() = default;
247 /// Computed properties for each node in the graph.
248 std::vector<NodeInfo> ScheduleInfo;
250 enum OrderKind { BottomUp = 0, TopDown = 1 };
251 /// Computed node ordering for scheduling.
252 SetVector<SUnit *> NodeOrder;
254 using NodeSetType = SmallVector<NodeSet, 8>;
255 using ValueMapTy = DenseMap<unsigned, unsigned>;
256 using MBBVectorTy = SmallVectorImpl<MachineBasicBlock *>;
257 using InstrMapTy = DenseMap<MachineInstr *, MachineInstr *>;
259 /// Instructions to change when emitting the final schedule.
260 DenseMap<SUnit *, std::pair<unsigned, int64_t>> InstrChanges;
262 /// We may create a new instruction, so remember it because it
263 /// must be deleted when the pass is finished.
264 SmallPtrSet<MachineInstr *, 4> NewMIs;
266 /// Ordered list of DAG postprocessing steps.
267 std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
269 /// Helper class to implement Johnson's circuit finding algorithm.
271 std::vector<SUnit> &SUnits;
272 SetVector<SUnit *> Stack;
274 SmallVector<SmallPtrSet<SUnit *, 4>, 10> B;
275 SmallVector<SmallVector<int, 4>, 16> AdjK;
277 static unsigned MaxPaths;
280 Circuits(std::vector<SUnit> &SUs)
281 : SUnits(SUs), Blocked(SUs.size()), B(SUs.size()), AdjK(SUs.size()) {}
283 /// Reset the data structures used in the circuit algorithm.
287 B.assign(SUnits.size(), SmallPtrSet<SUnit *, 4>());
291 void createAdjacencyStructure(SwingSchedulerDAG *DAG);
292 bool circuit(int V, int S, NodeSetType &NodeSets, bool HasBackedge = false);
297 SwingSchedulerDAG(MachinePipeliner &P, MachineLoop &L, LiveIntervals &lis,
298 const RegisterClassInfo &rci)
299 : ScheduleDAGInstrs(*P.MF, P.MLI, false), Pass(P), Loop(L), LIS(lis),
300 RegClassInfo(rci), Topo(SUnits, &ExitSU) {
301 P.MF->getSubtarget().getSMSMutations(Mutations);
304 void schedule() override;
305 void finishBlock() override;
307 /// Return true if the loop kernel has been scheduled.
308 bool hasNewSchedule() { return Scheduled; }
310 /// Return the earliest time an instruction may be scheduled.
311 int getASAP(SUnit *Node) { return ScheduleInfo[Node->NodeNum].ASAP; }
313 /// Return the latest time an instruction my be scheduled.
314 int getALAP(SUnit *Node) { return ScheduleInfo[Node->NodeNum].ALAP; }
316 /// The mobility function, which the the number of slots in which
317 /// an instruction may be scheduled.
318 int getMOV(SUnit *Node) { return getALAP(Node) - getASAP(Node); }
320 /// The depth, in the dependence graph, for a node.
321 int getDepth(SUnit *Node) { return Node->getDepth(); }
323 /// The height, in the dependence graph, for a node.
324 int getHeight(SUnit *Node) { return Node->getHeight(); }
326 /// Return true if the dependence is a back-edge in the data dependence graph.
327 /// Since the DAG doesn't contain cycles, we represent a cycle in the graph
328 /// using an anti dependence from a Phi to an instruction.
329 bool isBackedge(SUnit *Source, const SDep &Dep) {
330 if (Dep.getKind() != SDep::Anti)
332 return Source->getInstr()->isPHI() || Dep.getSUnit()->getInstr()->isPHI();
335 /// Return true if the dependence is an order dependence between non-Phis.
336 static bool isOrder(SUnit *Source, const SDep &Dep) {
337 if (Dep.getKind() != SDep::Order)
339 return (!Source->getInstr()->isPHI() &&
340 !Dep.getSUnit()->getInstr()->isPHI());
343 bool isLoopCarriedOrder(SUnit *Source, const SDep &Dep, bool isSucc = true);
345 /// The latency of the dependence.
346 unsigned getLatency(SUnit *Source, const SDep &Dep) {
347 // Anti dependences represent recurrences, so use the latency of the
348 // instruction on the back-edge.
349 if (Dep.getKind() == SDep::Anti) {
350 if (Source->getInstr()->isPHI())
351 return Dep.getSUnit()->Latency;
352 if (Dep.getSUnit()->getInstr()->isPHI())
353 return Source->Latency;
354 return Dep.getLatency();
356 return Dep.getLatency();
359 /// The distance function, which indicates that operation V of iteration I
360 /// depends on operations U of iteration I-distance.
361 unsigned getDistance(SUnit *U, SUnit *V, const SDep &Dep) {
362 // Instructions that feed a Phi have a distance of 1. Computing larger
363 // values for arrays requires data dependence information.
364 if (V->getInstr()->isPHI() && Dep.getKind() == SDep::Anti)
369 /// Set the Minimum Initiation Interval for this schedule attempt.
370 void setMII(unsigned mii) { MII = mii; }
372 void applyInstrChange(MachineInstr *MI, SMSchedule &Schedule);
374 void fixupRegisterOverlaps(std::deque<SUnit *> &Instrs);
376 /// Return the new base register that was stored away for the changed
378 unsigned getInstrBaseReg(SUnit *SU) {
379 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
380 InstrChanges.find(SU);
381 if (It != InstrChanges.end())
382 return It->second.first;
386 void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
387 Mutations.push_back(std::move(Mutation));
391 void addLoopCarriedDependences(AliasAnalysis *AA);
392 void updatePhiDependences();
393 void changeDependences();
394 unsigned calculateResMII();
395 unsigned calculateRecMII(NodeSetType &RecNodeSets);
396 void findCircuits(NodeSetType &NodeSets);
397 void fuseRecs(NodeSetType &NodeSets);
398 void removeDuplicateNodes(NodeSetType &NodeSets);
399 void computeNodeFunctions(NodeSetType &NodeSets);
400 void registerPressureFilter(NodeSetType &NodeSets);
401 void colocateNodeSets(NodeSetType &NodeSets);
402 void checkNodeSets(NodeSetType &NodeSets);
403 void groupRemainingNodes(NodeSetType &NodeSets);
404 void addConnectedNodes(SUnit *SU, NodeSet &NewSet,
405 SetVector<SUnit *> &NodesAdded);
406 void computeNodeOrder(NodeSetType &NodeSets);
407 bool schedulePipeline(SMSchedule &Schedule);
408 void generatePipelinedLoop(SMSchedule &Schedule);
409 void generateProlog(SMSchedule &Schedule, unsigned LastStage,
410 MachineBasicBlock *KernelBB, ValueMapTy *VRMap,
411 MBBVectorTy &PrologBBs);
412 void generateEpilog(SMSchedule &Schedule, unsigned LastStage,
413 MachineBasicBlock *KernelBB, ValueMapTy *VRMap,
414 MBBVectorTy &EpilogBBs, MBBVectorTy &PrologBBs);
415 void generateExistingPhis(MachineBasicBlock *NewBB, MachineBasicBlock *BB1,
416 MachineBasicBlock *BB2, MachineBasicBlock *KernelBB,
417 SMSchedule &Schedule, ValueMapTy *VRMap,
418 InstrMapTy &InstrMap, unsigned LastStageNum,
419 unsigned CurStageNum, bool IsLast);
420 void generatePhis(MachineBasicBlock *NewBB, MachineBasicBlock *BB1,
421 MachineBasicBlock *BB2, MachineBasicBlock *KernelBB,
422 SMSchedule &Schedule, ValueMapTy *VRMap,
423 InstrMapTy &InstrMap, unsigned LastStageNum,
424 unsigned CurStageNum, bool IsLast);
425 void removeDeadInstructions(MachineBasicBlock *KernelBB,
426 MBBVectorTy &EpilogBBs);
427 void splitLifetimes(MachineBasicBlock *KernelBB, MBBVectorTy &EpilogBBs,
428 SMSchedule &Schedule);
429 void addBranches(MBBVectorTy &PrologBBs, MachineBasicBlock *KernelBB,
430 MBBVectorTy &EpilogBBs, SMSchedule &Schedule,
432 bool computeDelta(MachineInstr &MI, unsigned &Delta);
433 void updateMemOperands(MachineInstr &NewMI, MachineInstr &OldMI,
435 MachineInstr *cloneInstr(MachineInstr *OldMI, unsigned CurStageNum,
436 unsigned InstStageNum);
437 MachineInstr *cloneAndChangeInstr(MachineInstr *OldMI, unsigned CurStageNum,
438 unsigned InstStageNum,
439 SMSchedule &Schedule);
440 void updateInstruction(MachineInstr *NewMI, bool LastDef,
441 unsigned CurStageNum, unsigned InstStageNum,
442 SMSchedule &Schedule, ValueMapTy *VRMap);
443 MachineInstr *findDefInLoop(unsigned Reg);
444 unsigned getPrevMapVal(unsigned StageNum, unsigned PhiStage, unsigned LoopVal,
445 unsigned LoopStage, ValueMapTy *VRMap,
446 MachineBasicBlock *BB);
447 void rewritePhiValues(MachineBasicBlock *NewBB, unsigned StageNum,
448 SMSchedule &Schedule, ValueMapTy *VRMap,
449 InstrMapTy &InstrMap);
450 void rewriteScheduledInstr(MachineBasicBlock *BB, SMSchedule &Schedule,
451 InstrMapTy &InstrMap, unsigned CurStageNum,
452 unsigned PhiNum, MachineInstr *Phi,
453 unsigned OldReg, unsigned NewReg,
454 unsigned PrevReg = 0);
455 bool canUseLastOffsetValue(MachineInstr *MI, unsigned &BasePos,
456 unsigned &OffsetPos, unsigned &NewBase,
458 void postprocessDAG();
461 /// A NodeSet contains a set of SUnit DAG nodes with additional information
462 /// that assigns a priority to the set.
464 SetVector<SUnit *> Nodes;
465 bool HasRecurrence = false;
469 unsigned Colocate = 0;
470 SUnit *ExceedPressure = nullptr;
473 using iterator = SetVector<SUnit *>::const_iterator;
476 NodeSet(iterator S, iterator E) : Nodes(S, E), HasRecurrence(true) {}
478 bool insert(SUnit *SU) { return Nodes.insert(SU); }
480 void insert(iterator S, iterator E) { Nodes.insert(S, E); }
482 template <typename UnaryPredicate> bool remove_if(UnaryPredicate P) {
483 return Nodes.remove_if(P);
486 unsigned count(SUnit *SU) const { return Nodes.count(SU); }
488 bool hasRecurrence() { return HasRecurrence; };
490 unsigned size() const { return Nodes.size(); }
492 bool empty() const { return Nodes.empty(); }
494 SUnit *getNode(unsigned i) const { return Nodes[i]; };
496 void setRecMII(unsigned mii) { RecMII = mii; };
498 void setColocate(unsigned c) { Colocate = c; };
500 void setExceedPressure(SUnit *SU) { ExceedPressure = SU; }
502 bool isExceedSU(SUnit *SU) { return ExceedPressure == SU; }
504 int compareRecMII(NodeSet &RHS) { return RecMII - RHS.RecMII; }
506 int getRecMII() { return RecMII; }
508 /// Summarize node functions for the entire node set.
509 void computeNodeSetInfo(SwingSchedulerDAG *SSD) {
510 for (SUnit *SU : *this) {
511 MaxMOV = std::max(MaxMOV, SSD->getMOV(SU));
512 MaxDepth = std::max(MaxDepth, SSD->getDepth(SU));
519 HasRecurrence = false;
523 ExceedPressure = nullptr;
526 operator SetVector<SUnit *> &() { return Nodes; }
528 /// Sort the node sets by importance. First, rank them by recurrence MII,
529 /// then by mobility (least mobile done first), and finally by depth.
530 /// Each node set may contain a colocate value which is used as the first
531 /// tie breaker, if it's set.
532 bool operator>(const NodeSet &RHS) const {
533 if (RecMII == RHS.RecMII) {
534 if (Colocate != 0 && RHS.Colocate != 0 && Colocate != RHS.Colocate)
535 return Colocate < RHS.Colocate;
536 if (MaxMOV == RHS.MaxMOV)
537 return MaxDepth > RHS.MaxDepth;
538 return MaxMOV < RHS.MaxMOV;
540 return RecMII > RHS.RecMII;
543 bool operator==(const NodeSet &RHS) const {
544 return RecMII == RHS.RecMII && MaxMOV == RHS.MaxMOV &&
545 MaxDepth == RHS.MaxDepth;
548 bool operator!=(const NodeSet &RHS) const { return !operator==(RHS); }
550 iterator begin() { return Nodes.begin(); }
551 iterator end() { return Nodes.end(); }
553 void print(raw_ostream &os) const {
554 os << "Num nodes " << size() << " rec " << RecMII << " mov " << MaxMOV
555 << " depth " << MaxDepth << " col " << Colocate << "\n";
556 for (const auto &I : Nodes)
557 os << " SU(" << I->NodeNum << ") " << *(I->getInstr());
561 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
562 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
566 /// This class repesents the scheduled code. The main data structure is a
567 /// map from scheduled cycle to instructions. During scheduling, the
568 /// data structure explicitly represents all stages/iterations. When
569 /// the algorithm finshes, the schedule is collapsed into a single stage,
570 /// which represents instructions from different loop iterations.
572 /// The SMS algorithm allows negative values for cycles, so the first cycle
573 /// in the schedule is the smallest cycle value.
576 /// Map from execution cycle to instructions.
577 DenseMap<int, std::deque<SUnit *>> ScheduledInstrs;
579 /// Map from instruction to execution cycle.
580 std::map<SUnit *, int> InstrToCycle;
582 /// Map for each register and the max difference between its uses and def.
583 /// The first element in the pair is the max difference in stages. The
584 /// second is true if the register defines a Phi value and loop value is
585 /// scheduled before the Phi.
586 std::map<unsigned, std::pair<unsigned, bool>> RegToStageDiff;
588 /// Keep track of the first cycle value in the schedule. It starts
589 /// as zero, but the algorithm allows negative values.
592 /// Keep track of the last cycle value in the schedule.
595 /// The initiation interval (II) for the schedule.
596 int InitiationInterval = 0;
598 /// Target machine information.
599 const TargetSubtargetInfo &ST;
601 /// Virtual register information.
602 MachineRegisterInfo &MRI;
604 std::unique_ptr<DFAPacketizer> Resources;
607 SMSchedule(MachineFunction *mf)
608 : ST(mf->getSubtarget()), MRI(mf->getRegInfo()),
609 Resources(ST.getInstrInfo()->CreateTargetScheduleState(ST)) {}
612 ScheduledInstrs.clear();
613 InstrToCycle.clear();
614 RegToStageDiff.clear();
617 InitiationInterval = 0;
620 /// Set the initiation interval for this schedule.
621 void setInitiationInterval(int ii) { InitiationInterval = ii; }
623 /// Return the first cycle in the completed schedule. This
624 /// can be a negative value.
625 int getFirstCycle() const { return FirstCycle; }
627 /// Return the last cycle in the finalized schedule.
628 int getFinalCycle() const { return FirstCycle + InitiationInterval - 1; }
630 /// Return the cycle of the earliest scheduled instruction in the dependence
632 int earliestCycleInChain(const SDep &Dep);
634 /// Return the cycle of the latest scheduled instruction in the dependence
636 int latestCycleInChain(const SDep &Dep);
638 void computeStart(SUnit *SU, int *MaxEarlyStart, int *MinLateStart,
639 int *MinEnd, int *MaxStart, int II, SwingSchedulerDAG *DAG);
640 bool insert(SUnit *SU, int StartCycle, int EndCycle, int II);
642 /// Iterators for the cycle to instruction map.
643 using sched_iterator = DenseMap<int, std::deque<SUnit *>>::iterator;
644 using const_sched_iterator =
645 DenseMap<int, std::deque<SUnit *>>::const_iterator;
647 /// Return true if the instruction is scheduled at the specified stage.
648 bool isScheduledAtStage(SUnit *SU, unsigned StageNum) {
649 return (stageScheduled(SU) == (int)StageNum);
652 /// Return the stage for a scheduled instruction. Return -1 if
653 /// the instruction has not been scheduled.
654 int stageScheduled(SUnit *SU) const {
655 std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SU);
656 if (it == InstrToCycle.end())
658 return (it->second - FirstCycle) / InitiationInterval;
661 /// Return the cycle for a scheduled instruction. This function normalizes
662 /// the first cycle to be 0.
663 unsigned cycleScheduled(SUnit *SU) const {
664 std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SU);
665 assert(it != InstrToCycle.end() && "Instruction hasn't been scheduled.");
666 return (it->second - FirstCycle) % InitiationInterval;
669 /// Return the maximum stage count needed for this schedule.
670 unsigned getMaxStageCount() {
671 return (LastCycle - FirstCycle) / InitiationInterval;
674 /// Return the max. number of stages/iterations that can occur between a
675 /// register definition and its uses.
676 unsigned getStagesForReg(int Reg, unsigned CurStage) {
677 std::pair<unsigned, bool> Stages = RegToStageDiff[Reg];
678 if (CurStage > getMaxStageCount() && Stages.first == 0 && Stages.second)
683 /// The number of stages for a Phi is a little different than other
684 /// instructions. The minimum value computed in RegToStageDiff is 1
685 /// because we assume the Phi is needed for at least 1 iteration.
686 /// This is not the case if the loop value is scheduled prior to the
687 /// Phi in the same stage. This function returns the number of stages
688 /// or iterations needed between the Phi definition and any uses.
689 unsigned getStagesForPhi(int Reg) {
690 std::pair<unsigned, bool> Stages = RegToStageDiff[Reg];
693 return Stages.first - 1;
696 /// Return the instructions that are scheduled at the specified cycle.
697 std::deque<SUnit *> &getInstructions(int cycle) {
698 return ScheduledInstrs[cycle];
701 bool isValidSchedule(SwingSchedulerDAG *SSD);
702 void finalizeSchedule(SwingSchedulerDAG *SSD);
703 bool orderDependence(SwingSchedulerDAG *SSD, SUnit *SU,
704 std::deque<SUnit *> &Insts);
705 bool isLoopCarried(SwingSchedulerDAG *SSD, MachineInstr &Phi);
706 bool isLoopCarriedDefOfUse(SwingSchedulerDAG *SSD, MachineInstr *Inst,
708 void print(raw_ostream &os) const;
712 } // end anonymous namespace
714 unsigned SwingSchedulerDAG::Circuits::MaxPaths = 5;
715 char MachinePipeliner::ID = 0;
717 int MachinePipeliner::NumTries = 0;
719 char &llvm::MachinePipelinerID = MachinePipeliner::ID;
721 INITIALIZE_PASS_BEGIN(MachinePipeliner, DEBUG_TYPE,
722 "Modulo Software Pipelining", false, false)
723 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
724 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
725 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
726 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
727 INITIALIZE_PASS_END(MachinePipeliner, DEBUG_TYPE,
728 "Modulo Software Pipelining", false, false)
730 /// The "main" function for implementing Swing Modulo Scheduling.
731 bool MachinePipeliner::runOnMachineFunction(MachineFunction &mf) {
732 if (skipFunction(mf.getFunction()))
738 if (mf.getFunction().getAttributes().hasAttribute(
739 AttributeList::FunctionIndex, Attribute::OptimizeForSize) &&
740 !EnableSWPOptSize.getPosition())
744 MLI = &getAnalysis<MachineLoopInfo>();
745 MDT = &getAnalysis<MachineDominatorTree>();
746 TII = MF->getSubtarget().getInstrInfo();
747 RegClassInfo.runOnMachineFunction(*MF);
755 /// Attempt to perform the SMS algorithm on the specified loop. This function is
756 /// the main entry point for the algorithm. The function identifies candidate
757 /// loops, calculates the minimum initiation interval, and attempts to schedule
759 bool MachinePipeliner::scheduleLoop(MachineLoop &L) {
760 bool Changed = false;
761 for (auto &InnerLoop : L)
762 Changed |= scheduleLoop(*InnerLoop);
765 // Stop trying after reaching the limit (if any).
766 int Limit = SwpLoopLimit;
768 if (NumTries >= SwpLoopLimit)
774 if (!canPipelineLoop(L))
779 Changed = swingModuloScheduler(L);
784 /// Return true if the loop can be software pipelined. The algorithm is
785 /// restricted to loops with a single basic block. Make sure that the
786 /// branch in the loop can be analyzed.
787 bool MachinePipeliner::canPipelineLoop(MachineLoop &L) {
788 if (L.getNumBlocks() != 1)
791 // Check if the branch can't be understood because we can't do pipelining
792 // if that's the case.
796 if (TII->analyzeBranch(*L.getHeader(), LI.TBB, LI.FBB, LI.BrCond))
799 LI.LoopInductionVar = nullptr;
800 LI.LoopCompare = nullptr;
801 if (TII->analyzeLoop(L, LI.LoopInductionVar, LI.LoopCompare))
804 if (!L.getLoopPreheader())
807 // If any of the Phis contain subregs, then we can't pipeline
808 // because we don't know how to maintain subreg information in the
810 MachineBasicBlock *MBB = L.getHeader();
811 for (MachineBasicBlock::iterator BBI = MBB->instr_begin(),
812 BBE = MBB->getFirstNonPHI();
814 for (unsigned i = 1; i != BBI->getNumOperands(); i += 2)
815 if (BBI->getOperand(i).getSubReg() != 0)
821 /// The SMS algorithm consists of the following main steps:
822 /// 1. Computation and analysis of the dependence graph.
823 /// 2. Ordering of the nodes (instructions).
824 /// 3. Attempt to Schedule the loop.
825 bool MachinePipeliner::swingModuloScheduler(MachineLoop &L) {
826 assert(L.getBlocks().size() == 1 && "SMS works on single blocks only.");
828 SwingSchedulerDAG SMS(*this, L, getAnalysis<LiveIntervals>(), RegClassInfo);
830 MachineBasicBlock *MBB = L.getHeader();
831 // The kernel should not include any terminator instructions. These
832 // will be added back later.
835 // Compute the number of 'real' instructions in the basic block by
836 // ignoring terminators.
837 unsigned size = MBB->size();
838 for (MachineBasicBlock::iterator I = MBB->getFirstTerminator(),
839 E = MBB->instr_end();
843 SMS.enterRegion(MBB, MBB->begin(), MBB->getFirstTerminator(), size);
848 return SMS.hasNewSchedule();
851 /// We override the schedule function in ScheduleDAGInstrs to implement the
852 /// scheduling part of the Swing Modulo Scheduling algorithm.
853 void SwingSchedulerDAG::schedule() {
854 AliasAnalysis *AA = &Pass.getAnalysis<AAResultsWrapperPass>().getAAResults();
856 addLoopCarriedDependences(AA);
857 updatePhiDependences();
858 Topo.InitDAGTopologicalSorting();
862 for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
863 SUnits[su].dumpAll(this);
866 NodeSetType NodeSets;
867 findCircuits(NodeSets);
869 // Calculate the MII.
870 unsigned ResMII = calculateResMII();
871 unsigned RecMII = calculateRecMII(NodeSets);
875 // This flag is used for testing and can cause correctness problems.
879 MII = std::max(ResMII, RecMII);
880 DEBUG(dbgs() << "MII = " << MII << " (rec=" << RecMII << ", res=" << ResMII
883 // Can't schedule a loop without a valid MII.
887 // Don't pipeline large loops.
888 if (SwpMaxMii != -1 && (int)MII > SwpMaxMii)
891 computeNodeFunctions(NodeSets);
893 registerPressureFilter(NodeSets);
895 colocateNodeSets(NodeSets);
897 checkNodeSets(NodeSets);
900 for (auto &I : NodeSets) {
901 dbgs() << " Rec NodeSet ";
906 std::sort(NodeSets.begin(), NodeSets.end(), std::greater<NodeSet>());
908 groupRemainingNodes(NodeSets);
910 removeDuplicateNodes(NodeSets);
913 for (auto &I : NodeSets) {
914 dbgs() << " NodeSet ";
919 computeNodeOrder(NodeSets);
921 SMSchedule Schedule(Pass.MF);
922 Scheduled = schedulePipeline(Schedule);
927 unsigned numStages = Schedule.getMaxStageCount();
928 // No need to generate pipeline if there are no overlapped iterations.
932 // Check that the maximum stage count is less than user-defined limit.
933 if (SwpMaxStages > -1 && (int)numStages > SwpMaxStages)
936 generatePipelinedLoop(Schedule);
940 /// Clean up after the software pipeliner runs.
941 void SwingSchedulerDAG::finishBlock() {
942 for (MachineInstr *I : NewMIs)
943 MF.DeleteMachineInstr(I);
946 // Call the superclass.
947 ScheduleDAGInstrs::finishBlock();
950 /// Return the register values for the operands of a Phi instruction.
951 /// This function assume the instruction is a Phi.
952 static void getPhiRegs(MachineInstr &Phi, MachineBasicBlock *Loop,
953 unsigned &InitVal, unsigned &LoopVal) {
954 assert(Phi.isPHI() && "Expecting a Phi.");
958 for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2)
959 if (Phi.getOperand(i + 1).getMBB() != Loop)
960 InitVal = Phi.getOperand(i).getReg();
962 LoopVal = Phi.getOperand(i).getReg();
964 assert(InitVal != 0 && LoopVal != 0 && "Unexpected Phi structure.");
967 /// Return the Phi register value that comes from the incoming block.
968 static unsigned getInitPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) {
969 for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2)
970 if (Phi.getOperand(i + 1).getMBB() != LoopBB)
971 return Phi.getOperand(i).getReg();
975 /// Return the Phi register value that comes the the loop block.
976 static unsigned getLoopPhiReg(MachineInstr &Phi, MachineBasicBlock *LoopBB) {
977 for (unsigned i = 1, e = Phi.getNumOperands(); i != e; i += 2)
978 if (Phi.getOperand(i + 1).getMBB() == LoopBB)
979 return Phi.getOperand(i).getReg();
983 /// Return true if SUb can be reached from SUa following the chain edges.
984 static bool isSuccOrder(SUnit *SUa, SUnit *SUb) {
985 SmallPtrSet<SUnit *, 8> Visited;
986 SmallVector<SUnit *, 8> Worklist;
987 Worklist.push_back(SUa);
988 while (!Worklist.empty()) {
989 const SUnit *SU = Worklist.pop_back_val();
990 for (auto &SI : SU->Succs) {
991 SUnit *SuccSU = SI.getSUnit();
992 if (SI.getKind() == SDep::Order) {
993 if (Visited.count(SuccSU))
997 Worklist.push_back(SuccSU);
998 Visited.insert(SuccSU);
1005 /// Return true if the instruction causes a chain between memory
1006 /// references before and after it.
1007 static bool isDependenceBarrier(MachineInstr &MI, AliasAnalysis *AA) {
1008 return MI.isCall() || MI.hasUnmodeledSideEffects() ||
1009 (MI.hasOrderedMemoryRef() &&
1010 (!MI.mayLoad() || !MI.isDereferenceableInvariantLoad(AA)));
1013 /// Return the underlying objects for the memory references of an instruction.
1014 /// This function calls the code in ValueTracking, but first checks that the
1015 /// instruction has a memory operand.
1016 static void getUnderlyingObjects(MachineInstr *MI,
1017 SmallVectorImpl<Value *> &Objs,
1018 const DataLayout &DL) {
1019 if (!MI->hasOneMemOperand())
1021 MachineMemOperand *MM = *MI->memoperands_begin();
1022 if (!MM->getValue())
1024 GetUnderlyingObjects(const_cast<Value *>(MM->getValue()), Objs, DL);
1027 /// Add a chain edge between a load and store if the store can be an
1028 /// alias of the load on a subsequent iteration, i.e., a loop carried
1029 /// dependence. This code is very similar to the code in ScheduleDAGInstrs
1030 /// but that code doesn't create loop carried dependences.
1031 void SwingSchedulerDAG::addLoopCarriedDependences(AliasAnalysis *AA) {
1032 MapVector<Value *, SmallVector<SUnit *, 4>> PendingLoads;
1033 for (auto &SU : SUnits) {
1034 MachineInstr &MI = *SU.getInstr();
1035 if (isDependenceBarrier(MI, AA))
1036 PendingLoads.clear();
1037 else if (MI.mayLoad()) {
1038 SmallVector<Value *, 4> Objs;
1039 getUnderlyingObjects(&MI, Objs, MF.getDataLayout());
1040 for (auto V : Objs) {
1041 SmallVector<SUnit *, 4> &SUs = PendingLoads[V];
1044 } else if (MI.mayStore()) {
1045 SmallVector<Value *, 4> Objs;
1046 getUnderlyingObjects(&MI, Objs, MF.getDataLayout());
1047 for (auto V : Objs) {
1048 MapVector<Value *, SmallVector<SUnit *, 4>>::iterator I =
1049 PendingLoads.find(V);
1050 if (I == PendingLoads.end())
1052 for (auto Load : I->second) {
1053 if (isSuccOrder(Load, &SU))
1055 MachineInstr &LdMI = *Load->getInstr();
1056 // First, perform the cheaper check that compares the base register.
1057 // If they are the same and the load offset is less than the store
1058 // offset, then mark the dependence as loop carried potentially.
1059 unsigned BaseReg1, BaseReg2;
1060 int64_t Offset1, Offset2;
1061 if (!TII->getMemOpBaseRegImmOfs(LdMI, BaseReg1, Offset1, TRI) ||
1062 !TII->getMemOpBaseRegImmOfs(MI, BaseReg2, Offset2, TRI)) {
1063 SU.addPred(SDep(Load, SDep::Barrier));
1066 if (BaseReg1 == BaseReg2 && (int)Offset1 < (int)Offset2) {
1067 assert(TII->areMemAccessesTriviallyDisjoint(LdMI, MI, AA) &&
1068 "What happened to the chain edge?");
1069 SU.addPred(SDep(Load, SDep::Barrier));
1072 // Second, the more expensive check that uses alias analysis on the
1073 // base registers. If they alias, and the load offset is less than
1074 // the store offset, the mark the dependence as loop carried.
1076 SU.addPred(SDep(Load, SDep::Barrier));
1079 MachineMemOperand *MMO1 = *LdMI.memoperands_begin();
1080 MachineMemOperand *MMO2 = *MI.memoperands_begin();
1081 if (!MMO1->getValue() || !MMO2->getValue()) {
1082 SU.addPred(SDep(Load, SDep::Barrier));
1085 if (MMO1->getValue() == MMO2->getValue() &&
1086 MMO1->getOffset() <= MMO2->getOffset()) {
1087 SU.addPred(SDep(Load, SDep::Barrier));
1090 AliasResult AAResult = AA->alias(
1091 MemoryLocation(MMO1->getValue(), MemoryLocation::UnknownSize,
1093 MemoryLocation(MMO2->getValue(), MemoryLocation::UnknownSize,
1094 MMO2->getAAInfo()));
1096 if (AAResult != NoAlias)
1097 SU.addPred(SDep(Load, SDep::Barrier));
1104 /// Update the phi dependences to the DAG because ScheduleDAGInstrs no longer
1105 /// processes dependences for PHIs. This function adds true dependences
1106 /// from a PHI to a use, and a loop carried dependence from the use to the
1107 /// PHI. The loop carried dependence is represented as an anti dependence
1108 /// edge. This function also removes chain dependences between unrelated
1110 void SwingSchedulerDAG::updatePhiDependences() {
1111 SmallVector<SDep, 4> RemoveDeps;
1112 const TargetSubtargetInfo &ST = MF.getSubtarget<TargetSubtargetInfo>();
1114 // Iterate over each DAG node.
1115 for (SUnit &I : SUnits) {
1117 // Set to true if the instruction has an operand defined by a Phi.
1118 unsigned HasPhiUse = 0;
1119 unsigned HasPhiDef = 0;
1120 MachineInstr *MI = I.getInstr();
1121 // Iterate over each operand, and we process the definitions.
1122 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
1123 MOE = MI->operands_end();
1124 MOI != MOE; ++MOI) {
1127 unsigned Reg = MOI->getReg();
1129 // If the register is used by a Phi, then create an anti dependence.
1130 for (MachineRegisterInfo::use_instr_iterator
1131 UI = MRI.use_instr_begin(Reg),
1132 UE = MRI.use_instr_end();
1134 MachineInstr *UseMI = &*UI;
1135 SUnit *SU = getSUnit(UseMI);
1136 if (SU != nullptr && UseMI->isPHI()) {
1138 SDep Dep(SU, SDep::Anti, Reg);
1142 // Add a chain edge to a dependent Phi that isn't an existing
1144 if (SU->NodeNum < I.NodeNum && !I.isPred(SU))
1145 I.addPred(SDep(SU, SDep::Barrier));
1149 } else if (MOI->isUse()) {
1150 // If the register is defined by a Phi, then create a true dependence.
1151 MachineInstr *DefMI = MRI.getUniqueVRegDef(Reg);
1152 if (DefMI == nullptr)
1154 SUnit *SU = getSUnit(DefMI);
1155 if (SU != nullptr && DefMI->isPHI()) {
1157 SDep Dep(SU, SDep::Data, Reg);
1159 ST.adjustSchedDependency(SU, &I, Dep);
1163 // Add a chain edge to a dependent Phi that isn't an existing
1165 if (SU->NodeNum < I.NodeNum && !I.isPred(SU))
1166 I.addPred(SDep(SU, SDep::Barrier));
1171 // Remove order dependences from an unrelated Phi.
1174 for (auto &PI : I.Preds) {
1175 MachineInstr *PMI = PI.getSUnit()->getInstr();
1176 if (PMI->isPHI() && PI.getKind() == SDep::Order) {
1177 if (I.getInstr()->isPHI()) {
1178 if (PMI->getOperand(0).getReg() == HasPhiUse)
1180 if (getLoopPhiReg(*PMI, PMI->getParent()) == HasPhiDef)
1183 RemoveDeps.push_back(PI);
1186 for (int i = 0, e = RemoveDeps.size(); i != e; ++i)
1187 I.removePred(RemoveDeps[i]);
1191 /// Iterate over each DAG node and see if we can change any dependences
1192 /// in order to reduce the recurrence MII.
1193 void SwingSchedulerDAG::changeDependences() {
1194 // See if an instruction can use a value from the previous iteration.
1195 // If so, we update the base and offset of the instruction and change
1197 for (SUnit &I : SUnits) {
1198 unsigned BasePos = 0, OffsetPos = 0, NewBase = 0;
1199 int64_t NewOffset = 0;
1200 if (!canUseLastOffsetValue(I.getInstr(), BasePos, OffsetPos, NewBase,
1204 // Get the MI and SUnit for the instruction that defines the original base.
1205 unsigned OrigBase = I.getInstr()->getOperand(BasePos).getReg();
1206 MachineInstr *DefMI = MRI.getUniqueVRegDef(OrigBase);
1209 SUnit *DefSU = getSUnit(DefMI);
1212 // Get the MI and SUnit for the instruction that defins the new base.
1213 MachineInstr *LastMI = MRI.getUniqueVRegDef(NewBase);
1216 SUnit *LastSU = getSUnit(LastMI);
1220 if (Topo.IsReachable(&I, LastSU))
1223 // Remove the dependence. The value now depends on a prior iteration.
1224 SmallVector<SDep, 4> Deps;
1225 for (SUnit::pred_iterator P = I.Preds.begin(), E = I.Preds.end(); P != E;
1227 if (P->getSUnit() == DefSU)
1229 for (int i = 0, e = Deps.size(); i != e; i++) {
1230 Topo.RemovePred(&I, Deps[i].getSUnit());
1231 I.removePred(Deps[i]);
1233 // Remove the chain dependence between the instructions.
1235 for (auto &P : LastSU->Preds)
1236 if (P.getSUnit() == &I && P.getKind() == SDep::Order)
1238 for (int i = 0, e = Deps.size(); i != e; i++) {
1239 Topo.RemovePred(LastSU, Deps[i].getSUnit());
1240 LastSU->removePred(Deps[i]);
1243 // Add a dependence between the new instruction and the instruction
1244 // that defines the new base.
1245 SDep Dep(&I, SDep::Anti, NewBase);
1246 LastSU->addPred(Dep);
1248 // Remember the base and offset information so that we can update the
1249 // instruction during code generation.
1250 InstrChanges[&I] = std::make_pair(NewBase, NewOffset);
1256 // FuncUnitSorter - Comparison operator used to sort instructions by
1257 // the number of functional unit choices.
1258 struct FuncUnitSorter {
1259 const InstrItineraryData *InstrItins;
1260 DenseMap<unsigned, unsigned> Resources;
1262 FuncUnitSorter(const InstrItineraryData *IID) : InstrItins(IID) {}
1264 // Compute the number of functional unit alternatives needed
1265 // at each stage, and take the minimum value. We prioritize the
1266 // instructions by the least number of choices first.
1267 unsigned minFuncUnits(const MachineInstr *Inst, unsigned &F) const {
1268 unsigned schedClass = Inst->getDesc().getSchedClass();
1269 unsigned min = UINT_MAX;
1270 for (const InstrStage *IS = InstrItins->beginStage(schedClass),
1271 *IE = InstrItins->endStage(schedClass);
1273 unsigned funcUnits = IS->getUnits();
1274 unsigned numAlternatives = countPopulation(funcUnits);
1275 if (numAlternatives < min) {
1276 min = numAlternatives;
1283 // Compute the critical resources needed by the instruction. This
1284 // function records the functional units needed by instructions that
1285 // must use only one functional unit. We use this as a tie breaker
1286 // for computing the resource MII. The instrutions that require
1287 // the same, highly used, functional unit have high priority.
1288 void calcCriticalResources(MachineInstr &MI) {
1289 unsigned SchedClass = MI.getDesc().getSchedClass();
1290 for (const InstrStage *IS = InstrItins->beginStage(SchedClass),
1291 *IE = InstrItins->endStage(SchedClass);
1293 unsigned FuncUnits = IS->getUnits();
1294 if (countPopulation(FuncUnits) == 1)
1295 Resources[FuncUnits]++;
1299 /// Return true if IS1 has less priority than IS2.
1300 bool operator()(const MachineInstr *IS1, const MachineInstr *IS2) const {
1301 unsigned F1 = 0, F2 = 0;
1302 unsigned MFUs1 = minFuncUnits(IS1, F1);
1303 unsigned MFUs2 = minFuncUnits(IS2, F2);
1304 if (MFUs1 == 1 && MFUs2 == 1)
1305 return Resources.lookup(F1) < Resources.lookup(F2);
1306 return MFUs1 > MFUs2;
1310 } // end anonymous namespace
1312 /// Calculate the resource constrained minimum initiation interval for the
1313 /// specified loop. We use the DFA to model the resources needed for
1314 /// each instruction, and we ignore dependences. A different DFA is created
1315 /// for each cycle that is required. When adding a new instruction, we attempt
1316 /// to add it to each existing DFA, until a legal space is found. If the
1317 /// instruction cannot be reserved in an existing DFA, we create a new one.
1318 unsigned SwingSchedulerDAG::calculateResMII() {
1319 SmallVector<DFAPacketizer *, 8> Resources;
1320 MachineBasicBlock *MBB = Loop.getHeader();
1321 Resources.push_back(TII->CreateTargetScheduleState(MF.getSubtarget()));
1323 // Sort the instructions by the number of available choices for scheduling,
1324 // least to most. Use the number of critical resources as the tie breaker.
1325 FuncUnitSorter FUS =
1326 FuncUnitSorter(MF.getSubtarget().getInstrItineraryData());
1327 for (MachineBasicBlock::iterator I = MBB->getFirstNonPHI(),
1328 E = MBB->getFirstTerminator();
1330 FUS.calcCriticalResources(*I);
1331 PriorityQueue<MachineInstr *, std::vector<MachineInstr *>, FuncUnitSorter>
1334 for (MachineBasicBlock::iterator I = MBB->getFirstNonPHI(),
1335 E = MBB->getFirstTerminator();
1337 FuncUnitOrder.push(&*I);
1339 while (!FuncUnitOrder.empty()) {
1340 MachineInstr *MI = FuncUnitOrder.top();
1341 FuncUnitOrder.pop();
1342 if (TII->isZeroCost(MI->getOpcode()))
1344 // Attempt to reserve the instruction in an existing DFA. At least one
1345 // DFA is needed for each cycle.
1346 unsigned NumCycles = getSUnit(MI)->Latency;
1347 unsigned ReservedCycles = 0;
1348 SmallVectorImpl<DFAPacketizer *>::iterator RI = Resources.begin();
1349 SmallVectorImpl<DFAPacketizer *>::iterator RE = Resources.end();
1350 for (unsigned C = 0; C < NumCycles; ++C)
1352 if ((*RI++)->canReserveResources(*MI)) {
1357 // Start reserving resources using existing DFAs.
1358 for (unsigned C = 0; C < ReservedCycles; ++C) {
1360 (*RI)->reserveResources(*MI);
1362 // Add new DFAs, if needed, to reserve resources.
1363 for (unsigned C = ReservedCycles; C < NumCycles; ++C) {
1364 DFAPacketizer *NewResource =
1365 TII->CreateTargetScheduleState(MF.getSubtarget());
1366 assert(NewResource->canReserveResources(*MI) && "Reserve error.");
1367 NewResource->reserveResources(*MI);
1368 Resources.push_back(NewResource);
1371 int Resmii = Resources.size();
1372 // Delete the memory for each of the DFAs that were created earlier.
1373 for (DFAPacketizer *RI : Resources) {
1374 DFAPacketizer *D = RI;
1381 /// Calculate the recurrence-constrainted minimum initiation interval.
1382 /// Iterate over each circuit. Compute the delay(c) and distance(c)
1383 /// for each circuit. The II needs to satisfy the inequality
1384 /// delay(c) - II*distance(c) <= 0. For each circuit, choose the smallest
1385 /// II that satistifies the inequality, and the RecMII is the maximum
1386 /// of those values.
1387 unsigned SwingSchedulerDAG::calculateRecMII(NodeSetType &NodeSets) {
1388 unsigned RecMII = 0;
1390 for (NodeSet &Nodes : NodeSets) {
1394 unsigned Delay = Nodes.size() - 1;
1395 unsigned Distance = 1;
1397 // ii = ceil(delay / distance)
1398 unsigned CurMII = (Delay + Distance - 1) / Distance;
1399 Nodes.setRecMII(CurMII);
1400 if (CurMII > RecMII)
1407 /// Swap all the anti dependences in the DAG. That means it is no longer a DAG,
1408 /// but we do this to find the circuits, and then change them back.
1409 static void swapAntiDependences(std::vector<SUnit> &SUnits) {
1410 SmallVector<std::pair<SUnit *, SDep>, 8> DepsAdded;
1411 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
1412 SUnit *SU = &SUnits[i];
1413 for (SUnit::pred_iterator IP = SU->Preds.begin(), EP = SU->Preds.end();
1415 if (IP->getKind() != SDep::Anti)
1417 DepsAdded.push_back(std::make_pair(SU, *IP));
1420 for (SmallVector<std::pair<SUnit *, SDep>, 8>::iterator I = DepsAdded.begin(),
1421 E = DepsAdded.end();
1423 // Remove this anti dependency and add one in the reverse direction.
1424 SUnit *SU = I->first;
1425 SDep &D = I->second;
1426 SUnit *TargetSU = D.getSUnit();
1427 unsigned Reg = D.getReg();
1428 unsigned Lat = D.getLatency();
1430 SDep Dep(SU, SDep::Anti, Reg);
1431 Dep.setLatency(Lat);
1432 TargetSU->addPred(Dep);
1436 /// Create the adjacency structure of the nodes in the graph.
1437 void SwingSchedulerDAG::Circuits::createAdjacencyStructure(
1438 SwingSchedulerDAG *DAG) {
1439 BitVector Added(SUnits.size());
1440 for (int i = 0, e = SUnits.size(); i != e; ++i) {
1442 // Add any successor to the adjacency matrix and exclude duplicates.
1443 for (auto &SI : SUnits[i].Succs) {
1444 // Do not process a boundary node and a back-edge is processed only
1445 // if it goes to a Phi.
1446 if (SI.getSUnit()->isBoundaryNode() ||
1447 (SI.getKind() == SDep::Anti && !SI.getSUnit()->getInstr()->isPHI()))
1449 int N = SI.getSUnit()->NodeNum;
1450 if (!Added.test(N)) {
1451 AdjK[i].push_back(N);
1455 // A chain edge between a store and a load is treated as a back-edge in the
1456 // adjacency matrix.
1457 for (auto &PI : SUnits[i].Preds) {
1458 if (!SUnits[i].getInstr()->mayStore() ||
1459 !DAG->isLoopCarriedOrder(&SUnits[i], PI, false))
1461 if (PI.getKind() == SDep::Order && PI.getSUnit()->getInstr()->mayLoad()) {
1462 int N = PI.getSUnit()->NodeNum;
1463 if (!Added.test(N)) {
1464 AdjK[i].push_back(N);
1472 /// Identify an elementary circuit in the dependence graph starting at the
1474 bool SwingSchedulerDAG::Circuits::circuit(int V, int S, NodeSetType &NodeSets,
1476 SUnit *SV = &SUnits[V];
1481 for (auto W : AdjK[V]) {
1482 if (NumPaths > MaxPaths)
1488 NodeSets.push_back(NodeSet(Stack.begin(), Stack.end()));
1492 } else if (!Blocked.test(W)) {
1493 if (circuit(W, S, NodeSets, W < V ? true : HasBackedge))
1501 for (auto W : AdjK[V]) {
1504 if (B[W].count(SV) == 0)
1512 /// Unblock a node in the circuit finding algorithm.
1513 void SwingSchedulerDAG::Circuits::unblock(int U) {
1515 SmallPtrSet<SUnit *, 4> &BU = B[U];
1516 while (!BU.empty()) {
1517 SmallPtrSet<SUnit *, 4>::iterator SI = BU.begin();
1518 assert(SI != BU.end() && "Invalid B set.");
1521 if (Blocked.test(W->NodeNum))
1522 unblock(W->NodeNum);
1526 /// Identify all the elementary circuits in the dependence graph using
1527 /// Johnson's circuit algorithm.
1528 void SwingSchedulerDAG::findCircuits(NodeSetType &NodeSets) {
1529 // Swap all the anti dependences in the DAG. That means it is no longer a DAG,
1530 // but we do this to find the circuits, and then change them back.
1531 swapAntiDependences(SUnits);
1533 Circuits Cir(SUnits);
1534 // Create the adjacency structure.
1535 Cir.createAdjacencyStructure(this);
1536 for (int i = 0, e = SUnits.size(); i != e; ++i) {
1538 Cir.circuit(i, i, NodeSets);
1541 // Change the dependences back so that we've created a DAG again.
1542 swapAntiDependences(SUnits);
1545 /// Return true for DAG nodes that we ignore when computing the cost functions.
1546 /// We ignore the back-edge recurrence in order to avoid unbounded recurison
1547 /// in the calculation of the ASAP, ALAP, etc functions.
1548 static bool ignoreDependence(const SDep &D, bool isPred) {
1549 if (D.isArtificial())
1551 return D.getKind() == SDep::Anti && isPred;
1554 /// Compute several functions need to order the nodes for scheduling.
1555 /// ASAP - Earliest time to schedule a node.
1556 /// ALAP - Latest time to schedule a node.
1557 /// MOV - Mobility function, difference between ALAP and ASAP.
1558 /// D - Depth of each node.
1559 /// H - Height of each node.
1560 void SwingSchedulerDAG::computeNodeFunctions(NodeSetType &NodeSets) {
1561 ScheduleInfo.resize(SUnits.size());
1564 for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(),
1567 SUnit *SU = &SUnits[*I];
1574 for (ScheduleDAGTopologicalSort::const_iterator I = Topo.begin(),
1578 SUnit *SU = &SUnits[*I];
1579 for (SUnit::const_pred_iterator IP = SU->Preds.begin(),
1580 EP = SU->Preds.end();
1582 if (ignoreDependence(*IP, true))
1584 SUnit *pred = IP->getSUnit();
1585 asap = std::max(asap, (int)(getASAP(pred) + getLatency(SU, *IP) -
1586 getDistance(pred, SU, *IP) * MII));
1588 maxASAP = std::max(maxASAP, asap);
1589 ScheduleInfo[*I].ASAP = asap;
1592 // Compute ALAP and MOV.
1593 for (ScheduleDAGTopologicalSort::const_reverse_iterator I = Topo.rbegin(),
1597 SUnit *SU = &SUnits[*I];
1598 for (SUnit::const_succ_iterator IS = SU->Succs.begin(),
1599 ES = SU->Succs.end();
1601 if (ignoreDependence(*IS, true))
1603 SUnit *succ = IS->getSUnit();
1604 alap = std::min(alap, (int)(getALAP(succ) - getLatency(SU, *IS) +
1605 getDistance(SU, succ, *IS) * MII));
1608 ScheduleInfo[*I].ALAP = alap;
1611 // After computing the node functions, compute the summary for each node set.
1612 for (NodeSet &I : NodeSets)
1613 I.computeNodeSetInfo(this);
1616 for (unsigned i = 0; i < SUnits.size(); i++) {
1617 dbgs() << "\tNode " << i << ":\n";
1618 dbgs() << "\t ASAP = " << getASAP(&SUnits[i]) << "\n";
1619 dbgs() << "\t ALAP = " << getALAP(&SUnits[i]) << "\n";
1620 dbgs() << "\t MOV = " << getMOV(&SUnits[i]) << "\n";
1621 dbgs() << "\t D = " << getDepth(&SUnits[i]) << "\n";
1622 dbgs() << "\t H = " << getHeight(&SUnits[i]) << "\n";
1627 /// Compute the Pred_L(O) set, as defined in the paper. The set is defined
1628 /// as the predecessors of the elements of NodeOrder that are not also in
1630 static bool pred_L(SetVector<SUnit *> &NodeOrder,
1631 SmallSetVector<SUnit *, 8> &Preds,
1632 const NodeSet *S = nullptr) {
1634 for (SetVector<SUnit *>::iterator I = NodeOrder.begin(), E = NodeOrder.end();
1636 for (SUnit::pred_iterator PI = (*I)->Preds.begin(), PE = (*I)->Preds.end();
1638 if (S && S->count(PI->getSUnit()) == 0)
1640 if (ignoreDependence(*PI, true))
1642 if (NodeOrder.count(PI->getSUnit()) == 0)
1643 Preds.insert(PI->getSUnit());
1645 // Back-edges are predecessors with an anti-dependence.
1646 for (SUnit::const_succ_iterator IS = (*I)->Succs.begin(),
1647 ES = (*I)->Succs.end();
1649 if (IS->getKind() != SDep::Anti)
1651 if (S && S->count(IS->getSUnit()) == 0)
1653 if (NodeOrder.count(IS->getSUnit()) == 0)
1654 Preds.insert(IS->getSUnit());
1657 return !Preds.empty();
1660 /// Compute the Succ_L(O) set, as defined in the paper. The set is defined
1661 /// as the successors of the elements of NodeOrder that are not also in
1663 static bool succ_L(SetVector<SUnit *> &NodeOrder,
1664 SmallSetVector<SUnit *, 8> &Succs,
1665 const NodeSet *S = nullptr) {
1667 for (SetVector<SUnit *>::iterator I = NodeOrder.begin(), E = NodeOrder.end();
1669 for (SUnit::succ_iterator SI = (*I)->Succs.begin(), SE = (*I)->Succs.end();
1671 if (S && S->count(SI->getSUnit()) == 0)
1673 if (ignoreDependence(*SI, false))
1675 if (NodeOrder.count(SI->getSUnit()) == 0)
1676 Succs.insert(SI->getSUnit());
1678 for (SUnit::const_pred_iterator PI = (*I)->Preds.begin(),
1679 PE = (*I)->Preds.end();
1681 if (PI->getKind() != SDep::Anti)
1683 if (S && S->count(PI->getSUnit()) == 0)
1685 if (NodeOrder.count(PI->getSUnit()) == 0)
1686 Succs.insert(PI->getSUnit());
1689 return !Succs.empty();
1692 /// Return true if there is a path from the specified node to any of the nodes
1693 /// in DestNodes. Keep track and return the nodes in any path.
1694 static bool computePath(SUnit *Cur, SetVector<SUnit *> &Path,
1695 SetVector<SUnit *> &DestNodes,
1696 SetVector<SUnit *> &Exclude,
1697 SmallPtrSet<SUnit *, 8> &Visited) {
1698 if (Cur->isBoundaryNode())
1700 if (Exclude.count(Cur) != 0)
1702 if (DestNodes.count(Cur) != 0)
1704 if (!Visited.insert(Cur).second)
1705 return Path.count(Cur) != 0;
1706 bool FoundPath = false;
1707 for (auto &SI : Cur->Succs)
1708 FoundPath |= computePath(SI.getSUnit(), Path, DestNodes, Exclude, Visited);
1709 for (auto &PI : Cur->Preds)
1710 if (PI.getKind() == SDep::Anti)
1712 computePath(PI.getSUnit(), Path, DestNodes, Exclude, Visited);
1718 /// Return true if Set1 is a subset of Set2.
1719 template <class S1Ty, class S2Ty> static bool isSubset(S1Ty &Set1, S2Ty &Set2) {
1720 for (typename S1Ty::iterator I = Set1.begin(), E = Set1.end(); I != E; ++I)
1721 if (Set2.count(*I) == 0)
1726 /// Compute the live-out registers for the instructions in a node-set.
1727 /// The live-out registers are those that are defined in the node-set,
1728 /// but not used. Except for use operands of Phis.
1729 static void computeLiveOuts(MachineFunction &MF, RegPressureTracker &RPTracker,
1731 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1732 MachineRegisterInfo &MRI = MF.getRegInfo();
1733 SmallVector<RegisterMaskPair, 8> LiveOutRegs;
1734 SmallSet<unsigned, 4> Uses;
1735 for (SUnit *SU : NS) {
1736 const MachineInstr *MI = SU->getInstr();
1739 for (const MachineOperand &MO : MI->operands())
1740 if (MO.isReg() && MO.isUse()) {
1741 unsigned Reg = MO.getReg();
1742 if (TargetRegisterInfo::isVirtualRegister(Reg))
1744 else if (MRI.isAllocatable(Reg))
1745 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
1746 Uses.insert(*Units);
1749 for (SUnit *SU : NS)
1750 for (const MachineOperand &MO : SU->getInstr()->operands())
1751 if (MO.isReg() && MO.isDef() && !MO.isDead()) {
1752 unsigned Reg = MO.getReg();
1753 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1754 if (!Uses.count(Reg))
1755 LiveOutRegs.push_back(RegisterMaskPair(Reg,
1756 LaneBitmask::getNone()));
1757 } else if (MRI.isAllocatable(Reg)) {
1758 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
1759 if (!Uses.count(*Units))
1760 LiveOutRegs.push_back(RegisterMaskPair(*Units,
1761 LaneBitmask::getNone()));
1764 RPTracker.addLiveRegs(LiveOutRegs);
1767 /// A heuristic to filter nodes in recurrent node-sets if the register
1768 /// pressure of a set is too high.
1769 void SwingSchedulerDAG::registerPressureFilter(NodeSetType &NodeSets) {
1770 for (auto &NS : NodeSets) {
1771 // Skip small node-sets since they won't cause register pressure problems.
1774 IntervalPressure RecRegPressure;
1775 RegPressureTracker RecRPTracker(RecRegPressure);
1776 RecRPTracker.init(&MF, &RegClassInfo, &LIS, BB, BB->end(), false, true);
1777 computeLiveOuts(MF, RecRPTracker, NS);
1778 RecRPTracker.closeBottom();
1780 std::vector<SUnit *> SUnits(NS.begin(), NS.end());
1781 std::sort(SUnits.begin(), SUnits.end(), [](const SUnit *A, const SUnit *B) {
1782 return A->NodeNum > B->NodeNum;
1785 for (auto &SU : SUnits) {
1786 // Since we're computing the register pressure for a subset of the
1787 // instructions in a block, we need to set the tracker for each
1788 // instruction in the node-set. The tracker is set to the instruction
1789 // just after the one we're interested in.
1790 MachineBasicBlock::const_iterator CurInstI = SU->getInstr();
1791 RecRPTracker.setPos(std::next(CurInstI));
1793 RegPressureDelta RPDelta;
1794 ArrayRef<PressureChange> CriticalPSets;
1795 RecRPTracker.getMaxUpwardPressureDelta(SU->getInstr(), nullptr, RPDelta,
1797 RecRegPressure.MaxSetPressure);
1798 if (RPDelta.Excess.isValid()) {
1799 DEBUG(dbgs() << "Excess register pressure: SU(" << SU->NodeNum << ") "
1800 << TRI->getRegPressureSetName(RPDelta.Excess.getPSet())
1801 << ":" << RPDelta.Excess.getUnitInc());
1802 NS.setExceedPressure(SU);
1805 RecRPTracker.recede();
1810 /// A heuristic to colocate node sets that have the same set of
1812 void SwingSchedulerDAG::colocateNodeSets(NodeSetType &NodeSets) {
1813 unsigned Colocate = 0;
1814 for (int i = 0, e = NodeSets.size(); i < e; ++i) {
1815 NodeSet &N1 = NodeSets[i];
1816 SmallSetVector<SUnit *, 8> S1;
1817 if (N1.empty() || !succ_L(N1, S1))
1819 for (int j = i + 1; j < e; ++j) {
1820 NodeSet &N2 = NodeSets[j];
1821 if (N1.compareRecMII(N2) != 0)
1823 SmallSetVector<SUnit *, 8> S2;
1824 if (N2.empty() || !succ_L(N2, S2))
1826 if (isSubset(S1, S2) && S1.size() == S2.size()) {
1827 N1.setColocate(++Colocate);
1828 N2.setColocate(Colocate);
1835 /// Check if the existing node-sets are profitable. If not, then ignore the
1836 /// recurrent node-sets, and attempt to schedule all nodes together. This is
1837 /// a heuristic. If the MII is large and there is a non-recurrent node with
1838 /// a large depth compared to the MII, then it's best to try and schedule
1839 /// all instruction together instead of starting with the recurrent node-sets.
1840 void SwingSchedulerDAG::checkNodeSets(NodeSetType &NodeSets) {
1841 // Look for loops with a large MII.
1844 // Check if the node-set contains only a simple add recurrence.
1845 for (auto &NS : NodeSets)
1848 // If the depth of any instruction is significantly larger than the MII, then
1849 // ignore the recurrent node-sets and treat all instructions equally.
1850 for (auto &SU : SUnits)
1851 if (SU.getDepth() > MII * 1.5) {
1853 DEBUG(dbgs() << "Clear recurrence node-sets\n");
1858 /// Add the nodes that do not belong to a recurrence set into groups
1859 /// based upon connected componenets.
1860 void SwingSchedulerDAG::groupRemainingNodes(NodeSetType &NodeSets) {
1861 SetVector<SUnit *> NodesAdded;
1862 SmallPtrSet<SUnit *, 8> Visited;
1863 // Add the nodes that are on a path between the previous node sets and
1864 // the current node set.
1865 for (NodeSet &I : NodeSets) {
1866 SmallSetVector<SUnit *, 8> N;
1867 // Add the nodes from the current node set to the previous node set.
1869 SetVector<SUnit *> Path;
1870 for (SUnit *NI : N) {
1872 computePath(NI, Path, NodesAdded, I, Visited);
1875 I.insert(Path.begin(), Path.end());
1877 // Add the nodes from the previous node set to the current node set.
1879 if (succ_L(NodesAdded, N)) {
1880 SetVector<SUnit *> Path;
1881 for (SUnit *NI : N) {
1883 computePath(NI, Path, I, NodesAdded, Visited);
1886 I.insert(Path.begin(), Path.end());
1888 NodesAdded.insert(I.begin(), I.end());
1891 // Create a new node set with the connected nodes of any successor of a node
1892 // in a recurrent set.
1894 SmallSetVector<SUnit *, 8> N;
1895 if (succ_L(NodesAdded, N))
1897 addConnectedNodes(I, NewSet, NodesAdded);
1898 if (!NewSet.empty())
1899 NodeSets.push_back(NewSet);
1901 // Create a new node set with the connected nodes of any predecessor of a node
1902 // in a recurrent set.
1904 if (pred_L(NodesAdded, N))
1906 addConnectedNodes(I, NewSet, NodesAdded);
1907 if (!NewSet.empty())
1908 NodeSets.push_back(NewSet);
1910 // Create new nodes sets with the connected nodes any any remaining node that
1911 // has no predecessor.
1912 for (unsigned i = 0; i < SUnits.size(); ++i) {
1913 SUnit *SU = &SUnits[i];
1914 if (NodesAdded.count(SU) == 0) {
1916 addConnectedNodes(SU, NewSet, NodesAdded);
1917 if (!NewSet.empty())
1918 NodeSets.push_back(NewSet);
1923 /// Add the node to the set, and add all is its connected nodes to the set.
1924 void SwingSchedulerDAG::addConnectedNodes(SUnit *SU, NodeSet &NewSet,
1925 SetVector<SUnit *> &NodesAdded) {
1927 NodesAdded.insert(SU);
1928 for (auto &SI : SU->Succs) {
1929 SUnit *Successor = SI.getSUnit();
1930 if (!SI.isArtificial() && NodesAdded.count(Successor) == 0)
1931 addConnectedNodes(Successor, NewSet, NodesAdded);
1933 for (auto &PI : SU->Preds) {
1934 SUnit *Predecessor = PI.getSUnit();
1935 if (!PI.isArtificial() && NodesAdded.count(Predecessor) == 0)
1936 addConnectedNodes(Predecessor, NewSet, NodesAdded);
1940 /// Return true if Set1 contains elements in Set2. The elements in common
1941 /// are returned in a different container.
1942 static bool isIntersect(SmallSetVector<SUnit *, 8> &Set1, const NodeSet &Set2,
1943 SmallSetVector<SUnit *, 8> &Result) {
1945 for (unsigned i = 0, e = Set1.size(); i != e; ++i) {
1946 SUnit *SU = Set1[i];
1947 if (Set2.count(SU) != 0)
1950 return !Result.empty();
1953 /// Merge the recurrence node sets that have the same initial node.
1954 void SwingSchedulerDAG::fuseRecs(NodeSetType &NodeSets) {
1955 for (NodeSetType::iterator I = NodeSets.begin(), E = NodeSets.end(); I != E;
1958 for (NodeSetType::iterator J = I + 1; J != E;) {
1960 if (NI.getNode(0)->NodeNum == NJ.getNode(0)->NodeNum) {
1961 if (NJ.compareRecMII(NI) > 0)
1962 NI.setRecMII(NJ.getRecMII());
1963 for (NodeSet::iterator NII = J->begin(), ENI = J->end(); NII != ENI;
1975 /// Remove nodes that have been scheduled in previous NodeSets.
1976 void SwingSchedulerDAG::removeDuplicateNodes(NodeSetType &NodeSets) {
1977 for (NodeSetType::iterator I = NodeSets.begin(), E = NodeSets.end(); I != E;
1979 for (NodeSetType::iterator J = I + 1; J != E;) {
1980 J->remove_if([&](SUnit *SUJ) { return I->count(SUJ); });
1991 /// Return true if Inst1 defines a value that is used in Inst2.
1992 static bool hasDataDependence(SUnit *Inst1, SUnit *Inst2) {
1993 for (auto &SI : Inst1->Succs)
1994 if (SI.getSUnit() == Inst2 && SI.getKind() == SDep::Data)
1999 /// Compute an ordered list of the dependence graph nodes, which
2000 /// indicates the order that the nodes will be scheduled. This is a
2001 /// two-level algorithm. First, a partial order is created, which
2002 /// consists of a list of sets ordered from highest to lowest priority.
2003 void SwingSchedulerDAG::computeNodeOrder(NodeSetType &NodeSets) {
2004 SmallSetVector<SUnit *, 8> R;
2007 for (auto &Nodes : NodeSets) {
2008 DEBUG(dbgs() << "NodeSet size " << Nodes.size() << "\n");
2010 SmallSetVector<SUnit *, 8> N;
2011 if (pred_L(NodeOrder, N) && isSubset(N, Nodes)) {
2012 R.insert(N.begin(), N.end());
2014 DEBUG(dbgs() << " Bottom up (preds) ");
2015 } else if (succ_L(NodeOrder, N) && isSubset(N, Nodes)) {
2016 R.insert(N.begin(), N.end());
2018 DEBUG(dbgs() << " Top down (succs) ");
2019 } else if (isIntersect(N, Nodes, R)) {
2020 // If some of the successors are in the existing node-set, then use the
2021 // top-down ordering.
2023 DEBUG(dbgs() << " Top down (intersect) ");
2024 } else if (NodeSets.size() == 1) {
2025 for (auto &N : Nodes)
2026 if (N->Succs.size() == 0)
2029 DEBUG(dbgs() << " Bottom up (all) ");
2031 // Find the node with the highest ASAP.
2032 SUnit *maxASAP = nullptr;
2033 for (SUnit *SU : Nodes) {
2034 if (maxASAP == nullptr || getASAP(SU) >= getASAP(maxASAP))
2039 DEBUG(dbgs() << " Bottom up (default) ");
2042 while (!R.empty()) {
2043 if (Order == TopDown) {
2044 // Choose the node with the maximum height. If more than one, choose
2045 // the node with the lowest MOV. If still more than one, check if there
2046 // is a dependence between the instructions.
2047 while (!R.empty()) {
2048 SUnit *maxHeight = nullptr;
2049 for (SUnit *I : R) {
2050 if (maxHeight == nullptr || getHeight(I) > getHeight(maxHeight))
2052 else if (getHeight(I) == getHeight(maxHeight) &&
2053 getMOV(I) < getMOV(maxHeight) &&
2054 !hasDataDependence(maxHeight, I))
2056 else if (hasDataDependence(I, maxHeight))
2059 NodeOrder.insert(maxHeight);
2060 DEBUG(dbgs() << maxHeight->NodeNum << " ");
2061 R.remove(maxHeight);
2062 for (const auto &I : maxHeight->Succs) {
2063 if (Nodes.count(I.getSUnit()) == 0)
2065 if (NodeOrder.count(I.getSUnit()) != 0)
2067 if (ignoreDependence(I, false))
2069 R.insert(I.getSUnit());
2071 // Back-edges are predecessors with an anti-dependence.
2072 for (const auto &I : maxHeight->Preds) {
2073 if (I.getKind() != SDep::Anti)
2075 if (Nodes.count(I.getSUnit()) == 0)
2077 if (NodeOrder.count(I.getSUnit()) != 0)
2079 R.insert(I.getSUnit());
2083 DEBUG(dbgs() << "\n Switching order to bottom up ");
2084 SmallSetVector<SUnit *, 8> N;
2085 if (pred_L(NodeOrder, N, &Nodes))
2086 R.insert(N.begin(), N.end());
2088 // Choose the node with the maximum depth. If more than one, choose
2089 // the node with the lowest MOV. If there is still more than one, check
2090 // for a dependence between the instructions.
2091 while (!R.empty()) {
2092 SUnit *maxDepth = nullptr;
2093 for (SUnit *I : R) {
2094 if (maxDepth == nullptr || getDepth(I) > getDepth(maxDepth))
2096 else if (getDepth(I) == getDepth(maxDepth) &&
2097 getMOV(I) < getMOV(maxDepth) &&
2098 !hasDataDependence(I, maxDepth))
2100 else if (hasDataDependence(maxDepth, I))
2103 NodeOrder.insert(maxDepth);
2104 DEBUG(dbgs() << maxDepth->NodeNum << " ");
2106 if (Nodes.isExceedSU(maxDepth)) {
2109 R.insert(Nodes.getNode(0));
2112 for (const auto &I : maxDepth->Preds) {
2113 if (Nodes.count(I.getSUnit()) == 0)
2115 if (NodeOrder.count(I.getSUnit()) != 0)
2117 if (I.getKind() == SDep::Anti)
2119 R.insert(I.getSUnit());
2121 // Back-edges are predecessors with an anti-dependence.
2122 for (const auto &I : maxDepth->Succs) {
2123 if (I.getKind() != SDep::Anti)
2125 if (Nodes.count(I.getSUnit()) == 0)
2127 if (NodeOrder.count(I.getSUnit()) != 0)
2129 R.insert(I.getSUnit());
2133 DEBUG(dbgs() << "\n Switching order to top down ");
2134 SmallSetVector<SUnit *, 8> N;
2135 if (succ_L(NodeOrder, N, &Nodes))
2136 R.insert(N.begin(), N.end());
2139 DEBUG(dbgs() << "\nDone with Nodeset\n");
2143 dbgs() << "Node order: ";
2144 for (SUnit *I : NodeOrder)
2145 dbgs() << " " << I->NodeNum << " ";
2150 /// Process the nodes in the computed order and create the pipelined schedule
2151 /// of the instructions, if possible. Return true if a schedule is found.
2152 bool SwingSchedulerDAG::schedulePipeline(SMSchedule &Schedule) {
2153 if (NodeOrder.empty())
2156 bool scheduleFound = false;
2157 // Keep increasing II until a valid schedule is found.
2158 for (unsigned II = MII; II < MII + 10 && !scheduleFound; ++II) {
2160 Schedule.setInitiationInterval(II);
2161 DEBUG(dbgs() << "Try to schedule with " << II << "\n");
2163 SetVector<SUnit *>::iterator NI = NodeOrder.begin();
2164 SetVector<SUnit *>::iterator NE = NodeOrder.end();
2168 // Compute the schedule time for the instruction, which is based
2169 // upon the scheduled time for any predecessors/successors.
2170 int EarlyStart = INT_MIN;
2171 int LateStart = INT_MAX;
2172 // These values are set when the size of the schedule window is limited
2173 // due to chain dependences.
2174 int SchedEnd = INT_MAX;
2175 int SchedStart = INT_MIN;
2176 Schedule.computeStart(SU, &EarlyStart, &LateStart, &SchedEnd, &SchedStart,
2179 dbgs() << "Inst (" << SU->NodeNum << ") ";
2180 SU->getInstr()->dump();
2184 dbgs() << "\tes: " << EarlyStart << " ls: " << LateStart
2185 << " me: " << SchedEnd << " ms: " << SchedStart << "\n";
2188 if (EarlyStart > LateStart || SchedEnd < EarlyStart ||
2189 SchedStart > LateStart)
2190 scheduleFound = false;
2191 else if (EarlyStart != INT_MIN && LateStart == INT_MAX) {
2192 SchedEnd = std::min(SchedEnd, EarlyStart + (int)II - 1);
2193 scheduleFound = Schedule.insert(SU, EarlyStart, SchedEnd, II);
2194 } else if (EarlyStart == INT_MIN && LateStart != INT_MAX) {
2195 SchedStart = std::max(SchedStart, LateStart - (int)II + 1);
2196 scheduleFound = Schedule.insert(SU, LateStart, SchedStart, II);
2197 } else if (EarlyStart != INT_MIN && LateStart != INT_MAX) {
2199 std::min(SchedEnd, std::min(LateStart, EarlyStart + (int)II - 1));
2200 // When scheduling a Phi it is better to start at the late cycle and go
2201 // backwards. The default order may insert the Phi too far away from
2202 // its first dependence.
2203 if (SU->getInstr()->isPHI())
2204 scheduleFound = Schedule.insert(SU, SchedEnd, EarlyStart, II);
2206 scheduleFound = Schedule.insert(SU, EarlyStart, SchedEnd, II);
2208 int FirstCycle = Schedule.getFirstCycle();
2209 scheduleFound = Schedule.insert(SU, FirstCycle + getASAP(SU),
2210 FirstCycle + getASAP(SU) + II - 1, II);
2212 // Even if we find a schedule, make sure the schedule doesn't exceed the
2213 // allowable number of stages. We keep trying if this happens.
2215 if (SwpMaxStages > -1 &&
2216 Schedule.getMaxStageCount() > (unsigned)SwpMaxStages)
2217 scheduleFound = false;
2221 dbgs() << "\tCan't schedule\n";
2223 } while (++NI != NE && scheduleFound);
2225 // If a schedule is found, check if it is a valid schedule too.
2227 scheduleFound = Schedule.isValidSchedule(this);
2230 DEBUG(dbgs() << "Schedule Found? " << scheduleFound << "\n");
2233 Schedule.finalizeSchedule(this);
2237 return scheduleFound && Schedule.getMaxStageCount() > 0;
2240 /// Given a schedule for the loop, generate a new version of the loop,
2241 /// and replace the old version. This function generates a prolog
2242 /// that contains the initial iterations in the pipeline, and kernel
2243 /// loop, and the epilogue that contains the code for the final
2245 void SwingSchedulerDAG::generatePipelinedLoop(SMSchedule &Schedule) {
2246 // Create a new basic block for the kernel and add it to the CFG.
2247 MachineBasicBlock *KernelBB = MF.CreateMachineBasicBlock(BB->getBasicBlock());
2249 unsigned MaxStageCount = Schedule.getMaxStageCount();
2251 // Remember the registers that are used in different stages. The index is
2252 // the iteration, or stage, that the instruction is scheduled in. This is
2253 // a map between register names in the orignal block and the names created
2254 // in each stage of the pipelined loop.
2255 ValueMapTy *VRMap = new ValueMapTy[(MaxStageCount + 1) * 2];
2256 InstrMapTy InstrMap;
2258 SmallVector<MachineBasicBlock *, 4> PrologBBs;
2259 // Generate the prolog instructions that set up the pipeline.
2260 generateProlog(Schedule, MaxStageCount, KernelBB, VRMap, PrologBBs);
2261 MF.insert(BB->getIterator(), KernelBB);
2263 // Rearrange the instructions to generate the new, pipelined loop,
2264 // and update register names as needed.
2265 for (int Cycle = Schedule.getFirstCycle(),
2266 LastCycle = Schedule.getFinalCycle();
2267 Cycle <= LastCycle; ++Cycle) {
2268 std::deque<SUnit *> &CycleInstrs = Schedule.getInstructions(Cycle);
2269 // This inner loop schedules each instruction in the cycle.
2270 for (SUnit *CI : CycleInstrs) {
2271 if (CI->getInstr()->isPHI())
2273 unsigned StageNum = Schedule.stageScheduled(getSUnit(CI->getInstr()));
2274 MachineInstr *NewMI = cloneInstr(CI->getInstr(), MaxStageCount, StageNum);
2275 updateInstruction(NewMI, false, MaxStageCount, StageNum, Schedule, VRMap);
2276 KernelBB->push_back(NewMI);
2277 InstrMap[NewMI] = CI->getInstr();
2281 // Copy any terminator instructions to the new kernel, and update
2283 for (MachineBasicBlock::iterator I = BB->getFirstTerminator(),
2284 E = BB->instr_end();
2286 MachineInstr *NewMI = MF.CloneMachineInstr(&*I);
2287 updateInstruction(NewMI, false, MaxStageCount, 0, Schedule, VRMap);
2288 KernelBB->push_back(NewMI);
2289 InstrMap[NewMI] = &*I;
2292 KernelBB->transferSuccessors(BB);
2293 KernelBB->replaceSuccessor(BB, KernelBB);
2295 generateExistingPhis(KernelBB, PrologBBs.back(), KernelBB, KernelBB, Schedule,
2296 VRMap, InstrMap, MaxStageCount, MaxStageCount, false);
2297 generatePhis(KernelBB, PrologBBs.back(), KernelBB, KernelBB, Schedule, VRMap,
2298 InstrMap, MaxStageCount, MaxStageCount, false);
2300 DEBUG(dbgs() << "New block\n"; KernelBB->dump(););
2302 SmallVector<MachineBasicBlock *, 4> EpilogBBs;
2303 // Generate the epilog instructions to complete the pipeline.
2304 generateEpilog(Schedule, MaxStageCount, KernelBB, VRMap, EpilogBBs,
2307 // We need this step because the register allocation doesn't handle some
2308 // situations well, so we insert copies to help out.
2309 splitLifetimes(KernelBB, EpilogBBs, Schedule);
2311 // Remove dead instructions due to loop induction variables.
2312 removeDeadInstructions(KernelBB, EpilogBBs);
2314 // Add branches between prolog and epilog blocks.
2315 addBranches(PrologBBs, KernelBB, EpilogBBs, Schedule, VRMap);
2317 // Remove the original loop since it's no longer referenced.
2319 BB->eraseFromParent();
2324 /// Generate the pipeline prolog code.
2325 void SwingSchedulerDAG::generateProlog(SMSchedule &Schedule, unsigned LastStage,
2326 MachineBasicBlock *KernelBB,
2328 MBBVectorTy &PrologBBs) {
2329 MachineBasicBlock *PreheaderBB = MLI->getLoopFor(BB)->getLoopPreheader();
2330 assert(PreheaderBB != nullptr &&
2331 "Need to add code to handle loops w/o preheader");
2332 MachineBasicBlock *PredBB = PreheaderBB;
2333 InstrMapTy InstrMap;
2335 // Generate a basic block for each stage, not including the last stage,
2336 // which will be generated in the kernel. Each basic block may contain
2337 // instructions from multiple stages/iterations.
2338 for (unsigned i = 0; i < LastStage; ++i) {
2339 // Create and insert the prolog basic block prior to the original loop
2340 // basic block. The original loop is removed later.
2341 MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock(BB->getBasicBlock());
2342 PrologBBs.push_back(NewBB);
2343 MF.insert(BB->getIterator(), NewBB);
2344 NewBB->transferSuccessors(PredBB);
2345 PredBB->addSuccessor(NewBB);
2348 // Generate instructions for each appropriate stage. Process instructions
2349 // in original program order.
2350 for (int StageNum = i; StageNum >= 0; --StageNum) {
2351 for (MachineBasicBlock::iterator BBI = BB->instr_begin(),
2352 BBE = BB->getFirstTerminator();
2353 BBI != BBE; ++BBI) {
2354 if (Schedule.isScheduledAtStage(getSUnit(&*BBI), (unsigned)StageNum)) {
2357 MachineInstr *NewMI =
2358 cloneAndChangeInstr(&*BBI, i, (unsigned)StageNum, Schedule);
2359 updateInstruction(NewMI, false, i, (unsigned)StageNum, Schedule,
2361 NewBB->push_back(NewMI);
2362 InstrMap[NewMI] = &*BBI;
2366 rewritePhiValues(NewBB, i, Schedule, VRMap, InstrMap);
2368 dbgs() << "prolog:\n";
2373 PredBB->replaceSuccessor(BB, KernelBB);
2375 // Check if we need to remove the branch from the preheader to the original
2376 // loop, and replace it with a branch to the new loop.
2377 unsigned numBranches = TII->removeBranch(*PreheaderBB);
2379 SmallVector<MachineOperand, 0> Cond;
2380 TII->insertBranch(*PreheaderBB, PrologBBs[0], nullptr, Cond, DebugLoc());
2384 /// Generate the pipeline epilog code. The epilog code finishes the iterations
2385 /// that were started in either the prolog or the kernel. We create a basic
2386 /// block for each stage that needs to complete.
2387 void SwingSchedulerDAG::generateEpilog(SMSchedule &Schedule, unsigned LastStage,
2388 MachineBasicBlock *KernelBB,
2390 MBBVectorTy &EpilogBBs,
2391 MBBVectorTy &PrologBBs) {
2392 // We need to change the branch from the kernel to the first epilog block, so
2393 // this call to analyze branch uses the kernel rather than the original BB.
2394 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
2395 SmallVector<MachineOperand, 4> Cond;
2396 bool checkBranch = TII->analyzeBranch(*KernelBB, TBB, FBB, Cond);
2397 assert(!checkBranch && "generateEpilog must be able to analyze the branch");
2401 MachineBasicBlock::succ_iterator LoopExitI = KernelBB->succ_begin();
2402 if (*LoopExitI == KernelBB)
2404 assert(LoopExitI != KernelBB->succ_end() && "Expecting a successor");
2405 MachineBasicBlock *LoopExitBB = *LoopExitI;
2407 MachineBasicBlock *PredBB = KernelBB;
2408 MachineBasicBlock *EpilogStart = LoopExitBB;
2409 InstrMapTy InstrMap;
2411 // Generate a basic block for each stage, not including the last stage,
2412 // which was generated for the kernel. Each basic block may contain
2413 // instructions from multiple stages/iterations.
2414 int EpilogStage = LastStage + 1;
2415 for (unsigned i = LastStage; i >= 1; --i, ++EpilogStage) {
2416 MachineBasicBlock *NewBB = MF.CreateMachineBasicBlock();
2417 EpilogBBs.push_back(NewBB);
2418 MF.insert(BB->getIterator(), NewBB);
2420 PredBB->replaceSuccessor(LoopExitBB, NewBB);
2421 NewBB->addSuccessor(LoopExitBB);
2423 if (EpilogStart == LoopExitBB)
2424 EpilogStart = NewBB;
2426 // Add instructions to the epilog depending on the current block.
2427 // Process instructions in original program order.
2428 for (unsigned StageNum = i; StageNum <= LastStage; ++StageNum) {
2429 for (auto &BBI : *BB) {
2432 MachineInstr *In = &BBI;
2433 if (Schedule.isScheduledAtStage(getSUnit(In), StageNum)) {
2434 MachineInstr *NewMI = cloneInstr(In, EpilogStage - LastStage, 0);
2435 updateInstruction(NewMI, i == 1, EpilogStage, 0, Schedule, VRMap);
2436 NewBB->push_back(NewMI);
2437 InstrMap[NewMI] = In;
2441 generateExistingPhis(NewBB, PrologBBs[i - 1], PredBB, KernelBB, Schedule,
2442 VRMap, InstrMap, LastStage, EpilogStage, i == 1);
2443 generatePhis(NewBB, PrologBBs[i - 1], PredBB, KernelBB, Schedule, VRMap,
2444 InstrMap, LastStage, EpilogStage, i == 1);
2448 dbgs() << "epilog:\n";
2453 // Fix any Phi nodes in the loop exit block.
2454 for (MachineInstr &MI : *LoopExitBB) {
2457 for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
2458 MachineOperand &MO = MI.getOperand(i);
2459 if (MO.getMBB() == BB)
2464 // Create a branch to the new epilog from the kernel.
2465 // Remove the original branch and add a new branch to the epilog.
2466 TII->removeBranch(*KernelBB);
2467 TII->insertBranch(*KernelBB, KernelBB, EpilogStart, Cond, DebugLoc());
2468 // Add a branch to the loop exit.
2469 if (EpilogBBs.size() > 0) {
2470 MachineBasicBlock *LastEpilogBB = EpilogBBs.back();
2471 SmallVector<MachineOperand, 4> Cond1;
2472 TII->insertBranch(*LastEpilogBB, LoopExitBB, nullptr, Cond1, DebugLoc());
2476 /// Replace all uses of FromReg that appear outside the specified
2477 /// basic block with ToReg.
2478 static void replaceRegUsesAfterLoop(unsigned FromReg, unsigned ToReg,
2479 MachineBasicBlock *MBB,
2480 MachineRegisterInfo &MRI,
2481 LiveIntervals &LIS) {
2482 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(FromReg),
2485 MachineOperand &O = *I;
2487 if (O.getParent()->getParent() != MBB)
2490 if (!LIS.hasInterval(ToReg))
2491 LIS.createEmptyInterval(ToReg);
2494 /// Return true if the register has a use that occurs outside the
2496 static bool hasUseAfterLoop(unsigned Reg, MachineBasicBlock *BB,
2497 MachineRegisterInfo &MRI) {
2498 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(Reg),
2501 if (I->getParent()->getParent() != BB)
2506 /// Generate Phis for the specific block in the generated pipelined code.
2507 /// This function looks at the Phis from the original code to guide the
2508 /// creation of new Phis.
2509 void SwingSchedulerDAG::generateExistingPhis(
2510 MachineBasicBlock *NewBB, MachineBasicBlock *BB1, MachineBasicBlock *BB2,
2511 MachineBasicBlock *KernelBB, SMSchedule &Schedule, ValueMapTy *VRMap,
2512 InstrMapTy &InstrMap, unsigned LastStageNum, unsigned CurStageNum,
2514 // Compute the stage number for the initial value of the Phi, which
2515 // comes from the prolog. The prolog to use depends on to which kernel/
2516 // epilog that we're adding the Phi.
2517 unsigned PrologStage = 0;
2518 unsigned PrevStage = 0;
2519 bool InKernel = (LastStageNum == CurStageNum);
2521 PrologStage = LastStageNum - 1;
2522 PrevStage = CurStageNum;
2524 PrologStage = LastStageNum - (CurStageNum - LastStageNum);
2525 PrevStage = LastStageNum + (CurStageNum - LastStageNum) - 1;
2528 for (MachineBasicBlock::iterator BBI = BB->instr_begin(),
2529 BBE = BB->getFirstNonPHI();
2530 BBI != BBE; ++BBI) {
2531 unsigned Def = BBI->getOperand(0).getReg();
2533 unsigned InitVal = 0;
2534 unsigned LoopVal = 0;
2535 getPhiRegs(*BBI, BB, InitVal, LoopVal);
2537 unsigned PhiOp1 = 0;
2538 // The Phi value from the loop body typically is defined in the loop, but
2539 // not always. So, we need to check if the value is defined in the loop.
2540 unsigned PhiOp2 = LoopVal;
2541 if (VRMap[LastStageNum].count(LoopVal))
2542 PhiOp2 = VRMap[LastStageNum][LoopVal];
2544 int StageScheduled = Schedule.stageScheduled(getSUnit(&*BBI));
2546 Schedule.stageScheduled(getSUnit(MRI.getVRegDef(LoopVal)));
2547 unsigned NumStages = Schedule.getStagesForReg(Def, CurStageNum);
2548 if (NumStages == 0) {
2549 // We don't need to generate a Phi anymore, but we need to rename any uses
2550 // of the Phi value.
2551 unsigned NewReg = VRMap[PrevStage][LoopVal];
2552 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, 0, &*BBI,
2554 if (VRMap[CurStageNum].count(LoopVal))
2555 VRMap[CurStageNum][Def] = VRMap[CurStageNum][LoopVal];
2557 // Adjust the number of Phis needed depending on the number of prologs left,
2558 // and the distance from where the Phi is first scheduled.
2559 unsigned NumPhis = NumStages;
2560 if (!InKernel && (int)PrologStage < LoopValStage)
2561 // The NumPhis is the maximum number of new Phis needed during the steady
2562 // state. If the Phi has not been scheduled in current prolog, then we
2563 // need to generate less Phis.
2564 NumPhis = std::max((int)NumPhis - (int)(LoopValStage - PrologStage), 1);
2565 // The number of Phis cannot exceed the number of prolog stages. Each
2566 // stage can potentially define two values.
2567 NumPhis = std::min(NumPhis, PrologStage + 2);
2569 unsigned NewReg = 0;
2571 unsigned AccessStage = (LoopValStage != -1) ? LoopValStage : StageScheduled;
2572 // In the epilog, we may need to look back one stage to get the correct
2573 // Phi name because the epilog and prolog blocks execute the same stage.
2574 // The correct name is from the previous block only when the Phi has
2575 // been completely scheduled prior to the epilog, and Phi value is not
2576 // needed in multiple stages.
2578 if (!InKernel && StageScheduled >= LoopValStage && AccessStage == 0 &&
2581 // Adjust the computations below when the phi and the loop definition
2582 // are scheduled in different stages.
2583 if (InKernel && LoopValStage != -1 && StageScheduled > LoopValStage)
2584 StageDiff = StageScheduled - LoopValStage;
2585 for (unsigned np = 0; np < NumPhis; ++np) {
2586 // If the Phi hasn't been scheduled, then use the initial Phi operand
2587 // value. Otherwise, use the scheduled version of the instruction. This
2588 // is a little complicated when a Phi references another Phi.
2589 if (np > PrologStage || StageScheduled >= (int)LastStageNum)
2591 // Check if the Phi has already been scheduled in a prolog stage.
2592 else if (PrologStage >= AccessStage + StageDiff + np &&
2593 VRMap[PrologStage - StageDiff - np].count(LoopVal) != 0)
2594 PhiOp1 = VRMap[PrologStage - StageDiff - np][LoopVal];
2595 // Check if the Phi has already been scheduled, but the loop intruction
2596 // is either another Phi, or doesn't occur in the loop.
2597 else if (PrologStage >= AccessStage + StageDiff + np) {
2598 // If the Phi references another Phi, we need to examine the other
2599 // Phi to get the correct value.
2601 MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1);
2603 while (InstOp1 && InstOp1->isPHI() && InstOp1->getParent() == BB) {
2604 int PhiStage = Schedule.stageScheduled(getSUnit(InstOp1));
2605 if ((int)(PrologStage - StageDiff - np) < PhiStage + Indirects)
2606 PhiOp1 = getInitPhiReg(*InstOp1, BB);
2608 PhiOp1 = getLoopPhiReg(*InstOp1, BB);
2609 InstOp1 = MRI.getVRegDef(PhiOp1);
2610 int PhiOpStage = Schedule.stageScheduled(getSUnit(InstOp1));
2611 int StageAdj = (PhiOpStage != -1 ? PhiStage - PhiOpStage : 0);
2612 if (PhiOpStage != -1 && PrologStage - StageAdj >= Indirects + np &&
2613 VRMap[PrologStage - StageAdj - Indirects - np].count(PhiOp1)) {
2614 PhiOp1 = VRMap[PrologStage - StageAdj - Indirects - np][PhiOp1];
2621 // If this references a generated Phi in the kernel, get the Phi operand
2622 // from the incoming block.
2623 if (MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1))
2624 if (InstOp1->isPHI() && InstOp1->getParent() == KernelBB)
2625 PhiOp1 = getInitPhiReg(*InstOp1, KernelBB);
2627 MachineInstr *PhiInst = MRI.getVRegDef(LoopVal);
2628 bool LoopDefIsPhi = PhiInst && PhiInst->isPHI();
2629 // In the epilog, a map lookup is needed to get the value from the kernel,
2630 // or previous epilog block. How is does this depends on if the
2631 // instruction is scheduled in the previous block.
2633 int StageDiffAdj = 0;
2634 if (LoopValStage != -1 && StageScheduled > LoopValStage)
2635 StageDiffAdj = StageScheduled - LoopValStage;
2636 // Use the loop value defined in the kernel, unless the kernel
2637 // contains the last definition of the Phi.
2638 if (np == 0 && PrevStage == LastStageNum &&
2639 (StageScheduled != 0 || LoopValStage != 0) &&
2640 VRMap[PrevStage - StageDiffAdj].count(LoopVal))
2641 PhiOp2 = VRMap[PrevStage - StageDiffAdj][LoopVal];
2642 // Use the value defined by the Phi. We add one because we switch
2643 // from looking at the loop value to the Phi definition.
2644 else if (np > 0 && PrevStage == LastStageNum &&
2645 VRMap[PrevStage - np + 1].count(Def))
2646 PhiOp2 = VRMap[PrevStage - np + 1][Def];
2647 // Use the loop value defined in the kernel.
2648 else if ((unsigned)LoopValStage + StageDiffAdj > PrologStage + 1 &&
2649 VRMap[PrevStage - StageDiffAdj - np].count(LoopVal))
2650 PhiOp2 = VRMap[PrevStage - StageDiffAdj - np][LoopVal];
2651 // Use the value defined by the Phi, unless we're generating the first
2652 // epilog and the Phi refers to a Phi in a different stage.
2653 else if (VRMap[PrevStage - np].count(Def) &&
2654 (!LoopDefIsPhi || PrevStage != LastStageNum))
2655 PhiOp2 = VRMap[PrevStage - np][Def];
2658 // Check if we can reuse an existing Phi. This occurs when a Phi
2659 // references another Phi, and the other Phi is scheduled in an
2660 // earlier stage. We can try to reuse an existing Phi up until the last
2661 // stage of the current Phi.
2662 if (LoopDefIsPhi && (int)PrologStage >= StageScheduled) {
2663 int LVNumStages = Schedule.getStagesForPhi(LoopVal);
2664 int StageDiff = (StageScheduled - LoopValStage);
2665 LVNumStages -= StageDiff;
2666 if (LVNumStages > (int)np) {
2668 unsigned ReuseStage = CurStageNum;
2669 if (Schedule.isLoopCarried(this, *PhiInst))
2670 ReuseStage -= LVNumStages;
2671 // Check if the Phi to reuse has been generated yet. If not, then
2672 // there is nothing to reuse.
2673 if (VRMap[ReuseStage].count(LoopVal)) {
2674 NewReg = VRMap[ReuseStage][LoopVal];
2676 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2677 &*BBI, Def, NewReg);
2678 // Update the map with the new Phi name.
2679 VRMap[CurStageNum - np][Def] = NewReg;
2681 if (VRMap[LastStageNum - np - 1].count(LoopVal))
2682 PhiOp2 = VRMap[LastStageNum - np - 1][LoopVal];
2684 if (IsLast && np == NumPhis - 1)
2685 replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS);
2688 } else if (InKernel && StageDiff > 0 &&
2689 VRMap[CurStageNum - StageDiff - np].count(LoopVal))
2690 PhiOp2 = VRMap[CurStageNum - StageDiff - np][LoopVal];
2693 const TargetRegisterClass *RC = MRI.getRegClass(Def);
2694 NewReg = MRI.createVirtualRegister(RC);
2696 MachineInstrBuilder NewPhi =
2697 BuildMI(*NewBB, NewBB->getFirstNonPHI(), DebugLoc(),
2698 TII->get(TargetOpcode::PHI), NewReg);
2699 NewPhi.addReg(PhiOp1).addMBB(BB1);
2700 NewPhi.addReg(PhiOp2).addMBB(BB2);
2702 InstrMap[NewPhi] = &*BBI;
2704 // We define the Phis after creating the new pipelined code, so
2705 // we need to rename the Phi values in scheduled instructions.
2707 unsigned PrevReg = 0;
2708 if (InKernel && VRMap[PrevStage - np].count(LoopVal))
2709 PrevReg = VRMap[PrevStage - np][LoopVal];
2710 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np, &*BBI,
2711 Def, NewReg, PrevReg);
2712 // If the Phi has been scheduled, use the new name for rewriting.
2713 if (VRMap[CurStageNum - np].count(Def)) {
2714 unsigned R = VRMap[CurStageNum - np][Def];
2715 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np, &*BBI,
2719 // Check if we need to rename any uses that occurs after the loop. The
2720 // register to replace depends on whether the Phi is scheduled in the
2722 if (IsLast && np == NumPhis - 1)
2723 replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS);
2725 // In the kernel, a dependent Phi uses the value from this Phi.
2729 // Update the map with the new Phi name.
2730 VRMap[CurStageNum - np][Def] = NewReg;
2733 while (NumPhis++ < NumStages) {
2734 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, NumPhis,
2735 &*BBI, Def, NewReg, 0);
2738 // Check if we need to rename a Phi that has been eliminated due to
2740 if (NumStages == 0 && IsLast && VRMap[CurStageNum].count(LoopVal))
2741 replaceRegUsesAfterLoop(Def, VRMap[CurStageNum][LoopVal], BB, MRI, LIS);
2745 /// Generate Phis for the specified block in the generated pipelined code.
2746 /// These are new Phis needed because the definition is scheduled after the
2747 /// use in the pipelened sequence.
2748 void SwingSchedulerDAG::generatePhis(
2749 MachineBasicBlock *NewBB, MachineBasicBlock *BB1, MachineBasicBlock *BB2,
2750 MachineBasicBlock *KernelBB, SMSchedule &Schedule, ValueMapTy *VRMap,
2751 InstrMapTy &InstrMap, unsigned LastStageNum, unsigned CurStageNum,
2753 // Compute the stage number that contains the initial Phi value, and
2754 // the Phi from the previous stage.
2755 unsigned PrologStage = 0;
2756 unsigned PrevStage = 0;
2757 unsigned StageDiff = CurStageNum - LastStageNum;
2758 bool InKernel = (StageDiff == 0);
2760 PrologStage = LastStageNum - 1;
2761 PrevStage = CurStageNum;
2763 PrologStage = LastStageNum - StageDiff;
2764 PrevStage = LastStageNum + StageDiff - 1;
2767 for (MachineBasicBlock::iterator BBI = BB->getFirstNonPHI(),
2768 BBE = BB->instr_end();
2769 BBI != BBE; ++BBI) {
2770 for (unsigned i = 0, e = BBI->getNumOperands(); i != e; ++i) {
2771 MachineOperand &MO = BBI->getOperand(i);
2772 if (!MO.isReg() || !MO.isDef() ||
2773 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
2776 int StageScheduled = Schedule.stageScheduled(getSUnit(&*BBI));
2777 assert(StageScheduled != -1 && "Expecting scheduled instruction.");
2778 unsigned Def = MO.getReg();
2779 unsigned NumPhis = Schedule.getStagesForReg(Def, CurStageNum);
2780 // An instruction scheduled in stage 0 and is used after the loop
2781 // requires a phi in the epilog for the last definition from either
2782 // the kernel or prolog.
2783 if (!InKernel && NumPhis == 0 && StageScheduled == 0 &&
2784 hasUseAfterLoop(Def, BB, MRI))
2786 if (!InKernel && (unsigned)StageScheduled > PrologStage)
2789 unsigned PhiOp2 = VRMap[PrevStage][Def];
2790 if (MachineInstr *InstOp2 = MRI.getVRegDef(PhiOp2))
2791 if (InstOp2->isPHI() && InstOp2->getParent() == NewBB)
2792 PhiOp2 = getLoopPhiReg(*InstOp2, BB2);
2793 // The number of Phis can't exceed the number of prolog stages. The
2794 // prolog stage number is zero based.
2795 if (NumPhis > PrologStage + 1 - StageScheduled)
2796 NumPhis = PrologStage + 1 - StageScheduled;
2797 for (unsigned np = 0; np < NumPhis; ++np) {
2798 unsigned PhiOp1 = VRMap[PrologStage][Def];
2799 if (np <= PrologStage)
2800 PhiOp1 = VRMap[PrologStage - np][Def];
2801 if (MachineInstr *InstOp1 = MRI.getVRegDef(PhiOp1)) {
2802 if (InstOp1->isPHI() && InstOp1->getParent() == KernelBB)
2803 PhiOp1 = getInitPhiReg(*InstOp1, KernelBB);
2804 if (InstOp1->isPHI() && InstOp1->getParent() == NewBB)
2805 PhiOp1 = getInitPhiReg(*InstOp1, NewBB);
2808 PhiOp2 = VRMap[PrevStage - np][Def];
2810 const TargetRegisterClass *RC = MRI.getRegClass(Def);
2811 unsigned NewReg = MRI.createVirtualRegister(RC);
2813 MachineInstrBuilder NewPhi =
2814 BuildMI(*NewBB, NewBB->getFirstNonPHI(), DebugLoc(),
2815 TII->get(TargetOpcode::PHI), NewReg);
2816 NewPhi.addReg(PhiOp1).addMBB(BB1);
2817 NewPhi.addReg(PhiOp2).addMBB(BB2);
2819 InstrMap[NewPhi] = &*BBI;
2821 // Rewrite uses and update the map. The actions depend upon whether
2822 // we generating code for the kernel or epilog blocks.
2824 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2825 &*BBI, PhiOp1, NewReg);
2826 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2827 &*BBI, PhiOp2, NewReg);
2830 VRMap[PrevStage - np - 1][Def] = NewReg;
2832 VRMap[CurStageNum - np][Def] = NewReg;
2833 if (np == NumPhis - 1)
2834 rewriteScheduledInstr(NewBB, Schedule, InstrMap, CurStageNum, np,
2835 &*BBI, Def, NewReg);
2837 if (IsLast && np == NumPhis - 1)
2838 replaceRegUsesAfterLoop(Def, NewReg, BB, MRI, LIS);
2844 /// Remove instructions that generate values with no uses.
2845 /// Typically, these are induction variable operations that generate values
2846 /// used in the loop itself. A dead instruction has a definition with
2847 /// no uses, or uses that occur in the original loop only.
2848 void SwingSchedulerDAG::removeDeadInstructions(MachineBasicBlock *KernelBB,
2849 MBBVectorTy &EpilogBBs) {
2850 // For each epilog block, check that the value defined by each instruction
2851 // is used. If not, delete it.
2852 for (MBBVectorTy::reverse_iterator MBB = EpilogBBs.rbegin(),
2853 MBE = EpilogBBs.rend();
2855 for (MachineBasicBlock::reverse_instr_iterator MI = (*MBB)->instr_rbegin(),
2856 ME = (*MBB)->instr_rend();
2858 // From DeadMachineInstructionElem. Don't delete inline assembly.
2859 if (MI->isInlineAsm()) {
2863 bool SawStore = false;
2864 // Check if it's safe to remove the instruction due to side effects.
2865 // We can, and want to, remove Phis here.
2866 if (!MI->isSafeToMove(nullptr, SawStore) && !MI->isPHI()) {
2871 for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
2872 MOE = MI->operands_end();
2873 MOI != MOE; ++MOI) {
2874 if (!MOI->isReg() || !MOI->isDef())
2876 unsigned reg = MOI->getReg();
2877 unsigned realUses = 0;
2878 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(reg),
2881 // Check if there are any uses that occur only in the original
2882 // loop. If so, that's not a real use.
2883 if (UI->getParent()->getParent() != BB) {
2894 MI++->eraseFromParent();
2899 // In the kernel block, check if we can remove a Phi that generates a value
2900 // used in an instruction removed in the epilog block.
2901 for (MachineBasicBlock::iterator BBI = KernelBB->instr_begin(),
2902 BBE = KernelBB->getFirstNonPHI();
2904 MachineInstr *MI = &*BBI;
2906 unsigned reg = MI->getOperand(0).getReg();
2907 if (MRI.use_begin(reg) == MRI.use_end()) {
2908 MI->eraseFromParent();
2913 /// For loop carried definitions, we split the lifetime of a virtual register
2914 /// that has uses past the definition in the next iteration. A copy with a new
2915 /// virtual register is inserted before the definition, which helps with
2916 /// generating a better register assignment.
2918 /// v1 = phi(a, v2) v1 = phi(a, v2)
2919 /// v2 = phi(b, v3) v2 = phi(b, v3)
2920 /// v3 = .. v4 = copy v1
2923 void SwingSchedulerDAG::splitLifetimes(MachineBasicBlock *KernelBB,
2924 MBBVectorTy &EpilogBBs,
2925 SMSchedule &Schedule) {
2926 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2927 for (MachineBasicBlock::iterator BBI = KernelBB->instr_begin(),
2928 BBF = KernelBB->getFirstNonPHI();
2929 BBI != BBF; ++BBI) {
2930 unsigned Def = BBI->getOperand(0).getReg();
2931 // Check for any Phi definition that used as an operand of another Phi
2932 // in the same block.
2933 for (MachineRegisterInfo::use_instr_iterator I = MRI.use_instr_begin(Def),
2934 E = MRI.use_instr_end();
2936 if (I->isPHI() && I->getParent() == KernelBB) {
2937 // Get the loop carried definition.
2938 unsigned LCDef = getLoopPhiReg(*BBI, KernelBB);
2941 MachineInstr *MI = MRI.getVRegDef(LCDef);
2942 if (!MI || MI->getParent() != KernelBB || MI->isPHI())
2944 // Search through the rest of the block looking for uses of the Phi
2945 // definition. If one occurs, then split the lifetime.
2946 unsigned SplitReg = 0;
2947 for (auto &BBJ : make_range(MachineBasicBlock::instr_iterator(MI),
2948 KernelBB->instr_end()))
2949 if (BBJ.readsRegister(Def)) {
2950 // We split the lifetime when we find the first use.
2951 if (SplitReg == 0) {
2952 SplitReg = MRI.createVirtualRegister(MRI.getRegClass(Def));
2953 BuildMI(*KernelBB, MI, MI->getDebugLoc(),
2954 TII->get(TargetOpcode::COPY), SplitReg)
2957 BBJ.substituteRegister(Def, SplitReg, 0, *TRI);
2961 // Search through each of the epilog blocks for any uses to be renamed.
2962 for (auto &Epilog : EpilogBBs)
2963 for (auto &I : *Epilog)
2964 if (I.readsRegister(Def))
2965 I.substituteRegister(Def, SplitReg, 0, *TRI);
2972 /// Remove the incoming block from the Phis in a basic block.
2973 static void removePhis(MachineBasicBlock *BB, MachineBasicBlock *Incoming) {
2974 for (MachineInstr &MI : *BB) {
2977 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2)
2978 if (MI.getOperand(i + 1).getMBB() == Incoming) {
2979 MI.RemoveOperand(i + 1);
2980 MI.RemoveOperand(i);
2986 /// Create branches from each prolog basic block to the appropriate epilog
2987 /// block. These edges are needed if the loop ends before reaching the
2989 void SwingSchedulerDAG::addBranches(MBBVectorTy &PrologBBs,
2990 MachineBasicBlock *KernelBB,
2991 MBBVectorTy &EpilogBBs,
2992 SMSchedule &Schedule, ValueMapTy *VRMap) {
2993 assert(PrologBBs.size() == EpilogBBs.size() && "Prolog/Epilog mismatch");
2994 MachineInstr *IndVar = Pass.LI.LoopInductionVar;
2995 MachineInstr *Cmp = Pass.LI.LoopCompare;
2996 MachineBasicBlock *LastPro = KernelBB;
2997 MachineBasicBlock *LastEpi = KernelBB;
2999 // Start from the blocks connected to the kernel and work "out"
3000 // to the first prolog and the last epilog blocks.
3001 SmallVector<MachineInstr *, 4> PrevInsts;
3002 unsigned MaxIter = PrologBBs.size() - 1;
3003 unsigned LC = UINT_MAX;
3004 unsigned LCMin = UINT_MAX;
3005 for (unsigned i = 0, j = MaxIter; i <= MaxIter; ++i, --j) {
3006 // Add branches to the prolog that go to the corresponding
3007 // epilog, and the fall-thru prolog/kernel block.
3008 MachineBasicBlock *Prolog = PrologBBs[j];
3009 MachineBasicBlock *Epilog = EpilogBBs[i];
3010 // We've executed one iteration, so decrement the loop count and check for
3012 SmallVector<MachineOperand, 4> Cond;
3013 // Check if the LOOP0 has already been removed. If so, then there is no need
3014 // to reduce the trip count.
3016 LC = TII->reduceLoopCount(*Prolog, IndVar, *Cmp, Cond, PrevInsts, j,
3019 // Record the value of the first trip count, which is used to determine if
3020 // branches and blocks can be removed for constant trip counts.
3021 if (LCMin == UINT_MAX)
3024 unsigned numAdded = 0;
3025 if (TargetRegisterInfo::isVirtualRegister(LC)) {
3026 Prolog->addSuccessor(Epilog);
3027 numAdded = TII->insertBranch(*Prolog, Epilog, LastPro, Cond, DebugLoc());
3028 } else if (j >= LCMin) {
3029 Prolog->addSuccessor(Epilog);
3030 Prolog->removeSuccessor(LastPro);
3031 LastEpi->removeSuccessor(Epilog);
3032 numAdded = TII->insertBranch(*Prolog, Epilog, nullptr, Cond, DebugLoc());
3033 removePhis(Epilog, LastEpi);
3034 // Remove the blocks that are no longer referenced.
3035 if (LastPro != LastEpi) {
3037 LastEpi->eraseFromParent();
3040 LastPro->eraseFromParent();
3042 numAdded = TII->insertBranch(*Prolog, LastPro, nullptr, Cond, DebugLoc());
3043 removePhis(Epilog, Prolog);
3047 for (MachineBasicBlock::reverse_instr_iterator I = Prolog->instr_rbegin(),
3048 E = Prolog->instr_rend();
3049 I != E && numAdded > 0; ++I, --numAdded)
3050 updateInstruction(&*I, false, j, 0, Schedule, VRMap);
3054 /// Return true if we can compute the amount the instruction changes
3055 /// during each iteration. Set Delta to the amount of the change.
3056 bool SwingSchedulerDAG::computeDelta(MachineInstr &MI, unsigned &Delta) {
3057 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
3060 if (!TII->getMemOpBaseRegImmOfs(MI, BaseReg, Offset, TRI))
3063 MachineRegisterInfo &MRI = MF.getRegInfo();
3064 // Check if there is a Phi. If so, get the definition in the loop.
3065 MachineInstr *BaseDef = MRI.getVRegDef(BaseReg);
3066 if (BaseDef && BaseDef->isPHI()) {
3067 BaseReg = getLoopPhiReg(*BaseDef, MI.getParent());
3068 BaseDef = MRI.getVRegDef(BaseReg);
3074 if (!TII->getIncrementValue(*BaseDef, D) && D >= 0)
3081 /// Update the memory operand with a new offset when the pipeliner
3082 /// generates a new copy of the instruction that refers to a
3083 /// different memory location.
3084 void SwingSchedulerDAG::updateMemOperands(MachineInstr &NewMI,
3085 MachineInstr &OldMI, unsigned Num) {
3088 // If the instruction has memory operands, then adjust the offset
3089 // when the instruction appears in different stages.
3090 unsigned NumRefs = NewMI.memoperands_end() - NewMI.memoperands_begin();
3093 MachineInstr::mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NumRefs);
3095 for (MachineMemOperand *MMO : NewMI.memoperands()) {
3096 if (MMO->isVolatile() || (MMO->isInvariant() && MMO->isDereferenceable()) ||
3097 (!MMO->getValue())) {
3098 NewMemRefs[Refs++] = MMO;
3102 if (computeDelta(OldMI, Delta)) {
3103 int64_t AdjOffset = Delta * Num;
3104 NewMemRefs[Refs++] =
3105 MF.getMachineMemOperand(MMO, AdjOffset, MMO->getSize());
3107 NewMemRefs[Refs++] = MF.getMachineMemOperand(MMO, 0, UINT64_MAX);
3109 NewMI.setMemRefs(NewMemRefs, NewMemRefs + NumRefs);
3112 /// Clone the instruction for the new pipelined loop and update the
3113 /// memory operands, if needed.
3114 MachineInstr *SwingSchedulerDAG::cloneInstr(MachineInstr *OldMI,
3115 unsigned CurStageNum,
3116 unsigned InstStageNum) {
3117 MachineInstr *NewMI = MF.CloneMachineInstr(OldMI);
3118 // Check for tied operands in inline asm instructions. This should be handled
3119 // elsewhere, but I'm not sure of the best solution.
3120 if (OldMI->isInlineAsm())
3121 for (unsigned i = 0, e = OldMI->getNumOperands(); i != e; ++i) {
3122 const auto &MO = OldMI->getOperand(i);
3123 if (MO.isReg() && MO.isUse())
3126 if (OldMI->isRegTiedToUseOperand(i, &UseIdx))
3127 NewMI->tieOperands(i, UseIdx);
3129 updateMemOperands(*NewMI, *OldMI, CurStageNum - InstStageNum);
3133 /// Clone the instruction for the new pipelined loop. If needed, this
3134 /// function updates the instruction using the values saved in the
3135 /// InstrChanges structure.
3136 MachineInstr *SwingSchedulerDAG::cloneAndChangeInstr(MachineInstr *OldMI,
3137 unsigned CurStageNum,
3138 unsigned InstStageNum,
3139 SMSchedule &Schedule) {
3140 MachineInstr *NewMI = MF.CloneMachineInstr(OldMI);
3141 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
3142 InstrChanges.find(getSUnit(OldMI));
3143 if (It != InstrChanges.end()) {
3144 std::pair<unsigned, int64_t> RegAndOffset = It->second;
3145 unsigned BasePos, OffsetPos;
3146 if (!TII->getBaseAndOffsetPosition(*OldMI, BasePos, OffsetPos))
3148 int64_t NewOffset = OldMI->getOperand(OffsetPos).getImm();
3149 MachineInstr *LoopDef = findDefInLoop(RegAndOffset.first);
3150 if (Schedule.stageScheduled(getSUnit(LoopDef)) > (signed)InstStageNum)
3151 NewOffset += RegAndOffset.second * (CurStageNum - InstStageNum);
3152 NewMI->getOperand(OffsetPos).setImm(NewOffset);
3154 updateMemOperands(*NewMI, *OldMI, CurStageNum - InstStageNum);
3158 /// Update the machine instruction with new virtual registers. This
3159 /// function may change the defintions and/or uses.
3160 void SwingSchedulerDAG::updateInstruction(MachineInstr *NewMI, bool LastDef,
3161 unsigned CurStageNum,
3162 unsigned InstrStageNum,
3163 SMSchedule &Schedule,
3164 ValueMapTy *VRMap) {
3165 for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
3166 MachineOperand &MO = NewMI->getOperand(i);
3167 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
3169 unsigned reg = MO.getReg();
3171 // Create a new virtual register for the definition.
3172 const TargetRegisterClass *RC = MRI.getRegClass(reg);
3173 unsigned NewReg = MRI.createVirtualRegister(RC);
3175 VRMap[CurStageNum][reg] = NewReg;
3177 replaceRegUsesAfterLoop(reg, NewReg, BB, MRI, LIS);
3178 } else if (MO.isUse()) {
3179 MachineInstr *Def = MRI.getVRegDef(reg);
3180 // Compute the stage that contains the last definition for instruction.
3181 int DefStageNum = Schedule.stageScheduled(getSUnit(Def));
3182 unsigned StageNum = CurStageNum;
3183 if (DefStageNum != -1 && (int)InstrStageNum > DefStageNum) {
3184 // Compute the difference in stages between the defintion and the use.
3185 unsigned StageDiff = (InstrStageNum - DefStageNum);
3186 // Make an adjustment to get the last definition.
3187 StageNum -= StageDiff;
3189 if (VRMap[StageNum].count(reg))
3190 MO.setReg(VRMap[StageNum][reg]);
3195 /// Return the instruction in the loop that defines the register.
3196 /// If the definition is a Phi, then follow the Phi operand to
3197 /// the instruction in the loop.
3198 MachineInstr *SwingSchedulerDAG::findDefInLoop(unsigned Reg) {
3199 SmallPtrSet<MachineInstr *, 8> Visited;
3200 MachineInstr *Def = MRI.getVRegDef(Reg);
3201 while (Def->isPHI()) {
3202 if (!Visited.insert(Def).second)
3204 for (unsigned i = 1, e = Def->getNumOperands(); i < e; i += 2)
3205 if (Def->getOperand(i + 1).getMBB() == BB) {
3206 Def = MRI.getVRegDef(Def->getOperand(i).getReg());
3213 /// Return the new name for the value from the previous stage.
3214 unsigned SwingSchedulerDAG::getPrevMapVal(unsigned StageNum, unsigned PhiStage,
3215 unsigned LoopVal, unsigned LoopStage,
3217 MachineBasicBlock *BB) {
3218 unsigned PrevVal = 0;
3219 if (StageNum > PhiStage) {
3220 MachineInstr *LoopInst = MRI.getVRegDef(LoopVal);
3221 if (PhiStage == LoopStage && VRMap[StageNum - 1].count(LoopVal))
3222 // The name is defined in the previous stage.
3223 PrevVal = VRMap[StageNum - 1][LoopVal];
3224 else if (VRMap[StageNum].count(LoopVal))
3225 // The previous name is defined in the current stage when the instruction
3226 // order is swapped.
3227 PrevVal = VRMap[StageNum][LoopVal];
3228 else if (!LoopInst->isPHI() || LoopInst->getParent() != BB)
3229 // The loop value hasn't yet been scheduled.
3231 else if (StageNum == PhiStage + 1)
3232 // The loop value is another phi, which has not been scheduled.
3233 PrevVal = getInitPhiReg(*LoopInst, BB);
3234 else if (StageNum > PhiStage + 1 && LoopInst->getParent() == BB)
3235 // The loop value is another phi, which has been scheduled.
3237 getPrevMapVal(StageNum - 1, PhiStage, getLoopPhiReg(*LoopInst, BB),
3238 LoopStage, VRMap, BB);
3243 /// Rewrite the Phi values in the specified block to use the mappings
3244 /// from the initial operand. Once the Phi is scheduled, we switch
3245 /// to using the loop value instead of the Phi value, so those names
3246 /// do not need to be rewritten.
3247 void SwingSchedulerDAG::rewritePhiValues(MachineBasicBlock *NewBB,
3249 SMSchedule &Schedule,
3251 InstrMapTy &InstrMap) {
3252 for (MachineBasicBlock::iterator BBI = BB->instr_begin(),
3253 BBE = BB->getFirstNonPHI();
3254 BBI != BBE; ++BBI) {
3255 unsigned InitVal = 0;
3256 unsigned LoopVal = 0;
3257 getPhiRegs(*BBI, BB, InitVal, LoopVal);
3258 unsigned PhiDef = BBI->getOperand(0).getReg();
3261 (unsigned)Schedule.stageScheduled(getSUnit(MRI.getVRegDef(PhiDef)));
3262 unsigned LoopStage =
3263 (unsigned)Schedule.stageScheduled(getSUnit(MRI.getVRegDef(LoopVal)));
3264 unsigned NumPhis = Schedule.getStagesForPhi(PhiDef);
3265 if (NumPhis > StageNum)
3267 for (unsigned np = 0; np <= NumPhis; ++np) {
3269 getPrevMapVal(StageNum - np, PhiStage, LoopVal, LoopStage, VRMap, BB);
3272 rewriteScheduledInstr(NewBB, Schedule, InstrMap, StageNum - np, np, &*BBI,
3278 /// Rewrite a previously scheduled instruction to use the register value
3279 /// from the new instruction. Make sure the instruction occurs in the
3280 /// basic block, and we don't change the uses in the new instruction.
3281 void SwingSchedulerDAG::rewriteScheduledInstr(
3282 MachineBasicBlock *BB, SMSchedule &Schedule, InstrMapTy &InstrMap,
3283 unsigned CurStageNum, unsigned PhiNum, MachineInstr *Phi, unsigned OldReg,
3284 unsigned NewReg, unsigned PrevReg) {
3285 bool InProlog = (CurStageNum < Schedule.getMaxStageCount());
3286 int StagePhi = Schedule.stageScheduled(getSUnit(Phi)) + PhiNum;
3287 // Rewrite uses that have been scheduled already to use the new
3289 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(OldReg),
3292 MachineOperand &UseOp = *UI;
3293 MachineInstr *UseMI = UseOp.getParent();
3295 if (UseMI->getParent() != BB)
3297 if (UseMI->isPHI()) {
3298 if (!Phi->isPHI() && UseMI->getOperand(0).getReg() == NewReg)
3300 if (getLoopPhiReg(*UseMI, BB) != OldReg)
3303 InstrMapTy::iterator OrigInstr = InstrMap.find(UseMI);
3304 assert(OrigInstr != InstrMap.end() && "Instruction not scheduled.");
3305 SUnit *OrigMISU = getSUnit(OrigInstr->second);
3306 int StageSched = Schedule.stageScheduled(OrigMISU);
3307 int CycleSched = Schedule.cycleScheduled(OrigMISU);
3308 unsigned ReplaceReg = 0;
3309 // This is the stage for the scheduled instruction.
3310 if (StagePhi == StageSched && Phi->isPHI()) {
3311 int CyclePhi = Schedule.cycleScheduled(getSUnit(Phi));
3312 if (PrevReg && InProlog)
3313 ReplaceReg = PrevReg;
3314 else if (PrevReg && !Schedule.isLoopCarried(this, *Phi) &&
3315 (CyclePhi <= CycleSched || OrigMISU->getInstr()->isPHI()))
3316 ReplaceReg = PrevReg;
3318 ReplaceReg = NewReg;
3320 // The scheduled instruction occurs before the scheduled Phi, and the
3321 // Phi is not loop carried.
3322 if (!InProlog && StagePhi + 1 == StageSched &&
3323 !Schedule.isLoopCarried(this, *Phi))
3324 ReplaceReg = NewReg;
3325 if (StagePhi > StageSched && Phi->isPHI())
3326 ReplaceReg = NewReg;
3327 if (!InProlog && !Phi->isPHI() && StagePhi < StageSched)
3328 ReplaceReg = NewReg;
3330 MRI.constrainRegClass(ReplaceReg, MRI.getRegClass(OldReg));
3331 UseOp.setReg(ReplaceReg);
3336 /// Check if we can change the instruction to use an offset value from the
3337 /// previous iteration. If so, return true and set the base and offset values
3338 /// so that we can rewrite the load, if necessary.
3339 /// v1 = Phi(v0, v3)
3341 /// v3 = post_store v1, 4, x
3342 /// This function enables the load to be rewritten as v2 = load v3, 4.
3343 bool SwingSchedulerDAG::canUseLastOffsetValue(MachineInstr *MI,
3345 unsigned &OffsetPos,
3348 // Get the load instruction.
3349 if (TII->isPostIncrement(*MI))
3351 unsigned BasePosLd, OffsetPosLd;
3352 if (!TII->getBaseAndOffsetPosition(*MI, BasePosLd, OffsetPosLd))
3354 unsigned BaseReg = MI->getOperand(BasePosLd).getReg();
3356 // Look for the Phi instruction.
3357 MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
3358 MachineInstr *Phi = MRI.getVRegDef(BaseReg);
3359 if (!Phi || !Phi->isPHI())
3361 // Get the register defined in the loop block.
3362 unsigned PrevReg = getLoopPhiReg(*Phi, MI->getParent());
3366 // Check for the post-increment load/store instruction.
3367 MachineInstr *PrevDef = MRI.getVRegDef(PrevReg);
3368 if (!PrevDef || PrevDef == MI)
3371 if (!TII->isPostIncrement(*PrevDef))
3374 unsigned BasePos1 = 0, OffsetPos1 = 0;
3375 if (!TII->getBaseAndOffsetPosition(*PrevDef, BasePos1, OffsetPos1))
3378 // Make sure offset values are both positive or both negative.
3379 int64_t LoadOffset = MI->getOperand(OffsetPosLd).getImm();
3380 int64_t StoreOffset = PrevDef->getOperand(OffsetPos1).getImm();
3381 if ((LoadOffset >= 0) != (StoreOffset >= 0))
3384 // Set the return value once we determine that we return true.
3385 BasePos = BasePosLd;
3386 OffsetPos = OffsetPosLd;
3388 Offset = StoreOffset;
3392 /// Apply changes to the instruction if needed. The changes are need
3393 /// to improve the scheduling and depend up on the final schedule.
3394 void SwingSchedulerDAG::applyInstrChange(MachineInstr *MI,
3395 SMSchedule &Schedule) {
3396 SUnit *SU = getSUnit(MI);
3397 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
3398 InstrChanges.find(SU);
3399 if (It != InstrChanges.end()) {
3400 std::pair<unsigned, int64_t> RegAndOffset = It->second;
3401 unsigned BasePos, OffsetPos;
3402 if (!TII->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos))
3404 unsigned BaseReg = MI->getOperand(BasePos).getReg();
3405 MachineInstr *LoopDef = findDefInLoop(BaseReg);
3406 int DefStageNum = Schedule.stageScheduled(getSUnit(LoopDef));
3407 int DefCycleNum = Schedule.cycleScheduled(getSUnit(LoopDef));
3408 int BaseStageNum = Schedule.stageScheduled(SU);
3409 int BaseCycleNum = Schedule.cycleScheduled(SU);
3410 if (BaseStageNum < DefStageNum) {
3411 MachineInstr *NewMI = MF.CloneMachineInstr(MI);
3412 int OffsetDiff = DefStageNum - BaseStageNum;
3413 if (DefCycleNum < BaseCycleNum) {
3414 NewMI->getOperand(BasePos).setReg(RegAndOffset.first);
3419 MI->getOperand(OffsetPos).getImm() + RegAndOffset.second * OffsetDiff;
3420 NewMI->getOperand(OffsetPos).setImm(NewOffset);
3421 SU->setInstr(NewMI);
3422 MISUnitMap[NewMI] = SU;
3423 NewMIs.insert(NewMI);
3428 /// Return true for an order dependence that is loop carried potentially.
3429 /// An order dependence is loop carried if the destination defines a value
3430 /// that may be used by the source in a subsequent iteration.
3431 bool SwingSchedulerDAG::isLoopCarriedOrder(SUnit *Source, const SDep &Dep,
3433 if (!isOrder(Source, Dep) || Dep.isArtificial())
3436 if (!SwpPruneLoopCarried)
3439 MachineInstr *SI = Source->getInstr();
3440 MachineInstr *DI = Dep.getSUnit()->getInstr();
3443 assert(SI != nullptr && DI != nullptr && "Expecting SUnit with an MI.");
3445 // Assume ordered loads and stores may have a loop carried dependence.
3446 if (SI->hasUnmodeledSideEffects() || DI->hasUnmodeledSideEffects() ||
3447 SI->hasOrderedMemoryRef() || DI->hasOrderedMemoryRef())
3450 // Only chain dependences between a load and store can be loop carried.
3451 if (!DI->mayStore() || !SI->mayLoad())
3454 unsigned DeltaS, DeltaD;
3455 if (!computeDelta(*SI, DeltaS) || !computeDelta(*DI, DeltaD))
3458 unsigned BaseRegS, BaseRegD;
3459 int64_t OffsetS, OffsetD;
3460 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
3461 if (!TII->getMemOpBaseRegImmOfs(*SI, BaseRegS, OffsetS, TRI) ||
3462 !TII->getMemOpBaseRegImmOfs(*DI, BaseRegD, OffsetD, TRI))
3465 if (BaseRegS != BaseRegD)
3468 uint64_t AccessSizeS = (*SI->memoperands_begin())->getSize();
3469 uint64_t AccessSizeD = (*DI->memoperands_begin())->getSize();
3471 // This is the main test, which checks the offset values and the loop
3472 // increment value to determine if the accesses may be loop carried.
3473 if (OffsetS >= OffsetD)
3474 return OffsetS + AccessSizeS > DeltaS;
3476 return OffsetD + AccessSizeD > DeltaD;
3481 void SwingSchedulerDAG::postprocessDAG() {
3482 for (auto &M : Mutations)
3486 /// Try to schedule the node at the specified StartCycle and continue
3487 /// until the node is schedule or the EndCycle is reached. This function
3488 /// returns true if the node is scheduled. This routine may search either
3489 /// forward or backward for a place to insert the instruction based upon
3490 /// the relative values of StartCycle and EndCycle.
3491 bool SMSchedule::insert(SUnit *SU, int StartCycle, int EndCycle, int II) {
3492 bool forward = true;
3493 if (StartCycle > EndCycle)
3496 // The terminating condition depends on the direction.
3497 int termCycle = forward ? EndCycle + 1 : EndCycle - 1;
3498 for (int curCycle = StartCycle; curCycle != termCycle;
3499 forward ? ++curCycle : --curCycle) {
3501 // Add the already scheduled instructions at the specified cycle to the DFA.
3502 Resources->clearResources();
3503 for (int checkCycle = FirstCycle + ((curCycle - FirstCycle) % II);
3504 checkCycle <= LastCycle; checkCycle += II) {
3505 std::deque<SUnit *> &cycleInstrs = ScheduledInstrs[checkCycle];
3507 for (std::deque<SUnit *>::iterator I = cycleInstrs.begin(),
3508 E = cycleInstrs.end();
3510 if (ST.getInstrInfo()->isZeroCost((*I)->getInstr()->getOpcode()))
3512 assert(Resources->canReserveResources(*(*I)->getInstr()) &&
3513 "These instructions have already been scheduled.");
3514 Resources->reserveResources(*(*I)->getInstr());
3517 if (ST.getInstrInfo()->isZeroCost(SU->getInstr()->getOpcode()) ||
3518 Resources->canReserveResources(*SU->getInstr())) {
3520 dbgs() << "\tinsert at cycle " << curCycle << " ";
3521 SU->getInstr()->dump();
3524 ScheduledInstrs[curCycle].push_back(SU);
3525 InstrToCycle.insert(std::make_pair(SU, curCycle));
3526 if (curCycle > LastCycle)
3527 LastCycle = curCycle;
3528 if (curCycle < FirstCycle)
3529 FirstCycle = curCycle;
3533 dbgs() << "\tfailed to insert at cycle " << curCycle << " ";
3534 SU->getInstr()->dump();
3540 // Return the cycle of the earliest scheduled instruction in the chain.
3541 int SMSchedule::earliestCycleInChain(const SDep &Dep) {
3542 SmallPtrSet<SUnit *, 8> Visited;
3543 SmallVector<SDep, 8> Worklist;
3544 Worklist.push_back(Dep);
3545 int EarlyCycle = INT_MAX;
3546 while (!Worklist.empty()) {
3547 const SDep &Cur = Worklist.pop_back_val();
3548 SUnit *PrevSU = Cur.getSUnit();
3549 if (Visited.count(PrevSU))
3551 std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(PrevSU);
3552 if (it == InstrToCycle.end())
3554 EarlyCycle = std::min(EarlyCycle, it->second);
3555 for (const auto &PI : PrevSU->Preds)
3556 if (SwingSchedulerDAG::isOrder(PrevSU, PI))
3557 Worklist.push_back(PI);
3558 Visited.insert(PrevSU);
3563 // Return the cycle of the latest scheduled instruction in the chain.
3564 int SMSchedule::latestCycleInChain(const SDep &Dep) {
3565 SmallPtrSet<SUnit *, 8> Visited;
3566 SmallVector<SDep, 8> Worklist;
3567 Worklist.push_back(Dep);
3568 int LateCycle = INT_MIN;
3569 while (!Worklist.empty()) {
3570 const SDep &Cur = Worklist.pop_back_val();
3571 SUnit *SuccSU = Cur.getSUnit();
3572 if (Visited.count(SuccSU))
3574 std::map<SUnit *, int>::const_iterator it = InstrToCycle.find(SuccSU);
3575 if (it == InstrToCycle.end())
3577 LateCycle = std::max(LateCycle, it->second);
3578 for (const auto &SI : SuccSU->Succs)
3579 if (SwingSchedulerDAG::isOrder(SuccSU, SI))
3580 Worklist.push_back(SI);
3581 Visited.insert(SuccSU);
3586 /// If an instruction has a use that spans multiple iterations, then
3587 /// return true. These instructions are characterized by having a back-ege
3588 /// to a Phi, which contains a reference to another Phi.
3589 static SUnit *multipleIterations(SUnit *SU, SwingSchedulerDAG *DAG) {
3590 for (auto &P : SU->Preds)
3591 if (DAG->isBackedge(SU, P) && P.getSUnit()->getInstr()->isPHI())
3592 for (auto &S : P.getSUnit()->Succs)
3593 if (S.getKind() == SDep::Order && S.getSUnit()->getInstr()->isPHI())
3594 return P.getSUnit();
3598 /// Compute the scheduling start slot for the instruction. The start slot
3599 /// depends on any predecessor or successor nodes scheduled already.
3600 void SMSchedule::computeStart(SUnit *SU, int *MaxEarlyStart, int *MinLateStart,
3601 int *MinEnd, int *MaxStart, int II,
3602 SwingSchedulerDAG *DAG) {
3603 // Iterate over each instruction that has been scheduled already. The start
3604 // slot computuation depends on whether the previously scheduled instruction
3605 // is a predecessor or successor of the specified instruction.
3606 for (int cycle = getFirstCycle(); cycle <= LastCycle; ++cycle) {
3608 // Iterate over each instruction in the current cycle.
3609 for (SUnit *I : getInstructions(cycle)) {
3610 // Because we're processing a DAG for the dependences, we recognize
3611 // the back-edge in recurrences by anti dependences.
3612 for (unsigned i = 0, e = (unsigned)SU->Preds.size(); i != e; ++i) {
3613 const SDep &Dep = SU->Preds[i];
3614 if (Dep.getSUnit() == I) {
3615 if (!DAG->isBackedge(SU, Dep)) {
3616 int EarlyStart = cycle + DAG->getLatency(SU, Dep) -
3617 DAG->getDistance(Dep.getSUnit(), SU, Dep) * II;
3618 *MaxEarlyStart = std::max(*MaxEarlyStart, EarlyStart);
3619 if (DAG->isLoopCarriedOrder(SU, Dep, false)) {
3620 int End = earliestCycleInChain(Dep) + (II - 1);
3621 *MinEnd = std::min(*MinEnd, End);
3624 int LateStart = cycle - DAG->getLatency(SU, Dep) +
3625 DAG->getDistance(SU, Dep.getSUnit(), Dep) * II;
3626 *MinLateStart = std::min(*MinLateStart, LateStart);
3629 // For instruction that requires multiple iterations, make sure that
3630 // the dependent instruction is not scheduled past the definition.
3631 SUnit *BE = multipleIterations(I, DAG);
3632 if (BE && Dep.getSUnit() == BE && !SU->getInstr()->isPHI() &&
3634 *MinLateStart = std::min(*MinLateStart, cycle);
3636 for (unsigned i = 0, e = (unsigned)SU->Succs.size(); i != e; ++i)
3637 if (SU->Succs[i].getSUnit() == I) {
3638 const SDep &Dep = SU->Succs[i];
3639 if (!DAG->isBackedge(SU, Dep)) {
3640 int LateStart = cycle - DAG->getLatency(SU, Dep) +
3641 DAG->getDistance(SU, Dep.getSUnit(), Dep) * II;
3642 *MinLateStart = std::min(*MinLateStart, LateStart);
3643 if (DAG->isLoopCarriedOrder(SU, Dep)) {
3644 int Start = latestCycleInChain(Dep) + 1 - II;
3645 *MaxStart = std::max(*MaxStart, Start);
3648 int EarlyStart = cycle + DAG->getLatency(SU, Dep) -
3649 DAG->getDistance(Dep.getSUnit(), SU, Dep) * II;
3650 *MaxEarlyStart = std::max(*MaxEarlyStart, EarlyStart);
3657 /// Order the instructions within a cycle so that the definitions occur
3658 /// before the uses. Returns true if the instruction is added to the start
3659 /// of the list, or false if added to the end.
3660 bool SMSchedule::orderDependence(SwingSchedulerDAG *SSD, SUnit *SU,
3661 std::deque<SUnit *> &Insts) {
3662 MachineInstr *MI = SU->getInstr();
3663 bool OrderBeforeUse = false;
3664 bool OrderAfterDef = false;
3665 bool OrderBeforeDef = false;
3666 unsigned MoveDef = 0;
3667 unsigned MoveUse = 0;
3668 int StageInst1 = stageScheduled(SU);
3671 for (std::deque<SUnit *>::iterator I = Insts.begin(), E = Insts.end(); I != E;
3673 // Relative order of Phis does not matter.
3674 if (MI->isPHI() && (*I)->getInstr()->isPHI())
3676 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
3677 MachineOperand &MO = MI->getOperand(i);
3678 if (!MO.isReg() || !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
3680 unsigned Reg = MO.getReg();
3681 unsigned BasePos, OffsetPos;
3682 if (ST.getInstrInfo()->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos))
3683 if (MI->getOperand(BasePos).getReg() == Reg)
3684 if (unsigned NewReg = SSD->getInstrBaseReg(SU))
3687 std::tie(Reads, Writes) =
3688 (*I)->getInstr()->readsWritesVirtualRegister(Reg);
3689 if (MO.isDef() && Reads && stageScheduled(*I) <= StageInst1) {
3690 OrderBeforeUse = true;
3692 } else if (MO.isDef() && Reads && stageScheduled(*I) > StageInst1) {
3693 // Add the instruction after the scheduled instruction.
3694 OrderAfterDef = true;
3696 } else if (MO.isUse() && Writes && stageScheduled(*I) == StageInst1) {
3697 if (cycleScheduled(*I) == cycleScheduled(SU) && !(*I)->isSucc(SU)) {
3698 OrderBeforeUse = true;
3701 OrderAfterDef = true;
3704 } else if (MO.isUse() && Writes && stageScheduled(*I) > StageInst1) {
3705 OrderBeforeUse = true;
3708 OrderAfterDef = true;
3711 } else if (MO.isUse() && Writes && stageScheduled(*I) < StageInst1) {
3712 // Add the instruction before the scheduled instruction.
3713 OrderBeforeUse = true;
3715 } else if (MO.isUse() && stageScheduled(*I) == StageInst1 &&
3716 isLoopCarriedDefOfUse(SSD, (*I)->getInstr(), MO)) {
3717 OrderBeforeDef = true;
3721 // Check for order dependences between instructions. Make sure the source
3722 // is ordered before the destination.
3723 for (auto &S : SU->Succs)
3724 if (S.getKind() == SDep::Order) {
3725 if (S.getSUnit() == *I && stageScheduled(*I) == StageInst1) {
3726 OrderBeforeUse = true;
3729 } else if (TargetRegisterInfo::isPhysicalRegister(S.getReg())) {
3730 if (cycleScheduled(SU) != cycleScheduled(S.getSUnit())) {
3731 if (S.isAssignedRegDep()) {
3732 OrderAfterDef = true;
3736 OrderBeforeUse = true;
3740 for (auto &P : SU->Preds)
3741 if (P.getKind() == SDep::Order) {
3742 if (P.getSUnit() == *I && stageScheduled(*I) == StageInst1) {
3743 OrderAfterDef = true;
3746 } else if (TargetRegisterInfo::isPhysicalRegister(P.getReg())) {
3747 if (cycleScheduled(SU) != cycleScheduled(P.getSUnit())) {
3748 if (P.isAssignedRegDep()) {
3749 OrderBeforeUse = true;
3753 OrderAfterDef = true;
3759 // A circular dependence.
3760 if (OrderAfterDef && OrderBeforeUse && MoveUse == MoveDef)
3761 OrderBeforeUse = false;
3763 // OrderAfterDef takes precedences over OrderBeforeDef. The latter is due
3764 // to a loop-carried dependence.
3766 OrderBeforeUse = !OrderAfterDef || (MoveUse > MoveDef);
3768 // The uncommon case when the instruction order needs to be updated because
3769 // there is both a use and def.
3770 if (OrderBeforeUse && OrderAfterDef) {
3771 SUnit *UseSU = Insts.at(MoveUse);
3772 SUnit *DefSU = Insts.at(MoveDef);
3773 if (MoveUse > MoveDef) {
3774 Insts.erase(Insts.begin() + MoveUse);
3775 Insts.erase(Insts.begin() + MoveDef);
3777 Insts.erase(Insts.begin() + MoveDef);
3778 Insts.erase(Insts.begin() + MoveUse);
3780 if (orderDependence(SSD, UseSU, Insts)) {
3781 Insts.push_front(SU);
3782 orderDependence(SSD, DefSU, Insts);
3786 Insts.push_back(SU);
3787 Insts.push_back(UseSU);
3788 orderDependence(SSD, DefSU, Insts);
3791 // Put the new instruction first if there is a use in the list. Otherwise,
3792 // put it at the end of the list.
3794 Insts.push_front(SU);
3796 Insts.push_back(SU);
3797 return OrderBeforeUse;
3800 /// Return true if the scheduled Phi has a loop carried operand.
3801 bool SMSchedule::isLoopCarried(SwingSchedulerDAG *SSD, MachineInstr &Phi) {
3804 assert(Phi.isPHI() && "Expecing a Phi.");
3805 SUnit *DefSU = SSD->getSUnit(&Phi);
3806 unsigned DefCycle = cycleScheduled(DefSU);
3807 int DefStage = stageScheduled(DefSU);
3809 unsigned InitVal = 0;
3810 unsigned LoopVal = 0;
3811 getPhiRegs(Phi, Phi.getParent(), InitVal, LoopVal);
3812 SUnit *UseSU = SSD->getSUnit(MRI.getVRegDef(LoopVal));
3815 if (UseSU->getInstr()->isPHI())
3817 unsigned LoopCycle = cycleScheduled(UseSU);
3818 int LoopStage = stageScheduled(UseSU);
3819 return (LoopCycle > DefCycle) || (LoopStage <= DefStage);
3822 /// Return true if the instruction is a definition that is loop carried
3823 /// and defines the use on the next iteration.
3824 /// v1 = phi(v2, v3)
3825 /// (Def) v3 = op v1
3827 /// If MO appears before Def, then then v1 and v3 may get assigned to the same
3829 bool SMSchedule::isLoopCarriedDefOfUse(SwingSchedulerDAG *SSD,
3830 MachineInstr *Def, MachineOperand &MO) {
3835 MachineInstr *Phi = MRI.getVRegDef(MO.getReg());
3836 if (!Phi || !Phi->isPHI() || Phi->getParent() != Def->getParent())
3838 if (!isLoopCarried(SSD, *Phi))
3840 unsigned LoopReg = getLoopPhiReg(*Phi, Phi->getParent());
3841 for (unsigned i = 0, e = Def->getNumOperands(); i != e; ++i) {
3842 MachineOperand &DMO = Def->getOperand(i);
3843 if (!DMO.isReg() || !DMO.isDef())
3845 if (DMO.getReg() == LoopReg)
3851 // Check if the generated schedule is valid. This function checks if
3852 // an instruction that uses a physical register is scheduled in a
3853 // different stage than the definition. The pipeliner does not handle
3854 // physical register values that may cross a basic block boundary.
3855 bool SMSchedule::isValidSchedule(SwingSchedulerDAG *SSD) {
3856 for (int i = 0, e = SSD->SUnits.size(); i < e; ++i) {
3857 SUnit &SU = SSD->SUnits[i];
3858 if (!SU.hasPhysRegDefs)
3860 int StageDef = stageScheduled(&SU);
3861 assert(StageDef != -1 && "Instruction should have been scheduled.");
3862 for (auto &SI : SU.Succs)
3863 if (SI.isAssignedRegDep())
3864 if (ST.getRegisterInfo()->isPhysicalRegister(SI.getReg()))
3865 if (stageScheduled(SI.getSUnit()) != StageDef)
3871 /// Attempt to fix the degenerate cases when the instruction serialization
3872 /// causes the register lifetimes to overlap. For example,
3873 /// p' = store_pi(p, b)
3874 /// = load p, offset
3875 /// In this case p and p' overlap, which means that two registers are needed.
3876 /// Instead, this function changes the load to use p' and updates the offset.
3877 void SwingSchedulerDAG::fixupRegisterOverlaps(std::deque<SUnit *> &Instrs) {
3878 unsigned OverlapReg = 0;
3879 unsigned NewBaseReg = 0;
3880 for (SUnit *SU : Instrs) {
3881 MachineInstr *MI = SU->getInstr();
3882 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
3883 const MachineOperand &MO = MI->getOperand(i);
3884 // Look for an instruction that uses p. The instruction occurs in the
3885 // same cycle but occurs later in the serialized order.
3886 if (MO.isReg() && MO.isUse() && MO.getReg() == OverlapReg) {
3887 // Check that the instruction appears in the InstrChanges structure,
3888 // which contains instructions that can have the offset updated.
3889 DenseMap<SUnit *, std::pair<unsigned, int64_t>>::iterator It =
3890 InstrChanges.find(SU);
3891 if (It != InstrChanges.end()) {
3892 unsigned BasePos, OffsetPos;
3893 // Update the base register and adjust the offset.
3894 if (TII->getBaseAndOffsetPosition(*MI, BasePos, OffsetPos)) {
3895 MachineInstr *NewMI = MF.CloneMachineInstr(MI);
3896 NewMI->getOperand(BasePos).setReg(NewBaseReg);
3898 MI->getOperand(OffsetPos).getImm() - It->second.second;
3899 NewMI->getOperand(OffsetPos).setImm(NewOffset);
3900 SU->setInstr(NewMI);
3901 MISUnitMap[NewMI] = SU;
3902 NewMIs.insert(NewMI);
3909 // Look for an instruction of the form p' = op(p), which uses and defines
3910 // two virtual registers that get allocated to the same physical register.
3911 unsigned TiedUseIdx = 0;
3912 if (MI->isRegTiedToUseOperand(i, &TiedUseIdx)) {
3913 // OverlapReg is p in the example above.
3914 OverlapReg = MI->getOperand(TiedUseIdx).getReg();
3915 // NewBaseReg is p' in the example above.
3916 NewBaseReg = MI->getOperand(i).getReg();
3923 /// After the schedule has been formed, call this function to combine
3924 /// the instructions from the different stages/cycles. That is, this
3925 /// function creates a schedule that represents a single iteration.
3926 void SMSchedule::finalizeSchedule(SwingSchedulerDAG *SSD) {
3927 // Move all instructions to the first stage from later stages.
3928 for (int cycle = getFirstCycle(); cycle <= getFinalCycle(); ++cycle) {
3929 for (int stage = 1, lastStage = getMaxStageCount(); stage <= lastStage;
3931 std::deque<SUnit *> &cycleInstrs =
3932 ScheduledInstrs[cycle + (stage * InitiationInterval)];
3933 for (std::deque<SUnit *>::reverse_iterator I = cycleInstrs.rbegin(),
3934 E = cycleInstrs.rend();
3936 ScheduledInstrs[cycle].push_front(*I);
3939 // Iterate over the definitions in each instruction, and compute the
3940 // stage difference for each use. Keep the maximum value.
3941 for (auto &I : InstrToCycle) {
3942 int DefStage = stageScheduled(I.first);
3943 MachineInstr *MI = I.first->getInstr();
3944 for (unsigned i = 0, e = MI->getNumOperands(); i < e; ++i) {
3945 MachineOperand &Op = MI->getOperand(i);
3946 if (!Op.isReg() || !Op.isDef())
3949 unsigned Reg = Op.getReg();
3950 unsigned MaxDiff = 0;
3951 bool PhiIsSwapped = false;
3952 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(Reg),
3955 MachineOperand &UseOp = *UI;
3956 MachineInstr *UseMI = UseOp.getParent();
3957 SUnit *SUnitUse = SSD->getSUnit(UseMI);
3958 int UseStage = stageScheduled(SUnitUse);
3960 if (UseStage != -1 && UseStage >= DefStage)
3961 Diff = UseStage - DefStage;
3963 if (isLoopCarried(SSD, *MI))
3966 PhiIsSwapped = true;
3968 MaxDiff = std::max(Diff, MaxDiff);
3970 RegToStageDiff[Reg] = std::make_pair(MaxDiff, PhiIsSwapped);
3974 // Erase all the elements in the later stages. Only one iteration should
3975 // remain in the scheduled list, and it contains all the instructions.
3976 for (int cycle = getFinalCycle() + 1; cycle <= LastCycle; ++cycle)
3977 ScheduledInstrs.erase(cycle);
3979 // Change the registers in instruction as specified in the InstrChanges
3980 // map. We need to use the new registers to create the correct order.
3981 for (int i = 0, e = SSD->SUnits.size(); i != e; ++i) {
3982 SUnit *SU = &SSD->SUnits[i];
3983 SSD->applyInstrChange(SU->getInstr(), *this);
3986 // Reorder the instructions in each cycle to fix and improve the
3988 for (int Cycle = getFirstCycle(), E = getFinalCycle(); Cycle <= E; ++Cycle) {
3989 std::deque<SUnit *> &cycleInstrs = ScheduledInstrs[Cycle];
3990 std::deque<SUnit *> newOrderZC;
3991 // Put the zero-cost, pseudo instructions at the start of the cycle.
3992 for (unsigned i = 0, e = cycleInstrs.size(); i < e; ++i) {
3993 SUnit *SU = cycleInstrs[i];
3994 if (ST.getInstrInfo()->isZeroCost(SU->getInstr()->getOpcode()))
3995 orderDependence(SSD, SU, newOrderZC);
3997 std::deque<SUnit *> newOrderI;
3998 // Then, add the regular instructions back.
3999 for (unsigned i = 0, e = cycleInstrs.size(); i < e; ++i) {
4000 SUnit *SU = cycleInstrs[i];
4001 if (!ST.getInstrInfo()->isZeroCost(SU->getInstr()->getOpcode()))
4002 orderDependence(SSD, SU, newOrderI);
4004 // Replace the old order with the new order.
4005 cycleInstrs.swap(newOrderZC);
4006 cycleInstrs.insert(cycleInstrs.end(), newOrderI.begin(), newOrderI.end());
4007 SSD->fixupRegisterOverlaps(cycleInstrs);
4013 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4014 /// Print the schedule information to the given output.
4015 void SMSchedule::print(raw_ostream &os) const {
4016 // Iterate over each cycle.
4017 for (int cycle = getFirstCycle(); cycle <= getFinalCycle(); ++cycle) {
4018 // Iterate over each instruction in the cycle.
4019 const_sched_iterator cycleInstrs = ScheduledInstrs.find(cycle);
4020 for (SUnit *CI : cycleInstrs->second) {
4021 os << "cycle " << cycle << " (" << stageScheduled(CI) << ") ";
4022 os << "(" << CI->NodeNum << ") ";
4023 CI->getInstr()->print(os);
4029 /// Utility function used for debugging to print the schedule.
4030 LLVM_DUMP_METHOD void SMSchedule::dump() const { print(dbgs()); }