2011-12-02 05:10:21 +08:00
|
|
|
//=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This class implements a deterministic finite automaton (DFA) based
|
|
|
|
// packetizing mechanism for VLIW architectures. It provides APIs to
|
|
|
|
// determine whether there exists a legal mapping of instructions to
|
|
|
|
// functional unit assignments in a packet. The DFA is auto-generated from
|
|
|
|
// the target's Schedule.td file.
|
|
|
|
//
|
|
|
|
// A DFA consists of 3 major elements: states, inputs, and transitions. For
|
|
|
|
// the packetizing mechanism, the input is the set of instruction classes for
|
|
|
|
// a target. The state models all possible combinations of functional unit
|
|
|
|
// consumption for a given set of instructions in a packet. A transition
|
|
|
|
// models the addition of an instruction to a packet. In the DFA constructed
|
|
|
|
// by this class, if an instruction can be added to a packet, then a valid
|
|
|
|
// transition exists from the corresponding state. Invalid transitions
|
|
|
|
// indicate that the instruction cannot be added to the current packet.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-15 03:04:26 +08:00
|
|
|
#define DEBUG_TYPE "packets"
|
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
#include "llvm/CodeGen/DFAPacketizer.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2012-02-16 02:55:14 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBundle.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/ScheduleDAGInstrs.h"
|
2011-12-02 05:10:21 +08:00
|
|
|
#include "llvm/MC/MCInstrItineraries.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2016-01-15 05:17:04 +08:00
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-11-22 23:20:19 +08:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
|
|
|
|
return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the DFAInput for an instruction class input vector.
|
|
|
|
/// This function is used in both DFAPacketizer.cpp and in
|
|
|
|
/// DFAPacketizerEmitter.cpp.
|
|
|
|
DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
|
|
|
|
DFAInput InsnInput = 0;
|
2016-01-15 05:17:04 +08:00
|
|
|
assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
|
|
|
|
"Exceeded maximum number of DFA terms");
|
2015-11-22 23:20:19 +08:00
|
|
|
for (auto U : InsnClass)
|
|
|
|
InsnInput = addDFAFuncUnits(InsnInput, U);
|
|
|
|
return InsnInput;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
|
|
|
|
const DFAStateInput (*SIT)[2],
|
2011-12-07 01:34:16 +08:00
|
|
|
const unsigned *SET):
|
2011-12-02 05:10:21 +08:00
|
|
|
InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
|
2015-11-22 04:00:45 +08:00
|
|
|
DFAStateEntryTable(SET) {
|
|
|
|
// Make sure DFA types are large enough for the number of terms & resources.
|
2016-05-27 19:36:04 +08:00
|
|
|
static_assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <=
|
|
|
|
(8 * sizeof(DFAInput)),
|
|
|
|
"(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
|
|
|
|
static_assert(
|
|
|
|
(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput)),
|
|
|
|
"(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
// Read the DFA transition table and update CachedTable.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
// Format of the transition tables:
|
|
|
|
// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
|
|
|
|
// transitions
|
|
|
|
// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
|
|
|
|
// for the ith state
|
|
|
|
//
|
|
|
|
void DFAPacketizer::ReadTable(unsigned int state) {
|
|
|
|
unsigned ThisState = DFAStateEntryTable[state];
|
|
|
|
unsigned NextStateInTable = DFAStateEntryTable[state+1];
|
|
|
|
// Early exit in case CachedTable has already contains this
|
2011-12-07 01:34:11 +08:00
|
|
|
// state's transitions.
|
2016-01-15 05:17:04 +08:00
|
|
|
if (CachedTable.count(UnsignPair(state, DFAStateInputTable[ThisState][0])))
|
2011-12-02 05:10:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (unsigned i = ThisState; i < NextStateInTable; i++)
|
|
|
|
CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
|
|
|
|
DFAStateInputTable[i][1];
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// Return the DFAInput for an instruction class.
|
2015-11-22 04:00:45 +08:00
|
|
|
DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
|
|
|
|
// Note: this logic must match that in DFAPacketizerDefs.h for input vectors.
|
|
|
|
DFAInput InsnInput = 0;
|
|
|
|
unsigned i = 0;
|
2016-01-15 05:17:04 +08:00
|
|
|
(void)i;
|
2015-11-22 04:00:45 +08:00
|
|
|
for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
|
2016-01-15 05:17:04 +08:00
|
|
|
*IE = InstrItins->endStage(InsnClass); IS != IE; ++IS) {
|
2015-11-22 04:00:45 +08:00
|
|
|
InsnInput = addDFAFuncUnits(InsnInput, IS->getUnits());
|
2016-01-15 05:17:04 +08:00
|
|
|
assert((i++ < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
return InsnInput;
|
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// Return the DFAInput for an instruction class input vector.
|
2015-11-22 23:20:19 +08:00
|
|
|
DFAInput DFAPacketizer::getInsnInput(const std::vector<unsigned> &InsnClass) {
|
|
|
|
return getDFAInsnInput(InsnClass);
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// Check if the resources occupied by a MCInstrDesc are available in the
|
|
|
|
// current state.
|
2011-12-07 01:34:16 +08:00
|
|
|
bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
|
2011-12-02 05:10:21 +08:00
|
|
|
unsigned InsnClass = MID->getSchedClass();
|
2015-11-22 04:00:45 +08:00
|
|
|
DFAInput InsnInput = getInsnInput(InsnClass);
|
|
|
|
UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
|
2011-12-02 05:10:21 +08:00
|
|
|
ReadTable(CurrentState);
|
2016-01-15 05:17:04 +08:00
|
|
|
return CachedTable.count(StateTrans) != 0;
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// Reserve the resources occupied by a MCInstrDesc and change the current
|
|
|
|
// state to reflect that change.
|
2011-12-07 01:34:16 +08:00
|
|
|
void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
|
2011-12-02 05:10:21 +08:00
|
|
|
unsigned InsnClass = MID->getSchedClass();
|
2015-11-22 04:00:45 +08:00
|
|
|
DFAInput InsnInput = getInsnInput(InsnClass);
|
|
|
|
UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
|
2011-12-02 05:10:21 +08:00
|
|
|
ReadTable(CurrentState);
|
|
|
|
assert(CachedTable.count(StateTrans) != 0);
|
|
|
|
CurrentState = CachedTable[StateTrans];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
// Check if the resources occupied by a machine instruction are available
|
|
|
|
// in the current state.
|
2016-02-28 03:09:00 +08:00
|
|
|
bool DFAPacketizer::canReserveResources(llvm::MachineInstr &MI) {
|
|
|
|
const llvm::MCInstrDesc &MID = MI.getDesc();
|
2011-12-02 05:10:21 +08:00
|
|
|
return canReserveResources(&MID);
|
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// Reserve the resources occupied by a machine instruction and change the
|
|
|
|
// current state to reflect that change.
|
2016-02-28 03:09:00 +08:00
|
|
|
void DFAPacketizer::reserveResources(llvm::MachineInstr &MI) {
|
|
|
|
const llvm::MCInstrDesc &MID = MI.getDesc();
|
2011-12-02 05:10:21 +08:00
|
|
|
reserveResources(&MID);
|
|
|
|
}
|
2012-02-16 02:55:14 +08:00
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
2012-05-02 05:28:30 +08:00
|
|
|
namespace llvm {
|
2016-01-15 05:17:04 +08:00
|
|
|
// This class extends ScheduleDAGInstrs and overrides the schedule method
|
|
|
|
// to build the dependence graph.
|
2012-02-16 02:55:14 +08:00
|
|
|
class DefaultVLIWScheduler : public ScheduleDAGInstrs {
|
2015-12-15 04:35:13 +08:00
|
|
|
private:
|
|
|
|
AliasAnalysis *AA;
|
2016-03-08 23:33:51 +08:00
|
|
|
/// Ordered list of DAG postprocessing steps.
|
|
|
|
std::vector<std::unique_ptr<ScheduleDAGMutation>> Mutations;
|
2012-02-16 02:55:14 +08:00
|
|
|
public:
|
2015-12-15 04:35:13 +08:00
|
|
|
DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI,
|
|
|
|
AliasAnalysis *AA);
|
2016-01-15 05:17:04 +08:00
|
|
|
// Actual scheduling work.
|
2014-03-07 17:26:03 +08:00
|
|
|
void schedule() override;
|
2016-03-08 23:33:51 +08:00
|
|
|
|
|
|
|
/// DefaultVLIWScheduler takes ownership of the Mutation object.
|
|
|
|
void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation) {
|
|
|
|
Mutations.push_back(std::move(Mutation));
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
void postprocessDAG();
|
2012-02-16 02:55:14 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2012-02-16 07:34:15 +08:00
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
2014-08-21 04:57:26 +08:00
|
|
|
DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
|
2015-12-15 04:35:13 +08:00
|
|
|
MachineLoopInfo &MLI,
|
|
|
|
AliasAnalysis *AA)
|
|
|
|
: ScheduleDAGInstrs(MF, &MLI), AA(AA) {
|
2012-05-02 05:28:30 +08:00
|
|
|
CanHandleTerminators = true;
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
2016-03-08 23:33:51 +08:00
|
|
|
/// Apply each ScheduleDAGMutation step in order.
|
|
|
|
void DefaultVLIWScheduler::postprocessDAG() {
|
|
|
|
for (auto &M : Mutations)
|
|
|
|
M->apply(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-08 07:00:49 +08:00
|
|
|
void DefaultVLIWScheduler::schedule() {
|
2012-02-16 02:55:14 +08:00
|
|
|
// Build the scheduling graph.
|
2015-12-15 04:35:13 +08:00
|
|
|
buildSchedGraph(AA);
|
2016-03-08 23:33:51 +08:00
|
|
|
postprocessDAG();
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
VLIWPacketizerList::VLIWPacketizerList(MachineFunction &mf,
|
|
|
|
MachineLoopInfo &mli, AliasAnalysis *aa)
|
|
|
|
: MF(mf), TII(mf.getSubtarget().getInstrInfo()), AA(aa) {
|
2014-10-09 09:59:35 +08:00
|
|
|
ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
|
2016-01-15 05:17:04 +08:00
|
|
|
VLIWScheduler = new DefaultVLIWScheduler(MF, mli, AA);
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
2012-02-16 02:55:14 +08:00
|
|
|
VLIWPacketizerList::~VLIWPacketizerList() {
|
2012-05-02 05:28:30 +08:00
|
|
|
if (VLIWScheduler)
|
|
|
|
delete VLIWScheduler;
|
|
|
|
if (ResourceTracker)
|
|
|
|
delete ResourceTracker;
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// End the current packet, bundle packet instructions and reset DFA state.
|
2016-02-28 03:09:00 +08:00
|
|
|
void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator MI) {
|
2012-02-16 02:55:14 +08:00
|
|
|
if (CurrentPacketMIs.size() > 1) {
|
2016-02-28 03:09:00 +08:00
|
|
|
MachineInstr &MIFirst = *CurrentPacketMIs.front();
|
|
|
|
finalizeBundle(*MBB, MIFirst.getIterator(), MI.getInstrIterator());
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
|
|
|
CurrentPacketMIs.clear();
|
|
|
|
ResourceTracker->clearResources();
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << "End packet\n");
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:17:04 +08:00
|
|
|
|
|
|
|
// Bundle machine instructions into packets.
|
2012-02-16 02:55:14 +08:00
|
|
|
void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator BeginItr,
|
|
|
|
MachineBasicBlock::iterator EndItr) {
|
2012-05-02 05:28:30 +08:00
|
|
|
assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
|
|
|
|
VLIWScheduler->startBlock(MBB);
|
2013-08-24 01:48:33 +08:00
|
|
|
VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
|
|
|
|
std::distance(BeginItr, EndItr));
|
2012-05-02 05:28:30 +08:00
|
|
|
VLIWScheduler->schedule();
|
|
|
|
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG({
|
|
|
|
dbgs() << "Scheduling DAG of the packetize region\n";
|
|
|
|
for (SUnit &SU : VLIWScheduler->SUnits)
|
|
|
|
SU.dumpAll(VLIWScheduler);
|
|
|
|
});
|
|
|
|
|
2012-05-02 05:28:30 +08:00
|
|
|
// Generate MI -> SU map.
|
|
|
|
MIToSUnit.clear();
|
2016-01-15 05:17:04 +08:00
|
|
|
for (SUnit &SU : VLIWScheduler->SUnits)
|
|
|
|
MIToSUnit[SU.getInstr()] = &SU;
|
2012-02-16 02:55:14 +08:00
|
|
|
|
|
|
|
// The main packetizer loop.
|
|
|
|
for (; BeginItr != EndItr; ++BeginItr) {
|
2016-02-28 03:09:00 +08:00
|
|
|
MachineInstr &MI = *BeginItr;
|
2016-01-15 05:17:04 +08:00
|
|
|
initPacketizerState();
|
2012-02-16 02:55:14 +08:00
|
|
|
|
|
|
|
// End the current packet if needed.
|
2016-01-15 05:17:04 +08:00
|
|
|
if (isSoloInstruction(MI)) {
|
2012-02-16 02:55:14 +08:00
|
|
|
endPacket(MBB, MI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-02 05:28:30 +08:00
|
|
|
// Ignore pseudo instructions.
|
2016-01-15 05:17:04 +08:00
|
|
|
if (ignorePseudoInstruction(MI, MBB))
|
2012-05-02 05:28:30 +08:00
|
|
|
continue;
|
|
|
|
|
2016-02-28 03:09:00 +08:00
|
|
|
SUnit *SUI = MIToSUnit[&MI];
|
2012-02-16 02:55:14 +08:00
|
|
|
assert(SUI && "Missing SUnit Info!");
|
|
|
|
|
|
|
|
// Ask DFA if machine resource is available for MI.
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << "Checking resources for adding MI to packet " << MI);
|
|
|
|
|
2012-02-16 02:55:14 +08:00
|
|
|
bool ResourceAvail = ResourceTracker->canReserveResources(MI);
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG({
|
|
|
|
if (ResourceAvail)
|
|
|
|
dbgs() << " Resources are available for adding MI to packet\n";
|
|
|
|
else
|
|
|
|
dbgs() << " Resources NOT available\n";
|
|
|
|
});
|
2015-12-17 00:38:16 +08:00
|
|
|
if (ResourceAvail && shouldAddToPacket(MI)) {
|
2012-02-16 02:55:14 +08:00
|
|
|
// Dependency check for MI with instructions in CurrentPacketMIs.
|
2016-01-15 05:17:04 +08:00
|
|
|
for (auto MJ : CurrentPacketMIs) {
|
2012-05-02 05:28:30 +08:00
|
|
|
SUnit *SUJ = MIToSUnit[MJ];
|
2012-02-16 02:55:14 +08:00
|
|
|
assert(SUJ && "Missing SUnit Info!");
|
|
|
|
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << " Checking against MJ " << *MJ);
|
2012-02-16 02:55:14 +08:00
|
|
|
// Is it legal to packetize SUI and SUJ together.
|
2016-01-15 05:17:04 +08:00
|
|
|
if (!isLegalToPacketizeTogether(SUI, SUJ)) {
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << " Not legal to add MI, try to prune\n");
|
2012-02-16 02:55:14 +08:00
|
|
|
// Allow packetization if dependency can be pruned.
|
2016-01-15 05:17:04 +08:00
|
|
|
if (!isLegalToPruneDependencies(SUI, SUJ)) {
|
2012-02-16 02:55:14 +08:00
|
|
|
// End the packet if dependency cannot be pruned.
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << " Could not prune dependencies for adding MI\n");
|
2012-02-16 02:55:14 +08:00
|
|
|
endPacket(MBB, MI);
|
|
|
|
break;
|
2016-01-15 05:17:04 +08:00
|
|
|
}
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << " Pruned dependence for adding MI\n");
|
2016-01-15 05:17:04 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-16 02:55:14 +08:00
|
|
|
} else {
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(if (ResourceAvail)
|
|
|
|
dbgs() << "Resources are available, but instruction should not be "
|
|
|
|
"added to packet\n " << MI);
|
2015-12-17 00:38:16 +08:00
|
|
|
// End the packet if resource is not available, or if the instruction
|
|
|
|
// shoud not be added to the current packet.
|
2012-02-16 02:55:14 +08:00
|
|
|
endPacket(MBB, MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add MI to the current packet.
|
2016-07-15 03:04:26 +08:00
|
|
|
DEBUG(dbgs() << "* Adding MI to packet " << MI << '\n');
|
2016-01-15 05:17:04 +08:00
|
|
|
BeginItr = addToPacket(MI);
|
|
|
|
} // For all instructions in the packetization range.
|
2012-02-16 02:55:14 +08:00
|
|
|
|
|
|
|
// End any packet left behind.
|
|
|
|
endPacket(MBB, EndItr);
|
2012-05-02 05:28:30 +08:00
|
|
|
VLIWScheduler->exitRegion();
|
|
|
|
VLIWScheduler->finishBlock();
|
2012-02-16 02:55:14 +08:00
|
|
|
}
|
2016-03-08 23:33:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Add a DAG mutation object to the ordered list.
|
|
|
|
void VLIWPacketizerList::addMutation(
|
|
|
|
std::unique_ptr<ScheduleDAGMutation> Mutation) {
|
|
|
|
VLIWScheduler->addMutation(std::move(Mutation));
|
|
|
|
}
|