1 | //===-- WebAssemblyDebugFixup.cpp - Debug Fixup ------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | /// |
9 | /// \file |
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. |
14 | /// |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
18 | #include "WebAssembly.h" |
19 | #include "WebAssemblyMachineFunctionInfo.h" |
20 | #include "WebAssemblySubtarget.h" |
21 | #include "WebAssemblyUtilities.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" |
31 | using namespace llvm; |
32 | |
33 | #define DEBUG_TYPE "wasm-debug-fixup" |
34 | |
35 | namespace { |
36 | class WebAssemblyDebugFixup final : public MachineFunctionPass { |
37 | StringRef getPassName() const override { return "WebAssembly Debug Fixup" ; } |
38 | |
39 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
40 | AU.setPreservesCFG(); |
41 | MachineFunctionPass::getAnalysisUsage(AU); |
42 | } |
43 | |
44 | bool runOnMachineFunction(MachineFunction &MF) override; |
45 | |
46 | public: |
47 | static char ID; // Pass identification, replacement for typeid |
48 | WebAssemblyDebugFixup() : MachineFunctionPass(ID) {} |
49 | }; |
50 | } // end anonymous namespace |
51 | |
52 | char WebAssemblyDebugFixup::ID = 0; |
53 | INITIALIZE_PASS( |
54 | WebAssemblyDebugFixup, DEBUG_TYPE, |
55 | "Ensures debug_value's that have been stackified become stack relative" , |
56 | false, false) |
57 | |
58 | FunctionPass *llvm::createWebAssemblyDebugFixup() { |
59 | return new WebAssemblyDebugFixup(); |
60 | } |
61 | |
62 | // At this very end of the compilation pipeline, if any DBG_VALUEs with |
63 | // registers remain, it means they are dangling info which we failed to update |
64 | // when their corresponding def instruction was transformed/moved/splitted etc. |
65 | // Because Wasm cannot access values in LLVM virtual registers in the debugger, |
66 | // these dangling DBG_VALUEs in effect kill the effect of any previous DBG_VALUE |
67 | // associated with the variable, which will appear as "optimized out". |
68 | static void setDanglingDebugValuesUndef(MachineBasicBlock &MBB, |
69 | const TargetInstrInfo *TII) { |
70 | for (auto &MI : llvm::make_early_inc_range(Range&: MBB)) { |
71 | if (MI.isDebugValue() && MI.getDebugOperand(Index: 0).isReg() && |
72 | !MI.isUndefDebugValue()) { |
73 | LLVM_DEBUG(dbgs() << "Warning: dangling DBG_VALUE set to undef: " << MI |
74 | << "\n" ); |
75 | MI.setDebugValueUndef(); |
76 | } |
77 | } |
78 | } |
79 | |
80 | bool WebAssemblyDebugFixup::runOnMachineFunction(MachineFunction &MF) { |
81 | LLVM_DEBUG(dbgs() << "********** Debug Fixup **********\n" |
82 | "********** Function: " |
83 | << MF.getName() << '\n'); |
84 | |
85 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
86 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
87 | |
88 | struct StackElem { |
89 | unsigned Reg; |
90 | MachineInstr *DebugValue; |
91 | }; |
92 | std::vector<StackElem> Stack; |
93 | for (MachineBasicBlock &MBB : MF) { |
94 | // We may insert into this list. |
95 | for (auto MII = MBB.begin(); MII != MBB.end(); ++MII) { |
96 | MachineInstr &MI = *MII; |
97 | if (MI.isDebugValue()) { |
98 | auto &MO = MI.getOperand(i: 0); |
99 | // Also check if not a $noreg: likely a DBG_VALUE we just inserted. |
100 | if (MO.isReg() && MO.getReg().isValid() && |
101 | MFI.isVRegStackified(VReg: MO.getReg())) { |
102 | // Found a DBG_VALUE with a stackified register we will |
103 | // change into a stack operand. |
104 | // Search for register rather than assume it is on top (which it |
105 | // typically is if it appears right after the def), since |
106 | // DBG_VALUE's may shift under some circumstances. |
107 | for (auto &Elem : reverse(C&: Stack)) { |
108 | if (MO.getReg() == Elem.Reg) { |
109 | auto Depth = static_cast<unsigned>(&Elem - &Stack[0]); |
110 | LLVM_DEBUG(dbgs() << "Debug Value VReg " << MO.getReg() |
111 | << " -> Stack Relative " << Depth << "\n" ); |
112 | MO.ChangeToTargetIndex(Idx: WebAssembly::TI_OPERAND_STACK, Offset: Depth); |
113 | // Save the DBG_VALUE instruction that defined this stackified |
114 | // variable since later we need it to construct another one on |
115 | // pop. |
116 | Elem.DebugValue = &MI; |
117 | break; |
118 | } |
119 | } |
120 | // If the Reg was not found, we have a DBG_VALUE outside of its |
121 | // def-use range, and we leave it unmodified as reg, which means |
122 | // it will be culled later. |
123 | } |
124 | } else { |
125 | // Track stack depth. |
126 | for (MachineOperand &MO : reverse(C: MI.explicit_uses())) { |
127 | if (MO.isReg() && MFI.isVRegStackified(VReg: MO.getReg())) { |
128 | auto Prev = Stack.back(); |
129 | Stack.pop_back(); |
130 | assert(Prev.Reg == MO.getReg() && |
131 | "WebAssemblyDebugFixup: Pop: Register not matched!" ); |
132 | // We should not put a DBG_VALUE after a terminator; debug ranges |
133 | // are terminated at the end of a BB anyway. |
134 | if (Prev.DebugValue && !MI.isTerminator()) { |
135 | // This stackified reg is a variable that started life at |
136 | // Prev.DebugValue, so now that we're popping it we must insert |
137 | // a $noreg DBG_VALUE for the variable to end it, right after |
138 | // the current instruction. |
139 | BuildMI(BB&: *Prev.DebugValue->getParent(), I: std::next(x: MII), |
140 | DL: Prev.DebugValue->getDebugLoc(), |
141 | MCID: TII->get(Opcode: WebAssembly::DBG_VALUE), IsIndirect: false, Reg: Register(), |
142 | Variable: Prev.DebugValue->getOperand(i: 2).getMetadata(), |
143 | Expr: Prev.DebugValue->getOperand(i: 3).getMetadata()); |
144 | } |
145 | } |
146 | } |
147 | for (MachineOperand &MO : MI.defs()) { |
148 | if (MO.isReg() && MFI.isVRegStackified(VReg: MO.getReg())) { |
149 | Stack.push_back(x: {.Reg: MO.getReg(), .DebugValue: nullptr}); |
150 | } |
151 | } |
152 | } |
153 | } |
154 | assert(Stack.empty() && |
155 | "WebAssemblyDebugFixup: Stack not empty at end of basic block!" ); |
156 | |
157 | setDanglingDebugValuesUndef(MBB, TII); |
158 | } |
159 | |
160 | return true; |
161 | } |
162 | |