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 28 MachineBasicBlock::iterator DI = *Instr; 29 ++DI; 30 for (MachineBasicBlock::iterator DE = Instr->getParent()->end(); DI != DE; 31 ++DI) { 32 if (DI->isDebugValue() && 33 DI->getDebugOperandForReg(Instr->getOperand(0).getReg())) 34 DbgValues.push_back(&*DI); 35 } 36 } 37 38 void WebAssemblyDebugValueManager::move(MachineInstr *Insert) { 39 MachineBasicBlock *MBB = Insert->getParent(); 40 for (MachineInstr *DBI : reverse(DbgValues)) 41 MBB->splice(Insert, DBI->getParent(), DBI); 42 } 43 44 void WebAssemblyDebugValueManager::updateReg(unsigned Reg) { 45 for (auto *DBI : DbgValues) 46 DBI->getDebugOperand(0).setReg(Reg); 47 } 48 49 void WebAssemblyDebugValueManager::clone(MachineInstr *Insert, 50 unsigned NewReg) { 51 MachineBasicBlock *MBB = Insert->getParent(); 52 MachineFunction *MF = MBB->getParent(); 53 for (MachineInstr *DBI : reverse(DbgValues)) { 54 MachineInstr *Clone = MF->CloneMachineInstr(DBI); 55 Clone->getDebugOperand(0).setReg(NewReg); 56 MBB->insert(Insert, Clone); 57 } 58 } 59 60 void WebAssemblyDebugValueManager::replaceWithLocal(unsigned LocalId) { 61 for (auto *DBI : DbgValues) { 62 MachineOperand &Op = DBI->getDebugOperand(0); 63 Op.ChangeToTargetIndex(llvm::WebAssembly::TI_LOCAL, LocalId); 64 } 65 } 66