xref: /llvm-project/llvm/lib/CodeGen/MachineCombiner.cpp (revision 5bdd0beeee56dae90a2b60a0d81461cdae8e361c)
1 //===---- MachineCombiner.cpp - Instcombining on SSA form machine code ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The machine combiner pass uses machine trace metrics to ensure the combined
10 // instructions do not lengthen the critical path or the resource depth.
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/ProfileSummaryInfo.h"
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
17 #include "llvm/CodeGen/MachineCombinerPattern.h"
18 #include "llvm/CodeGen/MachineDominators.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineLoopInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/MachineSizeOpts.h"
24 #include "llvm/CodeGen/MachineTraceMetrics.h"
25 #include "llvm/CodeGen/RegisterClassInfo.h"
26 #include "llvm/CodeGen/TargetInstrInfo.h"
27 #include "llvm/CodeGen/TargetRegisterInfo.h"
28 #include "llvm/CodeGen/TargetSchedule.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "machine-combiner"
38 
39 STATISTIC(NumInstCombined, "Number of machineinst combined");
40 
41 static cl::opt<unsigned>
42 inc_threshold("machine-combiner-inc-threshold", cl::Hidden,
43               cl::desc("Incremental depth computation will be used for basic "
44                        "blocks with more instructions."), cl::init(500));
45 
46 static cl::opt<bool> dump_intrs("machine-combiner-dump-subst-intrs", cl::Hidden,
47                                 cl::desc("Dump all substituted intrs"),
48                                 cl::init(false));
49 
50 #ifdef EXPENSIVE_CHECKS
51 static cl::opt<bool> VerifyPatternOrder(
52     "machine-combiner-verify-pattern-order", cl::Hidden,
53     cl::desc(
54         "Verify that the generated patterns are ordered by increasing latency"),
55     cl::init(true));
56 #else
57 static cl::opt<bool> VerifyPatternOrder(
58     "machine-combiner-verify-pattern-order", cl::Hidden,
59     cl::desc(
60         "Verify that the generated patterns are ordered by increasing latency"),
61     cl::init(false));
62 #endif
63 
64 namespace {
65 class MachineCombiner : public MachineFunctionPass {
66   const TargetSubtargetInfo *STI;
67   const TargetInstrInfo *TII;
68   const TargetRegisterInfo *TRI;
69   MCSchedModel SchedModel;
70   MachineRegisterInfo *MRI;
71   MachineLoopInfo *MLI; // Current MachineLoopInfo
72   MachineTraceMetrics *Traces;
73   MachineTraceMetrics::Ensemble *TraceEnsemble;
74   MachineBlockFrequencyInfo *MBFI;
75   ProfileSummaryInfo *PSI;
76   RegisterClassInfo RegClassInfo;
77 
78   TargetSchedModel TSchedModel;
79 
80   /// True if optimizing for code size.
81   bool OptSize;
82 
83 public:
84   static char ID;
85   MachineCombiner() : MachineFunctionPass(ID) {
86     initializeMachineCombinerPass(*PassRegistry::getPassRegistry());
87   }
88   void getAnalysisUsage(AnalysisUsage &AU) const override;
89   bool runOnMachineFunction(MachineFunction &MF) override;
90   StringRef getPassName() const override { return "Machine InstCombiner"; }
91 
92 private:
93   bool combineInstructions(MachineBasicBlock *);
94   MachineInstr *getOperandDef(const MachineOperand &MO);
95   bool isTransientMI(const MachineInstr *MI);
96   unsigned getDepth(SmallVectorImpl<MachineInstr *> &InsInstrs,
97                     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
98                     MachineTraceMetrics::Trace BlockTrace);
99   unsigned getLatency(MachineInstr *Root, MachineInstr *NewRoot,
100                       MachineTraceMetrics::Trace BlockTrace);
101   bool
102   improvesCriticalPathLen(MachineBasicBlock *MBB, MachineInstr *Root,
103                           MachineTraceMetrics::Trace BlockTrace,
104                           SmallVectorImpl<MachineInstr *> &InsInstrs,
105                           SmallVectorImpl<MachineInstr *> &DelInstrs,
106                           DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
107                           MachineCombinerPattern Pattern, bool SlackIsAccurate);
108   bool reduceRegisterPressure(MachineInstr &Root, MachineBasicBlock *MBB,
109                               SmallVectorImpl<MachineInstr *> &InsInstrs,
110                               SmallVectorImpl<MachineInstr *> &DelInstrs,
111                               MachineCombinerPattern Pattern);
112   bool preservesResourceLen(MachineBasicBlock *MBB,
113                             MachineTraceMetrics::Trace BlockTrace,
114                             SmallVectorImpl<MachineInstr *> &InsInstrs,
115                             SmallVectorImpl<MachineInstr *> &DelInstrs);
116   void instr2instrSC(SmallVectorImpl<MachineInstr *> &Instrs,
117                      SmallVectorImpl<const MCSchedClassDesc *> &InstrsSC);
118   std::pair<unsigned, unsigned>
119   getLatenciesForInstrSequences(MachineInstr &MI,
120                                 SmallVectorImpl<MachineInstr *> &InsInstrs,
121                                 SmallVectorImpl<MachineInstr *> &DelInstrs,
122                                 MachineTraceMetrics::Trace BlockTrace);
123 
124   void verifyPatternOrder(MachineBasicBlock *MBB, MachineInstr &Root,
125                           SmallVector<MachineCombinerPattern, 16> &Patterns);
126 };
127 }
128 
129 char MachineCombiner::ID = 0;
130 char &llvm::MachineCombinerID = MachineCombiner::ID;
131 
132 INITIALIZE_PASS_BEGIN(MachineCombiner, DEBUG_TYPE,
133                       "Machine InstCombiner", false, false)
134 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
135 INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
136 INITIALIZE_PASS_END(MachineCombiner, DEBUG_TYPE, "Machine InstCombiner",
137                     false, false)
138 
139 void MachineCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
140   AU.setPreservesCFG();
141   AU.addPreserved<MachineDominatorTree>();
142   AU.addRequired<MachineLoopInfo>();
143   AU.addPreserved<MachineLoopInfo>();
144   AU.addRequired<MachineTraceMetrics>();
145   AU.addPreserved<MachineTraceMetrics>();
146   AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
147   AU.addRequired<ProfileSummaryInfoWrapperPass>();
148   MachineFunctionPass::getAnalysisUsage(AU);
149 }
150 
151 MachineInstr *MachineCombiner::getOperandDef(const MachineOperand &MO) {
152   MachineInstr *DefInstr = nullptr;
153   // We need a virtual register definition.
154   if (MO.isReg() && MO.getReg().isVirtual())
155     DefInstr = MRI->getUniqueVRegDef(MO.getReg());
156   // PHI's have no depth etc.
157   if (DefInstr && DefInstr->isPHI())
158     DefInstr = nullptr;
159   return DefInstr;
160 }
161 
162 /// Return true if MI is unlikely to generate an actual target instruction.
163 bool MachineCombiner::isTransientMI(const MachineInstr *MI) {
164   if (!MI->isCopy())
165     return MI->isTransient();
166 
167   // If MI is a COPY, check if its src and dst registers can be coalesced.
168   Register Dst = MI->getOperand(0).getReg();
169   Register Src = MI->getOperand(1).getReg();
170 
171   if (!MI->isFullCopy()) {
172     // If src RC contains super registers of dst RC, it can also be coalesced.
173     if (MI->getOperand(0).getSubReg() || Src.isPhysical() || Dst.isPhysical())
174       return false;
175 
176     auto SrcSub = MI->getOperand(1).getSubReg();
177     auto SrcRC = MRI->getRegClass(Src);
178     auto DstRC = MRI->getRegClass(Dst);
179     return TRI->getMatchingSuperRegClass(SrcRC, DstRC, SrcSub) != nullptr;
180   }
181 
182   if (Src.isPhysical() && Dst.isPhysical())
183     return Src == Dst;
184 
185   if (Src.isVirtual() && Dst.isVirtual()) {
186     auto SrcRC = MRI->getRegClass(Src);
187     auto DstRC = MRI->getRegClass(Dst);
188     return SrcRC->hasSuperClassEq(DstRC) || SrcRC->hasSubClassEq(DstRC);
189   }
190 
191   if (Src.isVirtual())
192     std::swap(Src, Dst);
193 
194   // Now Src is physical register, Dst is virtual register.
195   auto DstRC = MRI->getRegClass(Dst);
196   return DstRC->contains(Src);
197 }
198 
199 /// Computes depth of instructions in vector \InsInstr.
200 ///
201 /// \param InsInstrs is a vector of machine instructions
202 /// \param InstrIdxForVirtReg is a dense map of virtual register to index
203 /// of defining machine instruction in \p InsInstrs
204 /// \param BlockTrace is a trace of machine instructions
205 ///
206 /// \returns Depth of last instruction in \InsInstrs ("NewRoot")
207 unsigned
208 MachineCombiner::getDepth(SmallVectorImpl<MachineInstr *> &InsInstrs,
209                           DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
210                           MachineTraceMetrics::Trace BlockTrace) {
211   SmallVector<unsigned, 16> InstrDepth;
212   // For each instruction in the new sequence compute the depth based on the
213   // operands. Use the trace information when possible. For new operands which
214   // are tracked in the InstrIdxForVirtReg map depth is looked up in InstrDepth
215   for (auto *InstrPtr : InsInstrs) { // for each Use
216     unsigned IDepth = 0;
217     for (const MachineOperand &MO : InstrPtr->operands()) {
218       // Check for virtual register operand.
219       if (!(MO.isReg() && MO.getReg().isVirtual()))
220         continue;
221       if (!MO.isUse())
222         continue;
223       unsigned DepthOp = 0;
224       unsigned LatencyOp = 0;
225       DenseMap<unsigned, unsigned>::iterator II =
226           InstrIdxForVirtReg.find(MO.getReg());
227       if (II != InstrIdxForVirtReg.end()) {
228         // Operand is new virtual register not in trace
229         assert(II->second < InstrDepth.size() && "Bad Index");
230         MachineInstr *DefInstr = InsInstrs[II->second];
231         assert(DefInstr &&
232                "There must be a definition for a new virtual register");
233         DepthOp = InstrDepth[II->second];
234         int DefIdx = DefInstr->findRegisterDefOperandIdx(MO.getReg());
235         int UseIdx = InstrPtr->findRegisterUseOperandIdx(MO.getReg());
236         LatencyOp = TSchedModel.computeOperandLatency(DefInstr, DefIdx,
237                                                       InstrPtr, UseIdx);
238       } else {
239         MachineInstr *DefInstr = getOperandDef(MO);
240         if (DefInstr) {
241           DepthOp = BlockTrace.getInstrCycles(*DefInstr).Depth;
242           if (!isTransientMI(DefInstr))
243             LatencyOp = TSchedModel.computeOperandLatency(
244                 DefInstr, DefInstr->findRegisterDefOperandIdx(MO.getReg()),
245                 InstrPtr, InstrPtr->findRegisterUseOperandIdx(MO.getReg()));
246         }
247       }
248       IDepth = std::max(IDepth, DepthOp + LatencyOp);
249     }
250     InstrDepth.push_back(IDepth);
251   }
252   unsigned NewRootIdx = InsInstrs.size() - 1;
253   return InstrDepth[NewRootIdx];
254 }
255 
256 /// Computes instruction latency as max of latency of defined operands.
257 ///
258 /// \param Root is a machine instruction that could be replaced by NewRoot.
259 /// It is used to compute a more accurate latency information for NewRoot in
260 /// case there is a dependent instruction in the same trace (\p BlockTrace)
261 /// \param NewRoot is the instruction for which the latency is computed
262 /// \param BlockTrace is a trace of machine instructions
263 ///
264 /// \returns Latency of \p NewRoot
265 unsigned MachineCombiner::getLatency(MachineInstr *Root, MachineInstr *NewRoot,
266                                      MachineTraceMetrics::Trace BlockTrace) {
267   // Check each definition in NewRoot and compute the latency
268   unsigned NewRootLatency = 0;
269 
270   for (const MachineOperand &MO : NewRoot->operands()) {
271     // Check for virtual register operand.
272     if (!(MO.isReg() && MO.getReg().isVirtual()))
273       continue;
274     if (!MO.isDef())
275       continue;
276     // Get the first instruction that uses MO
277     MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(MO.getReg());
278     RI++;
279     if (RI == MRI->reg_end())
280       continue;
281     MachineInstr *UseMO = RI->getParent();
282     unsigned LatencyOp = 0;
283     if (UseMO && BlockTrace.isDepInTrace(*Root, *UseMO)) {
284       LatencyOp = TSchedModel.computeOperandLatency(
285           NewRoot, NewRoot->findRegisterDefOperandIdx(MO.getReg()), UseMO,
286           UseMO->findRegisterUseOperandIdx(MO.getReg()));
287     } else {
288       LatencyOp = TSchedModel.computeInstrLatency(NewRoot);
289     }
290     NewRootLatency = std::max(NewRootLatency, LatencyOp);
291   }
292   return NewRootLatency;
293 }
294 
295 /// The combiner's goal may differ based on which pattern it is attempting
296 /// to optimize.
297 enum class CombinerObjective {
298   MustReduceDepth,            // The data dependency chain must be improved.
299   MustReduceRegisterPressure, // The register pressure must be reduced.
300   Default                     // The critical path must not be lengthened.
301 };
302 
303 static CombinerObjective getCombinerObjective(MachineCombinerPattern P) {
304   // TODO: If C++ ever gets a real enum class, make this part of the
305   // MachineCombinerPattern class.
306   switch (P) {
307   case MachineCombinerPattern::REASSOC_AX_BY:
308   case MachineCombinerPattern::REASSOC_AX_YB:
309   case MachineCombinerPattern::REASSOC_XA_BY:
310   case MachineCombinerPattern::REASSOC_XA_YB:
311   case MachineCombinerPattern::REASSOC_XY_AMM_BMM:
312   case MachineCombinerPattern::REASSOC_XMM_AMM_BMM:
313   case MachineCombinerPattern::SUBADD_OP1:
314   case MachineCombinerPattern::SUBADD_OP2:
315   case MachineCombinerPattern::FMADD_AX:
316   case MachineCombinerPattern::FMADD_XA:
317   case MachineCombinerPattern::FMSUB:
318   case MachineCombinerPattern::FNMSUB:
319     return CombinerObjective::MustReduceDepth;
320   case MachineCombinerPattern::REASSOC_XY_BCA:
321   case MachineCombinerPattern::REASSOC_XY_BAC:
322     return CombinerObjective::MustReduceRegisterPressure;
323   default:
324     return CombinerObjective::Default;
325   }
326 }
327 
328 /// Estimate the latency of the new and original instruction sequence by summing
329 /// up the latencies of the inserted and deleted instructions. This assumes
330 /// that the inserted and deleted instructions are dependent instruction chains,
331 /// which might not hold in all cases.
332 std::pair<unsigned, unsigned> MachineCombiner::getLatenciesForInstrSequences(
333     MachineInstr &MI, SmallVectorImpl<MachineInstr *> &InsInstrs,
334     SmallVectorImpl<MachineInstr *> &DelInstrs,
335     MachineTraceMetrics::Trace BlockTrace) {
336   assert(!InsInstrs.empty() && "Only support sequences that insert instrs.");
337   unsigned NewRootLatency = 0;
338   // NewRoot is the last instruction in the \p InsInstrs vector.
339   MachineInstr *NewRoot = InsInstrs.back();
340   for (unsigned i = 0; i < InsInstrs.size() - 1; i++)
341     NewRootLatency += TSchedModel.computeInstrLatency(InsInstrs[i]);
342   NewRootLatency += getLatency(&MI, NewRoot, BlockTrace);
343 
344   unsigned RootLatency = 0;
345   for (auto *I : DelInstrs)
346     RootLatency += TSchedModel.computeInstrLatency(I);
347 
348   return {NewRootLatency, RootLatency};
349 }
350 
351 bool MachineCombiner::reduceRegisterPressure(
352     MachineInstr &Root, MachineBasicBlock *MBB,
353     SmallVectorImpl<MachineInstr *> &InsInstrs,
354     SmallVectorImpl<MachineInstr *> &DelInstrs,
355     MachineCombinerPattern Pattern) {
356   // FIXME: for now, we don't do any check for the register pressure patterns.
357   // We treat them as always profitable. But we can do better if we make
358   // RegPressureTracker class be aware of TIE attribute. Then we can get an
359   // accurate compare of register pressure with DelInstrs or InsInstrs.
360   return true;
361 }
362 
363 /// The DAGCombine code sequence ends in MI (Machine Instruction) Root.
364 /// The new code sequence ends in MI NewRoot. A necessary condition for the new
365 /// sequence to replace the old sequence is that it cannot lengthen the critical
366 /// path. The definition of "improve" may be restricted by specifying that the
367 /// new path improves the data dependency chain (MustReduceDepth).
368 bool MachineCombiner::improvesCriticalPathLen(
369     MachineBasicBlock *MBB, MachineInstr *Root,
370     MachineTraceMetrics::Trace BlockTrace,
371     SmallVectorImpl<MachineInstr *> &InsInstrs,
372     SmallVectorImpl<MachineInstr *> &DelInstrs,
373     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
374     MachineCombinerPattern Pattern,
375     bool SlackIsAccurate) {
376   // Get depth and latency of NewRoot and Root.
377   unsigned NewRootDepth = getDepth(InsInstrs, InstrIdxForVirtReg, BlockTrace);
378   unsigned RootDepth = BlockTrace.getInstrCycles(*Root).Depth;
379 
380   LLVM_DEBUG(dbgs() << "  Dependence data for " << *Root << "\tNewRootDepth: "
381                     << NewRootDepth << "\tRootDepth: " << RootDepth);
382 
383   // For a transform such as reassociation, the cost equation is
384   // conservatively calculated so that we must improve the depth (data
385   // dependency cycles) in the critical path to proceed with the transform.
386   // Being conservative also protects against inaccuracies in the underlying
387   // machine trace metrics and CPU models.
388   if (getCombinerObjective(Pattern) == CombinerObjective::MustReduceDepth) {
389     LLVM_DEBUG(dbgs() << "\tIt MustReduceDepth ");
390     LLVM_DEBUG(NewRootDepth < RootDepth
391                    ? dbgs() << "\t  and it does it\n"
392                    : dbgs() << "\t  but it does NOT do it\n");
393     return NewRootDepth < RootDepth;
394   }
395 
396   // A more flexible cost calculation for the critical path includes the slack
397   // of the original code sequence. This may allow the transform to proceed
398   // even if the instruction depths (data dependency cycles) become worse.
399 
400   // Account for the latency of the inserted and deleted instructions by
401   unsigned NewRootLatency, RootLatency;
402   std::tie(NewRootLatency, RootLatency) =
403       getLatenciesForInstrSequences(*Root, InsInstrs, DelInstrs, BlockTrace);
404 
405   unsigned RootSlack = BlockTrace.getInstrSlack(*Root);
406   unsigned NewCycleCount = NewRootDepth + NewRootLatency;
407   unsigned OldCycleCount =
408       RootDepth + RootLatency + (SlackIsAccurate ? RootSlack : 0);
409   LLVM_DEBUG(dbgs() << "\n\tNewRootLatency: " << NewRootLatency
410                     << "\tRootLatency: " << RootLatency << "\n\tRootSlack: "
411                     << RootSlack << " SlackIsAccurate=" << SlackIsAccurate
412                     << "\n\tNewRootDepth + NewRootLatency = " << NewCycleCount
413                     << "\n\tRootDepth + RootLatency + RootSlack = "
414                     << OldCycleCount;);
415   LLVM_DEBUG(NewCycleCount <= OldCycleCount
416                  ? dbgs() << "\n\t  It IMPROVES PathLen because"
417                  : dbgs() << "\n\t  It DOES NOT improve PathLen because");
418   LLVM_DEBUG(dbgs() << "\n\t\tNewCycleCount = " << NewCycleCount
419                     << ", OldCycleCount = " << OldCycleCount << "\n");
420 
421   return NewCycleCount <= OldCycleCount;
422 }
423 
424 /// helper routine to convert instructions into SC
425 void MachineCombiner::instr2instrSC(
426     SmallVectorImpl<MachineInstr *> &Instrs,
427     SmallVectorImpl<const MCSchedClassDesc *> &InstrsSC) {
428   for (auto *InstrPtr : Instrs) {
429     unsigned Opc = InstrPtr->getOpcode();
430     unsigned Idx = TII->get(Opc).getSchedClass();
431     const MCSchedClassDesc *SC = SchedModel.getSchedClassDesc(Idx);
432     InstrsSC.push_back(SC);
433   }
434 }
435 
436 /// True when the new instructions do not increase resource length
437 bool MachineCombiner::preservesResourceLen(
438     MachineBasicBlock *MBB, MachineTraceMetrics::Trace BlockTrace,
439     SmallVectorImpl<MachineInstr *> &InsInstrs,
440     SmallVectorImpl<MachineInstr *> &DelInstrs) {
441   if (!TSchedModel.hasInstrSchedModel())
442     return true;
443 
444   // Compute current resource length
445 
446   //ArrayRef<const MachineBasicBlock *> MBBarr(MBB);
447   SmallVector <const MachineBasicBlock *, 1> MBBarr;
448   MBBarr.push_back(MBB);
449   unsigned ResLenBeforeCombine = BlockTrace.getResourceLength(MBBarr);
450 
451   // Deal with SC rather than Instructions.
452   SmallVector<const MCSchedClassDesc *, 16> InsInstrsSC;
453   SmallVector<const MCSchedClassDesc *, 16> DelInstrsSC;
454 
455   instr2instrSC(InsInstrs, InsInstrsSC);
456   instr2instrSC(DelInstrs, DelInstrsSC);
457 
458   ArrayRef<const MCSchedClassDesc *> MSCInsArr{InsInstrsSC};
459   ArrayRef<const MCSchedClassDesc *> MSCDelArr{DelInstrsSC};
460 
461   // Compute new resource length.
462   unsigned ResLenAfterCombine =
463       BlockTrace.getResourceLength(MBBarr, MSCInsArr, MSCDelArr);
464 
465   LLVM_DEBUG(dbgs() << "\t\tResource length before replacement: "
466                     << ResLenBeforeCombine
467                     << " and after: " << ResLenAfterCombine << "\n";);
468   LLVM_DEBUG(
469       ResLenAfterCombine <=
470       ResLenBeforeCombine + TII->getExtendResourceLenLimit()
471           ? dbgs() << "\t\t  As result it IMPROVES/PRESERVES Resource Length\n"
472           : dbgs() << "\t\t  As result it DOES NOT improve/preserve Resource "
473                       "Length\n");
474 
475   return ResLenAfterCombine <=
476          ResLenBeforeCombine + TII->getExtendResourceLenLimit();
477 }
478 
479 /// Inserts InsInstrs and deletes DelInstrs. Incrementally updates instruction
480 /// depths if requested.
481 ///
482 /// \param MBB basic block to insert instructions in
483 /// \param MI current machine instruction
484 /// \param InsInstrs new instructions to insert in \p MBB
485 /// \param DelInstrs instruction to delete from \p MBB
486 /// \param TraceEnsemble is a pointer to the machine trace information
487 /// \param RegUnits set of live registers, needed to compute instruction depths
488 /// \param TII is target instruction info, used to call target hook
489 /// \param Pattern is used to call target hook finalizeInsInstrs
490 /// \param IncrementalUpdate if true, compute instruction depths incrementally,
491 ///                          otherwise invalidate the trace
492 static void insertDeleteInstructions(
493     MachineBasicBlock *MBB, MachineInstr &MI,
494     SmallVector<MachineInstr *, 16> InsInstrs,
495     SmallVector<MachineInstr *, 16> DelInstrs,
496     MachineTraceMetrics::Ensemble *TraceEnsemble,
497     SparseSet<LiveRegUnit> &RegUnits, const TargetInstrInfo *TII,
498     MachineCombinerPattern Pattern, bool IncrementalUpdate) {
499   // If we want to fix up some placeholder for some target, do it now.
500   // We need this because in genAlternativeCodeSequence, we have not decided the
501   // better pattern InsInstrs or DelInstrs, so we don't want generate some
502   // sideeffect to the function. For example we need to delay the constant pool
503   // entry creation here after InsInstrs is selected as better pattern.
504   // Otherwise the constant pool entry created for InsInstrs will not be deleted
505   // even if InsInstrs is not the better pattern.
506   TII->finalizeInsInstrs(MI, Pattern, InsInstrs);
507 
508   for (auto *InstrPtr : InsInstrs)
509     MBB->insert((MachineBasicBlock::iterator)&MI, InstrPtr);
510 
511   for (auto *InstrPtr : DelInstrs) {
512     InstrPtr->eraseFromParent();
513     // Erase all LiveRegs defined by the removed instruction
514     for (auto *I = RegUnits.begin(); I != RegUnits.end();) {
515       if (I->MI == InstrPtr)
516         I = RegUnits.erase(I);
517       else
518         I++;
519     }
520   }
521 
522   if (IncrementalUpdate)
523     for (auto *InstrPtr : InsInstrs)
524       TraceEnsemble->updateDepth(MBB, *InstrPtr, RegUnits);
525   else
526     TraceEnsemble->invalidate(MBB);
527 
528   NumInstCombined++;
529 }
530 
531 // Check that the difference between original and new latency is decreasing for
532 // later patterns. This helps to discover sub-optimal pattern orderings.
533 void MachineCombiner::verifyPatternOrder(
534     MachineBasicBlock *MBB, MachineInstr &Root,
535     SmallVector<MachineCombinerPattern, 16> &Patterns) {
536   long PrevLatencyDiff = std::numeric_limits<long>::max();
537   (void)PrevLatencyDiff; // Variable is used in assert only.
538   for (auto P : Patterns) {
539     SmallVector<MachineInstr *, 16> InsInstrs;
540     SmallVector<MachineInstr *, 16> DelInstrs;
541     DenseMap<unsigned, unsigned> InstrIdxForVirtReg;
542     TII->genAlternativeCodeSequence(Root, P, InsInstrs, DelInstrs,
543                                     InstrIdxForVirtReg);
544     // Found pattern, but did not generate alternative sequence.
545     // This can happen e.g. when an immediate could not be materialized
546     // in a single instruction.
547     if (InsInstrs.empty() || !TSchedModel.hasInstrSchedModelOrItineraries())
548       continue;
549 
550     unsigned NewRootLatency, RootLatency;
551     std::tie(NewRootLatency, RootLatency) = getLatenciesForInstrSequences(
552         Root, InsInstrs, DelInstrs, TraceEnsemble->getTrace(MBB));
553     long CurrentLatencyDiff = ((long)RootLatency) - ((long)NewRootLatency);
554     assert(CurrentLatencyDiff <= PrevLatencyDiff &&
555            "Current pattern is better than previous pattern.");
556     PrevLatencyDiff = CurrentLatencyDiff;
557   }
558 }
559 
560 /// Substitute a slow code sequence with a faster one by
561 /// evaluating instruction combining pattern.
562 /// The prototype of such a pattern is MUl + ADD -> MADD. Performs instruction
563 /// combining based on machine trace metrics. Only combine a sequence of
564 /// instructions  when this neither lengthens the critical path nor increases
565 /// resource pressure. When optimizing for codesize always combine when the new
566 /// sequence is shorter.
567 bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) {
568   bool Changed = false;
569   LLVM_DEBUG(dbgs() << "Combining MBB " << MBB->getName() << "\n");
570 
571   bool IncrementalUpdate = false;
572   auto BlockIter = MBB->begin();
573   decltype(BlockIter) LastUpdate;
574   // Check if the block is in a loop.
575   const MachineLoop *ML = MLI->getLoopFor(MBB);
576   if (!TraceEnsemble)
577     TraceEnsemble = Traces->getEnsemble(MachineTraceStrategy::TS_MinInstrCount);
578 
579   SparseSet<LiveRegUnit> RegUnits;
580   RegUnits.setUniverse(TRI->getNumRegUnits());
581 
582   bool OptForSize = OptSize || llvm::shouldOptimizeForSize(MBB, PSI, MBFI);
583 
584   bool DoRegPressureReduce =
585       TII->shouldReduceRegisterPressure(MBB, &RegClassInfo);
586 
587   while (BlockIter != MBB->end()) {
588     auto &MI = *BlockIter++;
589     SmallVector<MachineCombinerPattern, 16> Patterns;
590     // The motivating example is:
591     //
592     //     MUL  Other        MUL_op1 MUL_op2  Other
593     //      \    /               \      |    /
594     //      ADD/SUB      =>        MADD/MSUB
595     //      (=Root)                (=NewRoot)
596 
597     // The DAGCombine code always replaced MUL + ADD/SUB by MADD. While this is
598     // usually beneficial for code size it unfortunately can hurt performance
599     // when the ADD is on the critical path, but the MUL is not. With the
600     // substitution the MUL becomes part of the critical path (in form of the
601     // MADD) and can lengthen it on architectures where the MADD latency is
602     // longer than the ADD latency.
603     //
604     // For each instruction we check if it can be the root of a combiner
605     // pattern. Then for each pattern the new code sequence in form of MI is
606     // generated and evaluated. When the efficiency criteria (don't lengthen
607     // critical path, don't use more resources) is met the new sequence gets
608     // hooked up into the basic block before the old sequence is removed.
609     //
610     // The algorithm does not try to evaluate all patterns and pick the best.
611     // This is only an artificial restriction though. In practice there is
612     // mostly one pattern, and getMachineCombinerPatterns() can order patterns
613     // based on an internal cost heuristic. If
614     // machine-combiner-verify-pattern-order is enabled, all patterns are
615     // checked to ensure later patterns do not provide better latency savings.
616 
617     if (!TII->getMachineCombinerPatterns(MI, Patterns, DoRegPressureReduce))
618       continue;
619 
620     if (VerifyPatternOrder)
621       verifyPatternOrder(MBB, MI, Patterns);
622 
623     for (const auto P : Patterns) {
624       SmallVector<MachineInstr *, 16> InsInstrs;
625       SmallVector<MachineInstr *, 16> DelInstrs;
626       DenseMap<unsigned, unsigned> InstrIdxForVirtReg;
627       TII->genAlternativeCodeSequence(MI, P, InsInstrs, DelInstrs,
628                                       InstrIdxForVirtReg);
629       // Found pattern, but did not generate alternative sequence.
630       // This can happen e.g. when an immediate could not be materialized
631       // in a single instruction.
632       if (InsInstrs.empty())
633         continue;
634 
635       LLVM_DEBUG(if (dump_intrs) {
636         dbgs() << "\tFor the Pattern (" << (int)P
637                << ") these instructions could be removed\n";
638         for (auto const *InstrPtr : DelInstrs)
639           InstrPtr->print(dbgs(), /*IsStandalone*/false, /*SkipOpers*/false,
640                           /*SkipDebugLoc*/false, /*AddNewLine*/true, TII);
641         dbgs() << "\tThese instructions could replace the removed ones\n";
642         for (auto const *InstrPtr : InsInstrs)
643           InstrPtr->print(dbgs(), /*IsStandalone*/false, /*SkipOpers*/false,
644                           /*SkipDebugLoc*/false, /*AddNewLine*/true, TII);
645       });
646 
647       if (IncrementalUpdate && LastUpdate != BlockIter) {
648         // Update depths since the last incremental update.
649         TraceEnsemble->updateDepths(LastUpdate, BlockIter, RegUnits);
650         LastUpdate = BlockIter;
651       }
652 
653       if (DoRegPressureReduce &&
654           getCombinerObjective(P) ==
655               CombinerObjective::MustReduceRegisterPressure) {
656         if (MBB->size() > inc_threshold) {
657           // Use incremental depth updates for basic blocks above threshold
658           IncrementalUpdate = true;
659           LastUpdate = BlockIter;
660         }
661         if (reduceRegisterPressure(MI, MBB, InsInstrs, DelInstrs, P)) {
662           // Replace DelInstrs with InsInstrs.
663           insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, TraceEnsemble,
664                                    RegUnits, TII, P, IncrementalUpdate);
665           Changed |= true;
666 
667           // Go back to previous instruction as it may have ILP reassociation
668           // opportunity.
669           BlockIter--;
670           break;
671         }
672       }
673 
674       if (ML && TII->isThroughputPattern(P)) {
675         LLVM_DEBUG(dbgs() << "\t Replacing due to throughput pattern in loop\n");
676         insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, TraceEnsemble,
677                                  RegUnits, TII, P, IncrementalUpdate);
678         // Eagerly stop after the first pattern fires.
679         Changed = true;
680         break;
681       } else if (OptForSize && InsInstrs.size() < DelInstrs.size()) {
682         LLVM_DEBUG(dbgs() << "\t Replacing due to OptForSize ("
683                           << InsInstrs.size() << " < "
684                           << DelInstrs.size() << ")\n");
685         insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, TraceEnsemble,
686                                  RegUnits, TII, P, IncrementalUpdate);
687         // Eagerly stop after the first pattern fires.
688         Changed = true;
689         break;
690       } else {
691         // For big basic blocks, we only compute the full trace the first time
692         // we hit this. We do not invalidate the trace, but instead update the
693         // instruction depths incrementally.
694         // NOTE: Only the instruction depths up to MI are accurate. All other
695         // trace information is not updated.
696         MachineTraceMetrics::Trace BlockTrace = TraceEnsemble->getTrace(MBB);
697         Traces->verifyAnalysis();
698         if (improvesCriticalPathLen(MBB, &MI, BlockTrace, InsInstrs, DelInstrs,
699                                     InstrIdxForVirtReg, P,
700                                     !IncrementalUpdate) &&
701             preservesResourceLen(MBB, BlockTrace, InsInstrs, DelInstrs)) {
702           if (MBB->size() > inc_threshold) {
703             // Use incremental depth updates for basic blocks above treshold
704             IncrementalUpdate = true;
705             LastUpdate = BlockIter;
706           }
707 
708           insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, TraceEnsemble,
709                                    RegUnits, TII, P, IncrementalUpdate);
710 
711           // Eagerly stop after the first pattern fires.
712           Changed = true;
713           break;
714         }
715         // Cleanup instructions of the alternative code sequence. There is no
716         // use for them.
717         MachineFunction *MF = MBB->getParent();
718         for (auto *InstrPtr : InsInstrs)
719           MF->deleteMachineInstr(InstrPtr);
720       }
721       InstrIdxForVirtReg.clear();
722     }
723   }
724 
725   if (Changed && IncrementalUpdate)
726     Traces->invalidate(MBB);
727   return Changed;
728 }
729 
730 bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
731   STI = &MF.getSubtarget();
732   TII = STI->getInstrInfo();
733   TRI = STI->getRegisterInfo();
734   SchedModel = STI->getSchedModel();
735   TSchedModel.init(STI);
736   MRI = &MF.getRegInfo();
737   MLI = &getAnalysis<MachineLoopInfo>();
738   Traces = &getAnalysis<MachineTraceMetrics>();
739   PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
740   MBFI = (PSI && PSI->hasProfileSummary()) ?
741          &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
742          nullptr;
743   TraceEnsemble = nullptr;
744   OptSize = MF.getFunction().hasOptSize();
745   RegClassInfo.runOnMachineFunction(MF);
746 
747   LLVM_DEBUG(dbgs() << getPassName() << ": " << MF.getName() << '\n');
748   if (!TII->useMachineCombiner()) {
749     LLVM_DEBUG(
750         dbgs()
751         << "  Skipping pass: Target does not support machine combiner\n");
752     return false;
753   }
754 
755   bool Changed = false;
756 
757   // Try to combine instructions.
758   for (auto &MBB : MF)
759     Changed |= combineInstructions(&MBB);
760 
761   return Changed;
762 }
763