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" 275f757f3fSDimitry 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" 36*0fca6ea1SDimitry Andric #include "llvm/IR/GlobalAlias.h" 370b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 395ffd83dbSDimitry Andric #include <iterator> 400b57cec5SDimitry Andric using namespace llvm; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric #define DEBUG_TYPE "wasm-reg-stackify" 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric namespace { 450b57cec5SDimitry Andric class WebAssemblyRegStackify final : public MachineFunctionPass { 460b57cec5SDimitry Andric StringRef getPassName() const override { 470b57cec5SDimitry Andric return "WebAssembly Register Stackify"; 480b57cec5SDimitry Andric } 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 510b57cec5SDimitry Andric AU.setPreservesCFG(); 52*0fca6ea1SDimitry Andric AU.addRequired<MachineDominatorTreeWrapperPass>(); 53*0fca6ea1SDimitry Andric AU.addRequired<LiveIntervalsWrapperPass>(); 54*0fca6ea1SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>(); 55*0fca6ea1SDimitry Andric AU.addPreserved<SlotIndexesWrapperPass>(); 56*0fca6ea1SDimitry Andric AU.addPreserved<LiveIntervalsWrapperPass>(); 570b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID); 58*0fca6ea1SDimitry Andric AU.addPreserved<MachineDominatorTreeWrapperPass>(); 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. 84*0fca6ea1SDimitry Andric if (!MI->definesRegister(WebAssembly::VALUE_STACK, /*TRI=*/nullptr)) 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. 90*0fca6ea1SDimitry Andric if (!MI->readsRegister(WebAssembly::VALUE_STACK, /*TRI=*/nullptr)) 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) { 123fe6060f1SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_V128_I64x2)); 124fe6060f1SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0)); 125fe6060f1SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0)); 1260b57cec5SDimitry Andric } else { 1270b57cec5SDimitry Andric llvm_unreachable("Unexpected reg class"); 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Determine whether a call to the callee referenced by 1320b57cec5SDimitry Andric // MI->getOperand(CalleeOpNo) reads memory, writes memory, and/or has side 1330b57cec5SDimitry Andric // effects. 1345ffd83dbSDimitry Andric static void queryCallee(const MachineInstr &MI, bool &Read, bool &Write, 1355ffd83dbSDimitry Andric bool &Effects, bool &StackPointer) { 1360b57cec5SDimitry Andric // All calls can use the stack pointer. 1370b57cec5SDimitry Andric StackPointer = true; 1380b57cec5SDimitry Andric 1395ffd83dbSDimitry Andric const MachineOperand &MO = WebAssembly::getCalleeOp(MI); 1400b57cec5SDimitry Andric if (MO.isGlobal()) { 1410b57cec5SDimitry Andric const Constant *GV = MO.getGlobal(); 1420b57cec5SDimitry Andric if (const auto *GA = dyn_cast<GlobalAlias>(GV)) 1430b57cec5SDimitry Andric if (!GA->isInterposable()) 1440b57cec5SDimitry Andric GV = GA->getAliasee(); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric if (const auto *F = dyn_cast<Function>(GV)) { 1470b57cec5SDimitry Andric if (!F->doesNotThrow()) 1480b57cec5SDimitry Andric Effects = true; 1490b57cec5SDimitry Andric if (F->doesNotAccessMemory()) 1500b57cec5SDimitry Andric return; 1510b57cec5SDimitry Andric if (F->onlyReadsMemory()) { 1520b57cec5SDimitry Andric Read = true; 1530b57cec5SDimitry Andric return; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric // Assume the worst. 1590b57cec5SDimitry Andric Write = true; 1600b57cec5SDimitry Andric Read = true; 1610b57cec5SDimitry Andric Effects = true; 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Determine whether MI reads memory, writes memory, has side effects, 1650b57cec5SDimitry Andric // and/or uses the stack pointer value. 166fcaf7f86SDimitry Andric static void query(const MachineInstr &MI, bool &Read, bool &Write, 167fcaf7f86SDimitry Andric bool &Effects, bool &StackPointer) { 1680b57cec5SDimitry Andric assert(!MI.isTerminator()); 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric if (MI.isDebugInstr() || MI.isPosition()) 1710b57cec5SDimitry Andric return; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // Check for loads. 174fcaf7f86SDimitry Andric if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad()) 1750b57cec5SDimitry Andric Read = true; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric // Check for stores. 1780b57cec5SDimitry Andric if (MI.mayStore()) { 1790b57cec5SDimitry Andric Write = true; 1800b57cec5SDimitry Andric } else if (MI.hasOrderedMemoryRef()) { 1810b57cec5SDimitry Andric switch (MI.getOpcode()) { 1820b57cec5SDimitry Andric case WebAssembly::DIV_S_I32: 1830b57cec5SDimitry Andric case WebAssembly::DIV_S_I64: 1840b57cec5SDimitry Andric case WebAssembly::REM_S_I32: 1850b57cec5SDimitry Andric case WebAssembly::REM_S_I64: 1860b57cec5SDimitry Andric case WebAssembly::DIV_U_I32: 1870b57cec5SDimitry Andric case WebAssembly::DIV_U_I64: 1880b57cec5SDimitry Andric case WebAssembly::REM_U_I32: 1890b57cec5SDimitry Andric case WebAssembly::REM_U_I64: 1900b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F32: 1910b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F32: 1920b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F64: 1930b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F64: 1940b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F32: 1950b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F32: 1960b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F64: 1970b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F64: 1980b57cec5SDimitry Andric // These instruction have hasUnmodeledSideEffects() returning true 1990b57cec5SDimitry Andric // because they trap on overflow and invalid so they can't be arbitrarily 2000b57cec5SDimitry Andric // moved, however hasOrderedMemoryRef() interprets this plus their lack 2010b57cec5SDimitry Andric // of memoperands as having a potential unknown memory reference. 2020b57cec5SDimitry Andric break; 2030b57cec5SDimitry Andric default: 2040b57cec5SDimitry Andric // Record volatile accesses, unless it's a call, as calls are handled 2050b57cec5SDimitry Andric // specially below. 2060b57cec5SDimitry Andric if (!MI.isCall()) { 2070b57cec5SDimitry Andric Write = true; 2080b57cec5SDimitry Andric Effects = true; 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric break; 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // Check for side effects. 2150b57cec5SDimitry Andric if (MI.hasUnmodeledSideEffects()) { 2160b57cec5SDimitry Andric switch (MI.getOpcode()) { 2170b57cec5SDimitry Andric case WebAssembly::DIV_S_I32: 2180b57cec5SDimitry Andric case WebAssembly::DIV_S_I64: 2190b57cec5SDimitry Andric case WebAssembly::REM_S_I32: 2200b57cec5SDimitry Andric case WebAssembly::REM_S_I64: 2210b57cec5SDimitry Andric case WebAssembly::DIV_U_I32: 2220b57cec5SDimitry Andric case WebAssembly::DIV_U_I64: 2230b57cec5SDimitry Andric case WebAssembly::REM_U_I32: 2240b57cec5SDimitry Andric case WebAssembly::REM_U_I64: 2250b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F32: 2260b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F32: 2270b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F64: 2280b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F64: 2290b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F32: 2300b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F32: 2310b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F64: 2320b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F64: 2330b57cec5SDimitry Andric // These instructions have hasUnmodeledSideEffects() returning true 2340b57cec5SDimitry Andric // because they trap on overflow and invalid so they can't be arbitrarily 2350b57cec5SDimitry Andric // moved, however in the specific case of register stackifying, it is safe 2360b57cec5SDimitry Andric // to move them because overflow and invalid are Undefined Behavior. 2370b57cec5SDimitry Andric break; 2380b57cec5SDimitry Andric default: 2390b57cec5SDimitry Andric Effects = true; 2400b57cec5SDimitry Andric break; 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric // Check for writes to __stack_pointer global. 2455ffd83dbSDimitry Andric if ((MI.getOpcode() == WebAssembly::GLOBAL_SET_I32 || 2465ffd83dbSDimitry Andric MI.getOpcode() == WebAssembly::GLOBAL_SET_I64) && 2470b57cec5SDimitry Andric strcmp(MI.getOperand(0).getSymbolName(), "__stack_pointer") == 0) 2480b57cec5SDimitry Andric StackPointer = true; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // Analyze calls. 2510b57cec5SDimitry Andric if (MI.isCall()) { 2525ffd83dbSDimitry Andric queryCallee(MI, Read, Write, Effects, StackPointer); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // Test whether Def is safe and profitable to rematerialize. 257fcaf7f86SDimitry Andric static bool shouldRematerialize(const MachineInstr &Def, 2580b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII) { 259fcaf7f86SDimitry Andric return Def.isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def); 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric // Identify the definition for this register at this point. This is a 2630b57cec5SDimitry Andric // generalization of MachineRegisterInfo::getUniqueVRegDef that uses 2640b57cec5SDimitry Andric // LiveIntervals to handle complex cases. 2650b57cec5SDimitry Andric static MachineInstr *getVRegDef(unsigned Reg, const MachineInstr *Insert, 2660b57cec5SDimitry Andric const MachineRegisterInfo &MRI, 2670b57cec5SDimitry Andric const LiveIntervals &LIS) { 2680b57cec5SDimitry Andric // Most registers are in SSA form here so we try a quick MRI query first. 2690b57cec5SDimitry Andric if (MachineInstr *Def = MRI.getUniqueVRegDef(Reg)) 2700b57cec5SDimitry Andric return Def; 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // MRI doesn't know what the Def is. Try asking LIS. 2730b57cec5SDimitry Andric if (const VNInfo *ValNo = LIS.getInterval(Reg).getVNInfoBefore( 2740b57cec5SDimitry Andric LIS.getInstructionIndex(*Insert))) 2750b57cec5SDimitry Andric return LIS.getInstructionFromIndex(ValNo->def); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric return nullptr; 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric // Test whether Reg, as defined at Def, has exactly one use. This is a 28106c3fb27SDimitry Andric // generalization of MachineRegisterInfo::hasOneNonDBGUse that uses 28206c3fb27SDimitry Andric // LiveIntervals to handle complex cases. 28306c3fb27SDimitry Andric static bool hasOneNonDBGUse(unsigned Reg, MachineInstr *Def, 28406c3fb27SDimitry Andric MachineRegisterInfo &MRI, MachineDominatorTree &MDT, 28506c3fb27SDimitry Andric LiveIntervals &LIS) { 2860b57cec5SDimitry Andric // Most registers are in SSA form here so we try a quick MRI query first. 28706c3fb27SDimitry Andric if (MRI.hasOneNonDBGUse(Reg)) 2880b57cec5SDimitry Andric return true; 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric bool HasOne = false; 2910b57cec5SDimitry Andric const LiveInterval &LI = LIS.getInterval(Reg); 2920b57cec5SDimitry Andric const VNInfo *DefVNI = 2930b57cec5SDimitry Andric LI.getVNInfoAt(LIS.getInstructionIndex(*Def).getRegSlot()); 2940b57cec5SDimitry Andric assert(DefVNI); 2950b57cec5SDimitry Andric for (auto &I : MRI.use_nodbg_operands(Reg)) { 2960b57cec5SDimitry Andric const auto &Result = LI.Query(LIS.getInstructionIndex(*I.getParent())); 2970b57cec5SDimitry Andric if (Result.valueIn() == DefVNI) { 2980b57cec5SDimitry Andric if (!Result.isKill()) 2990b57cec5SDimitry Andric return false; 3000b57cec5SDimitry Andric if (HasOne) 3010b57cec5SDimitry Andric return false; 3020b57cec5SDimitry Andric HasOne = true; 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric return HasOne; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric // Test whether it's safe to move Def to just before Insert. 3090b57cec5SDimitry Andric // TODO: Compute memory dependencies in a way that doesn't require always 3100b57cec5SDimitry Andric // walking the block. 3110b57cec5SDimitry Andric // TODO: Compute memory dependencies in a way that uses AliasAnalysis to be 3120b57cec5SDimitry Andric // more precise. 3135ffd83dbSDimitry Andric static bool isSafeToMove(const MachineOperand *Def, const MachineOperand *Use, 314fcaf7f86SDimitry Andric const MachineInstr *Insert, 3155ffd83dbSDimitry Andric const WebAssemblyFunctionInfo &MFI, 3165ffd83dbSDimitry Andric const MachineRegisterInfo &MRI) { 3175ffd83dbSDimitry Andric const MachineInstr *DefI = Def->getParent(); 3185ffd83dbSDimitry Andric const MachineInstr *UseI = Use->getParent(); 3195ffd83dbSDimitry Andric assert(DefI->getParent() == Insert->getParent()); 3205ffd83dbSDimitry Andric assert(UseI->getParent() == Insert->getParent()); 3215ffd83dbSDimitry Andric 3225ffd83dbSDimitry Andric // The first def of a multivalue instruction can be stackified by moving, 3235ffd83dbSDimitry Andric // since the later defs can always be placed into locals if necessary. Later 3245ffd83dbSDimitry Andric // defs can only be stackified if all previous defs are already stackified 3255ffd83dbSDimitry Andric // since ExplicitLocals will not know how to place a def in a local if a 3265ffd83dbSDimitry Andric // subsequent def is stackified. But only one def can be stackified by moving 3275ffd83dbSDimitry Andric // the instruction, so it must be the first one. 3285ffd83dbSDimitry Andric // 3295ffd83dbSDimitry Andric // TODO: This could be loosened to be the first *live* def, but care would 3305ffd83dbSDimitry Andric // have to be taken to ensure the drops of the initial dead defs can be 3315ffd83dbSDimitry Andric // placed. This would require checking that no previous defs are used in the 3325ffd83dbSDimitry Andric // same instruction as subsequent defs. 3335ffd83dbSDimitry Andric if (Def != DefI->defs().begin()) 3345ffd83dbSDimitry Andric return false; 3355ffd83dbSDimitry Andric 3365ffd83dbSDimitry Andric // If any subsequent def is used prior to the current value by the same 3375ffd83dbSDimitry Andric // instruction in which the current value is used, we cannot 3385ffd83dbSDimitry Andric // stackify. Stackifying in this case would require that def moving below the 3395ffd83dbSDimitry Andric // current def in the stack, which cannot be achieved, even with locals. 340bdd1243dSDimitry Andric // Also ensure we don't sink the def past any other prior uses. 341e8d8bef9SDimitry Andric for (const auto &SubsequentDef : drop_begin(DefI->defs())) { 342bdd1243dSDimitry Andric auto I = std::next(MachineBasicBlock::const_iterator(DefI)); 343bdd1243dSDimitry Andric auto E = std::next(MachineBasicBlock::const_iterator(UseI)); 344bdd1243dSDimitry Andric for (; I != E; ++I) { 345bdd1243dSDimitry Andric for (const auto &PriorUse : I->uses()) { 3465ffd83dbSDimitry Andric if (&PriorUse == Use) 3475ffd83dbSDimitry Andric break; 3485ffd83dbSDimitry Andric if (PriorUse.isReg() && SubsequentDef.getReg() == PriorUse.getReg()) 3495ffd83dbSDimitry Andric return false; 3505ffd83dbSDimitry Andric } 3515ffd83dbSDimitry Andric } 352bdd1243dSDimitry Andric } 3535ffd83dbSDimitry Andric 3545ffd83dbSDimitry Andric // If moving is a semantic nop, it is always allowed 3555ffd83dbSDimitry Andric const MachineBasicBlock *MBB = DefI->getParent(); 3565ffd83dbSDimitry Andric auto NextI = std::next(MachineBasicBlock::const_iterator(DefI)); 3575ffd83dbSDimitry Andric for (auto E = MBB->end(); NextI != E && NextI->isDebugInstr(); ++NextI) 3585ffd83dbSDimitry Andric ; 3595ffd83dbSDimitry Andric if (NextI == Insert) 3605ffd83dbSDimitry Andric return true; 3610b57cec5SDimitry Andric 362e8d8bef9SDimitry Andric // 'catch' and 'catch_all' should be the first instruction of a BB and cannot 363e8d8bef9SDimitry Andric // move. 364e8d8bef9SDimitry Andric if (WebAssembly::isCatch(DefI->getOpcode())) 3650b57cec5SDimitry Andric return false; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric // Check for register dependencies. 3680b57cec5SDimitry Andric SmallVector<unsigned, 4> MutableRegisters; 3695ffd83dbSDimitry Andric for (const MachineOperand &MO : DefI->operands()) { 3700b57cec5SDimitry Andric if (!MO.isReg() || MO.isUndef()) 3710b57cec5SDimitry Andric continue; 3728bcb0991SDimitry Andric Register Reg = MO.getReg(); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // If the register is dead here and at Insert, ignore it. 375*0fca6ea1SDimitry Andric if (MO.isDead() && Insert->definesRegister(Reg, /*TRI=*/nullptr) && 376*0fca6ea1SDimitry Andric !Insert->readsRegister(Reg, /*TRI=*/nullptr)) 3770b57cec5SDimitry Andric continue; 3780b57cec5SDimitry Andric 379bdd1243dSDimitry Andric if (Reg.isPhysical()) { 3800b57cec5SDimitry Andric // Ignore ARGUMENTS; it's just used to keep the ARGUMENT_* instructions 3810b57cec5SDimitry Andric // from moving down, and we've already checked for that. 3820b57cec5SDimitry Andric if (Reg == WebAssembly::ARGUMENTS) 3830b57cec5SDimitry Andric continue; 3840b57cec5SDimitry Andric // If the physical register is never modified, ignore it. 3850b57cec5SDimitry Andric if (!MRI.isPhysRegModified(Reg)) 3860b57cec5SDimitry Andric continue; 3870b57cec5SDimitry Andric // Otherwise, it's a physical register with unknown liveness. 3880b57cec5SDimitry Andric return false; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric // If one of the operands isn't in SSA form, it has different values at 3920b57cec5SDimitry Andric // different times, and we need to make sure we don't move our use across 3930b57cec5SDimitry Andric // a different def. 3940b57cec5SDimitry Andric if (!MO.isDef() && !MRI.hasOneDef(Reg)) 3950b57cec5SDimitry Andric MutableRegisters.push_back(Reg); 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric bool Read = false, Write = false, Effects = false, StackPointer = false; 399fcaf7f86SDimitry Andric query(*DefI, Read, Write, Effects, StackPointer); 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric // If the instruction does not access memory and has no side effects, it has 4020b57cec5SDimitry Andric // no additional dependencies. 4030b57cec5SDimitry Andric bool HasMutableRegisters = !MutableRegisters.empty(); 4040b57cec5SDimitry Andric if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters) 4050b57cec5SDimitry Andric return true; 4060b57cec5SDimitry Andric 4075ffd83dbSDimitry Andric // Scan through the intervening instructions between DefI and Insert. 4085ffd83dbSDimitry Andric MachineBasicBlock::const_iterator D(DefI), I(Insert); 4090b57cec5SDimitry Andric for (--I; I != D; --I) { 4100b57cec5SDimitry Andric bool InterveningRead = false; 4110b57cec5SDimitry Andric bool InterveningWrite = false; 4120b57cec5SDimitry Andric bool InterveningEffects = false; 4130b57cec5SDimitry Andric bool InterveningStackPointer = false; 414fcaf7f86SDimitry Andric query(*I, InterveningRead, InterveningWrite, InterveningEffects, 4150b57cec5SDimitry Andric InterveningStackPointer); 4160b57cec5SDimitry Andric if (Effects && InterveningEffects) 4170b57cec5SDimitry Andric return false; 4180b57cec5SDimitry Andric if (Read && InterveningWrite) 4190b57cec5SDimitry Andric return false; 4200b57cec5SDimitry Andric if (Write && (InterveningRead || InterveningWrite)) 4210b57cec5SDimitry Andric return false; 4220b57cec5SDimitry Andric if (StackPointer && InterveningStackPointer) 4230b57cec5SDimitry Andric return false; 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric for (unsigned Reg : MutableRegisters) 4260b57cec5SDimitry Andric for (const MachineOperand &MO : I->operands()) 4270b57cec5SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) 4280b57cec5SDimitry Andric return false; 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric return true; 4320b57cec5SDimitry Andric } 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric /// Test whether OneUse, a use of Reg, dominates all of Reg's other uses. 4350b57cec5SDimitry Andric static bool oneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse, 4360b57cec5SDimitry Andric const MachineBasicBlock &MBB, 4370b57cec5SDimitry Andric const MachineRegisterInfo &MRI, 4380b57cec5SDimitry Andric const MachineDominatorTree &MDT, 4390b57cec5SDimitry Andric LiveIntervals &LIS, 4400b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI) { 4410b57cec5SDimitry Andric const LiveInterval &LI = LIS.getInterval(Reg); 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric const MachineInstr *OneUseInst = OneUse.getParent(); 4440b57cec5SDimitry Andric VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst)); 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric for (const MachineOperand &Use : MRI.use_nodbg_operands(Reg)) { 4470b57cec5SDimitry Andric if (&Use == &OneUse) 4480b57cec5SDimitry Andric continue; 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric const MachineInstr *UseInst = Use.getParent(); 4510b57cec5SDimitry Andric VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst)); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric if (UseVNI != OneUseVNI) 4540b57cec5SDimitry Andric continue; 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric if (UseInst == OneUseInst) { 4570b57cec5SDimitry Andric // Another use in the same instruction. We need to ensure that the one 4580b57cec5SDimitry Andric // selected use happens "before" it. 4590b57cec5SDimitry Andric if (&OneUse > &Use) 4600b57cec5SDimitry Andric return false; 4610b57cec5SDimitry Andric } else { 4620b57cec5SDimitry Andric // Test that the use is dominated by the one selected use. 4630b57cec5SDimitry Andric while (!MDT.dominates(OneUseInst, UseInst)) { 4640b57cec5SDimitry Andric // Actually, dominating is over-conservative. Test that the use would 4650b57cec5SDimitry Andric // happen after the one selected use in the stack evaluation order. 4660b57cec5SDimitry Andric // 4670b57cec5SDimitry Andric // This is needed as a consequence of using implicit local.gets for 4680b57cec5SDimitry Andric // uses and implicit local.sets for defs. 4690b57cec5SDimitry Andric if (UseInst->getDesc().getNumDefs() == 0) 4700b57cec5SDimitry Andric return false; 4710b57cec5SDimitry Andric const MachineOperand &MO = UseInst->getOperand(0); 4720b57cec5SDimitry Andric if (!MO.isReg()) 4730b57cec5SDimitry Andric return false; 4748bcb0991SDimitry Andric Register DefReg = MO.getReg(); 475bdd1243dSDimitry Andric if (!DefReg.isVirtual() || !MFI.isVRegStackified(DefReg)) 4760b57cec5SDimitry Andric return false; 4770b57cec5SDimitry Andric assert(MRI.hasOneNonDBGUse(DefReg)); 4780b57cec5SDimitry Andric const MachineOperand &NewUse = *MRI.use_nodbg_begin(DefReg); 4790b57cec5SDimitry Andric const MachineInstr *NewUseInst = NewUse.getParent(); 4800b57cec5SDimitry Andric if (NewUseInst == OneUseInst) { 4810b57cec5SDimitry Andric if (&OneUse > &NewUse) 4820b57cec5SDimitry Andric return false; 4830b57cec5SDimitry Andric break; 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric UseInst = NewUseInst; 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric return true; 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric /// Get the appropriate tee opcode for the given register class. 4930b57cec5SDimitry Andric static unsigned getTeeOpcode(const TargetRegisterClass *RC) { 4940b57cec5SDimitry Andric if (RC == &WebAssembly::I32RegClass) 4950b57cec5SDimitry Andric return WebAssembly::TEE_I32; 4960b57cec5SDimitry Andric if (RC == &WebAssembly::I64RegClass) 4970b57cec5SDimitry Andric return WebAssembly::TEE_I64; 4980b57cec5SDimitry Andric if (RC == &WebAssembly::F32RegClass) 4990b57cec5SDimitry Andric return WebAssembly::TEE_F32; 5000b57cec5SDimitry Andric if (RC == &WebAssembly::F64RegClass) 5010b57cec5SDimitry Andric return WebAssembly::TEE_F64; 5020b57cec5SDimitry Andric if (RC == &WebAssembly::V128RegClass) 5030b57cec5SDimitry Andric return WebAssembly::TEE_V128; 504349cc55cSDimitry Andric if (RC == &WebAssembly::EXTERNREFRegClass) 505349cc55cSDimitry Andric return WebAssembly::TEE_EXTERNREF; 506349cc55cSDimitry Andric if (RC == &WebAssembly::FUNCREFRegClass) 507349cc55cSDimitry Andric return WebAssembly::TEE_FUNCREF; 508*0fca6ea1SDimitry Andric if (RC == &WebAssembly::EXNREFRegClass) 509*0fca6ea1SDimitry Andric return WebAssembly::TEE_EXNREF; 5100b57cec5SDimitry Andric llvm_unreachable("Unexpected register class"); 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // Shrink LI to its uses, cleaning up LI. 5140b57cec5SDimitry Andric static void shrinkToUses(LiveInterval &LI, LiveIntervals &LIS) { 5150b57cec5SDimitry Andric if (LIS.shrinkToUses(&LI)) { 5160b57cec5SDimitry Andric SmallVector<LiveInterval *, 4> SplitLIs; 5170b57cec5SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric /// A single-use def in the same block with no intervening memory or register 5220b57cec5SDimitry Andric /// dependencies; move the def down and nest it with the current instruction. 5230b57cec5SDimitry Andric static MachineInstr *moveForSingleUse(unsigned Reg, MachineOperand &Op, 5240b57cec5SDimitry Andric MachineInstr *Def, MachineBasicBlock &MBB, 5250b57cec5SDimitry Andric MachineInstr *Insert, LiveIntervals &LIS, 5260b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI, 5270b57cec5SDimitry Andric MachineRegisterInfo &MRI) { 5280b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Move for single use: "; Def->dump()); 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(Def); 53106c3fb27SDimitry Andric DefDIs.sink(Insert); 5320b57cec5SDimitry Andric LIS.handleMove(*Def); 5330b57cec5SDimitry Andric 53406c3fb27SDimitry Andric if (MRI.hasOneDef(Reg) && MRI.hasOneNonDBGUse(Reg)) { 5350b57cec5SDimitry Andric // No one else is using this register for anything so we can just stackify 5360b57cec5SDimitry Andric // it in place. 5375ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, Reg); 5380b57cec5SDimitry Andric } else { 5390b57cec5SDimitry Andric // The register may have unrelated uses or defs; create a new register for 5400b57cec5SDimitry Andric // just our one def and use so that we can stackify it. 5418bcb0991SDimitry Andric Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); 5420b57cec5SDimitry Andric Op.setReg(NewReg); 54306c3fb27SDimitry Andric DefDIs.updateReg(NewReg); 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric // Tell LiveIntervals about the new register. 5460b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(NewReg); 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric // Tell LiveIntervals about the changes to the old register. 5490b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 5500b57cec5SDimitry Andric LI.removeSegment(LIS.getInstructionIndex(*Def).getRegSlot(), 5510b57cec5SDimitry Andric LIS.getInstructionIndex(*Op.getParent()).getRegSlot(), 5520b57cec5SDimitry Andric /*RemoveDeadValNo=*/true); 5530b57cec5SDimitry Andric 5545ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, NewReg); 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); 5570b57cec5SDimitry Andric } 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric imposeStackOrdering(Def); 5600b57cec5SDimitry Andric return Def; 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric 56306c3fb27SDimitry Andric static MachineInstr *getPrevNonDebugInst(MachineInstr *MI) { 56406c3fb27SDimitry Andric for (auto *I = MI->getPrevNode(); I; I = I->getPrevNode()) 56506c3fb27SDimitry Andric if (!I->isDebugInstr()) 56606c3fb27SDimitry Andric return I; 56706c3fb27SDimitry Andric return nullptr; 56806c3fb27SDimitry Andric } 56906c3fb27SDimitry Andric 5700b57cec5SDimitry Andric /// A trivially cloneable instruction; clone it and nest the new copy with the 5710b57cec5SDimitry Andric /// current instruction. 5720b57cec5SDimitry Andric static MachineInstr *rematerializeCheapDef( 5730b57cec5SDimitry Andric unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB, 5740b57cec5SDimitry Andric MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS, 5750b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, 5760b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI) { 5770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump()); 5780b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - for use in "; Op.getParent()->dump()); 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(&Def); 5810b57cec5SDimitry Andric 5828bcb0991SDimitry Andric Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); 58306c3fb27SDimitry Andric DefDIs.cloneSink(&*Insert, NewReg); 5840b57cec5SDimitry Andric Op.setReg(NewReg); 58506c3fb27SDimitry Andric MachineInstr *Clone = getPrevNonDebugInst(&*Insert); 58606c3fb27SDimitry Andric assert(Clone); 5870b57cec5SDimitry Andric LIS.InsertMachineInstrInMaps(*Clone); 5880b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(NewReg); 5895ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, NewReg); 5900b57cec5SDimitry Andric imposeStackOrdering(Clone); 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Cloned to "; Clone->dump()); 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric // Shrink the interval. 5950b57cec5SDimitry Andric bool IsDead = MRI.use_empty(Reg); 5960b57cec5SDimitry Andric if (!IsDead) { 5970b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 5980b57cec5SDimitry Andric shrinkToUses(LI, LIS); 5990b57cec5SDimitry Andric IsDead = !LI.liveAt(LIS.getInstructionIndex(Def).getDeadSlot()); 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // If that was the last use of the original, delete the original. 6030b57cec5SDimitry Andric if (IsDead) { 6040b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Deleting original\n"); 6050b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot(); 606e8d8bef9SDimitry Andric LIS.removePhysRegDefAt(MCRegister::from(WebAssembly::ARGUMENTS), Idx); 6070b57cec5SDimitry Andric LIS.removeInterval(Reg); 6080b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(Def); 60906c3fb27SDimitry Andric DefDIs.removeDef(); 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric return Clone; 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric /// A multiple-use def in the same block with no intervening memory or register 6160b57cec5SDimitry Andric /// dependencies; move the def down, nest it with the current instruction, and 6170b57cec5SDimitry Andric /// insert a tee to satisfy the rest of the uses. As an illustration, rewrite 6180b57cec5SDimitry Andric /// this: 6190b57cec5SDimitry Andric /// 6200b57cec5SDimitry Andric /// Reg = INST ... // Def 6210b57cec5SDimitry Andric /// INST ..., Reg, ... // Insert 6220b57cec5SDimitry Andric /// INST ..., Reg, ... 6230b57cec5SDimitry Andric /// INST ..., Reg, ... 6240b57cec5SDimitry Andric /// 6250b57cec5SDimitry Andric /// to this: 6260b57cec5SDimitry Andric /// 6270b57cec5SDimitry Andric /// DefReg = INST ... // Def (to become the new Insert) 6280b57cec5SDimitry Andric /// TeeReg, Reg = TEE_... DefReg 6290b57cec5SDimitry Andric /// INST ..., TeeReg, ... // Insert 6300b57cec5SDimitry Andric /// INST ..., Reg, ... 6310b57cec5SDimitry Andric /// INST ..., Reg, ... 6320b57cec5SDimitry Andric /// 6330b57cec5SDimitry Andric /// with DefReg and TeeReg stackified. This eliminates a local.get from the 6340b57cec5SDimitry Andric /// resulting code. 6350b57cec5SDimitry Andric static MachineInstr *moveAndTeeForMultiUse( 6360b57cec5SDimitry Andric unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB, 6370b57cec5SDimitry Andric MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, 6380b57cec5SDimitry Andric MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) { 6390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump()); 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric const auto *RegClass = MRI.getRegClass(Reg); 6428bcb0991SDimitry Andric Register TeeReg = MRI.createVirtualRegister(RegClass); 6438bcb0991SDimitry Andric Register DefReg = MRI.createVirtualRegister(RegClass); 64406c3fb27SDimitry Andric 64506c3fb27SDimitry Andric // Move Def into place. 64606c3fb27SDimitry Andric WebAssemblyDebugValueManager DefDIs(Def); 64706c3fb27SDimitry Andric DefDIs.sink(Insert); 64806c3fb27SDimitry Andric LIS.handleMove(*Def); 64906c3fb27SDimitry Andric 65006c3fb27SDimitry Andric // Create the Tee and attach the registers. 6510b57cec5SDimitry Andric MachineOperand &DefMO = Def->getOperand(0); 6520b57cec5SDimitry Andric MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(), 6530b57cec5SDimitry Andric TII->get(getTeeOpcode(RegClass)), TeeReg) 6540b57cec5SDimitry Andric .addReg(Reg, RegState::Define) 6550b57cec5SDimitry Andric .addReg(DefReg, getUndefRegState(DefMO.isDead())); 6560b57cec5SDimitry Andric Op.setReg(TeeReg); 65706c3fb27SDimitry Andric DefDIs.updateReg(DefReg); 6580b57cec5SDimitry Andric SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot(); 6590b57cec5SDimitry Andric SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot(); 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric // Tell LiveIntervals we moved the original vreg def from Def to Tee. 6620b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 6630b57cec5SDimitry Andric LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx); 6640b57cec5SDimitry Andric VNInfo *ValNo = LI.getVNInfoAt(DefIdx); 6650b57cec5SDimitry Andric I->start = TeeIdx; 6660b57cec5SDimitry Andric ValNo->def = TeeIdx; 6670b57cec5SDimitry Andric shrinkToUses(LI, LIS); 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric // Finish stackifying the new regs. 6700b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(TeeReg); 6710b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(DefReg); 6725ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, DefReg); 6735ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, TeeReg); 6740b57cec5SDimitry Andric imposeStackOrdering(Def); 6750b57cec5SDimitry Andric imposeStackOrdering(Tee); 6760b57cec5SDimitry Andric 67706c3fb27SDimitry Andric // Even though 'TeeReg, Reg = TEE ...', has two defs, we don't need to clone 67806c3fb27SDimitry Andric // DBG_VALUEs for both of them, given that the latter will cancel the former 67906c3fb27SDimitry Andric // anyway. Here we only clone DBG_VALUEs for TeeReg, which will be converted 68006c3fb27SDimitry Andric // to a local index in ExplicitLocals pass. 68106c3fb27SDimitry Andric DefDIs.cloneSink(Insert, TeeReg, /* CloneDef */ false); 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); 6840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Tee instruction: "; Tee->dump()); 6850b57cec5SDimitry Andric return Def; 6860b57cec5SDimitry Andric } 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric namespace { 6890b57cec5SDimitry Andric /// A stack for walking the tree of instructions being built, visiting the 6900b57cec5SDimitry Andric /// MachineOperands in DFS order. 6910b57cec5SDimitry Andric class TreeWalkerState { 6920b57cec5SDimitry Andric using mop_iterator = MachineInstr::mop_iterator; 6930b57cec5SDimitry Andric using mop_reverse_iterator = std::reverse_iterator<mop_iterator>; 6940b57cec5SDimitry Andric using RangeTy = iterator_range<mop_reverse_iterator>; 6950b57cec5SDimitry Andric SmallVector<RangeTy, 4> Worklist; 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric public: 6980b57cec5SDimitry Andric explicit TreeWalkerState(MachineInstr *Insert) { 6990b57cec5SDimitry Andric const iterator_range<mop_iterator> &Range = Insert->explicit_uses(); 700e8d8bef9SDimitry Andric if (!Range.empty()) 7010b57cec5SDimitry Andric Worklist.push_back(reverse(Range)); 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric bool done() const { return Worklist.empty(); } 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric MachineOperand &pop() { 7070b57cec5SDimitry Andric RangeTy &Range = Worklist.back(); 7080b57cec5SDimitry Andric MachineOperand &Op = *Range.begin(); 709e8d8bef9SDimitry Andric Range = drop_begin(Range); 710e8d8bef9SDimitry Andric if (Range.empty()) 7110b57cec5SDimitry Andric Worklist.pop_back(); 712e8d8bef9SDimitry Andric assert((Worklist.empty() || !Worklist.back().empty()) && 7130b57cec5SDimitry Andric "Empty ranges shouldn't remain in the worklist"); 7140b57cec5SDimitry Andric return Op; 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric /// Push Instr's operands onto the stack to be visited. 7180b57cec5SDimitry Andric void pushOperands(MachineInstr *Instr) { 7190b57cec5SDimitry Andric const iterator_range<mop_iterator> &Range(Instr->explicit_uses()); 720e8d8bef9SDimitry Andric if (!Range.empty()) 7210b57cec5SDimitry Andric Worklist.push_back(reverse(Range)); 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric /// Some of Instr's operands are on the top of the stack; remove them and 7250b57cec5SDimitry Andric /// re-insert them starting from the beginning (because we've commuted them). 7260b57cec5SDimitry Andric void resetTopOperands(MachineInstr *Instr) { 7270b57cec5SDimitry Andric assert(hasRemainingOperands(Instr) && 7280b57cec5SDimitry Andric "Reseting operands should only be done when the instruction has " 7290b57cec5SDimitry Andric "an operand still on the stack"); 7300b57cec5SDimitry Andric Worklist.back() = reverse(Instr->explicit_uses()); 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric /// Test whether Instr has operands remaining to be visited at the top of 7340b57cec5SDimitry Andric /// the stack. 7350b57cec5SDimitry Andric bool hasRemainingOperands(const MachineInstr *Instr) const { 7360b57cec5SDimitry Andric if (Worklist.empty()) 7370b57cec5SDimitry Andric return false; 7380b57cec5SDimitry Andric const RangeTy &Range = Worklist.back(); 739e8d8bef9SDimitry Andric return !Range.empty() && Range.begin()->getParent() == Instr; 7400b57cec5SDimitry Andric } 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric /// Test whether the given register is present on the stack, indicating an 7430b57cec5SDimitry Andric /// operand in the tree that we haven't visited yet. Moving a definition of 7440b57cec5SDimitry Andric /// Reg to a point in the tree after that would change its value. 7450b57cec5SDimitry Andric /// 7460b57cec5SDimitry Andric /// This is needed as a consequence of using implicit local.gets for 7470b57cec5SDimitry Andric /// uses and implicit local.sets for defs. 7480b57cec5SDimitry Andric bool isOnStack(unsigned Reg) const { 7490b57cec5SDimitry Andric for (const RangeTy &Range : Worklist) 7500b57cec5SDimitry Andric for (const MachineOperand &MO : Range) 7510b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() == Reg) 7520b57cec5SDimitry Andric return true; 7530b57cec5SDimitry Andric return false; 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric }; 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric /// State to keep track of whether commuting is in flight or whether it's been 7580b57cec5SDimitry Andric /// tried for the current instruction and didn't work. 7590b57cec5SDimitry Andric class CommutingState { 7600b57cec5SDimitry Andric /// There are effectively three states: the initial state where we haven't 7610b57cec5SDimitry Andric /// started commuting anything and we don't know anything yet, the tentative 7620b57cec5SDimitry Andric /// state where we've commuted the operands of the current instruction and are 7630b57cec5SDimitry Andric /// revisiting it, and the declined state where we've reverted the operands 7640b57cec5SDimitry Andric /// back to their original order and will no longer commute it further. 7650b57cec5SDimitry Andric bool TentativelyCommuting = false; 7660b57cec5SDimitry Andric bool Declined = false; 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric /// During the tentative state, these hold the operand indices of the commuted 7690b57cec5SDimitry Andric /// operands. 7700b57cec5SDimitry Andric unsigned Operand0, Operand1; 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric public: 7730b57cec5SDimitry Andric /// Stackification for an operand was not successful due to ordering 7740b57cec5SDimitry Andric /// constraints. If possible, and if we haven't already tried it and declined 7750b57cec5SDimitry Andric /// it, commute Insert's operands and prepare to revisit it. 7760b57cec5SDimitry Andric void maybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker, 7770b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII) { 7780b57cec5SDimitry Andric if (TentativelyCommuting) { 7790b57cec5SDimitry Andric assert(!Declined && 7800b57cec5SDimitry Andric "Don't decline commuting until you've finished trying it"); 7810b57cec5SDimitry Andric // Commuting didn't help. Revert it. 7820b57cec5SDimitry Andric TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1); 7830b57cec5SDimitry Andric TentativelyCommuting = false; 7840b57cec5SDimitry Andric Declined = true; 7850b57cec5SDimitry Andric } else if (!Declined && TreeWalker.hasRemainingOperands(Insert)) { 7860b57cec5SDimitry Andric Operand0 = TargetInstrInfo::CommuteAnyOperandIndex; 7870b57cec5SDimitry Andric Operand1 = TargetInstrInfo::CommuteAnyOperandIndex; 7880b57cec5SDimitry Andric if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) { 7890b57cec5SDimitry Andric // Tentatively commute the operands and try again. 7900b57cec5SDimitry Andric TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1); 7910b57cec5SDimitry Andric TreeWalker.resetTopOperands(Insert); 7920b57cec5SDimitry Andric TentativelyCommuting = true; 7930b57cec5SDimitry Andric Declined = false; 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric /// Stackification for some operand was successful. Reset to the default 7990b57cec5SDimitry Andric /// state. 8000b57cec5SDimitry Andric void reset() { 8010b57cec5SDimitry Andric TentativelyCommuting = false; 8020b57cec5SDimitry Andric Declined = false; 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric }; 8050b57cec5SDimitry Andric } // end anonymous namespace 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) { 8080b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Register Stackifying **********\n" 8090b57cec5SDimitry Andric "********** Function: " 8100b57cec5SDimitry Andric << MF.getName() << '\n'); 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andric bool Changed = false; 8130b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 8140b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); 8150b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); 8160b57cec5SDimitry Andric const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); 817*0fca6ea1SDimitry Andric auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); 818*0fca6ea1SDimitry Andric auto &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS(); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric // Walk the instructions from the bottom up. Currently we don't look past 8210b57cec5SDimitry Andric // block boundaries, and the blocks aren't ordered so the block visitation 8220b57cec5SDimitry Andric // order isn't significant, but we may want to change this in the future. 8230b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 8240b57cec5SDimitry Andric // Don't use a range-based for loop, because we modify the list as we're 8250b57cec5SDimitry Andric // iterating over it and the end iterator may change. 8260b57cec5SDimitry Andric for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) { 8270b57cec5SDimitry Andric MachineInstr *Insert = &*MII; 8280b57cec5SDimitry Andric // Don't nest anything inside an inline asm, because we don't have 8290b57cec5SDimitry Andric // constraints for $push inputs. 8300b57cec5SDimitry Andric if (Insert->isInlineAsm()) 8310b57cec5SDimitry Andric continue; 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric // Ignore debugging intrinsics. 8340b57cec5SDimitry Andric if (Insert->isDebugValue()) 8350b57cec5SDimitry Andric continue; 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric // Iterate through the inputs in reverse order, since we'll be pulling 8380b57cec5SDimitry Andric // operands off the stack in LIFO order. 8390b57cec5SDimitry Andric CommutingState Commuting; 8400b57cec5SDimitry Andric TreeWalkerState TreeWalker(Insert); 8410b57cec5SDimitry Andric while (!TreeWalker.done()) { 8425ffd83dbSDimitry Andric MachineOperand &Use = TreeWalker.pop(); 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric // We're only interested in explicit virtual register operands. 8455ffd83dbSDimitry Andric if (!Use.isReg()) 8460b57cec5SDimitry Andric continue; 8470b57cec5SDimitry Andric 8485ffd83dbSDimitry Andric Register Reg = Use.getReg(); 8495ffd83dbSDimitry Andric assert(Use.isUse() && "explicit_uses() should only iterate over uses"); 8505ffd83dbSDimitry Andric assert(!Use.isImplicit() && 8510b57cec5SDimitry Andric "explicit_uses() should only iterate over explicit operands"); 852bdd1243dSDimitry Andric if (Reg.isPhysical()) 8530b57cec5SDimitry Andric continue; 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric // Identify the definition for this register at this point. 8565ffd83dbSDimitry Andric MachineInstr *DefI = getVRegDef(Reg, Insert, MRI, LIS); 8575ffd83dbSDimitry Andric if (!DefI) 8580b57cec5SDimitry Andric continue; 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric // Don't nest an INLINE_ASM def into anything, because we don't have 8610b57cec5SDimitry Andric // constraints for $pop outputs. 8625ffd83dbSDimitry Andric if (DefI->isInlineAsm()) 8630b57cec5SDimitry Andric continue; 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric // Argument instructions represent live-in registers and not real 8660b57cec5SDimitry Andric // instructions. 8675ffd83dbSDimitry Andric if (WebAssembly::isArgument(DefI->getOpcode())) 8680b57cec5SDimitry Andric continue; 8690b57cec5SDimitry Andric 870*0fca6ea1SDimitry Andric MachineOperand *Def = 871*0fca6ea1SDimitry Andric DefI->findRegisterDefOperand(Reg, /*TRI=*/nullptr); 8725ffd83dbSDimitry Andric assert(Def != nullptr); 8735ffd83dbSDimitry Andric 8740b57cec5SDimitry Andric // Decide which strategy to take. Prefer to move a single-use value 8750b57cec5SDimitry Andric // over cloning it, and prefer cloning over introducing a tee. 8760b57cec5SDimitry Andric // For moving, we require the def to be in the same block as the use; 8770b57cec5SDimitry Andric // this makes things simpler (LiveIntervals' handleMove function only 8780b57cec5SDimitry Andric // supports intra-block moves) and it's MachineSink's job to catch all 8790b57cec5SDimitry Andric // the sinking opportunities anyway. 8805ffd83dbSDimitry Andric bool SameBlock = DefI->getParent() == &MBB; 881fcaf7f86SDimitry Andric bool CanMove = SameBlock && isSafeToMove(Def, &Use, Insert, MFI, MRI) && 8820b57cec5SDimitry Andric !TreeWalker.isOnStack(Reg); 88306c3fb27SDimitry Andric if (CanMove && hasOneNonDBGUse(Reg, DefI, MRI, MDT, LIS)) { 8845ffd83dbSDimitry Andric Insert = moveForSingleUse(Reg, Use, DefI, MBB, Insert, LIS, MFI, MRI); 8855ffd83dbSDimitry Andric 8865ffd83dbSDimitry Andric // If we are removing the frame base reg completely, remove the debug 8875ffd83dbSDimitry Andric // info as well. 8885ffd83dbSDimitry Andric // TODO: Encode this properly as a stackified value. 8895ffd83dbSDimitry Andric if (MFI.isFrameBaseVirtual() && MFI.getFrameBaseVreg() == Reg) 8905ffd83dbSDimitry Andric MFI.clearFrameBaseVreg(); 891fcaf7f86SDimitry Andric } else if (shouldRematerialize(*DefI, TII)) { 8920b57cec5SDimitry Andric Insert = 8935ffd83dbSDimitry Andric rematerializeCheapDef(Reg, Use, *DefI, MBB, Insert->getIterator(), 8940b57cec5SDimitry Andric LIS, MFI, MRI, TII, TRI); 8955ffd83dbSDimitry Andric } else if (CanMove && oneUseDominatesOtherUses(Reg, Use, MBB, MRI, MDT, 8965ffd83dbSDimitry Andric LIS, MFI)) { 8975ffd83dbSDimitry Andric Insert = moveAndTeeForMultiUse(Reg, Use, DefI, MBB, Insert, LIS, MFI, 8980b57cec5SDimitry Andric MRI, TII); 8990b57cec5SDimitry Andric } else { 9000b57cec5SDimitry Andric // We failed to stackify the operand. If the problem was ordering 9010b57cec5SDimitry Andric // constraints, Commuting may be able to help. 9020b57cec5SDimitry Andric if (!CanMove && SameBlock) 9030b57cec5SDimitry Andric Commuting.maybeCommute(Insert, TreeWalker, TII); 9040b57cec5SDimitry Andric // Proceed to the next operand. 9050b57cec5SDimitry Andric continue; 9060b57cec5SDimitry Andric } 9070b57cec5SDimitry Andric 9085ffd83dbSDimitry Andric // Stackifying a multivalue def may unlock in-place stackification of 9095ffd83dbSDimitry Andric // subsequent defs. TODO: Handle the case where the consecutive uses are 9105ffd83dbSDimitry Andric // not all in the same instruction. 9115ffd83dbSDimitry Andric auto *SubsequentDef = Insert->defs().begin(); 9125ffd83dbSDimitry Andric auto *SubsequentUse = &Use; 9135ffd83dbSDimitry Andric while (SubsequentDef != Insert->defs().end() && 9145ffd83dbSDimitry Andric SubsequentUse != Use.getParent()->uses().end()) { 9155ffd83dbSDimitry Andric if (!SubsequentDef->isReg() || !SubsequentUse->isReg()) 9165ffd83dbSDimitry Andric break; 91704eeddc0SDimitry Andric Register DefReg = SubsequentDef->getReg(); 91804eeddc0SDimitry Andric Register UseReg = SubsequentUse->getReg(); 9195ffd83dbSDimitry Andric // TODO: This single-use restriction could be relaxed by using tees 92006c3fb27SDimitry Andric if (DefReg != UseReg || !MRI.hasOneNonDBGUse(DefReg)) 9215ffd83dbSDimitry Andric break; 9225ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, DefReg); 9235ffd83dbSDimitry Andric ++SubsequentDef; 9245ffd83dbSDimitry Andric ++SubsequentUse; 9255ffd83dbSDimitry Andric } 9265ffd83dbSDimitry Andric 9270b57cec5SDimitry Andric // If the instruction we just stackified is an IMPLICIT_DEF, convert it 9280b57cec5SDimitry Andric // to a constant 0 so that the def is explicit, and the push/pop 9290b57cec5SDimitry Andric // correspondence is maintained. 9300b57cec5SDimitry Andric if (Insert->getOpcode() == TargetOpcode::IMPLICIT_DEF) 9310b57cec5SDimitry Andric convertImplicitDefToConstZero(Insert, MRI, TII, MF, LIS); 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric // We stackified an operand. Add the defining instruction's operands to 9340b57cec5SDimitry Andric // the worklist stack now to continue to build an ever deeper tree. 9350b57cec5SDimitry Andric Commuting.reset(); 9360b57cec5SDimitry Andric TreeWalker.pushOperands(Insert); 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // If we stackified any operands, skip over the tree to start looking for 9400b57cec5SDimitry Andric // the next instruction we can build a tree on. 9410b57cec5SDimitry Andric if (Insert != &*MII) { 9420b57cec5SDimitry Andric imposeStackOrdering(&*MII); 9430b57cec5SDimitry Andric MII = MachineBasicBlock::iterator(Insert).getReverse(); 9440b57cec5SDimitry Andric Changed = true; 9450b57cec5SDimitry Andric } 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric } 9480b57cec5SDimitry Andric 9490b57cec5SDimitry Andric // If we used VALUE_STACK anywhere, add it to the live-in sets everywhere so 9500b57cec5SDimitry Andric // that it never looks like a use-before-def. 9510b57cec5SDimitry Andric if (Changed) { 9520b57cec5SDimitry Andric MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK); 9530b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) 9540b57cec5SDimitry Andric MBB.addLiveIn(WebAssembly::VALUE_STACK); 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric #ifndef NDEBUG 9580b57cec5SDimitry Andric // Verify that pushes and pops are performed in LIFO order. 9590b57cec5SDimitry Andric SmallVector<unsigned, 0> Stack; 9600b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 9610b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 9620b57cec5SDimitry Andric if (MI.isDebugInstr()) 9630b57cec5SDimitry Andric continue; 9645ffd83dbSDimitry Andric for (MachineOperand &MO : reverse(MI.explicit_uses())) { 9650b57cec5SDimitry Andric if (!MO.isReg()) 9660b57cec5SDimitry Andric continue; 9678bcb0991SDimitry Andric Register Reg = MO.getReg(); 9685ffd83dbSDimitry Andric if (MFI.isVRegStackified(Reg)) 9690b57cec5SDimitry Andric assert(Stack.pop_back_val() == Reg && 9700b57cec5SDimitry Andric "Register stack pop should be paired with a push"); 9710b57cec5SDimitry Andric } 9725ffd83dbSDimitry Andric for (MachineOperand &MO : MI.defs()) { 9735ffd83dbSDimitry Andric if (!MO.isReg()) 9745ffd83dbSDimitry Andric continue; 9755ffd83dbSDimitry Andric Register Reg = MO.getReg(); 9765ffd83dbSDimitry Andric if (MFI.isVRegStackified(Reg)) 9775ffd83dbSDimitry Andric Stack.push_back(MO.getReg()); 9780b57cec5SDimitry Andric } 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric // TODO: Generalize this code to support keeping values on the stack across 9810b57cec5SDimitry Andric // basic block boundaries. 9820b57cec5SDimitry Andric assert(Stack.empty() && 9830b57cec5SDimitry Andric "Register stack pushes and pops should be balanced"); 9840b57cec5SDimitry Andric } 9850b57cec5SDimitry Andric #endif 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric return Changed; 9880b57cec5SDimitry Andric } 989