2009-02-07 01:12:10 +08:00
|
|
|
//==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
|
2008-11-20 07:18:57 +08: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-07 01:12:10 +08:00
|
|
|
#ifndef SCHEDULEDAGINSTRS_H
|
|
|
|
#define SCHEDULEDAGINSTRS_H
|
2008-11-20 07:18:57 +08:00
|
|
|
|
2009-02-11 07:27:53 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/CodeGen/ScheduleDAG.h"
|
2009-02-11 07:27:53 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-01-16 03:20:50 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2012-01-14 10:17:18 +08:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2009-09-19 05:02:19 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2009-02-11 07:27:53 +08:00
|
|
|
#include <map>
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2008-12-16 11:25:46 +08:00
|
|
|
class MachineLoopInfo;
|
|
|
|
class MachineDominatorTree;
|
|
|
|
|
2009-02-11 07:27:53 +08: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.
|
|
|
|
///
|
2010-05-12 04:16:09 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY LoopDependencies {
|
2009-02-11 07:27:53 +08:00
|
|
|
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) {
|
2011-10-07 14:33:09 +08:00
|
|
|
assert(Deps.empty() && "stale loop dependencies");
|
|
|
|
|
2009-02-11 07:27:53 +08:00
|
|
|
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();
|
2010-06-29 12:48:13 +08:00
|
|
|
I != E; ++I) {
|
2009-02-11 07:27:53 +08:00
|
|
|
const MachineInstr *MI = I;
|
2010-06-29 12:48:13 +08:00
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
2009-02-11 07:27:53 +08:00
|
|
|
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)));
|
|
|
|
}
|
2010-06-29 12:48:13 +08:00
|
|
|
++Count; // Not every iteration due to dbg_value above.
|
2009-02-11 07:27:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2010-05-12 04:16:09 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY ScheduleDAGInstrs : public ScheduleDAG {
|
2008-12-16 11:25:46 +08:00
|
|
|
const MachineLoopInfo &MLI;
|
|
|
|
const MachineDominatorTree &MDT;
|
2009-10-19 03:58:47 +08:00
|
|
|
const MachineFrameInfo *MFI;
|
2010-09-10 09:29:16 +08:00
|
|
|
const InstrItineraryData *InstrItins;
|
2008-12-16 11:25:46 +08:00
|
|
|
|
2012-01-14 10:17:12 +08:00
|
|
|
/// isPostRA flag indicates vregs cannot be present.
|
|
|
|
bool IsPostRA;
|
|
|
|
|
2012-01-14 10:17:18 +08:00
|
|
|
/// UnitLatencies (misnamed) flag avoids computing def-use latencies, using
|
|
|
|
/// the def-side latency only.
|
2012-01-14 10:17:15 +08:00
|
|
|
bool UnitLatencies;
|
|
|
|
|
2012-01-14 10:17:12 +08:00
|
|
|
/// Defs, Uses - Remember where defs and uses of each 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.
|
2010-07-24 14:01:53 +08:00
|
|
|
std::vector<std::vector<SUnit *> > Defs;
|
|
|
|
std::vector<std::vector<SUnit *> > Uses;
|
2011-10-07 14:27:02 +08:00
|
|
|
|
2012-01-14 10:17:18 +08:00
|
|
|
// Virtual register Defs and Uses.
|
|
|
|
//
|
|
|
|
// TODO: Eliminate VRegUses by creating SUnits in a prepass and looking up
|
|
|
|
// the live range's reaching def.
|
|
|
|
IndexedMap<SUnit*, VirtReg2IndexFunctor> VRegDefs;
|
|
|
|
IndexedMap<std::vector<SUnit*>, VirtReg2IndexFunctor> VRegUses;
|
|
|
|
|
2009-01-16 03:20:50 +08:00
|
|
|
/// 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-11 07:27:53 +08:00
|
|
|
/// LoopRegs - Track which registers are used for loop-carried dependencies.
|
|
|
|
///
|
|
|
|
LoopDependencies LoopRegs;
|
|
|
|
|
2011-06-03 05:26:52 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/// DbgValues - Remember instruction that preceeds DBG_VALUE.
|
2011-10-07 14:27:02 +08:00
|
|
|
typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
|
2011-06-03 05:26:52 +08:00
|
|
|
DbgValueVector;
|
|
|
|
DbgValueVector DbgValues;
|
|
|
|
MachineInstr *FirstDbgValue;
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
public:
|
2009-02-11 12:27:20 +08:00
|
|
|
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-16 03:20:50 +08:00
|
|
|
explicit ScheduleDAGInstrs(MachineFunction &mf,
|
|
|
|
const MachineLoopInfo &mli,
|
2012-01-14 10:17:12 +08:00
|
|
|
const MachineDominatorTree &mdt,
|
|
|
|
bool IsPostRAFlag);
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
virtual ~ScheduleDAGInstrs() {}
|
|
|
|
|
|
|
|
/// NewSUnit - Creates a new SUnit and return a ptr to it.
|
|
|
|
///
|
|
|
|
SUnit *NewSUnit(MachineInstr *MI) {
|
2008-12-23 05:08:08 +08:00
|
|
|
#ifndef NDEBUG
|
2009-01-20 17:05:19 +08:00
|
|
|
const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
|
2008-12-23 05:08:08 +08:00
|
|
|
#endif
|
2008-11-20 07:18:57 +08:00
|
|
|
SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
|
2009-01-20 17:05:19 +08:00
|
|
|
assert((Addr == 0 || Addr == &SUnits[0]) &&
|
|
|
|
"SUnits std::vector reallocated on the fly!");
|
2008-11-20 07:18:57 +08:00
|
|
|
SUnits.back().OrigNode = &SUnits.back();
|
|
|
|
return &SUnits.back();
|
|
|
|
}
|
|
|
|
|
2009-02-11 12:27:20 +08:00
|
|
|
/// Run - perform scheduling.
|
|
|
|
///
|
|
|
|
void Run(MachineBasicBlock *bb,
|
|
|
|
MachineBasicBlock::iterator begin,
|
|
|
|
MachineBasicBlock::iterator end,
|
|
|
|
unsigned endindex);
|
|
|
|
|
2008-12-24 02:36:58 +08:00
|
|
|
/// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
|
2008-11-20 07:18:57 +08:00
|
|
|
/// input.
|
2009-10-10 07:27:56 +08:00
|
|
|
virtual void BuildSchedGraph(AliasAnalysis *AA);
|
2008-11-20 07:18:57 +08:00
|
|
|
|
2010-10-23 10:10:46 +08:00
|
|
|
/// AddSchedBarrierDeps - Add dependencies from instructions in the current
|
|
|
|
/// list of instructions being scheduled to scheduling barrier. We want to
|
|
|
|
/// make sure instructions which define registers that are either used by
|
|
|
|
/// the terminator or are live-out are properly scheduled. This is
|
|
|
|
/// especially important when the definition latency of the return value(s)
|
|
|
|
/// are too high to be hidden by the branch or when the liveout registers
|
|
|
|
/// used by instructions in the fallthrough block.
|
|
|
|
void AddSchedBarrierDeps();
|
|
|
|
|
2008-11-21 08:12:10 +08:00
|
|
|
/// ComputeLatency - Compute node latency.
|
|
|
|
///
|
|
|
|
virtual void ComputeLatency(SUnit *SU);
|
|
|
|
|
2009-08-20 00:08:58 +08:00
|
|
|
/// ComputeOperandLatency - Override dependence edge latency using
|
|
|
|
/// operand use/def information
|
|
|
|
///
|
|
|
|
virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
|
|
|
|
SDep& dep) const;
|
|
|
|
|
2010-05-01 08:01:06 +08:00
|
|
|
virtual MachineBasicBlock *EmitSchedule();
|
2008-11-20 07:18:57 +08:00
|
|
|
|
2009-02-11 07:27:53 +08:00
|
|
|
/// StartBlock - Prepare to perform scheduling in the given block.
|
|
|
|
///
|
|
|
|
virtual void StartBlock(MachineBasicBlock *BB);
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
/// Schedule - Order nodes according to selected style, filling
|
|
|
|
/// in the Sequence member.
|
|
|
|
///
|
|
|
|
virtual void Schedule() = 0;
|
|
|
|
|
2009-02-11 07:27:53 +08:00
|
|
|
/// FinishBlock - Clean up after scheduling in the given block.
|
|
|
|
///
|
|
|
|
virtual void FinishBlock();
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
virtual void dumpNode(const SUnit *SU) const;
|
|
|
|
|
|
|
|
virtual std::string getGraphNodeLabel(const SUnit *SU) const;
|
2012-01-14 10:17:15 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
|
2012-01-14 10:17:18 +08:00
|
|
|
void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
|
|
|
|
void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
|
2008-11-20 07:18:57 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|