2016-01-27 02:48:36 +08:00
|
|
|
//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class parses the Schedule.td file and produces an API that can be used
|
|
|
|
// to reason about whether an instruction can be added to a packet on a VLIW
|
|
|
|
// architecture. The class internally generates a deterministic finite
|
|
|
|
// automaton (DFA) that models all possible mappings of machine instructions
|
|
|
|
// to functional units as instructions are added to a packet.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
#define DEBUG_TYPE "dfa-emitter"
|
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
#include "CodeGenTarget.h"
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2016-12-01 01:48:10 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2015-11-22 04:00:45 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include "llvm/TableGen/TableGenBackend.h"
|
2015-11-22 04:00:45 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-12-01 01:48:10 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2012-06-11 23:37:55 +08:00
|
|
|
#include <map>
|
2016-12-01 01:48:10 +08:00
|
|
|
#include <set>
|
2012-06-11 23:37:55 +08:00
|
|
|
#include <string>
|
2016-12-01 01:48:10 +08:00
|
|
|
#include <vector>
|
2016-01-27 02:48:36 +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
|
|
|
|
|
|
|
|
// DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
|
|
|
|
// This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
|
|
|
|
//
|
|
|
|
// e.g. terms x resource bit combinations that fit in uint32_t:
|
|
|
|
// 4 terms x 8 bits = 32 bits
|
|
|
|
// 3 terms x 10 bits = 30 bits
|
|
|
|
// 2 terms x 16 bits = 32 bits
|
|
|
|
//
|
|
|
|
// e.g. terms x resource bit combinations that fit in uint64_t:
|
|
|
|
// 8 terms x 8 bits = 64 bits
|
|
|
|
// 7 terms x 9 bits = 63 bits
|
|
|
|
// 6 terms x 10 bits = 60 bits
|
|
|
|
// 5 terms x 12 bits = 60 bits
|
|
|
|
// 4 terms x 16 bits = 64 bits <--- current
|
|
|
|
// 3 terms x 21 bits = 63 bits
|
|
|
|
// 2 terms x 32 bits = 64 bits
|
|
|
|
//
|
|
|
|
#define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
|
|
|
|
#define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
|
|
|
|
|
|
|
|
typedef uint64_t DFAInput;
|
|
|
|
typedef int64_t DFAStateInput;
|
|
|
|
#define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
|
|
|
|
|
|
|
|
namespace {
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2015-11-22 23:20:19 +08:00
|
|
|
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-12-01 01:48:10 +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;
|
|
|
|
}
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2016-01-27 02:48:36 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2015-11-22 23:20:19 +08:00
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2015-11-22 06:46:52 +08:00
|
|
|
#ifndef NDEBUG
|
2015-11-22 04:00:45 +08:00
|
|
|
// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
|
|
|
|
//
|
|
|
|
// dbgsInsnClass - When debugging, print instruction class stages.
|
|
|
|
//
|
|
|
|
void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
|
|
|
|
//
|
|
|
|
// dbgsStateInfo - When debugging, print the set of state info.
|
|
|
|
//
|
|
|
|
void dbgsStateInfo(const std::set<unsigned> &stateInfo);
|
|
|
|
//
|
|
|
|
// dbgsIndent - When debugging, indent by the specified amount.
|
|
|
|
//
|
|
|
|
void dbgsIndent(unsigned indent);
|
2015-11-22 06:46:52 +08:00
|
|
|
#endif
|
2015-11-22 04:00:45 +08:00
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
//
|
|
|
|
// class DFAPacketizerEmitter: class that generates and prints out the DFA
|
|
|
|
// for resource tracking.
|
|
|
|
//
|
|
|
|
namespace {
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
class DFAPacketizerEmitter {
|
|
|
|
private:
|
|
|
|
std::string TargetName;
|
|
|
|
//
|
|
|
|
// allInsnClasses is the set of all possible resources consumed by an
|
|
|
|
// InstrStage.
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
std::vector<std::vector<unsigned>> allInsnClasses;
|
2012-06-11 23:37:55 +08:00
|
|
|
RecordKeeper &Records;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DFAPacketizerEmitter(RecordKeeper &R);
|
|
|
|
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// collectAllFuncUnits - Construct a map of function unit names to bits.
|
|
|
|
//
|
|
|
|
int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
int &maxResources,
|
|
|
|
raw_ostream &OS);
|
|
|
|
|
|
|
|
//
|
|
|
|
// collectAllComboFuncs - Construct a map from a combo function unit bit to
|
|
|
|
// the bits of all included functional units.
|
|
|
|
//
|
|
|
|
int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap,
|
|
|
|
raw_ostream &OS);
|
|
|
|
|
|
|
|
//
|
|
|
|
// collectOneInsnClass - Populate allInsnClasses with one instruction class.
|
|
|
|
//
|
|
|
|
int collectOneInsnClass(const std::string &ProcName,
|
|
|
|
std::vector<Record*> &ProcItinList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
Record *ItinData,
|
|
|
|
raw_ostream &OS);
|
|
|
|
|
|
|
|
//
|
|
|
|
// collectAllInsnClasses - Populate allInsnClasses which is a set of units
|
2012-06-11 23:37:55 +08:00
|
|
|
// used in each stage.
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
int collectAllInsnClasses(const std::string &ProcName,
|
|
|
|
std::vector<Record*> &ProcItinList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
std::vector<Record*> &ItinDataList,
|
|
|
|
int &maxStages,
|
|
|
|
raw_ostream &OS);
|
2012-06-11 23:37:55 +08:00
|
|
|
|
|
|
|
void run(raw_ostream &OS);
|
|
|
|
};
|
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
// State represents the usage of machine resources if the packet contains
|
|
|
|
// a set of instruction classes.
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Specifically, currentState is a set of bit-masks.
|
2011-12-02 05:10:21 +08:00
|
|
|
// The nth bit in a bit-mask indicates whether the nth resource is being used
|
|
|
|
// by this state. The set of bit-masks in a state represent the different
|
|
|
|
// possible outcomes of transitioning to this state.
|
2011-12-07 01:34:11 +08:00
|
|
|
// For example: consider a two resource architecture: resource L and resource M
|
|
|
|
// with three instruction classes: L, M, and L_or_M.
|
2011-12-02 05:10:21 +08:00
|
|
|
// From the initial state (currentState = 0x00), if we add instruction class
|
|
|
|
// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
|
|
|
|
// represents the possible resource states that can result from adding a L_or_M
|
|
|
|
// instruction
|
|
|
|
//
|
|
|
|
// Another way of thinking about this transition is we are mapping a NDFA with
|
2011-12-07 01:34:11 +08:00
|
|
|
// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2012-09-08 05:35:43 +08:00
|
|
|
// A State instance also contains a collection of transitions from that state:
|
|
|
|
// a map from inputs to new states.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
class State {
|
|
|
|
public:
|
|
|
|
static int currentStateNum;
|
2014-04-22 06:35:11 +08:00
|
|
|
// stateNum is the only member used for equality/ordering, all other members
|
|
|
|
// can be mutated even in const State objects.
|
|
|
|
const int stateNum;
|
|
|
|
mutable bool isInitial;
|
|
|
|
mutable std::set<unsigned> stateInfo;
|
2015-11-22 04:00:45 +08:00
|
|
|
typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
|
2014-04-22 06:35:11 +08:00
|
|
|
mutable TransitionMap Transitions;
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
State();
|
|
|
|
|
2012-09-08 05:35:43 +08:00
|
|
|
bool operator<(const State &s) const {
|
|
|
|
return stateNum < s.stateNum;
|
|
|
|
}
|
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
|
|
|
|
// may be a valid transition from this state i.e., can an instruction of type
|
|
|
|
// InsnClass be added to the packet represented by this state.
|
|
|
|
//
|
|
|
|
// Note that for multiple stages, this quick check does not take into account
|
|
|
|
// any possible resource competition between the stages themselves. That is
|
|
|
|
// enforced in AddInsnClassStages which checks the cross product of all
|
|
|
|
// stages for resource availability (which is a more involved check).
|
|
|
|
//
|
|
|
|
bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
//
|
|
|
|
// AddInsnClass - Return all combinations of resource reservation
|
|
|
|
// which are possible from this state (PossibleStates).
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
// PossibleStates is the set of valid resource states that ensue from valid
|
2011-12-07 01:34:11 +08:00
|
|
|
// transitions.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
void AddInsnClass(std::vector<unsigned> &InsnClass,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap,
|
|
|
|
std::set<unsigned> &PossibleStates) const;
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2012-06-28 03:38:29 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// AddInsnClassStages - Return all combinations of resource reservation
|
|
|
|
// resulting from the cross product of all stages for this InsnClass
|
2012-06-28 03:38:29 +08:00
|
|
|
// which are possible from this state (PossibleStates).
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
void AddInsnClassStages(std::vector<unsigned> &InsnClass,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap,
|
|
|
|
unsigned chkstage, unsigned numstages,
|
|
|
|
unsigned prevState, unsigned origState,
|
|
|
|
DenseSet<unsigned> &VisitedResourceStates,
|
|
|
|
std::set<unsigned> &PossibleStates) const;
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
//
|
2012-09-08 05:35:43 +08:00
|
|
|
// addTransition - Add a transition from this state given the input InsnClass
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2012-09-08 05:35:43 +08:00
|
|
|
//
|
|
|
|
// hasTransition - Returns true if there is a transition from this state
|
|
|
|
// given the input InsnClass
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
bool hasTransition(std::vector<unsigned> InsnClass) const;
|
2011-12-02 05:10:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// class DFA: deterministic finite automaton for processor resource tracking.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
class DFA {
|
|
|
|
public:
|
2016-12-01 01:48:10 +08:00
|
|
|
DFA() = default;
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Set of states. Need to keep this sorted to emit the transition table.
|
2014-04-22 06:35:11 +08:00
|
|
|
typedef std::set<State> StateSet;
|
2012-09-08 05:35:43 +08:00
|
|
|
StateSet states;
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2016-12-01 01:48:10 +08:00
|
|
|
State *currentState = nullptr;
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Modify the DFA.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2014-04-22 06:35:11 +08:00
|
|
|
const State &newState();
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// writeTable: Print out a table representing the DFA.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
|
|
|
|
int numInsnClasses = 0,
|
|
|
|
int maxResources = 0, int numCombos = 0, int maxStages = 0);
|
2011-12-02 05:10:21 +08:00
|
|
|
};
|
2016-12-01 01:48:10 +08:00
|
|
|
|
2016-01-27 02:48:36 +08:00
|
|
|
} // end anonymous namespace
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2015-11-22 06:19:50 +08:00
|
|
|
#ifndef NDEBUG
|
2015-11-22 04:00:45 +08:00
|
|
|
// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
|
|
|
|
//
|
|
|
|
// dbgsInsnClass - When debugging, print instruction class stages.
|
|
|
|
//
|
|
|
|
void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "InsnClass: ");
|
2015-11-22 04:00:45 +08:00
|
|
|
for (unsigned i = 0; i < InsnClass.size(); ++i) {
|
|
|
|
if (i > 0) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ", ");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(InsnClass[i]));
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
DFAInput InsnInput = getDFAInsnInput(InsnClass);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " (input: 0x" << Twine::utohexstr(InsnInput) << ")");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// dbgsStateInfo - When debugging, print the set of state info.
|
|
|
|
//
|
|
|
|
void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "StateInfo: ");
|
2015-11-22 04:00:45 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (std::set<unsigned>::iterator SI = stateInfo.begin();
|
|
|
|
SI != stateInfo.end(); ++SI, ++i) {
|
|
|
|
unsigned thisState = *SI;
|
|
|
|
if (i > 0) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << ", ");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(thisState));
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// dbgsIndent - When debugging, indent by the specified amount.
|
|
|
|
//
|
|
|
|
void dbgsIndent(unsigned indent) {
|
|
|
|
for (unsigned i = 0; i < indent; ++i) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " ");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-27 02:48:36 +08:00
|
|
|
#endif // NDEBUG
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
2012-09-08 05:35:43 +08:00
|
|
|
// Constructors and destructors for State and DFA
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
State::State() :
|
|
|
|
stateNum(currentStateNum++), isInitial(false) {}
|
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
//
|
2012-09-08 05:35:43 +08:00
|
|
|
// addTransition - Add a transition from this state given the input InsnClass
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
|
|
|
|
const {
|
2012-09-08 05:35:43 +08:00
|
|
|
assert(!Transitions.count(InsnClass) &&
|
|
|
|
"Cannot have multiple transitions for the same input");
|
|
|
|
Transitions[InsnClass] = To;
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
2012-09-08 05:35:43 +08:00
|
|
|
//
|
|
|
|
// hasTransition - Returns true if there is a transition from this state
|
|
|
|
// given the input InsnClass
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
bool State::hasTransition(std::vector<unsigned> InsnClass) const {
|
2012-09-08 05:35:43 +08:00
|
|
|
return Transitions.count(InsnClass) > 0;
|
2012-06-28 03:38:29 +08:00
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
2012-06-28 03:38:29 +08:00
|
|
|
// AddInsnClass - Return all combinations of resource reservation
|
|
|
|
// which are possible from this state (PossibleStates).
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// PossibleStates is the set of valid resource states that ensue from valid
|
|
|
|
// transitions.
|
|
|
|
//
|
|
|
|
void State::AddInsnClass(std::vector<unsigned> &InsnClass,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap,
|
|
|
|
std::set<unsigned> &PossibleStates) const {
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Iterate over all resource states in currentState.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
unsigned numstages = InsnClass.size();
|
|
|
|
assert((numstages > 0) && "InsnClass has no stages");
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
for (std::set<unsigned>::iterator SI = stateInfo.begin();
|
|
|
|
SI != stateInfo.end(); ++SI) {
|
|
|
|
unsigned thisState = *SI;
|
|
|
|
|
2015-11-22 01:38:33 +08:00
|
|
|
DenseSet<unsigned> VisitedResourceStates;
|
2015-11-22 04:00:45 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " thisState: 0x" << Twine::utohexstr(thisState)
|
|
|
|
<< "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
AddInsnClassStages(InsnClass, ComboBitToBitsMap,
|
|
|
|
numstages - 1, numstages,
|
|
|
|
thisState, thisState,
|
|
|
|
VisitedResourceStates, PossibleStates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap,
|
|
|
|
unsigned chkstage, unsigned numstages,
|
|
|
|
unsigned prevState, unsigned origState,
|
|
|
|
DenseSet<unsigned> &VisitedResourceStates,
|
|
|
|
std::set<unsigned> &PossibleStates) const {
|
|
|
|
assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
|
|
|
|
unsigned thisStage = InsnClass[chkstage];
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgsIndent((1 + numstages - chkstage) << 1);
|
|
|
|
dbgs() << "AddInsnClassStages " << chkstage << " (0x"
|
2017-12-29 00:58:54 +08:00
|
|
|
<< Twine::utohexstr(thisStage) << ") from ";
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgsInsnClass(InsnClass);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Iterate over all possible resources used in thisStage.
|
|
|
|
// For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
|
|
|
|
//
|
|
|
|
for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
|
|
|
|
unsigned resourceMask = (0x1 << j);
|
|
|
|
if (resourceMask & thisStage) {
|
|
|
|
unsigned combo = ComboBitToBitsMap[resourceMask];
|
|
|
|
if (combo && ((~prevState & combo) != combo)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tSkipped Add 0x" << Twine::utohexstr(prevState)
|
|
|
|
<< " - combo op 0x" << Twine::utohexstr(resourceMask)
|
|
|
|
<< " (0x" << Twine::utohexstr(combo)
|
|
|
|
<< ") cannot be scheduled\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// For each possible resource used in thisStage, generate the
|
|
|
|
// resource state if that resource was used.
|
|
|
|
//
|
|
|
|
unsigned ResultingResourceState = prevState | resourceMask | combo;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgsIndent((2 + numstages - chkstage) << 1);
|
2017-12-29 00:58:54 +08:00
|
|
|
dbgs() << "0x" << Twine::utohexstr(prevState) << " | 0x"
|
|
|
|
<< Twine::utohexstr(resourceMask);
|
2015-11-22 06:46:52 +08:00
|
|
|
if (combo)
|
2017-12-29 00:58:54 +08:00
|
|
|
dbgs() << " | 0x" << Twine::utohexstr(combo);
|
|
|
|
dbgs() << " = 0x" << Twine::utohexstr(ResultingResourceState) << " ";
|
2015-11-22 06:46:52 +08:00
|
|
|
});
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// If this is the final stage for this class
|
|
|
|
//
|
|
|
|
if (chkstage == 0) {
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
// Check if the resulting resource state can be accommodated in this
|
2011-12-07 01:34:11 +08:00
|
|
|
// packet.
|
2015-11-22 04:00:45 +08:00
|
|
|
// We compute resource OR prevState (originally started as origState).
|
|
|
|
// If the result of the OR is different than origState, it implies
|
2011-12-02 05:10:21 +08:00
|
|
|
// that there is at least one resource that can be used to schedule
|
2015-11-22 04:00:45 +08:00
|
|
|
// thisStage in the current packet.
|
2011-12-02 05:10:21 +08:00
|
|
|
// Insert ResultingResourceState into PossibleStates only if we haven't
|
2011-12-07 01:34:11 +08:00
|
|
|
// processed ResultingResourceState before.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
if (ResultingResourceState != prevState) {
|
|
|
|
if (VisitedResourceStates.count(ResultingResourceState) == 0) {
|
|
|
|
VisitedResourceStates.insert(ResultingResourceState);
|
|
|
|
PossibleStates.insert(ResultingResourceState);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs()
|
|
|
|
<< "\tResultingResourceState: 0x"
|
|
|
|
<< Twine::utohexstr(ResultingResourceState) << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs()
|
|
|
|
<< "\tSkipped Add - no final resources available\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// If the current resource can be accommodated, check the next
|
|
|
|
// stage in InsnClass for available resources.
|
|
|
|
//
|
|
|
|
if (ResultingResourceState != prevState) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
AddInsnClassStages(InsnClass, ComboBitToBitsMap,
|
|
|
|
chkstage - 1, numstages,
|
|
|
|
ResultingResourceState, origState,
|
|
|
|
VisitedResourceStates, PossibleStates);
|
|
|
|
} else {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 03:38:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
|
|
|
|
// may be a valid transition from this state i.e., can an instruction of type
|
|
|
|
// InsnClass be added to the packet represented by this state.
|
2012-06-28 03:38:29 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// Note that this routine is performing conservative checks that can be
|
|
|
|
// quickly executed acting as a filter before calling AddInsnClassStages.
|
|
|
|
// Any cases allowed through here will be caught later in AddInsnClassStages
|
|
|
|
// which performs the more expensive exact check.
|
|
|
|
//
|
|
|
|
bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
|
2012-06-28 15:47:50 +08:00
|
|
|
for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
|
2012-06-28 03:38:29 +08:00
|
|
|
SI != stateInfo.end(); ++SI) {
|
2015-11-22 04:00:45 +08:00
|
|
|
// Check to see if all required resources are available.
|
|
|
|
bool available = true;
|
|
|
|
|
|
|
|
// Inspect each stage independently.
|
|
|
|
// note: This is a conservative check as we aren't checking for
|
|
|
|
// possible resource competition between the stages themselves
|
|
|
|
// The full cross product is examined later in AddInsnClass.
|
|
|
|
for (unsigned i = 0; i < InsnClass.size(); ++i) {
|
|
|
|
unsigned resources = *SI;
|
|
|
|
if ((~resources & InsnClass[i]) == 0) {
|
|
|
|
available = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Make sure _all_ resources for a combo function are available.
|
|
|
|
// note: This is a quick conservative check as it won't catch an
|
|
|
|
// unscheduleable combo if this stage is an OR expression
|
|
|
|
// containing a combo.
|
|
|
|
// These cases are caught later in AddInsnClass.
|
|
|
|
unsigned combo = ComboBitToBitsMap[InsnClass[i]];
|
|
|
|
if (combo && ((~resources & combo) != combo)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x"
|
|
|
|
<< Twine::utohexstr(resources) << " - combo op 0x"
|
|
|
|
<< Twine::utohexstr(InsnClass[i]) << " (0x"
|
|
|
|
<< Twine::utohexstr(combo)
|
|
|
|
<< ") cannot be scheduled\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
available = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (available) {
|
2012-06-28 03:38:29 +08:00
|
|
|
return true;
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2012-06-28 03:38:29 +08:00
|
|
|
}
|
|
|
|
return false;
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
2014-04-22 06:35:11 +08:00
|
|
|
const State &DFA::newState() {
|
2014-04-22 06:46:09 +08:00
|
|
|
auto IterPair = states.insert(State());
|
2014-04-22 06:35:11 +08:00
|
|
|
assert(IterPair.second && "State already exists");
|
|
|
|
return *IterPair.first;
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int State::currentStateNum = 0;
|
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
|
2016-12-01 01:48:10 +08:00
|
|
|
TargetName(CodeGenTarget(R).getName()), Records(R) {}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// writeTableAndAPI - Print out a table representing the DFA and the
|
2011-12-07 01:34:11 +08:00
|
|
|
// associated API to create a DFA packetizer.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
// Format:
|
|
|
|
// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
|
2011-12-07 01:34:11 +08:00
|
|
|
// transitions.
|
2011-12-02 05:10:21 +08:00
|
|
|
// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
|
2011-12-07 01:34:11 +08:00
|
|
|
// the ith state.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
|
|
|
|
int numInsnClasses,
|
|
|
|
int maxResources, int numCombos, int maxStages) {
|
|
|
|
unsigned numStates = states.size();
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
|
|
|
|
"----------------------\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "writeTableAndAPI\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "Total states: " << numStates << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
OS << "namespace llvm {\n";
|
|
|
|
|
|
|
|
OS << "\n// Input format:\n";
|
|
|
|
OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
|
|
|
|
<< "\t// maximum AND'ed resource terms\n";
|
|
|
|
OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
|
|
|
|
<< "\t// maximum resource bits in one term\n";
|
|
|
|
|
|
|
|
OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
|
|
|
|
<< "pairs of <Input, NextState> for all valid\n";
|
|
|
|
OS << "// transitions.\n";
|
|
|
|
OS << "// " << numStates << "\tstates\n";
|
|
|
|
OS << "// " << numInsnClasses << "\tinstruction classes\n";
|
|
|
|
OS << "// " << maxResources << "\tresources max\n";
|
|
|
|
OS << "// " << numCombos << "\tcombo resources\n";
|
|
|
|
OS << "// " << maxStages << "\tstages max\n";
|
|
|
|
OS << "const " << DFA_TBLTYPE << " "
|
|
|
|
<< TargetName << "DFAStateInputTable[][2] = {\n";
|
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
// This table provides a map to the beginning of the transitions for State s
|
2011-12-07 01:34:11 +08:00
|
|
|
// in DFAStateInputTable.
|
2015-11-22 04:00:45 +08:00
|
|
|
std::vector<int> StateEntry(numStates+1);
|
|
|
|
static const std::string SentinelEntry = "{-1, -1}";
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
// Tracks the total valid transitions encountered so far. It is used
|
2011-12-07 01:34:11 +08:00
|
|
|
// to construct the StateEntry table.
|
2011-12-02 05:10:21 +08:00
|
|
|
int ValidTransitions = 0;
|
2015-11-22 04:00:45 +08:00
|
|
|
DFA::StateSet::iterator SI = states.begin();
|
|
|
|
for (unsigned i = 0; i < numStates; ++i, ++SI) {
|
2014-04-22 06:35:11 +08:00
|
|
|
assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
|
2011-12-02 05:10:21 +08:00
|
|
|
StateEntry[i] = ValidTransitions;
|
2012-09-08 05:35:43 +08:00
|
|
|
for (State::TransitionMap::iterator
|
2014-04-22 06:35:11 +08:00
|
|
|
II = SI->Transitions.begin(), IE = SI->Transitions.end();
|
2012-09-08 05:35:43 +08:00
|
|
|
II != IE; ++II) {
|
2017-12-29 00:58:54 +08:00
|
|
|
OS << "{0x" << Twine::utohexstr(getDFAInsnInput(II->first)) << ", "
|
|
|
|
<< II->second->stateNum << "},\t";
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
2014-04-22 06:35:11 +08:00
|
|
|
ValidTransitions += SI->Transitions.size();
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// If there are no valid transitions from this stage, we need a sentinel
|
|
|
|
// transition.
|
2012-02-04 05:08:25 +08:00
|
|
|
if (ValidTransitions == StateEntry[i]) {
|
2015-11-22 04:00:45 +08:00
|
|
|
OS << SentinelEntry << ",\t";
|
2012-02-04 05:08:25 +08:00
|
|
|
++ValidTransitions;
|
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
OS << " // state " << i << ": " << StateEntry[i];
|
|
|
|
if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
|
|
|
|
OS << "-" << (ValidTransitions-1);
|
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2012-12-11 06:45:57 +08:00
|
|
|
|
|
|
|
// Print out a sentinel entry at the end of the StateInputTable. This is
|
|
|
|
// needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
|
2015-11-22 04:00:45 +08:00
|
|
|
OS << SentinelEntry << "\t";
|
|
|
|
OS << " // state " << numStates << ": " << ValidTransitions;
|
|
|
|
OS << "\n";
|
|
|
|
|
2011-12-02 05:10:21 +08:00
|
|
|
OS << "};\n\n";
|
2015-11-22 04:00:45 +08:00
|
|
|
OS << "// " << TargetName << "DFAStateEntryTable[i] = "
|
|
|
|
<< "Index of the first entry in DFAStateInputTable for\n";
|
|
|
|
OS << "// "
|
|
|
|
<< "the ith state.\n";
|
|
|
|
OS << "// " << numStates << " states\n";
|
2011-12-02 05:10:21 +08:00
|
|
|
OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
|
|
|
|
|
|
|
|
// Multiply i by 2 since each entry in DFAStateInputTable is a set of
|
2011-12-07 01:34:11 +08:00
|
|
|
// two numbers.
|
2015-11-22 04:00:45 +08:00
|
|
|
unsigned lastState = 0;
|
|
|
|
for (unsigned i = 0; i < numStates; ++i) {
|
|
|
|
if (i && ((i % 10) == 0)) {
|
|
|
|
lastState = i-1;
|
|
|
|
OS << " // states " << (i-10) << ":" << lastState << "\n";
|
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
OS << StateEntry[i] << ", ";
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2012-12-11 06:45:57 +08:00
|
|
|
// Print out the index to the sentinel entry in StateInputTable
|
|
|
|
OS << ValidTransitions << ", ";
|
2015-11-22 04:00:45 +08:00
|
|
|
OS << " // states " << (lastState+1) << ":" << numStates << "\n";
|
2012-12-11 06:45:57 +08:00
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
OS << "};\n";
|
2011-12-02 05:10:21 +08:00
|
|
|
OS << "} // namespace\n";
|
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Emit DFA Packetizer tables if the target is a VLIW machine.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
|
|
|
|
OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
|
|
|
|
OS << "namespace llvm {\n";
|
2011-12-07 01:34:16 +08:00
|
|
|
OS << "DFAPacketizer *" << SubTargetClassName << "::"
|
2011-12-02 05:10:21 +08:00
|
|
|
<< "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
|
|
|
|
<< " return new DFAPacketizer(IID, " << TargetName
|
|
|
|
<< "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
|
|
|
|
OS << "} // End llvm namespace \n";
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// collectAllFuncUnits - Construct a map of function unit names to bits.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
int DFAPacketizerEmitter::collectAllFuncUnits(
|
|
|
|
std::vector<Record*> &ProcItinList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
int &maxFUs,
|
|
|
|
raw_ostream &OS) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
|
|
|
|
"----------------------\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
|
|
|
|
LLVM_DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
int totalFUs = 0;
|
2011-12-02 05:10:21 +08:00
|
|
|
// Parse functional units for all the itineraries.
|
|
|
|
for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
|
|
|
|
Record *Proc = ProcItinList[i];
|
|
|
|
std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " FU:" << i << " (" << FUs.size() << " FUs) "
|
|
|
|
<< Proc->getName());
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
// Convert macros to bits for each stage.
|
|
|
|
unsigned numFUs = FUs.size();
|
|
|
|
for (unsigned j = 0; j < numFUs; ++j) {
|
|
|
|
assert ((j < DFA_MAX_RESOURCES) &&
|
|
|
|
"Exceeded maximum number of representable resources");
|
|
|
|
unsigned FuncResources = (unsigned) (1U << j);
|
|
|
|
FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << FUs[j]->getName() << ":0x"
|
|
|
|
<< Twine::utohexstr(FuncResources));
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
if (((int) numFUs) > maxFUs) {
|
|
|
|
maxFUs = numFUs;
|
|
|
|
}
|
|
|
|
totalFUs += numFUs;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
|
|
|
return totalFUs;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// collectAllComboFuncs - Construct a map from a combo function unit bit to
|
|
|
|
// the bits of all included functional units.
|
|
|
|
//
|
|
|
|
int DFAPacketizerEmitter::collectAllComboFuncs(
|
|
|
|
std::vector<Record*> &ComboFuncList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
std::map<unsigned, unsigned> &ComboBitToBitsMap,
|
|
|
|
raw_ostream &OS) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
|
|
|
|
"----------------------\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "collectAllComboFuncs");
|
|
|
|
LLVM_DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
int numCombos = 0;
|
|
|
|
for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
|
|
|
|
Record *Func = ComboFuncList[i];
|
|
|
|
std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " CFD:" << i << " (" << FUs.size() << " combo FUs) "
|
|
|
|
<< Func->getName() << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Convert macros to bits for each stage.
|
2015-11-22 04:00:45 +08:00
|
|
|
for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
|
|
|
|
assert ((j < DFA_MAX_RESOURCES) &&
|
|
|
|
"Exceeded maximum number of DFA resources");
|
|
|
|
Record *FuncData = FUs[j];
|
|
|
|
Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
|
|
|
|
const std::vector<Record*> &FuncList =
|
|
|
|
FuncData->getValueAsListOfDefs("FuncList");
|
2016-06-13 01:30:47 +08:00
|
|
|
const std::string &ComboFuncName = ComboFunc->getName();
|
2015-11-22 04:00:45 +08:00
|
|
|
unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
|
|
|
|
unsigned ComboResources = ComboBit;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " combo: " << ComboFuncName << ":0x"
|
|
|
|
<< Twine::utohexstr(ComboResources) << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
|
|
|
|
std::string FuncName = FuncList[k]->getName();
|
|
|
|
unsigned FuncResources = FUNameToBitsMap[FuncName];
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << FuncName << ":0x"
|
|
|
|
<< Twine::utohexstr(FuncResources) << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
ComboResources |= FuncResources;
|
|
|
|
}
|
|
|
|
ComboBitToBitsMap[ComboBit] = ComboResources;
|
|
|
|
numCombos++;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
|
|
|
|
<< Twine::utohexstr(ComboBit) << " = 0x"
|
|
|
|
<< Twine::utohexstr(ComboResources) << "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
2015-11-22 04:00:45 +08:00
|
|
|
return numCombos;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// collectOneInsnClass - Populate allInsnClasses with one instruction class
|
|
|
|
//
|
|
|
|
int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
|
|
|
|
std::vector<Record*> &ProcItinList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
Record *ItinData,
|
|
|
|
raw_ostream &OS) {
|
2011-12-02 05:10:21 +08:00
|
|
|
const std::vector<Record*> &StageList =
|
|
|
|
ItinData->getValueAsListOfDefs("Stages");
|
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// The number of stages.
|
2015-11-22 04:00:45 +08:00
|
|
|
unsigned NStages = StageList.size();
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName()
|
|
|
|
<< "\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
std::vector<unsigned> UnitBits;
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Compute the bitwise or of each unit used in this stage.
|
2011-12-02 05:10:21 +08:00
|
|
|
for (unsigned i = 0; i < NStages; ++i) {
|
|
|
|
const Record *Stage = StageList[i];
|
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Get unit list.
|
2011-12-02 05:10:21 +08:00
|
|
|
const std::vector<Record*> &UnitList =
|
|
|
|
Stage->getValueAsListOfDefs("Units");
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " stage:" << i << " [" << UnitList.size()
|
|
|
|
<< " units]:");
|
2015-11-22 04:00:45 +08:00
|
|
|
unsigned dbglen = 26; // cursor after stage dbgs
|
|
|
|
|
|
|
|
// Compute the bitwise or of each unit used in this stage.
|
|
|
|
unsigned UnitBitValue = 0;
|
2011-12-02 05:10:21 +08:00
|
|
|
for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
|
2011-12-07 01:34:11 +08:00
|
|
|
// Conduct bitwise or.
|
2011-12-02 05:10:21 +08:00
|
|
|
std::string UnitName = UnitList[j]->getName();
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << j << ":" << UnitName);
|
2015-11-22 04:00:45 +08:00
|
|
|
dbglen += 3 + UnitName.length();
|
|
|
|
assert(FUNameToBitsMap.count(UnitName));
|
|
|
|
UnitBitValue |= FUNameToBitsMap[UnitName];
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UnitBitValue != 0)
|
2015-11-22 04:00:45 +08:00
|
|
|
UnitBits.push_back(UnitBitValue);
|
|
|
|
|
|
|
|
while (dbglen <= 64) { // line up bits dbgs
|
|
|
|
dbglen += 8;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\t");
|
2015-11-22 04:00:45 +08:00
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " (bits: 0x" << Twine::utohexstr(UnitBitValue)
|
|
|
|
<< ")\n");
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
2015-11-22 04:00:45 +08:00
|
|
|
|
2016-12-01 01:48:10 +08:00
|
|
|
if (!UnitBits.empty())
|
2015-11-22 04:00:45 +08:00
|
|
|
allInsnClasses.push_back(UnitBits);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgs() << " ";
|
|
|
|
dbgsInsnClass(UnitBits);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
return NStages;
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
//
|
|
|
|
// collectAllInsnClasses - Populate allInsnClasses which is a set of units
|
|
|
|
// used in each stage.
|
|
|
|
//
|
|
|
|
int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
|
|
|
|
std::vector<Record*> &ProcItinList,
|
|
|
|
std::map<std::string, unsigned> &FUNameToBitsMap,
|
|
|
|
std::vector<Record*> &ItinDataList,
|
|
|
|
int &maxStages,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
// Collect all instruction classes.
|
|
|
|
unsigned M = ItinDataList.size();
|
|
|
|
|
|
|
|
int numInsnClasses = 0;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
|
|
|
|
"----------------------\n"
|
|
|
|
<< "collectAllInsnClasses " << ProcName << " (" << M
|
|
|
|
<< " classes)\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
|
|
|
|
// Collect stages for each instruction class for all itinerary data
|
|
|
|
for (unsigned j = 0; j < M; j++) {
|
|
|
|
Record *ItinData = ItinDataList[j];
|
|
|
|
int NStages = collectOneInsnClass(ProcName, ProcItinList,
|
|
|
|
FUNameToBitsMap, ItinData, OS);
|
|
|
|
if (NStages > maxStages) {
|
|
|
|
maxStages = NStages;
|
|
|
|
}
|
|
|
|
numInsnClasses++;
|
|
|
|
}
|
|
|
|
return numInsnClasses;
|
|
|
|
}
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Run the worklist algorithm to generate the DFA.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2012-06-11 23:37:55 +08:00
|
|
|
void DFAPacketizerEmitter::run(raw_ostream &OS) {
|
2011-12-07 01:34:11 +08:00
|
|
|
// Collect processor iteraries.
|
2011-12-02 05:10:21 +08:00
|
|
|
std::vector<Record*> ProcItinList =
|
|
|
|
Records.getAllDerivedDefinitions("ProcessorItineraries");
|
|
|
|
|
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
// Collect the Functional units.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2015-11-22 04:00:45 +08:00
|
|
|
std::map<std::string, unsigned> FUNameToBitsMap;
|
|
|
|
int maxResources = 0;
|
|
|
|
collectAllFuncUnits(ProcItinList,
|
|
|
|
FUNameToBitsMap, maxResources, OS);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Collect the Combo Functional units.
|
|
|
|
//
|
|
|
|
std::map<unsigned, unsigned> ComboBitToBitsMap;
|
|
|
|
std::vector<Record*> ComboFuncList =
|
|
|
|
Records.getAllDerivedDefinitions("ComboFuncUnits");
|
|
|
|
int numCombos = collectAllComboFuncs(ComboFuncList,
|
|
|
|
FUNameToBitsMap, ComboBitToBitsMap, OS);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Collect the itineraries.
|
|
|
|
//
|
|
|
|
int maxStages = 0;
|
|
|
|
int numInsnClasses = 0;
|
2011-12-02 05:10:21 +08:00
|
|
|
for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
|
|
|
|
Record *Proc = ProcItinList[i];
|
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Get processor itinerary name.
|
2015-11-22 04:00:45 +08:00
|
|
|
const std::string &ProcName = Proc->getName();
|
2011-12-02 05:10:21 +08:00
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Skip default.
|
2015-11-22 04:00:45 +08:00
|
|
|
if (ProcName == "NoItineraries")
|
2011-12-02 05:10:21 +08:00
|
|
|
continue;
|
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Sanity check for at least one instruction itinerary class.
|
2011-12-02 05:10:21 +08:00
|
|
|
unsigned NItinClasses =
|
|
|
|
Records.getAllDerivedDefinitions("InstrItinClass").size();
|
|
|
|
if (NItinClasses == 0)
|
|
|
|
return;
|
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Get itinerary data list.
|
2011-12-02 05:10:21 +08:00
|
|
|
std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
|
|
|
|
|
2015-11-22 04:00:45 +08:00
|
|
|
// Collect all instruction classes
|
|
|
|
numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
|
|
|
|
FUNameToBitsMap, ItinDataList, maxStages, OS);
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Run a worklist algorithm to generate the DFA.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
|
|
|
DFA D;
|
2014-04-22 06:35:11 +08:00
|
|
|
const State *Initial = &D.newState();
|
2011-12-02 05:10:21 +08:00
|
|
|
Initial->isInitial = true;
|
|
|
|
Initial->stateInfo.insert(0x0);
|
2014-04-22 06:35:11 +08:00
|
|
|
SmallVector<const State*, 32> WorkList;
|
|
|
|
std::map<std::set<unsigned>, const State*> Visited;
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
WorkList.push_back(Initial);
|
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// Worklist algorithm to create a DFA for processor resource tracking.
|
2011-12-02 05:10:21 +08:00
|
|
|
// C = {set of InsnClasses}
|
|
|
|
// Begin with initial node in worklist. Initial node does not have
|
|
|
|
// any consumed resources,
|
|
|
|
// ResourceState = 0x0
|
|
|
|
// Visited = {}
|
|
|
|
// While worklist != empty
|
|
|
|
// S = first element of worklist
|
|
|
|
// For every instruction class C
|
|
|
|
// if we can accommodate C in S:
|
|
|
|
// S' = state with resource states = {S Union C}
|
|
|
|
// Add a new transition: S x C -> S'
|
|
|
|
// If S' is not in Visited:
|
|
|
|
// Add S' to worklist
|
|
|
|
// Add S' to Visited
|
|
|
|
//
|
|
|
|
while (!WorkList.empty()) {
|
2014-04-22 06:35:11 +08:00
|
|
|
const State *current = WorkList.pop_back_val();
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgs() << "---------------------\n";
|
|
|
|
dbgs() << "Processing state: " << current->stateNum << " - ";
|
|
|
|
dbgsStateInfo(current->stateInfo);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2015-11-22 04:00:45 +08:00
|
|
|
for (unsigned i = 0; i < allInsnClasses.size(); i++) {
|
|
|
|
std::vector<unsigned> InsnClass = allInsnClasses[i];
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgs() << i << " ";
|
|
|
|
dbgsInsnClass(InsnClass);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
std::set<unsigned> NewStateResources;
|
|
|
|
//
|
|
|
|
// If we haven't already created a transition for this input
|
2011-12-07 01:34:11 +08:00
|
|
|
// and the state can accommodate this InsnClass, create a transition.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2012-09-08 05:35:43 +08:00
|
|
|
if (!current->hasTransition(InsnClass) &&
|
2015-11-22 04:00:45 +08:00
|
|
|
current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
|
2016-01-27 02:48:36 +08:00
|
|
|
const State *NewState = nullptr;
|
2015-11-22 04:00:45 +08:00
|
|
|
current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
|
2016-12-01 01:48:10 +08:00
|
|
|
if (NewStateResources.empty()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Skipped - no new states generated\n");
|
2015-11-22 04:00:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgs() << "\t";
|
|
|
|
dbgsStateInfo(NewStateResources);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2011-12-02 05:10:21 +08:00
|
|
|
|
|
|
|
//
|
2011-12-07 01:34:11 +08:00
|
|
|
// If we have seen this state before, then do not create a new state.
|
2011-12-02 05:10:21 +08:00
|
|
|
//
|
2014-04-22 06:35:11 +08:00
|
|
|
auto VI = Visited.find(NewStateResources);
|
2015-11-22 04:00:45 +08:00
|
|
|
if (VI != Visited.end()) {
|
2011-12-02 05:10:21 +08:00
|
|
|
NewState = VI->second;
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgs() << "\tFound existing state: " << NewState->stateNum
|
|
|
|
<< " - ";
|
|
|
|
dbgsStateInfo(NewState->stateInfo);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2015-11-22 04:00:45 +08:00
|
|
|
} else {
|
2014-04-22 06:35:11 +08:00
|
|
|
NewState = &D.newState();
|
2011-12-02 05:10:21 +08:00
|
|
|
NewState->stateInfo = NewStateResources;
|
|
|
|
Visited[NewStateResources] = NewState;
|
|
|
|
WorkList.push_back(NewState);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2015-11-22 06:46:52 +08:00
|
|
|
dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
|
|
|
|
dbgsStateInfo(NewState->stateInfo);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
2015-11-22 04:00:45 +08:00
|
|
|
|
2012-09-08 05:35:43 +08:00
|
|
|
current->addTransition(InsnClass, NewState);
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 01:34:11 +08:00
|
|
|
// Print out the table.
|
2015-11-22 04:00:45 +08:00
|
|
|
D.writeTableAndAPI(OS, TargetName,
|
|
|
|
numInsnClasses, maxResources, numCombos, maxStages);
|
2011-12-02 05:10:21 +08:00
|
|
|
}
|
2012-06-11 23:37:55 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
|
|
|
|
emitSourceFileHeader("Target DFA Packetizer Tables", OS);
|
|
|
|
DFAPacketizerEmitter(RK).run(OS);
|
|
|
|
}
|
|
|
|
|
2016-12-01 01:48:10 +08:00
|
|
|
} // end namespace llvm
|