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_*
23*5f7ddb14SDimitry Andric #include "Utils/WebAssemblyUtilities.h"
240b57cec5SDimitry Andric #include "WebAssembly.h"
250b57cec5SDimitry Andric #include "WebAssemblyDebugValueManager.h"
260b57cec5SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
270b57cec5SDimitry Andric #include "WebAssemblySubtarget.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
290b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
370b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
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 {
getPassName() const460b57cec5SDimitry Andric StringRef getPassName() const override {
470b57cec5SDimitry Andric return "WebAssembly Register Stackify";
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const500b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
510b57cec5SDimitry Andric AU.setPreservesCFG();
520b57cec5SDimitry Andric AU.addRequired<AAResultsWrapperPass>();
530b57cec5SDimitry Andric AU.addRequired<MachineDominatorTree>();
540b57cec5SDimitry Andric AU.addRequired<LiveIntervals>();
550b57cec5SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfo>();
560b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>();
570b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>();
580b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID);
590b57cec5SDimitry Andric AU.addPreserved<MachineDominatorTree>();
600b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric public:
660b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid
WebAssemblyRegStackify()670b57cec5SDimitry Andric WebAssemblyRegStackify() : MachineFunctionPass(ID) {}
680b57cec5SDimitry Andric };
690b57cec5SDimitry Andric } // end anonymous namespace
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric char WebAssemblyRegStackify::ID = 0;
720b57cec5SDimitry Andric INITIALIZE_PASS(WebAssemblyRegStackify, DEBUG_TYPE,
730b57cec5SDimitry Andric "Reorder instructions to use the WebAssembly value stack",
740b57cec5SDimitry Andric false, false)
750b57cec5SDimitry Andric
createWebAssemblyRegStackify()760b57cec5SDimitry Andric FunctionPass *llvm::createWebAssemblyRegStackify() {
770b57cec5SDimitry Andric return new WebAssemblyRegStackify();
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric // Decorate the given instruction with implicit operands that enforce the
810b57cec5SDimitry Andric // expression stack ordering constraints for an instruction which is on
820b57cec5SDimitry Andric // the expression stack.
imposeStackOrdering(MachineInstr * MI)830b57cec5SDimitry Andric static void imposeStackOrdering(MachineInstr *MI) {
840b57cec5SDimitry Andric // Write the opaque VALUE_STACK register.
850b57cec5SDimitry Andric if (!MI->definesRegister(WebAssembly::VALUE_STACK))
860b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK,
870b57cec5SDimitry Andric /*isDef=*/true,
880b57cec5SDimitry Andric /*isImp=*/true));
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // Also read the opaque VALUE_STACK register.
910b57cec5SDimitry Andric if (!MI->readsRegister(WebAssembly::VALUE_STACK))
920b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK,
930b57cec5SDimitry Andric /*isDef=*/false,
940b57cec5SDimitry Andric /*isImp=*/true));
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric // Convert an IMPLICIT_DEF instruction into an instruction which defines
980b57cec5SDimitry Andric // a constant zero value.
convertImplicitDefToConstZero(MachineInstr * MI,MachineRegisterInfo & MRI,const TargetInstrInfo * TII,MachineFunction & MF,LiveIntervals & LIS)990b57cec5SDimitry Andric static void convertImplicitDefToConstZero(MachineInstr *MI,
1000b57cec5SDimitry Andric MachineRegisterInfo &MRI,
1010b57cec5SDimitry Andric const TargetInstrInfo *TII,
1020b57cec5SDimitry Andric MachineFunction &MF,
1030b57cec5SDimitry Andric LiveIntervals &LIS) {
1040b57cec5SDimitry Andric assert(MI->getOpcode() == TargetOpcode::IMPLICIT_DEF);
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric const auto *RegClass = MRI.getRegClass(MI->getOperand(0).getReg());
1070b57cec5SDimitry Andric if (RegClass == &WebAssembly::I32RegClass) {
1080b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_I32));
1090b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0));
1100b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::I64RegClass) {
1110b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_I64));
1120b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0));
1130b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::F32RegClass) {
1140b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_F32));
1150b57cec5SDimitry Andric auto *Val = cast<ConstantFP>(Constant::getNullValue(
1160b57cec5SDimitry Andric Type::getFloatTy(MF.getFunction().getContext())));
1170b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateFPImm(Val));
1180b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::F64RegClass) {
1190b57cec5SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_F64));
1200b57cec5SDimitry Andric auto *Val = cast<ConstantFP>(Constant::getNullValue(
1210b57cec5SDimitry Andric Type::getDoubleTy(MF.getFunction().getContext())));
1220b57cec5SDimitry Andric MI->addOperand(MachineOperand::CreateFPImm(Val));
1230b57cec5SDimitry Andric } else if (RegClass == &WebAssembly::V128RegClass) {
124*5f7ddb14SDimitry Andric MI->setDesc(TII->get(WebAssembly::CONST_V128_I64x2));
125*5f7ddb14SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0));
126*5f7ddb14SDimitry Andric MI->addOperand(MachineOperand::CreateImm(0));
1270b57cec5SDimitry Andric } else {
1280b57cec5SDimitry Andric llvm_unreachable("Unexpected reg class");
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric // Determine whether a call to the callee referenced by
1330b57cec5SDimitry Andric // MI->getOperand(CalleeOpNo) reads memory, writes memory, and/or has side
1340b57cec5SDimitry Andric // effects.
queryCallee(const MachineInstr & MI,bool & Read,bool & Write,bool & Effects,bool & StackPointer)1355ffd83dbSDimitry Andric static void queryCallee(const MachineInstr &MI, bool &Read, bool &Write,
1365ffd83dbSDimitry Andric bool &Effects, bool &StackPointer) {
1370b57cec5SDimitry Andric // All calls can use the stack pointer.
1380b57cec5SDimitry Andric StackPointer = true;
1390b57cec5SDimitry Andric
1405ffd83dbSDimitry Andric const MachineOperand &MO = WebAssembly::getCalleeOp(MI);
1410b57cec5SDimitry Andric if (MO.isGlobal()) {
1420b57cec5SDimitry Andric const Constant *GV = MO.getGlobal();
1430b57cec5SDimitry Andric if (const auto *GA = dyn_cast<GlobalAlias>(GV))
1440b57cec5SDimitry Andric if (!GA->isInterposable())
1450b57cec5SDimitry Andric GV = GA->getAliasee();
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric if (const auto *F = dyn_cast<Function>(GV)) {
1480b57cec5SDimitry Andric if (!F->doesNotThrow())
1490b57cec5SDimitry Andric Effects = true;
1500b57cec5SDimitry Andric if (F->doesNotAccessMemory())
1510b57cec5SDimitry Andric return;
1520b57cec5SDimitry Andric if (F->onlyReadsMemory()) {
1530b57cec5SDimitry Andric Read = true;
1540b57cec5SDimitry Andric return;
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric // Assume the worst.
1600b57cec5SDimitry Andric Write = true;
1610b57cec5SDimitry Andric Read = true;
1620b57cec5SDimitry Andric Effects = true;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric // Determine whether MI reads memory, writes memory, has side effects,
1660b57cec5SDimitry Andric // and/or uses the stack pointer value.
query(const MachineInstr & MI,AliasAnalysis & AA,bool & Read,bool & Write,bool & Effects,bool & StackPointer)1670b57cec5SDimitry Andric static void query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read,
1680b57cec5SDimitry Andric bool &Write, bool &Effects, bool &StackPointer) {
1690b57cec5SDimitry Andric assert(!MI.isTerminator());
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric if (MI.isDebugInstr() || MI.isPosition())
1720b57cec5SDimitry Andric return;
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric // Check for loads.
1750b57cec5SDimitry Andric if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(&AA))
1760b57cec5SDimitry Andric Read = true;
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric // Check for stores.
1790b57cec5SDimitry Andric if (MI.mayStore()) {
1800b57cec5SDimitry Andric Write = true;
1810b57cec5SDimitry Andric } else if (MI.hasOrderedMemoryRef()) {
1820b57cec5SDimitry Andric switch (MI.getOpcode()) {
1830b57cec5SDimitry Andric case WebAssembly::DIV_S_I32:
1840b57cec5SDimitry Andric case WebAssembly::DIV_S_I64:
1850b57cec5SDimitry Andric case WebAssembly::REM_S_I32:
1860b57cec5SDimitry Andric case WebAssembly::REM_S_I64:
1870b57cec5SDimitry Andric case WebAssembly::DIV_U_I32:
1880b57cec5SDimitry Andric case WebAssembly::DIV_U_I64:
1890b57cec5SDimitry Andric case WebAssembly::REM_U_I32:
1900b57cec5SDimitry Andric case WebAssembly::REM_U_I64:
1910b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F32:
1920b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F32:
1930b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F64:
1940b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F64:
1950b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F32:
1960b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F32:
1970b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F64:
1980b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F64:
1990b57cec5SDimitry Andric // These instruction have hasUnmodeledSideEffects() returning true
2000b57cec5SDimitry Andric // because they trap on overflow and invalid so they can't be arbitrarily
2010b57cec5SDimitry Andric // moved, however hasOrderedMemoryRef() interprets this plus their lack
2020b57cec5SDimitry Andric // of memoperands as having a potential unknown memory reference.
2030b57cec5SDimitry Andric break;
2040b57cec5SDimitry Andric default:
2050b57cec5SDimitry Andric // Record volatile accesses, unless it's a call, as calls are handled
2060b57cec5SDimitry Andric // specially below.
2070b57cec5SDimitry Andric if (!MI.isCall()) {
2080b57cec5SDimitry Andric Write = true;
2090b57cec5SDimitry Andric Effects = true;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric break;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric // Check for side effects.
2160b57cec5SDimitry Andric if (MI.hasUnmodeledSideEffects()) {
2170b57cec5SDimitry Andric switch (MI.getOpcode()) {
2180b57cec5SDimitry Andric case WebAssembly::DIV_S_I32:
2190b57cec5SDimitry Andric case WebAssembly::DIV_S_I64:
2200b57cec5SDimitry Andric case WebAssembly::REM_S_I32:
2210b57cec5SDimitry Andric case WebAssembly::REM_S_I64:
2220b57cec5SDimitry Andric case WebAssembly::DIV_U_I32:
2230b57cec5SDimitry Andric case WebAssembly::DIV_U_I64:
2240b57cec5SDimitry Andric case WebAssembly::REM_U_I32:
2250b57cec5SDimitry Andric case WebAssembly::REM_U_I64:
2260b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F32:
2270b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F32:
2280b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_S_F64:
2290b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_S_F64:
2300b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F32:
2310b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F32:
2320b57cec5SDimitry Andric case WebAssembly::I32_TRUNC_U_F64:
2330b57cec5SDimitry Andric case WebAssembly::I64_TRUNC_U_F64:
2340b57cec5SDimitry Andric // These instructions have hasUnmodeledSideEffects() returning true
2350b57cec5SDimitry Andric // because they trap on overflow and invalid so they can't be arbitrarily
2360b57cec5SDimitry Andric // moved, however in the specific case of register stackifying, it is safe
2370b57cec5SDimitry Andric // to move them because overflow and invalid are Undefined Behavior.
2380b57cec5SDimitry Andric break;
2390b57cec5SDimitry Andric default:
2400b57cec5SDimitry Andric Effects = true;
2410b57cec5SDimitry Andric break;
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric // Check for writes to __stack_pointer global.
2465ffd83dbSDimitry Andric if ((MI.getOpcode() == WebAssembly::GLOBAL_SET_I32 ||
2475ffd83dbSDimitry Andric MI.getOpcode() == WebAssembly::GLOBAL_SET_I64) &&
2480b57cec5SDimitry Andric strcmp(MI.getOperand(0).getSymbolName(), "__stack_pointer") == 0)
2490b57cec5SDimitry Andric StackPointer = true;
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric // Analyze calls.
2520b57cec5SDimitry Andric if (MI.isCall()) {
2535ffd83dbSDimitry Andric queryCallee(MI, Read, Write, Effects, StackPointer);
2540b57cec5SDimitry Andric }
2550b57cec5SDimitry Andric }
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // Test whether Def is safe and profitable to rematerialize.
shouldRematerialize(const MachineInstr & Def,AliasAnalysis & AA,const WebAssemblyInstrInfo * TII)2580b57cec5SDimitry Andric static bool shouldRematerialize(const MachineInstr &Def, AliasAnalysis &AA,
2590b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII) {
2600b57cec5SDimitry Andric return Def.isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def, &AA);
2610b57cec5SDimitry Andric }
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andric // Identify the definition for this register at this point. This is a
2640b57cec5SDimitry Andric // generalization of MachineRegisterInfo::getUniqueVRegDef that uses
2650b57cec5SDimitry Andric // LiveIntervals to handle complex cases.
getVRegDef(unsigned Reg,const MachineInstr * Insert,const MachineRegisterInfo & MRI,const LiveIntervals & LIS)2660b57cec5SDimitry Andric static MachineInstr *getVRegDef(unsigned Reg, const MachineInstr *Insert,
2670b57cec5SDimitry Andric const MachineRegisterInfo &MRI,
2680b57cec5SDimitry Andric const LiveIntervals &LIS) {
2690b57cec5SDimitry Andric // Most registers are in SSA form here so we try a quick MRI query first.
2700b57cec5SDimitry Andric if (MachineInstr *Def = MRI.getUniqueVRegDef(Reg))
2710b57cec5SDimitry Andric return Def;
2720b57cec5SDimitry Andric
2730b57cec5SDimitry Andric // MRI doesn't know what the Def is. Try asking LIS.
2740b57cec5SDimitry Andric if (const VNInfo *ValNo = LIS.getInterval(Reg).getVNInfoBefore(
2750b57cec5SDimitry Andric LIS.getInstructionIndex(*Insert)))
2760b57cec5SDimitry Andric return LIS.getInstructionFromIndex(ValNo->def);
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andric return nullptr;
2790b57cec5SDimitry Andric }
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andric // Test whether Reg, as defined at Def, has exactly one use. This is a
2820b57cec5SDimitry Andric // generalization of MachineRegisterInfo::hasOneUse that uses LiveIntervals
2830b57cec5SDimitry Andric // to handle complex cases.
hasOneUse(unsigned Reg,MachineInstr * Def,MachineRegisterInfo & MRI,MachineDominatorTree & MDT,LiveIntervals & LIS)2840b57cec5SDimitry Andric static bool hasOneUse(unsigned Reg, MachineInstr *Def, MachineRegisterInfo &MRI,
2850b57cec5SDimitry Andric MachineDominatorTree &MDT, LiveIntervals &LIS) {
2860b57cec5SDimitry Andric // Most registers are in SSA form here so we try a quick MRI query first.
2870b57cec5SDimitry Andric if (MRI.hasOneUse(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.
isSafeToMove(const MachineOperand * Def,const MachineOperand * Use,const MachineInstr * Insert,AliasAnalysis & AA,const WebAssemblyFunctionInfo & MFI,const MachineRegisterInfo & MRI)3135ffd83dbSDimitry Andric static bool isSafeToMove(const MachineOperand *Def, const MachineOperand *Use,
3145ffd83dbSDimitry Andric const MachineInstr *Insert, AliasAnalysis &AA,
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.
340af732203SDimitry Andric for (const auto &SubsequentDef : drop_begin(DefI->defs())) {
3415ffd83dbSDimitry Andric for (const auto &PriorUse : UseI->uses()) {
3425ffd83dbSDimitry Andric if (&PriorUse == Use)
3435ffd83dbSDimitry Andric break;
3445ffd83dbSDimitry Andric if (PriorUse.isReg() && SubsequentDef.getReg() == PriorUse.getReg())
3455ffd83dbSDimitry Andric return false;
3465ffd83dbSDimitry Andric }
3475ffd83dbSDimitry Andric }
3485ffd83dbSDimitry Andric
3495ffd83dbSDimitry Andric // If moving is a semantic nop, it is always allowed
3505ffd83dbSDimitry Andric const MachineBasicBlock *MBB = DefI->getParent();
3515ffd83dbSDimitry Andric auto NextI = std::next(MachineBasicBlock::const_iterator(DefI));
3525ffd83dbSDimitry Andric for (auto E = MBB->end(); NextI != E && NextI->isDebugInstr(); ++NextI)
3535ffd83dbSDimitry Andric ;
3545ffd83dbSDimitry Andric if (NextI == Insert)
3555ffd83dbSDimitry Andric return true;
3560b57cec5SDimitry Andric
357af732203SDimitry Andric // 'catch' and 'catch_all' should be the first instruction of a BB and cannot
358af732203SDimitry Andric // move.
359af732203SDimitry Andric if (WebAssembly::isCatch(DefI->getOpcode()))
3600b57cec5SDimitry Andric return false;
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric // Check for register dependencies.
3630b57cec5SDimitry Andric SmallVector<unsigned, 4> MutableRegisters;
3645ffd83dbSDimitry Andric for (const MachineOperand &MO : DefI->operands()) {
3650b57cec5SDimitry Andric if (!MO.isReg() || MO.isUndef())
3660b57cec5SDimitry Andric continue;
3678bcb0991SDimitry Andric Register Reg = MO.getReg();
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric // If the register is dead here and at Insert, ignore it.
3700b57cec5SDimitry Andric if (MO.isDead() && Insert->definesRegister(Reg) &&
3710b57cec5SDimitry Andric !Insert->readsRegister(Reg))
3720b57cec5SDimitry Andric continue;
3730b57cec5SDimitry Andric
3748bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) {
3750b57cec5SDimitry Andric // Ignore ARGUMENTS; it's just used to keep the ARGUMENT_* instructions
3760b57cec5SDimitry Andric // from moving down, and we've already checked for that.
3770b57cec5SDimitry Andric if (Reg == WebAssembly::ARGUMENTS)
3780b57cec5SDimitry Andric continue;
3790b57cec5SDimitry Andric // If the physical register is never modified, ignore it.
3800b57cec5SDimitry Andric if (!MRI.isPhysRegModified(Reg))
3810b57cec5SDimitry Andric continue;
3820b57cec5SDimitry Andric // Otherwise, it's a physical register with unknown liveness.
3830b57cec5SDimitry Andric return false;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric // If one of the operands isn't in SSA form, it has different values at
3870b57cec5SDimitry Andric // different times, and we need to make sure we don't move our use across
3880b57cec5SDimitry Andric // a different def.
3890b57cec5SDimitry Andric if (!MO.isDef() && !MRI.hasOneDef(Reg))
3900b57cec5SDimitry Andric MutableRegisters.push_back(Reg);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric bool Read = false, Write = false, Effects = false, StackPointer = false;
3945ffd83dbSDimitry Andric query(*DefI, AA, Read, Write, Effects, StackPointer);
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric // If the instruction does not access memory and has no side effects, it has
3970b57cec5SDimitry Andric // no additional dependencies.
3980b57cec5SDimitry Andric bool HasMutableRegisters = !MutableRegisters.empty();
3990b57cec5SDimitry Andric if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters)
4000b57cec5SDimitry Andric return true;
4010b57cec5SDimitry Andric
4025ffd83dbSDimitry Andric // Scan through the intervening instructions between DefI and Insert.
4035ffd83dbSDimitry Andric MachineBasicBlock::const_iterator D(DefI), I(Insert);
4040b57cec5SDimitry Andric for (--I; I != D; --I) {
4050b57cec5SDimitry Andric bool InterveningRead = false;
4060b57cec5SDimitry Andric bool InterveningWrite = false;
4070b57cec5SDimitry Andric bool InterveningEffects = false;
4080b57cec5SDimitry Andric bool InterveningStackPointer = false;
4090b57cec5SDimitry Andric query(*I, AA, InterveningRead, InterveningWrite, InterveningEffects,
4100b57cec5SDimitry Andric InterveningStackPointer);
4110b57cec5SDimitry Andric if (Effects && InterveningEffects)
4120b57cec5SDimitry Andric return false;
4130b57cec5SDimitry Andric if (Read && InterveningWrite)
4140b57cec5SDimitry Andric return false;
4150b57cec5SDimitry Andric if (Write && (InterveningRead || InterveningWrite))
4160b57cec5SDimitry Andric return false;
4170b57cec5SDimitry Andric if (StackPointer && InterveningStackPointer)
4180b57cec5SDimitry Andric return false;
4190b57cec5SDimitry Andric
4200b57cec5SDimitry Andric for (unsigned Reg : MutableRegisters)
4210b57cec5SDimitry Andric for (const MachineOperand &MO : I->operands())
4220b57cec5SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == Reg)
4230b57cec5SDimitry Andric return false;
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric
4260b57cec5SDimitry Andric return true;
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andric /// Test whether OneUse, a use of Reg, dominates all of Reg's other uses.
oneUseDominatesOtherUses(unsigned Reg,const MachineOperand & OneUse,const MachineBasicBlock & MBB,const MachineRegisterInfo & MRI,const MachineDominatorTree & MDT,LiveIntervals & LIS,WebAssemblyFunctionInfo & MFI)4300b57cec5SDimitry Andric static bool oneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse,
4310b57cec5SDimitry Andric const MachineBasicBlock &MBB,
4320b57cec5SDimitry Andric const MachineRegisterInfo &MRI,
4330b57cec5SDimitry Andric const MachineDominatorTree &MDT,
4340b57cec5SDimitry Andric LiveIntervals &LIS,
4350b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI) {
4360b57cec5SDimitry Andric const LiveInterval &LI = LIS.getInterval(Reg);
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric const MachineInstr *OneUseInst = OneUse.getParent();
4390b57cec5SDimitry Andric VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst));
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric for (const MachineOperand &Use : MRI.use_nodbg_operands(Reg)) {
4420b57cec5SDimitry Andric if (&Use == &OneUse)
4430b57cec5SDimitry Andric continue;
4440b57cec5SDimitry Andric
4450b57cec5SDimitry Andric const MachineInstr *UseInst = Use.getParent();
4460b57cec5SDimitry Andric VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst));
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric if (UseVNI != OneUseVNI)
4490b57cec5SDimitry Andric continue;
4500b57cec5SDimitry Andric
4510b57cec5SDimitry Andric if (UseInst == OneUseInst) {
4520b57cec5SDimitry Andric // Another use in the same instruction. We need to ensure that the one
4530b57cec5SDimitry Andric // selected use happens "before" it.
4540b57cec5SDimitry Andric if (&OneUse > &Use)
4550b57cec5SDimitry Andric return false;
4560b57cec5SDimitry Andric } else {
4570b57cec5SDimitry Andric // Test that the use is dominated by the one selected use.
4580b57cec5SDimitry Andric while (!MDT.dominates(OneUseInst, UseInst)) {
4590b57cec5SDimitry Andric // Actually, dominating is over-conservative. Test that the use would
4600b57cec5SDimitry Andric // happen after the one selected use in the stack evaluation order.
4610b57cec5SDimitry Andric //
4620b57cec5SDimitry Andric // This is needed as a consequence of using implicit local.gets for
4630b57cec5SDimitry Andric // uses and implicit local.sets for defs.
4640b57cec5SDimitry Andric if (UseInst->getDesc().getNumDefs() == 0)
4650b57cec5SDimitry Andric return false;
4660b57cec5SDimitry Andric const MachineOperand &MO = UseInst->getOperand(0);
4670b57cec5SDimitry Andric if (!MO.isReg())
4680b57cec5SDimitry Andric return false;
4698bcb0991SDimitry Andric Register DefReg = MO.getReg();
4708bcb0991SDimitry Andric if (!Register::isVirtualRegister(DefReg) ||
4710b57cec5SDimitry Andric !MFI.isVRegStackified(DefReg))
4720b57cec5SDimitry Andric return false;
4730b57cec5SDimitry Andric assert(MRI.hasOneNonDBGUse(DefReg));
4740b57cec5SDimitry Andric const MachineOperand &NewUse = *MRI.use_nodbg_begin(DefReg);
4750b57cec5SDimitry Andric const MachineInstr *NewUseInst = NewUse.getParent();
4760b57cec5SDimitry Andric if (NewUseInst == OneUseInst) {
4770b57cec5SDimitry Andric if (&OneUse > &NewUse)
4780b57cec5SDimitry Andric return false;
4790b57cec5SDimitry Andric break;
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric UseInst = NewUseInst;
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric }
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric return true;
4860b57cec5SDimitry Andric }
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andric /// Get the appropriate tee opcode for the given register class.
getTeeOpcode(const TargetRegisterClass * RC)4890b57cec5SDimitry Andric static unsigned getTeeOpcode(const TargetRegisterClass *RC) {
4900b57cec5SDimitry Andric if (RC == &WebAssembly::I32RegClass)
4910b57cec5SDimitry Andric return WebAssembly::TEE_I32;
4920b57cec5SDimitry Andric if (RC == &WebAssembly::I64RegClass)
4930b57cec5SDimitry Andric return WebAssembly::TEE_I64;
4940b57cec5SDimitry Andric if (RC == &WebAssembly::F32RegClass)
4950b57cec5SDimitry Andric return WebAssembly::TEE_F32;
4960b57cec5SDimitry Andric if (RC == &WebAssembly::F64RegClass)
4970b57cec5SDimitry Andric return WebAssembly::TEE_F64;
4980b57cec5SDimitry Andric if (RC == &WebAssembly::V128RegClass)
4990b57cec5SDimitry Andric return WebAssembly::TEE_V128;
5000b57cec5SDimitry Andric llvm_unreachable("Unexpected register class");
5010b57cec5SDimitry Andric }
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric // Shrink LI to its uses, cleaning up LI.
shrinkToUses(LiveInterval & LI,LiveIntervals & LIS)5040b57cec5SDimitry Andric static void shrinkToUses(LiveInterval &LI, LiveIntervals &LIS) {
5050b57cec5SDimitry Andric if (LIS.shrinkToUses(&LI)) {
5060b57cec5SDimitry Andric SmallVector<LiveInterval *, 4> SplitLIs;
5070b57cec5SDimitry Andric LIS.splitSeparateComponents(LI, SplitLIs);
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric
5110b57cec5SDimitry Andric /// A single-use def in the same block with no intervening memory or register
5120b57cec5SDimitry Andric /// dependencies; move the def down and nest it with the current instruction.
moveForSingleUse(unsigned Reg,MachineOperand & Op,MachineInstr * Def,MachineBasicBlock & MBB,MachineInstr * Insert,LiveIntervals & LIS,WebAssemblyFunctionInfo & MFI,MachineRegisterInfo & MRI)5130b57cec5SDimitry Andric static MachineInstr *moveForSingleUse(unsigned Reg, MachineOperand &Op,
5140b57cec5SDimitry Andric MachineInstr *Def, MachineBasicBlock &MBB,
5150b57cec5SDimitry Andric MachineInstr *Insert, LiveIntervals &LIS,
5160b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI,
5170b57cec5SDimitry Andric MachineRegisterInfo &MRI) {
5180b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Move for single use: "; Def->dump());
5190b57cec5SDimitry Andric
5200b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(Def);
5210b57cec5SDimitry Andric MBB.splice(Insert, &MBB, Def);
5220b57cec5SDimitry Andric DefDIs.move(Insert);
5230b57cec5SDimitry Andric LIS.handleMove(*Def);
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric if (MRI.hasOneDef(Reg) && MRI.hasOneUse(Reg)) {
5260b57cec5SDimitry Andric // No one else is using this register for anything so we can just stackify
5270b57cec5SDimitry Andric // it in place.
5285ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, Reg);
5290b57cec5SDimitry Andric } else {
5300b57cec5SDimitry Andric // The register may have unrelated uses or defs; create a new register for
5310b57cec5SDimitry Andric // just our one def and use so that we can stackify it.
5328bcb0991SDimitry Andric Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
5330b57cec5SDimitry Andric Def->getOperand(0).setReg(NewReg);
5340b57cec5SDimitry Andric Op.setReg(NewReg);
5350b57cec5SDimitry Andric
5360b57cec5SDimitry Andric // Tell LiveIntervals about the new register.
5370b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(NewReg);
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // Tell LiveIntervals about the changes to the old register.
5400b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
5410b57cec5SDimitry Andric LI.removeSegment(LIS.getInstructionIndex(*Def).getRegSlot(),
5420b57cec5SDimitry Andric LIS.getInstructionIndex(*Op.getParent()).getRegSlot(),
5430b57cec5SDimitry Andric /*RemoveDeadValNo=*/true);
5440b57cec5SDimitry Andric
5455ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, NewReg);
5460b57cec5SDimitry Andric
5470b57cec5SDimitry Andric DefDIs.updateReg(NewReg);
5480b57cec5SDimitry Andric
5490b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump());
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric imposeStackOrdering(Def);
5530b57cec5SDimitry Andric return Def;
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric /// A trivially cloneable instruction; clone it and nest the new copy with the
5570b57cec5SDimitry Andric /// current instruction.
rematerializeCheapDef(unsigned Reg,MachineOperand & Op,MachineInstr & Def,MachineBasicBlock & MBB,MachineBasicBlock::instr_iterator Insert,LiveIntervals & LIS,WebAssemblyFunctionInfo & MFI,MachineRegisterInfo & MRI,const WebAssemblyInstrInfo * TII,const WebAssemblyRegisterInfo * TRI)5580b57cec5SDimitry Andric static MachineInstr *rematerializeCheapDef(
5590b57cec5SDimitry Andric unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB,
5600b57cec5SDimitry Andric MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS,
5610b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI,
5620b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI) {
5630b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump());
5640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - for use in "; Op.getParent()->dump());
5650b57cec5SDimitry Andric
5660b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(&Def);
5670b57cec5SDimitry Andric
5688bcb0991SDimitry Andric Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
5690b57cec5SDimitry Andric TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI);
5700b57cec5SDimitry Andric Op.setReg(NewReg);
5710b57cec5SDimitry Andric MachineInstr *Clone = &*std::prev(Insert);
5720b57cec5SDimitry Andric LIS.InsertMachineInstrInMaps(*Clone);
5730b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(NewReg);
5745ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, NewReg);
5750b57cec5SDimitry Andric imposeStackOrdering(Clone);
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Cloned to "; Clone->dump());
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andric // Shrink the interval.
5800b57cec5SDimitry Andric bool IsDead = MRI.use_empty(Reg);
5810b57cec5SDimitry Andric if (!IsDead) {
5820b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
5830b57cec5SDimitry Andric shrinkToUses(LI, LIS);
5840b57cec5SDimitry Andric IsDead = !LI.liveAt(LIS.getInstructionIndex(Def).getDeadSlot());
5850b57cec5SDimitry Andric }
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andric // If that was the last use of the original, delete the original.
5880b57cec5SDimitry Andric // Move or clone corresponding DBG_VALUEs to the 'Insert' location.
5890b57cec5SDimitry Andric if (IsDead) {
5900b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Deleting original\n");
5910b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot();
592af732203SDimitry Andric LIS.removePhysRegDefAt(MCRegister::from(WebAssembly::ARGUMENTS), Idx);
5930b57cec5SDimitry Andric LIS.removeInterval(Reg);
5940b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(Def);
5950b57cec5SDimitry Andric Def.eraseFromParent();
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric DefDIs.move(&*Insert);
5980b57cec5SDimitry Andric DefDIs.updateReg(NewReg);
5990b57cec5SDimitry Andric } else {
6000b57cec5SDimitry Andric DefDIs.clone(&*Insert, NewReg);
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric
6030b57cec5SDimitry Andric return Clone;
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric
6060b57cec5SDimitry Andric /// A multiple-use def in the same block with no intervening memory or register
6070b57cec5SDimitry Andric /// dependencies; move the def down, nest it with the current instruction, and
6080b57cec5SDimitry Andric /// insert a tee to satisfy the rest of the uses. As an illustration, rewrite
6090b57cec5SDimitry Andric /// this:
6100b57cec5SDimitry Andric ///
6110b57cec5SDimitry Andric /// Reg = INST ... // Def
6120b57cec5SDimitry Andric /// INST ..., Reg, ... // Insert
6130b57cec5SDimitry Andric /// INST ..., Reg, ...
6140b57cec5SDimitry Andric /// INST ..., Reg, ...
6150b57cec5SDimitry Andric ///
6160b57cec5SDimitry Andric /// to this:
6170b57cec5SDimitry Andric ///
6180b57cec5SDimitry Andric /// DefReg = INST ... // Def (to become the new Insert)
6190b57cec5SDimitry Andric /// TeeReg, Reg = TEE_... DefReg
6200b57cec5SDimitry Andric /// INST ..., TeeReg, ... // Insert
6210b57cec5SDimitry Andric /// INST ..., Reg, ...
6220b57cec5SDimitry Andric /// INST ..., Reg, ...
6230b57cec5SDimitry Andric ///
6240b57cec5SDimitry Andric /// with DefReg and TeeReg stackified. This eliminates a local.get from the
6250b57cec5SDimitry Andric /// resulting code.
moveAndTeeForMultiUse(unsigned Reg,MachineOperand & Op,MachineInstr * Def,MachineBasicBlock & MBB,MachineInstr * Insert,LiveIntervals & LIS,WebAssemblyFunctionInfo & MFI,MachineRegisterInfo & MRI,const WebAssemblyInstrInfo * TII)6260b57cec5SDimitry Andric static MachineInstr *moveAndTeeForMultiUse(
6270b57cec5SDimitry Andric unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB,
6280b57cec5SDimitry Andric MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI,
6290b57cec5SDimitry Andric MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) {
6300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump());
6310b57cec5SDimitry Andric
6320b57cec5SDimitry Andric WebAssemblyDebugValueManager DefDIs(Def);
6330b57cec5SDimitry Andric
6340b57cec5SDimitry Andric // Move Def into place.
6350b57cec5SDimitry Andric MBB.splice(Insert, &MBB, Def);
6360b57cec5SDimitry Andric LIS.handleMove(*Def);
6370b57cec5SDimitry Andric
6380b57cec5SDimitry Andric // Create the Tee and attach the registers.
6390b57cec5SDimitry Andric const auto *RegClass = MRI.getRegClass(Reg);
6408bcb0991SDimitry Andric Register TeeReg = MRI.createVirtualRegister(RegClass);
6418bcb0991SDimitry Andric Register DefReg = MRI.createVirtualRegister(RegClass);
6420b57cec5SDimitry Andric MachineOperand &DefMO = Def->getOperand(0);
6430b57cec5SDimitry Andric MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(),
6440b57cec5SDimitry Andric TII->get(getTeeOpcode(RegClass)), TeeReg)
6450b57cec5SDimitry Andric .addReg(Reg, RegState::Define)
6460b57cec5SDimitry Andric .addReg(DefReg, getUndefRegState(DefMO.isDead()));
6470b57cec5SDimitry Andric Op.setReg(TeeReg);
6480b57cec5SDimitry Andric DefMO.setReg(DefReg);
6490b57cec5SDimitry Andric SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot();
6500b57cec5SDimitry Andric SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot();
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric DefDIs.move(Insert);
6530b57cec5SDimitry Andric
6540b57cec5SDimitry Andric // Tell LiveIntervals we moved the original vreg def from Def to Tee.
6550b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
6560b57cec5SDimitry Andric LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx);
6570b57cec5SDimitry Andric VNInfo *ValNo = LI.getVNInfoAt(DefIdx);
6580b57cec5SDimitry Andric I->start = TeeIdx;
6590b57cec5SDimitry Andric ValNo->def = TeeIdx;
6600b57cec5SDimitry Andric shrinkToUses(LI, LIS);
6610b57cec5SDimitry Andric
6620b57cec5SDimitry Andric // Finish stackifying the new regs.
6630b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(TeeReg);
6640b57cec5SDimitry Andric LIS.createAndComputeVirtRegInterval(DefReg);
6655ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, DefReg);
6665ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, TeeReg);
6670b57cec5SDimitry Andric imposeStackOrdering(Def);
6680b57cec5SDimitry Andric imposeStackOrdering(Tee);
6690b57cec5SDimitry Andric
6700b57cec5SDimitry Andric DefDIs.clone(Tee, DefReg);
6710b57cec5SDimitry Andric DefDIs.clone(Insert, TeeReg);
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump());
6740b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " - Tee instruction: "; Tee->dump());
6750b57cec5SDimitry Andric return Def;
6760b57cec5SDimitry Andric }
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric namespace {
6790b57cec5SDimitry Andric /// A stack for walking the tree of instructions being built, visiting the
6800b57cec5SDimitry Andric /// MachineOperands in DFS order.
6810b57cec5SDimitry Andric class TreeWalkerState {
6820b57cec5SDimitry Andric using mop_iterator = MachineInstr::mop_iterator;
6830b57cec5SDimitry Andric using mop_reverse_iterator = std::reverse_iterator<mop_iterator>;
6840b57cec5SDimitry Andric using RangeTy = iterator_range<mop_reverse_iterator>;
6850b57cec5SDimitry Andric SmallVector<RangeTy, 4> Worklist;
6860b57cec5SDimitry Andric
6870b57cec5SDimitry Andric public:
TreeWalkerState(MachineInstr * Insert)6880b57cec5SDimitry Andric explicit TreeWalkerState(MachineInstr *Insert) {
6890b57cec5SDimitry Andric const iterator_range<mop_iterator> &Range = Insert->explicit_uses();
690af732203SDimitry Andric if (!Range.empty())
6910b57cec5SDimitry Andric Worklist.push_back(reverse(Range));
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric
done() const6940b57cec5SDimitry Andric bool done() const { return Worklist.empty(); }
6950b57cec5SDimitry Andric
pop()6960b57cec5SDimitry Andric MachineOperand &pop() {
6970b57cec5SDimitry Andric RangeTy &Range = Worklist.back();
6980b57cec5SDimitry Andric MachineOperand &Op = *Range.begin();
699af732203SDimitry Andric Range = drop_begin(Range);
700af732203SDimitry Andric if (Range.empty())
7010b57cec5SDimitry Andric Worklist.pop_back();
702af732203SDimitry Andric assert((Worklist.empty() || !Worklist.back().empty()) &&
7030b57cec5SDimitry Andric "Empty ranges shouldn't remain in the worklist");
7040b57cec5SDimitry Andric return Op;
7050b57cec5SDimitry Andric }
7060b57cec5SDimitry Andric
7070b57cec5SDimitry Andric /// Push Instr's operands onto the stack to be visited.
pushOperands(MachineInstr * Instr)7080b57cec5SDimitry Andric void pushOperands(MachineInstr *Instr) {
7090b57cec5SDimitry Andric const iterator_range<mop_iterator> &Range(Instr->explicit_uses());
710af732203SDimitry Andric if (!Range.empty())
7110b57cec5SDimitry Andric Worklist.push_back(reverse(Range));
7120b57cec5SDimitry Andric }
7130b57cec5SDimitry Andric
7140b57cec5SDimitry Andric /// Some of Instr's operands are on the top of the stack; remove them and
7150b57cec5SDimitry Andric /// re-insert them starting from the beginning (because we've commuted them).
resetTopOperands(MachineInstr * Instr)7160b57cec5SDimitry Andric void resetTopOperands(MachineInstr *Instr) {
7170b57cec5SDimitry Andric assert(hasRemainingOperands(Instr) &&
7180b57cec5SDimitry Andric "Reseting operands should only be done when the instruction has "
7190b57cec5SDimitry Andric "an operand still on the stack");
7200b57cec5SDimitry Andric Worklist.back() = reverse(Instr->explicit_uses());
7210b57cec5SDimitry Andric }
7220b57cec5SDimitry Andric
7230b57cec5SDimitry Andric /// Test whether Instr has operands remaining to be visited at the top of
7240b57cec5SDimitry Andric /// the stack.
hasRemainingOperands(const MachineInstr * Instr) const7250b57cec5SDimitry Andric bool hasRemainingOperands(const MachineInstr *Instr) const {
7260b57cec5SDimitry Andric if (Worklist.empty())
7270b57cec5SDimitry Andric return false;
7280b57cec5SDimitry Andric const RangeTy &Range = Worklist.back();
729af732203SDimitry Andric return !Range.empty() && Range.begin()->getParent() == Instr;
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric /// Test whether the given register is present on the stack, indicating an
7330b57cec5SDimitry Andric /// operand in the tree that we haven't visited yet. Moving a definition of
7340b57cec5SDimitry Andric /// Reg to a point in the tree after that would change its value.
7350b57cec5SDimitry Andric ///
7360b57cec5SDimitry Andric /// This is needed as a consequence of using implicit local.gets for
7370b57cec5SDimitry Andric /// uses and implicit local.sets for defs.
isOnStack(unsigned Reg) const7380b57cec5SDimitry Andric bool isOnStack(unsigned Reg) const {
7390b57cec5SDimitry Andric for (const RangeTy &Range : Worklist)
7400b57cec5SDimitry Andric for (const MachineOperand &MO : Range)
7410b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() == Reg)
7420b57cec5SDimitry Andric return true;
7430b57cec5SDimitry Andric return false;
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric };
7460b57cec5SDimitry Andric
7470b57cec5SDimitry Andric /// State to keep track of whether commuting is in flight or whether it's been
7480b57cec5SDimitry Andric /// tried for the current instruction and didn't work.
7490b57cec5SDimitry Andric class CommutingState {
7500b57cec5SDimitry Andric /// There are effectively three states: the initial state where we haven't
7510b57cec5SDimitry Andric /// started commuting anything and we don't know anything yet, the tentative
7520b57cec5SDimitry Andric /// state where we've commuted the operands of the current instruction and are
7530b57cec5SDimitry Andric /// revisiting it, and the declined state where we've reverted the operands
7540b57cec5SDimitry Andric /// back to their original order and will no longer commute it further.
7550b57cec5SDimitry Andric bool TentativelyCommuting = false;
7560b57cec5SDimitry Andric bool Declined = false;
7570b57cec5SDimitry Andric
7580b57cec5SDimitry Andric /// During the tentative state, these hold the operand indices of the commuted
7590b57cec5SDimitry Andric /// operands.
7600b57cec5SDimitry Andric unsigned Operand0, Operand1;
7610b57cec5SDimitry Andric
7620b57cec5SDimitry Andric public:
7630b57cec5SDimitry Andric /// Stackification for an operand was not successful due to ordering
7640b57cec5SDimitry Andric /// constraints. If possible, and if we haven't already tried it and declined
7650b57cec5SDimitry Andric /// it, commute Insert's operands and prepare to revisit it.
maybeCommute(MachineInstr * Insert,TreeWalkerState & TreeWalker,const WebAssemblyInstrInfo * TII)7660b57cec5SDimitry Andric void maybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker,
7670b57cec5SDimitry Andric const WebAssemblyInstrInfo *TII) {
7680b57cec5SDimitry Andric if (TentativelyCommuting) {
7690b57cec5SDimitry Andric assert(!Declined &&
7700b57cec5SDimitry Andric "Don't decline commuting until you've finished trying it");
7710b57cec5SDimitry Andric // Commuting didn't help. Revert it.
7720b57cec5SDimitry Andric TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1);
7730b57cec5SDimitry Andric TentativelyCommuting = false;
7740b57cec5SDimitry Andric Declined = true;
7750b57cec5SDimitry Andric } else if (!Declined && TreeWalker.hasRemainingOperands(Insert)) {
7760b57cec5SDimitry Andric Operand0 = TargetInstrInfo::CommuteAnyOperandIndex;
7770b57cec5SDimitry Andric Operand1 = TargetInstrInfo::CommuteAnyOperandIndex;
7780b57cec5SDimitry Andric if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) {
7790b57cec5SDimitry Andric // Tentatively commute the operands and try again.
7800b57cec5SDimitry Andric TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1);
7810b57cec5SDimitry Andric TreeWalker.resetTopOperands(Insert);
7820b57cec5SDimitry Andric TentativelyCommuting = true;
7830b57cec5SDimitry Andric Declined = false;
7840b57cec5SDimitry Andric }
7850b57cec5SDimitry Andric }
7860b57cec5SDimitry Andric }
7870b57cec5SDimitry Andric
7880b57cec5SDimitry Andric /// Stackification for some operand was successful. Reset to the default
7890b57cec5SDimitry Andric /// state.
reset()7900b57cec5SDimitry Andric void reset() {
7910b57cec5SDimitry Andric TentativelyCommuting = false;
7920b57cec5SDimitry Andric Declined = false;
7930b57cec5SDimitry Andric }
7940b57cec5SDimitry Andric };
7950b57cec5SDimitry Andric } // end anonymous namespace
7960b57cec5SDimitry Andric
runOnMachineFunction(MachineFunction & MF)7970b57cec5SDimitry Andric bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
7980b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Register Stackifying **********\n"
7990b57cec5SDimitry Andric "********** Function: "
8000b57cec5SDimitry Andric << MF.getName() << '\n');
8010b57cec5SDimitry Andric
8020b57cec5SDimitry Andric bool Changed = false;
8030b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
8040b57cec5SDimitry Andric WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
8050b57cec5SDimitry Andric const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
8060b57cec5SDimitry Andric const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
8070b57cec5SDimitry Andric AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
8080b57cec5SDimitry Andric auto &MDT = getAnalysis<MachineDominatorTree>();
8090b57cec5SDimitry Andric auto &LIS = getAnalysis<LiveIntervals>();
8100b57cec5SDimitry Andric
8110b57cec5SDimitry Andric // Walk the instructions from the bottom up. Currently we don't look past
8120b57cec5SDimitry Andric // block boundaries, and the blocks aren't ordered so the block visitation
8130b57cec5SDimitry Andric // order isn't significant, but we may want to change this in the future.
8140b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) {
8150b57cec5SDimitry Andric // Don't use a range-based for loop, because we modify the list as we're
8160b57cec5SDimitry Andric // iterating over it and the end iterator may change.
8170b57cec5SDimitry Andric for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
8180b57cec5SDimitry Andric MachineInstr *Insert = &*MII;
8190b57cec5SDimitry Andric // Don't nest anything inside an inline asm, because we don't have
8200b57cec5SDimitry Andric // constraints for $push inputs.
8210b57cec5SDimitry Andric if (Insert->isInlineAsm())
8220b57cec5SDimitry Andric continue;
8230b57cec5SDimitry Andric
8240b57cec5SDimitry Andric // Ignore debugging intrinsics.
8250b57cec5SDimitry Andric if (Insert->isDebugValue())
8260b57cec5SDimitry Andric continue;
8270b57cec5SDimitry Andric
8280b57cec5SDimitry Andric // Iterate through the inputs in reverse order, since we'll be pulling
8290b57cec5SDimitry Andric // operands off the stack in LIFO order.
8300b57cec5SDimitry Andric CommutingState Commuting;
8310b57cec5SDimitry Andric TreeWalkerState TreeWalker(Insert);
8320b57cec5SDimitry Andric while (!TreeWalker.done()) {
8335ffd83dbSDimitry Andric MachineOperand &Use = TreeWalker.pop();
8340b57cec5SDimitry Andric
8350b57cec5SDimitry Andric // We're only interested in explicit virtual register operands.
8365ffd83dbSDimitry Andric if (!Use.isReg())
8370b57cec5SDimitry Andric continue;
8380b57cec5SDimitry Andric
8395ffd83dbSDimitry Andric Register Reg = Use.getReg();
8405ffd83dbSDimitry Andric assert(Use.isUse() && "explicit_uses() should only iterate over uses");
8415ffd83dbSDimitry Andric assert(!Use.isImplicit() &&
8420b57cec5SDimitry Andric "explicit_uses() should only iterate over explicit operands");
8438bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg))
8440b57cec5SDimitry Andric continue;
8450b57cec5SDimitry Andric
8460b57cec5SDimitry Andric // Identify the definition for this register at this point.
8475ffd83dbSDimitry Andric MachineInstr *DefI = getVRegDef(Reg, Insert, MRI, LIS);
8485ffd83dbSDimitry Andric if (!DefI)
8490b57cec5SDimitry Andric continue;
8500b57cec5SDimitry Andric
8510b57cec5SDimitry Andric // Don't nest an INLINE_ASM def into anything, because we don't have
8520b57cec5SDimitry Andric // constraints for $pop outputs.
8535ffd83dbSDimitry Andric if (DefI->isInlineAsm())
8540b57cec5SDimitry Andric continue;
8550b57cec5SDimitry Andric
8560b57cec5SDimitry Andric // Argument instructions represent live-in registers and not real
8570b57cec5SDimitry Andric // instructions.
8585ffd83dbSDimitry Andric if (WebAssembly::isArgument(DefI->getOpcode()))
8590b57cec5SDimitry Andric continue;
8600b57cec5SDimitry Andric
8615ffd83dbSDimitry Andric MachineOperand *Def = DefI->findRegisterDefOperand(Reg);
8625ffd83dbSDimitry Andric assert(Def != nullptr);
8635ffd83dbSDimitry Andric
8640b57cec5SDimitry Andric // Decide which strategy to take. Prefer to move a single-use value
8650b57cec5SDimitry Andric // over cloning it, and prefer cloning over introducing a tee.
8660b57cec5SDimitry Andric // For moving, we require the def to be in the same block as the use;
8670b57cec5SDimitry Andric // this makes things simpler (LiveIntervals' handleMove function only
8680b57cec5SDimitry Andric // supports intra-block moves) and it's MachineSink's job to catch all
8690b57cec5SDimitry Andric // the sinking opportunities anyway.
8705ffd83dbSDimitry Andric bool SameBlock = DefI->getParent() == &MBB;
8715ffd83dbSDimitry Andric bool CanMove = SameBlock &&
8725ffd83dbSDimitry Andric isSafeToMove(Def, &Use, Insert, AA, MFI, MRI) &&
8730b57cec5SDimitry Andric !TreeWalker.isOnStack(Reg);
8745ffd83dbSDimitry Andric if (CanMove && hasOneUse(Reg, DefI, MRI, MDT, LIS)) {
8755ffd83dbSDimitry Andric Insert = moveForSingleUse(Reg, Use, DefI, MBB, Insert, LIS, MFI, MRI);
8765ffd83dbSDimitry Andric
8775ffd83dbSDimitry Andric // If we are removing the frame base reg completely, remove the debug
8785ffd83dbSDimitry Andric // info as well.
8795ffd83dbSDimitry Andric // TODO: Encode this properly as a stackified value.
8805ffd83dbSDimitry Andric if (MFI.isFrameBaseVirtual() && MFI.getFrameBaseVreg() == Reg)
8815ffd83dbSDimitry Andric MFI.clearFrameBaseVreg();
8825ffd83dbSDimitry Andric } else if (shouldRematerialize(*DefI, AA, TII)) {
8830b57cec5SDimitry Andric Insert =
8845ffd83dbSDimitry Andric rematerializeCheapDef(Reg, Use, *DefI, MBB, Insert->getIterator(),
8850b57cec5SDimitry Andric LIS, MFI, MRI, TII, TRI);
8865ffd83dbSDimitry Andric } else if (CanMove && oneUseDominatesOtherUses(Reg, Use, MBB, MRI, MDT,
8875ffd83dbSDimitry Andric LIS, MFI)) {
8885ffd83dbSDimitry Andric Insert = moveAndTeeForMultiUse(Reg, Use, DefI, MBB, Insert, LIS, MFI,
8890b57cec5SDimitry Andric MRI, TII);
8900b57cec5SDimitry Andric } else {
8910b57cec5SDimitry Andric // We failed to stackify the operand. If the problem was ordering
8920b57cec5SDimitry Andric // constraints, Commuting may be able to help.
8930b57cec5SDimitry Andric if (!CanMove && SameBlock)
8940b57cec5SDimitry Andric Commuting.maybeCommute(Insert, TreeWalker, TII);
8950b57cec5SDimitry Andric // Proceed to the next operand.
8960b57cec5SDimitry Andric continue;
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric
8995ffd83dbSDimitry Andric // Stackifying a multivalue def may unlock in-place stackification of
9005ffd83dbSDimitry Andric // subsequent defs. TODO: Handle the case where the consecutive uses are
9015ffd83dbSDimitry Andric // not all in the same instruction.
9025ffd83dbSDimitry Andric auto *SubsequentDef = Insert->defs().begin();
9035ffd83dbSDimitry Andric auto *SubsequentUse = &Use;
9045ffd83dbSDimitry Andric while (SubsequentDef != Insert->defs().end() &&
9055ffd83dbSDimitry Andric SubsequentUse != Use.getParent()->uses().end()) {
9065ffd83dbSDimitry Andric if (!SubsequentDef->isReg() || !SubsequentUse->isReg())
9075ffd83dbSDimitry Andric break;
9085ffd83dbSDimitry Andric unsigned DefReg = SubsequentDef->getReg();
9095ffd83dbSDimitry Andric unsigned UseReg = SubsequentUse->getReg();
9105ffd83dbSDimitry Andric // TODO: This single-use restriction could be relaxed by using tees
9115ffd83dbSDimitry Andric if (DefReg != UseReg || !MRI.hasOneUse(DefReg))
9125ffd83dbSDimitry Andric break;
9135ffd83dbSDimitry Andric MFI.stackifyVReg(MRI, DefReg);
9145ffd83dbSDimitry Andric ++SubsequentDef;
9155ffd83dbSDimitry Andric ++SubsequentUse;
9165ffd83dbSDimitry Andric }
9175ffd83dbSDimitry Andric
9180b57cec5SDimitry Andric // If the instruction we just stackified is an IMPLICIT_DEF, convert it
9190b57cec5SDimitry Andric // to a constant 0 so that the def is explicit, and the push/pop
9200b57cec5SDimitry Andric // correspondence is maintained.
9210b57cec5SDimitry Andric if (Insert->getOpcode() == TargetOpcode::IMPLICIT_DEF)
9220b57cec5SDimitry Andric convertImplicitDefToConstZero(Insert, MRI, TII, MF, LIS);
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric // We stackified an operand. Add the defining instruction's operands to
9250b57cec5SDimitry Andric // the worklist stack now to continue to build an ever deeper tree.
9260b57cec5SDimitry Andric Commuting.reset();
9270b57cec5SDimitry Andric TreeWalker.pushOperands(Insert);
9280b57cec5SDimitry Andric }
9290b57cec5SDimitry Andric
9300b57cec5SDimitry Andric // If we stackified any operands, skip over the tree to start looking for
9310b57cec5SDimitry Andric // the next instruction we can build a tree on.
9320b57cec5SDimitry Andric if (Insert != &*MII) {
9330b57cec5SDimitry Andric imposeStackOrdering(&*MII);
9340b57cec5SDimitry Andric MII = MachineBasicBlock::iterator(Insert).getReverse();
9350b57cec5SDimitry Andric Changed = true;
9360b57cec5SDimitry Andric }
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric }
9390b57cec5SDimitry Andric
9400b57cec5SDimitry Andric // If we used VALUE_STACK anywhere, add it to the live-in sets everywhere so
9410b57cec5SDimitry Andric // that it never looks like a use-before-def.
9420b57cec5SDimitry Andric if (Changed) {
9430b57cec5SDimitry Andric MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK);
9440b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF)
9450b57cec5SDimitry Andric MBB.addLiveIn(WebAssembly::VALUE_STACK);
9460b57cec5SDimitry Andric }
9470b57cec5SDimitry Andric
9480b57cec5SDimitry Andric #ifndef NDEBUG
9490b57cec5SDimitry Andric // Verify that pushes and pops are performed in LIFO order.
9500b57cec5SDimitry Andric SmallVector<unsigned, 0> Stack;
9510b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) {
9520b57cec5SDimitry Andric for (MachineInstr &MI : MBB) {
9530b57cec5SDimitry Andric if (MI.isDebugInstr())
9540b57cec5SDimitry Andric continue;
9555ffd83dbSDimitry Andric for (MachineOperand &MO : reverse(MI.explicit_uses())) {
9560b57cec5SDimitry Andric if (!MO.isReg())
9570b57cec5SDimitry Andric continue;
9588bcb0991SDimitry Andric Register Reg = MO.getReg();
9595ffd83dbSDimitry Andric if (MFI.isVRegStackified(Reg))
9600b57cec5SDimitry Andric assert(Stack.pop_back_val() == Reg &&
9610b57cec5SDimitry Andric "Register stack pop should be paired with a push");
9620b57cec5SDimitry Andric }
9635ffd83dbSDimitry Andric for (MachineOperand &MO : MI.defs()) {
9645ffd83dbSDimitry Andric if (!MO.isReg())
9655ffd83dbSDimitry Andric continue;
9665ffd83dbSDimitry Andric Register Reg = MO.getReg();
9675ffd83dbSDimitry Andric if (MFI.isVRegStackified(Reg))
9685ffd83dbSDimitry Andric Stack.push_back(MO.getReg());
9690b57cec5SDimitry Andric }
9700b57cec5SDimitry Andric }
9710b57cec5SDimitry Andric // TODO: Generalize this code to support keeping values on the stack across
9720b57cec5SDimitry Andric // basic block boundaries.
9730b57cec5SDimitry Andric assert(Stack.empty() &&
9740b57cec5SDimitry Andric "Register stack pushes and pops should be balanced");
9750b57cec5SDimitry Andric }
9760b57cec5SDimitry Andric #endif
9770b57cec5SDimitry Andric
9780b57cec5SDimitry Andric return Changed;
9790b57cec5SDimitry Andric }
980