DbgEntityHistoryCalculator.cpp 24 KB

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