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