@@ -164,6 +164,45 @@ static MVT typeForRegClass(const TargetRegisterClass *RC) {
164164 llvm_unreachable (" unrecognized register class" );
165165}
166166
167+ static void fixupFollowingDebugValues (DenseMap<unsigned , unsigned > &Reg2Local,
168+ MachineRegisterInfo &MRI,
169+ MachineBasicBlock::iterator B,
170+ MachineBasicBlock::iterator E) {
171+ // Scan DBG_VALUE and modify virtual registers with known locals.
172+ // Stop at first non-DBG_VALUE instruction.
173+ for (auto I = B; I != E && I->isDebugInstr ();) {
174+ MachineInstr &MI = *I++;
175+ for (MachineOperand &MO : reverse (MI.uses ())) {
176+ if (!MO.isReg ())
177+ continue ;
178+
179+ unsigned OldReg = MO.getReg ();
180+ auto I = Reg2Local.find (OldReg);
181+ if (I == Reg2Local.end ())
182+ continue ;
183+
184+ unsigned LocalId = I->second ;
185+ MO.ChangeToTargetIndex (llvm::WebAssembly::TI_LOCAL_START, LocalId);
186+ }
187+ }
188+ }
189+
190+ static void fixupFollowingDebugValues (unsigned Reg, unsigned LocalId,
191+ MachineRegisterInfo &MRI,
192+ MachineBasicBlock::iterator B,
193+ MachineBasicBlock::iterator E) {
194+ // Scan DBG_VALUE and modify the specified virtual registers with the local.
195+ // Stop at first non-DBG_VALUE instruction.
196+ for (auto I = B; I != E && I->isDebugInstr ();) {
197+ MachineInstr &MI = *I++;
198+ for (MachineOperand &MO : reverse (MI.uses ())) {
199+ if (!MO.isReg () || MO.getReg () != Reg)
200+ continue ;
201+ MO.ChangeToTargetIndex (llvm::WebAssembly::TI_LOCAL_START, LocalId);
202+ }
203+ }
204+ }
205+
167206// / Given a MachineOperand of a stackified vreg, return the instruction at the
168207// / start of the expression tree.
169208static MachineInstr *findStartOfTree (MachineOperand &MO,
@@ -262,6 +301,11 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
262301 .addImm (LocalId)
263302 .addReg (MI.getOperand (2 ).getReg ());
264303
304+ auto Next = std::next (MachineBasicBlock::iterator (&MI));
305+ fixupFollowingDebugValues (Reg2Local, MRI, Next, MBB.end ());
306+ fixupFollowingDebugValues (MI.getOperand (0 ).getReg (), LocalId, MRI, Next,
307+ MBB.end ());
308+
265309 MI.eraseFromParent ();
266310 Changed = true ;
267311 continue ;
@@ -294,13 +338,18 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
294338 BuildMI (MBB, InsertPt, MI.getDebugLoc (), TII->get (Opc))
295339 .addImm (LocalId)
296340 .addReg (NewReg);
341+ fixupFollowingDebugValues (NewReg, LocalId, MRI, InsertPt,
342+ MBB.end ());
297343 }
298344 MI.getOperand (0 ).setReg (NewReg);
299345 // This register operand is now being used by the inserted drop
300346 // instruction, so make it undead.
301347 MI.getOperand (0 ).setIsDead (false );
302348 MFI.stackifyVReg (NewReg);
303349 Changed = true ;
350+
351+ fixupFollowingDebugValues (Reg2Local, MRI, InsertPt, MBB.end ());
352+
304353 }
305354 }
306355
@@ -353,6 +402,8 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
353402 MO.setReg (NewReg);
354403 MFI.stackifyVReg (NewReg);
355404 Changed = true ;
405+
406+ fixupFollowingDebugValues (OldReg, LocalId, MRI, InsertPt, MBB.end ());
356407 }
357408
358409 // Coalesce and eliminate COPY instructions.
0 commit comments