1 //===- WebAssemblyPrepareForLiveIntervals.cpp - Prepare for LiveIntervals -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Fix up code to meet LiveInterval's requirements.
13 /// Some CodeGen passes don't preserve LiveInterval's requirements, because
14 /// they run after register allocation and it isn't important. However,
15 /// WebAssembly runs LiveIntervals in a late pass. This pass transforms code
16 /// to meet LiveIntervals' requirements; primarily, it ensures that all
17 /// virtual register uses have definitions (IMPLICIT_DEF definitions if
20 //===----------------------------------------------------------------------===//
22 #include "WebAssembly.h"
23 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
24 #include "WebAssemblyMachineFunctionInfo.h"
25 #include "WebAssemblySubtarget.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/Passes.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "wasm-prepare-for-live-intervals"
37 class WebAssemblyPrepareForLiveIntervals final : public MachineFunctionPass {
39 static char ID; // Pass identification, replacement for typeid
40 WebAssemblyPrepareForLiveIntervals() : MachineFunctionPass(ID) {}
43 const char *getPassName() const override {
44 return "WebAssembly Prepare For LiveIntervals";
47 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 MachineFunctionPass::getAnalysisUsage(AU);
52 bool runOnMachineFunction(MachineFunction &MF) override;
54 } // end anonymous namespace
56 char WebAssemblyPrepareForLiveIntervals::ID = 0;
57 FunctionPass *llvm::createWebAssemblyPrepareForLiveIntervals() {
58 return new WebAssemblyPrepareForLiveIntervals();
61 /// Test whether the given instruction is an ARGUMENT.
62 static bool IsArgument(const MachineInstr *MI) {
63 switch (MI->getOpcode()) {
64 case WebAssembly::ARGUMENT_I32:
65 case WebAssembly::ARGUMENT_I64:
66 case WebAssembly::ARGUMENT_F32:
67 case WebAssembly::ARGUMENT_F64:
74 // Test whether the given register has an ARGUMENT def.
75 static bool HasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
76 for (auto &Def : MRI.def_instructions(Reg))
82 bool WebAssemblyPrepareForLiveIntervals::runOnMachineFunction(MachineFunction &MF) {
84 dbgs() << "********** Prepare For LiveIntervals **********\n"
85 << "********** Function: " << MF.getName() << '\n';
89 MachineRegisterInfo &MRI = MF.getRegInfo();
90 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
91 MachineBasicBlock &Entry = *MF.begin();
93 assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
94 "LiveIntervals shouldn't be active yet!");
96 // We don't preserve SSA form.
99 // BranchFolding and perhaps other passes don't preserve IMPLICIT_DEF
100 // instructions. LiveIntervals requires that all paths to virtual register
101 // uses provide a definition. Insert IMPLICIT_DEFs in the entry block to
102 // conservatively satisfy this.
104 // TODO: This is fairly heavy-handed; find a better approach.
106 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
107 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
109 // Skip unused registers.
110 if (MRI.use_nodbg_empty(Reg))
113 // Skip registers that have an ARGUMENT definition.
114 if (HasArgumentDef(Reg, MRI))
117 BuildMI(Entry, Entry.begin(), DebugLoc(),
118 TII.get(WebAssembly::IMPLICIT_DEF), Reg);
122 // Move ARGUMENT_* instructions to the top of the entry block, so that their
123 // liveness reflects the fact that these really are live-in values.
124 for (auto MII = Entry.begin(), MIE = Entry.end(); MII != MIE; ) {
125 MachineInstr *MI = &*MII++;
126 if (IsArgument(MI)) {
127 MI->removeFromParent();
128 Entry.insert(Entry.begin(), MI);
132 // Ok, we're now ready to run LiveIntervalAnalysis again.
133 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);