33 #define DEBUG_TYPE "machine-combiner"
35 STATISTIC(NumInstCombined,
"Number of machineinst combined");
59 StringRef getPassName()
const override {
return "Machine InstCombiner"; }
62 bool doSubstitute(
unsigned NewSize,
unsigned OldSize);
90 "Machine InstCombiner",
false,
false)
96 void MachineCombiner::getAnalysisUsage(
AnalysisUsage &AU)
const {
110 DefInstr =
MRI->getUniqueVRegDef(MO.
getReg());
112 if (DefInstr && DefInstr->
isPHI())
130 assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
131 "Missing machine model\n");
136 for (
auto *InstrPtr : InsInstrs) {
147 unsigned DepthOp = 0;
148 unsigned LatencyOp = 0;
151 if (II != InstrIdxForVirtReg.
end()) {
153 assert(II->second < InstrDepth.
size() &&
"Bad Index");
156 "There must be a definition for a new virtual register");
157 DepthOp = InstrDepth[II->second];
158 LatencyOp = TSchedModel.computeOperandLatency(
159 DefInstr, DefInstr->findRegisterDefOperandIdx(MO.
getReg()),
160 InstrPtr, InstrPtr->findRegisterUseOperandIdx(MO.
getReg()));
165 LatencyOp = TSchedModel.computeOperandLatency(
167 InstrPtr, InstrPtr->findRegisterUseOperandIdx(MO.
getReg()));
170 IDepth =
std::max(IDepth, DepthOp + LatencyOp);
174 unsigned NewRootIdx = InsInstrs.size() - 1;
175 return InstrDepth[NewRootIdx];
189 assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
190 "Missing machine model\n");
193 unsigned NewRootLatency = 0;
205 unsigned LatencyOp = 0;
207 LatencyOp = TSchedModel.computeOperandLatency(
211 LatencyOp = TSchedModel.computeInstrLatency(NewRoot);
213 NewRootLatency =
std::max(NewRootLatency, LatencyOp);
215 return NewRootLatency;
244 bool MachineCombiner::improvesCriticalPathLen(
251 assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
252 "Missing machine model\n");
254 unsigned NewRootIdx = InsInstrs.
size() - 1;
258 unsigned NewRootDepth = getDepth(InsInstrs, InstrIdxForVirtReg, BlockTrace);
261 DEBUG(
dbgs() <<
"DEPENDENCE DATA FOR " << Root <<
"\n";
262 dbgs() <<
" NewRootDepth: " << NewRootDepth <<
"\n";
263 dbgs() <<
" RootDepth: " << RootDepth <<
"\n");
271 return NewRootDepth < RootDepth;
277 unsigned NewRootLatency =
getLatency(Root, NewRoot, BlockTrace);
278 unsigned RootLatency = 0;
280 for (
auto I : DelInstrs)
281 RootLatency += TSchedModel.computeInstrLatency(
I);
285 DEBUG(
dbgs() <<
" NewRootLatency: " << NewRootLatency <<
"\n";
286 dbgs() <<
" RootLatency: " << RootLatency <<
"\n";
287 dbgs() <<
" RootSlack: " << RootSlack <<
"\n";
288 dbgs() <<
" NewRootDepth + NewRootLatency = "
289 << NewRootDepth + NewRootLatency <<
"\n";
290 dbgs() <<
" RootDepth + RootLatency + RootSlack = "
291 << RootDepth + RootLatency + RootSlack <<
"\n";);
293 unsigned NewCycleCount = NewRootDepth + NewRootLatency;
294 unsigned OldCycleCount = RootDepth + RootLatency + RootSlack;
296 return NewCycleCount <= OldCycleCount;
300 void MachineCombiner::instr2instrSC(
303 for (
auto *InstrPtr : Instrs) {
304 unsigned Opc = InstrPtr->getOpcode();
305 unsigned Idx =
TII->get(Opc).getSchedClass();
312 bool MachineCombiner::preservesResourceLen(
316 if (!TSchedModel.hasInstrSchedModel())
330 instr2instrSC(InsInstrs, InsInstrsSC);
331 instr2instrSC(DelInstrs, DelInstrsSC);
337 unsigned ResLenAfterCombine =
341 dbgs() <<
" resource len before: " << ResLenBeforeCombine
342 <<
" after: " << ResLenAfterCombine <<
"\n";);
344 return ResLenAfterCombine <= ResLenBeforeCombine;
349 bool MachineCombiner::doSubstitute(
unsigned NewSize,
unsigned OldSize) {
350 if (OptSize && (NewSize < OldSize))
352 if (!TSchedModel.hasInstrSchedModelOrItineraries())
361 for (
auto *InstrPtr : InsInstrs)
363 for (
auto *InstrPtr : DelInstrs)
364 InstrPtr->eraseFromParentAndMarkDBGValuesForRemoval();
378 bool Changed =
false;
381 auto BlockIter = MBB->
begin();
385 while (BlockIter != MBB->
end()) {
386 auto &
MI = *BlockIter++;
415 if (!
TII->getMachineCombinerPatterns(
MI, Patterns))
418 for (
auto P : Patterns) {
424 Traces->verifyAnalysis();
425 TII->genAlternativeCodeSequence(
MI,
P, InsInstrs, DelInstrs,
427 unsigned NewInstCount = InsInstrs.
size();
428 unsigned OldInstCount = DelInstrs.
size();
435 bool SubstituteAlways =
false;
436 if (ML &&
TII->isThroughputPattern(
P))
437 SubstituteAlways =
true;
443 if (SubstituteAlways || doSubstitute(NewInstCount, OldInstCount)) {
452 if (improvesCriticalPathLen(MBB, &
MI, BlockTrace, InsInstrs, DelInstrs,
453 InstrIdxForVirtReg,
P) &&
454 preservesResourceLen(MBB, BlockTrace, InsInstrs, DelInstrs)) {
463 for (
auto *InstrPtr : InsInstrs)
466 InstrIdxForVirtReg.
clear();
478 TSchedModel.init(SchedModel, &STI,
TII);
480 MLI = &getAnalysis<MachineLoopInfo>();
481 Traces = &getAnalysis<MachineTraceMetrics>();
486 if (!
TII->useMachineCombiner()) {
487 DEBUG(
dbgs() <<
" Skipping pass: Target does not support machine combiner\n");
491 bool Changed =
false;
495 Changed |= combineInstructions(&MBB);
void push_back(const T &Elt)
char & MachineCombinerID
This pass performs instruction combining using trace metrics to estimate critical-path and resource d...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
STATISTIC(NumFunctions,"Total number of functions")
unsigned Depth
Earliest issue cycle as determined by data dependencies and instruction latencies from the beginning ...
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
A trace ensemble is a collection of traces selected using the same strategy, for example 'minimum res...
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
iterator_range< mop_iterator > operands()
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
InstrCycles getInstrCycles(const MachineInstr &MI) const
Return the depth and height of MI.
#define INITIALIZE_PASS_DEPENDENCY(depName)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Provide an instruction scheduling machine model to CodeGen passes.
const HexagonInstrInfo * TII
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
The core instruction combiner logic.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
const MCSchedModel & getSchedModel() const
Get the machine model for this subtarget's CPU.
Select the trace through a block that has the fewest instructions.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
static int getLatency(LLVMDisasmContext *DC, const MCInst &Inst)
Gets latency information for Inst, based on DC information.
TargetInstrInfo - Interface to description of machine instruction set.
static CombinerObjective getCombinerObjective(MachineCombinerPattern P)
INITIALIZE_PASS_BEGIN(MachineCombiner, DEBUG_TYPE,"Machine InstCombiner", false, false) INITIALIZE_PASS_END(MachineCombiner
iterator find(const_arg_type_t< KeyT > Val)
unsigned const MachineRegisterInfo * MRI
int findRegisterDefOperandIdx(unsigned Reg, bool isDead=false, bool Overlap=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a def of the specified register or -1 if it is not found...
Summarize the scheduling resources required for an instruction of a particular scheduling class...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
unsigned getResourceLength(ArrayRef< const MachineBasicBlock * > Extrablocks=None, ArrayRef< const MCSchedClassDesc * > ExtraInstrs=None, ArrayRef< const MCSchedClassDesc * > RemoveInstrs=None) const
Return the resource length of the trace.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
CombinerObjective
The combiner's goal may differ based on which pattern it is attempting to optimize.
MachineCombinerPattern
These are instruction patterns matched by the machine combiner pass.
void DeleteMachineInstr(MachineInstr *MI)
DeleteMachineInstr - Delete the given MachineInstr.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A trace represents a plausible sequence of executed basic blocks that passes through the current basi...
int findRegisterUseOperandIdx(unsigned Reg, bool isKill=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a use of the specific register or -1 if it is not found...
MachineOperand class - Representation of each machine instruction operand.
CHAIN = SC CHAIN, Imm128 - System call.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
void invalidate(const MachineBasicBlock *MBB)
Invalidate cached information about MBB.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
TargetSubtargetInfo - Generic base class for all target subtargets.
unsigned getInstrSlack(const MachineInstr &MI) const
Return the slack of MI.
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static void insertDeleteInstructions(MachineBasicBlock *MBB, MachineInstr &MI, SmallVector< MachineInstr *, 16 > InsInstrs, SmallVector< MachineInstr *, 16 > DelInstrs, MachineTraceMetrics *Traces)
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
bool isDepInTrace(const MachineInstr &DefMI, const MachineInstr &UseMI) const
A dependence is useful if the basic block of the defining instruction is part of the trace of the use...
void initializeMachineCombinerPass(PassRegistry &)
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
StringRef - Represent a constant reference to a string, i.e.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
reg_begin/reg_end - Provide iteration support to walk over all definitions and uses of a register wit...
Machine model for scheduling, bundling, and heuristics.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...