WebAssemblyDebugFixup.cpp 6.5 KB

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