1*9880d681SAndroid Build Coastguard Worker //===-- WebAssemblyRegStackify.cpp - Register Stackification --------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker ///
10*9880d681SAndroid Build Coastguard Worker /// \file
11*9880d681SAndroid Build Coastguard Worker /// \brief This file implements a register stacking pass.
12*9880d681SAndroid Build Coastguard Worker ///
13*9880d681SAndroid Build Coastguard Worker /// This pass reorders instructions to put register uses and defs in an order
14*9880d681SAndroid Build Coastguard Worker /// such that they form single-use expression trees. Registers fitting this form
15*9880d681SAndroid Build Coastguard Worker /// are then marked as "stackified", meaning references to them are replaced by
16*9880d681SAndroid Build Coastguard Worker /// "push" and "pop" from the stack.
17*9880d681SAndroid Build Coastguard Worker ///
18*9880d681SAndroid Build Coastguard Worker /// This is primarily a code size optimization, since temporary values on the
19*9880d681SAndroid Build Coastguard Worker /// expression don't need to be named.
20*9880d681SAndroid Build Coastguard Worker ///
21*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Worker #include "WebAssembly.h"
24*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_*
25*9880d681SAndroid Build Coastguard Worker #include "WebAssemblyMachineFunctionInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "WebAssemblySubtarget.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveIntervalAnalysis.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "wasm-reg-stackify"
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker namespace {
41*9880d681SAndroid Build Coastguard Worker class WebAssemblyRegStackify final : public MachineFunctionPass {
getPassName() const42*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
43*9880d681SAndroid Build Coastguard Worker return "WebAssembly Register Stackify";
44*9880d681SAndroid Build Coastguard Worker }
45*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const46*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
47*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
48*9880d681SAndroid Build Coastguard Worker AU.addRequired<AAResultsWrapperPass>();
49*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
50*9880d681SAndroid Build Coastguard Worker AU.addRequired<LiveIntervals>();
51*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineBlockFrequencyInfo>();
52*9880d681SAndroid Build Coastguard Worker AU.addPreserved<SlotIndexes>();
53*9880d681SAndroid Build Coastguard Worker AU.addPreserved<LiveIntervals>();
54*9880d681SAndroid Build Coastguard Worker AU.addPreservedID(LiveVariablesID);
55*9880d681SAndroid Build Coastguard Worker AU.addPreserved<MachineDominatorTree>();
56*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker public:
62*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
WebAssemblyRegStackify()63*9880d681SAndroid Build Coastguard Worker WebAssemblyRegStackify() : MachineFunctionPass(ID) {}
64*9880d681SAndroid Build Coastguard Worker };
65*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker char WebAssemblyRegStackify::ID = 0;
createWebAssemblyRegStackify()68*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createWebAssemblyRegStackify() {
69*9880d681SAndroid Build Coastguard Worker return new WebAssemblyRegStackify();
70*9880d681SAndroid Build Coastguard Worker }
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker // Decorate the given instruction with implicit operands that enforce the
73*9880d681SAndroid Build Coastguard Worker // expression stack ordering constraints for an instruction which is on
74*9880d681SAndroid Build Coastguard Worker // the expression stack.
ImposeStackOrdering(MachineInstr * MI)75*9880d681SAndroid Build Coastguard Worker static void ImposeStackOrdering(MachineInstr *MI) {
76*9880d681SAndroid Build Coastguard Worker // Write the opaque EXPR_STACK register.
77*9880d681SAndroid Build Coastguard Worker if (!MI->definesRegister(WebAssembly::EXPR_STACK))
78*9880d681SAndroid Build Coastguard Worker MI->addOperand(MachineOperand::CreateReg(WebAssembly::EXPR_STACK,
79*9880d681SAndroid Build Coastguard Worker /*isDef=*/true,
80*9880d681SAndroid Build Coastguard Worker /*isImp=*/true));
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker // Also read the opaque EXPR_STACK register.
83*9880d681SAndroid Build Coastguard Worker if (!MI->readsRegister(WebAssembly::EXPR_STACK))
84*9880d681SAndroid Build Coastguard Worker MI->addOperand(MachineOperand::CreateReg(WebAssembly::EXPR_STACK,
85*9880d681SAndroid Build Coastguard Worker /*isDef=*/false,
86*9880d681SAndroid Build Coastguard Worker /*isImp=*/true));
87*9880d681SAndroid Build Coastguard Worker }
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker // Determine whether a call to the callee referenced by
90*9880d681SAndroid Build Coastguard Worker // MI->getOperand(CalleeOpNo) reads memory, writes memory, and/or has side
91*9880d681SAndroid Build Coastguard Worker // effects.
QueryCallee(const MachineInstr & MI,unsigned CalleeOpNo,bool & Read,bool & Write,bool & Effects,bool & StackPointer)92*9880d681SAndroid Build Coastguard Worker static void QueryCallee(const MachineInstr &MI, unsigned CalleeOpNo, bool &Read,
93*9880d681SAndroid Build Coastguard Worker bool &Write, bool &Effects, bool &StackPointer) {
94*9880d681SAndroid Build Coastguard Worker // All calls can use the stack pointer.
95*9880d681SAndroid Build Coastguard Worker StackPointer = true;
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(CalleeOpNo);
98*9880d681SAndroid Build Coastguard Worker if (MO.isGlobal()) {
99*9880d681SAndroid Build Coastguard Worker const Constant *GV = MO.getGlobal();
100*9880d681SAndroid Build Coastguard Worker if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
101*9880d681SAndroid Build Coastguard Worker if (!GA->isInterposable())
102*9880d681SAndroid Build Coastguard Worker GV = GA->getAliasee();
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker if (const Function *F = dyn_cast<Function>(GV)) {
105*9880d681SAndroid Build Coastguard Worker if (!F->doesNotThrow())
106*9880d681SAndroid Build Coastguard Worker Effects = true;
107*9880d681SAndroid Build Coastguard Worker if (F->doesNotAccessMemory())
108*9880d681SAndroid Build Coastguard Worker return;
109*9880d681SAndroid Build Coastguard Worker if (F->onlyReadsMemory()) {
110*9880d681SAndroid Build Coastguard Worker Read = true;
111*9880d681SAndroid Build Coastguard Worker return;
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker }
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker // Assume the worst.
117*9880d681SAndroid Build Coastguard Worker Write = true;
118*9880d681SAndroid Build Coastguard Worker Read = true;
119*9880d681SAndroid Build Coastguard Worker Effects = true;
120*9880d681SAndroid Build Coastguard Worker }
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker // Determine whether MI reads memory, writes memory, has side effects,
123*9880d681SAndroid Build Coastguard Worker // and/or uses the __stack_pointer value.
Query(const MachineInstr & MI,AliasAnalysis & AA,bool & Read,bool & Write,bool & Effects,bool & StackPointer)124*9880d681SAndroid Build Coastguard Worker static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read,
125*9880d681SAndroid Build Coastguard Worker bool &Write, bool &Effects, bool &StackPointer) {
126*9880d681SAndroid Build Coastguard Worker assert(!MI.isPosition());
127*9880d681SAndroid Build Coastguard Worker assert(!MI.isTerminator());
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker if (MI.isDebugValue())
130*9880d681SAndroid Build Coastguard Worker return;
131*9880d681SAndroid Build Coastguard Worker
132*9880d681SAndroid Build Coastguard Worker // Check for loads.
133*9880d681SAndroid Build Coastguard Worker if (MI.mayLoad() && !MI.isInvariantLoad(&AA))
134*9880d681SAndroid Build Coastguard Worker Read = true;
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker // Check for stores.
137*9880d681SAndroid Build Coastguard Worker if (MI.mayStore()) {
138*9880d681SAndroid Build Coastguard Worker Write = true;
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker // Check for stores to __stack_pointer.
141*9880d681SAndroid Build Coastguard Worker for (auto MMO : MI.memoperands()) {
142*9880d681SAndroid Build Coastguard Worker const MachinePointerInfo &MPI = MMO->getPointerInfo();
143*9880d681SAndroid Build Coastguard Worker if (MPI.V.is<const PseudoSourceValue *>()) {
144*9880d681SAndroid Build Coastguard Worker auto PSV = MPI.V.get<const PseudoSourceValue *>();
145*9880d681SAndroid Build Coastguard Worker if (const ExternalSymbolPseudoSourceValue *EPSV =
146*9880d681SAndroid Build Coastguard Worker dyn_cast<ExternalSymbolPseudoSourceValue>(PSV))
147*9880d681SAndroid Build Coastguard Worker if (StringRef(EPSV->getSymbol()) == "__stack_pointer")
148*9880d681SAndroid Build Coastguard Worker StackPointer = true;
149*9880d681SAndroid Build Coastguard Worker }
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker } else if (MI.hasOrderedMemoryRef()) {
152*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
153*9880d681SAndroid Build Coastguard Worker case WebAssembly::DIV_S_I32: case WebAssembly::DIV_S_I64:
154*9880d681SAndroid Build Coastguard Worker case WebAssembly::REM_S_I32: case WebAssembly::REM_S_I64:
155*9880d681SAndroid Build Coastguard Worker case WebAssembly::DIV_U_I32: case WebAssembly::DIV_U_I64:
156*9880d681SAndroid Build Coastguard Worker case WebAssembly::REM_U_I32: case WebAssembly::REM_U_I64:
157*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_S_F32: case WebAssembly::I64_TRUNC_S_F32:
158*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_S_F64: case WebAssembly::I64_TRUNC_S_F64:
159*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_U_F32: case WebAssembly::I64_TRUNC_U_F32:
160*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_U_F64: case WebAssembly::I64_TRUNC_U_F64:
161*9880d681SAndroid Build Coastguard Worker // These instruction have hasUnmodeledSideEffects() returning true
162*9880d681SAndroid Build Coastguard Worker // because they trap on overflow and invalid so they can't be arbitrarily
163*9880d681SAndroid Build Coastguard Worker // moved, however hasOrderedMemoryRef() interprets this plus their lack
164*9880d681SAndroid Build Coastguard Worker // of memoperands as having a potential unknown memory reference.
165*9880d681SAndroid Build Coastguard Worker break;
166*9880d681SAndroid Build Coastguard Worker default:
167*9880d681SAndroid Build Coastguard Worker // Record volatile accesses, unless it's a call, as calls are handled
168*9880d681SAndroid Build Coastguard Worker // specially below.
169*9880d681SAndroid Build Coastguard Worker if (!MI.isCall()) {
170*9880d681SAndroid Build Coastguard Worker Write = true;
171*9880d681SAndroid Build Coastguard Worker Effects = true;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker break;
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker }
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker // Check for side effects.
178*9880d681SAndroid Build Coastguard Worker if (MI.hasUnmodeledSideEffects()) {
179*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
180*9880d681SAndroid Build Coastguard Worker case WebAssembly::DIV_S_I32: case WebAssembly::DIV_S_I64:
181*9880d681SAndroid Build Coastguard Worker case WebAssembly::REM_S_I32: case WebAssembly::REM_S_I64:
182*9880d681SAndroid Build Coastguard Worker case WebAssembly::DIV_U_I32: case WebAssembly::DIV_U_I64:
183*9880d681SAndroid Build Coastguard Worker case WebAssembly::REM_U_I32: case WebAssembly::REM_U_I64:
184*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_S_F32: case WebAssembly::I64_TRUNC_S_F32:
185*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_S_F64: case WebAssembly::I64_TRUNC_S_F64:
186*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_U_F32: case WebAssembly::I64_TRUNC_U_F32:
187*9880d681SAndroid Build Coastguard Worker case WebAssembly::I32_TRUNC_U_F64: case WebAssembly::I64_TRUNC_U_F64:
188*9880d681SAndroid Build Coastguard Worker // These instructions have hasUnmodeledSideEffects() returning true
189*9880d681SAndroid Build Coastguard Worker // because they trap on overflow and invalid so they can't be arbitrarily
190*9880d681SAndroid Build Coastguard Worker // moved, however in the specific case of register stackifying, it is safe
191*9880d681SAndroid Build Coastguard Worker // to move them because overflow and invalid are Undefined Behavior.
192*9880d681SAndroid Build Coastguard Worker break;
193*9880d681SAndroid Build Coastguard Worker default:
194*9880d681SAndroid Build Coastguard Worker Effects = true;
195*9880d681SAndroid Build Coastguard Worker break;
196*9880d681SAndroid Build Coastguard Worker }
197*9880d681SAndroid Build Coastguard Worker }
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker // Analyze calls.
200*9880d681SAndroid Build Coastguard Worker if (MI.isCall()) {
201*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
202*9880d681SAndroid Build Coastguard Worker case WebAssembly::CALL_VOID:
203*9880d681SAndroid Build Coastguard Worker case WebAssembly::CALL_INDIRECT_VOID:
204*9880d681SAndroid Build Coastguard Worker QueryCallee(MI, 0, Read, Write, Effects, StackPointer);
205*9880d681SAndroid Build Coastguard Worker break;
206*9880d681SAndroid Build Coastguard Worker case WebAssembly::CALL_I32: case WebAssembly::CALL_I64:
207*9880d681SAndroid Build Coastguard Worker case WebAssembly::CALL_F32: case WebAssembly::CALL_F64:
208*9880d681SAndroid Build Coastguard Worker case WebAssembly::CALL_INDIRECT_I32: case WebAssembly::CALL_INDIRECT_I64:
209*9880d681SAndroid Build Coastguard Worker case WebAssembly::CALL_INDIRECT_F32: case WebAssembly::CALL_INDIRECT_F64:
210*9880d681SAndroid Build Coastguard Worker QueryCallee(MI, 1, Read, Write, Effects, StackPointer);
211*9880d681SAndroid Build Coastguard Worker break;
212*9880d681SAndroid Build Coastguard Worker default:
213*9880d681SAndroid Build Coastguard Worker llvm_unreachable("unexpected call opcode");
214*9880d681SAndroid Build Coastguard Worker }
215*9880d681SAndroid Build Coastguard Worker }
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker
218*9880d681SAndroid Build Coastguard Worker // Test whether Def is safe and profitable to rematerialize.
ShouldRematerialize(const MachineInstr & Def,AliasAnalysis & AA,const WebAssemblyInstrInfo * TII)219*9880d681SAndroid Build Coastguard Worker static bool ShouldRematerialize(const MachineInstr &Def, AliasAnalysis &AA,
220*9880d681SAndroid Build Coastguard Worker const WebAssemblyInstrInfo *TII) {
221*9880d681SAndroid Build Coastguard Worker return Def.isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def, &AA);
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker // Identify the definition for this register at this point. This is a
225*9880d681SAndroid Build Coastguard Worker // generalization of MachineRegisterInfo::getUniqueVRegDef that uses
226*9880d681SAndroid Build Coastguard Worker // LiveIntervals to handle complex cases.
GetVRegDef(unsigned Reg,const MachineInstr * Insert,const MachineRegisterInfo & MRI,const LiveIntervals & LIS)227*9880d681SAndroid Build Coastguard Worker static MachineInstr *GetVRegDef(unsigned Reg, const MachineInstr *Insert,
228*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo &MRI,
229*9880d681SAndroid Build Coastguard Worker const LiveIntervals &LIS)
230*9880d681SAndroid Build Coastguard Worker {
231*9880d681SAndroid Build Coastguard Worker // Most registers are in SSA form here so we try a quick MRI query first.
232*9880d681SAndroid Build Coastguard Worker if (MachineInstr *Def = MRI.getUniqueVRegDef(Reg))
233*9880d681SAndroid Build Coastguard Worker return Def;
234*9880d681SAndroid Build Coastguard Worker
235*9880d681SAndroid Build Coastguard Worker // MRI doesn't know what the Def is. Try asking LIS.
236*9880d681SAndroid Build Coastguard Worker if (const VNInfo *ValNo = LIS.getInterval(Reg).getVNInfoBefore(
237*9880d681SAndroid Build Coastguard Worker LIS.getInstructionIndex(*Insert)))
238*9880d681SAndroid Build Coastguard Worker return LIS.getInstructionFromIndex(ValNo->def);
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker return nullptr;
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker // Test whether Reg, as defined at Def, has exactly one use. This is a
244*9880d681SAndroid Build Coastguard Worker // generalization of MachineRegisterInfo::hasOneUse that uses LiveIntervals
245*9880d681SAndroid Build Coastguard Worker // to handle complex cases.
HasOneUse(unsigned Reg,MachineInstr * Def,MachineRegisterInfo & MRI,MachineDominatorTree & MDT,LiveIntervals & LIS)246*9880d681SAndroid Build Coastguard Worker static bool HasOneUse(unsigned Reg, MachineInstr *Def,
247*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI, MachineDominatorTree &MDT,
248*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS) {
249*9880d681SAndroid Build Coastguard Worker // Most registers are in SSA form here so we try a quick MRI query first.
250*9880d681SAndroid Build Coastguard Worker if (MRI.hasOneUse(Reg))
251*9880d681SAndroid Build Coastguard Worker return true;
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker bool HasOne = false;
254*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LIS.getInterval(Reg);
255*9880d681SAndroid Build Coastguard Worker const VNInfo *DefVNI = LI.getVNInfoAt(
256*9880d681SAndroid Build Coastguard Worker LIS.getInstructionIndex(*Def).getRegSlot());
257*9880d681SAndroid Build Coastguard Worker assert(DefVNI);
258*9880d681SAndroid Build Coastguard Worker for (auto I : MRI.use_nodbg_operands(Reg)) {
259*9880d681SAndroid Build Coastguard Worker const auto &Result = LI.Query(LIS.getInstructionIndex(*I.getParent()));
260*9880d681SAndroid Build Coastguard Worker if (Result.valueIn() == DefVNI) {
261*9880d681SAndroid Build Coastguard Worker if (!Result.isKill())
262*9880d681SAndroid Build Coastguard Worker return false;
263*9880d681SAndroid Build Coastguard Worker if (HasOne)
264*9880d681SAndroid Build Coastguard Worker return false;
265*9880d681SAndroid Build Coastguard Worker HasOne = true;
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker }
268*9880d681SAndroid Build Coastguard Worker return HasOne;
269*9880d681SAndroid Build Coastguard Worker }
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker // Test whether it's safe to move Def to just before Insert.
272*9880d681SAndroid Build Coastguard Worker // TODO: Compute memory dependencies in a way that doesn't require always
273*9880d681SAndroid Build Coastguard Worker // walking the block.
274*9880d681SAndroid Build Coastguard Worker // TODO: Compute memory dependencies in a way that uses AliasAnalysis to be
275*9880d681SAndroid Build Coastguard Worker // more precise.
IsSafeToMove(const MachineInstr * Def,const MachineInstr * Insert,AliasAnalysis & AA,const LiveIntervals & LIS,const MachineRegisterInfo & MRI)276*9880d681SAndroid Build Coastguard Worker static bool IsSafeToMove(const MachineInstr *Def, const MachineInstr *Insert,
277*9880d681SAndroid Build Coastguard Worker AliasAnalysis &AA, const LiveIntervals &LIS,
278*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo &MRI) {
279*9880d681SAndroid Build Coastguard Worker assert(Def->getParent() == Insert->getParent());
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard Worker // Check for register dependencies.
282*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : Def->operands()) {
283*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || MO.isUndef())
284*9880d681SAndroid Build Coastguard Worker continue;
285*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
286*9880d681SAndroid Build Coastguard Worker
287*9880d681SAndroid Build Coastguard Worker // If the register is dead here and at Insert, ignore it.
288*9880d681SAndroid Build Coastguard Worker if (MO.isDead() && Insert->definesRegister(Reg) &&
289*9880d681SAndroid Build Coastguard Worker !Insert->readsRegister(Reg))
290*9880d681SAndroid Build Coastguard Worker continue;
291*9880d681SAndroid Build Coastguard Worker
292*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
293*9880d681SAndroid Build Coastguard Worker // Ignore ARGUMENTS; it's just used to keep the ARGUMENT_* instructions
294*9880d681SAndroid Build Coastguard Worker // from moving down, and we've already checked for that.
295*9880d681SAndroid Build Coastguard Worker if (Reg == WebAssembly::ARGUMENTS)
296*9880d681SAndroid Build Coastguard Worker continue;
297*9880d681SAndroid Build Coastguard Worker // If the physical register is never modified, ignore it.
298*9880d681SAndroid Build Coastguard Worker if (!MRI.isPhysRegModified(Reg))
299*9880d681SAndroid Build Coastguard Worker continue;
300*9880d681SAndroid Build Coastguard Worker // Otherwise, it's a physical register with unknown liveness.
301*9880d681SAndroid Build Coastguard Worker return false;
302*9880d681SAndroid Build Coastguard Worker }
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker // Ask LiveIntervals whether moving this virtual register use or def to
305*9880d681SAndroid Build Coastguard Worker // Insert will change which value numbers are seen.
306*9880d681SAndroid Build Coastguard Worker //
307*9880d681SAndroid Build Coastguard Worker // If the operand is a use of a register that is also defined in the same
308*9880d681SAndroid Build Coastguard Worker // instruction, test that the newly defined value reaches the insert point,
309*9880d681SAndroid Build Coastguard Worker // since the operand will be moving along with the def.
310*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LIS.getInterval(Reg);
311*9880d681SAndroid Build Coastguard Worker VNInfo *DefVNI =
312*9880d681SAndroid Build Coastguard Worker (MO.isDef() || Def->definesRegister(Reg)) ?
313*9880d681SAndroid Build Coastguard Worker LI.getVNInfoAt(LIS.getInstructionIndex(*Def).getRegSlot()) :
314*9880d681SAndroid Build Coastguard Worker LI.getVNInfoBefore(LIS.getInstructionIndex(*Def));
315*9880d681SAndroid Build Coastguard Worker assert(DefVNI && "Instruction input missing value number");
316*9880d681SAndroid Build Coastguard Worker VNInfo *InsVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*Insert));
317*9880d681SAndroid Build Coastguard Worker if (InsVNI && DefVNI != InsVNI)
318*9880d681SAndroid Build Coastguard Worker return false;
319*9880d681SAndroid Build Coastguard Worker }
320*9880d681SAndroid Build Coastguard Worker
321*9880d681SAndroid Build Coastguard Worker bool Read = false, Write = false, Effects = false, StackPointer = false;
322*9880d681SAndroid Build Coastguard Worker Query(*Def, AA, Read, Write, Effects, StackPointer);
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker // If the instruction does not access memory and has no side effects, it has
325*9880d681SAndroid Build Coastguard Worker // no additional dependencies.
326*9880d681SAndroid Build Coastguard Worker if (!Read && !Write && !Effects && !StackPointer)
327*9880d681SAndroid Build Coastguard Worker return true;
328*9880d681SAndroid Build Coastguard Worker
329*9880d681SAndroid Build Coastguard Worker // Scan through the intervening instructions between Def and Insert.
330*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::const_iterator D(Def), I(Insert);
331*9880d681SAndroid Build Coastguard Worker for (--I; I != D; --I) {
332*9880d681SAndroid Build Coastguard Worker bool InterveningRead = false;
333*9880d681SAndroid Build Coastguard Worker bool InterveningWrite = false;
334*9880d681SAndroid Build Coastguard Worker bool InterveningEffects = false;
335*9880d681SAndroid Build Coastguard Worker bool InterveningStackPointer = false;
336*9880d681SAndroid Build Coastguard Worker Query(*I, AA, InterveningRead, InterveningWrite, InterveningEffects,
337*9880d681SAndroid Build Coastguard Worker InterveningStackPointer);
338*9880d681SAndroid Build Coastguard Worker if (Effects && InterveningEffects)
339*9880d681SAndroid Build Coastguard Worker return false;
340*9880d681SAndroid Build Coastguard Worker if (Read && InterveningWrite)
341*9880d681SAndroid Build Coastguard Worker return false;
342*9880d681SAndroid Build Coastguard Worker if (Write && (InterveningRead || InterveningWrite))
343*9880d681SAndroid Build Coastguard Worker return false;
344*9880d681SAndroid Build Coastguard Worker if (StackPointer && InterveningStackPointer)
345*9880d681SAndroid Build Coastguard Worker return false;
346*9880d681SAndroid Build Coastguard Worker }
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker return true;
349*9880d681SAndroid Build Coastguard Worker }
350*9880d681SAndroid Build Coastguard Worker
351*9880d681SAndroid Build Coastguard Worker /// 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)352*9880d681SAndroid Build Coastguard Worker static bool OneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse,
353*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock &MBB,
354*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo &MRI,
355*9880d681SAndroid Build Coastguard Worker const MachineDominatorTree &MDT,
356*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS,
357*9880d681SAndroid Build Coastguard Worker WebAssemblyFunctionInfo &MFI) {
358*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LIS.getInterval(Reg);
359*9880d681SAndroid Build Coastguard Worker
360*9880d681SAndroid Build Coastguard Worker const MachineInstr *OneUseInst = OneUse.getParent();
361*9880d681SAndroid Build Coastguard Worker VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst));
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &Use : MRI.use_operands(Reg)) {
364*9880d681SAndroid Build Coastguard Worker if (&Use == &OneUse)
365*9880d681SAndroid Build Coastguard Worker continue;
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker const MachineInstr *UseInst = Use.getParent();
368*9880d681SAndroid Build Coastguard Worker VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst));
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker if (UseVNI != OneUseVNI)
371*9880d681SAndroid Build Coastguard Worker continue;
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker const MachineInstr *OneUseInst = OneUse.getParent();
374*9880d681SAndroid Build Coastguard Worker if (UseInst == OneUseInst) {
375*9880d681SAndroid Build Coastguard Worker // Another use in the same instruction. We need to ensure that the one
376*9880d681SAndroid Build Coastguard Worker // selected use happens "before" it.
377*9880d681SAndroid Build Coastguard Worker if (&OneUse > &Use)
378*9880d681SAndroid Build Coastguard Worker return false;
379*9880d681SAndroid Build Coastguard Worker } else {
380*9880d681SAndroid Build Coastguard Worker // Test that the use is dominated by the one selected use.
381*9880d681SAndroid Build Coastguard Worker while (!MDT.dominates(OneUseInst, UseInst)) {
382*9880d681SAndroid Build Coastguard Worker // Actually, dominating is over-conservative. Test that the use would
383*9880d681SAndroid Build Coastguard Worker // happen after the one selected use in the stack evaluation order.
384*9880d681SAndroid Build Coastguard Worker //
385*9880d681SAndroid Build Coastguard Worker // This is needed as a consequence of using implicit get_locals for
386*9880d681SAndroid Build Coastguard Worker // uses and implicit set_locals for defs.
387*9880d681SAndroid Build Coastguard Worker if (UseInst->getDesc().getNumDefs() == 0)
388*9880d681SAndroid Build Coastguard Worker return false;
389*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = UseInst->getOperand(0);
390*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
391*9880d681SAndroid Build Coastguard Worker return false;
392*9880d681SAndroid Build Coastguard Worker unsigned DefReg = MO.getReg();
393*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(DefReg) ||
394*9880d681SAndroid Build Coastguard Worker !MFI.isVRegStackified(DefReg))
395*9880d681SAndroid Build Coastguard Worker return false;
396*9880d681SAndroid Build Coastguard Worker assert(MRI.hasOneUse(DefReg));
397*9880d681SAndroid Build Coastguard Worker const MachineOperand &NewUse = *MRI.use_begin(DefReg);
398*9880d681SAndroid Build Coastguard Worker const MachineInstr *NewUseInst = NewUse.getParent();
399*9880d681SAndroid Build Coastguard Worker if (NewUseInst == OneUseInst) {
400*9880d681SAndroid Build Coastguard Worker if (&OneUse > &NewUse)
401*9880d681SAndroid Build Coastguard Worker return false;
402*9880d681SAndroid Build Coastguard Worker break;
403*9880d681SAndroid Build Coastguard Worker }
404*9880d681SAndroid Build Coastguard Worker UseInst = NewUseInst;
405*9880d681SAndroid Build Coastguard Worker }
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker }
408*9880d681SAndroid Build Coastguard Worker return true;
409*9880d681SAndroid Build Coastguard Worker }
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker /// Get the appropriate tee_local opcode for the given register class.
GetTeeLocalOpcode(const TargetRegisterClass * RC)412*9880d681SAndroid Build Coastguard Worker static unsigned GetTeeLocalOpcode(const TargetRegisterClass *RC) {
413*9880d681SAndroid Build Coastguard Worker if (RC == &WebAssembly::I32RegClass)
414*9880d681SAndroid Build Coastguard Worker return WebAssembly::TEE_LOCAL_I32;
415*9880d681SAndroid Build Coastguard Worker if (RC == &WebAssembly::I64RegClass)
416*9880d681SAndroid Build Coastguard Worker return WebAssembly::TEE_LOCAL_I64;
417*9880d681SAndroid Build Coastguard Worker if (RC == &WebAssembly::F32RegClass)
418*9880d681SAndroid Build Coastguard Worker return WebAssembly::TEE_LOCAL_F32;
419*9880d681SAndroid Build Coastguard Worker if (RC == &WebAssembly::F64RegClass)
420*9880d681SAndroid Build Coastguard Worker return WebAssembly::TEE_LOCAL_F64;
421*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unexpected register class");
422*9880d681SAndroid Build Coastguard Worker }
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker // Shrink LI to its uses, cleaning up LI.
ShrinkToUses(LiveInterval & LI,LiveIntervals & LIS)425*9880d681SAndroid Build Coastguard Worker static void ShrinkToUses(LiveInterval &LI, LiveIntervals &LIS) {
426*9880d681SAndroid Build Coastguard Worker if (LIS.shrinkToUses(&LI)) {
427*9880d681SAndroid Build Coastguard Worker SmallVector<LiveInterval*, 4> SplitLIs;
428*9880d681SAndroid Build Coastguard Worker LIS.splitSeparateComponents(LI, SplitLIs);
429*9880d681SAndroid Build Coastguard Worker }
430*9880d681SAndroid Build Coastguard Worker }
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker /// A single-use def in the same block with no intervening memory or register
433*9880d681SAndroid Build Coastguard Worker /// 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)434*9880d681SAndroid Build Coastguard Worker static MachineInstr *MoveForSingleUse(unsigned Reg, MachineOperand& Op,
435*9880d681SAndroid Build Coastguard Worker MachineInstr *Def,
436*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB,
437*9880d681SAndroid Build Coastguard Worker MachineInstr *Insert, LiveIntervals &LIS,
438*9880d681SAndroid Build Coastguard Worker WebAssemblyFunctionInfo &MFI,
439*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI) {
440*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Move for single use: "; Def->dump());
441*9880d681SAndroid Build Coastguard Worker
442*9880d681SAndroid Build Coastguard Worker MBB.splice(Insert, &MBB, Def);
443*9880d681SAndroid Build Coastguard Worker LIS.handleMove(*Def);
444*9880d681SAndroid Build Coastguard Worker
445*9880d681SAndroid Build Coastguard Worker if (MRI.hasOneDef(Reg) && MRI.hasOneUse(Reg)) {
446*9880d681SAndroid Build Coastguard Worker // No one else is using this register for anything so we can just stackify
447*9880d681SAndroid Build Coastguard Worker // it in place.
448*9880d681SAndroid Build Coastguard Worker MFI.stackifyVReg(Reg);
449*9880d681SAndroid Build Coastguard Worker } else {
450*9880d681SAndroid Build Coastguard Worker // The register may have unrelated uses or defs; create a new register for
451*9880d681SAndroid Build Coastguard Worker // just our one def and use so that we can stackify it.
452*9880d681SAndroid Build Coastguard Worker unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
453*9880d681SAndroid Build Coastguard Worker Def->getOperand(0).setReg(NewReg);
454*9880d681SAndroid Build Coastguard Worker Op.setReg(NewReg);
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard Worker // Tell LiveIntervals about the new register.
457*9880d681SAndroid Build Coastguard Worker LIS.createAndComputeVirtRegInterval(NewReg);
458*9880d681SAndroid Build Coastguard Worker
459*9880d681SAndroid Build Coastguard Worker // Tell LiveIntervals about the changes to the old register.
460*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(Reg);
461*9880d681SAndroid Build Coastguard Worker LI.removeSegment(LIS.getInstructionIndex(*Def).getRegSlot(),
462*9880d681SAndroid Build Coastguard Worker LIS.getInstructionIndex(*Op.getParent()).getRegSlot(),
463*9880d681SAndroid Build Coastguard Worker /*RemoveDeadValNo=*/true);
464*9880d681SAndroid Build Coastguard Worker
465*9880d681SAndroid Build Coastguard Worker MFI.stackifyVReg(NewReg);
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " - Replaced register: "; Def->dump());
468*9880d681SAndroid Build Coastguard Worker }
469*9880d681SAndroid Build Coastguard Worker
470*9880d681SAndroid Build Coastguard Worker ImposeStackOrdering(Def);
471*9880d681SAndroid Build Coastguard Worker return Def;
472*9880d681SAndroid Build Coastguard Worker }
473*9880d681SAndroid Build Coastguard Worker
474*9880d681SAndroid Build Coastguard Worker /// A trivially cloneable instruction; clone it and nest the new copy with the
475*9880d681SAndroid Build Coastguard Worker /// 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)476*9880d681SAndroid Build Coastguard Worker static MachineInstr *RematerializeCheapDef(
477*9880d681SAndroid Build Coastguard Worker unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB,
478*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS,
479*9880d681SAndroid Build Coastguard Worker WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI,
480*9880d681SAndroid Build Coastguard Worker const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI) {
481*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump());
482*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " - for use in "; Op.getParent()->dump());
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard Worker unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
485*9880d681SAndroid Build Coastguard Worker TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI);
486*9880d681SAndroid Build Coastguard Worker Op.setReg(NewReg);
487*9880d681SAndroid Build Coastguard Worker MachineInstr *Clone = &*std::prev(Insert);
488*9880d681SAndroid Build Coastguard Worker LIS.InsertMachineInstrInMaps(*Clone);
489*9880d681SAndroid Build Coastguard Worker LIS.createAndComputeVirtRegInterval(NewReg);
490*9880d681SAndroid Build Coastguard Worker MFI.stackifyVReg(NewReg);
491*9880d681SAndroid Build Coastguard Worker ImposeStackOrdering(Clone);
492*9880d681SAndroid Build Coastguard Worker
493*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " - Cloned to "; Clone->dump());
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard Worker // Shrink the interval.
496*9880d681SAndroid Build Coastguard Worker bool IsDead = MRI.use_empty(Reg);
497*9880d681SAndroid Build Coastguard Worker if (!IsDead) {
498*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(Reg);
499*9880d681SAndroid Build Coastguard Worker ShrinkToUses(LI, LIS);
500*9880d681SAndroid Build Coastguard Worker IsDead = !LI.liveAt(LIS.getInstructionIndex(Def).getDeadSlot());
501*9880d681SAndroid Build Coastguard Worker }
502*9880d681SAndroid Build Coastguard Worker
503*9880d681SAndroid Build Coastguard Worker // If that was the last use of the original, delete the original.
504*9880d681SAndroid Build Coastguard Worker if (IsDead) {
505*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " - Deleting original\n");
506*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot();
507*9880d681SAndroid Build Coastguard Worker LIS.removePhysRegDefAt(WebAssembly::ARGUMENTS, Idx);
508*9880d681SAndroid Build Coastguard Worker LIS.removeInterval(Reg);
509*9880d681SAndroid Build Coastguard Worker LIS.RemoveMachineInstrFromMaps(Def);
510*9880d681SAndroid Build Coastguard Worker Def.eraseFromParent();
511*9880d681SAndroid Build Coastguard Worker }
512*9880d681SAndroid Build Coastguard Worker
513*9880d681SAndroid Build Coastguard Worker return Clone;
514*9880d681SAndroid Build Coastguard Worker }
515*9880d681SAndroid Build Coastguard Worker
516*9880d681SAndroid Build Coastguard Worker /// A multiple-use def in the same block with no intervening memory or register
517*9880d681SAndroid Build Coastguard Worker /// dependencies; move the def down, nest it with the current instruction, and
518*9880d681SAndroid Build Coastguard Worker /// insert a tee_local to satisfy the rest of the uses. As an illustration,
519*9880d681SAndroid Build Coastguard Worker /// rewrite this:
520*9880d681SAndroid Build Coastguard Worker ///
521*9880d681SAndroid Build Coastguard Worker /// Reg = INST ... // Def
522*9880d681SAndroid Build Coastguard Worker /// INST ..., Reg, ... // Insert
523*9880d681SAndroid Build Coastguard Worker /// INST ..., Reg, ...
524*9880d681SAndroid Build Coastguard Worker /// INST ..., Reg, ...
525*9880d681SAndroid Build Coastguard Worker ///
526*9880d681SAndroid Build Coastguard Worker /// to this:
527*9880d681SAndroid Build Coastguard Worker ///
528*9880d681SAndroid Build Coastguard Worker /// DefReg = INST ... // Def (to become the new Insert)
529*9880d681SAndroid Build Coastguard Worker /// TeeReg, Reg = TEE_LOCAL_... DefReg
530*9880d681SAndroid Build Coastguard Worker /// INST ..., TeeReg, ... // Insert
531*9880d681SAndroid Build Coastguard Worker /// INST ..., Reg, ...
532*9880d681SAndroid Build Coastguard Worker /// INST ..., Reg, ...
533*9880d681SAndroid Build Coastguard Worker ///
534*9880d681SAndroid Build Coastguard Worker /// with DefReg and TeeReg stackified. This eliminates a get_local from the
535*9880d681SAndroid Build Coastguard Worker /// resulting code.
MoveAndTeeForMultiUse(unsigned Reg,MachineOperand & Op,MachineInstr * Def,MachineBasicBlock & MBB,MachineInstr * Insert,LiveIntervals & LIS,WebAssemblyFunctionInfo & MFI,MachineRegisterInfo & MRI,const WebAssemblyInstrInfo * TII)536*9880d681SAndroid Build Coastguard Worker static MachineInstr *MoveAndTeeForMultiUse(
537*9880d681SAndroid Build Coastguard Worker unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB,
538*9880d681SAndroid Build Coastguard Worker MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI,
539*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) {
540*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump());
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker // Move Def into place.
543*9880d681SAndroid Build Coastguard Worker MBB.splice(Insert, &MBB, Def);
544*9880d681SAndroid Build Coastguard Worker LIS.handleMove(*Def);
545*9880d681SAndroid Build Coastguard Worker
546*9880d681SAndroid Build Coastguard Worker // Create the Tee and attach the registers.
547*9880d681SAndroid Build Coastguard Worker const auto *RegClass = MRI.getRegClass(Reg);
548*9880d681SAndroid Build Coastguard Worker unsigned TeeReg = MRI.createVirtualRegister(RegClass);
549*9880d681SAndroid Build Coastguard Worker unsigned DefReg = MRI.createVirtualRegister(RegClass);
550*9880d681SAndroid Build Coastguard Worker MachineOperand &DefMO = Def->getOperand(0);
551*9880d681SAndroid Build Coastguard Worker MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(),
552*9880d681SAndroid Build Coastguard Worker TII->get(GetTeeLocalOpcode(RegClass)), TeeReg)
553*9880d681SAndroid Build Coastguard Worker .addReg(Reg, RegState::Define)
554*9880d681SAndroid Build Coastguard Worker .addReg(DefReg, getUndefRegState(DefMO.isDead()));
555*9880d681SAndroid Build Coastguard Worker Op.setReg(TeeReg);
556*9880d681SAndroid Build Coastguard Worker DefMO.setReg(DefReg);
557*9880d681SAndroid Build Coastguard Worker SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot();
558*9880d681SAndroid Build Coastguard Worker SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot();
559*9880d681SAndroid Build Coastguard Worker
560*9880d681SAndroid Build Coastguard Worker // Tell LiveIntervals we moved the original vreg def from Def to Tee.
561*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LIS.getInterval(Reg);
562*9880d681SAndroid Build Coastguard Worker LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx);
563*9880d681SAndroid Build Coastguard Worker VNInfo *ValNo = LI.getVNInfoAt(DefIdx);
564*9880d681SAndroid Build Coastguard Worker I->start = TeeIdx;
565*9880d681SAndroid Build Coastguard Worker ValNo->def = TeeIdx;
566*9880d681SAndroid Build Coastguard Worker ShrinkToUses(LI, LIS);
567*9880d681SAndroid Build Coastguard Worker
568*9880d681SAndroid Build Coastguard Worker // Finish stackifying the new regs.
569*9880d681SAndroid Build Coastguard Worker LIS.createAndComputeVirtRegInterval(TeeReg);
570*9880d681SAndroid Build Coastguard Worker LIS.createAndComputeVirtRegInterval(DefReg);
571*9880d681SAndroid Build Coastguard Worker MFI.stackifyVReg(DefReg);
572*9880d681SAndroid Build Coastguard Worker MFI.stackifyVReg(TeeReg);
573*9880d681SAndroid Build Coastguard Worker ImposeStackOrdering(Def);
574*9880d681SAndroid Build Coastguard Worker ImposeStackOrdering(Tee);
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " - Replaced register: "; Def->dump());
577*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " - Tee instruction: "; Tee->dump());
578*9880d681SAndroid Build Coastguard Worker return Def;
579*9880d681SAndroid Build Coastguard Worker }
580*9880d681SAndroid Build Coastguard Worker
581*9880d681SAndroid Build Coastguard Worker namespace {
582*9880d681SAndroid Build Coastguard Worker /// A stack for walking the tree of instructions being built, visiting the
583*9880d681SAndroid Build Coastguard Worker /// MachineOperands in DFS order.
584*9880d681SAndroid Build Coastguard Worker class TreeWalkerState {
585*9880d681SAndroid Build Coastguard Worker typedef MachineInstr::mop_iterator mop_iterator;
586*9880d681SAndroid Build Coastguard Worker typedef std::reverse_iterator<mop_iterator> mop_reverse_iterator;
587*9880d681SAndroid Build Coastguard Worker typedef iterator_range<mop_reverse_iterator> RangeTy;
588*9880d681SAndroid Build Coastguard Worker SmallVector<RangeTy, 4> Worklist;
589*9880d681SAndroid Build Coastguard Worker
590*9880d681SAndroid Build Coastguard Worker public:
TreeWalkerState(MachineInstr * Insert)591*9880d681SAndroid Build Coastguard Worker explicit TreeWalkerState(MachineInstr *Insert) {
592*9880d681SAndroid Build Coastguard Worker const iterator_range<mop_iterator> &Range = Insert->explicit_uses();
593*9880d681SAndroid Build Coastguard Worker if (Range.begin() != Range.end())
594*9880d681SAndroid Build Coastguard Worker Worklist.push_back(reverse(Range));
595*9880d681SAndroid Build Coastguard Worker }
596*9880d681SAndroid Build Coastguard Worker
Done() const597*9880d681SAndroid Build Coastguard Worker bool Done() const { return Worklist.empty(); }
598*9880d681SAndroid Build Coastguard Worker
Pop()599*9880d681SAndroid Build Coastguard Worker MachineOperand &Pop() {
600*9880d681SAndroid Build Coastguard Worker RangeTy &Range = Worklist.back();
601*9880d681SAndroid Build Coastguard Worker MachineOperand &Op = *Range.begin();
602*9880d681SAndroid Build Coastguard Worker Range = drop_begin(Range, 1);
603*9880d681SAndroid Build Coastguard Worker if (Range.begin() == Range.end())
604*9880d681SAndroid Build Coastguard Worker Worklist.pop_back();
605*9880d681SAndroid Build Coastguard Worker assert((Worklist.empty() ||
606*9880d681SAndroid Build Coastguard Worker Worklist.back().begin() != Worklist.back().end()) &&
607*9880d681SAndroid Build Coastguard Worker "Empty ranges shouldn't remain in the worklist");
608*9880d681SAndroid Build Coastguard Worker return Op;
609*9880d681SAndroid Build Coastguard Worker }
610*9880d681SAndroid Build Coastguard Worker
611*9880d681SAndroid Build Coastguard Worker /// Push Instr's operands onto the stack to be visited.
PushOperands(MachineInstr * Instr)612*9880d681SAndroid Build Coastguard Worker void PushOperands(MachineInstr *Instr) {
613*9880d681SAndroid Build Coastguard Worker const iterator_range<mop_iterator> &Range(Instr->explicit_uses());
614*9880d681SAndroid Build Coastguard Worker if (Range.begin() != Range.end())
615*9880d681SAndroid Build Coastguard Worker Worklist.push_back(reverse(Range));
616*9880d681SAndroid Build Coastguard Worker }
617*9880d681SAndroid Build Coastguard Worker
618*9880d681SAndroid Build Coastguard Worker /// Some of Instr's operands are on the top of the stack; remove them and
619*9880d681SAndroid Build Coastguard Worker /// re-insert them starting from the beginning (because we've commuted them).
ResetTopOperands(MachineInstr * Instr)620*9880d681SAndroid Build Coastguard Worker void ResetTopOperands(MachineInstr *Instr) {
621*9880d681SAndroid Build Coastguard Worker assert(HasRemainingOperands(Instr) &&
622*9880d681SAndroid Build Coastguard Worker "Reseting operands should only be done when the instruction has "
623*9880d681SAndroid Build Coastguard Worker "an operand still on the stack");
624*9880d681SAndroid Build Coastguard Worker Worklist.back() = reverse(Instr->explicit_uses());
625*9880d681SAndroid Build Coastguard Worker }
626*9880d681SAndroid Build Coastguard Worker
627*9880d681SAndroid Build Coastguard Worker /// Test whether Instr has operands remaining to be visited at the top of
628*9880d681SAndroid Build Coastguard Worker /// the stack.
HasRemainingOperands(const MachineInstr * Instr) const629*9880d681SAndroid Build Coastguard Worker bool HasRemainingOperands(const MachineInstr *Instr) const {
630*9880d681SAndroid Build Coastguard Worker if (Worklist.empty())
631*9880d681SAndroid Build Coastguard Worker return false;
632*9880d681SAndroid Build Coastguard Worker const RangeTy &Range = Worklist.back();
633*9880d681SAndroid Build Coastguard Worker return Range.begin() != Range.end() && Range.begin()->getParent() == Instr;
634*9880d681SAndroid Build Coastguard Worker }
635*9880d681SAndroid Build Coastguard Worker
636*9880d681SAndroid Build Coastguard Worker /// Test whether the given register is present on the stack, indicating an
637*9880d681SAndroid Build Coastguard Worker /// operand in the tree that we haven't visited yet. Moving a definition of
638*9880d681SAndroid Build Coastguard Worker /// Reg to a point in the tree after that would change its value.
639*9880d681SAndroid Build Coastguard Worker ///
640*9880d681SAndroid Build Coastguard Worker /// This is needed as a consequence of using implicit get_locals for
641*9880d681SAndroid Build Coastguard Worker /// uses and implicit set_locals for defs.
IsOnStack(unsigned Reg) const642*9880d681SAndroid Build Coastguard Worker bool IsOnStack(unsigned Reg) const {
643*9880d681SAndroid Build Coastguard Worker for (const RangeTy &Range : Worklist)
644*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : Range)
645*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.getReg() == Reg)
646*9880d681SAndroid Build Coastguard Worker return true;
647*9880d681SAndroid Build Coastguard Worker return false;
648*9880d681SAndroid Build Coastguard Worker }
649*9880d681SAndroid Build Coastguard Worker };
650*9880d681SAndroid Build Coastguard Worker
651*9880d681SAndroid Build Coastguard Worker /// State to keep track of whether commuting is in flight or whether it's been
652*9880d681SAndroid Build Coastguard Worker /// tried for the current instruction and didn't work.
653*9880d681SAndroid Build Coastguard Worker class CommutingState {
654*9880d681SAndroid Build Coastguard Worker /// There are effectively three states: the initial state where we haven't
655*9880d681SAndroid Build Coastguard Worker /// started commuting anything and we don't know anything yet, the tenative
656*9880d681SAndroid Build Coastguard Worker /// state where we've commuted the operands of the current instruction and are
657*9880d681SAndroid Build Coastguard Worker /// revisting it, and the declined state where we've reverted the operands
658*9880d681SAndroid Build Coastguard Worker /// back to their original order and will no longer commute it further.
659*9880d681SAndroid Build Coastguard Worker bool TentativelyCommuting;
660*9880d681SAndroid Build Coastguard Worker bool Declined;
661*9880d681SAndroid Build Coastguard Worker
662*9880d681SAndroid Build Coastguard Worker /// During the tentative state, these hold the operand indices of the commuted
663*9880d681SAndroid Build Coastguard Worker /// operands.
664*9880d681SAndroid Build Coastguard Worker unsigned Operand0, Operand1;
665*9880d681SAndroid Build Coastguard Worker
666*9880d681SAndroid Build Coastguard Worker public:
CommutingState()667*9880d681SAndroid Build Coastguard Worker CommutingState() : TentativelyCommuting(false), Declined(false) {}
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard Worker /// Stackification for an operand was not successful due to ordering
670*9880d681SAndroid Build Coastguard Worker /// constraints. If possible, and if we haven't already tried it and declined
671*9880d681SAndroid Build Coastguard Worker /// it, commute Insert's operands and prepare to revisit it.
MaybeCommute(MachineInstr * Insert,TreeWalkerState & TreeWalker,const WebAssemblyInstrInfo * TII)672*9880d681SAndroid Build Coastguard Worker void MaybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker,
673*9880d681SAndroid Build Coastguard Worker const WebAssemblyInstrInfo *TII) {
674*9880d681SAndroid Build Coastguard Worker if (TentativelyCommuting) {
675*9880d681SAndroid Build Coastguard Worker assert(!Declined &&
676*9880d681SAndroid Build Coastguard Worker "Don't decline commuting until you've finished trying it");
677*9880d681SAndroid Build Coastguard Worker // Commuting didn't help. Revert it.
678*9880d681SAndroid Build Coastguard Worker TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1);
679*9880d681SAndroid Build Coastguard Worker TentativelyCommuting = false;
680*9880d681SAndroid Build Coastguard Worker Declined = true;
681*9880d681SAndroid Build Coastguard Worker } else if (!Declined && TreeWalker.HasRemainingOperands(Insert)) {
682*9880d681SAndroid Build Coastguard Worker Operand0 = TargetInstrInfo::CommuteAnyOperandIndex;
683*9880d681SAndroid Build Coastguard Worker Operand1 = TargetInstrInfo::CommuteAnyOperandIndex;
684*9880d681SAndroid Build Coastguard Worker if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) {
685*9880d681SAndroid Build Coastguard Worker // Tentatively commute the operands and try again.
686*9880d681SAndroid Build Coastguard Worker TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1);
687*9880d681SAndroid Build Coastguard Worker TreeWalker.ResetTopOperands(Insert);
688*9880d681SAndroid Build Coastguard Worker TentativelyCommuting = true;
689*9880d681SAndroid Build Coastguard Worker Declined = false;
690*9880d681SAndroid Build Coastguard Worker }
691*9880d681SAndroid Build Coastguard Worker }
692*9880d681SAndroid Build Coastguard Worker }
693*9880d681SAndroid Build Coastguard Worker
694*9880d681SAndroid Build Coastguard Worker /// Stackification for some operand was successful. Reset to the default
695*9880d681SAndroid Build Coastguard Worker /// state.
Reset()696*9880d681SAndroid Build Coastguard Worker void Reset() {
697*9880d681SAndroid Build Coastguard Worker TentativelyCommuting = false;
698*9880d681SAndroid Build Coastguard Worker Declined = false;
699*9880d681SAndroid Build Coastguard Worker }
700*9880d681SAndroid Build Coastguard Worker };
701*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
702*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & MF)703*9880d681SAndroid Build Coastguard Worker bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
704*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "********** Register Stackifying **********\n"
705*9880d681SAndroid Build Coastguard Worker "********** Function: "
706*9880d681SAndroid Build Coastguard Worker << MF.getName() << '\n');
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker bool Changed = false;
709*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF.getRegInfo();
710*9880d681SAndroid Build Coastguard Worker WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
711*9880d681SAndroid Build Coastguard Worker const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
712*9880d681SAndroid Build Coastguard Worker const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo();
713*9880d681SAndroid Build Coastguard Worker AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
714*9880d681SAndroid Build Coastguard Worker MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
715*9880d681SAndroid Build Coastguard Worker LiveIntervals &LIS = getAnalysis<LiveIntervals>();
716*9880d681SAndroid Build Coastguard Worker
717*9880d681SAndroid Build Coastguard Worker // Walk the instructions from the bottom up. Currently we don't look past
718*9880d681SAndroid Build Coastguard Worker // block boundaries, and the blocks aren't ordered so the block visitation
719*9880d681SAndroid Build Coastguard Worker // order isn't significant, but we may want to change this in the future.
720*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock &MBB : MF) {
721*9880d681SAndroid Build Coastguard Worker // Don't use a range-based for loop, because we modify the list as we're
722*9880d681SAndroid Build Coastguard Worker // iterating over it and the end iterator may change.
723*9880d681SAndroid Build Coastguard Worker for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
724*9880d681SAndroid Build Coastguard Worker MachineInstr *Insert = &*MII;
725*9880d681SAndroid Build Coastguard Worker // Don't nest anything inside an inline asm, because we don't have
726*9880d681SAndroid Build Coastguard Worker // constraints for $push inputs.
727*9880d681SAndroid Build Coastguard Worker if (Insert->getOpcode() == TargetOpcode::INLINEASM)
728*9880d681SAndroid Build Coastguard Worker continue;
729*9880d681SAndroid Build Coastguard Worker
730*9880d681SAndroid Build Coastguard Worker // Ignore debugging intrinsics.
731*9880d681SAndroid Build Coastguard Worker if (Insert->getOpcode() == TargetOpcode::DBG_VALUE)
732*9880d681SAndroid Build Coastguard Worker continue;
733*9880d681SAndroid Build Coastguard Worker
734*9880d681SAndroid Build Coastguard Worker // Iterate through the inputs in reverse order, since we'll be pulling
735*9880d681SAndroid Build Coastguard Worker // operands off the stack in LIFO order.
736*9880d681SAndroid Build Coastguard Worker CommutingState Commuting;
737*9880d681SAndroid Build Coastguard Worker TreeWalkerState TreeWalker(Insert);
738*9880d681SAndroid Build Coastguard Worker while (!TreeWalker.Done()) {
739*9880d681SAndroid Build Coastguard Worker MachineOperand &Op = TreeWalker.Pop();
740*9880d681SAndroid Build Coastguard Worker
741*9880d681SAndroid Build Coastguard Worker // We're only interested in explicit virtual register operands.
742*9880d681SAndroid Build Coastguard Worker if (!Op.isReg())
743*9880d681SAndroid Build Coastguard Worker continue;
744*9880d681SAndroid Build Coastguard Worker
745*9880d681SAndroid Build Coastguard Worker unsigned Reg = Op.getReg();
746*9880d681SAndroid Build Coastguard Worker assert(Op.isUse() && "explicit_uses() should only iterate over uses");
747*9880d681SAndroid Build Coastguard Worker assert(!Op.isImplicit() &&
748*9880d681SAndroid Build Coastguard Worker "explicit_uses() should only iterate over explicit operands");
749*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg))
750*9880d681SAndroid Build Coastguard Worker continue;
751*9880d681SAndroid Build Coastguard Worker
752*9880d681SAndroid Build Coastguard Worker // Identify the definition for this register at this point. Most
753*9880d681SAndroid Build Coastguard Worker // registers are in SSA form here so we try a quick MRI query first.
754*9880d681SAndroid Build Coastguard Worker MachineInstr *Def = GetVRegDef(Reg, Insert, MRI, LIS);
755*9880d681SAndroid Build Coastguard Worker if (!Def)
756*9880d681SAndroid Build Coastguard Worker continue;
757*9880d681SAndroid Build Coastguard Worker
758*9880d681SAndroid Build Coastguard Worker // Don't nest an INLINE_ASM def into anything, because we don't have
759*9880d681SAndroid Build Coastguard Worker // constraints for $pop outputs.
760*9880d681SAndroid Build Coastguard Worker if (Def->getOpcode() == TargetOpcode::INLINEASM)
761*9880d681SAndroid Build Coastguard Worker continue;
762*9880d681SAndroid Build Coastguard Worker
763*9880d681SAndroid Build Coastguard Worker // Argument instructions represent live-in registers and not real
764*9880d681SAndroid Build Coastguard Worker // instructions.
765*9880d681SAndroid Build Coastguard Worker if (Def->getOpcode() == WebAssembly::ARGUMENT_I32 ||
766*9880d681SAndroid Build Coastguard Worker Def->getOpcode() == WebAssembly::ARGUMENT_I64 ||
767*9880d681SAndroid Build Coastguard Worker Def->getOpcode() == WebAssembly::ARGUMENT_F32 ||
768*9880d681SAndroid Build Coastguard Worker Def->getOpcode() == WebAssembly::ARGUMENT_F64)
769*9880d681SAndroid Build Coastguard Worker continue;
770*9880d681SAndroid Build Coastguard Worker
771*9880d681SAndroid Build Coastguard Worker // Decide which strategy to take. Prefer to move a single-use value
772*9880d681SAndroid Build Coastguard Worker // over cloning it, and prefer cloning over introducing a tee_local.
773*9880d681SAndroid Build Coastguard Worker // For moving, we require the def to be in the same block as the use;
774*9880d681SAndroid Build Coastguard Worker // this makes things simpler (LiveIntervals' handleMove function only
775*9880d681SAndroid Build Coastguard Worker // supports intra-block moves) and it's MachineSink's job to catch all
776*9880d681SAndroid Build Coastguard Worker // the sinking opportunities anyway.
777*9880d681SAndroid Build Coastguard Worker bool SameBlock = Def->getParent() == &MBB;
778*9880d681SAndroid Build Coastguard Worker bool CanMove = SameBlock && IsSafeToMove(Def, Insert, AA, LIS, MRI) &&
779*9880d681SAndroid Build Coastguard Worker !TreeWalker.IsOnStack(Reg);
780*9880d681SAndroid Build Coastguard Worker if (CanMove && HasOneUse(Reg, Def, MRI, MDT, LIS)) {
781*9880d681SAndroid Build Coastguard Worker Insert = MoveForSingleUse(Reg, Op, Def, MBB, Insert, LIS, MFI, MRI);
782*9880d681SAndroid Build Coastguard Worker } else if (ShouldRematerialize(*Def, AA, TII)) {
783*9880d681SAndroid Build Coastguard Worker Insert =
784*9880d681SAndroid Build Coastguard Worker RematerializeCheapDef(Reg, Op, *Def, MBB, Insert->getIterator(),
785*9880d681SAndroid Build Coastguard Worker LIS, MFI, MRI, TII, TRI);
786*9880d681SAndroid Build Coastguard Worker } else if (CanMove &&
787*9880d681SAndroid Build Coastguard Worker OneUseDominatesOtherUses(Reg, Op, MBB, MRI, MDT, LIS, MFI)) {
788*9880d681SAndroid Build Coastguard Worker Insert = MoveAndTeeForMultiUse(Reg, Op, Def, MBB, Insert, LIS, MFI,
789*9880d681SAndroid Build Coastguard Worker MRI, TII);
790*9880d681SAndroid Build Coastguard Worker } else {
791*9880d681SAndroid Build Coastguard Worker // We failed to stackify the operand. If the problem was ordering
792*9880d681SAndroid Build Coastguard Worker // constraints, Commuting may be able to help.
793*9880d681SAndroid Build Coastguard Worker if (!CanMove && SameBlock)
794*9880d681SAndroid Build Coastguard Worker Commuting.MaybeCommute(Insert, TreeWalker, TII);
795*9880d681SAndroid Build Coastguard Worker // Proceed to the next operand.
796*9880d681SAndroid Build Coastguard Worker continue;
797*9880d681SAndroid Build Coastguard Worker }
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker // We stackified an operand. Add the defining instruction's operands to
800*9880d681SAndroid Build Coastguard Worker // the worklist stack now to continue to build an ever deeper tree.
801*9880d681SAndroid Build Coastguard Worker Commuting.Reset();
802*9880d681SAndroid Build Coastguard Worker TreeWalker.PushOperands(Insert);
803*9880d681SAndroid Build Coastguard Worker }
804*9880d681SAndroid Build Coastguard Worker
805*9880d681SAndroid Build Coastguard Worker // If we stackified any operands, skip over the tree to start looking for
806*9880d681SAndroid Build Coastguard Worker // the next instruction we can build a tree on.
807*9880d681SAndroid Build Coastguard Worker if (Insert != &*MII) {
808*9880d681SAndroid Build Coastguard Worker ImposeStackOrdering(&*MII);
809*9880d681SAndroid Build Coastguard Worker MII = std::prev(
810*9880d681SAndroid Build Coastguard Worker llvm::make_reverse_iterator(MachineBasicBlock::iterator(Insert)));
811*9880d681SAndroid Build Coastguard Worker Changed = true;
812*9880d681SAndroid Build Coastguard Worker }
813*9880d681SAndroid Build Coastguard Worker }
814*9880d681SAndroid Build Coastguard Worker }
815*9880d681SAndroid Build Coastguard Worker
816*9880d681SAndroid Build Coastguard Worker // If we used EXPR_STACK anywhere, add it to the live-in sets everywhere so
817*9880d681SAndroid Build Coastguard Worker // that it never looks like a use-before-def.
818*9880d681SAndroid Build Coastguard Worker if (Changed) {
819*9880d681SAndroid Build Coastguard Worker MF.getRegInfo().addLiveIn(WebAssembly::EXPR_STACK);
820*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock &MBB : MF)
821*9880d681SAndroid Build Coastguard Worker MBB.addLiveIn(WebAssembly::EXPR_STACK);
822*9880d681SAndroid Build Coastguard Worker }
823*9880d681SAndroid Build Coastguard Worker
824*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
825*9880d681SAndroid Build Coastguard Worker // Verify that pushes and pops are performed in LIFO order.
826*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 0> Stack;
827*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock &MBB : MF) {
828*9880d681SAndroid Build Coastguard Worker for (MachineInstr &MI : MBB) {
829*9880d681SAndroid Build Coastguard Worker if (MI.isDebugValue())
830*9880d681SAndroid Build Coastguard Worker continue;
831*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : reverse(MI.explicit_operands())) {
832*9880d681SAndroid Build Coastguard Worker if (!MO.isReg())
833*9880d681SAndroid Build Coastguard Worker continue;
834*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
835*9880d681SAndroid Build Coastguard Worker
836*9880d681SAndroid Build Coastguard Worker if (MFI.isVRegStackified(Reg)) {
837*9880d681SAndroid Build Coastguard Worker if (MO.isDef())
838*9880d681SAndroid Build Coastguard Worker Stack.push_back(Reg);
839*9880d681SAndroid Build Coastguard Worker else
840*9880d681SAndroid Build Coastguard Worker assert(Stack.pop_back_val() == Reg &&
841*9880d681SAndroid Build Coastguard Worker "Register stack pop should be paired with a push");
842*9880d681SAndroid Build Coastguard Worker }
843*9880d681SAndroid Build Coastguard Worker }
844*9880d681SAndroid Build Coastguard Worker }
845*9880d681SAndroid Build Coastguard Worker // TODO: Generalize this code to support keeping values on the stack across
846*9880d681SAndroid Build Coastguard Worker // basic block boundaries.
847*9880d681SAndroid Build Coastguard Worker assert(Stack.empty() &&
848*9880d681SAndroid Build Coastguard Worker "Register stack pushes and pops should be balanced");
849*9880d681SAndroid Build Coastguard Worker }
850*9880d681SAndroid Build Coastguard Worker #endif
851*9880d681SAndroid Build Coastguard Worker
852*9880d681SAndroid Build Coastguard Worker return Changed;
853*9880d681SAndroid Build Coastguard Worker }
854