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