Path: blob/main/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyDebugFixup.cpp
35266 views
//===-- WebAssemblyDebugFixup.cpp - Debug Fixup ------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// Several prior passes may "stackify" registers, here we ensure any references10/// in such registers in debug_value instructions become stack relative also.11/// This is done in a separate pass such that not all previous passes need to12/// track stack depth when values get stackified.13///14//===----------------------------------------------------------------------===//1516#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"17#include "WebAssembly.h"18#include "WebAssemblyMachineFunctionInfo.h"19#include "WebAssemblySubtarget.h"20#include "WebAssemblyUtilities.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"30using namespace llvm;3132#define DEBUG_TYPE "wasm-debug-fixup"3334namespace {35class WebAssemblyDebugFixup final : public MachineFunctionPass {36StringRef getPassName() const override { return "WebAssembly Debug Fixup"; }3738void getAnalysisUsage(AnalysisUsage &AU) const override {39AU.setPreservesCFG();40MachineFunctionPass::getAnalysisUsage(AU);41}4243bool runOnMachineFunction(MachineFunction &MF) override;4445public:46static char ID; // Pass identification, replacement for typeid47WebAssemblyDebugFixup() : MachineFunctionPass(ID) {}48};49} // end anonymous namespace5051char WebAssemblyDebugFixup::ID = 0;52INITIALIZE_PASS(53WebAssemblyDebugFixup, DEBUG_TYPE,54"Ensures debug_value's that have been stackified become stack relative",55false, false)5657FunctionPass *llvm::createWebAssemblyDebugFixup() {58return new WebAssemblyDebugFixup();59}6061// At this very end of the compilation pipeline, if any DBG_VALUEs with62// registers remain, it means they are dangling info which we failed to update63// when their corresponding def instruction was transformed/moved/splitted etc.64// Because Wasm cannot access values in LLVM virtual registers in the debugger,65// these dangling DBG_VALUEs in effect kill the effect of any previous DBG_VALUE66// associated with the variable, which will appear as "optimized out".67static void setDanglingDebugValuesUndef(MachineBasicBlock &MBB,68const TargetInstrInfo *TII) {69for (auto &MI : llvm::make_early_inc_range(MBB)) {70if (MI.isDebugValue() && MI.getDebugOperand(0).isReg() &&71!MI.isUndefDebugValue()) {72LLVM_DEBUG(dbgs() << "Warning: dangling DBG_VALUE set to undef: " << MI73<< "\n");74MI.setDebugValueUndef();75}76}77}7879bool WebAssemblyDebugFixup::runOnMachineFunction(MachineFunction &MF) {80LLVM_DEBUG(dbgs() << "********** Debug Fixup **********\n"81"********** Function: "82<< MF.getName() << '\n');8384WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();85const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();8687struct StackElem {88unsigned Reg;89MachineInstr *DebugValue;90};91std::vector<StackElem> Stack;92for (MachineBasicBlock &MBB : MF) {93// We may insert into this list.94for (auto MII = MBB.begin(); MII != MBB.end(); ++MII) {95MachineInstr &MI = *MII;96if (MI.isDebugValue()) {97auto &MO = MI.getOperand(0);98// Also check if not a $noreg: likely a DBG_VALUE we just inserted.99if (MO.isReg() && MO.getReg().isValid() &&100MFI.isVRegStackified(MO.getReg())) {101// Found a DBG_VALUE with a stackified register we will102// change into a stack operand.103// Search for register rather than assume it is on top (which it104// typically is if it appears right after the def), since105// DBG_VALUE's may shift under some circumstances.106for (auto &Elem : reverse(Stack)) {107if (MO.getReg() == Elem.Reg) {108auto Depth = static_cast<unsigned>(&Elem - &Stack[0]);109LLVM_DEBUG(dbgs() << "Debug Value VReg " << MO.getReg()110<< " -> Stack Relative " << Depth << "\n");111MO.ChangeToTargetIndex(WebAssembly::TI_OPERAND_STACK, Depth);112// Save the DBG_VALUE instruction that defined this stackified113// variable since later we need it to construct another one on114// pop.115Elem.DebugValue = &MI;116break;117}118}119// If the Reg was not found, we have a DBG_VALUE outside of its120// def-use range, and we leave it unmodified as reg, which means121// it will be culled later.122}123} else {124// Track stack depth.125for (MachineOperand &MO : reverse(MI.explicit_uses())) {126if (MO.isReg() && MFI.isVRegStackified(MO.getReg())) {127auto Prev = Stack.back();128Stack.pop_back();129assert(Prev.Reg == MO.getReg() &&130"WebAssemblyDebugFixup: Pop: Register not matched!");131// We should not put a DBG_VALUE after a terminator; debug ranges132// are terminated at the end of a BB anyway.133if (Prev.DebugValue && !MI.isTerminator()) {134// This stackified reg is a variable that started life at135// Prev.DebugValue, so now that we're popping it we must insert136// a $noreg DBG_VALUE for the variable to end it, right after137// the current instruction.138BuildMI(*Prev.DebugValue->getParent(), std::next(MII),139Prev.DebugValue->getDebugLoc(),140TII->get(WebAssembly::DBG_VALUE), false, Register(),141Prev.DebugValue->getOperand(2).getMetadata(),142Prev.DebugValue->getOperand(3).getMetadata());143}144}145}146for (MachineOperand &MO : MI.defs()) {147if (MO.isReg() && MFI.isVRegStackified(MO.getReg())) {148Stack.push_back({MO.getReg(), nullptr});149}150}151}152}153assert(Stack.empty() &&154"WebAssemblyDebugFixup: Stack not empty at end of basic block!");155156setDanglingDebugValuesUndef(MBB, TII);157}158159return true;160}161162163