xref: /aosp_15_r20/external/llvm/lib/Target/WebAssembly/WebAssemblyPrepareForLiveIntervals.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- WebAssemblyPrepareForLiveIntervals.cpp - Prepare for LiveIntervals -===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker ///
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief Fix up code to meet LiveInterval's requirements.
12*9880d681SAndroid Build Coastguard Worker ///
13*9880d681SAndroid Build Coastguard Worker /// Some CodeGen passes don't preserve LiveInterval's requirements, because
14*9880d681SAndroid Build Coastguard Worker /// they run after register allocation and it isn't important. However,
15*9880d681SAndroid Build Coastguard Worker /// WebAssembly runs LiveIntervals in a late pass. This pass transforms code
16*9880d681SAndroid Build Coastguard Worker /// to meet LiveIntervals' requirements; primarily, it ensures that all
17*9880d681SAndroid Build Coastguard Worker /// virtual register uses have definitions (IMPLICIT_DEF definitions if
18*9880d681SAndroid Build Coastguard Worker /// nothing else).
19*9880d681SAndroid Build Coastguard Worker ///
20*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker #include "WebAssembly.h"
23*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
24*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyMachineFunctionInfo.h"
25*9880d681SAndroid Build Coastguard Worker #include "WebAssemblySubtarget.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
32*9880d681SAndroid Build Coastguard Worker using namespace llvm;
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "wasm-prepare-for-live-intervals"
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker namespace {
37*9880d681SAndroid Build Coastguard Worker class WebAssemblyPrepareForLiveIntervals final : public MachineFunctionPass {
38*9880d681SAndroid Build Coastguard Worker public:
39*9880d681SAndroid Build Coastguard Worker   static char ID; // Pass identification, replacement for typeid
WebAssemblyPrepareForLiveIntervals()40*9880d681SAndroid Build Coastguard Worker   WebAssemblyPrepareForLiveIntervals() : MachineFunctionPass(ID) {}
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker private:
getPassName() const43*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
44*9880d681SAndroid Build Coastguard Worker     return "WebAssembly Prepare For LiveIntervals";
45*9880d681SAndroid Build Coastguard Worker   }
46*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const47*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
48*9880d681SAndroid Build Coastguard Worker     AU.setPreservesCFG();
49*9880d681SAndroid Build Coastguard Worker     MachineFunctionPass::getAnalysisUsage(AU);
50*9880d681SAndroid Build Coastguard Worker   }
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &MF) override;
53*9880d681SAndroid Build Coastguard Worker };
54*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker char WebAssemblyPrepareForLiveIntervals::ID = 0;
createWebAssemblyPrepareForLiveIntervals()57*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createWebAssemblyPrepareForLiveIntervals() {
58*9880d681SAndroid Build Coastguard Worker   return new WebAssemblyPrepareForLiveIntervals();
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker 
61*9880d681SAndroid Build Coastguard Worker /// Test whether the given instruction is an ARGUMENT.
IsArgument(const MachineInstr * MI)62*9880d681SAndroid Build Coastguard Worker static bool IsArgument(const MachineInstr *MI) {
63*9880d681SAndroid Build Coastguard Worker   switch (MI->getOpcode()) {
64*9880d681SAndroid Build Coastguard Worker   case WebAssembly::ARGUMENT_I32:
65*9880d681SAndroid Build Coastguard Worker   case WebAssembly::ARGUMENT_I64:
66*9880d681SAndroid Build Coastguard Worker   case WebAssembly::ARGUMENT_F32:
67*9880d681SAndroid Build Coastguard Worker   case WebAssembly::ARGUMENT_F64:
68*9880d681SAndroid Build Coastguard Worker     return true;
69*9880d681SAndroid Build Coastguard Worker   default:
70*9880d681SAndroid Build Coastguard Worker     return false;
71*9880d681SAndroid Build Coastguard Worker   }
72*9880d681SAndroid Build Coastguard Worker }
73*9880d681SAndroid Build Coastguard Worker 
74*9880d681SAndroid Build Coastguard Worker // Test whether the given register has an ARGUMENT def.
HasArgumentDef(unsigned Reg,const MachineRegisterInfo & MRI)75*9880d681SAndroid Build Coastguard Worker static bool HasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
76*9880d681SAndroid Build Coastguard Worker   for (auto &Def : MRI.def_instructions(Reg))
77*9880d681SAndroid Build Coastguard Worker     if (IsArgument(&Def))
78*9880d681SAndroid Build Coastguard Worker       return true;
79*9880d681SAndroid Build Coastguard Worker   return false;
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)82*9880d681SAndroid Build Coastguard Worker bool WebAssemblyPrepareForLiveIntervals::runOnMachineFunction(MachineFunction &MF) {
83*9880d681SAndroid Build Coastguard Worker   DEBUG({
84*9880d681SAndroid Build Coastguard Worker     dbgs() << "********** Prepare For LiveIntervals **********\n"
85*9880d681SAndroid Build Coastguard Worker            << "********** Function: " << MF.getName() << '\n';
86*9880d681SAndroid Build Coastguard Worker   });
87*9880d681SAndroid Build Coastguard Worker 
88*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
89*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo &MRI = MF.getRegInfo();
90*9880d681SAndroid Build Coastguard Worker   const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
91*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock &Entry = *MF.begin();
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker   assert(!mustPreserveAnalysisID(LiveIntervalsID) &&
94*9880d681SAndroid Build Coastguard Worker          "LiveIntervals shouldn't be active yet!");
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker   // We don't preserve SSA form.
97*9880d681SAndroid Build Coastguard Worker   MRI.leaveSSA();
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   // BranchFolding and perhaps other passes don't preserve IMPLICIT_DEF
100*9880d681SAndroid Build Coastguard Worker   // instructions. LiveIntervals requires that all paths to virtual register
101*9880d681SAndroid Build Coastguard Worker   // uses provide a definition. Insert IMPLICIT_DEFs in the entry block to
102*9880d681SAndroid Build Coastguard Worker   // conservatively satisfy this.
103*9880d681SAndroid Build Coastguard Worker   //
104*9880d681SAndroid Build Coastguard Worker   // TODO: This is fairly heavy-handed; find a better approach.
105*9880d681SAndroid Build Coastguard Worker   //
106*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
107*9880d681SAndroid Build Coastguard Worker     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker     // Skip unused registers.
110*9880d681SAndroid Build Coastguard Worker     if (MRI.use_nodbg_empty(Reg))
111*9880d681SAndroid Build Coastguard Worker       continue;
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker     // Skip registers that have an ARGUMENT definition.
114*9880d681SAndroid Build Coastguard Worker     if (HasArgumentDef(Reg, MRI))
115*9880d681SAndroid Build Coastguard Worker       continue;
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker     BuildMI(Entry, Entry.begin(), DebugLoc(),
118*9880d681SAndroid Build Coastguard Worker             TII.get(WebAssembly::IMPLICIT_DEF), Reg);
119*9880d681SAndroid Build Coastguard Worker     Changed = true;
120*9880d681SAndroid Build Coastguard Worker   }
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   // Move ARGUMENT_* instructions to the top of the entry block, so that their
123*9880d681SAndroid Build Coastguard Worker   // liveness reflects the fact that these really are live-in values.
124*9880d681SAndroid Build Coastguard Worker   for (auto MII = Entry.begin(), MIE = Entry.end(); MII != MIE; ) {
125*9880d681SAndroid Build Coastguard Worker     MachineInstr *MI = &*MII++;
126*9880d681SAndroid Build Coastguard Worker     if (IsArgument(MI)) {
127*9880d681SAndroid Build Coastguard Worker       MI->removeFromParent();
128*9880d681SAndroid Build Coastguard Worker       Entry.insert(Entry.begin(), MI);
129*9880d681SAndroid Build Coastguard Worker     }
130*9880d681SAndroid Build Coastguard Worker   }
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker   // Ok, we're now ready to run LiveIntervalAnalysis again.
133*9880d681SAndroid Build Coastguard Worker   MF.getProperties().set(MachineFunctionProperties::Property::TracksLiveness);
134*9880d681SAndroid Build Coastguard Worker 
135*9880d681SAndroid Build Coastguard Worker   return Changed;
136*9880d681SAndroid Build Coastguard Worker }
137