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 | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 22 | #include "ExactHazardRecognizer.h" |
| 23 | #include "SimpleHazardRecognizer.h" |
Dan Gohman | 6dc75fe | 2009-02-06 17:12:10 +0000 | [diff] [blame] | 24 | #include "ScheduleDAGInstrs.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" |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/ScheduleHazardRecognizer.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetInstrInfo.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetSubtarget.h" |
Chris Lattner | 459525d | 2008-01-14 19:00:06 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Compiler.h" |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ErrorHandling.h" |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 45 | #include <map> |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 46 | #include <set> |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 49 | STATISTIC(NumNoops, "Number of noops inserted"); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 50 | STATISTIC(NumStalls, "Number of pipeline stalls"); |
| 51 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 52 | // Post-RA scheduling is enabled with |
| 53 | // TargetSubtarget.enablePostRAScheduler(). This flag can be used to |
| 54 | // override the target. |
| 55 | static cl::opt<bool> |
| 56 | EnablePostRAScheduler("post-RA-scheduler", |
| 57 | cl::desc("Enable scheduling after register allocation"), |
David Goodwin | 9843a93 | 2009-10-01 22:19:57 +0000 | [diff] [blame] | 58 | cl::init(false), cl::Hidden); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 59 | static cl::opt<bool> |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 60 | EnableAntiDepBreaking("break-anti-dependencies", |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 61 | cl::desc("Break post-RA scheduling anti-dependencies"), |
| 62 | cl::init(true), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 63 | static cl::opt<bool> |
| 64 | EnablePostRAHazardAvoidance("avoid-hazards", |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 65 | cl::desc("Enable exact hazard avoidance"), |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 66 | cl::init(true), cl::Hidden); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 67 | |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 68 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 69 | static cl::opt<int> |
| 70 | DebugDiv("postra-sched-debugdiv", |
| 71 | cl::desc("Debug control MBBs that are scheduled"), |
| 72 | cl::init(0), cl::Hidden); |
| 73 | static cl::opt<int> |
| 74 | DebugMod("postra-sched-debugmod", |
| 75 | cl::desc("Debug control MBBs that are scheduled"), |
| 76 | cl::init(0), cl::Hidden); |
| 77 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 78 | namespace { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 79 | class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass { |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 80 | AliasAnalysis *AA; |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 81 | CodeGenOpt::Level OptLevel; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 82 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 83 | public: |
| 84 | static char ID; |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 85 | PostRAScheduler(CodeGenOpt::Level ol) : |
| 86 | MachineFunctionPass(&ID), OptLevel(ol) {} |
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>(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 91 | AU.addRequired<MachineDominatorTree>(); |
| 92 | AU.addPreserved<MachineDominatorTree>(); |
| 93 | AU.addRequired<MachineLoopInfo>(); |
| 94 | AU.addPreserved<MachineLoopInfo>(); |
| 95 | MachineFunctionPass::getAnalysisUsage(AU); |
| 96 | } |
| 97 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 98 | const char *getPassName() const { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 99 | return "Post RA top-down list latency scheduler"; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool runOnMachineFunction(MachineFunction &Fn); |
| 103 | }; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 104 | char PostRAScheduler::ID = 0; |
| 105 | |
| 106 | class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 107 | /// AvailableQueue - The priority queue to use for the available SUnits. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 108 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 109 | LatencyPriorityQueue AvailableQueue; |
| 110 | |
| 111 | /// PendingQueue - This contains all of the instructions whose operands have |
| 112 | /// been issued, but their results are not ready yet (due to the latency of |
| 113 | /// the operation). Once the operands becomes available, the instruction is |
| 114 | /// added to the AvailableQueue. |
| 115 | std::vector<SUnit*> PendingQueue; |
| 116 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 117 | /// Topo - A topological ordering for SUnits. |
| 118 | ScheduleDAGTopologicalSort Topo; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 119 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 120 | /// AllocatableSet - The set of allocatable registers. |
| 121 | /// We'll be ignoring anti-dependencies on non-allocatable registers, |
| 122 | /// because they may not be safe to break. |
| 123 | const BitVector AllocatableSet; |
| 124 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 125 | /// HazardRec - The hazard recognizer to use. |
| 126 | ScheduleHazardRecognizer *HazardRec; |
| 127 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 128 | /// AA - AliasAnalysis for making memory reference queries. |
| 129 | AliasAnalysis *AA; |
| 130 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 131 | /// Classes - For live regs that are only used in one register class in a |
| 132 | /// live range, the register class. If the register is not live, the |
| 133 | /// corresponding value is null. If the register is live but used in |
| 134 | /// multiple register classes, the corresponding value is -1 casted to a |
| 135 | /// pointer. |
| 136 | const TargetRegisterClass * |
| 137 | Classes[TargetRegisterInfo::FirstVirtualRegister]; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 138 | |
| 139 | /// RegRegs - Map registers to all their references within a live range. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 140 | std::multimap<unsigned, MachineOperand *> RegRefs; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 141 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 142 | /// KillIndices - The index of the most recent kill (proceding bottom-up), |
| 143 | /// or ~0u if the register is not live. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 144 | unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 145 | |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 146 | /// DefIndices - The index of the most recent complete def (proceding bottom |
| 147 | /// up), or ~0u if the register is live. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 148 | unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; |
| 149 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 150 | /// KeepRegs - A set of registers which are live and cannot be changed to |
| 151 | /// break anti-dependencies. |
| 152 | SmallSet<unsigned, 4> KeepRegs; |
| 153 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 154 | public: |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 155 | SchedulePostRATDList(MachineFunction &MF, |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 156 | const MachineLoopInfo &MLI, |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 157 | const MachineDominatorTree &MDT, |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 158 | ScheduleHazardRecognizer *HR, |
| 159 | AliasAnalysis *aa) |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 160 | : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits), |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 161 | AllocatableSet(TRI->getAllocatableSet(MF)), |
| 162 | HazardRec(HR), AA(aa) {} |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 163 | |
| 164 | ~SchedulePostRATDList() { |
| 165 | delete HazardRec; |
| 166 | } |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 167 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 168 | /// StartBlock - Initialize register live-range state for scheduling in |
| 169 | /// this block. |
| 170 | /// |
| 171 | void StartBlock(MachineBasicBlock *BB); |
| 172 | |
| 173 | /// Schedule - Schedule the instruction range using list scheduling. |
| 174 | /// |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 175 | void Schedule(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 176 | |
| 177 | /// FixupKills - Fix register kill flags that have been made |
| 178 | /// invalid due to scheduling |
| 179 | /// |
| 180 | void FixupKills(MachineBasicBlock *MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 181 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 182 | /// Observe - Update liveness information to account for the current |
| 183 | /// instruction, which will not be scheduled. |
| 184 | /// |
| 185 | void Observe(MachineInstr *MI, unsigned Count); |
| 186 | |
| 187 | /// FinishBlock - Clean up register live-range state. |
| 188 | /// |
| 189 | void FinishBlock(); |
| 190 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 191 | private: |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 192 | void PrescanInstruction(MachineInstr *MI); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 193 | void ScanInstruction(MachineInstr *MI, unsigned Count); |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 194 | void ReleaseSucc(SUnit *SU, SDep *SuccEdge); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 195 | void ReleaseSuccessors(SUnit *SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 196 | void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); |
| 197 | void ListScheduleTopDown(); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 198 | bool BreakAntiDependencies(); |
| 199 | unsigned findSuitableFreeRegister(unsigned AntiDepReg, |
| 200 | unsigned LastNewReg, |
| 201 | const TargetRegisterClass *); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 202 | void StartBlockForKills(MachineBasicBlock *BB); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 203 | |
| 204 | // ToggleKillFlag - Toggle a register operand kill flag. Other |
| 205 | // adjustments may be made to the instruction if necessary. Return |
| 206 | // true if the operand has been deleted, false if not. |
| 207 | bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 208 | }; |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 211 | /// isSchedulingBoundary - Test if the given instruction should be |
| 212 | /// considered a scheduling boundary. This primarily includes labels |
| 213 | /// and terminators. |
| 214 | /// |
| 215 | static bool isSchedulingBoundary(const MachineInstr *MI, |
| 216 | const MachineFunction &MF) { |
| 217 | // Terminators and labels can't be scheduled around. |
| 218 | if (MI->getDesc().isTerminator() || MI->isLabel()) |
| 219 | return true; |
| 220 | |
Dan Gohman | bed353d | 2009-02-10 23:29:38 +0000 | [diff] [blame] | 221 | // Don't attempt to schedule around any instruction that modifies |
| 222 | // a stack-oriented pointer, as it's unlikely to be profitable. This |
| 223 | // saves compile time, because it doesn't require every single |
| 224 | // stack slot reference to depend on the instruction that does the |
| 225 | // modification. |
| 226 | const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); |
| 227 | if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore())) |
| 228 | return true; |
| 229 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 233 | bool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { |
Dan Gohman | 5bf7c2a | 2009-10-10 00:15:38 +0000 | [diff] [blame] | 234 | AA = &getAnalysis<AliasAnalysis>(); |
| 235 | |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 236 | // Check for explicit enable/disable of post-ra scheduling. |
| 237 | if (EnablePostRAScheduler.getPosition() > 0) { |
| 238 | if (!EnablePostRAScheduler) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 239 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 240 | } else { |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 241 | // Check that post-RA scheduling is enabled for this target. |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 242 | const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>(); |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 243 | if (!ST.enablePostRAScheduler(OptLevel)) |
Evan Cheng | c83da2f9 | 2009-10-16 06:10:34 +0000 | [diff] [blame] | 244 | return false; |
David Goodwin | 471850a | 2009-10-01 21:46:35 +0000 | [diff] [blame] | 245 | } |
David Goodwin | 0dad89f | 2009-09-30 00:10:16 +0000 | [diff] [blame] | 246 | |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 247 | DEBUG(errs() << "PostRAScheduler\n"); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 248 | |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 249 | const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 250 | const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 251 | const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData(); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 252 | ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ? |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 253 | (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) : |
| 254 | (ScheduleHazardRecognizer *)new SimpleHazardRecognizer(); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 255 | |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 256 | SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR, AA); |
Dan Gohman | 79ce276 | 2009-01-15 19:20:50 +0000 | [diff] [blame] | 257 | |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 258 | // Loop over all of the basic blocks |
| 259 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 260 | MBB != MBBe; ++MBB) { |
David Goodwin | 1f15228 | 2009-09-01 18:34:03 +0000 | [diff] [blame] | 261 | #ifndef NDEBUG |
| 262 | // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod |
| 263 | if (DebugDiv > 0) { |
| 264 | static int bbcnt = 0; |
| 265 | if (bbcnt++ % DebugDiv != DebugMod) |
| 266 | continue; |
| 267 | errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() << |
| 268 | ":MBB ID#" << MBB->getNumber() << " ***\n"; |
| 269 | } |
| 270 | #endif |
| 271 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 272 | // Initialize register live-range state for scheduling in this block. |
| 273 | Scheduler.StartBlock(MBB); |
| 274 | |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 275 | // Schedule each sequence of instructions not interrupted by a label |
| 276 | // or anything else that effectively needs to shut down scheduling. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 277 | MachineBasicBlock::iterator Current = MBB->end(); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 278 | unsigned Count = MBB->size(), CurrentCount = Count; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 279 | for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { |
| 280 | MachineInstr *MI = prior(I); |
| 281 | if (isSchedulingBoundary(MI, Fn)) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 282 | Scheduler.Run(MBB, I, Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 283 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 284 | Current = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 285 | CurrentCount = Count - 1; |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 286 | Scheduler.Observe(MI, CurrentCount); |
Dan Gohman | f711939 | 2009-01-16 22:10:20 +0000 | [diff] [blame] | 287 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 288 | I = MI; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 289 | --Count; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 290 | } |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 291 | assert(Count == 0 && "Instruction count mismatch!"); |
Duncan Sands | 9e8bd0b | 2009-03-11 09:04:34 +0000 | [diff] [blame] | 292 | assert((MBB->begin() == Current || CurrentCount != 0) && |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 293 | "Instruction count mismatch!"); |
| 294 | Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount); |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 295 | Scheduler.EmitSchedule(0); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 296 | |
| 297 | // Clean up register live-range state. |
| 298 | Scheduler.FinishBlock(); |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 299 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 300 | // Update register kills |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 301 | Scheduler.FixupKills(MBB); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 302 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 307 | /// StartBlock - Initialize register live-range state for scheduling in |
| 308 | /// this block. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 309 | /// |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 310 | void SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) { |
| 311 | // Call the superclass. |
| 312 | ScheduleDAGInstrs::StartBlock(BB); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 313 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 314 | // Reset the hazard recognizer. |
| 315 | HazardRec->Reset(); |
| 316 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 317 | // Clear out the register class data. |
| 318 | std::fill(Classes, array_endof(Classes), |
| 319 | static_cast<const TargetRegisterClass *>(0)); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 320 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 321 | // Initialize the indices to indicate that no registers are live. |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 322 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 323 | std::fill(DefIndices, array_endof(DefIndices), BB->size()); |
| 324 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 325 | // Clear "do not change" set. |
| 326 | KeepRegs.clear(); |
| 327 | |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 328 | bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn()); |
| 329 | |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 330 | // Determine the live-out physregs for this block. |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 331 | if (IsReturnBlock) { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 332 | // In a return block, examine the function live-out regs. |
| 333 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 334 | E = MRI.liveout_end(); I != E; ++I) { |
| 335 | unsigned Reg = *I; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 336 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 337 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 338 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 339 | // Repeat, for all aliases. |
| 340 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 341 | unsigned AliasReg = *Alias; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 342 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 343 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 344 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
David Goodwin | c7951f8 | 2009-10-01 19:45:32 +0000 | [diff] [blame] | 347 | } else { |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 348 | // In a non-return block, examine the live-in regs of all successors. |
| 349 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 350 | SE = BB->succ_end(); SI != SE; ++SI) |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 351 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 352 | E = (*SI)->livein_end(); I != E; ++I) { |
| 353 | unsigned Reg = *I; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 354 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 355 | KillIndices[Reg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 356 | DefIndices[Reg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 357 | // Repeat, for all aliases. |
| 358 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 359 | unsigned AliasReg = *Alias; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 360 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 361 | KillIndices[AliasReg] = BB->size(); |
Dan Gohman | 6c3643c | 2008-12-19 22:23:43 +0000 | [diff] [blame] | 362 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 365 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 366 | |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 367 | // Mark live-out callee-saved registers. In a return block this is |
| 368 | // all callee-saved registers. In non-return this is any |
| 369 | // callee-saved register that is not saved in the prolog. |
| 370 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 371 | BitVector Pristine = MFI->getPristineRegs(BB); |
| 372 | for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) { |
| 373 | unsigned Reg = *I; |
| 374 | if (!IsReturnBlock && !Pristine.test(Reg)) continue; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 375 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 376 | KillIndices[Reg] = BB->size(); |
| 377 | DefIndices[Reg] = ~0u; |
| 378 | // Repeat, for all aliases. |
| 379 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 380 | unsigned AliasReg = *Alias; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 381 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
David Goodwin | 63bcbb7 | 2009-10-01 23:28:47 +0000 | [diff] [blame] | 382 | KillIndices[AliasReg] = BB->size(); |
| 383 | DefIndices[AliasReg] = ~0u; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /// Schedule - Schedule the instruction range using list scheduling. |
| 389 | /// |
| 390 | void SchedulePostRATDList::Schedule() { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 391 | DEBUG(errs() << "********** List Scheduling **********\n"); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 392 | |
| 393 | // Build the scheduling graph. |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 394 | BuildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 395 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 396 | if (EnableAntiDepBreaking) { |
| 397 | if (BreakAntiDependencies()) { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 398 | // We made changes. Update the dependency graph. |
| 399 | // Theoretically we could update the graph in place: |
| 400 | // When a live range is changed to use a different register, remove |
| 401 | // the def's anti-dependence *and* output-dependence edges due to |
| 402 | // that register, and add new anti-dependence and output-dependence |
| 403 | // edges based on the next live range of the register. |
| 404 | SUnits.clear(); |
| 405 | EntrySU = SUnit(); |
| 406 | ExitSU = SUnit(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 407 | BuildSchedGraph(AA); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 411 | DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) |
| 412 | SUnits[su].dumpAll(this)); |
| 413 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 414 | AvailableQueue.initNodes(SUnits); |
| 415 | |
| 416 | ListScheduleTopDown(); |
| 417 | |
| 418 | AvailableQueue.releaseState(); |
| 419 | } |
| 420 | |
| 421 | /// Observe - Update liveness information to account for the current |
| 422 | /// instruction, which will not be scheduled. |
| 423 | /// |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 424 | void SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { |
Dan Gohman | 1274ced | 2009-03-10 18:10:43 +0000 | [diff] [blame] | 425 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 426 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 427 | // Any register which was defined within the previous scheduling region |
| 428 | // may have been rescheduled and its lifetime may overlap with registers |
| 429 | // in ways not reflected in our current liveness state. For each such |
| 430 | // register, adjust the liveness state to be conservatively correct. |
| 431 | for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) |
| 432 | if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) { |
| 433 | assert(KillIndices[Reg] == ~0u && "Clobbered register is live!"); |
| 434 | // Mark this register to be non-renamable. |
| 435 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 436 | // Move the def index to the end of the previous region, to reflect |
| 437 | // that the def could theoretically have been scheduled at the end. |
| 438 | DefIndices[Reg] = InsertPosIndex; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 439 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 440 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 441 | PrescanInstruction(MI); |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 442 | ScanInstruction(MI, Count); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /// FinishBlock - Clean up register live-range state. |
| 446 | /// |
| 447 | void SchedulePostRATDList::FinishBlock() { |
| 448 | RegRefs.clear(); |
| 449 | |
| 450 | // Call the superclass. |
| 451 | ScheduleDAGInstrs::FinishBlock(); |
| 452 | } |
| 453 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 454 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 455 | /// critical path. |
| 456 | static SDep *CriticalPathStep(SUnit *SU) { |
| 457 | SDep *Next = 0; |
| 458 | unsigned NextDepth = 0; |
| 459 | // Find the predecessor edge with the greatest depth. |
| 460 | for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
| 461 | P != PE; ++P) { |
| 462 | SUnit *PredSU = P->getSUnit(); |
| 463 | unsigned PredLatency = P->getLatency(); |
| 464 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 465 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 466 | // other types of edges. |
| 467 | if (NextDepth < PredTotalLatency || |
| 468 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 469 | NextDepth = PredTotalLatency; |
| 470 | Next = &*P; |
| 471 | } |
| 472 | } |
| 473 | return Next; |
| 474 | } |
| 475 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 476 | void SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) { |
| 477 | // Scan the register operands for this instruction and update |
| 478 | // Classes and RegRefs. |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 479 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 480 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 481 | if (!MO.isReg()) continue; |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 482 | unsigned Reg = MO.getReg(); |
| 483 | if (Reg == 0) continue; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 484 | const TargetRegisterClass *NewRC = 0; |
| 485 | |
| 486 | if (i < MI->getDesc().getNumOperands()) |
| 487 | NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 488 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 489 | // For now, only allow the register to be changed if its register |
| 490 | // class is consistent across all uses. |
| 491 | if (!Classes[Reg] && NewRC) |
| 492 | Classes[Reg] = NewRC; |
| 493 | else if (!NewRC || Classes[Reg] != NewRC) |
| 494 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 495 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 496 | // Now check for aliases. |
| 497 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 498 | // If an alias of the reg is used during the live range, give up. |
| 499 | // Note that this allows us to skip checking if AntiDepReg |
| 500 | // overlaps with any of the aliases, among other things. |
| 501 | unsigned AliasReg = *Alias; |
| 502 | if (Classes[AliasReg]) { |
| 503 | Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 504 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 507 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 508 | // If we're still willing to consider this register, note the reference. |
| 509 | if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1)) |
| 510 | RegRefs.insert(std::make_pair(Reg, &MO)); |
| 511 | |
| 512 | // It's not safe to change register allocation for source operands of |
| 513 | // that have special allocation requirements. |
| 514 | if (MO.isUse() && MI->getDesc().hasExtraSrcRegAllocReq()) { |
| 515 | if (KeepRegs.insert(Reg)) { |
| 516 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 517 | *Subreg; ++Subreg) |
| 518 | KeepRegs.insert(*Subreg); |
| 519 | } |
| 520 | } |
| 521 | } |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void SchedulePostRATDList::ScanInstruction(MachineInstr *MI, |
| 525 | unsigned Count) { |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 526 | // Update liveness. |
| 527 | // Proceding upwards, registers that are defed but not used in this |
| 528 | // instruction are now dead. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 529 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 530 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 531 | if (!MO.isReg()) continue; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 532 | unsigned Reg = MO.getReg(); |
| 533 | if (Reg == 0) continue; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 534 | if (!MO.isDef()) continue; |
| 535 | // Ignore two-addr defs. |
| 536 | if (MI->isRegTiedToUseOperand(i)) continue; |
David Goodwin | 7441d14 | 2009-10-20 22:50:43 +0000 | [diff] [blame] | 537 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 538 | DefIndices[Reg] = Count; |
| 539 | KillIndices[Reg] = ~0u; |
| 540 | assert(((KillIndices[Reg] == ~0u) != |
| 541 | (DefIndices[Reg] == ~0u)) && |
| 542 | "Kill and Def maps aren't consistent for Reg!"); |
| 543 | KeepRegs.erase(Reg); |
| 544 | Classes[Reg] = 0; |
| 545 | RegRefs.erase(Reg); |
| 546 | // Repeat, for all subregs. |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 547 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 548 | *Subreg; ++Subreg) { |
| 549 | unsigned SubregReg = *Subreg; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 550 | DefIndices[SubregReg] = Count; |
| 551 | KillIndices[SubregReg] = ~0u; |
| 552 | KeepRegs.erase(SubregReg); |
| 553 | Classes[SubregReg] = 0; |
| 554 | RegRefs.erase(SubregReg); |
| 555 | } |
| 556 | // Conservatively mark super-registers as unusable. |
| 557 | for (const unsigned *Super = TRI->getSuperRegisters(Reg); |
| 558 | *Super; ++Super) { |
| 559 | unsigned SuperReg = *Super; |
| 560 | Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 561 | } |
| 562 | } |
| 563 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 564 | MachineOperand &MO = MI->getOperand(i); |
| 565 | if (!MO.isReg()) continue; |
| 566 | unsigned Reg = MO.getReg(); |
| 567 | if (Reg == 0) continue; |
| 568 | if (!MO.isUse()) continue; |
| 569 | |
| 570 | const TargetRegisterClass *NewRC = 0; |
| 571 | if (i < MI->getDesc().getNumOperands()) |
| 572 | NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); |
| 573 | |
| 574 | // For now, only allow the register to be changed if its register |
| 575 | // class is consistent across all uses. |
| 576 | if (!Classes[Reg] && NewRC) |
| 577 | Classes[Reg] = NewRC; |
| 578 | else if (!NewRC || Classes[Reg] != NewRC) |
| 579 | Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); |
| 580 | |
| 581 | RegRefs.insert(std::make_pair(Reg, &MO)); |
| 582 | |
| 583 | // It wasn't previously live but now it is, this is a kill. |
| 584 | if (KillIndices[Reg] == ~0u) { |
| 585 | KillIndices[Reg] = Count; |
| 586 | DefIndices[Reg] = ~0u; |
| 587 | assert(((KillIndices[Reg] == ~0u) != |
| 588 | (DefIndices[Reg] == ~0u)) && |
| 589 | "Kill and Def maps aren't consistent for Reg!"); |
| 590 | } |
| 591 | // Repeat, for all aliases. |
| 592 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 593 | unsigned AliasReg = *Alias; |
| 594 | if (KillIndices[AliasReg] == ~0u) { |
| 595 | KillIndices[AliasReg] = Count; |
| 596 | DefIndices[AliasReg] = ~0u; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 602 | unsigned |
| 603 | SchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg, |
| 604 | unsigned LastNewReg, |
| 605 | const TargetRegisterClass *RC) { |
| 606 | for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF), |
| 607 | RE = RC->allocation_order_end(MF); R != RE; ++R) { |
| 608 | unsigned NewReg = *R; |
| 609 | // Don't replace a register with itself. |
| 610 | if (NewReg == AntiDepReg) continue; |
| 611 | // Don't replace a register with one that was recently used to repair |
| 612 | // an anti-dependence with this AntiDepReg, because that would |
| 613 | // re-introduce that anti-dependence. |
| 614 | if (NewReg == LastNewReg) continue; |
| 615 | // If NewReg is dead and NewReg's most recent def is not before |
| 616 | // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg. |
| 617 | assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) && |
| 618 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
| 619 | assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) && |
| 620 | "Kill and Def maps aren't consistent for NewReg!"); |
| 621 | if (KillIndices[NewReg] != ~0u || |
| 622 | Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) || |
| 623 | KillIndices[AntiDepReg] > DefIndices[NewReg]) |
| 624 | continue; |
| 625 | return NewReg; |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // No registers are free and available! |
| 629 | return 0; |
| 630 | } |
| 631 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 632 | /// BreakAntiDependencies - Identifiy anti-dependencies along the critical path |
| 633 | /// of the ScheduleDAG and break them by renaming registers. |
| 634 | /// |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 635 | bool SchedulePostRATDList::BreakAntiDependencies() { |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 636 | // The code below assumes that there is at least one instruction, |
| 637 | // so just duck out immediately if the block is empty. |
| 638 | if (SUnits.empty()) return false; |
| 639 | |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 640 | // Find the node at the bottom of the critical path. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 641 | SUnit *Max = 0; |
| 642 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 643 | SUnit *SU = &SUnits[i]; |
| 644 | if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency) |
| 645 | Max = SU; |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 648 | #ifndef NDEBUG |
David Goodwin | 480c529 | 2009-10-20 19:54:44 +0000 | [diff] [blame] | 649 | { |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 650 | DEBUG(errs() << "Critical path has total latency " |
| 651 | << (Max->getDepth() + Max->Latency) << "\n"); |
David Goodwin | d452ea6 | 2009-10-13 19:16:03 +0000 | [diff] [blame] | 652 | DEBUG(errs() << "Available regs:"); |
| 653 | for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) { |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 654 | if (KillIndices[Reg] == ~0u) |
David Goodwin | d452ea6 | 2009-10-13 19:16:03 +0000 | [diff] [blame] | 655 | DEBUG(errs() << " " << TRI->getName(Reg)); |
| 656 | } |
| 657 | DEBUG(errs() << '\n'); |
| 658 | } |
| 659 | #endif |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 660 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 661 | // Track progress along the critical path through the SUnit graph as we walk |
| 662 | // the instructions. |
| 663 | SUnit *CriticalPathSU = Max; |
| 664 | MachineInstr *CriticalPathMI = CriticalPathSU->getInstr(); |
| 665 | |
| 666 | // Consider this pattern: |
| 667 | // A = ... |
| 668 | // ... = A |
| 669 | // A = ... |
| 670 | // ... = A |
| 671 | // A = ... |
| 672 | // ... = A |
| 673 | // A = ... |
| 674 | // ... = A |
| 675 | // There are three anti-dependencies here, and without special care, |
| 676 | // we'd break all of them using the same register: |
| 677 | // A = ... |
| 678 | // ... = A |
| 679 | // B = ... |
| 680 | // ... = B |
| 681 | // B = ... |
| 682 | // ... = B |
| 683 | // B = ... |
| 684 | // ... = B |
| 685 | // because at each anti-dependence, B is the first register that |
| 686 | // isn't A which is free. This re-introduces anti-dependencies |
| 687 | // at all but one of the original anti-dependencies that we were |
| 688 | // trying to break. To avoid this, keep track of the most recent |
| 689 | // register that each register was replaced with, avoid |
| 690 | // using it to repair an anti-dependence on the same register. |
| 691 | // This lets us produce this: |
| 692 | // A = ... |
| 693 | // ... = A |
| 694 | // B = ... |
| 695 | // ... = B |
| 696 | // C = ... |
| 697 | // ... = C |
| 698 | // B = ... |
| 699 | // ... = B |
| 700 | // This still has an anti-dependence on B, but at least it isn't on the |
| 701 | // original critical path. |
| 702 | // |
| 703 | // TODO: If we tracked more than one register here, we could potentially |
| 704 | // fix that remaining critical edge too. This is a little more involved, |
| 705 | // because unlike the most recent register, less recent registers should |
| 706 | // still be considered, though only if no other registers are available. |
| 707 | unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {}; |
| 708 | |
| 709 | // Attempt to break anti-dependence edges on the critical path. Walk the |
| 710 | // instructions from the bottom up, tracking information about liveness |
| 711 | // as we go to help determine which registers are available. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 712 | bool Changed = false; |
Dan Gohman | 47ac0f0 | 2009-02-11 04:27:20 +0000 | [diff] [blame] | 713 | unsigned Count = InsertPosIndex - 1; |
| 714 | for (MachineBasicBlock::iterator I = InsertPos, E = Begin; |
Dan Gohman | 43f07fb | 2009-02-03 18:57:45 +0000 | [diff] [blame] | 715 | I != E; --Count) { |
| 716 | MachineInstr *MI = --I; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 717 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 718 | // Check if this instruction has a dependence on the critical path that |
| 719 | // is an anti-dependence that we may be able to break. If it is, set |
| 720 | // AntiDepReg to the non-zero register associated with the anti-dependence. |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 721 | // |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 722 | // We limit our attention to the critical path as a heuristic to avoid |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 723 | // breaking anti-dependence edges that aren't going to significantly |
| 724 | // impact the overall schedule. There are a limited number of registers |
| 725 | // and we want to save them for the important edges. |
| 726 | // |
| 727 | // TODO: Instructions with multiple defs could have multiple |
| 728 | // anti-dependencies. The current code here only knows how to break one |
| 729 | // edge per instruction. Note that we'd have to be able to break all of |
| 730 | // the anti-dependencies in an instruction in order to be effective. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 731 | unsigned AntiDepReg = 0; |
| 732 | if (MI == CriticalPathMI) { |
| 733 | if (SDep *Edge = CriticalPathStep(CriticalPathSU)) { |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 734 | SUnit *NextSU = Edge->getSUnit(); |
| 735 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 736 | // Only consider anti-dependence edges. |
| 737 | if (Edge->getKind() == SDep::Anti) { |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 738 | AntiDepReg = Edge->getReg(); |
| 739 | assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 740 | if (!AllocatableSet.test(AntiDepReg)) |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 741 | // Don't break anti-dependencies on non-allocatable registers. |
| 742 | AntiDepReg = 0; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 743 | else if (KeepRegs.count(AntiDepReg)) |
| 744 | // Don't break anti-dependencies if an use down below requires |
| 745 | // this exact register. |
| 746 | AntiDepReg = 0; |
| 747 | else { |
| 748 | // If the SUnit has other dependencies on the SUnit that it |
| 749 | // anti-depends on, don't bother breaking the anti-dependency |
| 750 | // since those edges would prevent such units from being |
| 751 | // scheduled past each other regardless. |
| 752 | // |
| 753 | // Also, if there are dependencies on other SUnits with the |
| 754 | // same register as the anti-dependency, don't attempt to |
| 755 | // break it. |
| 756 | for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(), |
| 757 | PE = CriticalPathSU->Preds.end(); P != PE; ++P) |
| 758 | if (P->getSUnit() == NextSU ? |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 759 | (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : |
| 760 | (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 761 | AntiDepReg = 0; |
| 762 | break; |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 766 | CriticalPathSU = NextSU; |
| 767 | CriticalPathMI = CriticalPathSU->getInstr(); |
Dan Gohman | 00dc84a | 2008-12-16 19:27:52 +0000 | [diff] [blame] | 768 | } else { |
| 769 | // We've reached the end of the critical path. |
| 770 | CriticalPathSU = 0; |
| 771 | CriticalPathMI = 0; |
| 772 | } |
| 773 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 774 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 775 | PrescanInstruction(MI); |
| 776 | |
| 777 | if (MI->getDesc().hasExtraDefRegAllocReq()) |
| 778 | // If this instruction's defs have special allocation requirement, don't |
| 779 | // break this anti-dependency. |
Evan Cheng | 714e8bc | 2009-10-01 08:26:23 +0000 | [diff] [blame] | 780 | AntiDepReg = 0; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 781 | else if (AntiDepReg) { |
| 782 | // If this instruction has a use of AntiDepReg, breaking it |
| 783 | // is invalid. |
| 784 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 785 | MachineOperand &MO = MI->getOperand(i); |
| 786 | if (!MO.isReg()) continue; |
| 787 | unsigned Reg = MO.getReg(); |
| 788 | if (Reg == 0) continue; |
| 789 | if (MO.isUse() && AntiDepReg == Reg) { |
| 790 | AntiDepReg = 0; |
| 791 | break; |
| 792 | } |
| 793 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 796 | // Determine AntiDepReg's register class, if it is live and is |
| 797 | // consistently used within a single class. |
| 798 | const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0; |
| 799 | assert((AntiDepReg == 0 || RC != NULL) && |
| 800 | "Register should be live if it's causing an anti-dependence!"); |
| 801 | if (RC == reinterpret_cast<TargetRegisterClass *>(-1)) |
| 802 | AntiDepReg = 0; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 803 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 804 | // Look for a suitable register to use to break the anti-depenence. |
| 805 | // |
| 806 | // TODO: Instead of picking the first free register, consider which might |
| 807 | // be the best. |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 808 | if (AntiDepReg != 0) { |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 809 | if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg, |
| 810 | LastNewReg[AntiDepReg], |
| 811 | RC)) { |
| 812 | DEBUG(errs() << "Breaking anti-dependence edge on " |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 813 | << TRI->getName(AntiDepReg) |
| 814 | << " with " << RegRefs.count(AntiDepReg) << " references" |
| 815 | << " using " << TRI->getName(NewReg) << "!\n"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 816 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 817 | // Update the references to the old register to refer to the new |
| 818 | // register. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 819 | std::pair<std::multimap<unsigned, MachineOperand *>::iterator, |
| 820 | std::multimap<unsigned, MachineOperand *>::iterator> |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 821 | Range = RegRefs.equal_range(AntiDepReg); |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 822 | for (std::multimap<unsigned, MachineOperand *>::iterator |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 823 | Q = Range.first, QE = Range.second; Q != QE; ++Q) |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 824 | Q->second->setReg(NewReg); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 825 | |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 826 | // We just went back in time and modified history; the |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 827 | // liveness information for the anti-depenence reg is now |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 828 | // inconsistent. Set the state as if it were dead. |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 829 | Classes[NewReg] = Classes[AntiDepReg]; |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 830 | DefIndices[NewReg] = DefIndices[AntiDepReg]; |
| 831 | KillIndices[NewReg] = KillIndices[AntiDepReg]; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 832 | assert(((KillIndices[NewReg] == ~0u) != |
| 833 | (DefIndices[NewReg] == ~0u)) && |
| 834 | "Kill and Def maps aren't consistent for NewReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 835 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 836 | Classes[AntiDepReg] = 0; |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 837 | DefIndices[AntiDepReg] = KillIndices[AntiDepReg]; |
| 838 | KillIndices[AntiDepReg] = ~0u; |
| 839 | assert(((KillIndices[AntiDepReg] == ~0u) != |
| 840 | (DefIndices[AntiDepReg] == ~0u)) && |
| 841 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 842 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 843 | RegRefs.erase(AntiDepReg); |
Dan Gohman | 26255ad | 2009-08-12 01:33:27 +0000 | [diff] [blame] | 844 | Changed = true; |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 845 | LastNewReg[AntiDepReg] = NewReg; |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 849 | ScanInstruction(MI, Count); |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 850 | } |
Dan Gohman | 21d9003 | 2008-11-25 00:52:40 +0000 | [diff] [blame] | 851 | |
| 852 | return Changed; |
| 853 | } |
| 854 | |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 855 | /// StartBlockForKills - Initialize register live-range state for updating kills |
| 856 | /// |
| 857 | void SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { |
| 858 | // Initialize the indices to indicate that no registers are live. |
| 859 | std::fill(KillIndices, array_endof(KillIndices), ~0u); |
| 860 | |
| 861 | // Determine the live-out physregs for this block. |
| 862 | if (!BB->empty() && BB->back().getDesc().isReturn()) { |
| 863 | // In a return block, examine the function live-out regs. |
| 864 | for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), |
| 865 | E = MRI.liveout_end(); I != E; ++I) { |
| 866 | unsigned Reg = *I; |
| 867 | KillIndices[Reg] = BB->size(); |
| 868 | // Repeat, for all subregs. |
| 869 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 870 | *Subreg; ++Subreg) { |
| 871 | KillIndices[*Subreg] = BB->size(); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | else { |
| 876 | // In a non-return block, examine the live-in regs of all successors. |
| 877 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 878 | SE = BB->succ_end(); SI != SE; ++SI) { |
| 879 | for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), |
| 880 | E = (*SI)->livein_end(); I != E; ++I) { |
| 881 | unsigned Reg = *I; |
| 882 | KillIndices[Reg] = BB->size(); |
| 883 | // Repeat, for all subregs. |
| 884 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 885 | *Subreg; ++Subreg) { |
| 886 | KillIndices[*Subreg] = BB->size(); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 893 | bool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, |
| 894 | MachineOperand &MO) { |
| 895 | // Setting kill flag... |
| 896 | if (!MO.isKill()) { |
| 897 | MO.setIsKill(true); |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | // If MO itself is live, clear the kill flag... |
| 902 | if (KillIndices[MO.getReg()] != ~0u) { |
| 903 | MO.setIsKill(false); |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | // If any subreg of MO is live, then create an imp-def for that |
| 908 | // subreg and keep MO marked as killed. |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 909 | MO.setIsKill(false); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 910 | bool AllDead = true; |
| 911 | const unsigned SuperReg = MO.getReg(); |
| 912 | for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg); |
| 913 | *Subreg; ++Subreg) { |
| 914 | if (KillIndices[*Subreg] != ~0u) { |
| 915 | MI->addOperand(MachineOperand::CreateReg(*Subreg, |
| 916 | true /*IsDef*/, |
| 917 | true /*IsImp*/, |
| 918 | false /*IsKill*/, |
| 919 | false /*IsDead*/)); |
| 920 | AllDead = false; |
| 921 | } |
| 922 | } |
| 923 | |
Dan Gohman | c1ae8c9 | 2009-10-21 01:44:44 +0000 | [diff] [blame^] | 924 | if(AllDead) |
Benjamin Kramer | 8bff4af | 2009-10-02 15:59:52 +0000 | [diff] [blame] | 925 | MO.setIsKill(true); |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 929 | /// FixupKills - Fix the register kill flags, they may have been made |
| 930 | /// incorrect by instruction reordering. |
| 931 | /// |
| 932 | void SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { |
| 933 | DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n'); |
| 934 | |
| 935 | std::set<unsigned> killedRegs; |
| 936 | BitVector ReservedRegs = TRI->getReservedRegs(MF); |
David Goodwin | 5e41178 | 2009-09-03 22:15:25 +0000 | [diff] [blame] | 937 | |
| 938 | StartBlockForKills(MBB); |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 939 | |
| 940 | // Examine block from end to start... |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 941 | unsigned Count = MBB->size(); |
| 942 | for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); |
| 943 | I != E; --Count) { |
| 944 | MachineInstr *MI = --I; |
| 945 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 946 | // Update liveness. Registers that are defed but not used in this |
| 947 | // instruction are now dead. Mark register and all subregs as they |
| 948 | // are completely defined. |
| 949 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 950 | MachineOperand &MO = MI->getOperand(i); |
| 951 | if (!MO.isReg()) continue; |
| 952 | unsigned Reg = MO.getReg(); |
| 953 | if (Reg == 0) continue; |
| 954 | if (!MO.isDef()) continue; |
| 955 | // Ignore two-addr defs. |
| 956 | if (MI->isRegTiedToUseOperand(i)) continue; |
| 957 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 958 | KillIndices[Reg] = ~0u; |
| 959 | |
| 960 | // Repeat for all subregs. |
| 961 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 962 | *Subreg; ++Subreg) { |
| 963 | KillIndices[*Subreg] = ~0u; |
| 964 | } |
| 965 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 966 | |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 967 | // Examine all used registers and set/clear kill flag. When a |
| 968 | // register is used multiple times we only set the kill flag on |
| 969 | // the first use. |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 970 | killedRegs.clear(); |
| 971 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 972 | MachineOperand &MO = MI->getOperand(i); |
| 973 | if (!MO.isReg() || !MO.isUse()) continue; |
| 974 | unsigned Reg = MO.getReg(); |
| 975 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 976 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 977 | bool kill = false; |
| 978 | if (killedRegs.find(Reg) == killedRegs.end()) { |
| 979 | kill = true; |
| 980 | // A register is not killed if any subregs are live... |
| 981 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 982 | *Subreg; ++Subreg) { |
| 983 | if (KillIndices[*Subreg] != ~0u) { |
| 984 | kill = false; |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | // If subreg is not live, then register is killed if it became |
| 990 | // live in this instruction |
| 991 | if (kill) |
| 992 | kill = (KillIndices[Reg] == ~0u); |
| 993 | } |
| 994 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 995 | if (MO.isKill() != kill) { |
David Goodwin | 8f90934 | 2009-09-23 16:35:25 +0000 | [diff] [blame] | 996 | bool removed = ToggleKillFlag(MI, MO); |
| 997 | if (removed) { |
| 998 | DEBUG(errs() << "Fixed <removed> in "); |
| 999 | } else { |
| 1000 | DEBUG(errs() << "Fixed " << MO << " in "); |
| 1001 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1002 | DEBUG(MI->dump()); |
| 1003 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1004 | |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1005 | killedRegs.insert(Reg); |
| 1006 | } |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1007 | |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 1008 | // Mark any used register (that is not using undef) and subregs as |
| 1009 | // now live... |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1010 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1011 | MachineOperand &MO = MI->getOperand(i); |
David Goodwin | a3251db | 2009-08-31 20:47:02 +0000 | [diff] [blame] | 1012 | if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1013 | unsigned Reg = MO.getReg(); |
| 1014 | if ((Reg == 0) || ReservedRegs.test(Reg)) continue; |
| 1015 | |
David Goodwin | 7886cd8 | 2009-08-29 00:11:13 +0000 | [diff] [blame] | 1016 | KillIndices[Reg] = Count; |
| 1017 | |
| 1018 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 1019 | *Subreg; ++Subreg) { |
| 1020 | KillIndices[*Subreg] = Count; |
| 1021 | } |
| 1022 | } |
David Goodwin | 88a589c | 2009-08-25 17:03:05 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1026 | //===----------------------------------------------------------------------===// |
| 1027 | // Top-Down Scheduling |
| 1028 | //===----------------------------------------------------------------------===// |
| 1029 | |
| 1030 | /// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to |
| 1031 | /// the PendingQueue if the count reaches zero. Also update its cycle bound. |
Dan Gohman | 54e4c36 | 2008-12-09 22:54:47 +0000 | [diff] [blame] | 1032 | void SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { |
| 1033 | SUnit *SuccSU = SuccEdge->getSUnit(); |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 1034 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1035 | #ifndef NDEBUG |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 1036 | if (SuccSU->NumPredsLeft == 0) { |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 1037 | errs() << "*** Scheduling failed! ***\n"; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1038 | SuccSU->dump(this); |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 1039 | errs() << " has been released too many times!\n"; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1040 | llvm_unreachable(0); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1041 | } |
| 1042 | #endif |
Reid Kleckner | c277ab0 | 2009-09-30 20:15:38 +0000 | [diff] [blame] | 1043 | --SuccSU->NumPredsLeft; |
| 1044 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1045 | // Compute how many cycles it will be before this actually becomes |
| 1046 | // available. This is the max of the start time of all predecessors plus |
| 1047 | // their latencies. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1048 | SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1049 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1050 | // If all the node's predecessors are scheduled, this node is ready |
| 1051 | // to be scheduled. Ignore the special ExitSU node. |
| 1052 | if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1053 | PendingQueue.push_back(SuccSU); |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. |
| 1057 | void SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { |
| 1058 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 1059 | I != E; ++I) |
| 1060 | ReleaseSucc(SU, &*I); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending |
| 1064 | /// count of its successors. If a successor pending count is zero, add it to |
| 1065 | /// the Available queue. |
| 1066 | void SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { |
David Goodwin | 3a5f0d4 | 2009-08-11 01:44:26 +0000 | [diff] [blame] | 1067 | DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1068 | DEBUG(SU->dump(this)); |
| 1069 | |
| 1070 | Sequence.push_back(SU); |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1071 | assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); |
| 1072 | SU->setDepthToAtLeast(CurCycle); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1073 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1074 | ReleaseSuccessors(SU); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1075 | SU->isScheduled = true; |
| 1076 | AvailableQueue.ScheduledNode(SU); |
| 1077 | } |
| 1078 | |
| 1079 | /// ListScheduleTopDown - The main loop of list scheduling for top-down |
| 1080 | /// schedulers. |
| 1081 | void SchedulePostRATDList::ListScheduleTopDown() { |
| 1082 | unsigned CurCycle = 0; |
| 1083 | |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1084 | // Release any successors of the special Entry node. |
| 1085 | ReleaseSuccessors(&EntrySU); |
| 1086 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1087 | // All leaves to Available queue. |
| 1088 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
| 1089 | // It is available if it has no predecessors. |
| 1090 | if (SUnits[i].Preds.empty()) { |
| 1091 | AvailableQueue.push(&SUnits[i]); |
| 1092 | SUnits[i].isAvailable = true; |
| 1093 | } |
| 1094 | } |
Dan Gohman | 9e64bbb | 2009-02-10 23:27:53 +0000 | [diff] [blame] | 1095 | |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1096 | // In any cycle where we can't schedule any instructions, we must |
| 1097 | // stall or emit a noop, depending on the target. |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1098 | bool CycleHasInsts = false; |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1099 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1100 | // While Available queue is not empty, grab the node with the highest |
| 1101 | // priority. If it is not ready put it back. Schedule the node. |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1102 | std::vector<SUnit*> NotReady; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1103 | Sequence.reserve(SUnits.size()); |
| 1104 | while (!AvailableQueue.empty() || !PendingQueue.empty()) { |
| 1105 | // Check to see if any of the pending instructions are ready to issue. If |
| 1106 | // so, add them to the available queue. |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1107 | unsigned MinDepth = ~0u; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1108 | for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1109 | if (PendingQueue[i]->getDepth() <= CurCycle) { |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1110 | AvailableQueue.push(PendingQueue[i]); |
| 1111 | PendingQueue[i]->isAvailable = true; |
| 1112 | PendingQueue[i] = PendingQueue.back(); |
| 1113 | PendingQueue.pop_back(); |
| 1114 | --i; --e; |
Dan Gohman | 3f23744 | 2008-12-16 03:25:46 +0000 | [diff] [blame] | 1115 | } else if (PendingQueue[i]->getDepth() < MinDepth) |
| 1116 | MinDepth = PendingQueue[i]->getDepth(); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1117 | } |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 1118 | |
David Goodwin | 7cd0118 | 2009-08-11 17:56:42 +0000 | [diff] [blame] | 1119 | DEBUG(errs() << "\n*** Examining Available\n"; |
| 1120 | LatencyPriorityQueue q = AvailableQueue; |
| 1121 | while (!q.empty()) { |
| 1122 | SUnit *su = q.pop(); |
| 1123 | errs() << "Height " << su->getHeight() << ": "; |
| 1124 | su->dump(this); |
| 1125 | }); |
David Goodwin | c93d837 | 2009-08-11 17:35:23 +0000 | [diff] [blame] | 1126 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1127 | SUnit *FoundSUnit = 0; |
| 1128 | |
| 1129 | bool HasNoopHazards = false; |
| 1130 | while (!AvailableQueue.empty()) { |
| 1131 | SUnit *CurSUnit = AvailableQueue.pop(); |
| 1132 | |
| 1133 | ScheduleHazardRecognizer::HazardType HT = |
| 1134 | HazardRec->getHazardType(CurSUnit); |
| 1135 | if (HT == ScheduleHazardRecognizer::NoHazard) { |
| 1136 | FoundSUnit = CurSUnit; |
| 1137 | break; |
| 1138 | } |
| 1139 | |
| 1140 | // Remember if this is a noop hazard. |
| 1141 | HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; |
| 1142 | |
| 1143 | NotReady.push_back(CurSUnit); |
| 1144 | } |
| 1145 | |
| 1146 | // Add the nodes that aren't ready back onto the available list. |
| 1147 | if (!NotReady.empty()) { |
| 1148 | AvailableQueue.push_all(NotReady); |
| 1149 | NotReady.clear(); |
| 1150 | } |
| 1151 | |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1152 | // If we found a node to schedule, do it now. |
| 1153 | if (FoundSUnit) { |
| 1154 | ScheduleNodeTopDown(FoundSUnit, CurCycle); |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1155 | HazardRec->EmitInstruction(FoundSUnit); |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1156 | CycleHasInsts = true; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1157 | |
David Goodwin | d94a4e5 | 2009-08-10 15:55:25 +0000 | [diff] [blame] | 1158 | // If we are using the target-specific hazards, then don't |
| 1159 | // advance the cycle time just because we schedule a node. If |
| 1160 | // the target allows it we can schedule multiple nodes in the |
| 1161 | // same cycle. |
| 1162 | if (!EnablePostRAHazardAvoidance) { |
| 1163 | if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! |
| 1164 | ++CurCycle; |
| 1165 | } |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1166 | } else { |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1167 | if (CycleHasInsts) { |
David Goodwin | 2ffb0ce | 2009-08-12 21:47:46 +0000 | [diff] [blame] | 1168 | DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n'); |
| 1169 | HazardRec->AdvanceCycle(); |
| 1170 | } else if (!HasNoopHazards) { |
| 1171 | // Otherwise, we have a pipeline stall, but no other problem, |
| 1172 | // just advance the current cycle and try again. |
| 1173 | DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n'); |
| 1174 | HazardRec->AdvanceCycle(); |
| 1175 | ++NumStalls; |
| 1176 | } else { |
| 1177 | // Otherwise, we have no instructions to issue and we have instructions |
| 1178 | // that will fault if we don't do this right. This is the case for |
| 1179 | // processors without pipeline interlocks and other cases. |
| 1180 | DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n'); |
| 1181 | HazardRec->EmitNoop(); |
| 1182 | Sequence.push_back(0); // NULL here means noop |
| 1183 | ++NumNoops; |
| 1184 | } |
| 1185 | |
Dan Gohman | 2836c28 | 2009-01-16 01:33:36 +0000 | [diff] [blame] | 1186 | ++CurCycle; |
Benjamin Kramer | be441c0 | 2009-09-06 12:10:17 +0000 | [diff] [blame] | 1187 | CycleHasInsts = false; |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | #ifndef NDEBUG |
Dan Gohman | a1e6d36 | 2008-11-20 01:26:25 +0000 | [diff] [blame] | 1192 | VerifySchedule(/*isBottomUp=*/false); |
Dan Gohman | 343f0c0 | 2008-11-19 23:18:57 +0000 | [diff] [blame] | 1193 | #endif |
| 1194 | } |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1195 | |
| 1196 | //===----------------------------------------------------------------------===// |
| 1197 | // Public Constructor Functions |
| 1198 | //===----------------------------------------------------------------------===// |
| 1199 | |
Evan Cheng | fa16354 | 2009-10-16 21:06:15 +0000 | [diff] [blame] | 1200 | FunctionPass *llvm::createPostRAScheduler(CodeGenOpt::Level OptLevel) { |
| 1201 | return new PostRAScheduler(OptLevel); |
Dale Johannesen | e7e7d0d | 2007-07-13 17:13:54 +0000 | [diff] [blame] | 1202 | } |