PostRASchedulerList.cpp revision 77e300e8f0b8db8eec448cae9c87d7c5bfad9757
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"
2282c7248518a8b759a567fbb4b3176542ad2cf414David Goodwin#include "AntiDepBreaker.h"
23348777110a960f0e017025dd5141cb29472c3984David Goodwin#include "AggressiveAntiDepBreaker.h"
242e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin#include "CriticalAntiDepBreaker.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"
29c7951f8e09a65e07626e7b9a272ce9911b249472David Goodwin#include "llvm/CodeGen/MachineFrameInfo.h"
30e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/CodeGen/MachineFunctionPass.h"
313f23744df4809eba94284e601e81489212c974d4Dan Gohman#include "llvm/CodeGen/MachineLoopInfo.h"
3221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h"
331525260b3e50cc578939ef41b60609689eecfdd2Andrew Trick#include "llvm/CodeGen/RegisterClassInfo.h"
34ed395c8c475692f5a43eb4b5c5562503d67616d0Andrew Trick#include "llvm/CodeGen/ScheduleDAGInstrs.h"
352836c283bb1c14baa50994f60769d665da608ad7Dan Gohman#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
36a70dca156fa76d452f54829b5c5f962ddfd94ef2Dan Gohman#include "llvm/Analysis/AliasAnalysis.h"
37bed353d0163a6b17beecc20c23b67de9b06e7b5cDan Gohman#include "llvm/Target/TargetLowering.h"
3879ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman#include "llvm/Target/TargetMachine.h"
3921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/Target/TargetInstrInfo.h"
4021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman#include "llvm/Target/TargetRegisterInfo.h"
415b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng#include "llvm/Target/TargetSubtargetInfo.h"
42e10deca33e74a7c70ab585f78eee3fb52937f668David Goodwin#include "llvm/Support/CommandLine.h"
43e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen#include "llvm/Support/Debug.h"
44c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h"
453a5f0d444cf21e2b90d5eb965bb677c7ce098546David Goodwin#include "llvm/Support/raw_ostream.h"
462e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin#include "llvm/ADT/BitVector.h"
47343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#include "llvm/ADT/Statistic.h"
48e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesenusing namespace llvm;
49e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen
502836c283bb1c14baa50994f60769d665da608ad7Dan GohmanSTATISTIC(NumNoops, "Number of noops inserted");
51343f0c046702831a4a6aec951b6a297a23241a55Dan GohmanSTATISTIC(NumStalls, "Number of pipeline stalls");
522e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid GoodwinSTATISTIC(NumFixedAnti, "Number of fixed anti-dependencies");
53343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
54471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin// Post-RA scheduling is enabled with
555b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng// TargetSubtargetInfo.enablePostRAScheduler(). This flag can be used to
56471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin// override the target.
57471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwinstatic cl::opt<bool>
58471850ab84301dd47cab2bf8d694fcb5766c1169David GoodwinEnablePostRAScheduler("post-RA-scheduler",
59471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin                       cl::desc("Enable scheduling after register allocation"),
609843a93e830e76f96e9a997b3002624a28ca5aa6David Goodwin                       cl::init(false), cl::Hidden);
612e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwinstatic cl::opt<std::string>
6221d9003087c9a707e6cd95460136b499df358fb8Dan GohmanEnableAntiDepBreaking("break-anti-dependencies",
632e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin                      cl::desc("Break post-RA scheduling anti-dependencies: "
642e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin                               "\"critical\", \"all\", or \"none\""),
652e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin                      cl::init("none"), cl::Hidden);
662836c283bb1c14baa50994f60769d665da608ad7Dan Gohman
671f1522839838a33e69d68656a423a244e19dffb8David Goodwin// If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
681f1522839838a33e69d68656a423a244e19dffb8David Goodwinstatic cl::opt<int>
691f1522839838a33e69d68656a423a244e19dffb8David GoodwinDebugDiv("postra-sched-debugdiv",
701f1522839838a33e69d68656a423a244e19dffb8David Goodwin                      cl::desc("Debug control MBBs that are scheduled"),
711f1522839838a33e69d68656a423a244e19dffb8David Goodwin                      cl::init(0), cl::Hidden);
721f1522839838a33e69d68656a423a244e19dffb8David Goodwinstatic cl::opt<int>
731f1522839838a33e69d68656a423a244e19dffb8David GoodwinDebugMod("postra-sched-debugmod",
741f1522839838a33e69d68656a423a244e19dffb8David Goodwin                      cl::desc("Debug control MBBs that are scheduled"),
751f1522839838a33e69d68656a423a244e19dffb8David Goodwin                      cl::init(0), cl::Hidden);
761f1522839838a33e69d68656a423a244e19dffb8David Goodwin
77ada0ef86d98df4ac36808e4a0f0098250bf1a842David GoodwinAntiDepBreaker::~AntiDepBreaker() { }
78ada0ef86d98df4ac36808e4a0f0098250bf1a842David Goodwin
79e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesennamespace {
806726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky  class PostRAScheduler : public MachineFunctionPass {
8186050dc8cc0aaea8c9dfeb89de02cafbd7f48d92Evan Cheng    const TargetInstrInfo *TII;
82fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen    RegisterClassInfo RegClassInfo;
83a70dca156fa76d452f54829b5c5f962ddfd94ef2Dan Gohman
84e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen  public:
85e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen    static char ID;
86c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick    PostRAScheduler() : MachineFunctionPass(ID) {}
8721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman
883f23744df4809eba94284e601e81489212c974d4Dan Gohman    void getAnalysisUsage(AnalysisUsage &AU) const {
89845012e6d31799c7fbd1193fa1af8ee2d12e9231Dan Gohman      AU.setPreservesCFG();
90a70dca156fa76d452f54829b5c5f962ddfd94ef2Dan Gohman      AU.addRequired<AliasAnalysis>();
91c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick      AU.addRequired<TargetPassConfig>();
923f23744df4809eba94284e601e81489212c974d4Dan Gohman      AU.addRequired<MachineDominatorTree>();
933f23744df4809eba94284e601e81489212c974d4Dan Gohman      AU.addPreserved<MachineDominatorTree>();
943f23744df4809eba94284e601e81489212c974d4Dan Gohman      AU.addRequired<MachineLoopInfo>();
953f23744df4809eba94284e601e81489212c974d4Dan Gohman      AU.addPreserved<MachineLoopInfo>();
963f23744df4809eba94284e601e81489212c974d4Dan Gohman      MachineFunctionPass::getAnalysisUsage(AU);
973f23744df4809eba94284e601e81489212c974d4Dan Gohman    }
983f23744df4809eba94284e601e81489212c974d4Dan Gohman
99343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    bool runOnMachineFunction(MachineFunction &Fn);
100343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  };
101343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  char PostRAScheduler::ID = 0;
102343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
1036726b6d75a8b679068a58cb954ba97cf9d1690baNick Lewycky  class SchedulePostRATDList : public ScheduleDAGInstrs {
104343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// AvailableQueue - The priority queue to use for the available SUnits.
105c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    ///
106343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    LatencyPriorityQueue AvailableQueue;
1079001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
108343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// PendingQueue - This contains all of the instructions whose operands have
109343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// been issued, but their results are not ready yet (due to the latency of
110343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// the operation).  Once the operands becomes available, the instruction is
111343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    /// added to the AvailableQueue.
112343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    std::vector<SUnit*> PendingQueue;
113343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
11421d9003087c9a707e6cd95460136b499df358fb8Dan Gohman    /// Topo - A topological ordering for SUnits.
11521d9003087c9a707e6cd95460136b499df358fb8Dan Gohman    ScheduleDAGTopologicalSort Topo;
116e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen
117c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    /// HazardRec - The hazard recognizer to use.
118c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    ScheduleHazardRecognizer *HazardRec;
119c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman
1202e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    /// AntiDepBreak - Anti-dependence breaking object, or NULL if none
1212e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    AntiDepBreaker *AntiDepBreak;
1222e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin
123c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    /// AA - AliasAnalysis for making memory reference queries.
124c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    AliasAnalysis *AA;
125480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin
12646252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer    /// LiveRegs - true if the register is live.
12746252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer    BitVector LiveRegs;
1289e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
12947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    /// The schedule. Null SUnit*'s represent noop instructions.
13047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    std::vector<SUnit*> Sequence;
13147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
13221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman  public:
1332da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick    SchedulePostRATDList(
1342da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick      MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
135fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen      AliasAnalysis *AA, const RegisterClassInfo&,
1365b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng      TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
13744d23825d61d530b8d562329ec8fc2d4f843bb8dCraig Topper      SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs);
1382da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick
1392da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick    ~SchedulePostRATDList();
140343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
141953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    /// startBlock - Initialize register live-range state for scheduling in
1429e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    /// this block.
1439e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    ///
144953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    void startBlock(MachineBasicBlock *BB);
1459e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
14647c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    /// Initialize the scheduler state for the next scheduling region.
14747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    virtual void enterRegion(MachineBasicBlock *bb,
14847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick                             MachineBasicBlock::iterator begin,
14947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick                             MachineBasicBlock::iterator end,
15047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick                             unsigned endcount);
15147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
15247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    /// Notify that the scheduler has finished scheduling the current region.
15347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    virtual void exitRegion();
15447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
155480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin    /// Schedule - Schedule the instruction range using list scheduling.
1569e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    ///
157953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    void schedule();
1589001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
15984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    void EmitSchedule();
16084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick
161c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    /// Observe - Update liveness information to account for the current
162c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    /// instruction, which will not be scheduled.
163c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    ///
164c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    void Observe(MachineInstr *MI, unsigned Count);
165480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin
166953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    /// finishBlock - Clean up register live-range state.
167c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman    ///
168953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    void finishBlock();
169480c529e026942f28e1a792d2cec6d6b5bc0edbaDavid Goodwin
1702e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    /// FixupKills - Fix register kill flags that have been made
1712e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    /// invalid due to scheduling
1722e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    ///
1732e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    void FixupKills(MachineBasicBlock *MBB);
1742e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin
175c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman  private:
176557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin    void ReleaseSucc(SUnit *SU, SDep *SuccEdge);
177557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin    void ReleaseSuccessors(SUnit *SU);
178557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin    void ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle);
179557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin    void ListScheduleTopDown();
1805e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    void StartBlockForKills(MachineBasicBlock *BB);
1819001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
1828f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    // ToggleKillFlag - Toggle a register operand kill flag. Other
1838f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    // adjustments may be made to the instruction if necessary. Return
1848f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    // true if the operand has been deleted, false if not.
1858f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    bool ToggleKillFlag(MachineInstr *MI, MachineOperand &MO);
18673ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick
18773ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick    void dumpSchedule() const;
188e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen  };
189e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen}
190e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen
1911dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trickchar &llvm::PostRASchedulerID = PostRAScheduler::ID;
1921dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trick
1931dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew TrickINITIALIZE_PASS(PostRAScheduler, "post-RA-sched",
1941dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trick                "Post RA top-down list latency scheduler", false, false)
1951dd8c8560d45d36a8e507cd014352f1d313f9f9eAndrew Trick
1962da8bc8a5f7705ac131184cd247f48500da0d74eAndrew TrickSchedulePostRATDList::SchedulePostRATDList(
1972da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
198fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen  AliasAnalysis *AA, const RegisterClassInfo &RCI,
1995b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng  TargetSubtargetInfo::AntiDepBreakMode AntiDepMode,
20044d23825d61d530b8d562329ec8fc2d4f843bb8dCraig Topper  SmallVectorImpl<const TargetRegisterClass*> &CriticalPathRCs)
2015e920d7c83c20474fc3470209869978628ccf8daAndrew Trick  : ScheduleDAGInstrs(MF, MLI, MDT, /*IsPostRA=*/true), Topo(SUnits), AA(AA),
20246252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer    LiveRegs(TRI->getNumRegs())
2032da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick{
2042da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  const TargetMachine &TM = MF.getTarget();
2052da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  const InstrItineraryData *InstrItins = TM.getInstrItineraryData();
2062da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  HazardRec =
2072da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick    TM.getInstrInfo()->CreateTargetPostRAHazardRecognizer(InstrItins, this);
2086a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd
2096a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd  assert((AntiDepMode == TargetSubtargetInfo::ANTIDEP_NONE ||
2106a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd          MRI.tracksLiveness()) &&
2116a8c7bf8e72338e55f0f9583e1828f62da165d4aPreston Gurd         "Live-ins must be accurate for anti-dependency breaking");
2122da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  AntiDepBreak =
2135b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng    ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_ALL) ?
214fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen     (AntiDepBreaker *)new AggressiveAntiDepBreaker(MF, RCI, CriticalPathRCs) :
2155b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng     ((AntiDepMode == TargetSubtargetInfo::ANTIDEP_CRITICAL) ?
216fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen      (AntiDepBreaker *)new CriticalAntiDepBreaker(MF, RCI) : NULL));
2172da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick}
2182da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick
2192da8bc8a5f7705ac131184cd247f48500da0d74eAndrew TrickSchedulePostRATDList::~SchedulePostRATDList() {
2202da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  delete HazardRec;
2212da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  delete AntiDepBreak;
2222da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick}
2232da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick
22447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick/// Initialize state associated with the next scheduling region.
22547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trickvoid SchedulePostRATDList::enterRegion(MachineBasicBlock *bb,
22647c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick                 MachineBasicBlock::iterator begin,
22747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick                 MachineBasicBlock::iterator end,
22847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick                 unsigned endcount) {
22947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  ScheduleDAGInstrs::enterRegion(bb, begin, end, endcount);
23047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  Sequence.clear();
23147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick}
23247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
23347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick/// Print the schedule before exiting the region.
23447c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trickvoid SchedulePostRATDList::exitRegion() {
23547c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  DEBUG({
23647c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick      dbgs() << "*** Final schedule ***\n";
23747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick      dumpSchedule();
23847c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick      dbgs() << '\n';
23947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    });
24047c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick  ScheduleDAGInstrs::exitRegion();
24147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick}
24247c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick
24377e300e8f0b8db8eec448cae9c87d7c5bfad9757Manman Ren#ifndef NDEBUG
24473ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick/// dumpSchedule - dump the scheduled Sequence.
24573ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trickvoid SchedulePostRATDList::dumpSchedule() const {
24673ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick  for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
24773ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick    if (SUnit *SU = Sequence[i])
24873ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick      SU->dump(this);
24973ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick    else
25073ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick      dbgs() << "**** NOOP ****\n";
25173ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick  }
25273ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick}
25377e300e8f0b8db8eec448cae9c87d7c5bfad9757Manman Ren#endif
25473ba69b6843f7f23345b1e8745cb328952cae0d8Andrew Trick
255343f0c046702831a4a6aec951b6a297a23241a55Dan Gohmanbool PostRAScheduler::runOnMachineFunction(MachineFunction &Fn) {
25686050dc8cc0aaea8c9dfeb89de02cafbd7f48d92Evan Cheng  TII = Fn.getTarget().getInstrInfo();
2572da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
2582da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
2592da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick  AliasAnalysis *AA = &getAnalysis<AliasAnalysis>();
260c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick  TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>();
261c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick
262fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen  RegClassInfo.runOnMachineFunction(Fn);
2635bf7c2a34679b6afe46ba4f605f1921da950a66bDan Gohman
264471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin  // Check for explicit enable/disable of post-ra scheduling.
265ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng  TargetSubtargetInfo::AntiDepBreakMode AntiDepMode =
266ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng    TargetSubtargetInfo::ANTIDEP_NONE;
26744d23825d61d530b8d562329ec8fc2d4f843bb8dCraig Topper  SmallVector<const TargetRegisterClass*, 4> CriticalPathRCs;
268471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin  if (EnablePostRAScheduler.getPosition() > 0) {
269471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin    if (!EnablePostRAScheduler)
270c83da2f9e389332b9cf8eae9d823f745be5624b8Evan Cheng      return false;
271471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin  } else {
272c83da2f9e389332b9cf8eae9d823f745be5624b8Evan Cheng    // Check that post-RA scheduling is enabled for this target.
2732da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick    // This may upgrade the AntiDepMode.
2745b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng    const TargetSubtargetInfo &ST = Fn.getTarget().getSubtarget<TargetSubtargetInfo>();
275c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick    if (!ST.enablePostRAScheduler(PassConfig->getOptLevel(), AntiDepMode,
276c7d081b5946b9ff9f7400d5b41f36cad3fc317aaAndrew Trick                                  CriticalPathRCs))
277c83da2f9e389332b9cf8eae9d823f745be5624b8Evan Cheng      return false;
278471850ab84301dd47cab2bf8d694fcb5766c1169David Goodwin  }
2790dad89fa94536284d51f60868326294b725a0c61David Goodwin
2804c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin  // Check for antidep breaking override...
2814c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin  if (EnableAntiDepBreaking.getPosition() > 0) {
2825b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng    AntiDepMode = (EnableAntiDepBreaking == "all")
2835b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng      ? TargetSubtargetInfo::ANTIDEP_ALL
2845b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng      : ((EnableAntiDepBreaking == "critical")
2855b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng         ? TargetSubtargetInfo::ANTIDEP_CRITICAL
2865b1b4489cf3a0f56f8be0673fc5cc380a32d277bEvan Cheng         : TargetSubtargetInfo::ANTIDEP_NONE);
2874c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin  }
2884c3715c2e5e17d7216a96ac2baf9720630f04408David Goodwin
289e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene  DEBUG(dbgs() << "PostRAScheduler\n");
290e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen
291fa796dd720f1b34596a043f17f098fac18ecc028Jakob Stoklund Olesen  SchedulePostRATDList Scheduler(Fn, MLI, MDT, AA, RegClassInfo, AntiDepMode,
2922da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick                                 CriticalPathRCs);
29379ce276083ced01256a0eb7d80731e4948ca6e87Dan Gohman
294e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen  // Loop over all of the basic blocks
295e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen  for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
296343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman       MBB != MBBe; ++MBB) {
2971f1522839838a33e69d68656a423a244e19dffb8David Goodwin#ifndef NDEBUG
2981f1522839838a33e69d68656a423a244e19dffb8David Goodwin    // If DebugDiv > 0 then only schedule MBB with (ID % DebugDiv) == DebugMod
2991f1522839838a33e69d68656a423a244e19dffb8David Goodwin    if (DebugDiv > 0) {
3001f1522839838a33e69d68656a423a244e19dffb8David Goodwin      static int bbcnt = 0;
3011f1522839838a33e69d68656a423a244e19dffb8David Goodwin      if (bbcnt++ % DebugDiv != DebugMod)
3021f1522839838a33e69d68656a423a244e19dffb8David Goodwin        continue;
30396601ca332ab388754ca4673be8973396fea2dddCraig Topper      dbgs() << "*** DEBUG scheduling " << Fn.getName()
304a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer             << ":BB#" << MBB->getNumber() << " ***\n";
3051f1522839838a33e69d68656a423a244e19dffb8David Goodwin    }
3061f1522839838a33e69d68656a423a244e19dffb8David Goodwin#endif
3071f1522839838a33e69d68656a423a244e19dffb8David Goodwin
3089e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    // Initialize register live-range state for scheduling in this block.
309953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    Scheduler.startBlock(MBB);
3109e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
311f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman    // Schedule each sequence of instructions not interrupted by a label
312f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman    // or anything else that effectively needs to shut down scheduling.
3139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    MachineBasicBlock::iterator Current = MBB->end();
31447ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman    unsigned Count = MBB->size(), CurrentCount = Count;
3159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    for (MachineBasicBlock::iterator I = Current; I != MBB->begin(); ) {
31686050dc8cc0aaea8c9dfeb89de02cafbd7f48d92Evan Cheng      MachineInstr *MI = llvm::prior(I);
317976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen      // Calls are not scheduling boundaries before register allocation, but
318976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen      // post-ra we don't gain anything by scheduling across calls since we
319976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen      // don't need to worry about register pressure.
320976647d95da89e38c66d9ed869a9d345b36d386dJakob Stoklund Olesen      if (MI->isCall() || TII->isSchedulingBoundary(MI, MBB, Fn)) {
32147c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick        Scheduler.enterRegion(MBB, I, Current, CurrentCount);
322953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick        Scheduler.schedule();
32347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick        Scheduler.exitRegion();
324af1d8ca44a18f304f207e209b3bdb94b590f86ffDan Gohman        Scheduler.EmitSchedule();
3259e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman        Current = MI;
32647ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman        CurrentCount = Count - 1;
3271274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman        Scheduler.Observe(MI, CurrentCount);
328f7119393a97c2a10757084b6bc186380f8c19a73Dan Gohman      }
3299e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman      I = MI;
33047ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman      --Count;
331ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng      if (MI->isBundle())
332ddfd1377d2e4154d44dc3ad217735adc15af2e3fEvan Cheng        Count -= MI->getBundleSize();
33347ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman    }
33447ac0f0c7c39289f5970688154e385be22b7f293Dan Gohman    assert(Count == 0 && "Instruction count mismatch!");
3359e8bd0b362c034e629b9ee1f32e4e1adf037f529Duncan Sands    assert((MBB->begin() == Current || CurrentCount != 0) &&
3361274ced8a3f0fd1e9a6f7c7e17d69368c4f78b90Dan Gohman           "Instruction count mismatch!");
33747c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    Scheduler.enterRegion(MBB, MBB->begin(), Current, CurrentCount);
338953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    Scheduler.schedule();
33947c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick    Scheduler.exitRegion();
340af1d8ca44a18f304f207e209b3bdb94b590f86ffDan Gohman    Scheduler.EmitSchedule();
3419e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
3429e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman    // Clean up register live-range state.
343953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick    Scheduler.finishBlock();
34488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin
3455e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    // Update register kills
34688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin    Scheduler.FixupKills(MBB);
347343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
348e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen
349e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen  return true;
350e7e7d0d7e39d0c7c659d26b97e8081fce0fcd749Dale Johannesen}
3519001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
3529e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// StartBlock - Initialize register live-range state for scheduling in
3539e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// this block.
3549e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman///
355953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid SchedulePostRATDList::startBlock(MachineBasicBlock *BB) {
3569e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  // Call the superclass.
357953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  ScheduleDAGInstrs::startBlock(BB);
3589e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
3592e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin  // Reset the hazard recognizer and anti-dep breaker.
360d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin  HazardRec->Reset();
3612e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin  if (AntiDepBreak != NULL)
3622e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    AntiDepBreak->StartBlock(BB);
3639e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman}
3649e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
3659e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// Schedule - Schedule the instruction range using list scheduling.
3669e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman///
367953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid SchedulePostRATDList::schedule() {
368c9a5b9e38b442c2ae6b115213a07df3fcd14708dDan Gohman  // Build the scheduling graph.
369953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  buildSchedGraph(AA);
370343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
3712e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin  if (AntiDepBreak != NULL) {
3729001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach    unsigned Broken =
37368675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick      AntiDepBreak->BreakAntiDependencies(SUnits, RegionBegin, RegionEnd,
37468675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick                                          EndIndex, DbgValues);
3759001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
376557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin    if (Broken != 0) {
37721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman      // We made changes. Update the dependency graph.
37821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman      // Theoretically we could update the graph in place:
37921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman      // When a live range is changed to use a different register, remove
38021d9003087c9a707e6cd95460136b499df358fb8Dan Gohman      // the def's anti-dependence *and* output-dependence edges due to
38121d9003087c9a707e6cd95460136b499df358fb8Dan Gohman      // that register, and add new anti-dependence and output-dependence
38221d9003087c9a707e6cd95460136b499df358fb8Dan Gohman      // edges based on the next live range of the register.
38347c144505b9be28ed22c626b3a407c11dba2fec5Andrew Trick      ScheduleDAG::clearDAG();
384953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick      buildSchedGraph(AA);
3859001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
3862e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin      NumFixedAnti += Broken;
38721d9003087c9a707e6cd95460136b499df358fb8Dan Gohman    }
38821d9003087c9a707e6cd95460136b499df358fb8Dan Gohman  }
38921d9003087c9a707e6cd95460136b499df358fb8Dan Gohman
390e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene  DEBUG(dbgs() << "********** List Scheduling **********\n");
391d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin  DEBUG(for (unsigned su = 0, e = SUnits.size(); su != e; ++su)
392d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin          SUnits[su].dumpAll(this));
393d94a4e5d8de1145be200ff7223f98b0928462b94David Goodwin
394343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  AvailableQueue.initNodes(SUnits);
395557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin  ListScheduleTopDown();
396343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  AvailableQueue.releaseState();
397343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
398343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
3999e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// Observe - Update liveness information to account for the current
4009e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// instruction, which will not be scheduled.
4019e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman///
40247ac0f0c7c39289f5970688154e385be22b7f293Dan Gohmanvoid SchedulePostRATDList::Observe(MachineInstr *MI, unsigned Count) {
4032e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin  if (AntiDepBreak != NULL)
404cf46b5acfd6e0ab5d21ec3160cec195d0eb77b0bAndrew Trick    AntiDepBreak->Observe(MI, Count, EndIndex);
4059e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman}
4069e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
4079e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// FinishBlock - Clean up register live-range state.
4089e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman///
409953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trickvoid SchedulePostRATDList::finishBlock() {
4102e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin  if (AntiDepBreak != NULL)
4112e7be612d5d0eb42ee3ae08194dbb03b750cc6bfDavid Goodwin    AntiDepBreak->FinishBlock();
4129e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
4139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  // Call the superclass.
414953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  ScheduleDAGInstrs::finishBlock();
4159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman}
4169e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
4175e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin/// StartBlockForKills - Initialize register live-range state for updating kills
4185e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin///
4195e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwinvoid SchedulePostRATDList::StartBlockForKills(MachineBasicBlock *BB) {
42046252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer  // Start with no live registers.
42146252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer  LiveRegs.reset();
4225e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin
4235e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin  // Determine the live-out physregs for this block.
4245a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng  if (!BB->empty() && BB->back().isReturn()) {
4255e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    // In a return block, examine the function live-out regs.
4265e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
4275e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin           E = MRI.liveout_end(); I != E; ++I) {
4285e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin      unsigned Reg = *I;
42946252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer      LiveRegs.set(Reg);
4305e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin      // Repeat, for all subregs.
431396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen      for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
432396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen        LiveRegs.set(*SubRegs);
4335e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    }
4345e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin  }
4355e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin  else {
4365e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    // In a non-return block, examine the live-in regs of all successors.
4375e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
4385e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin           SE = BB->succ_end(); SI != SE; ++SI) {
4395e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin      for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
4405e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin             E = (*SI)->livein_end(); I != E; ++I) {
4415e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin        unsigned Reg = *I;
44246252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer        LiveRegs.set(Reg);
4435e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin        // Repeat, for all subregs.
444396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen        for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
445396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen          LiveRegs.set(*SubRegs);
4465e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin      }
4475e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin    }
4485e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin  }
4495e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin}
4505e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin
4518f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwinbool SchedulePostRATDList::ToggleKillFlag(MachineInstr *MI,
4528f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin                                          MachineOperand &MO) {
4538f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  // Setting kill flag...
4548f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  if (!MO.isKill()) {
4558f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    MO.setIsKill(true);
4568f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    return false;
4578f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  }
4589001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
4598f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  // If MO itself is live, clear the kill flag...
46046252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer  if (LiveRegs.test(MO.getReg())) {
4618f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    MO.setIsKill(false);
4628f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    return false;
4638f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  }
4648f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin
4658f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  // If any subreg of MO is live, then create an imp-def for that
4668f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  // subreg and keep MO marked as killed.
4678bff4af61219031345e7dae0c1840315e6bfab7fBenjamin Kramer  MO.setIsKill(false);
4688f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  bool AllDead = true;
4698f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  const unsigned SuperReg = MO.getReg();
470396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen  for (MCSubRegIterator SubRegs(SuperReg, TRI); SubRegs.isValid(); ++SubRegs) {
471396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen    if (LiveRegs.test(*SubRegs)) {
472396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen      MI->addOperand(MachineOperand::CreateReg(*SubRegs,
4738f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin                                               true  /*IsDef*/,
4748f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin                                               true  /*IsImp*/,
4758f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin                                               false /*IsKill*/,
4768f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin                                               false /*IsDead*/));
4778f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin      AllDead = false;
4788f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    }
4798f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  }
4808f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin
481c1ae8c9b8f426b74215abf0f7e46bffecc6f52d9Dan Gohman  if(AllDead)
4828bff4af61219031345e7dae0c1840315e6bfab7fBenjamin Kramer    MO.setIsKill(true);
4838f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin  return false;
4848f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin}
4858f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin
48688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// FixupKills - Fix the register kill flags, they may have been made
48788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin/// incorrect by instruction reordering.
48888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin///
48988a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwinvoid SchedulePostRATDList::FixupKills(MachineBasicBlock *MBB) {
490e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene  DEBUG(dbgs() << "Fixup kills for BB#" << MBB->getNumber() << '\n');
49188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin
49249b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer  BitVector killedRegs(TRI->getNumRegs());
49388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin  BitVector ReservedRegs = TRI->getReservedRegs(MF);
4945e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin
4955e41178a6ee9a0faa2c031811d32543d7e9d0affDavid Goodwin  StartBlockForKills(MBB);
4969001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
4977886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin  // Examine block from end to start...
49888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin  unsigned Count = MBB->size();
49988a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin  for (MachineBasicBlock::iterator I = MBB->end(), E = MBB->begin();
50088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin       I != E; --Count) {
50188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin    MachineInstr *MI = --I;
502b0812f114b83a32c4b90a4b553c7177c557558b5Dale Johannesen    if (MI->isDebugValue())
503b0812f114b83a32c4b90a4b553c7177c557558b5Dale Johannesen      continue;
50488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin
5057886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    // Update liveness.  Registers that are defed but not used in this
5067886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    // instruction are now dead. Mark register and all subregs as they
5077886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    // are completely defined.
5087886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
5097886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      MachineOperand &MO = MI->getOperand(i);
510f19a5926cb1fcd145a61116b7dd2b18312e76fd7Jakob Stoklund Olesen      if (MO.isRegMask())
511b6bd8ccd02f7c2febee19b1eae2cb1ce90ad4001Benjamin Kramer        LiveRegs.clearBitsNotInMask(MO.getRegMask());
5127886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      if (!MO.isReg()) continue;
5137886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      unsigned Reg = MO.getReg();
5147886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      if (Reg == 0) continue;
5157886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      if (!MO.isDef()) continue;
5167886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      // Ignore two-addr defs.
5177886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      if (MI->isRegTiedToUseOperand(i)) continue;
5189001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
51946252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer      LiveRegs.reset(Reg);
5209001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
5217886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      // Repeat for all subregs.
522396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen      for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
523396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen        LiveRegs.reset(*SubRegs);
5247886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    }
52588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin
5268f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    // Examine all used registers and set/clear kill flag. When a
5278f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    // register is used multiple times we only set the kill flag on
5288f909345bcabd0cbcb99d01d23f1d77b8b1518ecDavid Goodwin    // the first use.
52949b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer    killedRegs.reset();
53088a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin    for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
53188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin      MachineOperand &MO = MI->getOperand(i);
53288a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin      if (!MO.isReg() || !MO.isUse()) continue;
53388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin      unsigned Reg = MO.getReg();
53488a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin      if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
53588a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin
5367886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      bool kill = false;
53749b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer      if (!killedRegs.test(Reg)) {
5387886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin        kill = true;
5397886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin        // A register is not killed if any subregs are live...
540396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen        for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
541396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen          if (LiveRegs.test(*SubRegs)) {
5427886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin            kill = false;
5437886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin            break;
5447886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin          }
5457886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin        }
5467886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin
5477886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin        // If subreg is not live, then register is killed if it became
5487886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin        // live in this instruction
5497886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin        if (kill)
55046252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer          kill = !LiveRegs.test(Reg);
5517886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      }
5529001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
55388a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin      if (MO.isKill() != kill) {
554e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene        DEBUG(dbgs() << "Fixing " << MO << " in ");
55515d75d9f11f877bf3716257013b563e67341b0edJakob Stoklund Olesen        // Warning: ToggleKillFlag may invalidate MO.
55615d75d9f11f877bf3716257013b563e67341b0edJakob Stoklund Olesen        ToggleKillFlag(MI, MO);
55788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin        DEBUG(MI->dump());
55888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin      }
5599001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
56049b726c339b6f3b9c51638bd0f453610f9765f2dBenjamin Kramer      killedRegs.set(Reg);
56188a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin    }
5629001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
563a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin    // Mark any used register (that is not using undef) and subregs as
564a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin    // now live...
5657886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
5667886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      MachineOperand &MO = MI->getOperand(i);
567a3251db21a474affaca945e3fc53f22d30d20f00David Goodwin      if (!MO.isReg() || !MO.isUse() || MO.isUndef()) continue;
5687886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      unsigned Reg = MO.getReg();
5697886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin      if ((Reg == 0) || ReservedRegs.test(Reg)) continue;
5707886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin
57146252d8ea1d264ca341e3ca3e7f4e3c82e32940eBenjamin Kramer      LiveRegs.set(Reg);
5729001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
573396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen      for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
574396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen        LiveRegs.set(*SubRegs);
5757886cd85b21db4498ff042a4e42aded7bf3272eeDavid Goodwin    }
57688a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin  }
57788a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin}
57888a589c4b39830bbeed23654521ef2f77bb87abeDavid Goodwin
579343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===//
580343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//  Top-Down Scheduling
581343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman//===----------------------------------------------------------------------===//
582343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
583343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ReleaseSucc - Decrement the NumPredsLeft count of a successor. Add it to
584343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// the PendingQueue if the count reaches zero. Also update its cycle bound.
585557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ReleaseSucc(SUnit *SU, SDep *SuccEdge) {
58654e4c36a7349e94a84773afb56eccd4ca65b49e9Dan Gohman  SUnit *SuccSU = SuccEdge->getSUnit();
587c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner
588343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG
589c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner  if (SuccSU->NumPredsLeft == 0) {
590e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene    dbgs() << "*** Scheduling failed! ***\n";
591343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    SuccSU->dump(this);
592e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene    dbgs() << " has been released too many times!\n";
593c23197a26f34f559ea9797de51e187087c039c42Torok Edwin    llvm_unreachable(0);
594343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
595343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#endif
596c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner  --SuccSU->NumPredsLeft;
597c277ab08a24d2dbe9b4ff1a9154ea6115ed6a4e3Reid Kleckner
59889fd43778e47b0698582f906e3dac900c376102eAndrew Trick  // Standard scheduler algorithms will recompute the depth of the successor
59915ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // here as such:
60015ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  //   SuccSU->setDepthToAtLeast(SU->getDepth() + SuccEdge->getLatency());
60115ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  //
60215ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // However, we lazily compute node depth instead. Note that
60315ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // ScheduleNodeTopDown has already updated the depth of this node which causes
60415ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // all descendents to be marked dirty. Setting the successor depth explicitly
60515ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // here would cause depth to be recomputed for all its ancestors. If the
60615ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // successor is not yet ready (because of a transitively redundant edge) then
60715ab3594eba495ffd1f070207a4aceeae9492c11Andrew Trick  // this causes depth computation to be quadratic in the size of the DAG.
6089001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
6099e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  // If all the node's predecessors are scheduled, this node is ready
6109e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  // to be scheduled. Ignore the special ExitSU node.
6119e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  if (SuccSU->NumPredsLeft == 0 && SuccSU != &ExitSU)
612343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    PendingQueue.push_back(SuccSU);
6139e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman}
6149e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
6159e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman/// ReleaseSuccessors - Call ReleaseSucc on each of SU's successors.
616557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ReleaseSuccessors(SUnit *SU) {
6179e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
6184de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin       I != E; ++I) {
619557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin    ReleaseSucc(SU, &*I);
6204de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin  }
621343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
622343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
623343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ScheduleNodeTopDown - Add the node to the schedule. Decrement the pending
624343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// count of its successors. If a successor pending count is zero, add it to
625343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// the Available queue.
626557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ScheduleNodeTopDown(SUnit *SU, unsigned CurCycle) {
627e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene  DEBUG(dbgs() << "*** Scheduling [" << CurCycle << "]: ");
628343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  DEBUG(SU->dump(this));
6299001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
630343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  Sequence.push_back(SU);
6319001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach  assert(CurCycle >= SU->getDepth() &&
6324de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin         "Node scheduled above its depth!");
633557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin  SU->setDepthToAtLeast(CurCycle);
634343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
635557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin  ReleaseSuccessors(SU);
636343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  SU->isScheduled = true;
637953be893e8cffa0ef9bf410036cd96aeb526e98aAndrew Trick  AvailableQueue.scheduledNode(SU);
638343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
639343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
640343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// ListScheduleTopDown - The main loop of list scheduling for top-down
641343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman/// schedulers.
642557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwinvoid SchedulePostRATDList::ListScheduleTopDown() {
643343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  unsigned CurCycle = 0;
6449001303a3fc3e901c1e5e8b5daea56e55989c114Jim Grosbach
6454de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin  // We're scheduling top-down but we're visiting the regions in
6464de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin  // bottom-up order, so we don't know the hazards at the start of a
6474de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin  // region. So assume no hazards (this should usually be ok as most
6484de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin  // blocks are a single region).
6494de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin  HazardRec->Reset();
6504de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin
6519e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman  // Release any successors of the special Entry node.
652557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin  ReleaseSuccessors(&EntrySU);
6539e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
654557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin  // Add all leaves to Available queue.
655343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
656343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    // It is available if it has no predecessors.
6574de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin    bool available = SUnits[i].Preds.empty();
6584de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin    if (available) {
659343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      AvailableQueue.push(&SUnits[i]);
660343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman      SUnits[i].isAvailable = true;
661343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
662343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
6639e64bbb322417c09f27afdf08e3946287c9df5aaDan Gohman
6642ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin  // In any cycle where we can't schedule any instructions, we must
6652ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin  // stall or emit a noop, depending on the target.
666be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer  bool CycleHasInsts = false;
6672ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin
668343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // While Available queue is not empty, grab the node with the highest
669343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  // priority. If it is not ready put it back.  Schedule the node.
6702836c283bb1c14baa50994f60769d665da608ad7Dan Gohman  std::vector<SUnit*> NotReady;
671343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  Sequence.reserve(SUnits.size());
672343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  while (!AvailableQueue.empty() || !PendingQueue.empty()) {
673343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    // Check to see if any of the pending instructions are ready to issue.  If
674343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    // so, add them to the available queue.
6753f23744df4809eba94284e601e81489212c974d4Dan Gohman    unsigned MinDepth = ~0u;
676343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    for (unsigned i = 0, e = PendingQueue.size(); i != e; ++i) {
677557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin      if (PendingQueue[i]->getDepth() <= CurCycle) {
678343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        AvailableQueue.push(PendingQueue[i]);
679343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        PendingQueue[i]->isAvailable = true;
680343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        PendingQueue[i] = PendingQueue.back();
681343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        PendingQueue.pop_back();
682343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman        --i; --e;
683557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin      } else if (PendingQueue[i]->getDepth() < MinDepth)
684557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin        MinDepth = PendingQueue[i]->getDepth();
685343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
686c93d8373c93159c590838957b3194900caeb8a03David Goodwin
6872da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick    DEBUG(dbgs() << "\n*** Examining Available\n"; AvailableQueue.dump(this));
688c93d8373c93159c590838957b3194900caeb8a03David Goodwin
6892836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    SUnit *FoundSUnit = 0;
6902836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    bool HasNoopHazards = false;
6912836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    while (!AvailableQueue.empty()) {
6922836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      SUnit *CurSUnit = AvailableQueue.pop();
6932836c283bb1c14baa50994f60769d665da608ad7Dan Gohman
6942836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      ScheduleHazardRecognizer::HazardType HT =
6952da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick        HazardRec->getHazardType(CurSUnit, 0/*no stalls*/);
6962836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      if (HT == ScheduleHazardRecognizer::NoHazard) {
6972836c283bb1c14baa50994f60769d665da608ad7Dan Gohman        FoundSUnit = CurSUnit;
6982836c283bb1c14baa50994f60769d665da608ad7Dan Gohman        break;
6992836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      }
7002836c283bb1c14baa50994f60769d665da608ad7Dan Gohman
7012836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      // Remember if this is a noop hazard.
7022836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      HasNoopHazards |= HT == ScheduleHazardRecognizer::NoopHazard;
7032836c283bb1c14baa50994f60769d665da608ad7Dan Gohman
7042836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      NotReady.push_back(CurSUnit);
7052836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    }
7062836c283bb1c14baa50994f60769d665da608ad7Dan Gohman
7072836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    // Add the nodes that aren't ready back onto the available list.
7082836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    if (!NotReady.empty()) {
7092836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      AvailableQueue.push_all(NotReady);
7102836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      NotReady.clear();
7112836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    }
7122836c283bb1c14baa50994f60769d665da608ad7Dan Gohman
7134de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin    // If we found a node to schedule...
714343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    if (FoundSUnit) {
7154de099d8ca651e00fa5fac22bace4f4dba2d0292David Goodwin      // ... schedule the node...
716557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin      ScheduleNodeTopDown(FoundSUnit, CurCycle);
7172836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      HazardRec->EmitInstruction(FoundSUnit);
718be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer      CycleHasInsts = true;
719cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick      if (HazardRec->atIssueLimit()) {
720cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick        DEBUG(dbgs() << "*** Max instructions per cycle " << CurCycle << '\n');
721cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick        HazardRec->AdvanceCycle();
722cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick        ++CurCycle;
723cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick        CycleHasInsts = false;
724cf9aa284b332bc2613def3612b80c5883d4b9985Andrew Trick      }
7252836c283bb1c14baa50994f60769d665da608ad7Dan Gohman    } else {
726be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer      if (CycleHasInsts) {
727e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene        DEBUG(dbgs() << "*** Finished cycle " << CurCycle << '\n');
7282ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        HazardRec->AdvanceCycle();
7292ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin      } else if (!HasNoopHazards) {
7302ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        // Otherwise, we have a pipeline stall, but no other problem,
7312ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        // just advance the current cycle and try again.
732e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene        DEBUG(dbgs() << "*** Stall in cycle " << CurCycle << '\n');
7332ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        HazardRec->AdvanceCycle();
734557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin        ++NumStalls;
7352ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin      } else {
7362ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        // Otherwise, we have no instructions to issue and we have instructions
7372ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        // that will fault if we don't do this right.  This is the case for
7382ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        // processors without pipeline interlocks and other cases.
739e1b2129471e994cfb7d34cc5134eb99dd1ae39f2David Greene        DEBUG(dbgs() << "*** Emitting noop in cycle " << CurCycle << '\n');
7402ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        HazardRec->EmitNoop();
7412ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin        Sequence.push_back(0);   // NULL here means noop
742557bbe6b5d13faaec38f85a266db457c7cb09ff2David Goodwin        ++NumNoops;
7432ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin      }
7442ffb0ce7dce2d5c243b90493807308af6fab0528David Goodwin
7452836c283bb1c14baa50994f60769d665da608ad7Dan Gohman      ++CurCycle;
746be441c0f348a1c02a3632718832f6e2d42c4f8f0Benjamin Kramer      CycleHasInsts = false;
747343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman    }
748343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman  }
749343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman
750343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman#ifndef NDEBUG
7514c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  unsigned ScheduledNodes = VerifyScheduledDAG(/*isBottomUp=*/false);
7524c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  unsigned Noops = 0;
7534c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  for (unsigned i = 0, e = Sequence.size(); i != e; ++i)
7544c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick    if (!Sequence[i])
7554c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick      ++Noops;
7564c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick  assert(Sequence.size() - Noops == ScheduledNodes &&
7574c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick         "The number of nodes scheduled doesn't match the expected number!");
7584c727204271067f3dbf50bd23098b2df8e1cc47aAndrew Trick#endif // NDEBUG
759343f0c046702831a4a6aec951b6a297a23241a55Dan Gohman}
76084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick
76184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick// EmitSchedule - Emit the machine code in scheduled order.
76284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trickvoid SchedulePostRATDList::EmitSchedule() {
76368675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick  RegionBegin = RegionEnd;
76484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick
76584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  // If first instruction was a DBG_VALUE then put it back.
76684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  if (FirstDbgValue)
76768675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick    BB->splice(RegionEnd, BB, FirstDbgValue);
76884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick
76984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  // Then re-insert them according to the given schedule.
77084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
77184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    if (SUnit *SU = Sequence[i])
77268675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick      BB->splice(RegionEnd, BB, SU->getInstr());
77384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    else
77484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick      // Null SUnit* is a noop.
77568675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick      TII->insertNoop(*BB, RegionEnd);
77684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick
77784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    // Update the Begin iterator, as the first instruction in the block
77884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    // may have been scheduled later.
77984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    if (i == 0)
78068675c6c5b173021807e4e12cd250eeba63f6d0dAndrew Trick      RegionBegin = prior(RegionEnd);
78184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  }
78284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick
78384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  // Reinsert any remaining debug_values.
78484b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  for (std::vector<std::pair<MachineInstr *, MachineInstr *> >::iterator
78584b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick         DI = DbgValues.end(), DE = DbgValues.begin(); DI != DE; --DI) {
78684b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    std::pair<MachineInstr *, MachineInstr *> P = *prior(DI);
78784b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    MachineInstr *DbgValue = P.first;
78884b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    MachineBasicBlock::iterator OrigPrivMI = P.second;
78984b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick    BB->splice(++OrigPrivMI, BB, DbgValue);
79084b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  }
79184b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  DbgValues.clear();
79284b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick  FirstDbgValue = NULL;
79384b454d1a270a5d685e01686ed15e68c44b0b56aAndrew Trick}
794