xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- WebAssemblyExplicitLocals.cpp - Make Locals Explicit --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file converts any remaining registers into WebAssembly locals.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric /// After register stackification and register coloring, convert non-stackified
130b57cec5SDimitry Andric /// registers into locals, inserting explicit local.get and local.set
140b57cec5SDimitry Andric /// instructions.
150b57cec5SDimitry Andric ///
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
190b57cec5SDimitry Andric #include "WebAssembly.h"
200b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
210b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
220b57cec5SDimitry Andric #include "WebAssemblyUtilities.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
270b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
290b57cec5SDimitry Andric using namespace llvm;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-explicit-locals"
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric // A command-line option to disable this pass, and keep implicit locals
340b57cec5SDimitry Andric // for the purpose of testing with lit/llc ONLY.
350b57cec5SDimitry Andric // This produces output which is not valid WebAssembly, and is not supported
360b57cec5SDimitry Andric // by assemblers/disassemblers and other MC based tools.
370b57cec5SDimitry Andric static cl::opt<bool> WasmDisableExplicitLocals(
380b57cec5SDimitry Andric     "wasm-disable-explicit-locals", cl::Hidden,
390b57cec5SDimitry Andric     cl::desc("WebAssembly: output implicit locals in"
400b57cec5SDimitry Andric              " instruction output for test purposes only."),
410b57cec5SDimitry Andric     cl::init(false));
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric namespace {
440b57cec5SDimitry Andric class WebAssemblyExplicitLocals final : public MachineFunctionPass {
450b57cec5SDimitry Andric   StringRef getPassName() const override {
460b57cec5SDimitry Andric     return "WebAssembly Explicit Locals";
470b57cec5SDimitry Andric   }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
500b57cec5SDimitry Andric     AU.setPreservesCFG();
510b57cec5SDimitry Andric     AU.addPreserved<MachineBlockFrequencyInfo>();
520b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric public:
580b57cec5SDimitry Andric   static char ID; // Pass identification, replacement for typeid
590b57cec5SDimitry Andric   WebAssemblyExplicitLocals() : MachineFunctionPass(ID) {}
600b57cec5SDimitry Andric };
610b57cec5SDimitry Andric } // end anonymous namespace
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric char WebAssemblyExplicitLocals::ID = 0;
640b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyExplicitLocals, DEBUG_TYPE,
650b57cec5SDimitry Andric                 "Convert registers to WebAssembly locals", false, false)
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyExplicitLocals() {
680b57cec5SDimitry Andric   return new WebAssemblyExplicitLocals();
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric /// Return a local id number for the given register, assigning it a new one
720b57cec5SDimitry Andric /// if it doesn't yet have one.
730b57cec5SDimitry Andric static unsigned getLocalId(DenseMap<unsigned, unsigned> &Reg2Local,
740b57cec5SDimitry Andric                            unsigned &CurLocal, unsigned Reg) {
750b57cec5SDimitry Andric   auto P = Reg2Local.insert(std::make_pair(Reg, CurLocal));
760b57cec5SDimitry Andric   if (P.second)
770b57cec5SDimitry Andric     ++CurLocal;
780b57cec5SDimitry Andric   return P.first->second;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric /// Get the appropriate drop opcode for the given register class.
820b57cec5SDimitry Andric static unsigned getDropOpcode(const TargetRegisterClass *RC) {
830b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
840b57cec5SDimitry Andric     return WebAssembly::DROP_I32;
850b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
860b57cec5SDimitry Andric     return WebAssembly::DROP_I64;
870b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
880b57cec5SDimitry Andric     return WebAssembly::DROP_F32;
890b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
900b57cec5SDimitry Andric     return WebAssembly::DROP_F64;
910b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
920b57cec5SDimitry Andric     return WebAssembly::DROP_V128;
930b57cec5SDimitry Andric   if (RC == &WebAssembly::EXNREFRegClass)
940b57cec5SDimitry Andric     return WebAssembly::DROP_EXNREF;
950b57cec5SDimitry Andric   llvm_unreachable("Unexpected register class");
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric /// Get the appropriate local.get opcode for the given register class.
990b57cec5SDimitry Andric static unsigned getLocalGetOpcode(const TargetRegisterClass *RC) {
1000b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
1010b57cec5SDimitry Andric     return WebAssembly::LOCAL_GET_I32;
1020b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
1030b57cec5SDimitry Andric     return WebAssembly::LOCAL_GET_I64;
1040b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
1050b57cec5SDimitry Andric     return WebAssembly::LOCAL_GET_F32;
1060b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
1070b57cec5SDimitry Andric     return WebAssembly::LOCAL_GET_F64;
1080b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
1090b57cec5SDimitry Andric     return WebAssembly::LOCAL_GET_V128;
1100b57cec5SDimitry Andric   if (RC == &WebAssembly::EXNREFRegClass)
1110b57cec5SDimitry Andric     return WebAssembly::LOCAL_GET_EXNREF;
1120b57cec5SDimitry Andric   llvm_unreachable("Unexpected register class");
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric /// Get the appropriate local.set opcode for the given register class.
1160b57cec5SDimitry Andric static unsigned getLocalSetOpcode(const TargetRegisterClass *RC) {
1170b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
1180b57cec5SDimitry Andric     return WebAssembly::LOCAL_SET_I32;
1190b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
1200b57cec5SDimitry Andric     return WebAssembly::LOCAL_SET_I64;
1210b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
1220b57cec5SDimitry Andric     return WebAssembly::LOCAL_SET_F32;
1230b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
1240b57cec5SDimitry Andric     return WebAssembly::LOCAL_SET_F64;
1250b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
1260b57cec5SDimitry Andric     return WebAssembly::LOCAL_SET_V128;
1270b57cec5SDimitry Andric   if (RC == &WebAssembly::EXNREFRegClass)
1280b57cec5SDimitry Andric     return WebAssembly::LOCAL_SET_EXNREF;
1290b57cec5SDimitry Andric   llvm_unreachable("Unexpected register class");
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric /// Get the appropriate local.tee opcode for the given register class.
1330b57cec5SDimitry Andric static unsigned getLocalTeeOpcode(const TargetRegisterClass *RC) {
1340b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
1350b57cec5SDimitry Andric     return WebAssembly::LOCAL_TEE_I32;
1360b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
1370b57cec5SDimitry Andric     return WebAssembly::LOCAL_TEE_I64;
1380b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
1390b57cec5SDimitry Andric     return WebAssembly::LOCAL_TEE_F32;
1400b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
1410b57cec5SDimitry Andric     return WebAssembly::LOCAL_TEE_F64;
1420b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
1430b57cec5SDimitry Andric     return WebAssembly::LOCAL_TEE_V128;
1440b57cec5SDimitry Andric   if (RC == &WebAssembly::EXNREFRegClass)
1450b57cec5SDimitry Andric     return WebAssembly::LOCAL_TEE_EXNREF;
1460b57cec5SDimitry Andric   llvm_unreachable("Unexpected register class");
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric /// Get the type associated with the given register class.
1500b57cec5SDimitry Andric static MVT typeForRegClass(const TargetRegisterClass *RC) {
1510b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
1520b57cec5SDimitry Andric     return MVT::i32;
1530b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
1540b57cec5SDimitry Andric     return MVT::i64;
1550b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
1560b57cec5SDimitry Andric     return MVT::f32;
1570b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
1580b57cec5SDimitry Andric     return MVT::f64;
1590b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
1600b57cec5SDimitry Andric     return MVT::v16i8;
1610b57cec5SDimitry Andric   if (RC == &WebAssembly::EXNREFRegClass)
1620b57cec5SDimitry Andric     return MVT::exnref;
1630b57cec5SDimitry Andric   llvm_unreachable("unrecognized register class");
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric /// Given a MachineOperand of a stackified vreg, return the instruction at the
1670b57cec5SDimitry Andric /// start of the expression tree.
1680b57cec5SDimitry Andric static MachineInstr *findStartOfTree(MachineOperand &MO,
1690b57cec5SDimitry Andric                                      MachineRegisterInfo &MRI,
1700b57cec5SDimitry Andric                                      WebAssemblyFunctionInfo &MFI) {
171*8bcb0991SDimitry Andric   Register Reg = MO.getReg();
1720b57cec5SDimitry Andric   assert(MFI.isVRegStackified(Reg));
1730b57cec5SDimitry Andric   MachineInstr *Def = MRI.getVRegDef(Reg);
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   // Find the first stackified use and proceed from there.
1760b57cec5SDimitry Andric   for (MachineOperand &DefMO : Def->explicit_uses()) {
1770b57cec5SDimitry Andric     if (!DefMO.isReg())
1780b57cec5SDimitry Andric       continue;
1790b57cec5SDimitry Andric     return findStartOfTree(DefMO, MRI, MFI);
1800b57cec5SDimitry Andric   }
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   // If there were no stackified uses, we've reached the start.
1830b57cec5SDimitry Andric   return Def;
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
1870b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** Make Locals Explicit **********\n"
1880b57cec5SDimitry Andric                        "********** Function: "
1890b57cec5SDimitry Andric                     << MF.getName() << '\n');
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   // Disable this pass if directed to do so.
1920b57cec5SDimitry Andric   if (WasmDisableExplicitLocals)
1930b57cec5SDimitry Andric     return false;
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   bool Changed = false;
1960b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
1970b57cec5SDimitry Andric   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
1980b57cec5SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   // Map non-stackified virtual registers to their local ids.
2010b57cec5SDimitry Andric   DenseMap<unsigned, unsigned> Reg2Local;
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   // Handle ARGUMENTS first to ensure that they get the designated numbers.
2040b57cec5SDimitry Andric   for (MachineBasicBlock::iterator I = MF.begin()->begin(),
2050b57cec5SDimitry Andric                                    E = MF.begin()->end();
2060b57cec5SDimitry Andric        I != E;) {
2070b57cec5SDimitry Andric     MachineInstr &MI = *I++;
2080b57cec5SDimitry Andric     if (!WebAssembly::isArgument(MI.getOpcode()))
2090b57cec5SDimitry Andric       break;
210*8bcb0991SDimitry Andric     Register Reg = MI.getOperand(0).getReg();
2110b57cec5SDimitry Andric     assert(!MFI.isVRegStackified(Reg));
2120b57cec5SDimitry Andric     Reg2Local[Reg] = static_cast<unsigned>(MI.getOperand(1).getImm());
2130b57cec5SDimitry Andric     MI.eraseFromParent();
2140b57cec5SDimitry Andric     Changed = true;
2150b57cec5SDimitry Andric   }
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   // Start assigning local numbers after the last parameter.
2180b57cec5SDimitry Andric   unsigned CurLocal = static_cast<unsigned>(MFI.getParams().size());
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   // Precompute the set of registers that are unused, so that we can insert
2210b57cec5SDimitry Andric   // drops to their defs.
2220b57cec5SDimitry Andric   BitVector UseEmpty(MRI.getNumVirtRegs());
2230b57cec5SDimitry Andric   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I)
224*8bcb0991SDimitry Andric     UseEmpty[I] = MRI.use_empty(Register::index2VirtReg(I));
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   // Visit each instruction in the function.
2270b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
2280b57cec5SDimitry Andric     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
2290b57cec5SDimitry Andric       MachineInstr &MI = *I++;
2300b57cec5SDimitry Andric       assert(!WebAssembly::isArgument(MI.getOpcode()));
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric       if (MI.isDebugInstr() || MI.isLabel())
2330b57cec5SDimitry Andric         continue;
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric       // Replace tee instructions with local.tee. The difference is that tee
2360b57cec5SDimitry Andric       // instructions have two defs, while local.tee instructions have one def
2370b57cec5SDimitry Andric       // and an index of a local to write to.
2380b57cec5SDimitry Andric       if (WebAssembly::isTee(MI.getOpcode())) {
2390b57cec5SDimitry Andric         assert(MFI.isVRegStackified(MI.getOperand(0).getReg()));
2400b57cec5SDimitry Andric         assert(!MFI.isVRegStackified(MI.getOperand(1).getReg()));
241*8bcb0991SDimitry Andric         Register OldReg = MI.getOperand(2).getReg();
2420b57cec5SDimitry Andric         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric         // Stackify the input if it isn't stackified yet.
2450b57cec5SDimitry Andric         if (!MFI.isVRegStackified(OldReg)) {
2460b57cec5SDimitry Andric           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
247*8bcb0991SDimitry Andric           Register NewReg = MRI.createVirtualRegister(RC);
2480b57cec5SDimitry Andric           unsigned Opc = getLocalGetOpcode(RC);
2490b57cec5SDimitry Andric           BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc), NewReg)
2500b57cec5SDimitry Andric               .addImm(LocalId);
2510b57cec5SDimitry Andric           MI.getOperand(2).setReg(NewReg);
2520b57cec5SDimitry Andric           MFI.stackifyVReg(NewReg);
2530b57cec5SDimitry Andric         }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric         // Replace the TEE with a LOCAL_TEE.
2560b57cec5SDimitry Andric         unsigned LocalId =
2570b57cec5SDimitry Andric             getLocalId(Reg2Local, CurLocal, MI.getOperand(1).getReg());
2580b57cec5SDimitry Andric         unsigned Opc = getLocalTeeOpcode(RC);
2590b57cec5SDimitry Andric         BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc),
2600b57cec5SDimitry Andric                 MI.getOperand(0).getReg())
2610b57cec5SDimitry Andric             .addImm(LocalId)
2620b57cec5SDimitry Andric             .addReg(MI.getOperand(2).getReg());
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric         MI.eraseFromParent();
2650b57cec5SDimitry Andric         Changed = true;
2660b57cec5SDimitry Andric         continue;
2670b57cec5SDimitry Andric       }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric       // Insert local.sets for any defs that aren't stackified yet. Currently
2700b57cec5SDimitry Andric       // we handle at most one def.
2710b57cec5SDimitry Andric       assert(MI.getDesc().getNumDefs() <= 1);
2720b57cec5SDimitry Andric       if (MI.getDesc().getNumDefs() == 1) {
273*8bcb0991SDimitry Andric         Register OldReg = MI.getOperand(0).getReg();
2740b57cec5SDimitry Andric         if (!MFI.isVRegStackified(OldReg)) {
2750b57cec5SDimitry Andric           const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
276*8bcb0991SDimitry Andric           Register NewReg = MRI.createVirtualRegister(RC);
2770b57cec5SDimitry Andric           auto InsertPt = std::next(MI.getIterator());
2780b57cec5SDimitry Andric           if (MI.getOpcode() == WebAssembly::IMPLICIT_DEF) {
2790b57cec5SDimitry Andric             MI.eraseFromParent();
2800b57cec5SDimitry Andric             Changed = true;
2810b57cec5SDimitry Andric             continue;
2820b57cec5SDimitry Andric           }
283*8bcb0991SDimitry Andric           if (UseEmpty[Register::virtReg2Index(OldReg)]) {
2840b57cec5SDimitry Andric             unsigned Opc = getDropOpcode(RC);
2850b57cec5SDimitry Andric             MachineInstr *Drop =
2860b57cec5SDimitry Andric                 BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
2870b57cec5SDimitry Andric                     .addReg(NewReg);
2880b57cec5SDimitry Andric             // After the drop instruction, this reg operand will not be used
2890b57cec5SDimitry Andric             Drop->getOperand(0).setIsKill();
2900b57cec5SDimitry Andric           } else {
2910b57cec5SDimitry Andric             unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2920b57cec5SDimitry Andric             unsigned Opc = getLocalSetOpcode(RC);
2930b57cec5SDimitry Andric             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
2940b57cec5SDimitry Andric                 .addImm(LocalId)
2950b57cec5SDimitry Andric                 .addReg(NewReg);
2960b57cec5SDimitry Andric           }
2970b57cec5SDimitry Andric           MI.getOperand(0).setReg(NewReg);
2980b57cec5SDimitry Andric           // This register operand of the original instruction is now being used
2990b57cec5SDimitry Andric           // by the inserted drop or local.set instruction, so make it not dead
3000b57cec5SDimitry Andric           // yet.
3010b57cec5SDimitry Andric           MI.getOperand(0).setIsDead(false);
3020b57cec5SDimitry Andric           MFI.stackifyVReg(NewReg);
3030b57cec5SDimitry Andric           Changed = true;
3040b57cec5SDimitry Andric         }
3050b57cec5SDimitry Andric       }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric       // Insert local.gets for any uses that aren't stackified yet.
3080b57cec5SDimitry Andric       MachineInstr *InsertPt = &MI;
3090b57cec5SDimitry Andric       for (MachineOperand &MO : reverse(MI.explicit_uses())) {
3100b57cec5SDimitry Andric         if (!MO.isReg())
3110b57cec5SDimitry Andric           continue;
3120b57cec5SDimitry Andric 
313*8bcb0991SDimitry Andric         Register OldReg = MO.getReg();
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric         // Inline asm may have a def in the middle of the operands. Our contract
3160b57cec5SDimitry Andric         // with inline asm register operands is to provide local indices as
3170b57cec5SDimitry Andric         // immediates.
3180b57cec5SDimitry Andric         if (MO.isDef()) {
3190b57cec5SDimitry Andric           assert(MI.isInlineAsm());
3200b57cec5SDimitry Andric           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
3210b57cec5SDimitry Andric           // If this register operand is tied to another operand, we can't
3220b57cec5SDimitry Andric           // change it to an immediate. Untie it first.
3230b57cec5SDimitry Andric           MI.untieRegOperand(MI.getOperandNo(&MO));
3240b57cec5SDimitry Andric           MO.ChangeToImmediate(LocalId);
3250b57cec5SDimitry Andric           continue;
3260b57cec5SDimitry Andric         }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric         // If we see a stackified register, prepare to insert subsequent
3290b57cec5SDimitry Andric         // local.gets before the start of its tree.
3300b57cec5SDimitry Andric         if (MFI.isVRegStackified(OldReg)) {
3310b57cec5SDimitry Andric           InsertPt = findStartOfTree(MO, MRI, MFI);
3320b57cec5SDimitry Andric           continue;
3330b57cec5SDimitry Andric         }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric         // Our contract with inline asm register operands is to provide local
3360b57cec5SDimitry Andric         // indices as immediates.
3370b57cec5SDimitry Andric         if (MI.isInlineAsm()) {
3380b57cec5SDimitry Andric           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
3390b57cec5SDimitry Andric           // Untie it first if this reg operand is tied to another operand.
3400b57cec5SDimitry Andric           MI.untieRegOperand(MI.getOperandNo(&MO));
3410b57cec5SDimitry Andric           MO.ChangeToImmediate(LocalId);
3420b57cec5SDimitry Andric           continue;
3430b57cec5SDimitry Andric         }
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric         // Insert a local.get.
3460b57cec5SDimitry Andric         unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
3470b57cec5SDimitry Andric         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
348*8bcb0991SDimitry Andric         Register NewReg = MRI.createVirtualRegister(RC);
3490b57cec5SDimitry Andric         unsigned Opc = getLocalGetOpcode(RC);
3500b57cec5SDimitry Andric         InsertPt =
3510b57cec5SDimitry Andric             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc), NewReg)
3520b57cec5SDimitry Andric                 .addImm(LocalId);
3530b57cec5SDimitry Andric         MO.setReg(NewReg);
3540b57cec5SDimitry Andric         MFI.stackifyVReg(NewReg);
3550b57cec5SDimitry Andric         Changed = true;
3560b57cec5SDimitry Andric       }
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric       // Coalesce and eliminate COPY instructions.
3590b57cec5SDimitry Andric       if (WebAssembly::isCopy(MI.getOpcode())) {
3600b57cec5SDimitry Andric         MRI.replaceRegWith(MI.getOperand(1).getReg(),
3610b57cec5SDimitry Andric                            MI.getOperand(0).getReg());
3620b57cec5SDimitry Andric         MI.eraseFromParent();
3630b57cec5SDimitry Andric         Changed = true;
3640b57cec5SDimitry Andric       }
3650b57cec5SDimitry Andric     }
3660b57cec5SDimitry Andric   }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   // Define the locals.
3690b57cec5SDimitry Andric   // TODO: Sort the locals for better compression.
3700b57cec5SDimitry Andric   MFI.setNumLocals(CurLocal - MFI.getParams().size());
3710b57cec5SDimitry Andric   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
372*8bcb0991SDimitry Andric     unsigned Reg = Register::index2VirtReg(I);
3730b57cec5SDimitry Andric     auto RL = Reg2Local.find(Reg);
3740b57cec5SDimitry Andric     if (RL == Reg2Local.end() || RL->second < MFI.getParams().size())
3750b57cec5SDimitry Andric       continue;
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric     MFI.setLocal(RL->second - MFI.getParams().size(),
3780b57cec5SDimitry Andric                  typeForRegClass(MRI.getRegClass(Reg)));
3790b57cec5SDimitry Andric     Changed = true;
3800b57cec5SDimitry Andric   }
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric #ifndef NDEBUG
3830b57cec5SDimitry Andric   // Assert that all registers have been stackified at this point.
3840b57cec5SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
3850b57cec5SDimitry Andric     for (const MachineInstr &MI : MBB) {
3860b57cec5SDimitry Andric       if (MI.isDebugInstr() || MI.isLabel())
3870b57cec5SDimitry Andric         continue;
3880b57cec5SDimitry Andric       for (const MachineOperand &MO : MI.explicit_operands()) {
3890b57cec5SDimitry Andric         assert(
3900b57cec5SDimitry Andric             (!MO.isReg() || MRI.use_empty(MO.getReg()) ||
3910b57cec5SDimitry Andric              MFI.isVRegStackified(MO.getReg())) &&
3920b57cec5SDimitry Andric             "WebAssemblyExplicitLocals failed to stackify a register operand");
3930b57cec5SDimitry Andric       }
3940b57cec5SDimitry Andric     }
3950b57cec5SDimitry Andric   }
3960b57cec5SDimitry Andric #endif
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric   return Changed;
3990b57cec5SDimitry Andric }
400