RemoveRedundantDebugValues.cpp 7.3 KB

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