LLVM 20.0.0git
WebAssemblyDebugFixup.cpp
Go to the documentation of this file.
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
18#include "WebAssembly.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/Support/Debug.h"
28using namespace llvm;
29
30#define DEBUG_TYPE "wasm-debug-fixup"
31
32namespace {
33class WebAssemblyDebugFixup final : public MachineFunctionPass {
34 StringRef getPassName() const override { return "WebAssembly Debug Fixup"; }
35
36 void getAnalysisUsage(AnalysisUsage &AU) const override {
37 AU.setPreservesCFG();
39 }
40
41 bool runOnMachineFunction(MachineFunction &MF) override;
42
43public:
44 static char ID; // Pass identification, replacement for typeid
45 WebAssemblyDebugFixup() : MachineFunctionPass(ID) {}
46};
47} // end anonymous namespace
48
49char WebAssemblyDebugFixup::ID = 0;
51 WebAssemblyDebugFixup, DEBUG_TYPE,
52 "Ensures debug_value's that have been stackified become stack relative",
53 false, false)
54
56 return new WebAssemblyDebugFixup();
57}
58
59// At this very end of the compilation pipeline, if any DBG_VALUEs with
60// registers remain, it means they are dangling info which we failed to update
61// when their corresponding def instruction was transformed/moved/splitted etc.
62// Because Wasm cannot access values in LLVM virtual registers in the debugger,
63// these dangling DBG_VALUEs in effect kill the effect of any previous DBG_VALUE
64// associated with the variable, which will appear as "optimized out".
66 const TargetInstrInfo *TII) {
67 for (auto &MI : llvm::make_early_inc_range(MBB)) {
68 if (MI.isDebugValue() && MI.getDebugOperand(0).isReg() &&
69 !MI.isUndefDebugValue()) {
70 LLVM_DEBUG(dbgs() << "Warning: dangling DBG_VALUE set to undef: " << MI
71 << "\n");
72 MI.setDebugValueUndef();
73 }
74 }
75}
76
77bool WebAssemblyDebugFixup::runOnMachineFunction(MachineFunction &MF) {
78 LLVM_DEBUG(dbgs() << "********** Debug Fixup **********\n"
79 "********** Function: "
80 << MF.getName() << '\n');
81
83 const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
84
85 struct StackElem {
86 unsigned Reg;
87 MachineInstr *DebugValue;
88 };
89 std::vector<StackElem> Stack;
90 for (MachineBasicBlock &MBB : MF) {
91 // We may insert into this list.
92 for (auto MII = MBB.begin(); MII != MBB.end(); ++MII) {
93 MachineInstr &MI = *MII;
94 if (MI.isDebugValue()) {
95 auto &MO = MI.getOperand(0);
96 // Also check if not a $noreg: likely a DBG_VALUE we just inserted.
97 if (MO.isReg() && MO.getReg().isValid() &&
98 MFI.isVRegStackified(MO.getReg())) {
99 // Found a DBG_VALUE with a stackified register we will
100 // change into a stack operand.
101 // Search for register rather than assume it is on top (which it
102 // typically is if it appears right after the def), since
103 // DBG_VALUE's may shift under some circumstances.
104 for (auto &Elem : reverse(Stack)) {
105 if (MO.getReg() == Elem.Reg) {
106 auto Depth = static_cast<unsigned>(&Elem - &Stack[0]);
107 LLVM_DEBUG(dbgs() << "Debug Value VReg " << printReg(MO.getReg())
108 << " -> Stack Relative " << Depth << "\n");
109 MO.ChangeToTargetIndex(WebAssembly::TI_OPERAND_STACK, Depth);
110 // Save the DBG_VALUE instruction that defined this stackified
111 // variable since later we need it to construct another one on
112 // pop.
113 Elem.DebugValue = &MI;
114 break;
115 }
116 }
117 // If the Reg was not found, we have a DBG_VALUE outside of its
118 // def-use range, and we leave it unmodified as reg, which means
119 // it will be culled later.
120 }
121 } else {
122 // Track stack depth.
123 for (MachineOperand &MO : reverse(MI.explicit_uses())) {
124 if (MO.isReg() && MFI.isVRegStackified(MO.getReg())) {
125 auto Prev = Stack.back();
126 Stack.pop_back();
127 assert(Prev.Reg == MO.getReg() &&
128 "WebAssemblyDebugFixup: Pop: Register not matched!");
129 // We should not put a DBG_VALUE after a terminator; debug ranges
130 // are terminated at the end of a BB anyway.
131 if (Prev.DebugValue && !MI.isTerminator()) {
132 // This stackified reg is a variable that started life at
133 // Prev.DebugValue, so now that we're popping it we must insert
134 // a $noreg DBG_VALUE for the variable to end it, right after
135 // the current instruction.
136 BuildMI(*Prev.DebugValue->getParent(), std::next(MII),
137 Prev.DebugValue->getDebugLoc(),
138 TII->get(WebAssembly::DBG_VALUE), false, Register(),
139 Prev.DebugValue->getOperand(2).getMetadata(),
140 Prev.DebugValue->getOperand(3).getMetadata());
141 }
142 }
143 }
144 for (MachineOperand &MO : MI.defs()) {
145 if (MO.isReg() && MFI.isVRegStackified(MO.getReg())) {
146 Stack.push_back({MO.getReg(), nullptr});
147 }
148 }
149 }
150 }
151 assert(Stack.empty() &&
152 "WebAssemblyDebugFixup: Stack not empty at end of basic block!");
153
155 }
156
157 return true;
158}
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
unsigned Reg
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void setDanglingDebugValuesUndef(MachineBasicBlock &MBB, const TargetInstrInfo *TII)
#define DEBUG_TYPE
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
TargetInstrInfo - Interface to description of machine instruction set.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
FunctionPass * createWebAssemblyDebugFixup()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.