llvm.org GIT mirror llvm / 04fd1a0
[DebugInfo] MCP: collect and update DBG_VALUEs encountered in local block MCP currently uses changeDebugValuesDefReg / collectDebugValues to find debug users of a register, however those functions assume that all DBG_VALUEs immediately follow the specified instruction, which isn't necessarily true. This is going to become very often untrue when we turn off CodeGenPrepare::placeDbgValues. Instead of calling changeDebugValuesDefReg on an instruction to change its debug users, in this patch we instead collect DBG_VALUEs of copies as we iterate over insns, and update the debug users of copies that are made dead. This isn't a non-functional change, because MCP will now update DBG_VALUEs that aren't immediately after a copy, but refer to the same register. I've hijacked the regression test for PR38773 to test for this new behaviour, an entirely new test seemed overkill. Differential Revision: https://reviews.llvm.org/D56265 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368835 91177308-0d34-0410-b5e6-96231b3b80d8 Jeremy Morse a month ago
3 changed file(s) with 44 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
817817 /// deleted during LiveDebugVariables analysis.
818818 void markUsesInDebugValueAsUndef(unsigned Reg) const;
819819
820 /// updateDbgUsersToReg - Update a collection of DBG_VALUE instructions
821 /// to refer to the designated register.
822 void updateDbgUsersToReg(unsigned Reg,
823 ArrayRef Users) const {
824 for (MachineInstr *MI : Users) {
825 assert(MI->isDebugInstr());
826 assert(MI->getOperand(0).isReg());
827 MI->getOperand(0).setReg(Reg);
828 }
829 }
830
820831 /// Return true if the specified register is modified in this function.
821832 /// This checks that no defining machine operands exist for the register or
822833 /// any of its aliases. Definitions found on functions marked noreturn are
204204 }
205205
206206 private:
207 typedef enum { DebugUse = false, RegularUse = true } DebugType;
208
207209 void ClobberRegister(unsigned Reg);
208 void ReadRegister(unsigned Reg);
210 void ReadRegister(unsigned Reg, MachineInstr &Reader,
211 DebugType DT);
209212 void CopyPropagateBlock(MachineBasicBlock &MBB);
210213 bool eraseIfRedundant(MachineInstr &Copy, unsigned Src, unsigned Def);
211214 void forwardUses(MachineInstr &MI);
216219 /// Candidates for deletion.
217220 SmallSetVector MaybeDeadCopies;
218221
222 /// Multimap tracking debug users in current BB
223 DenseMap> CopyDbgUsers;
224
219225 CopyTracker Tracker;
220226
221227 bool Changed;
230236 INITIALIZE_PASS(MachineCopyPropagation, DEBUG_TYPE,
231237 "Machine Copy Propagation Pass", false, false)
232238
233 void MachineCopyPropagation::ReadRegister(unsigned Reg) {
239 void MachineCopyPropagation::ReadRegister(unsigned Reg, MachineInstr &Reader,
240 DebugType DT) {
234241 // If 'Reg' is defined by a copy, the copy is no longer a candidate
235 // for elimination.
242 // for elimination. If a copy is "read" by a debug user, record the user
243 // for propagation.
236244 for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
237245 if (MachineInstr *Copy = Tracker.findCopyForUnit(*RUI, *TRI)) {
238 LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
239 MaybeDeadCopies.remove(Copy);
246 if (DT == RegularUse) {
247 LLVM_DEBUG(dbgs() << "MCP: Copy is used - not dead: "; Copy->dump());
248 MaybeDeadCopies.remove(Copy);
249 } else {
250 CopyDbgUsers[Copy].push_back(&Reader);
251 }
240252 }
241253 }
242254 }
487499
488500 // If Src is defined by a previous copy, the previous copy cannot be
489501 // eliminated.
490 ReadRegister(Src);
502 ReadRegister(Src, *MI, RegularUse);
491503 for (const MachineOperand &MO : MI->implicit_operands()) {
492504 if (!MO.isReg() || !MO.readsReg())
493505 continue;
494506 unsigned Reg = MO.getReg();
495507 if (!Reg)
496508 continue;
497 ReadRegister(Reg);
509 ReadRegister(Reg, *MI, RegularUse);
498510 }
499511
500512 LLVM_DEBUG(dbgs() << "MCP: Copy is a deletion candidate: "; MI->dump());
533545 // instruction, so we need to make sure we don't remove it as dead
534546 // later.
535547 if (MO.isTied())
536 ReadRegister(Reg);
548 ReadRegister(Reg, *MI, RegularUse);
537549 Tracker.clobberRegister(Reg, *TRI);
538550 }
539551
557569 if (MO.isDef() && !MO.isEarlyClobber()) {
558570 Defs.push_back(Reg);
559571 continue;
560 } else if (!MO.isDebug() && MO.readsReg())
561 ReadRegister(Reg);
572 } else if (MO.readsReg())
573 ReadRegister(Reg, *MI, MO.isDebug() ? DebugUse : RegularUse);
562574 }
563575
564576 // The instruction has a register mask operand which means that it clobbers
608620 MaybeDead->dump());
609621 assert(!MRI->isReserved(MaybeDead->getOperand(0).getReg()));
610622
611 // Update matching debug values.
623 // Update matching debug values, if any.
612624 assert(MaybeDead->isCopy());
613 MaybeDead->changeDebugValuesDefReg(MaybeDead->getOperand(1).getReg());
625 unsigned SrcReg = MaybeDead->getOperand(1).getReg();
626 MRI->updateDbgUsersToReg(SrcReg, CopyDbgUsers[MaybeDead]);
614627
615628 MaybeDead->eraseFromParent();
616629 Changed = true;
619632 }
620633
621634 MaybeDeadCopies.clear();
635 CopyDbgUsers.clear();
622636 Tracker.clear();
623637 }
624638
9898 ; CHECK: IDIV32r killed renamable $ecx
9999 ; CHECK-NEXT: DBG_VALUE $eax, $noreg, !12, !DIExpression(), debug-location !13
100100 DBG_VALUE $ecx, $noreg, !12, !DIExpression(), debug-location !13
101 ; The following mov and DBG_VALUE have been inserted after the PR was
102 ; resolved to check that MCP will update debug users that are not
103 ; immediately after the dead copy.
104 ; CHECK-NEXT: $edx = MOV32r0
105 $edx = MOV32r0 implicit-def dead $eflags
106 ; CHECK-NEXT: DBG_VALUE $eax, $noreg, !12, !DIExpression(), debug-location !13
107 DBG_VALUE $ecx, $noreg, !12, !DIExpression(), debug-location !13
101108 $eax = COPY killed renamable $ecx
102109 RET 0, $eax
103110