RemoveRedundantDebugValues.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //===- RemoveRedundantDebugValues.cpp - Remove Redundant Debug Value MIs --===//
  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. #include "llvm/ADT/DenseMap.h"
  9. #include "llvm/ADT/DenseSet.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/Statistic.h"
  12. #include "llvm/CodeGen/MachineBasicBlock.h"
  13. #include "llvm/CodeGen/MachineFunctionPass.h"
  14. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  15. #include "llvm/IR/DebugInfoMetadata.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/PassRegistry.h"
  20. /// \file RemoveRedundantDebugValues.cpp
  21. ///
  22. /// The RemoveRedundantDebugValues pass removes redundant DBG_VALUEs that
  23. /// appear in MIR after the register allocator.
  24. #define DEBUG_TYPE "removeredundantdebugvalues"
  25. using namespace llvm;
  26. STATISTIC(NumRemovedBackward, "Number of DBG_VALUEs removed (backward scan)");
  27. STATISTIC(NumRemovedForward, "Number of DBG_VALUEs removed (forward scan)");
  28. namespace {
  29. class RemoveRedundantDebugValues : public MachineFunctionPass {
  30. public:
  31. static char ID;
  32. RemoveRedundantDebugValues();
  33. bool reduceDbgValues(MachineFunction &MF);
  34. /// Remove redundant debug value MIs for the given machine function.
  35. bool runOnMachineFunction(MachineFunction &MF) override;
  36. void getAnalysisUsage(AnalysisUsage &AU) const override {
  37. AU.setPreservesCFG();
  38. MachineFunctionPass::getAnalysisUsage(AU);
  39. }
  40. };
  41. } // namespace
  42. //===----------------------------------------------------------------------===//
  43. // Implementation
  44. //===----------------------------------------------------------------------===//
  45. char RemoveRedundantDebugValues::ID = 0;
  46. char &llvm::RemoveRedundantDebugValuesID = RemoveRedundantDebugValues::ID;
  47. INITIALIZE_PASS(RemoveRedundantDebugValues, DEBUG_TYPE,
  48. "Remove Redundant DEBUG_VALUE analysis", false, false)
  49. /// Default construct and initialize the pass.
  50. RemoveRedundantDebugValues::RemoveRedundantDebugValues()
  51. : MachineFunctionPass(ID) {
  52. initializeRemoveRedundantDebugValuesPass(*PassRegistry::getPassRegistry());
  53. }
  54. // This analysis aims to remove redundant DBG_VALUEs by going forward
  55. // in the basic block by considering the first DBG_VALUE as a valid
  56. // until its first (location) operand is not clobbered/modified.
  57. // For example:
  58. // (1) DBG_VALUE $edi, !"var1", ...
  59. // (2) <block of code that does affect $edi>
  60. // (3) DBG_VALUE $edi, !"var1", ...
  61. // ...
  62. // in this case, we can remove (3).
  63. // TODO: Support DBG_VALUE_LIST and other debug instructions.
  64. static bool reduceDbgValsForwardScan(MachineBasicBlock &MBB) {
  65. LLVM_DEBUG(dbgs() << "\n == Forward Scan == \n");
  66. SmallVector<MachineInstr *, 8> DbgValsToBeRemoved;
  67. DenseMap<DebugVariable, std::pair<MachineOperand *, const DIExpression *>>
  68. VariableMap;
  69. const auto *TRI = MBB.getParent()->getSubtarget().getRegisterInfo();
  70. for (auto &MI : MBB) {
  71. if (MI.isDebugValue()) {
  72. DebugVariable Var(MI.getDebugVariable(), std::nullopt,
  73. MI.getDebugLoc()->getInlinedAt());
  74. auto VMI = VariableMap.find(Var);
  75. // Just stop tracking this variable, until we cover DBG_VALUE_LIST.
  76. // 1 DBG_VALUE $rax, "x", DIExpression()
  77. // ...
  78. // 2 DBG_VALUE_LIST "x", DIExpression(...), $rax, $rbx
  79. // ...
  80. // 3 DBG_VALUE $rax, "x", DIExpression()
  81. if (MI.isDebugValueList() && VMI != VariableMap.end()) {
  82. VariableMap.erase(VMI);
  83. continue;
  84. }
  85. MachineOperand &Loc = MI.getDebugOperand(0);
  86. if (!Loc.isReg()) {
  87. // If it it's not a register, just stop tracking such variable.
  88. if (VMI != VariableMap.end())
  89. VariableMap.erase(VMI);
  90. continue;
  91. }
  92. // We have found a new value for a variable.
  93. if (VMI == VariableMap.end() ||
  94. VMI->second.first->getReg() != Loc.getReg() ||
  95. VMI->second.second != MI.getDebugExpression()) {
  96. VariableMap[Var] = {&Loc, MI.getDebugExpression()};
  97. continue;
  98. }
  99. // Found an identical DBG_VALUE, so it can be considered
  100. // for later removal.
  101. DbgValsToBeRemoved.push_back(&MI);
  102. }
  103. if (MI.isMetaInstruction())
  104. continue;
  105. // Stop tracking any location that is clobbered by this instruction.
  106. for (auto &Var : VariableMap) {
  107. auto &LocOp = Var.second.first;
  108. if (MI.modifiesRegister(LocOp->getReg(), TRI))
  109. VariableMap.erase(Var.first);
  110. }
  111. }
  112. for (auto &Instr : DbgValsToBeRemoved) {
  113. LLVM_DEBUG(dbgs() << "removing "; Instr->dump());
  114. Instr->eraseFromParent();
  115. ++NumRemovedForward;
  116. }
  117. return !DbgValsToBeRemoved.empty();
  118. }
  119. // This analysis aims to remove redundant DBG_VALUEs by going backward
  120. // in the basic block and removing all but the last DBG_VALUE for any
  121. // given variable in a set of consecutive DBG_VALUE instructions.
  122. // For example:
  123. // (1) DBG_VALUE $edi, !"var1", ...
  124. // (2) DBG_VALUE $esi, !"var2", ...
  125. // (3) DBG_VALUE $edi, !"var1", ...
  126. // ...
  127. // in this case, we can remove (1).
  128. static bool reduceDbgValsBackwardScan(MachineBasicBlock &MBB) {
  129. LLVM_DEBUG(dbgs() << "\n == Backward Scan == \n");
  130. SmallVector<MachineInstr *, 8> DbgValsToBeRemoved;
  131. SmallDenseSet<DebugVariable> VariableSet;
  132. for (MachineInstr &MI : llvm::reverse(MBB)) {
  133. if (MI.isDebugValue()) {
  134. DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
  135. MI.getDebugLoc()->getInlinedAt());
  136. auto R = VariableSet.insert(Var);
  137. // If it is a DBG_VALUE describing a constant as:
  138. // DBG_VALUE 0, ...
  139. // we just don't consider such instructions as candidates
  140. // for redundant removal.
  141. if (MI.isNonListDebugValue()) {
  142. MachineOperand &Loc = MI.getDebugOperand(0);
  143. if (!Loc.isReg()) {
  144. // If we have already encountered this variable, just stop
  145. // tracking it.
  146. if (!R.second)
  147. VariableSet.erase(Var);
  148. continue;
  149. }
  150. }
  151. // We have already encountered the value for this variable,
  152. // so this one can be deleted.
  153. if (!R.second)
  154. DbgValsToBeRemoved.push_back(&MI);
  155. continue;
  156. }
  157. // If we encountered a non-DBG_VALUE, try to find the next
  158. // sequence with consecutive DBG_VALUE instructions.
  159. VariableSet.clear();
  160. }
  161. for (auto &Instr : DbgValsToBeRemoved) {
  162. LLVM_DEBUG(dbgs() << "removing "; Instr->dump());
  163. Instr->eraseFromParent();
  164. ++NumRemovedBackward;
  165. }
  166. return !DbgValsToBeRemoved.empty();
  167. }
  168. bool RemoveRedundantDebugValues::reduceDbgValues(MachineFunction &MF) {
  169. LLVM_DEBUG(dbgs() << "\nDebug Value Reduction\n");
  170. bool Changed = false;
  171. for (auto &MBB : MF) {
  172. Changed |= reduceDbgValsBackwardScan(MBB);
  173. Changed |= reduceDbgValsForwardScan(MBB);
  174. }
  175. return Changed;
  176. }
  177. bool RemoveRedundantDebugValues::runOnMachineFunction(MachineFunction &MF) {
  178. // Skip functions without debugging information.
  179. if (!MF.getFunction().getSubprogram())
  180. return false;
  181. // Skip functions from NoDebug compilation units.
  182. if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() ==
  183. DICompileUnit::NoDebug)
  184. return false;
  185. bool Changed = reduceDbgValues(MF);
  186. return Changed;
  187. }