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" 22d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/Passes.h" 23348777110a960f0e017025dd5141cb29472c3984David Goodwin#include "AggressiveAntiDepBreaker.h" 24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "AntiDepBreaker.h" 252e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin#include "CriticalAntiDepBreaker.h" 26d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/BitVector.h" 27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Statistic.h" 28d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/AliasAnalysis.h" 29343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/CodeGen/LatencyPriorityQueue.h" 303f23744df4809eba94284e601e81489212c974d4Dan Gohman#include "llvm/CodeGen/MachineDominators.h" 31c7951f8e09a65e07626e7b9a272ce9911b249472David Goodwin#include "llvm/CodeGen/MachineFrameInfo.h" 32e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/CodeGen/MachineFunctionPass.h" 337b79b9862c9e6fc31ec072acb09171fd6ec7b0e0Jakob Stoklund Olesen#include "llvm/CodeGen/MachineInstrBuilder.h" 343f23744df4809eba94284e601e81489212c974d4Dan Gohman#include "llvm/CodeGen/MachineLoopInfo.h" 3521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h" 361525260b3e50cc578939ef41b60609689eecfdd2Andrew Trick#include "llvm/CodeGen/RegisterClassInfo.h" 37ed395c8c475692f5a43eb4b5c5562503d67616d0Andrew Trick#include "llvm/CodeGen/ScheduleDAGInstrs.h" 382836c283bb1c14baa50994f60769d665da608ad7Dan Gohman#include "llvm/CodeGen/ScheduleHazardRecognizer.h" 39d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/CodeGen/SchedulerRegistry.h" 40e10deca33e74a7c70ab585f78eee3fb52937f668David Goodwin#include "llvm/Support/CommandLine.h" 41e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/Support/Debug.h" 42c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h" 433a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin#include "llvm/Support/raw_ostream.h" 44d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetInstrInfo.h" 45d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetLowering.h" 46d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetMachine.h" 47d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetRegisterInfo.h" 48d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetSubtargetInfo.h" 49e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesenusing namespace llvm; 50e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 512836c283bb1c14baa50994f60769d665da608ad7Dan GohmanSTATISTIC(NumNoops, "Number of noops inserted"); 52343f0c046702831a4a6aec951b6a297a23241a55Dan GohmanSTATISTIC(NumStalls, "Number of pipeline stalls"); 532e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid GoodwinSTATISTIC(NumFixedAnti, "Number of fixed anti-dependencies"); 54343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 55471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin// Post-RA scheduling is enabled with 565b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to 57471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin// override the target. 58471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwinstatic cl::opt<bool> 59471850ab84301dd47cab2bf8d694fcb5766c1169David GoodwinEnablePostRAScheduler("post-RA-scheduler", 60471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin cl::desc("Enable scheduling after register allocation"), 619843a93e830e76f96e9a997b3002624a28ca5aa6David Goodwin cl::init(false), cl::Hidden); 622e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwinstatic cl::opt<std::string> 6321d9003087c9a707e6cd95460136b499df358fb8Dan GohmanEnableAntiDepBreaking("break-anti-dependencies", 642e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin cl::desc("Break post-RA scheduling anti-dependencies: " 652e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin "\"critical\", \"all\", or \"none\""), 662e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin cl::init("none"), cl::Hidden); 672836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 681f1522839838a33e69d68656a423a244e19dffb8David Goodwin// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod 691f1522839838a33e69d68656a423a244e19dffb8David Goodwinstatic cl::opt<int> 701f1522839838a33e69d68656a423a244e19dffb8David GoodwinDebugDiv("postra-sched-debugdiv", 711f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::desc("Debug control MBBs that are scheduled"), 721f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::init(0), cl::Hidden); 731f1522839838a33e69d68656a423a244e19dffb8David Goodwinstatic cl::opt<int> 741f1522839838a33e69d68656a423a244e19dffb8David GoodwinDebugMod("postra-sched-debugmod", 751f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::desc("Debug control MBBs that are scheduled"), 761f1522839838a33e69d68656a423a244e19dffb8David Goodwin cl::init(0), cl::Hidden); 771f1522839838a33e69d68656a423a244e19dffb8David Goodwin 78ada0ef86d98df4ac36808e4a0f0098250bf1a842David GoodwinAntiDepBreaker::~AntiDepBreaker() { } 79ada0ef86d98df4ac36808e4a0f0098250bf1a842David Goodwin 80e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesennamespace { 816726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky class PostRAScheduler : public MachineFunctionPass { 8286050dc8cc0aaea8c9dfeb89de02cafbd7f48d92Evan Cheng const TargetInstrInfo *TII; 83fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen RegisterClassInfo RegClassInfo; 84a70dca156fa76d452f54829b5c5f962ddfd94ef2Dan Gohman 85e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen public: 86e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen static char ID; 87c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick PostRAScheduler() : MachineFunctionPass(ID) {} 8821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 893f23744df4809eba94284e601e81489212c974d4Dan Gohman void getAnalysisUsage(AnalysisUsage &AU) const { 90845012e6d31799c7fbd1193fa1af8ee2d12e9231Dan Gohman AU.setPreservesCFG(); 91a70dca156fa76d452f54829b5c5f962ddfd94ef2Dan Gohman AU.addRequired<AliasAnalysis>(); 92c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick AU.addRequired<TargetPassConfig>(); 933f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addRequired<MachineDominatorTree>(); 943f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addPreserved<MachineDominatorTree>(); 953f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addRequired<MachineLoopInfo>(); 963f23744df4809eba94284e601e81489212c974d4Dan Gohman AU.addPreserved<MachineLoopInfo>(); 973f23744df4809eba94284e601e81489212c974d4Dan Gohman MachineFunctionPass::getAnalysisUsage(AU); 983f23744df4809eba94284e601e81489212c974d4Dan Gohman } 993f23744df4809eba94284e601e81489212c974d4Dan Gohman 100343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman bool runOnMachineFunction(MachineFunction &Fn); 101343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman }; 102343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman char PostRAScheduler::ID = 0; 103343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 1046726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky class SchedulePostRATDList : public ScheduleDAGInstrs { 105343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// AvailableQueue - The priority queue to use for the available SUnits. 106c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// 107343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman LatencyPriorityQueue AvailableQueue; 1089001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 109343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// PendingQueue - This contains all of the instructions whose operands have 110343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// been issued, but their results are not ready yet (due to the latency of 111343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// the operation). Once the operands becomes available, the instruction is 112343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman /// added to the AvailableQueue. 113343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman std::vector<SUnit*> PendingQueue; 114343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 115c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// HazardRec - The hazard recognizer to use. 116c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman ScheduleHazardRecognizer *HazardRec; 117c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman 1182e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin /// AntiDepBreak - Anti-dependence breaking object, or NULL if none 1192e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin AntiDepBreaker *AntiDepBreak; 1202e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin 121c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// AA - AliasAnalysis for making memory reference queries. 122c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman AliasAnalysis *AA; 123480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin 12446252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer /// LiveRegs - true if the register is live. 12546252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer BitVector LiveRegs; 1269e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 12747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick /// The schedule. Null SUnit*'s represent noop instructions. 12847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick std::vector<SUnit*> Sequence; 12947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick 13021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman public: 1312da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick SchedulePostRATDList( 1322da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, 133fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen AliasAnalysis *AA, const RegisterClassInfo&, 1345b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, 13544d23825d61d530b8d562329ec8fc2d4f843bb8dCraig Topper SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs); 1362da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick 1372da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick ~SchedulePostRATDList(); 138343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 139953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick /// startBlock - Initialize register live-range state for scheduling in 1409e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// this block. 1419e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// 142953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick void startBlock(MachineBasicBlock *BB); 1439e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 14447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick /// Initialize the scheduler state for the next scheduling region. 14547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick virtual void enterRegion(MachineBasicBlock *bb, 14647c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick MachineBasicBlock::iterator begin, 14747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick MachineBasicBlock::iterator end, 14847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick unsigned endcount); 14947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick 15047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick /// Notify that the scheduler has finished scheduling the current region. 15147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick virtual void exitRegion(); 15247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick 153480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin /// Schedule - Schedule the instruction range using list scheduling. 1549e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman /// 155953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick void schedule(); 1569001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 15784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick void EmitSchedule(); 15884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick 159c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// Observe - Update liveness information to account for the current 160c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// instruction, which will not be scheduled. 161c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// 162c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman void Observe(MachineInstr *MI, unsigned Count); 163480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin 164953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick /// finishBlock - Clean up register live-range state. 165c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman /// 166953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick void finishBlock(); 167480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin 1682e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin /// FixupKills - Fix register kill flags that have been made 1692e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin /// invalid due to scheduling 1702e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin /// 1712e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin void FixupKills(MachineBasicBlock *MBB); 1722e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin 173c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman private: 174557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin void ReleaseSucc(SUnit *SU, SDep *SuccEdge); 175557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin void ReleaseSuccessors(SUnit *SU); 176557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle); 177557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin void ListScheduleTopDown(); 1785e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin void StartBlockForKills(MachineBasicBlock *BB); 1799001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 1808f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // ToggleKillFlag - Toggle a register operand kill flag. Other 1818f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // adjustments may be made to the instruction if necessary. Return 1828f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // true if the operand has been deleted, false if not. 1838f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO); 18473ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick 18573ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick void dumpSchedule() const; 186e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen }; 187e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen} 188e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 1891dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trickchar &llvm::PostRASchedulerID = PostRAScheduler::ID; 1901dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trick 1911dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew TrickINITIALIZE_PASS(PostRAScheduler, "post-RA-sched", 1921dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trick "Post RA top-down list latency scheduler", false, false) 1931dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trick 1942da8bc8a5f7705ac131184cd247f48500da0d74eAndrew TrickSchedulePostRATDList::SchedulePostRATDList( 1952da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT, 196fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen AliasAnalysis *AA, const RegisterClassInfo &RCI, 1975b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng TargetSubtargetInfo::AntiDepBreakMode AntiDepMode, 19844d23825d61d530b8d562329ec8fc2d4f843bb8dCraig Topper SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs) 199ae692f2baedf53504af2715993b166950e185a55Andrew Trick : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), AA(AA), 20046252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer LiveRegs(TRI->getNumRegs()) 2012da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick{ 2022da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick const TargetMachine &TM = MF.getTarget(); 2032da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick const InstrItineraryData *InstrItins = TM.getInstrItineraryData(); 2042da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick HazardRec = 2052da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this); 2066a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd 2076a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE || 2086a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd MRI.tracksLiveness()) && 2096a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd "Live-ins must be accurate for anti-dependency breaking"); 2102da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick AntiDepBreak = 2115b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ? 212fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) : 2135b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ? 214fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL)); 2152da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick} 2162da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick 2172da8bc8a5f7705ac131184cd247f48500da0d74eAndrew TrickSchedulePostRATDList::~SchedulePostRATDList() { 2182da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick delete HazardRec; 2192da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick delete AntiDepBreak; 2202da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick} 2212da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick 22247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick/// Initialize state associated with the next scheduling region. 22347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trickvoid SchedulePostRATDList::enterRegion(MachineBasicBlock *bb, 22447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick MachineBasicBlock::iterator begin, 22547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick MachineBasicBlock::iterator end, 22647c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick unsigned endcount) { 22747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount); 22847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick Sequence.clear(); 22947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick} 23047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick 23147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick/// Print the schedule before exiting the region. 23247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trickvoid SchedulePostRATDList::exitRegion() { 23347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick DEBUG({ 23447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick dbgs() << "*** Final schedule ***\n"; 23547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick dumpSchedule(); 23647c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick dbgs() << '\n'; 23747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick }); 23847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick ScheduleDAGInstrs::exitRegion(); 23947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick} 24047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick 241b720be6a50f4e1b3280d2b029ee38dda14577525Manman Ren#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 24273ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick/// dumpSchedule - dump the scheduled Sequence. 24373ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trickvoid SchedulePostRATDList::dumpSchedule() const { 24473ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick for (unsigned i = 0, e = Sequence.size(); i != e; i++) { 24573ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick if (SUnit *SU = Sequence[i]) 24673ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick SU->dump(this); 24773ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick else 24873ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick dbgs() << "**** NOOP ****\n"; 24973ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick } 25073ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick} 25177e300e8f0b8db8eec448cae9c87d7c5bfad9757Manman Ren#endif 25273ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick 253343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanbool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) { 25486050dc8cc0aaea8c9dfeb89de02cafbd7f48d92Evan Cheng TII = Fn.getTarget().getInstrInfo(); 2552da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); 2562da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); 2572da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick AliasAnalysis *AA = &getAnalysis<AliasAnalysis>(); 258c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); 259c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick 260fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen RegClassInfo.runOnMachineFunction(Fn); 2615bf7c2a34679b6afe46ba4f605f1921da950a66bDan Gohman 262471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin // Check for explicit enable/disable of post-ra scheduling. 263ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng TargetSubtargetInfo::AntiDepBreakMode AntiDepMode = 264ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng TargetSubtargetInfo::ANTIDEP_NONE; 26544d23825d61d530b8d562329ec8fc2d4f843bb8dCraig Topper SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs; 266471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin if (EnablePostRAScheduler.getPosition() > 0) { 267471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin if (!EnablePostRAScheduler) 268c83da2f9e389332b9cf8eae9d823f745be5624b8Evan Cheng return false; 269471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin } else { 270c83da2f9e389332b9cf8eae9d823f745be5624b8Evan Cheng // Check that post-RA scheduling is enabled for this target. 2712da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick // This may upgrade the AntiDepMode. 2725b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>(); 273c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode, 274c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick CriticalPathRCs)) 275c83da2f9e389332b9cf8eae9d823f745be5624b8Evan Cheng return false; 276471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin } 2770dad89fa94536284d51f60868326294b725a0c61David Goodwin 2784c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin // Check for antidep breaking override... 2794c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin if (EnableAntiDepBreaking.getPosition() > 0) { 2805b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng AntiDepMode = (EnableAntiDepBreaking == "all") 2815b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng ? TargetSubtargetInfo::ANTIDEP_ALL 2825b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng : ((EnableAntiDepBreaking == "critical") 2835b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng ? TargetSubtargetInfo::ANTIDEP_CRITICAL 2845b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng : TargetSubtargetInfo::ANTIDEP_NONE); 2854c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin } 2864c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin 287e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "PostRAScheduler\n"); 288e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 289fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode, 2902da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick CriticalPathRCs); 29179ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman 292e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen // Loop over all of the basic blocks 293e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); 294343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman MBB != MBBe; ++MBB) { 2951f1522839838a33e69d68656a423a244e19dffb8David Goodwin#ifndef NDEBUG 2961f1522839838a33e69d68656a423a244e19dffb8David Goodwin // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod 2971f1522839838a33e69d68656a423a244e19dffb8David Goodwin if (DebugDiv > 0) { 2981f1522839838a33e69d68656a423a244e19dffb8David Goodwin static int bbcnt = 0; 2991f1522839838a33e69d68656a423a244e19dffb8David Goodwin if (bbcnt++ % DebugDiv != DebugMod) 3001f1522839838a33e69d68656a423a244e19dffb8David Goodwin continue; 30196601ca332ab388754ca4673be8973396fea2dddCraig Topper dbgs() << "*** DEBUG scheduling " << Fn.getName() 302a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer << ":BB#" << MBB->getNumber() << " ***\n"; 3031f1522839838a33e69d68656a423a244e19dffb8David Goodwin } 3041f1522839838a33e69d68656a423a244e19dffb8David Goodwin#endif 3051f1522839838a33e69d68656a423a244e19dffb8David Goodwin 3069e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Initialize register live-range state for scheduling in this block. 307953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick Scheduler.startBlock(MBB); 3089e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 309f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman // Schedule each sequence of instructions not interrupted by a label 310f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman // or anything else that effectively needs to shut down scheduling. 3119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman MachineBasicBlock::iterator Current = MBB->end(); 31247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman unsigned Count = MBB->size(), CurrentCount = Count; 3139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) { 31486050dc8cc0aaea8c9dfeb89de02cafbd7f48d92Evan Cheng MachineInstr *MI = llvm::prior(I); 315976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen // Calls are not scheduling boundaries before register allocation, but 316976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen // post-ra we don't gain anything by scheduling across calls since we 317976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen // don't need to worry about register pressure. 318976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) { 31947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick Scheduler.enterRegion(MBB, I, Current, CurrentCount); 320953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick Scheduler.schedule(); 32147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick Scheduler.exitRegion(); 322af1d8ca44a18f304f207e209b3bdb94b590f86ffDan Gohman Scheduler.EmitSchedule(); 3239e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman Current = MI; 32447ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman CurrentCount = Count - 1; 3251274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman Scheduler.Observe(MI, CurrentCount); 326f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman } 3279e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman I = MI; 32847ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman --Count; 329ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng if (MI->isBundle()) 330ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng Count -= MI->getBundleSize(); 33147ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman } 33247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman assert(Count == 0 && "Instruction count mismatch!"); 3339e8bd0b362c034e629b9ee1f32e4e1adf037f529Duncan Sands assert((MBB->begin() == Current || CurrentCount != 0) && 3341274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman "Instruction count mismatch!"); 33547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount); 336953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick Scheduler.schedule(); 33747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick Scheduler.exitRegion(); 338af1d8ca44a18f304f207e209b3bdb94b590f86ffDan Gohman Scheduler.EmitSchedule(); 3399e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 3409e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Clean up register live-range state. 341953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick Scheduler.finishBlock(); 34288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 3435e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin // Update register kills 34488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin Scheduler.FixupKills(MBB); 345343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 346e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen 347e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen return true; 348e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen} 3499001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 3509e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// StartBlock - Initialize register live-range state for scheduling in 3519e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// this block. 3529e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 353953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid SchedulePostRATDList::startBlock(MachineBasicBlock *BB) { 3549e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Call the superclass. 355953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick ScheduleDAGInstrs::startBlock(BB); 3569e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 3572e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin // Reset the hazard recognizer and anti-dep breaker. 358d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin HazardRec->Reset(); 3592e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin if (AntiDepBreak != NULL) 3602e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin AntiDepBreak->StartBlock(BB); 3619e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 3629e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 3639e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// Schedule - Schedule the instruction range using list scheduling. 3649e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 365953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid SchedulePostRATDList::schedule() { 366c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman // Build the scheduling graph. 367953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick buildSchedGraph(AA); 368343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 3692e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin if (AntiDepBreak != NULL) { 3709001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach unsigned Broken = 37168675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd, 37268675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick EndIndex, DbgValues); 3739001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 374557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin if (Broken != 0) { 37521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // We made changes. Update the dependency graph. 37621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // Theoretically we could update the graph in place: 37721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // When a live range is changed to use a different register, remove 37821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // the def's anti-dependence *and* output-dependence edges due to 37921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // that register, and add new anti-dependence and output-dependence 38021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman // edges based on the next live range of the register. 38147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick ScheduleDAG::clearDAG(); 382953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick buildSchedGraph(AA); 3839001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 3842e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin NumFixedAnti += Broken; 38521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 38621d9003087c9a707e6cd95460136b499df358fb8Dan Gohman } 38721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman 388e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "********** List Scheduling **********\n"); 389d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su) 390d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin SUnits[su].dumpAll(this)); 391d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin 392343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.initNodes(SUnits); 393557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ListScheduleTopDown(); 394343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.releaseState(); 395343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 396343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 3979e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// Observe - Update liveness information to account for the current 3989e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// instruction, which will not be scheduled. 3999e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 40047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohmanvoid SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) { 4012e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin if (AntiDepBreak != NULL) 402cf46b5acfd6e0ab5d21ec3160cec195d0eb77b0bAndrew Trick AntiDepBreak->Observe(MI, Count, EndIndex); 4039e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 4049e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4059e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// FinishBlock - Clean up register live-range state. 4069e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// 407953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid SchedulePostRATDList::finishBlock() { 4082e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin if (AntiDepBreak != NULL) 4092e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin AntiDepBreak->FinishBlock(); 4109e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Call the superclass. 412953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick ScheduleDAGInstrs::finishBlock(); 4139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 4149e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 4155e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin/// StartBlockForKills - Initialize register live-range state for updating kills 4165e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin/// 4175e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwinvoid SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) { 41846252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer // Start with no live registers. 41946252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer LiveRegs.reset(); 4205e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin 421b45e4deb102d47602f5b941da7f412ecc9a867e9Jakob Stoklund Olesen // Examine the live-in regs of all successors. 422b45e4deb102d47602f5b941da7f412ecc9a867e9Jakob Stoklund Olesen for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), 423b45e4deb102d47602f5b941da7f412ecc9a867e9Jakob Stoklund Olesen SE = BB->succ_end(); SI != SE; ++SI) { 424b45e4deb102d47602f5b941da7f412ecc9a867e9Jakob Stoklund Olesen for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(), 425b45e4deb102d47602f5b941da7f412ecc9a867e9Jakob Stoklund Olesen E = (*SI)->livein_end(); I != E; ++I) { 4265e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin unsigned Reg = *I; 42762c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier // Repeat, for reg and all subregs. 42862c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 42962c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier SubRegs.isValid(); ++SubRegs) 430396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen LiveRegs.set(*SubRegs); 4315e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 4325e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin } 4335e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin} 4345e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin 4358f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwinbool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI, 4368f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MachineOperand &MO) { 4378f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // Setting kill flag... 4388f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin if (!MO.isKill()) { 4398f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MO.setIsKill(true); 4408f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin return false; 4418f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 4429001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 4438f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // If MO itself is live, clear the kill flag... 44446252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer if (LiveRegs.test(MO.getReg())) { 4458f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin MO.setIsKill(false); 4468f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin return false; 4478f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 4488f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 4498f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // If any subreg of MO is live, then create an imp-def for that 4508f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // subreg and keep MO marked as killed. 4518bff4af61219031345e7dae0c1840315e6bfab7fBenjamin Kramer MO.setIsKill(false); 4528f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin bool AllDead = true; 4538f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin const unsigned SuperReg = MO.getReg(); 4547b79b9862c9e6fc31ec072acb09171fd6ec7b0e0Jakob Stoklund Olesen MachineInstrBuilder MIB(MF, MI); 455396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) { 456396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen if (LiveRegs.test(*SubRegs)) { 4577b79b9862c9e6fc31ec072acb09171fd6ec7b0e0Jakob Stoklund Olesen MIB.addReg(*SubRegs, RegState::ImplicitDefine); 4588f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin AllDead = false; 4598f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 4608f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin } 4618f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 462c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman if(AllDead) 4638bff4af61219031345e7dae0c1840315e6bfab7fBenjamin Kramer MO.setIsKill(true); 4648f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin return false; 4658f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin} 4668f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin 46788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// FixupKills - Fix the register kill flags, they may have been made 46888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// incorrect by instruction reordering. 46988a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// 47088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwinvoid SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) { 471e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n'); 47288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 47349b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer BitVector killedRegs(TRI->getNumRegs()); 4745e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin 4755e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin StartBlockForKills(MBB); 4769001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 4777886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Examine block from end to start... 47888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin unsigned Count = MBB->size(); 47988a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin(); 48088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin I != E; --Count) { 48188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin MachineInstr *MI = --I; 482b0812f114b83a32c4b90a4b553c7177c557558b5Dale Johannesen if (MI->isDebugValue()) 483b0812f114b83a32c4b90a4b553c7177c557558b5Dale Johannesen continue; 48488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 4857886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Update liveness. Registers that are defed but not used in this 4867886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // instruction are now dead. Mark register and all subregs as they 4877886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // are completely defined. 4887886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 4897886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin MachineOperand &MO = MI->getOperand(i); 490f19a5926cb1fcd145a61116b7dd2b18312e76fd7Jakob Stoklund Olesen if (MO.isRegMask()) 491b6bd8ccd02f7c2febee19b1eae2cb1ce90ad4001Benjamin Kramer LiveRegs.clearBitsNotInMask(MO.getRegMask()); 4927886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (!MO.isReg()) continue; 4937886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin unsigned Reg = MO.getReg(); 4947886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (Reg == 0) continue; 4957886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (!MO.isDef()) continue; 4967886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // Ignore two-addr defs. 4977886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (MI->isRegTiedToUseOperand(i)) continue; 4989001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 49962c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier // Repeat for reg and all subregs. 50062c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 50162c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier SubRegs.isValid(); ++SubRegs) 502396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen LiveRegs.reset(*SubRegs); 5037886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 50488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 5058f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // Examine all used registers and set/clear kill flag. When a 5068f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // register is used multiple times we only set the kill flag on 5078f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin // the first use. 50849b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer killedRegs.reset(); 50988a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 51088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin MachineOperand &MO = MI->getOperand(i); 51188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin if (!MO.isReg() || !MO.isUse()) continue; 51288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin unsigned Reg = MO.getReg(); 513fb9ebbf236974beac31705eaeb9f50ab585af6abJakob Stoklund Olesen if ((Reg == 0) || MRI.isReserved(Reg)) continue; 51488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 5157886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin bool kill = false; 51649b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer if (!killedRegs.test(Reg)) { 5177886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin kill = true; 5187886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // A register is not killed if any subregs are live... 519396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { 520396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen if (LiveRegs.test(*SubRegs)) { 5217886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin kill = false; 5227886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin break; 5237886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 5247886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 5257886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 5267886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // If subreg is not live, then register is killed if it became 5277886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin // live in this instruction 5287886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin if (kill) 52946252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer kill = !LiveRegs.test(Reg); 5307886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 5319001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 53288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin if (MO.isKill() != kill) { 533e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "Fixing " << MO << " in "); 53415d75d9f11f877bf3716257013b563e67341b0edJakob Stoklund Olesen // Warning: ToggleKillFlag may invalidate MO. 53515d75d9f11f877bf3716257013b563e67341b0edJakob Stoklund Olesen ToggleKillFlag(MI, MO); 53688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin DEBUG(MI->dump()); 53788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin } 5389001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 53949b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer killedRegs.set(Reg); 54088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin } 5419001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 542a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin // Mark any used register (that is not using undef) and subregs as 543a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin // now live... 5447886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 5457886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin MachineOperand &MO = MI->getOperand(i); 546a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue; 5477886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin unsigned Reg = MO.getReg(); 548fb9ebbf236974beac31705eaeb9f50ab585af6abJakob Stoklund Olesen if ((Reg == 0) || MRI.isReserved(Reg)) continue; 5497886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin 55062c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 55162c320a755ac27ac2b7f64e927892249e0f486e0Chad Rosier SubRegs.isValid(); ++SubRegs) 552396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen LiveRegs.set(*SubRegs); 5537886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin } 55488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin } 55588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin} 55688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin 557343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===// 558343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman// Top-Down Scheduling 559343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===// 560343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 561343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to 562ae692f2baedf53504af2715993b166950e185a55Andrew Trick/// the PendingQueue if the count reaches zero. 563557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) { 56454e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohman SUnit *SuccSU = SuccEdge->getSUnit(); 565c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner 566cf6b6131dd0da37903a6e3a5173ea12aa8263713Andrew Trick if (SuccEdge->isWeak()) { 567ae692f2baedf53504af2715993b166950e185a55Andrew Trick --SuccSU->WeakPredsLeft; 568ae692f2baedf53504af2715993b166950e185a55Andrew Trick return; 569ae692f2baedf53504af2715993b166950e185a55Andrew Trick } 570343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG 571c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner if (SuccSU->NumPredsLeft == 0) { 572e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene dbgs() << "*** Scheduling failed! ***\n"; 573343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman SuccSU->dump(this); 574e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene dbgs() << " has been released too many times!\n"; 575c23197a26f34f559ea9797de51e187087c039c42Torok Edwin llvm_unreachable(0); 576343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 577343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#endif 578c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner --SuccSU->NumPredsLeft; 579c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner 58089fd43778e47b0698582f906e3dac900c376102eAndrew Trick // Standard scheduler algorithms will recompute the depth of the successor 58115ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // here as such: 58215ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency()); 58315ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // 58415ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // However, we lazily compute node depth instead. Note that 58515ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // ScheduleNodeTopDown has already updated the depth of this node which causes 58615ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // all descendents to be marked dirty. Setting the successor depth explicitly 58715ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // here would cause depth to be recomputed for all its ancestors. If the 58815ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // successor is not yet ready (because of a transitively redundant edge) then 58915ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick // this causes depth computation to be quadratic in the size of the DAG. 5909001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 5919e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // If all the node's predecessors are scheduled, this node is ready 5929e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // to be scheduled. Ignore the special ExitSU node. 5939e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU) 594343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue.push_back(SuccSU); 5959e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman} 5969e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 5979e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors. 598557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) { 5999e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); 6004de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin I != E; ++I) { 601557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ReleaseSucc(SU, &*I); 6024de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin } 603343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 604343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 605343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending 606343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// count of its successors. If a successor pending count is zero, add it to 607343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// the Available queue. 608557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) { 609e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: "); 610343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman DEBUG(SU->dump(this)); 6119001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 612343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman Sequence.push_back(SU); 6139001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach assert(CurCycle >= SU->getDepth() && 6144de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin "Node scheduled above its depth!"); 615557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin SU->setDepthToAtLeast(CurCycle); 616343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 617557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ReleaseSuccessors(SU); 618343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman SU->isScheduled = true; 619953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick AvailableQueue.scheduledNode(SU); 620343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 621343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 622343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ListScheduleTopDown - The main loop of list scheduling for top-down 623343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// schedulers. 624557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ListScheduleTopDown() { 625343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman unsigned CurCycle = 0; 6269001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach 6274de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin // We're scheduling top-down but we're visiting the regions in 6284de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin // bottom-up order, so we don't know the hazards at the start of a 6294de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin // region. So assume no hazards (this should usually be ok as most 6304de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin // blocks are a single region). 6314de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin HazardRec->Reset(); 6324de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin 6339e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman // Release any successors of the special Entry node. 634557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ReleaseSuccessors(&EntrySU); 6359e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 636557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin // Add all leaves to Available queue. 637343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { 638343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // It is available if it has no predecessors. 639ae692f2baedf53504af2715993b166950e185a55Andrew Trick if (!SUnits[i].NumPredsLeft && !SUnits[i].isAvailable) { 640343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.push(&SUnits[i]); 641343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman SUnits[i].isAvailable = true; 642343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 643343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 6449e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman 6452ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // In any cycle where we can't schedule any instructions, we must 6462ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // stall or emit a noop, depending on the target. 647be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer bool CycleHasInsts = false; 6482ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin 649343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // While Available queue is not empty, grab the node with the highest 650343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // priority. If it is not ready put it back. Schedule the node. 6512836c283bb1c14baa50994f60769d665da608ad7Dan Gohman std::vector<SUnit*> NotReady; 652343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman Sequence.reserve(SUnits.size()); 653343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman while (!AvailableQueue.empty() || !PendingQueue.empty()) { 654343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // Check to see if any of the pending instructions are ready to issue. If 655343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman // so, add them to the available queue. 6563f23744df4809eba94284e601e81489212c974d4Dan Gohman unsigned MinDepth = ~0u; 657343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) { 658557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin if (PendingQueue[i]->getDepth() <= CurCycle) { 659343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman AvailableQueue.push(PendingQueue[i]); 660343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue[i]->isAvailable = true; 661343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue[i] = PendingQueue.back(); 662343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman PendingQueue.pop_back(); 663343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman --i; --e; 664557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin } else if (PendingQueue[i]->getDepth() < MinDepth) 665557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin MinDepth = PendingQueue[i]->getDepth(); 666343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 667c93d8373c93159c590838957b3194900caeb8a03David Goodwin 6682da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this)); 669c93d8373c93159c590838957b3194900caeb8a03David Goodwin 6702836c283bb1c14baa50994f60769d665da608ad7Dan Gohman SUnit *FoundSUnit = 0; 6712836c283bb1c14baa50994f60769d665da608ad7Dan Gohman bool HasNoopHazards = false; 6722836c283bb1c14baa50994f60769d665da608ad7Dan Gohman while (!AvailableQueue.empty()) { 6732836c283bb1c14baa50994f60769d665da608ad7Dan Gohman SUnit *CurSUnit = AvailableQueue.pop(); 6742836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 6752836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ScheduleHazardRecognizer::HazardType HT = 6762da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick HazardRec->getHazardType(CurSUnit, 0/*no stalls*/); 6772836c283bb1c14baa50994f60769d665da608ad7Dan Gohman if (HT == ScheduleHazardRecognizer::NoHazard) { 6782836c283bb1c14baa50994f60769d665da608ad7Dan Gohman FoundSUnit = CurSUnit; 6792836c283bb1c14baa50994f60769d665da608ad7Dan Gohman break; 6802836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 6812836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 6822836c283bb1c14baa50994f60769d665da608ad7Dan Gohman // Remember if this is a noop hazard. 6832836c283bb1c14baa50994f60769d665da608ad7Dan Gohman HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard; 6842836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 6852836c283bb1c14baa50994f60769d665da608ad7Dan Gohman NotReady.push_back(CurSUnit); 6862836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 6872836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 6882836c283bb1c14baa50994f60769d665da608ad7Dan Gohman // Add the nodes that aren't ready back onto the available list. 6892836c283bb1c14baa50994f60769d665da608ad7Dan Gohman if (!NotReady.empty()) { 6902836c283bb1c14baa50994f60769d665da608ad7Dan Gohman AvailableQueue.push_all(NotReady); 6912836c283bb1c14baa50994f60769d665da608ad7Dan Gohman NotReady.clear(); 6922836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } 6932836c283bb1c14baa50994f60769d665da608ad7Dan Gohman 6944de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin // If we found a node to schedule... 695343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman if (FoundSUnit) { 6964de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin // ... schedule the node... 697557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ScheduleNodeTopDown(FoundSUnit, CurCycle); 6982836c283bb1c14baa50994f60769d665da608ad7Dan Gohman HazardRec->EmitInstruction(FoundSUnit); 699be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer CycleHasInsts = true; 700cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick if (HazardRec->atIssueLimit()) { 701cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n'); 702cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick HazardRec->AdvanceCycle(); 703cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick ++CurCycle; 704cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick CycleHasInsts = false; 705cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick } 7062836c283bb1c14baa50994f60769d665da608ad7Dan Gohman } else { 707be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer if (CycleHasInsts) { 708e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n'); 7092ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin HazardRec->AdvanceCycle(); 7102ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin } else if (!HasNoopHazards) { 7112ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // Otherwise, we have a pipeline stall, but no other problem, 7122ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // just advance the current cycle and try again. 713e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n'); 7142ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin HazardRec->AdvanceCycle(); 715557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ++NumStalls; 7162ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin } else { 7172ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // Otherwise, we have no instructions to issue and we have instructions 7182ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // that will fault if we don't do this right. This is the case for 7192ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin // processors without pipeline interlocks and other cases. 720e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n'); 7212ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin HazardRec->EmitNoop(); 7222ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin Sequence.push_back(0); // NULL here means noop 723557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin ++NumNoops; 7242ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin } 7252ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin 7262836c283bb1c14baa50994f60769d665da608ad7Dan Gohman ++CurCycle; 727be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer CycleHasInsts = false; 728343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 729343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman } 730343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman 731343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG 7324c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false); 7334c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick unsigned Noops = 0; 7344c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick for (unsigned i = 0, e = Sequence.size(); i != e; ++i) 7354c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick if (!Sequence[i]) 7364c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick ++Noops; 7374c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick assert(Sequence.size() - Noops == ScheduledNodes && 7384c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick "The number of nodes scheduled doesn't match the expected number!"); 7394c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick#endif // NDEBUG 740343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman} 74184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick 74284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick// EmitSchedule - Emit the machine code in scheduled order. 74384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trickvoid SchedulePostRATDList::EmitSchedule() { 74468675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick RegionBegin = RegionEnd; 74584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick 74684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick // If first instruction was a DBG_VALUE then put it back. 74784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick if (FirstDbgValue) 74868675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick BB->splice(RegionEnd, BB, FirstDbgValue); 74984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick 75084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick // Then re-insert them according to the given schedule. 75184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick for (unsigned i = 0, e = Sequence.size(); i != e; i++) { 75284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick if (SUnit *SU = Sequence[i]) 75368675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick BB->splice(RegionEnd, BB, SU->getInstr()); 75484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick else 75584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick // Null SUnit* is a noop. 75668675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick TII->insertNoop(*BB, RegionEnd); 75784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick 75884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick // Update the Begin iterator, as the first instruction in the block 75984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick // may have been scheduled later. 76084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick if (i == 0) 76168675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick RegionBegin = prior(RegionEnd); 76284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick } 76384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick 76484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick // Reinsert any remaining debug_values. 76584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator 76684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) { 76784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick std::pair<MachineInstr *, MachineInstr *> P = *prior(DI); 76884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick MachineInstr *DbgValue = P.first; 76984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick MachineBasicBlock::iterator OrigPrivMI = P.second; 77084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick BB->splice(++OrigPrivMI, BB, DbgValue); 77184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick } 77284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick DbgValues.clear(); 77384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick FirstDbgValue = NULL; 77484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick} 775