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