LLVM  8.0.1
WebAssemblyOptimizeLiveIntervals.cpp
Go to the documentation of this file.
1 //===--- WebAssemblyOptimizeLiveIntervals.cpp - LiveInterval processing ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// Optimize LiveIntervals for use in a post-RA context.
12 //
13 /// LiveIntervals normally runs before register allocation when the code is
14 /// only recently lowered out of SSA form, so it's uncommon for registers to
15 /// have multiple defs, and when they do, the defs are usually closely related.
16 /// Later, after coalescing, tail duplication, and other optimizations, it's
17 /// more common to see registers with multiple unrelated defs. This pass
18 /// updates LiveIntervals to distribute the value numbers across separate
19 /// LiveIntervals.
20 ///
21 //===----------------------------------------------------------------------===//
22 
23 #include "WebAssembly.h"
24 #include "WebAssemblySubtarget.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/Support/Debug.h"
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "wasm-optimize-live-intervals"
34 
35 namespace {
36 class WebAssemblyOptimizeLiveIntervals final : public MachineFunctionPass {
37  StringRef getPassName() const override {
38  return "WebAssembly Optimize Live Intervals";
39  }
40 
41  void getAnalysisUsage(AnalysisUsage &AU) const override {
42  AU.setPreservesCFG();
50  }
51 
52  bool runOnMachineFunction(MachineFunction &MF) override;
53 
54 public:
55  static char ID; // Pass identification, replacement for typeid
56  WebAssemblyOptimizeLiveIntervals() : MachineFunctionPass(ID) {}
57 };
58 } // end anonymous namespace
59 
61 INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE,
62  "Optimize LiveIntervals for WebAssembly", false, false)
63 
65  return new WebAssemblyOptimizeLiveIntervals();
66 }
67 
68 bool WebAssemblyOptimizeLiveIntervals::runOnMachineFunction(
69  MachineFunction &MF) {
70  LLVM_DEBUG(dbgs() << "********** Optimize LiveIntervals **********\n"
71  "********** Function: "
72  << MF.getName() << '\n');
73 
75  LiveIntervals &LIS = getAnalysis<LiveIntervals>();
76 
77  // We don't preserve SSA form.
78  MRI.leaveSSA();
79 
80  assert(MRI.tracksLiveness() && "OptimizeLiveIntervals expects liveness");
81 
82  // Split multiple-VN LiveIntervals into multiple LiveIntervals.
84  for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
86  if (MRI.reg_nodbg_empty(Reg))
87  continue;
88 
89  LIS.splitSeparateComponents(LIS.getInterval(Reg), SplitLIs);
90  SplitLIs.clear();
91  }
92 
93  // In PrepareForLiveIntervals, we conservatively inserted IMPLICIT_DEF
94  // instructions to satisfy LiveIntervals' requirement that all uses be
95  // dominated by defs. Now that LiveIntervals has computed which of these
96  // defs are actually needed and which are dead, remove the dead ones.
97  for (auto MII = MF.begin()->begin(), MIE = MF.begin()->end(); MII != MIE;) {
98  MachineInstr *MI = &*MII++;
99  if (MI->isImplicitDef() && MI->getOperand(0).isDead()) {
100  LiveInterval &LI = LIS.getInterval(MI->getOperand(0).getReg());
101  LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(*MI).getRegSlot());
103  MI->eraseFromParent();
104  }
105  }
106 
107  return false;
108 }
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:638
unsigned getReg() const
getReg - Returns the register number.
unsigned Reg
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
AnalysisUsage & addRequired()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void eraseFromParent()
Unlink &#39;this&#39; from the containing basic block and delete it.
INITIALIZE_PASS(WebAssemblyOptimizeLiveIntervals, DEBUG_TYPE, "Optimize LiveIntervals for WebAssembly", false, false) FunctionPass *llvm
SlotIndexes pass.
Definition: SlotIndexes.h:331
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
Definition: SlotIndexes.h:255
AnalysisUsage & addPreservedID(const void *ID)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
char & LiveVariablesID
LiveVariables pass - This pass computes the set of blocks in which each variable is life and sets mac...
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void splitSeparateComponents(LiveInterval &LI, SmallVectorImpl< LiveInterval *> &SplitLIs)
Split separate components in LiveInterval LI into separate intervals.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bool isImplicitDef() const
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:286
LiveInterval & getInterval(unsigned Reg)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
FunctionPass * createWebAssemblyOptimizeLiveIntervals()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define LLVM_DEBUG(X)
Definition: Debug.h:123
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
void removeVRegDefAt(LiveInterval &LI, SlotIndex Pos)
Remove value number and related live segments of LI and its subranges that start at position Pos...