blob: 52fdaf0417a4e55c61be88a889b44a71753bcf61 [file] [log] [blame]
Dan Gohman0cfb5f82016-05-10 04:24:02 +00001//===- WebAssemblyPrepareForLiveIntervals.cpp - Prepare for LiveIntervals -===//
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/// \brief Fix up code to meet LiveInterval's requirements.
12///
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
18/// nothing else).
19///
20//===----------------------------------------------------------------------===//
21
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"
32using namespace llvm;
33
34#define DEBUG_TYPE "wasm-prepare-for-live-intervals"
35
36namespace {
37class WebAssemblyPrepareForLiveIntervals final : public MachineFunctionPass {
38public:
39 static char ID; // Pass identification, replacement for typeid
40 WebAssemblyPrepareForLiveIntervals() : MachineFunctionPass(ID) {}
41
42private:
Mehdi Amini117296c2016-10-01 02:56:57 +000043 StringRef getPassName() const override {
Dan Gohman0cfb5f82016-05-10 04:24:02 +000044 return "WebAssembly Prepare For LiveIntervals";
45 }
46
47 void getAnalysisUsage(AnalysisUsage &AU) const override {
48 AU.setPreservesCFG();
49 MachineFunctionPass::getAnalysisUsage(AU);
50 }
51
52 bool runOnMachineFunction(MachineFunction &MF) override;
53};
54} // end anonymous namespace
55
56char WebAssemblyPrepareForLiveIntervals::ID = 0;
57FunctionPass *llvm::createWebAssemblyPrepareForLiveIntervals() {
58 return new WebAssemblyPrepareForLiveIntervals();
59}
60
61/// Test whether the given instruction is an ARGUMENT.
62static 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:
Derek Schuff39bf39f2016-08-02 23:16:09 +000068 case WebAssembly::ARGUMENT_v16i8:
69 case WebAssembly::ARGUMENT_v8i16:
70 case WebAssembly::ARGUMENT_v4i32:
71 case WebAssembly::ARGUMENT_v4f32:
Dan Gohman0cfb5f82016-05-10 04:24:02 +000072 return true;
73 default:
74 return false;
75 }
76}
77
78// Test whether the given register has an ARGUMENT def.
79static bool HasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
Derek Schuff39bf39f2016-08-02 23:16:09 +000080 for (const auto &Def : MRI.def_instructions(Reg))
Dan Gohman0cfb5f82016-05-10 04:24:02 +000081 if (IsArgument(&Def))
82 return true;
83 return false;
84}
85
86bool WebAssemblyPrepareForLiveIntervals::runOnMachineFunction(MachineFunction &MF) {
87 DEBUG({
88 dbgs() << "********** Prepare For LiveIntervals **********\n"
89 << "********** Function: " << MF.getName() << '\n';
90 });
91
92 bool Changed = false;
93 MachineRegisterInfo &MRI = MF.getRegInfo();
94 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
95 MachineBasicBlock &Entry = *MF.begin();
96
97 assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
98 "LiveIntervals shouldn't be active yet!");
99
100 // We don't preserve SSA form.
101 MRI.leaveSSA();
102
103 // BranchFolding and perhaps other passes don't preserve IMPLICIT_DEF
104 // instructions. LiveIntervals requires that all paths to virtual register
105 // uses provide a definition. Insert IMPLICIT_DEFs in the entry block to
106 // conservatively satisfy this.
107 //
108 // TODO: This is fairly heavy-handed; find a better approach.
109 //
110 for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
111 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
112
113 // Skip unused registers.
114 if (MRI.use_nodbg_empty(Reg))
115 continue;
116
117 // Skip registers that have an ARGUMENT definition.
118 if (HasArgumentDef(Reg, MRI))
119 continue;
120
121 BuildMI(Entry, Entry.begin(), DebugLoc(),
122 TII.get(WebAssembly::IMPLICIT_DEF), Reg);
123 Changed = true;
124 }
125
126 // Move ARGUMENT_* instructions to the top of the entry block, so that their
127 // liveness reflects the fact that these really are live-in values.
128 for (auto MII = Entry.begin(), MIE = Entry.end(); MII != MIE; ) {
129 MachineInstr *MI = &*MII++;
130 if (IsArgument(MI)) {
131 MI->removeFromParent();
132 Entry.insert(Entry.begin(), MI);
133 }
134 }
135
136 // Ok, we're now ready to run LiveIntervalAnalysis again.
137 MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
138
139 return Changed;
140}