2008-12-08 17:50:35 +00:00
|
|
|
//===---- ScheduleDAGInstrs.cpp - MachineInstr Rescheduling ---------------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-12-08 17:50:35 +00:00
|
|
|
// This implements the ScheduleDAGInstrs class, which implements re-scheduling
|
|
|
|
// of MachineInstrs.
|
2008-11-19 23:18:57 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "sched-instrs"
|
2009-02-06 17:12:10 +00:00
|
|
|
#include "ScheduleDAGInstrs.h"
|
2009-07-17 20:58:59 +00:00
|
|
|
#include "llvm/Operator.h"
|
2009-01-30 02:49:14 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2008-12-16 03:25:46 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2008-12-04 01:35:46 +00:00
|
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2008-12-16 03:25:46 +00:00
|
|
|
#include "llvm/Target/TargetSubtarget.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-12-16 03:25:46 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-01-15 19:20:50 +00:00
|
|
|
ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf,
|
2008-12-16 03:25:46 +00:00
|
|
|
const MachineLoopInfo &mli,
|
|
|
|
const MachineDominatorTree &mdt)
|
2009-02-10 23:27:53 +00:00
|
|
|
: ScheduleDAG(mf), MLI(mli), MDT(mdt), LoopRegs(MLI, MDT) {}
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2009-02-11 04:27:20 +00:00
|
|
|
/// Run - perform scheduling.
|
|
|
|
///
|
|
|
|
void ScheduleDAGInstrs::Run(MachineBasicBlock *bb,
|
|
|
|
MachineBasicBlock::iterator begin,
|
|
|
|
MachineBasicBlock::iterator end,
|
|
|
|
unsigned endcount) {
|
|
|
|
BB = bb;
|
|
|
|
Begin = begin;
|
|
|
|
InsertPosIndex = endcount;
|
|
|
|
|
|
|
|
ScheduleDAG::Run(bb, end);
|
|
|
|
}
|
|
|
|
|
2009-01-30 02:49:14 +00:00
|
|
|
/// getUnderlyingObjectFromInt - This is the function that does the work of
|
|
|
|
/// looking through basic ptrtoint+arithmetic+inttoptr sequences.
|
|
|
|
static const Value *getUnderlyingObjectFromInt(const Value *V) {
|
|
|
|
do {
|
2009-07-17 20:58:59 +00:00
|
|
|
if (const Operator *U = dyn_cast<Operator>(V)) {
|
2009-01-30 02:49:14 +00:00
|
|
|
// If we find a ptrtoint, we can transfer control back to the
|
|
|
|
// regular getUnderlyingObjectFromInt.
|
2009-07-17 20:58:59 +00:00
|
|
|
if (U->getOpcode() == Instruction::PtrToInt)
|
2009-01-30 02:49:14 +00:00
|
|
|
return U->getOperand(0);
|
|
|
|
// If we find an add of a constant or a multiplied value, it's
|
|
|
|
// likely that the other operand will lead us to the base
|
|
|
|
// object. We don't have to worry about the case where the
|
2009-08-07 01:26:06 +00:00
|
|
|
// object address is somehow being computed by the multiply,
|
2009-01-30 02:49:14 +00:00
|
|
|
// because our callers only care when the result is an
|
|
|
|
// identifibale object.
|
2009-07-17 20:58:59 +00:00
|
|
|
if (U->getOpcode() != Instruction::Add ||
|
2009-01-30 02:49:14 +00:00
|
|
|
(!isa<ConstantInt>(U->getOperand(1)) &&
|
2009-07-17 20:58:59 +00:00
|
|
|
Operator::getOpcode(U->getOperand(1)) != Instruction::Mul))
|
2009-01-30 02:49:14 +00:00
|
|
|
return V;
|
|
|
|
V = U->getOperand(0);
|
|
|
|
} else {
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
assert(isa<IntegerType>(V->getType()) && "Unexpected operand type!");
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getUnderlyingObject - This is a wrapper around Value::getUnderlyingObject
|
|
|
|
/// and adds support for basic ptrtoint+arithmetic+inttoptr sequences.
|
|
|
|
static const Value *getUnderlyingObject(const Value *V) {
|
|
|
|
// First just call Value::getUnderlyingObject to let it do what it does.
|
|
|
|
do {
|
|
|
|
V = V->getUnderlyingObject();
|
|
|
|
// If it found an inttoptr, use special code to continue climing.
|
2009-07-17 20:58:59 +00:00
|
|
|
if (Operator::getOpcode(V) != Instruction::IntToPtr)
|
2009-01-30 02:49:14 +00:00
|
|
|
break;
|
|
|
|
const Value *O = getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
|
|
|
|
// If that succeeded in finding a pointer, continue the search.
|
|
|
|
if (!isa<PointerType>(O->getType()))
|
|
|
|
break;
|
|
|
|
V = O;
|
|
|
|
} while (1);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getUnderlyingObjectForInstr - If this machine instr has memory reference
|
|
|
|
/// information and it can be tracked to a normal reference to a known
|
|
|
|
/// object, return the Value for that object. Otherwise return null.
|
|
|
|
static const Value *getUnderlyingObjectForInstr(const MachineInstr *MI) {
|
|
|
|
if (!MI->hasOneMemOperand() ||
|
|
|
|
!MI->memoperands_begin()->getValue() ||
|
|
|
|
MI->memoperands_begin()->isVolatile())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const Value *V = MI->memoperands_begin()->getValue();
|
|
|
|
if (!V)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
V = getUnderlyingObject(V);
|
|
|
|
if (!isa<PseudoSourceValue>(V) && !isIdentifiedObject(V))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
void ScheduleDAGInstrs::StartBlock(MachineBasicBlock *BB) {
|
|
|
|
if (MachineLoop *ML = MLI.getLoopFor(BB))
|
|
|
|
if (BB == ML->getLoopLatch()) {
|
|
|
|
MachineBasicBlock *Header = ML->getHeader();
|
|
|
|
for (MachineBasicBlock::livein_iterator I = Header->livein_begin(),
|
|
|
|
E = Header->livein_end(); I != E; ++I)
|
|
|
|
LoopLiveInRegs.insert(*I);
|
|
|
|
LoopRegs.VisitLoop(ML);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-23 18:36:58 +00:00
|
|
|
void ScheduleDAGInstrs::BuildSchedGraph() {
|
2009-02-10 23:27:53 +00:00
|
|
|
// We'll be allocating one SUnit for each instruction, plus one for
|
|
|
|
// the region exit node.
|
2008-11-19 23:18:57 +00:00
|
|
|
SUnits.reserve(BB->size());
|
|
|
|
|
2008-12-04 01:35:46 +00:00
|
|
|
// We build scheduling units by walking a block's instruction list from bottom
|
|
|
|
// to top.
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2008-12-16 03:25:46 +00:00
|
|
|
// Check to see if the scheduler cares about latencies.
|
|
|
|
bool UnitLatencies = ForceUnitLatencies();
|
|
|
|
|
2008-12-16 03:35:01 +00:00
|
|
|
// Ask the target if address-backscheduling is desirable, and if so how much.
|
|
|
|
unsigned SpecialAddressLatency =
|
|
|
|
TM.getSubtarget<TargetSubtarget>().getSpecialAddressLatency();
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
// Walk the list of instructions, from bottom moving up.
|
2009-02-11 04:27:20 +00:00
|
|
|
for (MachineBasicBlock::iterator MII = InsertPos, MIE = Begin;
|
2008-11-19 23:18:57 +00:00
|
|
|
MII != MIE; --MII) {
|
|
|
|
MachineInstr *MI = prior(MII);
|
2008-12-16 03:25:46 +00:00
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
2009-02-10 23:27:53 +00:00
|
|
|
assert(!TID.isTerminator() && !MI->isLabel() &&
|
|
|
|
"Cannot schedule terminators or labels!");
|
|
|
|
// Create the SUnit for this MI.
|
2008-11-19 23:18:57 +00:00
|
|
|
SUnit *SU = NewSUnit(MI);
|
|
|
|
|
2008-12-09 22:54:47 +00:00
|
|
|
// Assign the Latency field of SU using target-provided information.
|
2008-12-16 03:25:46 +00:00
|
|
|
if (UnitLatencies)
|
|
|
|
SU->Latency = 1;
|
|
|
|
else
|
|
|
|
ComputeLatency(SU);
|
2008-12-09 22:54:47 +00:00
|
|
|
|
2008-12-04 01:35:46 +00:00
|
|
|
// Add register-based dependencies (data, anti, and output).
|
2008-11-19 23:18:57 +00: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];
|
2008-12-16 03:25:46 +00:00
|
|
|
std::vector<SUnit *> &DefList = Defs[Reg];
|
2008-11-21 19:16:58 +00:00
|
|
|
// Optionally add output and anti dependencies.
|
2008-12-09 22:54:47 +00: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-12-16 03:25:46 +00:00
|
|
|
for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
|
|
|
|
SUnit *DefSU = DefList[i];
|
|
|
|
if (DefSU != SU &&
|
|
|
|
(Kind != SDep::Output || !MO.isDead() ||
|
|
|
|
!DefSU->getInstr()->registerDefIsDead(Reg)))
|
|
|
|
DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/Reg));
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
2008-12-16 03:25:46 +00:00
|
|
|
std::vector<SUnit *> &DefList = Defs[*Alias];
|
|
|
|
for (unsigned i = 0, e = DefList.size(); i != e; ++i) {
|
|
|
|
SUnit *DefSU = DefList[i];
|
|
|
|
if (DefSU != SU &&
|
|
|
|
(Kind != SDep::Output || !MO.isDead() ||
|
|
|
|
!DefSU->getInstr()->registerDefIsDead(Reg)))
|
|
|
|
DefSU->addPred(SDep(SU, Kind, /*Latency=*/1, /*Reg=*/ *Alias));
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MO.isDef()) {
|
|
|
|
// Add any data dependencies.
|
2008-12-16 03:25:46 +00:00
|
|
|
unsigned DataLatency = SU->Latency;
|
|
|
|
for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
|
|
|
|
SUnit *UseSU = UseList[i];
|
|
|
|
if (UseSU != SU) {
|
2008-12-16 03:35:01 +00:00
|
|
|
unsigned LDataLatency = DataLatency;
|
|
|
|
// Optionally add in a special extra latency for nodes that
|
|
|
|
// feed addresses.
|
|
|
|
// TODO: Do this for register aliases too.
|
|
|
|
if (SpecialAddressLatency != 0 && !UnitLatencies) {
|
|
|
|
MachineInstr *UseMI = UseSU->getInstr();
|
|
|
|
const TargetInstrDesc &UseTID = UseMI->getDesc();
|
|
|
|
int RegUseIndex = UseMI->findRegisterUseOperandIdx(Reg);
|
|
|
|
assert(RegUseIndex >= 0 && "UseMI doesn's use register!");
|
|
|
|
if ((UseTID.mayLoad() || UseTID.mayStore()) &&
|
|
|
|
(unsigned)RegUseIndex < UseTID.getNumOperands() &&
|
|
|
|
UseTID.OpInfo[RegUseIndex].isLookupPtrRegClass())
|
|
|
|
LDataLatency += SpecialAddressLatency;
|
|
|
|
}
|
|
|
|
UseSU->addPred(SDep(SU, SDep::Data, LDataLatency, Reg));
|
2008-12-16 03:25:46 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
|
|
|
|
std::vector<SUnit *> &UseList = Uses[*Alias];
|
2008-12-16 03:25:46 +00:00
|
|
|
for (unsigned i = 0, e = UseList.size(); i != e; ++i) {
|
|
|
|
SUnit *UseSU = UseList[i];
|
|
|
|
if (UseSU != SU)
|
|
|
|
UseSU->addPred(SDep(SU, SDep::Data, DataLatency, *Alias));
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-16 03:35:01 +00:00
|
|
|
// If a def is going to wrap back around to the top of the loop,
|
|
|
|
// backschedule it.
|
2009-02-10 23:27:53 +00:00
|
|
|
if (!UnitLatencies && DefList.empty()) {
|
2008-12-16 03:35:01 +00:00
|
|
|
LoopDependencies::LoopDeps::iterator I = LoopRegs.Deps.find(Reg);
|
|
|
|
if (I != LoopRegs.Deps.end()) {
|
|
|
|
const MachineOperand *UseMO = I->second.first;
|
|
|
|
unsigned Count = I->second.second;
|
|
|
|
const MachineInstr *UseMI = UseMO->getParent();
|
|
|
|
unsigned UseMOIdx = UseMO - &UseMI->getOperand(0);
|
|
|
|
const TargetInstrDesc &UseTID = UseMI->getDesc();
|
|
|
|
// TODO: If we knew the total depth of the region here, we could
|
|
|
|
// handle the case where the whole loop is inside the region but
|
|
|
|
// is large enough that the isScheduleHigh trick isn't needed.
|
|
|
|
if (UseMOIdx < UseTID.getNumOperands()) {
|
|
|
|
// Currently, we only support scheduling regions consisting of
|
|
|
|
// single basic blocks. Check to see if the instruction is in
|
|
|
|
// the same region by checking to see if it has the same parent.
|
|
|
|
if (UseMI->getParent() != MI->getParent()) {
|
|
|
|
unsigned Latency = SU->Latency;
|
|
|
|
if (UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass())
|
|
|
|
Latency += SpecialAddressLatency;
|
|
|
|
// This is a wild guess as to the portion of the latency which
|
|
|
|
// will be overlapped by work done outside the current
|
|
|
|
// scheduling region.
|
|
|
|
Latency -= std::min(Latency, Count);
|
|
|
|
// Add the artifical edge.
|
2009-02-10 23:27:53 +00:00
|
|
|
ExitSU.addPred(SDep(SU, SDep::Order, Latency,
|
|
|
|
/*Reg=*/0, /*isNormalMemory=*/false,
|
|
|
|
/*isMustAlias=*/false,
|
|
|
|
/*isArtificial=*/true));
|
2008-12-16 03:35:01 +00:00
|
|
|
} else if (SpecialAddressLatency > 0 &&
|
|
|
|
UseTID.OpInfo[UseMOIdx].isLookupPtrRegClass()) {
|
|
|
|
// The entire loop body is within the current scheduling region
|
|
|
|
// and the latency of this operation is assumed to be greater
|
|
|
|
// than the latency of the loop.
|
|
|
|
// TODO: Recursively mark data-edge predecessors as
|
|
|
|
// isScheduleHigh too.
|
|
|
|
SU->isScheduleHigh = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LoopRegs.Deps.erase(I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
UseList.clear();
|
2008-12-16 03:25:46 +00:00
|
|
|
if (!MO.isDead())
|
|
|
|
DefList.clear();
|
|
|
|
DefList.push_back(SU);
|
2008-11-19 23:18:57 +00:00
|
|
|
} else {
|
|
|
|
UseList.push_back(SU);
|
|
|
|
}
|
|
|
|
}
|
2008-12-04 01:35:46 +00: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.
|
2009-02-10 23:27:53 +00:00
|
|
|
if (TID.isCall() || TID.hasUnmodeledSideEffects()) {
|
2008-12-04 01:35:46 +00:00
|
|
|
new_chain:
|
2008-12-08 17:50:35 +00:00
|
|
|
// This is the conservative case. Add dependencies on all memory
|
|
|
|
// references.
|
2008-11-19 23:18:57 +00:00
|
|
|
if (Chain)
|
2008-12-09 22:54:47 +00:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00:00
|
|
|
Chain = SU;
|
2008-11-19 23:18:57 +00:00
|
|
|
for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
|
2008-12-09 22:54:47 +00:00
|
|
|
PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-11-19 23:18:57 +00:00
|
|
|
PendingLoads.clear();
|
2008-12-04 01:35:46 +00:00
|
|
|
for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
|
|
|
|
E = MemDefs.end(); I != E; ++I) {
|
2008-12-09 22:54:47 +00:00
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00: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-09 22:54:47 +00:00
|
|
|
I->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00: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();
|
2009-02-10 23:27:53 +00:00
|
|
|
if (!ChainTID.isCall() &&
|
2008-12-04 01:35:46 +00:00
|
|
|
!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()) {
|
2009-01-30 02:49:14 +00:00
|
|
|
if (const Value *V = getUnderlyingObjectForInstr(MI)) {
|
2008-12-04 01:35:46 +00:00
|
|
|
// A store to a specific PseudoSourceValue. Add precise dependencies.
|
|
|
|
// 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-09 22:54:47 +00:00
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
|
|
|
|
/*isNormalMemory=*/true));
|
2008-12-04 01:35:46 +00:00
|
|
|
I->second = SU;
|
|
|
|
} else {
|
|
|
|
MemDefs[V] = SU;
|
|
|
|
}
|
|
|
|
// Handle the uses in MemUses, if there are any.
|
2008-12-08 17:50:35 +00:00
|
|
|
std::map<const Value *, std::vector<SUnit *> >::iterator J =
|
|
|
|
MemUses.find(V);
|
2008-12-04 01:35:46 +00:00
|
|
|
if (J != MemUses.end()) {
|
|
|
|
for (unsigned i = 0, e = J->second.size(); i != e; ++i)
|
2008-12-09 22:54:47 +00:00
|
|
|
J->second[i]->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
|
|
|
|
/*isNormalMemory=*/true));
|
2008-12-04 01:35:46 +00:00
|
|
|
J->second.clear();
|
|
|
|
}
|
2009-01-30 02:49:14 +00:00
|
|
|
// Add dependencies from all the PendingLoads, since without
|
|
|
|
// memoperands we must assume they alias anything.
|
|
|
|
for (unsigned k = 0, m = PendingLoads.size(); k != m; ++k)
|
|
|
|
PendingLoads[k]->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00:00
|
|
|
// Add a general dependence too, if needed.
|
|
|
|
if (Chain)
|
2008-12-09 22:54:47 +00:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00:00
|
|
|
} else
|
|
|
|
// Treat all other stores conservatively.
|
|
|
|
goto new_chain;
|
|
|
|
} else if (TID.mayLoad()) {
|
|
|
|
if (TII->isInvariantLoad(MI)) {
|
|
|
|
// Invariant load, no chain dependencies needed!
|
2009-01-30 02:49:14 +00:00
|
|
|
} else if (const Value *V = getUnderlyingObjectForInstr(MI)) {
|
2008-12-04 01:35:46 +00:00
|
|
|
// A load from a specific PseudoSourceValue. Add precise dependencies.
|
|
|
|
std::map<const Value *, SUnit *>::iterator I = MemDefs.find(V);
|
|
|
|
if (I != MemDefs.end())
|
2008-12-09 22:54:47 +00:00
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency, /*Reg=*/0,
|
|
|
|
/*isNormalMemory=*/true));
|
2008-12-04 01:35:46 +00:00
|
|
|
MemUses[V].push_back(SU);
|
|
|
|
|
|
|
|
// Add a general dependence too, if needed.
|
|
|
|
if (Chain && (!ChainMMO ||
|
|
|
|
(ChainMMO->isStore() || ChainMMO->isVolatile())))
|
2008-12-09 22:54:47 +00:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00:00
|
|
|
} else if (MI->hasVolatileMemoryRef()) {
|
|
|
|
// Treat volatile loads conservatively. Note that this includes
|
|
|
|
// cases where memoperand information is unavailable.
|
|
|
|
goto new_chain;
|
|
|
|
} else {
|
2009-01-30 02:49:14 +00:00
|
|
|
// A normal load. Depend on the general chain, as well as on
|
|
|
|
// all stores. In the absense of MachineMemOperand information,
|
|
|
|
// we can't even assume that the load doesn't alias well-behaved
|
|
|
|
// memory locations.
|
2008-12-04 01:35:46 +00:00
|
|
|
if (Chain)
|
2008-12-09 22:54:47 +00:00
|
|
|
Chain->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2009-01-30 02:49:14 +00:00
|
|
|
for (std::map<const Value *, SUnit *>::iterator I = MemDefs.begin(),
|
|
|
|
E = MemDefs.end(); I != E; ++I)
|
|
|
|
I->second->addPred(SDep(SU, SDep::Order, SU->Latency));
|
2008-12-04 01:35:46 +00:00
|
|
|
PendingLoads.push_back(SU);
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-15 19:20:50 +00:00
|
|
|
|
|
|
|
for (int i = 0, e = TRI->getNumRegs(); i != e; ++i) {
|
|
|
|
Defs[i].clear();
|
|
|
|
Uses[i].clear();
|
|
|
|
}
|
|
|
|
PendingLoads.clear();
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
void ScheduleDAGInstrs::FinishBlock() {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
|
2008-11-21 00:12:10 +00: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-12-16 02:38:22 +00:00
|
|
|
|
|
|
|
// Simplistic target-independent heuristic: assume that loads take
|
|
|
|
// extra time.
|
|
|
|
if (InstrItins.isEmpty())
|
|
|
|
if (SU->getInstr()->getDesc().mayLoad())
|
|
|
|
SU->Latency += 2;
|
2008-11-21 00:12:10 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 23:18:57 +00: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);
|
2009-02-10 23:27:53 +00:00
|
|
|
if (SU == &EntrySU)
|
|
|
|
oss << "<entry>";
|
|
|
|
else if (SU == &ExitSU)
|
|
|
|
oss << "<exit>";
|
|
|
|
else
|
|
|
|
SU->getInstr()->print(oss);
|
2008-11-19 23:18:57 +00:00
|
|
|
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.
|
2009-02-11 04:27:20 +00:00
|
|
|
while (Begin != InsertPos) {
|
2009-01-16 22:10:20 +00:00
|
|
|
MachineBasicBlock::iterator I = Begin;
|
|
|
|
++Begin;
|
|
|
|
BB->remove(I);
|
|
|
|
}
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2008-12-23 21:37:04 +00:00
|
|
|
// Then re-insert them according to the given schedule.
|
2008-11-19 23:18:57 +00:00
|
|
|
for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
|
|
|
|
SUnit *SU = Sequence[i];
|
|
|
|
if (!SU) {
|
|
|
|
// Null SUnit* is a noop.
|
|
|
|
EmitNoop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-11 04:27:20 +00:00
|
|
|
BB->insert(InsertPos, SU->getInstr());
|
2008-11-19 23:18:57 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
// Update the Begin iterator, as the first instruction in the block
|
|
|
|
// may have been scheduled later.
|
|
|
|
if (!Sequence.empty())
|
|
|
|
Begin = Sequence[0]->getInstr();
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
return BB;
|
|
|
|
}
|