1 //===- MachineCSE.cpp - Machine Common Subexpression Elimination Pass -----===//
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 // This pass performs global common subexpression elimination on machine
10 // instructions using a scoped hash table based value numbering scheme. It
11 // must be run while the machine function is still in SSA form.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/ScopedHashTable.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/CFG.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
25 #include "llvm/CodeGen/MachineDominators.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/Passes.h"
32 #include "llvm/CodeGen/TargetInstrInfo.h"
33 #include "llvm/CodeGen/TargetOpcodes.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/InitializePasses.h"
37 #include "llvm/MC/MCInstrDesc.h"
38 #include "llvm/MC/MCRegister.h"
39 #include "llvm/MC/MCRegisterInfo.h"
40 #include "llvm/Pass.h"
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/RecyclingAllocator.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include <cassert>
46 #include <iterator>
47 #include <utility>
48 #include <vector>
49 
50 using namespace llvm;
51 
52 #define DEBUG_TYPE "machine-cse"
53 
54 STATISTIC(NumCoalesces, "Number of copies coalesced");
55 STATISTIC(NumCSEs,      "Number of common subexpression eliminated");
56 STATISTIC(NumPREs,      "Number of partial redundant expression"
57                         " transformed to fully redundant");
58 STATISTIC(NumPhysCSEs,
59           "Number of physreg referencing common subexpr eliminated");
60 STATISTIC(NumCrossBBCSEs,
61           "Number of cross-MBB physreg referencing CS eliminated");
62 STATISTIC(NumCommutes,  "Number of copies coalesced after commuting");
63 
64 namespace {
65 
66   class MachineCSE : public MachineFunctionPass {
67     const TargetInstrInfo *TII;
68     const TargetRegisterInfo *TRI;
69     AliasAnalysis *AA;
70     MachineDominatorTree *DT;
71     MachineRegisterInfo *MRI;
72     MachineBlockFrequencyInfo *MBFI;
73 
74   public:
75     static char ID; // Pass identification
76 
77     MachineCSE() : MachineFunctionPass(ID) {
78       initializeMachineCSEPass(*PassRegistry::getPassRegistry());
79     }
80 
81     bool runOnMachineFunction(MachineFunction &MF) override;
82 
83     void getAnalysisUsage(AnalysisUsage &AU) const override {
84       AU.setPreservesCFG();
85       MachineFunctionPass::getAnalysisUsage(AU);
86       AU.addRequired<AAResultsWrapperPass>();
87       AU.addPreservedID(MachineLoopInfoID);
88       AU.addRequired<MachineDominatorTree>();
89       AU.addPreserved<MachineDominatorTree>();
90       AU.addRequired<MachineBlockFrequencyInfo>();
91       AU.addPreserved<MachineBlockFrequencyInfo>();
92     }
93 
94     void releaseMemory() override {
95       ScopeMap.clear();
96       PREMap.clear();
97       Exps.clear();
98     }
99 
100   private:
101     using AllocatorTy = RecyclingAllocator<BumpPtrAllocator,
102                             ScopedHashTableVal<MachineInstr *, unsigned>>;
103     using ScopedHTType =
104         ScopedHashTable<MachineInstr *, unsigned, MachineInstrExpressionTrait,
105                         AllocatorTy>;
106     using ScopeType = ScopedHTType::ScopeTy;
107     using PhysDefVector = SmallVector<std::pair<unsigned, unsigned>, 2>;
108 
109     unsigned LookAheadLimit = 0;
110     DenseMap<MachineBasicBlock *, ScopeType *> ScopeMap;
111     DenseMap<MachineInstr *, MachineBasicBlock *, MachineInstrExpressionTrait>
112         PREMap;
113     ScopedHTType VNT;
114     SmallVector<MachineInstr *, 64> Exps;
115     unsigned CurrVN = 0;
116 
117     bool PerformTrivialCopyPropagation(MachineInstr *MI,
118                                        MachineBasicBlock *MBB);
119     bool isPhysDefTriviallyDead(MCRegister Reg,
120                                 MachineBasicBlock::const_iterator I,
121                                 MachineBasicBlock::const_iterator E) const;
122     bool hasLivePhysRegDefUses(const MachineInstr *MI,
123                                const MachineBasicBlock *MBB,
124                                SmallSet<MCRegister, 8> &PhysRefs,
125                                PhysDefVector &PhysDefs, bool &PhysUseDef) const;
126     bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
127                           SmallSet<MCRegister, 8> &PhysRefs,
128                           PhysDefVector &PhysDefs, bool &NonLocal) const;
129     bool isCSECandidate(MachineInstr *MI);
130     bool isProfitableToCSE(Register CSReg, Register Reg,
131                            MachineBasicBlock *CSBB, MachineInstr *MI);
132     void EnterScope(MachineBasicBlock *MBB);
133     void ExitScope(MachineBasicBlock *MBB);
134     bool ProcessBlockCSE(MachineBasicBlock *MBB);
135     void ExitScopeIfDone(MachineDomTreeNode *Node,
136                          DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren);
137     bool PerformCSE(MachineDomTreeNode *Node);
138 
139     bool isPRECandidate(MachineInstr *MI);
140     bool ProcessBlockPRE(MachineDominatorTree *MDT, MachineBasicBlock *MBB);
141     bool PerformSimplePRE(MachineDominatorTree *DT);
142     /// Heuristics to see if it's profitable to move common computations of MBB
143     /// and MBB1 to CandidateBB.
144     bool isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
145                                  MachineBasicBlock *MBB,
146                                  MachineBasicBlock *MBB1);
147   };
148 
149 } // end anonymous namespace
150 
151 char MachineCSE::ID = 0;
152 
153 char &llvm::MachineCSEID = MachineCSE::ID;
154 
155 INITIALIZE_PASS_BEGIN(MachineCSE, DEBUG_TYPE,
156                       "Machine Common Subexpression Elimination", false, false)
157 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
158 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
159 INITIALIZE_PASS_END(MachineCSE, DEBUG_TYPE,
160                     "Machine Common Subexpression Elimination", false, false)
161 
162 /// The source register of a COPY machine instruction can be propagated to all
163 /// its users, and this propagation could increase the probability of finding
164 /// common subexpressions. If the COPY has only one user, the COPY itself can
165 /// be removed.
166 bool MachineCSE::PerformTrivialCopyPropagation(MachineInstr *MI,
167                                                MachineBasicBlock *MBB) {
168   bool Changed = false;
169   for (MachineOperand &MO : MI->operands()) {
170     if (!MO.isReg() || !MO.isUse())
171       continue;
172     Register Reg = MO.getReg();
173     if (!Register::isVirtualRegister(Reg))
174       continue;
175     bool OnlyOneUse = MRI->hasOneNonDBGUse(Reg);
176     MachineInstr *DefMI = MRI->getVRegDef(Reg);
177     if (!DefMI->isCopy())
178       continue;
179     Register SrcReg = DefMI->getOperand(1).getReg();
180     if (!Register::isVirtualRegister(SrcReg))
181       continue;
182     if (DefMI->getOperand(0).getSubReg())
183       continue;
184     // FIXME: We should trivially coalesce subregister copies to expose CSE
185     // opportunities on instructions with truncated operands (see
186     // cse-add-with-overflow.ll). This can be done here as follows:
187     // if (SrcSubReg)
188     //  RC = TRI->getMatchingSuperRegClass(MRI->getRegClass(SrcReg), RC,
189     //                                     SrcSubReg);
190     // MO.substVirtReg(SrcReg, SrcSubReg, *TRI);
191     //
192     // The 2-addr pass has been updated to handle coalesced subregs. However,
193     // some machine-specific code still can't handle it.
194     // To handle it properly we also need a way find a constrained subregister
195     // class given a super-reg class and subreg index.
196     if (DefMI->getOperand(1).getSubReg())
197       continue;
198     if (!MRI->constrainRegAttrs(SrcReg, Reg))
199       continue;
200     LLVM_DEBUG(dbgs() << "Coalescing: " << *DefMI);
201     LLVM_DEBUG(dbgs() << "***     to: " << *MI);
202 
203     // Propagate SrcReg of copies to MI.
204     MO.setReg(SrcReg);
205     MRI->clearKillFlags(SrcReg);
206     // Coalesce single use copies.
207     if (OnlyOneUse) {
208       // If (and only if) we've eliminated all uses of the copy, also
209       // copy-propagate to any debug-users of MI, or they'll be left using
210       // an undefined value.
211       DefMI->changeDebugValuesDefReg(SrcReg);
212 
213       DefMI->eraseFromParent();
214       ++NumCoalesces;
215     }
216     Changed = true;
217   }
218 
219   return Changed;
220 }
221 
222 bool MachineCSE::isPhysDefTriviallyDead(
223     MCRegister Reg, MachineBasicBlock::const_iterator I,
224     MachineBasicBlock::const_iterator E) const {
225   unsigned LookAheadLeft = LookAheadLimit;
226   while (LookAheadLeft) {
227     // Skip over dbg_value's.
228     I = skipDebugInstructionsForward(I, E);
229 
230     if (I == E)
231       // Reached end of block, we don't know if register is dead or not.
232       return false;
233 
234     bool SeenDef = false;
235     for (const MachineOperand &MO : I->operands()) {
236       if (MO.isRegMask() && MO.clobbersPhysReg(Reg))
237         SeenDef = true;
238       if (!MO.isReg() || !MO.getReg())
239         continue;
240       if (!TRI->regsOverlap(MO.getReg(), Reg))
241         continue;
242       if (MO.isUse())
243         // Found a use!
244         return false;
245       SeenDef = true;
246     }
247     if (SeenDef)
248       // See a def of Reg (or an alias) before encountering any use, it's
249       // trivially dead.
250       return true;
251 
252     --LookAheadLeft;
253     ++I;
254   }
255   return false;
256 }
257 
258 static bool isCallerPreservedOrConstPhysReg(MCRegister Reg,
259                                             const MachineFunction &MF,
260                                             const TargetRegisterInfo &TRI) {
261   // MachineRegisterInfo::isConstantPhysReg directly called by
262   // MachineRegisterInfo::isCallerPreservedOrConstPhysReg expects the
263   // reserved registers to be frozen. That doesn't cause a problem  post-ISel as
264   // most (if not all) targets freeze reserved registers right after ISel.
265   //
266   // It does cause issues mid-GlobalISel, however, hence the additional
267   // reservedRegsFrozen check.
268   const MachineRegisterInfo &MRI = MF.getRegInfo();
269   return TRI.isCallerPreservedPhysReg(Reg, MF) ||
270          (MRI.reservedRegsFrozen() && MRI.isConstantPhysReg(Reg));
271 }
272 
273 /// hasLivePhysRegDefUses - Return true if the specified instruction read/write
274 /// physical registers (except for dead defs of physical registers). It also
275 /// returns the physical register def by reference if it's the only one and the
276 /// instruction does not uses a physical register.
277 bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
278                                        const MachineBasicBlock *MBB,
279                                        SmallSet<MCRegister, 8> &PhysRefs,
280                                        PhysDefVector &PhysDefs,
281                                        bool &PhysUseDef) const {
282   // First, add all uses to PhysRefs.
283   for (const MachineOperand &MO : MI->operands()) {
284     if (!MO.isReg() || MO.isDef())
285       continue;
286     Register Reg = MO.getReg();
287     if (!Reg)
288       continue;
289     if (Register::isVirtualRegister(Reg))
290       continue;
291     // Reading either caller preserved or constant physregs is ok.
292     if (!isCallerPreservedOrConstPhysReg(Reg.asMCReg(), *MI->getMF(), *TRI))
293       for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
294         PhysRefs.insert(*AI);
295   }
296 
297   // Next, collect all defs into PhysDefs.  If any is already in PhysRefs
298   // (which currently contains only uses), set the PhysUseDef flag.
299   PhysUseDef = false;
300   MachineBasicBlock::const_iterator I = MI; I = std::next(I);
301   for (const auto &MOP : llvm::enumerate(MI->operands())) {
302     const MachineOperand &MO = MOP.value();
303     if (!MO.isReg() || !MO.isDef())
304       continue;
305     Register Reg = MO.getReg();
306     if (!Reg)
307       continue;
308     if (Register::isVirtualRegister(Reg))
309       continue;
310     // Check against PhysRefs even if the def is "dead".
311     if (PhysRefs.count(Reg.asMCReg()))
312       PhysUseDef = true;
313     // If the def is dead, it's ok. But the def may not marked "dead". That's
314     // common since this pass is run before livevariables. We can scan
315     // forward a few instructions and check if it is obviously dead.
316     if (!MO.isDead() && !isPhysDefTriviallyDead(Reg.asMCReg(), I, MBB->end()))
317       PhysDefs.push_back(std::make_pair(MOP.index(), Reg));
318   }
319 
320   // Finally, add all defs to PhysRefs as well.
321   for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i)
322     for (MCRegAliasIterator AI(PhysDefs[i].second, TRI, true); AI.isValid();
323          ++AI)
324       PhysRefs.insert(*AI);
325 
326   return !PhysRefs.empty();
327 }
328 
329 bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
330                                   SmallSet<MCRegister, 8> &PhysRefs,
331                                   PhysDefVector &PhysDefs,
332                                   bool &NonLocal) const {
333   // For now conservatively returns false if the common subexpression is
334   // not in the same basic block as the given instruction. The only exception
335   // is if the common subexpression is in the sole predecessor block.
336   const MachineBasicBlock *MBB = MI->getParent();
337   const MachineBasicBlock *CSMBB = CSMI->getParent();
338 
339   bool CrossMBB = false;
340   if (CSMBB != MBB) {
341     if (MBB->pred_size() != 1 || *MBB->pred_begin() != CSMBB)
342       return false;
343 
344     for (unsigned i = 0, e = PhysDefs.size(); i != e; ++i) {
345       if (MRI->isAllocatable(PhysDefs[i].second) ||
346           MRI->isReserved(PhysDefs[i].second))
347         // Avoid extending live range of physical registers if they are
348         //allocatable or reserved.
349         return false;
350     }
351     CrossMBB = true;
352   }
353   MachineBasicBlock::const_iterator I = CSMI; I = std::next(I);
354   MachineBasicBlock::const_iterator E = MI;
355   MachineBasicBlock::const_iterator EE = CSMBB->end();
356   unsigned LookAheadLeft = LookAheadLimit;
357   while (LookAheadLeft) {
358     // Skip over dbg_value's.
359     while (I != E && I != EE && I->isDebugInstr())
360       ++I;
361 
362     if (I == EE) {
363       assert(CrossMBB && "Reaching end-of-MBB without finding MI?");
364       (void)CrossMBB;
365       CrossMBB = false;
366       NonLocal = true;
367       I = MBB->begin();
368       EE = MBB->end();
369       continue;
370     }
371 
372     if (I == E)
373       return true;
374 
375     for (const MachineOperand &MO : I->operands()) {
376       // RegMasks go on instructions like calls that clobber lots of physregs.
377       // Don't attempt to CSE across such an instruction.
378       if (MO.isRegMask())
379         return false;
380       if (!MO.isReg() || !MO.isDef())
381         continue;
382       Register MOReg = MO.getReg();
383       if (Register::isVirtualRegister(MOReg))
384         continue;
385       if (PhysRefs.count(MOReg.asMCReg()))
386         return false;
387     }
388 
389     --LookAheadLeft;
390     ++I;
391   }
392 
393   return false;
394 }
395 
396 bool MachineCSE::isCSECandidate(MachineInstr *MI) {
397   if (MI->isPosition() || MI->isPHI() || MI->isImplicitDef() || MI->isKill() ||
398       MI->isInlineAsm() || MI->isDebugInstr())
399     return false;
400 
401   // Ignore copies.
402   if (MI->isCopyLike())
403     return false;
404 
405   // Ignore stuff that we obviously can't move.
406   if (MI->mayStore() || MI->isCall() || MI->isTerminator() ||
407       MI->mayRaiseFPException() || MI->hasUnmodeledSideEffects())
408     return false;
409 
410   if (MI->mayLoad()) {
411     // Okay, this instruction does a load. As a refinement, we allow the target
412     // to decide whether the loaded value is actually a constant. If so, we can
413     // actually use it as a load.
414     if (!MI->isDereferenceableInvariantLoad(AA))
415       // FIXME: we should be able to hoist loads with no other side effects if
416       // there are no other instructions which can change memory in this loop.
417       // This is a trivial form of alias analysis.
418       return false;
419   }
420 
421   // Ignore stack guard loads, otherwise the register that holds CSEed value may
422   // be spilled and get loaded back with corrupted data.
423   if (MI->getOpcode() == TargetOpcode::LOAD_STACK_GUARD)
424     return false;
425 
426   return true;
427 }
428 
429 /// isProfitableToCSE - Return true if it's profitable to eliminate MI with a
430 /// common expression that defines Reg. CSBB is basic block where CSReg is
431 /// defined.
432 bool MachineCSE::isProfitableToCSE(Register CSReg, Register Reg,
433                                    MachineBasicBlock *CSBB, MachineInstr *MI) {
434   // FIXME: Heuristics that works around the lack the live range splitting.
435 
436   MachineBasicBlock *BB = MI->getParent();
437   // Prevent CSE-ing non-local convergent instructions.
438   if (MI->isConvergent() && CSBB != BB)
439     return false;
440 
441   // If CSReg is used at all uses of Reg, CSE should not increase register
442   // pressure of CSReg.
443   bool MayIncreasePressure = true;
444   if (Register::isVirtualRegister(CSReg) && Register::isVirtualRegister(Reg)) {
445     MayIncreasePressure = false;
446     SmallPtrSet<MachineInstr*, 8> CSUses;
447     for (MachineInstr &MI : MRI->use_nodbg_instructions(CSReg)) {
448       CSUses.insert(&MI);
449     }
450     for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
451       if (!CSUses.count(&MI)) {
452         MayIncreasePressure = true;
453         break;
454       }
455     }
456   }
457   if (!MayIncreasePressure) return true;
458 
459   // Heuristics #1: Don't CSE "cheap" computation if the def is not local or in
460   // an immediate predecessor. We don't want to increase register pressure and
461   // end up causing other computation to be spilled.
462   if (TII->isAsCheapAsAMove(*MI)) {
463     if (CSBB != BB && !CSBB->isSuccessor(BB))
464       return false;
465   }
466 
467   // Heuristics #2: If the expression doesn't not use a vr and the only use
468   // of the redundant computation are copies, do not cse.
469   bool HasVRegUse = false;
470   for (const MachineOperand &MO : MI->operands()) {
471     if (MO.isReg() && MO.isUse() && Register::isVirtualRegister(MO.getReg())) {
472       HasVRegUse = true;
473       break;
474     }
475   }
476   if (!HasVRegUse) {
477     bool HasNonCopyUse = false;
478     for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
479       // Ignore copies.
480       if (!MI.isCopyLike()) {
481         HasNonCopyUse = true;
482         break;
483       }
484     }
485     if (!HasNonCopyUse)
486       return false;
487   }
488 
489   // Heuristics #3: If the common subexpression is used by PHIs, do not reuse
490   // it unless the defined value is already used in the BB of the new use.
491   bool HasPHI = false;
492   for (MachineInstr &UseMI : MRI->use_nodbg_instructions(CSReg)) {
493     HasPHI |= UseMI.isPHI();
494     if (UseMI.getParent() == MI->getParent())
495       return true;
496   }
497 
498   return !HasPHI;
499 }
500 
501 void MachineCSE::EnterScope(MachineBasicBlock *MBB) {
502   LLVM_DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n');
503   ScopeType *Scope = new ScopeType(VNT);
504   ScopeMap[MBB] = Scope;
505 }
506 
507 void MachineCSE::ExitScope(MachineBasicBlock *MBB) {
508   LLVM_DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n');
509   DenseMap<MachineBasicBlock*, ScopeType*>::iterator SI = ScopeMap.find(MBB);
510   assert(SI != ScopeMap.end());
511   delete SI->second;
512   ScopeMap.erase(SI);
513 }
514 
515 bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
516   bool Changed = false;
517 
518   SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs;
519   SmallVector<unsigned, 2> ImplicitDefsToUpdate;
520   SmallVector<unsigned, 2> ImplicitDefs;
521   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E; ) {
522     MachineInstr *MI = &*I;
523     ++I;
524 
525     if (!isCSECandidate(MI))
526       continue;
527 
528     bool FoundCSE = VNT.count(MI);
529     if (!FoundCSE) {
530       // Using trivial copy propagation to find more CSE opportunities.
531       if (PerformTrivialCopyPropagation(MI, MBB)) {
532         Changed = true;
533 
534         // After coalescing MI itself may become a copy.
535         if (MI->isCopyLike())
536           continue;
537 
538         // Try again to see if CSE is possible.
539         FoundCSE = VNT.count(MI);
540       }
541     }
542 
543     // Commute commutable instructions.
544     bool Commuted = false;
545     if (!FoundCSE && MI->isCommutable()) {
546       if (MachineInstr *NewMI = TII->commuteInstruction(*MI)) {
547         Commuted = true;
548         FoundCSE = VNT.count(NewMI);
549         if (NewMI != MI) {
550           // New instruction. It doesn't need to be kept.
551           NewMI->eraseFromParent();
552           Changed = true;
553         } else if (!FoundCSE)
554           // MI was changed but it didn't help, commute it back!
555           (void)TII->commuteInstruction(*MI);
556       }
557     }
558 
559     // If the instruction defines physical registers and the values *may* be
560     // used, then it's not safe to replace it with a common subexpression.
561     // It's also not safe if the instruction uses physical registers.
562     bool CrossMBBPhysDef = false;
563     SmallSet<MCRegister, 8> PhysRefs;
564     PhysDefVector PhysDefs;
565     bool PhysUseDef = false;
566     if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs,
567                                           PhysDefs, PhysUseDef)) {
568       FoundCSE = false;
569 
570       // ... Unless the CS is local or is in the sole predecessor block
571       // and it also defines the physical register which is not clobbered
572       // in between and the physical register uses were not clobbered.
573       // This can never be the case if the instruction both uses and
574       // defines the same physical register, which was detected above.
575       if (!PhysUseDef) {
576         unsigned CSVN = VNT.lookup(MI);
577         MachineInstr *CSMI = Exps[CSVN];
578         if (PhysRegDefsReach(CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
579           FoundCSE = true;
580       }
581     }
582 
583     if (!FoundCSE) {
584       VNT.insert(MI, CurrVN++);
585       Exps.push_back(MI);
586       continue;
587     }
588 
589     // Found a common subexpression, eliminate it.
590     unsigned CSVN = VNT.lookup(MI);
591     MachineInstr *CSMI = Exps[CSVN];
592     LLVM_DEBUG(dbgs() << "Examining: " << *MI);
593     LLVM_DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI);
594 
595     // Check if it's profitable to perform this CSE.
596     bool DoCSE = true;
597     unsigned NumDefs = MI->getNumDefs();
598 
599     for (unsigned i = 0, e = MI->getNumOperands(); NumDefs && i != e; ++i) {
600       MachineOperand &MO = MI->getOperand(i);
601       if (!MO.isReg() || !MO.isDef())
602         continue;
603       Register OldReg = MO.getReg();
604       Register NewReg = CSMI->getOperand(i).getReg();
605 
606       // Go through implicit defs of CSMI and MI, if a def is not dead at MI,
607       // we should make sure it is not dead at CSMI.
608       if (MO.isImplicit() && !MO.isDead() && CSMI->getOperand(i).isDead())
609         ImplicitDefsToUpdate.push_back(i);
610 
611       // Keep track of implicit defs of CSMI and MI, to clear possibly
612       // made-redundant kill flags.
613       if (MO.isImplicit() && !MO.isDead() && OldReg == NewReg)
614         ImplicitDefs.push_back(OldReg);
615 
616       if (OldReg == NewReg) {
617         --NumDefs;
618         continue;
619       }
620 
621       assert(Register::isVirtualRegister(OldReg) &&
622              Register::isVirtualRegister(NewReg) &&
623              "Do not CSE physical register defs!");
624 
625       if (!isProfitableToCSE(NewReg, OldReg, CSMI->getParent(), MI)) {
626         LLVM_DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n");
627         DoCSE = false;
628         break;
629       }
630 
631       // Don't perform CSE if the result of the new instruction cannot exist
632       // within the constraints (register class, bank, or low-level type) of
633       // the old instruction.
634       if (!MRI->constrainRegAttrs(NewReg, OldReg)) {
635         LLVM_DEBUG(
636             dbgs() << "*** Not the same register constraints, avoid CSE!\n");
637         DoCSE = false;
638         break;
639       }
640 
641       CSEPairs.push_back(std::make_pair(OldReg, NewReg));
642       --NumDefs;
643     }
644 
645     // Actually perform the elimination.
646     if (DoCSE) {
647       for (const std::pair<unsigned, unsigned> &CSEPair : CSEPairs) {
648         unsigned OldReg = CSEPair.first;
649         unsigned NewReg = CSEPair.second;
650         // OldReg may have been unused but is used now, clear the Dead flag
651         MachineInstr *Def = MRI->getUniqueVRegDef(NewReg);
652         assert(Def != nullptr && "CSEd register has no unique definition?");
653         Def->clearRegisterDeads(NewReg);
654         // Replace with NewReg and clear kill flags which may be wrong now.
655         MRI->replaceRegWith(OldReg, NewReg);
656         MRI->clearKillFlags(NewReg);
657       }
658 
659       // Go through implicit defs of CSMI and MI, if a def is not dead at MI,
660       // we should make sure it is not dead at CSMI.
661       for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
662         CSMI->getOperand(ImplicitDefToUpdate).setIsDead(false);
663       for (const auto &PhysDef : PhysDefs)
664         if (!MI->getOperand(PhysDef.first).isDead())
665           CSMI->getOperand(PhysDef.first).setIsDead(false);
666 
667       // Go through implicit defs of CSMI and MI, and clear the kill flags on
668       // their uses in all the instructions between CSMI and MI.
669       // We might have made some of the kill flags redundant, consider:
670       //   subs  ... implicit-def %nzcv    <- CSMI
671       //   csinc ... implicit killed %nzcv <- this kill flag isn't valid anymore
672       //   subs  ... implicit-def %nzcv    <- MI, to be eliminated
673       //   csinc ... implicit killed %nzcv
674       // Since we eliminated MI, and reused a register imp-def'd by CSMI
675       // (here %nzcv), that register, if it was killed before MI, should have
676       // that kill flag removed, because it's lifetime was extended.
677       if (CSMI->getParent() == MI->getParent()) {
678         for (MachineBasicBlock::iterator II = CSMI, IE = MI; II != IE; ++II)
679           for (auto ImplicitDef : ImplicitDefs)
680             if (MachineOperand *MO = II->findRegisterUseOperand(
681                     ImplicitDef, /*isKill=*/true, TRI))
682               MO->setIsKill(false);
683       } else {
684         // If the instructions aren't in the same BB, bail out and clear the
685         // kill flag on all uses of the imp-def'd register.
686         for (auto ImplicitDef : ImplicitDefs)
687           MRI->clearKillFlags(ImplicitDef);
688       }
689 
690       if (CrossMBBPhysDef) {
691         // Add physical register defs now coming in from a predecessor to MBB
692         // livein list.
693         while (!PhysDefs.empty()) {
694           auto LiveIn = PhysDefs.pop_back_val();
695           if (!MBB->isLiveIn(LiveIn.second))
696             MBB->addLiveIn(LiveIn.second);
697         }
698         ++NumCrossBBCSEs;
699       }
700 
701       MI->eraseFromParent();
702       ++NumCSEs;
703       if (!PhysRefs.empty())
704         ++NumPhysCSEs;
705       if (Commuted)
706         ++NumCommutes;
707       Changed = true;
708     } else {
709       VNT.insert(MI, CurrVN++);
710       Exps.push_back(MI);
711     }
712     CSEPairs.clear();
713     ImplicitDefsToUpdate.clear();
714     ImplicitDefs.clear();
715   }
716 
717   return Changed;
718 }
719 
720 /// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given
721 /// dominator tree node if its a leaf or all of its children are done. Walk
722 /// up the dominator tree to destroy ancestors which are now done.
723 void
724 MachineCSE::ExitScopeIfDone(MachineDomTreeNode *Node,
725                         DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren) {
726   if (OpenChildren[Node])
727     return;
728 
729   // Pop scope.
730   ExitScope(Node->getBlock());
731 
732   // Now traverse upwards to pop ancestors whose offsprings are all done.
733   while (MachineDomTreeNode *Parent = Node->getIDom()) {
734     unsigned Left = --OpenChildren[Parent];
735     if (Left != 0)
736       break;
737     ExitScope(Parent->getBlock());
738     Node = Parent;
739   }
740 }
741 
742 bool MachineCSE::PerformCSE(MachineDomTreeNode *Node) {
743   SmallVector<MachineDomTreeNode*, 32> Scopes;
744   SmallVector<MachineDomTreeNode*, 8> WorkList;
745   DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
746 
747   CurrVN = 0;
748 
749   // Perform a DFS walk to determine the order of visit.
750   WorkList.push_back(Node);
751   do {
752     Node = WorkList.pop_back_val();
753     Scopes.push_back(Node);
754     OpenChildren[Node] = Node->getNumChildren();
755     append_range(WorkList, Node->children());
756   } while (!WorkList.empty());
757 
758   // Now perform CSE.
759   bool Changed = false;
760   for (MachineDomTreeNode *Node : Scopes) {
761     MachineBasicBlock *MBB = Node->getBlock();
762     EnterScope(MBB);
763     Changed |= ProcessBlockCSE(MBB);
764     // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
765     ExitScopeIfDone(Node, OpenChildren);
766   }
767 
768   return Changed;
769 }
770 
771 // We use stronger checks for PRE candidate rather than for CSE ones to embrace
772 // checks inside ProcessBlockCSE(), not only inside isCSECandidate(). This helps
773 // to exclude instrs created by PRE that won't be CSEed later.
774 bool MachineCSE::isPRECandidate(MachineInstr *MI) {
775   if (!isCSECandidate(MI) ||
776       MI->isNotDuplicable() ||
777       MI->mayLoad() ||
778       MI->isAsCheapAsAMove() ||
779       MI->getNumDefs() != 1 ||
780       MI->getNumExplicitDefs() != 1)
781     return false;
782 
783   for (const auto &def : MI->defs())
784     if (!Register::isVirtualRegister(def.getReg()))
785       return false;
786 
787   for (const auto &use : MI->uses())
788     if (use.isReg() && !Register::isVirtualRegister(use.getReg()))
789       return false;
790 
791   return true;
792 }
793 
794 bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT,
795                                  MachineBasicBlock *MBB) {
796   bool Changed = false;
797   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
798     MachineInstr *MI = &*I;
799     ++I;
800 
801     if (!isPRECandidate(MI))
802       continue;
803 
804     if (!PREMap.count(MI)) {
805       PREMap[MI] = MBB;
806       continue;
807     }
808 
809     auto MBB1 = PREMap[MI];
810     assert(
811         !DT->properlyDominates(MBB, MBB1) &&
812         "MBB cannot properly dominate MBB1 while DFS through dominators tree!");
813     auto CMBB = DT->findNearestCommonDominator(MBB, MBB1);
814     if (!CMBB->isLegalToHoistInto())
815       continue;
816 
817     if (!isProfitableToHoistInto(CMBB, MBB, MBB1))
818       continue;
819 
820     // Two instrs are partial redundant if their basic blocks are reachable
821     // from one to another but one doesn't dominate another.
822     if (CMBB != MBB1) {
823       auto BB = MBB->getBasicBlock(), BB1 = MBB1->getBasicBlock();
824       if (BB != nullptr && BB1 != nullptr &&
825           (isPotentiallyReachable(BB1, BB) ||
826            isPotentiallyReachable(BB, BB1))) {
827 
828         assert(MI->getOperand(0).isDef() &&
829                "First operand of instr with one explicit def must be this def");
830         Register VReg = MI->getOperand(0).getReg();
831         Register NewReg = MRI->cloneVirtualRegister(VReg);
832         if (!isProfitableToCSE(NewReg, VReg, CMBB, MI))
833           continue;
834         MachineInstr &NewMI =
835             TII->duplicate(*CMBB, CMBB->getFirstTerminator(), *MI);
836 
837         // When hoisting, make sure we don't carry the debug location of
838         // the original instruction, as that's not correct and can cause
839         // unexpected jumps when debugging optimized code.
840         auto EmptyDL = DebugLoc();
841         NewMI.setDebugLoc(EmptyDL);
842 
843         NewMI.getOperand(0).setReg(NewReg);
844 
845         PREMap[MI] = CMBB;
846         ++NumPREs;
847         Changed = true;
848       }
849     }
850   }
851   return Changed;
852 }
853 
854 // This simple PRE (partial redundancy elimination) pass doesn't actually
855 // eliminate partial redundancy but transforms it to full redundancy,
856 // anticipating that the next CSE step will eliminate this created redundancy.
857 // If CSE doesn't eliminate this, than created instruction will remain dead
858 // and eliminated later by Remove Dead Machine Instructions pass.
859 bool MachineCSE::PerformSimplePRE(MachineDominatorTree *DT) {
860   SmallVector<MachineDomTreeNode *, 32> BBs;
861 
862   PREMap.clear();
863   bool Changed = false;
864   BBs.push_back(DT->getRootNode());
865   do {
866     auto Node = BBs.pop_back_val();
867     append_range(BBs, Node->children());
868 
869     MachineBasicBlock *MBB = Node->getBlock();
870     Changed |= ProcessBlockPRE(DT, MBB);
871 
872   } while (!BBs.empty());
873 
874   return Changed;
875 }
876 
877 bool MachineCSE::isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
878                                          MachineBasicBlock *MBB,
879                                          MachineBasicBlock *MBB1) {
880   if (CandidateBB->getParent()->getFunction().hasMinSize())
881     return true;
882   assert(DT->dominates(CandidateBB, MBB) && "CandidateBB should dominate MBB");
883   assert(DT->dominates(CandidateBB, MBB1) &&
884          "CandidateBB should dominate MBB1");
885   return MBFI->getBlockFreq(CandidateBB) <=
886          MBFI->getBlockFreq(MBB) + MBFI->getBlockFreq(MBB1);
887 }
888 
889 bool MachineCSE::runOnMachineFunction(MachineFunction &MF) {
890   if (skipFunction(MF.getFunction()))
891     return false;
892 
893   TII = MF.getSubtarget().getInstrInfo();
894   TRI = MF.getSubtarget().getRegisterInfo();
895   MRI = &MF.getRegInfo();
896   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
897   DT = &getAnalysis<MachineDominatorTree>();
898   MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
899   LookAheadLimit = TII->getMachineCSELookAheadLimit();
900   bool ChangedPRE, ChangedCSE;
901   ChangedPRE = PerformSimplePRE(DT);
902   ChangedCSE = PerformCSE(DT->getRootNode());
903   return ChangedPRE || ChangedCSE;
904 }
905