14fc4e42dSDan Gohman //===-- WebAssemblyExplicitLocals.cpp - Make Locals Explicit --------------===//
24fc4e42dSDan Gohman //
34fc4e42dSDan Gohman //                     The LLVM Compiler Infrastructure
44fc4e42dSDan Gohman //
54fc4e42dSDan Gohman // This file is distributed under the University of Illinois Open Source
64fc4e42dSDan Gohman // License. See LICENSE.TXT for details.
74fc4e42dSDan Gohman //
84fc4e42dSDan Gohman //===----------------------------------------------------------------------===//
94fc4e42dSDan Gohman ///
104fc4e42dSDan Gohman /// \file
115f8f34e4SAdrian Prantl /// This file converts any remaining registers into WebAssembly locals.
124fc4e42dSDan Gohman ///
134fc4e42dSDan Gohman /// After register stackification and register coloring, convert non-stackified
144fc4e42dSDan Gohman /// registers into locals, inserting explicit get_local and set_local
154fc4e42dSDan Gohman /// instructions.
164fc4e42dSDan Gohman ///
174fc4e42dSDan Gohman //===----------------------------------------------------------------------===//
184fc4e42dSDan Gohman 
194fc4e42dSDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
204fc4e42dSDan Gohman #include "WebAssembly.h"
214fc4e42dSDan Gohman #include "WebAssemblyMachineFunctionInfo.h"
224fc4e42dSDan Gohman #include "WebAssemblySubtarget.h"
234fc4e42dSDan Gohman #include "WebAssemblyUtilities.h"
244fc4e42dSDan Gohman #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
254fc4e42dSDan Gohman #include "llvm/CodeGen/MachineInstrBuilder.h"
264fc4e42dSDan Gohman #include "llvm/CodeGen/MachineRegisterInfo.h"
274fc4e42dSDan Gohman #include "llvm/CodeGen/Passes.h"
284fc4e42dSDan Gohman #include "llvm/Support/Debug.h"
294fc4e42dSDan Gohman #include "llvm/Support/raw_ostream.h"
304fc4e42dSDan Gohman using namespace llvm;
314fc4e42dSDan Gohman 
324fc4e42dSDan Gohman #define DEBUG_TYPE "wasm-explicit-locals"
334fc4e42dSDan Gohman 
347d7409e5SDan Gohman // A command-line option to disable this pass. Note that this produces output
357d7409e5SDan Gohman // which is not valid WebAssembly, though it may be more convenient for writing
367d7409e5SDan Gohman // LLVM unit tests with.
377d7409e5SDan Gohman static cl::opt<bool> DisableWebAssemblyExplicitLocals(
387d7409e5SDan Gohman     "disable-wasm-explicit-locals", cl::ReallyHidden,
397d7409e5SDan Gohman     cl::desc("WebAssembly: Disable emission of get_local/set_local."),
407d7409e5SDan Gohman     cl::init(false));
417d7409e5SDan Gohman 
424fc4e42dSDan Gohman namespace {
434fc4e42dSDan Gohman class WebAssemblyExplicitLocals final : public MachineFunctionPass {
444fc4e42dSDan Gohman   StringRef getPassName() const override {
454fc4e42dSDan Gohman     return "WebAssembly Explicit Locals";
464fc4e42dSDan Gohman   }
474fc4e42dSDan Gohman 
484fc4e42dSDan Gohman   void getAnalysisUsage(AnalysisUsage &AU) const override {
494fc4e42dSDan Gohman     AU.setPreservesCFG();
504fc4e42dSDan Gohman     AU.addPreserved<MachineBlockFrequencyInfo>();
514fc4e42dSDan Gohman     MachineFunctionPass::getAnalysisUsage(AU);
524fc4e42dSDan Gohman   }
534fc4e42dSDan Gohman 
544fc4e42dSDan Gohman   bool runOnMachineFunction(MachineFunction &MF) override;
554fc4e42dSDan Gohman 
564fc4e42dSDan Gohman public:
574fc4e42dSDan Gohman   static char ID; // Pass identification, replacement for typeid
584fc4e42dSDan Gohman   WebAssemblyExplicitLocals() : MachineFunctionPass(ID) {}
594fc4e42dSDan Gohman };
604fc4e42dSDan Gohman } // end anonymous namespace
614fc4e42dSDan Gohman 
624fc4e42dSDan Gohman char WebAssemblyExplicitLocals::ID = 0;
6340926451SJacob Gravelle INITIALIZE_PASS(WebAssemblyExplicitLocals, DEBUG_TYPE,
6440926451SJacob Gravelle                 "Convert registers to WebAssembly locals", false, false)
6540926451SJacob Gravelle 
664fc4e42dSDan Gohman FunctionPass *llvm::createWebAssemblyExplicitLocals() {
674fc4e42dSDan Gohman   return new WebAssemblyExplicitLocals();
684fc4e42dSDan Gohman }
694fc4e42dSDan Gohman 
704fc4e42dSDan Gohman /// Return a local id number for the given register, assigning it a new one
714fc4e42dSDan Gohman /// if it doesn't yet have one.
724fc4e42dSDan Gohman static unsigned getLocalId(DenseMap<unsigned, unsigned> &Reg2Local,
734fc4e42dSDan Gohman                            unsigned &CurLocal, unsigned Reg) {
74d934cb88SDan Gohman   auto P = Reg2Local.insert(std::make_pair(Reg, CurLocal));
75d934cb88SDan Gohman   if (P.second)
76d934cb88SDan Gohman     ++CurLocal;
77d934cb88SDan Gohman   return P.first->second;
78d934cb88SDan Gohman }
79d934cb88SDan Gohman 
80d934cb88SDan Gohman /// Get the appropriate drop opcode for the given register class.
81d934cb88SDan Gohman static unsigned getDropOpcode(const TargetRegisterClass *RC) {
82d934cb88SDan Gohman   if (RC == &WebAssembly::I32RegClass)
83d934cb88SDan Gohman     return WebAssembly::DROP_I32;
84d934cb88SDan Gohman   if (RC == &WebAssembly::I64RegClass)
85d934cb88SDan Gohman     return WebAssembly::DROP_I64;
86d934cb88SDan Gohman   if (RC == &WebAssembly::F32RegClass)
87d934cb88SDan Gohman     return WebAssembly::DROP_F32;
88d934cb88SDan Gohman   if (RC == &WebAssembly::F64RegClass)
89d934cb88SDan Gohman     return WebAssembly::DROP_F64;
90d934cb88SDan Gohman   if (RC == &WebAssembly::V128RegClass)
91d934cb88SDan Gohman     return WebAssembly::DROP_V128;
920de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
930de58729SHeejin Ahn     return WebAssembly::DROP_EXCEPT_REF;
94d934cb88SDan Gohman   llvm_unreachable("Unexpected register class");
954fc4e42dSDan Gohman }
964fc4e42dSDan Gohman 
974fc4e42dSDan Gohman /// Get the appropriate get_local opcode for the given register class.
984fc4e42dSDan Gohman static unsigned getGetLocalOpcode(const TargetRegisterClass *RC) {
994fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
1004fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_I32;
1014fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1024fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_I64;
1034fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1044fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_F32;
1054fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1064fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_F64;
1074fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
1084fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_V128;
1090de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
1100de58729SHeejin Ahn     return WebAssembly::GET_LOCAL_EXCEPT_REF;
1114fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1124fc4e42dSDan Gohman }
1134fc4e42dSDan Gohman 
1144fc4e42dSDan Gohman /// Get the appropriate set_local opcode for the given register class.
1154fc4e42dSDan Gohman static unsigned getSetLocalOpcode(const TargetRegisterClass *RC) {
1164fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
1174fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_I32;
1184fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1194fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_I64;
1204fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1214fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_F32;
1224fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1234fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_F64;
1244fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
1254fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_V128;
1260de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
1270de58729SHeejin Ahn     return WebAssembly::SET_LOCAL_EXCEPT_REF;
1284fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1294fc4e42dSDan Gohman }
1304fc4e42dSDan Gohman 
1314fc4e42dSDan Gohman /// Get the appropriate tee_local opcode for the given register class.
1324fc4e42dSDan Gohman static unsigned getTeeLocalOpcode(const TargetRegisterClass *RC) {
1334fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
1344fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_I32;
1354fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1364fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_I64;
1374fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1384fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_F32;
1394fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1404fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_F64;
1414fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
1424fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_V128;
1430de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
1440de58729SHeejin Ahn     return WebAssembly::TEE_LOCAL_EXCEPT_REF;
1454fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1464fc4e42dSDan Gohman }
1474fc4e42dSDan Gohman 
1484fc4e42dSDan Gohman /// Get the type associated with the given register class.
1493acb187dSDan Gohman static MVT typeForRegClass(const TargetRegisterClass *RC) {
1504fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
1513acb187dSDan Gohman     return MVT::i32;
1524fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1533acb187dSDan Gohman     return MVT::i64;
1544fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1553acb187dSDan Gohman     return MVT::f32;
1564fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1573acb187dSDan Gohman     return MVT::f64;
1580de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
1590de58729SHeejin Ahn     return MVT::ExceptRef;
1604fc4e42dSDan Gohman   llvm_unreachable("unrecognized register class");
1614fc4e42dSDan Gohman }
1624fc4e42dSDan Gohman 
1634fc4e42dSDan Gohman /// Given a MachineOperand of a stackified vreg, return the instruction at the
1644fc4e42dSDan Gohman /// start of the expression tree.
1654fc4e42dSDan Gohman static MachineInstr *FindStartOfTree(MachineOperand &MO,
1664fc4e42dSDan Gohman                                      MachineRegisterInfo &MRI,
1674fc4e42dSDan Gohman                                      WebAssemblyFunctionInfo &MFI) {
1684fc4e42dSDan Gohman   unsigned Reg = MO.getReg();
1694fc4e42dSDan Gohman   assert(MFI.isVRegStackified(Reg));
1704fc4e42dSDan Gohman   MachineInstr *Def = MRI.getVRegDef(Reg);
1714fc4e42dSDan Gohman 
1724fc4e42dSDan Gohman   // Find the first stackified use and proceed from there.
1734fc4e42dSDan Gohman   for (MachineOperand &DefMO : Def->explicit_uses()) {
1744fc4e42dSDan Gohman     if (!DefMO.isReg())
1754fc4e42dSDan Gohman       continue;
1764fc4e42dSDan Gohman     return FindStartOfTree(DefMO, MRI, MFI);
1774fc4e42dSDan Gohman   }
1784fc4e42dSDan Gohman 
1794fc4e42dSDan Gohman   // If there were no stackified uses, we've reached the start.
1804fc4e42dSDan Gohman   return Def;
1814fc4e42dSDan Gohman }
1824fc4e42dSDan Gohman 
1834fc4e42dSDan Gohman bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
1844fc4e42dSDan Gohman   DEBUG(dbgs() << "********** Make Locals Explicit **********\n"
1854fc4e42dSDan Gohman                   "********** Function: "
1864fc4e42dSDan Gohman                << MF.getName() << '\n');
1874fc4e42dSDan Gohman 
1887d7409e5SDan Gohman   // Disable this pass if directed to do so.
1897d7409e5SDan Gohman   if (DisableWebAssemblyExplicitLocals)
1907d7409e5SDan Gohman     return false;
1917d7409e5SDan Gohman 
19266caac57SDan Gohman   // Disable this pass if we aren't doing direct wasm object emission.
19366caac57SDan Gohman   if (MF.getSubtarget<WebAssemblySubtarget>()
19466caac57SDan Gohman         .getTargetTriple().isOSBinFormatELF())
19566caac57SDan Gohman     return false;
19666caac57SDan Gohman 
1974fc4e42dSDan Gohman   bool Changed = false;
1984fc4e42dSDan Gohman   MachineRegisterInfo &MRI = MF.getRegInfo();
1994fc4e42dSDan Gohman   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
2004fc4e42dSDan Gohman   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
2014fc4e42dSDan Gohman 
2024fc4e42dSDan Gohman   // Map non-stackified virtual registers to their local ids.
2034fc4e42dSDan Gohman   DenseMap<unsigned, unsigned> Reg2Local;
2044fc4e42dSDan Gohman 
2054fc4e42dSDan Gohman   // Handle ARGUMENTS first to ensure that they get the designated numbers.
2064fc4e42dSDan Gohman   for (MachineBasicBlock::iterator I = MF.begin()->begin(),
2074fc4e42dSDan Gohman                                    E = MF.begin()->end();
2084fc4e42dSDan Gohman        I != E;) {
2094fc4e42dSDan Gohman     MachineInstr &MI = *I++;
2104fc4e42dSDan Gohman     if (!WebAssembly::isArgument(MI))
2114fc4e42dSDan Gohman       break;
2124fc4e42dSDan Gohman     unsigned Reg = MI.getOperand(0).getReg();
2134fc4e42dSDan Gohman     assert(!MFI.isVRegStackified(Reg));
2144fc4e42dSDan Gohman     Reg2Local[Reg] = MI.getOperand(1).getImm();
2154fc4e42dSDan Gohman     MI.eraseFromParent();
2164fc4e42dSDan Gohman     Changed = true;
2174fc4e42dSDan Gohman   }
2184fc4e42dSDan Gohman 
2194fc4e42dSDan Gohman   // Start assigning local numbers after the last parameter.
2204fc4e42dSDan Gohman   unsigned CurLocal = MFI.getParams().size();
2214fc4e42dSDan Gohman 
222d934cb88SDan Gohman   // Precompute the set of registers that are unused, so that we can insert
223d934cb88SDan Gohman   // drops to their defs.
224d934cb88SDan Gohman   BitVector UseEmpty(MRI.getNumVirtRegs());
225d934cb88SDan Gohman   for (unsigned i = 0, e = MRI.getNumVirtRegs(); i < e; ++i)
226d934cb88SDan Gohman     UseEmpty[i] = MRI.use_empty(TargetRegisterInfo::index2VirtReg(i));
227d934cb88SDan Gohman 
2284fc4e42dSDan Gohman   // Visit each instruction in the function.
2294fc4e42dSDan Gohman   for (MachineBasicBlock &MBB : MF) {
2304fc4e42dSDan Gohman     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
2314fc4e42dSDan Gohman       MachineInstr &MI = *I++;
2324fc4e42dSDan Gohman       assert(!WebAssembly::isArgument(MI));
2334fc4e42dSDan Gohman 
234*801bf7ebSShiva Chen       if (MI.isDebugInstr() || MI.isLabel())
2354fc4e42dSDan Gohman         continue;
2364fc4e42dSDan Gohman 
2374fc4e42dSDan Gohman       // Replace tee instructions with tee_local. The difference is that tee
2384fc4e42dSDan Gohman       // instructins have two defs, while tee_local instructions have one def
2394fc4e42dSDan Gohman       // and an index of a local to write to.
2404fc4e42dSDan Gohman       if (WebAssembly::isTee(MI)) {
2414fc4e42dSDan Gohman         assert(MFI.isVRegStackified(MI.getOperand(0).getReg()));
2424fc4e42dSDan Gohman         assert(!MFI.isVRegStackified(MI.getOperand(1).getReg()));
2434fc4e42dSDan Gohman         unsigned OldReg = MI.getOperand(2).getReg();
2444fc4e42dSDan Gohman         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2454fc4e42dSDan Gohman 
2464fc4e42dSDan Gohman         // Stackify the input if it isn't stackified yet.
2474fc4e42dSDan Gohman         if (!MFI.isVRegStackified(OldReg)) {
2484fc4e42dSDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2494fc4e42dSDan Gohman           unsigned NewReg = MRI.createVirtualRegister(RC);
2504fc4e42dSDan Gohman           unsigned Opc = getGetLocalOpcode(RC);
2514fc4e42dSDan Gohman           BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc), NewReg)
2524fc4e42dSDan Gohman               .addImm(LocalId);
2534fc4e42dSDan Gohman           MI.getOperand(2).setReg(NewReg);
2544fc4e42dSDan Gohman           MFI.stackifyVReg(NewReg);
2554fc4e42dSDan Gohman         }
2564fc4e42dSDan Gohman 
2574fc4e42dSDan Gohman         // Replace the TEE with a TEE_LOCAL.
2584fc4e42dSDan Gohman         unsigned LocalId =
2594fc4e42dSDan Gohman             getLocalId(Reg2Local, CurLocal, MI.getOperand(1).getReg());
2604fc4e42dSDan Gohman         unsigned Opc = getTeeLocalOpcode(RC);
2614fc4e42dSDan Gohman         BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc),
2624fc4e42dSDan Gohman                 MI.getOperand(0).getReg())
2634fc4e42dSDan Gohman             .addImm(LocalId)
2644fc4e42dSDan Gohman             .addReg(MI.getOperand(2).getReg());
2654fc4e42dSDan Gohman 
2664fc4e42dSDan Gohman         MI.eraseFromParent();
2674fc4e42dSDan Gohman         Changed = true;
2684fc4e42dSDan Gohman         continue;
2694fc4e42dSDan Gohman       }
2704fc4e42dSDan Gohman 
2714fc4e42dSDan Gohman       // Insert set_locals for any defs that aren't stackified yet. Currently
2724fc4e42dSDan Gohman       // we handle at most one def.
2734fc4e42dSDan Gohman       assert(MI.getDesc().getNumDefs() <= 1);
2744fc4e42dSDan Gohman       if (MI.getDesc().getNumDefs() == 1) {
2754fc4e42dSDan Gohman         unsigned OldReg = MI.getOperand(0).getReg();
276d934cb88SDan Gohman         if (!MFI.isVRegStackified(OldReg)) {
2774fc4e42dSDan Gohman           const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2784fc4e42dSDan Gohman           unsigned NewReg = MRI.createVirtualRegister(RC);
2794fc4e42dSDan Gohman           auto InsertPt = std::next(MachineBasicBlock::iterator(&MI));
280d934cb88SDan Gohman           if (MI.getOpcode() == WebAssembly::IMPLICIT_DEF) {
281d934cb88SDan Gohman             MI.eraseFromParent();
282d934cb88SDan Gohman             Changed = true;
283d934cb88SDan Gohman             continue;
284d934cb88SDan Gohman           }
285d934cb88SDan Gohman           if (UseEmpty[TargetRegisterInfo::virtReg2Index(OldReg)]) {
286d934cb88SDan Gohman             unsigned Opc = getDropOpcode(RC);
287d934cb88SDan Gohman             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
288d934cb88SDan Gohman                 .addReg(NewReg);
289d934cb88SDan Gohman           } else {
290d934cb88SDan Gohman             unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2914fc4e42dSDan Gohman             unsigned Opc = getSetLocalOpcode(RC);
2924fc4e42dSDan Gohman             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
2934fc4e42dSDan Gohman                 .addImm(LocalId)
2944fc4e42dSDan Gohman                 .addReg(NewReg);
295d934cb88SDan Gohman           }
2964fc4e42dSDan Gohman           MI.getOperand(0).setReg(NewReg);
2974fc4e42dSDan Gohman           MFI.stackifyVReg(NewReg);
2984fc4e42dSDan Gohman           Changed = true;
2994fc4e42dSDan Gohman         }
3004fc4e42dSDan Gohman       }
3014fc4e42dSDan Gohman 
3024fc4e42dSDan Gohman       // Insert get_locals for any uses that aren't stackified yet.
3034fc4e42dSDan Gohman       MachineInstr *InsertPt = &MI;
3044fc4e42dSDan Gohman       for (MachineOperand &MO : reverse(MI.explicit_uses())) {
3054fc4e42dSDan Gohman         if (!MO.isReg())
3064fc4e42dSDan Gohman           continue;
3074fc4e42dSDan Gohman 
3084fc4e42dSDan Gohman         unsigned OldReg = MO.getReg();
3094fc4e42dSDan Gohman 
310b465aa05SDan Gohman         // Inline asm may have a def in the middle of the operands. Our contract
311b465aa05SDan Gohman         // with inline asm register operands is to provide local indices as
312b465aa05SDan Gohman         // immediates.
313b465aa05SDan Gohman         if (MO.isDef()) {
314b465aa05SDan Gohman           assert(MI.getOpcode() == TargetOpcode::INLINEASM);
315b465aa05SDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
316b465aa05SDan Gohman           MRI.removeRegOperandFromUseList(&MO);
317b465aa05SDan Gohman           MO = MachineOperand::CreateImm(LocalId);
318b465aa05SDan Gohman           continue;
319b465aa05SDan Gohman         }
320b465aa05SDan Gohman 
3214fc4e42dSDan Gohman         // If we see a stackified register, prepare to insert subsequent
3224fc4e42dSDan Gohman         // get_locals before the start of its tree.
3234fc4e42dSDan Gohman         if (MFI.isVRegStackified(OldReg)) {
3244fc4e42dSDan Gohman           InsertPt = FindStartOfTree(MO, MRI, MFI);
3254fc4e42dSDan Gohman           continue;
3264fc4e42dSDan Gohman         }
3274fc4e42dSDan Gohman 
328b465aa05SDan Gohman         // Our contract with inline asm register operands is to provide local
329b465aa05SDan Gohman         // indices as immediates.
330b465aa05SDan Gohman         if (MI.getOpcode() == TargetOpcode::INLINEASM) {
331b465aa05SDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
332b465aa05SDan Gohman           MRI.removeRegOperandFromUseList(&MO);
333b465aa05SDan Gohman           MO = MachineOperand::CreateImm(LocalId);
334b465aa05SDan Gohman           continue;
335b465aa05SDan Gohman         }
336b465aa05SDan Gohman 
3374fc4e42dSDan Gohman         // Insert a get_local.
3384fc4e42dSDan Gohman         unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
3394fc4e42dSDan Gohman         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
3404fc4e42dSDan Gohman         unsigned NewReg = MRI.createVirtualRegister(RC);
3414fc4e42dSDan Gohman         unsigned Opc = getGetLocalOpcode(RC);
3424fc4e42dSDan Gohman         InsertPt =
3434fc4e42dSDan Gohman             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc), NewReg)
3444fc4e42dSDan Gohman                 .addImm(LocalId);
3454fc4e42dSDan Gohman         MO.setReg(NewReg);
3464fc4e42dSDan Gohman         MFI.stackifyVReg(NewReg);
3474fc4e42dSDan Gohman         Changed = true;
3484fc4e42dSDan Gohman       }
3494fc4e42dSDan Gohman 
3504fc4e42dSDan Gohman       // Coalesce and eliminate COPY instructions.
3514fc4e42dSDan Gohman       if (WebAssembly::isCopy(MI)) {
3524fc4e42dSDan Gohman         MRI.replaceRegWith(MI.getOperand(1).getReg(),
3534fc4e42dSDan Gohman                            MI.getOperand(0).getReg());
3544fc4e42dSDan Gohman         MI.eraseFromParent();
3554fc4e42dSDan Gohman         Changed = true;
3564fc4e42dSDan Gohman       }
3574fc4e42dSDan Gohman     }
3584fc4e42dSDan Gohman   }
3594fc4e42dSDan Gohman 
3603acb187dSDan Gohman   // Define the locals.
361d934cb88SDan Gohman   // TODO: Sort the locals for better compression.
362d934cb88SDan Gohman   MFI.setNumLocals(CurLocal - MFI.getParams().size());
3634fc4e42dSDan Gohman   for (size_t i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
3644fc4e42dSDan Gohman     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
3654fc4e42dSDan Gohman     auto I = Reg2Local.find(Reg);
3664fc4e42dSDan Gohman     if (I == Reg2Local.end() || I->second < MFI.getParams().size())
3674fc4e42dSDan Gohman       continue;
3684fc4e42dSDan Gohman 
369d934cb88SDan Gohman     MFI.setLocal(I->second - MFI.getParams().size(),
370d934cb88SDan Gohman                  typeForRegClass(MRI.getRegClass(Reg)));
3714fc4e42dSDan Gohman     Changed = true;
3724fc4e42dSDan Gohman   }
3734fc4e42dSDan Gohman 
3744fc4e42dSDan Gohman #ifndef NDEBUG
3754fc4e42dSDan Gohman   // Assert that all registers have been stackified at this point.
3764fc4e42dSDan Gohman   for (const MachineBasicBlock &MBB : MF) {
3774fc4e42dSDan Gohman     for (const MachineInstr &MI : MBB) {
378*801bf7ebSShiva Chen       if (MI.isDebugInstr() || MI.isLabel())
3794fc4e42dSDan Gohman         continue;
3804fc4e42dSDan Gohman       for (const MachineOperand &MO : MI.explicit_operands()) {
3814fc4e42dSDan Gohman         assert(
3824fc4e42dSDan Gohman             (!MO.isReg() || MRI.use_empty(MO.getReg()) ||
3834fc4e42dSDan Gohman              MFI.isVRegStackified(MO.getReg())) &&
3844fc4e42dSDan Gohman             "WebAssemblyExplicitLocals failed to stackify a register operand");
3854fc4e42dSDan Gohman       }
3864fc4e42dSDan Gohman     }
3874fc4e42dSDan Gohman   }
3884fc4e42dSDan Gohman #endif
3894fc4e42dSDan Gohman 
3904fc4e42dSDan Gohman   return Changed;
3914fc4e42dSDan Gohman }
392