LLVM 24.0.0git
LiveDebugVariables.cpp
Go to the documentation of this file.
1//===- LiveDebugVariables.cpp - Tracking debug info variables -------------===//
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//
9// This file implements the LiveDebugVariables analysis.
10//
11// Remove all DBG_VALUE instructions referencing virtual registers and replace
12// them with a data structure tracking where live user variables are kept - in a
13// virtual register or in a stack slot.
14//
15// Allow the data structure to be updated during register allocation when values
16// are moved between registers and stack slots. Finally emit new DBG_VALUE
17// instructions after register allocation is complete.
18//
19//===----------------------------------------------------------------------===//
20
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/MapVector.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/SmallSet.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/StringRef.h"
48#include "llvm/Config/llvm-config.h"
50#include "llvm/IR/DebugLoc.h"
51#include "llvm/IR/Function.h"
53#include "llvm/Pass.h"
56#include "llvm/Support/Debug.h"
58#include <algorithm>
59#include <cassert>
60#include <iterator>
61#include <map>
62#include <memory>
63#include <optional>
64#include <utility>
65
66using namespace llvm;
67
68#define DEBUG_TYPE "livedebugvars"
69
70static cl::opt<bool>
71EnableLDV("live-debug-variables", cl::init(true),
72 cl::desc("Enable the live debug variables pass"), cl::Hidden);
73
74STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted");
75STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted");
76STATISTIC(NumStaleIndexes, "Number of stale SlotIndexes repaired");
77STATISTIC(NumMergedIntervals,
78 "Number of debug value intervals merged while repairing indexes");
79
81
83 "Debug Variable Analysis", false, false)
86 "Debug Variable Analysis", false, true)
87
89 AnalysisUsage &AU) const {
90 AU.addRequiredTransitive<LiveIntervalsWrapperPass>();
91 AU.setPreservesAll();
93}
94
97
98enum : unsigned { UndefLocNo = ~0U };
99
100namespace {
101/// Describes a debug variable value by location number and expression along
102/// with some flags about the original usage of the location.
103class DbgVariableValue {
104public:
105 DbgVariableValue(ArrayRef<unsigned> NewLocs, bool WasIndirect, bool WasList,
106 const DIExpression &Expr)
107 : WasIndirect(WasIndirect), WasList(WasList), Expression(&Expr) {
108 assert(!(WasIndirect && WasList) &&
109 "DBG_VALUE_LISTs should not be indirect.");
110 SmallVector<unsigned> LocNoVec;
111 for (unsigned LocNo : NewLocs) {
112 auto It = find(LocNoVec, LocNo);
113 if (It == LocNoVec.end())
114 LocNoVec.push_back(LocNo);
115 else {
116 // Loc duplicates an element in LocNos; replace references to Op
117 // with references to the duplicating element.
118 unsigned OpIdx = LocNoVec.size();
119 unsigned DuplicatingIdx = std::distance(LocNoVec.begin(), It);
120 Expression =
121 DIExpression::replaceArg(Expression, OpIdx, DuplicatingIdx);
122 }
123 }
124 // FIXME: Debug values referencing 64+ unique machine locations are rare and
125 // currently unsupported for performance reasons. If we can verify that
126 // performance is acceptable for such debug values, we can increase the
127 // bit-width of LocNoCount to 14 to enable up to 16384 unique machine
128 // locations. We will also need to verify that this does not cause issues
129 // with LiveDebugVariables' use of IntervalMap.
130 if (LocNoVec.size() < 64) {
131 LocNoCount = LocNoVec.size();
132 if (LocNoCount > 0) {
133 LocNos = std::make_unique<unsigned[]>(LocNoCount);
134 llvm::copy(LocNoVec, loc_nos_begin());
135 }
136 } else {
137 LLVM_DEBUG(dbgs() << "Found debug value with 64+ unique machine "
138 "locations, dropping...\n");
139 LocNoCount = 1;
140 // Turn this into an undef debug value list; right now, the simplest form
141 // of this is an expression with one arg, and an undef debug operand.
142 Expression =
143 DIExpression::get(Expr.getContext(), {dwarf::DW_OP_LLVM_arg, 0});
144 if (auto FragmentInfoOpt = Expr.getFragmentInfo())
146 Expression, FragmentInfoOpt->OffsetInBits,
147 FragmentInfoOpt->SizeInBits);
148 LocNos = std::make_unique<unsigned[]>(LocNoCount);
149 LocNos[0] = UndefLocNo;
150 }
151 }
152
153 DbgVariableValue() : LocNoCount(0), WasIndirect(false), WasList(false) {}
154 DbgVariableValue(const DbgVariableValue &Other)
155 : LocNoCount(Other.LocNoCount), WasIndirect(Other.getWasIndirect()),
156 WasList(Other.getWasList()), Expression(Other.getExpression()) {
157 if (Other.getLocNoCount()) {
158 LocNos.reset(new unsigned[Other.getLocNoCount()]);
159 std::copy(Other.loc_nos_begin(), Other.loc_nos_end(), loc_nos_begin());
160 }
161 }
162
163 DbgVariableValue &operator=(const DbgVariableValue &Other) {
164 if (this == &Other)
165 return *this;
166 if (Other.getLocNoCount()) {
167 LocNos.reset(new unsigned[Other.getLocNoCount()]);
168 std::copy(Other.loc_nos_begin(), Other.loc_nos_end(), loc_nos_begin());
169 } else {
170 LocNos.release();
171 }
172 LocNoCount = Other.getLocNoCount();
173 WasIndirect = Other.getWasIndirect();
174 WasList = Other.getWasList();
175 Expression = Other.getExpression();
176 return *this;
177 }
178
179 const DIExpression *getExpression() const { return Expression; }
180 uint8_t getLocNoCount() const { return LocNoCount; }
181 bool containsLocNo(unsigned LocNo) const {
182 return is_contained(loc_nos(), LocNo);
183 }
184 bool getWasIndirect() const { return WasIndirect; }
185 bool getWasList() const { return WasList; }
186 bool isUndef() const { return LocNoCount == 0 || containsLocNo(UndefLocNo); }
187
188 DbgVariableValue decrementLocNosAfterPivot(unsigned Pivot) const {
189 SmallVector<unsigned, 4> NewLocNos;
190 for (unsigned LocNo : loc_nos())
191 NewLocNos.push_back(LocNo != UndefLocNo && LocNo > Pivot ? LocNo - 1
192 : LocNo);
193 return DbgVariableValue(NewLocNos, WasIndirect, WasList, *Expression);
194 }
195
196 DbgVariableValue remapLocNos(ArrayRef<unsigned> LocNoMap) const {
197 SmallVector<unsigned> NewLocNos;
198 for (unsigned LocNo : loc_nos())
199 // Undef values don't exist in locations (and thus not in LocNoMap
200 // either) so skip over them. See getLocationNo().
201 NewLocNos.push_back(LocNo == UndefLocNo ? UndefLocNo : LocNoMap[LocNo]);
202 return DbgVariableValue(NewLocNos, WasIndirect, WasList, *Expression);
203 }
204
205 DbgVariableValue changeLocNo(unsigned OldLocNo, unsigned NewLocNo) const {
206 SmallVector<unsigned> NewLocNos;
207 NewLocNos.assign(loc_nos_begin(), loc_nos_end());
208 auto OldLocIt = find(NewLocNos, OldLocNo);
209 assert(OldLocIt != NewLocNos.end() && "Old location must be present.");
210 *OldLocIt = NewLocNo;
211 return DbgVariableValue(NewLocNos, WasIndirect, WasList, *Expression);
212 }
213
214 bool hasLocNoGreaterThan(unsigned LocNo) const {
215 return any_of(loc_nos(),
216 [LocNo](unsigned ThisLocNo) { return ThisLocNo > LocNo; });
217 }
218
219 void printLocNos(llvm::raw_ostream &OS) const {
220 for (const unsigned &Loc : loc_nos())
221 OS << (&Loc == loc_nos_begin() ? " " : ", ") << Loc;
222 }
223
224 friend inline bool operator==(const DbgVariableValue &LHS,
225 const DbgVariableValue &RHS) {
226 if (std::tie(LHS.LocNoCount, LHS.WasIndirect, LHS.WasList,
227 LHS.Expression) !=
228 std::tie(RHS.LocNoCount, RHS.WasIndirect, RHS.WasList, RHS.Expression))
229 return false;
230 return std::equal(LHS.loc_nos_begin(), LHS.loc_nos_end(),
231 RHS.loc_nos_begin());
232 }
233
234 friend inline bool operator!=(const DbgVariableValue &LHS,
235 const DbgVariableValue &RHS) {
236 return !(LHS == RHS);
237 }
238
239 unsigned *loc_nos_begin() { return LocNos.get(); }
240 const unsigned *loc_nos_begin() const { return LocNos.get(); }
241 unsigned *loc_nos_end() { return LocNos.get() + LocNoCount; }
242 const unsigned *loc_nos_end() const { return LocNos.get() + LocNoCount; }
243 ArrayRef<unsigned> loc_nos() const {
244 return ArrayRef<unsigned>(LocNos.get(), LocNoCount);
245 }
246
247private:
248 // IntervalMap requires the value object to be very small, to the extent
249 // that we do not have enough room for an std::vector. Using a C-style array
250 // (with a unique_ptr wrapper for convenience) allows us to optimize for this
251 // specific case by packing the array size into only 6 bits (it is highly
252 // unlikely that any debug value will need 64+ locations).
253 std::unique_ptr<unsigned[]> LocNos;
254 uint8_t LocNoCount : 6;
255 bool WasIndirect : 1;
256 bool WasList : 1;
257 const DIExpression *Expression = nullptr;
258};
259} // namespace
260
261/// Map of where a user value is live to that value.
263
264/// Map of stack slot offsets for spilled locations.
265/// Non-spilled locations are not added to the map.
267
268/// Caches used by findInsertLocation so that repeated insertions at the same
269/// position don't rescan the debug instructions already inserted there.
271 /// Per block, the last PHI/Label/Debug instruction found by
272 /// MachineBasicBlock::SkipPHIsLabelsAndDebug, used as the starting position
273 /// for the next search at the start of that block.
275 /// Per instruction, the last debug instruction found after it, used as the
276 /// starting position for the next search after that instruction.
278
279 void clear() {
280 BlockStart.clear();
281 AfterInstr.clear();
282 }
283};
284
285namespace {
286
287/// A user value is a part of a debug info user variable.
288///
289/// A DBG_VALUE instruction notes that (a sub-register of) a virtual register
290/// holds part of a user variable. The part is identified by a byte offset.
291///
292/// UserValues are grouped into equivalence classes for easier searching. Two
293/// user values are related if they are held by the same virtual register. The
294/// equivalence class is the transitive closure of that relation.
295class UserValue {
296 using LDVImpl = LiveDebugVariables::LDVImpl;
297
298 const DILocalVariable *Variable; ///< The debug info variable we are part of.
299 /// The part of the variable we describe.
300 const std::optional<DIExpression::FragmentInfo> Fragment;
301 DebugLoc dl; ///< The debug location for the variable. This is
302 ///< used by dwarf writer to find lexical scope.
303 UserValue *leader; ///< Equivalence class leader.
304 UserValue *next = nullptr; ///< Next value in equivalence class, or null.
305
306 /// Numbered locations referenced by locmap.
307 SmallVector<MachineOperand, 4> locations;
308
309 /// Map of slot indices where this value is live.
310 LocMap locInts;
311
312 /// Set of interval start indexes that have been trimmed to the
313 /// lexical scope.
314 SmallSet<SlotIndex, 2> trimmedDefs;
315
316 /// Insert a DBG_VALUE into MBB at Idx for DbgValue.
317 void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
318 SlotIndex StopIdx, DbgVariableValue DbgValue,
319 ArrayRef<bool> LocSpills,
320 ArrayRef<unsigned> SpillOffsets, LiveIntervals &LIS,
321 const TargetInstrInfo &TII,
322 const TargetRegisterInfo &TRI, InsertLocCache &Cache);
323
324 /// Replace OldLocNo ranges with NewRegs ranges where NewRegs
325 /// is live. Returns true if any changes were made.
326 bool splitLocation(unsigned OldLocNo, ArrayRef<Register> NewRegs,
327 LiveIntervals &LIS);
328
329public:
330 /// Create a new UserValue.
331 UserValue(const DILocalVariable *var,
332 std::optional<DIExpression::FragmentInfo> Fragment, DebugLoc L,
333 LocMap::Allocator &alloc)
334 : Variable(var), Fragment(Fragment), dl(std::move(L)), leader(this),
335 locInts(alloc) {}
336
337 /// Get the leader of this value's equivalence class.
338 UserValue *getLeader() {
339 UserValue *l = leader;
340 while (l != l->leader)
341 l = l->leader;
342 return leader = l;
343 }
344
345 /// Return the next UserValue in the equivalence class.
346 UserValue *getNext() const { return next; }
347
348 /// Merge equivalence classes.
349 static UserValue *merge(UserValue *L1, UserValue *L2) {
350 L2 = L2->getLeader();
351 if (!L1)
352 return L2;
353 L1 = L1->getLeader();
354 if (L1 == L2)
355 return L1;
356 // Splice L2 before L1's members.
357 UserValue *End = L2;
358 while (End->next) {
359 End->leader = L1;
360 End = End->next;
361 }
362 End->leader = L1;
363 End->next = L1->next;
364 L1->next = L2;
365 return L1;
366 }
367
368 /// Return the location number that matches Loc.
369 ///
370 /// For undef values we always return location number UndefLocNo without
371 /// inserting anything in locations. Since locations is a vector and the
372 /// location number is the position in the vector and UndefLocNo is ~0,
373 /// we would need a very big vector to put the value at the right position.
374 unsigned getLocationNo(const MachineOperand &LocMO) {
375 if (LocMO.isReg()) {
376 if (LocMO.getReg() == 0)
377 return UndefLocNo;
378 // For register locations we dont care about use/def and other flags.
379 for (unsigned i = 0, e = locations.size(); i != e; ++i)
380 if (locations[i].isReg() &&
381 locations[i].getReg() == LocMO.getReg() &&
382 locations[i].getSubReg() == LocMO.getSubReg())
383 return i;
384 } else
385 for (unsigned i = 0, e = locations.size(); i != e; ++i)
386 if (LocMO.isIdenticalTo(locations[i]))
387 return i;
388 locations.push_back(LocMO);
389 // We are storing a MachineOperand outside a MachineInstr.
390 locations.back().clearParent();
391 // Don't store def operands.
392 if (locations.back().isReg()) {
393 if (locations.back().isDef())
394 locations.back().setIsDead(false);
395 locations.back().setIsUse();
396 }
397 return locations.size() - 1;
398 }
399
400 /// Remove (recycle) a location number. If \p LocNo still is used by the
401 /// locInts nothing is done.
402 void removeLocationIfUnused(unsigned LocNo) {
403 // Bail out if LocNo still is used.
404 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
405 const DbgVariableValue &DbgValue = I.value();
406 if (DbgValue.containsLocNo(LocNo))
407 return;
408 }
409 // Remove the entry in the locations vector, and adjust all references to
410 // location numbers above the removed entry.
411 locations.erase(locations.begin() + LocNo);
412 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
413 const DbgVariableValue &DbgValue = I.value();
414 if (DbgValue.hasLocNoGreaterThan(LocNo))
415 I.setValueUnchecked(DbgValue.decrementLocNosAfterPivot(LocNo));
416 }
417 }
418
419 /// Ensure that all virtual register locations are mapped.
420 void mapVirtRegs(LDVImpl *LDV);
421
422 /// Add a definition point to this user value.
423 void addDef(SlotIndex Idx, ArrayRef<MachineOperand> LocMOs, bool IsIndirect,
424 bool IsList, const DIExpression &Expr) {
425 SmallVector<unsigned> Locs;
426 for (const MachineOperand &Op : LocMOs)
427 Locs.push_back(getLocationNo(Op));
428 DbgVariableValue DbgValue(Locs, IsIndirect, IsList, Expr);
429 // Add a singular (Idx,Idx) -> value mapping.
430 LocMap::iterator I = locInts.find(Idx);
431 if (!I.valid() || I.start() != Idx)
432 I.insert(Idx, Idx.getNextSlot(), std::move(DbgValue));
433 else
434 // A later DBG_VALUE at the same SlotIndex overrides the old location.
435 I.setValue(std::move(DbgValue));
436 }
437
438 /// Extend the current definition as far as possible down.
439 ///
440 /// Stop when meeting an existing def or when leaving the live
441 /// range of VNI. End points where VNI is no longer live are added to Kills.
442 ///
443 /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a
444 /// data-flow analysis to propagate them beyond basic block boundaries.
445 ///
446 /// \param Idx Starting point for the definition.
447 /// \param DbgValue value to propagate.
448 /// \param LiveIntervalInfo For each location number key in this map,
449 /// restricts liveness to where the LiveRange has the value equal to the\
450 /// VNInfo.
451 /// \param [out] Kills Append end points of VNI's live range to Kills.
452 /// \param LIS Live intervals analysis.
453 void
454 extendDef(SlotIndex Idx, DbgVariableValue DbgValue,
455 SmallDenseMap<unsigned, std::pair<LiveRange *, const VNInfo *>>
456 &LiveIntervalInfo,
457 std::optional<std::pair<SlotIndex, SmallVector<unsigned>>> &Kills,
458 LiveIntervals &LIS);
459
460 /// The value in LI may be copies to other registers. Determine if
461 /// any of the copies are available at the kill points, and add defs if
462 /// possible.
463 ///
464 /// \param DbgValue Location number of LI->reg, and DIExpression.
465 /// \param LocIntervals Scan for copies of the value for each location in the
466 /// corresponding LiveInterval->reg.
467 /// \param KilledAt The point where the range of DbgValue could be extended.
468 /// \param [in,out] NewDefs Append (Idx, DbgValue) of inserted defs here.
469 void addDefsFromCopies(
470 DbgVariableValue DbgValue,
471 SmallVectorImpl<std::pair<unsigned, LiveInterval *>> &LocIntervals,
472 SlotIndex KilledAt,
473 SmallVectorImpl<std::pair<SlotIndex, DbgVariableValue>> &NewDefs,
474 MachineRegisterInfo &MRI, LiveIntervals &LIS);
475
476 /// Compute the live intervals of all locations after collecting all their
477 /// def points.
478 void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
479 LiveIntervals &LIS, LexicalScopes &LS);
480
481 /// Replace OldReg ranges with NewRegs ranges where NewRegs is
482 /// live. Returns true if any changes were made.
483 bool splitRegister(Register OldReg, ArrayRef<Register> NewRegs,
484 LiveIntervals &LIS);
485
486 /// Replace the stale indexes in locInts and trimmedDefs.
487 void canonicalizeIndexes(const SlotIndexes &SI);
488
489 /// Rewrite virtual register locations according to the provided virtual
490 /// register map. Record the stack slot offsets for the locations that
491 /// were spilled.
492 void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
493 const TargetInstrInfo &TII,
494 const TargetRegisterInfo &TRI,
495 SpillOffsetMap &SpillOffsets);
496
497 /// Recreate DBG_VALUE instruction from data structures.
498 void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
499 const TargetInstrInfo &TII,
500 const TargetRegisterInfo &TRI,
501 const SpillOffsetMap &SpillOffsets,
502 InsertLocCache &Cache);
503
504 /// Return DebugLoc of this UserValue.
505 const DebugLoc &getDebugLoc() { return dl; }
506
507 void print(raw_ostream &, const TargetRegisterInfo *);
508};
509
510/// A user label is a part of a debug info user label.
511class UserLabel {
512 const DILabel *Label; ///< The debug info label we are part of.
513 DebugLoc dl; ///< The debug location for the label. This is
514 ///< used by dwarf writer to find lexical scope.
515 SlotIndex loc; ///< Slot used by the debug label.
516
517 /// Insert a DBG_LABEL into MBB at Idx.
518 void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
519 LiveIntervals &LIS, const TargetInstrInfo &TII,
520 InsertLocCache &Cache);
521
522public:
523 /// Create a new UserLabel.
524 UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx)
525 : Label(label), dl(std::move(L)), loc(Idx) {}
526
527 /// Does this UserLabel match the parameters?
528 bool matches(const DILabel *L, const DILocation *IA,
529 const SlotIndex Index) const {
530 return Label == L && dl->getInlinedAt() == IA && loc == Index;
531 }
532
533 /// Recreate DBG_LABEL instruction from data structures.
534 void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII,
535 InsertLocCache &Cache);
536
537 /// Replace loc if it is stale, and report whether it was.
538 bool canonicalizeIndex(const SlotIndexes &SI) {
539 bool WasStale = SI.isStaleIndex(loc);
540 loc = SI.canonicalizeIndex(loc);
541 assert(!SI.isStaleIndex(loc) &&
542 "Canonicalized label still refers to an erased instruction");
543 return WasStale;
544 }
545
546 /// Return DebugLoc of this UserLabel.
547 const DebugLoc &getDebugLoc() { return dl; }
548
549 void print(raw_ostream &, const TargetRegisterInfo *);
550};
551
552} // end anonymous namespace
553
554namespace llvm {
555
557 LocMap::Allocator allocator;
558 MachineFunction *MF = nullptr;
559 LiveIntervals *LIS;
560 const TargetRegisterInfo *TRI;
561
562 /// Position and VReg of a PHI instruction during register allocation.
563 struct PHIValPos {
564 SlotIndex SI; /// Slot where this PHI occurs.
565 Register Reg; /// VReg this PHI occurs in.
566 unsigned SubReg; /// Qualifiying subregister for Reg.
567 };
568
569 /// Map from debug instruction number to PHI position during allocation.
570 std::map<unsigned, PHIValPos> PHIValToPos;
571 /// Index of, for each VReg, which debug instruction numbers and corresponding
572 /// PHIs are sensitive to splitting. Each VReg may have multiple PHI defs,
573 /// at different positions.
575
576 /// Record for any debug instructions unlinked from their blocks during
577 /// regalloc. Stores the instr and it's location, so that they can be
578 /// re-inserted after regalloc is over.
579 struct InstrPos {
580 MachineInstr *MI; ///< Debug instruction, unlinked from it's block.
581 SlotIndex Idx; ///< Slot position where MI should be re-inserted.
582 MachineBasicBlock *MBB; ///< Block that MI was in.
583 };
584
585 /// Collection of stored debug instructions, preserved until after regalloc.
586 SmallVector<InstrPos, 32> StashedDebugInstrs;
587
588 /// Whether emitDebugValues is called.
589 bool EmitDone = false;
590
591 /// Whether the machine function is modified during the pass.
592 bool ModifiedMF = false;
593
594 /// All allocated UserValue instances.
596
597 /// All allocated UserLabel instances.
599
600 /// Map virtual register to eq class leader.
602 VRMap virtRegToEqClass;
603
604 /// Map to find existing UserValue instances.
606 UVMap userVarMap;
607
608 /// Find or create a UserValue.
609 UserValue *getUserValue(const DILocalVariable *Var,
610 std::optional<DIExpression::FragmentInfo> Fragment,
611 const DebugLoc &DL);
612
613 /// Find the EC leader for VirtReg or null.
614 UserValue *lookupVirtReg(Register VirtReg);
615
616 /// Add DBG_VALUE instruction to our maps.
617 ///
618 /// \param MI DBG_VALUE instruction
619 /// \param Idx Last valid SLotIndex before instruction.
620 ///
621 /// \returns True if the DBG_VALUE instruction should be deleted.
622 bool handleDebugValue(MachineInstr &MI, SlotIndex Idx);
623
624 /// Track variable location debug instructions while using the instruction
625 /// referencing implementation. Such debug instructions do not need to be
626 /// updated during regalloc because they identify instructions rather than
627 /// register locations. However, they needs to be removed from the
628 /// MachineFunction during regalloc, then re-inserted later, to avoid
629 /// disrupting the allocator.
630 ///
631 /// \param MI Any DBG_VALUE / DBG_INSTR_REF / DBG_PHI instruction
632 /// \param Idx Last valid SlotIndex before instruction
633 ///
634 /// \returns Iterator to continue processing from after unlinking.
636
637 /// Add DBG_LABEL instruction to UserLabel.
638 ///
639 /// \param MI DBG_LABEL instruction
640 /// \param Idx Last valid SlotIndex before instruction.
641 ///
642 /// \returns True if the DBG_LABEL instruction should be deleted.
643 bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx);
644
645 /// Collect and erase all DBG_VALUE instructions, adding a UserValue def
646 /// for each instruction.
647 ///
648 /// \param mf MachineFunction to be scanned.
649 /// \param InstrRef Whether to operate in instruction referencing mode. If
650 /// true, most of LiveDebugVariables doesn't run.
651 ///
652 /// \returns True if any debug values were found.
653 bool collectDebugValues(MachineFunction &mf, bool InstrRef);
654
655 /// Compute the live intervals of all user values after collecting all
656 /// their def points.
657 void computeIntervals();
658
659public:
660 LDVImpl(LiveIntervals *LIS) : LIS(LIS) {}
661
662 bool runOnMachineFunction(MachineFunction &mf, bool InstrRef);
663
664 /// Release all memory.
665 void clear() {
666 MF = nullptr;
667 PHIValToPos.clear();
668 RegToPHIIdx.clear();
669 StashedDebugInstrs.clear();
670 userValues.clear();
671 userLabels.clear();
672 virtRegToEqClass.clear();
673 userVarMap.clear();
674 // Make sure we call emitDebugValues if the machine function was modified.
675 assert((!ModifiedMF || EmitDone) &&
676 "Dbg values are not emitted in LDV");
677 EmitDone = false;
678 ModifiedMF = false;
679 }
680
681 /// Map virtual register to an equivalence class.
682 void mapVirtReg(Register VirtReg, UserValue *EC);
683
684 /// Replace any PHI referring to OldReg with its corresponding NewReg, if
685 /// present.
686 void splitPHIRegister(Register OldReg, ArrayRef<Register> NewRegs);
687
688 /// Replace all references to OldReg with NewRegs.
689 void splitRegister(Register OldReg, ArrayRef<Register> NewRegs);
690
691 /// Replace every stale index held by this analysis.
693
694 /// Recreate DBG_VALUE instruction from data structures.
695 void emitDebugValues(VirtRegMap *VRM);
696
697 void print(raw_ostream&);
698};
699
700/// Implementation of the LiveDebugVariables pass.
701
705
706} // namespace llvm
707
708static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS,
709 const LLVMContext &Ctx) {
710 if (!DL)
711 return;
712
713 auto *Scope = cast<DIScope>(DL.getScope());
714 // Omit the directory, because it's likely to be long and uninteresting.
715 CommentOS << Scope->getFilename();
716 CommentOS << ':' << DL.getLine();
717 if (DL.getCol() != 0)
718 CommentOS << ':' << DL.getCol();
719
720 DebugLoc InlinedAtDL = DL.getInlinedAt();
721 if (!InlinedAtDL)
722 return;
723
724 CommentOS << " @[ ";
725 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
726 CommentOS << " ]";
727}
728
729static void printExtendedName(raw_ostream &OS, const DINode *Node,
730 const DILocation *DL) {
731 const LLVMContext &Ctx = Node->getContext();
732 StringRef Res;
733 unsigned Line = 0;
734 if (const auto *V = dyn_cast<const DILocalVariable>(Node)) {
735 Res = V->getName();
736 Line = V->getLine();
737 } else if (const auto *L = dyn_cast<const DILabel>(Node)) {
738 Res = L->getName();
739 Line = L->getLine();
740 }
741
742 if (!Res.empty())
743 OS << Res << "," << Line;
744 auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr;
745 if (InlinedAt) {
746 if (DebugLoc InlinedAtDL = InlinedAt) {
747 OS << " @[";
748 printDebugLoc(InlinedAtDL, OS, Ctx);
749 OS << "]";
750 }
751 }
752}
753
754void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
755 OS << "!\"";
756 printExtendedName(OS, Variable, dl);
757
758 OS << "\"\t";
759 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
760 OS << " [" << I.start() << ';' << I.stop() << "):";
761 if (I.value().isUndef())
762 OS << " undef";
763 else {
764 I.value().printLocNos(OS);
765 if (I.value().getWasIndirect())
766 OS << " ind";
767 else if (I.value().getWasList())
768 OS << " list";
769 }
770 }
771 for (unsigned i = 0, e = locations.size(); i != e; ++i) {
772 OS << " Loc" << i << '=';
773 locations[i].print(OS, TRI);
774 }
775 OS << '\n';
776}
777
778void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) {
779 OS << "!\"";
780 printExtendedName(OS, Label, dl);
781
782 OS << "\"\t";
783 OS << loc;
784 OS << '\n';
785}
786
788 OS << "********** DEBUG VARIABLES **********\n";
789 for (auto &userValue : userValues)
790 userValue->print(OS, TRI);
791 OS << "********** DEBUG LABELS **********\n";
792 for (auto &userLabel : userLabels)
793 userLabel->print(OS, TRI);
794}
795
796void UserValue::mapVirtRegs(LiveDebugVariables::LDVImpl *LDV) {
797 for (const MachineOperand &MO : locations)
798 if (MO.isReg() && MO.getReg().isVirtual())
799 LDV->mapVirtReg(MO.getReg(), this);
800}
801
802UserValue *LiveDebugVariables::LDVImpl::getUserValue(
803 const DILocalVariable *Var,
804 std::optional<DIExpression::FragmentInfo> Fragment, const DebugLoc &DL) {
805 // FIXME: Handle partially overlapping fragments. See
806 // https://reviews.llvm.org/D70121#1849741.
807 DebugVariable ID(Var, Fragment, DL->getInlinedAt());
808 UserValue *&UV = userVarMap[ID];
809 if (!UV) {
810 userValues.push_back(
811 std::make_unique<UserValue>(Var, Fragment, DL, allocator));
812 UV = userValues.back().get();
813 }
814 return UV;
815}
816
818 assert(VirtReg.isVirtual() && "Only map VirtRegs");
819 UserValue *&Leader = virtRegToEqClass[VirtReg];
820 Leader = UserValue::merge(Leader, EC);
821}
822
823UserValue *LiveDebugVariables::LDVImpl::lookupVirtReg(Register VirtReg) {
824 if (UserValue *UV = virtRegToEqClass.lookup(VirtReg))
825 return UV->getLeader();
826 return nullptr;
827}
828
829bool LiveDebugVariables::LDVImpl::handleDebugValue(MachineInstr &MI,
830 SlotIndex Idx) {
831 // DBG_VALUE loc, offset, variable, expr
832 // DBG_VALUE_LIST variable, expr, locs...
833 if (!MI.isDebugValue()) {
834 LLVM_DEBUG(dbgs() << "Can't handle non-DBG_VALUE*: " << MI);
835 return false;
836 }
837 if (!MI.getDebugVariableOp().isMetadata()) {
838 LLVM_DEBUG(dbgs() << "Can't handle DBG_VALUE* with invalid variable: "
839 << MI);
840 return false;
841 }
842 if (MI.isNonListDebugValue() &&
843 (MI.getNumOperands() != 4 ||
844 !(MI.getDebugOffset().isImm() || MI.getDebugOffset().isReg()))) {
845 LLVM_DEBUG(dbgs() << "Can't handle malformed DBG_VALUE: " << MI);
846 return false;
847 }
848
849 // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual
850 // register that hasn't been defined yet. If we do not remove those here, then
851 // the re-insertion of the DBG_VALUE instruction after register allocation
852 // will be incorrect.
853 bool Discard = false;
854 for (const MachineOperand &Op : MI.debug_operands()) {
855 if (Op.isReg() && Op.getReg().isVirtual()) {
856 const Register Reg = Op.getReg();
857 if (!LIS->hasInterval(Reg)) {
858 // The DBG_VALUE is described by a virtual register that does not have a
859 // live interval. Discard the DBG_VALUE.
860 Discard = true;
861 LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx
862 << " " << MI);
863 } else {
864 // The DBG_VALUE is only valid if either Reg is live out from Idx, or
865 // Reg is defined dead at Idx (where Idx is the slot index for the
866 // instruction preceding the DBG_VALUE).
867 const LiveInterval &LI = LIS->getInterval(Reg);
868 LiveQueryResult LRQ = LI.Query(Idx);
869 if (!LRQ.valueOutOrDead()) {
870 // We have found a DBG_VALUE with the value in a virtual register that
871 // is not live. Discard the DBG_VALUE.
872 Discard = true;
873 LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx
874 << " " << MI);
875 }
876 }
877 }
878 }
879
880 // Get or create the UserValue for (variable,offset) here.
881 bool IsIndirect = MI.isDebugOffsetImm();
882 if (IsIndirect)
883 assert(MI.getDebugOffset().getImm() == 0 &&
884 "DBG_VALUE with nonzero offset");
885 bool IsList = MI.isDebugValueList();
886 const DILocalVariable *Var = MI.getDebugVariable();
887 const DIExpression *Expr = MI.getDebugExpression();
888 UserValue *UV = getUserValue(Var, Expr->getFragmentInfo(), MI.getDebugLoc());
889 if (!Discard)
890 UV->addDef(Idx,
891 ArrayRef<MachineOperand>(MI.debug_operands().begin(),
892 MI.debug_operands().end()),
893 IsIndirect, IsList, *Expr);
894 else {
895 MachineOperand MO = MachineOperand::CreateReg(0U, false);
896 MO.setIsDebug();
897 // We should still pass a list the same size as MI.debug_operands() even if
898 // all MOs are undef, so that DbgVariableValue can correctly adjust the
899 // expression while removing the duplicated undefs.
900 SmallVector<MachineOperand, 4> UndefMOs(MI.getNumDebugOperands(), MO);
901 UV->addDef(Idx, UndefMOs, false, IsList, *Expr);
902 }
903 return true;
904}
905
907LiveDebugVariables::LDVImpl::handleDebugInstr(MachineInstr &MI, SlotIndex Idx) {
908 assert(MI.isDebugValueLike() || MI.isDebugPHI());
909
910 // In instruction referencing mode, there should be no DBG_VALUE instructions
911 // that refer to virtual registers. They might still refer to constants.
912 if (MI.isDebugValueLike())
913 assert(none_of(MI.debug_operands(),
914 [](const MachineOperand &MO) {
915 return MO.isReg() && MO.getReg().isVirtual();
916 }) &&
917 "MIs should not refer to Virtual Registers in InstrRef mode.");
918
919 // Unlink the instruction, store it in the debug instructions collection.
920 auto NextInst = std::next(MI.getIterator());
921 auto *MBB = MI.getParent();
922 MI.removeFromParent();
923 StashedDebugInstrs.push_back({&MI, Idx, MBB});
924 return NextInst;
925}
926
927bool LiveDebugVariables::LDVImpl::handleDebugLabel(MachineInstr &MI,
928 SlotIndex Idx) {
929 // DBG_LABEL label
930 if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) {
931 LLVM_DEBUG(dbgs() << "Can't handle " << MI);
932 return false;
933 }
934
935 // Get or create the UserLabel for label here.
936 const DILabel *Label = MI.getDebugLabel();
937 const DebugLoc &DL = MI.getDebugLoc();
938 bool Found = false;
939 for (auto const &L : userLabels) {
940 if (L->matches(Label, DL->getInlinedAt(), Idx)) {
941 Found = true;
942 break;
943 }
944 }
945 if (!Found)
946 userLabels.push_back(std::make_unique<UserLabel>(Label, DL, Idx));
947
948 return true;
949}
950
951bool LiveDebugVariables::LDVImpl::collectDebugValues(MachineFunction &mf,
952 bool InstrRef) {
953 bool Changed = false;
954 for (MachineBasicBlock &MBB : mf) {
955 for (MachineBasicBlock::iterator MBBI = MBB.begin(), MBBE = MBB.end();
956 MBBI != MBBE;) {
957 // Use the first debug instruction in the sequence to get a SlotIndex
958 // for following consecutive debug instructions.
959 if (!MBBI->isDebugOrPseudoInstr()) {
960 ++MBBI;
961 continue;
962 }
963 // Debug instructions has no slot index. Use the previous
964 // non-debug instruction's SlotIndex as its SlotIndex.
965 SlotIndex Idx =
966 MBBI == MBB.begin()
967 ? LIS->getMBBStartIdx(&MBB)
968 : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot();
969 // Handle consecutive debug instructions with the same slot index.
970 do {
971 // In instruction referencing mode, pass each instr to handleDebugInstr
972 // to be unlinked. Ignore DBG_VALUE_LISTs -- they refer to vregs, and
973 // need to go through the normal live interval splitting process.
974 if (InstrRef && (MBBI->isNonListDebugValue() || MBBI->isDebugPHI() ||
975 MBBI->isDebugRef())) {
976 MBBI = handleDebugInstr(*MBBI, Idx);
977 Changed = true;
978 // In normal debug mode, use the dedicated DBG_VALUE / DBG_LABEL handler
979 // to track things through register allocation, and erase the instr.
980 } else if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) ||
981 (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) {
982 MBBI = MBB.erase(MBBI);
983 Changed = true;
984 } else
985 ++MBBI;
986 } while (MBBI != MBBE && MBBI->isDebugOrPseudoInstr());
987 }
988 }
989 return Changed;
990}
991
992void UserValue::extendDef(
993 SlotIndex Idx, DbgVariableValue DbgValue,
994 SmallDenseMap<unsigned, std::pair<LiveRange *, const VNInfo *>>
995 &LiveIntervalInfo,
996 std::optional<std::pair<SlotIndex, SmallVector<unsigned>>> &Kills,
997 LiveIntervals &LIS) {
998 SlotIndex Start = Idx;
999 MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start);
1000 SlotIndex Stop = LIS.getMBBEndIdx(MBB);
1001 LocMap::iterator I = locInts.find(Start);
1002
1003 // Limit to the intersection of the VNIs' live ranges.
1004 for (auto &LII : LiveIntervalInfo) {
1005 LiveRange *LR = LII.second.first;
1006 assert(LR && LII.second.second && "Missing range info for Idx.");
1007 LiveInterval::Segment *Segment = LR->getSegmentContaining(Start);
1008 assert(Segment && Segment->valno == LII.second.second &&
1009 "Invalid VNInfo for Idx given?");
1010 if (Segment->end < Stop) {
1011 Stop = Segment->end;
1012 Kills = {Stop, {LII.first}};
1013 } else if (Segment->end == Stop && Kills) {
1014 // If multiple locations end at the same place, track all of them in
1015 // Kills.
1016 Kills->second.push_back(LII.first);
1017 }
1018 }
1019
1020 // There could already be a short def at Start.
1021 if (I.valid() && I.start() <= Start) {
1022 // Stop when meeting a different location or an already extended interval.
1023 Start = Start.getNextSlot();
1024 if (I.value() != DbgValue || I.stop() != Start) {
1025 // Clear `Kills`, as we have a new def available.
1026 Kills = std::nullopt;
1027 return;
1028 }
1029 // This is a one-slot placeholder. Just skip it.
1030 ++I;
1031 }
1032
1033 // Limited by the next def.
1034 if (I.valid() && I.start() < Stop) {
1035 Stop = I.start();
1036 // Clear `Kills`, as we have a new def available.
1037 Kills = std::nullopt;
1038 }
1039
1040 if (Start < Stop) {
1041 DbgVariableValue ExtDbgValue(DbgValue);
1042 I.insert(Start, Stop, std::move(ExtDbgValue));
1043 }
1044}
1045
1046void UserValue::addDefsFromCopies(
1047 DbgVariableValue DbgValue,
1048 SmallVectorImpl<std::pair<unsigned, LiveInterval *>> &LocIntervals,
1049 SlotIndex KilledAt,
1050 SmallVectorImpl<std::pair<SlotIndex, DbgVariableValue>> &NewDefs,
1051 MachineRegisterInfo &MRI, LiveIntervals &LIS) {
1052 // Don't track copies from physregs, there are too many uses.
1053 if (any_of(LocIntervals,
1054 [](auto LocI) { return !LocI.second->reg().isVirtual(); }))
1055 return;
1056
1057 // Collect all the (vreg, valno) pairs that are copies of LI.
1058 SmallDenseMap<unsigned,
1060 CopyValues;
1061 for (auto &LocInterval : LocIntervals) {
1062 unsigned LocNo = LocInterval.first;
1063 LiveInterval *LI = LocInterval.second;
1064 for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg())) {
1065 MachineInstr *MI = MO.getParent();
1066 // Copies of the full value.
1067 if (MO.getSubReg() || !MI->isCopy())
1068 continue;
1069 Register DstReg = MI->getOperand(0).getReg();
1070
1071 // Don't follow copies to physregs. These are usually setting up call
1072 // arguments, and the argument registers are always call clobbered. We are
1073 // better off in the source register which could be a callee-saved
1074 // register, or it could be spilled.
1075 if (!DstReg.isVirtual())
1076 continue;
1077
1078 // Is the value extended to reach this copy? If not, another def may be
1079 // blocking it, or we are looking at a wrong value of LI.
1080 SlotIndex Idx = LIS.getInstructionIndex(*MI);
1081 LocMap::iterator I = locInts.find(Idx.getRegSlot(true));
1082 if (!I.valid() || I.value() != DbgValue)
1083 continue;
1084
1085 if (!LIS.hasInterval(DstReg))
1086 continue;
1087 LiveInterval *DstLI = &LIS.getInterval(DstReg);
1088 const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot());
1089 assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value");
1090 CopyValues[LocNo].push_back(std::make_pair(DstLI, DstVNI));
1091 }
1092 }
1093
1094 if (CopyValues.empty())
1095 return;
1096
1097#if !defined(NDEBUG)
1098 for (auto &LocInterval : LocIntervals)
1099 LLVM_DEBUG(dbgs() << "Got " << CopyValues[LocInterval.first].size()
1100 << " copies of " << *LocInterval.second << '\n');
1101#endif
1102
1103 // Try to add defs of the copied values for the kill point. Check that there
1104 // isn't already a def at Idx.
1105 LocMap::iterator I = locInts.find(KilledAt);
1106 if (I.valid() && I.start() <= KilledAt)
1107 return;
1108 DbgVariableValue NewValue(DbgValue);
1109 for (auto &LocInterval : LocIntervals) {
1110 unsigned LocNo = LocInterval.first;
1111 bool FoundCopy = false;
1112 for (auto &LIAndVNI : CopyValues[LocNo]) {
1113 LiveInterval *DstLI = LIAndVNI.first;
1114 const VNInfo *DstVNI = LIAndVNI.second;
1115 if (DstLI->getVNInfoAt(KilledAt) != DstVNI)
1116 continue;
1117 LLVM_DEBUG(dbgs() << "Kill at " << KilledAt << " covered by valno #"
1118 << DstVNI->id << " in " << *DstLI << '\n');
1119 MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def);
1120 assert(CopyMI && CopyMI->isCopy() && "Bad copy value");
1121 unsigned NewLocNo = getLocationNo(CopyMI->getOperand(0));
1122 NewValue = NewValue.changeLocNo(LocNo, NewLocNo);
1123 FoundCopy = true;
1124 break;
1125 }
1126 // If there are any killed locations we can't find a copy for, we can't
1127 // extend the variable value.
1128 if (!FoundCopy)
1129 return;
1130 }
1131 I.insert(KilledAt, KilledAt.getNextSlot(), NewValue);
1132 NewDefs.push_back(std::make_pair(KilledAt, NewValue));
1133}
1134
1135void UserValue::computeIntervals(MachineRegisterInfo &MRI,
1136 const TargetRegisterInfo &TRI,
1137 LiveIntervals &LIS, LexicalScopes &LS) {
1139
1140 // Collect all defs to be extended (Skipping undefs).
1141 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
1142 if (!I.value().isUndef())
1143 Defs.push_back(std::make_pair(I.start(), I.value()));
1144
1145 // Extend all defs, and possibly add new ones along the way.
1146 for (unsigned i = 0; i != Defs.size(); ++i) {
1147 SlotIndex Idx = Defs[i].first;
1148 DbgVariableValue DbgValue = Defs[i].second;
1149 SmallDenseMap<unsigned, std::pair<LiveRange *, const VNInfo *>> LIs;
1150 bool ShouldExtendDef = false;
1151 for (unsigned LocNo : DbgValue.loc_nos()) {
1152 const MachineOperand &LocMO = locations[LocNo];
1153 if (!LocMO.isReg() || !LocMO.getReg().isVirtual()) {
1154 ShouldExtendDef |= !LocMO.isReg();
1155 continue;
1156 }
1157 ShouldExtendDef = true;
1158 LiveInterval *LI = nullptr;
1159 const VNInfo *VNI = nullptr;
1160 if (LIS.hasInterval(LocMO.getReg())) {
1161 LI = &LIS.getInterval(LocMO.getReg());
1162 VNI = LI->getVNInfoAt(Idx);
1163 }
1164 if (LI && VNI)
1165 LIs[LocNo] = {LI, VNI};
1166 }
1167 if (ShouldExtendDef) {
1168 std::optional<std::pair<SlotIndex, SmallVector<unsigned>>> Kills;
1169 extendDef(Idx, DbgValue, LIs, Kills, LIS);
1170
1171 if (Kills) {
1173 bool AnySubreg = false;
1174 for (unsigned LocNo : Kills->second) {
1175 const MachineOperand &LocMO = this->locations[LocNo];
1176 if (LocMO.getSubReg()) {
1177 AnySubreg = true;
1178 break;
1179 }
1180 LiveInterval *LI = &LIS.getInterval(LocMO.getReg());
1181 KilledLocIntervals.push_back({LocNo, LI});
1182 }
1183
1184 // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that
1185 // if the original location for example is %vreg0:sub_hi, and we find a
1186 // full register copy in addDefsFromCopies (at the moment it only
1187 // handles full register copies), then we must add the sub1 sub-register
1188 // index to the new location. However, that is only possible if the new
1189 // virtual register is of the same regclass (or if there is an
1190 // equivalent sub-register in that regclass). For now, simply skip
1191 // handling copies if a sub-register is involved.
1192 if (!AnySubreg)
1193 addDefsFromCopies(DbgValue, KilledLocIntervals, Kills->first, Defs,
1194 MRI, LIS);
1195 }
1196 }
1197
1198 // For physregs, we only mark the start slot idx. DwarfDebug will see it
1199 // as if the DBG_VALUE is valid up until the end of the basic block, or
1200 // the next def of the physical register. So we do not need to extend the
1201 // range. It might actually happen that the DBG_VALUE is the last use of
1202 // the physical register (e.g. if this is an unused input argument to a
1203 // function).
1204 }
1205
1206 // The computed intervals may extend beyond the range of the debug
1207 // location's lexical scope. In this case, splitting of an interval
1208 // can result in an interval outside of the scope being created,
1209 // causing extra unnecessary DBG_VALUEs to be emitted. To prevent
1210 // this, trim the intervals to the lexical scope in the case of inlined
1211 // variables, since heavy inlining may cause production of dramatically big
1212 // number of DBG_VALUEs to be generated.
1213 if (!dl.getInlinedAt())
1214 return;
1215
1216 LexicalScope *Scope = LS.findLexicalScope(dl);
1217 if (!Scope)
1218 return;
1219
1220 SlotIndex PrevEnd;
1221 LocMap::iterator I = locInts.begin();
1222
1223 // Iterate over the lexical scope ranges. Each time round the loop
1224 // we check the intervals for overlap with the end of the previous
1225 // range and the start of the next. The first range is handled as
1226 // a special case where there is no PrevEnd.
1227 for (const InsnRange &Range : Scope->getRanges()) {
1228 SlotIndex RStart = LIS.getInstructionIndex(*Range.first);
1229 SlotIndex REnd = LIS.getInstructionIndex(*Range.second);
1230
1231 // Variable locations at the first instruction of a block should be
1232 // based on the block's SlotIndex, not the first instruction's index.
1233 if (Range.first == Range.first->getParent()->begin())
1234 RStart = LIS.getSlotIndexes()->getIndexBefore(*Range.first);
1235
1236 // At the start of each iteration I has been advanced so that
1237 // I.stop() >= PrevEnd. Check for overlap.
1238 if (PrevEnd && I.start() < PrevEnd) {
1239 SlotIndex IStop = I.stop();
1240 DbgVariableValue DbgValue = I.value();
1241
1242 // Stop overlaps previous end - trim the end of the interval to
1243 // the scope range.
1244 I.setStopUnchecked(PrevEnd);
1245 ++I;
1246
1247 // If the interval also overlaps the start of the "next" (i.e.
1248 // current) range create a new interval for the remainder (which
1249 // may be further trimmed).
1250 if (RStart < IStop)
1251 I.insert(RStart, IStop, DbgValue);
1252 }
1253
1254 // Advance I so that I.stop() >= RStart, and check for overlap.
1255 I.advanceTo(RStart);
1256 if (!I.valid())
1257 return;
1258
1259 if (I.start() < RStart) {
1260 // Interval start overlaps range - trim to the scope range.
1261 I.setStartUnchecked(RStart);
1262 // Remember that this interval was trimmed.
1263 trimmedDefs.insert(RStart);
1264 }
1265
1266 // The end of a lexical scope range is the last instruction in the
1267 // range. To convert to an interval we need the index of the
1268 // instruction after it.
1269 REnd = REnd.getNextIndex();
1270
1271 // Advance I to first interval outside current range.
1272 I.advanceTo(REnd);
1273 if (!I.valid())
1274 return;
1275
1276 PrevEnd = REnd;
1277 }
1278
1279 // Check for overlap with end of final range.
1280 if (PrevEnd && I.start() < PrevEnd)
1281 I.setStopUnchecked(PrevEnd);
1282}
1283
1284void LiveDebugVariables::LDVImpl::computeIntervals() {
1285 LexicalScopes LS;
1286 LS.scanFunction(*MF);
1287
1288 for (const auto &UV : userValues) {
1289 UV->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS);
1290 UV->mapVirtRegs(this);
1291 }
1292}
1293
1295 bool InstrRef) {
1296 clear();
1297 MF = &mf;
1298 TRI = mf.getSubtarget().getRegisterInfo();
1299 LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: "
1300 << mf.getName() << " **********\n");
1301
1302 bool Changed = collectDebugValues(mf, InstrRef);
1303 computeIntervals();
1304 LLVM_DEBUG(print(dbgs()));
1305
1306 // Collect the set of VReg / SlotIndexs where PHIs occur; index the sensitive
1307 // VRegs too, for when we're notified of a range split.
1308 SlotIndexes *Slots = LIS->getSlotIndexes();
1309 for (const auto &PHIIt : MF->DebugPHIPositions) {
1310 const MachineFunction::DebugPHIRegallocPos &Position = PHIIt.second;
1311 MachineBasicBlock *MBB = Position.MBB;
1312 Register Reg = Position.Reg;
1313 unsigned SubReg = Position.SubReg;
1314 SlotIndex SI = Slots->getMBBStartIdx(MBB);
1315 PHIValPos VP = {SI, Reg, SubReg};
1316 PHIValToPos.insert(std::make_pair(PHIIt.first, VP));
1317 RegToPHIIdx[Reg].push_back(PHIIt.first);
1318 }
1319
1320 ModifiedMF = Changed;
1321 return Changed;
1322}
1323
1325 for (MachineBasicBlock &MBB : mf) {
1327 if (MI.isDebugInstr())
1328 MBB.erase(&MI);
1329 }
1330}
1331
1333 MachineFunction &mf) {
1334 auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
1335
1336 Impl = std::make_unique<LiveDebugVariables>();
1337 Impl->analyze(mf, LIS);
1338 return false;
1339}
1340
1341AnalysisKey LiveDebugVariablesAnalysis::Key;
1342
1346 MFPropsModifier _(*this, MF);
1347
1348 auto *LIS = &MFAM.getResult<LiveIntervalsAnalysis>(MF);
1350 LDV.analyze(MF, LIS);
1351 return LDV;
1352}
1353
1361
1363 if (PImpl)
1364 PImpl->clear();
1365}
1366
1369 MachineFunctionAnalysisManager::Invalidator &) {
1370 auto PAC = PA.getChecker<LiveDebugVariablesAnalysis>();
1371 // Some architectures split the register allocation into multiple phases based
1372 // on register classes. This requires preserving analyses between the phases
1373 // by default.
1374 return !PAC.preservedWhenStateless();
1375}
1376
1378 if (!EnableLDV)
1379 return;
1380 if (!MF.getFunction().getSubprogram()) {
1382 return;
1383 }
1384
1385 PImpl.reset(new LDVImpl(LIS));
1386
1387 // Have we been asked to track variable locations using instruction
1388 // referencing?
1389 bool InstrRef = MF.useDebugInstrRef();
1390 PImpl->runOnMachineFunction(MF, InstrRef);
1391}
1392
1393//===----------------------------------------------------------------------===//
1394// Live Range Splitting
1395//===----------------------------------------------------------------------===//
1396
1397bool
1398UserValue::splitLocation(unsigned OldLocNo, ArrayRef<Register> NewRegs,
1399 LiveIntervals& LIS) {
1400 LLVM_DEBUG({
1401 dbgs() << "Splitting Loc" << OldLocNo << '\t';
1402 print(dbgs(), nullptr);
1403 });
1404 bool DidChange = false;
1405 LocMap::iterator LocMapI;
1406 LocMapI.setMap(locInts);
1407 for (Register NewReg : NewRegs) {
1408 LiveInterval *LI = &LIS.getInterval(NewReg);
1409 if (LI->empty())
1410 continue;
1411
1412 // Don't allocate the new LocNo until it is needed.
1413 unsigned NewLocNo = UndefLocNo;
1414
1415 // Iterate over the overlaps between locInts and LI.
1416 LocMapI.find(LI->beginIndex());
1417 if (!LocMapI.valid())
1418 continue;
1419 LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start());
1420 LiveInterval::iterator LIE = LI->end();
1421 while (LocMapI.valid() && LII != LIE) {
1422 // At this point, we know that LocMapI.stop() > LII->start.
1423 LII = LI->advanceTo(LII, LocMapI.start());
1424 if (LII == LIE)
1425 break;
1426
1427 // Now LII->end > LocMapI.start(). Do we have an overlap?
1428 if (LocMapI.value().containsLocNo(OldLocNo) &&
1429 LII->start < LocMapI.stop()) {
1430 // Overlapping correct location. Allocate NewLocNo now.
1431 if (NewLocNo == UndefLocNo) {
1432 MachineOperand MO = MachineOperand::CreateReg(LI->reg(), false);
1433 MO.setSubReg(locations[OldLocNo].getSubReg());
1434 NewLocNo = getLocationNo(MO);
1435 DidChange = true;
1436 }
1437
1438 SlotIndex LStart = LocMapI.start();
1439 SlotIndex LStop = LocMapI.stop();
1440 DbgVariableValue OldDbgValue = LocMapI.value();
1441
1442 // Trim LocMapI down to the LII overlap.
1443 if (LStart < LII->start)
1444 LocMapI.setStartUnchecked(LII->start);
1445 if (LStop > LII->end)
1446 LocMapI.setStopUnchecked(LII->end);
1447
1448 // Change the value in the overlap. This may trigger coalescing.
1449 LocMapI.setValue(OldDbgValue.changeLocNo(OldLocNo, NewLocNo));
1450
1451 // Re-insert any removed OldDbgValue ranges.
1452 if (LStart < LocMapI.start()) {
1453 LocMapI.insert(LStart, LocMapI.start(), OldDbgValue);
1454 ++LocMapI;
1455 assert(LocMapI.valid() && "Unexpected coalescing");
1456 }
1457 if (LStop > LocMapI.stop()) {
1458 ++LocMapI;
1459 LocMapI.insert(LII->end, LStop, OldDbgValue);
1460 --LocMapI;
1461 }
1462 }
1463
1464 // Advance to the next overlap.
1465 if (LII->end < LocMapI.stop()) {
1466 if (++LII == LIE)
1467 break;
1468 LocMapI.advanceTo(LII->start);
1469 } else {
1470 ++LocMapI;
1471 if (!LocMapI.valid())
1472 break;
1473 LII = LI->advanceTo(LII, LocMapI.start());
1474 }
1475 }
1476 }
1477
1478 // Finally, remove OldLocNo unless it is still used by some interval in the
1479 // locInts map. One case when OldLocNo still is in use is when the register
1480 // has been spilled. In such situations the spilled register is kept as a
1481 // location until rewriteLocations is called (VirtRegMap is mapping the old
1482 // register to the spill slot). So for a while we can have locations that map
1483 // to virtual registers that have been removed from both the MachineFunction
1484 // and from LiveIntervals.
1485 //
1486 // We may also just be using the location for a value with a different
1487 // expression.
1488 removeLocationIfUnused(OldLocNo);
1489
1490 LLVM_DEBUG({
1491 dbgs() << "Split result: \t";
1492 print(dbgs(), nullptr);
1493 });
1494 return DidChange;
1495}
1496
1497bool
1498UserValue::splitRegister(Register OldReg, ArrayRef<Register> NewRegs,
1499 LiveIntervals &LIS) {
1500 bool DidChange = false;
1501 // Split locations referring to OldReg. Iterate backwards so splitLocation can
1502 // safely erase unused locations.
1503 for (unsigned i = locations.size(); i ; --i) {
1504 unsigned LocNo = i-1;
1505 const MachineOperand *Loc = &locations[LocNo];
1506 if (!Loc->isReg() || Loc->getReg() != OldReg)
1507 continue;
1508 DidChange |= splitLocation(LocNo, NewRegs, LIS);
1509 }
1510 return DidChange;
1511}
1512
1514 ArrayRef<Register> NewRegs) {
1515 auto RegIt = RegToPHIIdx.find(OldReg);
1516 if (RegIt == RegToPHIIdx.end())
1517 return;
1518
1519 std::vector<std::pair<Register, unsigned>> NewRegIdxes;
1520 // Iterate over all the debug instruction numbers affected by this split.
1521 for (unsigned InstrID : RegIt->second) {
1522 auto PHIIt = PHIValToPos.find(InstrID);
1523 assert(PHIIt != PHIValToPos.end());
1524 const SlotIndex &Slot = PHIIt->second.SI;
1525 assert(OldReg == PHIIt->second.Reg);
1526
1527 // Find the new register that covers this position.
1528 for (auto NewReg : NewRegs) {
1529 const LiveInterval &LI = LIS->getInterval(NewReg);
1530 auto LII = LI.find(Slot);
1531 if (LII != LI.end() && LII->start <= Slot) {
1532 // This new register covers this PHI position, record this for indexing.
1533 NewRegIdxes.push_back(std::make_pair(NewReg, InstrID));
1534 // Record that this value lives in a different VReg now.
1535 PHIIt->second.Reg = NewReg;
1536 break;
1537 }
1538 }
1539
1540 // If we do not find a new register covering this PHI, then register
1541 // allocation has dropped its location, for example because it's not live.
1542 // The old VReg will not be mapped to a physreg, and the instruction
1543 // number will have been optimized out.
1544 }
1545
1546 // Re-create register index using the new register numbers.
1547 RegToPHIIdx.erase(RegIt);
1548 for (auto &RegAndInstr : NewRegIdxes)
1549 RegToPHIIdx[RegAndInstr.first].push_back(RegAndInstr.second);
1550}
1551
1553 ArrayRef<Register> NewRegs) {
1554 // Consider whether this split range affects any PHI locations.
1555 splitPHIRegister(OldReg, NewRegs);
1556
1557 // Check whether any intervals mapped by a DBG_VALUE were split and need
1558 // updating.
1559 bool DidChange = false;
1560 for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext())
1561 DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS);
1562
1563 if (!DidChange)
1564 return;
1565
1566 // Map all of the new virtual registers.
1567 UserValue *UV = lookupVirtReg(OldReg);
1568 for (Register NewReg : NewRegs)
1569 mapVirtReg(NewReg, UV);
1570}
1571
1574 if (PImpl)
1575 PImpl->splitRegister(OldReg, NewRegs);
1576}
1577
1578//===----------------------------------------------------------------------===//
1579// Stale Index Canonicalization
1580//===----------------------------------------------------------------------===//
1581
1582void UserValue::canonicalizeIndexes(const SlotIndexes &SI) {
1583 unsigned NumStale = 0;
1584 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
1585 NumStale += SI.isStaleIndex(I.start()) + SI.isStaleIndex(I.stop());
1586 for (SlotIndex Idx : trimmedDefs)
1587 NumStale += SI.isStaleIndex(Idx);
1588 NumStaleIndexes += NumStale;
1589
1590 if (NumStale) {
1591 // trimmedDefs is looked up by interval start. Remapping it here is safe:
1592 // trimmed starts are block slots, so the Stop < Start case below cannot
1593 // reach them, and a merge drops a start that then matches nothing.
1594 if (!trimmedDefs.empty()) {
1595 SmallVector<SlotIndex, 8> Defs(trimmedDefs.begin(), trimmedDefs.end());
1596 trimmedDefs.clear();
1597 for (SlotIndex Idx : Defs) {
1598 SlotIndex Canon = SI.canonicalizeIndex(Idx);
1599 if (!SI.isBlockBoundaryIndex(Canon))
1600 trimmedDefs.insert(Canon);
1601 }
1602 }
1603
1604 // Rebuild rather than move the keys of the existing map: it has to stay
1605 // ordered and non-empty at every step, which canonicalization does not
1606 // respect.
1607 struct CanonicalInterval {
1608 SlotIndex Start;
1609 SlotIndex Stop;
1610 DbgVariableValue Value;
1611 };
1613
1614 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) {
1615 SlotIndex Start = SI.canonicalizeIndex(I.start());
1616 SlotIndex Stop = SI.canonicalizeIndex(I.stop());
1617
1618 // A stale stop can land below a start that sat on the same instruction's
1619 // dead slot. Both resolve to the same insert location.
1620 if (Stop < Start)
1621 Start = Stop;
1622
1623 if (!Intervals.empty()) {
1624 CanonicalInterval &Prev = Intervals.back();
1625 if (Start <= Prev.Start) {
1626 // Both DBG_VALUEs would be emitted at the same position, where the
1627 // later one overrides the earlier before it covers anything.
1628 Prev.Stop = std::max(Prev.Stop, Stop);
1629 Prev.Value = I.value();
1630 ++NumMergedIntervals;
1631 continue;
1632 }
1633 Prev.Stop = std::min(Prev.Stop, Start);
1634 }
1635 Intervals.push_back({Start, Stop, I.value()});
1636 }
1637
1638 // The map cannot hold empty intervals. Use the smallest extent there is: a
1639 // wider one would span more blocks, and emitDebugValues() emits a DBG_VALUE
1640 // per block covered.
1641 for (CanonicalInterval &Interval : Intervals) {
1642 if (Interval.Stop > Interval.Start)
1643 continue;
1644 Interval.Stop = Interval.Start.getNextSlot();
1645 assert(!SI.isStaleIndex(Interval.Stop) &&
1646 "No room left for a canonicalized interval");
1647 }
1648
1649 locInts.clear();
1650 for (const CanonicalInterval &Interval : Intervals)
1651 locInts.insert(Interval.Start, Interval.Stop, Interval.Value);
1652 }
1653
1654#ifndef NDEBUG
1655 for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I)
1656 assert(!SI.isStaleIndex(I.start()) && !SI.isStaleIndex(I.stop()) &&
1657 "Canonicalized interval still refers to an erased instruction");
1658 for (SlotIndex Idx : trimmedDefs)
1659 assert(!SI.isStaleIndex(Idx) &&
1660 "Canonicalized trimmed def still refers to an erased instruction");
1661#endif
1662}
1663
1665 for (auto &userValue : userValues)
1666 userValue->canonicalizeIndexes(SI);
1667 for (auto &userLabel : userLabels)
1668 NumStaleIndexes += userLabel->canonicalizeIndex(SI);
1669
1670 // emitDebugValues() walks forwards to the next live instruction, which is the
1671 // same iterator as inserting after the preceding one, and stays inside
1672 // InstrPos::MBB. Canonicalization is monotonic, so entries sharing a slot are
1673 // still re-inserted as one batch.
1674 for (InstrPos &Stashed : StashedDebugInstrs) {
1675 if (!SI.isStaleIndex(Stashed.Idx))
1676 continue;
1677 ++NumStaleIndexes;
1678 Stashed.Idx = SI.canonicalizeIndex(Stashed.Idx);
1679 assert(!SI.isStaleIndex(Stashed.Idx) &&
1680 "Canonicalized debug instr still refers to an erased instruction");
1681 }
1682
1683#ifndef NDEBUG
1684 // PHI positions are block starts, which are boundaries. A block erased by
1685 // removeMBBFromMaps() would make one look stale.
1686 for (const auto &P : PHIValToPos)
1687 assert(!SI.isStaleIndex(P.second.SI) &&
1688 "PHI position refers to an erased instruction");
1689#endif
1690}
1691
1693 if (PImpl)
1694 PImpl->canonicalizeIndexes(SI);
1695}
1696
1697void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF,
1698 const TargetInstrInfo &TII,
1699 const TargetRegisterInfo &TRI,
1700 SpillOffsetMap &SpillOffsets) {
1701 // Build a set of new locations with new numbers so we can coalesce our
1702 // IntervalMap if two vreg intervals collapse to the same physical location.
1703 // Use MapVector instead of SetVector because MapVector::insert returns the
1704 // position of the previously or newly inserted element. The boolean value
1705 // tracks if the location was produced by a spill.
1706 // FIXME: This will be problematic if we ever support direct and indirect
1707 // frame index locations, i.e. expressing both variables in memory and
1708 // 'int x, *px = &x'. The "spilled" bit must become part of the location.
1710 SmallVector<unsigned, 4> LocNoMap(locations.size());
1711 for (unsigned I = 0, E = locations.size(); I != E; ++I) {
1712 bool Spilled = false;
1713 unsigned SpillOffset = 0;
1714 MachineOperand Loc = locations[I];
1715 // Only virtual registers are rewritten.
1716 if (Loc.isReg() && Loc.getReg() && Loc.getReg().isVirtual()) {
1717 Register VirtReg = Loc.getReg();
1718 if (VRM.isAssignedReg(VirtReg) && VRM.hasPhys(VirtReg)) {
1719 // This can create a %noreg operand in rare cases when the sub-register
1720 // index is no longer available. That means the user value is in a
1721 // non-existent sub-register, and %noreg is exactly what we want.
1722 Loc.substPhysReg(VRM.getPhys(VirtReg), TRI);
1723 } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
1724 // Retrieve the stack slot offset.
1725 unsigned SpillSize;
1726 const MachineRegisterInfo &MRI = MF.getRegInfo();
1727 const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg);
1728 bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize,
1729 SpillOffset, MF);
1730
1731 // FIXME: Invalidate the location if the offset couldn't be calculated.
1732 (void)Success;
1733
1735 Spilled = true;
1736 } else {
1737 Loc.setReg(0);
1738 Loc.setSubReg(0);
1739 }
1740 }
1741
1742 // Insert this location if it doesn't already exist and record a mapping
1743 // from the old number to the new number.
1744 auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}});
1745 unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first);
1746 LocNoMap[I] = NewLocNo;
1747 }
1748
1749 // Rewrite the locations and record the stack slot offsets for spills.
1750 locations.clear();
1751 SpillOffsets.clear();
1752 for (auto &Pair : NewLocations) {
1753 bool Spilled;
1754 unsigned SpillOffset;
1755 std::tie(Spilled, SpillOffset) = Pair.second;
1756 locations.push_back(Pair.first);
1757 if (Spilled) {
1758 unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair);
1759 SpillOffsets[NewLocNo] = SpillOffset;
1760 }
1761 }
1762
1763 // Update the interval map, but only coalesce left, since intervals to the
1764 // right use the old location numbers. This should merge two contiguous
1765 // DBG_VALUE intervals with different vregs that were allocated to the same
1766 // physical register.
1767 for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) {
1768 I.setValueUnchecked(I.value().remapLocNos(LocNoMap));
1769 I.setStart(I.start());
1770 }
1771}
1772
1773/// Find an iterator for inserting a DBG_VALUE instruction.
1775 SlotIndex Idx,
1776 LiveIntervals &LIS,
1777 InsertLocCache &Cache) {
1778 SlotIndex Start = LIS.getMBBStartIdx(MBB);
1779 Idx = Idx.getBaseIndex();
1780
1781 // Try to find an insert location by going backwards from Idx.
1783 while (!(MI = LIS.getInstructionFromIndex(Idx))) {
1784 // We've reached the beginning of MBB.
1785 if (Idx == Start) {
1786 // Retrieve the last PHI/Label/Debug location found when calling
1787 // SkipPHIsLabelsAndDebug last time. Start searching from there.
1788 //
1789 // Note the iterator kept in Cache.BlockStart is one step back based
1790 // on the iterator returned by SkipPHIsLabelsAndDebug last time.
1791 // One exception is when SkipPHIsLabelsAndDebug returns MBB->begin(),
1792 // Cache.BlockStart won't save it. This is to consider the case that
1793 // new instructions may be inserted at the beginning of MBB after
1794 // last call of SkipPHIsLabelsAndDebug. If we save MBB->begin() in
1795 // Cache.BlockStart, after new non-phi/non-label/non-debug instructions
1796 // are inserted at the beginning of the MBB, the iterator in
1797 // Cache.BlockStart won't point to the beginning of the MBB anymore.
1798 // Therefore The next search in SkipPHIsLabelsAndDebug will skip those
1799 // newly added instructions and that is unwanted.
1801 auto MapIt = Cache.BlockStart.find(MBB);
1802 if (MapIt == Cache.BlockStart.end())
1803 BeginIt = MBB->begin();
1804 else
1805 BeginIt = std::next(MapIt->second);
1806 auto I = MBB->SkipPHIsLabelsAndDebug(BeginIt);
1807 if (I != BeginIt)
1808 Cache.BlockStart[MBB] = std::prev(I);
1809 return I;
1810 }
1811 Idx = Idx.getPrevIndex();
1812 }
1813
1814 // Don't insert anything after the first terminator, though.
1815 if (MI->isTerminator())
1816 return MBB->getFirstTerminator();
1817
1818 // Resume from the last debug instruction found after MI on a previous call,
1819 // so that repeated insertions after MI don't rescan the same debug
1820 // instructions.
1822 if (auto CacheIt = Cache.AfterInstr.find(MI);
1823 CacheIt != Cache.AfterInstr.end())
1824 Prev = CacheIt->second;
1825 auto BeginIt = std::next(Prev);
1826 auto I = skipDebugInstructionsForward(BeginIt, MBB->end());
1827 if (I != BeginIt)
1828 Cache.AfterInstr[MI] = std::prev(I);
1829 return I;
1830}
1831
1832/// Find an iterator for inserting the next DBG_VALUE instruction
1833/// (or end if no more insert locations found).
1836 SlotIndex StopIdx, ArrayRef<MachineOperand> LocMOs,
1837 LiveIntervals &LIS, const TargetRegisterInfo &TRI) {
1839 for (const MachineOperand &LocMO : LocMOs)
1840 if (LocMO.isReg())
1841 Regs.push_back(LocMO.getReg());
1842 if (Regs.empty())
1843 return MBB->instr_end();
1844
1845 // Find the next instruction in the MBB that define the register Reg.
1846 while (I != MBB->end() && !I->isTerminator()) {
1847 if (!LIS.isNotInMIMap(*I) &&
1849 break;
1850 if (any_of(Regs, [&I, &TRI](Register &Reg) {
1851 return I->definesRegister(Reg, &TRI);
1852 }))
1853 // The insert location is directly after the instruction/bundle.
1854 return std::next(I);
1855 ++I;
1856 }
1857 return MBB->end();
1858}
1859
1860void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
1861 SlotIndex StopIdx, DbgVariableValue DbgValue,
1862 ArrayRef<bool> LocSpills,
1863 ArrayRef<unsigned> SpillOffsets,
1864 LiveIntervals &LIS, const TargetInstrInfo &TII,
1865 const TargetRegisterInfo &TRI,
1866 InsertLocCache &Cache) {
1867 SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB);
1868 // Only search within the current MBB.
1869 StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx;
1870 MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS, Cache);
1871 // Undef values don't exist in locations so create new "noreg" register MOs
1872 // for them. See getLocationNo().
1874 if (DbgValue.isUndef()) {
1875 MOs.assign(DbgValue.loc_nos().size(),
1877 /* Reg */ 0, /* isDef */ false, /* isImp */ false,
1878 /* isKill */ false, /* isDead */ false,
1879 /* isUndef */ false, /* isEarlyClobber */ false,
1880 /* SubReg */ 0, /* isDebug */ true));
1881 } else {
1882 for (unsigned LocNo : DbgValue.loc_nos())
1883 MOs.push_back(locations[LocNo]);
1884 }
1885
1886 ++NumInsertedDebugValues;
1887
1889 ->isValidLocationForIntrinsic(getDebugLoc()) &&
1890 "Expected inlined-at fields to agree");
1891
1892 // If the location was spilled, the new DBG_VALUE will be indirect. If the
1893 // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1894 // that the original virtual register was a pointer. Also, add the stack slot
1895 // offset for the spilled register to the expression.
1896 const DIExpression *Expr = DbgValue.getExpression();
1897 bool IsIndirect = DbgValue.getWasIndirect();
1898 bool IsList = DbgValue.getWasList();
1899 for (unsigned I = 0, E = LocSpills.size(); I != E; ++I) {
1900 if (LocSpills[I]) {
1901 if (!IsList) {
1902 uint8_t DIExprFlags = DIExpression::ApplyOffset;
1903 if (IsIndirect)
1904 DIExprFlags |= DIExpression::DerefAfter;
1905 Expr = DIExpression::prepend(Expr, DIExprFlags, SpillOffsets[I]);
1906 IsIndirect = true;
1907 } else {
1908 SmallVector<uint64_t, 4> Ops;
1909 DIExpression::appendOffset(Ops, SpillOffsets[I]);
1910 Ops.push_back(dwarf::DW_OP_deref);
1911 Expr = DIExpression::appendOpsToArg(Expr, Ops, I);
1912 }
1913 }
1914
1915 assert((!LocSpills[I] || MOs[I].isFI()) &&
1916 "a spilled location must be a frame index");
1917 }
1918
1919 unsigned DbgValueOpcode =
1920 IsList ? TargetOpcode::DBG_VALUE_LIST : TargetOpcode::DBG_VALUE;
1921 do {
1922 BuildMI(*MBB, I, getDebugLoc(), TII.get(DbgValueOpcode), IsIndirect, MOs,
1923 Variable, Expr);
1924
1925 // Continue and insert DBG_VALUES after every redefinition of a register
1926 // associated with the debug value within the range
1927 I = findNextInsertLocation(MBB, I, StopIdx, MOs, LIS, TRI);
1928 } while (I != MBB->end());
1929}
1930
1931void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx,
1932 LiveIntervals &LIS, const TargetInstrInfo &TII,
1933 InsertLocCache &Cache) {
1935 ++NumInsertedDebugLabels;
1936 BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL))
1937 .addMetadata(Label);
1938}
1939
1940void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
1941 const TargetInstrInfo &TII,
1942 const TargetRegisterInfo &TRI,
1943 const SpillOffsetMap &SpillOffsets,
1944 InsertLocCache &Cache) {
1946
1947 for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
1948 SlotIndex Start = I.start();
1949 SlotIndex Stop = I.stop();
1950 DbgVariableValue DbgValue = I.value();
1951
1952 SmallVector<bool> SpilledLocs;
1953 SmallVector<unsigned> LocSpillOffsets;
1954 for (unsigned LocNo : DbgValue.loc_nos()) {
1955 auto SpillIt =
1956 !DbgValue.isUndef() ? SpillOffsets.find(LocNo) : SpillOffsets.end();
1957 bool Spilled = SpillIt != SpillOffsets.end();
1958 SpilledLocs.push_back(Spilled);
1959 LocSpillOffsets.push_back(Spilled ? SpillIt->second : 0);
1960 }
1961
1962 // If the interval start was trimmed to the lexical scope insert the
1963 // DBG_VALUE at the previous index (otherwise it appears after the
1964 // first instruction in the range).
1965 if (trimmedDefs.count(Start))
1966 Start = Start.getPrevIndex();
1967
1968 LLVM_DEBUG(auto &dbg = dbgs(); dbg << "\t[" << Start << ';' << Stop << "):";
1969 DbgValue.printLocNos(dbg));
1971 SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
1972
1973 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1974 insertDebugValue(&*MBB, Start, Stop, DbgValue, SpilledLocs, LocSpillOffsets,
1975 LIS, TII, TRI, Cache);
1976 // This interval may span multiple basic blocks.
1977 // Insert a DBG_VALUE into each one.
1978 while (Stop > MBBEnd) {
1979 // Move to the next block.
1980 Start = MBBEnd;
1981 if (++MBB == MFEnd)
1982 break;
1983 MBBEnd = LIS.getMBBEndIdx(&*MBB);
1984 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd);
1985 insertDebugValue(&*MBB, Start, Stop, DbgValue, SpilledLocs,
1986 LocSpillOffsets, LIS, TII, TRI, Cache);
1987 }
1988 LLVM_DEBUG(dbgs() << '\n');
1989 if (MBB == MFEnd)
1990 break;
1991
1992 ++I;
1993 }
1994}
1995
1996void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII,
1997 InsertLocCache &Cache) {
1998 LLVM_DEBUG(dbgs() << "\t" << loc);
2000
2001 LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB));
2002 insertDebugLabel(&*MBB, loc, LIS, TII, Cache);
2003
2004 LLVM_DEBUG(dbgs() << '\n');
2005}
2006
2008 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n");
2009 if (!MF)
2010 return;
2011
2012 // Instructions may have been erased since the last allocator run.
2013 canonicalizeIndexes(*LIS->getSlotIndexes());
2014
2015 InsertLocCache Cache;
2016 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
2017 SpillOffsetMap SpillOffsets;
2018 for (auto &userValue : userValues) {
2019 LLVM_DEBUG(userValue->print(dbgs(), TRI));
2020 userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets);
2021 userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets, Cache);
2022 }
2023 LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n");
2024 for (auto &userLabel : userLabels) {
2025 LLVM_DEBUG(userLabel->print(dbgs(), TRI));
2026 userLabel->emitDebugLabel(*LIS, *TII, Cache);
2027 }
2028
2029 LLVM_DEBUG(dbgs() << "********** EMITTING DEBUG PHIS **********\n");
2030
2031 auto Slots = LIS->getSlotIndexes();
2032 for (auto &It : PHIValToPos) {
2033 // For each ex-PHI, identify its physreg location or stack slot, and emit
2034 // a DBG_PHI for it.
2035 unsigned InstNum = It.first;
2036 auto Slot = It.second.SI;
2037 Register Reg = It.second.Reg;
2038 unsigned SubReg = It.second.SubReg;
2039
2040 MachineBasicBlock *OrigMBB = Slots->getMBBFromIndex(Slot);
2041 if (VRM->isAssignedReg(Reg) && VRM->hasPhys(Reg)) {
2042 unsigned PhysReg = VRM->getPhys(Reg);
2043 if (SubReg != 0)
2044 PhysReg = TRI->getSubReg(PhysReg, SubReg);
2045
2046 auto Builder = BuildMI(*OrigMBB, OrigMBB->begin(), DebugLoc(),
2047 TII->get(TargetOpcode::DBG_PHI));
2048 Builder.addReg(PhysReg);
2049 Builder.addImm(InstNum);
2050 } else if (VRM->getStackSlot(Reg) != VirtRegMap::NO_STACK_SLOT) {
2051 const MachineRegisterInfo &MRI = MF->getRegInfo();
2052 const TargetRegisterClass *TRC = MRI.getRegClass(Reg);
2053 unsigned SpillSize, SpillOffset;
2054
2055 unsigned regSizeInBits = TRI->getRegSizeInBits(*TRC);
2056 if (SubReg)
2057 regSizeInBits = TRI->getSubRegIdxSize(SubReg);
2058
2059 // Test whether this location is legal with the given subreg. If the
2060 // subregister has a nonzero offset, drop this location, it's too complex
2061 // to describe. (TODO: future work).
2062 bool Success =
2063 TII->getStackSlotRange(TRC, SubReg, SpillSize, SpillOffset, *MF);
2064
2065 if (Success && SpillOffset == 0) {
2066 auto Builder = BuildMI(*OrigMBB, OrigMBB->begin(), DebugLoc(),
2067 TII->get(TargetOpcode::DBG_PHI));
2068 Builder.addFrameIndex(VRM->getStackSlot(Reg));
2069 Builder.addImm(InstNum);
2070 // Record how large the original value is. The stack slot might be
2071 // merged and altered during optimisation, but we will want to know how
2072 // large the value is, at this DBG_PHI.
2073 Builder.addImm(regSizeInBits);
2074 }
2075
2076 LLVM_DEBUG(if (SpillOffset != 0) {
2077 dbgs() << "DBG_PHI for " << printReg(Reg, TRI, SubReg)
2078 << " has nonzero offset\n";
2079 });
2080 }
2081 // If there was no mapping for a value ID, it's optimized out. Create no
2082 // DBG_PHI, and any variables using this value will become optimized out.
2083 }
2084 MF->DebugPHIPositions.clear();
2085
2086 LLVM_DEBUG(dbgs() << "********** EMITTING INSTR REFERENCES **********\n");
2087
2088 // Re-insert any debug instrs back in the position they were. We must
2089 // re-insert in the same order to ensure that debug instructions don't swap,
2090 // which could re-order assignments. Do so in a batch -- once we find the
2091 // insert position, insert all instructions at the same SlotIdx. They are
2092 // guaranteed to appear in-sequence in StashedDebugInstrs because we insert
2093 // them in order.
2094 for (auto *StashIt = StashedDebugInstrs.begin();
2095 StashIt != StashedDebugInstrs.end(); ++StashIt) {
2096 SlotIndex Idx = StashIt->Idx;
2097 MachineBasicBlock *MBB = StashIt->MBB;
2098 MachineInstr *MI = StashIt->MI;
2099
2100 auto EmitInstsHere = [this, &StashIt, MBB, Idx,
2101 MI](MachineBasicBlock::iterator InsertPos) {
2102 // Insert this debug instruction.
2103 MBB->insert(InsertPos, MI);
2104
2105 // Look at subsequent stashed debug instructions: if they're at the same
2106 // index, insert those too.
2107 auto NextItem = std::next(StashIt);
2108 while (NextItem != StashedDebugInstrs.end() && NextItem->Idx == Idx) {
2109 assert(NextItem->MBB == MBB && "Instrs with same slot index should be"
2110 "in the same block");
2111 MBB->insert(InsertPos, NextItem->MI);
2112 StashIt = NextItem;
2113 NextItem = std::next(StashIt);
2114 };
2115 };
2116
2117 // Start block index: find the first non-debug instr in the block, and
2118 // insert before it.
2119 if (Idx == Slots->getMBBStartIdx(MBB)) {
2120 MachineBasicBlock::iterator InsertPos =
2121 findInsertLocation(MBB, Idx, *LIS, Cache);
2122 EmitInstsHere(InsertPos);
2123 continue;
2124 }
2125
2126 if (MachineInstr *Pos = Slots->getInstructionFromIndex(Idx)) {
2127 // Insert at the end of any debug instructions.
2128 auto PostDebug = std::next(MachineBasicBlock::iterator(Pos));
2129 PostDebug = skipDebugInstructionsForward(PostDebug, MBB->end());
2130 EmitInstsHere(PostDebug);
2131 } else {
2132 // Insert position disappeared; walk forwards through slots until we
2133 // find a new one.
2134 SlotIndex End = Slots->getMBBEndIdx(MBB);
2135 for (; Idx < End; Idx = Slots->getNextNonNullIndex(Idx)) {
2136 Pos = Slots->getInstructionFromIndex(Idx);
2137 if (Pos) {
2138 EmitInstsHere(Pos->getIterator());
2139 break;
2140 }
2141 }
2142
2143 // We have reached the end of the block and didn't find anywhere to
2144 // insert! It's not safe to discard any debug instructions; place them
2145 // in front of the first terminator, or in front of end().
2146 if (Idx >= End) {
2147 auto TermIt = MBB->getFirstTerminator();
2148 EmitInstsHere(TermIt);
2149 }
2150 }
2151 }
2152
2153 EmitDone = true;
2154 Cache.clear();
2155}
2156
2158 if (PImpl)
2159 PImpl->emitDebugValues(VRM);
2160}
2161
2162#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2164#endif
2165
2167 if (PImpl)
2168 PImpl->print(OS);
2169}
#define Success
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis false
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:682
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
This file implements a coalescing interval map for small objects.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static void printExtendedName(raw_ostream &OS, const DINode *Node, const DILocation *DL)
static MachineBasicBlock::iterator findNextInsertLocation(MachineBasicBlock *MBB, MachineBasicBlock::iterator I, SlotIndex StopIdx, ArrayRef< MachineOperand > LocMOs, LiveIntervals &LIS, const TargetRegisterInfo &TRI)
Find an iterator for inserting the next DBG_VALUE instruction (or end if no more insert locations fou...
IntervalMap< SlotIndex, DbgVariableValue, 4 > LocMap
Map of where a user value is live to that value.
static MachineBasicBlock::iterator findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, LiveIntervals &LIS, InsertLocCache &Cache)
Find an iterator for inserting a DBG_VALUE instruction.
static cl::opt< bool > EnableLDV("live-debug-variables", cl::init(true), cl::desc("Enable the live debug variables pass"), cl::Hidden)
static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS, const LLVMContext &Ctx)
DenseMap< unsigned, unsigned > SpillOffsetMap
Map of stack slot offsets for spilled locations.
static void removeDebugInstrs(MachineFunction &mf)
static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B)
#define I(x, y, z)
Definition MD5.cpp:57
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
std::pair< uint64_t, uint64_t > Interval
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
SI Optimize VGPR LiveRange
Func MI getDebugLoc()))
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
Value * RHS
Value * LHS
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
static LLVM_ABI DIExpression * replaceArg(const DIExpression *Expr, uint64_t OldArg, uint64_t NewArg)
Create a copy of Expr with each instance of DW_OP_LLVM_arg, \p OldArg replaced with DW_OP_LLVM_arg,...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
Tagged DWARF-like metadata node.
A debug info location.
Definition DebugLoc.h:126
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
Identifies a unique instance of a variable.
bool empty() const
Definition DenseMap.h:716
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
DISubprogram * getSubprogram() const
Get the attached subprogram.
const_iterator begin() const
void insert(KeyT a, KeyT b, ValT y)
insert - Add a mapping of [a;b] to y, coalesce with adjacent intervals.
const_iterator find(KeyT x) const
find - Return an iterator pointing to the first interval ending at or after x, or end().
void clear()
clear - Remove all entries.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool runOnMachineFunction(MachineFunction &) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void splitRegister(Register OldReg, ArrayRef< Register > NewRegs)
Replace all references to OldReg with NewRegs.
bool runOnMachineFunction(MachineFunction &mf, bool InstrRef)
void mapVirtReg(Register VirtReg, UserValue *EC)
Map virtual register to an equivalence class.
void canonicalizeIndexes(const SlotIndexes &SI)
Replace every stale index held by this analysis.
void emitDebugValues(VirtRegMap *VRM)
Recreate DBG_VALUE instruction from data structures.
void splitPHIRegister(Register OldReg, ArrayRef< Register > NewRegs)
Replace any PHI referring to OldReg with its corresponding NewReg, if present.
LLVM_ABI void canonicalizeIndexes(const SlotIndexes &SI)
canonicalizeIndexes - Replace every SlotIndex held by this analysis that refers to an erased instruct...
LLVM_ABI ~LiveDebugVariables()
void dump() const
dump - Print data structures to dbgs().
LLVM_ABI void splitRegister(Register OldReg, ArrayRef< Register > NewRegs, LiveIntervals &LIS)
splitRegister - Move any user variables in OldReg to the live ranges in NewRegs where they are live.
LLVM_ABI LiveDebugVariables()
Implementation of the LiveDebugVariables pass.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI void analyze(MachineFunction &MF, LiveIntervals *LIS)
LLVM_ABI void emitDebugValues(VirtRegMap *VRM)
emitDebugValues - Emit new DBG_VALUE instructions reflecting the changes that happened during registe...
LLVM_ABI bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
LiveInterval - This class represents the liveness of a register, or stack slot.
Register reg() const
bool hasInterval(Register Reg) const
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Return the first index in the given basic block.
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
SlotIndexes * getSlotIndexes() const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const
Return the last index in the given basic block.
LiveInterval & getInterval(Register Reg)
bool isNotInMIMap(const MachineInstr &Instr) const
Returns true if the specified machine instr has been removed or was never entered in the map.
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
VNInfo * valueOutOrDead() const
Returns the value alive at the end of the instruction, if any.
Segments::iterator iterator
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
iterator advanceTo(iterator I, SlotIndex Pos)
advanceTo - Advance the specified iterator to point to the Segment containing the specified position,...
bool empty() const
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
iterator begin()
SlotIndex beginIndex() const
beginIndex - Return the lowest numbered slot covered.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
LLVM_ABI iterator find(SlotIndex Pos)
find - Return an iterator pointing to the first segment that ends after Pos, or end().
LLVMContext & getContext() const
Definition Metadata.h:1245
An RAII based helper class to modify MachineFunctionProperties when running pass.
void push_back(MachineInstr *MI)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineInstrBundleIterator< MachineInstr > iterator
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Location of a PHI instruction that is also a debug-info variable value, for the duration of register ...
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void push_back(MachineBasicBlock *MBB)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
const MachineInstrBuilder & addMetadata(const MDNode *MD) const
Representation of each machine instruction.
bool isCopy() const
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
void setIsDebug(bool Val=true)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
iterator begin()
Definition MapVector.h:67
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition MapVector.h:126
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SlotIndex getNextIndex() const
Returns the next index.
static bool isEarlierEqualInstr(SlotIndex A, SlotIndex B)
Return true if A refers to the same instruction as B or an earlier one.
SlotIndex getNextSlot() const
Returns the next slot in the index list.
SlotIndexes pass.
SlotIndex getIndexBefore(const MachineInstr &MI) const
getIndexBefore - Returns the index of the last indexed instruction before MI, or the start index of i...
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Returns the first index in the given basic block.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
void assign(size_type NumElts, ValueParamT Elt)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
unsigned id
The ID number of this value.
SlotIndex def
The index of the defining instruction.
int getStackSlot(Register virtReg) const
returns the stack slot mapped to the specified virtual register
Definition VirtRegMap.h:172
MachineFunction & getMachineFunction() const
Definition VirtRegMap.h:75
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition VirtRegMap.h:87
bool isAssignedReg(Register virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
Definition VirtRegMap.h:162
static constexpr int NO_STACK_SLOT
Definition VirtRegMap.h:66
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2139
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
std::pair< const MachineInstr *, const MachineInstr * > InsnRange
This is used to track range of instructions with identical lexical scope.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Success
The lock was released successfully.
@ Other
Any other memory.
Definition ModRef.h:68
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1901
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
Caches used by findInsertLocation so that repeated insertions at the same position don't rescan the d...
DenseMap< const MachineInstr *, MachineBasicBlock::iterator > AfterInstr
Per instruction, the last debug instruction found after it, used as the starting position for the nex...
DenseMap< MachineBasicBlock *, MachineBasicBlock::iterator > BlockStart
Per block, the last PHI/Label/Debug instruction found by MachineBasicBlock::SkipPHIsLabelsAndDebug,...
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29