xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyDebugValueManager.cpp (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 //===-- WebAssemblyDebugValueManager.cpp - WebAssembly DebugValue Manager -===//
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 /// This file implements the manager for MachineInstr DebugValues.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "WebAssemblyDebugValueManager.h"
15 #include "WebAssembly.h"
16 #include "WebAssemblyMachineFunctionInfo.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 
19 using namespace llvm;
20 
21 WebAssemblyDebugValueManager::WebAssemblyDebugValueManager(
22     MachineInstr *Instr) {
23   // This code differs from MachineInstr::collectDebugValues in that it scans
24   // the whole BB, not just contiguous DBG_VALUEs.
25   if (!Instr->getOperand(0).isReg())
26     return;
27   CurrentReg = Instr->getOperand(0).getReg();
28 
29   MachineBasicBlock::iterator DI = *Instr;
30   ++DI;
31   for (MachineBasicBlock::iterator DE = Instr->getParent()->end(); DI != DE;
32        ++DI) {
33     if (DI->isDebugValue() &&
34         DI->hasDebugOperandForReg(Instr->getOperand(0).getReg()))
35       DbgValues.push_back(&*DI);
36   }
37 }
38 
39 void WebAssemblyDebugValueManager::move(MachineInstr *Insert) {
40   MachineBasicBlock *MBB = Insert->getParent();
41   for (MachineInstr *DBI : reverse(DbgValues))
42     MBB->splice(Insert, DBI->getParent(), DBI);
43 }
44 
45 void WebAssemblyDebugValueManager::updateReg(unsigned Reg) {
46   for (auto *DBI : DbgValues)
47     for (auto &MO : DBI->getDebugOperandsForReg(CurrentReg))
48       MO.setReg(Reg);
49   CurrentReg = Reg;
50 }
51 
52 void WebAssemblyDebugValueManager::clone(MachineInstr *Insert,
53                                          unsigned NewReg) {
54   MachineBasicBlock *MBB = Insert->getParent();
55   MachineFunction *MF = MBB->getParent();
56   for (MachineInstr *DBI : reverse(DbgValues)) {
57     MachineInstr *Clone = MF->CloneMachineInstr(DBI);
58     for (auto &MO : Clone->getDebugOperandsForReg(CurrentReg))
59       MO.setReg(NewReg);
60     MBB->insert(Insert, Clone);
61   }
62 }
63 
64 void WebAssemblyDebugValueManager::replaceWithLocal(unsigned LocalId) {
65   for (auto *DBI : DbgValues) {
66     auto IndexType = DBI->isIndirectDebugValue()
67                          ? llvm::WebAssembly::TI_LOCAL_INDIRECT
68                          : llvm::WebAssembly::TI_LOCAL;
69     for (auto &MO : DBI->getDebugOperandsForReg(CurrentReg))
70       MO.ChangeToTargetIndex(IndexType, LocalId);
71   }
72 }
73