DbgEntityHistoryCalculator.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //===- llvm/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp -------------===//
  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/CodeGen/DbgEntityHistoryCalculator.h"
  9. #include "llvm/ADT/BitVector.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/ADT/SmallSet.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/CodeGen/MachineBasicBlock.h"
  14. #include "llvm/CodeGen/MachineFunction.h"
  15. #include "llvm/CodeGen/MachineInstr.h"
  16. #include "llvm/CodeGen/MachineOperand.h"
  17. #include "llvm/CodeGen/TargetLowering.h"
  18. #include "llvm/CodeGen/TargetRegisterInfo.h"
  19. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  20. #include "llvm/IR/DebugInfoMetadata.h"
  21. #include "llvm/IR/DebugLoc.h"
  22. #include "llvm/MC/MCRegisterInfo.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <cassert>
  26. #include <map>
  27. #include <utility>
  28. using namespace llvm;
  29. #define DEBUG_TYPE "dwarfdebug"
  30. namespace {
  31. using EntryIndex = DbgValueHistoryMap::EntryIndex;
  32. }
  33. // If @MI is a DBG_VALUE with debug value described by a
  34. // defined register, returns the number of this register.
  35. // In the other case, returns 0.
  36. static Register isDescribedByReg(const MachineInstr &MI) {
  37. assert(MI.isDebugValue());
  38. assert(MI.getNumOperands() == 4);
  39. // If the location of variable is an entry value (DW_OP_entry_value)
  40. // do not consider it as a register location.
  41. if (MI.getDebugExpression()->isEntryValue())
  42. return 0;
  43. // If location of variable is described using a register (directly or
  44. // indirectly), this register is always a first operand.
  45. return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : Register();
  46. }
  47. bool DbgValueHistoryMap::startDbgValue(InlinedEntity Var,
  48. const MachineInstr &MI,
  49. EntryIndex &NewIndex) {
  50. // Instruction range should start with a DBG_VALUE instruction for the
  51. // variable.
  52. assert(MI.isDebugValue() && "not a DBG_VALUE");
  53. auto &Entries = VarEntries[Var];
  54. if (!Entries.empty() && Entries.back().isDbgValue() &&
  55. !Entries.back().isClosed() &&
  56. Entries.back().getInstr()->isIdenticalTo(MI)) {
  57. LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
  58. << "\t" << Entries.back().getInstr() << "\t" << MI
  59. << "\n");
  60. return false;
  61. }
  62. Entries.emplace_back(&MI, Entry::DbgValue);
  63. NewIndex = Entries.size() - 1;
  64. return true;
  65. }
  66. EntryIndex DbgValueHistoryMap::startClobber(InlinedEntity Var,
  67. const MachineInstr &MI) {
  68. auto &Entries = VarEntries[Var];
  69. // If an instruction clobbers multiple registers that the variable is
  70. // described by, then we may have already created a clobbering instruction.
  71. if (Entries.back().isClobber() && Entries.back().getInstr() == &MI)
  72. return Entries.size() - 1;
  73. Entries.emplace_back(&MI, Entry::Clobber);
  74. return Entries.size() - 1;
  75. }
  76. void DbgValueHistoryMap::Entry::endEntry(EntryIndex Index) {
  77. // For now, instruction ranges are not allowed to cross basic block
  78. // boundaries.
  79. assert(isDbgValue() && "Setting end index for non-debug value");
  80. assert(!isClosed() && "End index has already been set");
  81. EndIndex = Index;
  82. }
  83. void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
  84. assert(MI.isDebugLabel() && "not a DBG_LABEL");
  85. LabelInstr[Label] = &MI;
  86. }
  87. namespace {
  88. // Maps physreg numbers to the variables they describe.
  89. using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
  90. using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
  91. // Keeps track of the debug value entries that are currently live for each
  92. // inlined entity. As the history map entries are stored in a SmallVector, they
  93. // may be moved at insertion of new entries, so store indices rather than
  94. // pointers.
  95. using DbgValueEntriesMap = std::map<InlinedEntity, SmallSet<EntryIndex, 1>>;
  96. } // end anonymous namespace
  97. // Claim that @Var is not described by @RegNo anymore.
  98. static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
  99. InlinedEntity Var) {
  100. const auto &I = RegVars.find(RegNo);
  101. assert(RegNo != 0U && I != RegVars.end());
  102. auto &VarSet = I->second;
  103. const auto &VarPos = llvm::find(VarSet, Var);
  104. assert(VarPos != VarSet.end());
  105. VarSet.erase(VarPos);
  106. // Don't keep empty sets in a map to keep it as small as possible.
  107. if (VarSet.empty())
  108. RegVars.erase(I);
  109. }
  110. // Claim that @Var is now described by @RegNo.
  111. static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
  112. InlinedEntity Var) {
  113. assert(RegNo != 0U);
  114. auto &VarSet = RegVars[RegNo];
  115. assert(!is_contained(VarSet, Var));
  116. VarSet.push_back(Var);
  117. }
  118. /// Create a clobbering entry and end all open debug value entries
  119. /// for \p Var that are described by \p RegNo using that entry.
  120. static void clobberRegEntries(InlinedEntity Var, unsigned RegNo,
  121. const MachineInstr &ClobberingInstr,
  122. DbgValueEntriesMap &LiveEntries,
  123. DbgValueHistoryMap &HistMap) {
  124. EntryIndex ClobberIndex = HistMap.startClobber(Var, ClobberingInstr);
  125. // Close all entries whose values are described by the register.
  126. SmallVector<EntryIndex, 4> IndicesToErase;
  127. for (auto Index : LiveEntries[Var]) {
  128. auto &Entry = HistMap.getEntry(Var, Index);
  129. assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
  130. if (isDescribedByReg(*Entry.getInstr()) == RegNo) {
  131. IndicesToErase.push_back(Index);
  132. Entry.endEntry(ClobberIndex);
  133. }
  134. }
  135. // Drop all entries that have ended.
  136. for (auto Index : IndicesToErase)
  137. LiveEntries[Var].erase(Index);
  138. }
  139. /// Add a new debug value for \p Var. Closes all overlapping debug values.
  140. static void handleNewDebugValue(InlinedEntity Var, const MachineInstr &DV,
  141. RegDescribedVarsMap &RegVars,
  142. DbgValueEntriesMap &LiveEntries,
  143. DbgValueHistoryMap &HistMap) {
  144. EntryIndex NewIndex;
  145. if (HistMap.startDbgValue(Var, DV, NewIndex)) {
  146. SmallDenseMap<unsigned, bool, 4> TrackedRegs;
  147. // If we have created a new debug value entry, close all preceding
  148. // live entries that overlap.
  149. SmallVector<EntryIndex, 4> IndicesToErase;
  150. const DIExpression *DIExpr = DV.getDebugExpression();
  151. for (auto Index : LiveEntries[Var]) {
  152. auto &Entry = HistMap.getEntry(Var, Index);
  153. assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
  154. const MachineInstr &DV = *Entry.getInstr();
  155. bool Overlaps = DIExpr->fragmentsOverlap(DV.getDebugExpression());
  156. if (Overlaps) {
  157. IndicesToErase.push_back(Index);
  158. Entry.endEntry(NewIndex);
  159. }
  160. if (Register Reg = isDescribedByReg(DV))
  161. TrackedRegs[Reg] |= !Overlaps;
  162. }
  163. // If the new debug value is described by a register, add tracking of
  164. // that register if it is not already tracked.
  165. if (Register NewReg = isDescribedByReg(DV)) {
  166. if (!TrackedRegs.count(NewReg))
  167. addRegDescribedVar(RegVars, NewReg, Var);
  168. LiveEntries[Var].insert(NewIndex);
  169. TrackedRegs[NewReg] = true;
  170. }
  171. // Drop tracking of registers that are no longer used.
  172. for (auto I : TrackedRegs)
  173. if (!I.second)
  174. dropRegDescribedVar(RegVars, I.first, Var);
  175. // Drop all entries that have ended, and mark the new entry as live.
  176. for (auto Index : IndicesToErase)
  177. LiveEntries[Var].erase(Index);
  178. LiveEntries[Var].insert(NewIndex);
  179. }
  180. }
  181. // Terminate the location range for variables described by register at
  182. // @I by inserting @ClobberingInstr to their history.
  183. static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
  184. RegDescribedVarsMap::iterator I,
  185. DbgValueHistoryMap &HistMap,
  186. DbgValueEntriesMap &LiveEntries,
  187. const MachineInstr &ClobberingInstr) {
  188. // Iterate over all variables described by this register and add this
  189. // instruction to their history, clobbering it.
  190. for (const auto &Var : I->second)
  191. clobberRegEntries(Var, I->first, ClobberingInstr, LiveEntries, HistMap);
  192. RegVars.erase(I);
  193. }
  194. // Terminate the location range for variables described by register
  195. // @RegNo by inserting @ClobberingInstr to their history.
  196. static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
  197. DbgValueHistoryMap &HistMap,
  198. DbgValueEntriesMap &LiveEntries,
  199. const MachineInstr &ClobberingInstr) {
  200. const auto &I = RegVars.find(RegNo);
  201. if (I == RegVars.end())
  202. return;
  203. clobberRegisterUses(RegVars, I, HistMap, LiveEntries, ClobberingInstr);
  204. }
  205. void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
  206. const TargetRegisterInfo *TRI,
  207. DbgValueHistoryMap &DbgValues,
  208. DbgLabelInstrMap &DbgLabels) {
  209. const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
  210. unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
  211. Register FrameReg = TRI->getFrameRegister(*MF);
  212. RegDescribedVarsMap RegVars;
  213. DbgValueEntriesMap LiveEntries;
  214. for (const auto &MBB : *MF) {
  215. for (const auto &MI : MBB) {
  216. if (MI.isDebugValue()) {
  217. assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
  218. // Use the base variable (without any DW_OP_piece expressions)
  219. // as index into History. The full variables including the
  220. // piece expressions are attached to the MI.
  221. const DILocalVariable *RawVar = MI.getDebugVariable();
  222. assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
  223. "Expected inlined-at fields to agree");
  224. InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
  225. handleNewDebugValue(Var, MI, RegVars, LiveEntries, DbgValues);
  226. } else if (MI.isDebugLabel()) {
  227. assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
  228. const DILabel *RawLabel = MI.getDebugLabel();
  229. assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
  230. "Expected inlined-at fields to agree");
  231. // When collecting debug information for labels, there is no MCSymbol
  232. // generated for it. So, we keep MachineInstr in DbgLabels in order
  233. // to query MCSymbol afterward.
  234. InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
  235. DbgLabels.addInstr(L, MI);
  236. }
  237. if (MI.isDebugInstr())
  238. continue;
  239. // Not a DBG_VALUE instruction. It may clobber registers which describe
  240. // some variables.
  241. for (const MachineOperand &MO : MI.operands()) {
  242. if (MO.isReg() && MO.isDef() && MO.getReg()) {
  243. // Ignore call instructions that claim to clobber SP. The AArch64
  244. // backend does this for aggregate function arguments.
  245. if (MI.isCall() && MO.getReg() == SP)
  246. continue;
  247. // If this is a virtual register, only clobber it since it doesn't
  248. // have aliases.
  249. if (Register::isVirtualRegister(MO.getReg()))
  250. clobberRegisterUses(RegVars, MO.getReg(), DbgValues, LiveEntries,
  251. MI);
  252. // If this is a register def operand, it may end a debug value
  253. // range. Ignore frame-register defs in the epilogue and prologue,
  254. // we expect debuggers to understand that stack-locations are
  255. // invalid outside of the function body.
  256. else if (MO.getReg() != FrameReg ||
  257. (!MI.getFlag(MachineInstr::FrameDestroy) &&
  258. !MI.getFlag(MachineInstr::FrameSetup))) {
  259. for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
  260. ++AI)
  261. clobberRegisterUses(RegVars, *AI, DbgValues, LiveEntries, MI);
  262. }
  263. } else if (MO.isRegMask()) {
  264. // If this is a register mask operand, clobber all debug values in
  265. // non-CSRs.
  266. SmallVector<unsigned, 32> RegsToClobber;
  267. // Don't consider SP to be clobbered by register masks.
  268. for (auto It : RegVars) {
  269. unsigned int Reg = It.first;
  270. if (Reg != SP && Register::isPhysicalRegister(Reg) &&
  271. MO.clobbersPhysReg(Reg))
  272. RegsToClobber.push_back(Reg);
  273. }
  274. for (unsigned Reg : RegsToClobber) {
  275. clobberRegisterUses(RegVars, Reg, DbgValues, LiveEntries, MI);
  276. }
  277. }
  278. } // End MO loop.
  279. } // End instr loop.
  280. // Make sure locations for all variables are valid only until the end of
  281. // the basic block (unless it's the last basic block, in which case let
  282. // their liveness run off to the end of the function).
  283. if (!MBB.empty() && &MBB != &MF->back()) {
  284. // Iterate over all variables that have open debug values.
  285. for (auto &Pair : LiveEntries) {
  286. if (Pair.second.empty())
  287. continue;
  288. // Create a clobbering entry.
  289. EntryIndex ClobIdx = DbgValues.startClobber(Pair.first, MBB.back());
  290. // End all entries.
  291. for (EntryIndex Idx : Pair.second) {
  292. DbgValueHistoryMap::Entry &Ent = DbgValues.getEntry(Pair.first, Idx);
  293. assert(Ent.isDbgValue() && !Ent.isClosed());
  294. Ent.endEntry(ClobIdx);
  295. }
  296. }
  297. LiveEntries.clear();
  298. RegVars.clear();
  299. }
  300. }
  301. }
  302. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  303. LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
  304. dbgs() << "DbgValueHistoryMap:\n";
  305. for (const auto &VarRangePair : *this) {
  306. const InlinedEntity &Var = VarRangePair.first;
  307. const Entries &Entries = VarRangePair.second;
  308. const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
  309. const DILocation *Location = Var.second;
  310. dbgs() << " - " << LocalVar->getName() << " at ";
  311. if (Location)
  312. dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
  313. << Location->getColumn();
  314. else
  315. dbgs() << "<unknown location>";
  316. dbgs() << " --\n";
  317. for (const auto &E : enumerate(Entries)) {
  318. const auto &Entry = E.value();
  319. dbgs() << " Entry[" << E.index() << "]: ";
  320. if (Entry.isDbgValue())
  321. dbgs() << "Debug value\n";
  322. else
  323. dbgs() << "Clobber\n";
  324. dbgs() << " Instr: " << *Entry.getInstr();
  325. if (Entry.isDbgValue()) {
  326. if (Entry.getEndIndex() == NoEntry)
  327. dbgs() << " - Valid until end of function\n";
  328. else
  329. dbgs() << " - Closed by Entry[" << Entry.getEndIndex() << "]\n";
  330. }
  331. dbgs() << "\n";
  332. }
  333. }
  334. }
  335. #endif