DbgValueHistoryCalculator.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "DbgValueHistoryCalculator.h"
  10. #include "llvm/ADT/BitVector.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/CodeGen/MachineBasicBlock.h"
  13. #include "llvm/CodeGen/MachineFunction.h"
  14. #include "llvm/IR/DebugInfo.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include "llvm/Target/TargetRegisterInfo.h"
  18. #include <algorithm>
  19. #include <map>
  20. using namespace llvm;
  21. #define DEBUG_TYPE "dwarfdebug"
  22. // \brief If @MI is a DBG_VALUE with debug value described by a
  23. // defined register, returns the number of this register.
  24. // In the other case, returns 0.
  25. static unsigned isDescribedByReg(const MachineInstr &MI) {
  26. assert(MI.isDebugValue());
  27. assert(MI.getNumOperands() == 4);
  28. // If location of variable is described using a register (directly or
  29. // indirecltly), this register is always a first operand.
  30. return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
  31. }
  32. void DbgValueHistoryMap::startInstrRange(InlinedVariable Var,
  33. const MachineInstr &MI) {
  34. // Instruction range should start with a DBG_VALUE instruction for the
  35. // variable.
  36. assert(MI.isDebugValue() && "not a DBG_VALUE");
  37. auto &Ranges = VarInstrRanges[Var];
  38. if (!Ranges.empty() && Ranges.back().second == nullptr &&
  39. Ranges.back().first->isIdenticalTo(&MI)) {
  40. DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
  41. << "\t" << Ranges.back().first << "\t" << MI << "\n");
  42. return;
  43. }
  44. Ranges.push_back(std::make_pair(&MI, nullptr));
  45. }
  46. void DbgValueHistoryMap::endInstrRange(InlinedVariable Var,
  47. const MachineInstr &MI) {
  48. auto &Ranges = VarInstrRanges[Var];
  49. // Verify that the current instruction range is not yet closed.
  50. assert(!Ranges.empty() && Ranges.back().second == nullptr);
  51. // For now, instruction ranges are not allowed to cross basic block
  52. // boundaries.
  53. assert(Ranges.back().first->getParent() == MI.getParent());
  54. Ranges.back().second = &MI;
  55. }
  56. unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const {
  57. const auto &I = VarInstrRanges.find(Var);
  58. if (I == VarInstrRanges.end())
  59. return 0;
  60. const auto &Ranges = I->second;
  61. if (Ranges.empty() || Ranges.back().second != nullptr)
  62. return 0;
  63. return isDescribedByReg(*Ranges.back().first);
  64. }
  65. namespace {
  66. // Maps physreg numbers to the variables they describe.
  67. typedef DbgValueHistoryMap::InlinedVariable InlinedVariable;
  68. typedef std::map<unsigned, SmallVector<InlinedVariable, 1>> RegDescribedVarsMap;
  69. }
  70. // \brief Claim that @Var is not described by @RegNo anymore.
  71. static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
  72. InlinedVariable Var) {
  73. const auto &I = RegVars.find(RegNo);
  74. assert(RegNo != 0U && I != RegVars.end());
  75. auto &VarSet = I->second;
  76. const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
  77. assert(VarPos != VarSet.end());
  78. VarSet.erase(VarPos);
  79. // Don't keep empty sets in a map to keep it as small as possible.
  80. if (VarSet.empty())
  81. RegVars.erase(I);
  82. }
  83. // \brief Claim that @Var is now described by @RegNo.
  84. static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
  85. InlinedVariable Var) {
  86. assert(RegNo != 0U);
  87. auto &VarSet = RegVars[RegNo];
  88. assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
  89. VarSet.push_back(Var);
  90. }
  91. // \brief Terminate the location range for variables described by register at
  92. // @I by inserting @ClobberingInstr to their history.
  93. static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
  94. RegDescribedVarsMap::iterator I,
  95. DbgValueHistoryMap &HistMap,
  96. const MachineInstr &ClobberingInstr) {
  97. // Iterate over all variables described by this register and add this
  98. // instruction to their history, clobbering it.
  99. for (const auto &Var : I->second)
  100. HistMap.endInstrRange(Var, ClobberingInstr);
  101. RegVars.erase(I);
  102. }
  103. // \brief Terminate the location range for variables described by register
  104. // @RegNo by inserting @ClobberingInstr to their history.
  105. static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
  106. DbgValueHistoryMap &HistMap,
  107. const MachineInstr &ClobberingInstr) {
  108. const auto &I = RegVars.find(RegNo);
  109. if (I == RegVars.end())
  110. return;
  111. clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
  112. }
  113. // \brief Collect all registers clobbered by @MI and apply the functor
  114. // @Func to their RegNo.
  115. // @Func should be a functor with a void(unsigned) signature. We're
  116. // not using std::function here for performance reasons. It has a
  117. // small but measurable impact. By using a functor instead of a
  118. // std::set& here, we can avoid the overhead of constructing
  119. // temporaries in calculateDbgValueHistory, which has a significant
  120. // performance impact.
  121. template<typename Callable>
  122. static void applyToClobberedRegisters(const MachineInstr &MI,
  123. const TargetRegisterInfo *TRI,
  124. Callable Func) {
  125. for (const MachineOperand &MO : MI.operands()) {
  126. if (!MO.isReg() || !MO.isDef() || !MO.getReg())
  127. continue;
  128. for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
  129. Func(*AI);
  130. }
  131. }
  132. // \brief Returns the first instruction in @MBB which corresponds to
  133. // the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
  134. static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
  135. auto LastMI = MBB.getLastNonDebugInstr();
  136. if (LastMI == MBB.end() || !LastMI->isReturn())
  137. return nullptr;
  138. // Assume that epilogue starts with instruction having the same debug location
  139. // as the return instruction.
  140. DebugLoc LastLoc = LastMI->getDebugLoc();
  141. auto Res = LastMI;
  142. for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)),
  143. E = MBB.rend();
  144. I != E; ++I) {
  145. if (I->getDebugLoc() != LastLoc)
  146. return Res;
  147. Res = &*I;
  148. }
  149. // If all instructions have the same debug location, assume whole MBB is
  150. // an epilogue.
  151. return MBB.begin();
  152. }
  153. // \brief Collect registers that are modified in the function body (their
  154. // contents is changed outside of the prologue and epilogue).
  155. static void collectChangingRegs(const MachineFunction *MF,
  156. const TargetRegisterInfo *TRI,
  157. BitVector &Regs) {
  158. for (const auto &MBB : *MF) {
  159. auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
  160. for (const auto &MI : MBB) {
  161. if (&MI == FirstEpilogueInst)
  162. break;
  163. if (!MI.getFlag(MachineInstr::FrameSetup))
  164. applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.set(r); });
  165. }
  166. }
  167. }
  168. void llvm::calculateDbgValueHistory(const MachineFunction *MF,
  169. const TargetRegisterInfo *TRI,
  170. DbgValueHistoryMap &Result) {
  171. BitVector ChangingRegs(TRI->getNumRegs());
  172. collectChangingRegs(MF, TRI, ChangingRegs);
  173. RegDescribedVarsMap RegVars;
  174. for (const auto &MBB : *MF) {
  175. for (const auto &MI : MBB) {
  176. if (!MI.isDebugValue()) {
  177. // Not a DBG_VALUE instruction. It may clobber registers which describe
  178. // some variables.
  179. applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) {
  180. if (ChangingRegs.test(RegNo))
  181. clobberRegisterUses(RegVars, RegNo, Result, MI);
  182. });
  183. continue;
  184. }
  185. assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
  186. // Use the base variable (without any DW_OP_piece expressions)
  187. // as index into History. The full variables including the
  188. // piece expressions are attached to the MI.
  189. const DILocalVariable *RawVar = MI.getDebugVariable();
  190. assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
  191. "Expected inlined-at fields to agree");
  192. InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
  193. if (unsigned PrevReg = Result.getRegisterForVar(Var))
  194. dropRegDescribedVar(RegVars, PrevReg, Var);
  195. Result.startInstrRange(Var, MI);
  196. if (unsigned NewReg = isDescribedByReg(MI))
  197. addRegDescribedVar(RegVars, NewReg, Var);
  198. }
  199. // Make sure locations for register-described variables are valid only
  200. // until the end of the basic block (unless it's the last basic block, in
  201. // which case let their liveness run off to the end of the function).
  202. if (!MBB.empty() && &MBB != &MF->back()) {
  203. for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
  204. auto CurElem = I++; // CurElem can be erased below.
  205. if (ChangingRegs.test(CurElem->first))
  206. clobberRegisterUses(RegVars, CurElem, Result, MBB.back());
  207. }
  208. }
  209. }
  210. }