10b57cec5SDimitry Andric //===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
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 /// Optimize LiveIntervals for use in a post-RA context.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric /// LiveIntervals normally runs before register allocation when the code is
130b57cec5SDimitry Andric /// only recently lowered out of SSA form, so it's uncommon for registers to
140b57cec5SDimitry Andric /// have multiple defs, and when they do, the defs are usually closely related.
150b57cec5SDimitry Andric /// Later, after coalescing, tail duplication, and other optimizations, it's
160b57cec5SDimitry Andric /// more common to see registers with multiple unrelated defs. This pass
170b57cec5SDimitry Andric /// updates LiveIntervals to distribute the value numbers across separate
180b57cec5SDimitry Andric /// LiveIntervals.
190b57cec5SDimitry Andric ///
200b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric #include "WebAssembly.h"
235ffd83dbSDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
240b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
290b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
300b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric using namespace llvm;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-optimize-live-intervals"
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric namespace {
360b57cec5SDimitry Andric class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
getPassName() const370b57cec5SDimitry Andric StringRef getPassName() const override {
380b57cec5SDimitry Andric return "WebAssembly Optimize Live Intervals";
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const410b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
420b57cec5SDimitry Andric AU.setPreservesCFG();
430b57cec5SDimitry Andric AU.addRequired<LiveIntervals>();
440b57cec5SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfo>();
450b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>();
460b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>();
470b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID);
480b57cec5SDimitry Andric AU.addPreservedID(MachineDominatorsID);
490b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric public:
550b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
WebAssemblyOptimizeLiveIntervals()560b57cec5SDimitry Andric WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
570b57cec5SDimitry Andric };
580b57cec5SDimitry Andric } // end anonymous namespace
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric char WebAssemblyOptimizeLiveIntervals::ID = 0;
610b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE,
620b57cec5SDimitry Andric "Optimize LiveIntervals for WebAssembly", false, false)
630b57cec5SDimitry Andric
createWebAssemblyOptimizeLiveIntervals()640b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() {
650b57cec5SDimitry Andric return new WebAssemblyOptimizeLiveIntervals();
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)680b57cec5SDimitry Andric bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(
690b57cec5SDimitry Andric MachineFunction &MF) {
700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
710b57cec5SDimitry Andric "********** Function: "
720b57cec5SDimitry Andric << MF.getName() << '\n');
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
750b57cec5SDimitry Andric auto &LIS = getAnalysis<LiveIntervals>();
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric // We don't preserve SSA form.
780b57cec5SDimitry Andric MRI.leaveSSA();
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness");
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric // Split multiple-VN LiveIntervals into multiple LiveIntervals.
830b57cec5SDimitry Andric SmallVector<LiveInterval *, 4> SplitLIs;
840b57cec5SDimitry Andric for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
858bcb0991SDimitry Andric unsigned Reg = Register::index2VirtReg(I);
865ffd83dbSDimitry Andric auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
875ffd83dbSDimitry Andric
880b57cec5SDimitry Andric if (MRI.reg_nodbg_empty(Reg))
890b57cec5SDimitry Andric continue;
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
925ffd83dbSDimitry Andric if (Reg == TRI.getFrameRegister(MF) && SplitLIs.size() > 0) {
935ffd83dbSDimitry Andric // The live interval for the frame register was split, resulting in a new
945ffd83dbSDimitry Andric // VReg. For now we only support debug info output for a single frame base
955ffd83dbSDimitry Andric // value for the function, so just use the last one. It will certainly be
965ffd83dbSDimitry Andric // wrong for some part of the function, but until we are able to track
975ffd83dbSDimitry Andric // values through live-range splitting and stackification, it will have to
985ffd83dbSDimitry Andric // do.
995ffd83dbSDimitry Andric MF.getInfo<WebAssemblyFunctionInfo>()->setFrameBaseVreg(
100*af732203SDimitry Andric SplitLIs.back()->reg());
1015ffd83dbSDimitry Andric }
1020b57cec5SDimitry Andric SplitLIs.clear();
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric // In PrepareForLiveIntervals, we conservatively inserted IMPLICIT_DEF
1060b57cec5SDimitry Andric // instructions to satisfy LiveIntervals' requirement that all uses be
1070b57cec5SDimitry Andric // dominated by defs. Now that LiveIntervals has computed which of these
1080b57cec5SDimitry Andric // defs are actually needed and which are dead, remove the dead ones.
1090b57cec5SDimitry Andric for (auto MII = MF.begin()->begin(), MIE = MF.begin()->end(); MII != MIE;) {
1100b57cec5SDimitry Andric MachineInstr *MI = &*MII++;
1110b57cec5SDimitry Andric if (MI->isImplicitDef() && MI->getOperand(0).isDead()) {
1120b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(MI->getOperand(0).getReg());
1130b57cec5SDimitry Andric LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(*MI).getRegSlot());
1140b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI);
1150b57cec5SDimitry Andric MI->eraseFromParent();
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric
1195ffd83dbSDimitry Andric return true;
1200b57cec5SDimitry Andric }
121