Dale Johannesen | 72f1596 | 2007-07-13 17:31:29 +0000 | [diff] [blame] | 1 | //===----- SchedulePostRAList.cpp - list scheduler ------------------------===// |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements a top-down list scheduler, using standard algorithms. |
| 11 | // The basic approach uses a priority queue of available nodes to schedule. |
| 12 | // One at a time, nodes are taken from the priority queue (thus in priority |
| 13 | // order), checked for legality to schedule, and emitted if legal. |
| 14 | // |
| 15 | // Nodes may not be legal to schedule either due to structural hazards (e.g. |
| 16 | // pipeline or resource constraints) or because an input to the instruction has |
| 17 | // not completed execution. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #define DEBUG_TYPE "post-RA-sched" |
David Goodwin | 82c7248 | 2009-10-28 18:29:54 +0000 | [diff] [blame] | 22 | #include "AntiDepBreaker.h" |
David Goodwin | 3487771 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 23 | #include "AggressiveAntiDepBreaker.h" |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 24 | #include "CriticalAntiDepBreaker.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/Passes.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LatencyPriorityQueue.h" |
| 27 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineDominators.h" |
David Goodwin | c7951f8 | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Andrew Trick | 1525260 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Andrew Trick | ed395c8 | 2012-03-07 23:01:06 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/ScheduleDAGInstrs.h" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetSubtargetInfo.h" |
David Goodwin | e10deca | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/BitVector.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/Statistic.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 50 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 51 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 52 | STATISTIC(NumFixedAnti, "Number of fixed anti-dependencies"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 53 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 54 | // Post-RA scheduling is enabled with |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 55 | // TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 56 | // override the target. |
| 57 | static cl::opt<bool> |
| 58 | EnablePostRAScheduler("post-RA-scheduler", |
| 59 | cl::desc("Enable scheduling after register allocation"), |
David Goodwin | 9843a93 | 2009-10-01 22:19:57 +0000 | [diff] [blame] | 60 | cl::init(false), cl::Hidden); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 61 | static cl::opt<std::string> |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 62 | EnableAntiDepBreaking("break-anti-dependencies", |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 63 | cl::desc("Break post-RA scheduling anti-dependencies: " |
| 64 | "\"critical\", \"all\", or \"none\""), |
| 65 | cl::init("none"), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 66 | |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 67 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 68 | static cl::opt<int> |
| 69 | DebugDiv("postra-sched-debugdiv", |
| 70 | cl::desc("Debug control MBBs that are scheduled"), |
| 71 | cl::init(0), cl::Hidden); |
| 72 | static cl::opt<int> |
| 73 | DebugMod("postra-sched-debugmod", |
| 74 | cl::desc("Debug control MBBs that are scheduled"), |
| 75 | cl::init(0), cl::Hidden); |
| 76 | |
David Goodwin | ada0ef8 | 2009-10-26 19:41:00 +0000 | [diff] [blame] | 77 | AntiDepBreaker::~AntiDepBreaker() { } |
| 78 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 79 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 80 | class PostRAScheduler : public MachineFunctionPass { |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 81 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 82 | RegisterClassInfo RegClassInfo; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 83 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 84 | public: |
| 85 | static char ID; |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 86 | PostRAScheduler() : MachineFunctionPass(ID) {} |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 87 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 88 | void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 89 | AU.setPreservesCFG(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 90 | AU.addRequired<AliasAnalysis>(); |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 91 | AU.addRequired<TargetPassConfig>(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 92 | AU.addRequired<MachineDominatorTree>(); |
| 93 | AU.addPreserved<MachineDominatorTree>(); |
| 94 | AU.addRequired<MachineLoopInfo>(); |
| 95 | AU.addPreserved<MachineLoopInfo>(); |
| 96 | MachineFunctionPass::getAnalysisUsage(AU); |
| 97 | } |
| 98 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 99 | bool runOnMachineFunction(MachineFunction &Fn); |
| 100 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 101 | char PostRAScheduler::ID = 0; |
| 102 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 103 | class SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 104 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 105 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 106 | LatencyPriorityQueue AvailableQueue; |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 107 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 108 | /// PendingQueue - This contains all of the instructions whose operands have |
| 109 | /// been issued, but their results are not ready yet (due to the latency of |
| 110 | /// the operation). Once the operands becomes available, the instruction is |
| 111 | /// added to the AvailableQueue. |
| 112 | std::vector<SUnit*> PendingQueue; |
| 113 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 114 | /// Topo - A topological ordering for SUnits. |
| 115 | ScheduleDAGTopologicalSort Topo; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 116 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 117 | /// HazardRec - The hazard recognizer to use. |
| 118 | ScheduleHazardRecognizer *HazardRec; |
| 119 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 120 | /// AntiDepBreak - Anti-dependence breaking object, or NULL if none |
| 121 | AntiDepBreaker *AntiDepBreak; |
| 122 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 123 | /// AA - AliasAnalysis for making memory reference queries. |
| 124 | AliasAnalysis *AA; |
| 125 | |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 126 | /// LiveRegs - true if the register is live. |
| 127 | BitVector LiveRegs; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 128 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 129 | /// The schedule. Null SUnit*'s represent noop instructions. |
| 130 | std::vector<SUnit*> Sequence; |
| 131 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 132 | public: |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 133 | SchedulePostRATDList( |
| 134 | MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 135 | AliasAnalysis *AA, const RegisterClassInfo&, |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 136 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 137 | SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 138 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 139 | ~SchedulePostRATDList(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 140 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 141 | /// startBlock - Initialize register live-range state for scheduling in |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 142 | /// this block. |
| 143 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 144 | void startBlock(MachineBasicBlock *BB); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 145 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 146 | /// Initialize the scheduler state for the next scheduling region. |
| 147 | virtual void enterRegion(MachineBasicBlock *bb, |
| 148 | MachineBasicBlock::iterator begin, |
| 149 | MachineBasicBlock::iterator end, |
| 150 | unsigned endcount); |
| 151 | |
| 152 | /// Notify that the scheduler has finished scheduling the current region. |
| 153 | virtual void exitRegion(); |
| 154 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 155 | /// Schedule - Schedule the instruction range using list scheduling. |
| 156 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 157 | void schedule(); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 158 | |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 159 | void EmitSchedule(); |
| 160 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 161 | /// Observe - Update liveness information to account for the current |
| 162 | /// instruction, which will not be scheduled. |
| 163 | /// |
| 164 | void Observe(MachineInstr *MI, unsigned Count); |
| 165 | |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 166 | /// finishBlock - Clean up register live-range state. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 167 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 168 | void finishBlock(); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 169 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 170 | /// FixupKills - Fix register kill flags that have been made |
| 171 | /// invalid due to scheduling |
| 172 | /// |
| 173 | void FixupKills(MachineBasicBlock *MBB); |
| 174 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 175 | private: |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 176 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
| 177 | void ReleaseSuccessors(SUnit *SU); |
| 178 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 179 | void ListScheduleTopDown(); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 180 | void StartBlockForKills(MachineBasicBlock *BB); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 181 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 182 | // ToggleKillFlag - Toggle a register operand kill flag. Other |
| 183 | // adjustments may be made to the instruction if necessary. Return |
| 184 | // true if the operand has been deleted, false if not. |
| 185 | bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); |
Andrew Trick | 73ba69b | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 186 | |
| 187 | void dumpSchedule() const; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 188 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 191 | char &llvm::PostRASchedulerID = PostRAScheduler::ID; |
| 192 | |
| 193 | INITIALIZE_PASS(PostRAScheduler, "post-RA-sched", |
| 194 | "Post RA top-down list latency scheduler", false, false) |
| 195 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 196 | SchedulePostRATDList::SchedulePostRATDList( |
| 197 | MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 198 | AliasAnalysis *AA, const RegisterClassInfo &RCI, |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 199 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 200 | SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs) |
Andrew Trick | 5e920d7 | 2012-01-14 02:17:12 +0000 | [diff] [blame] | 201 | : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA), |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 202 | LiveRegs(TRI->getNumRegs()) |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 203 | { |
| 204 | const TargetMachine &TM = MF.getTarget(); |
| 205 | const InstrItineraryData *InstrItins = TM.getInstrItineraryData(); |
| 206 | HazardRec = |
| 207 | TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this); |
Preston Gurd | 6a8c7bf | 2012-04-23 21:39:35 +0000 | [diff] [blame] | 208 | |
| 209 | assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE || |
| 210 | MRI.tracksLiveness()) && |
| 211 | "Live-ins must be accurate for anti-dependency breaking"); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 212 | AntiDepBreak = |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 213 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ? |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 214 | (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) : |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 215 | ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ? |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 216 | (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL)); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | SchedulePostRATDList::~SchedulePostRATDList() { |
| 220 | delete HazardRec; |
| 221 | delete AntiDepBreak; |
| 222 | } |
| 223 | |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 224 | /// Initialize state associated with the next scheduling region. |
| 225 | void SchedulePostRATDList::enterRegion(MachineBasicBlock *bb, |
| 226 | MachineBasicBlock::iterator begin, |
| 227 | MachineBasicBlock::iterator end, |
| 228 | unsigned endcount) { |
| 229 | ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount); |
| 230 | Sequence.clear(); |
| 231 | } |
| 232 | |
| 233 | /// Print the schedule before exiting the region. |
| 234 | void SchedulePostRATDList::exitRegion() { |
| 235 | DEBUG({ |
| 236 | dbgs() << "*** Final schedule ***\n"; |
| 237 | dumpSchedule(); |
| 238 | dbgs() << '\n'; |
| 239 | }); |
| 240 | ScheduleDAGInstrs::exitRegion(); |
| 241 | } |
| 242 | |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame^] | 243 | #ifndef NDEBUG |
Andrew Trick | 73ba69b | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 244 | /// dumpSchedule - dump the scheduled Sequence. |
| 245 | void SchedulePostRATDList::dumpSchedule() const { |
| 246 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 247 | if (SUnit *SU = Sequence[i]) |
| 248 | SU->dump(this); |
| 249 | else |
| 250 | dbgs() << "**** NOOP ****\n"; |
| 251 | } |
| 252 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame^] | 253 | #endif |
Andrew Trick | 73ba69b | 2012-03-07 05:21:40 +0000 | [diff] [blame] | 254 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 255 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 256 | TII = Fn.getTarget().getInstrInfo(); |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 257 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 258 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
| 259 | AliasAnalysis *AA = &getAnalysis<AliasAnalysis>(); |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 260 | TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); |
| 261 | |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 262 | RegClassInfo.runOnMachineFunction(Fn); |
Dan Gohman | 5bf7c2a | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 263 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 264 | // Check for explicit enable/disable of post-ra scheduling. |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 265 | TargetSubtargetInfo::AntiDepBreakMode AntiDepMode = |
| 266 | TargetSubtargetInfo::ANTIDEP_NONE; |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 267 | SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 268 | if (EnablePostRAScheduler.getPosition() > 0) { |
| 269 | if (!EnablePostRAScheduler) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 270 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 271 | } else { |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 272 | // Check that post-RA scheduling is enabled for this target. |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 273 | // This may upgrade the AntiDepMode. |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 274 | const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>(); |
Andrew Trick | c7d081b | 2012-02-08 21:22:53 +0000 | [diff] [blame] | 275 | if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode, |
| 276 | CriticalPathRCs)) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 277 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 278 | } |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 279 | |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 280 | // Check for antidep breaking override... |
| 281 | if (EnableAntiDepBreaking.getPosition() > 0) { |
Evan Cheng | 5b1b4489 | 2011-07-01 21:01:15 +0000 | [diff] [blame] | 282 | AntiDepMode = (EnableAntiDepBreaking == "all") |
| 283 | ? TargetSubtargetInfo::ANTIDEP_ALL |
| 284 | : ((EnableAntiDepBreaking == "critical") |
| 285 | ? TargetSubtargetInfo::ANTIDEP_CRITICAL |
| 286 | : TargetSubtargetInfo::ANTIDEP_NONE); |
David Goodwin | 4c3715c | 2009-10-22 23:19:17 +0000 | [diff] [blame] | 287 | } |
| 288 | |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 289 | DEBUG(dbgs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 290 | |
Jakob Stoklund Olesen | fa796dd | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 291 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode, |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 292 | CriticalPathRCs); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 293 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 294 | // Loop over all of the basic blocks |
| 295 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 296 | MBB != MBBe; ++MBB) { |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 297 | #ifndef NDEBUG |
| 298 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 299 | if (DebugDiv > 0) { |
| 300 | static int bbcnt = 0; |
| 301 | if (bbcnt++ % DebugDiv != DebugMod) |
| 302 | continue; |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 303 | dbgs() << "*** DEBUG scheduling " << Fn.getName() |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 304 | << ":BB#" << MBB->getNumber() << " ***\n"; |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 305 | } |
| 306 | #endif |
| 307 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 308 | // Initialize register live-range state for scheduling in this block. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 309 | Scheduler.startBlock(MBB); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 310 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 311 | // Schedule each sequence of instructions not interrupted by a label |
| 312 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 313 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 314 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 315 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 316 | MachineInstr *MI = llvm::prior(I); |
Jakob Stoklund Olesen | 976647d | 2012-02-23 17:54:21 +0000 | [diff] [blame] | 317 | // Calls are not scheduling boundaries before register allocation, but |
| 318 | // post-ra we don't gain anything by scheduling across calls since we |
| 319 | // don't need to worry about register pressure. |
| 320 | if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) { |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 321 | Scheduler.enterRegion(MBB, I, Current, CurrentCount); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 322 | Scheduler.schedule(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 323 | Scheduler.exitRegion(); |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 324 | Scheduler.EmitSchedule(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 325 | Current = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 326 | CurrentCount = Count - 1; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 327 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 328 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 329 | I = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 330 | --Count; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 331 | if (MI->isBundle()) |
| 332 | Count -= MI->getBundleSize(); |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 333 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 334 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 335 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 336 | "Instruction count mismatch!"); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 337 | Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 338 | Scheduler.schedule(); |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 339 | Scheduler.exitRegion(); |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 340 | Scheduler.EmitSchedule(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 341 | |
| 342 | // Clean up register live-range state. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 343 | Scheduler.finishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 344 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 345 | // Update register kills |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 346 | Scheduler.FixupKills(MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 347 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 348 | |
| 349 | return true; |
| 350 | } |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 351 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 352 | /// StartBlock - Initialize register live-range state for scheduling in |
| 353 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 354 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 355 | void SchedulePostRATDList::startBlock(MachineBasicBlock *BB) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 356 | // Call the superclass. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 357 | ScheduleDAGInstrs::startBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 358 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 359 | // Reset the hazard recognizer and anti-dep breaker. |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 360 | HazardRec->Reset(); |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 361 | if (AntiDepBreak != NULL) |
| 362 | AntiDepBreak->StartBlock(BB); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /// Schedule - Schedule the instruction range using list scheduling. |
| 366 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 367 | void SchedulePostRATDList::schedule() { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 368 | // Build the scheduling graph. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 369 | buildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 370 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 371 | if (AntiDepBreak != NULL) { |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 372 | unsigned Broken = |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 373 | AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd, |
| 374 | EndIndex, DbgValues); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 375 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 376 | if (Broken != 0) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 377 | // We made changes. Update the dependency graph. |
| 378 | // Theoretically we could update the graph in place: |
| 379 | // When a live range is changed to use a different register, remove |
| 380 | // the def's anti-dependence *and* output-dependence edges due to |
| 381 | // that register, and add new anti-dependence and output-dependence |
| 382 | // edges based on the next live range of the register. |
Andrew Trick | 47c1445 | 2012-03-07 05:21:52 +0000 | [diff] [blame] | 383 | ScheduleDAG::clearDAG(); |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 384 | buildSchedGraph(AA); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 385 | |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 386 | NumFixedAnti += Broken; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 390 | DEBUG(dbgs() << "********** List Scheduling **********\n"); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 391 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 392 | SUnits[su].dumpAll(this)); |
| 393 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 394 | AvailableQueue.initNodes(SUnits); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 395 | ListScheduleTopDown(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 396 | AvailableQueue.releaseState(); |
| 397 | } |
| 398 | |
| 399 | /// Observe - Update liveness information to account for the current |
| 400 | /// instruction, which will not be scheduled. |
| 401 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 402 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 403 | if (AntiDepBreak != NULL) |
Andrew Trick | cf46b5a | 2012-03-07 23:00:52 +0000 | [diff] [blame] | 404 | AntiDepBreak->Observe(MI, Count, EndIndex); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /// FinishBlock - Clean up register live-range state. |
| 408 | /// |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 409 | void SchedulePostRATDList::finishBlock() { |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 410 | if (AntiDepBreak != NULL) |
| 411 | AntiDepBreak->FinishBlock(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 412 | |
| 413 | // Call the superclass. |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 414 | ScheduleDAGInstrs::finishBlock(); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 415 | } |
| 416 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 417 | /// StartBlockForKills - Initialize register live-range state for updating kills |
| 418 | /// |
| 419 | void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 420 | // Start with no live registers. |
| 421 | LiveRegs.reset(); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 422 | |
| 423 | // Determine the live-out physregs for this block. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 424 | if (!BB->empty() && BB->back().isReturn()) { |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 425 | // In a return block, examine the function live-out regs. |
| 426 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 427 | E = MRI.liveout_end(); I != E; ++I) { |
| 428 | unsigned Reg = *I; |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 429 | LiveRegs.set(Reg); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 430 | // Repeat, for all subregs. |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 431 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 432 | LiveRegs.set(*SubRegs); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | else { |
| 436 | // In a non-return block, examine the live-in regs of all successors. |
| 437 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 438 | SE = BB->succ_end(); SI != SE; ++SI) { |
| 439 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 440 | E = (*SI)->livein_end(); I != E; ++I) { |
| 441 | unsigned Reg = *I; |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 442 | LiveRegs.set(Reg); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 443 | // Repeat, for all subregs. |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 444 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 445 | LiveRegs.set(*SubRegs); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 451 | bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, |
| 452 | MachineOperand &MO) { |
| 453 | // Setting kill flag... |
| 454 | if (!MO.isKill()) { |
| 455 | MO.setIsKill(true); |
| 456 | return false; |
| 457 | } |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 458 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 459 | // If MO itself is live, clear the kill flag... |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 460 | if (LiveRegs.test(MO.getReg())) { |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 461 | MO.setIsKill(false); |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | // If any subreg of MO is live, then create an imp-def for that |
| 466 | // subreg and keep MO marked as killed. |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 467 | MO.setIsKill(false); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 468 | bool AllDead = true; |
| 469 | const unsigned SuperReg = MO.getReg(); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 470 | for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 471 | if (LiveRegs.test(*SubRegs)) { |
| 472 | MI->addOperand(MachineOperand::CreateReg(*SubRegs, |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 473 | true /*IsDef*/, |
| 474 | true /*IsImp*/, |
| 475 | false /*IsKill*/, |
| 476 | false /*IsDead*/)); |
| 477 | AllDead = false; |
| 478 | } |
| 479 | } |
| 480 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame] | 481 | if(AllDead) |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 482 | MO.setIsKill(true); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 483 | return false; |
| 484 | } |
| 485 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 486 | /// FixupKills - Fix the register kill flags, they may have been made |
| 487 | /// incorrect by instruction reordering. |
| 488 | /// |
| 489 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 490 | DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n'); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 491 | |
Benjamin Kramer | 49b726c | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 492 | BitVector killedRegs(TRI->getNumRegs()); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 493 | BitVector ReservedRegs = TRI->getReservedRegs(MF); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 494 | |
| 495 | StartBlockForKills(MBB); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 496 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 497 | // Examine block from end to start... |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 498 | unsigned Count = MBB->size(); |
| 499 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 500 | I != E; --Count) { |
| 501 | MachineInstr *MI = --I; |
Dale Johannesen | b0812f1 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 502 | if (MI->isDebugValue()) |
| 503 | continue; |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 504 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 505 | // Update liveness. Registers that are defed but not used in this |
| 506 | // instruction are now dead. Mark register and all subregs as they |
| 507 | // are completely defined. |
| 508 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 509 | MachineOperand &MO = MI->getOperand(i); |
Jakob Stoklund Olesen | f19a592 | 2012-02-23 01:22:15 +0000 | [diff] [blame] | 510 | if (MO.isRegMask()) |
Benjamin Kramer | b6bd8cc | 2012-02-23 19:29:25 +0000 | [diff] [blame] | 511 | LiveRegs.clearBitsNotInMask(MO.getRegMask()); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 512 | if (!MO.isReg()) continue; |
| 513 | unsigned Reg = MO.getReg(); |
| 514 | if (Reg == 0) continue; |
| 515 | if (!MO.isDef()) continue; |
| 516 | // Ignore two-addr defs. |
| 517 | if (MI->isRegTiedToUseOperand(i)) continue; |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 518 | |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 519 | LiveRegs.reset(Reg); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 520 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 521 | // Repeat for all subregs. |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 522 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 523 | LiveRegs.reset(*SubRegs); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 524 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 525 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 526 | // Examine all used registers and set/clear kill flag. When a |
| 527 | // register is used multiple times we only set the kill flag on |
| 528 | // the first use. |
Benjamin Kramer | 49b726c | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 529 | killedRegs.reset(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 530 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 531 | MachineOperand &MO = MI->getOperand(i); |
| 532 | if (!MO.isReg() || !MO.isUse()) continue; |
| 533 | unsigned Reg = MO.getReg(); |
| 534 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 535 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 536 | bool kill = false; |
Benjamin Kramer | 49b726c | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 537 | if (!killedRegs.test(Reg)) { |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 538 | kill = true; |
| 539 | // A register is not killed if any subregs are live... |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 540 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 541 | if (LiveRegs.test(*SubRegs)) { |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 542 | kill = false; |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // If subreg is not live, then register is killed if it became |
| 548 | // live in this instruction |
| 549 | if (kill) |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 550 | kill = !LiveRegs.test(Reg); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 551 | } |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 552 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 553 | if (MO.isKill() != kill) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 554 | DEBUG(dbgs() << "Fixing " << MO << " in "); |
Jakob Stoklund Olesen | 15d75d9 | 2009-12-03 01:49:56 +0000 | [diff] [blame] | 555 | // Warning: ToggleKillFlag may invalidate MO. |
| 556 | ToggleKillFlag(MI, MO); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 557 | DEBUG(MI->dump()); |
| 558 | } |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 559 | |
Benjamin Kramer | 49b726c | 2012-02-23 18:28:32 +0000 | [diff] [blame] | 560 | killedRegs.set(Reg); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 561 | } |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 562 | |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 563 | // Mark any used register (that is not using undef) and subregs as |
| 564 | // now live... |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 566 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 567 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 568 | unsigned Reg = MO.getReg(); |
| 569 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 570 | |
Benjamin Kramer | 46252d8 | 2012-02-23 19:15:40 +0000 | [diff] [blame] | 571 | LiveRegs.set(Reg); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 572 | |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 573 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 574 | LiveRegs.set(*SubRegs); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 575 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 579 | //===----------------------------------------------------------------------===// |
| 580 | // Top-Down Scheduling |
| 581 | //===----------------------------------------------------------------------===// |
| 582 | |
| 583 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 584 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 585 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 586 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 587 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 588 | #ifndef NDEBUG |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 589 | if (SuccSU->NumPredsLeft == 0) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 590 | dbgs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 591 | SuccSU->dump(this); |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 592 | dbgs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 593 | llvm_unreachable(0); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 594 | } |
| 595 | #endif |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 596 | --SuccSU->NumPredsLeft; |
| 597 | |
Andrew Trick | 89fd437 | 2011-05-06 18:14:32 +0000 | [diff] [blame] | 598 | // Standard scheduler algorithms will recompute the depth of the successor |
Andrew Trick | 15ab359 | 2011-05-06 17:09:08 +0000 | [diff] [blame] | 599 | // here as such: |
| 600 | // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
| 601 | // |
| 602 | // However, we lazily compute node depth instead. Note that |
| 603 | // ScheduleNodeTopDown has already updated the depth of this node which causes |
| 604 | // all descendents to be marked dirty. Setting the successor depth explicitly |
| 605 | // here would cause depth to be recomputed for all its ancestors. If the |
| 606 | // successor is not yet ready (because of a transitively redundant edge) then |
| 607 | // this causes depth computation to be quadratic in the size of the DAG. |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 608 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 609 | // If all the node's predecessors are scheduled, this node is ready |
| 610 | // to be scheduled. Ignore the special ExitSU node. |
| 611 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 612 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 616 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 617 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 618 | I != E; ++I) { |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 619 | ReleaseSucc(SU, &*I); |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 620 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 624 | /// count of its successors. If a successor pending count is zero, add it to |
| 625 | /// the Available queue. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 626 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 627 | DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 628 | DEBUG(SU->dump(this)); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 629 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 630 | Sequence.push_back(SU); |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 631 | assert(CurCycle >= SU->getDepth() && |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 632 | "Node scheduled above its depth!"); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 633 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 634 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 635 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 636 | SU->isScheduled = true; |
Andrew Trick | 953be89 | 2012-03-07 23:00:49 +0000 | [diff] [blame] | 637 | AvailableQueue.scheduledNode(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 641 | /// schedulers. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 642 | void SchedulePostRATDList::ListScheduleTopDown() { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 643 | unsigned CurCycle = 0; |
Jim Grosbach | 9001303 | 2010-05-14 21:19:48 +0000 | [diff] [blame] | 644 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 645 | // We're scheduling top-down but we're visiting the regions in |
| 646 | // bottom-up order, so we don't know the hazards at the start of a |
| 647 | // region. So assume no hazards (this should usually be ok as most |
| 648 | // blocks are a single region). |
| 649 | HazardRec->Reset(); |
| 650 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 651 | // Release any successors of the special Entry node. |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 652 | ReleaseSuccessors(&EntrySU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 653 | |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 654 | // Add all leaves to Available queue. |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 655 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 656 | // It is available if it has no predecessors. |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 657 | bool available = SUnits[i].Preds.empty(); |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 658 | if (available) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 659 | AvailableQueue.push(&SUnits[i]); |
| 660 | SUnits[i].isAvailable = true; |
| 661 | } |
| 662 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 663 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 664 | // In any cycle where we can't schedule any instructions, we must |
| 665 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 666 | bool CycleHasInsts = false; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 667 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 668 | // While Available queue is not empty, grab the node with the highest |
| 669 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 670 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 671 | Sequence.reserve(SUnits.size()); |
| 672 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 673 | // Check to see if any of the pending instructions are ready to issue. If |
| 674 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 675 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 676 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 677 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 678 | AvailableQueue.push(PendingQueue[i]); |
| 679 | PendingQueue[i]->isAvailable = true; |
| 680 | PendingQueue[i] = PendingQueue.back(); |
| 681 | PendingQueue.pop_back(); |
| 682 | --i; --e; |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 683 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 684 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 685 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 686 | |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 687 | DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this)); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 688 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 689 | SUnit *FoundSUnit = 0; |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 690 | bool HasNoopHazards = false; |
| 691 | while (!AvailableQueue.empty()) { |
| 692 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 693 | |
| 694 | ScheduleHazardRecognizer::HazardType HT = |
Andrew Trick | 2da8bc8 | 2010-12-24 05:03:26 +0000 | [diff] [blame] | 695 | HazardRec->getHazardType(CurSUnit, 0/*no stalls*/); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 696 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
| 697 | FoundSUnit = CurSUnit; |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | // Remember if this is a noop hazard. |
| 702 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 703 | |
| 704 | NotReady.push_back(CurSUnit); |
| 705 | } |
| 706 | |
| 707 | // Add the nodes that aren't ready back onto the available list. |
| 708 | if (!NotReady.empty()) { |
| 709 | AvailableQueue.push_all(NotReady); |
| 710 | NotReady.clear(); |
| 711 | } |
| 712 | |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 713 | // If we found a node to schedule... |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 714 | if (FoundSUnit) { |
David Goodwin | 4de099d | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 715 | // ... schedule the node... |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 716 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 717 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 718 | CycleHasInsts = true; |
Andrew Trick | cf9aa28 | 2011-06-01 03:27:56 +0000 | [diff] [blame] | 719 | if (HazardRec->atIssueLimit()) { |
| 720 | DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n'); |
| 721 | HazardRec->AdvanceCycle(); |
| 722 | ++CurCycle; |
| 723 | CycleHasInsts = false; |
| 724 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 725 | } else { |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 726 | if (CycleHasInsts) { |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 727 | DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n'); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 728 | HazardRec->AdvanceCycle(); |
| 729 | } else if (!HasNoopHazards) { |
| 730 | // Otherwise, we have a pipeline stall, but no other problem, |
| 731 | // just advance the current cycle and try again. |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 732 | DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n'); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 733 | HazardRec->AdvanceCycle(); |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 734 | ++NumStalls; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 735 | } else { |
| 736 | // Otherwise, we have no instructions to issue and we have instructions |
| 737 | // that will fault if we don't do this right. This is the case for |
| 738 | // processors without pipeline interlocks and other cases. |
David Greene | e1b2129 | 2010-01-05 01:26:01 +0000 | [diff] [blame] | 739 | DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 740 | HazardRec->EmitNoop(); |
| 741 | Sequence.push_back(0); // NULL here means noop |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 742 | ++NumNoops; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 745 | ++CurCycle; |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 746 | CycleHasInsts = false; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | #ifndef NDEBUG |
Andrew Trick | 4c72720 | 2012-03-07 05:21:36 +0000 | [diff] [blame] | 751 | unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false); |
| 752 | unsigned Noops = 0; |
| 753 | for (unsigned i = 0, e = Sequence.size(); i != e; ++i) |
| 754 | if (!Sequence[i]) |
| 755 | ++Noops; |
| 756 | assert(Sequence.size() - Noops == ScheduledNodes && |
| 757 | "The number of nodes scheduled doesn't match the expected number!"); |
| 758 | #endif // NDEBUG |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 759 | } |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 760 | |
| 761 | // EmitSchedule - Emit the machine code in scheduled order. |
| 762 | void SchedulePostRATDList::EmitSchedule() { |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 763 | RegionBegin = RegionEnd; |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 764 | |
| 765 | // If first instruction was a DBG_VALUE then put it back. |
| 766 | if (FirstDbgValue) |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 767 | BB->splice(RegionEnd, BB, FirstDbgValue); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 768 | |
| 769 | // Then re-insert them according to the given schedule. |
| 770 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 771 | if (SUnit *SU = Sequence[i]) |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 772 | BB->splice(RegionEnd, BB, SU->getInstr()); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 773 | else |
| 774 | // Null SUnit* is a noop. |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 775 | TII->insertNoop(*BB, RegionEnd); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 776 | |
| 777 | // Update the Begin iterator, as the first instruction in the block |
| 778 | // may have been scheduled later. |
| 779 | if (i == 0) |
Andrew Trick | 68675c6 | 2012-03-09 04:29:02 +0000 | [diff] [blame] | 780 | RegionBegin = prior(RegionEnd); |
Andrew Trick | 84b454d | 2012-03-07 05:21:44 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // Reinsert any remaining debug_values. |
| 784 | for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator |
| 785 | DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { |
| 786 | std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); |
| 787 | MachineInstr *DbgValue = P.first; |
| 788 | MachineBasicBlock::iterator OrigPrivMI = P.second; |
| 789 | BB->splice(++OrigPrivMI, BB, DbgValue); |
| 790 | } |
| 791 | DbgValues.clear(); |
| 792 | FirstDbgValue = NULL; |
| 793 | } |