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 { 370b57cec5SDimitry Andric StringRef getPassName() const override { 380b57cec5SDimitry Andric return "WebAssembly Optimize Live Intervals"; 390b57cec5SDimitry Andric } 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 420b57cec5SDimitry Andric AU.setPreservesCFG(); 43*0fca6ea1SDimitry Andric AU.addRequired<LiveIntervalsWrapperPass>(); 44*0fca6ea1SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>(); 45*0fca6ea1SDimitry Andric AU.addPreserved<SlotIndexesWrapperPass>(); 46*0fca6ea1SDimitry Andric AU.addPreserved<LiveIntervalsWrapperPass>(); 470b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID); 480b57cec5SDimitry Andric AU.addPreservedID(MachineDominatorsID); 490b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 5281ad6265SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 5381ad6265SDimitry Andric return MachineFunctionProperties().set( 5481ad6265SDimitry Andric MachineFunctionProperties::Property::TracksLiveness); 5581ad6265SDimitry Andric } 5681ad6265SDimitry Andric 570b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric public: 600b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 610b57cec5SDimitry Andric WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {} 620b57cec5SDimitry Andric }; 630b57cec5SDimitry Andric } // end anonymous namespace 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric char WebAssemblyOptimizeLiveIntervals::ID = 0; 660b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE, 670b57cec5SDimitry Andric "Optimize LiveIntervals for WebAssembly", false, false) 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyOptimizeLiveIntervals() { 700b57cec5SDimitry Andric return new WebAssemblyOptimizeLiveIntervals(); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction( 740b57cec5SDimitry Andric MachineFunction &MF) { 750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n" 760b57cec5SDimitry Andric "********** Function: " 770b57cec5SDimitry Andric << MF.getName() << '\n'); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 80*0fca6ea1SDimitry Andric auto &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS(); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // We don't preserve SSA form. 830b57cec5SDimitry Andric MRI.leaveSSA(); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness"); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric // Split multiple-VN LiveIntervals into multiple LiveIntervals. 880b57cec5SDimitry Andric SmallVector<LiveInterval *, 4> SplitLIs; 890b57cec5SDimitry Andric for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) { 9004eeddc0SDimitry Andric Register Reg = Register::index2VirtReg(I); 915ffd83dbSDimitry Andric auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); 925ffd83dbSDimitry Andric 930b57cec5SDimitry Andric if (MRI.reg_nodbg_empty(Reg)) 940b57cec5SDimitry Andric continue; 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs); 975ffd83dbSDimitry Andric if (Reg == TRI.getFrameRegister(MF) && SplitLIs.size() > 0) { 985ffd83dbSDimitry Andric // The live interval for the frame register was split, resulting in a new 995ffd83dbSDimitry Andric // VReg. For now we only support debug info output for a single frame base 1005ffd83dbSDimitry Andric // value for the function, so just use the last one. It will certainly be 1015ffd83dbSDimitry Andric // wrong for some part of the function, but until we are able to track 1025ffd83dbSDimitry Andric // values through live-range splitting and stackification, it will have to 1035ffd83dbSDimitry Andric // do. 1045ffd83dbSDimitry Andric MF.getInfo<WebAssemblyFunctionInfo>()->setFrameBaseVreg( 105e8d8bef9SDimitry Andric SplitLIs.back()->reg()); 1065ffd83dbSDimitry Andric } 1070b57cec5SDimitry Andric SplitLIs.clear(); 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 11081ad6265SDimitry Andric // In FixIrreducibleControlFlow, we conservatively inserted IMPLICIT_DEF 1110b57cec5SDimitry Andric // instructions to satisfy LiveIntervals' requirement that all uses be 1120b57cec5SDimitry Andric // dominated by defs. Now that LiveIntervals has computed which of these 1130b57cec5SDimitry Andric // defs are actually needed and which are dead, remove the dead ones. 114349cc55cSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MF.front())) { 115349cc55cSDimitry Andric if (MI.isImplicitDef() && MI.getOperand(0).isDead()) { 116349cc55cSDimitry Andric LiveInterval &LI = LIS.getInterval(MI.getOperand(0).getReg()); 117349cc55cSDimitry Andric LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(MI).getRegSlot()); 118349cc55cSDimitry Andric LIS.RemoveMachineInstrFromMaps(MI); 119349cc55cSDimitry Andric MI.eraseFromParent(); 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 1235ffd83dbSDimitry Andric return true; 1240b57cec5SDimitry Andric } 125