1 //===---- MachineCombiner.cpp - Instcombining on SSA form machine code ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The machine combiner pass uses machine trace metrics to ensure the combined
11 // instructions do not lengthen the critical path or the resource depth.
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/CodeGen/MachineDominators.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineLoopInfo.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/MachineTraceMetrics.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/CodeGen/TargetInstrInfo.h"
25 #include "llvm/CodeGen/TargetRegisterInfo.h"
26 #include "llvm/CodeGen/TargetSchedule.h"
27 #include "llvm/CodeGen/TargetSubtargetInfo.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
31 
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "machine-combiner"
35 
36 STATISTIC(NumInstCombined, "Number of machineinst combined");
37 
38 static cl::opt<unsigned>
39 inc_threshold("machine-combiner-inc-threshold", cl::Hidden,
40               cl::desc("Incremental depth computation will be used for basic "
41                        "blocks with more instructions."), cl::init(500));
42 
43 namespace {
44 class MachineCombiner : public MachineFunctionPass {
45   const TargetInstrInfo *TII;
46   const TargetRegisterInfo *TRI;
47   MCSchedModel SchedModel;
48   MachineRegisterInfo *MRI;
49   MachineLoopInfo *MLI; // Current MachineLoopInfo
50   MachineTraceMetrics *Traces;
51   MachineTraceMetrics::Ensemble *MinInstr;
52 
53   TargetSchedModel TSchedModel;
54 
55   /// True if optimizing for code size.
56   bool OptSize;
57 
58 public:
59   static char ID;
60   MachineCombiner() : MachineFunctionPass(ID) {
61     initializeMachineCombinerPass(*PassRegistry::getPassRegistry());
62   }
63   void getAnalysisUsage(AnalysisUsage &AU) const override;
64   bool runOnMachineFunction(MachineFunction &MF) override;
65   StringRef getPassName() const override { return "Machine InstCombiner"; }
66 
67 private:
68   bool doSubstitute(unsigned NewSize, unsigned OldSize);
69   bool combineInstructions(MachineBasicBlock *);
70   MachineInstr *getOperandDef(const MachineOperand &MO);
71   unsigned getDepth(SmallVectorImpl<MachineInstr *> &InsInstrs,
72                     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
73                     MachineTraceMetrics::Trace BlockTrace);
74   unsigned getLatency(MachineInstr *Root, MachineInstr *NewRoot,
75                       MachineTraceMetrics::Trace BlockTrace);
76   bool
77   improvesCriticalPathLen(MachineBasicBlock *MBB, MachineInstr *Root,
78                           MachineTraceMetrics::Trace BlockTrace,
79                           SmallVectorImpl<MachineInstr *> &InsInstrs,
80                           SmallVectorImpl<MachineInstr *> &DelInstrs,
81                           DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
82                           MachineCombinerPattern Pattern, bool SlackIsAccurate);
83   bool preservesResourceLen(MachineBasicBlock *MBB,
84                             MachineTraceMetrics::Trace BlockTrace,
85                             SmallVectorImpl<MachineInstr *> &InsInstrs,
86                             SmallVectorImpl<MachineInstr *> &DelInstrs);
87   void instr2instrSC(SmallVectorImpl<MachineInstr *> &Instrs,
88                      SmallVectorImpl<const MCSchedClassDesc *> &InstrsSC);
89 };
90 }
91 
92 char MachineCombiner::ID = 0;
93 char &llvm::MachineCombinerID = MachineCombiner::ID;
94 
95 INITIALIZE_PASS_BEGIN(MachineCombiner, DEBUG_TYPE,
96                       "Machine InstCombiner", false, false)
97 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
98 INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
99 INITIALIZE_PASS_END(MachineCombiner, DEBUG_TYPE, "Machine InstCombiner",
100                     false, false)
101 
102 void MachineCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
103   AU.setPreservesCFG();
104   AU.addPreserved<MachineDominatorTree>();
105   AU.addRequired<MachineLoopInfo>();
106   AU.addPreserved<MachineLoopInfo>();
107   AU.addRequired<MachineTraceMetrics>();
108   AU.addPreserved<MachineTraceMetrics>();
109   MachineFunctionPass::getAnalysisUsage(AU);
110 }
111 
112 MachineInstr *MachineCombiner::getOperandDef(const MachineOperand &MO) {
113   MachineInstr *DefInstr = nullptr;
114   // We need a virtual register definition.
115   if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg()))
116     DefInstr = MRI->getUniqueVRegDef(MO.getReg());
117   // PHI's have no depth etc.
118   if (DefInstr && DefInstr->isPHI())
119     DefInstr = nullptr;
120   return DefInstr;
121 }
122 
123 /// Computes depth of instructions in vector \InsInstr.
124 ///
125 /// \param InsInstrs is a vector of machine instructions
126 /// \param InstrIdxForVirtReg is a dense map of virtual register to index
127 /// of defining machine instruction in \p InsInstrs
128 /// \param BlockTrace is a trace of machine instructions
129 ///
130 /// \returns Depth of last instruction in \InsInstrs ("NewRoot")
131 unsigned
132 MachineCombiner::getDepth(SmallVectorImpl<MachineInstr *> &InsInstrs,
133                           DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
134                           MachineTraceMetrics::Trace BlockTrace) {
135   SmallVector<unsigned, 16> InstrDepth;
136   assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
137          "Missing machine model\n");
138 
139   // For each instruction in the new sequence compute the depth based on the
140   // operands. Use the trace information when possible. For new operands which
141   // are tracked in the InstrIdxForVirtReg map depth is looked up in InstrDepth
142   for (auto *InstrPtr : InsInstrs) { // for each Use
143     unsigned IDepth = 0;
144     DEBUG(dbgs() << "NEW INSTR ";
145           InstrPtr->print(dbgs(), TII);
146           dbgs() << "\n";);
147     for (const MachineOperand &MO : InstrPtr->operands()) {
148       // Check for virtual register operand.
149       if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
150         continue;
151       if (!MO.isUse())
152         continue;
153       unsigned DepthOp = 0;
154       unsigned LatencyOp = 0;
155       DenseMap<unsigned, unsigned>::iterator II =
156           InstrIdxForVirtReg.find(MO.getReg());
157       if (II != InstrIdxForVirtReg.end()) {
158         // Operand is new virtual register not in trace
159         assert(II->second < InstrDepth.size() && "Bad Index");
160         MachineInstr *DefInstr = InsInstrs[II->second];
161         assert(DefInstr &&
162                "There must be a definition for a new virtual register");
163         DepthOp = InstrDepth[II->second];
164         int DefIdx = DefInstr->findRegisterDefOperandIdx(MO.getReg());
165         int UseIdx = InstrPtr->findRegisterUseOperandIdx(MO.getReg());
166         LatencyOp = TSchedModel.computeOperandLatency(DefInstr, DefIdx,
167                                                       InstrPtr, UseIdx);
168       } else {
169         MachineInstr *DefInstr = getOperandDef(MO);
170         if (DefInstr) {
171           DepthOp = BlockTrace.getInstrCycles(*DefInstr).Depth;
172           LatencyOp = TSchedModel.computeOperandLatency(
173               DefInstr, DefInstr->findRegisterDefOperandIdx(MO.getReg()),
174               InstrPtr, InstrPtr->findRegisterUseOperandIdx(MO.getReg()));
175         }
176       }
177       IDepth = std::max(IDepth, DepthOp + LatencyOp);
178     }
179     InstrDepth.push_back(IDepth);
180   }
181   unsigned NewRootIdx = InsInstrs.size() - 1;
182   return InstrDepth[NewRootIdx];
183 }
184 
185 /// Computes instruction latency as max of latency of defined operands.
186 ///
187 /// \param Root is a machine instruction that could be replaced by NewRoot.
188 /// It is used to compute a more accurate latency information for NewRoot in
189 /// case there is a dependent instruction in the same trace (\p BlockTrace)
190 /// \param NewRoot is the instruction for which the latency is computed
191 /// \param BlockTrace is a trace of machine instructions
192 ///
193 /// \returns Latency of \p NewRoot
194 unsigned MachineCombiner::getLatency(MachineInstr *Root, MachineInstr *NewRoot,
195                                      MachineTraceMetrics::Trace BlockTrace) {
196   assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
197          "Missing machine model\n");
198 
199   // Check each definition in NewRoot and compute the latency
200   unsigned NewRootLatency = 0;
201 
202   for (const MachineOperand &MO : NewRoot->operands()) {
203     // Check for virtual register operand.
204     if (!(MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())))
205       continue;
206     if (!MO.isDef())
207       continue;
208     // Get the first instruction that uses MO
209     MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(MO.getReg());
210     RI++;
211     MachineInstr *UseMO = RI->getParent();
212     unsigned LatencyOp = 0;
213     if (UseMO && BlockTrace.isDepInTrace(*Root, *UseMO)) {
214       LatencyOp = TSchedModel.computeOperandLatency(
215           NewRoot, NewRoot->findRegisterDefOperandIdx(MO.getReg()), UseMO,
216           UseMO->findRegisterUseOperandIdx(MO.getReg()));
217     } else {
218       LatencyOp = TSchedModel.computeInstrLatency(NewRoot);
219     }
220     NewRootLatency = std::max(NewRootLatency, LatencyOp);
221   }
222   return NewRootLatency;
223 }
224 
225 /// The combiner's goal may differ based on which pattern it is attempting
226 /// to optimize.
227 enum class CombinerObjective {
228   MustReduceDepth, // The data dependency chain must be improved.
229   Default          // The critical path must not be lengthened.
230 };
231 
232 static CombinerObjective getCombinerObjective(MachineCombinerPattern P) {
233   // TODO: If C++ ever gets a real enum class, make this part of the
234   // MachineCombinerPattern class.
235   switch (P) {
236   case MachineCombinerPattern::REASSOC_AX_BY:
237   case MachineCombinerPattern::REASSOC_AX_YB:
238   case MachineCombinerPattern::REASSOC_XA_BY:
239   case MachineCombinerPattern::REASSOC_XA_YB:
240     return CombinerObjective::MustReduceDepth;
241   default:
242     return CombinerObjective::Default;
243   }
244 }
245 
246 /// The DAGCombine code sequence ends in MI (Machine Instruction) Root.
247 /// The new code sequence ends in MI NewRoot. A necessary condition for the new
248 /// sequence to replace the old sequence is that it cannot lengthen the critical
249 /// path. The definition of "improve" may be restricted by specifying that the
250 /// new path improves the data dependency chain (MustReduceDepth).
251 bool MachineCombiner::improvesCriticalPathLen(
252     MachineBasicBlock *MBB, MachineInstr *Root,
253     MachineTraceMetrics::Trace BlockTrace,
254     SmallVectorImpl<MachineInstr *> &InsInstrs,
255     SmallVectorImpl<MachineInstr *> &DelInstrs,
256     DenseMap<unsigned, unsigned> &InstrIdxForVirtReg,
257     MachineCombinerPattern Pattern,
258     bool SlackIsAccurate) {
259   assert(TSchedModel.hasInstrSchedModelOrItineraries() &&
260          "Missing machine model\n");
261   // NewRoot is the last instruction in the \p InsInstrs vector.
262   unsigned NewRootIdx = InsInstrs.size() - 1;
263   MachineInstr *NewRoot = InsInstrs[NewRootIdx];
264 
265   // Get depth and latency of NewRoot and Root.
266   unsigned NewRootDepth = getDepth(InsInstrs, InstrIdxForVirtReg, BlockTrace);
267   unsigned RootDepth = BlockTrace.getInstrCycles(*Root).Depth;
268 
269   DEBUG(dbgs() << "DEPENDENCE DATA FOR " << *Root << "\n";
270         dbgs() << " NewRootDepth: " << NewRootDepth << "\n";
271         dbgs() << " RootDepth: " << RootDepth << "\n");
272 
273   // For a transform such as reassociation, the cost equation is
274   // conservatively calculated so that we must improve the depth (data
275   // dependency cycles) in the critical path to proceed with the transform.
276   // Being conservative also protects against inaccuracies in the underlying
277   // machine trace metrics and CPU models.
278   if (getCombinerObjective(Pattern) == CombinerObjective::MustReduceDepth)
279     return NewRootDepth < RootDepth;
280 
281   // A more flexible cost calculation for the critical path includes the slack
282   // of the original code sequence. This may allow the transform to proceed
283   // even if the instruction depths (data dependency cycles) become worse.
284 
285   // Account for the latency of the inserted and deleted instructions by
286   // adding up their latencies. This assumes that the inserted and deleted
287   // instructions are dependent instruction chains, which might not hold
288   // in all cases.
289   unsigned NewRootLatency = 0;
290   for (unsigned i = 0; i < InsInstrs.size() - 1; i++)
291     NewRootLatency += TSchedModel.computeInstrLatency(InsInstrs[i]);
292   NewRootLatency += getLatency(Root, NewRoot, BlockTrace);
293 
294   unsigned RootLatency = 0;
295   for (auto I : DelInstrs)
296     RootLatency += TSchedModel.computeInstrLatency(I);
297 
298   unsigned RootSlack = BlockTrace.getInstrSlack(*Root);
299   unsigned NewCycleCount = NewRootDepth + NewRootLatency;
300   unsigned OldCycleCount = RootDepth + RootLatency +
301                            (SlackIsAccurate ? RootSlack : 0);
302   DEBUG(dbgs() << " NewRootLatency: " << NewRootLatency << "\n";
303         dbgs() << " RootLatency: " << RootLatency << "\n";
304         dbgs() << " RootSlack: " << RootSlack << " SlackIsAccurate="
305                << SlackIsAccurate << "\n";
306         dbgs() << " NewRootDepth + NewRootLatency = "
307                << NewCycleCount << "\n";
308         dbgs() << " RootDepth + RootLatency + RootSlack = "
309                << OldCycleCount << "\n";
310         );
311 
312   return NewCycleCount <= OldCycleCount;
313 }
314 
315 /// helper routine to convert instructions into SC
316 void MachineCombiner::instr2instrSC(
317     SmallVectorImpl<MachineInstr *> &Instrs,
318     SmallVectorImpl<const MCSchedClassDesc *> &InstrsSC) {
319   for (auto *InstrPtr : Instrs) {
320     unsigned Opc = InstrPtr->getOpcode();
321     unsigned Idx = TII->get(Opc).getSchedClass();
322     const MCSchedClassDesc *SC = SchedModel.getSchedClassDesc(Idx);
323     InstrsSC.push_back(SC);
324   }
325 }
326 
327 /// True when the new instructions do not increase resource length
328 bool MachineCombiner::preservesResourceLen(
329     MachineBasicBlock *MBB, MachineTraceMetrics::Trace BlockTrace,
330     SmallVectorImpl<MachineInstr *> &InsInstrs,
331     SmallVectorImpl<MachineInstr *> &DelInstrs) {
332   if (!TSchedModel.hasInstrSchedModel())
333     return true;
334 
335   // Compute current resource length
336 
337   //ArrayRef<const MachineBasicBlock *> MBBarr(MBB);
338   SmallVector <const MachineBasicBlock *, 1> MBBarr;
339   MBBarr.push_back(MBB);
340   unsigned ResLenBeforeCombine = BlockTrace.getResourceLength(MBBarr);
341 
342   // Deal with SC rather than Instructions.
343   SmallVector<const MCSchedClassDesc *, 16> InsInstrsSC;
344   SmallVector<const MCSchedClassDesc *, 16> DelInstrsSC;
345 
346   instr2instrSC(InsInstrs, InsInstrsSC);
347   instr2instrSC(DelInstrs, DelInstrsSC);
348 
349   ArrayRef<const MCSchedClassDesc *> MSCInsArr = makeArrayRef(InsInstrsSC);
350   ArrayRef<const MCSchedClassDesc *> MSCDelArr = makeArrayRef(DelInstrsSC);
351 
352   // Compute new resource length.
353   unsigned ResLenAfterCombine =
354       BlockTrace.getResourceLength(MBBarr, MSCInsArr, MSCDelArr);
355 
356   DEBUG(dbgs() << "RESOURCE DATA: \n";
357         dbgs() << " resource len before: " << ResLenBeforeCombine
358                << " after: " << ResLenAfterCombine << "\n";);
359 
360   return ResLenAfterCombine <= ResLenBeforeCombine;
361 }
362 
363 /// \returns true when new instruction sequence should be generated
364 /// independent if it lengthens critical path or not
365 bool MachineCombiner::doSubstitute(unsigned NewSize, unsigned OldSize) {
366   if (OptSize && (NewSize < OldSize))
367     return true;
368   if (!TSchedModel.hasInstrSchedModelOrItineraries())
369     return true;
370   return false;
371 }
372 
373 /// Inserts InsInstrs and deletes DelInstrs. Incrementally updates instruction
374 /// depths if requested.
375 ///
376 /// \param MBB basic block to insert instructions in
377 /// \param MI current machine instruction
378 /// \param InsInstrs new instructions to insert in \p MBB
379 /// \param DelInstrs instruction to delete from \p MBB
380 /// \param MinInstr is a pointer to the machine trace information
381 /// \param RegUnits set of live registers, needed to compute instruction depths
382 /// \param IncrementalUpdate if true, compute instruction depths incrementally,
383 ///                          otherwise invalidate the trace
384 static void insertDeleteInstructions(MachineBasicBlock *MBB, MachineInstr &MI,
385                                      SmallVector<MachineInstr *, 16> InsInstrs,
386                                      SmallVector<MachineInstr *, 16> DelInstrs,
387                                      MachineTraceMetrics::Ensemble *MinInstr,
388                                      SparseSet<LiveRegUnit> &RegUnits,
389                                      bool IncrementalUpdate) {
390   for (auto *InstrPtr : InsInstrs)
391     MBB->insert((MachineBasicBlock::iterator)&MI, InstrPtr);
392 
393   for (auto *InstrPtr : DelInstrs) {
394     InstrPtr->eraseFromParentAndMarkDBGValuesForRemoval();
395     // Erase all LiveRegs defined by the removed instruction
396     for (auto I = RegUnits.begin(); I != RegUnits.end(); ) {
397       if (I->MI == InstrPtr)
398         I = RegUnits.erase(I);
399       else
400         I++;
401     }
402   }
403 
404   if (IncrementalUpdate)
405     for (auto *InstrPtr : InsInstrs)
406       MinInstr->updateDepth(MBB, *InstrPtr, RegUnits);
407   else
408     MinInstr->invalidate(MBB);
409 
410   NumInstCombined++;
411 }
412 
413 /// Substitute a slow code sequence with a faster one by
414 /// evaluating instruction combining pattern.
415 /// The prototype of such a pattern is MUl + ADD -> MADD. Performs instruction
416 /// combining based on machine trace metrics. Only combine a sequence of
417 /// instructions  when this neither lengthens the critical path nor increases
418 /// resource pressure. When optimizing for codesize always combine when the new
419 /// sequence is shorter.
420 bool MachineCombiner::combineInstructions(MachineBasicBlock *MBB) {
421   bool Changed = false;
422   DEBUG(dbgs() << "Combining MBB " << MBB->getName() << "\n");
423 
424   bool IncrementalUpdate = false;
425   auto BlockIter = MBB->begin();
426   decltype(BlockIter) LastUpdate;
427   // Check if the block is in a loop.
428   const MachineLoop *ML = MLI->getLoopFor(MBB);
429   if (!MinInstr)
430     MinInstr = Traces->getEnsemble(MachineTraceMetrics::TS_MinInstrCount);
431 
432   SparseSet<LiveRegUnit> RegUnits;
433   RegUnits.setUniverse(TRI->getNumRegUnits());
434 
435   while (BlockIter != MBB->end()) {
436     auto &MI = *BlockIter++;
437 
438     DEBUG(dbgs() << "INSTR "; MI.dump(); dbgs() << "\n";);
439     SmallVector<MachineCombinerPattern, 16> Patterns;
440     // The motivating example is:
441     //
442     //     MUL  Other        MUL_op1 MUL_op2  Other
443     //      \    /               \      |    /
444     //      ADD/SUB      =>        MADD/MSUB
445     //      (=Root)                (=NewRoot)
446 
447     // The DAGCombine code always replaced MUL + ADD/SUB by MADD. While this is
448     // usually beneficial for code size it unfortunately can hurt performance
449     // when the ADD is on the critical path, but the MUL is not. With the
450     // substitution the MUL becomes part of the critical path (in form of the
451     // MADD) and can lengthen it on architectures where the MADD latency is
452     // longer than the ADD latency.
453     //
454     // For each instruction we check if it can be the root of a combiner
455     // pattern. Then for each pattern the new code sequence in form of MI is
456     // generated and evaluated. When the efficiency criteria (don't lengthen
457     // critical path, don't use more resources) is met the new sequence gets
458     // hooked up into the basic block before the old sequence is removed.
459     //
460     // The algorithm does not try to evaluate all patterns and pick the best.
461     // This is only an artificial restriction though. In practice there is
462     // mostly one pattern, and getMachineCombinerPatterns() can order patterns
463     // based on an internal cost heuristic.
464 
465     if (!TII->getMachineCombinerPatterns(MI, Patterns))
466       continue;
467 
468     for (auto P : Patterns) {
469       SmallVector<MachineInstr *, 16> InsInstrs;
470       SmallVector<MachineInstr *, 16> DelInstrs;
471       DenseMap<unsigned, unsigned> InstrIdxForVirtReg;
472       TII->genAlternativeCodeSequence(MI, P, InsInstrs, DelInstrs,
473                                       InstrIdxForVirtReg);
474       unsigned NewInstCount = InsInstrs.size();
475       unsigned OldInstCount = DelInstrs.size();
476       // Found pattern, but did not generate alternative sequence.
477       // This can happen e.g. when an immediate could not be materialized
478       // in a single instruction.
479       if (!NewInstCount)
480         continue;
481 
482       bool SubstituteAlways = false;
483       if (ML && TII->isThroughputPattern(P))
484         SubstituteAlways = true;
485 
486       if (IncrementalUpdate) {
487         // Update depths since the last incremental update.
488         MinInstr->updateDepths(LastUpdate, BlockIter, RegUnits);
489         LastUpdate = BlockIter;
490       }
491 
492       // Substitute when we optimize for codesize and the new sequence has
493       // fewer instructions OR
494       // the new sequence neither lengthens the critical path nor increases
495       // resource pressure.
496       if (SubstituteAlways || doSubstitute(NewInstCount, OldInstCount)) {
497         insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, MinInstr,
498                                  RegUnits, IncrementalUpdate);
499         // Eagerly stop after the first pattern fires.
500         Changed = true;
501         break;
502       } else {
503         // For big basic blocks, we only compute the full trace the first time
504         // we hit this. We do not invalidate the trace, but instead update the
505         // instruction depths incrementally.
506         // NOTE: Only the instruction depths up to MI are accurate. All other
507         // trace information is not updated.
508         MachineTraceMetrics::Trace BlockTrace = MinInstr->getTrace(MBB);
509         Traces->verifyAnalysis();
510         if (improvesCriticalPathLen(MBB, &MI, BlockTrace, InsInstrs, DelInstrs,
511                                     InstrIdxForVirtReg, P,
512                                     !IncrementalUpdate) &&
513             preservesResourceLen(MBB, BlockTrace, InsInstrs, DelInstrs)) {
514           if (MBB->size() > inc_threshold) {
515             // Use incremental depth updates for basic blocks above treshold
516             IncrementalUpdate = true;
517             LastUpdate = BlockIter;
518           }
519 
520           insertDeleteInstructions(MBB, MI, InsInstrs, DelInstrs, MinInstr,
521                                    RegUnits, IncrementalUpdate);
522 
523           // Eagerly stop after the first pattern fires.
524           Changed = true;
525           break;
526         }
527         // Cleanup instructions of the alternative code sequence. There is no
528         // use for them.
529         MachineFunction *MF = MBB->getParent();
530         for (auto *InstrPtr : InsInstrs)
531           MF->DeleteMachineInstr(InstrPtr);
532       }
533       InstrIdxForVirtReg.clear();
534     }
535   }
536 
537   if (Changed && IncrementalUpdate)
538     Traces->invalidate(MBB);
539   return Changed;
540 }
541 
542 bool MachineCombiner::runOnMachineFunction(MachineFunction &MF) {
543   const TargetSubtargetInfo &STI = MF.getSubtarget();
544   TII = STI.getInstrInfo();
545   TRI = STI.getRegisterInfo();
546   SchedModel = STI.getSchedModel();
547   TSchedModel.init(SchedModel, &STI, TII);
548   MRI = &MF.getRegInfo();
549   MLI = &getAnalysis<MachineLoopInfo>();
550   Traces = &getAnalysis<MachineTraceMetrics>();
551   MinInstr = nullptr;
552   OptSize = MF.getFunction()->optForSize();
553 
554   DEBUG(dbgs() << getPassName() << ": " << MF.getName() << '\n');
555   if (!TII->useMachineCombiner()) {
556     DEBUG(dbgs() << "  Skipping pass: Target does not support machine combiner\n");
557     return false;
558   }
559 
560   bool Changed = false;
561 
562   // Try to combine instructions.
563   for (auto &MBB : MF)
564     Changed |= combineInstructions(&MBB);
565 
566   return Changed;
567 }
568