2009-02-06 17:12:10 +00:00
|
|
|
//==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
|
2008-11-19 23:18:57 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the ScheduleDAGInstrs class, which implements
|
|
|
|
// scheduling for a MachineInstr-based dependency graph.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-06 17:12:10 +00:00
|
|
|
#ifndef SCHEDULEDAGINSTRS_H
|
|
|
|
#define SCHEDULEDAGINSTRS_H
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleDAG.h"
|
2009-02-10 23:27:53 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-01-15 19:20:50 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-02-10 23:27:53 +00:00
|
|
|
#include <map>
|
2008-11-19 23:18:57 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2008-12-16 03:25:46 +00:00
|
|
|
class MachineLoopInfo;
|
|
|
|
class MachineDominatorTree;
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// LoopDependencies - This class analyzes loop-oriented register
|
|
|
|
/// dependencies, which are used to guide scheduling decisions.
|
|
|
|
/// For example, loop induction variable increments should be
|
|
|
|
/// scheduled as soon as possible after the variable's last use.
|
|
|
|
///
|
|
|
|
class VISIBILITY_HIDDEN LoopDependencies {
|
|
|
|
const MachineLoopInfo &MLI;
|
|
|
|
const MachineDominatorTree &MDT;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::map<unsigned, std::pair<const MachineOperand *, unsigned> >
|
|
|
|
LoopDeps;
|
|
|
|
LoopDeps Deps;
|
|
|
|
|
|
|
|
LoopDependencies(const MachineLoopInfo &mli,
|
|
|
|
const MachineDominatorTree &mdt) :
|
|
|
|
MLI(mli), MDT(mdt) {}
|
|
|
|
|
|
|
|
/// VisitLoop - Clear out any previous state and analyze the given loop.
|
|
|
|
///
|
|
|
|
void VisitLoop(const MachineLoop *Loop) {
|
|
|
|
Deps.clear();
|
|
|
|
MachineBasicBlock *Header = Loop->getHeader();
|
|
|
|
SmallSet<unsigned, 8> LoopLiveIns;
|
|
|
|
for (MachineBasicBlock::livein_iterator LI = Header->livein_begin(),
|
|
|
|
LE = Header->livein_end(); LI != LE; ++LI)
|
|
|
|
LoopLiveIns.insert(*LI);
|
|
|
|
|
|
|
|
const MachineDomTreeNode *Node = MDT.getNode(Header);
|
|
|
|
const MachineBasicBlock *MBB = Node->getBlock();
|
|
|
|
assert(Loop->contains(MBB) &&
|
|
|
|
"Loop does not contain header!");
|
|
|
|
VisitRegion(Node, MBB, Loop, LoopLiveIns);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void VisitRegion(const MachineDomTreeNode *Node,
|
|
|
|
const MachineBasicBlock *MBB,
|
|
|
|
const MachineLoop *Loop,
|
|
|
|
const SmallSet<unsigned, 8> &LoopLiveIns) {
|
|
|
|
unsigned Count = 0;
|
|
|
|
for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
|
|
|
|
I != E; ++I, ++Count) {
|
|
|
|
const MachineInstr *MI = I;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
continue;
|
|
|
|
unsigned MOReg = MO.getReg();
|
|
|
|
if (LoopLiveIns.count(MOReg))
|
|
|
|
Deps.insert(std::make_pair(MOReg, std::make_pair(&MO, Count)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
|
|
|
|
for (std::vector<MachineDomTreeNode*>::const_iterator I =
|
|
|
|
Children.begin(), E = Children.end(); I != E; ++I) {
|
|
|
|
const MachineDomTreeNode *ChildNode = *I;
|
|
|
|
MachineBasicBlock *ChildBlock = ChildNode->getBlock();
|
|
|
|
if (Loop->contains(ChildBlock))
|
|
|
|
VisitRegion(ChildNode, ChildBlock, Loop, LoopLiveIns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
|
|
|
|
/// MachineInstrs.
|
|
|
|
class VISIBILITY_HIDDEN ScheduleDAGInstrs : public ScheduleDAG {
|
2008-12-16 03:25:46 +00:00
|
|
|
const MachineLoopInfo &MLI;
|
|
|
|
const MachineDominatorTree &MDT;
|
|
|
|
|
2009-01-15 19:20:50 +00:00
|
|
|
/// Defs, Uses - Remember where defs and uses of each physical register
|
|
|
|
/// are as we iterate upward through the instructions. This is allocated
|
|
|
|
/// here instead of inside BuildSchedGraph to avoid the need for it to be
|
|
|
|
/// initialized and destructed for each block.
|
|
|
|
std::vector<SUnit *> Defs[TargetRegisterInfo::FirstVirtualRegister];
|
|
|
|
std::vector<SUnit *> Uses[TargetRegisterInfo::FirstVirtualRegister];
|
|
|
|
|
|
|
|
/// PendingLoads - Remember where unknown loads are after the most recent
|
|
|
|
/// unknown store, as we iterate. As with Defs and Uses, this is here
|
|
|
|
/// to minimize construction/destruction.
|
|
|
|
std::vector<SUnit *> PendingLoads;
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// LoopRegs - Track which registers are used for loop-carried dependencies.
|
|
|
|
///
|
|
|
|
LoopDependencies LoopRegs;
|
|
|
|
|
|
|
|
/// LoopLiveInRegs - Track which regs are live into a loop, to help guide
|
|
|
|
/// back-edge-aware scheduling.
|
|
|
|
///
|
|
|
|
SmallSet<unsigned, 8> LoopLiveInRegs;
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
public:
|
2009-02-11 04:27:20 +00:00
|
|
|
MachineBasicBlock *BB; // Current basic block
|
|
|
|
MachineBasicBlock::iterator Begin; // The beginning of the range to
|
|
|
|
// be scheduled. The range extends
|
|
|
|
// to InsertPos.
|
|
|
|
unsigned InsertPosIndex; // The index in BB of InsertPos.
|
|
|
|
|
2009-01-15 19:20:50 +00:00
|
|
|
explicit ScheduleDAGInstrs(MachineFunction &mf,
|
|
|
|
const MachineLoopInfo &mli,
|
|
|
|
const MachineDominatorTree &mdt);
|
2008-11-19 23:18:57 +00:00
|
|
|
|
|
|
|
virtual ~ScheduleDAGInstrs() {}
|
|
|
|
|
|
|
|
/// NewSUnit - Creates a new SUnit and return a ptr to it.
|
|
|
|
///
|
|
|
|
SUnit *NewSUnit(MachineInstr *MI) {
|
2008-12-22 21:08:08 +00:00
|
|
|
#ifndef NDEBUG
|
2009-01-20 09:05:19 +00:00
|
|
|
const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
|
2008-12-22 21:08:08 +00:00
|
|
|
#endif
|
2008-11-19 23:18:57 +00:00
|
|
|
SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
|
2009-01-20 09:05:19 +00:00
|
|
|
assert((Addr == 0 || Addr == &SUnits[0]) &&
|
|
|
|
"SUnits std::vector reallocated on the fly!");
|
2008-11-19 23:18:57 +00:00
|
|
|
SUnits.back().OrigNode = &SUnits.back();
|
|
|
|
return &SUnits.back();
|
|
|
|
}
|
|
|
|
|
2009-02-11 04:27:20 +00:00
|
|
|
/// Run - perform scheduling.
|
|
|
|
///
|
|
|
|
void Run(MachineBasicBlock *bb,
|
|
|
|
MachineBasicBlock::iterator begin,
|
|
|
|
MachineBasicBlock::iterator end,
|
|
|
|
unsigned endindex);
|
|
|
|
|
2008-12-23 18:36:58 +00:00
|
|
|
/// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
|
2008-11-19 23:18:57 +00:00
|
|
|
/// input.
|
2008-12-23 18:36:58 +00:00
|
|
|
virtual void BuildSchedGraph();
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2008-11-21 00:12:10 +00:00
|
|
|
/// ComputeLatency - Compute node latency.
|
|
|
|
///
|
|
|
|
virtual void ComputeLatency(SUnit *SU);
|
|
|
|
|
2009-08-19 16:08:58 +00:00
|
|
|
/// ComputeOperandLatency - Override dependence edge latency using
|
|
|
|
/// operand use/def information
|
|
|
|
///
|
|
|
|
virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
|
|
|
|
SDep& dep) const;
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
virtual MachineBasicBlock *EmitSchedule();
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// StartBlock - Prepare to perform scheduling in the given block.
|
|
|
|
///
|
|
|
|
virtual void StartBlock(MachineBasicBlock *BB);
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
/// Schedule - Order nodes according to selected style, filling
|
|
|
|
/// in the Sequence member.
|
|
|
|
///
|
|
|
|
virtual void Schedule() = 0;
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// FinishBlock - Clean up after scheduling in the given block.
|
|
|
|
///
|
|
|
|
virtual void FinishBlock();
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
virtual void dumpNode(const SUnit *SU) const;
|
|
|
|
|
|
|
|
virtual std::string getGraphNodeLabel(const SUnit *SU) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|