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
114fc4e42dSDan Gohman /// \brief 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 
344fc4e42dSDan Gohman namespace {
354fc4e42dSDan Gohman class WebAssemblyExplicitLocals final : public MachineFunctionPass {
364fc4e42dSDan Gohman   StringRef getPassName() const override {
374fc4e42dSDan Gohman     return "WebAssembly Explicit Locals";
384fc4e42dSDan Gohman   }
394fc4e42dSDan Gohman 
404fc4e42dSDan Gohman   void getAnalysisUsage(AnalysisUsage &AU) const override {
414fc4e42dSDan Gohman     AU.setPreservesCFG();
424fc4e42dSDan Gohman     AU.addPreserved<MachineBlockFrequencyInfo>();
434fc4e42dSDan Gohman     MachineFunctionPass::getAnalysisUsage(AU);
444fc4e42dSDan Gohman   }
454fc4e42dSDan Gohman 
464fc4e42dSDan Gohman   bool runOnMachineFunction(MachineFunction &MF) override;
474fc4e42dSDan Gohman 
484fc4e42dSDan Gohman public:
494fc4e42dSDan Gohman   static char ID; // Pass identification, replacement for typeid
504fc4e42dSDan Gohman   WebAssemblyExplicitLocals() : MachineFunctionPass(ID) {}
514fc4e42dSDan Gohman };
524fc4e42dSDan Gohman } // end anonymous namespace
534fc4e42dSDan Gohman 
544fc4e42dSDan Gohman char WebAssemblyExplicitLocals::ID = 0;
554fc4e42dSDan Gohman FunctionPass *llvm::createWebAssemblyExplicitLocals() {
564fc4e42dSDan Gohman   return new WebAssemblyExplicitLocals();
574fc4e42dSDan Gohman }
584fc4e42dSDan Gohman 
594fc4e42dSDan Gohman /// Return a local id number for the given register, assigning it a new one
604fc4e42dSDan Gohman /// if it doesn't yet have one.
614fc4e42dSDan Gohman static unsigned getLocalId(DenseMap<unsigned, unsigned> &Reg2Local,
624fc4e42dSDan Gohman                            unsigned &CurLocal, unsigned Reg) {
634fc4e42dSDan Gohman   return Reg2Local.insert(std::make_pair(Reg, CurLocal++)).first->second;
644fc4e42dSDan Gohman }
654fc4e42dSDan Gohman 
664fc4e42dSDan Gohman /// Get the appropriate get_local opcode for the given register class.
674fc4e42dSDan Gohman static unsigned getGetLocalOpcode(const TargetRegisterClass *RC) {
684fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
694fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_I32;
704fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
714fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_I64;
724fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
734fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_F32;
744fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
754fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_F64;
764fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
774fc4e42dSDan Gohman     return WebAssembly::GET_LOCAL_V128;
784fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
794fc4e42dSDan Gohman }
804fc4e42dSDan Gohman 
814fc4e42dSDan Gohman /// Get the appropriate set_local opcode for the given register class.
824fc4e42dSDan Gohman static unsigned getSetLocalOpcode(const TargetRegisterClass *RC) {
834fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
844fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_I32;
854fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
864fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_I64;
874fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
884fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_F32;
894fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
904fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_F64;
914fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
924fc4e42dSDan Gohman     return WebAssembly::SET_LOCAL_V128;
934fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
944fc4e42dSDan Gohman }
954fc4e42dSDan Gohman 
964fc4e42dSDan Gohman /// Get the appropriate tee_local opcode for the given register class.
974fc4e42dSDan Gohman static unsigned getTeeLocalOpcode(const TargetRegisterClass *RC) {
984fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
994fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_I32;
1004fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1014fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_I64;
1024fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1034fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_F32;
1044fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1054fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_F64;
1064fc4e42dSDan Gohman   if (RC == &WebAssembly::V128RegClass)
1074fc4e42dSDan Gohman     return WebAssembly::TEE_LOCAL_V128;
1084fc4e42dSDan Gohman   llvm_unreachable("Unexpected register class");
1094fc4e42dSDan Gohman }
1104fc4e42dSDan Gohman 
1114fc4e42dSDan Gohman /// Get the type associated with the given register class.
1123acb187dSDan Gohman static MVT typeForRegClass(const TargetRegisterClass *RC) {
1134fc4e42dSDan Gohman   if (RC == &WebAssembly::I32RegClass)
1143acb187dSDan Gohman     return MVT::i32;
1154fc4e42dSDan Gohman   if (RC == &WebAssembly::I64RegClass)
1163acb187dSDan Gohman     return MVT::i64;
1174fc4e42dSDan Gohman   if (RC == &WebAssembly::F32RegClass)
1183acb187dSDan Gohman     return MVT::f32;
1194fc4e42dSDan Gohman   if (RC == &WebAssembly::F64RegClass)
1203acb187dSDan Gohman     return MVT::f64;
1214fc4e42dSDan Gohman   llvm_unreachable("unrecognized register class");
1224fc4e42dSDan Gohman }
1234fc4e42dSDan Gohman 
1244fc4e42dSDan Gohman /// Given a MachineOperand of a stackified vreg, return the instruction at the
1254fc4e42dSDan Gohman /// start of the expression tree.
1264fc4e42dSDan Gohman static MachineInstr *FindStartOfTree(MachineOperand &MO,
1274fc4e42dSDan Gohman                                      MachineRegisterInfo &MRI,
1284fc4e42dSDan Gohman                                      WebAssemblyFunctionInfo &MFI) {
1294fc4e42dSDan Gohman   unsigned Reg = MO.getReg();
1304fc4e42dSDan Gohman   assert(MFI.isVRegStackified(Reg));
1314fc4e42dSDan Gohman   MachineInstr *Def = MRI.getVRegDef(Reg);
1324fc4e42dSDan Gohman 
1334fc4e42dSDan Gohman   // Find the first stackified use and proceed from there.
1344fc4e42dSDan Gohman   for (MachineOperand &DefMO : Def->explicit_uses()) {
1354fc4e42dSDan Gohman     if (!DefMO.isReg())
1364fc4e42dSDan Gohman       continue;
1374fc4e42dSDan Gohman     return FindStartOfTree(DefMO, MRI, MFI);
1384fc4e42dSDan Gohman   }
1394fc4e42dSDan Gohman 
1404fc4e42dSDan Gohman   // If there were no stackified uses, we've reached the start.
1414fc4e42dSDan Gohman   return Def;
1424fc4e42dSDan Gohman }
1434fc4e42dSDan Gohman 
1444fc4e42dSDan Gohman bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
1454fc4e42dSDan Gohman   DEBUG(dbgs() << "********** Make Locals Explicit **********\n"
1464fc4e42dSDan Gohman                   "********** Function: "
1474fc4e42dSDan Gohman                << MF.getName() << '\n');
1484fc4e42dSDan Gohman 
149*66caac57SDan Gohman   // Disable this pass if we aren't doing direct wasm object emission.
150*66caac57SDan Gohman   if (MF.getSubtarget<WebAssemblySubtarget>()
151*66caac57SDan Gohman         .getTargetTriple().isOSBinFormatELF())
152*66caac57SDan Gohman     return false;
153*66caac57SDan Gohman 
1544fc4e42dSDan Gohman   bool Changed = false;
1554fc4e42dSDan Gohman   MachineRegisterInfo &MRI = MF.getRegInfo();
1564fc4e42dSDan Gohman   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
1574fc4e42dSDan Gohman   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
1584fc4e42dSDan Gohman 
1594fc4e42dSDan Gohman   // Map non-stackified virtual registers to their local ids.
1604fc4e42dSDan Gohman   DenseMap<unsigned, unsigned> Reg2Local;
1614fc4e42dSDan Gohman 
1624fc4e42dSDan Gohman   // Handle ARGUMENTS first to ensure that they get the designated numbers.
1634fc4e42dSDan Gohman   for (MachineBasicBlock::iterator I = MF.begin()->begin(),
1644fc4e42dSDan Gohman                                    E = MF.begin()->end();
1654fc4e42dSDan Gohman        I != E;) {
1664fc4e42dSDan Gohman     MachineInstr &MI = *I++;
1674fc4e42dSDan Gohman     if (!WebAssembly::isArgument(MI))
1684fc4e42dSDan Gohman       break;
1694fc4e42dSDan Gohman     unsigned Reg = MI.getOperand(0).getReg();
1704fc4e42dSDan Gohman     assert(!MFI.isVRegStackified(Reg));
1714fc4e42dSDan Gohman     Reg2Local[Reg] = MI.getOperand(1).getImm();
1724fc4e42dSDan Gohman     MI.eraseFromParent();
1734fc4e42dSDan Gohman     Changed = true;
1744fc4e42dSDan Gohman   }
1754fc4e42dSDan Gohman 
1764fc4e42dSDan Gohman   // Start assigning local numbers after the last parameter.
1774fc4e42dSDan Gohman   unsigned CurLocal = MFI.getParams().size();
1784fc4e42dSDan Gohman 
1794fc4e42dSDan Gohman   // Visit each instruction in the function.
1804fc4e42dSDan Gohman   for (MachineBasicBlock &MBB : MF) {
1814fc4e42dSDan Gohman     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) {
1824fc4e42dSDan Gohman       MachineInstr &MI = *I++;
1834fc4e42dSDan Gohman       assert(!WebAssembly::isArgument(MI));
1844fc4e42dSDan Gohman 
1854fc4e42dSDan Gohman       if (MI.isDebugValue() || MI.isLabel())
1864fc4e42dSDan Gohman         continue;
1874fc4e42dSDan Gohman 
1884fc4e42dSDan Gohman       // Replace tee instructions with tee_local. The difference is that tee
1894fc4e42dSDan Gohman       // instructins have two defs, while tee_local instructions have one def
1904fc4e42dSDan Gohman       // and an index of a local to write to.
1914fc4e42dSDan Gohman       if (WebAssembly::isTee(MI)) {
1924fc4e42dSDan Gohman         assert(MFI.isVRegStackified(MI.getOperand(0).getReg()));
1934fc4e42dSDan Gohman         assert(!MFI.isVRegStackified(MI.getOperand(1).getReg()));
1944fc4e42dSDan Gohman         unsigned OldReg = MI.getOperand(2).getReg();
1954fc4e42dSDan Gohman         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
1964fc4e42dSDan Gohman 
1974fc4e42dSDan Gohman         // Stackify the input if it isn't stackified yet.
1984fc4e42dSDan Gohman         if (!MFI.isVRegStackified(OldReg)) {
1994fc4e42dSDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2004fc4e42dSDan Gohman           unsigned NewReg = MRI.createVirtualRegister(RC);
2014fc4e42dSDan Gohman           unsigned Opc = getGetLocalOpcode(RC);
2024fc4e42dSDan Gohman           BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc), NewReg)
2034fc4e42dSDan Gohman               .addImm(LocalId);
2044fc4e42dSDan Gohman           MI.getOperand(2).setReg(NewReg);
2054fc4e42dSDan Gohman           MFI.stackifyVReg(NewReg);
2064fc4e42dSDan Gohman         }
2074fc4e42dSDan Gohman 
2084fc4e42dSDan Gohman         // Replace the TEE with a TEE_LOCAL.
2094fc4e42dSDan Gohman         unsigned LocalId =
2104fc4e42dSDan Gohman             getLocalId(Reg2Local, CurLocal, MI.getOperand(1).getReg());
2114fc4e42dSDan Gohman         unsigned Opc = getTeeLocalOpcode(RC);
2124fc4e42dSDan Gohman         BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc),
2134fc4e42dSDan Gohman                 MI.getOperand(0).getReg())
2144fc4e42dSDan Gohman             .addImm(LocalId)
2154fc4e42dSDan Gohman             .addReg(MI.getOperand(2).getReg());
2164fc4e42dSDan Gohman 
2174fc4e42dSDan Gohman         MI.eraseFromParent();
2184fc4e42dSDan Gohman         Changed = true;
2194fc4e42dSDan Gohman         continue;
2204fc4e42dSDan Gohman       }
2214fc4e42dSDan Gohman 
2224fc4e42dSDan Gohman       // Insert set_locals for any defs that aren't stackified yet. Currently
2234fc4e42dSDan Gohman       // we handle at most one def.
2244fc4e42dSDan Gohman       assert(MI.getDesc().getNumDefs() <= 1);
2254fc4e42dSDan Gohman       if (MI.getDesc().getNumDefs() == 1) {
2264fc4e42dSDan Gohman         unsigned OldReg = MI.getOperand(0).getReg();
2274fc4e42dSDan Gohman         if (!MFI.isVRegStackified(OldReg) && !MRI.use_empty(OldReg)) {
2284fc4e42dSDan Gohman           unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2294fc4e42dSDan Gohman           const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2304fc4e42dSDan Gohman           unsigned NewReg = MRI.createVirtualRegister(RC);
2314fc4e42dSDan Gohman           auto InsertPt = std::next(MachineBasicBlock::iterator(&MI));
2324fc4e42dSDan Gohman           unsigned Opc = getSetLocalOpcode(RC);
2334fc4e42dSDan Gohman           BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc))
2344fc4e42dSDan Gohman               .addImm(LocalId)
2354fc4e42dSDan Gohman               .addReg(NewReg);
2364fc4e42dSDan Gohman           MI.getOperand(0).setReg(NewReg);
2374fc4e42dSDan Gohman           MFI.stackifyVReg(NewReg);
2384fc4e42dSDan Gohman           Changed = true;
2394fc4e42dSDan Gohman         }
2404fc4e42dSDan Gohman       }
2414fc4e42dSDan Gohman 
2424fc4e42dSDan Gohman       // Insert get_locals for any uses that aren't stackified yet.
2434fc4e42dSDan Gohman       MachineInstr *InsertPt = &MI;
2444fc4e42dSDan Gohman       for (MachineOperand &MO : reverse(MI.explicit_uses())) {
2454fc4e42dSDan Gohman         if (!MO.isReg())
2464fc4e42dSDan Gohman           continue;
2474fc4e42dSDan Gohman 
2484fc4e42dSDan Gohman         unsigned OldReg = MO.getReg();
2494fc4e42dSDan Gohman 
2504fc4e42dSDan Gohman         // If we see a stackified register, prepare to insert subsequent
2514fc4e42dSDan Gohman         // get_locals before the start of its tree.
2524fc4e42dSDan Gohman         if (MFI.isVRegStackified(OldReg)) {
2534fc4e42dSDan Gohman           InsertPt = FindStartOfTree(MO, MRI, MFI);
2544fc4e42dSDan Gohman           continue;
2554fc4e42dSDan Gohman         }
2564fc4e42dSDan Gohman 
2574fc4e42dSDan Gohman         // Insert a get_local.
2584fc4e42dSDan Gohman         unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg);
2594fc4e42dSDan Gohman         const TargetRegisterClass *RC = MRI.getRegClass(OldReg);
2604fc4e42dSDan Gohman         unsigned NewReg = MRI.createVirtualRegister(RC);
2614fc4e42dSDan Gohman         unsigned Opc = getGetLocalOpcode(RC);
2624fc4e42dSDan Gohman         InsertPt =
2634fc4e42dSDan Gohman             BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc), NewReg)
2644fc4e42dSDan Gohman                 .addImm(LocalId);
2654fc4e42dSDan Gohman         MO.setReg(NewReg);
2664fc4e42dSDan Gohman         MFI.stackifyVReg(NewReg);
2674fc4e42dSDan Gohman         Changed = true;
2684fc4e42dSDan Gohman       }
2694fc4e42dSDan Gohman 
2704fc4e42dSDan Gohman       // Coalesce and eliminate COPY instructions.
2714fc4e42dSDan Gohman       if (WebAssembly::isCopy(MI)) {
2724fc4e42dSDan Gohman         MRI.replaceRegWith(MI.getOperand(1).getReg(),
2734fc4e42dSDan Gohman                            MI.getOperand(0).getReg());
2744fc4e42dSDan Gohman         MI.eraseFromParent();
2754fc4e42dSDan Gohman         Changed = true;
2764fc4e42dSDan Gohman       }
2774fc4e42dSDan Gohman     }
2784fc4e42dSDan Gohman   }
2794fc4e42dSDan Gohman 
2803acb187dSDan Gohman   // Define the locals.
2814fc4e42dSDan Gohman   for (size_t i = 0, e = MRI.getNumVirtRegs(); i < e; ++i) {
2824fc4e42dSDan Gohman     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
2834fc4e42dSDan Gohman     auto I = Reg2Local.find(Reg);
2844fc4e42dSDan Gohman     if (I == Reg2Local.end() || I->second < MFI.getParams().size())
2854fc4e42dSDan Gohman       continue;
2864fc4e42dSDan Gohman 
2873acb187dSDan Gohman     MFI.addLocal(typeForRegClass(MRI.getRegClass(Reg)));
2884fc4e42dSDan Gohman     Changed = true;
2894fc4e42dSDan Gohman   }
2904fc4e42dSDan Gohman 
2914fc4e42dSDan Gohman #ifndef NDEBUG
2924fc4e42dSDan Gohman   // Assert that all registers have been stackified at this point.
2934fc4e42dSDan Gohman   for (const MachineBasicBlock &MBB : MF) {
2944fc4e42dSDan Gohman     for (const MachineInstr &MI : MBB) {
2954fc4e42dSDan Gohman       if (MI.isDebugValue() || MI.isLabel())
2964fc4e42dSDan Gohman         continue;
2974fc4e42dSDan Gohman       for (const MachineOperand &MO : MI.explicit_operands()) {
2984fc4e42dSDan Gohman         assert(
2994fc4e42dSDan Gohman             (!MO.isReg() || MRI.use_empty(MO.getReg()) ||
3004fc4e42dSDan Gohman              MFI.isVRegStackified(MO.getReg())) &&
3014fc4e42dSDan Gohman             "WebAssemblyExplicitLocals failed to stackify a register operand");
3024fc4e42dSDan Gohman       }
3034fc4e42dSDan Gohman     }
3044fc4e42dSDan Gohman   }
3054fc4e42dSDan Gohman #endif
3064fc4e42dSDan Gohman 
3074fc4e42dSDan Gohman   return Changed;
3084fc4e42dSDan Gohman }
309