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" 27*5f757f3fSDimitry Andric #include "WebAssemblyUtilities.h" 280b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h" 340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 350b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 360b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 385ffd83dbSDimitry Andric #include <iterator> 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<MachineDominatorTree>(); 520b57cec5SDimitry Andric AU.addRequired<LiveIntervals>(); 530b57cec5SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfo>(); 540b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>(); 550b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>(); 560b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID); 570b57cec5SDimitry Andric AU.addPreserved<MachineDominatorTree>(); 580b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric public: 640b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 650b57cec5SDimitry Andric WebAssemblyRegStackify() : MachineFunctionPass(ID) {} 660b57cec5SDimitry Andric }; 670b57cec5SDimitry Andric } // end anonymous namespace 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric char WebAssemblyRegStackify::ID = 0; 700b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyRegStackify, DEBUG_TYPE, 710b57cec5SDimitry Andric "Reorder instructions to use the WebAssembly value stack", 720b57cec5SDimitry Andric false, false) 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyRegStackify() { 750b57cec5SDimitry Andric return new WebAssemblyRegStackify(); 760b57cec5SDimitry Andric } 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric // Decorate the given instruction with implicit operands that enforce the 790b57cec5SDimitry Andric // expression stack ordering constraints for an instruction which is on 800b57cec5SDimitry Andric // the expression stack. 810b57cec5SDimitry Andric static void imposeStackOrdering(MachineInstr *MI) { 820b57cec5SDimitry Andric // Write the opaque VALUE_STACK register. 830b57cec5SDimitry Andric if (!MI->definesRegister(WebAssembly::VALUE_STACK)) 840b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK, 850b57cec5SDimitry Andric /*isDef=*/true, 860b57cec5SDimitry Andric /*isImp=*/true)); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric // Also read the opaque VALUE_STACK register. 890b57cec5SDimitry Andric if (!MI->readsRegister(WebAssembly::VALUE_STACK)) 900b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK, 910b57cec5SDimitry Andric /*isDef=*/false, 920b57cec5SDimitry Andric /*isImp=*/true)); 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric // Convert an IMPLICIT_DEF instruction into an instruction which defines 960b57cec5SDimitry Andric // a constant zero value. 970b57cec5SDimitry Andric static void convertImplicitDefToConstZero(MachineInstr *MI, 980b57cec5SDimitry Andric MachineRegisterInfo &MRI, 990b57cec5SDimitry Andric const TargetInstrInfo *TII, 1000b57cec5SDimitry Andric MachineFunction &MF, 1010b57cec5SDimitry Andric LiveIntervals &LIS) { 1020b57cec5SDimitry Andric assert(MI->getOpcode() == TargetOpcode::IMPLICIT_DEF); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric const auto *RegClass = MRI.getRegClass(MI->getOperand(0).getReg()); 1050b57cec5SDimitry Andric if (RegClass == &WebAssembly::I32RegClass) { 1060b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_I32)); 1070b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0)); 1080b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::I64RegClass) { 1090b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_I64)); 1100b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0)); 1110b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::F32RegClass) { 1120b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_F32)); 1130b57cec5SDimitry Andric auto *Val = cast<ConstantFP>(Constant::getNullValue( 1140b57cec5SDimitry Andric Type::getFloatTy(MF.getFunction().getContext()))); 1150b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateFPImm(Val)); 1160b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::F64RegClass) { 1170b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_F64)); 1180b57cec5SDimitry Andric auto *Val = cast<ConstantFP>(Constant::getNullValue( 1190b57cec5SDimitry Andric Type::getDoubleTy(MF.getFunction().getContext()))); 1200b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateFPImm(Val)); 1210b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::V128RegClass) { 122fe6060f1SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_V128_I64x2)); 123fe6060f1SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0)); 124fe6060f1SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0)); 1250b57cec5SDimitry Andric } else { 1260b57cec5SDimitry Andric llvm_unreachable("Unexpected reg class"); 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric // Determine whether a call to the callee referenced by 1310b57cec5SDimitry Andric // MI->getOperand(CalleeOpNo) reads memory, writes memory, and/or has side 1320b57cec5SDimitry Andric // effects. 1335ffd83dbSDimitry Andric static void queryCallee(const MachineInstr &MI, bool &Read, bool &Write, 1345ffd83dbSDimitry Andric bool &Effects, bool &StackPointer) { 1350b57cec5SDimitry Andric // All calls can use the stack pointer. 1360b57cec5SDimitry Andric StackPointer = true; 1370b57cec5SDimitry Andric 1385ffd83dbSDimitry Andric const MachineOperand &MO = WebAssembly::getCalleeOp(MI); 1390b57cec5SDimitry Andric if (MO.isGlobal()) { 1400b57cec5SDimitry Andric const Constant *GV = MO.getGlobal(); 1410b57cec5SDimitry Andric if (const auto *GA = dyn_cast<GlobalAlias>(GV)) 1420b57cec5SDimitry Andric if (!GA->isInterposable()) 1430b57cec5SDimitry Andric GV = GA->getAliasee(); 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric if (const auto *F = dyn_cast<Function>(GV)) { 1460b57cec5SDimitry Andric if (!F->doesNotThrow()) 1470b57cec5SDimitry Andric Effects = true; 1480b57cec5SDimitry Andric if (F->doesNotAccessMemory()) 1490b57cec5SDimitry Andric return; 1500b57cec5SDimitry Andric if (F->onlyReadsMemory()) { 1510b57cec5SDimitry Andric Read = true; 1520b57cec5SDimitry Andric return; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric // Assume the worst. 1580b57cec5SDimitry Andric Write = true; 1590b57cec5SDimitry Andric Read = true; 1600b57cec5SDimitry Andric Effects = true; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric // Determine whether MI reads memory, writes memory, has side effects, 1640b57cec5SDimitry Andric // and/or uses the stack pointer value. 165fcaf7f86SDimitry Andric static void query(const MachineInstr &MI, bool &Read, bool &Write, 166fcaf7f86SDimitry Andric bool &Effects, bool &StackPointer) { 1670b57cec5SDimitry Andric assert(!MI.isTerminator()); 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric if (MI.isDebugInstr() || MI.isPosition()) 1700b57cec5SDimitry Andric return; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Check for loads. 173fcaf7f86SDimitry Andric if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad()) 1740b57cec5SDimitry Andric Read = true; 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric // Check for stores. 1770b57cec5SDimitry Andric if (MI.mayStore()) { 1780b57cec5SDimitry Andric Write = true; 1790b57cec5SDimitry Andric } else if (MI.hasOrderedMemoryRef()) { 1800b57cec5SDimitry Andric switch (MI.getOpcode()) { 1810b57cec5SDimitry Andric case WebAssembly::DIV_S_I32: 1820b57cec5SDimitry Andric case WebAssembly::DIV_S_I64: 1830b57cec5SDimitry Andric case WebAssembly::REM_S_I32: 1840b57cec5SDimitry Andric case WebAssembly::REM_S_I64: 1850b57cec5SDimitry Andric case WebAssembly::DIV_U_I32: 1860b57cec5SDimitry Andric case WebAssembly::DIV_U_I64: 1870b57cec5SDimitry Andric case WebAssembly::REM_U_I32: 1880b57cec5SDimitry Andric case WebAssembly::REM_U_I64: 1890b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F32: 1900b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F32: 1910b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F64: 1920b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F64: 1930b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F32: 1940b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F32: 1950b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F64: 1960b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F64: 1970b57cec5SDimitry Andric // These instruction have hasUnmodeledSideEffects() returning true 1980b57cec5SDimitry Andric // because they trap on overflow and invalid so they can't be arbitrarily 1990b57cec5SDimitry Andric // moved, however hasOrderedMemoryRef() interprets this plus their lack 2000b57cec5SDimitry Andric // of memoperands as having a potential unknown memory reference. 2010b57cec5SDimitry Andric break; 2020b57cec5SDimitry Andric default: 2030b57cec5SDimitry Andric // Record volatile accesses, unless it's a call, as calls are handled 2040b57cec5SDimitry Andric // specially below. 2050b57cec5SDimitry Andric if (!MI.isCall()) { 2060b57cec5SDimitry Andric Write = true; 2070b57cec5SDimitry Andric Effects = true; 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric break; 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric // Check for side effects. 2140b57cec5SDimitry Andric if (MI.hasUnmodeledSideEffects()) { 2150b57cec5SDimitry Andric switch (MI.getOpcode()) { 2160b57cec5SDimitry Andric case WebAssembly::DIV_S_I32: 2170b57cec5SDimitry Andric case WebAssembly::DIV_S_I64: 2180b57cec5SDimitry Andric case WebAssembly::REM_S_I32: 2190b57cec5SDimitry Andric case WebAssembly::REM_S_I64: 2200b57cec5SDimitry Andric case WebAssembly::DIV_U_I32: 2210b57cec5SDimitry Andric case WebAssembly::DIV_U_I64: 2220b57cec5SDimitry Andric case WebAssembly::REM_U_I32: 2230b57cec5SDimitry Andric case WebAssembly::REM_U_I64: 2240b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F32: 2250b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F32: 2260b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F64: 2270b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F64: 2280b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F32: 2290b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F32: 2300b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F64: 2310b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F64: 2320b57cec5SDimitry Andric // These instructions have hasUnmodeledSideEffects() returning true 2330b57cec5SDimitry Andric // because they trap on overflow and invalid so they can't be arbitrarily 2340b57cec5SDimitry Andric // moved, however in the specific case of register stackifying, it is safe 2350b57cec5SDimitry Andric // to move them because overflow and invalid are Undefined Behavior. 2360b57cec5SDimitry Andric break; 2370b57cec5SDimitry Andric default: 2380b57cec5SDimitry Andric Effects = true; 2390b57cec5SDimitry Andric break; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // Check for writes to __stack_pointer global. 2445ffd83dbSDimitry Andric if ((MI.getOpcode() == WebAssembly::GLOBAL_SET_I32 || 2455ffd83dbSDimitry Andric MI.getOpcode() == WebAssembly::GLOBAL_SET_I64) && 2460b57cec5SDimitry Andric strcmp(MI.getOperand(0).getSymbolName(), "__stack_pointer") == 0) 2470b57cec5SDimitry Andric StackPointer = true; 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric // Analyze calls. 2500b57cec5SDimitry Andric if (MI.isCall()) { 2515ffd83dbSDimitry Andric queryCallee(MI, Read, Write, Effects, StackPointer); 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric // Test whether Def is safe and profitable to rematerialize. 256fcaf7f86SDimitry Andric static bool shouldRematerialize(const MachineInstr &Def, 2570b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII) { 258fcaf7f86SDimitry Andric return Def.isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def); 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric // Identify the definition for this register at this point. This is a 2620b57cec5SDimitry Andric // generalization of MachineRegisterInfo::getUniqueVRegDef that uses 2630b57cec5SDimitry Andric // LiveIntervals to handle complex cases. 2640b57cec5SDimitry Andric static MachineInstr *getVRegDef(unsigned Reg, const MachineInstr *Insert, 2650b57cec5SDimitry Andric const MachineRegisterInfo &MRI, 2660b57cec5SDimitry Andric const LiveIntervals &LIS) { 2670b57cec5SDimitry Andric // Most registers are in SSA form here so we try a quick MRI query first. 2680b57cec5SDimitry Andric if (MachineInstr *Def = MRI.getUniqueVRegDef(Reg)) 2690b57cec5SDimitry Andric return Def; 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // MRI doesn't know what the Def is. Try asking LIS. 2720b57cec5SDimitry Andric if (const VNInfo *ValNo = LIS.getInterval(Reg).getVNInfoBefore( 2730b57cec5SDimitry Andric LIS.getInstructionIndex(*Insert))) 2740b57cec5SDimitry Andric return LIS.getInstructionFromIndex(ValNo->def); 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric return nullptr; 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric // Test whether Reg, as defined at Def, has exactly one use. This is a 28006c3fb27SDimitry Andric // generalization of MachineRegisterInfo::hasOneNonDBGUse that uses 28106c3fb27SDimitry Andric // LiveIntervals to handle complex cases. 28206c3fb27SDimitry Andric static bool hasOneNonDBGUse(unsigned Reg, MachineInstr *Def, 28306c3fb27SDimitry Andric MachineRegisterInfo &MRI, MachineDominatorTree &MDT, 28406c3fb27SDimitry Andric LiveIntervals &LIS) { 2850b57cec5SDimitry Andric // Most registers are in SSA form here so we try a quick MRI query first. 28606c3fb27SDimitry Andric if (MRI.hasOneNonDBGUse(Reg)) 2870b57cec5SDimitry Andric return true; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric bool HasOne = false; 2900b57cec5SDimitry Andric const LiveInterval &LI = LIS.getInterval(Reg); 2910b57cec5SDimitry Andric const VNInfo *DefVNI = 2920b57cec5SDimitry Andric LI.getVNInfoAt(LIS.getInstructionIndex(*Def).getRegSlot()); 2930b57cec5SDimitry Andric assert(DefVNI); 2940b57cec5SDimitry Andric for (auto &I : MRI.use_nodbg_operands(Reg)) { 2950b57cec5SDimitry Andric const auto &Result = LI.Query(LIS.getInstructionIndex(*I.getParent())); 2960b57cec5SDimitry Andric if (Result.valueIn() == DefVNI) { 2970b57cec5SDimitry Andric if (!Result.isKill()) 2980b57cec5SDimitry Andric return false; 2990b57cec5SDimitry Andric if (HasOne) 3000b57cec5SDimitry Andric return false; 3010b57cec5SDimitry Andric HasOne = true; 3020b57cec5SDimitry Andric } 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric return HasOne; 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric // Test whether it's safe to move Def to just before Insert. 3080b57cec5SDimitry Andric // TODO: Compute memory dependencies in a way that doesn't require always 3090b57cec5SDimitry Andric // walking the block. 3100b57cec5SDimitry Andric // TODO: Compute memory dependencies in a way that uses AliasAnalysis to be 3110b57cec5SDimitry Andric // more precise. 3125ffd83dbSDimitry Andric static bool isSafeToMove(const MachineOperand *Def, const MachineOperand *Use, 313fcaf7f86SDimitry Andric const MachineInstr *Insert, 3145ffd83dbSDimitry Andric const WebAssemblyFunctionInfo &MFI, 3155ffd83dbSDimitry Andric const MachineRegisterInfo &MRI) { 3165ffd83dbSDimitry Andric const MachineInstr *DefI = Def->getParent(); 3175ffd83dbSDimitry Andric const MachineInstr *UseI = Use->getParent(); 3185ffd83dbSDimitry Andric assert(DefI->getParent() == Insert->getParent()); 3195ffd83dbSDimitry Andric assert(UseI->getParent() == Insert->getParent()); 3205ffd83dbSDimitry Andric 3215ffd83dbSDimitry Andric // The first def of a multivalue instruction can be stackified by moving, 3225ffd83dbSDimitry Andric // since the later defs can always be placed into locals if necessary. Later 3235ffd83dbSDimitry Andric // defs can only be stackified if all previous defs are already stackified 3245ffd83dbSDimitry Andric // since ExplicitLocals will not know how to place a def in a local if a 3255ffd83dbSDimitry Andric // subsequent def is stackified. But only one def can be stackified by moving 3265ffd83dbSDimitry Andric // the instruction, so it must be the first one. 3275ffd83dbSDimitry Andric // 3285ffd83dbSDimitry Andric // TODO: This could be loosened to be the first *live* def, but care would 3295ffd83dbSDimitry Andric // have to be taken to ensure the drops of the initial dead defs can be 3305ffd83dbSDimitry Andric // placed. This would require checking that no previous defs are used in the 3315ffd83dbSDimitry Andric // same instruction as subsequent defs. 3325ffd83dbSDimitry Andric if (Def != DefI->defs().begin()) 3335ffd83dbSDimitry Andric return false; 3345ffd83dbSDimitry Andric 3355ffd83dbSDimitry Andric // If any subsequent def is used prior to the current value by the same 3365ffd83dbSDimitry Andric // instruction in which the current value is used, we cannot 3375ffd83dbSDimitry Andric // stackify. Stackifying in this case would require that def moving below the 3385ffd83dbSDimitry Andric // current def in the stack, which cannot be achieved, even with locals. 339bdd1243dSDimitry Andric // Also ensure we don't sink the def past any other prior uses. 340e8d8bef9SDimitry Andric for (const auto &SubsequentDef : drop_begin(DefI->defs())) { 341bdd1243dSDimitry Andric auto I = std::next(MachineBasicBlock::const_iterator(DefI)); 342bdd1243dSDimitry Andric auto E = std::next(MachineBasicBlock::const_iterator(UseI)); 343bdd1243dSDimitry Andric for (; I != E; ++I) { 344bdd1243dSDimitry Andric for (const auto &PriorUse : I->uses()) { 3455ffd83dbSDimitry Andric if (&PriorUse == Use) 3465ffd83dbSDimitry Andric break; 3475ffd83dbSDimitry Andric if (PriorUse.isReg() && SubsequentDef.getReg() == PriorUse.getReg()) 3485ffd83dbSDimitry Andric return false; 3495ffd83dbSDimitry Andric } 3505ffd83dbSDimitry Andric } 351bdd1243dSDimitry Andric } 3525ffd83dbSDimitry Andric 3535ffd83dbSDimitry Andric // If moving is a semantic nop, it is always allowed 3545ffd83dbSDimitry Andric const MachineBasicBlock *MBB = DefI->getParent(); 3555ffd83dbSDimitry Andric auto NextI = std::next(MachineBasicBlock::const_iterator(DefI)); 3565ffd83dbSDimitry Andric for (auto E = MBB->end(); NextI != E && NextI->isDebugInstr(); ++NextI) 3575ffd83dbSDimitry Andric ; 3585ffd83dbSDimitry Andric if (NextI == Insert) 3595ffd83dbSDimitry Andric return true; 3600b57cec5SDimitry Andric 361e8d8bef9SDimitry Andric // 'catch' and 'catch_all' should be the first instruction of a BB and cannot 362e8d8bef9SDimitry Andric // move. 363e8d8bef9SDimitry Andric if (WebAssembly::isCatch(DefI->getOpcode())) 3640b57cec5SDimitry Andric return false; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric // Check for register dependencies. 3670b57cec5SDimitry Andric SmallVector<unsigned, 4> MutableRegisters; 3685ffd83dbSDimitry Andric for (const MachineOperand &MO : DefI->operands()) { 3690b57cec5SDimitry Andric if (!MO.isReg() || MO.isUndef()) 3700b57cec5SDimitry Andric continue; 3718bcb0991SDimitry Andric Register Reg = MO.getReg(); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric // If the register is dead here and at Insert, ignore it. 3740b57cec5SDimitry Andric if (MO.isDead() && Insert->definesRegister(Reg) && 3750b57cec5SDimitry Andric !Insert->readsRegister(Reg)) 3760b57cec5SDimitry Andric continue; 3770b57cec5SDimitry Andric 378bdd1243dSDimitry Andric if (Reg.isPhysical()) { 3790b57cec5SDimitry Andric // Ignore ARGUMENTS; it's just used to keep the ARGUMENT_* instructions 3800b57cec5SDimitry Andric // from moving down, and we've already checked for that. 3810b57cec5SDimitry Andric if (Reg == WebAssembly::ARGUMENTS) 3820b57cec5SDimitry Andric continue; 3830b57cec5SDimitry Andric // If the physical register is never modified, ignore it. 3840b57cec5SDimitry Andric if (!MRI.isPhysRegModified(Reg)) 3850b57cec5SDimitry Andric continue; 3860b57cec5SDimitry Andric // Otherwise, it's a physical register with unknown liveness. 3870b57cec5SDimitry Andric return false; 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric // If one of the operands isn't in SSA form, it has different values at 3910b57cec5SDimitry Andric // different times, and we need to make sure we don't move our use across 3920b57cec5SDimitry Andric // a different def. 3930b57cec5SDimitry Andric if (!MO.isDef() && !MRI.hasOneDef(Reg)) 3940b57cec5SDimitry Andric MutableRegisters.push_back(Reg); 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric bool Read = false, Write = false, Effects = false, StackPointer = false; 398fcaf7f86SDimitry Andric query(*DefI, Read, Write, Effects, StackPointer); 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric // If the instruction does not access memory and has no side effects, it has 4010b57cec5SDimitry Andric // no additional dependencies. 4020b57cec5SDimitry Andric bool HasMutableRegisters = !MutableRegisters.empty(); 4030b57cec5SDimitry Andric if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters) 4040b57cec5SDimitry Andric return true; 4050b57cec5SDimitry Andric 4065ffd83dbSDimitry Andric // Scan through the intervening instructions between DefI and Insert. 4075ffd83dbSDimitry Andric MachineBasicBlock::const_iterator D(DefI), I(Insert); 4080b57cec5SDimitry Andric for (--I; I != D; --I) { 4090b57cec5SDimitry Andric bool InterveningRead = false; 4100b57cec5SDimitry Andric bool InterveningWrite = false; 4110b57cec5SDimitry Andric bool InterveningEffects = false; 4120b57cec5SDimitry Andric bool InterveningStackPointer = false; 413fcaf7f86SDimitry Andric query(*I, InterveningRead, InterveningWrite, InterveningEffects, 4140b57cec5SDimitry Andric InterveningStackPointer); 4150b57cec5SDimitry Andric if (Effects && InterveningEffects) 4160b57cec5SDimitry Andric return false; 4170b57cec5SDimitry Andric if (Read && InterveningWrite) 4180b57cec5SDimitry Andric return false; 4190b57cec5SDimitry Andric if (Write && (InterveningRead || InterveningWrite)) 4200b57cec5SDimitry Andric return false; 4210b57cec5SDimitry Andric if (StackPointer && InterveningStackPointer) 4220b57cec5SDimitry Andric return false; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric for (unsigned Reg : MutableRegisters) 4250b57cec5SDimitry Andric for (const MachineOperand &MO : I->operands()) 4260b57cec5SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) 4270b57cec5SDimitry Andric return false; 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric return true; 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric /// Test whether OneUse, a use of Reg, dominates all of Reg's other uses. 4340b57cec5SDimitry Andric static bool oneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse, 4350b57cec5SDimitry Andric const MachineBasicBlock &MBB, 4360b57cec5SDimitry Andric const MachineRegisterInfo &MRI, 4370b57cec5SDimitry Andric const MachineDominatorTree &MDT, 4380b57cec5SDimitry Andric LiveIntervals &LIS, 4390b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI) { 4400b57cec5SDimitry Andric const LiveInterval &LI = LIS.getInterval(Reg); 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric const MachineInstr *OneUseInst = OneUse.getParent(); 4430b57cec5SDimitry Andric VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst)); 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric for (const MachineOperand &Use : MRI.use_nodbg_operands(Reg)) { 4460b57cec5SDimitry Andric if (&Use == &OneUse) 4470b57cec5SDimitry Andric continue; 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric const MachineInstr *UseInst = Use.getParent(); 4500b57cec5SDimitry Andric VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst)); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric if (UseVNI != OneUseVNI) 4530b57cec5SDimitry Andric continue; 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric if (UseInst == OneUseInst) { 4560b57cec5SDimitry Andric // Another use in the same instruction. We need to ensure that the one 4570b57cec5SDimitry Andric // selected use happens "before" it. 4580b57cec5SDimitry Andric if (&OneUse > &Use) 4590b57cec5SDimitry Andric return false; 4600b57cec5SDimitry Andric } else { 4610b57cec5SDimitry Andric // Test that the use is dominated by the one selected use. 4620b57cec5SDimitry Andric while (!MDT.dominates(OneUseInst, UseInst)) { 4630b57cec5SDimitry Andric // Actually, dominating is over-conservative. Test that the use would 4640b57cec5SDimitry Andric // happen after the one selected use in the stack evaluation order. 4650b57cec5SDimitry Andric // 4660b57cec5SDimitry Andric // This is needed as a consequence of using implicit local.gets for 4670b57cec5SDimitry Andric // uses and implicit local.sets for defs. 4680b57cec5SDimitry Andric if (UseInst->getDesc().getNumDefs() == 0) 4690b57cec5SDimitry Andric return false; 4700b57cec5SDimitry Andric const MachineOperand &MO = UseInst->getOperand(0); 4710b57cec5SDimitry Andric if (!MO.isReg()) 4720b57cec5SDimitry Andric return false; 4738bcb0991SDimitry Andric Register DefReg = MO.getReg(); 474bdd1243dSDimitry Andric if (!DefReg.isVirtual() || !MFI.isVRegStackified(DefReg)) 4750b57cec5SDimitry Andric return false; 4760b57cec5SDimitry Andric assert(MRI.hasOneNonDBGUse(DefReg)); 4770b57cec5SDimitry Andric const MachineOperand &NewUse = *MRI.use_nodbg_begin(DefReg); 4780b57cec5SDimitry Andric const MachineInstr *NewUseInst = NewUse.getParent(); 4790b57cec5SDimitry Andric if (NewUseInst == OneUseInst) { 4800b57cec5SDimitry Andric if (&OneUse > &NewUse) 4810b57cec5SDimitry Andric return false; 4820b57cec5SDimitry Andric break; 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric UseInst = NewUseInst; 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric return true; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric /// Get the appropriate tee opcode for the given register class. 4920b57cec5SDimitry Andric static unsigned getTeeOpcode(const TargetRegisterClass *RC) { 4930b57cec5SDimitry Andric if (RC == &WebAssembly::I32RegClass) 4940b57cec5SDimitry Andric return WebAssembly::TEE_I32; 4950b57cec5SDimitry Andric if (RC == &WebAssembly::I64RegClass) 4960b57cec5SDimitry Andric return WebAssembly::TEE_I64; 4970b57cec5SDimitry Andric if (RC == &WebAssembly::F32RegClass) 4980b57cec5SDimitry Andric return WebAssembly::TEE_F32; 4990b57cec5SDimitry Andric if (RC == &WebAssembly::F64RegClass) 5000b57cec5SDimitry Andric return WebAssembly::TEE_F64; 5010b57cec5SDimitry Andric if (RC == &WebAssembly::V128RegClass) 5020b57cec5SDimitry Andric return WebAssembly::TEE_V128; 503349cc55cSDimitry Andric if (RC == &WebAssembly::EXTERNREFRegClass) 504349cc55cSDimitry Andric return WebAssembly::TEE_EXTERNREF; 505349cc55cSDimitry Andric if (RC == &WebAssembly::FUNCREFRegClass) 506349cc55cSDimitry Andric return WebAssembly::TEE_FUNCREF; 5070b57cec5SDimitry Andric llvm_unreachable("Unexpected register class"); 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric // Shrink LI to its uses, cleaning up LI. 5110b57cec5SDimitry Andric static void shrinkToUses(LiveInterval &LI, LiveIntervals &LIS) { 5120b57cec5SDimitry Andric if (LIS.shrinkToUses(&LI)) { 5130b57cec5SDimitry Andric SmallVector<LiveInterval *, 4> SplitLIs; 5140b57cec5SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs); 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric } 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric /// A single-use def in the same block with no intervening memory or register 5190b57cec5SDimitry Andric /// dependencies; move the def down and nest it with the current instruction. 5200b57cec5SDimitry Andric static MachineInstr *moveForSingleUse(unsigned Reg, MachineOperand &Op, 5210b57cec5SDimitry Andric MachineInstr *Def, MachineBasicBlock &MBB, 5220b57cec5SDimitry Andric MachineInstr *Insert, LiveIntervals &LIS, 5230b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI, 5240b57cec5SDimitry Andric MachineRegisterInfo &MRI) { 5250b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Move for single use: "; Def->dump()); 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(Def); 52806c3fb27SDimitry Andric DefDIs.sink(Insert); 5290b57cec5SDimitry Andric LIS.handleMove(*Def); 5300b57cec5SDimitry Andric 53106c3fb27SDimitry Andric if (MRI.hasOneDef(Reg) && MRI.hasOneNonDBGUse(Reg)) { 5320b57cec5SDimitry Andric // No one else is using this register for anything so we can just stackify 5330b57cec5SDimitry Andric // it in place. 5345ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, Reg); 5350b57cec5SDimitry Andric } else { 5360b57cec5SDimitry Andric // The register may have unrelated uses or defs; create a new register for 5370b57cec5SDimitry Andric // just our one def and use so that we can stackify it. 5388bcb0991SDimitry Andric Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); 5390b57cec5SDimitry Andric Op.setReg(NewReg); 54006c3fb27SDimitry Andric DefDIs.updateReg(NewReg); 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Tell LiveIntervals about the new register. 5430b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(NewReg); 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric // Tell LiveIntervals about the changes to the old register. 5460b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 5470b57cec5SDimitry Andric LI.removeSegment(LIS.getInstructionIndex(*Def).getRegSlot(), 5480b57cec5SDimitry Andric LIS.getInstructionIndex(*Op.getParent()).getRegSlot(), 5490b57cec5SDimitry Andric /*RemoveDeadValNo=*/true); 5500b57cec5SDimitry Andric 5515ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, NewReg); 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric imposeStackOrdering(Def); 5570b57cec5SDimitry Andric return Def; 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 56006c3fb27SDimitry Andric static MachineInstr *getPrevNonDebugInst(MachineInstr *MI) { 56106c3fb27SDimitry Andric for (auto *I = MI->getPrevNode(); I; I = I->getPrevNode()) 56206c3fb27SDimitry Andric if (!I->isDebugInstr()) 56306c3fb27SDimitry Andric return I; 56406c3fb27SDimitry Andric return nullptr; 56506c3fb27SDimitry Andric } 56606c3fb27SDimitry Andric 5670b57cec5SDimitry Andric /// A trivially cloneable instruction; clone it and nest the new copy with the 5680b57cec5SDimitry Andric /// current instruction. 5690b57cec5SDimitry Andric static MachineInstr *rematerializeCheapDef( 5700b57cec5SDimitry Andric unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB, 5710b57cec5SDimitry Andric MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS, 5720b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, 5730b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI) { 5740b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump()); 5750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - for use in "; Op.getParent()->dump()); 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(&Def); 5780b57cec5SDimitry Andric 5798bcb0991SDimitry Andric Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); 58006c3fb27SDimitry Andric DefDIs.cloneSink(&*Insert, NewReg); 5810b57cec5SDimitry Andric Op.setReg(NewReg); 58206c3fb27SDimitry Andric MachineInstr *Clone = getPrevNonDebugInst(&*Insert); 58306c3fb27SDimitry Andric assert(Clone); 5840b57cec5SDimitry Andric LIS.InsertMachineInstrInMaps(*Clone); 5850b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(NewReg); 5865ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, NewReg); 5870b57cec5SDimitry Andric imposeStackOrdering(Clone); 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Cloned to "; Clone->dump()); 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric // Shrink the interval. 5920b57cec5SDimitry Andric bool IsDead = MRI.use_empty(Reg); 5930b57cec5SDimitry Andric if (!IsDead) { 5940b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 5950b57cec5SDimitry Andric shrinkToUses(LI, LIS); 5960b57cec5SDimitry Andric IsDead = !LI.liveAt(LIS.getInstructionIndex(Def).getDeadSlot()); 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric // If that was the last use of the original, delete the original. 6000b57cec5SDimitry Andric if (IsDead) { 6010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Deleting original\n"); 6020b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot(); 603e8d8bef9SDimitry Andric LIS.removePhysRegDefAt(MCRegister::from(WebAssembly::ARGUMENTS), Idx); 6040b57cec5SDimitry Andric LIS.removeInterval(Reg); 6050b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(Def); 60606c3fb27SDimitry Andric DefDIs.removeDef(); 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric return Clone; 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric /// A multiple-use def in the same block with no intervening memory or register 6130b57cec5SDimitry Andric /// dependencies; move the def down, nest it with the current instruction, and 6140b57cec5SDimitry Andric /// insert a tee to satisfy the rest of the uses. As an illustration, rewrite 6150b57cec5SDimitry Andric /// this: 6160b57cec5SDimitry Andric /// 6170b57cec5SDimitry Andric /// Reg = INST ... // Def 6180b57cec5SDimitry Andric /// INST ..., Reg, ... // Insert 6190b57cec5SDimitry Andric /// INST ..., Reg, ... 6200b57cec5SDimitry Andric /// INST ..., Reg, ... 6210b57cec5SDimitry Andric /// 6220b57cec5SDimitry Andric /// to this: 6230b57cec5SDimitry Andric /// 6240b57cec5SDimitry Andric /// DefReg = INST ... // Def (to become the new Insert) 6250b57cec5SDimitry Andric /// TeeReg, Reg = TEE_... DefReg 6260b57cec5SDimitry Andric /// INST ..., TeeReg, ... // Insert 6270b57cec5SDimitry Andric /// INST ..., Reg, ... 6280b57cec5SDimitry Andric /// INST ..., Reg, ... 6290b57cec5SDimitry Andric /// 6300b57cec5SDimitry Andric /// with DefReg and TeeReg stackified. This eliminates a local.get from the 6310b57cec5SDimitry Andric /// resulting code. 6320b57cec5SDimitry Andric static MachineInstr *moveAndTeeForMultiUse( 6330b57cec5SDimitry Andric unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB, 6340b57cec5SDimitry Andric MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, 6350b57cec5SDimitry Andric MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) { 6360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump()); 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric const auto *RegClass = MRI.getRegClass(Reg); 6398bcb0991SDimitry Andric Register TeeReg = MRI.createVirtualRegister(RegClass); 6408bcb0991SDimitry Andric Register DefReg = MRI.createVirtualRegister(RegClass); 64106c3fb27SDimitry Andric 64206c3fb27SDimitry Andric // Move Def into place. 64306c3fb27SDimitry Andric WebAssemblyDebugValueManager DefDIs(Def); 64406c3fb27SDimitry Andric DefDIs.sink(Insert); 64506c3fb27SDimitry Andric LIS.handleMove(*Def); 64606c3fb27SDimitry Andric 64706c3fb27SDimitry Andric // Create the Tee and attach the registers. 6480b57cec5SDimitry Andric MachineOperand &DefMO = Def->getOperand(0); 6490b57cec5SDimitry Andric MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(), 6500b57cec5SDimitry Andric TII->get(getTeeOpcode(RegClass)), TeeReg) 6510b57cec5SDimitry Andric .addReg(Reg, RegState::Define) 6520b57cec5SDimitry Andric .addReg(DefReg, getUndefRegState(DefMO.isDead())); 6530b57cec5SDimitry Andric Op.setReg(TeeReg); 65406c3fb27SDimitry Andric DefDIs.updateReg(DefReg); 6550b57cec5SDimitry Andric SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot(); 6560b57cec5SDimitry Andric SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot(); 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric // Tell LiveIntervals we moved the original vreg def from Def to Tee. 6590b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 6600b57cec5SDimitry Andric LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx); 6610b57cec5SDimitry Andric VNInfo *ValNo = LI.getVNInfoAt(DefIdx); 6620b57cec5SDimitry Andric I->start = TeeIdx; 6630b57cec5SDimitry Andric ValNo->def = TeeIdx; 6640b57cec5SDimitry Andric shrinkToUses(LI, LIS); 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric // Finish stackifying the new regs. 6670b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(TeeReg); 6680b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(DefReg); 6695ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, DefReg); 6705ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, TeeReg); 6710b57cec5SDimitry Andric imposeStackOrdering(Def); 6720b57cec5SDimitry Andric imposeStackOrdering(Tee); 6730b57cec5SDimitry Andric 67406c3fb27SDimitry Andric // Even though 'TeeReg, Reg = TEE ...', has two defs, we don't need to clone 67506c3fb27SDimitry Andric // DBG_VALUEs for both of them, given that the latter will cancel the former 67606c3fb27SDimitry Andric // anyway. Here we only clone DBG_VALUEs for TeeReg, which will be converted 67706c3fb27SDimitry Andric // to a local index in ExplicitLocals pass. 67806c3fb27SDimitry Andric DefDIs.cloneSink(Insert, TeeReg, /* CloneDef */ false); 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); 6810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Tee instruction: "; Tee->dump()); 6820b57cec5SDimitry Andric return Def; 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric namespace { 6860b57cec5SDimitry Andric /// A stack for walking the tree of instructions being built, visiting the 6870b57cec5SDimitry Andric /// MachineOperands in DFS order. 6880b57cec5SDimitry Andric class TreeWalkerState { 6890b57cec5SDimitry Andric using mop_iterator = MachineInstr::mop_iterator; 6900b57cec5SDimitry Andric using mop_reverse_iterator = std::reverse_iterator<mop_iterator>; 6910b57cec5SDimitry Andric using RangeTy = iterator_range<mop_reverse_iterator>; 6920b57cec5SDimitry Andric SmallVector<RangeTy, 4> Worklist; 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric public: 6950b57cec5SDimitry Andric explicit TreeWalkerState(MachineInstr *Insert) { 6960b57cec5SDimitry Andric const iterator_range<mop_iterator> &Range = Insert->explicit_uses(); 697e8d8bef9SDimitry Andric if (!Range.empty()) 6980b57cec5SDimitry Andric Worklist.push_back(reverse(Range)); 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric bool done() const { return Worklist.empty(); } 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric MachineOperand &pop() { 7040b57cec5SDimitry Andric RangeTy &Range = Worklist.back(); 7050b57cec5SDimitry Andric MachineOperand &Op = *Range.begin(); 706e8d8bef9SDimitry Andric Range = drop_begin(Range); 707e8d8bef9SDimitry Andric if (Range.empty()) 7080b57cec5SDimitry Andric Worklist.pop_back(); 709e8d8bef9SDimitry Andric assert((Worklist.empty() || !Worklist.back().empty()) && 7100b57cec5SDimitry Andric "Empty ranges shouldn't remain in the worklist"); 7110b57cec5SDimitry Andric return Op; 7120b57cec5SDimitry Andric } 7130b57cec5SDimitry Andric 7140b57cec5SDimitry Andric /// Push Instr's operands onto the stack to be visited. 7150b57cec5SDimitry Andric void pushOperands(MachineInstr *Instr) { 7160b57cec5SDimitry Andric const iterator_range<mop_iterator> &Range(Instr->explicit_uses()); 717e8d8bef9SDimitry Andric if (!Range.empty()) 7180b57cec5SDimitry Andric Worklist.push_back(reverse(Range)); 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric /// Some of Instr's operands are on the top of the stack; remove them and 7220b57cec5SDimitry Andric /// re-insert them starting from the beginning (because we've commuted them). 7230b57cec5SDimitry Andric void resetTopOperands(MachineInstr *Instr) { 7240b57cec5SDimitry Andric assert(hasRemainingOperands(Instr) && 7250b57cec5SDimitry Andric "Reseting operands should only be done when the instruction has " 7260b57cec5SDimitry Andric "an operand still on the stack"); 7270b57cec5SDimitry Andric Worklist.back() = reverse(Instr->explicit_uses()); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric /// Test whether Instr has operands remaining to be visited at the top of 7310b57cec5SDimitry Andric /// the stack. 7320b57cec5SDimitry Andric bool hasRemainingOperands(const MachineInstr *Instr) const { 7330b57cec5SDimitry Andric if (Worklist.empty()) 7340b57cec5SDimitry Andric return false; 7350b57cec5SDimitry Andric const RangeTy &Range = Worklist.back(); 736e8d8bef9SDimitry Andric return !Range.empty() && Range.begin()->getParent() == Instr; 7370b57cec5SDimitry Andric } 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric /// Test whether the given register is present on the stack, indicating an 7400b57cec5SDimitry Andric /// operand in the tree that we haven't visited yet. Moving a definition of 7410b57cec5SDimitry Andric /// Reg to a point in the tree after that would change its value. 7420b57cec5SDimitry Andric /// 7430b57cec5SDimitry Andric /// This is needed as a consequence of using implicit local.gets for 7440b57cec5SDimitry Andric /// uses and implicit local.sets for defs. 7450b57cec5SDimitry Andric bool isOnStack(unsigned Reg) const { 7460b57cec5SDimitry Andric for (const RangeTy &Range : Worklist) 7470b57cec5SDimitry Andric for (const MachineOperand &MO : Range) 7480b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() == Reg) 7490b57cec5SDimitry Andric return true; 7500b57cec5SDimitry Andric return false; 7510b57cec5SDimitry Andric } 7520b57cec5SDimitry Andric }; 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric /// State to keep track of whether commuting is in flight or whether it's been 7550b57cec5SDimitry Andric /// tried for the current instruction and didn't work. 7560b57cec5SDimitry Andric class CommutingState { 7570b57cec5SDimitry Andric /// There are effectively three states: the initial state where we haven't 7580b57cec5SDimitry Andric /// started commuting anything and we don't know anything yet, the tentative 7590b57cec5SDimitry Andric /// state where we've commuted the operands of the current instruction and are 7600b57cec5SDimitry Andric /// revisiting it, and the declined state where we've reverted the operands 7610b57cec5SDimitry Andric /// back to their original order and will no longer commute it further. 7620b57cec5SDimitry Andric bool TentativelyCommuting = false; 7630b57cec5SDimitry Andric bool Declined = false; 7640b57cec5SDimitry Andric 7650b57cec5SDimitry Andric /// During the tentative state, these hold the operand indices of the commuted 7660b57cec5SDimitry Andric /// operands. 7670b57cec5SDimitry Andric unsigned Operand0, Operand1; 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric public: 7700b57cec5SDimitry Andric /// Stackification for an operand was not successful due to ordering 7710b57cec5SDimitry Andric /// constraints. If possible, and if we haven't already tried it and declined 7720b57cec5SDimitry Andric /// it, commute Insert's operands and prepare to revisit it. 7730b57cec5SDimitry Andric void maybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker, 7740b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII) { 7750b57cec5SDimitry Andric if (TentativelyCommuting) { 7760b57cec5SDimitry Andric assert(!Declined && 7770b57cec5SDimitry Andric "Don't decline commuting until you've finished trying it"); 7780b57cec5SDimitry Andric // Commuting didn't help. Revert it. 7790b57cec5SDimitry Andric TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1); 7800b57cec5SDimitry Andric TentativelyCommuting = false; 7810b57cec5SDimitry Andric Declined = true; 7820b57cec5SDimitry Andric } else if (!Declined && TreeWalker.hasRemainingOperands(Insert)) { 7830b57cec5SDimitry Andric Operand0 = TargetInstrInfo::CommuteAnyOperandIndex; 7840b57cec5SDimitry Andric Operand1 = TargetInstrInfo::CommuteAnyOperandIndex; 7850b57cec5SDimitry Andric if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) { 7860b57cec5SDimitry Andric // Tentatively commute the operands and try again. 7870b57cec5SDimitry Andric TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1); 7880b57cec5SDimitry Andric TreeWalker.resetTopOperands(Insert); 7890b57cec5SDimitry Andric TentativelyCommuting = true; 7900b57cec5SDimitry Andric Declined = false; 7910b57cec5SDimitry Andric } 7920b57cec5SDimitry Andric } 7930b57cec5SDimitry Andric } 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric /// Stackification for some operand was successful. Reset to the default 7960b57cec5SDimitry Andric /// state. 7970b57cec5SDimitry Andric void reset() { 7980b57cec5SDimitry Andric TentativelyCommuting = false; 7990b57cec5SDimitry Andric Declined = false; 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric }; 8020b57cec5SDimitry Andric } // end anonymous namespace 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) { 8050b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Register Stackifying **********\n" 8060b57cec5SDimitry Andric "********** Function: " 8070b57cec5SDimitry Andric << MF.getName() << '\n'); 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric bool Changed = false; 8100b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 8110b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); 8120b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 8130b57cec5SDimitry Andric const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); 8140b57cec5SDimitry Andric auto &MDT = getAnalysis<MachineDominatorTree>(); 8150b57cec5SDimitry Andric auto &LIS = getAnalysis<LiveIntervals>(); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric // Walk the instructions from the bottom up. Currently we don't look past 8180b57cec5SDimitry Andric // block boundaries, and the blocks aren't ordered so the block visitation 8190b57cec5SDimitry Andric // order isn't significant, but we may want to change this in the future. 8200b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 8210b57cec5SDimitry Andric // Don't use a range-based for loop, because we modify the list as we're 8220b57cec5SDimitry Andric // iterating over it and the end iterator may change. 8230b57cec5SDimitry Andric for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) { 8240b57cec5SDimitry Andric MachineInstr *Insert = &*MII; 8250b57cec5SDimitry Andric // Don't nest anything inside an inline asm, because we don't have 8260b57cec5SDimitry Andric // constraints for $push inputs. 8270b57cec5SDimitry Andric if (Insert->isInlineAsm()) 8280b57cec5SDimitry Andric continue; 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric // Ignore debugging intrinsics. 8310b57cec5SDimitry Andric if (Insert->isDebugValue()) 8320b57cec5SDimitry Andric continue; 8330b57cec5SDimitry Andric 8340b57cec5SDimitry Andric // Iterate through the inputs in reverse order, since we'll be pulling 8350b57cec5SDimitry Andric // operands off the stack in LIFO order. 8360b57cec5SDimitry Andric CommutingState Commuting; 8370b57cec5SDimitry Andric TreeWalkerState TreeWalker(Insert); 8380b57cec5SDimitry Andric while (!TreeWalker.done()) { 8395ffd83dbSDimitry Andric MachineOperand &Use = TreeWalker.pop(); 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric // We're only interested in explicit virtual register operands. 8425ffd83dbSDimitry Andric if (!Use.isReg()) 8430b57cec5SDimitry Andric continue; 8440b57cec5SDimitry Andric 8455ffd83dbSDimitry Andric Register Reg = Use.getReg(); 8465ffd83dbSDimitry Andric assert(Use.isUse() && "explicit_uses() should only iterate over uses"); 8475ffd83dbSDimitry Andric assert(!Use.isImplicit() && 8480b57cec5SDimitry Andric "explicit_uses() should only iterate over explicit operands"); 849bdd1243dSDimitry Andric if (Reg.isPhysical()) 8500b57cec5SDimitry Andric continue; 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric // Identify the definition for this register at this point. 8535ffd83dbSDimitry Andric MachineInstr *DefI = getVRegDef(Reg, Insert, MRI, LIS); 8545ffd83dbSDimitry Andric if (!DefI) 8550b57cec5SDimitry Andric continue; 8560b57cec5SDimitry Andric 8570b57cec5SDimitry Andric // Don't nest an INLINE_ASM def into anything, because we don't have 8580b57cec5SDimitry Andric // constraints for $pop outputs. 8595ffd83dbSDimitry Andric if (DefI->isInlineAsm()) 8600b57cec5SDimitry Andric continue; 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric // Argument instructions represent live-in registers and not real 8630b57cec5SDimitry Andric // instructions. 8645ffd83dbSDimitry Andric if (WebAssembly::isArgument(DefI->getOpcode())) 8650b57cec5SDimitry Andric continue; 8660b57cec5SDimitry Andric 8675ffd83dbSDimitry Andric MachineOperand *Def = DefI->findRegisterDefOperand(Reg); 8685ffd83dbSDimitry Andric assert(Def != nullptr); 8695ffd83dbSDimitry Andric 8700b57cec5SDimitry Andric // Decide which strategy to take. Prefer to move a single-use value 8710b57cec5SDimitry Andric // over cloning it, and prefer cloning over introducing a tee. 8720b57cec5SDimitry Andric // For moving, we require the def to be in the same block as the use; 8730b57cec5SDimitry Andric // this makes things simpler (LiveIntervals' handleMove function only 8740b57cec5SDimitry Andric // supports intra-block moves) and it's MachineSink's job to catch all 8750b57cec5SDimitry Andric // the sinking opportunities anyway. 8765ffd83dbSDimitry Andric bool SameBlock = DefI->getParent() == &MBB; 877fcaf7f86SDimitry Andric bool CanMove = SameBlock && isSafeToMove(Def, &Use, Insert, MFI, MRI) && 8780b57cec5SDimitry Andric !TreeWalker.isOnStack(Reg); 87906c3fb27SDimitry Andric if (CanMove && hasOneNonDBGUse(Reg, DefI, MRI, MDT, LIS)) { 8805ffd83dbSDimitry Andric Insert = moveForSingleUse(Reg, Use, DefI, MBB, Insert, LIS, MFI, MRI); 8815ffd83dbSDimitry Andric 8825ffd83dbSDimitry Andric // If we are removing the frame base reg completely, remove the debug 8835ffd83dbSDimitry Andric // info as well. 8845ffd83dbSDimitry Andric // TODO: Encode this properly as a stackified value. 8855ffd83dbSDimitry Andric if (MFI.isFrameBaseVirtual() && MFI.getFrameBaseVreg() == Reg) 8865ffd83dbSDimitry Andric MFI.clearFrameBaseVreg(); 887fcaf7f86SDimitry Andric } else if (shouldRematerialize(*DefI, TII)) { 8880b57cec5SDimitry Andric Insert = 8895ffd83dbSDimitry Andric rematerializeCheapDef(Reg, Use, *DefI, MBB, Insert->getIterator(), 8900b57cec5SDimitry Andric LIS, MFI, MRI, TII, TRI); 8915ffd83dbSDimitry Andric } else if (CanMove && oneUseDominatesOtherUses(Reg, Use, MBB, MRI, MDT, 8925ffd83dbSDimitry Andric LIS, MFI)) { 8935ffd83dbSDimitry Andric Insert = moveAndTeeForMultiUse(Reg, Use, DefI, MBB, Insert, LIS, MFI, 8940b57cec5SDimitry Andric MRI, TII); 8950b57cec5SDimitry Andric } else { 8960b57cec5SDimitry Andric // We failed to stackify the operand. If the problem was ordering 8970b57cec5SDimitry Andric // constraints, Commuting may be able to help. 8980b57cec5SDimitry Andric if (!CanMove && SameBlock) 8990b57cec5SDimitry Andric Commuting.maybeCommute(Insert, TreeWalker, TII); 9000b57cec5SDimitry Andric // Proceed to the next operand. 9010b57cec5SDimitry Andric continue; 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric 9045ffd83dbSDimitry Andric // Stackifying a multivalue def may unlock in-place stackification of 9055ffd83dbSDimitry Andric // subsequent defs. TODO: Handle the case where the consecutive uses are 9065ffd83dbSDimitry Andric // not all in the same instruction. 9075ffd83dbSDimitry Andric auto *SubsequentDef = Insert->defs().begin(); 9085ffd83dbSDimitry Andric auto *SubsequentUse = &Use; 9095ffd83dbSDimitry Andric while (SubsequentDef != Insert->defs().end() && 9105ffd83dbSDimitry Andric SubsequentUse != Use.getParent()->uses().end()) { 9115ffd83dbSDimitry Andric if (!SubsequentDef->isReg() || !SubsequentUse->isReg()) 9125ffd83dbSDimitry Andric break; 91304eeddc0SDimitry Andric Register DefReg = SubsequentDef->getReg(); 91404eeddc0SDimitry Andric Register UseReg = SubsequentUse->getReg(); 9155ffd83dbSDimitry Andric // TODO: This single-use restriction could be relaxed by using tees 91606c3fb27SDimitry Andric if (DefReg != UseReg || !MRI.hasOneNonDBGUse(DefReg)) 9175ffd83dbSDimitry Andric break; 9185ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, DefReg); 9195ffd83dbSDimitry Andric ++SubsequentDef; 9205ffd83dbSDimitry Andric ++SubsequentUse; 9215ffd83dbSDimitry Andric } 9225ffd83dbSDimitry Andric 9230b57cec5SDimitry Andric // If the instruction we just stackified is an IMPLICIT_DEF, convert it 9240b57cec5SDimitry Andric // to a constant 0 so that the def is explicit, and the push/pop 9250b57cec5SDimitry Andric // correspondence is maintained. 9260b57cec5SDimitry Andric if (Insert->getOpcode() == TargetOpcode::IMPLICIT_DEF) 9270b57cec5SDimitry Andric convertImplicitDefToConstZero(Insert, MRI, TII, MF, LIS); 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric // We stackified an operand. Add the defining instruction's operands to 9300b57cec5SDimitry Andric // the worklist stack now to continue to build an ever deeper tree. 9310b57cec5SDimitry Andric Commuting.reset(); 9320b57cec5SDimitry Andric TreeWalker.pushOperands(Insert); 9330b57cec5SDimitry Andric } 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric // If we stackified any operands, skip over the tree to start looking for 9360b57cec5SDimitry Andric // the next instruction we can build a tree on. 9370b57cec5SDimitry Andric if (Insert != &*MII) { 9380b57cec5SDimitry Andric imposeStackOrdering(&*MII); 9390b57cec5SDimitry Andric MII = MachineBasicBlock::iterator(Insert).getReverse(); 9400b57cec5SDimitry Andric Changed = true; 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric } 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric // If we used VALUE_STACK anywhere, add it to the live-in sets everywhere so 9460b57cec5SDimitry Andric // that it never looks like a use-before-def. 9470b57cec5SDimitry Andric if (Changed) { 9480b57cec5SDimitry Andric MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK); 9490b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) 9500b57cec5SDimitry Andric MBB.addLiveIn(WebAssembly::VALUE_STACK); 9510b57cec5SDimitry Andric } 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric #ifndef NDEBUG 9540b57cec5SDimitry Andric // Verify that pushes and pops are performed in LIFO order. 9550b57cec5SDimitry Andric SmallVector<unsigned, 0> Stack; 9560b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 9570b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 9580b57cec5SDimitry Andric if (MI.isDebugInstr()) 9590b57cec5SDimitry Andric continue; 9605ffd83dbSDimitry Andric for (MachineOperand &MO : reverse(MI.explicit_uses())) { 9610b57cec5SDimitry Andric if (!MO.isReg()) 9620b57cec5SDimitry Andric continue; 9638bcb0991SDimitry Andric Register Reg = MO.getReg(); 9645ffd83dbSDimitry Andric if (MFI.isVRegStackified(Reg)) 9650b57cec5SDimitry Andric assert(Stack.pop_back_val() == Reg && 9660b57cec5SDimitry Andric "Register stack pop should be paired with a push"); 9670b57cec5SDimitry Andric } 9685ffd83dbSDimitry Andric for (MachineOperand &MO : MI.defs()) { 9695ffd83dbSDimitry Andric if (!MO.isReg()) 9705ffd83dbSDimitry Andric continue; 9715ffd83dbSDimitry Andric Register Reg = MO.getReg(); 9725ffd83dbSDimitry Andric if (MFI.isVRegStackified(Reg)) 9735ffd83dbSDimitry Andric Stack.push_back(MO.getReg()); 9740b57cec5SDimitry Andric } 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric // TODO: Generalize this code to support keeping values on the stack across 9770b57cec5SDimitry Andric // basic block boundaries. 9780b57cec5SDimitry Andric assert(Stack.empty() && 9790b57cec5SDimitry Andric "Register stack pushes and pops should be balanced"); 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric #endif 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric return Changed; 9840b57cec5SDimitry Andric } 985