2008-12-09 01:50:35 +08:00
|
|
|
//===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-12-09 01:50:35 +08:00
|
|
|
// This implements the ScheduleDAGInstrs class, which implements re-scheduling
|
|
|
|
// of MachineInstrs.
|
2008-11-20 07:18:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "sched-instrs"
|
|
|
|
#include "llvm/CodeGen/ScheduleDAGInstrs.h"
|
2008-12-04 09:35:46 +08:00
|
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
2008-11-20 07:18:57 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-12-04 09:35:46 +08:00
|
|
|
#include <map>
|
2008-11-20 07:18:57 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
ScheduleDAGInstrs::ScheduleDAGInstrs(MachineBasicBlock *bb,
|
|
|
|
const TargetMachine &tm)
|
|
|
|
: ScheduleDAG(0, bb, tm) {}
|
|
|
|
|
|
|
|
void ScheduleDAGInstrs::BuildSchedUnits() {
|
|
|
|
SUnits.clear();
|
|
|
|
SUnits.reserve(BB->size());
|
|
|
|
|
2008-12-04 09:35:46 +08:00
|
|
|
// We build scheduling units by walking a block's instruction list from bottom
|
|
|
|
// to top.
|
|
|
|
|
|
|
|
// Remember where defs and uses of each physical register are as we procede.
|
2008-11-20 07:18:57 +08:00
|
|
|
SUnit *Defs[TargetRegisterInfo::FirstVirtualRegister] = {};
|
|
|
|
std::vector<SUnit *> Uses[TargetRegisterInfo::FirstVirtualRegister] = {};
|
2008-12-04 09:35:46 +08:00
|
|
|
|
|
|
|
// Remember where unknown loads are after the most recent unknown store
|
|
|
|
// as we procede.
|
|
|
|
std::vector<SUnit *> PendingLoads;
|
|
|
|
|
|
|
|
// Remember where a generic side-effecting instruction is as we procede. If
|
|
|
|
// ChainMMO is null, this is assumed to have arbitrary side-effects. If
|
|
|
|
// ChainMMO is non-null, then Chain makes only a single memory reference.
|
|
|
|
SUnit *Chain = 0;
|
|
|
|
MachineMemOperand *ChainMMO = 0;
|
|
|
|
|
|
|
|
// Memory references to specific known memory locations are tracked so that
|
|
|
|
// they can be given more precise dependencies.
|
|
|
|
std::map<const Value *, SUnit *> MemDefs;
|
|
|
|
std::map<const Value *, std::vector<SUnit *> > MemUses;
|
|
|
|
|
|
|
|
// Terminators can perform control transfers, we we need to make sure that
|
|
|
|
// all the work of the block is done before the terminator.
|
|
|
|
SUnit *Terminator = 0;
|
2008-11-20 07:18:57 +08:00
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator MII = BB->end(), MIE = BB->begin();
|
|
|
|
MII != MIE; --MII) {
|
|
|
|
MachineInstr *MI = prior(MII);
|
|
|
|
SUnit *SU = NewSUnit(MI);
|
|
|
|
|
2008-12-10 06:54:47 +08:00
|
|
|
// Assign the Latency field of SU using target-provided information.
|
|
|
|
ComputeLatency(SU);
|
|
|
|
|
2008-12-04 09:35:46 +08:00
|
|
|
// Add register-based dependencies (data, anti, and output).
|
2008-11-20 07:18:57 +08:00
|
|
|
for (unsigned j = 0, n = MI->getNumOperands(); j != n; ++j) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(j);
|
|
|
|
if (!MO.isReg()) continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (Reg == 0) continue;
|
|
|
|
|
|
|
|
assert(TRI->isPhysicalRegister(Reg) && "Virtual register encountered!");
|
|
|
|
std::vector<SUnit *> &UseList = Uses[Reg];
|
|
|
|
SUnit *&Def = Defs[Reg];
|
2008-11-22 03:16:58 +08:00
|
|
|
// Optionally add output and anti dependencies.
|
2008-12-10 06:54:47 +08:00
|
|
|
// TODO: Using a latency of 1 here assumes there's no cost for
|
|
|
|
// reusing registers.
|
|
|
|
SDep::Kind Kind = MO.isUse() ? SDep::Anti : SDep::Output;
|
2008-11-20 07:18:57 +08:00
|
|
|
if (Def && Def != SU)
|
2008-12-10 06:54:47 +08:00
|
|
|
Def->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
|
2008-11-20 07:18:57 +08:00
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
SUnit *&Def = Defs[*Alias];
|
|
|
|
if (Def && Def != SU)
|
2008-12-10 06:54:47 +08:00
|
|
|
Def->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MO.isDef()) {
|
|
|
|
// Add any data dependencies.
|
|
|
|
for (unsigned i = 0, e = UseList.size(); i != e; ++i)
|
|
|
|
if (UseList[i] != SU)
|
2008-12-10 06:54:47 +08:00
|
|
|
UseList[i]->addPred(SDep(SU, SDep::Data, SU->Latency, Reg));
|
2008-11-20 07:18:57 +08:00
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
std::vector<SUnit *> &UseList = Uses[*Alias];
|
|
|
|
for (unsigned i = 0, e = UseList.size(); i != e; ++i)
|
|
|
|
if (UseList[i] != SU)
|
2008-12-10 06:54:47 +08:00
|
|
|
UseList[i]->addPred(SDep(SU, SDep::Data, SU->Latency, *Alias));
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UseList.clear();
|
|
|
|
Def = SU;
|
|
|
|
} else {
|
|
|
|
UseList.push_back(SU);
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 09:35:46 +08:00
|
|
|
|
|
|
|
// Add chain dependencies.
|
|
|
|
// Note that isStoreToStackSlot and isLoadFromStackSLot are not usable
|
|
|
|
// after stack slots are lowered to actual addresses.
|
|
|
|
// TODO: Use an AliasAnalysis and do real alias-analysis queries, and
|
|
|
|
// produce more precise dependence information.
|
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
|
|
|
if (TID.isCall() || TID.isReturn() || TID.isBranch() ||
|
|
|
|
TID.hasUnmodeledSideEffects()) {
|
|
|
|
new_chain:
|
2008-12-09 01:50:35 +08:00
|
|
|
// This is the conservative case. Add dependencies on all memory
|
|
|
|
// references.
|
2008-11-20 07:18:57 +08:00
|
|
|
if (Chain)
|
2008-12-10 06:54:47 +08:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
Chain = SU;
|
2008-11-20 07:18:57 +08:00
|
|
|
for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
|
2008-12-10 06:54:47 +08:00
|
|
|
PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-11-20 07:18:57 +08:00
|
|
|
PendingLoads.clear();
|
2008-12-04 09:35:46 +08:00
|
|
|
for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
|
|
|
|
E = MemDefs.end(); I != E; ++I) {
|
2008-12-10 06:54:47 +08:00
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
I->second = SU;
|
|
|
|
}
|
|
|
|
for (std::map<const Value *, std::vector<SUnit *> >::iterator I =
|
|
|
|
MemUses.begin(), E = MemUses.end(); I != E; ++I) {
|
|
|
|
for (unsigned i = 0, e = I->second.size(); i != e; ++i)
|
2008-12-10 06:54:47 +08:00
|
|
|
I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
I->second.clear();
|
|
|
|
}
|
|
|
|
// See if it is known to just have a single memory reference.
|
|
|
|
MachineInstr *ChainMI = Chain->getInstr();
|
|
|
|
const TargetInstrDesc &ChainTID = ChainMI->getDesc();
|
|
|
|
if (!ChainTID.isCall() && !ChainTID.isReturn() && !ChainTID.isBranch() &&
|
|
|
|
!ChainTID.hasUnmodeledSideEffects() &&
|
|
|
|
ChainMI->hasOneMemOperand() &&
|
|
|
|
!ChainMI->memoperands_begin()->isVolatile() &&
|
|
|
|
ChainMI->memoperands_begin()->getValue())
|
|
|
|
// We know that the Chain accesses one specific memory location.
|
|
|
|
ChainMMO = &*ChainMI->memoperands_begin();
|
|
|
|
else
|
|
|
|
// Unknown memory accesses. Assume the worst.
|
|
|
|
ChainMMO = 0;
|
|
|
|
} else if (TID.mayStore()) {
|
|
|
|
if (MI->hasOneMemOperand() &&
|
|
|
|
MI->memoperands_begin()->getValue() &&
|
|
|
|
!MI->memoperands_begin()->isVolatile() &&
|
|
|
|
isa<PseudoSourceValue>(MI->memoperands_begin()->getValue())) {
|
|
|
|
// A store to a specific PseudoSourceValue. Add precise dependencies.
|
|
|
|
const Value *V = MI->memoperands_begin()->getValue();
|
|
|
|
// Handle the def in MemDefs, if there is one.
|
|
|
|
std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
|
|
|
|
if (I != MemDefs.end()) {
|
2008-12-10 06:54:47 +08:00
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
|
|
|
|
/*isNormalMemory=*/true));
|
2008-12-04 09:35:46 +08:00
|
|
|
I->second = SU;
|
|
|
|
} else {
|
|
|
|
MemDefs[V] = SU;
|
|
|
|
}
|
|
|
|
// Handle the uses in MemUses, if there are any.
|
2008-12-09 01:50:35 +08:00
|
|
|
std::map<const Value *, std::vector<SUnit *> >::iterator J =
|
|
|
|
MemUses.find(V);
|
2008-12-04 09:35:46 +08:00
|
|
|
if (J != MemUses.end()) {
|
|
|
|
for (unsigned i = 0, e = J->second.size(); i != e; ++i)
|
2008-12-10 06:54:47 +08:00
|
|
|
J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
|
|
|
|
/*isNormalMemory=*/true));
|
2008-12-04 09:35:46 +08:00
|
|
|
J->second.clear();
|
|
|
|
}
|
|
|
|
// Add a general dependence too, if needed.
|
|
|
|
if (Chain)
|
2008-12-10 06:54:47 +08:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
} else
|
|
|
|
// Treat all other stores conservatively.
|
|
|
|
goto new_chain;
|
|
|
|
} else if (TID.mayLoad()) {
|
|
|
|
if (TII->isInvariantLoad(MI)) {
|
|
|
|
// Invariant load, no chain dependencies needed!
|
|
|
|
} else if (MI->hasOneMemOperand() &&
|
|
|
|
MI->memoperands_begin()->getValue() &&
|
|
|
|
!MI->memoperands_begin()->isVolatile() &&
|
|
|
|
isa<PseudoSourceValue>(MI->memoperands_begin()->getValue())) {
|
|
|
|
// A load from a specific PseudoSourceValue. Add precise dependencies.
|
|
|
|
const Value *V = MI->memoperands_begin()->getValue();
|
|
|
|
std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
|
|
|
|
if (I != MemDefs.end())
|
2008-12-10 06:54:47 +08:00
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
|
|
|
|
/*isNormalMemory=*/true));
|
2008-12-04 09:35:46 +08:00
|
|
|
MemUses[V].push_back(SU);
|
|
|
|
|
|
|
|
// Add a general dependence too, if needed.
|
|
|
|
if (Chain && (!ChainMMO ||
|
|
|
|
(ChainMMO->isStore() || ChainMMO->isVolatile())))
|
2008-12-10 06:54:47 +08:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
} else if (MI->hasVolatileMemoryRef()) {
|
|
|
|
// Treat volatile loads conservatively. Note that this includes
|
|
|
|
// cases where memoperand information is unavailable.
|
|
|
|
goto new_chain;
|
|
|
|
} else {
|
|
|
|
// A normal load. Just depend on the general chain.
|
|
|
|
if (Chain)
|
2008-12-10 06:54:47 +08:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
PendingLoads.push_back(SU);
|
|
|
|
}
|
2008-11-20 07:18:57 +08:00
|
|
|
}
|
2008-12-04 09:35:46 +08:00
|
|
|
|
2008-12-09 01:50:35 +08:00
|
|
|
// Add chain edges from the terminator to ensure that all the work of the
|
|
|
|
// block is completed before any control transfers.
|
2008-11-20 07:18:57 +08:00
|
|
|
if (Terminator && SU->Succs.empty())
|
2008-12-10 06:54:47 +08:00
|
|
|
Terminator->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 09:35:46 +08:00
|
|
|
if (TID.isTerminator() || MI->isLabel())
|
2008-11-20 07:18:57 +08:00
|
|
|
Terminator = SU;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-21 08:12:10 +08:00
|
|
|
void ScheduleDAGInstrs::ComputeLatency(SUnit *SU) {
|
|
|
|
const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
|
|
|
|
|
|
|
|
// Compute the latency for the node. We use the sum of the latencies for
|
|
|
|
// all nodes flagged together into this SUnit.
|
|
|
|
SU->Latency =
|
|
|
|
InstrItins.getLatency(SU->getInstr()->getDesc().getSchedClass());
|
|
|
|
}
|
|
|
|
|
2008-11-20 07:18:57 +08:00
|
|
|
void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const {
|
|
|
|
SU->getInstr()->dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const {
|
|
|
|
std::string s;
|
|
|
|
raw_string_ostream oss(s);
|
|
|
|
SU->getInstr()->print(oss);
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitSchedule - Emit the machine code in scheduled order.
|
|
|
|
MachineBasicBlock *ScheduleDAGInstrs::EmitSchedule() {
|
|
|
|
// For MachineInstr-based scheduling, we're rescheduling the instructions in
|
|
|
|
// the block, so start by removing them from the block.
|
|
|
|
while (!BB->empty())
|
|
|
|
BB->remove(BB->begin());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
|
|
|
|
SUnit *SU = Sequence[i];
|
|
|
|
if (!SU) {
|
|
|
|
// Null SUnit* is a noop.
|
|
|
|
EmitNoop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BB->push_back(SU->getInstr());
|
|
|
|
}
|
|
|
|
|
|
|
|
return BB;
|
|
|
|
}
|