| 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 | |
| 21 | #include "llvm/CodeGen/LiveDebugVariables.h" |
| 22 | #include "llvm/ADT/ArrayRef.h" |
| 23 | #include "llvm/ADT/DenseMap.h" |
| 24 | #include "llvm/ADT/IntervalMap.h" |
| 25 | #include "llvm/ADT/MapVector.h" |
| 26 | #include "llvm/ADT/STLExtras.h" |
| 27 | #include "llvm/ADT/SmallSet.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/StringRef.h" |
| 31 | #include "llvm/BinaryFormat/Dwarf.h" |
| 32 | #include "llvm/CodeGen/LexicalScopes.h" |
| 33 | #include "llvm/CodeGen/LiveInterval.h" |
| 34 | #include "llvm/CodeGen/LiveIntervals.h" |
| 35 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 36 | #include "llvm/CodeGen/MachineFunction.h" |
| 37 | #include "llvm/CodeGen/MachineInstr.h" |
| 38 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 39 | #include "llvm/CodeGen/MachineOperand.h" |
| 40 | #include "llvm/CodeGen/MachinePassManager.h" |
| 41 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 42 | #include "llvm/CodeGen/SlotIndexes.h" |
| 43 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| 44 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 45 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 46 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 47 | #include "llvm/CodeGen/VirtRegMap.h" |
| 48 | #include "llvm/Config/llvm-config.h" |
| 49 | #include "llvm/IR/DebugInfoMetadata.h" |
| 50 | #include "llvm/IR/DebugLoc.h" |
| 51 | #include "llvm/IR/Function.h" |
| 52 | #include "llvm/InitializePasses.h" |
| 53 | #include "llvm/Pass.h" |
| 54 | #include "llvm/Support/Casting.h" |
| 55 | #include "llvm/Support/CommandLine.h" |
| 56 | #include "llvm/Support/Debug.h" |
| 57 | #include "llvm/Support/raw_ostream.h" |
| 58 | #include <algorithm> |
| 59 | #include <cassert> |
| 60 | #include <iterator> |
| 61 | #include <map> |
| 62 | #include <memory> |
| 63 | #include <optional> |
| 64 | #include <utility> |
| 65 | |
| 66 | using namespace llvm; |
| 67 | |
| 68 | #define DEBUG_TYPE "livedebugvars" |
| 69 | |
| 70 | static cl::opt<bool> |
| 71 | EnableLDV("live-debug-variables" , cl::init(Val: true), |
| 72 | cl::desc("Enable the live debug variables pass" ), cl::Hidden); |
| 73 | |
| 74 | STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted" ); |
| 75 | STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted" ); |
| 76 | STATISTIC(NumStaleIndexes, "Number of stale SlotIndexes repaired" ); |
| 77 | STATISTIC(NumMergedIntervals, |
| 78 | "Number of debug value intervals merged while repairing indexes" ); |
| 79 | |
| 80 | char LiveDebugVariablesWrapperLegacy::ID = 0; |
| 81 | |
| 82 | INITIALIZE_PASS_BEGIN(LiveDebugVariablesWrapperLegacy, DEBUG_TYPE, |
| 83 | "Debug Variable Analysis" , false, false) |
| 84 | INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass) |
| 85 | INITIALIZE_PASS_END(LiveDebugVariablesWrapperLegacy, DEBUG_TYPE, |
| 86 | "Debug Variable Analysis" , false, true) |
| 87 | |
| 88 | void LiveDebugVariablesWrapperLegacy::getAnalysisUsage( |
| 89 | AnalysisUsage &AU) const { |
| 90 | AU.addRequiredTransitive<LiveIntervalsWrapperPass>(); |
| 91 | AU.setPreservesAll(); |
| 92 | MachineFunctionPass::getAnalysisUsage(AU); |
| 93 | } |
| 94 | |
| 95 | LiveDebugVariablesWrapperLegacy::LiveDebugVariablesWrapperLegacy() |
| 96 | : MachineFunctionPass(ID) {} |
| 97 | |
| 98 | enum : unsigned { UndefLocNo = ~0U }; |
| 99 | |
| 100 | namespace { |
| 101 | /// Describes a debug variable value by location number and expression along |
| 102 | /// with some flags about the original usage of the location. |
| 103 | class DbgVariableValue { |
| 104 | public: |
| 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(Range&: LocNoVec, Val: LocNo); |
| 113 | if (It == LocNoVec.end()) |
| 114 | LocNoVec.push_back(Elt: 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(first: LocNoVec.begin(), last: It); |
| 120 | Expression = |
| 121 | DIExpression::replaceArg(Expr: Expression, OldArg: OpIdx, NewArg: 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[]>(num: LocNoCount); |
| 134 | llvm::copy(Range&: LocNoVec, Out: 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(Context&: Expr.getContext(), Elements: {dwarf::DW_OP_LLVM_arg, 0}); |
| 144 | if (auto FragmentInfoOpt = Expr.getFragmentInfo()) |
| 145 | Expression = *DIExpression::createFragmentExpression( |
| 146 | Expr: Expression, OffsetInBits: FragmentInfoOpt->OffsetInBits, |
| 147 | SizeInBits: FragmentInfoOpt->SizeInBits); |
| 148 | LocNos = std::make_unique<unsigned[]>(num: 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(p: new unsigned[Other.getLocNoCount()]); |
| 159 | std::copy(first: Other.loc_nos_begin(), last: Other.loc_nos_end(), result: 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(p: new unsigned[Other.getLocNoCount()]); |
| 168 | std::copy(first: Other.loc_nos_begin(), last: Other.loc_nos_end(), result: 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(Range: loc_nos(), Element: LocNo); |
| 183 | } |
| 184 | bool getWasIndirect() const { return WasIndirect; } |
| 185 | bool getWasList() const { return WasList; } |
| 186 | bool isUndef() const { return LocNoCount == 0 || containsLocNo(LocNo: UndefLocNo); } |
| 187 | |
| 188 | DbgVariableValue decrementLocNosAfterPivot(unsigned Pivot) const { |
| 189 | SmallVector<unsigned, 4> NewLocNos; |
| 190 | for (unsigned LocNo : loc_nos()) |
| 191 | NewLocNos.push_back(Elt: 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(Elt: 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(in_start: loc_nos_begin(), in_end: loc_nos_end()); |
| 208 | auto OldLocIt = find(Range&: NewLocNos, Val: 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(Range: loc_nos(), |
| 216 | P: [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(args: LHS.LocNoCount, args: LHS.WasIndirect, args: LHS.WasList, |
| 227 | args: LHS.Expression) != |
| 228 | std::tie(args: RHS.LocNoCount, args: RHS.WasIndirect, args: RHS.WasList, args: RHS.Expression)) |
| 229 | return false; |
| 230 | return std::equal(first1: LHS.loc_nos_begin(), last1: LHS.loc_nos_end(), |
| 231 | first2: 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 | |
| 247 | private: |
| 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. |
| 262 | using LocMap = IntervalMap<SlotIndex, DbgVariableValue, 4>; |
| 263 | |
| 264 | /// Map of stack slot offsets for spilled locations. |
| 265 | /// Non-spilled locations are not added to the map. |
| 266 | using SpillOffsetMap = DenseMap<unsigned, unsigned>; |
| 267 | |
| 268 | /// Caches used by findInsertLocation so that repeated insertions at the same |
| 269 | /// position don't rescan the debug instructions already inserted there. |
| 270 | struct InsertLocCache { |
| 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. |
| 274 | DenseMap<MachineBasicBlock *, MachineBasicBlock::iterator> BlockStart; |
| 275 | /// Per instruction, the last debug instruction found after it, used as the |
| 276 | /// starting position for the next search after that instruction. |
| 277 | DenseMap<const MachineInstr *, MachineBasicBlock::iterator> AfterInstr; |
| 278 | |
| 279 | void clear() { |
| 280 | BlockStart.clear(); |
| 281 | AfterInstr.clear(); |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | namespace { |
| 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. |
| 295 | class 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 | |
| 329 | public: |
| 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(Other: locations[i])) |
| 387 | return i; |
| 388 | locations.push_back(Elt: 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(CI: 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(Pivot: 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(Elt: getLocationNo(LocMO: Op)); |
| 428 | DbgVariableValue DbgValue(Locs, IsIndirect, IsList, Expr); |
| 429 | // Add a singular (Idx,Idx) -> value mapping. |
| 430 | LocMap::iterator I = locInts.find(x: Idx); |
| 431 | if (!I.valid() || I.start() != Idx) |
| 432 | I.insert(a: Idx, b: Idx.getNextSlot(), y: 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. |
| 511 | class 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 | |
| 522 | public: |
| 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(Idx: loc); |
| 540 | loc = SI.canonicalizeIndex(Idx: 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 | |
| 554 | namespace llvm { |
| 555 | |
| 556 | class LiveDebugVariables::LDVImpl { |
| 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. |
| 574 | DenseMap<Register, std::vector<unsigned>> RegToPHIIdx; |
| 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. |
| 595 | SmallVector<std::unique_ptr<UserValue>, 8> userValues; |
| 596 | |
| 597 | /// All allocated UserLabel instances. |
| 598 | SmallVector<std::unique_ptr<UserLabel>, 2> userLabels; |
| 599 | |
| 600 | /// Map virtual register to eq class leader. |
| 601 | using VRMap = DenseMap<Register, UserValue *>; |
| 602 | VRMap virtRegToEqClass; |
| 603 | |
| 604 | /// Map to find existing UserValue instances. |
| 605 | using UVMap = DenseMap<DebugVariable, UserValue *>; |
| 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. |
| 635 | MachineBasicBlock::iterator handleDebugInstr(MachineInstr &MI, SlotIndex Idx); |
| 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 | |
| 659 | public: |
| 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. |
| 692 | void canonicalizeIndexes(const SlotIndexes &SI); |
| 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 | |
| 702 | LiveDebugVariables::LiveDebugVariables() = default; |
| 703 | LiveDebugVariables::~LiveDebugVariables() = default; |
| 704 | LiveDebugVariables::LiveDebugVariables(LiveDebugVariables &&) = default; |
| 705 | |
| 706 | } // namespace llvm |
| 707 | |
| 708 | static void printDebugLoc(const DebugLoc &DL, raw_ostream &, |
| 709 | const LLVMContext &Ctx) { |
| 710 | if (!DL) |
| 711 | return; |
| 712 | |
| 713 | auto *Scope = cast<DIScope>(Val: 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(DL: InlinedAtDL, CommentOS, Ctx); |
| 726 | CommentOS << " ]" ; |
| 727 | } |
| 728 | |
| 729 | static 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>(Val: Node)) { |
| 735 | Res = V->getName(); |
| 736 | Line = V->getLine(); |
| 737 | } else if (const auto *L = dyn_cast<const DILabel>(Val: 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(DL: InlinedAtDL, CommentOS&: OS, Ctx); |
| 749 | OS << "]" ; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { |
| 755 | OS << "!\"" ; |
| 756 | printExtendedName(OS, Node: Variable, DL: 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&: OS, TRI); |
| 774 | } |
| 775 | OS << '\n'; |
| 776 | } |
| 777 | |
| 778 | void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { |
| 779 | OS << "!\"" ; |
| 780 | printExtendedName(OS, Node: Label, DL: dl); |
| 781 | |
| 782 | OS << "\"\t" ; |
| 783 | OS << loc; |
| 784 | OS << '\n'; |
| 785 | } |
| 786 | |
| 787 | void LiveDebugVariables::LDVImpl::print(raw_ostream &OS) { |
| 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 | |
| 796 | void UserValue::mapVirtRegs(LiveDebugVariables::LDVImpl *LDV) { |
| 797 | for (const MachineOperand &MO : locations) |
| 798 | if (MO.isReg() && MO.getReg().isVirtual()) |
| 799 | LDV->mapVirtReg(VirtReg: MO.getReg(), EC: this); |
| 800 | } |
| 801 | |
| 802 | UserValue *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 | Elt: std::make_unique<UserValue>(args&: Var, args&: Fragment, args: DL, args&: allocator)); |
| 812 | UV = userValues.back().get(); |
| 813 | } |
| 814 | return UV; |
| 815 | } |
| 816 | |
| 817 | void LiveDebugVariables::LDVImpl::mapVirtReg(Register VirtReg, UserValue *EC) { |
| 818 | assert(VirtReg.isVirtual() && "Only map VirtRegs" ); |
| 819 | UserValue *&Leader = virtRegToEqClass[VirtReg]; |
| 820 | Leader = UserValue::merge(L1: Leader, L2: EC); |
| 821 | } |
| 822 | |
| 823 | UserValue *LiveDebugVariables::LDVImpl::lookupVirtReg(Register VirtReg) { |
| 824 | if (UserValue *UV = virtRegToEqClass.lookup(Val: VirtReg)) |
| 825 | return UV->getLeader(); |
| 826 | return nullptr; |
| 827 | } |
| 828 | |
| 829 | bool 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, Fragment: Expr->getFragmentInfo(), DL: MI.getDebugLoc()); |
| 889 | if (!Discard) |
| 890 | UV->addDef(Idx, |
| 891 | LocMOs: ArrayRef<MachineOperand>(MI.debug_operands().begin(), |
| 892 | MI.debug_operands().end()), |
| 893 | IsIndirect, IsList, Expr: *Expr); |
| 894 | else { |
| 895 | MachineOperand MO = MachineOperand::CreateReg(Reg: 0U, isDef: 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, LocMOs: UndefMOs, IsIndirect: false, IsList, Expr: *Expr); |
| 902 | } |
| 903 | return true; |
| 904 | } |
| 905 | |
| 906 | MachineBasicBlock::iterator |
| 907 | LiveDebugVariables::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(x: MI.getIterator()); |
| 921 | auto *MBB = MI.getParent(); |
| 922 | MI.removeFromParent(); |
| 923 | StashedDebugInstrs.push_back(Elt: {.MI: &MI, .Idx: Idx, .MBB: MBB}); |
| 924 | return NextInst; |
| 925 | } |
| 926 | |
| 927 | bool LiveDebugVariables::LDVImpl::handleDebugLabel(MachineInstr &MI, |
| 928 | SlotIndex Idx) { |
| 929 | // DBG_LABEL label |
| 930 | if (MI.getNumOperands() != 1 || !MI.getOperand(i: 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(L: Label, IA: DL->getInlinedAt(), Index: Idx)) { |
| 941 | Found = true; |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | if (!Found) |
| 946 | userLabels.push_back(Elt: std::make_unique<UserLabel>(args&: Label, args: DL, args&: Idx)); |
| 947 | |
| 948 | return true; |
| 949 | } |
| 950 | |
| 951 | bool 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: &MBB) |
| 968 | : LIS->getInstructionIndex(Instr: *std::prev(x: 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(MI&: *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(MI&: *MBBI, Idx)) || |
| 981 | (MBBI->isDebugLabel() && handleDebugLabel(MI&: *MBBI, Idx))) { |
| 982 | MBBI = MBB.erase(I: MBBI); |
| 983 | Changed = true; |
| 984 | } else |
| 985 | ++MBBI; |
| 986 | } while (MBBI != MBBE && MBBI->isDebugOrPseudoInstr()); |
| 987 | } |
| 988 | } |
| 989 | return Changed; |
| 990 | } |
| 991 | |
| 992 | void 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(index: Start); |
| 1000 | SlotIndex Stop = LIS.getMBBEndIdx(mbb: MBB); |
| 1001 | LocMap::iterator I = locInts.find(x: 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(Idx: 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(Elt: 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(a: Start, b: Stop, y: std::move(ExtDbgValue)); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | void 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(Range&: LocIntervals, |
| 1054 | P: [](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, |
| 1059 | SmallVector<std::pair<LiveInterval *, const VNInfo *>, 4>> |
| 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(Reg: 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(i: 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(Instr: *MI); |
| 1081 | LocMap::iterator I = locInts.find(x: Idx.getRegSlot(EC: true)); |
| 1082 | if (!I.valid() || I.value() != DbgValue) |
| 1083 | continue; |
| 1084 | |
| 1085 | if (!LIS.hasInterval(Reg: DstReg)) |
| 1086 | continue; |
| 1087 | LiveInterval *DstLI = &LIS.getInterval(Reg: DstReg); |
| 1088 | const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx: Idx.getRegSlot()); |
| 1089 | assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value" ); |
| 1090 | CopyValues[LocNo].push_back(Elt: std::make_pair(x&: DstLI, y&: 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(x: 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(Idx: 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(index: DstVNI->def); |
| 1120 | assert(CopyMI && CopyMI->isCopy() && "Bad copy value" ); |
| 1121 | unsigned NewLocNo = getLocationNo(LocMO: CopyMI->getOperand(i: 0)); |
| 1122 | NewValue = NewValue.changeLocNo(OldLocNo: 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(a: KilledAt, b: KilledAt.getNextSlot(), y: NewValue); |
| 1132 | NewDefs.push_back(Elt: std::make_pair(x&: KilledAt, y&: NewValue)); |
| 1133 | } |
| 1134 | |
| 1135 | void UserValue::computeIntervals(MachineRegisterInfo &MRI, |
| 1136 | const TargetRegisterInfo &TRI, |
| 1137 | LiveIntervals &LIS, LexicalScopes &LS) { |
| 1138 | SmallVector<std::pair<SlotIndex, DbgVariableValue>, 16> Defs; |
| 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(Elt: std::make_pair(x: I.start(), y: 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(Reg: LocMO.getReg())) { |
| 1161 | LI = &LIS.getInterval(Reg: 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, LiveIntervalInfo&: LIs, Kills, LIS); |
| 1170 | |
| 1171 | if (Kills) { |
| 1172 | SmallVector<std::pair<unsigned, LiveInterval *>, 2> KilledLocIntervals; |
| 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(Reg: LocMO.getReg()); |
| 1181 | KilledLocIntervals.push_back(Elt: {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, LocIntervals&: KilledLocIntervals, KilledAt: Kills->first, NewDefs&: 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: 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(Instr: *Range.first); |
| 1229 | SlotIndex REnd = LIS.getInstructionIndex(Instr: *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(MI: *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(a: RStart, b: IStop, y: DbgValue); |
| 1252 | } |
| 1253 | |
| 1254 | // Advance I so that I.stop() >= RStart, and check for overlap. |
| 1255 | I.advanceTo(x: 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(V: 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(x: 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 | |
| 1284 | void LiveDebugVariables::LDVImpl::computeIntervals() { |
| 1285 | LexicalScopes LS; |
| 1286 | LS.scanFunction(*MF); |
| 1287 | |
| 1288 | for (const auto &UV : userValues) { |
| 1289 | UV->computeIntervals(MRI&: MF->getRegInfo(), TRI: *TRI, LIS&: *LIS, LS); |
| 1290 | UV->mapVirtRegs(LDV: this); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | bool LiveDebugVariables::LDVImpl::runOnMachineFunction(MachineFunction &mf, |
| 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: MBB); |
| 1315 | PHIValPos VP = {.SI: SI, .Reg: Reg, .SubReg: SubReg}; |
| 1316 | PHIValToPos.insert(x: std::make_pair(x: PHIIt.first, y&: VP)); |
| 1317 | RegToPHIIdx[Reg].push_back(x: PHIIt.first); |
| 1318 | } |
| 1319 | |
| 1320 | ModifiedMF = Changed; |
| 1321 | return Changed; |
| 1322 | } |
| 1323 | |
| 1324 | static void removeDebugInstrs(MachineFunction &mf) { |
| 1325 | for (MachineBasicBlock &MBB : mf) { |
| 1326 | for (MachineInstr &MI : llvm::make_early_inc_range(Range&: MBB)) |
| 1327 | if (MI.isDebugInstr()) |
| 1328 | MBB.erase(I: &MI); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | bool LiveDebugVariablesWrapperLegacy::runOnMachineFunction( |
| 1333 | MachineFunction &mf) { |
| 1334 | auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS(); |
| 1335 | |
| 1336 | Impl = std::make_unique<LiveDebugVariables>(); |
| 1337 | Impl->analyze(MF&: mf, LIS); |
| 1338 | return false; |
| 1339 | } |
| 1340 | |
| 1341 | AnalysisKey LiveDebugVariablesAnalysis::Key; |
| 1342 | |
| 1343 | LiveDebugVariables |
| 1344 | LiveDebugVariablesAnalysis::run(MachineFunction &MF, |
| 1345 | MachineFunctionAnalysisManager &MFAM) { |
| 1346 | MFPropsModifier _(*this, MF); |
| 1347 | |
| 1348 | auto *LIS = &MFAM.getResult<LiveIntervalsAnalysis>(IR&: MF); |
| 1349 | LiveDebugVariables LDV; |
| 1350 | LDV.analyze(MF, LIS); |
| 1351 | return LDV; |
| 1352 | } |
| 1353 | |
| 1354 | PreservedAnalyses |
| 1355 | LiveDebugVariablesPrinterPass::run(MachineFunction &MF, |
| 1356 | MachineFunctionAnalysisManager &MFAM) { |
| 1357 | auto &LDV = MFAM.getResult<LiveDebugVariablesAnalysis>(IR&: MF); |
| 1358 | LDV.print(OS); |
| 1359 | return PreservedAnalyses::all(); |
| 1360 | } |
| 1361 | |
| 1362 | void LiveDebugVariables::releaseMemory() { |
| 1363 | if (PImpl) |
| 1364 | PImpl->clear(); |
| 1365 | } |
| 1366 | |
| 1367 | bool LiveDebugVariables::invalidate( |
| 1368 | MachineFunction &, const PreservedAnalyses &PA, |
| 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 | |
| 1377 | void LiveDebugVariables::analyze(MachineFunction &MF, LiveIntervals *LIS) { |
| 1378 | if (!EnableLDV) |
| 1379 | return; |
| 1380 | if (!MF.getFunction().getSubprogram()) { |
| 1381 | removeDebugInstrs(mf&: MF); |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | PImpl.reset(p: 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&: MF, InstrRef); |
| 1391 | } |
| 1392 | |
| 1393 | //===----------------------------------------------------------------------===// |
| 1394 | // Live Range Splitting |
| 1395 | //===----------------------------------------------------------------------===// |
| 1396 | |
| 1397 | bool |
| 1398 | UserValue::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(Reg: 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(x: LI->beginIndex()); |
| 1417 | if (!LocMapI.valid()) |
| 1418 | continue; |
| 1419 | LiveInterval::iterator LII = LI->advanceTo(I: LI->begin(), Pos: 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(I: LII, Pos: 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(LocNo: OldLocNo) && |
| 1429 | LII->start < LocMapI.stop()) { |
| 1430 | // Overlapping correct location. Allocate NewLocNo now. |
| 1431 | if (NewLocNo == UndefLocNo) { |
| 1432 | MachineOperand MO = MachineOperand::CreateReg(Reg: LI->reg(), isDef: false); |
| 1433 | MO.setSubReg(locations[OldLocNo].getSubReg()); |
| 1434 | NewLocNo = getLocationNo(LocMO: 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(a: LStart, b: LocMapI.start(), y: OldDbgValue); |
| 1454 | ++LocMapI; |
| 1455 | assert(LocMapI.valid() && "Unexpected coalescing" ); |
| 1456 | } |
| 1457 | if (LStop > LocMapI.stop()) { |
| 1458 | ++LocMapI; |
| 1459 | LocMapI.insert(a: LII->end, b: LStop, y: 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(x: LII->start); |
| 1469 | } else { |
| 1470 | ++LocMapI; |
| 1471 | if (!LocMapI.valid()) |
| 1472 | break; |
| 1473 | LII = LI->advanceTo(I: LII, Pos: 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(LocNo: OldLocNo); |
| 1489 | |
| 1490 | LLVM_DEBUG({ |
| 1491 | dbgs() << "Split result: \t" ; |
| 1492 | print(dbgs(), nullptr); |
| 1493 | }); |
| 1494 | return DidChange; |
| 1495 | } |
| 1496 | |
| 1497 | bool |
| 1498 | UserValue::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(OldLocNo: LocNo, NewRegs, LIS); |
| 1509 | } |
| 1510 | return DidChange; |
| 1511 | } |
| 1512 | |
| 1513 | void LiveDebugVariables::LDVImpl::splitPHIRegister(Register OldReg, |
| 1514 | ArrayRef<Register> NewRegs) { |
| 1515 | auto RegIt = RegToPHIIdx.find(Val: 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(x: 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(Reg: NewReg); |
| 1530 | auto LII = LI.find(Pos: 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(x: std::make_pair(x&: NewReg, y&: 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(I: RegIt); |
| 1548 | for (auto &RegAndInstr : NewRegIdxes) |
| 1549 | RegToPHIIdx[RegAndInstr.first].push_back(x: RegAndInstr.second); |
| 1550 | } |
| 1551 | |
| 1552 | void LiveDebugVariables::LDVImpl::splitRegister(Register OldReg, |
| 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(VirtReg: OldReg); UV; UV = UV->getNext()) |
| 1561 | DidChange |= UV->splitRegister(OldReg, NewRegs, LIS&: *LIS); |
| 1562 | |
| 1563 | if (!DidChange) |
| 1564 | return; |
| 1565 | |
| 1566 | // Map all of the new virtual registers. |
| 1567 | UserValue *UV = lookupVirtReg(VirtReg: OldReg); |
| 1568 | for (Register NewReg : NewRegs) |
| 1569 | mapVirtReg(VirtReg: NewReg, EC: UV); |
| 1570 | } |
| 1571 | |
| 1572 | void LiveDebugVariables:: |
| 1573 | splitRegister(Register OldReg, ArrayRef<Register> NewRegs, LiveIntervals &LIS) { |
| 1574 | if (PImpl) |
| 1575 | PImpl->splitRegister(OldReg, NewRegs); |
| 1576 | } |
| 1577 | |
| 1578 | //===----------------------------------------------------------------------===// |
| 1579 | // Stale Index Canonicalization |
| 1580 | //===----------------------------------------------------------------------===// |
| 1581 | |
| 1582 | void UserValue::canonicalizeIndexes(const SlotIndexes &SI) { |
| 1583 | unsigned NumStale = 0; |
| 1584 | for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) |
| 1585 | NumStale += SI.isStaleIndex(Idx: I.start()) + SI.isStaleIndex(Idx: 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(Idx: Canon)) |
| 1600 | trimmedDefs.insert(V: 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 | }; |
| 1612 | SmallVector<CanonicalInterval, 8> Intervals; |
| 1613 | |
| 1614 | for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { |
| 1615 | SlotIndex Start = SI.canonicalizeIndex(Idx: I.start()); |
| 1616 | SlotIndex Stop = SI.canonicalizeIndex(Idx: 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(a: Prev.Stop, b: Stop); |
| 1629 | Prev.Value = I.value(); |
| 1630 | ++NumMergedIntervals; |
| 1631 | continue; |
| 1632 | } |
| 1633 | Prev.Stop = std::min(a: Prev.Stop, b: Start); |
| 1634 | } |
| 1635 | Intervals.push_back(Elt: {.Start: Start, .Stop: Stop, .Value: 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(a: Interval.Start, b: Interval.Stop, y: 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 | |
| 1664 | void LiveDebugVariables::LDVImpl::canonicalizeIndexes(const SlotIndexes &SI) { |
| 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(Idx: Stashed.Idx)) |
| 1676 | continue; |
| 1677 | ++NumStaleIndexes; |
| 1678 | Stashed.Idx = SI.canonicalizeIndex(Idx: 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 | |
| 1692 | void LiveDebugVariables::canonicalizeIndexes(const SlotIndexes &SI) { |
| 1693 | if (PImpl) |
| 1694 | PImpl->canonicalizeIndexes(SI); |
| 1695 | } |
| 1696 | |
| 1697 | void 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. |
| 1709 | MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations; |
| 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: VirtReg) && VRM.hasPhys(virtReg: 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(Reg: VRM.getPhys(virtReg: VirtReg), TRI); |
| 1723 | } else if (VRM.getStackSlot(virtReg: 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(Reg: VirtReg); |
| 1728 | bool Success = TII.getStackSlotRange(RC: TRC, SubIdx: Loc.getSubReg(), Size&: SpillSize, |
| 1729 | Offset&: SpillOffset, MF); |
| 1730 | |
| 1731 | // FIXME: Invalidate the location if the offset couldn't be calculated. |
| 1732 | (void)Success; |
| 1733 | |
| 1734 | Loc = MachineOperand::CreateFI(Idx: VRM.getStackSlot(virtReg: VirtReg)); |
| 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(KV: {Loc, {Spilled, SpillOffset}}); |
| 1745 | unsigned NewLocNo = std::distance(first: NewLocations.begin(), last: 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(args&: Spilled, args&: SpillOffset) = Pair.second; |
| 1756 | locations.push_back(Elt: Pair.first); |
| 1757 | if (Spilled) { |
| 1758 | unsigned NewLocNo = std::distance(first: &*NewLocations.begin(), last: &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. |
| 1774 | static MachineBasicBlock::iterator findInsertLocation(MachineBasicBlock *MBB, |
| 1775 | SlotIndex Idx, |
| 1776 | LiveIntervals &LIS, |
| 1777 | InsertLocCache &Cache) { |
| 1778 | SlotIndex Start = LIS.getMBBStartIdx(mbb: MBB); |
| 1779 | Idx = Idx.getBaseIndex(); |
| 1780 | |
| 1781 | // Try to find an insert location by going backwards from Idx. |
| 1782 | MachineInstr *MI; |
| 1783 | while (!(MI = LIS.getInstructionFromIndex(index: 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. |
| 1800 | MachineBasicBlock::iterator BeginIt; |
| 1801 | auto MapIt = Cache.BlockStart.find(Val: MBB); |
| 1802 | if (MapIt == Cache.BlockStart.end()) |
| 1803 | BeginIt = MBB->begin(); |
| 1804 | else |
| 1805 | BeginIt = std::next(x: MapIt->second); |
| 1806 | auto I = MBB->SkipPHIsLabelsAndDebug(I: BeginIt); |
| 1807 | if (I != BeginIt) |
| 1808 | Cache.BlockStart[MBB] = std::prev(x: 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. |
| 1821 | MachineBasicBlock::iterator Prev(MI); |
| 1822 | if (auto CacheIt = Cache.AfterInstr.find(Val: MI); |
| 1823 | CacheIt != Cache.AfterInstr.end()) |
| 1824 | Prev = CacheIt->second; |
| 1825 | auto BeginIt = std::next(x: Prev); |
| 1826 | auto I = skipDebugInstructionsForward(It: BeginIt, End: MBB->end()); |
| 1827 | if (I != BeginIt) |
| 1828 | Cache.AfterInstr[MI] = std::prev(x: 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). |
| 1834 | static MachineBasicBlock::iterator |
| 1835 | findNextInsertLocation(MachineBasicBlock *MBB, MachineBasicBlock::iterator I, |
| 1836 | SlotIndex StopIdx, ArrayRef<MachineOperand> LocMOs, |
| 1837 | LiveIntervals &LIS, const TargetRegisterInfo &TRI) { |
| 1838 | SmallVector<Register, 4> Regs; |
| 1839 | for (const MachineOperand &LocMO : LocMOs) |
| 1840 | if (LocMO.isReg()) |
| 1841 | Regs.push_back(Elt: 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(Instr: *I) && |
| 1848 | SlotIndex::isEarlierEqualInstr(A: StopIdx, B: LIS.getInstructionIndex(Instr: *I))) |
| 1849 | break; |
| 1850 | if (any_of(Range&: Regs, P: [&I, &TRI](Register &Reg) { |
| 1851 | return I->definesRegister(Reg, TRI: &TRI); |
| 1852 | })) |
| 1853 | // The insert location is directly after the instruction/bundle. |
| 1854 | return std::next(x: I); |
| 1855 | ++I; |
| 1856 | } |
| 1857 | return MBB->end(); |
| 1858 | } |
| 1859 | |
| 1860 | void 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: &*MBB); |
| 1868 | // Only search within the current MBB. |
| 1869 | StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx; |
| 1870 | MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx: StartIdx, LIS, Cache); |
| 1871 | // Undef values don't exist in locations so create new "noreg" register MOs |
| 1872 | // for them. See getLocationNo(). |
| 1873 | SmallVector<MachineOperand, 8> MOs; |
| 1874 | if (DbgValue.isUndef()) { |
| 1875 | MOs.assign(NumElts: DbgValue.loc_nos().size(), |
| 1876 | Elt: MachineOperand::CreateReg( |
| 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(Elt: locations[LocNo]); |
| 1884 | } |
| 1885 | |
| 1886 | ++NumInsertedDebugValues; |
| 1887 | |
| 1888 | assert(cast<DILocalVariable>(Variable) |
| 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, Flags: DIExprFlags, Offset: SpillOffsets[I]); |
| 1906 | IsIndirect = true; |
| 1907 | } else { |
| 1908 | SmallVector<uint64_t, 4> Ops; |
| 1909 | DIExpression::appendOffset(Ops, Offset: SpillOffsets[I]); |
| 1910 | Ops.push_back(Elt: dwarf::DW_OP_deref); |
| 1911 | Expr = DIExpression::appendOpsToArg(Expr, Ops, ArgNo: 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(BB&: *MBB, I, DL: getDebugLoc(), MCID: TII.get(Opcode: 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, LocMOs: MOs, LIS, TRI); |
| 1928 | } while (I != MBB->end()); |
| 1929 | } |
| 1930 | |
| 1931 | void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx, |
| 1932 | LiveIntervals &LIS, const TargetInstrInfo &TII, |
| 1933 | InsertLocCache &Cache) { |
| 1934 | MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS, Cache); |
| 1935 | ++NumInsertedDebugLabels; |
| 1936 | BuildMI(BB&: *MBB, I, MIMD: getDebugLoc(), MCID: TII.get(Opcode: TargetOpcode::DBG_LABEL)) |
| 1937 | .addMetadata(MD: Label); |
| 1938 | } |
| 1939 | |
| 1940 | void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, |
| 1941 | const TargetInstrInfo &TII, |
| 1942 | const TargetRegisterInfo &TRI, |
| 1943 | const SpillOffsetMap &SpillOffsets, |
| 1944 | InsertLocCache &Cache) { |
| 1945 | MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); |
| 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(Val: LocNo) : SpillOffsets.end(); |
| 1957 | bool Spilled = SpillIt != SpillOffsets.end(); |
| 1958 | SpilledLocs.push_back(Elt: Spilled); |
| 1959 | LocSpillOffsets.push_back(Elt: 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(V: Start)) |
| 1966 | Start = Start.getPrevIndex(); |
| 1967 | |
| 1968 | LLVM_DEBUG(auto &dbg = dbgs(); dbg << "\t[" << Start << ';' << Stop << "):" ; |
| 1969 | DbgValue.printLocNos(dbg)); |
| 1970 | MachineFunction::iterator MBB = LIS.getMBBFromIndex(index: Start)->getIterator(); |
| 1971 | SlotIndex MBBEnd = LIS.getMBBEndIdx(mbb: &*MBB); |
| 1972 | |
| 1973 | LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); |
| 1974 | insertDebugValue(MBB: &*MBB, StartIdx: Start, StopIdx: Stop, DbgValue, LocSpills: SpilledLocs, SpillOffsets: 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: &*MBB); |
| 1984 | LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); |
| 1985 | insertDebugValue(MBB: &*MBB, StartIdx: Start, StopIdx: Stop, DbgValue, LocSpills: SpilledLocs, |
| 1986 | SpillOffsets: LocSpillOffsets, LIS, TII, TRI, Cache); |
| 1987 | } |
| 1988 | LLVM_DEBUG(dbgs() << '\n'); |
| 1989 | if (MBB == MFEnd) |
| 1990 | break; |
| 1991 | |
| 1992 | ++I; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII, |
| 1997 | InsertLocCache &Cache) { |
| 1998 | LLVM_DEBUG(dbgs() << "\t" << loc); |
| 1999 | MachineFunction::iterator MBB = LIS.getMBBFromIndex(index: loc)->getIterator(); |
| 2000 | |
| 2001 | LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB)); |
| 2002 | insertDebugLabel(MBB: &*MBB, Idx: loc, LIS, TII, Cache); |
| 2003 | |
| 2004 | LLVM_DEBUG(dbgs() << '\n'); |
| 2005 | } |
| 2006 | |
| 2007 | void LiveDebugVariables::LDVImpl::emitDebugValues(VirtRegMap *VRM) { |
| 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(SI: *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&: *VRM, MF: *MF, TII: *TII, TRI: *TRI, SpillOffsets); |
| 2021 | userValue->emitDebugValues(VRM, LIS&: *LIS, TII: *TII, TRI: *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&: *LIS, TII: *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(index: Slot); |
| 2041 | if (VRM->isAssignedReg(virtReg: Reg) && VRM->hasPhys(virtReg: Reg)) { |
| 2042 | unsigned PhysReg = VRM->getPhys(virtReg: Reg); |
| 2043 | if (SubReg != 0) |
| 2044 | PhysReg = TRI->getSubReg(Reg: PhysReg, Idx: SubReg); |
| 2045 | |
| 2046 | auto Builder = BuildMI(BB&: *OrigMBB, I: OrigMBB->begin(), MIMD: DebugLoc(), |
| 2047 | MCID: TII->get(Opcode: TargetOpcode::DBG_PHI)); |
| 2048 | Builder.addReg(RegNo: PhysReg); |
| 2049 | Builder.addImm(Val: InstNum); |
| 2050 | } else if (VRM->getStackSlot(virtReg: 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(RC: *TRC); |
| 2056 | if (SubReg) |
| 2057 | regSizeInBits = TRI->getSubRegIdxSize(Idx: 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(RC: TRC, SubIdx: SubReg, Size&: SpillSize, Offset&: SpillOffset, MF: *MF); |
| 2064 | |
| 2065 | if (Success && SpillOffset == 0) { |
| 2066 | auto Builder = BuildMI(BB&: *OrigMBB, I: OrigMBB->begin(), MIMD: DebugLoc(), |
| 2067 | MCID: TII->get(Opcode: TargetOpcode::DBG_PHI)); |
| 2068 | Builder.addFrameIndex(Idx: VRM->getStackSlot(virtReg: Reg)); |
| 2069 | Builder.addImm(Val: 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(Val: 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(I: 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(x: 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(I: InsertPos, MI: NextItem->MI); |
| 2112 | StashIt = NextItem; |
| 2113 | NextItem = std::next(x: 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: MBB)) { |
| 2120 | MachineBasicBlock::iterator InsertPos = |
| 2121 | findInsertLocation(MBB, Idx, LIS&: *LIS, Cache); |
| 2122 | EmitInstsHere(InsertPos); |
| 2123 | continue; |
| 2124 | } |
| 2125 | |
| 2126 | if (MachineInstr *Pos = Slots->getInstructionFromIndex(index: Idx)) { |
| 2127 | // Insert at the end of any debug instructions. |
| 2128 | auto PostDebug = std::next(x: MachineBasicBlock::iterator(Pos)); |
| 2129 | PostDebug = skipDebugInstructionsForward(It: PostDebug, End: 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: MBB); |
| 2135 | for (; Idx < End; Idx = Slots->getNextNonNullIndex(Index: Idx)) { |
| 2136 | Pos = Slots->getInstructionFromIndex(index: 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 | |
| 2157 | void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { |
| 2158 | if (PImpl) |
| 2159 | PImpl->emitDebugValues(VRM); |
| 2160 | } |
| 2161 | |
| 2162 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 2163 | LLVM_DUMP_METHOD void LiveDebugVariables::dump() const { print(dbgs()); } |
| 2164 | #endif |
| 2165 | |
| 2166 | void LiveDebugVariables::print(raw_ostream &OS) const { |
| 2167 | if (PImpl) |
| 2168 | PImpl->print(OS); |
| 2169 | } |
| 2170 | |