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
14*6a87ddacSThomas Lively /// registers into locals, inserting explicit local.get and local.set
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 
348a9cb242SWouter van Oortmerssen // A command-line option to disable this pass, and keep implicit locals
358a9cb242SWouter van Oortmerssen // for the purpose of testing with lit/llc ONLY.
368a9cb242SWouter van Oortmerssen // This produces output which is not valid WebAssembly, and is not supported
378a9cb242SWouter van Oortmerssen // by assemblers/disassemblers and other MC based tools.
388a9cb242SWouter van Oortmerssen static cl::opt<bool> WasmDisableExplicitLocals(
398a9cb242SWouter van Oortmerssen     "wasm-disable-explicit-locals", cl::Hidden,
408a9cb242SWouter van Oortmerssen     cl::desc("WebAssembly: output implicit locals in"
418a9cb242SWouter van Oortmerssen              " instruction output for test purposes only."),
427d7409e5SDan Gohman     cl::init(false));
437d7409e5SDan Gohman 
444fc4e42dSDan Gohman namespace {
454fc4e42dSDan Gohman class WebAssemblyExplicitLocals final : public MachineFunctionPass {
464fc4e42dSDan Gohman   StringRef getPassName() const override {
474fc4e42dSDan Gohman     return "WebAssembly Explicit Locals";
484fc4e42dSDan Gohman   }
494fc4e42dSDan Gohman 
504fc4e42dSDan Gohman   void getAnalysisUsage(AnalysisUsage &AU) const override {
514fc4e42dSDan Gohman     AU.setPreservesCFG();
524fc4e42dSDan Gohman     AU.addPreserved<MachineBlockFrequencyInfo>();
534fc4e42dSDan Gohman     MachineFunctionPass::getAnalysisUsage(AU);
544fc4e42dSDan Gohman   }
554fc4e42dSDan Gohman 
564fc4e42dSDan Gohman   bool runOnMachineFunction(MachineFunction &MF) override;
574fc4e42dSDan Gohman 
584fc4e42dSDan Gohman public:
594fc4e42dSDan Gohman   static char ID; // Pass identification, replacement for typeid
604fc4e42dSDan Gohman   WebAssemblyExplicitLocals() : MachineFunctionPass(ID) {}
614fc4e42dSDan Gohman };
624fc4e42dSDan Gohman } // end anonymous namespace
634fc4e42dSDan Gohman 
644fc4e42dSDan Gohman char WebAssemblyExplicitLocals::ID = 0;
6540926451SJacob Gravelle INITIALIZE_PASS(WebAssemblyExplicitLocals, DEBUG_TYPE,
6640926451SJacob Gravelle                 "Convert registers to WebAssembly locals", false, false)
6740926451SJacob Gravelle 
684fc4e42dSDan Gohman FunctionPass *llvm::createWebAssemblyExplicitLocals() {
694fc4e42dSDan Gohman   return new WebAssemblyExplicitLocals();
704fc4e42dSDan Gohman }
714fc4e42dSDan Gohman 
724fc4e42dSDan Gohman /// Return a local id number for the given register, assigning it a new one
734fc4e42dSDan Gohman /// if it doesn't yet have one.
744fc4e42dSDan Gohman static unsigned getLocalId(DenseMap<unsigned, unsigned> &Reg2Local,
754fc4e42dSDan Gohman                            unsigned &CurLocal, unsigned Reg) {
76d934cb88SDan Gohman   auto P = Reg2Local.insert(std::make_pair(Reg, CurLocal));
77d934cb88SDan Gohman   if (P.second)
78d934cb88SDan Gohman     ++CurLocal;
79d934cb88SDan Gohman   return P.first->second;
80d934cb88SDan Gohman }
81d934cb88SDan Gohman 
82d934cb88SDan Gohman /// Get the appropriate drop opcode for the given register class.
83d934cb88SDan Gohman static unsigned getDropOpcode(const TargetRegisterClass *RC) {
84d934cb88SDan Gohman   if (RC == &WebAssembly::I32RegClass)
85d934cb88SDan Gohman     return WebAssembly::DROP_I32;
86d934cb88SDan Gohman   if (RC == &WebAssembly::I64RegClass)
87d934cb88SDan Gohman     return WebAssembly::DROP_I64;
88d934cb88SDan Gohman   if (RC == &WebAssembly::F32RegClass)
89d934cb88SDan Gohman     return WebAssembly::DROP_F32;
90d934cb88SDan Gohman   if (RC == &WebAssembly::F64RegClass)
91d934cb88SDan Gohman     return WebAssembly::DROP_F64;
92d934cb88SDan Gohman   if (RC == &WebAssembly::V128RegClass)
93d934cb88SDan Gohman     return WebAssembly::DROP_V128;
940de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
950de58729SHeejin Ahn     return WebAssembly::DROP_EXCEPT_REF;
96d934cb88SDan Gohman   llvm_unreachable("Unexpected register class");
974fc4e42dSDan Gohman }
984fc4e42dSDan Gohman 
99*6a87ddacSThomas Lively /// Get the appropriate local.get opcode for the given register class.
1004fc4e42dSDan Gohman static unsigned getGetLocalOpcode(const TargetRegisterClass *RC) {
1014fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
102*6a87ddacSThomas Lively     return WebAssembly::LOCAL_GET_I32;
1034fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
104*6a87ddacSThomas Lively     return WebAssembly::LOCAL_GET_I64;
1054fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
106*6a87ddacSThomas Lively     return WebAssembly::LOCAL_GET_F32;
1074fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
108*6a87ddacSThomas Lively     return WebAssembly::LOCAL_GET_F64;
1094fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
110*6a87ddacSThomas Lively     return WebAssembly::LOCAL_GET_V128;
1110de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
112*6a87ddacSThomas Lively     return WebAssembly::LOCAL_GET_EXCEPT_REF;
1134fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1144fc4e42dSDan Gohman }
1154fc4e42dSDan Gohman 
116*6a87ddacSThomas Lively /// Get the appropriate local.set opcode for the given register class.
1174fc4e42dSDan Gohman static unsigned getSetLocalOpcode(const TargetRegisterClass *RC) {
1184fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
119*6a87ddacSThomas Lively     return WebAssembly::LOCAL_SET_I32;
1204fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
121*6a87ddacSThomas Lively     return WebAssembly::LOCAL_SET_I64;
1224fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
123*6a87ddacSThomas Lively     return WebAssembly::LOCAL_SET_F32;
1244fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
125*6a87ddacSThomas Lively     return WebAssembly::LOCAL_SET_F64;
1264fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
127*6a87ddacSThomas Lively     return WebAssembly::LOCAL_SET_V128;
1280de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
129*6a87ddacSThomas Lively     return WebAssembly::LOCAL_SET_EXCEPT_REF;
1304fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1314fc4e42dSDan Gohman }
1324fc4e42dSDan Gohman 
133*6a87ddacSThomas Lively /// Get the appropriate local.tee opcode for the given register class.
1344fc4e42dSDan Gohman static unsigned getTeeLocalOpcode(const TargetRegisterClass *RC) {
1354fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
136*6a87ddacSThomas Lively     return WebAssembly::LOCAL_TEE_I32;
1374fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
138*6a87ddacSThomas Lively     return WebAssembly::LOCAL_TEE_I64;
1394fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
140*6a87ddacSThomas Lively     return WebAssembly::LOCAL_TEE_F32;
1414fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
142*6a87ddacSThomas Lively     return WebAssembly::LOCAL_TEE_F64;
1434fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
144*6a87ddacSThomas Lively     return WebAssembly::LOCAL_TEE_V128;
1450de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
146*6a87ddacSThomas Lively     return WebAssembly::LOCAL_TEE_EXCEPT_REF;
1474fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1484fc4e42dSDan Gohman }
1494fc4e42dSDan Gohman 
1504fc4e42dSDan Gohman /// Get the type associated with the given register class.
1513acb187dSDan Gohman static MVT typeForRegClass(const TargetRegisterClass *RC) {
1524fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
1533acb187dSDan Gohman     return MVT::i32;
1544fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1553acb187dSDan Gohman     return MVT::i64;
1564fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1573acb187dSDan Gohman     return MVT::f32;
1584fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1593acb187dSDan Gohman     return MVT::f64;
160409f5840SThomas Lively   if (RC == &WebAssembly::V128RegClass)
161409f5840SThomas Lively     return MVT::v16i8;
1620de58729SHeejin Ahn   if (RC == &WebAssembly::EXCEPT_REFRegClass)
1630de58729SHeejin Ahn     return MVT::ExceptRef;
1644fc4e42dSDan Gohman   llvm_unreachable("unrecognized register class");
1654fc4e42dSDan Gohman }
1664fc4e42dSDan Gohman 
1674fc4e42dSDan Gohman /// Given a MachineOperand of a stackified vreg, return the instruction at the
1684fc4e42dSDan Gohman /// start of the expression tree.
1698a9cb242SWouter van Oortmerssen static MachineInstr *findStartOfTree(MachineOperand &MO,
1704fc4e42dSDan Gohman                                      MachineRegisterInfo &MRI,
1714fc4e42dSDan Gohman                                      WebAssemblyFunctionInfo &MFI) {
1724fc4e42dSDan Gohman   unsigned Reg = MO.getReg();
1734fc4e42dSDan Gohman   assert(MFI.isVRegStackified(Reg));
1744fc4e42dSDan Gohman   MachineInstr *Def = MRI.getVRegDef(Reg);
1754fc4e42dSDan Gohman 
1764fc4e42dSDan Gohman   // Find the first stackified use and proceed from there.
1774fc4e42dSDan Gohman   for (MachineOperand &DefMO : Def->explicit_uses()) {
1784fc4e42dSDan Gohman     if (!DefMO.isReg())
1794fc4e42dSDan Gohman       continue;
1808a9cb242SWouter van Oortmerssen     return findStartOfTree(DefMO, MRI, MFI);
1814fc4e42dSDan Gohman   }
1824fc4e42dSDan Gohman 
1834fc4e42dSDan Gohman   // If there were no stackified uses, we've reached the start.
1844fc4e42dSDan Gohman   return Def;
1854fc4e42dSDan Gohman }
1864fc4e42dSDan Gohman 
1874fc4e42dSDan Gohman bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
188d34e60caSNicola Zaghen   LLVM_DEBUG(dbgs() << "********** Make Locals Explicit **********\n"
1894fc4e42dSDan Gohman                        "********** Function: "
1904fc4e42dSDan Gohman                     << MF.getName() << '\n');
1914fc4e42dSDan Gohman 
1927d7409e5SDan Gohman   // Disable this pass if directed to do so.
1938a9cb242SWouter van Oortmerssen   if (WasmDisableExplicitLocals)
1947d7409e5SDan Gohman     return false;
1957d7409e5SDan Gohman 
1964fc4e42dSDan Gohman   bool Changed = false;
1974fc4e42dSDan Gohman   MachineRegisterInfo &MRI = MF.getRegInfo();
1984fc4e42dSDan Gohman   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
1994fc4e42dSDan Gohman   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
2004fc4e42dSDan Gohman 
2014fc4e42dSDan Gohman   // Map non-stackified virtual registers to their local ids.
2024fc4e42dSDan Gohman   DenseMap<unsigned, unsigned> Reg2Local;
2034fc4e42dSDan Gohman 
2044fc4e42dSDan Gohman   // Handle ARGUMENTS first to ensure that they get the designated numbers.
2054fc4e42dSDan Gohman   for (MachineBasicBlock::iterator I = MF.begin()->begin(),
2064fc4e42dSDan Gohman                                    E = MF.begin()->end();
2074fc4e42dSDan Gohman        I != E;) {
2084fc4e42dSDan Gohman     MachineInstr &MI = *I++;
2094fc4e42dSDan Gohman     if (!WebAssembly::isArgument(MI))
2104fc4e42dSDan Gohman       break;
2114fc4e42dSDan Gohman     unsigned Reg = MI.getOperand(0).getReg();
2124fc4e42dSDan Gohman     assert(!MFI.isVRegStackified(Reg));
2138a9cb242SWouter van Oortmerssen     Reg2Local[Reg] = static_cast<unsigned>(MI.getOperand(1).getImm());
2144fc4e42dSDan Gohman     MI.eraseFromParent();
2154fc4e42dSDan Gohman     Changed = true;
2164fc4e42dSDan Gohman   }
2174fc4e42dSDan Gohman 
2184fc4e42dSDan Gohman   // Start assigning local numbers after the last parameter.
2198a9cb242SWouter van Oortmerssen   unsigned CurLocal = static_cast<unsigned>(MFI.getParams().size());
2204fc4e42dSDan Gohman 
221d934cb88SDan Gohman   // Precompute the set of registers that are unused, so that we can insert
222d934cb88SDan Gohman   // drops to their defs.
223d934cb88SDan Gohman   BitVector UseEmpty(MRI.getNumVirtRegs());
2248a9cb242SWouter van Oortmerssen   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I)
2258a9cb242SWouter van Oortmerssen     UseEmpty[I] = MRI.use_empty(TargetRegisterInfo::index2VirtReg(I));
226d934cb88SDan Gohman 
2274fc4e42dSDan Gohman   // Visit each instruction in the function.
2284fc4e42dSDan Gohman   for (MachineBasicBlock &MBB : MF) {
2294fc4e42dSDan Gohman     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
2304fc4e42dSDan Gohman       MachineInstr &MI = *I++;
2314fc4e42dSDan Gohman       assert(!WebAssembly::isArgument(MI));
2324fc4e42dSDan Gohman 
233801bf7ebSShiva Chen       if (MI.isDebugInstr() || MI.isLabel())
2344fc4e42dSDan Gohman         continue;
2354fc4e42dSDan Gohman 
236*6a87ddacSThomas Lively       // Replace tee instructions with local.tee. The difference is that tee
237*6a87ddacSThomas Lively       // instructions have two defs, while local.tee instructions have one def
2384fc4e42dSDan Gohman       // and an index of a local to write to.
2394fc4e42dSDan Gohman       if (WebAssembly::isTee(MI)) {
2404fc4e42dSDan Gohman         assert(MFI.isVRegStackified(MI.getOperand(0).getReg()));
2414fc4e42dSDan Gohman         assert(!MFI.isVRegStackified(MI.getOperand(1).getReg()));
2424fc4e42dSDan Gohman         unsigned OldReg = MI.getOperand(2).getReg();
2434fc4e42dSDan Gohman         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2444fc4e42dSDan Gohman 
2454fc4e42dSDan Gohman         // Stackify the input if it isn't stackified yet.
2464fc4e42dSDan Gohman         if (!MFI.isVRegStackified(OldReg)) {
2474fc4e42dSDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2484fc4e42dSDan Gohman           unsigned NewReg = MRI.createVirtualRegister(RC);
2494fc4e42dSDan Gohman           unsigned Opc = getGetLocalOpcode(RC);
2504fc4e42dSDan Gohman           BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc), NewReg)
2514fc4e42dSDan Gohman               .addImm(LocalId);
2524fc4e42dSDan Gohman           MI.getOperand(2).setReg(NewReg);
2534fc4e42dSDan Gohman           MFI.stackifyVReg(NewReg);
2544fc4e42dSDan Gohman         }
2554fc4e42dSDan Gohman 
256*6a87ddacSThomas Lively         // Replace the TEE with a LOCAL_TEE.
2574fc4e42dSDan Gohman         unsigned LocalId =
2584fc4e42dSDan Gohman             getLocalId(Reg2Local, CurLocal, MI.getOperand(1).getReg());
2594fc4e42dSDan Gohman         unsigned Opc = getTeeLocalOpcode(RC);
2604fc4e42dSDan Gohman         BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc),
2614fc4e42dSDan Gohman                 MI.getOperand(0).getReg())
2624fc4e42dSDan Gohman             .addImm(LocalId)
2634fc4e42dSDan Gohman             .addReg(MI.getOperand(2).getReg());
2644fc4e42dSDan Gohman 
2654fc4e42dSDan Gohman         MI.eraseFromParent();
2664fc4e42dSDan Gohman         Changed = true;
2674fc4e42dSDan Gohman         continue;
2684fc4e42dSDan Gohman       }
2694fc4e42dSDan Gohman 
270*6a87ddacSThomas Lively       // Insert local.sets for any defs that aren't stackified yet. Currently
2714fc4e42dSDan Gohman       // we handle at most one def.
2724fc4e42dSDan Gohman       assert(MI.getDesc().getNumDefs() <= 1);
2734fc4e42dSDan Gohman       if (MI.getDesc().getNumDefs() == 1) {
2744fc4e42dSDan Gohman         unsigned OldReg = MI.getOperand(0).getReg();
275d934cb88SDan Gohman         if (!MFI.isVRegStackified(OldReg)) {
2764fc4e42dSDan Gohman           const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2774fc4e42dSDan Gohman           unsigned NewReg = MRI.createVirtualRegister(RC);
2784fc4e42dSDan Gohman           auto InsertPt = std::next(MachineBasicBlock::iterator(&MI));
279d934cb88SDan Gohman           if (MI.getOpcode() == WebAssembly::IMPLICIT_DEF) {
280d934cb88SDan Gohman             MI.eraseFromParent();
281d934cb88SDan Gohman             Changed = true;
282d934cb88SDan Gohman             continue;
283d934cb88SDan Gohman           }
284d934cb88SDan Gohman           if (UseEmpty[TargetRegisterInfo::virtReg2Index(OldReg)]) {
285d934cb88SDan Gohman             unsigned Opc = getDropOpcode(RC);
286891a7472SHeejin Ahn             MachineInstr *Drop =
287d934cb88SDan Gohman                 BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
288d934cb88SDan Gohman                     .addReg(NewReg);
289891a7472SHeejin Ahn             // After the drop instruction, this reg operand will not be used
290891a7472SHeejin Ahn             Drop->getOperand(0).setIsKill();
291d934cb88SDan Gohman           } else {
292d934cb88SDan Gohman             unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2934fc4e42dSDan Gohman             unsigned Opc = getSetLocalOpcode(RC);
2944fc4e42dSDan Gohman             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
2954fc4e42dSDan Gohman                 .addImm(LocalId)
2964fc4e42dSDan Gohman                 .addReg(NewReg);
297d934cb88SDan Gohman           }
2984fc4e42dSDan Gohman           MI.getOperand(0).setReg(NewReg);
2994d98dfb6SHeejin Ahn           // This register operand of the original instruction is now being used
300*6a87ddacSThomas Lively           // by the inserted drop or local.set instruction, so make it not dead
3014d98dfb6SHeejin Ahn           // yet.
302891a7472SHeejin Ahn           MI.getOperand(0).setIsDead(false);
3034fc4e42dSDan Gohman           MFI.stackifyVReg(NewReg);
3044fc4e42dSDan Gohman           Changed = true;
3054fc4e42dSDan Gohman         }
3064fc4e42dSDan Gohman       }
3074fc4e42dSDan Gohman 
308*6a87ddacSThomas Lively       // Insert local.gets for any uses that aren't stackified yet.
3094fc4e42dSDan Gohman       MachineInstr *InsertPt = &MI;
3104fc4e42dSDan Gohman       for (MachineOperand &MO : reverse(MI.explicit_uses())) {
3114fc4e42dSDan Gohman         if (!MO.isReg())
3124fc4e42dSDan Gohman           continue;
3134fc4e42dSDan Gohman 
3144fc4e42dSDan Gohman         unsigned OldReg = MO.getReg();
3154fc4e42dSDan Gohman 
316b465aa05SDan Gohman         // Inline asm may have a def in the middle of the operands. Our contract
317b465aa05SDan Gohman         // with inline asm register operands is to provide local indices as
318b465aa05SDan Gohman         // immediates.
319b465aa05SDan Gohman         if (MO.isDef()) {
320b465aa05SDan Gohman           assert(MI.getOpcode() == TargetOpcode::INLINEASM);
321b465aa05SDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
322300f42fbSHeejin Ahn           // If this register operand is tied to another operand, we can't
323300f42fbSHeejin Ahn           // change it to an immediate. Untie it first.
324300f42fbSHeejin Ahn           MI.untieRegOperand(MI.getOperandNo(&MO));
325045a217bSDan Gohman           MO.ChangeToImmediate(LocalId);
326b465aa05SDan Gohman           continue;
327b465aa05SDan Gohman         }
328b465aa05SDan Gohman 
3294fc4e42dSDan Gohman         // If we see a stackified register, prepare to insert subsequent
330*6a87ddacSThomas Lively         // local.gets before the start of its tree.
3314fc4e42dSDan Gohman         if (MFI.isVRegStackified(OldReg)) {
3328a9cb242SWouter van Oortmerssen           InsertPt = findStartOfTree(MO, MRI, MFI);
3334fc4e42dSDan Gohman           continue;
3344fc4e42dSDan Gohman         }
3354fc4e42dSDan Gohman 
336b465aa05SDan Gohman         // Our contract with inline asm register operands is to provide local
337b465aa05SDan Gohman         // indices as immediates.
338b465aa05SDan Gohman         if (MI.getOpcode() == TargetOpcode::INLINEASM) {
339b465aa05SDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
340300f42fbSHeejin Ahn           // Untie it first if this reg operand is tied to another operand.
341300f42fbSHeejin Ahn           MI.untieRegOperand(MI.getOperandNo(&MO));
342045a217bSDan Gohman           MO.ChangeToImmediate(LocalId);
343b465aa05SDan Gohman           continue;
344b465aa05SDan Gohman         }
345b465aa05SDan Gohman 
346*6a87ddacSThomas Lively         // Insert a local.get.
3474fc4e42dSDan Gohman         unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
3484fc4e42dSDan Gohman         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
3494fc4e42dSDan Gohman         unsigned NewReg = MRI.createVirtualRegister(RC);
3504fc4e42dSDan Gohman         unsigned Opc = getGetLocalOpcode(RC);
3514fc4e42dSDan Gohman         InsertPt =
3524fc4e42dSDan Gohman             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc), NewReg)
3534fc4e42dSDan Gohman                 .addImm(LocalId);
3544fc4e42dSDan Gohman         MO.setReg(NewReg);
3554fc4e42dSDan Gohman         MFI.stackifyVReg(NewReg);
3564fc4e42dSDan Gohman         Changed = true;
3574fc4e42dSDan Gohman       }
3584fc4e42dSDan Gohman 
3594fc4e42dSDan Gohman       // Coalesce and eliminate COPY instructions.
3604fc4e42dSDan Gohman       if (WebAssembly::isCopy(MI)) {
3614fc4e42dSDan Gohman         MRI.replaceRegWith(MI.getOperand(1).getReg(),
3624fc4e42dSDan Gohman                            MI.getOperand(0).getReg());
3634fc4e42dSDan Gohman         MI.eraseFromParent();
3644fc4e42dSDan Gohman         Changed = true;
3654fc4e42dSDan Gohman       }
3664fc4e42dSDan Gohman     }
3674fc4e42dSDan Gohman   }
3684fc4e42dSDan Gohman 
3693acb187dSDan Gohman   // Define the locals.
370d934cb88SDan Gohman   // TODO: Sort the locals for better compression.
371d934cb88SDan Gohman   MFI.setNumLocals(CurLocal - MFI.getParams().size());
3728a9cb242SWouter van Oortmerssen   for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
3738a9cb242SWouter van Oortmerssen     unsigned Reg = TargetRegisterInfo::index2VirtReg(I);
3748a9cb242SWouter van Oortmerssen     auto RL = Reg2Local.find(Reg);
3758a9cb242SWouter van Oortmerssen     if (RL == Reg2Local.end() || RL->second < MFI.getParams().size())
3764fc4e42dSDan Gohman       continue;
3774fc4e42dSDan Gohman 
3788a9cb242SWouter van Oortmerssen     MFI.setLocal(RL->second - MFI.getParams().size(),
379d934cb88SDan Gohman                  typeForRegClass(MRI.getRegClass(Reg)));
3804fc4e42dSDan Gohman     Changed = true;
3814fc4e42dSDan Gohman   }
3824fc4e42dSDan Gohman 
383a7be3755SWouter van Oortmerssen #ifndef NDEBUG
384a7be3755SWouter van Oortmerssen   // Assert that all registers have been stackified at this point.
385a7be3755SWouter van Oortmerssen   for (const MachineBasicBlock &MBB : MF) {
386a7be3755SWouter van Oortmerssen     for (const MachineInstr &MI : MBB) {
387a7be3755SWouter van Oortmerssen       if (MI.isDebugInstr() || MI.isLabel())
388a7be3755SWouter van Oortmerssen         continue;
389a7be3755SWouter van Oortmerssen       for (const MachineOperand &MO : MI.explicit_operands()) {
390a7be3755SWouter van Oortmerssen         assert(
391a7be3755SWouter van Oortmerssen             (!MO.isReg() || MRI.use_empty(MO.getReg()) ||
392a7be3755SWouter van Oortmerssen              MFI.isVRegStackified(MO.getReg())) &&
393a7be3755SWouter van Oortmerssen             "WebAssemblyExplicitLocals failed to stackify a register operand");
394a67c4137SWouter van Oortmerssen       }
395a7be3755SWouter van Oortmerssen     }
396a7be3755SWouter van Oortmerssen   }
397a7be3755SWouter van Oortmerssen #endif
398ab26bd06SWouter van Oortmerssen 
399a7be3755SWouter van Oortmerssen   return Changed;
400ab26bd06SWouter van Oortmerssen }
401