xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeShrink.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- LiveRangeShrink.cpp - Move instructions to shrink live range -------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This pass moves instructions close to the definition of its operands to
110b57cec5SDimitry Andric /// shrink live range of the def instruction. The code motion is limited within
120b57cec5SDimitry Andric /// the basic block. The moved instruction should have 1 def, and more than one
130b57cec5SDimitry Andric /// uses, all of which are the only use of the def.
140b57cec5SDimitry Andric ///
150b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
180b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
190b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
26*5f757f3fSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
27480093f4SDimitry Andric #include "llvm/InitializePasses.h"
280b57cec5SDimitry Andric #include "llvm/Pass.h"
290b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
300b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric #include <iterator>
320b57cec5SDimitry Andric #include <utility>
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric using namespace llvm;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric #define DEBUG_TYPE "lrshrink"
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric STATISTIC(NumInstrsHoistedToShrinkLiveRange,
390b57cec5SDimitry Andric           "Number of insructions hoisted to shrink live range.");
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric namespace {
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric class LiveRangeShrink : public MachineFunctionPass {
440b57cec5SDimitry Andric public:
450b57cec5SDimitry Andric   static char ID;
460b57cec5SDimitry Andric 
LiveRangeShrink()470b57cec5SDimitry Andric   LiveRangeShrink() : MachineFunctionPass(ID) {
480b57cec5SDimitry Andric     initializeLiveRangeShrinkPass(*PassRegistry::getPassRegistry());
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const510b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
520b57cec5SDimitry Andric     AU.setPreservesCFG();
530b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric 
getPassName() const560b57cec5SDimitry Andric   StringRef getPassName() const override { return "Live Range Shrink"; }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
590b57cec5SDimitry Andric };
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric } // end anonymous namespace
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric char LiveRangeShrink::ID = 0;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric char &llvm::LiveRangeShrinkID = LiveRangeShrink::ID;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric INITIALIZE_PASS(LiveRangeShrink, "lrshrink", "Live Range Shrink Pass", false,
680b57cec5SDimitry Andric                 false)
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric using InstOrderMap = DenseMap<MachineInstr *, unsigned>;
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric /// Returns \p New if it's dominated by \p Old, otherwise return \p Old.
730b57cec5SDimitry Andric /// \p M maintains a map from instruction to its dominating order that satisfies
740b57cec5SDimitry Andric /// M[A] > M[B] guarantees that A is dominated by B.
750b57cec5SDimitry Andric /// If \p New is not in \p M, return \p Old. Otherwise if \p Old is null, return
760b57cec5SDimitry Andric /// \p New.
FindDominatedInstruction(MachineInstr & New,MachineInstr * Old,const InstOrderMap & M)770b57cec5SDimitry Andric static MachineInstr *FindDominatedInstruction(MachineInstr &New,
780b57cec5SDimitry Andric                                               MachineInstr *Old,
790b57cec5SDimitry Andric                                               const InstOrderMap &M) {
800b57cec5SDimitry Andric   auto NewIter = M.find(&New);
810b57cec5SDimitry Andric   if (NewIter == M.end())
820b57cec5SDimitry Andric     return Old;
830b57cec5SDimitry Andric   if (Old == nullptr)
840b57cec5SDimitry Andric     return &New;
850b57cec5SDimitry Andric   unsigned OrderOld = M.find(Old)->second;
860b57cec5SDimitry Andric   unsigned OrderNew = NewIter->second;
870b57cec5SDimitry Andric   if (OrderOld != OrderNew)
880b57cec5SDimitry Andric     return OrderOld < OrderNew ? &New : Old;
890b57cec5SDimitry Andric   // OrderOld == OrderNew, we need to iterate down from Old to see if it
900b57cec5SDimitry Andric   // can reach New, if yes, New is dominated by Old.
910b57cec5SDimitry Andric   for (MachineInstr *I = Old->getNextNode(); M.find(I)->second == OrderNew;
920b57cec5SDimitry Andric        I = I->getNextNode())
930b57cec5SDimitry Andric     if (I == &New)
940b57cec5SDimitry Andric       return &New;
950b57cec5SDimitry Andric   return Old;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric /// Builds Instruction to its dominating order number map \p M by traversing
990b57cec5SDimitry Andric /// from instruction \p Start.
BuildInstOrderMap(MachineBasicBlock::iterator Start,InstOrderMap & M)1000b57cec5SDimitry Andric static void BuildInstOrderMap(MachineBasicBlock::iterator Start,
1010b57cec5SDimitry Andric                               InstOrderMap &M) {
1020b57cec5SDimitry Andric   M.clear();
1030b57cec5SDimitry Andric   unsigned i = 0;
1040b57cec5SDimitry Andric   for (MachineInstr &I : make_range(Start, Start->getParent()->end()))
1050b57cec5SDimitry Andric     M[&I] = i++;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)1080b57cec5SDimitry Andric bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) {
1090b57cec5SDimitry Andric   if (skipFunction(MF.getFunction()))
1100b57cec5SDimitry Andric     return false;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
113*5f757f3fSDimitry Andric   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "**** Analysing " << MF.getName() << '\n');
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   InstOrderMap IOM;
1180b57cec5SDimitry Andric   // Map from register to instruction order (value of IOM) where the
1190b57cec5SDimitry Andric   // register is used last. When moving instructions up, we need to
1200b57cec5SDimitry Andric   // make sure all its defs (including dead def) will not cross its
1210b57cec5SDimitry Andric   // last use when moving up.
1220b57cec5SDimitry Andric   DenseMap<unsigned, std::pair<unsigned, MachineInstr *>> UseMap;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
1250b57cec5SDimitry Andric     if (MBB.empty())
1260b57cec5SDimitry Andric       continue;
1270b57cec5SDimitry Andric     bool SawStore = false;
1280b57cec5SDimitry Andric     BuildInstOrderMap(MBB.begin(), IOM);
1290b57cec5SDimitry Andric     UseMap.clear();
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric     for (MachineBasicBlock::iterator Next = MBB.begin(); Next != MBB.end();) {
1320b57cec5SDimitry Andric       MachineInstr &MI = *Next;
1330b57cec5SDimitry Andric       ++Next;
134fe6060f1SDimitry Andric       if (MI.isPHI() || MI.isDebugOrPseudoInstr())
1350b57cec5SDimitry Andric         continue;
1360b57cec5SDimitry Andric       if (MI.mayStore())
1370b57cec5SDimitry Andric         SawStore = true;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric       unsigned CurrentOrder = IOM[&MI];
1400b57cec5SDimitry Andric       unsigned Barrier = 0;
1410b57cec5SDimitry Andric       MachineInstr *BarrierMI = nullptr;
1420b57cec5SDimitry Andric       for (const MachineOperand &MO : MI.operands()) {
1430b57cec5SDimitry Andric         if (!MO.isReg() || MO.isDebug())
1440b57cec5SDimitry Andric           continue;
1450b57cec5SDimitry Andric         if (MO.isUse())
1460b57cec5SDimitry Andric           UseMap[MO.getReg()] = std::make_pair(CurrentOrder, &MI);
1470b57cec5SDimitry Andric         else if (MO.isDead() && UseMap.count(MO.getReg()))
1480b57cec5SDimitry Andric           // Barrier is the last instruction where MO get used. MI should not
1490b57cec5SDimitry Andric           // be moved above Barrier.
1500b57cec5SDimitry Andric           if (Barrier < UseMap[MO.getReg()].first) {
1510b57cec5SDimitry Andric             Barrier = UseMap[MO.getReg()].first;
1520b57cec5SDimitry Andric             BarrierMI = UseMap[MO.getReg()].second;
1530b57cec5SDimitry Andric           }
1540b57cec5SDimitry Andric       }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric       if (!MI.isSafeToMove(nullptr, SawStore)) {
1570b57cec5SDimitry Andric         // If MI has side effects, it should become a barrier for code motion.
1580b57cec5SDimitry Andric         // IOM is rebuild from the next instruction to prevent later
1590b57cec5SDimitry Andric         // instructions from being moved before this MI.
160d409305fSDimitry Andric         if (MI.hasUnmodeledSideEffects() && !MI.isPseudoProbe() &&
161d409305fSDimitry Andric             Next != MBB.end()) {
1620b57cec5SDimitry Andric           BuildInstOrderMap(Next, IOM);
1630b57cec5SDimitry Andric           SawStore = false;
1640b57cec5SDimitry Andric         }
1650b57cec5SDimitry Andric         continue;
1660b57cec5SDimitry Andric       }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric       const MachineOperand *DefMO = nullptr;
1690b57cec5SDimitry Andric       MachineInstr *Insert = nullptr;
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric       // Number of live-ranges that will be shortened. We do not count
1720b57cec5SDimitry Andric       // live-ranges that are defined by a COPY as it could be coalesced later.
1730b57cec5SDimitry Andric       unsigned NumEligibleUse = 0;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric       for (const MachineOperand &MO : MI.operands()) {
1760b57cec5SDimitry Andric         if (!MO.isReg() || MO.isDead() || MO.isDebug())
1770b57cec5SDimitry Andric           continue;
1788bcb0991SDimitry Andric         Register Reg = MO.getReg();
1790b57cec5SDimitry Andric         // Do not move the instruction if it def/uses a physical register,
1800b57cec5SDimitry Andric         // unless it is a constant physical register or a noreg.
181bdd1243dSDimitry Andric         if (!Reg.isVirtual()) {
1820b57cec5SDimitry Andric           if (!Reg || MRI.isConstantPhysReg(Reg))
1830b57cec5SDimitry Andric             continue;
1840b57cec5SDimitry Andric           Insert = nullptr;
1850b57cec5SDimitry Andric           break;
1860b57cec5SDimitry Andric         }
1870b57cec5SDimitry Andric         if (MO.isDef()) {
1880b57cec5SDimitry Andric           // Do not move if there is more than one def.
1890b57cec5SDimitry Andric           if (DefMO) {
1900b57cec5SDimitry Andric             Insert = nullptr;
1910b57cec5SDimitry Andric             break;
1920b57cec5SDimitry Andric           }
1930b57cec5SDimitry Andric           DefMO = &MO;
1940b57cec5SDimitry Andric         } else if (MRI.hasOneNonDBGUse(Reg) && MRI.hasOneDef(Reg) && DefMO &&
1950b57cec5SDimitry Andric                    MRI.getRegClass(DefMO->getReg()) ==
1960b57cec5SDimitry Andric                        MRI.getRegClass(MO.getReg())) {
1970b57cec5SDimitry Andric           // The heuristic does not handle different register classes yet
1980b57cec5SDimitry Andric           // (registers of different sizes, looser/tighter constraints). This
1990b57cec5SDimitry Andric           // is because it needs more accurate model to handle register
2000b57cec5SDimitry Andric           // pressure correctly.
2010b57cec5SDimitry Andric           MachineInstr &DefInstr = *MRI.def_instr_begin(Reg);
202*5f757f3fSDimitry Andric           if (!TII.isCopyInstr(DefInstr))
2030b57cec5SDimitry Andric             NumEligibleUse++;
2040b57cec5SDimitry Andric           Insert = FindDominatedInstruction(DefInstr, Insert, IOM);
2050b57cec5SDimitry Andric         } else {
2060b57cec5SDimitry Andric           Insert = nullptr;
2070b57cec5SDimitry Andric           break;
2080b57cec5SDimitry Andric         }
2090b57cec5SDimitry Andric       }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric       // If Barrier equals IOM[I], traverse forward to find if BarrierMI is
2120b57cec5SDimitry Andric       // after Insert, if yes, then we should not hoist.
2130b57cec5SDimitry Andric       for (MachineInstr *I = Insert; I && IOM[I] == Barrier;
2140b57cec5SDimitry Andric            I = I->getNextNode())
2150b57cec5SDimitry Andric         if (I == BarrierMI) {
2160b57cec5SDimitry Andric           Insert = nullptr;
2170b57cec5SDimitry Andric           break;
2180b57cec5SDimitry Andric         }
2190b57cec5SDimitry Andric       // Move the instruction when # of shrunk live range > 1.
2200b57cec5SDimitry Andric       if (DefMO && Insert && NumEligibleUse > 1 && Barrier <= IOM[Insert]) {
2210b57cec5SDimitry Andric         MachineBasicBlock::iterator I = std::next(Insert->getIterator());
2220b57cec5SDimitry Andric         // Skip all the PHI and debug instructions.
223fe6060f1SDimitry Andric         while (I != MBB.end() && (I->isPHI() || I->isDebugOrPseudoInstr()))
2240b57cec5SDimitry Andric           I = std::next(I);
2250b57cec5SDimitry Andric         if (I == MI.getIterator())
2260b57cec5SDimitry Andric           continue;
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric         // Update the dominator order to be the same as the insertion point.
2290b57cec5SDimitry Andric         // We do this to maintain a non-decreasing order without need to update
2300b57cec5SDimitry Andric         // all instruction orders after the insertion point.
2310b57cec5SDimitry Andric         unsigned NewOrder = IOM[&*I];
2320b57cec5SDimitry Andric         IOM[&MI] = NewOrder;
2330b57cec5SDimitry Andric         NumInstrsHoistedToShrinkLiveRange++;
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric         // Find MI's debug value following MI.
2360b57cec5SDimitry Andric         MachineBasicBlock::iterator EndIter = std::next(MI.getIterator());
2370b57cec5SDimitry Andric         if (MI.getOperand(0).isReg())
2380b57cec5SDimitry Andric           for (; EndIter != MBB.end() && EndIter->isDebugValue() &&
239fe6060f1SDimitry Andric                  EndIter->hasDebugOperandForReg(MI.getOperand(0).getReg());
2400b57cec5SDimitry Andric                ++EndIter, ++Next)
2410b57cec5SDimitry Andric             IOM[&*EndIter] = NewOrder;
2420b57cec5SDimitry Andric         MBB.splice(I, &MBB, MI.getIterator(), EndIter);
2430b57cec5SDimitry Andric       }
2440b57cec5SDimitry Andric     }
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric   return false;
2470b57cec5SDimitry Andric }
248