LLVM 24.0.0git
WebAssemblyOptimizeLiveIntervals.cpp
Go to the documentation of this file.
1//===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Optimize LiveIntervals for use in a post-RA context.
11//
12/// LiveIntervals normally runs before register allocation when the code is
13/// only recently lowered out of SSA form, so it's uncommon for registers to
14/// have multiple defs, and when they do, the defs are usually closely related.
15/// Later, after coalescing, tail duplication, and other optimizations, it's
16/// more common to see registers with multiple unrelated defs. This pass
17/// updates LiveIntervals to distribute the value numbers across separate
18/// LiveIntervals.
19///
20//===----------------------------------------------------------------------===//
21
22#include "WebAssembly.h"
30#include "llvm/CodeGen/Passes.h"
32#include "llvm/IR/Analysis.h"
33#include "llvm/Support/Debug.h"
35using namespace llvm;
36
37#define DEBUG_TYPE "wasm-optimize-live-intervals"
38
39namespace {
40class WebAssemblyOptimizeLiveIntervalsLegacy final
41 : public MachineFunctionPass {
42 StringRef getPassName() const override {
43 return "WebAssembly Optimize Live Intervals";
44 }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.setPreservesCFG();
54 }
55
56 MachineFunctionProperties getRequiredProperties() const override {
57 return MachineFunctionProperties().setTracksLiveness();
58 }
59
60 bool runOnMachineFunction(MachineFunction &MF) override;
61
62public:
63 static char ID; // Pass identification, replacement for typeid
64 WebAssemblyOptimizeLiveIntervalsLegacy() : MachineFunctionPass(ID) {}
65};
66} // end anonymous namespace
67
68char WebAssemblyOptimizeLiveIntervalsLegacy::ID = 0;
69INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervalsLegacy, DEBUG_TYPE,
70 "Optimize LiveIntervals for WebAssembly", false, false)
71
73 return new WebAssemblyOptimizeLiveIntervalsLegacy();
74}
75
77 LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
78 "********** Function: "
79 << MF.getName() << '\n');
80
82
83 // We don't preserve SSA form.
84 MRI.leaveSSA();
85
86 assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness");
87
88 // Split multiple-VN LiveIntervals into multiple LiveIntervals.
90 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
92 auto &TRI = *MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
93
94 if (MRI.reg_nodbg_empty(Reg))
95 continue;
96
97 LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
98 if (Reg == TRI.getFrameRegister(MF) && SplitLIs.size() > 0) {
99 // The live interval for the frame register was split, resulting in a new
100 // VReg. For now we only support debug info output for a single frame base
101 // value for the function, so just use the last one. It will certainly be
102 // wrong for some part of the function, but until we are able to track
103 // values through live-range splitting and stackification, it will have to
104 // do.
105 MF.getInfo<WebAssemblyFunctionInfo>()->setFrameBaseVreg(
106 SplitLIs.back()->reg());
107 }
108 SplitLIs.clear();
109 }
110
111 // In FixIrreducibleControlFlow, we conservatively inserted IMPLICIT_DEF
112 // instructions to satisfy LiveIntervals' requirement that all uses be
113 // dominated by defs. Now that LiveIntervals has computed which of these
114 // defs are actually needed and which are dead, remove the dead ones.
116 if (MI.isImplicitDef() && MI.getOperand(0).isDead()) {
117 LiveInterval &LI = LIS.getInterval(MI.getOperand(0).getReg());
120 MI.eraseFromParent();
121 }
122 }
123
124 return true;
125}
126
127bool WebAssemblyOptimizeLiveIntervalsLegacy::runOnMachineFunction(
128 MachineFunction &MF) {
130 MF, getAnalysis<LiveIntervalsWrapperPass>().getLIS());
131}
132
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file declares WebAssembly-specific per-machine-function information.
static bool optimizeLiveIntervals(MachineFunction &MF, LiveIntervals &LIS)
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LiveInterval - This class represents the liveness of a register, or stack slot.
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
LiveInterval & getInterval(Register Reg)
LLVM_ABI void removeVRegDefAt(LiveInterval &LI, SlotIndex Pos)
Remove value number and related live segments of LI and its subranges that start at position Pos.
LLVM_ABI void splitSeparateComponents(LiveInterval &LI, SmallVectorImpl< LiveInterval * > &SplitLIs)
Split separate components in LiveInterval LI into separate intervals.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool tracksLiveness() const
tracksLiveness - Returns true when tracking register liveness accurately.
bool reg_nodbg_empty(Register RegNo) const
reg_nodbg_empty - Return true if the only instructions using or defining Reg are Debug instructions.
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Changed
Pass manager infrastructure for declaring and invalidating analyses.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createWebAssemblyOptimizeLiveIntervalsLegacyPass()