1*0b57cec5SDimitry Andric //===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric ///
9*0b57cec5SDimitry Andric /// \file
10*0b57cec5SDimitry Andric /// Optimize LiveIntervals for use in a post-RA context.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric /// LiveIntervals normally runs before register allocation when the code is
13*0b57cec5SDimitry Andric /// only recently lowered out of SSA form, so it's uncommon for registers to
14*0b57cec5SDimitry Andric /// have multiple defs, and when they do, the defs are usually closely related.
15*0b57cec5SDimitry Andric /// Later, after coalescing, tail duplication, and other optimizations, it's
16*0b57cec5SDimitry Andric /// more common to see registers with multiple unrelated defs. This pass
17*0b57cec5SDimitry Andric /// updates LiveIntervals to distribute the value numbers across separate
18*0b57cec5SDimitry Andric /// LiveIntervals.
19*0b57cec5SDimitry Andric ///
20*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric #include "WebAssembly.h"
23*0b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
24*0b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
25*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
26*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
28*0b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
29*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
30*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
31*0b57cec5SDimitry Andric using namespace llvm;
32*0b57cec5SDimitry Andric
33*0b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-optimize-live-intervals"
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric namespace {
36*0b57cec5SDimitry Andric class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
getPassName() const37*0b57cec5SDimitry Andric StringRef getPassName() const override {
38*0b57cec5SDimitry Andric return "WebAssembly Optimize Live Intervals";
39*0b57cec5SDimitry Andric }
40*0b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const41*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
42*0b57cec5SDimitry Andric AU.setPreservesCFG();
43*0b57cec5SDimitry Andric AU.addRequired<LiveIntervals>();
44*0b57cec5SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfo>();
45*0b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>();
46*0b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>();
47*0b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID);
48*0b57cec5SDimitry Andric AU.addPreservedID(MachineDominatorsID);
49*0b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric
getRequiredProperties() const52*0b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
53*0b57cec5SDimitry Andric return MachineFunctionProperties().set(
54*0b57cec5SDimitry Andric MachineFunctionProperties::Property::TracksLiveness);
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric
57*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
58*0b57cec5SDimitry Andric
59*0b57cec5SDimitry Andric public:
60*0b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
WebAssemblyOptimizeLiveIntervals()61*0b57cec5SDimitry Andric WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
62*0b57cec5SDimitry Andric };
63*0b57cec5SDimitry Andric } // end anonymous namespace
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric char WebAssemblyOptimizeLiveIntervals::ID = 0;
66*0b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE,
67*0b57cec5SDimitry Andric "Optimize LiveIntervals for WebAssembly", false, false)
68*0b57cec5SDimitry Andric
createWebAssemblyOptimizeLiveIntervals()69*0b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() {
70*0b57cec5SDimitry Andric return new WebAssemblyOptimizeLiveIntervals();
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)73*0b57cec5SDimitry Andric bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(
74*0b57cec5SDimitry Andric MachineFunction &MF) {
75*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
76*0b57cec5SDimitry Andric "********** Function: "
77*0b57cec5SDimitry Andric << MF.getName() << '\n');
78*0b57cec5SDimitry Andric
79*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
80*0b57cec5SDimitry Andric auto &LIS = getAnalysis<LiveIntervals>();
81*0b57cec5SDimitry Andric
82*0b57cec5SDimitry Andric // We don't preserve SSA form.
83*0b57cec5SDimitry Andric MRI.leaveSSA();
84*0b57cec5SDimitry Andric
85*0b57cec5SDimitry Andric assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness");
86*0b57cec5SDimitry Andric
87*0b57cec5SDimitry Andric // Split multiple-VN LiveIntervals into multiple LiveIntervals.
88*0b57cec5SDimitry Andric SmallVector<LiveInterval *, 4> SplitLIs;
89*0b57cec5SDimitry Andric for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
90*0b57cec5SDimitry Andric Register Reg = Register::index2VirtReg(I);
91*0b57cec5SDimitry Andric auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
92*0b57cec5SDimitry Andric
93*0b57cec5SDimitry Andric if (MRI.reg_nodbg_empty(Reg))
94*0b57cec5SDimitry Andric continue;
95*0b57cec5SDimitry Andric
96*0b57cec5SDimitry Andric LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
97*0b57cec5SDimitry Andric if (Reg == TRI.getFrameRegister(MF) && SplitLIs.size() > 0) {
98*0b57cec5SDimitry Andric // The live interval for the frame register was split, resulting in a new
99*0b57cec5SDimitry Andric // VReg. For now we only support debug info output for a single frame base
100*0b57cec5SDimitry Andric // value for the function, so just use the last one. It will certainly be
101*0b57cec5SDimitry Andric // wrong for some part of the function, but until we are able to track
102*0b57cec5SDimitry Andric // values through live-range splitting and stackification, it will have to
103*0b57cec5SDimitry Andric // do.
104*0b57cec5SDimitry Andric MF.getInfo<WebAssemblyFunctionInfo>()->setFrameBaseVreg(
105*0b57cec5SDimitry Andric SplitLIs.back()->reg());
106*0b57cec5SDimitry Andric }
107*0b57cec5SDimitry Andric SplitLIs.clear();
108 }
109
110 // In FixIrreducibleControlFlow, we conservatively inserted IMPLICIT_DEF
111 // instructions to satisfy LiveIntervals' requirement that all uses be
112 // dominated by defs. Now that LiveIntervals has computed which of these
113 // defs are actually needed and which are dead, remove the dead ones.
114 for (MachineInstr &MI : llvm::make_early_inc_range(MF.front())) {
115 if (MI.isImplicitDef() && MI.getOperand(0).isDead()) {
116 LiveInterval &LI = LIS.getInterval(MI.getOperand(0).getReg());
117 LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(MI).getRegSlot());
118 LIS.RemoveMachineInstrFromMaps(MI);
119 MI.eraseFromParent();
120 }
121 }
122
123 return true;
124 }
125