PostRASchedulerList.cpp revision 0dad89fa94536284d51f60868326294b725a0c61
172f159640382a16e036b63dcb9c0b427e6d5dc0aDale Johannesen//===----- SchedulePostRAList.cpp - list scheduler ------------------------===// 2e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// 3e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// The LLVM Compiler Infrastructure 4e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// 8e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen//===----------------------------------------------------------------------===// 9e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// 10e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// This implements a top-down list scheduler, using standard algorithms. 11e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// The basic approach uses a priority queue of available nodes to schedule. 12e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// One at a time, nodes are taken from the priority queue (thus in priority 13e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// order), checked for legality to schedule, and emitted if legal. 14e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// 15e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// Nodes may not be legal to schedule either due to structural hazards (e.g. 16e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// pipeline or resource constraints) or because an input to the instruction has 17e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// not completed execution. 18e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// 19e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen//===----------------------------------------------------------------------===// 20e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 21e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#define DEBUG_TYPE "post-RA-sched" 22d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin#include "ExactHazardRecognizer.h" 23d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin#include "SimpleHazardRecognizer.h" 246dc75fe5279e2c12bda13dcc4a1a13908de8596fDan Gohman#include "ScheduleDAGInstrs.h" 25e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/CodeGen/Passes.h" 26343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/LatencyPriorityQueue.h" 27343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/SchedulerRegistry.h" 283f23744df4809eba94284e601e81489212c974d4Dan Gohman#include "llvm/CodeGen/MachineDominators.h" 29e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/CodeGen/MachineFunctionPass.h" 303f23744df4809eba94284e601e81489212c974d4Dan Gohman#include "llvm/CodeGen/MachineLoopInfo.h" 3121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h" 322836c283bb1c14baa50994f60769d665da608ad7Dan Gohman#include "llvm/CodeGen/ScheduleHazardRecognizer.h" 33bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman#include "llvm/Target/TargetLowering.h" 3479ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman#include "llvm/Target/TargetMachine.h" 3521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/Target/TargetInstrInfo.h" 3621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/Target/TargetRegisterInfo.h" 370dad89fa94536284d51f60868326294b725a0c61David Goodwin#include "llvm/Target/TargetSubtarget.h" 38459525df1e003597077197b5f802bd5d9cd7d94cChris Lattner#include "llvm/Support/Compiler.h" 39e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/Support/Debug.h" 40c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h" 413a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin#include "llvm/Support/raw_ostream.h" 42343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/ADT/Statistic.h" 4321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include <map> 4488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin#include <set> 45e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesenusing namespace llvm; 46e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 472836c283bb1c14baa50994f60769d665da608ad7Dan GohmanSTATISTIC(NumNoops, "Number of noops inserted"); 48343f0c046702831a4a6aec951b6a297a23241a55Dan GohmanSTATISTIC(NumStalls, "Number of pipeline stalls"); 49343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 5021d9003087c9a707e6cd95460136b499df358fb8Dan Gohmanstatic cl::opt<bool> 5121d9003087c9a707e6cd95460136b499df358fb8Dan GohmanEnableAntiDepBreaking("break-anti-dependencies", 5200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman cl::desc("Break post-RA scheduling anti-dependencies"), 5300dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman cl::init(true), cl::Hidden); 5421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 552836c283bb1c14baa50994f60769d665da608ad7Dan Gohmanstatic cl::opt<bool> 562836c283bb1c14baa50994f60769d665da608ad7Dan GohmanEnablePostRAHazardAvoidance("avoid-hazards", 57d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin cl::desc("Enable exact hazard avoidance"), 585e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin cl::init(true), cl::Hidden); 592836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 601f1522839838a33e69d68656a423a244e19dffb8David Goodwin// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod 611f1522839838a33e69d68656a423a244e19dffb8David Goodwinstatic cl::opt<int> 621f1522839838a33e69d68656a423a244e19dffb8David GoodwinDebugDiv("postra-sched-debugdiv", 631f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::desc("Debug control MBBs that are scheduled"), 641f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::init(0), cl::Hidden); 651f1522839838a33e69d68656a423a244e19dffb8David Goodwinstatic cl::opt<int> 661f1522839838a33e69d68656a423a244e19dffb8David GoodwinDebugMod("postra-sched-debugmod", 671f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::desc("Debug control MBBs that are scheduled"), 681f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::init(0), cl::Hidden); 691f1522839838a33e69d68656a423a244e19dffb8David Goodwin 70e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesennamespace { 71343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman class VISIBILITY_HIDDEN PostRAScheduler : public MachineFunctionPass { 72e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen public: 73e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen static char ID; 74343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PostRAScheduler() : MachineFunctionPass(&ID) {} 7521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 763f23744df4809eba94284e601e81489212c974d4Dan Gohman void getAnalysisUsage(AnalysisUsage &AU) const { 77845012e6d31799c7fbd1193fa1af8ee2d12e9231Dan Gohman AU.setPreservesCFG(); 783f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addRequired<MachineDominatorTree>(); 793f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addPreserved<MachineDominatorTree>(); 803f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addRequired<MachineLoopInfo>(); 813f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addPreserved<MachineLoopInfo>(); 823f23744df4809eba94284e601e81489212c974d4Dan Gohman MachineFunctionPass::getAnalysisUsage(AU); 833f23744df4809eba94284e601e81489212c974d4Dan Gohman } 843f23744df4809eba94284e601e81489212c974d4Dan Gohman 85343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman const char *getPassName() const { 8621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman return "Post RA top-down list latency scheduler"; 87343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 88343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 89343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman bool runOnMachineFunction(MachineFunction &Fn); 90343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman }; 91343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman char PostRAScheduler::ID = 0; 92343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 93343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman class VISIBILITY_HIDDEN SchedulePostRATDList : public ScheduleDAGInstrs { 94343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// AvailableQueue - The priority queue to use for the available SUnits. 95343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// 96343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman LatencyPriorityQueue AvailableQueue; 97343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 98343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// PendingQueue - This contains all of the instructions whose operands have 99343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// been issued, but their results are not ready yet (due to the latency of 100343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// the operation). Once the operands becomes available, the instruction is 101343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// added to the AvailableQueue. 102343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman std::vector<SUnit*> PendingQueue; 103343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 10421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman /// Topo - A topological ordering for SUnits. 10521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman ScheduleDAGTopologicalSort Topo; 106e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 10779ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman /// AllocatableSet - The set of allocatable registers. 10879ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman /// We'll be ignoring anti-dependencies on non-allocatable registers, 10979ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman /// because they may not be safe to break. 11079ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman const BitVector AllocatableSet; 11179ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman 1122836c283bb1c14baa50994f60769d665da608ad7Dan Gohman /// HazardRec - The hazard recognizer to use. 1132836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ScheduleHazardRecognizer *HazardRec; 1142836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 1159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// Classes - For live regs that are only used in one register class in a 1169e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// live range, the register class. If the register is not live, the 1179e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// corresponding value is null. If the register is live but used in 1189e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// multiple register classes, the corresponding value is -1 casted to a 1199e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// pointer. 1209e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman const TargetRegisterClass * 1219e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[TargetRegisterInfo::FirstVirtualRegister]; 1229e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 1239e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// RegRegs - Map registers to all their references within a live range. 1249e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman std::multimap<unsigned, MachineOperand *> RegRefs; 1259e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 1269e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// The index of the most recent kill (proceding bottom-up), or ~0u if 1279e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// the register is not live. 1289e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned KillIndices[TargetRegisterInfo::FirstVirtualRegister]; 1299e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 1309e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// The index of the most recent complete def (proceding bottom up), or ~0u 1319e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// if the register is live. 1329e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned DefIndices[TargetRegisterInfo::FirstVirtualRegister]; 1339e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 13421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman public: 13579ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman SchedulePostRATDList(MachineFunction &MF, 1363f23744df4809eba94284e601e81489212c974d4Dan Gohman const MachineLoopInfo &MLI, 1372836c283bb1c14baa50994f60769d665da608ad7Dan Gohman const MachineDominatorTree &MDT, 1382836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ScheduleHazardRecognizer *HR) 13979ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman : ScheduleDAGInstrs(MF, MLI, MDT), Topo(SUnits), 1402836c283bb1c14baa50994f60769d665da608ad7Dan Gohman AllocatableSet(TRI->getAllocatableSet(MF)), 1412836c283bb1c14baa50994f60769d665da608ad7Dan Gohman HazardRec(HR) {} 1422836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 1432836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ~SchedulePostRATDList() { 1442836c283bb1c14baa50994f60769d665da608ad7Dan Gohman delete HazardRec; 1452836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 146343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1479e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// StartBlock - Initialize register live-range state for scheduling in 1489e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// this block. 1499e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// 1509e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman void StartBlock(MachineBasicBlock *BB); 1519e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 1529e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// Schedule - Schedule the instruction range using list scheduling. 1539e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// 154343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman void Schedule(); 15588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 15688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin /// FixupKills - Fix register kill flags that have been made 15788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin /// invalid due to scheduling 15888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin /// 15988a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin void FixupKills(MachineBasicBlock *MBB); 160343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1619e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// Observe - Update liveness information to account for the current 1629e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// instruction, which will not be scheduled. 1639e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// 16447ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman void Observe(MachineInstr *MI, unsigned Count); 1659e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 1669e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// FinishBlock - Clean up register live-range state. 1679e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// 1689e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman void FinishBlock(); 1699e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 170343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman private: 1719e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman void PrescanInstruction(MachineInstr *MI); 1729e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman void ScanInstruction(MachineInstr *MI, unsigned Count); 17354e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohman void ReleaseSucc(SUnit *SU, SDep *SuccEdge); 1749e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman void ReleaseSuccessors(SUnit *SU); 175343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); 176343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman void ListScheduleTopDown(); 17721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman bool BreakAntiDependencies(); 17826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman unsigned findSuitableFreeRegister(unsigned AntiDepReg, 17926255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman unsigned LastNewReg, 18026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman const TargetRegisterClass *); 1815e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin void StartBlockForKills(MachineBasicBlock *BB); 1828f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 1838f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // ToggleKillFlag - Toggle a register operand kill flag. Other 1848f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // adjustments may be made to the instruction if necessary. Return 1858f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // true if the operand has been deleted, false if not. 1868f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); 187e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen }; 188e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen} 189e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 1909e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// isSchedulingBoundary - Test if the given instruction should be 1919e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// considered a scheduling boundary. This primarily includes labels 1929e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// and terminators. 1939e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 1949e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohmanstatic bool isSchedulingBoundary(const MachineInstr *MI, 1959e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman const MachineFunction &MF) { 1969e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Terminators and labels can't be scheduled around. 1979e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (MI->getDesc().isTerminator() || MI->isLabel()) 1989e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman return true; 1999e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 200bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman // Don't attempt to schedule around any instruction that modifies 201bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman // a stack-oriented pointer, as it's unlikely to be profitable. This 202bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman // saves compile time, because it doesn't require every single 203bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman // stack slot reference to depend on the instruction that does the 204bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman // modification. 205bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); 206bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore())) 207bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman return true; 208bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman 2099e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman return false; 2109e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 2119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 212343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanbool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { 2130dad89fa94536284d51f60868326294b725a0c61David Goodwin // Check that post-RA scheduling is enabled for this function 2140dad89fa94536284d51f60868326294b725a0c61David Goodwin const TargetSubtarget &ST = Fn.getTarget().getSubtarget<TargetSubtarget>(); 2150dad89fa94536284d51f60868326294b725a0c61David Goodwin if (!ST.enablePostRAScheduler()) 2160dad89fa94536284d51f60868326294b725a0c61David Goodwin return true; 2170dad89fa94536284d51f60868326294b725a0c61David Goodwin 2183a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin DEBUG(errs() << "PostRAScheduler\n"); 219e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 2203f23744df4809eba94284e601e81489212c974d4Dan Gohman const MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 2213f23744df4809eba94284e601e81489212c974d4Dan Gohman const MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); 222d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin const InstrItineraryData &InstrItins = Fn.getTarget().getInstrItineraryData(); 2232836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ScheduleHazardRecognizer *HR = EnablePostRAHazardAvoidance ? 224d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin (ScheduleHazardRecognizer *)new ExactHazardRecognizer(InstrItins) : 225d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin (ScheduleHazardRecognizer *)new SimpleHazardRecognizer(); 2263f23744df4809eba94284e601e81489212c974d4Dan Gohman 2272836c283bb1c14baa50994f60769d665da608ad7Dan Gohman SchedulePostRATDList Scheduler(Fn, MLI, MDT, HR); 22879ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman 229e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen // Loop over all of the basic blocks 230e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); 231343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman MBB != MBBe; ++MBB) { 2321f1522839838a33e69d68656a423a244e19dffb8David Goodwin#ifndef NDEBUG 2331f1522839838a33e69d68656a423a244e19dffb8David Goodwin // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod 2341f1522839838a33e69d68656a423a244e19dffb8David Goodwin if (DebugDiv > 0) { 2351f1522839838a33e69d68656a423a244e19dffb8David Goodwin static int bbcnt = 0; 2361f1522839838a33e69d68656a423a244e19dffb8David Goodwin if (bbcnt++ % DebugDiv != DebugMod) 2371f1522839838a33e69d68656a423a244e19dffb8David Goodwin continue; 2381f1522839838a33e69d68656a423a244e19dffb8David Goodwin errs() << "*** DEBUG scheduling " << Fn.getFunction()->getNameStr() << 2391f1522839838a33e69d68656a423a244e19dffb8David Goodwin ":MBB ID#" << MBB->getNumber() << " ***\n"; 2401f1522839838a33e69d68656a423a244e19dffb8David Goodwin } 2411f1522839838a33e69d68656a423a244e19dffb8David Goodwin#endif 2421f1522839838a33e69d68656a423a244e19dffb8David Goodwin 2439e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Initialize register live-range state for scheduling in this block. 2449e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Scheduler.StartBlock(MBB); 2459e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 246f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman // Schedule each sequence of instructions not interrupted by a label 247f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman // or anything else that effectively needs to shut down scheduling. 2489e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman MachineBasicBlock::iterator Current = MBB->end(); 24947ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman unsigned Count = MBB->size(), CurrentCount = Count; 2509e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { 2519e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman MachineInstr *MI = prior(I); 2529e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (isSchedulingBoundary(MI, Fn)) { 2531274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman Scheduler.Run(MBB, I, Current, CurrentCount); 254fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng Scheduler.EmitSchedule(0); 2559e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Current = MI; 25647ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman CurrentCount = Count - 1; 2571274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman Scheduler.Observe(MI, CurrentCount); 258f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman } 2599e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman I = MI; 26047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman --Count; 26147ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman } 26247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman assert(Count == 0 && "Instruction count mismatch!"); 2639e8bd0b362c034e629b9ee1f32e4e1adf037f529Duncan Sands assert((MBB->begin() == Current || CurrentCount != 0) && 2641274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman "Instruction count mismatch!"); 2651274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman Scheduler.Run(MBB, MBB->begin(), Current, CurrentCount); 266fb2e752e4175920d0531f2afc93a23d0cdf4db14Evan Cheng Scheduler.EmitSchedule(0); 2679e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 2689e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Clean up register live-range state. 2699e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Scheduler.FinishBlock(); 27088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 2715e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Update register kills 27288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin Scheduler.FixupKills(MBB); 273343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 274e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 275e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen return true; 276e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen} 277e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 2789e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// StartBlock - Initialize register live-range state for scheduling in 2799e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// this block. 2809e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 2819e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohmanvoid SchedulePostRATDList::StartBlock(MachineBasicBlock *BB) { 2829e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Call the superclass. 2839e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ScheduleDAGInstrs::StartBlock(BB); 2849e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 285d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin // Reset the hazard recognizer. 286d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin HazardRec->Reset(); 287d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin 2889e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Clear out the register class data. 2899e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman std::fill(Classes, array_endof(Classes), 2909e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman static_cast<const TargetRegisterClass *>(0)); 2919e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 2929e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Initialize the indices to indicate that no registers are live. 2939e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman std::fill(KillIndices, array_endof(KillIndices), ~0u); 2949e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman std::fill(DefIndices, array_endof(DefIndices), BB->size()); 2959e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 2969e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Determine the live-out physregs for this block. 2979e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!BB->empty() && BB->back().getDesc().isReturn()) 2989e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // In a return block, examine the function live-out regs. 2999e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), 3009e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman E = MRI.liveout_end(); I != E; ++I) { 3019e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned Reg = *I; 3029e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 3039e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[Reg] = BB->size(); 3049e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[Reg] = ~0u; 3059e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Repeat, for all aliases. 3069e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { 3079e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned AliasReg = *Alias; 3089e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); 3099e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[AliasReg] = BB->size(); 3109e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[AliasReg] = ~0u; 3119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 3129e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 3139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman else 3149e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // In a non-return block, examine the live-in regs of all successors. 3159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), 31647ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman SE = BB->succ_end(); SI != SE; ++SI) 3179e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), 3189e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman E = (*SI)->livein_end(); I != E; ++I) { 3199e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned Reg = *I; 3209e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 3219e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[Reg] = BB->size(); 3229e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[Reg] = ~0u; 3239e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Repeat, for all aliases. 3249e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { 3259e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned AliasReg = *Alias; 3269e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); 3279e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[AliasReg] = BB->size(); 3289e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[AliasReg] = ~0u; 3299e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 3309e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 3319e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 3325e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Consider callee-saved registers as live-out, since we're running after 3335e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // prologue/epilogue insertion so there's no way to add additional 3345e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // saved registers. 3355e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // 3365e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // TODO: there is a new method 3375e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // MachineFrameInfo::getPristineRegs(MBB). It gives you a list of 3385e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // CSRs that have not been saved when entering the MBB. The 3395e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // remaining CSRs have been saved and can be treated like call 3405e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // clobbered registers. 3415e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) { 3425e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin unsigned Reg = *I; 3435e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 3445e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin KillIndices[Reg] = BB->size(); 3455e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin DefIndices[Reg] = ~0u; 3465e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Repeat, for all aliases. 3475e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { 3485e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin unsigned AliasReg = *Alias; 3495e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); 3505e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin KillIndices[AliasReg] = BB->size(); 3515e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin DefIndices[AliasReg] = ~0u; 3529e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 3539e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 3549e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 3559e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 3569e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// Schedule - Schedule the instruction range using list scheduling. 3579e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 358343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanvoid SchedulePostRATDList::Schedule() { 3593a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin DEBUG(errs() << "********** List Scheduling **********\n"); 360343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 361c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman // Build the scheduling graph. 362c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman BuildSchedGraph(); 363343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 36421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman if (EnableAntiDepBreaking) { 36521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman if (BreakAntiDependencies()) { 36621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // We made changes. Update the dependency graph. 36721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // Theoretically we could update the graph in place: 36821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // When a live range is changed to use a different register, remove 36921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // the def's anti-dependence *and* output-dependence edges due to 37021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // that register, and add new anti-dependence and output-dependence 37121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // edges based on the next live range of the register. 37221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman SUnits.clear(); 3739e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman EntrySU = SUnit(); 3749e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ExitSU = SUnit(); 375c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman BuildSchedGraph(); 37621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 37721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 37821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 379d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) 380d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin SUnits[su].dumpAll(this)); 381d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin 382343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.initNodes(SUnits); 38321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 384343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman ListScheduleTopDown(); 385343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 386343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.releaseState(); 387343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 388343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 3899e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// Observe - Update liveness information to account for the current 3909e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// instruction, which will not be scheduled. 3919e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 39247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohmanvoid SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { 3931274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman assert(Count < InsertPosIndex && "Instruction index out of expected range!"); 3941274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman 3951274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // Any register which was defined within the previous scheduling region 3961274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // may have been rescheduled and its lifetime may overlap with registers 3971274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // in ways not reflected in our current liveness state. For each such 3981274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // register, adjust the liveness state to be conservatively correct. 3991274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) 4001274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) { 4011274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman assert(KillIndices[Reg] == ~0u && "Clobbered register is live!"); 4021274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // Mark this register to be non-renamable. 4031274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 4041274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // Move the def index to the end of the previous region, to reflect 4051274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman // that the def could theoretically have been scheduled at the end. 4061274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman DefIndices[Reg] = InsertPosIndex; 4071274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman } 4081274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman 4099e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman PrescanInstruction(MI); 41047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman ScanInstruction(MI, Count); 4119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 4129e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// FinishBlock - Clean up register live-range state. 4149e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 4159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohmanvoid SchedulePostRATDList::FinishBlock() { 4169e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman RegRefs.clear(); 4179e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4189e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Call the superclass. 4199e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ScheduleDAGInstrs::FinishBlock(); 4209e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 4219e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4223f23744df4809eba94284e601e81489212c974d4Dan Gohman/// CriticalPathStep - Return the next SUnit after SU on the bottom-up 4233f23744df4809eba94284e601e81489212c974d4Dan Gohman/// critical path. 4243f23744df4809eba94284e601e81489212c974d4Dan Gohmanstatic SDep *CriticalPathStep(SUnit *SU) { 4253f23744df4809eba94284e601e81489212c974d4Dan Gohman SDep *Next = 0; 4263f23744df4809eba94284e601e81489212c974d4Dan Gohman unsigned NextDepth = 0; 4273f23744df4809eba94284e601e81489212c974d4Dan Gohman // Find the predecessor edge with the greatest depth. 4283f23744df4809eba94284e601e81489212c974d4Dan Gohman for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); 4293f23744df4809eba94284e601e81489212c974d4Dan Gohman P != PE; ++P) { 4303f23744df4809eba94284e601e81489212c974d4Dan Gohman SUnit *PredSU = P->getSUnit(); 4313f23744df4809eba94284e601e81489212c974d4Dan Gohman unsigned PredLatency = P->getLatency(); 4323f23744df4809eba94284e601e81489212c974d4Dan Gohman unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; 4333f23744df4809eba94284e601e81489212c974d4Dan Gohman // In the case of a latency tie, prefer an anti-dependency edge over 4343f23744df4809eba94284e601e81489212c974d4Dan Gohman // other types of edges. 4353f23744df4809eba94284e601e81489212c974d4Dan Gohman if (NextDepth < PredTotalLatency || 4363f23744df4809eba94284e601e81489212c974d4Dan Gohman (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { 4373f23744df4809eba94284e601e81489212c974d4Dan Gohman NextDepth = PredTotalLatency; 4383f23744df4809eba94284e601e81489212c974d4Dan Gohman Next = &*P; 4393f23744df4809eba94284e601e81489212c974d4Dan Gohman } 4403f23744df4809eba94284e601e81489212c974d4Dan Gohman } 4413f23744df4809eba94284e601e81489212c974d4Dan Gohman return Next; 4423f23744df4809eba94284e601e81489212c974d4Dan Gohman} 4433f23744df4809eba94284e601e81489212c974d4Dan Gohman 4449e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohmanvoid SchedulePostRATDList::PrescanInstruction(MachineInstr *MI) { 4459e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Scan the register operands for this instruction and update 4469e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Classes and RegRefs. 4479e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 4489e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman MachineOperand &MO = MI->getOperand(i); 4499e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!MO.isReg()) continue; 4509e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned Reg = MO.getReg(); 4519e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (Reg == 0) continue; 4522a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner const TargetRegisterClass *NewRC = 0; 4532a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner 4542a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner if (i < MI->getDesc().getNumOperands()) 4552a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); 4569e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4579e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // For now, only allow the register to be changed if its register 4589e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // class is consistent across all uses. 4599e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!Classes[Reg] && NewRC) 4609e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = NewRC; 4619e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman else if (!NewRC || Classes[Reg] != NewRC) 4629e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 4639e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4649e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Now check for aliases. 4659e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { 4669e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // If an alias of the reg is used during the live range, give up. 4679e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Note that this allows us to skip checking if AntiDepReg 4689e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // overlaps with any of the aliases, among other things. 4699e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned AliasReg = *Alias; 4709e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (Classes[AliasReg]) { 4719e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1); 4729e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 4739e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 4749e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 4759e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4769e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // If we're still willing to consider this register, note the reference. 4779e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1)) 4789e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman RegRefs.insert(std::make_pair(Reg, &MO)); 4799e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 4809e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 4819e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4829e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohmanvoid SchedulePostRATDList::ScanInstruction(MachineInstr *MI, 4839e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned Count) { 4849e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Update liveness. 4859e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Proceding upwards, registers that are defed but not used in this 4869e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // instruction are now dead. 4879e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 4889e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman MachineOperand &MO = MI->getOperand(i); 4899e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!MO.isReg()) continue; 4909e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned Reg = MO.getReg(); 4919e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (Reg == 0) continue; 4929e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!MO.isDef()) continue; 4939e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Ignore two-addr defs. 494d9df5017040489303acb57bdd8697ef0f8bafc08Bob Wilson if (MI->isRegTiedToUseOperand(i)) continue; 4959e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4969e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[Reg] = Count; 4979e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[Reg] = ~0u; 49847ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman assert(((KillIndices[Reg] == ~0u) != 49947ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman (DefIndices[Reg] == ~0u)) && 50047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman "Kill and Def maps aren't consistent for Reg!"); 5019e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = 0; 5029e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman RegRefs.erase(Reg); 5039e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Repeat, for all subregs. 5049e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (const unsigned *Subreg = TRI->getSubRegisters(Reg); 5059e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman *Subreg; ++Subreg) { 5069e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned SubregReg = *Subreg; 5079e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[SubregReg] = Count; 5089e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[SubregReg] = ~0u; 5099e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[SubregReg] = 0; 5109e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman RegRefs.erase(SubregReg); 5119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5127886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Conservatively mark super-registers as unusable. 5139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (const unsigned *Super = TRI->getSuperRegisters(Reg); 5149e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman *Super; ++Super) { 5159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned SuperReg = *Super; 5169e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1); 5179e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5189e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5199e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 5209e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman MachineOperand &MO = MI->getOperand(i); 5219e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!MO.isReg()) continue; 5229e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned Reg = MO.getReg(); 5239e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (Reg == 0) continue; 5249e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!MO.isUse()) continue; 5259e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 5262a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner const TargetRegisterClass *NewRC = 0; 5272a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner if (i < MI->getDesc().getNumOperands()) 5282a3868849438a0a0ad4f9a50f2b94eb1639b554eChris Lattner NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI); 5299e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 5309e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // For now, only allow the register to be changed if its register 5319e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // class is consistent across all uses. 5329e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (!Classes[Reg] && NewRC) 5339e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = NewRC; 5349e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman else if (!NewRC || Classes[Reg] != NewRC) 5359e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 5369e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 5379e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman RegRefs.insert(std::make_pair(Reg, &MO)); 5389e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 5399e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // It wasn't previously live but now it is, this is a kill. 5409e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (KillIndices[Reg] == ~0u) { 5419e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[Reg] = Count; 5429e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[Reg] = ~0u; 54347ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman assert(((KillIndices[Reg] == ~0u) != 54447ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman (DefIndices[Reg] == ~0u)) && 54547ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman "Kill and Def maps aren't consistent for Reg!"); 5469e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5479e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Repeat, for all aliases. 5489e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { 5499e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman unsigned AliasReg = *Alias; 5509e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (KillIndices[AliasReg] == ~0u) { 5519e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman KillIndices[AliasReg] = Count; 5529e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman DefIndices[AliasReg] = ~0u; 5539e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5549e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5559e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman } 5569e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 5579e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 55826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohmanunsigned 55926255adcaa5836fafe32e5e296d81df63a0fb9c5Dan GohmanSchedulePostRATDList::findSuitableFreeRegister(unsigned AntiDepReg, 56026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman unsigned LastNewReg, 56126255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman const TargetRegisterClass *RC) { 56226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF), 56326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman RE = RC->allocation_order_end(MF); R != RE; ++R) { 56426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman unsigned NewReg = *R; 56526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // Don't replace a register with itself. 56626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman if (NewReg == AntiDepReg) continue; 56726255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // Don't replace a register with one that was recently used to repair 56826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // an anti-dependence with this AntiDepReg, because that would 56926255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // re-introduce that anti-dependence. 57026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman if (NewReg == LastNewReg) continue; 57126255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // If NewReg is dead and NewReg's most recent def is not before 57226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg. 57326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u)) && 57426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman "Kill and Def maps aren't consistent for AntiDepReg!"); 57526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u)) && 57626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman "Kill and Def maps aren't consistent for NewReg!"); 577da2775767b34a503735b78422c3558815933cc94Dan Gohman if (KillIndices[NewReg] != ~0u || 578da2775767b34a503735b78422c3558815933cc94Dan Gohman Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) || 579da2775767b34a503735b78422c3558815933cc94Dan Gohman KillIndices[AntiDepReg] > DefIndices[NewReg]) 58026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman continue; 58126255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman return NewReg; 58226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman } 58326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman 58426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // No registers are free and available! 58526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman return 0; 58626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman} 58726255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman 58821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman/// BreakAntiDependencies - Identifiy anti-dependencies along the critical path 58921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman/// of the ScheduleDAG and break them by renaming registers. 59021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman/// 59121d9003087c9a707e6cd95460136b499df358fb8Dan Gohmanbool SchedulePostRATDList::BreakAntiDependencies() { 59221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // The code below assumes that there is at least one instruction, 59321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // so just duck out immediately if the block is empty. 5948554449e311e50f2e96db1081a17ccf7151ef7f6Dan Gohman if (SUnits.empty()) return false; 59521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 5963f23744df4809eba94284e601e81489212c974d4Dan Gohman // Find the node at the bottom of the critical path. 59721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman SUnit *Max = 0; 5983f23744df4809eba94284e601e81489212c974d4Dan Gohman for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { 5993f23744df4809eba94284e601e81489212c974d4Dan Gohman SUnit *SU = &SUnits[i]; 6003f23744df4809eba94284e601e81489212c974d4Dan Gohman if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency) 60121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman Max = SU; 60221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 60321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 6043a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin DEBUG(errs() << "Critical path has total latency " 6053a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin << (Max->getDepth() + Max->Latency) << "\n"); 60621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 60700dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // Track progress along the critical path through the SUnit graph as we walk 60800dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // the instructions. 60900dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman SUnit *CriticalPathSU = Max; 61000dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman MachineInstr *CriticalPathMI = CriticalPathSU->getInstr(); 61121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 61221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // Consider this pattern: 61321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // A = ... 61421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = A 61521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // A = ... 61621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = A 61721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // A = ... 61821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = A 61921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // A = ... 62021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = A 62121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // There are three anti-dependencies here, and without special care, 62221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // we'd break all of them using the same register: 62321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // A = ... 62421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = A 62521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // B = ... 62621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = B 62721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // B = ... 62821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = B 62921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // B = ... 63021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = B 63121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // because at each anti-dependence, B is the first register that 63221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // isn't A which is free. This re-introduces anti-dependencies 63321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // at all but one of the original anti-dependencies that we were 63421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // trying to break. To avoid this, keep track of the most recent 635c93d8373c93159c590838957b3194900caeb8a03David Goodwin // register that each register was replaced with, avoid 63621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // using it to repair an anti-dependence on the same register. 63721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // This lets us produce this: 63821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // A = ... 63921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = A 64021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // B = ... 64121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = B 64221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // C = ... 64321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = C 64421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // B = ... 64521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // ... = B 64621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // This still has an anti-dependence on B, but at least it isn't on the 64721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // original critical path. 64821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // 64921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // TODO: If we tracked more than one register here, we could potentially 65021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // fix that remaining critical edge too. This is a little more involved, 65121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // because unlike the most recent register, less recent registers should 65221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // still be considered, though only if no other registers are available. 65321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {}; 65421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 65521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // Attempt to break anti-dependence edges on the critical path. Walk the 65621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // instructions from the bottom up, tracking information about liveness 65721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // as we go to help determine which registers are available. 65821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman bool Changed = false; 65947ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman unsigned Count = InsertPosIndex - 1; 66047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman for (MachineBasicBlock::iterator I = InsertPos, E = Begin; 66143f07fb6c3e3d775d8e5f1eaeb380b93bb3bc09dDan Gohman I != E; --Count) { 66243f07fb6c3e3d775d8e5f1eaeb380b93bb3bc09dDan Gohman MachineInstr *MI = --I; 66321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 664544df3651e15f31ae2c14588c1272fd870560250Jakob Stoklund Olesen // After regalloc, KILL instructions aren't safe to treat as 665544df3651e15f31ae2c14588c1272fd870560250Jakob Stoklund Olesen // dependence-breaking. In the case of an INSERT_SUBREG, the KILL 666490b1833a96bf0391ebdfba7828d43a8c3512851Dan Gohman // is left behind appearing to clobber the super-register, while the 667490b1833a96bf0391ebdfba7828d43a8c3512851Dan Gohman // subregister needs to remain live. So we just ignore them. 668544df3651e15f31ae2c14588c1272fd870560250Jakob Stoklund Olesen if (MI->getOpcode() == TargetInstrInfo::KILL) 669490b1833a96bf0391ebdfba7828d43a8c3512851Dan Gohman continue; 670490b1833a96bf0391ebdfba7828d43a8c3512851Dan Gohman 67100dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // Check if this instruction has a dependence on the critical path that 67200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // is an anti-dependence that we may be able to break. If it is, set 67300dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // AntiDepReg to the non-zero register associated with the anti-dependence. 67400dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // 67500dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // We limit our attention to the critical path as a heuristic to avoid 67600dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // breaking anti-dependence edges that aren't going to significantly 67700dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // impact the overall schedule. There are a limited number of registers 67800dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // and we want to save them for the important edges. 67900dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // 68000dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // TODO: Instructions with multiple defs could have multiple 68100dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // anti-dependencies. The current code here only knows how to break one 68200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // edge per instruction. Note that we'd have to be able to break all of 68300dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // the anti-dependencies in an instruction in order to be effective. 68400dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman unsigned AntiDepReg = 0; 68500dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman if (MI == CriticalPathMI) { 68600dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman if (SDep *Edge = CriticalPathStep(CriticalPathSU)) { 68700dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman SUnit *NextSU = Edge->getSUnit(); 68800dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman 68900dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // Only consider anti-dependence edges. 69000dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman if (Edge->getKind() == SDep::Anti) { 69100dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman AntiDepReg = Edge->getReg(); 69200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); 69300dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // Don't break anti-dependencies on non-allocatable registers. 69449bb50e0b65d4646a1d44eec3196c003c13caa96Dan Gohman if (!AllocatableSet.test(AntiDepReg)) 69549bb50e0b65d4646a1d44eec3196c003c13caa96Dan Gohman AntiDepReg = 0; 69649bb50e0b65d4646a1d44eec3196c003c13caa96Dan Gohman else { 69700dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // If the SUnit has other dependencies on the SUnit that it 69800dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // anti-depends on, don't bother breaking the anti-dependency 69900dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // since those edges would prevent such units from being 70000dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // scheduled past each other regardless. 70100dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // 70200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // Also, if there are dependencies on other SUnits with the 70300dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // same register as the anti-dependency, don't attempt to 70400dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // break it. 70500dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman for (SUnit::pred_iterator P = CriticalPathSU->Preds.begin(), 70600dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman PE = CriticalPathSU->Preds.end(); P != PE; ++P) 70700dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman if (P->getSUnit() == NextSU ? 70800dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : 70900dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { 71000dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman AntiDepReg = 0; 71100dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman break; 71200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman } 71300dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman } 71400dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman } 71500dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman CriticalPathSU = NextSU; 71600dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman CriticalPathMI = CriticalPathSU->getInstr(); 71700dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman } else { 71800dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman // We've reached the end of the critical path. 71900dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman CriticalPathSU = 0; 72000dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman CriticalPathMI = 0; 72100dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman } 72200dc84a2ca81be8adfee63ea0aa2a5abcb623060Dan Gohman } 72321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 7249e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman PrescanInstruction(MI); 7259e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 7269e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // If this instruction has a use of AntiDepReg, breaking it 7279e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // is invalid. 72821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 72921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman MachineOperand &MO = MI->getOperand(i); 73021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman if (!MO.isReg()) continue; 73121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman unsigned Reg = MO.getReg(); 73221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman if (Reg == 0) continue; 7339e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (MO.isUse() && AntiDepReg == Reg) { 73421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman AntiDepReg = 0; 7359e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman break; 73621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 73721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 73821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 73921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // Determine AntiDepReg's register class, if it is live and is 74021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // consistently used within a single class. 74121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0; 742a89d102b32d74fed5d3c7aeea8869a11c3074063Nick Lewycky assert((AntiDepReg == 0 || RC != NULL) && 74321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman "Register should be live if it's causing an anti-dependence!"); 74421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman if (RC == reinterpret_cast<TargetRegisterClass *>(-1)) 74521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman AntiDepReg = 0; 74621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 74721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // Look for a suitable register to use to break the anti-depenence. 74821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // 74921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // TODO: Instead of picking the first free register, consider which might 75021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // be the best. 75121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman if (AntiDepReg != 0) { 75226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman if (unsigned NewReg = findSuitableFreeRegister(AntiDepReg, 75326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman LastNewReg[AntiDepReg], 75426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman RC)) { 75526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman DEBUG(errs() << "Breaking anti-dependence edge on " 75626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman << TRI->getName(AntiDepReg) 75726255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman << " with " << RegRefs.count(AntiDepReg) << " references" 75826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman << " using " << TRI->getName(NewReg) << "!\n"); 75926255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman 76026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // Update the references to the old register to refer to the new 76126255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // register. 76226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman std::pair<std::multimap<unsigned, MachineOperand *>::iterator, 76326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman std::multimap<unsigned, MachineOperand *>::iterator> 76426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman Range = RegRefs.equal_range(AntiDepReg); 76526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman for (std::multimap<unsigned, MachineOperand *>::iterator 76626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman Q = Range.first, QE = Range.second; Q != QE; ++Q) 76726255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman Q->second->setReg(NewReg); 76826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman 76926255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // We just went back in time and modified history; the 77026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // liveness information for the anti-depenence reg is now 77126255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman // inconsistent. Set the state as if it were dead. 77226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman Classes[NewReg] = Classes[AntiDepReg]; 77326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman DefIndices[NewReg] = DefIndices[AntiDepReg]; 77426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman KillIndices[NewReg] = KillIndices[AntiDepReg]; 77526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman assert(((KillIndices[NewReg] == ~0u) != 77626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman (DefIndices[NewReg] == ~0u)) && 77726255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman "Kill and Def maps aren't consistent for NewReg!"); 77826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman 77926255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman Classes[AntiDepReg] = 0; 78026255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman DefIndices[AntiDepReg] = KillIndices[AntiDepReg]; 78126255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman KillIndices[AntiDepReg] = ~0u; 78226255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman assert(((KillIndices[AntiDepReg] == ~0u) != 78326255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman (DefIndices[AntiDepReg] == ~0u)) && 78426255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman "Kill and Def maps aren't consistent for AntiDepReg!"); 78526255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman 78626255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman RegRefs.erase(AntiDepReg); 78726255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman Changed = true; 78826255adcaa5836fafe32e5e296d81df63a0fb9c5Dan Gohman LastNewReg[AntiDepReg] = NewReg; 78921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 79021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 79121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 7929e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ScanInstruction(MI, Count); 79321d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 79421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 79521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman return Changed; 79621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman} 79721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 7985e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin/// StartBlockForKills - Initialize register live-range state for updating kills 7995e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin/// 8005e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwinvoid SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { 8015e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Initialize the indices to indicate that no registers are live. 8025e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin std::fill(KillIndices, array_endof(KillIndices), ~0u); 8035e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin 8045e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Determine the live-out physregs for this block. 8055e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin if (!BB->empty() && BB->back().getDesc().isReturn()) { 8065e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // In a return block, examine the function live-out regs. 8075e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(), 8085e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin E = MRI.liveout_end(); I != E; ++I) { 8095e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin unsigned Reg = *I; 8105e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin KillIndices[Reg] = BB->size(); 8115e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Repeat, for all subregs. 8125e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (const unsigned *Subreg = TRI->getSubRegisters(Reg); 8135e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin *Subreg; ++Subreg) { 8145e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin KillIndices[*Subreg] = BB->size(); 8155e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8165e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8175e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8185e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin else { 8195e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // In a non-return block, examine the live-in regs of all successors. 8205e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), 8215e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin SE = BB->succ_end(); SI != SE; ++SI) { 8225e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), 8235e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin E = (*SI)->livein_end(); I != E; ++I) { 8245e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin unsigned Reg = *I; 8255e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin KillIndices[Reg] = BB->size(); 8265e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Repeat, for all subregs. 8275e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin for (const unsigned *Subreg = TRI->getSubRegisters(Reg); 8285e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin *Subreg; ++Subreg) { 8295e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin KillIndices[*Subreg] = BB->size(); 8305e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8315e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8325e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8335e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 8345e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin} 8355e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin 8368f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwinbool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, 8378f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MachineOperand &MO) { 8388f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // Setting kill flag... 8398f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin if (!MO.isKill()) { 8408f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MO.setIsKill(true); 8418f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin return false; 8428f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 8438f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 8448f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // If MO itself is live, clear the kill flag... 8458f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin if (KillIndices[MO.getReg()] != ~0u) { 8468f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MO.setIsKill(false); 8478f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin return false; 8488f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 8498f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 8508f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // If any subreg of MO is live, then create an imp-def for that 8518f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // subreg and keep MO marked as killed. 8528f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin bool AllDead = true; 8538f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin const unsigned SuperReg = MO.getReg(); 8548f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin for (const unsigned *Subreg = TRI->getSubRegisters(SuperReg); 8558f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin *Subreg; ++Subreg) { 8568f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin if (KillIndices[*Subreg] != ~0u) { 8578f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MI->addOperand(MachineOperand::CreateReg(*Subreg, 8588f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin true /*IsDef*/, 8598f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin true /*IsImp*/, 8608f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin false /*IsKill*/, 8618f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin false /*IsDead*/)); 8628f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin AllDead = false; 8638f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 8648f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 8658f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 8668f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MO.setIsKill(AllDead); 8678f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin return false; 8688f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin} 8698f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 87088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// FixupKills - Fix the register kill flags, they may have been made 87188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// incorrect by instruction reordering. 87288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// 87388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwinvoid SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { 87488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin DEBUG(errs() << "Fixup kills for BB ID#" << MBB->getNumber() << '\n'); 87588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 87688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin std::set<unsigned> killedRegs; 87788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin BitVector ReservedRegs = TRI->getReservedRegs(MF); 8785e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin 8795e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin StartBlockForKills(MBB); 8807886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 8817886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Examine block from end to start... 88288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin unsigned Count = MBB->size(); 88388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); 88488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin I != E; --Count) { 88588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin MachineInstr *MI = --I; 88688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 8877886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Update liveness. Registers that are defed but not used in this 8887886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // instruction are now dead. Mark register and all subregs as they 8897886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // are completely defined. 8907886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 8917886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin MachineOperand &MO = MI->getOperand(i); 8927886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (!MO.isReg()) continue; 8937886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin unsigned Reg = MO.getReg(); 8947886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (Reg == 0) continue; 8957886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (!MO.isDef()) continue; 8967886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Ignore two-addr defs. 8977886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (MI->isRegTiedToUseOperand(i)) continue; 8987886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 8997886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin KillIndices[Reg] = ~0u; 9007886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 9017886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Repeat for all subregs. 9027886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (const unsigned *Subreg = TRI->getSubRegisters(Reg); 9037886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin *Subreg; ++Subreg) { 9047886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin KillIndices[*Subreg] = ~0u; 9057886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 9067886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 90788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 9088f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // Examine all used registers and set/clear kill flag. When a 9098f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // register is used multiple times we only set the kill flag on 9108f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // the first use. 91188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin killedRegs.clear(); 91288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 91388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin MachineOperand &MO = MI->getOperand(i); 91488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin if (!MO.isReg() || !MO.isUse()) continue; 91588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin unsigned Reg = MO.getReg(); 91688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin if ((Reg == 0) || ReservedRegs.test(Reg)) continue; 91788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 9187886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin bool kill = false; 9197886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (killedRegs.find(Reg) == killedRegs.end()) { 9207886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin kill = true; 9217886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // A register is not killed if any subregs are live... 9227886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (const unsigned *Subreg = TRI->getSubRegisters(Reg); 9237886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin *Subreg; ++Subreg) { 9247886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (KillIndices[*Subreg] != ~0u) { 9257886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin kill = false; 9267886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin break; 9277886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 9287886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 9297886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 9307886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // If subreg is not live, then register is killed if it became 9317886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // live in this instruction 9327886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (kill) 9337886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin kill = (KillIndices[Reg] == ~0u); 9347886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 9357886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 93688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin if (MO.isKill() != kill) { 9378f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin bool removed = ToggleKillFlag(MI, MO); 9388f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin if (removed) { 9398f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin DEBUG(errs() << "Fixed <removed> in "); 9408f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } else { 9418f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin DEBUG(errs() << "Fixed " << MO << " in "); 9428f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 94388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin DEBUG(MI->dump()); 94488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin } 9457886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 94688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin killedRegs.insert(Reg); 94788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin } 9487886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 949a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin // Mark any used register (that is not using undef) and subregs as 950a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin // now live... 9517886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 9527886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin MachineOperand &MO = MI->getOperand(i); 953a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; 9547886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin unsigned Reg = MO.getReg(); 9557886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if ((Reg == 0) || ReservedRegs.test(Reg)) continue; 9567886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 9577886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin KillIndices[Reg] = Count; 9587886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 9597886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (const unsigned *Subreg = TRI->getSubRegisters(Reg); 9607886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin *Subreg; ++Subreg) { 9617886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin KillIndices[*Subreg] = Count; 9627886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 9637886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 96488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin } 96588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin} 96688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 967343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===// 968343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// Top-Down Scheduling 969343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===// 970343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 971343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to 972343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// the PendingQueue if the count reaches zero. Also update its cycle bound. 97354e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohmanvoid SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { 97454e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohman SUnit *SuccSU = SuccEdge->getSUnit(); 975343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman --SuccSU->NumPredsLeft; 976343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 977343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG 978343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman if (SuccSU->NumPredsLeft < 0) { 979103289e9383ad1eb66caf28c9b166aebce963a35Chris Lattner errs() << "*** Scheduling failed! ***\n"; 980343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman SuccSU->dump(this); 981103289e9383ad1eb66caf28c9b166aebce963a35Chris Lattner errs() << " has been released too many times!\n"; 982c23197a26f34f559ea9797de51e187087c039c42Torok Edwin llvm_unreachable(0); 983343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 984343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#endif 985343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 986343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // Compute how many cycles it will be before this actually becomes 987343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // available. This is the max of the start time of all predecessors plus 988343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // their latencies. 9893f23744df4809eba94284e601e81489212c974d4Dan Gohman SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); 990343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 9919e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // If all the node's predecessors are scheduled, this node is ready 9929e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // to be scheduled. Ignore the special ExitSU node. 9939e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) 994343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue.push_back(SuccSU); 9959e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 9969e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 9979e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. 9989e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohmanvoid SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { 9999e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 10009e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman I != E; ++I) 10019e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ReleaseSucc(SU, &*I); 1002343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 1003343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1004343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending 1005343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// count of its successors. If a successor pending count is zero, add it to 1006343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// the Available queue. 1007343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanvoid SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { 10083a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin DEBUG(errs() << "*** Scheduling [" << CurCycle << "]: "); 1009343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman DEBUG(SU->dump(this)); 1010343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1011343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman Sequence.push_back(SU); 10123f23744df4809eba94284e601e81489212c974d4Dan Gohman assert(CurCycle >= SU->getDepth() && "Node scheduled above its depth!"); 10133f23744df4809eba94284e601e81489212c974d4Dan Gohman SU->setDepthToAtLeast(CurCycle); 1014343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 10159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ReleaseSuccessors(SU); 1016343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman SU->isScheduled = true; 1017343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.ScheduledNode(SU); 1018343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 1019343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1020343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ListScheduleTopDown - The main loop of list scheduling for top-down 1021343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// schedulers. 1022343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanvoid SchedulePostRATDList::ListScheduleTopDown() { 1023343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman unsigned CurCycle = 0; 1024343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 10259e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Release any successors of the special Entry node. 10269e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman ReleaseSuccessors(&EntrySU); 10279e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 1028343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // All leaves to Available queue. 1029343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { 1030343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // It is available if it has no predecessors. 1031343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman if (SUnits[i].Preds.empty()) { 1032343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.push(&SUnits[i]); 1033343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman SUnits[i].isAvailable = true; 1034343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 1035343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 10369e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 10372ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // In any cycle where we can't schedule any instructions, we must 10382ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // stall or emit a noop, depending on the target. 1039be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer bool CycleHasInsts = false; 10402ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin 1041343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // While Available queue is not empty, grab the node with the highest 1042343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // priority. If it is not ready put it back. Schedule the node. 10432836c283bb1c14baa50994f60769d665da608ad7Dan Gohman std::vector<SUnit*> NotReady; 1044343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman Sequence.reserve(SUnits.size()); 1045343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman while (!AvailableQueue.empty() || !PendingQueue.empty()) { 1046343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // Check to see if any of the pending instructions are ready to issue. If 1047343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // so, add them to the available queue. 10483f23744df4809eba94284e601e81489212c974d4Dan Gohman unsigned MinDepth = ~0u; 1049343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { 10503f23744df4809eba94284e601e81489212c974d4Dan Gohman if (PendingQueue[i]->getDepth() <= CurCycle) { 1051343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.push(PendingQueue[i]); 1052343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue[i]->isAvailable = true; 1053343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue[i] = PendingQueue.back(); 1054343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue.pop_back(); 1055343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman --i; --e; 10563f23744df4809eba94284e601e81489212c974d4Dan Gohman } else if (PendingQueue[i]->getDepth() < MinDepth) 10573f23744df4809eba94284e601e81489212c974d4Dan Gohman MinDepth = PendingQueue[i]->getDepth(); 1058343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 1059c93d8373c93159c590838957b3194900caeb8a03David Goodwin 10607cd0118c367314308cf94765ac12f81020a56271David Goodwin DEBUG(errs() << "\n*** Examining Available\n"; 10617cd0118c367314308cf94765ac12f81020a56271David Goodwin LatencyPriorityQueue q = AvailableQueue; 10627cd0118c367314308cf94765ac12f81020a56271David Goodwin while (!q.empty()) { 10637cd0118c367314308cf94765ac12f81020a56271David Goodwin SUnit *su = q.pop(); 10647cd0118c367314308cf94765ac12f81020a56271David Goodwin errs() << "Height " << su->getHeight() << ": "; 10657cd0118c367314308cf94765ac12f81020a56271David Goodwin su->dump(this); 10667cd0118c367314308cf94765ac12f81020a56271David Goodwin }); 1067c93d8373c93159c590838957b3194900caeb8a03David Goodwin 10682836c283bb1c14baa50994f60769d665da608ad7Dan Gohman SUnit *FoundSUnit = 0; 10692836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 10702836c283bb1c14baa50994f60769d665da608ad7Dan Gohman bool HasNoopHazards = false; 10712836c283bb1c14baa50994f60769d665da608ad7Dan Gohman while (!AvailableQueue.empty()) { 10722836c283bb1c14baa50994f60769d665da608ad7Dan Gohman SUnit *CurSUnit = AvailableQueue.pop(); 10732836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 10742836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ScheduleHazardRecognizer::HazardType HT = 10752836c283bb1c14baa50994f60769d665da608ad7Dan Gohman HazardRec->getHazardType(CurSUnit); 10762836c283bb1c14baa50994f60769d665da608ad7Dan Gohman if (HT == ScheduleHazardRecognizer::NoHazard) { 10772836c283bb1c14baa50994f60769d665da608ad7Dan Gohman FoundSUnit = CurSUnit; 10782836c283bb1c14baa50994f60769d665da608ad7Dan Gohman break; 10792836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 10802836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 10812836c283bb1c14baa50994f60769d665da608ad7Dan Gohman // Remember if this is a noop hazard. 10822836c283bb1c14baa50994f60769d665da608ad7Dan Gohman HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; 10832836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 10842836c283bb1c14baa50994f60769d665da608ad7Dan Gohman NotReady.push_back(CurSUnit); 10852836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 10862836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 10872836c283bb1c14baa50994f60769d665da608ad7Dan Gohman // Add the nodes that aren't ready back onto the available list. 10882836c283bb1c14baa50994f60769d665da608ad7Dan Gohman if (!NotReady.empty()) { 10892836c283bb1c14baa50994f60769d665da608ad7Dan Gohman AvailableQueue.push_all(NotReady); 10902836c283bb1c14baa50994f60769d665da608ad7Dan Gohman NotReady.clear(); 10912836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 10922836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 1093343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // If we found a node to schedule, do it now. 1094343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman if (FoundSUnit) { 1095343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman ScheduleNodeTopDown(FoundSUnit, CurCycle); 10962836c283bb1c14baa50994f60769d665da608ad7Dan Gohman HazardRec->EmitInstruction(FoundSUnit); 1097be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer CycleHasInsts = true; 1098343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1099d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin // If we are using the target-specific hazards, then don't 1100d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin // advance the cycle time just because we schedule a node. If 1101d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin // the target allows it we can schedule multiple nodes in the 1102d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin // same cycle. 1103d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin if (!EnablePostRAHazardAvoidance) { 1104d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin if (FoundSUnit->Latency) // Don't increment CurCycle for pseudo-ops! 1105d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin ++CurCycle; 1106d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin } 11072836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } else { 1108be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer if (CycleHasInsts) { 11092ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin DEBUG(errs() << "*** Finished cycle " << CurCycle << '\n'); 11102ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin HazardRec->AdvanceCycle(); 11112ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin } else if (!HasNoopHazards) { 11122ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // Otherwise, we have a pipeline stall, but no other problem, 11132ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // just advance the current cycle and try again. 11142ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin DEBUG(errs() << "*** Stall in cycle " << CurCycle << '\n'); 11152ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin HazardRec->AdvanceCycle(); 11162ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin ++NumStalls; 11172ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin } else { 11182ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // Otherwise, we have no instructions to issue and we have instructions 11192ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // that will fault if we don't do this right. This is the case for 11202ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // processors without pipeline interlocks and other cases. 11212ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin DEBUG(errs() << "*** Emitting noop in cycle " << CurCycle << '\n'); 11222ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin HazardRec->EmitNoop(); 11232ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin Sequence.push_back(0); // NULL here means noop 11242ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin ++NumNoops; 11252ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin } 11262ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin 11272836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ++CurCycle; 1128be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer CycleHasInsts = false; 1129343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 1130343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 1131343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1132343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG 1133a1e6d363e5efa9eb1a2e7ac21a0394c870bef5adDan Gohman VerifySchedule(/*isBottomUp=*/false); 1134343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#endif 1135343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 1136e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 1137e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen//===----------------------------------------------------------------------===// 1138e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen// Public Constructor Functions 1139e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen//===----------------------------------------------------------------------===// 1140e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 1141e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale JohannesenFunctionPass *llvm::createPostRAScheduler() { 1142343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman return new PostRAScheduler(); 1143e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen} 1144