xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- WebAssemblyRegStackify.cpp - Register Stackification --------------===//
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 implements a register stacking pass.
110b57cec5SDimitry Andric ///
120b57cec5SDimitry Andric /// This pass reorders instructions to put register uses and defs in an order
130b57cec5SDimitry Andric /// such that they form single-use expression trees. Registers fitting this form
140b57cec5SDimitry Andric /// are then marked as "stackified", meaning references to them are replaced by
150b57cec5SDimitry Andric /// "push" and "pop" from the value stack.
160b57cec5SDimitry Andric ///
170b57cec5SDimitry Andric /// This is primarily a code size optimization, since temporary values on the
180b57cec5SDimitry Andric /// value stack don't need to be named.
190b57cec5SDimitry Andric ///
200b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
230b57cec5SDimitry Andric #include "WebAssembly.h"
240b57cec5SDimitry Andric #include "WebAssemblyDebugValueManager.h"
250b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
260b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
270b57cec5SDimitry Andric #include "WebAssemblyUtilities.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
290b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
370b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
390b57cec5SDimitry Andric using namespace llvm;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-reg-stackify"
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric namespace {
440b57cec5SDimitry Andric class WebAssemblyRegStackify final : public MachineFunctionPass {
450b57cec5SDimitry Andric   StringRef getPassName() const override {
460b57cec5SDimitry Andric     return "WebAssembly Register Stackify";
470b57cec5SDimitry Andric   }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
500b57cec5SDimitry Andric     AU.setPreservesCFG();
510b57cec5SDimitry Andric     AU.addRequired<AAResultsWrapperPass>();
520b57cec5SDimitry Andric     AU.addRequired<MachineDominatorTree>();
530b57cec5SDimitry Andric     AU.addRequired<LiveIntervals>();
540b57cec5SDimitry Andric     AU.addPreserved<MachineBlockFrequencyInfo>();
550b57cec5SDimitry Andric     AU.addPreserved<SlotIndexes>();
560b57cec5SDimitry Andric     AU.addPreserved<LiveIntervals>();
570b57cec5SDimitry Andric     AU.addPreservedID(LiveVariablesID);
580b57cec5SDimitry Andric     AU.addPreserved<MachineDominatorTree>();
590b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric public:
650b57cec5SDimitry Andric   static char ID; // Pass identification, replacement for typeid
660b57cec5SDimitry Andric   WebAssemblyRegStackify() : MachineFunctionPass(ID) {}
670b57cec5SDimitry Andric };
680b57cec5SDimitry Andric } // end anonymous namespace
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric char WebAssemblyRegStackify::ID = 0;
710b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyRegStackify, DEBUG_TYPE,
720b57cec5SDimitry Andric                 "Reorder instructions to use the WebAssembly value stack",
730b57cec5SDimitry Andric                 false, false)
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyRegStackify() {
760b57cec5SDimitry Andric   return new WebAssemblyRegStackify();
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric // Decorate the given instruction with implicit operands that enforce the
800b57cec5SDimitry Andric // expression stack ordering constraints for an instruction which is on
810b57cec5SDimitry Andric // the expression stack.
820b57cec5SDimitry Andric static void imposeStackOrdering(MachineInstr *MI) {
830b57cec5SDimitry Andric   // Write the opaque VALUE_STACK register.
840b57cec5SDimitry Andric   if (!MI->definesRegister(WebAssembly::VALUE_STACK))
850b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK,
860b57cec5SDimitry Andric                                              /*isDef=*/true,
870b57cec5SDimitry Andric                                              /*isImp=*/true));
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   // Also read the opaque VALUE_STACK register.
900b57cec5SDimitry Andric   if (!MI->readsRegister(WebAssembly::VALUE_STACK))
910b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK,
920b57cec5SDimitry Andric                                              /*isDef=*/false,
930b57cec5SDimitry Andric                                              /*isImp=*/true));
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric // Convert an IMPLICIT_DEF instruction into an instruction which defines
970b57cec5SDimitry Andric // a constant zero value.
980b57cec5SDimitry Andric static void convertImplicitDefToConstZero(MachineInstr *MI,
990b57cec5SDimitry Andric                                           MachineRegisterInfo &MRI,
1000b57cec5SDimitry Andric                                           const TargetInstrInfo *TII,
1010b57cec5SDimitry Andric                                           MachineFunction &MF,
1020b57cec5SDimitry Andric                                           LiveIntervals &LIS) {
1030b57cec5SDimitry Andric   assert(MI->getOpcode() == TargetOpcode::IMPLICIT_DEF);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   const auto *RegClass = MRI.getRegClass(MI->getOperand(0).getReg());
1060b57cec5SDimitry Andric   if (RegClass == &WebAssembly::I32RegClass) {
1070b57cec5SDimitry Andric     MI->setDesc(TII->get(WebAssembly::CONST_I32));
1080b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateImm(0));
1090b57cec5SDimitry Andric   } else if (RegClass == &WebAssembly::I64RegClass) {
1100b57cec5SDimitry Andric     MI->setDesc(TII->get(WebAssembly::CONST_I64));
1110b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateImm(0));
1120b57cec5SDimitry Andric   } else if (RegClass == &WebAssembly::F32RegClass) {
1130b57cec5SDimitry Andric     MI->setDesc(TII->get(WebAssembly::CONST_F32));
1140b57cec5SDimitry Andric     auto *Val = cast<ConstantFP>(Constant::getNullValue(
1150b57cec5SDimitry Andric         Type::getFloatTy(MF.getFunction().getContext())));
1160b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateFPImm(Val));
1170b57cec5SDimitry Andric   } else if (RegClass == &WebAssembly::F64RegClass) {
1180b57cec5SDimitry Andric     MI->setDesc(TII->get(WebAssembly::CONST_F64));
1190b57cec5SDimitry Andric     auto *Val = cast<ConstantFP>(Constant::getNullValue(
1200b57cec5SDimitry Andric         Type::getDoubleTy(MF.getFunction().getContext())));
1210b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateFPImm(Val));
1220b57cec5SDimitry Andric   } else if (RegClass == &WebAssembly::V128RegClass) {
123*8bcb0991SDimitry Andric     Register TempReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
1240b57cec5SDimitry Andric     MI->setDesc(TII->get(WebAssembly::SPLAT_v4i32));
1250b57cec5SDimitry Andric     MI->addOperand(MachineOperand::CreateReg(TempReg, false));
1260b57cec5SDimitry Andric     MachineInstr *Const = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1270b57cec5SDimitry Andric                                   TII->get(WebAssembly::CONST_I32), TempReg)
1280b57cec5SDimitry Andric                               .addImm(0);
1290b57cec5SDimitry Andric     LIS.InsertMachineInstrInMaps(*Const);
1300b57cec5SDimitry Andric   } else {
1310b57cec5SDimitry Andric     llvm_unreachable("Unexpected reg class");
1320b57cec5SDimitry Andric   }
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric // Determine whether a call to the callee referenced by
1360b57cec5SDimitry Andric // MI->getOperand(CalleeOpNo) reads memory, writes memory, and/or has side
1370b57cec5SDimitry Andric // effects.
1380b57cec5SDimitry Andric static void queryCallee(const MachineInstr &MI, unsigned CalleeOpNo, bool &Read,
1390b57cec5SDimitry Andric                         bool &Write, bool &Effects, bool &StackPointer) {
1400b57cec5SDimitry Andric   // All calls can use the stack pointer.
1410b57cec5SDimitry Andric   StackPointer = true;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   const MachineOperand &MO = MI.getOperand(CalleeOpNo);
1440b57cec5SDimitry Andric   if (MO.isGlobal()) {
1450b57cec5SDimitry Andric     const Constant *GV = MO.getGlobal();
1460b57cec5SDimitry Andric     if (const auto *GA = dyn_cast<GlobalAlias>(GV))
1470b57cec5SDimitry Andric       if (!GA->isInterposable())
1480b57cec5SDimitry Andric         GV = GA->getAliasee();
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric     if (const auto *F = dyn_cast<Function>(GV)) {
1510b57cec5SDimitry Andric       if (!F->doesNotThrow())
1520b57cec5SDimitry Andric         Effects = true;
1530b57cec5SDimitry Andric       if (F->doesNotAccessMemory())
1540b57cec5SDimitry Andric         return;
1550b57cec5SDimitry Andric       if (F->onlyReadsMemory()) {
1560b57cec5SDimitry Andric         Read = true;
1570b57cec5SDimitry Andric         return;
1580b57cec5SDimitry Andric       }
1590b57cec5SDimitry Andric     }
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   // Assume the worst.
1630b57cec5SDimitry Andric   Write = true;
1640b57cec5SDimitry Andric   Read = true;
1650b57cec5SDimitry Andric   Effects = true;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric // Determine whether MI reads memory, writes memory, has side effects,
1690b57cec5SDimitry Andric // and/or uses the stack pointer value.
1700b57cec5SDimitry Andric static void query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read,
1710b57cec5SDimitry Andric                   bool &Write, bool &Effects, bool &StackPointer) {
1720b57cec5SDimitry Andric   assert(!MI.isTerminator());
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   if (MI.isDebugInstr() || MI.isPosition())
1750b57cec5SDimitry Andric     return;
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   // Check for loads.
1780b57cec5SDimitry Andric   if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(&AA))
1790b57cec5SDimitry Andric     Read = true;
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // Check for stores.
1820b57cec5SDimitry Andric   if (MI.mayStore()) {
1830b57cec5SDimitry Andric     Write = true;
1840b57cec5SDimitry Andric   } else if (MI.hasOrderedMemoryRef()) {
1850b57cec5SDimitry Andric     switch (MI.getOpcode()) {
1860b57cec5SDimitry Andric     case WebAssembly::DIV_S_I32:
1870b57cec5SDimitry Andric     case WebAssembly::DIV_S_I64:
1880b57cec5SDimitry Andric     case WebAssembly::REM_S_I32:
1890b57cec5SDimitry Andric     case WebAssembly::REM_S_I64:
1900b57cec5SDimitry Andric     case WebAssembly::DIV_U_I32:
1910b57cec5SDimitry Andric     case WebAssembly::DIV_U_I64:
1920b57cec5SDimitry Andric     case WebAssembly::REM_U_I32:
1930b57cec5SDimitry Andric     case WebAssembly::REM_U_I64:
1940b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_S_F32:
1950b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_S_F32:
1960b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_S_F64:
1970b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_S_F64:
1980b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_U_F32:
1990b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_U_F32:
2000b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_U_F64:
2010b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_U_F64:
2020b57cec5SDimitry Andric       // These instruction have hasUnmodeledSideEffects() returning true
2030b57cec5SDimitry Andric       // because they trap on overflow and invalid so they can't be arbitrarily
2040b57cec5SDimitry Andric       // moved, however hasOrderedMemoryRef() interprets this plus their lack
2050b57cec5SDimitry Andric       // of memoperands as having a potential unknown memory reference.
2060b57cec5SDimitry Andric       break;
2070b57cec5SDimitry Andric     default:
2080b57cec5SDimitry Andric       // Record volatile accesses, unless it's a call, as calls are handled
2090b57cec5SDimitry Andric       // specially below.
2100b57cec5SDimitry Andric       if (!MI.isCall()) {
2110b57cec5SDimitry Andric         Write = true;
2120b57cec5SDimitry Andric         Effects = true;
2130b57cec5SDimitry Andric       }
2140b57cec5SDimitry Andric       break;
2150b57cec5SDimitry Andric     }
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   // Check for side effects.
2190b57cec5SDimitry Andric   if (MI.hasUnmodeledSideEffects()) {
2200b57cec5SDimitry Andric     switch (MI.getOpcode()) {
2210b57cec5SDimitry Andric     case WebAssembly::DIV_S_I32:
2220b57cec5SDimitry Andric     case WebAssembly::DIV_S_I64:
2230b57cec5SDimitry Andric     case WebAssembly::REM_S_I32:
2240b57cec5SDimitry Andric     case WebAssembly::REM_S_I64:
2250b57cec5SDimitry Andric     case WebAssembly::DIV_U_I32:
2260b57cec5SDimitry Andric     case WebAssembly::DIV_U_I64:
2270b57cec5SDimitry Andric     case WebAssembly::REM_U_I32:
2280b57cec5SDimitry Andric     case WebAssembly::REM_U_I64:
2290b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_S_F32:
2300b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_S_F32:
2310b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_S_F64:
2320b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_S_F64:
2330b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_U_F32:
2340b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_U_F32:
2350b57cec5SDimitry Andric     case WebAssembly::I32_TRUNC_U_F64:
2360b57cec5SDimitry Andric     case WebAssembly::I64_TRUNC_U_F64:
2370b57cec5SDimitry Andric       // These instructions have hasUnmodeledSideEffects() returning true
2380b57cec5SDimitry Andric       // because they trap on overflow and invalid so they can't be arbitrarily
2390b57cec5SDimitry Andric       // moved, however in the specific case of register stackifying, it is safe
2400b57cec5SDimitry Andric       // to move them because overflow and invalid are Undefined Behavior.
2410b57cec5SDimitry Andric       break;
2420b57cec5SDimitry Andric     default:
2430b57cec5SDimitry Andric       Effects = true;
2440b57cec5SDimitry Andric       break;
2450b57cec5SDimitry Andric     }
2460b57cec5SDimitry Andric   }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   // Check for writes to __stack_pointer global.
2490b57cec5SDimitry Andric   if (MI.getOpcode() == WebAssembly::GLOBAL_SET_I32 &&
2500b57cec5SDimitry Andric       strcmp(MI.getOperand(0).getSymbolName(), "__stack_pointer") == 0)
2510b57cec5SDimitry Andric     StackPointer = true;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   // Analyze calls.
2540b57cec5SDimitry Andric   if (MI.isCall()) {
2550b57cec5SDimitry Andric     unsigned CalleeOpNo = WebAssembly::getCalleeOpNo(MI.getOpcode());
2560b57cec5SDimitry Andric     queryCallee(MI, CalleeOpNo, Read, Write, Effects, StackPointer);
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric // Test whether Def is safe and profitable to rematerialize.
2610b57cec5SDimitry Andric static bool shouldRematerialize(const MachineInstr &Def, AliasAnalysis &AA,
2620b57cec5SDimitry Andric                                 const WebAssemblyInstrInfo *TII) {
2630b57cec5SDimitry Andric   return Def.isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def, &AA);
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric // Identify the definition for this register at this point. This is a
2670b57cec5SDimitry Andric // generalization of MachineRegisterInfo::getUniqueVRegDef that uses
2680b57cec5SDimitry Andric // LiveIntervals to handle complex cases.
2690b57cec5SDimitry Andric static MachineInstr *getVRegDef(unsigned Reg, const MachineInstr *Insert,
2700b57cec5SDimitry Andric                                 const MachineRegisterInfo &MRI,
2710b57cec5SDimitry Andric                                 const LiveIntervals &LIS) {
2720b57cec5SDimitry Andric   // Most registers are in SSA form here so we try a quick MRI query first.
2730b57cec5SDimitry Andric   if (MachineInstr *Def = MRI.getUniqueVRegDef(Reg))
2740b57cec5SDimitry Andric     return Def;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // MRI doesn't know what the Def is. Try asking LIS.
2770b57cec5SDimitry Andric   if (const VNInfo *ValNo = LIS.getInterval(Reg).getVNInfoBefore(
2780b57cec5SDimitry Andric           LIS.getInstructionIndex(*Insert)))
2790b57cec5SDimitry Andric     return LIS.getInstructionFromIndex(ValNo->def);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   return nullptr;
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric // Test whether Reg, as defined at Def, has exactly one use. This is a
2850b57cec5SDimitry Andric // generalization of MachineRegisterInfo::hasOneUse that uses LiveIntervals
2860b57cec5SDimitry Andric // to handle complex cases.
2870b57cec5SDimitry Andric static bool hasOneUse(unsigned Reg, MachineInstr *Def, MachineRegisterInfo &MRI,
2880b57cec5SDimitry Andric                       MachineDominatorTree &MDT, LiveIntervals &LIS) {
2890b57cec5SDimitry Andric   // Most registers are in SSA form here so we try a quick MRI query first.
2900b57cec5SDimitry Andric   if (MRI.hasOneUse(Reg))
2910b57cec5SDimitry Andric     return true;
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   bool HasOne = false;
2940b57cec5SDimitry Andric   const LiveInterval &LI = LIS.getInterval(Reg);
2950b57cec5SDimitry Andric   const VNInfo *DefVNI =
2960b57cec5SDimitry Andric       LI.getVNInfoAt(LIS.getInstructionIndex(*Def).getRegSlot());
2970b57cec5SDimitry Andric   assert(DefVNI);
2980b57cec5SDimitry Andric   for (auto &I : MRI.use_nodbg_operands(Reg)) {
2990b57cec5SDimitry Andric     const auto &Result = LI.Query(LIS.getInstructionIndex(*I.getParent()));
3000b57cec5SDimitry Andric     if (Result.valueIn() == DefVNI) {
3010b57cec5SDimitry Andric       if (!Result.isKill())
3020b57cec5SDimitry Andric         return false;
3030b57cec5SDimitry Andric       if (HasOne)
3040b57cec5SDimitry Andric         return false;
3050b57cec5SDimitry Andric       HasOne = true;
3060b57cec5SDimitry Andric     }
3070b57cec5SDimitry Andric   }
3080b57cec5SDimitry Andric   return HasOne;
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric // Test whether it's safe to move Def to just before Insert.
3120b57cec5SDimitry Andric // TODO: Compute memory dependencies in a way that doesn't require always
3130b57cec5SDimitry Andric // walking the block.
3140b57cec5SDimitry Andric // TODO: Compute memory dependencies in a way that uses AliasAnalysis to be
3150b57cec5SDimitry Andric // more precise.
3160b57cec5SDimitry Andric static bool isSafeToMove(const MachineInstr *Def, const MachineInstr *Insert,
3170b57cec5SDimitry Andric                          AliasAnalysis &AA, const MachineRegisterInfo &MRI) {
3180b57cec5SDimitry Andric   assert(Def->getParent() == Insert->getParent());
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   // 'catch' and 'extract_exception' should be the first instruction of a BB and
3210b57cec5SDimitry Andric   // cannot move.
3220b57cec5SDimitry Andric   if (Def->getOpcode() == WebAssembly::CATCH ||
3230b57cec5SDimitry Andric       Def->getOpcode() == WebAssembly::EXTRACT_EXCEPTION_I32) {
3240b57cec5SDimitry Andric     const MachineBasicBlock *MBB = Def->getParent();
3250b57cec5SDimitry Andric     auto NextI = std::next(MachineBasicBlock::const_iterator(Def));
3260b57cec5SDimitry Andric     for (auto E = MBB->end(); NextI != E && NextI->isDebugInstr(); ++NextI)
3270b57cec5SDimitry Andric       ;
3280b57cec5SDimitry Andric     if (NextI != Insert)
3290b57cec5SDimitry Andric       return false;
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   // Check for register dependencies.
3330b57cec5SDimitry Andric   SmallVector<unsigned, 4> MutableRegisters;
3340b57cec5SDimitry Andric   for (const MachineOperand &MO : Def->operands()) {
3350b57cec5SDimitry Andric     if (!MO.isReg() || MO.isUndef())
3360b57cec5SDimitry Andric       continue;
337*8bcb0991SDimitry Andric     Register Reg = MO.getReg();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric     // If the register is dead here and at Insert, ignore it.
3400b57cec5SDimitry Andric     if (MO.isDead() && Insert->definesRegister(Reg) &&
3410b57cec5SDimitry Andric         !Insert->readsRegister(Reg))
3420b57cec5SDimitry Andric       continue;
3430b57cec5SDimitry Andric 
344*8bcb0991SDimitry Andric     if (Register::isPhysicalRegister(Reg)) {
3450b57cec5SDimitry Andric       // Ignore ARGUMENTS; it's just used to keep the ARGUMENT_* instructions
3460b57cec5SDimitry Andric       // from moving down, and we've already checked for that.
3470b57cec5SDimitry Andric       if (Reg == WebAssembly::ARGUMENTS)
3480b57cec5SDimitry Andric         continue;
3490b57cec5SDimitry Andric       // If the physical register is never modified, ignore it.
3500b57cec5SDimitry Andric       if (!MRI.isPhysRegModified(Reg))
3510b57cec5SDimitry Andric         continue;
3520b57cec5SDimitry Andric       // Otherwise, it's a physical register with unknown liveness.
3530b57cec5SDimitry Andric       return false;
3540b57cec5SDimitry Andric     }
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric     // If one of the operands isn't in SSA form, it has different values at
3570b57cec5SDimitry Andric     // different times, and we need to make sure we don't move our use across
3580b57cec5SDimitry Andric     // a different def.
3590b57cec5SDimitry Andric     if (!MO.isDef() && !MRI.hasOneDef(Reg))
3600b57cec5SDimitry Andric       MutableRegisters.push_back(Reg);
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   bool Read = false, Write = false, Effects = false, StackPointer = false;
3640b57cec5SDimitry Andric   query(*Def, AA, Read, Write, Effects, StackPointer);
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   // If the instruction does not access memory and has no side effects, it has
3670b57cec5SDimitry Andric   // no additional dependencies.
3680b57cec5SDimitry Andric   bool HasMutableRegisters = !MutableRegisters.empty();
3690b57cec5SDimitry Andric   if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters)
3700b57cec5SDimitry Andric     return true;
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   // Scan through the intervening instructions between Def and Insert.
3730b57cec5SDimitry Andric   MachineBasicBlock::const_iterator D(Def), I(Insert);
3740b57cec5SDimitry Andric   for (--I; I != D; --I) {
3750b57cec5SDimitry Andric     bool InterveningRead = false;
3760b57cec5SDimitry Andric     bool InterveningWrite = false;
3770b57cec5SDimitry Andric     bool InterveningEffects = false;
3780b57cec5SDimitry Andric     bool InterveningStackPointer = false;
3790b57cec5SDimitry Andric     query(*I, AA, InterveningRead, InterveningWrite, InterveningEffects,
3800b57cec5SDimitry Andric           InterveningStackPointer);
3810b57cec5SDimitry Andric     if (Effects && InterveningEffects)
3820b57cec5SDimitry Andric       return false;
3830b57cec5SDimitry Andric     if (Read && InterveningWrite)
3840b57cec5SDimitry Andric       return false;
3850b57cec5SDimitry Andric     if (Write && (InterveningRead || InterveningWrite))
3860b57cec5SDimitry Andric       return false;
3870b57cec5SDimitry Andric     if (StackPointer && InterveningStackPointer)
3880b57cec5SDimitry Andric       return false;
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric     for (unsigned Reg : MutableRegisters)
3910b57cec5SDimitry Andric       for (const MachineOperand &MO : I->operands())
3920b57cec5SDimitry Andric         if (MO.isReg() && MO.isDef() && MO.getReg() == Reg)
3930b57cec5SDimitry Andric           return false;
3940b57cec5SDimitry Andric   }
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   return true;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric /// Test whether OneUse, a use of Reg, dominates all of Reg's other uses.
4000b57cec5SDimitry Andric static bool oneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse,
4010b57cec5SDimitry Andric                                      const MachineBasicBlock &MBB,
4020b57cec5SDimitry Andric                                      const MachineRegisterInfo &MRI,
4030b57cec5SDimitry Andric                                      const MachineDominatorTree &MDT,
4040b57cec5SDimitry Andric                                      LiveIntervals &LIS,
4050b57cec5SDimitry Andric                                      WebAssemblyFunctionInfo &MFI) {
4060b57cec5SDimitry Andric   const LiveInterval &LI = LIS.getInterval(Reg);
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   const MachineInstr *OneUseInst = OneUse.getParent();
4090b57cec5SDimitry Andric   VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst));
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   for (const MachineOperand &Use : MRI.use_nodbg_operands(Reg)) {
4120b57cec5SDimitry Andric     if (&Use == &OneUse)
4130b57cec5SDimitry Andric       continue;
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric     const MachineInstr *UseInst = Use.getParent();
4160b57cec5SDimitry Andric     VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst));
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric     if (UseVNI != OneUseVNI)
4190b57cec5SDimitry Andric       continue;
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric     if (UseInst == OneUseInst) {
4220b57cec5SDimitry Andric       // Another use in the same instruction. We need to ensure that the one
4230b57cec5SDimitry Andric       // selected use happens "before" it.
4240b57cec5SDimitry Andric       if (&OneUse > &Use)
4250b57cec5SDimitry Andric         return false;
4260b57cec5SDimitry Andric     } else {
4270b57cec5SDimitry Andric       // Test that the use is dominated by the one selected use.
4280b57cec5SDimitry Andric       while (!MDT.dominates(OneUseInst, UseInst)) {
4290b57cec5SDimitry Andric         // Actually, dominating is over-conservative. Test that the use would
4300b57cec5SDimitry Andric         // happen after the one selected use in the stack evaluation order.
4310b57cec5SDimitry Andric         //
4320b57cec5SDimitry Andric         // This is needed as a consequence of using implicit local.gets for
4330b57cec5SDimitry Andric         // uses and implicit local.sets for defs.
4340b57cec5SDimitry Andric         if (UseInst->getDesc().getNumDefs() == 0)
4350b57cec5SDimitry Andric           return false;
4360b57cec5SDimitry Andric         const MachineOperand &MO = UseInst->getOperand(0);
4370b57cec5SDimitry Andric         if (!MO.isReg())
4380b57cec5SDimitry Andric           return false;
439*8bcb0991SDimitry Andric         Register DefReg = MO.getReg();
440*8bcb0991SDimitry Andric         if (!Register::isVirtualRegister(DefReg) ||
4410b57cec5SDimitry Andric             !MFI.isVRegStackified(DefReg))
4420b57cec5SDimitry Andric           return false;
4430b57cec5SDimitry Andric         assert(MRI.hasOneNonDBGUse(DefReg));
4440b57cec5SDimitry Andric         const MachineOperand &NewUse = *MRI.use_nodbg_begin(DefReg);
4450b57cec5SDimitry Andric         const MachineInstr *NewUseInst = NewUse.getParent();
4460b57cec5SDimitry Andric         if (NewUseInst == OneUseInst) {
4470b57cec5SDimitry Andric           if (&OneUse > &NewUse)
4480b57cec5SDimitry Andric             return false;
4490b57cec5SDimitry Andric           break;
4500b57cec5SDimitry Andric         }
4510b57cec5SDimitry Andric         UseInst = NewUseInst;
4520b57cec5SDimitry Andric       }
4530b57cec5SDimitry Andric     }
4540b57cec5SDimitry Andric   }
4550b57cec5SDimitry Andric   return true;
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric /// Get the appropriate tee opcode for the given register class.
4590b57cec5SDimitry Andric static unsigned getTeeOpcode(const TargetRegisterClass *RC) {
4600b57cec5SDimitry Andric   if (RC == &WebAssembly::I32RegClass)
4610b57cec5SDimitry Andric     return WebAssembly::TEE_I32;
4620b57cec5SDimitry Andric   if (RC == &WebAssembly::I64RegClass)
4630b57cec5SDimitry Andric     return WebAssembly::TEE_I64;
4640b57cec5SDimitry Andric   if (RC == &WebAssembly::F32RegClass)
4650b57cec5SDimitry Andric     return WebAssembly::TEE_F32;
4660b57cec5SDimitry Andric   if (RC == &WebAssembly::F64RegClass)
4670b57cec5SDimitry Andric     return WebAssembly::TEE_F64;
4680b57cec5SDimitry Andric   if (RC == &WebAssembly::V128RegClass)
4690b57cec5SDimitry Andric     return WebAssembly::TEE_V128;
4700b57cec5SDimitry Andric   llvm_unreachable("Unexpected register class");
4710b57cec5SDimitry Andric }
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric // Shrink LI to its uses, cleaning up LI.
4740b57cec5SDimitry Andric static void shrinkToUses(LiveInterval &LI, LiveIntervals &LIS) {
4750b57cec5SDimitry Andric   if (LIS.shrinkToUses(&LI)) {
4760b57cec5SDimitry Andric     SmallVector<LiveInterval *, 4> SplitLIs;
4770b57cec5SDimitry Andric     LIS.splitSeparateComponents(LI, SplitLIs);
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric /// A single-use def in the same block with no intervening memory or register
4820b57cec5SDimitry Andric /// dependencies; move the def down and nest it with the current instruction.
4830b57cec5SDimitry Andric static MachineInstr *moveForSingleUse(unsigned Reg, MachineOperand &Op,
4840b57cec5SDimitry Andric                                       MachineInstr *Def, MachineBasicBlock &MBB,
4850b57cec5SDimitry Andric                                       MachineInstr *Insert, LiveIntervals &LIS,
4860b57cec5SDimitry Andric                                       WebAssemblyFunctionInfo &MFI,
4870b57cec5SDimitry Andric                                       MachineRegisterInfo &MRI) {
4880b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Move for single use: "; Def->dump());
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   WebAssemblyDebugValueManager DefDIs(Def);
4910b57cec5SDimitry Andric   MBB.splice(Insert, &MBB, Def);
4920b57cec5SDimitry Andric   DefDIs.move(Insert);
4930b57cec5SDimitry Andric   LIS.handleMove(*Def);
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   if (MRI.hasOneDef(Reg) && MRI.hasOneUse(Reg)) {
4960b57cec5SDimitry Andric     // No one else is using this register for anything so we can just stackify
4970b57cec5SDimitry Andric     // it in place.
4980b57cec5SDimitry Andric     MFI.stackifyVReg(Reg);
4990b57cec5SDimitry Andric   } else {
5000b57cec5SDimitry Andric     // The register may have unrelated uses or defs; create a new register for
5010b57cec5SDimitry Andric     // just our one def and use so that we can stackify it.
502*8bcb0991SDimitry Andric     Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
5030b57cec5SDimitry Andric     Def->getOperand(0).setReg(NewReg);
5040b57cec5SDimitry Andric     Op.setReg(NewReg);
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric     // Tell LiveIntervals about the new register.
5070b57cec5SDimitry Andric     LIS.createAndComputeVirtRegInterval(NewReg);
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric     // Tell LiveIntervals about the changes to the old register.
5100b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
5110b57cec5SDimitry Andric     LI.removeSegment(LIS.getInstructionIndex(*Def).getRegSlot(),
5120b57cec5SDimitry Andric                      LIS.getInstructionIndex(*Op.getParent()).getRegSlot(),
5130b57cec5SDimitry Andric                      /*RemoveDeadValNo=*/true);
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric     MFI.stackifyVReg(NewReg);
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric     DefDIs.updateReg(NewReg);
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump());
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   imposeStackOrdering(Def);
5230b57cec5SDimitry Andric   return Def;
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric /// A trivially cloneable instruction; clone it and nest the new copy with the
5270b57cec5SDimitry Andric /// current instruction.
5280b57cec5SDimitry Andric static MachineInstr *rematerializeCheapDef(
5290b57cec5SDimitry Andric     unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB,
5300b57cec5SDimitry Andric     MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS,
5310b57cec5SDimitry Andric     WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI,
5320b57cec5SDimitry Andric     const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI) {
5330b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump());
5340b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " - for use in "; Op.getParent()->dump());
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   WebAssemblyDebugValueManager DefDIs(&Def);
5370b57cec5SDimitry Andric 
538*8bcb0991SDimitry Andric   Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
5390b57cec5SDimitry Andric   TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI);
5400b57cec5SDimitry Andric   Op.setReg(NewReg);
5410b57cec5SDimitry Andric   MachineInstr *Clone = &*std::prev(Insert);
5420b57cec5SDimitry Andric   LIS.InsertMachineInstrInMaps(*Clone);
5430b57cec5SDimitry Andric   LIS.createAndComputeVirtRegInterval(NewReg);
5440b57cec5SDimitry Andric   MFI.stackifyVReg(NewReg);
5450b57cec5SDimitry Andric   imposeStackOrdering(Clone);
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " - Cloned to "; Clone->dump());
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   // Shrink the interval.
5500b57cec5SDimitry Andric   bool IsDead = MRI.use_empty(Reg);
5510b57cec5SDimitry Andric   if (!IsDead) {
5520b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
5530b57cec5SDimitry Andric     shrinkToUses(LI, LIS);
5540b57cec5SDimitry Andric     IsDead = !LI.liveAt(LIS.getInstructionIndex(Def).getDeadSlot());
5550b57cec5SDimitry Andric   }
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   // If that was the last use of the original, delete the original.
5580b57cec5SDimitry Andric   // Move or clone corresponding DBG_VALUEs to the 'Insert' location.
5590b57cec5SDimitry Andric   if (IsDead) {
5600b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " - Deleting original\n");
5610b57cec5SDimitry Andric     SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot();
5620b57cec5SDimitry Andric     LIS.removePhysRegDefAt(WebAssembly::ARGUMENTS, Idx);
5630b57cec5SDimitry Andric     LIS.removeInterval(Reg);
5640b57cec5SDimitry Andric     LIS.RemoveMachineInstrFromMaps(Def);
5650b57cec5SDimitry Andric     Def.eraseFromParent();
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric     DefDIs.move(&*Insert);
5680b57cec5SDimitry Andric     DefDIs.updateReg(NewReg);
5690b57cec5SDimitry Andric   } else {
5700b57cec5SDimitry Andric     DefDIs.clone(&*Insert, NewReg);
5710b57cec5SDimitry Andric   }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   return Clone;
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric /// A multiple-use def in the same block with no intervening memory or register
5770b57cec5SDimitry Andric /// dependencies; move the def down, nest it with the current instruction, and
5780b57cec5SDimitry Andric /// insert a tee to satisfy the rest of the uses. As an illustration, rewrite
5790b57cec5SDimitry Andric /// this:
5800b57cec5SDimitry Andric ///
5810b57cec5SDimitry Andric ///    Reg = INST ...        // Def
5820b57cec5SDimitry Andric ///    INST ..., Reg, ...    // Insert
5830b57cec5SDimitry Andric ///    INST ..., Reg, ...
5840b57cec5SDimitry Andric ///    INST ..., Reg, ...
5850b57cec5SDimitry Andric ///
5860b57cec5SDimitry Andric /// to this:
5870b57cec5SDimitry Andric ///
5880b57cec5SDimitry Andric ///    DefReg = INST ...     // Def (to become the new Insert)
5890b57cec5SDimitry Andric ///    TeeReg, Reg = TEE_... DefReg
5900b57cec5SDimitry Andric ///    INST ..., TeeReg, ... // Insert
5910b57cec5SDimitry Andric ///    INST ..., Reg, ...
5920b57cec5SDimitry Andric ///    INST ..., Reg, ...
5930b57cec5SDimitry Andric ///
5940b57cec5SDimitry Andric /// with DefReg and TeeReg stackified. This eliminates a local.get from the
5950b57cec5SDimitry Andric /// resulting code.
5960b57cec5SDimitry Andric static MachineInstr *moveAndTeeForMultiUse(
5970b57cec5SDimitry Andric     unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB,
5980b57cec5SDimitry Andric     MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI,
5990b57cec5SDimitry Andric     MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) {
6000b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump());
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric   WebAssemblyDebugValueManager DefDIs(Def);
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric   // Move Def into place.
6050b57cec5SDimitry Andric   MBB.splice(Insert, &MBB, Def);
6060b57cec5SDimitry Andric   LIS.handleMove(*Def);
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   // Create the Tee and attach the registers.
6090b57cec5SDimitry Andric   const auto *RegClass = MRI.getRegClass(Reg);
610*8bcb0991SDimitry Andric   Register TeeReg = MRI.createVirtualRegister(RegClass);
611*8bcb0991SDimitry Andric   Register DefReg = MRI.createVirtualRegister(RegClass);
6120b57cec5SDimitry Andric   MachineOperand &DefMO = Def->getOperand(0);
6130b57cec5SDimitry Andric   MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(),
6140b57cec5SDimitry Andric                               TII->get(getTeeOpcode(RegClass)), TeeReg)
6150b57cec5SDimitry Andric                           .addReg(Reg, RegState::Define)
6160b57cec5SDimitry Andric                           .addReg(DefReg, getUndefRegState(DefMO.isDead()));
6170b57cec5SDimitry Andric   Op.setReg(TeeReg);
6180b57cec5SDimitry Andric   DefMO.setReg(DefReg);
6190b57cec5SDimitry Andric   SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot();
6200b57cec5SDimitry Andric   SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot();
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   DefDIs.move(Insert);
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric   // Tell LiveIntervals we moved the original vreg def from Def to Tee.
6250b57cec5SDimitry Andric   LiveInterval &LI = LIS.getInterval(Reg);
6260b57cec5SDimitry Andric   LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx);
6270b57cec5SDimitry Andric   VNInfo *ValNo = LI.getVNInfoAt(DefIdx);
6280b57cec5SDimitry Andric   I->start = TeeIdx;
6290b57cec5SDimitry Andric   ValNo->def = TeeIdx;
6300b57cec5SDimitry Andric   shrinkToUses(LI, LIS);
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   // Finish stackifying the new regs.
6330b57cec5SDimitry Andric   LIS.createAndComputeVirtRegInterval(TeeReg);
6340b57cec5SDimitry Andric   LIS.createAndComputeVirtRegInterval(DefReg);
6350b57cec5SDimitry Andric   MFI.stackifyVReg(DefReg);
6360b57cec5SDimitry Andric   MFI.stackifyVReg(TeeReg);
6370b57cec5SDimitry Andric   imposeStackOrdering(Def);
6380b57cec5SDimitry Andric   imposeStackOrdering(Tee);
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   DefDIs.clone(Tee, DefReg);
6410b57cec5SDimitry Andric   DefDIs.clone(Insert, TeeReg);
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump());
6440b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " - Tee instruction: "; Tee->dump());
6450b57cec5SDimitry Andric   return Def;
6460b57cec5SDimitry Andric }
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric namespace {
6490b57cec5SDimitry Andric /// A stack for walking the tree of instructions being built, visiting the
6500b57cec5SDimitry Andric /// MachineOperands in DFS order.
6510b57cec5SDimitry Andric class TreeWalkerState {
6520b57cec5SDimitry Andric   using mop_iterator = MachineInstr::mop_iterator;
6530b57cec5SDimitry Andric   using mop_reverse_iterator = std::reverse_iterator<mop_iterator>;
6540b57cec5SDimitry Andric   using RangeTy = iterator_range<mop_reverse_iterator>;
6550b57cec5SDimitry Andric   SmallVector<RangeTy, 4> Worklist;
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric public:
6580b57cec5SDimitry Andric   explicit TreeWalkerState(MachineInstr *Insert) {
6590b57cec5SDimitry Andric     const iterator_range<mop_iterator> &Range = Insert->explicit_uses();
6600b57cec5SDimitry Andric     if (Range.begin() != Range.end())
6610b57cec5SDimitry Andric       Worklist.push_back(reverse(Range));
6620b57cec5SDimitry Andric   }
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric   bool done() const { return Worklist.empty(); }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric   MachineOperand &pop() {
6670b57cec5SDimitry Andric     RangeTy &Range = Worklist.back();
6680b57cec5SDimitry Andric     MachineOperand &Op = *Range.begin();
6690b57cec5SDimitry Andric     Range = drop_begin(Range, 1);
6700b57cec5SDimitry Andric     if (Range.begin() == Range.end())
6710b57cec5SDimitry Andric       Worklist.pop_back();
6720b57cec5SDimitry Andric     assert((Worklist.empty() ||
6730b57cec5SDimitry Andric             Worklist.back().begin() != Worklist.back().end()) &&
6740b57cec5SDimitry Andric            "Empty ranges shouldn't remain in the worklist");
6750b57cec5SDimitry Andric     return Op;
6760b57cec5SDimitry Andric   }
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   /// Push Instr's operands onto the stack to be visited.
6790b57cec5SDimitry Andric   void pushOperands(MachineInstr *Instr) {
6800b57cec5SDimitry Andric     const iterator_range<mop_iterator> &Range(Instr->explicit_uses());
6810b57cec5SDimitry Andric     if (Range.begin() != Range.end())
6820b57cec5SDimitry Andric       Worklist.push_back(reverse(Range));
6830b57cec5SDimitry Andric   }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   /// Some of Instr's operands are on the top of the stack; remove them and
6860b57cec5SDimitry Andric   /// re-insert them starting from the beginning (because we've commuted them).
6870b57cec5SDimitry Andric   void resetTopOperands(MachineInstr *Instr) {
6880b57cec5SDimitry Andric     assert(hasRemainingOperands(Instr) &&
6890b57cec5SDimitry Andric            "Reseting operands should only be done when the instruction has "
6900b57cec5SDimitry Andric            "an operand still on the stack");
6910b57cec5SDimitry Andric     Worklist.back() = reverse(Instr->explicit_uses());
6920b57cec5SDimitry Andric   }
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   /// Test whether Instr has operands remaining to be visited at the top of
6950b57cec5SDimitry Andric   /// the stack.
6960b57cec5SDimitry Andric   bool hasRemainingOperands(const MachineInstr *Instr) const {
6970b57cec5SDimitry Andric     if (Worklist.empty())
6980b57cec5SDimitry Andric       return false;
6990b57cec5SDimitry Andric     const RangeTy &Range = Worklist.back();
7000b57cec5SDimitry Andric     return Range.begin() != Range.end() && Range.begin()->getParent() == Instr;
7010b57cec5SDimitry Andric   }
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric   /// Test whether the given register is present on the stack, indicating an
7040b57cec5SDimitry Andric   /// operand in the tree that we haven't visited yet. Moving a definition of
7050b57cec5SDimitry Andric   /// Reg to a point in the tree after that would change its value.
7060b57cec5SDimitry Andric   ///
7070b57cec5SDimitry Andric   /// This is needed as a consequence of using implicit local.gets for
7080b57cec5SDimitry Andric   /// uses and implicit local.sets for defs.
7090b57cec5SDimitry Andric   bool isOnStack(unsigned Reg) const {
7100b57cec5SDimitry Andric     for (const RangeTy &Range : Worklist)
7110b57cec5SDimitry Andric       for (const MachineOperand &MO : Range)
7120b57cec5SDimitry Andric         if (MO.isReg() && MO.getReg() == Reg)
7130b57cec5SDimitry Andric           return true;
7140b57cec5SDimitry Andric     return false;
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric };
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric /// State to keep track of whether commuting is in flight or whether it's been
7190b57cec5SDimitry Andric /// tried for the current instruction and didn't work.
7200b57cec5SDimitry Andric class CommutingState {
7210b57cec5SDimitry Andric   /// There are effectively three states: the initial state where we haven't
7220b57cec5SDimitry Andric   /// started commuting anything and we don't know anything yet, the tentative
7230b57cec5SDimitry Andric   /// state where we've commuted the operands of the current instruction and are
7240b57cec5SDimitry Andric   /// revisiting it, and the declined state where we've reverted the operands
7250b57cec5SDimitry Andric   /// back to their original order and will no longer commute it further.
7260b57cec5SDimitry Andric   bool TentativelyCommuting = false;
7270b57cec5SDimitry Andric   bool Declined = false;
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric   /// During the tentative state, these hold the operand indices of the commuted
7300b57cec5SDimitry Andric   /// operands.
7310b57cec5SDimitry Andric   unsigned Operand0, Operand1;
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric public:
7340b57cec5SDimitry Andric   /// Stackification for an operand was not successful due to ordering
7350b57cec5SDimitry Andric   /// constraints. If possible, and if we haven't already tried it and declined
7360b57cec5SDimitry Andric   /// it, commute Insert's operands and prepare to revisit it.
7370b57cec5SDimitry Andric   void maybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker,
7380b57cec5SDimitry Andric                     const WebAssemblyInstrInfo *TII) {
7390b57cec5SDimitry Andric     if (TentativelyCommuting) {
7400b57cec5SDimitry Andric       assert(!Declined &&
7410b57cec5SDimitry Andric              "Don't decline commuting until you've finished trying it");
7420b57cec5SDimitry Andric       // Commuting didn't help. Revert it.
7430b57cec5SDimitry Andric       TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1);
7440b57cec5SDimitry Andric       TentativelyCommuting = false;
7450b57cec5SDimitry Andric       Declined = true;
7460b57cec5SDimitry Andric     } else if (!Declined && TreeWalker.hasRemainingOperands(Insert)) {
7470b57cec5SDimitry Andric       Operand0 = TargetInstrInfo::CommuteAnyOperandIndex;
7480b57cec5SDimitry Andric       Operand1 = TargetInstrInfo::CommuteAnyOperandIndex;
7490b57cec5SDimitry Andric       if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) {
7500b57cec5SDimitry Andric         // Tentatively commute the operands and try again.
7510b57cec5SDimitry Andric         TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1);
7520b57cec5SDimitry Andric         TreeWalker.resetTopOperands(Insert);
7530b57cec5SDimitry Andric         TentativelyCommuting = true;
7540b57cec5SDimitry Andric         Declined = false;
7550b57cec5SDimitry Andric       }
7560b57cec5SDimitry Andric     }
7570b57cec5SDimitry Andric   }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   /// Stackification for some operand was successful. Reset to the default
7600b57cec5SDimitry Andric   /// state.
7610b57cec5SDimitry Andric   void reset() {
7620b57cec5SDimitry Andric     TentativelyCommuting = false;
7630b57cec5SDimitry Andric     Declined = false;
7640b57cec5SDimitry Andric   }
7650b57cec5SDimitry Andric };
7660b57cec5SDimitry Andric } // end anonymous namespace
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
7690b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** Register Stackifying **********\n"
7700b57cec5SDimitry Andric                        "********** Function: "
7710b57cec5SDimitry Andric                     << MF.getName() << '\n');
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric   bool Changed = false;
7740b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
7750b57cec5SDimitry Andric   WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
7760b57cec5SDimitry Andric   const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
7770b57cec5SDimitry Andric   const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
7780b57cec5SDimitry Andric   AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
7790b57cec5SDimitry Andric   auto &MDT = getAnalysis<MachineDominatorTree>();
7800b57cec5SDimitry Andric   auto &LIS = getAnalysis<LiveIntervals>();
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric   // Walk the instructions from the bottom up. Currently we don't look past
7830b57cec5SDimitry Andric   // block boundaries, and the blocks aren't ordered so the block visitation
7840b57cec5SDimitry Andric   // order isn't significant, but we may want to change this in the future.
7850b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
7860b57cec5SDimitry Andric     // Don't use a range-based for loop, because we modify the list as we're
7870b57cec5SDimitry Andric     // iterating over it and the end iterator may change.
7880b57cec5SDimitry Andric     for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
7890b57cec5SDimitry Andric       MachineInstr *Insert = &*MII;
7900b57cec5SDimitry Andric       // Don't nest anything inside an inline asm, because we don't have
7910b57cec5SDimitry Andric       // constraints for $push inputs.
7920b57cec5SDimitry Andric       if (Insert->isInlineAsm())
7930b57cec5SDimitry Andric         continue;
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric       // Ignore debugging intrinsics.
7960b57cec5SDimitry Andric       if (Insert->isDebugValue())
7970b57cec5SDimitry Andric         continue;
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric       // Iterate through the inputs in reverse order, since we'll be pulling
8000b57cec5SDimitry Andric       // operands off the stack in LIFO order.
8010b57cec5SDimitry Andric       CommutingState Commuting;
8020b57cec5SDimitry Andric       TreeWalkerState TreeWalker(Insert);
8030b57cec5SDimitry Andric       while (!TreeWalker.done()) {
8040b57cec5SDimitry Andric         MachineOperand &Op = TreeWalker.pop();
8050b57cec5SDimitry Andric 
8060b57cec5SDimitry Andric         // We're only interested in explicit virtual register operands.
8070b57cec5SDimitry Andric         if (!Op.isReg())
8080b57cec5SDimitry Andric           continue;
8090b57cec5SDimitry Andric 
810*8bcb0991SDimitry Andric         Register Reg = Op.getReg();
8110b57cec5SDimitry Andric         assert(Op.isUse() && "explicit_uses() should only iterate over uses");
8120b57cec5SDimitry Andric         assert(!Op.isImplicit() &&
8130b57cec5SDimitry Andric                "explicit_uses() should only iterate over explicit operands");
814*8bcb0991SDimitry Andric         if (Register::isPhysicalRegister(Reg))
8150b57cec5SDimitry Andric           continue;
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric         // Identify the definition for this register at this point.
8180b57cec5SDimitry Andric         MachineInstr *Def = getVRegDef(Reg, Insert, MRI, LIS);
8190b57cec5SDimitry Andric         if (!Def)
8200b57cec5SDimitry Andric           continue;
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric         // Don't nest an INLINE_ASM def into anything, because we don't have
8230b57cec5SDimitry Andric         // constraints for $pop outputs.
8240b57cec5SDimitry Andric         if (Def->isInlineAsm())
8250b57cec5SDimitry Andric           continue;
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric         // Argument instructions represent live-in registers and not real
8280b57cec5SDimitry Andric         // instructions.
8290b57cec5SDimitry Andric         if (WebAssembly::isArgument(Def->getOpcode()))
8300b57cec5SDimitry Andric           continue;
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric         // Currently catch's return value register cannot be stackified, because
8330b57cec5SDimitry Andric         // the wasm LLVM backend currently does not support live-in values
8340b57cec5SDimitry Andric         // entering blocks, which is a part of multi-value proposal.
8350b57cec5SDimitry Andric         //
8360b57cec5SDimitry Andric         // Once we support live-in values of wasm blocks, this can be:
8370b57cec5SDimitry Andric         // catch                           ; push exnref value onto stack
8380b57cec5SDimitry Andric         // block exnref -> i32
8390b57cec5SDimitry Andric         // br_on_exn $__cpp_exception      ; pop the exnref value
8400b57cec5SDimitry Andric         // end_block
8410b57cec5SDimitry Andric         //
8420b57cec5SDimitry Andric         // But because we don't support it yet, the catch instruction's dst
8430b57cec5SDimitry Andric         // register should be assigned to a local to be propagated across
8440b57cec5SDimitry Andric         // 'block' boundary now.
8450b57cec5SDimitry Andric         //
8460b57cec5SDimitry Andric         // TODO Fix this once we support the multi-value proposal.
8470b57cec5SDimitry Andric         if (Def->getOpcode() == WebAssembly::CATCH)
8480b57cec5SDimitry Andric           continue;
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric         // Decide which strategy to take. Prefer to move a single-use value
8510b57cec5SDimitry Andric         // over cloning it, and prefer cloning over introducing a tee.
8520b57cec5SDimitry Andric         // For moving, we require the def to be in the same block as the use;
8530b57cec5SDimitry Andric         // this makes things simpler (LiveIntervals' handleMove function only
8540b57cec5SDimitry Andric         // supports intra-block moves) and it's MachineSink's job to catch all
8550b57cec5SDimitry Andric         // the sinking opportunities anyway.
8560b57cec5SDimitry Andric         bool SameBlock = Def->getParent() == &MBB;
8570b57cec5SDimitry Andric         bool CanMove = SameBlock && isSafeToMove(Def, Insert, AA, MRI) &&
8580b57cec5SDimitry Andric                        !TreeWalker.isOnStack(Reg);
8590b57cec5SDimitry Andric         if (CanMove && hasOneUse(Reg, Def, MRI, MDT, LIS)) {
8600b57cec5SDimitry Andric           Insert = moveForSingleUse(Reg, Op, Def, MBB, Insert, LIS, MFI, MRI);
8610b57cec5SDimitry Andric         } else if (shouldRematerialize(*Def, AA, TII)) {
8620b57cec5SDimitry Andric           Insert =
8630b57cec5SDimitry Andric               rematerializeCheapDef(Reg, Op, *Def, MBB, Insert->getIterator(),
8640b57cec5SDimitry Andric                                     LIS, MFI, MRI, TII, TRI);
8650b57cec5SDimitry Andric         } else if (CanMove &&
8660b57cec5SDimitry Andric                    oneUseDominatesOtherUses(Reg, Op, MBB, MRI, MDT, LIS, MFI)) {
8670b57cec5SDimitry Andric           Insert = moveAndTeeForMultiUse(Reg, Op, Def, MBB, Insert, LIS, MFI,
8680b57cec5SDimitry Andric                                          MRI, TII);
8690b57cec5SDimitry Andric         } else {
8700b57cec5SDimitry Andric           // We failed to stackify the operand. If the problem was ordering
8710b57cec5SDimitry Andric           // constraints, Commuting may be able to help.
8720b57cec5SDimitry Andric           if (!CanMove && SameBlock)
8730b57cec5SDimitry Andric             Commuting.maybeCommute(Insert, TreeWalker, TII);
8740b57cec5SDimitry Andric           // Proceed to the next operand.
8750b57cec5SDimitry Andric           continue;
8760b57cec5SDimitry Andric         }
8770b57cec5SDimitry Andric 
8780b57cec5SDimitry Andric         // If the instruction we just stackified is an IMPLICIT_DEF, convert it
8790b57cec5SDimitry Andric         // to a constant 0 so that the def is explicit, and the push/pop
8800b57cec5SDimitry Andric         // correspondence is maintained.
8810b57cec5SDimitry Andric         if (Insert->getOpcode() == TargetOpcode::IMPLICIT_DEF)
8820b57cec5SDimitry Andric           convertImplicitDefToConstZero(Insert, MRI, TII, MF, LIS);
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric         // We stackified an operand. Add the defining instruction's operands to
8850b57cec5SDimitry Andric         // the worklist stack now to continue to build an ever deeper tree.
8860b57cec5SDimitry Andric         Commuting.reset();
8870b57cec5SDimitry Andric         TreeWalker.pushOperands(Insert);
8880b57cec5SDimitry Andric       }
8890b57cec5SDimitry Andric 
8900b57cec5SDimitry Andric       // If we stackified any operands, skip over the tree to start looking for
8910b57cec5SDimitry Andric       // the next instruction we can build a tree on.
8920b57cec5SDimitry Andric       if (Insert != &*MII) {
8930b57cec5SDimitry Andric         imposeStackOrdering(&*MII);
8940b57cec5SDimitry Andric         MII = MachineBasicBlock::iterator(Insert).getReverse();
8950b57cec5SDimitry Andric         Changed = true;
8960b57cec5SDimitry Andric       }
8970b57cec5SDimitry Andric     }
8980b57cec5SDimitry Andric   }
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric   // If we used VALUE_STACK anywhere, add it to the live-in sets everywhere so
9010b57cec5SDimitry Andric   // that it never looks like a use-before-def.
9020b57cec5SDimitry Andric   if (Changed) {
9030b57cec5SDimitry Andric     MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK);
9040b57cec5SDimitry Andric     for (MachineBasicBlock &MBB : MF)
9050b57cec5SDimitry Andric       MBB.addLiveIn(WebAssembly::VALUE_STACK);
9060b57cec5SDimitry Andric   }
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric #ifndef NDEBUG
9090b57cec5SDimitry Andric   // Verify that pushes and pops are performed in LIFO order.
9100b57cec5SDimitry Andric   SmallVector<unsigned, 0> Stack;
9110b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
9120b57cec5SDimitry Andric     for (MachineInstr &MI : MBB) {
9130b57cec5SDimitry Andric       if (MI.isDebugInstr())
9140b57cec5SDimitry Andric         continue;
9150b57cec5SDimitry Andric       for (MachineOperand &MO : reverse(MI.explicit_operands())) {
9160b57cec5SDimitry Andric         if (!MO.isReg())
9170b57cec5SDimitry Andric           continue;
918*8bcb0991SDimitry Andric         Register Reg = MO.getReg();
9190b57cec5SDimitry Andric 
9200b57cec5SDimitry Andric         if (MFI.isVRegStackified(Reg)) {
9210b57cec5SDimitry Andric           if (MO.isDef())
9220b57cec5SDimitry Andric             Stack.push_back(Reg);
9230b57cec5SDimitry Andric           else
9240b57cec5SDimitry Andric             assert(Stack.pop_back_val() == Reg &&
9250b57cec5SDimitry Andric                    "Register stack pop should be paired with a push");
9260b57cec5SDimitry Andric         }
9270b57cec5SDimitry Andric       }
9280b57cec5SDimitry Andric     }
9290b57cec5SDimitry Andric     // TODO: Generalize this code to support keeping values on the stack across
9300b57cec5SDimitry Andric     // basic block boundaries.
9310b57cec5SDimitry Andric     assert(Stack.empty() &&
9320b57cec5SDimitry Andric            "Register stack pushes and pops should be balanced");
9330b57cec5SDimitry Andric   }
9340b57cec5SDimitry Andric #endif
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric   return Changed;
9370b57cec5SDimitry Andric }
938