22 #define DEBUG_TYPE "llvm-mca" 31 : STI(sti), MCII(mcii),
MRI(mri), MCIA(mcia), FirstCallInst(
true),
32 FirstReturnInst(
true) {
45 using ResourcePlusCycles = std::pair<uint64_t, ResourceUsage>;
46 std::vector<ResourcePlusCycles> Worklist;
61 APInt Buffers(NumProcResources, 0);
63 bool AllInOrderResources =
true;
64 bool AnyDispatchHazards =
false;
71 <<
"Ignoring invalid write of zero cycles on processor resource " 74 <<
" (write index #" <<
I <<
")\n";
81 AllInOrderResources =
false;
89 Worklist.emplace_back(ResourcePlusCycles(Mask,
ResourceUsage(RCy)));
91 uint64_t Super = ProcResourceMasks[PR.
SuperIdx];
92 SuperResources[Super] += PRE->
Cycles;
100 sort(Worklist, [](
const ResourcePlusCycles &A,
const ResourcePlusCycles &
B) {
103 if (popcntA < popcntB)
105 if (popcntA > popcntB)
107 return A.first < B.first;
110 uint64_t UsedResourceUnits = 0;
111 uint64_t UsedResourceGroups = 0;
114 for (
unsigned I = 0,
E = Worklist.size();
I <
E; ++
I) {
115 ResourcePlusCycles &A = Worklist[
I];
116 if (!A.second.size()) {
123 uint64_t NormalizedMask = A.first;
125 UsedResourceUnits |= A.first;
129 UsedResourceGroups |= (A.first ^ NormalizedMask);
132 for (
unsigned J =
I + 1; J <
E; ++J) {
133 ResourcePlusCycles &B = Worklist[J];
134 if ((NormalizedMask & B.first) == NormalizedMask) {
135 B.second.CS.subtract(A.second.size() - SuperResources[A.first]);
159 for (ResourcePlusCycles &RPC : ID.
Resources) {
163 if ((Mask & UsedResourceUnits) == Mask)
164 RPC.second.setReserved();
169 for (
const std::pair<uint64_t, unsigned> &SR : SuperResources) {
170 for (
unsigned I = 1, E = NumProcResources;
I <
E; ++
I) {
175 uint64_t
Mask = ProcResourceMasks[
I];
176 if (Mask != SR.first && ((Mask & SR.first) == SR.first))
186 for (
const std::pair<uint64_t, ResourceUsage> &R : ID.
Resources)
188 <<
"Reserved=" << R.second.isReserved() <<
", " 189 <<
"#Units=" << R.second.NumUnits <<
", " 190 <<
"cy=" << R.second.size() <<
'\n';
193 uint64_t Current = BufferIDs & (-BufferIDs);
195 BufferIDs ^= Current;
221 unsigned NumExplicitDefs = MCDesc.
getNumDefs();
228 if (NumExplicitDefs) {
229 return make_error<InstructionError<MCInst>>(
230 "Expected more register operand definitions.", MCI);
237 std::string Message =
238 "expected a register operand for an optional definition. Instruction " 239 "has not been correctly analyzed.";
240 return make_error<InstructionError<MCInst>>(Message, MCI);
248 unsigned SchedClassID) {
295 unsigned NumExplicitDefs = MCDesc.
getNumDefs();
298 unsigned TotalDefs = NumExplicitDefs + NumImplicitDefs;
303 ID.
Writes.resize(TotalDefs + NumVariadicOps);
307 unsigned CurrentDef = 0;
309 for (; i < MCI.
getNumOperands() && CurrentDef < NumExplicitDefs; ++i) {
316 if (CurrentDef < NumWriteLatencyEntries) {
331 <<
", Latency=" << Write.
Latency 337 assert(CurrentDef == NumExplicitDefs &&
338 "Expected more register operand definitions.");
339 for (CurrentDef = 0; CurrentDef < NumImplicitDefs; ++CurrentDef) {
340 unsigned Index = NumExplicitDefs + CurrentDef;
344 if (Index < NumWriteLatencyEntries) {
362 <<
", Latency=" << Write.
Latency 376 <<
", Latency=" << Write.
Latency 393 CurrentDef = NumExplicitDefs + NumImplicitDefs + MCDesc.
hasOptionalDef();
395 I < NumVariadicOps && !AssumeUsesOnly; ++
I, ++OpIndex) {
409 <<
", Latency=" << Write.
Latency 414 ID.
Writes.resize(CurrentDef);
418 unsigned SchedClassID) {
426 unsigned TotalUses = NumExplicitUses + NumImplicitUses + NumVariadicOps;
427 ID.
Reads.resize(TotalUses);
428 unsigned CurrentUse = 0;
429 for (
unsigned I = 0, OpIndex = MCDesc.
getNumDefs();
I < NumExplicitUses;
441 <<
", UseIndex=" << Read.
UseIndex <<
'\n');
446 for (
unsigned I = 0;
I < NumImplicitUses; ++
I) {
453 <<
", UseIndex=" << Read.
UseIndex <<
", RegisterID=" 457 CurrentUse += NumImplicitUses;
466 I < NumVariadicOps && !AssumeDefsOnly; ++
I, ++OpIndex) {
473 Read.
UseIndex = NumExplicitUses + NumImplicitUses +
I;
477 <<
", UseIndex=" << Read.
UseIndex <<
'\n');
480 ID.
Reads.resize(CurrentUse);
484 const MCInst &MCI)
const {
490 bool UsesResources = !ID.
Resources.empty();
491 if (!UsesMemory && !UsesBuffers && !UsesResources)
496 Message =
"found an inconsistent instruction that decodes " 497 "into zero opcodes and that consumes load/store " 500 Message =
"found an inconsistent instruction that decodes " 501 "to zero opcodes and that consumes scheduler " 505 return make_error<InstructionError<MCInst>>(Message, MCI);
509 InstrBuilder::createInstrDescImpl(
const MCInst &MCI) {
511 "Itineraries are not yet supported!");
529 return make_error<InstructionError<MCInst>>(
530 "unable to resolve scheduling class for write variant.", MCI);
537 return make_error<InstructionError<MCInst>>(
538 "found an unsupported instruction in the input assembly sequence.",
543 LLVM_DEBUG(
dbgs() <<
"\t\tSchedClassID=" << SchedClassID <<
'\n');
546 std::unique_ptr<InstrDesc> ID = std::make_unique<InstrDesc>();
548 ID->SchedClassID = SchedClassID;
550 if (MCDesc.
isCall() && FirstCallInst) {
554 <<
"Assume a latency of 100cy.\n";
555 FirstCallInst =
false;
558 if (MCDesc.
isReturn() && FirstReturnInst) {
560 <<
" assembly sequence.\n";
562 FirstReturnInst =
false;
565 ID->MayLoad = MCDesc.
mayLoad();
568 ID->BeginGroup = SCDesc.BeginGroup;
569 ID->EndGroup = SCDesc.EndGroup;
575 return std::move(Err);
577 populateWrites(*ID, MCI, SchedClassID);
578 populateReads(*ID, MCI, SchedClassID);
580 LLVM_DEBUG(
dbgs() <<
"\t\tMaxLatency=" << ID->MaxLatency <<
'\n');
581 LLVM_DEBUG(
dbgs() <<
"\t\tNumMicroOps=" << ID->NumMicroOps <<
'\n');
584 if (
Error Err = verifyInstrDesc(*ID, MCI))
585 return std::move(Err);
589 if (!IsVariadic && !IsVariant) {
590 Descriptors[MCI.
getOpcode()] = std::move(ID);
594 VariantDescriptors[&MCI] = std::move(ID);
595 return *VariantDescriptors[&MCI];
599 InstrBuilder::getOrCreateInstrDesc(
const MCInst &MCI) {
600 if (Descriptors.find_as(MCI.
getOpcode()) != Descriptors.end())
603 if (VariantDescriptors.find(&MCI) != VariantDescriptors.end())
604 return *VariantDescriptors[&MCI];
606 return createInstrDescImpl(MCI);
615 std::unique_ptr<Instruction> NewIS = std::make_unique<Instruction>(
D);
620 bool IsZeroIdiom =
false;
621 bool IsDepBreaking =
false;
624 IsZeroIdiom = MCIA->
isZeroIdiom(MCI, Mask, ProcID);
628 NewIS->setOptimizableMove();
651 NewIS->getUses().emplace_back(RD, RegID);
657 if (Mask.isNullValue()) {
666 if (Mask.getBitWidth() > RD.
UseIndex) {
677 return std::move(NewIS);
689 unsigned WriteIndex = 0;
699 assert(RegID &&
"Expected a valid register ID!");
700 NewIS->getDefs().emplace_back(WD, RegID,
701 WriteMask[WriteIndex],
706 return std::move(NewIS);
Expected< std::unique_ptr< Instruction > > createInstruction(const MCInst &MCI)
unsigned getNumImplicitUses() const
Return the number of implicit uses this instruction has.
uint64_t getZExtValue() const
Get zero extended value.
unsigned getNumImplicitDefs() const
Return the number of implicit defs this instruct has.
This class represents lattice values for constants.
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Subclass of Error for the sole purpose of identifying the success path in the type system...
bool isImplicitRead() const
A register read descriptor.
const MCPhysReg * getImplicitUses() const
Return a list of registers that are potentially read by any instance of this machine instruction...
Describe properties that are true of each instruction in the target description file.
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by other flags.
const MCProcResourceDesc * getProcResource(unsigned ProcResourceIdx) const
unsigned SClassOrWriteResourceID
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
block Block Frequency true
unsigned getProcessorID() const
const MCSchedClassDesc * getSchedClassDesc(unsigned SchedClassIdx) const
Error takeError()
Take ownership of the stored error.
bool mayLoad() const
Return true if this instruction could possibly read memory.
bool isReturn() const
Return true if the instruction is a return.
void setBit(unsigned BitPosition)
Set a given bit to 1.
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Tracks register operand latency in cycles.
SmallVector< ReadDescriptor, 4 > Reads
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Tagged union holding either a T or a Error.
This file implements a class to represent arbitrary precision integral constant values and operations...
unsigned getReg() const
Returns the register number.
virtual bool isOptimizableRegisterMove(const MCInst &MI, unsigned CPUID) const
Returns true if MI is a candidate for move elimination.
void setIndependentFromDef()
Helper used by class InstrDesc to describe how hardware resources are used.
uint64_t UsedProcResUnits
uint16_t NumWriteProcResEntries
const MCWriteLatencyEntry * getWriteLatencyEntry(const MCSchedClassDesc *SC, unsigned DefIdx) const
A register write descriptor.
void computeProcResourceMasks(const MCSchedModel &SM, MutableArrayRef< uint64_t > Masks)
Populates vector Masks with processor resource masks.
Instances of this class represent a single low-level machine instruction.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
uint64_t UsedProcResGroups
unsigned getSchedClass() const
Return the scheduling class for this instruction.
const MCPhysReg * getImplicitDefs() const
Return a list of registers that are potentially written by any instance of this machine instruction...
virtual bool clearsSuperRegisters(const MCRegisterInfo &MRI, const MCInst &Inst, APInt &Writes) const
Returns true if at least one of the register writes performed by.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned const MachineRegisterInfo * MRI
bool hasOptionalDef() const
Set if this instruction has an optional definition, e.g.
Identify one of the processor resource kinds consumed by a particular scheduling class for the specif...
bool isVariadic() const
Return true if this instruction can have a variable number of operands.
static Error verifyOperands(const MCInstrDesc &MCDesc, const MCInst &MCI)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Summarize the scheduling resources required for an instruction of a particular scheduling class...
Interface to description of machine instruction set.
virtual bool isDependencyBreaking(const MCInst &MI, APInt &Mask, unsigned CPUID) const
Returns true if MI is a dependency breaking instruction for the subtarget associated with CPUID ...
unsigned getNumOperands() const
SmallVector< std::pair< uint64_t, ResourceUsage >, 4 > Resources
StringRef getName(unsigned Opcode) const
Returns the name for the instructions with the given opcode.
void sort(IteratorTy Start, IteratorTy End)
static const unsigned short InvalidNumMicroOps
bool hasInstrSchedModel() const
Does this machine model include instruction-level scheduling.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Specify the latency in cpu cycles for a particular scheduling class and def index.
bool isImplicitWrite() const
Define a kind of processor resource that will be modeled by the scheduler.
const MCOperand & getOperand(unsigned i) const
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Class for arbitrary precision integers.
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
bool mayStore() const
Return true if this instruction could possibly modify memory.
bool MustIssueImmediately
virtual bool isZeroIdiom(const MCInst &MI, APInt &Mask, unsigned CPUID) const
Returns true if MI is a dependency breaking zero-idiom for the given subtarget.
An instruction descriptor.
static void computeMaxLatency(InstrDesc &ID, const MCInstrDesc &MCDesc, const MCSchedClassDesc &SCDesc, const MCSubtargetInfo &STI)
static int computeInstrLatency(const MCSubtargetInfo &STI, const MCSchedClassDesc &SCDesc)
Returns the latency value for the scheduling class.
unsigned getResourceStateIndex(uint64_t Mask)
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
bool isCall() const
Return true if the instruction is a call.
Generic base class for all target subtargets.
virtual unsigned resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI, unsigned CPUID) const
Resolve a variant scheduling class for the given MCInst and CPU.
const MCWriteProcResEntry * getWriteProcResBegin(const MCSchedClassDesc *SC) const
Return an iterator at the first process resource consumed by the given scheduling class...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A builder class for instructions that are statically analyzed by llvm-mca.
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
Lightweight error class with error context and mandatory checking.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
uint16_t NumWriteLatencyEntries
StringRef - Represent a constant reference to a string, i.e.
unsigned getOpcode() const
Instances of this class represent operands of the MCInst class.
static void initializeUsedResources(InstrDesc &ID, const MCSchedClassDesc &SCDesc, const MCSubtargetInfo &STI, ArrayRef< uint64_t > ProcResourceMasks)
Machine model for scheduling, bundling, and heuristics.
const MCSchedModel & getSchedModel() const
Get the machine model for this subtarget's CPU.
unsigned getNumProcResourceKinds() const
SmallVector< WriteDescriptor, 4 > Writes