DbgEntityHistoryCalculator.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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/STLExtras.h"
  10. #include "llvm/ADT/SmallSet.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/CodeGen/LexicalScopes.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 <optional>
  28. #include <utility>
  29. using namespace llvm;
  30. #define DEBUG_TYPE "dwarfdebug"
  31. namespace {
  32. using EntryIndex = DbgValueHistoryMap::EntryIndex;
  33. }
  34. void InstructionOrdering::initialize(const MachineFunction &MF) {
  35. // We give meta instructions the same ordinal as the preceding instruction
  36. // because this class is written for the task of comparing positions of
  37. // variable location ranges against scope ranges. To reflect what we'll see
  38. // in the binary, when we look at location ranges we must consider all
  39. // DBG_VALUEs between two real instructions at the same position. And a
  40. // scope range which ends on a meta instruction should be considered to end
  41. // at the last seen real instruction. E.g.
  42. //
  43. // 1 instruction p Both the variable location for x and for y start
  44. // 1 DBG_VALUE for "x" after instruction p so we give them all the same
  45. // 1 DBG_VALUE for "y" number. If a scope range ends at DBG_VALUE for "y",
  46. // 2 instruction q we should treat it as ending after instruction p
  47. // because it will be the last real instruction in the
  48. // range. DBG_VALUEs at or after this position for
  49. // variables declared in the scope will have no effect.
  50. clear();
  51. unsigned Position = 0;
  52. for (const MachineBasicBlock &MBB : MF)
  53. for (const MachineInstr &MI : MBB)
  54. InstNumberMap[&MI] = MI.isMetaInstruction() ? Position : ++Position;
  55. }
  56. bool InstructionOrdering::isBefore(const MachineInstr *A,
  57. const MachineInstr *B) const {
  58. assert(A->getParent() && B->getParent() && "Operands must have a parent");
  59. assert(A->getMF() == B->getMF() &&
  60. "Operands must be in the same MachineFunction");
  61. return InstNumberMap.lookup(A) < InstNumberMap.lookup(B);
  62. }
  63. bool DbgValueHistoryMap::startDbgValue(InlinedEntity Var,
  64. const MachineInstr &MI,
  65. EntryIndex &NewIndex) {
  66. // Instruction range should start with a DBG_VALUE instruction for the
  67. // variable.
  68. assert(MI.isDebugValue() && "not a DBG_VALUE");
  69. auto &Entries = VarEntries[Var];
  70. if (!Entries.empty() && Entries.back().isDbgValue() &&
  71. !Entries.back().isClosed() &&
  72. Entries.back().getInstr()->isEquivalentDbgInstr(MI)) {
  73. LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
  74. << "\t" << Entries.back().getInstr() << "\t" << MI
  75. << "\n");
  76. return false;
  77. }
  78. Entries.emplace_back(&MI, Entry::DbgValue);
  79. NewIndex = Entries.size() - 1;
  80. return true;
  81. }
  82. EntryIndex DbgValueHistoryMap::startClobber(InlinedEntity Var,
  83. const MachineInstr &MI) {
  84. auto &Entries = VarEntries[Var];
  85. // If an instruction clobbers multiple registers that the variable is
  86. // described by, then we may have already created a clobbering instruction.
  87. if (Entries.back().isClobber() && Entries.back().getInstr() == &MI)
  88. return Entries.size() - 1;
  89. Entries.emplace_back(&MI, Entry::Clobber);
  90. return Entries.size() - 1;
  91. }
  92. void DbgValueHistoryMap::Entry::endEntry(EntryIndex Index) {
  93. // For now, instruction ranges are not allowed to cross basic block
  94. // boundaries.
  95. assert(isDbgValue() && "Setting end index for non-debug value");
  96. assert(!isClosed() && "End index has already been set");
  97. EndIndex = Index;
  98. }
  99. /// Check if the instruction range [StartMI, EndMI] intersects any instruction
  100. /// range in Ranges. EndMI can be nullptr to indicate that the range is
  101. /// unbounded. Assumes Ranges is ordered and disjoint. Returns true and points
  102. /// to the first intersecting scope range if one exists.
  103. static std::optional<ArrayRef<InsnRange>::iterator>
  104. intersects(const MachineInstr *StartMI, const MachineInstr *EndMI,
  105. const ArrayRef<InsnRange> &Ranges,
  106. const InstructionOrdering &Ordering) {
  107. for (auto RangesI = Ranges.begin(), RangesE = Ranges.end();
  108. RangesI != RangesE; ++RangesI) {
  109. if (EndMI && Ordering.isBefore(EndMI, RangesI->first))
  110. return std::nullopt;
  111. if (EndMI && !Ordering.isBefore(RangesI->second, EndMI))
  112. return RangesI;
  113. if (Ordering.isBefore(StartMI, RangesI->second))
  114. return RangesI;
  115. }
  116. return std::nullopt;
  117. }
  118. void DbgValueHistoryMap::trimLocationRanges(
  119. const MachineFunction &MF, LexicalScopes &LScopes,
  120. const InstructionOrdering &Ordering) {
  121. // The indices of the entries we're going to remove for each variable.
  122. SmallVector<EntryIndex, 4> ToRemove;
  123. // Entry reference count for each variable. Clobbers left with no references
  124. // will be removed.
  125. SmallVector<int, 4> ReferenceCount;
  126. // Entries reference other entries by index. Offsets is used to remap these
  127. // references if any entries are removed.
  128. SmallVector<size_t, 4> Offsets;
  129. for (auto &Record : VarEntries) {
  130. auto &HistoryMapEntries = Record.second;
  131. if (HistoryMapEntries.empty())
  132. continue;
  133. InlinedEntity Entity = Record.first;
  134. const DILocalVariable *LocalVar = cast<DILocalVariable>(Entity.first);
  135. LexicalScope *Scope = nullptr;
  136. if (const DILocation *InlinedAt = Entity.second) {
  137. Scope = LScopes.findInlinedScope(LocalVar->getScope(), InlinedAt);
  138. } else {
  139. Scope = LScopes.findLexicalScope(LocalVar->getScope());
  140. // Ignore variables for non-inlined function level scopes. The scope
  141. // ranges (from scope->getRanges()) will not include any instructions
  142. // before the first one with a debug-location, which could cause us to
  143. // incorrectly drop a location. We could introduce special casing for
  144. // these variables, but it doesn't seem worth it because no out-of-scope
  145. // locations have been observed for variables declared in function level
  146. // scopes.
  147. if (Scope &&
  148. (Scope->getScopeNode() == Scope->getScopeNode()->getSubprogram()) &&
  149. (Scope->getScopeNode() == LocalVar->getScope()))
  150. continue;
  151. }
  152. // If there is no scope for the variable then something has probably gone
  153. // wrong.
  154. if (!Scope)
  155. continue;
  156. ToRemove.clear();
  157. // Zero the reference counts.
  158. ReferenceCount.assign(HistoryMapEntries.size(), 0);
  159. // Index of the DBG_VALUE which marks the start of the current location
  160. // range.
  161. EntryIndex StartIndex = 0;
  162. ArrayRef<InsnRange> ScopeRanges(Scope->getRanges());
  163. for (auto EI = HistoryMapEntries.begin(), EE = HistoryMapEntries.end();
  164. EI != EE; ++EI, ++StartIndex) {
  165. // Only DBG_VALUEs can open location ranges so skip anything else.
  166. if (!EI->isDbgValue())
  167. continue;
  168. // Index of the entry which closes this range.
  169. EntryIndex EndIndex = EI->getEndIndex();
  170. // If this range is closed bump the reference count of the closing entry.
  171. if (EndIndex != NoEntry)
  172. ReferenceCount[EndIndex] += 1;
  173. // Skip this location range if the opening entry is still referenced. It
  174. // may close a location range which intersects a scope range.
  175. // TODO: We could be 'smarter' and trim these kinds of ranges such that
  176. // they do not leak out of the scope ranges if they partially overlap.
  177. if (ReferenceCount[StartIndex] > 0)
  178. continue;
  179. const MachineInstr *StartMI = EI->getInstr();
  180. const MachineInstr *EndMI = EndIndex != NoEntry
  181. ? HistoryMapEntries[EndIndex].getInstr()
  182. : nullptr;
  183. // Check if the location range [StartMI, EndMI] intersects with any scope
  184. // range for the variable.
  185. if (auto R = intersects(StartMI, EndMI, ScopeRanges, Ordering)) {
  186. // Adjust ScopeRanges to exclude ranges which subsequent location ranges
  187. // cannot possibly intersect.
  188. ScopeRanges = ArrayRef<InsnRange>(*R, ScopeRanges.end());
  189. } else {
  190. // If the location range does not intersect any scope range then the
  191. // DBG_VALUE which opened this location range is usless, mark it for
  192. // removal.
  193. ToRemove.push_back(StartIndex);
  194. // Because we'll be removing this entry we need to update the reference
  195. // count of the closing entry, if one exists.
  196. if (EndIndex != NoEntry)
  197. ReferenceCount[EndIndex] -= 1;
  198. }
  199. }
  200. // If there is nothing to remove then jump to next variable.
  201. if (ToRemove.empty())
  202. continue;
  203. // Mark clobbers that will no longer close any location ranges for removal.
  204. for (size_t i = 0; i < HistoryMapEntries.size(); ++i)
  205. if (ReferenceCount[i] <= 0 && HistoryMapEntries[i].isClobber())
  206. ToRemove.push_back(i);
  207. llvm::sort(ToRemove);
  208. // Build an offset map so we can update the EndIndex of the remaining
  209. // entries.
  210. // Zero the offsets.
  211. Offsets.assign(HistoryMapEntries.size(), 0);
  212. size_t CurOffset = 0;
  213. auto ToRemoveItr = ToRemove.begin();
  214. for (size_t EntryIdx = *ToRemoveItr; EntryIdx < HistoryMapEntries.size();
  215. ++EntryIdx) {
  216. // Check if this is an entry which will be removed.
  217. if (ToRemoveItr != ToRemove.end() && *ToRemoveItr == EntryIdx) {
  218. ++ToRemoveItr;
  219. ++CurOffset;
  220. }
  221. Offsets[EntryIdx] = CurOffset;
  222. }
  223. // Update the EndIndex of the entries to account for those which will be
  224. // removed.
  225. for (auto &Entry : HistoryMapEntries)
  226. if (Entry.isClosed())
  227. Entry.EndIndex -= Offsets[Entry.EndIndex];
  228. // Now actually remove the entries. Iterate backwards so that our remaining
  229. // ToRemove indices are valid after each erase.
  230. for (EntryIndex Idx : llvm::reverse(ToRemove))
  231. HistoryMapEntries.erase(HistoryMapEntries.begin() + Idx);
  232. }
  233. }
  234. bool DbgValueHistoryMap::hasNonEmptyLocation(const Entries &Entries) const {
  235. for (const auto &Entry : Entries) {
  236. if (!Entry.isDbgValue())
  237. continue;
  238. const MachineInstr *MI = Entry.getInstr();
  239. assert(MI->isDebugValue());
  240. // A DBG_VALUE $noreg is an empty variable location
  241. if (MI->isUndefDebugValue())
  242. continue;
  243. return true;
  244. }
  245. return false;
  246. }
  247. void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
  248. assert(MI.isDebugLabel() && "not a DBG_LABEL");
  249. LabelInstr[Label] = &MI;
  250. }
  251. namespace {
  252. // Maps physreg numbers to the variables they describe.
  253. using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
  254. using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
  255. // Keeps track of the debug value entries that are currently live for each
  256. // inlined entity. As the history map entries are stored in a SmallVector, they
  257. // may be moved at insertion of new entries, so store indices rather than
  258. // pointers.
  259. using DbgValueEntriesMap = std::map<InlinedEntity, SmallSet<EntryIndex, 1>>;
  260. } // end anonymous namespace
  261. // Claim that @Var is not described by @RegNo anymore.
  262. static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
  263. InlinedEntity Var) {
  264. const auto &I = RegVars.find(RegNo);
  265. assert(RegNo != 0U && I != RegVars.end());
  266. auto &VarSet = I->second;
  267. const auto &VarPos = llvm::find(VarSet, Var);
  268. assert(VarPos != VarSet.end());
  269. VarSet.erase(VarPos);
  270. // Don't keep empty sets in a map to keep it as small as possible.
  271. if (VarSet.empty())
  272. RegVars.erase(I);
  273. }
  274. // Claim that @Var is now described by @RegNo.
  275. static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
  276. InlinedEntity Var) {
  277. assert(RegNo != 0U);
  278. auto &VarSet = RegVars[RegNo];
  279. assert(!is_contained(VarSet, Var));
  280. VarSet.push_back(Var);
  281. }
  282. /// Create a clobbering entry and end all open debug value entries
  283. /// for \p Var that are described by \p RegNo using that entry. Inserts into \p
  284. /// FellowRegisters the set of Registers that were also used to describe \p Var
  285. /// alongside \p RegNo.
  286. static void clobberRegEntries(InlinedEntity Var, unsigned RegNo,
  287. const MachineInstr &ClobberingInstr,
  288. DbgValueEntriesMap &LiveEntries,
  289. DbgValueHistoryMap &HistMap,
  290. SmallVectorImpl<Register> &FellowRegisters) {
  291. EntryIndex ClobberIndex = HistMap.startClobber(Var, ClobberingInstr);
  292. // Close all entries whose values are described by the register.
  293. SmallVector<EntryIndex, 4> IndicesToErase;
  294. // If a given register appears in a live DBG_VALUE_LIST for Var alongside the
  295. // clobbered register, and never appears in a live DBG_VALUE* for Var without
  296. // the clobbered register, then it is no longer linked to the variable.
  297. SmallSet<Register, 4> MaybeRemovedRegisters;
  298. SmallSet<Register, 4> KeepRegisters;
  299. for (auto Index : LiveEntries[Var]) {
  300. auto &Entry = HistMap.getEntry(Var, Index);
  301. assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
  302. if (Entry.getInstr()->isDebugEntryValue())
  303. continue;
  304. if (Entry.getInstr()->hasDebugOperandForReg(RegNo)) {
  305. IndicesToErase.push_back(Index);
  306. Entry.endEntry(ClobberIndex);
  307. for (const auto &MO : Entry.getInstr()->debug_operands())
  308. if (MO.isReg() && MO.getReg() && MO.getReg() != RegNo)
  309. MaybeRemovedRegisters.insert(MO.getReg());
  310. } else {
  311. for (const auto &MO : Entry.getInstr()->debug_operands())
  312. if (MO.isReg() && MO.getReg())
  313. KeepRegisters.insert(MO.getReg());
  314. }
  315. }
  316. for (Register Reg : MaybeRemovedRegisters)
  317. if (!KeepRegisters.contains(Reg))
  318. FellowRegisters.push_back(Reg);
  319. // Drop all entries that have ended.
  320. for (auto Index : IndicesToErase)
  321. LiveEntries[Var].erase(Index);
  322. }
  323. /// Add a new debug value for \p Var. Closes all overlapping debug values.
  324. static void handleNewDebugValue(InlinedEntity Var, const MachineInstr &DV,
  325. RegDescribedVarsMap &RegVars,
  326. DbgValueEntriesMap &LiveEntries,
  327. DbgValueHistoryMap &HistMap) {
  328. EntryIndex NewIndex;
  329. if (HistMap.startDbgValue(Var, DV, NewIndex)) {
  330. SmallDenseMap<unsigned, bool, 4> TrackedRegs;
  331. // If we have created a new debug value entry, close all preceding
  332. // live entries that overlap.
  333. SmallVector<EntryIndex, 4> IndicesToErase;
  334. const DIExpression *DIExpr = DV.getDebugExpression();
  335. for (auto Index : LiveEntries[Var]) {
  336. auto &Entry = HistMap.getEntry(Var, Index);
  337. assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
  338. const MachineInstr &DV = *Entry.getInstr();
  339. bool Overlaps = DIExpr->fragmentsOverlap(DV.getDebugExpression());
  340. if (Overlaps) {
  341. IndicesToErase.push_back(Index);
  342. Entry.endEntry(NewIndex);
  343. }
  344. if (!DV.isDebugEntryValue())
  345. for (const MachineOperand &Op : DV.debug_operands())
  346. if (Op.isReg() && Op.getReg())
  347. TrackedRegs[Op.getReg()] |= !Overlaps;
  348. }
  349. // If the new debug value is described by a register, add tracking of
  350. // that register if it is not already tracked.
  351. if (!DV.isDebugEntryValue()) {
  352. for (const MachineOperand &Op : DV.debug_operands()) {
  353. if (Op.isReg() && Op.getReg()) {
  354. Register NewReg = Op.getReg();
  355. if (!TrackedRegs.count(NewReg))
  356. addRegDescribedVar(RegVars, NewReg, Var);
  357. LiveEntries[Var].insert(NewIndex);
  358. TrackedRegs[NewReg] = true;
  359. }
  360. }
  361. }
  362. // Drop tracking of registers that are no longer used.
  363. for (auto I : TrackedRegs)
  364. if (!I.second)
  365. dropRegDescribedVar(RegVars, I.first, Var);
  366. // Drop all entries that have ended, and mark the new entry as live.
  367. for (auto Index : IndicesToErase)
  368. LiveEntries[Var].erase(Index);
  369. LiveEntries[Var].insert(NewIndex);
  370. }
  371. }
  372. // Terminate the location range for variables described by register at
  373. // @I by inserting @ClobberingInstr to their history.
  374. static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
  375. RegDescribedVarsMap::iterator I,
  376. DbgValueHistoryMap &HistMap,
  377. DbgValueEntriesMap &LiveEntries,
  378. const MachineInstr &ClobberingInstr) {
  379. // Iterate over all variables described by this register and add this
  380. // instruction to their history, clobbering it. All registers that also
  381. // describe the clobbered variables (i.e. in variadic debug values) will have
  382. // those Variables removed from their DescribedVars.
  383. for (const auto &Var : I->second) {
  384. SmallVector<Register, 4> FellowRegisters;
  385. clobberRegEntries(Var, I->first, ClobberingInstr, LiveEntries, HistMap,
  386. FellowRegisters);
  387. for (Register RegNo : FellowRegisters)
  388. dropRegDescribedVar(RegVars, RegNo, Var);
  389. }
  390. RegVars.erase(I);
  391. }
  392. // Terminate the location range for variables described by register
  393. // @RegNo by inserting @ClobberingInstr to their history.
  394. static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
  395. DbgValueHistoryMap &HistMap,
  396. DbgValueEntriesMap &LiveEntries,
  397. const MachineInstr &ClobberingInstr) {
  398. const auto &I = RegVars.find(RegNo);
  399. if (I == RegVars.end())
  400. return;
  401. clobberRegisterUses(RegVars, I, HistMap, LiveEntries, ClobberingInstr);
  402. }
  403. void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
  404. const TargetRegisterInfo *TRI,
  405. DbgValueHistoryMap &DbgValues,
  406. DbgLabelInstrMap &DbgLabels) {
  407. const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
  408. Register SP = TLI->getStackPointerRegisterToSaveRestore();
  409. Register FrameReg = TRI->getFrameRegister(*MF);
  410. RegDescribedVarsMap RegVars;
  411. DbgValueEntriesMap LiveEntries;
  412. for (const auto &MBB : *MF) {
  413. for (const auto &MI : MBB) {
  414. if (MI.isDebugValue()) {
  415. assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
  416. // Use the base variable (without any DW_OP_piece expressions)
  417. // as index into History. The full variables including the
  418. // piece expressions are attached to the MI.
  419. const DILocalVariable *RawVar = MI.getDebugVariable();
  420. assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
  421. "Expected inlined-at fields to agree");
  422. InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
  423. handleNewDebugValue(Var, MI, RegVars, LiveEntries, DbgValues);
  424. } else if (MI.isDebugLabel()) {
  425. assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
  426. const DILabel *RawLabel = MI.getDebugLabel();
  427. assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
  428. "Expected inlined-at fields to agree");
  429. // When collecting debug information for labels, there is no MCSymbol
  430. // generated for it. So, we keep MachineInstr in DbgLabels in order
  431. // to query MCSymbol afterward.
  432. InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
  433. DbgLabels.addInstr(L, MI);
  434. }
  435. // Meta Instructions have no output and do not change any values and so
  436. // can be safely ignored.
  437. if (MI.isMetaInstruction())
  438. continue;
  439. // Not a DBG_VALUE instruction. It may clobber registers which describe
  440. // some variables.
  441. for (const MachineOperand &MO : MI.operands()) {
  442. if (MO.isReg() && MO.isDef() && MO.getReg()) {
  443. // Ignore call instructions that claim to clobber SP. The AArch64
  444. // backend does this for aggregate function arguments.
  445. if (MI.isCall() && MO.getReg() == SP)
  446. continue;
  447. // If this is a virtual register, only clobber it since it doesn't
  448. // have aliases.
  449. if (MO.getReg().isVirtual())
  450. clobberRegisterUses(RegVars, MO.getReg(), DbgValues, LiveEntries,
  451. MI);
  452. // If this is a register def operand, it may end a debug value
  453. // range. Ignore frame-register defs in the epilogue and prologue,
  454. // we expect debuggers to understand that stack-locations are
  455. // invalid outside of the function body.
  456. else if (MO.getReg() != FrameReg ||
  457. (!MI.getFlag(MachineInstr::FrameDestroy) &&
  458. !MI.getFlag(MachineInstr::FrameSetup))) {
  459. for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
  460. ++AI)
  461. clobberRegisterUses(RegVars, *AI, DbgValues, LiveEntries, MI);
  462. }
  463. } else if (MO.isRegMask()) {
  464. // If this is a register mask operand, clobber all debug values in
  465. // non-CSRs.
  466. SmallVector<unsigned, 32> RegsToClobber;
  467. // Don't consider SP to be clobbered by register masks.
  468. for (auto It : RegVars) {
  469. unsigned int Reg = It.first;
  470. if (Reg != SP && Register::isPhysicalRegister(Reg) &&
  471. MO.clobbersPhysReg(Reg))
  472. RegsToClobber.push_back(Reg);
  473. }
  474. for (unsigned Reg : RegsToClobber) {
  475. clobberRegisterUses(RegVars, Reg, DbgValues, LiveEntries, MI);
  476. }
  477. }
  478. } // End MO loop.
  479. } // End instr loop.
  480. // Make sure locations for all variables are valid only until the end of
  481. // the basic block (unless it's the last basic block, in which case let
  482. // their liveness run off to the end of the function).
  483. if (!MBB.empty() && &MBB != &MF->back()) {
  484. // Iterate over all variables that have open debug values.
  485. for (auto &Pair : LiveEntries) {
  486. if (Pair.second.empty())
  487. continue;
  488. // Create a clobbering entry.
  489. EntryIndex ClobIdx = DbgValues.startClobber(Pair.first, MBB.back());
  490. // End all entries.
  491. for (EntryIndex Idx : Pair.second) {
  492. DbgValueHistoryMap::Entry &Ent = DbgValues.getEntry(Pair.first, Idx);
  493. assert(Ent.isDbgValue() && !Ent.isClosed());
  494. Ent.endEntry(ClobIdx);
  495. }
  496. }
  497. LiveEntries.clear();
  498. RegVars.clear();
  499. }
  500. }
  501. }
  502. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  503. LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
  504. dbgs() << "DbgValueHistoryMap:\n";
  505. for (const auto &VarRangePair : *this) {
  506. const InlinedEntity &Var = VarRangePair.first;
  507. const Entries &Entries = VarRangePair.second;
  508. const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
  509. const DILocation *Location = Var.second;
  510. dbgs() << " - " << LocalVar->getName() << " at ";
  511. if (Location)
  512. dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
  513. << Location->getColumn();
  514. else
  515. dbgs() << "<unknown location>";
  516. dbgs() << " --\n";
  517. for (const auto &E : enumerate(Entries)) {
  518. const auto &Entry = E.value();
  519. dbgs() << " Entry[" << E.index() << "]: ";
  520. if (Entry.isDbgValue())
  521. dbgs() << "Debug value\n";
  522. else
  523. dbgs() << "Clobber\n";
  524. dbgs() << " Instr: " << *Entry.getInstr();
  525. if (Entry.isDbgValue()) {
  526. if (Entry.getEndIndex() == NoEntry)
  527. dbgs() << " - Valid until end of function\n";
  528. else
  529. dbgs() << " - Closed by Entry[" << Entry.getEndIndex() << "]\n";
  530. }
  531. dbgs() << "\n";
  532. }
  533. }
  534. }
  535. #endif