2014-08-04 05:35:39 +08:00
|
|
|
//===---- MachineCombiner.cpp - Instcombining on SSA form machine code ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The machine combiner pass uses machine trace metrics to ensure the combined
|
2017-03-16 05:50:46 +08:00
|
|
|
// instructions do not lengthen the critical path or the resource depth.
|
2014-08-04 05:35:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-07 07:24:35 +08:00
|
|
|
|
2014-08-04 05:35:39 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2016-04-18 17:17:29 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-08-04 05:35:39 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineTraceMetrics.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-07-14 03:30:52 +08:00
|
|
|
#define DEBUG_TYPE "machine-combiner"
|
|
|
|
|
2014-08-04 05:35:39 +08:00
|
|
|
STATISTIC(NumInstCombined, "Number of machineinst combined");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MachineCombiner : public MachineFunctionPass {
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
2014-09-03 01:43:54 +08:00
|
|
|
MCSchedModel SchedModel;
|
2014-08-04 05:35:39 +08:00
|
|
|
MachineRegisterInfo *MRI;
|
2016-04-24 13:14:01 +08:00
|
|
|
MachineLoopInfo *MLI; // Current MachineLoopInfo
|
2014-08-04 05:35:39 +08:00
|
|
|
MachineTraceMetrics *Traces;
|
|
|
|
MachineTraceMetrics::Ensemble *MinInstr;
|
|
|
|
|
|
|
|
TargetSchedModel TSchedModel;
|
|
|
|
|
2015-01-28 06:26:56 +08:00
|
|
|
/// True if optimizing for code size.
|
2014-08-04 05:35:39 +08:00
|
|
|
bool OptSize;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
MachineCombiner() : MachineFunctionPass(ID) {
|
|
|
|
initializeMachineCombinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Machine InstCombiner"; }
|
2014-08-04 05:35:39 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool doSubstitute(unsigned NewSize, unsigned OldSize);
|
|
|
|
bool combineInstructions(MachineBasicBlock *);
|
|
|
|
MachineInstr *getOperandDef(const MachineOperand &MO);
|
|
|
|
unsigned getDepth(SmallVectorImpl<MachineInstr *> &InsInstrs,
|
|
|
|
DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
|
|
|
|
MachineTraceMetrics::Trace BlockTrace);
|
|
|
|
unsigned getLatency(MachineInstr *Root, MachineInstr *NewRoot,
|
|
|
|
MachineTraceMetrics::Trace BlockTrace);
|
|
|
|
bool
|
2015-06-23 08:39:40 +08:00
|
|
|
improvesCriticalPathLen(MachineBasicBlock *MBB, MachineInstr *Root,
|
2015-11-11 00:48:53 +08:00
|
|
|
MachineTraceMetrics::Trace BlockTrace,
|
|
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs,
|
2016-12-12 03:39:32 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &DelInstrs,
|
2015-11-11 00:48:53 +08:00
|
|
|
DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
|
|
|
|
MachineCombinerPattern Pattern);
|
2014-08-04 05:35:39 +08:00
|
|
|
bool preservesResourceLen(MachineBasicBlock *MBB,
|
|
|
|
MachineTraceMetrics::Trace BlockTrace,
|
|
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs,
|
|
|
|
SmallVectorImpl<MachineInstr *> &DelInstrs);
|
|
|
|
void instr2instrSC(SmallVectorImpl<MachineInstr *> &Instrs,
|
|
|
|
SmallVectorImpl<const MCSchedClassDesc *> &InstrsSC);
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-08-04 05:35:39 +08:00
|
|
|
|
|
|
|
char MachineCombiner::ID = 0;
|
|
|
|
char &llvm::MachineCombinerID = MachineCombiner::ID;
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MachineCombiner, DEBUG_TYPE,
|
2014-08-04 05:35:39 +08:00
|
|
|
"Machine InstCombiner", false, false)
|
2016-04-24 13:14:01 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
2014-08-04 05:35:39 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS_END(MachineCombiner, DEBUG_TYPE, "Machine InstCombiner",
|
2014-08-04 05:35:39 +08:00
|
|
|
false, false)
|
|
|
|
|
|
|
|
void MachineCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
2016-04-24 13:14:01 +08:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
2014-08-04 05:35:39 +08:00
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addRequired<MachineTraceMetrics>();
|
|
|
|
AU.addPreserved<MachineTraceMetrics>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *MachineCombiner::getOperandDef(const MachineOperand &MO) {
|
|
|
|
MachineInstr *DefInstr = nullptr;
|
|
|
|
// We need a virtual register definition.
|
|
|
|
if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
|
|
|
|
DefInstr = MRI->getUniqueVRegDef(MO.getReg());
|
|
|
|
// PHI's have no depth etc.
|
|
|
|
if (DefInstr && DefInstr->isPHI())
|
|
|
|
DefInstr = nullptr;
|
|
|
|
return DefInstr;
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:26:56 +08:00
|
|
|
/// Computes depth of instructions in vector \InsInstr.
|
2014-08-04 05:35:39 +08:00
|
|
|
///
|
|
|
|
/// \param InsInstrs is a vector of machine instructions
|
|
|
|
/// \param InstrIdxForVirtReg is a dense map of virtual register to index
|
|
|
|
/// of defining machine instruction in \p InsInstrs
|
|
|
|
/// \param BlockTrace is a trace of machine instructions
|
|
|
|
///
|
|
|
|
/// \returns Depth of last instruction in \InsInstrs ("NewRoot")
|
|
|
|
unsigned
|
|
|
|
MachineCombiner::getDepth(SmallVectorImpl<MachineInstr *> &InsInstrs,
|
|
|
|
DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
|
|
|
|
MachineTraceMetrics::Trace BlockTrace) {
|
|
|
|
SmallVector<unsigned, 16> InstrDepth;
|
2015-07-15 16:22:23 +08:00
|
|
|
assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
|
|
|
|
"Missing machine model\n");
|
2014-08-04 05:35:39 +08:00
|
|
|
|
2015-01-28 06:16:52 +08:00
|
|
|
// For each instruction in the new sequence compute the depth based on the
|
2014-08-04 05:35:39 +08:00
|
|
|
// operands. Use the trace information when possible. For new operands which
|
|
|
|
// are tracked in the InstrIdxForVirtReg map depth is looked up in InstrDepth
|
|
|
|
for (auto *InstrPtr : InsInstrs) { // for each Use
|
|
|
|
unsigned IDepth = 0;
|
2017-01-30 02:20:42 +08:00
|
|
|
DEBUG(dbgs() << "NEW INSTR ";
|
|
|
|
InstrPtr->print(dbgs(), TII);
|
|
|
|
dbgs() << "\n";);
|
2015-05-22 01:43:26 +08:00
|
|
|
for (const MachineOperand &MO : InstrPtr->operands()) {
|
2014-08-04 05:35:39 +08:00
|
|
|
// Check for virtual register operand.
|
|
|
|
if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
|
|
|
|
continue;
|
|
|
|
if (!MO.isUse())
|
|
|
|
continue;
|
|
|
|
unsigned DepthOp = 0;
|
|
|
|
unsigned LatencyOp = 0;
|
|
|
|
DenseMap<unsigned, unsigned>::iterator II =
|
|
|
|
InstrIdxForVirtReg.find(MO.getReg());
|
|
|
|
if (II != InstrIdxForVirtReg.end()) {
|
|
|
|
// Operand is new virtual register not in trace
|
2014-08-04 07:00:38 +08:00
|
|
|
assert(II->second < InstrDepth.size() && "Bad Index");
|
2014-08-04 05:35:39 +08:00
|
|
|
MachineInstr *DefInstr = InsInstrs[II->second];
|
|
|
|
assert(DefInstr &&
|
|
|
|
"There must be a definition for a new virtual register");
|
|
|
|
DepthOp = InstrDepth[II->second];
|
|
|
|
LatencyOp = TSchedModel.computeOperandLatency(
|
|
|
|
DefInstr, DefInstr->findRegisterDefOperandIdx(MO.getReg()),
|
|
|
|
InstrPtr, InstrPtr->findRegisterUseOperandIdx(MO.getReg()));
|
|
|
|
} else {
|
|
|
|
MachineInstr *DefInstr = getOperandDef(MO);
|
|
|
|
if (DefInstr) {
|
2016-02-22 11:33:28 +08:00
|
|
|
DepthOp = BlockTrace.getInstrCycles(*DefInstr).Depth;
|
2014-08-04 05:35:39 +08:00
|
|
|
LatencyOp = TSchedModel.computeOperandLatency(
|
|
|
|
DefInstr, DefInstr->findRegisterDefOperandIdx(MO.getReg()),
|
|
|
|
InstrPtr, InstrPtr->findRegisterUseOperandIdx(MO.getReg()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IDepth = std::max(IDepth, DepthOp + LatencyOp);
|
|
|
|
}
|
|
|
|
InstrDepth.push_back(IDepth);
|
|
|
|
}
|
|
|
|
unsigned NewRootIdx = InsInstrs.size() - 1;
|
|
|
|
return InstrDepth[NewRootIdx];
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:26:56 +08:00
|
|
|
/// Computes instruction latency as max of latency of defined operands.
|
2014-08-04 05:35:39 +08:00
|
|
|
///
|
|
|
|
/// \param Root is a machine instruction that could be replaced by NewRoot.
|
|
|
|
/// It is used to compute a more accurate latency information for NewRoot in
|
|
|
|
/// case there is a dependent instruction in the same trace (\p BlockTrace)
|
|
|
|
/// \param NewRoot is the instruction for which the latency is computed
|
|
|
|
/// \param BlockTrace is a trace of machine instructions
|
|
|
|
///
|
|
|
|
/// \returns Latency of \p NewRoot
|
|
|
|
unsigned MachineCombiner::getLatency(MachineInstr *Root, MachineInstr *NewRoot,
|
|
|
|
MachineTraceMetrics::Trace BlockTrace) {
|
2015-07-15 16:22:23 +08:00
|
|
|
assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
|
|
|
|
"Missing machine model\n");
|
2014-08-04 05:35:39 +08:00
|
|
|
|
|
|
|
// Check each definition in NewRoot and compute the latency
|
|
|
|
unsigned NewRootLatency = 0;
|
|
|
|
|
2015-05-22 01:43:26 +08:00
|
|
|
for (const MachineOperand &MO : NewRoot->operands()) {
|
2014-08-04 05:35:39 +08:00
|
|
|
// Check for virtual register operand.
|
|
|
|
if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
|
|
|
|
continue;
|
|
|
|
if (!MO.isDef())
|
|
|
|
continue;
|
|
|
|
// Get the first instruction that uses MO
|
|
|
|
MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(MO.getReg());
|
|
|
|
RI++;
|
|
|
|
MachineInstr *UseMO = RI->getParent();
|
|
|
|
unsigned LatencyOp = 0;
|
2016-02-22 11:33:28 +08:00
|
|
|
if (UseMO && BlockTrace.isDepInTrace(*Root, *UseMO)) {
|
2014-08-04 05:35:39 +08:00
|
|
|
LatencyOp = TSchedModel.computeOperandLatency(
|
|
|
|
NewRoot, NewRoot->findRegisterDefOperandIdx(MO.getReg()), UseMO,
|
|
|
|
UseMO->findRegisterUseOperandIdx(MO.getReg()));
|
|
|
|
} else {
|
2015-08-05 15:45:28 +08:00
|
|
|
LatencyOp = TSchedModel.computeInstrLatency(NewRoot);
|
2014-08-04 05:35:39 +08:00
|
|
|
}
|
|
|
|
NewRootLatency = std::max(NewRootLatency, LatencyOp);
|
|
|
|
}
|
|
|
|
return NewRootLatency;
|
|
|
|
}
|
|
|
|
|
2015-11-11 00:48:53 +08:00
|
|
|
/// The combiner's goal may differ based on which pattern it is attempting
|
|
|
|
/// to optimize.
|
|
|
|
enum class CombinerObjective {
|
|
|
|
MustReduceDepth, // The data dependency chain must be improved.
|
|
|
|
Default // The critical path must not be lengthened.
|
|
|
|
};
|
|
|
|
|
|
|
|
static CombinerObjective getCombinerObjective(MachineCombinerPattern P) {
|
|
|
|
// TODO: If C++ ever gets a real enum class, make this part of the
|
|
|
|
// MachineCombinerPattern class.
|
|
|
|
switch (P) {
|
|
|
|
case MachineCombinerPattern::REASSOC_AX_BY:
|
|
|
|
case MachineCombinerPattern::REASSOC_AX_YB:
|
|
|
|
case MachineCombinerPattern::REASSOC_XA_BY:
|
|
|
|
case MachineCombinerPattern::REASSOC_XA_YB:
|
|
|
|
return CombinerObjective::MustReduceDepth;
|
|
|
|
default:
|
|
|
|
return CombinerObjective::Default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 08:39:40 +08:00
|
|
|
/// The DAGCombine code sequence ends in MI (Machine Instruction) Root.
|
|
|
|
/// The new code sequence ends in MI NewRoot. A necessary condition for the new
|
|
|
|
/// sequence to replace the old sequence is that it cannot lengthen the critical
|
2015-11-11 00:48:53 +08:00
|
|
|
/// path. The definition of "improve" may be restricted by specifying that the
|
|
|
|
/// new path improves the data dependency chain (MustReduceDepth).
|
2015-06-23 08:39:40 +08:00
|
|
|
bool MachineCombiner::improvesCriticalPathLen(
|
2014-08-04 05:35:39 +08:00
|
|
|
MachineBasicBlock *MBB, MachineInstr *Root,
|
|
|
|
MachineTraceMetrics::Trace BlockTrace,
|
|
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs,
|
2016-12-12 03:39:32 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &DelInstrs,
|
2015-06-23 08:39:40 +08:00
|
|
|
DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
|
2015-11-11 00:48:53 +08:00
|
|
|
MachineCombinerPattern Pattern) {
|
2015-07-15 16:22:23 +08:00
|
|
|
assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
|
|
|
|
"Missing machine model\n");
|
2015-06-11 03:52:58 +08:00
|
|
|
// NewRoot is the last instruction in the \p InsInstrs vector.
|
2014-08-04 05:35:39 +08:00
|
|
|
unsigned NewRootIdx = InsInstrs.size() - 1;
|
|
|
|
MachineInstr *NewRoot = InsInstrs[NewRootIdx];
|
|
|
|
|
2015-11-11 00:48:53 +08:00
|
|
|
// Get depth and latency of NewRoot and Root.
|
|
|
|
unsigned NewRootDepth = getDepth(InsInstrs, InstrIdxForVirtReg, BlockTrace);
|
2016-02-22 11:33:28 +08:00
|
|
|
unsigned RootDepth = BlockTrace.getInstrCycles(*Root).Depth;
|
2015-11-11 00:48:53 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "DEPENDENCE DATA FOR " << Root << "\n";
|
|
|
|
dbgs() << " NewRootDepth: " << NewRootDepth << "\n";
|
|
|
|
dbgs() << " RootDepth: " << RootDepth << "\n");
|
|
|
|
|
|
|
|
// For a transform such as reassociation, the cost equation is
|
|
|
|
// conservatively calculated so that we must improve the depth (data
|
|
|
|
// dependency cycles) in the critical path to proceed with the transform.
|
|
|
|
// Being conservative also protects against inaccuracies in the underlying
|
|
|
|
// machine trace metrics and CPU models.
|
|
|
|
if (getCombinerObjective(Pattern) == CombinerObjective::MustReduceDepth)
|
|
|
|
return NewRootDepth < RootDepth;
|
|
|
|
|
|
|
|
// A more flexible cost calculation for the critical path includes the slack
|
|
|
|
// of the original code sequence. This may allow the transform to proceed
|
|
|
|
// even if the instruction depths (data dependency cycles) become worse.
|
2016-12-12 03:39:32 +08:00
|
|
|
|
2015-11-11 00:48:53 +08:00
|
|
|
unsigned NewRootLatency = getLatency(Root, NewRoot, BlockTrace);
|
2016-12-12 03:39:32 +08:00
|
|
|
unsigned RootLatency = 0;
|
|
|
|
|
|
|
|
for (auto I : DelInstrs)
|
|
|
|
RootLatency += TSchedModel.computeInstrLatency(I);
|
|
|
|
|
2016-02-22 11:33:28 +08:00
|
|
|
unsigned RootSlack = BlockTrace.getInstrSlack(*Root);
|
2014-08-04 05:35:39 +08:00
|
|
|
|
2015-11-11 00:48:53 +08:00
|
|
|
DEBUG(dbgs() << " NewRootLatency: " << NewRootLatency << "\n";
|
|
|
|
dbgs() << " RootLatency: " << RootLatency << "\n";
|
|
|
|
dbgs() << " RootSlack: " << RootSlack << "\n";
|
2015-10-04 04:45:01 +08:00
|
|
|
dbgs() << " NewRootDepth + NewRootLatency = "
|
2014-08-04 05:35:39 +08:00
|
|
|
<< NewRootDepth + NewRootLatency << "\n";
|
2015-10-04 04:45:01 +08:00
|
|
|
dbgs() << " RootDepth + RootLatency + RootSlack = "
|
2014-08-04 05:35:39 +08:00
|
|
|
<< RootDepth + RootLatency + RootSlack << "\n";);
|
|
|
|
|
2015-06-23 08:39:40 +08:00
|
|
|
unsigned NewCycleCount = NewRootDepth + NewRootLatency;
|
|
|
|
unsigned OldCycleCount = RootDepth + RootLatency + RootSlack;
|
2016-02-27 09:10:43 +08:00
|
|
|
|
2015-11-11 00:48:53 +08:00
|
|
|
return NewCycleCount <= OldCycleCount;
|
2014-08-04 05:35:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// helper routine to convert instructions into SC
|
|
|
|
void MachineCombiner::instr2instrSC(
|
|
|
|
SmallVectorImpl<MachineInstr *> &Instrs,
|
|
|
|
SmallVectorImpl<const MCSchedClassDesc *> &InstrsSC) {
|
|
|
|
for (auto *InstrPtr : Instrs) {
|
|
|
|
unsigned Opc = InstrPtr->getOpcode();
|
|
|
|
unsigned Idx = TII->get(Opc).getSchedClass();
|
2014-09-03 01:43:54 +08:00
|
|
|
const MCSchedClassDesc *SC = SchedModel.getSchedClassDesc(Idx);
|
2014-08-04 05:35:39 +08:00
|
|
|
InstrsSC.push_back(SC);
|
|
|
|
}
|
|
|
|
}
|
2015-10-07 07:24:35 +08:00
|
|
|
|
2015-01-28 06:26:56 +08:00
|
|
|
/// True when the new instructions do not increase resource length
|
2014-08-04 05:35:39 +08:00
|
|
|
bool MachineCombiner::preservesResourceLen(
|
|
|
|
MachineBasicBlock *MBB, MachineTraceMetrics::Trace BlockTrace,
|
|
|
|
SmallVectorImpl<MachineInstr *> &InsInstrs,
|
|
|
|
SmallVectorImpl<MachineInstr *> &DelInstrs) {
|
2015-07-15 16:22:23 +08:00
|
|
|
if (!TSchedModel.hasInstrSchedModel())
|
|
|
|
return true;
|
2014-08-04 05:35:39 +08:00
|
|
|
|
|
|
|
// Compute current resource length
|
|
|
|
|
2014-08-08 05:40:58 +08:00
|
|
|
//ArrayRef<const MachineBasicBlock *> MBBarr(MBB);
|
|
|
|
SmallVector <const MachineBasicBlock *, 1> MBBarr;
|
|
|
|
MBBarr.push_back(MBB);
|
2014-08-04 05:35:39 +08:00
|
|
|
unsigned ResLenBeforeCombine = BlockTrace.getResourceLength(MBBarr);
|
|
|
|
|
|
|
|
// Deal with SC rather than Instructions.
|
|
|
|
SmallVector<const MCSchedClassDesc *, 16> InsInstrsSC;
|
|
|
|
SmallVector<const MCSchedClassDesc *, 16> DelInstrsSC;
|
|
|
|
|
|
|
|
instr2instrSC(InsInstrs, InsInstrsSC);
|
|
|
|
instr2instrSC(DelInstrs, DelInstrsSC);
|
|
|
|
|
|
|
|
ArrayRef<const MCSchedClassDesc *> MSCInsArr = makeArrayRef(InsInstrsSC);
|
|
|
|
ArrayRef<const MCSchedClassDesc *> MSCDelArr = makeArrayRef(DelInstrsSC);
|
|
|
|
|
2015-06-11 03:52:58 +08:00
|
|
|
// Compute new resource length.
|
2014-08-04 05:35:39 +08:00
|
|
|
unsigned ResLenAfterCombine =
|
|
|
|
BlockTrace.getResourceLength(MBBarr, MSCInsArr, MSCDelArr);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "RESOURCE DATA: \n";
|
|
|
|
dbgs() << " resource len before: " << ResLenBeforeCombine
|
|
|
|
<< " after: " << ResLenAfterCombine << "\n";);
|
|
|
|
|
|
|
|
return ResLenAfterCombine <= ResLenBeforeCombine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \returns true when new instruction sequence should be generated
|
2015-01-28 06:16:52 +08:00
|
|
|
/// independent if it lengthens critical path or not
|
2014-08-04 05:35:39 +08:00
|
|
|
bool MachineCombiner::doSubstitute(unsigned NewSize, unsigned OldSize) {
|
|
|
|
if (OptSize && (NewSize < OldSize))
|
|
|
|
return true;
|
2015-07-15 16:22:23 +08:00
|
|
|
if (!TSchedModel.hasInstrSchedModelOrItineraries())
|
2014-08-04 05:35:39 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-13 17:43:37 +08:00
|
|
|
static void insertDeleteInstructions(MachineBasicBlock *MBB, MachineInstr &MI,
|
|
|
|
SmallVector<MachineInstr *, 16> InsInstrs,
|
|
|
|
SmallVector<MachineInstr *, 16> DelInstrs,
|
|
|
|
MachineTraceMetrics *Traces) {
|
|
|
|
for (auto *InstrPtr : InsInstrs)
|
|
|
|
MBB->insert((MachineBasicBlock::iterator)&MI, InstrPtr);
|
|
|
|
for (auto *InstrPtr : DelInstrs)
|
|
|
|
InstrPtr->eraseFromParentAndMarkDBGValuesForRemoval();
|
|
|
|
++NumInstCombined;
|
|
|
|
Traces->invalidate(MBB);
|
|
|
|
Traces->verifyAnalysis();
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:26:56 +08:00
|
|
|
/// Substitute a slow code sequence with a faster one by
|
2014-08-04 05:35:39 +08:00
|
|
|
/// evaluating instruction combining pattern.
|
|
|
|
/// The prototype of such a pattern is MUl + ADD -> MADD. Performs instruction
|
|
|
|
/// combining based on machine trace metrics. Only combine a sequence of
|
|
|
|
/// instructions when this neither lengthens the critical path nor increases
|
|
|
|
/// resource pressure. When optimizing for codesize always combine when the new
|
|
|
|
/// sequence is shorter.
|
|
|
|
bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) {
|
|
|
|
bool Changed = false;
|
|
|
|
DEBUG(dbgs() << "Combining MBB " << MBB->getName() << "\n");
|
|
|
|
|
|
|
|
auto BlockIter = MBB->begin();
|
2016-04-24 13:14:01 +08:00
|
|
|
// Check if the block is in a loop.
|
|
|
|
const MachineLoop *ML = MLI->getLoopFor(MBB);
|
2014-08-04 05:35:39 +08:00
|
|
|
|
|
|
|
while (BlockIter != MBB->end()) {
|
|
|
|
auto &MI = *BlockIter++;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "INSTR "; MI.dump(); dbgs() << "\n";);
|
2015-11-06 03:34:57 +08:00
|
|
|
SmallVector<MachineCombinerPattern, 16> Patterns;
|
2014-08-04 05:35:39 +08:00
|
|
|
// The motivating example is:
|
|
|
|
//
|
|
|
|
// MUL Other MUL_op1 MUL_op2 Other
|
|
|
|
// \ / \ | /
|
|
|
|
// ADD/SUB => MADD/MSUB
|
|
|
|
// (=Root) (=NewRoot)
|
|
|
|
|
|
|
|
// The DAGCombine code always replaced MUL + ADD/SUB by MADD. While this is
|
|
|
|
// usually beneficial for code size it unfortunately can hurt performance
|
|
|
|
// when the ADD is on the critical path, but the MUL is not. With the
|
|
|
|
// substitution the MUL becomes part of the critical path (in form of the
|
|
|
|
// MADD) and can lengthen it on architectures where the MADD latency is
|
|
|
|
// longer than the ADD latency.
|
|
|
|
//
|
|
|
|
// For each instruction we check if it can be the root of a combiner
|
|
|
|
// pattern. Then for each pattern the new code sequence in form of MI is
|
|
|
|
// generated and evaluated. When the efficiency criteria (don't lengthen
|
|
|
|
// critical path, don't use more resources) is met the new sequence gets
|
|
|
|
// hooked up into the basic block before the old sequence is removed.
|
|
|
|
//
|
|
|
|
// The algorithm does not try to evaluate all patterns and pick the best.
|
|
|
|
// This is only an artificial restriction though. In practice there is
|
2015-06-20 07:21:42 +08:00
|
|
|
// mostly one pattern, and getMachineCombinerPatterns() can order patterns
|
|
|
|
// based on an internal cost heuristic.
|
2014-08-04 05:35:39 +08:00
|
|
|
|
2015-11-11 04:09:02 +08:00
|
|
|
if (!TII->getMachineCombinerPatterns(MI, Patterns))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto P : Patterns) {
|
|
|
|
SmallVector<MachineInstr *, 16> InsInstrs;
|
|
|
|
SmallVector<MachineInstr *, 16> DelInstrs;
|
|
|
|
DenseMap<unsigned, unsigned> InstrIdxForVirtReg;
|
|
|
|
if (!MinInstr)
|
|
|
|
MinInstr = Traces->getEnsemble(MachineTraceMetrics::TS_MinInstrCount);
|
|
|
|
Traces->verifyAnalysis();
|
|
|
|
TII->genAlternativeCodeSequence(MI, P, InsInstrs, DelInstrs,
|
|
|
|
InstrIdxForVirtReg);
|
|
|
|
unsigned NewInstCount = InsInstrs.size();
|
|
|
|
unsigned OldInstCount = DelInstrs.size();
|
|
|
|
// Found pattern, but did not generate alternative sequence.
|
|
|
|
// This can happen e.g. when an immediate could not be materialized
|
|
|
|
// in a single instruction.
|
|
|
|
if (!NewInstCount)
|
|
|
|
continue;
|
|
|
|
|
2016-04-24 13:14:01 +08:00
|
|
|
bool SubstituteAlways = false;
|
|
|
|
if (ML && TII->isThroughputPattern(P))
|
|
|
|
SubstituteAlways = true;
|
|
|
|
|
2015-11-11 04:09:02 +08:00
|
|
|
// Substitute when we optimize for codesize and the new sequence has
|
|
|
|
// fewer instructions OR
|
|
|
|
// the new sequence neither lengthens the critical path nor increases
|
|
|
|
// resource pressure.
|
2017-02-13 17:43:37 +08:00
|
|
|
if (SubstituteAlways || doSubstitute(NewInstCount, OldInstCount)) {
|
|
|
|
insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, Traces);
|
2015-11-11 04:09:02 +08:00
|
|
|
// Eagerly stop after the first pattern fires.
|
2017-02-13 17:43:37 +08:00
|
|
|
Changed = true;
|
2015-11-11 04:09:02 +08:00
|
|
|
break;
|
|
|
|
} else {
|
2017-02-13 17:43:37 +08:00
|
|
|
// Calculating the trace metrics may be expensive,
|
|
|
|
// so only do this when necessary.
|
|
|
|
MachineTraceMetrics::Trace BlockTrace = MinInstr->getTrace(MBB);
|
|
|
|
if (improvesCriticalPathLen(MBB, &MI, BlockTrace, InsInstrs, DelInstrs,
|
|
|
|
InstrIdxForVirtReg, P) &&
|
|
|
|
preservesResourceLen(MBB, BlockTrace, InsInstrs, DelInstrs)) {
|
|
|
|
insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, Traces);
|
|
|
|
// Eagerly stop after the first pattern fires.
|
|
|
|
Changed = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-11 04:09:02 +08:00
|
|
|
// Cleanup instructions of the alternative code sequence. There is no
|
|
|
|
// use for them.
|
|
|
|
MachineFunction *MF = MBB->getParent();
|
|
|
|
for (auto *InstrPtr : InsInstrs)
|
|
|
|
MF->DeleteMachineInstr(InstrPtr);
|
2014-08-04 05:35:39 +08:00
|
|
|
}
|
2015-11-11 04:09:02 +08:00
|
|
|
InstrIdxForVirtReg.clear();
|
2014-08-04 05:35:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
|
2015-01-27 15:31:29 +08:00
|
|
|
const TargetSubtargetInfo &STI = MF.getSubtarget();
|
2014-08-05 05:25:23 +08:00
|
|
|
TII = STI.getInstrInfo();
|
|
|
|
TRI = STI.getRegisterInfo();
|
2014-08-04 05:35:39 +08:00
|
|
|
SchedModel = STI.getSchedModel();
|
2014-09-03 01:43:54 +08:00
|
|
|
TSchedModel.init(SchedModel, &STI, TII);
|
2014-08-04 05:35:39 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2016-04-24 13:14:01 +08:00
|
|
|
MLI = &getAnalysis<MachineLoopInfo>();
|
2014-08-04 05:35:39 +08:00
|
|
|
Traces = &getAnalysis<MachineTraceMetrics>();
|
2015-10-07 07:24:35 +08:00
|
|
|
MinInstr = nullptr;
|
2015-08-11 22:31:14 +08:00
|
|
|
OptSize = MF.getFunction()->optForSize();
|
2014-08-04 05:35:39 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << getPassName() << ": " << MF.getName() << '\n');
|
|
|
|
if (!TII->useMachineCombiner()) {
|
|
|
|
DEBUG(dbgs() << " Skipping pass: Target does not support machine combiner\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Try to combine instructions.
|
|
|
|
for (auto &MBB : MF)
|
|
|
|
Changed |= combineInstructions(&MBB);
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|