@@ -164,6 +164,45 @@ static MVT typeForRegClass(const TargetRegisterClass *RC) {
164
164
llvm_unreachable (" unrecognized register class" );
165
165
}
166
166
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
+
167
206
// / Given a MachineOperand of a stackified vreg, return the instruction at the
168
207
// / start of the expression tree.
169
208
static MachineInstr *findStartOfTree (MachineOperand &MO,
@@ -262,6 +301,11 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
262
301
.addImm (LocalId)
263
302
.addReg (MI.getOperand (2 ).getReg ());
264
303
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
+
265
309
MI.eraseFromParent ();
266
310
Changed = true ;
267
311
continue ;
@@ -294,13 +338,18 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
294
338
BuildMI (MBB, InsertPt, MI.getDebugLoc (), TII->get (Opc))
295
339
.addImm (LocalId)
296
340
.addReg (NewReg);
341
+ fixupFollowingDebugValues (NewReg, LocalId, MRI, InsertPt,
342
+ MBB.end ());
297
343
}
298
344
MI.getOperand (0 ).setReg (NewReg);
299
345
// This register operand is now being used by the inserted drop
300
346
// instruction, so make it undead.
301
347
MI.getOperand (0 ).setIsDead (false );
302
348
MFI.stackifyVReg (NewReg);
303
349
Changed = true ;
350
+
351
+ fixupFollowingDebugValues (Reg2Local, MRI, InsertPt, MBB.end ());
352
+
304
353
}
305
354
}
306
355
@@ -353,6 +402,8 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
353
402
MO.setReg (NewReg);
354
403
MFI.stackifyVReg (NewReg);
355
404
Changed = true ;
405
+
406
+ fixupFollowingDebugValues (OldReg, LocalId, MRI, InsertPt, MBB.end ());
356
407
}
357
408
358
409
// Coalesce and eliminate COPY instructions.
@@ -379,6 +430,13 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
379
430
Changed = true ;
380
431
}
381
432
433
+ {
434
+ auto RL = Reg2Local.find (MFI.SPVReg );
435
+ if (RL != Reg2Local.end ()) {
436
+ MFI.SPLocal = RL->second ;
437
+ }
438
+ }
439
+
382
440
#ifndef NDEBUG
383
441
// Assert that all registers have been stackified at this point.
384
442
for (const MachineBasicBlock &MBB : MF) {
0 commit comments