1 //===-- WebAssemblyDebugFixup.cpp - Debug Fixup ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Several prior passes may "stackify" registers, here we ensure any references
11 /// in such registers in debug_value instructions become stack relative also.
12 /// This is done in a separate pass such that not all previous passes need to
13 /// track stack depth when values get stackified.
15 //===----------------------------------------------------------------------===//
17 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18 #include "Utils/WebAssemblyUtilities.h"
19 #include "WebAssembly.h"
20 #include "WebAssemblyMachineFunctionInfo.h"
21 #include "WebAssemblySubtarget.h"
22 #include "llvm/ADT/SCCIterator.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineLoopInfo.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "wasm-debug-fixup"
36 class WebAssemblyDebugFixup final
: public MachineFunctionPass
{
37 StringRef
getPassName() const override
{ return "WebAssembly Debug Fixup"; }
39 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
41 MachineFunctionPass::getAnalysisUsage(AU
);
44 bool runOnMachineFunction(MachineFunction
&MF
) override
;
47 static char ID
; // Pass identification, replacement for typeid
48 WebAssemblyDebugFixup() : MachineFunctionPass(ID
) {}
50 } // end anonymous namespace
52 char WebAssemblyDebugFixup::ID
= 0;
54 WebAssemblyDebugFixup
, DEBUG_TYPE
,
55 "Ensures debug_value's that have been stackified become stack relative",
58 FunctionPass
*llvm::createWebAssemblyDebugFixup() {
59 return new WebAssemblyDebugFixup();
62 bool WebAssemblyDebugFixup::runOnMachineFunction(MachineFunction
&MF
) {
63 LLVM_DEBUG(dbgs() << "********** Debug Fixup **********\n"
64 "********** Function: "
65 << MF
.getName() << '\n');
67 WebAssemblyFunctionInfo
&MFI
= *MF
.getInfo
<WebAssemblyFunctionInfo
>();
68 const auto *TII
= MF
.getSubtarget
<WebAssemblySubtarget
>().getInstrInfo();
72 MachineInstr
*DebugValue
;
74 std::vector
<StackElem
> Stack
;
75 for (MachineBasicBlock
&MBB
: MF
) {
76 // We may insert into this list.
77 for (auto MII
= MBB
.begin(); MII
!= MBB
.end(); ++MII
) {
78 MachineInstr
&MI
= *MII
;
79 if (MI
.isDebugValue()) {
80 auto &MO
= MI
.getOperand(0);
81 // Also check if not a $noreg: likely a DBG_VALUE we just inserted.
82 if (MO
.isReg() && MO
.getReg().isValid() &&
83 MFI
.isVRegStackified(MO
.getReg())) {
84 // Found a DBG_VALUE with a stackified register we will
85 // change into a stack operand.
86 // Search for register rather than assume it is on top (which it
87 // typically is if it appears right after the def), since
88 // DBG_VALUE's may shift under some circumstances.
89 for (auto &Elem
: reverse(Stack
)) {
90 if (MO
.getReg() == Elem
.Reg
) {
91 auto Depth
= static_cast<unsigned>(&Elem
- &Stack
[0]);
92 LLVM_DEBUG(dbgs() << "Debug Value VReg " << MO
.getReg()
93 << " -> Stack Relative " << Depth
<< "\n");
94 MO
.ChangeToTargetIndex(WebAssembly::TI_OPERAND_STACK
, Depth
);
95 // Save the DBG_VALUE instruction that defined this stackified
96 // variable since later we need it to construct another one on
98 Elem
.DebugValue
= &MI
;
102 // If the Reg was not found, we have a DBG_VALUE outside of its
103 // def-use range, and we leave it unmodified as reg, which means
104 // it will be culled later.
107 // Track stack depth.
108 for (MachineOperand
&MO
: reverse(MI
.explicit_uses())) {
109 if (MO
.isReg() && MFI
.isVRegStackified(MO
.getReg())) {
110 auto Prev
= Stack
.back();
112 assert(Prev
.Reg
== MO
.getReg() &&
113 "WebAssemblyDebugFixup: Pop: Register not matched!");
114 // We should not put a DBG_VALUE after a terminator; debug ranges
115 // are terminated at the end of a BB anyway.
116 if (Prev
.DebugValue
&& !MI
.isTerminator()) {
117 // This stackified reg is a variable that started life at
118 // Prev.DebugValue, so now that we're popping it we must insert
119 // a $noreg DBG_VALUE for the variable to end it, right after
120 // the current instruction.
121 BuildMI(*Prev
.DebugValue
->getParent(), std::next(MII
),
122 Prev
.DebugValue
->getDebugLoc(),
123 TII
->get(WebAssembly::DBG_VALUE
), false, Register(),
124 Prev
.DebugValue
->getOperand(2).getMetadata(),
125 Prev
.DebugValue
->getOperand(3).getMetadata());
129 for (MachineOperand
&MO
: MI
.defs()) {
130 if (MO
.isReg() && MFI
.isVRegStackified(MO
.getReg())) {
131 Stack
.push_back({MO
.getReg(), nullptr});
136 assert(Stack
.empty() &&
137 "WebAssemblyDebugFixup: Stack not empty at end of basic block!");