xref: /aosp_15_r20/external/llvm/lib/Target/WebAssembly/WebAssemblyOptimizeLiveIntervals.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker ///
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief Optimize LiveIntervals for use in a post-RA context.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker /// LiveIntervals normally runs before register allocation when the code is
14*9880d681SAndroid Build Coastguard Worker /// only recently lowered out of SSA form, so it's uncommon for registers to
15*9880d681SAndroid Build Coastguard Worker /// have multiple defs, and then they do, the defs are usually closely related.
16*9880d681SAndroid Build Coastguard Worker /// Later, after coalescing, tail duplication, and other optimizations, it's
17*9880d681SAndroid Build Coastguard Worker /// more common to see registers with multiple unrelated defs. This pass
18*9880d681SAndroid Build Coastguard Worker /// updates LiveIntervalAnalysis to distribute the value numbers across separate
19*9880d681SAndroid Build Coastguard Worker /// LiveIntervals.
20*9880d681SAndroid Build Coastguard Worker ///
21*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker #include "WebAssembly.h"
24*9880d681SAndroid Build Coastguard Worker #include "WebAssemblySubtarget.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveIntervalAnalysis.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "wasm-optimize-live-intervals"
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker namespace {
36*9880d681SAndroid Build Coastguard Worker class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
getPassName() const37*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
38*9880d681SAndroid Build Coastguard Worker     return "WebAssembly Optimize Live Intervals";
39*9880d681SAndroid Build Coastguard Worker   }
40*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const41*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
42*9880d681SAndroid Build Coastguard Worker     AU.setPreservesCFG();
43*9880d681SAndroid Build Coastguard Worker     AU.addRequired<LiveIntervals>();
44*9880d681SAndroid Build Coastguard Worker     AU.addPreserved<MachineBlockFrequencyInfo>();
45*9880d681SAndroid Build Coastguard Worker     AU.addPreserved<SlotIndexes>();
46*9880d681SAndroid Build Coastguard Worker     AU.addPreserved<LiveIntervals>();
47*9880d681SAndroid Build Coastguard Worker     AU.addPreservedID(LiveVariablesID);
48*9880d681SAndroid Build Coastguard Worker     AU.addPreservedID(MachineDominatorsID);
49*9880d681SAndroid Build Coastguard Worker     MachineFunctionPass::getAnalysisUsage(AU);
50*9880d681SAndroid Build Coastguard Worker   }
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &MF) override;
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker public:
55*9880d681SAndroid Build Coastguard Worker   static char ID; // Pass identification, replacement for typeid
WebAssemblyOptimizeLiveIntervals()56*9880d681SAndroid Build Coastguard Worker   WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
57*9880d681SAndroid Build Coastguard Worker };
58*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker char WebAssemblyOptimizeLiveIntervals::ID = 0;
createWebAssemblyOptimizeLiveIntervals()61*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() {
62*9880d681SAndroid Build Coastguard Worker   return new WebAssemblyOptimizeLiveIntervals();
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)65*9880d681SAndroid Build Coastguard Worker bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(MachineFunction &MF) {
66*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
67*9880d681SAndroid Build Coastguard Worker                   "********** Function: "
68*9880d681SAndroid Build Coastguard Worker                << MF.getName() << '\n');
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo &MRI = MF.getRegInfo();
71*9880d681SAndroid Build Coastguard Worker   LiveIntervals &LIS = getAnalysis<LiveIntervals>();
72*9880d681SAndroid Build Coastguard Worker 
73*9880d681SAndroid Build Coastguard Worker   // We don't preserve SSA form.
74*9880d681SAndroid Build Coastguard Worker   MRI.leaveSSA();
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker   assert(MRI.tracksLiveness() &&
77*9880d681SAndroid Build Coastguard Worker          "OptimizeLiveIntervals expects liveness");
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   // Split multiple-VN LiveIntervals into multiple LiveIntervals.
80*9880d681SAndroid Build Coastguard Worker   SmallVector<LiveInterval*, 4> SplitLIs;
81*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
82*9880d681SAndroid Build Coastguard Worker     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
83*9880d681SAndroid Build Coastguard Worker     if (MRI.reg_nodbg_empty(Reg))
84*9880d681SAndroid Build Coastguard Worker       continue;
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker     LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
87*9880d681SAndroid Build Coastguard Worker     SplitLIs.clear();
88*9880d681SAndroid Build Coastguard Worker   }
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker   // In PrepareForLiveIntervals, we conservatively inserted IMPLICIT_DEF
91*9880d681SAndroid Build Coastguard Worker   // instructions to satisfy LiveIntervals' requirement that all uses be
92*9880d681SAndroid Build Coastguard Worker   // dominated by defs. Now that LiveIntervals has computed which of these
93*9880d681SAndroid Build Coastguard Worker   // defs are actually needed and which are dead, remove the dead ones.
94*9880d681SAndroid Build Coastguard Worker   for (auto MII = MF.begin()->begin(), MIE = MF.begin()->end(); MII != MIE; ) {
95*9880d681SAndroid Build Coastguard Worker     MachineInstr *MI = &*MII++;
96*9880d681SAndroid Build Coastguard Worker     if (MI->isImplicitDef() && MI->getOperand(0).isDead()) {
97*9880d681SAndroid Build Coastguard Worker       LiveInterval &LI = LIS.getInterval(MI->getOperand(0).getReg());
98*9880d681SAndroid Build Coastguard Worker       LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(*MI).getRegSlot());
99*9880d681SAndroid Build Coastguard Worker       LIS.RemoveMachineInstrFromMaps(*MI);
100*9880d681SAndroid Build Coastguard Worker       MI->eraseFromParent();
101*9880d681SAndroid Build Coastguard Worker     }
102*9880d681SAndroid Build Coastguard Worker   }
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker   return false;
105*9880d681SAndroid Build Coastguard Worker }
106