1//===- InstrRefBasedImpl.cpp - Tracking Debug Value MIs -------------------===//
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/// \file InstrRefBasedImpl.cpp
9///
10/// This is a separate implementation of LiveDebugValues, see
11/// LiveDebugValues.cpp and VarLocBasedImpl.cpp for more information.
12///
13/// This pass propagates variable locations between basic blocks, resolving
14/// control flow conflicts between them. The problem is SSA construction, where
15/// each debug instruction assigns the *value* that a variable has, and every
16/// instruction where the variable is in scope uses that variable. The resulting
17/// map of instruction-to-value is then translated into a register (or spill)
18/// location for each variable over each instruction.
19///
20/// The primary difference from normal SSA construction is that we cannot
21/// _create_ PHI values that contain variable values. CodeGen has already
22/// completed, and we can't alter it just to make debug-info complete. Thus:
23/// we can identify function positions where we would like a PHI value for a
24/// variable, but must search the MachineFunction to see whether such a PHI is
25/// available. If no such PHI exists, the variable location must be dropped.
26///
27/// To achieve this, we perform two kinds of analysis. First, we identify
28/// every value defined by every instruction (ignoring those that only move
29/// another value), then re-compute an SSA-form representation of the
30/// MachineFunction, using value propagation to eliminate any un-necessary
31/// PHI values. This gives us a map of every value computed in the function,
32/// and its location within the register file / stack.
33///
34/// Secondly, for each variable we perform the same analysis, where each debug
35/// instruction is considered a def, and every instruction where the variable
36/// is in lexical scope as a use. Value propagation is used again to eliminate
37/// any un-necessary PHIs. This gives us a map of each variable to the value
38/// it should have in a block.
39///
40/// Once both are complete, we have two maps for each block:
41/// * Variables to the values they should have,
42/// * Values to the register / spill slot they are located in.
43/// After which we can marry-up variable values with a location, and emit
44/// DBG_VALUE instructions specifying those locations. Variable locations may
45/// be dropped in this process due to the desired variable value not being
46/// resident in any machine location, or because there is no PHI value in any
47/// location that accurately represents the desired value. The building of
48/// location lists for each block is left to DbgEntityHistoryCalculator.
49///
50/// This pass is kept efficient because the size of the first SSA problem
51/// is proportional to the working-set size of the function, which the compiler
52/// tries to keep small. (It's also proportional to the number of blocks).
53/// Additionally, we repeatedly perform the second SSA problem analysis with
54/// only the variables and blocks in a single lexical scope, exploiting their
55/// locality.
56///
57/// ### Terminology
58///
59/// A machine location is a register or spill slot, a value is something that's
60/// defined by an instruction or PHI node, while a variable value is the value
61/// assigned to a variable. A variable location is a machine location, that must
62/// contain the appropriate variable value. A value that is a PHI node is
63/// occasionally called an mphi.
64///
65/// The first SSA problem is the "machine value location" problem,
66/// because we're determining which machine locations contain which values.
67/// The "locations" are constant: what's unknown is what value they contain.
68///
69/// The second SSA problem (the one for variables) is the "variable value
70/// problem", because it's determining what values a variable has, rather than
71/// what location those values are placed in.
72///
73/// TODO:
74/// Overlapping fragments
75/// Entry values
76/// Add back DEBUG statements for debugging this
77/// Collect statistics
78///
79//===----------------------------------------------------------------------===//
80
81#include "llvm/ADT/DenseMap.h"
82#include "llvm/ADT/PostOrderIterator.h"
83#include "llvm/ADT/STLExtras.h"
84#include "llvm/ADT/SmallPtrSet.h"
85#include "llvm/ADT/SmallSet.h"
86#include "llvm/ADT/SmallVector.h"
87#include "llvm/BinaryFormat/Dwarf.h"
88#include "llvm/CodeGen/LexicalScopes.h"
89#include "llvm/CodeGen/MachineBasicBlock.h"
90#include "llvm/CodeGen/MachineDominators.h"
91#include "llvm/CodeGen/MachineFrameInfo.h"
92#include "llvm/CodeGen/MachineFunction.h"
93#include "llvm/CodeGen/MachineInstr.h"
94#include "llvm/CodeGen/MachineInstrBuilder.h"
95#include "llvm/CodeGen/MachineInstrBundle.h"
96#include "llvm/CodeGen/MachineMemOperand.h"
97#include "llvm/CodeGen/MachineOperand.h"
98#include "llvm/CodeGen/PseudoSourceValue.h"
99#include "llvm/CodeGen/TargetFrameLowering.h"
100#include "llvm/CodeGen/TargetInstrInfo.h"
101#include "llvm/CodeGen/TargetLowering.h"
102#include "llvm/CodeGen/TargetRegisterInfo.h"
103#include "llvm/CodeGen/TargetSubtargetInfo.h"
104#include "llvm/Config/llvm-config.h"
105#include "llvm/IR/DebugInfoMetadata.h"
106#include "llvm/IR/DebugLoc.h"
107#include "llvm/IR/Function.h"
108#include "llvm/MC/MCRegisterInfo.h"
109#include "llvm/Support/Casting.h"
110#include "llvm/Support/Compiler.h"
111#include "llvm/Support/Debug.h"
112#include "llvm/Support/GenericIteratedDominanceFrontier.h"
113#include "llvm/Support/TypeSize.h"
114#include "llvm/Support/raw_ostream.h"
115#include "llvm/Target/TargetMachine.h"
116#include "llvm/Transforms/Utils/SSAUpdaterImpl.h"
117#include <algorithm>
118#include <cassert>
119#include <climits>
120#include <cstdint>
121#include <functional>
122#include <queue>
123#include <tuple>
124#include <utility>
125#include <vector>
126
127#include "InstrRefBasedImpl.h"
128#include "LiveDebugValues.h"
129#include <optional>
130
131using namespace llvm;
132using namespace LiveDebugValues;
133
134// SSAUpdaterImple sets DEBUG_TYPE, change it.
135#undef DEBUG_TYPE
136#define DEBUG_TYPE "livedebugvalues"
137
138// Act more like the VarLoc implementation, by propagating some locations too
139// far and ignoring some transfers.
140static cl::opt<bool> EmulateOldLDV("emulate-old-livedebugvalues", cl::Hidden,
141 cl::desc("Act like old LiveDebugValues did"),
142 cl::init(Val: false));
143
144// Limit for the maximum number of stack slots we should track, past which we
145// will ignore any spills. InstrRefBasedLDV gathers detailed information on all
146// stack slots which leads to high memory consumption, and in some scenarios
147// (such as asan with very many locals) the working set of the function can be
148// very large, causing many spills. In these scenarios, it is very unlikely that
149// the developer has hundreds of variables live at the same time that they're
150// carefully thinking about -- instead, they probably autogenerated the code.
151// When this happens, gracefully stop tracking excess spill slots, rather than
152// consuming all the developer's memory.
153static cl::opt<unsigned>
154 StackWorkingSetLimit("livedebugvalues-max-stack-slots", cl::Hidden,
155 cl::desc("livedebugvalues-stack-ws-limit"),
156 cl::init(Val: 250));
157
158DbgOpID DbgOpID::UndefID = DbgOpID(0xffffffff);
159
160/// Tracker for converting machine value locations and variable values into
161/// variable locations (the output of LiveDebugValues), recorded as DBG_VALUEs
162/// specifying block live-in locations and transfers within blocks.
163///
164/// Operating on a per-block basis, this class takes a (pre-loaded) MLocTracker
165/// and must be initialized with the set of variable values that are live-in to
166/// the block. The caller then repeatedly calls process(). TransferTracker picks
167/// out variable locations for the live-in variable values (if there _is_ a
168/// location) and creates the corresponding DBG_VALUEs. Then, as the block is
169/// stepped through, transfers of values between machine locations are
170/// identified and if profitable, a DBG_VALUE created.
171///
172/// This is where debug use-before-defs would be resolved: a variable with an
173/// unavailable value could materialize in the middle of a block, when the
174/// value becomes available. Or, we could detect clobbers and re-specify the
175/// variable in a backup location. (XXX these are unimplemented).
176class TransferTracker {
177public:
178 const TargetInstrInfo *TII;
179 const TargetLowering *TLI;
180 /// This machine location tracker is assumed to always contain the up-to-date
181 /// value mapping for all machine locations. TransferTracker only reads
182 /// information from it. (XXX make it const?)
183 MLocTracker *MTracker;
184 MachineFunction &MF;
185 const DebugVariableMap &DVMap;
186 bool ShouldEmitDebugEntryValues;
187
188 /// Record of all changes in variable locations at a block position. Awkwardly
189 /// we allow inserting either before or after the point: MBB != nullptr
190 /// indicates it's before, otherwise after.
191 struct Transfer {
192 MachineBasicBlock::instr_iterator Pos; /// Position to insert DBG_VALUes
193 MachineBasicBlock *MBB; /// non-null if we should insert after.
194 /// Vector of DBG_VALUEs to insert. Store with their DebugVariableID so that
195 /// they can be sorted into a stable order for emission at a later time.
196 SmallVector<std::pair<DebugVariableID, MachineInstr *>, 4> Insts;
197 };
198
199 /// Stores the resolved operands (machine locations and constants) and
200 /// qualifying meta-information needed to construct a concrete DBG_VALUE-like
201 /// instruction.
202 struct ResolvedDbgValue {
203 SmallVector<ResolvedDbgOp> Ops;
204 DbgValueProperties Properties;
205
206 ResolvedDbgValue(SmallVectorImpl<ResolvedDbgOp> &Ops,
207 DbgValueProperties Properties)
208 : Ops(Ops.begin(), Ops.end()), Properties(Properties) {}
209
210 /// Returns all the LocIdx values used in this struct, in the order in which
211 /// they appear as operands in the debug value; may contain duplicates.
212 auto loc_indices() const {
213 return map_range(
214 C: make_filter_range(
215 Range: Ops, Pred: [](const ResolvedDbgOp &Op) { return !Op.IsConst; }),
216 F: [](const ResolvedDbgOp &Op) { return Op.Loc; });
217 }
218 };
219
220 /// Collection of transfers (DBG_VALUEs) to be inserted.
221 SmallVector<Transfer, 32> Transfers;
222
223 /// Local cache of what-value-is-in-what-LocIdx. Used to identify differences
224 /// between TransferTrackers view of variable locations and MLocTrackers. For
225 /// example, MLocTracker observes all clobbers, but TransferTracker lazily
226 /// does not.
227 SmallVector<ValueIDNum, 32> VarLocs;
228
229 /// Map from LocIdxes to which DebugVariables are based that location.
230 /// Mantained while stepping through the block. Not accurate if
231 /// VarLocs[Idx] != MTracker->LocIdxToIDNum[Idx].
232 DenseMap<LocIdx, SmallSet<DebugVariableID, 4>> ActiveMLocs;
233
234 /// Map from DebugVariable to it's current location and qualifying meta
235 /// information. To be used in conjunction with ActiveMLocs to construct
236 /// enough information for the DBG_VALUEs for a particular LocIdx.
237 DenseMap<DebugVariableID, ResolvedDbgValue> ActiveVLocs;
238
239 /// Temporary cache of DBG_VALUEs to be entered into the Transfers collection.
240 SmallVector<std::pair<DebugVariableID, MachineInstr *>, 4> PendingDbgValues;
241
242 /// Record of a use-before-def: created when a value that's live-in to the
243 /// current block isn't available in any machine location, but it will be
244 /// defined in this block.
245 struct UseBeforeDef {
246 /// Value of this variable, def'd in block.
247 SmallVector<DbgOp> Values;
248 /// Identity of this variable.
249 DebugVariableID VarID;
250 /// Additional variable properties.
251 DbgValueProperties Properties;
252 UseBeforeDef(ArrayRef<DbgOp> Values, DebugVariableID VarID,
253 const DbgValueProperties &Properties)
254 : Values(Values), VarID(VarID), Properties(Properties) {}
255 };
256
257 /// Map from instruction index (within the block) to the set of UseBeforeDefs
258 /// that become defined at that instruction.
259 DenseMap<unsigned, SmallVector<UseBeforeDef, 1>> UseBeforeDefs;
260
261 /// The set of variables that are in UseBeforeDefs and can become a location
262 /// once the relevant value is defined. An element being erased from this
263 /// collection prevents the use-before-def materializing.
264 DenseSet<DebugVariableID> UseBeforeDefVariables;
265
266 const TargetRegisterInfo &TRI;
267 const BitVector &CalleeSavedRegs;
268
269 TransferTracker(const TargetInstrInfo *TII, MLocTracker *MTracker,
270 MachineFunction &MF, const DebugVariableMap &DVMap,
271 const TargetRegisterInfo &TRI,
272 const BitVector &CalleeSavedRegs,
273 bool ShouldEmitDebugEntryValues)
274 : TII(TII), MTracker(MTracker), MF(MF), DVMap(DVMap), TRI(TRI),
275 CalleeSavedRegs(CalleeSavedRegs) {
276 TLI = MF.getSubtarget().getTargetLowering();
277 this->ShouldEmitDebugEntryValues = ShouldEmitDebugEntryValues;
278 }
279
280 bool isCalleeSaved(LocIdx L) const {
281 unsigned Reg = MTracker->LocIdxToLocID[L];
282 if (Reg >= MTracker->NumRegs)
283 return false;
284 for (MCRegAliasIterator RAI(Reg, &TRI, true); RAI.isValid(); ++RAI)
285 if (CalleeSavedRegs.test(Idx: (*RAI).id()))
286 return true;
287 return false;
288 };
289
290 // An estimate of the expected lifespan of values at a machine location, with
291 // a greater value corresponding to a longer expected lifespan, i.e. spill
292 // slots generally live longer than callee-saved registers which generally
293 // live longer than non-callee-saved registers. The minimum value of 0
294 // corresponds to an illegal location that cannot have a "lifespan" at all.
295 enum class LocationQuality : unsigned char {
296 Illegal = 0,
297 Register,
298 CalleeSavedRegister,
299 SpillSlot,
300 Best = SpillSlot
301 };
302
303 class LocationAndQuality {
304 unsigned Location : 24;
305 unsigned Quality : 8;
306
307 public:
308 LocationAndQuality() : Location(0), Quality(0) {}
309 LocationAndQuality(LocIdx L, LocationQuality Q)
310 : Location(L.asU64()), Quality(static_cast<unsigned>(Q)) {}
311 LocIdx getLoc() const {
312 if (!Quality)
313 return LocIdx::MakeIllegalLoc();
314 return LocIdx(Location);
315 }
316 LocationQuality getQuality() const { return LocationQuality(Quality); }
317 bool isIllegal() const { return !Quality; }
318 bool isBest() const { return getQuality() == LocationQuality::Best; }
319 };
320
321 using ValueLocPair = std::pair<ValueIDNum, LocationAndQuality>;
322
323 static inline bool ValueToLocSort(const ValueLocPair &A,
324 const ValueLocPair &B) {
325 return A.first < B.first;
326 };
327
328 // Returns the LocationQuality for the location L iff the quality of L is
329 // is strictly greater than the provided minimum quality.
330 std::optional<LocationQuality>
331 getLocQualityIfBetter(LocIdx L, LocationQuality Min) const {
332 if (L.isIllegal())
333 return std::nullopt;
334 if (Min >= LocationQuality::SpillSlot)
335 return std::nullopt;
336 if (MTracker->isSpill(Idx: L))
337 return LocationQuality::SpillSlot;
338 if (Min >= LocationQuality::CalleeSavedRegister)
339 return std::nullopt;
340 if (isCalleeSaved(L))
341 return LocationQuality::CalleeSavedRegister;
342 if (Min >= LocationQuality::Register)
343 return std::nullopt;
344 return LocationQuality::Register;
345 }
346
347 /// For a variable \p Var with the live-in value \p Value, attempts to resolve
348 /// the DbgValue to a concrete DBG_VALUE, emitting that value and loading the
349 /// tracking information to track Var throughout the block.
350 /// \p ValueToLoc is a map containing the best known location for every
351 /// ValueIDNum that Value may use.
352 /// \p MBB is the basic block that we are loading the live-in value for.
353 /// \p DbgOpStore is the map containing the DbgOpID->DbgOp mapping needed to
354 /// determine the values used by Value.
355 void loadVarInloc(MachineBasicBlock &MBB, DbgOpIDMap &DbgOpStore,
356 const SmallVectorImpl<ValueLocPair> &ValueToLoc,
357 DebugVariableID VarID, DbgValue Value) {
358 SmallVector<DbgOp> DbgOps;
359 SmallVector<ResolvedDbgOp> ResolvedDbgOps;
360 bool IsValueValid = true;
361 unsigned LastUseBeforeDef = 0;
362 bool DbgLocAvailableAndIsEntryVal = false;
363
364 // If every value used by the incoming DbgValue is available at block
365 // entry, ResolvedDbgOps will contain the machine locations/constants for
366 // those values and will be used to emit a debug location.
367 // If one or more values are not yet available, but will all be defined in
368 // this block, then LastUseBeforeDef will track the instruction index in
369 // this BB at which the last of those values is defined, DbgOps will
370 // contain the values that we will emit when we reach that instruction.
371 // If one or more values are undef or not available throughout this block,
372 // and we can't recover as an entry value, we set IsValueValid=false and
373 // skip this variable.
374 for (DbgOpID ID : Value.getDbgOpIDs()) {
375 DbgOp Op = DbgOpStore.find(ID);
376 DbgOps.push_back(Elt: Op);
377 if (ID.isUndef()) {
378 IsValueValid = false;
379 break;
380 }
381 if (ID.isConst()) {
382 ResolvedDbgOps.push_back(Elt: Op.MO);
383 continue;
384 }
385
386 // Search for the desired ValueIDNum, to examine the best location found
387 // for it. Use an empty ValueLocPair to search for an entry in ValueToLoc.
388 const ValueIDNum &Num = Op.ID;
389 ValueLocPair Probe(Num, LocationAndQuality());
390 auto ValuesPreferredLoc =
391 llvm::lower_bound(Range: ValueToLoc, Value&: Probe, C: ValueToLocSort);
392
393 // There must be a legitimate entry found for Num.
394 assert(ValuesPreferredLoc != ValueToLoc.end() &&
395 ValuesPreferredLoc->first == Num);
396
397 if (ValuesPreferredLoc->second.isIllegal()) {
398 // If it's a def that occurs in this block, register it as a
399 // use-before-def to be resolved as we step through the block.
400 // Continue processing values so that we add any other UseBeforeDef
401 // entries needed for later.
402 if (Num.getBlock() == (unsigned)MBB.getNumber() && !Num.isPHI()) {
403 LastUseBeforeDef = std::max(a: LastUseBeforeDef,
404 b: static_cast<unsigned>(Num.getInst()));
405 continue;
406 }
407 recoverAsEntryValue(VarID, Prop: Value.Properties, Num);
408 IsValueValid = false;
409 break;
410 }
411
412 // Defer modifying ActiveVLocs until after we've confirmed we have a
413 // live range.
414 LocIdx M = ValuesPreferredLoc->second.getLoc();
415 ResolvedDbgOps.push_back(Elt: M);
416 if (Value.Properties.DIExpr->isEntryValue())
417 DbgLocAvailableAndIsEntryVal = true;
418 }
419
420 // If we cannot produce a valid value for the LiveIn value within this
421 // block, skip this variable.
422 if (!IsValueValid)
423 return;
424
425 // Add UseBeforeDef entry for the last value to be defined in this block.
426 if (LastUseBeforeDef) {
427 addUseBeforeDef(VarID, Properties: Value.Properties, DbgOps, Inst: LastUseBeforeDef);
428 return;
429 }
430
431 auto &[Var, DILoc] = DVMap.lookupDVID(ID: VarID);
432 PendingDbgValues.push_back(
433 Elt: std::make_pair(x&: VarID, y: &*MTracker->emitLoc(DbgOps: ResolvedDbgOps, Var, DILoc,
434 Properties: Value.Properties)));
435
436 // If the location is available at block entry and is an entry value, skip
437 // tracking and recording thr transfer.
438 if (DbgLocAvailableAndIsEntryVal)
439 return;
440
441 // The LiveIn value is available at block entry, begin tracking and record
442 // the transfer.
443 for (const ResolvedDbgOp &Op : ResolvedDbgOps)
444 if (!Op.IsConst)
445 ActiveMLocs[Op.Loc].insert(V: VarID);
446 auto NewValue = ResolvedDbgValue{ResolvedDbgOps, Value.Properties};
447 auto Result = ActiveVLocs.insert(KV: std::make_pair(x&: VarID, y&: NewValue));
448 if (!Result.second)
449 Result.first->second = NewValue;
450 }
451
452 /// Load object with live-in variable values. \p mlocs contains the live-in
453 /// values in each machine location, while \p vlocs the live-in variable
454 /// values. This method picks variable locations for the live-in variables,
455 /// creates DBG_VALUEs and puts them in #Transfers, then prepares the other
456 /// object fields to track variable locations as we step through the block.
457 /// FIXME: could just examine mloctracker instead of passing in \p mlocs?
458 void
459 loadInlocs(MachineBasicBlock &MBB, ValueTable &MLocs, DbgOpIDMap &DbgOpStore,
460 const SmallVectorImpl<std::pair<DebugVariableID, DbgValue>> &VLocs,
461 unsigned NumLocs) {
462 ActiveMLocs.clear();
463 ActiveVLocs.clear();
464 VarLocs.clear();
465 VarLocs.reserve(N: NumLocs);
466 UseBeforeDefs.clear();
467 UseBeforeDefVariables.clear();
468
469 // Mapping of the preferred locations for each value. Collected into this
470 // vector then sorted for easy searching.
471 SmallVector<ValueLocPair, 16> ValueToLoc;
472
473 // Initialized the preferred-location map with illegal locations, to be
474 // filled in later.
475 for (const auto &VLoc : VLocs)
476 if (VLoc.second.Kind == DbgValue::Def)
477 for (DbgOpID OpID : VLoc.second.getDbgOpIDs())
478 if (!OpID.ID.IsConst)
479 ValueToLoc.push_back(
480 Elt: {DbgOpStore.find(ID: OpID).ID, LocationAndQuality()});
481
482 llvm::sort(C&: ValueToLoc, Comp: ValueToLocSort);
483 ActiveMLocs.reserve(NumEntries: VLocs.size());
484 ActiveVLocs.reserve(NumEntries: VLocs.size());
485
486 // Produce a map of value numbers to the current machine locs they live
487 // in. When emulating VarLocBasedImpl, there should only be one
488 // location; when not, we get to pick.
489 for (auto Location : MTracker->locations()) {
490 LocIdx Idx = Location.Idx;
491 ValueIDNum &VNum = MLocs[Idx.asU64()];
492 if (VNum == ValueIDNum::EmptyValue)
493 continue;
494 VarLocs.push_back(Elt: VNum);
495
496 // Is there a variable that wants a location for this value? If not, skip.
497 ValueLocPair Probe(VNum, LocationAndQuality());
498 auto VIt = llvm::lower_bound(Range&: ValueToLoc, Value&: Probe, C: ValueToLocSort);
499 if (VIt == ValueToLoc.end() || VIt->first != VNum)
500 continue;
501
502 auto &Previous = VIt->second;
503 // If this is the first location with that value, pick it. Otherwise,
504 // consider whether it's a "longer term" location.
505 std::optional<LocationQuality> ReplacementQuality =
506 getLocQualityIfBetter(L: Idx, Min: Previous.getQuality());
507 if (ReplacementQuality)
508 Previous = LocationAndQuality(Idx, *ReplacementQuality);
509 }
510
511 // Now map variables to their picked LocIdxes.
512 for (const auto &Var : VLocs) {
513 loadVarInloc(MBB, DbgOpStore, ValueToLoc, VarID: Var.first, Value: Var.second);
514 }
515 flushDbgValues(Pos: MBB.begin(), MBB: &MBB);
516 }
517
518 /// Record that \p Var has value \p ID, a value that becomes available
519 /// later in the function.
520 void addUseBeforeDef(DebugVariableID VarID,
521 const DbgValueProperties &Properties,
522 const SmallVectorImpl<DbgOp> &DbgOps, unsigned Inst) {
523 UseBeforeDefs[Inst].emplace_back(Args: DbgOps, Args&: VarID, Args: Properties);
524 UseBeforeDefVariables.insert(V: VarID);
525 }
526
527 /// After the instruction at index \p Inst and position \p pos has been
528 /// processed, check whether it defines a variable value in a use-before-def.
529 /// If so, and the variable value hasn't changed since the start of the
530 /// block, create a DBG_VALUE.
531 void checkInstForNewValues(unsigned Inst, MachineBasicBlock::iterator pos) {
532 auto MIt = UseBeforeDefs.find(Val: Inst);
533 if (MIt == UseBeforeDefs.end())
534 return;
535
536 // Map of values to the locations that store them for every value used by
537 // the variables that may have become available.
538 SmallDenseMap<ValueIDNum, LocationAndQuality> ValueToLoc;
539
540 // Populate ValueToLoc with illegal default mappings for every value used by
541 // any UseBeforeDef variables for this instruction.
542 for (auto &Use : MIt->second) {
543 if (!UseBeforeDefVariables.count(V: Use.VarID))
544 continue;
545
546 for (DbgOp &Op : Use.Values) {
547 assert(!Op.isUndef() && "UseBeforeDef erroneously created for a "
548 "DbgValue with undef values.");
549 if (Op.IsConst)
550 continue;
551
552 ValueToLoc.insert(KV: {Op.ID, LocationAndQuality()});
553 }
554 }
555
556 // Exit early if we have no DbgValues to produce.
557 if (ValueToLoc.empty())
558 return;
559
560 // Determine the best location for each desired value.
561 for (auto Location : MTracker->locations()) {
562 LocIdx Idx = Location.Idx;
563 ValueIDNum &LocValueID = Location.Value;
564
565 // Is there a variable that wants a location for this value? If not, skip.
566 auto VIt = ValueToLoc.find(Val: LocValueID);
567 if (VIt == ValueToLoc.end())
568 continue;
569
570 auto &Previous = VIt->second;
571 // If this is the first location with that value, pick it. Otherwise,
572 // consider whether it's a "longer term" location.
573 std::optional<LocationQuality> ReplacementQuality =
574 getLocQualityIfBetter(L: Idx, Min: Previous.getQuality());
575 if (ReplacementQuality)
576 Previous = LocationAndQuality(Idx, *ReplacementQuality);
577 }
578
579 // Using the map of values to locations, produce a final set of values for
580 // this variable.
581 for (auto &Use : MIt->second) {
582 if (!UseBeforeDefVariables.count(V: Use.VarID))
583 continue;
584
585 SmallVector<ResolvedDbgOp> DbgOps;
586
587 for (DbgOp &Op : Use.Values) {
588 if (Op.IsConst) {
589 DbgOps.push_back(Elt: Op.MO);
590 continue;
591 }
592 LocIdx NewLoc = ValueToLoc.find(Val: Op.ID)->second.getLoc();
593 if (NewLoc.isIllegal())
594 break;
595 DbgOps.push_back(Elt: NewLoc);
596 }
597
598 // If at least one value used by this debug value is no longer available,
599 // i.e. one of the values was killed before we finished defining all of
600 // the values used by this variable, discard.
601 if (DbgOps.size() != Use.Values.size())
602 continue;
603
604 // Otherwise, we're good to go.
605 auto &[Var, DILoc] = DVMap.lookupDVID(ID: Use.VarID);
606 PendingDbgValues.push_back(Elt: std::make_pair(
607 x&: Use.VarID, y: MTracker->emitLoc(DbgOps, Var, DILoc, Properties: Use.Properties)));
608 }
609 flushDbgValues(Pos: pos, MBB: nullptr);
610 }
611
612 /// Helper to move created DBG_VALUEs into Transfers collection.
613 void flushDbgValues(MachineBasicBlock::iterator Pos, MachineBasicBlock *MBB) {
614 if (PendingDbgValues.size() == 0)
615 return;
616
617 // Pick out the instruction start position.
618 MachineBasicBlock::instr_iterator BundleStart;
619 if (MBB && Pos == MBB->begin())
620 BundleStart = MBB->instr_begin();
621 else
622 BundleStart = getBundleStart(I: Pos->getIterator());
623
624 Transfers.push_back(Elt: {.Pos: BundleStart, .MBB: MBB, .Insts: PendingDbgValues});
625 PendingDbgValues.clear();
626 }
627
628 bool isEntryValueVariable(const DebugVariable &Var,
629 const DIExpression *Expr) const {
630 if (!Var.getVariable()->isParameter())
631 return false;
632
633 if (Var.getInlinedAt())
634 return false;
635
636 if (Expr->getNumElements() > 0 && !Expr->isDeref())
637 return false;
638
639 return true;
640 }
641
642 bool isEntryValueValue(const ValueIDNum &Val) const {
643 // Must be in entry block (block number zero), and be a PHI / live-in value.
644 if (Val.getBlock() || !Val.isPHI())
645 return false;
646
647 // Entry values must enter in a register.
648 if (MTracker->isSpill(Idx: Val.getLoc()))
649 return false;
650
651 Register SP = TLI->getStackPointerRegisterToSaveRestore();
652 Register FP = TRI.getFrameRegister(MF);
653 Register Reg = MTracker->LocIdxToLocID[Val.getLoc()];
654 return Reg != SP && Reg != FP;
655 }
656
657 bool recoverAsEntryValue(DebugVariableID VarID,
658 const DbgValueProperties &Prop,
659 const ValueIDNum &Num) {
660 // Is this variable location a candidate to be an entry value. First,
661 // should we be trying this at all?
662 if (!ShouldEmitDebugEntryValues)
663 return false;
664
665 const DIExpression *DIExpr = Prop.DIExpr;
666
667 // We don't currently emit entry values for DBG_VALUE_LISTs.
668 if (Prop.IsVariadic) {
669 // If this debug value can be converted to be non-variadic, then do so;
670 // otherwise give up.
671 auto NonVariadicExpression =
672 DIExpression::convertToNonVariadicExpression(Expr: DIExpr);
673 if (!NonVariadicExpression)
674 return false;
675 DIExpr = *NonVariadicExpression;
676 }
677
678 auto &[Var, DILoc] = DVMap.lookupDVID(ID: VarID);
679
680 // If the expression is a DW_OP_entry_value, emit the variable location
681 // as-is.
682 if (DIExpr->isEntryValue()) {
683 Register Reg = MTracker->LocIdxToLocID[Num.getLoc()];
684 MachineOperand MO = MachineOperand::CreateReg(Reg, isDef: false);
685 PendingDbgValues.push_back(Elt: std::make_pair(
686 x&: VarID, y: &*emitMOLoc(MO, Var, Properties: {DIExpr, Prop.Indirect, false})));
687 return true;
688 }
689
690 // Is the variable appropriate for entry values (i.e., is a parameter).
691 if (!isEntryValueVariable(Var, Expr: DIExpr))
692 return false;
693
694 // Is the value assigned to this variable still the entry value?
695 if (!isEntryValueValue(Val: Num))
696 return false;
697
698 // Emit a variable location using an entry value expression.
699 DIExpression *NewExpr =
700 DIExpression::prepend(Expr: DIExpr, Flags: DIExpression::EntryValue);
701 Register Reg = MTracker->LocIdxToLocID[Num.getLoc()];
702 MachineOperand MO = MachineOperand::CreateReg(Reg, isDef: false);
703 PendingDbgValues.push_back(Elt: std::make_pair(
704 x&: VarID, y: &*emitMOLoc(MO, Var, Properties: {NewExpr, Prop.Indirect, false})));
705 return true;
706 }
707
708 /// Change a variable value after encountering a DBG_VALUE inside a block.
709 void redefVar(const MachineInstr &MI) {
710 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
711 MI.getDebugLoc()->getInlinedAt());
712 DbgValueProperties Properties(MI);
713 DebugVariableID VarID = DVMap.getDVID(Var);
714
715 // Ignore non-register locations, we don't transfer those.
716 if (MI.isUndefDebugValue() || MI.getDebugExpression()->isEntryValue() ||
717 all_of(Range: MI.debug_operands(),
718 P: [](const MachineOperand &MO) { return !MO.isReg(); })) {
719 auto It = ActiveVLocs.find(Val: VarID);
720 if (It != ActiveVLocs.end()) {
721 for (LocIdx Loc : It->second.loc_indices())
722 ActiveMLocs[Loc].erase(V: VarID);
723 ActiveVLocs.erase(I: It);
724 }
725 // Any use-before-defs no longer apply.
726 UseBeforeDefVariables.erase(V: VarID);
727 return;
728 }
729
730 SmallVector<ResolvedDbgOp> NewLocs;
731 for (const MachineOperand &MO : MI.debug_operands()) {
732 if (MO.isReg()) {
733 // Any undef regs have already been filtered out above.
734 Register Reg = MO.getReg();
735 LocIdx NewLoc = MTracker->getRegMLoc(R: Reg);
736 NewLocs.push_back(Elt: NewLoc);
737 } else {
738 NewLocs.push_back(Elt: MO);
739 }
740 }
741
742 redefVar(MI, Properties, NewLocs);
743 }
744
745 /// Handle a change in variable location within a block. Terminate the
746 /// variables current location, and record the value it now refers to, so
747 /// that we can detect location transfers later on.
748 void redefVar(const MachineInstr &MI, const DbgValueProperties &Properties,
749 SmallVectorImpl<ResolvedDbgOp> &NewLocs) {
750 DebugVariable Var(MI.getDebugVariable(), MI.getDebugExpression(),
751 MI.getDebugLoc()->getInlinedAt());
752 DebugVariableID VarID = DVMap.getDVID(Var);
753 // Any use-before-defs no longer apply.
754 UseBeforeDefVariables.erase(V: VarID);
755
756 // Erase any previous location.
757 auto It = ActiveVLocs.find(Val: VarID);
758 if (It != ActiveVLocs.end()) {
759 for (LocIdx Loc : It->second.loc_indices())
760 ActiveMLocs[Loc].erase(V: VarID);
761 }
762
763 // If there _is_ no new location, all we had to do was erase.
764 if (NewLocs.empty()) {
765 if (It != ActiveVLocs.end())
766 ActiveVLocs.erase(I: It);
767 return;
768 }
769
770 SmallVector<std::pair<LocIdx, DebugVariableID>> LostMLocs;
771 for (ResolvedDbgOp &Op : NewLocs) {
772 if (Op.IsConst)
773 continue;
774
775 LocIdx NewLoc = Op.Loc;
776
777 // Check whether our local copy of values-by-location in #VarLocs is out
778 // of date. Wipe old tracking data for the location if it's been clobbered
779 // in the meantime.
780 if (MTracker->readMLoc(L: NewLoc) != VarLocs[NewLoc.asU64()]) {
781 for (const auto &P : ActiveMLocs[NewLoc]) {
782 auto LostVLocIt = ActiveVLocs.find(Val: P);
783 if (LostVLocIt != ActiveVLocs.end()) {
784 for (LocIdx Loc : LostVLocIt->second.loc_indices()) {
785 // Every active variable mapping for NewLoc will be cleared, no
786 // need to track individual variables.
787 if (Loc == NewLoc)
788 continue;
789 LostMLocs.emplace_back(Args&: Loc, Args: P);
790 }
791 }
792 ActiveVLocs.erase(Val: P);
793 }
794 for (const auto &LostMLoc : LostMLocs)
795 ActiveMLocs[LostMLoc.first].erase(V: LostMLoc.second);
796 LostMLocs.clear();
797 It = ActiveVLocs.find(Val: VarID);
798 ActiveMLocs[NewLoc.asU64()].clear();
799 VarLocs[NewLoc.asU64()] = MTracker->readMLoc(L: NewLoc);
800 }
801
802 ActiveMLocs[NewLoc].insert(V: VarID);
803 }
804
805 if (It == ActiveVLocs.end()) {
806 ActiveVLocs.insert(
807 KV: std::make_pair(x&: VarID, y: ResolvedDbgValue(NewLocs, Properties)));
808 } else {
809 It->second.Ops.assign(RHS: NewLocs);
810 It->second.Properties = Properties;
811 }
812 }
813
814 /// Account for a location \p mloc being clobbered. Examine the variable
815 /// locations that will be terminated: and try to recover them by using
816 /// another location. Optionally, given \p MakeUndef, emit a DBG_VALUE to
817 /// explicitly terminate a location if it can't be recovered.
818 void clobberMloc(LocIdx MLoc, MachineBasicBlock::iterator Pos,
819 bool MakeUndef = true) {
820 auto ActiveMLocIt = ActiveMLocs.find(Val: MLoc);
821 if (ActiveMLocIt == ActiveMLocs.end())
822 return;
823
824 // What was the old variable value?
825 ValueIDNum OldValue = VarLocs[MLoc.asU64()];
826 clobberMloc(MLoc, OldValue, Pos, MakeUndef);
827 }
828 /// Overload that takes an explicit value \p OldValue for when the value in
829 /// \p MLoc has changed and the TransferTracker's locations have not been
830 /// updated yet.
831 void clobberMloc(LocIdx MLoc, ValueIDNum OldValue,
832 MachineBasicBlock::iterator Pos, bool MakeUndef = true) {
833 auto ActiveMLocIt = ActiveMLocs.find(Val: MLoc);
834 if (ActiveMLocIt == ActiveMLocs.end())
835 return;
836
837 VarLocs[MLoc.asU64()] = ValueIDNum::EmptyValue;
838
839 // Examine the remaining variable locations: if we can find the same value
840 // again, we can recover the location.
841 std::optional<LocIdx> NewLoc;
842 for (auto Loc : MTracker->locations())
843 if (Loc.Value == OldValue)
844 NewLoc = Loc.Idx;
845
846 // If there is no location, and we weren't asked to make the variable
847 // explicitly undef, then stop here.
848 if (!NewLoc && !MakeUndef) {
849 // Try and recover a few more locations with entry values.
850 for (DebugVariableID VarID : ActiveMLocIt->second) {
851 auto &Prop = ActiveVLocs.find(Val: VarID)->second.Properties;
852 recoverAsEntryValue(VarID, Prop, Num: OldValue);
853 }
854 flushDbgValues(Pos, MBB: nullptr);
855 return;
856 }
857
858 // Examine all the variables based on this location.
859 DenseSet<DebugVariableID> NewMLocs;
860 // If no new location has been found, every variable that depends on this
861 // MLoc is dead, so end their existing MLoc->Var mappings as well.
862 SmallVector<std::pair<LocIdx, DebugVariableID>> LostMLocs;
863 for (DebugVariableID VarID : ActiveMLocIt->second) {
864 auto ActiveVLocIt = ActiveVLocs.find(Val: VarID);
865 // Re-state the variable location: if there's no replacement then NewLoc
866 // is std::nullopt and a $noreg DBG_VALUE will be created. Otherwise, a
867 // DBG_VALUE identifying the alternative location will be emitted.
868 const DbgValueProperties &Properties = ActiveVLocIt->second.Properties;
869
870 // Produce the new list of debug ops - an empty list if no new location
871 // was found, or the existing list with the substitution MLoc -> NewLoc
872 // otherwise.
873 SmallVector<ResolvedDbgOp> DbgOps;
874 if (NewLoc) {
875 ResolvedDbgOp OldOp(MLoc);
876 ResolvedDbgOp NewOp(*NewLoc);
877 // Insert illegal ops to overwrite afterwards.
878 DbgOps.insert(I: DbgOps.begin(), NumToInsert: ActiveVLocIt->second.Ops.size(),
879 Elt: ResolvedDbgOp(LocIdx::MakeIllegalLoc()));
880 replace_copy(Range&: ActiveVLocIt->second.Ops, Out: DbgOps.begin(), OldValue: OldOp, NewValue: NewOp);
881 }
882
883 auto &[Var, DILoc] = DVMap.lookupDVID(ID: VarID);
884 PendingDbgValues.push_back(Elt: std::make_pair(
885 x&: VarID, y: &*MTracker->emitLoc(DbgOps, Var, DILoc, Properties)));
886
887 // Update machine locations <=> variable locations maps. Defer updating
888 // ActiveMLocs to avoid invalidating the ActiveMLocIt iterator.
889 if (!NewLoc) {
890 for (LocIdx Loc : ActiveVLocIt->second.loc_indices()) {
891 if (Loc != MLoc)
892 LostMLocs.emplace_back(Args&: Loc, Args&: VarID);
893 }
894 ActiveVLocs.erase(I: ActiveVLocIt);
895 } else {
896 ActiveVLocIt->second.Ops = DbgOps;
897 NewMLocs.insert(V: VarID);
898 }
899 }
900
901 // Remove variables from ActiveMLocs if they no longer use any other MLocs
902 // due to being killed by this clobber.
903 for (auto &LocVarIt : LostMLocs) {
904 auto LostMLocIt = ActiveMLocs.find(Val: LocVarIt.first);
905 assert(LostMLocIt != ActiveMLocs.end() &&
906 "Variable was using this MLoc, but ActiveMLocs[MLoc] has no "
907 "entries?");
908 LostMLocIt->second.erase(V: LocVarIt.second);
909 }
910
911 // We lazily track what locations have which values; if we've found a new
912 // location for the clobbered value, remember it.
913 if (NewLoc)
914 VarLocs[NewLoc->asU64()] = OldValue;
915
916 flushDbgValues(Pos, MBB: nullptr);
917
918 // Commit ActiveMLoc changes.
919 ActiveMLocIt->second.clear();
920 if (!NewMLocs.empty())
921 ActiveMLocs[*NewLoc].insert_range(R&: NewMLocs);
922 }
923
924 /// Transfer variables based on \p Src to be based on \p Dst. This handles
925 /// both register copies as well as spills and restores. Creates DBG_VALUEs
926 /// describing the movement.
927 void transferMlocs(LocIdx Src, LocIdx Dst, MachineBasicBlock::iterator Pos) {
928 // Does Src still contain the value num we expect? If not, it's been
929 // clobbered in the meantime, and our variable locations are stale.
930 if (VarLocs[Src.asU64()] != MTracker->readMLoc(L: Src))
931 return;
932
933 // assert(ActiveMLocs[Dst].size() == 0);
934 //^^^ Legitimate scenario on account of un-clobbered slot being assigned to?
935
936 // Move set of active variables from one location to another.
937 auto MovingVars = ActiveMLocs[Src];
938 ActiveMLocs[Dst].insert_range(R&: MovingVars);
939 VarLocs[Dst.asU64()] = VarLocs[Src.asU64()];
940
941 // For each variable based on Src; create a location at Dst.
942 ResolvedDbgOp SrcOp(Src);
943 ResolvedDbgOp DstOp(Dst);
944 for (DebugVariableID VarID : MovingVars) {
945 auto ActiveVLocIt = ActiveVLocs.find(Val: VarID);
946 assert(ActiveVLocIt != ActiveVLocs.end());
947
948 // Update all instances of Src in the variable's tracked values to Dst.
949 llvm::replace(Range&: ActiveVLocIt->second.Ops, OldValue: SrcOp, NewValue: DstOp);
950
951 auto &[Var, DILoc] = DVMap.lookupDVID(ID: VarID);
952 MachineInstr *MI = MTracker->emitLoc(DbgOps: ActiveVLocIt->second.Ops, Var, DILoc,
953 Properties: ActiveVLocIt->second.Properties);
954 PendingDbgValues.push_back(Elt: std::make_pair(x&: VarID, y&: MI));
955 }
956 ActiveMLocs[Src].clear();
957 flushDbgValues(Pos, MBB: nullptr);
958
959 // XXX XXX XXX "pretend to be old LDV" means dropping all tracking data
960 // about the old location.
961 if (EmulateOldLDV)
962 VarLocs[Src.asU64()] = ValueIDNum::EmptyValue;
963 }
964
965 MachineInstrBuilder emitMOLoc(const MachineOperand &MO,
966 const DebugVariable &Var,
967 const DbgValueProperties &Properties) {
968 DebugLoc DL = DILocation::get(Context&: Var.getVariable()->getContext(), Line: 0, Column: 0,
969 Scope: Var.getVariable()->getScope(),
970 InlinedAt: const_cast<DILocation *>(Var.getInlinedAt()));
971 auto MIB = BuildMI(MF, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::DBG_VALUE));
972 MIB.add(MO);
973 if (Properties.Indirect)
974 MIB.addImm(Val: 0);
975 else
976 MIB.addReg(RegNo: 0);
977 MIB.addMetadata(MD: Var.getVariable());
978 MIB.addMetadata(MD: Properties.DIExpr);
979 return MIB;
980 }
981};
982
983//===----------------------------------------------------------------------===//
984// Implementation
985//===----------------------------------------------------------------------===//
986
987ValueIDNum ValueIDNum::EmptyValue = {UINT_MAX, UINT_MAX, UINT_MAX};
988ValueIDNum ValueIDNum::TombstoneValue = {UINT_MAX, UINT_MAX, UINT_MAX - 1};
989
990#ifndef NDEBUG
991void ResolvedDbgOp::dump(const MLocTracker *MTrack) const {
992 if (IsConst) {
993 dbgs() << MO;
994 } else {
995 dbgs() << MTrack->LocIdxToName(Loc);
996 }
997}
998void DbgOp::dump(const MLocTracker *MTrack) const {
999 if (IsConst) {
1000 dbgs() << MO;
1001 } else if (!isUndef()) {
1002 dbgs() << MTrack->IDAsString(ID);
1003 }
1004}
1005void DbgOpID::dump(const MLocTracker *MTrack, const DbgOpIDMap *OpStore) const {
1006 if (!OpStore) {
1007 dbgs() << "ID(" << asU32() << ")";
1008 } else {
1009 OpStore->find(*this).dump(MTrack);
1010 }
1011}
1012void DbgValue::dump(const MLocTracker *MTrack,
1013 const DbgOpIDMap *OpStore) const {
1014 if (Kind == NoVal) {
1015 dbgs() << "NoVal(" << BlockNo << ")";
1016 } else if (Kind == VPHI || Kind == Def) {
1017 if (Kind == VPHI)
1018 dbgs() << "VPHI(" << BlockNo << ",";
1019 else
1020 dbgs() << "Def(";
1021 for (unsigned Idx = 0; Idx < getDbgOpIDs().size(); ++Idx) {
1022 getDbgOpID(Idx).dump(MTrack, OpStore);
1023 if (Idx != 0)
1024 dbgs() << ",";
1025 }
1026 dbgs() << ")";
1027 }
1028 if (Properties.Indirect)
1029 dbgs() << " indir";
1030 if (Properties.DIExpr)
1031 dbgs() << " " << *Properties.DIExpr;
1032}
1033#endif
1034
1035MLocTracker::MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII,
1036 const TargetRegisterInfo &TRI,
1037 const TargetLowering &TLI)
1038 : MF(MF), TII(TII), TRI(TRI), TLI(TLI),
1039 LocIdxToIDNum(ValueIDNum::EmptyValue), LocIdxToLocID(0) {
1040 NumRegs = TRI.getNumRegs();
1041 reset();
1042 LocIDToLocIdx.resize(new_size: NumRegs, x: LocIdx::MakeIllegalLoc());
1043 assert(NumRegs < (1u << NUM_LOC_BITS)); // Detect bit packing failure
1044
1045 // Always track SP. This avoids the implicit clobbering caused by regmasks
1046 // from affectings its values. (LiveDebugValues disbelieves calls and
1047 // regmasks that claim to clobber SP).
1048 Register SP = TLI.getStackPointerRegisterToSaveRestore();
1049 if (SP) {
1050 unsigned ID = getLocID(Reg: SP);
1051 (void)lookupOrTrackRegister(ID);
1052
1053 for (MCRegAliasIterator RAI(SP, &TRI, true); RAI.isValid(); ++RAI)
1054 SPAliases.insert(V: *RAI);
1055 }
1056
1057 // Build some common stack positions -- full registers being spilt to the
1058 // stack.
1059 StackSlotIdxes.insert(KV: {{8, 0}, 0});
1060 StackSlotIdxes.insert(KV: {{16, 0}, 1});
1061 StackSlotIdxes.insert(KV: {{32, 0}, 2});
1062 StackSlotIdxes.insert(KV: {{64, 0}, 3});
1063 StackSlotIdxes.insert(KV: {{128, 0}, 4});
1064 StackSlotIdxes.insert(KV: {{256, 0}, 5});
1065 StackSlotIdxes.insert(KV: {{512, 0}, 6});
1066
1067 // Traverse all the subregister idxes, and ensure there's an index for them.
1068 // Duplicates are no problem: we're interested in their position in the
1069 // stack slot, we don't want to type the slot.
1070 for (unsigned int I = 1; I < TRI.getNumSubRegIndices(); ++I) {
1071 unsigned Size = TRI.getSubRegIdxSize(Idx: I);
1072 unsigned Offs = TRI.getSubRegIdxOffset(Idx: I);
1073 unsigned Idx = StackSlotIdxes.size();
1074
1075 // Some subregs have -1, -2 and so forth fed into their fields, to mean
1076 // special backend things. Ignore those.
1077 if (Size > 60000 || Offs > 60000)
1078 continue;
1079
1080 StackSlotIdxes.insert(KV: {{Size, Offs}, Idx});
1081 }
1082
1083 // There may also be strange register class sizes (think x86 fp80s).
1084 for (const TargetRegisterClass *RC : TRI.regclasses()) {
1085 unsigned Size = TRI.getRegSizeInBits(RC: *RC);
1086
1087 // We might see special reserved values as sizes, and classes for other
1088 // stuff the machine tries to model. If it's more than 512 bits, then it
1089 // is very unlikely to be a register than can be spilt.
1090 if (Size > 512)
1091 continue;
1092
1093 unsigned Idx = StackSlotIdxes.size();
1094 StackSlotIdxes.insert(KV: {{Size, 0}, Idx});
1095 }
1096
1097 for (auto &Idx : StackSlotIdxes)
1098 StackIdxesToPos[Idx.second] = Idx.first;
1099
1100 NumSlotIdxes = StackSlotIdxes.size();
1101}
1102
1103LocIdx MLocTracker::trackRegister(unsigned ID) {
1104 assert(ID != 0);
1105 LocIdx NewIdx = LocIdx(LocIdxToIDNum.size());
1106 LocIdxToIDNum.grow(N: NewIdx);
1107 LocIdxToLocID.grow(N: NewIdx);
1108
1109 // Default: it's an mphi.
1110 ValueIDNum ValNum = {CurBB, 0, NewIdx};
1111 // Was this reg ever touched by a regmask?
1112 for (const auto &MaskPair : reverse(C&: Masks)) {
1113 if (MaskPair.first->clobbersPhysReg(PhysReg: ID)) {
1114 // There was an earlier def we skipped.
1115 ValNum = {CurBB, MaskPair.second, NewIdx};
1116 break;
1117 }
1118 }
1119
1120 LocIdxToIDNum[NewIdx] = ValNum;
1121 LocIdxToLocID[NewIdx] = ID;
1122 return NewIdx;
1123}
1124
1125void MLocTracker::writeRegMask(const MachineOperand *MO, unsigned CurBB,
1126 unsigned InstID) {
1127 // Def any register we track have that isn't preserved. The regmask
1128 // terminates the liveness of a register, meaning its value can't be
1129 // relied upon -- we represent this by giving it a new value.
1130 for (auto Location : locations()) {
1131 unsigned ID = LocIdxToLocID[Location.Idx];
1132 // Don't clobber SP, even if the mask says it's clobbered.
1133 if (ID < NumRegs && !SPAliases.count(V: ID) && MO->clobbersPhysReg(PhysReg: ID))
1134 defReg(R: ID, BB: CurBB, Inst: InstID);
1135 }
1136 Masks.push_back(Elt: std::make_pair(x&: MO, y&: InstID));
1137}
1138
1139std::optional<SpillLocationNo> MLocTracker::getOrTrackSpillLoc(SpillLoc L) {
1140 SpillLocationNo SpillID(SpillLocs.idFor(Entry: L));
1141
1142 if (SpillID.id() == 0) {
1143 // If there is no location, and we have reached the limit of how many stack
1144 // slots to track, then don't track this one.
1145 if (SpillLocs.size() >= StackWorkingSetLimit)
1146 return std::nullopt;
1147
1148 // Spill location is untracked: create record for this one, and all
1149 // subregister slots too.
1150 SpillID = SpillLocationNo(SpillLocs.insert(Entry: L));
1151 for (unsigned StackIdx = 0; StackIdx < NumSlotIdxes; ++StackIdx) {
1152 unsigned L = getSpillIDWithIdx(Spill: SpillID, Idx: StackIdx);
1153 LocIdx Idx = LocIdx(LocIdxToIDNum.size()); // New idx
1154 LocIdxToIDNum.grow(N: Idx);
1155 LocIdxToLocID.grow(N: Idx);
1156 LocIDToLocIdx.push_back(x: Idx);
1157 LocIdxToLocID[Idx] = L;
1158 // Initialize to PHI value; corresponds to the location's live-in value
1159 // during transfer function construction.
1160 LocIdxToIDNum[Idx] = ValueIDNum(CurBB, 0, Idx);
1161 }
1162 }
1163 return SpillID;
1164}
1165
1166std::string MLocTracker::LocIdxToName(LocIdx Idx) const {
1167 unsigned ID = LocIdxToLocID[Idx];
1168 if (ID >= NumRegs) {
1169 StackSlotPos Pos = locIDToSpillIdx(ID);
1170 ID -= NumRegs;
1171 unsigned Slot = ID / NumSlotIdxes;
1172 return Twine("slot ")
1173 .concat(Suffix: Twine(Slot).concat(Suffix: Twine(" sz ").concat(Suffix: Twine(Pos.first)
1174 .concat(Suffix: Twine(" offs ").concat(Suffix: Twine(Pos.second))))))
1175 .str();
1176 } else {
1177 return TRI.getRegAsmName(Reg: ID).str();
1178 }
1179}
1180
1181std::string MLocTracker::IDAsString(const ValueIDNum &Num) const {
1182 std::string DefName = LocIdxToName(Idx: Num.getLoc());
1183 return Num.asString(mlocname: DefName);
1184}
1185
1186#ifndef NDEBUG
1187LLVM_DUMP_METHOD void MLocTracker::dump() {
1188 for (auto Location : locations()) {
1189 std::string MLocName = LocIdxToName(Location.Value.getLoc());
1190 std::string DefName = Location.Value.asString(MLocName);
1191 dbgs() << LocIdxToName(Location.Idx) << " --> " << DefName << "\n";
1192 }
1193}
1194
1195LLVM_DUMP_METHOD void MLocTracker::dump_mloc_map() {
1196 for (auto Location : locations()) {
1197 std::string foo = LocIdxToName(Location.Idx);
1198 dbgs() << "Idx " << Location.Idx.asU64() << " " << foo << "\n";
1199 }
1200}
1201#endif
1202
1203MachineInstrBuilder
1204MLocTracker::emitLoc(const SmallVectorImpl<ResolvedDbgOp> &DbgOps,
1205 const DebugVariable &Var, const DILocation *DILoc,
1206 const DbgValueProperties &Properties) {
1207 DebugLoc DL = DebugLoc(DILoc);
1208
1209 const MCInstrDesc &Desc = Properties.IsVariadic
1210 ? TII.get(Opcode: TargetOpcode::DBG_VALUE_LIST)
1211 : TII.get(Opcode: TargetOpcode::DBG_VALUE);
1212
1213#ifdef EXPENSIVE_CHECKS
1214 assert(all_of(DbgOps,
1215 [](const ResolvedDbgOp &Op) {
1216 return Op.IsConst || !Op.Loc.isIllegal();
1217 }) &&
1218 "Did not expect illegal ops in DbgOps.");
1219 assert((DbgOps.size() == 0 ||
1220 DbgOps.size() == Properties.getLocationOpCount()) &&
1221 "Expected to have either one DbgOp per MI LocationOp, or none.");
1222#endif
1223
1224 auto GetRegOp = [](unsigned Reg) -> MachineOperand {
1225 return MachineOperand::CreateReg(
1226 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
1227 /* isKill */ false, /* isDead */ false,
1228 /* isUndef */ false, /* isEarlyClobber */ false,
1229 /* SubReg */ 0, /* isDebug */ true);
1230 };
1231
1232 SmallVector<MachineOperand> MOs;
1233
1234 auto EmitUndef = [&]() {
1235 MOs.clear();
1236 MOs.assign(NumElts: Properties.getLocationOpCount(), Elt: GetRegOp(0));
1237 return BuildMI(MF, DL, MCID: Desc, IsIndirect: false, MOs, Variable: Var.getVariable(),
1238 Expr: Properties.DIExpr);
1239 };
1240
1241 // Don't bother passing any real operands to BuildMI if any of them would be
1242 // $noreg.
1243 if (DbgOps.empty())
1244 return EmitUndef();
1245
1246 bool Indirect = Properties.Indirect;
1247
1248 const DIExpression *Expr = Properties.DIExpr;
1249
1250 assert(DbgOps.size() == Properties.getLocationOpCount());
1251
1252 // If all locations are valid, accumulate them into our list of
1253 // MachineOperands. For any spilled locations, either update the indirectness
1254 // register or apply the appropriate transformations in the DIExpression.
1255 for (size_t Idx = 0; Idx < Properties.getLocationOpCount(); ++Idx) {
1256 const ResolvedDbgOp &Op = DbgOps[Idx];
1257
1258 if (Op.IsConst) {
1259 MOs.push_back(Elt: Op.MO);
1260 continue;
1261 }
1262
1263 LocIdx MLoc = Op.Loc;
1264 unsigned LocID = LocIdxToLocID[MLoc];
1265 if (LocID >= NumRegs) {
1266 SpillLocationNo SpillID = locIDToSpill(ID: LocID);
1267 StackSlotPos StackIdx = locIDToSpillIdx(ID: LocID);
1268 unsigned short Offset = StackIdx.second;
1269
1270 // TODO: support variables that are located in spill slots, with non-zero
1271 // offsets from the start of the spill slot. It would require some more
1272 // complex DIExpression calculations. This doesn't seem to be produced by
1273 // LLVM right now, so don't try and support it.
1274 // Accept no-subregister slots and subregisters where the offset is zero.
1275 // The consumer should already have type information to work out how large
1276 // the variable is.
1277 if (Offset == 0) {
1278 const SpillLoc &Spill = SpillLocs[SpillID.id()];
1279 unsigned Base = Spill.SpillBase;
1280
1281 // There are several ways we can dereference things, and several inputs
1282 // to consider:
1283 // * NRVO variables will appear with IsIndirect set, but should have
1284 // nothing else in their DIExpressions,
1285 // * Variables with DW_OP_stack_value in their expr already need an
1286 // explicit dereference of the stack location,
1287 // * Values that don't match the variable size need DW_OP_deref_size,
1288 // * Everything else can just become a simple location expression.
1289
1290 // We need to use deref_size whenever there's a mismatch between the
1291 // size of value and the size of variable portion being read.
1292 // Additionally, we should use it whenever dealing with stack_value
1293 // fragments, to avoid the consumer having to determine the deref size
1294 // from DW_OP_piece.
1295 bool UseDerefSize = false;
1296 unsigned ValueSizeInBits = getLocSizeInBits(L: MLoc);
1297 unsigned DerefSizeInBytes = ValueSizeInBits / 8;
1298 if (auto Fragment = Var.getFragment()) {
1299 unsigned VariableSizeInBits = Fragment->SizeInBits;
1300 if (VariableSizeInBits != ValueSizeInBits || Expr->isComplex())
1301 UseDerefSize = true;
1302 } else if (auto Size = Var.getVariable()->getSizeInBits()) {
1303 if (*Size != ValueSizeInBits) {
1304 UseDerefSize = true;
1305 }
1306 }
1307
1308 // https://github.com/llvm/llvm-project/issues/64093
1309 // in particular #issuecomment-2531264124. We use variable locations
1310 // such as DBG_VALUE $xmm0 as shorthand to refer to "the low lane of
1311 // $xmm0", and this is reflected in how DWARF is interpreted too.
1312 // However InstrRefBasedLDV tries to be smart and interprets such a
1313 // DBG_VALUE as a 128-bit reference. We then issue a DW_OP_deref_size
1314 // of 128 bits to the stack, which isn't permitted by DWARF (it's
1315 // larger than a pointer).
1316 //
1317 // Solve this for now by not using DW_OP_deref_size if it would be
1318 // illegal. Instead we'll use DW_OP_deref, and the consumer will load
1319 // the variable type from the stack, which should be correct.
1320 //
1321 // There's still a risk of imprecision when LLVM decides to use
1322 // smaller or larger value types than the source-variable type, which
1323 // manifests as too-little or too-much memory being read from the stack.
1324 // However we can't solve that without putting more type information in
1325 // debug-info.
1326 if (ValueSizeInBits > MF.getTarget().getPointerSizeInBits(AS: 0))
1327 UseDerefSize = false;
1328
1329 SmallVector<uint64_t, 5> OffsetOps;
1330 TRI.getOffsetOpcodes(Offset: Spill.SpillOffset, Ops&: OffsetOps);
1331 bool StackValue = false;
1332
1333 if (Properties.Indirect) {
1334 // This is something like an NRVO variable, where the pointer has been
1335 // spilt to the stack. It should end up being a memory location, with
1336 // the pointer to the variable loaded off the stack with a deref:
1337 assert(!Expr->isImplicit());
1338 OffsetOps.push_back(Elt: dwarf::DW_OP_deref);
1339 } else if (UseDerefSize && Expr->isSingleLocationExpression()) {
1340 // TODO: Figure out how to handle deref size issues for variadic
1341 // values.
1342 // We're loading a value off the stack that's not the same size as the
1343 // variable. Add / subtract stack offset, explicitly deref with a
1344 // size, and add DW_OP_stack_value if not already present.
1345 OffsetOps.push_back(Elt: dwarf::DW_OP_deref_size);
1346 OffsetOps.push_back(Elt: DerefSizeInBytes);
1347 StackValue = true;
1348 } else if (Expr->isComplex() || Properties.IsVariadic) {
1349 // A variable with no size ambiguity, but with extra elements in it's
1350 // expression. Manually dereference the stack location.
1351 OffsetOps.push_back(Elt: dwarf::DW_OP_deref);
1352 } else {
1353 // A plain value that has been spilt to the stack, with no further
1354 // context. Request a location expression, marking the DBG_VALUE as
1355 // IsIndirect.
1356 Indirect = true;
1357 }
1358
1359 Expr = DIExpression::appendOpsToArg(Expr, Ops: OffsetOps, ArgNo: Idx, StackValue);
1360 MOs.push_back(Elt: GetRegOp(Base));
1361 } else {
1362 // This is a stack location with a weird subregister offset: emit an
1363 // undef DBG_VALUE instead.
1364 return EmitUndef();
1365 }
1366 } else {
1367 // Non-empty, non-stack slot, must be a plain register.
1368 MOs.push_back(Elt: GetRegOp(LocID));
1369 }
1370 }
1371
1372 return BuildMI(MF, DL, MCID: Desc, IsIndirect: Indirect, MOs, Variable: Var.getVariable(), Expr);
1373}
1374
1375/// Default construct and initialize the pass.
1376InstrRefBasedLDV::InstrRefBasedLDV() = default;
1377
1378bool InstrRefBasedLDV::isCalleeSaved(LocIdx L) const {
1379 unsigned Reg = MTracker->LocIdxToLocID[L];
1380 return isCalleeSavedReg(R: Reg);
1381}
1382bool InstrRefBasedLDV::isCalleeSavedReg(Register R) const {
1383 for (MCRegAliasIterator RAI(R, TRI, true); RAI.isValid(); ++RAI)
1384 if (CalleeSavedRegs.test(Idx: (*RAI).id()))
1385 return true;
1386 return false;
1387}
1388
1389//===----------------------------------------------------------------------===//
1390// Debug Range Extension Implementation
1391//===----------------------------------------------------------------------===//
1392
1393#ifndef NDEBUG
1394// Something to restore in the future.
1395// void InstrRefBasedLDV::printVarLocInMBB(..)
1396#endif
1397
1398std::optional<SpillLocationNo>
1399InstrRefBasedLDV::extractSpillBaseRegAndOffset(const MachineInstr &MI) {
1400 assert(MI.hasOneMemOperand() &&
1401 "Spill instruction does not have exactly one memory operand?");
1402 auto MMOI = MI.memoperands_begin();
1403 const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
1404 assert(PVal->kind() == PseudoSourceValue::FixedStack &&
1405 "Inconsistent memory operand in spill instruction");
1406 int FI = cast<FixedStackPseudoSourceValue>(Val: PVal)->getFrameIndex();
1407 const MachineBasicBlock *MBB = MI.getParent();
1408 Register Reg;
1409 StackOffset Offset = TFI->getFrameIndexReference(MF: *MBB->getParent(), FI, FrameReg&: Reg);
1410 return MTracker->getOrTrackSpillLoc(L: {.SpillBase: Reg, .SpillOffset: Offset});
1411}
1412
1413std::optional<LocIdx>
1414InstrRefBasedLDV::findLocationForMemOperand(const MachineInstr &MI) {
1415 std::optional<SpillLocationNo> SpillLoc = extractSpillBaseRegAndOffset(MI);
1416 if (!SpillLoc)
1417 return std::nullopt;
1418
1419 // Where in the stack slot is this value defined -- i.e., what size of value
1420 // is this? An important question, because it could be loaded into a register
1421 // from the stack at some point. Happily the memory operand will tell us
1422 // the size written to the stack.
1423 auto *MemOperand = *MI.memoperands_begin();
1424 LocationSize SizeInBits = MemOperand->getSizeInBits();
1425 assert(SizeInBits.hasValue() && "Expected to find a valid size!");
1426
1427 // Find that position in the stack indexes we're tracking.
1428 auto IdxIt = MTracker->StackSlotIdxes.find(Val: {SizeInBits.getValue(), 0});
1429 if (IdxIt == MTracker->StackSlotIdxes.end())
1430 // That index is not tracked. This is suprising, and unlikely to ever
1431 // occur, but the safe action is to indicate the variable is optimised out.
1432 return std::nullopt;
1433
1434 unsigned SpillID = MTracker->getSpillIDWithIdx(Spill: *SpillLoc, Idx: IdxIt->second);
1435 return MTracker->getSpillMLoc(SpillID);
1436}
1437
1438/// End all previous ranges related to @MI and start a new range from @MI
1439/// if it is a DBG_VALUE instr.
1440bool InstrRefBasedLDV::transferDebugValue(const MachineInstr &MI) {
1441 if (!MI.isDebugValue())
1442 return false;
1443
1444 assert(MI.getDebugVariable()->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
1445 "Expected inlined-at fields to agree");
1446
1447 // If there are no instructions in this lexical scope, do no location tracking
1448 // at all, this variable shouldn't get a legitimate location range.
1449 auto *Scope = LS.findLexicalScope(DL: MI.getDebugLoc().get());
1450 if (Scope == nullptr)
1451 return true; // handled it; by doing nothing
1452
1453 // MLocTracker needs to know that this register is read, even if it's only
1454 // read by a debug inst.
1455 for (const MachineOperand &MO : MI.debug_operands())
1456 if (MO.isReg() && MO.getReg() != 0)
1457 (void)MTracker->readReg(R: MO.getReg());
1458
1459 // If we're preparing for the second analysis (variables), the machine value
1460 // locations are already solved, and we report this DBG_VALUE and the value
1461 // it refers to to VLocTracker.
1462 if (VTracker) {
1463 SmallVector<DbgOpID> DebugOps;
1464 // Feed defVar the new variable location, or if this is a DBG_VALUE $noreg,
1465 // feed defVar None.
1466 if (!MI.isUndefDebugValue()) {
1467 for (const MachineOperand &MO : MI.debug_operands()) {
1468 // There should be no undef registers here, as we've screened for undef
1469 // debug values.
1470 if (MO.isReg()) {
1471 DebugOps.push_back(Elt: DbgOpStore.insert(Op: MTracker->readReg(R: MO.getReg())));
1472 } else if (MO.isImm() || MO.isFPImm() || MO.isCImm()) {
1473 DebugOps.push_back(Elt: DbgOpStore.insert(Op: MO));
1474 } else {
1475 llvm_unreachable("Unexpected debug operand type.");
1476 }
1477 }
1478 }
1479 VTracker->defVar(MI, Properties: DbgValueProperties(MI), DebugOps);
1480 }
1481
1482 // If performing final tracking of transfers, report this variable definition
1483 // to the TransferTracker too.
1484 if (TTracker)
1485 TTracker->redefVar(MI);
1486 return true;
1487}
1488
1489std::optional<ValueIDNum> InstrRefBasedLDV::getValueForInstrRef(
1490 unsigned InstNo, unsigned OpNo, MachineInstr &MI,
1491 const FuncValueTable *MLiveOuts, const FuncValueTable *MLiveIns) {
1492 // Various optimizations may have happened to the value during codegen,
1493 // recorded in the value substitution table. Apply any substitutions to
1494 // the instruction / operand number in this DBG_INSTR_REF, and collect
1495 // any subregister extractions performed during optimization.
1496 const MachineFunction &MF = *MI.getParent()->getParent();
1497
1498 // Create dummy substitution with Src set, for lookup.
1499 auto SoughtSub =
1500 MachineFunction::DebugSubstitution({InstNo, OpNo}, {0, 0}, 0);
1501
1502 SmallVector<unsigned, 4> SeenSubregs;
1503 auto LowerBoundIt = llvm::lower_bound(Range: MF.DebugValueSubstitutions, Value&: SoughtSub);
1504 while (LowerBoundIt != MF.DebugValueSubstitutions.end() &&
1505 LowerBoundIt->Src == SoughtSub.Src) {
1506 std::tie(args&: InstNo, args&: OpNo) = LowerBoundIt->Dest;
1507 SoughtSub.Src = LowerBoundIt->Dest;
1508 if (unsigned Subreg = LowerBoundIt->Subreg)
1509 SeenSubregs.push_back(Elt: Subreg);
1510 LowerBoundIt = llvm::lower_bound(Range: MF.DebugValueSubstitutions, Value&: SoughtSub);
1511 }
1512
1513 // Default machine value number is <None> -- if no instruction defines
1514 // the corresponding value, it must have been optimized out.
1515 std::optional<ValueIDNum> NewID;
1516
1517 // Try to lookup the instruction number, and find the machine value number
1518 // that it defines. It could be an instruction, or a PHI.
1519 auto InstrIt = DebugInstrNumToInstr.find(x: InstNo);
1520 auto PHIIt = llvm::lower_bound(Range&: DebugPHINumToValue, Value&: InstNo);
1521 if (InstrIt != DebugInstrNumToInstr.end()) {
1522 const MachineInstr &TargetInstr = *InstrIt->second.first;
1523 uint64_t BlockNo = TargetInstr.getParent()->getNumber();
1524
1525 // Pick out the designated operand. It might be a memory reference, if
1526 // a register def was folded into a stack store.
1527 if (OpNo == MachineFunction::DebugOperandMemNumber &&
1528 TargetInstr.hasOneMemOperand()) {
1529 std::optional<LocIdx> L = findLocationForMemOperand(MI: TargetInstr);
1530 if (L)
1531 NewID = ValueIDNum(BlockNo, InstrIt->second.second, *L);
1532 } else if (OpNo != MachineFunction::DebugOperandMemNumber) {
1533 // Permit the debug-info to be completely wrong: identifying a nonexistant
1534 // operand, or one that is not a register definition, means something
1535 // unexpected happened during optimisation. Broken debug-info, however,
1536 // shouldn't crash the compiler -- instead leave the variable value as
1537 // None, which will make it appear "optimised out".
1538 if (OpNo < TargetInstr.getNumOperands()) {
1539 const MachineOperand &MO = TargetInstr.getOperand(i: OpNo);
1540
1541 if (MO.isReg() && MO.isDef() && MO.getReg()) {
1542 unsigned LocID = MTracker->getLocID(Reg: MO.getReg());
1543 LocIdx L = MTracker->LocIDToLocIdx[LocID];
1544 NewID = ValueIDNum(BlockNo, InstrIt->second.second, L);
1545 }
1546 }
1547
1548 if (!NewID) {
1549 LLVM_DEBUG(
1550 { dbgs() << "Seen instruction reference to illegal operand\n"; });
1551 }
1552 }
1553 // else: NewID is left as None.
1554 } else if (PHIIt != DebugPHINumToValue.end() && PHIIt->InstrNum == InstNo) {
1555 // It's actually a PHI value. Which value it is might not be obvious, use
1556 // the resolver helper to find out.
1557 assert(MLiveOuts && MLiveIns);
1558 NewID = resolveDbgPHIs(MF&: *MI.getParent()->getParent(), MLiveOuts: *MLiveOuts, MLiveIns: *MLiveIns,
1559 Here&: MI, InstrNum: InstNo);
1560 }
1561
1562 // Apply any subregister extractions, in reverse. We might have seen code
1563 // like this:
1564 // CALL64 @foo, implicit-def $rax
1565 // %0:gr64 = COPY $rax
1566 // %1:gr32 = COPY %0.sub_32bit
1567 // %2:gr16 = COPY %1.sub_16bit
1568 // %3:gr8 = COPY %2.sub_8bit
1569 // In which case each copy would have been recorded as a substitution with
1570 // a subregister qualifier. Apply those qualifiers now.
1571 if (NewID && !SeenSubregs.empty()) {
1572 unsigned Offset = 0;
1573 unsigned Size = 0;
1574
1575 // Look at each subregister that we passed through, and progressively
1576 // narrow in, accumulating any offsets that occur. Substitutions should
1577 // only ever be the same or narrower width than what they read from;
1578 // iterate in reverse order so that we go from wide to small.
1579 for (unsigned Subreg : reverse(C&: SeenSubregs)) {
1580 unsigned ThisSize = TRI->getSubRegIdxSize(Idx: Subreg);
1581 unsigned ThisOffset = TRI->getSubRegIdxOffset(Idx: Subreg);
1582 Offset += ThisOffset;
1583 Size = (Size == 0) ? ThisSize : std::min(a: Size, b: ThisSize);
1584 }
1585
1586 // If that worked, look for an appropriate subregister with the register
1587 // where the define happens. Don't look at values that were defined during
1588 // a stack write: we can't currently express register locations within
1589 // spills.
1590 LocIdx L = NewID->getLoc();
1591 if (NewID && !MTracker->isSpill(Idx: L)) {
1592 // Find the register class for the register where this def happened.
1593 // FIXME: no index for this?
1594 Register Reg = MTracker->LocIdxToLocID[L];
1595 const TargetRegisterClass *TRC = nullptr;
1596 for (const auto *TRCI : TRI->regclasses())
1597 if (TRCI->contains(Reg))
1598 TRC = TRCI;
1599 assert(TRC && "Couldn't find target register class?");
1600
1601 // If the register we have isn't the right size or in the right place,
1602 // Try to find a subregister inside it.
1603 unsigned MainRegSize = TRI->getRegSizeInBits(RC: *TRC);
1604 if (Size != MainRegSize || Offset) {
1605 // Enumerate all subregisters, searching.
1606 Register NewReg = Register();
1607 for (MCRegister SR : TRI->subregs(Reg)) {
1608 unsigned Subreg = TRI->getSubRegIndex(RegNo: Reg, SubRegNo: SR);
1609 unsigned SubregSize = TRI->getSubRegIdxSize(Idx: Subreg);
1610 unsigned SubregOffset = TRI->getSubRegIdxOffset(Idx: Subreg);
1611 if (SubregSize == Size && SubregOffset == Offset) {
1612 NewReg = SR;
1613 break;
1614 }
1615 }
1616
1617 // If we didn't find anything: there's no way to express our value.
1618 if (!NewReg) {
1619 NewID = std::nullopt;
1620 } else {
1621 // Re-state the value as being defined within the subregister
1622 // that we found.
1623 LocIdx NewLoc =
1624 MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: NewReg));
1625 NewID = ValueIDNum(NewID->getBlock(), NewID->getInst(), NewLoc);
1626 }
1627 }
1628 } else {
1629 // If we can't handle subregisters, unset the new value.
1630 NewID = std::nullopt;
1631 }
1632 }
1633
1634 return NewID;
1635}
1636
1637bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
1638 const FuncValueTable *MLiveOuts,
1639 const FuncValueTable *MLiveIns) {
1640 if (!MI.isDebugRef())
1641 return false;
1642
1643 // Only handle this instruction when we are building the variable value
1644 // transfer function.
1645 if (!VTracker && !TTracker)
1646 return false;
1647
1648 const DILocalVariable *Var = MI.getDebugVariable();
1649 const DIExpression *Expr = MI.getDebugExpression();
1650 const DILocation *DebugLoc = MI.getDebugLoc();
1651 const DILocation *InlinedAt = DebugLoc->getInlinedAt();
1652 assert(Var->isValidLocationForIntrinsic(DebugLoc) &&
1653 "Expected inlined-at fields to agree");
1654
1655 DebugVariable V(Var, Expr, InlinedAt);
1656
1657 auto *Scope = LS.findLexicalScope(DL: MI.getDebugLoc().get());
1658 if (Scope == nullptr)
1659 return true; // Handled by doing nothing. This variable is never in scope.
1660
1661 SmallVector<DbgOpID> DbgOpIDs;
1662 for (const MachineOperand &MO : MI.debug_operands()) {
1663 if (!MO.isDbgInstrRef()) {
1664 assert(!MO.isReg() && "DBG_INSTR_REF should not contain registers");
1665 DbgOpID ConstOpID = DbgOpStore.insert(Op: DbgOp(MO));
1666 DbgOpIDs.push_back(Elt: ConstOpID);
1667 continue;
1668 }
1669
1670 unsigned InstNo = MO.getInstrRefInstrIndex();
1671 unsigned OpNo = MO.getInstrRefOpIndex();
1672
1673 // Default machine value number is <None> -- if no instruction defines
1674 // the corresponding value, it must have been optimized out.
1675 std::optional<ValueIDNum> NewID =
1676 getValueForInstrRef(InstNo, OpNo, MI, MLiveOuts, MLiveIns);
1677 // We have a value number or std::nullopt. If the latter, then kill the
1678 // entire debug value.
1679 if (NewID) {
1680 DbgOpIDs.push_back(Elt: DbgOpStore.insert(Op: *NewID));
1681 } else {
1682 DbgOpIDs.clear();
1683 break;
1684 }
1685 }
1686
1687 // We have a DbgOpID for every value or for none. Tell the variable value
1688 // tracker about it. The rest of this LiveDebugValues implementation acts
1689 // exactly the same for DBG_INSTR_REFs as DBG_VALUEs (just, the former can
1690 // refer to values that aren't immediately available).
1691 DbgValueProperties Properties(Expr, false, true);
1692 if (VTracker)
1693 VTracker->defVar(MI, Properties, DebugOps: DbgOpIDs);
1694
1695 // If we're on the final pass through the function, decompose this INSTR_REF
1696 // into a plain DBG_VALUE.
1697 if (!TTracker)
1698 return true;
1699
1700 // Fetch the concrete DbgOps now, as we will need them later.
1701 SmallVector<DbgOp> DbgOps;
1702 for (DbgOpID OpID : DbgOpIDs) {
1703 DbgOps.push_back(Elt: DbgOpStore.find(ID: OpID));
1704 }
1705
1706 // Pick a location for the machine value number, if such a location exists.
1707 // (This information could be stored in TransferTracker to make it faster).
1708 SmallDenseMap<ValueIDNum, TransferTracker::LocationAndQuality> FoundLocs;
1709 SmallVector<ValueIDNum> ValuesToFind;
1710 // Initialized the preferred-location map with illegal locations, to be
1711 // filled in later.
1712 for (const DbgOp &Op : DbgOps) {
1713 if (!Op.IsConst)
1714 if (FoundLocs.try_emplace(Key: Op.ID).second)
1715 ValuesToFind.push_back(Elt: Op.ID);
1716 }
1717
1718 for (auto Location : MTracker->locations()) {
1719 LocIdx CurL = Location.Idx;
1720 ValueIDNum ID = MTracker->readMLoc(L: CurL);
1721 auto ValueToFindIt = find(Range&: ValuesToFind, Val: ID);
1722 if (ValueToFindIt == ValuesToFind.end())
1723 continue;
1724 auto &Previous = FoundLocs.find(Val: ID)->second;
1725 // If this is the first location with that value, pick it. Otherwise,
1726 // consider whether it's a "longer term" location.
1727 std::optional<TransferTracker::LocationQuality> ReplacementQuality =
1728 TTracker->getLocQualityIfBetter(L: CurL, Min: Previous.getQuality());
1729 if (ReplacementQuality) {
1730 Previous = TransferTracker::LocationAndQuality(CurL, *ReplacementQuality);
1731 if (Previous.isBest()) {
1732 ValuesToFind.erase(CI: ValueToFindIt);
1733 if (ValuesToFind.empty())
1734 break;
1735 }
1736 }
1737 }
1738
1739 SmallVector<ResolvedDbgOp> NewLocs;
1740 for (const DbgOp &DbgOp : DbgOps) {
1741 if (DbgOp.IsConst) {
1742 NewLocs.push_back(Elt: DbgOp.MO);
1743 continue;
1744 }
1745 LocIdx FoundLoc = FoundLocs.find(Val: DbgOp.ID)->second.getLoc();
1746 if (FoundLoc.isIllegal()) {
1747 NewLocs.clear();
1748 break;
1749 }
1750 NewLocs.push_back(Elt: FoundLoc);
1751 }
1752 // Tell transfer tracker that the variable value has changed.
1753 TTracker->redefVar(MI, Properties, NewLocs);
1754
1755 // If there were values with no location, but all such values are defined in
1756 // later instructions in this block, this is a block-local use-before-def.
1757 if (!DbgOps.empty() && NewLocs.empty()) {
1758 bool IsValidUseBeforeDef = true;
1759 uint64_t LastUseBeforeDef = 0;
1760 for (auto ValueLoc : FoundLocs) {
1761 ValueIDNum NewID = ValueLoc.first;
1762 LocIdx FoundLoc = ValueLoc.second.getLoc();
1763 if (!FoundLoc.isIllegal())
1764 continue;
1765 // If we have an value with no location that is not defined in this block,
1766 // then it has no location in this block, leaving this value undefined.
1767 if (NewID.getBlock() != CurBB || NewID.getInst() <= CurInst) {
1768 IsValidUseBeforeDef = false;
1769 break;
1770 }
1771 LastUseBeforeDef = std::max(a: LastUseBeforeDef, b: NewID.getInst());
1772 }
1773 if (IsValidUseBeforeDef) {
1774 DebugVariableID VID = DVMap.insertDVID(Var&: V, Loc: MI.getDebugLoc().get());
1775 TTracker->addUseBeforeDef(VarID: VID, Properties: {MI.getDebugExpression(), false, true},
1776 DbgOps, Inst: LastUseBeforeDef);
1777 }
1778 }
1779
1780 // Produce a DBG_VALUE representing what this DBG_INSTR_REF meant.
1781 // This DBG_VALUE is potentially a $noreg / undefined location, if
1782 // FoundLoc is illegal.
1783 // (XXX -- could morph the DBG_INSTR_REF in the future).
1784 MachineInstr *DbgMI =
1785 MTracker->emitLoc(DbgOps: NewLocs, Var: V, DILoc: MI.getDebugLoc().get(), Properties);
1786 DebugVariableID ID = DVMap.getDVID(Var: V);
1787
1788 TTracker->PendingDbgValues.push_back(Elt: std::make_pair(x&: ID, y&: DbgMI));
1789 TTracker->flushDbgValues(Pos: MI.getIterator(), MBB: nullptr);
1790 return true;
1791}
1792
1793bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1794 if (!MI.isDebugPHI())
1795 return false;
1796
1797 // Analyse these only when solving the machine value location problem.
1798 if (VTracker || TTracker)
1799 return true;
1800
1801 // First operand is the value location, either a stack slot or register.
1802 // Second is the debug instruction number of the original PHI.
1803 const MachineOperand &MO = MI.getOperand(i: 0);
1804 unsigned InstrNum = MI.getOperand(i: 1).getImm();
1805
1806 auto EmitBadPHI = [this, &MI, InstrNum]() -> bool {
1807 // Helper lambda to do any accounting when we fail to find a location for
1808 // a DBG_PHI. This can happen if DBG_PHIs are malformed, or refer to a
1809 // dead stack slot, for example.
1810 // Record a DebugPHIRecord with an empty value + location.
1811 DebugPHINumToValue.push_back(
1812 Elt: {.InstrNum: InstrNum, .MBB: MI.getParent(), .ValueRead: std::nullopt, .ReadLoc: std::nullopt});
1813 return true;
1814 };
1815
1816 if (MO.isReg() && MO.getReg()) {
1817 // The value is whatever's currently in the register. Read and record it,
1818 // to be analysed later.
1819 Register Reg = MO.getReg();
1820 ValueIDNum Num = MTracker->readReg(R: Reg);
1821 auto PHIRec = DebugPHIRecord(
1822 {.InstrNum: InstrNum, .MBB: MI.getParent(), .ValueRead: Num,
1823 .ReadLoc: MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg))});
1824 DebugPHINumToValue.push_back(Elt: PHIRec);
1825
1826 // Ensure this register is tracked.
1827 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
1828 MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: *RAI));
1829 } else if (MO.isFI()) {
1830 // The value is whatever's in this stack slot.
1831 unsigned FI = MO.getIndex();
1832
1833 // If the stack slot is dead, then this was optimized away.
1834 // FIXME: stack slot colouring should account for slots that get merged.
1835 if (MFI->isDeadObjectIndex(ObjectIdx: FI))
1836 return EmitBadPHI();
1837
1838 // Identify this spill slot, ensure it's tracked.
1839 Register Base;
1840 StackOffset Offs = TFI->getFrameIndexReference(MF: *MI.getMF(), FI, FrameReg&: Base);
1841 SpillLoc SL = {.SpillBase: Base, .SpillOffset: Offs};
1842 std::optional<SpillLocationNo> SpillNo = MTracker->getOrTrackSpillLoc(L: SL);
1843
1844 // We might be able to find a value, but have chosen not to, to avoid
1845 // tracking too much stack information.
1846 if (!SpillNo)
1847 return EmitBadPHI();
1848
1849 // Any stack location DBG_PHI should have an associate bit-size.
1850 assert(MI.getNumOperands() == 3 && "Stack DBG_PHI with no size?");
1851 unsigned slotBitSize = MI.getOperand(i: 2).getImm();
1852
1853 unsigned SpillID = MTracker->getLocID(Spill: *SpillNo, Idx: {slotBitSize, 0});
1854 LocIdx SpillLoc = MTracker->getSpillMLoc(SpillID);
1855 ValueIDNum Result = MTracker->readMLoc(L: SpillLoc);
1856
1857 // Record this DBG_PHI for later analysis.
1858 auto DbgPHI = DebugPHIRecord({.InstrNum: InstrNum, .MBB: MI.getParent(), .ValueRead: Result, .ReadLoc: SpillLoc});
1859 DebugPHINumToValue.push_back(Elt: DbgPHI);
1860 } else {
1861 // Else: if the operand is neither a legal register or a stack slot, then
1862 // we're being fed illegal debug-info. Record an empty PHI, so that any
1863 // debug users trying to read this number will be put off trying to
1864 // interpret the value.
1865 LLVM_DEBUG(
1866 { dbgs() << "Seen DBG_PHI with unrecognised operand format\n"; });
1867 return EmitBadPHI();
1868 }
1869
1870 return true;
1871}
1872
1873void InstrRefBasedLDV::transferRegisterDef(MachineInstr &MI) {
1874 // Meta Instructions do not affect the debug liveness of any register they
1875 // define.
1876 if (MI.isImplicitDef()) {
1877 // Except when there's an implicit def, and the location it's defining has
1878 // no value number. The whole point of an implicit def is to announce that
1879 // the register is live, without be specific about it's value. So define
1880 // a value if there isn't one already.
1881 ValueIDNum Num = MTracker->readReg(R: MI.getOperand(i: 0).getReg());
1882 // Has a legitimate value -> ignore the implicit def.
1883 if (Num.getLoc() != 0)
1884 return;
1885 // Otherwise, def it here.
1886 } else if (MI.isMetaInstruction())
1887 return;
1888
1889 // We always ignore SP defines on call instructions, they don't actually
1890 // change the value of the stack pointer... except for win32's _chkstk. This
1891 // is rare: filter quickly for the common case (no stack adjustments, not a
1892 // call, etc). If it is a call that modifies SP, recognise the SP register
1893 // defs.
1894 bool CallChangesSP = false;
1895 if (AdjustsStackInCalls && MI.isCall() && MI.getOperand(i: 0).isSymbol() &&
1896 !strcmp(s1: MI.getOperand(i: 0).getSymbolName(), s2: StackProbeSymbolName.data()))
1897 CallChangesSP = true;
1898
1899 // Test whether we should ignore a def of this register due to it being part
1900 // of the stack pointer.
1901 auto IgnoreSPAlias = [this, &MI, CallChangesSP](Register R) -> bool {
1902 if (CallChangesSP)
1903 return false;
1904 return MI.isCall() && MTracker->SPAliases.count(V: R);
1905 };
1906
1907 // Find the regs killed by MI, and find regmasks of preserved regs.
1908 // Max out the number of statically allocated elements in `DeadRegs`, as this
1909 // prevents fallback to std::set::count() operations.
1910 SmallSet<uint32_t, 32> DeadRegs;
1911 SmallVector<const uint32_t *, 4> RegMasks;
1912 SmallVector<const MachineOperand *, 4> RegMaskPtrs;
1913 for (const MachineOperand &MO : MI.operands()) {
1914 // Determine whether the operand is a register def.
1915 if (MO.isReg() && MO.isDef() && MO.getReg() && MO.getReg().isPhysical() &&
1916 !IgnoreSPAlias(MO.getReg())) {
1917 // Remove ranges of all aliased registers.
1918 for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI)
1919 // FIXME: Can we break out of this loop early if no insertion occurs?
1920 DeadRegs.insert(V: (*RAI).id());
1921 } else if (MO.isRegMask()) {
1922 RegMasks.push_back(Elt: MO.getRegMask());
1923 RegMaskPtrs.push_back(Elt: &MO);
1924 }
1925 }
1926
1927 // Tell MLocTracker about all definitions, of regmasks and otherwise.
1928 for (uint32_t DeadReg : DeadRegs)
1929 MTracker->defReg(R: DeadReg, BB: CurBB, Inst: CurInst);
1930
1931 for (const auto *MO : RegMaskPtrs)
1932 MTracker->writeRegMask(MO, CurBB, InstID: CurInst);
1933
1934 // If this instruction writes to a spill slot, def that slot.
1935 if (hasFoldedStackStore(MI)) {
1936 if (std::optional<SpillLocationNo> SpillNo =
1937 extractSpillBaseRegAndOffset(MI)) {
1938 for (unsigned int I = 0; I < MTracker->NumSlotIdxes; ++I) {
1939 unsigned SpillID = MTracker->getSpillIDWithIdx(Spill: *SpillNo, Idx: I);
1940 LocIdx L = MTracker->getSpillMLoc(SpillID);
1941 MTracker->setMLoc(L, Num: ValueIDNum(CurBB, CurInst, L));
1942 }
1943 }
1944 }
1945
1946 if (!TTracker)
1947 return;
1948
1949 // When committing variable values to locations: tell transfer tracker that
1950 // we've clobbered things. It may be able to recover the variable from a
1951 // different location.
1952
1953 // Inform TTracker about any direct clobbers.
1954 for (MCRegister DeadReg : DeadRegs) {
1955 LocIdx Loc = MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: DeadReg));
1956 TTracker->clobberMloc(MLoc: Loc, Pos: MI.getIterator(), MakeUndef: false);
1957 }
1958
1959 // Look for any clobbers performed by a register mask. Only test locations
1960 // that are actually being tracked.
1961 if (!RegMaskPtrs.empty()) {
1962 for (auto L : MTracker->locations()) {
1963 // Stack locations can't be clobbered by regmasks.
1964 if (MTracker->isSpill(Idx: L.Idx))
1965 continue;
1966
1967 Register Reg = MTracker->LocIdxToLocID[L.Idx];
1968 if (IgnoreSPAlias(Reg))
1969 continue;
1970
1971 for (const auto *MO : RegMaskPtrs)
1972 if (MO->clobbersPhysReg(PhysReg: Reg))
1973 TTracker->clobberMloc(MLoc: L.Idx, Pos: MI.getIterator(), MakeUndef: false);
1974 }
1975 }
1976
1977 // Tell TTracker about any folded stack store.
1978 if (hasFoldedStackStore(MI)) {
1979 if (std::optional<SpillLocationNo> SpillNo =
1980 extractSpillBaseRegAndOffset(MI)) {
1981 for (unsigned int I = 0; I < MTracker->NumSlotIdxes; ++I) {
1982 unsigned SpillID = MTracker->getSpillIDWithIdx(Spill: *SpillNo, Idx: I);
1983 LocIdx L = MTracker->getSpillMLoc(SpillID);
1984 TTracker->clobberMloc(MLoc: L, Pos: MI.getIterator(), MakeUndef: true);
1985 }
1986 }
1987 }
1988}
1989
1990void InstrRefBasedLDV::performCopy(Register SrcRegNum, Register DstRegNum) {
1991 // In all circumstances, re-def all aliases. It's definitely a new value now.
1992 for (MCRegAliasIterator RAI(DstRegNum, TRI, true); RAI.isValid(); ++RAI)
1993 MTracker->defReg(R: *RAI, BB: CurBB, Inst: CurInst);
1994
1995 ValueIDNum SrcValue = MTracker->readReg(R: SrcRegNum);
1996 MTracker->setReg(R: DstRegNum, ValueID: SrcValue);
1997
1998 // Copy subregisters from one location to another.
1999 for (MCSubRegIndexIterator SRI(SrcRegNum, TRI); SRI.isValid(); ++SRI) {
2000 MCRegister SrcSubReg = SRI.getSubReg();
2001 unsigned SubRegIdx = SRI.getSubRegIndex();
2002 MCRegister DstSubReg = TRI->getSubReg(Reg: DstRegNum, Idx: SubRegIdx);
2003 if (!DstSubReg)
2004 continue;
2005
2006 // Do copy. There are two matching subregisters, the source value should
2007 // have been def'd when the super-reg was, the latter might not be tracked
2008 // yet.
2009 // This will force SrcSubReg to be tracked, if it isn't yet. Will read
2010 // mphi values if it wasn't tracked.
2011 LocIdx SrcL =
2012 MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: SrcSubReg));
2013 LocIdx DstL =
2014 MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: DstSubReg));
2015 (void)SrcL;
2016 (void)DstL;
2017 ValueIDNum CpyValue = MTracker->readReg(R: SrcSubReg);
2018
2019 MTracker->setReg(R: DstSubReg, ValueID: CpyValue);
2020 }
2021}
2022
2023std::optional<SpillLocationNo>
2024InstrRefBasedLDV::isSpillInstruction(const MachineInstr &MI,
2025 MachineFunction *MF) {
2026 // TODO: Handle multiple stores folded into one.
2027 if (!MI.hasOneMemOperand())
2028 return std::nullopt;
2029
2030 // Reject any memory operand that's aliased -- we can't guarantee its value.
2031 auto MMOI = MI.memoperands_begin();
2032 const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue();
2033 if (PVal->isAliased(MFI))
2034 return std::nullopt;
2035
2036 if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII))
2037 return std::nullopt; // This is not a spill instruction, since no valid size
2038 // was returned from either function.
2039
2040 return extractSpillBaseRegAndOffset(MI);
2041}
2042
2043bool InstrRefBasedLDV::isLocationSpill(const MachineInstr &MI,
2044 MachineFunction *MF, unsigned &Reg) {
2045 if (!isSpillInstruction(MI, MF))
2046 return false;
2047
2048 int FI;
2049 Reg = TII->isStoreToStackSlotPostFE(MI, FrameIndex&: FI);
2050 return Reg != 0;
2051}
2052
2053std::optional<SpillLocationNo>
2054InstrRefBasedLDV::isRestoreInstruction(const MachineInstr &MI,
2055 MachineFunction *MF, unsigned &Reg) {
2056 if (!MI.hasOneMemOperand())
2057 return std::nullopt;
2058
2059 // FIXME: Handle folded restore instructions with more than one memory
2060 // operand.
2061 if (MI.getRestoreSize(TII)) {
2062 Reg = MI.getOperand(i: 0).getReg();
2063 return extractSpillBaseRegAndOffset(MI);
2064 }
2065 return std::nullopt;
2066}
2067
2068bool InstrRefBasedLDV::transferSpillOrRestoreInst(MachineInstr &MI) {
2069 // XXX -- it's too difficult to implement VarLocBasedImpl's stack location
2070 // limitations under the new model. Therefore, when comparing them, compare
2071 // versions that don't attempt spills or restores at all.
2072 if (EmulateOldLDV)
2073 return false;
2074
2075 // Strictly limit ourselves to plain loads and stores, not all instructions
2076 // that can access the stack.
2077 int DummyFI = -1;
2078 if (!TII->isStoreToStackSlotPostFE(MI, FrameIndex&: DummyFI) &&
2079 !TII->isLoadFromStackSlotPostFE(MI, FrameIndex&: DummyFI))
2080 return false;
2081
2082 MachineFunction *MF = MI.getMF();
2083 unsigned Reg;
2084
2085 LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump(););
2086
2087 // Strictly limit ourselves to plain loads and stores, not all instructions
2088 // that can access the stack.
2089 int FIDummy;
2090 if (!TII->isStoreToStackSlotPostFE(MI, FrameIndex&: FIDummy) &&
2091 !TII->isLoadFromStackSlotPostFE(MI, FrameIndex&: FIDummy))
2092 return false;
2093
2094 // First, if there are any DBG_VALUEs pointing at a spill slot that is
2095 // written to, terminate that variable location. The value in memory
2096 // will have changed. DbgEntityHistoryCalculator doesn't try to detect this.
2097 if (std::optional<SpillLocationNo> Loc = isSpillInstruction(MI, MF)) {
2098 // Un-set this location and clobber, so that earlier locations don't
2099 // continue past this store.
2100 for (unsigned SlotIdx = 0; SlotIdx < MTracker->NumSlotIdxes; ++SlotIdx) {
2101 unsigned SpillID = MTracker->getSpillIDWithIdx(Spill: *Loc, Idx: SlotIdx);
2102 std::optional<LocIdx> MLoc = MTracker->getSpillMLoc(SpillID);
2103 if (!MLoc)
2104 continue;
2105
2106 // We need to over-write the stack slot with something (here, a def at
2107 // this instruction) to ensure no values are preserved in this stack slot
2108 // after the spill. It also prevents TTracker from trying to recover the
2109 // location and re-installing it in the same place.
2110 ValueIDNum Def(CurBB, CurInst, *MLoc);
2111 MTracker->setMLoc(L: *MLoc, Num: Def);
2112 if (TTracker)
2113 TTracker->clobberMloc(MLoc: *MLoc, Pos: MI.getIterator());
2114 }
2115 }
2116
2117 // Try to recognise spill and restore instructions that may transfer a value.
2118 if (isLocationSpill(MI, MF, Reg)) {
2119 // isLocationSpill returning true should guarantee we can extract a
2120 // location.
2121 SpillLocationNo Loc = *extractSpillBaseRegAndOffset(MI);
2122
2123 auto DoTransfer = [&](Register SrcReg, unsigned SpillID) {
2124 auto ReadValue = MTracker->readReg(R: SrcReg);
2125 LocIdx DstLoc = MTracker->getSpillMLoc(SpillID);
2126 MTracker->setMLoc(L: DstLoc, Num: ReadValue);
2127
2128 if (TTracker) {
2129 LocIdx SrcLoc = MTracker->getRegMLoc(R: SrcReg);
2130 TTracker->transferMlocs(Src: SrcLoc, Dst: DstLoc, Pos: MI.getIterator());
2131 }
2132 };
2133
2134 // Then, transfer subreg bits.
2135 for (MCPhysReg SR : TRI->subregs(Reg)) {
2136 // Ensure this reg is tracked,
2137 (void)MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: SR));
2138 unsigned SubregIdx = TRI->getSubRegIndex(RegNo: Reg, SubRegNo: SR);
2139 unsigned SpillID = MTracker->getLocID(Spill: Loc, SpillSubReg: SubregIdx);
2140 DoTransfer(SR, SpillID);
2141 }
2142
2143 // Directly lookup size of main source reg, and transfer.
2144 unsigned Size = TRI->getRegSizeInBits(Reg, MRI: *MRI);
2145 unsigned SpillID = MTracker->getLocID(Spill: Loc, Idx: {Size, 0});
2146 DoTransfer(Reg, SpillID);
2147 } else {
2148 std::optional<SpillLocationNo> Loc = isRestoreInstruction(MI, MF, Reg);
2149 if (!Loc)
2150 return false;
2151
2152 // Assumption: we're reading from the base of the stack slot, not some
2153 // offset into it. It seems very unlikely LLVM would ever generate
2154 // restores where this wasn't true. This then becomes a question of what
2155 // subregisters in the destination register line up with positions in the
2156 // stack slot.
2157
2158 // Def all registers that alias the destination.
2159 for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI)
2160 MTracker->defReg(R: *RAI, BB: CurBB, Inst: CurInst);
2161
2162 // Now find subregisters within the destination register, and load values
2163 // from stack slot positions.
2164 auto DoTransfer = [&](Register DestReg, unsigned SpillID) {
2165 LocIdx SrcIdx = MTracker->getSpillMLoc(SpillID);
2166 auto ReadValue = MTracker->readMLoc(L: SrcIdx);
2167 MTracker->setReg(R: DestReg, ValueID: ReadValue);
2168 };
2169
2170 for (MCPhysReg SR : TRI->subregs(Reg)) {
2171 unsigned Subreg = TRI->getSubRegIndex(RegNo: Reg, SubRegNo: SR);
2172 unsigned SpillID = MTracker->getLocID(Spill: *Loc, SpillSubReg: Subreg);
2173 DoTransfer(SR, SpillID);
2174 }
2175
2176 // Directly look up this registers slot idx by size, and transfer.
2177 unsigned Size = TRI->getRegSizeInBits(Reg, MRI: *MRI);
2178 unsigned SpillID = MTracker->getLocID(Spill: *Loc, Idx: {Size, 0});
2179 DoTransfer(Reg, SpillID);
2180 }
2181 return true;
2182}
2183
2184bool InstrRefBasedLDV::transferRegisterCopy(MachineInstr &MI) {
2185 auto DestSrc = TII->isCopyLikeInstr(MI);
2186 if (!DestSrc)
2187 return false;
2188
2189 const MachineOperand *DestRegOp = DestSrc->Destination;
2190 const MachineOperand *SrcRegOp = DestSrc->Source;
2191
2192 Register SrcReg = SrcRegOp->getReg();
2193 Register DestReg = DestRegOp->getReg();
2194
2195 // Ignore identity copies. Yep, these make it as far as LiveDebugValues.
2196 if (SrcReg == DestReg)
2197 return true;
2198
2199 // For emulating VarLocBasedImpl:
2200 // We want to recognize instructions where destination register is callee
2201 // saved register. If register that could be clobbered by the call is
2202 // included, there would be a great chance that it is going to be clobbered
2203 // soon. It is more likely that previous register, which is callee saved, is
2204 // going to stay unclobbered longer, even if it is killed.
2205 //
2206 // For InstrRefBasedImpl, we can track multiple locations per value, so
2207 // ignore this condition.
2208 if (EmulateOldLDV && !isCalleeSavedReg(R: DestReg))
2209 return false;
2210
2211 // InstrRefBasedImpl only followed killing copies.
2212 if (EmulateOldLDV && !SrcRegOp->isKill())
2213 return false;
2214
2215 // Before we update MTracker, remember which values were present in each of
2216 // the locations about to be overwritten, so that we can recover any
2217 // potentially clobbered variables.
2218 DenseMap<LocIdx, ValueIDNum> ClobberedLocs;
2219 if (TTracker) {
2220 for (MCRegAliasIterator RAI(DestReg, TRI, true); RAI.isValid(); ++RAI) {
2221 LocIdx ClobberedLoc = MTracker->getRegMLoc(R: *RAI);
2222 auto MLocIt = TTracker->ActiveMLocs.find(Val: ClobberedLoc);
2223 // If ActiveMLocs isn't tracking this location or there are no variables
2224 // using it, don't bother remembering.
2225 if (MLocIt == TTracker->ActiveMLocs.end() || MLocIt->second.empty())
2226 continue;
2227 ValueIDNum Value = MTracker->readReg(R: *RAI);
2228 ClobberedLocs[ClobberedLoc] = Value;
2229 }
2230 }
2231
2232 // Copy MTracker info, including subregs if available.
2233 InstrRefBasedLDV::performCopy(SrcRegNum: SrcReg, DstRegNum: DestReg);
2234
2235 // The copy might have clobbered variables based on the destination register.
2236 // Tell TTracker about it, passing the old ValueIDNum to search for
2237 // alternative locations (or else terminating those variables).
2238 if (TTracker) {
2239 for (auto LocVal : ClobberedLocs) {
2240 TTracker->clobberMloc(MLoc: LocVal.first, OldValue: LocVal.second, Pos: MI.getIterator(), MakeUndef: false);
2241 }
2242 }
2243
2244 // Only produce a transfer of DBG_VALUE within a block where old LDV
2245 // would have. We might make use of the additional value tracking in some
2246 // other way, later.
2247 if (TTracker && isCalleeSavedReg(R: DestReg) && SrcRegOp->isKill())
2248 TTracker->transferMlocs(Src: MTracker->getRegMLoc(R: SrcReg),
2249 Dst: MTracker->getRegMLoc(R: DestReg), Pos: MI.getIterator());
2250
2251 // VarLocBasedImpl would quit tracking the old location after copying.
2252 if (EmulateOldLDV && SrcReg != DestReg)
2253 MTracker->defReg(R: SrcReg, BB: CurBB, Inst: CurInst);
2254
2255 return true;
2256}
2257
2258/// Accumulate a mapping between each DILocalVariable fragment and other
2259/// fragments of that DILocalVariable which overlap. This reduces work during
2260/// the data-flow stage from "Find any overlapping fragments" to "Check if the
2261/// known-to-overlap fragments are present".
2262/// \param MI A previously unprocessed debug instruction to analyze for
2263/// fragment usage.
2264void InstrRefBasedLDV::accumulateFragmentMap(MachineInstr &MI) {
2265 assert(MI.isDebugValueLike());
2266 DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(),
2267 MI.getDebugLoc()->getInlinedAt());
2268 FragmentInfo ThisFragment = MIVar.getFragmentOrDefault();
2269
2270 // If this is the first sighting of this variable, then we are guaranteed
2271 // there are currently no overlapping fragments either. Initialize the set
2272 // of seen fragments, record no overlaps for the current one, and return.
2273 auto [SeenIt, Inserted] = SeenFragments.try_emplace(Key: MIVar.getVariable());
2274 if (Inserted) {
2275 SeenIt->second.insert(V: ThisFragment);
2276
2277 OverlapFragments.insert(KV: {{MIVar.getVariable(), ThisFragment}, {}});
2278 return;
2279 }
2280
2281 // If this particular Variable/Fragment pair already exists in the overlap
2282 // map, it has already been accounted for.
2283 auto IsInOLapMap =
2284 OverlapFragments.insert(KV: {{MIVar.getVariable(), ThisFragment}, {}});
2285 if (!IsInOLapMap.second)
2286 return;
2287
2288 auto &ThisFragmentsOverlaps = IsInOLapMap.first->second;
2289 auto &AllSeenFragments = SeenIt->second;
2290
2291 // Otherwise, examine all other seen fragments for this variable, with "this"
2292 // fragment being a previously unseen fragment. Record any pair of
2293 // overlapping fragments.
2294 for (const auto &ASeenFragment : AllSeenFragments) {
2295 // Does this previously seen fragment overlap?
2296 if (DIExpression::fragmentsOverlap(A: ThisFragment, B: ASeenFragment)) {
2297 // Yes: Mark the current fragment as being overlapped.
2298 ThisFragmentsOverlaps.push_back(Elt: ASeenFragment);
2299 // Mark the previously seen fragment as being overlapped by the current
2300 // one.
2301 auto ASeenFragmentsOverlaps =
2302 OverlapFragments.find(Val: {MIVar.getVariable(), ASeenFragment});
2303 assert(ASeenFragmentsOverlaps != OverlapFragments.end() &&
2304 "Previously seen var fragment has no vector of overlaps");
2305 ASeenFragmentsOverlaps->second.push_back(Elt: ThisFragment);
2306 }
2307 }
2308
2309 AllSeenFragments.insert(V: ThisFragment);
2310}
2311
2312void InstrRefBasedLDV::process(MachineInstr &MI,
2313 const FuncValueTable *MLiveOuts,
2314 const FuncValueTable *MLiveIns) {
2315 // Try to interpret an MI as a debug or transfer instruction. Only if it's
2316 // none of these should we interpret it's register defs as new value
2317 // definitions.
2318 if (transferDebugValue(MI))
2319 return;
2320 if (transferDebugInstrRef(MI, MLiveOuts, MLiveIns))
2321 return;
2322 if (transferDebugPHI(MI))
2323 return;
2324 if (transferRegisterCopy(MI))
2325 return;
2326 if (transferSpillOrRestoreInst(MI))
2327 return;
2328 transferRegisterDef(MI);
2329}
2330
2331void InstrRefBasedLDV::produceMLocTransferFunction(
2332 MachineFunction &MF, SmallVectorImpl<MLocTransferMap> &MLocTransfer,
2333 unsigned MaxNumBlocks) {
2334 // Because we try to optimize around register mask operands by ignoring regs
2335 // that aren't currently tracked, we set up something ugly for later: RegMask
2336 // operands that are seen earlier than the first use of a register, still need
2337 // to clobber that register in the transfer function. But this information
2338 // isn't actively recorded. Instead, we track each RegMask used in each block,
2339 // and accumulated the clobbered but untracked registers in each block into
2340 // the following bitvector. Later, if new values are tracked, we can add
2341 // appropriate clobbers.
2342 SmallVector<BitVector, 32> BlockMasks;
2343 BlockMasks.resize(N: MaxNumBlocks);
2344
2345 // Reserve one bit per register for the masks described above.
2346 unsigned BVWords = MachineOperand::getRegMaskSize(NumRegs: TRI->getNumRegs());
2347 for (auto &BV : BlockMasks)
2348 BV.resize(N: TRI->getNumRegs(), t: true);
2349
2350 // Step through all instructions and inhale the transfer function.
2351 for (auto &MBB : MF) {
2352 // Object fields that are read by trackers to know where we are in the
2353 // function.
2354 CurBB = MBB.getNumber();
2355 CurInst = 1;
2356
2357 // Set all machine locations to a PHI value. For transfer function
2358 // production only, this signifies the live-in value to the block.
2359 MTracker->reset();
2360 MTracker->setMPhis(CurBB);
2361
2362 // Step through each instruction in this block.
2363 for (auto &MI : MBB) {
2364 // Pass in an empty unique_ptr for the value tables when accumulating the
2365 // machine transfer function.
2366 process(MI, MLiveOuts: nullptr, MLiveIns: nullptr);
2367
2368 // Also accumulate fragment map.
2369 if (MI.isDebugValueLike())
2370 accumulateFragmentMap(MI);
2371
2372 // Create a map from the instruction number (if present) to the
2373 // MachineInstr and its position.
2374 if (uint64_t InstrNo = MI.peekDebugInstrNum()) {
2375 auto InstrAndPos = std::make_pair(x: &MI, y&: CurInst);
2376 auto InsertResult =
2377 DebugInstrNumToInstr.insert(x: std::make_pair(x&: InstrNo, y&: InstrAndPos));
2378
2379 // There should never be duplicate instruction numbers.
2380 assert(InsertResult.second);
2381 (void)InsertResult;
2382 }
2383
2384 ++CurInst;
2385 }
2386
2387 // Produce the transfer function, a map of machine location to new value. If
2388 // any machine location has the live-in phi value from the start of the
2389 // block, it's live-through and doesn't need recording in the transfer
2390 // function.
2391 for (auto Location : MTracker->locations()) {
2392 LocIdx Idx = Location.Idx;
2393 ValueIDNum &P = Location.Value;
2394 if (P.isPHI() && P.getLoc() == Idx.asU64())
2395 continue;
2396
2397 // Insert-or-update.
2398 auto &TransferMap = MLocTransfer[CurBB];
2399 auto Result = TransferMap.insert(KV: std::make_pair(x: Idx.asU64(), y&: P));
2400 if (!Result.second)
2401 Result.first->second = P;
2402 }
2403
2404 // Accumulate any bitmask operands into the clobbered reg mask for this
2405 // block.
2406 for (auto &P : MTracker->Masks) {
2407 BlockMasks[CurBB].clearBitsNotInMask(Mask: P.first->getRegMask(), MaskWords: BVWords);
2408 }
2409 }
2410
2411 // Compute a bitvector of all the registers that are tracked in this block.
2412 BitVector UsedRegs(TRI->getNumRegs());
2413 for (auto Location : MTracker->locations()) {
2414 unsigned ID = MTracker->LocIdxToLocID[Location.Idx];
2415 // Ignore stack slots, and aliases of the stack pointer.
2416 if (ID >= TRI->getNumRegs() || MTracker->SPAliases.count(V: ID))
2417 continue;
2418 UsedRegs.set(ID);
2419 }
2420
2421 // Check that any regmask-clobber of a register that gets tracked, is not
2422 // live-through in the transfer function. It needs to be clobbered at the
2423 // very least.
2424 for (unsigned int I = 0; I < MaxNumBlocks; ++I) {
2425 BitVector &BV = BlockMasks[I];
2426 BV.flip();
2427 BV &= UsedRegs;
2428 // This produces all the bits that we clobber, but also use. Check that
2429 // they're all clobbered or at least set in the designated transfer
2430 // elem.
2431 for (unsigned Bit : BV.set_bits()) {
2432 unsigned ID = MTracker->getLocID(Reg: Bit);
2433 LocIdx Idx = MTracker->LocIDToLocIdx[ID];
2434 auto &TransferMap = MLocTransfer[I];
2435
2436 // Install a value representing the fact that this location is effectively
2437 // written to in this block. As there's no reserved value, instead use
2438 // a value number that is never generated. Pick the value number for the
2439 // first instruction in the block, def'ing this location, which we know
2440 // this block never used anyway.
2441 ValueIDNum NotGeneratedNum = ValueIDNum(I, 1, Idx);
2442 auto Result =
2443 TransferMap.insert(KV: std::make_pair(x: Idx.asU64(), y&: NotGeneratedNum));
2444 if (!Result.second) {
2445 ValueIDNum &ValueID = Result.first->second;
2446 if (ValueID.getBlock() == I && ValueID.isPHI())
2447 // It was left as live-through. Set it to clobbered.
2448 ValueID = NotGeneratedNum;
2449 }
2450 }
2451 }
2452}
2453
2454bool InstrRefBasedLDV::mlocJoin(
2455 MachineBasicBlock &MBB, SmallPtrSet<const MachineBasicBlock *, 16> &Visited,
2456 FuncValueTable &OutLocs, ValueTable &InLocs) {
2457 LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
2458 bool Changed = false;
2459
2460 // Handle value-propagation when control flow merges on entry to a block. For
2461 // any location without a PHI already placed, the location has the same value
2462 // as its predecessors. If a PHI is placed, test to see whether it's now a
2463 // redundant PHI that we can eliminate.
2464
2465 SmallVector<const MachineBasicBlock *, 8> BlockOrders(MBB.predecessors());
2466
2467 // Visit predecessors in RPOT order.
2468 auto Cmp = [&](const MachineBasicBlock *A, const MachineBasicBlock *B) {
2469 return BBToOrder.find(Val: A)->second < BBToOrder.find(Val: B)->second;
2470 };
2471 llvm::sort(C&: BlockOrders, Comp: Cmp);
2472
2473 // Skip entry block.
2474 if (BlockOrders.size() == 0) {
2475 // FIXME: We don't use assert here to prevent instr-ref-unreachable.mir
2476 // failing.
2477 LLVM_DEBUG(if (!MBB.isEntryBlock()) dbgs()
2478 << "Found not reachable block " << MBB.getFullName()
2479 << " from entry which may lead out of "
2480 "bound access to VarLocs\n");
2481 return false;
2482 }
2483
2484 // Step through all machine locations, look at each predecessor and test
2485 // whether we can eliminate redundant PHIs.
2486 for (auto Location : MTracker->locations()) {
2487 LocIdx Idx = Location.Idx;
2488
2489 // Pick out the first predecessors live-out value for this location. It's
2490 // guaranteed to not be a backedge, as we order by RPO.
2491 ValueIDNum FirstVal = OutLocs[*BlockOrders[0]][Idx.asU64()];
2492
2493 // If we've already eliminated a PHI here, do no further checking, just
2494 // propagate the first live-in value into this block.
2495 if (InLocs[Idx.asU64()] != ValueIDNum(MBB.getNumber(), 0, Idx)) {
2496 if (InLocs[Idx.asU64()] != FirstVal) {
2497 InLocs[Idx.asU64()] = FirstVal;
2498 Changed |= true;
2499 }
2500 continue;
2501 }
2502
2503 // We're now examining a PHI to see whether it's un-necessary. Loop around
2504 // the other live-in values and test whether they're all the same.
2505 bool Disagree = false;
2506 for (unsigned int I = 1; I < BlockOrders.size(); ++I) {
2507 const MachineBasicBlock *PredMBB = BlockOrders[I];
2508 const ValueIDNum &PredLiveOut = OutLocs[*PredMBB][Idx.asU64()];
2509
2510 // Incoming values agree, continue trying to eliminate this PHI.
2511 if (FirstVal == PredLiveOut)
2512 continue;
2513
2514 // We can also accept a PHI value that feeds back into itself.
2515 if (PredLiveOut == ValueIDNum(MBB.getNumber(), 0, Idx))
2516 continue;
2517
2518 // Live-out of a predecessor disagrees with the first predecessor.
2519 Disagree = true;
2520 }
2521
2522 // No disagreement? No PHI. Otherwise, leave the PHI in live-ins.
2523 if (!Disagree) {
2524 InLocs[Idx.asU64()] = FirstVal;
2525 Changed |= true;
2526 }
2527 }
2528
2529 // TODO: Reimplement NumInserted and NumRemoved.
2530 return Changed;
2531}
2532
2533void InstrRefBasedLDV::findStackIndexInterference(
2534 SmallVectorImpl<unsigned> &Slots) {
2535 // We could spend a bit of time finding the exact, minimal, set of stack
2536 // indexes that interfere with each other, much like reg units. Or, we can
2537 // rely on the fact that:
2538 // * The smallest / lowest index will interfere with everything at zero
2539 // offset, which will be the largest set of registers,
2540 // * Most indexes with non-zero offset will end up being interference units
2541 // anyway.
2542 // So just pick those out and return them.
2543
2544 // We can rely on a single-byte stack index existing already, because we
2545 // initialize them in MLocTracker.
2546 auto It = MTracker->StackSlotIdxes.find(Val: {8, 0});
2547 assert(It != MTracker->StackSlotIdxes.end());
2548 Slots.push_back(Elt: It->second);
2549
2550 // Find anything that has a non-zero offset and add that too.
2551 for (auto &Pair : MTracker->StackSlotIdxes) {
2552 // Is offset zero? If so, ignore.
2553 if (!Pair.first.second)
2554 continue;
2555 Slots.push_back(Elt: Pair.second);
2556 }
2557}
2558
2559void InstrRefBasedLDV::placeMLocPHIs(
2560 MachineFunction &MF, SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
2561 FuncValueTable &MInLocs, SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
2562 SmallVector<unsigned, 4> StackUnits;
2563 findStackIndexInterference(Slots&: StackUnits);
2564
2565 // To avoid repeatedly running the PHI placement algorithm, leverage the
2566 // fact that a def of register MUST also def its register units. Find the
2567 // units for registers, place PHIs for them, and then replicate them for
2568 // aliasing registers. Some inputs that are never def'd (DBG_PHIs of
2569 // arguments) don't lead to register units being tracked, just place PHIs for
2570 // those registers directly. Stack slots have their own form of "unit",
2571 // store them to one side.
2572 SmallSet<Register, 32> RegUnitsToPHIUp;
2573 SmallSet<LocIdx, 32> NormalLocsToPHI;
2574 SmallSet<SpillLocationNo, 32> StackSlots;
2575 for (auto Location : MTracker->locations()) {
2576 LocIdx L = Location.Idx;
2577 if (MTracker->isSpill(Idx: L)) {
2578 StackSlots.insert(V: MTracker->locIDToSpill(ID: MTracker->LocIdxToLocID[L]));
2579 continue;
2580 }
2581
2582 Register R = MTracker->LocIdxToLocID[L];
2583 SmallSet<Register, 8> FoundRegUnits;
2584 bool AnyIllegal = false;
2585 for (MCRegUnit Unit : TRI->regunits(Reg: R.asMCReg())) {
2586 for (MCRegUnitRootIterator URoot(Unit, TRI); URoot.isValid(); ++URoot) {
2587 if (!MTracker->isRegisterTracked(R: *URoot)) {
2588 // Not all roots were loaded into the tracking map: this register
2589 // isn't actually def'd anywhere, we only read from it. Generate PHIs
2590 // for this reg, but don't iterate units.
2591 AnyIllegal = true;
2592 } else {
2593 FoundRegUnits.insert(V: *URoot);
2594 }
2595 }
2596 }
2597
2598 if (AnyIllegal) {
2599 NormalLocsToPHI.insert(V: L);
2600 continue;
2601 }
2602
2603 RegUnitsToPHIUp.insert_range(R&: FoundRegUnits);
2604 }
2605
2606 // Lambda to fetch PHIs for a given location, and write into the PHIBlocks
2607 // collection.
2608 SmallVector<MachineBasicBlock *, 32> PHIBlocks;
2609 auto CollectPHIsForLoc = [&](LocIdx L) {
2610 // Collect the set of defs.
2611 SmallPtrSet<MachineBasicBlock *, 32> DefBlocks;
2612 for (MachineBasicBlock *MBB : OrderToBB) {
2613 const auto &TransferFunc = MLocTransfer[MBB->getNumber()];
2614 if (TransferFunc.contains(Val: L))
2615 DefBlocks.insert(Ptr: MBB);
2616 }
2617
2618 // The entry block defs the location too: it's the live-in / argument value.
2619 // Only insert if there are other defs though; everything is trivially live
2620 // through otherwise.
2621 if (!DefBlocks.empty())
2622 DefBlocks.insert(Ptr: &*MF.begin());
2623
2624 // Ask the SSA construction algorithm where we should put PHIs. Clear
2625 // anything that might have been hanging around from earlier.
2626 PHIBlocks.clear();
2627 BlockPHIPlacement(AllBlocks, DefBlocks, PHIBlocks);
2628 };
2629
2630 auto InstallPHIsAtLoc = [&PHIBlocks, &MInLocs](LocIdx L) {
2631 for (const MachineBasicBlock *MBB : PHIBlocks)
2632 MInLocs[*MBB][L.asU64()] = ValueIDNum(MBB->getNumber(), 0, L);
2633 };
2634
2635 // For locations with no reg units, just place PHIs.
2636 for (LocIdx L : NormalLocsToPHI) {
2637 CollectPHIsForLoc(L);
2638 // Install those PHI values into the live-in value array.
2639 InstallPHIsAtLoc(L);
2640 }
2641
2642 // For stack slots, calculate PHIs for the equivalent of the units, then
2643 // install for each index.
2644 for (SpillLocationNo Slot : StackSlots) {
2645 for (unsigned Idx : StackUnits) {
2646 unsigned SpillID = MTracker->getSpillIDWithIdx(Spill: Slot, Idx);
2647 LocIdx L = MTracker->getSpillMLoc(SpillID);
2648 CollectPHIsForLoc(L);
2649 InstallPHIsAtLoc(L);
2650
2651 // Find anything that aliases this stack index, install PHIs for it too.
2652 unsigned Size, Offset;
2653 std::tie(args&: Size, args&: Offset) = MTracker->StackIdxesToPos[Idx];
2654 for (auto &Pair : MTracker->StackSlotIdxes) {
2655 unsigned ThisSize, ThisOffset;
2656 std::tie(args&: ThisSize, args&: ThisOffset) = Pair.first;
2657 if (ThisSize + ThisOffset <= Offset || Size + Offset <= ThisOffset)
2658 continue;
2659
2660 unsigned ThisID = MTracker->getSpillIDWithIdx(Spill: Slot, Idx: Pair.second);
2661 LocIdx ThisL = MTracker->getSpillMLoc(SpillID: ThisID);
2662 InstallPHIsAtLoc(ThisL);
2663 }
2664 }
2665 }
2666
2667 // For reg units, place PHIs, and then place them for any aliasing registers.
2668 for (Register R : RegUnitsToPHIUp) {
2669 LocIdx L = MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: R));
2670 CollectPHIsForLoc(L);
2671
2672 // Install those PHI values into the live-in value array.
2673 InstallPHIsAtLoc(L);
2674
2675 // Now find aliases and install PHIs for those.
2676 for (MCRegAliasIterator RAI(R, TRI, true); RAI.isValid(); ++RAI) {
2677 // Super-registers that are "above" the largest register read/written by
2678 // the function will alias, but will not be tracked.
2679 if (!MTracker->isRegisterTracked(R: *RAI))
2680 continue;
2681
2682 LocIdx AliasLoc =
2683 MTracker->lookupOrTrackRegister(ID: MTracker->getLocID(Reg: *RAI));
2684 InstallPHIsAtLoc(AliasLoc);
2685 }
2686 }
2687}
2688
2689void InstrRefBasedLDV::buildMLocValueMap(
2690 MachineFunction &MF, FuncValueTable &MInLocs, FuncValueTable &MOutLocs,
2691 SmallVectorImpl<MLocTransferMap> &MLocTransfer) {
2692 std::priority_queue<unsigned int, std::vector<unsigned int>,
2693 std::greater<unsigned int>>
2694 Worklist, Pending;
2695
2696 // We track what is on the current and pending worklist to avoid inserting
2697 // the same thing twice. We could avoid this with a custom priority queue,
2698 // but this is probably not worth it.
2699 SmallPtrSet<MachineBasicBlock *, 16> OnPending, OnWorklist;
2700
2701 // Initialize worklist with every block to be visited. Also produce list of
2702 // all blocks.
2703 SmallPtrSet<MachineBasicBlock *, 32> AllBlocks;
2704 for (unsigned int I = 0; I < BBToOrder.size(); ++I) {
2705 Worklist.push(x: I);
2706 OnWorklist.insert(Ptr: OrderToBB[I]);
2707 AllBlocks.insert(Ptr: OrderToBB[I]);
2708 }
2709
2710 // Initialize entry block to PHIs. These represent arguments.
2711 for (auto Location : MTracker->locations())
2712 MInLocs.tableForEntryMBB()[Location.Idx.asU64()] =
2713 ValueIDNum(0, 0, Location.Idx);
2714
2715 MTracker->reset();
2716
2717 // Start by placing PHIs, using the usual SSA constructor algorithm. Consider
2718 // any machine-location that isn't live-through a block to be def'd in that
2719 // block.
2720 placeMLocPHIs(MF, AllBlocks, MInLocs, MLocTransfer);
2721
2722 // Propagate values to eliminate redundant PHIs. At the same time, this
2723 // produces the table of Block x Location => Value for the entry to each
2724 // block.
2725 // The kind of PHIs we can eliminate are, for example, where one path in a
2726 // conditional spills and restores a register, and the register still has
2727 // the same value once control flow joins, unbeknowns to the PHI placement
2728 // code. Propagating values allows us to identify such un-necessary PHIs and
2729 // remove them.
2730 SmallPtrSet<const MachineBasicBlock *, 16> Visited;
2731 while (!Worklist.empty() || !Pending.empty()) {
2732 // Vector for storing the evaluated block transfer function.
2733 SmallVector<std::pair<LocIdx, ValueIDNum>, 32> ToRemap;
2734
2735 while (!Worklist.empty()) {
2736 MachineBasicBlock *MBB = OrderToBB[Worklist.top()];
2737 CurBB = MBB->getNumber();
2738 Worklist.pop();
2739
2740 // Join the values in all predecessor blocks.
2741 bool InLocsChanged;
2742 InLocsChanged = mlocJoin(MBB&: *MBB, Visited, OutLocs&: MOutLocs, InLocs&: MInLocs[*MBB]);
2743 InLocsChanged |= Visited.insert(Ptr: MBB).second;
2744
2745 // Don't examine transfer function if we've visited this loc at least
2746 // once, and inlocs haven't changed.
2747 if (!InLocsChanged)
2748 continue;
2749
2750 // Load the current set of live-ins into MLocTracker.
2751 MTracker->loadFromArray(Locs&: MInLocs[*MBB], NewCurBB: CurBB);
2752
2753 // Each element of the transfer function can be a new def, or a read of
2754 // a live-in value. Evaluate each element, and store to "ToRemap".
2755 ToRemap.clear();
2756 for (auto &P : MLocTransfer[CurBB]) {
2757 if (P.second.getBlock() == CurBB && P.second.isPHI()) {
2758 // This is a movement of whatever was live in. Read it.
2759 ValueIDNum NewID = MTracker->readMLoc(L: P.second.getLoc());
2760 ToRemap.push_back(Elt: std::make_pair(x&: P.first, y&: NewID));
2761 } else {
2762 // It's a def. Just set it.
2763 assert(P.second.getBlock() == CurBB);
2764 ToRemap.push_back(Elt: std::make_pair(x&: P.first, y&: P.second));
2765 }
2766 }
2767
2768 // Commit the transfer function changes into mloc tracker, which
2769 // transforms the contents of the MLocTracker into the live-outs.
2770 for (auto &P : ToRemap)
2771 MTracker->setMLoc(L: P.first, Num: P.second);
2772
2773 // Now copy out-locs from mloc tracker into out-loc vector, checking
2774 // whether changes have occurred. These changes can have come from both
2775 // the transfer function, and mlocJoin.
2776 bool OLChanged = false;
2777 for (auto Location : MTracker->locations()) {
2778 OLChanged |= MOutLocs[*MBB][Location.Idx.asU64()] != Location.Value;
2779 MOutLocs[*MBB][Location.Idx.asU64()] = Location.Value;
2780 }
2781
2782 MTracker->reset();
2783
2784 // No need to examine successors again if out-locs didn't change.
2785 if (!OLChanged)
2786 continue;
2787
2788 // All successors should be visited: put any back-edges on the pending
2789 // list for the next pass-through, and any other successors to be
2790 // visited this pass, if they're not going to be already.
2791 for (auto *s : MBB->successors()) {
2792 // Does branching to this successor represent a back-edge?
2793 unsigned Order = BBToOrder[s];
2794 if (Order > BBToOrder[MBB]) {
2795 // No: visit it during this dataflow iteration.
2796 if (OnWorklist.insert(Ptr: s).second)
2797 Worklist.push(x: Order);
2798 } else {
2799 // Yes: visit it on the next iteration.
2800 if (OnPending.insert(Ptr: s).second)
2801 Pending.push(x: Order);
2802 }
2803 }
2804 }
2805
2806 Worklist.swap(pq&: Pending);
2807 std::swap(LHS&: OnPending, RHS&: OnWorklist);
2808 OnPending.clear();
2809 // At this point, pending must be empty, since it was just the empty
2810 // worklist
2811 assert(Pending.empty() && "Pending should be empty");
2812 }
2813
2814 // Once all the live-ins don't change on mlocJoin(), we've eliminated all
2815 // redundant PHIs.
2816}
2817
2818void InstrRefBasedLDV::BlockPHIPlacement(
2819 const SmallPtrSetImpl<MachineBasicBlock *> &AllBlocks,
2820 const SmallPtrSetImpl<MachineBasicBlock *> &DefBlocks,
2821 SmallVectorImpl<MachineBasicBlock *> &PHIBlocks) {
2822 // Apply IDF calculator to the designated set of location defs, storing
2823 // required PHIs into PHIBlocks. Uses the dominator tree stored in the
2824 // InstrRefBasedLDV object.
2825 IDFCalculatorBase<MachineBasicBlock, false> IDF(*DomTree);
2826
2827 IDF.setLiveInBlocks(AllBlocks);
2828 IDF.setDefiningBlocks(DefBlocks);
2829 IDF.calculate(IDFBlocks&: PHIBlocks);
2830}
2831
2832bool InstrRefBasedLDV::pickVPHILoc(
2833 SmallVectorImpl<DbgOpID> &OutValues, const MachineBasicBlock &MBB,
2834 const LiveIdxT &LiveOuts, FuncValueTable &MOutLocs,
2835 const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) {
2836
2837 // No predecessors means no PHIs.
2838 if (BlockOrders.empty())
2839 return false;
2840
2841 // All the location operands that do not already agree need to be joined,
2842 // track the indices of each such location operand here.
2843 SmallDenseSet<unsigned> LocOpsToJoin;
2844
2845 auto FirstValueIt = LiveOuts.find(Val: BlockOrders[0]);
2846 if (FirstValueIt == LiveOuts.end())
2847 return false;
2848 const DbgValue &FirstValue = *FirstValueIt->second;
2849
2850 for (const auto p : BlockOrders) {
2851 auto OutValIt = LiveOuts.find(Val: p);
2852 if (OutValIt == LiveOuts.end())
2853 // If we have a predecessor not in scope, we'll never find a PHI position.
2854 return false;
2855 const DbgValue &OutVal = *OutValIt->second;
2856
2857 // No-values cannot have locations we can join on.
2858 if (OutVal.Kind == DbgValue::NoVal)
2859 return false;
2860
2861 // For unjoined VPHIs where we don't know the location, we definitely
2862 // can't find a join loc unless the VPHI is a backedge.
2863 if (OutVal.isUnjoinedPHI() && OutVal.BlockNo != MBB.getNumber())
2864 return false;
2865
2866 if (!FirstValue.Properties.isJoinable(Other: OutVal.Properties))
2867 return false;
2868
2869 for (unsigned Idx = 0; Idx < FirstValue.getLocationOpCount(); ++Idx) {
2870 // An unjoined PHI has no defined locations, and so a shared location must
2871 // be found for every operand.
2872 if (OutVal.isUnjoinedPHI()) {
2873 LocOpsToJoin.insert(V: Idx);
2874 continue;
2875 }
2876 DbgOpID FirstValOp = FirstValue.getDbgOpID(Index: Idx);
2877 DbgOpID OutValOp = OutVal.getDbgOpID(Index: Idx);
2878 if (FirstValOp != OutValOp) {
2879 // We can never join constant ops - the ops must either both be equal
2880 // constant ops or non-const ops.
2881 if (FirstValOp.isConst() || OutValOp.isConst())
2882 return false;
2883 else
2884 LocOpsToJoin.insert(V: Idx);
2885 }
2886 }
2887 }
2888
2889 SmallVector<DbgOpID> NewDbgOps;
2890
2891 for (unsigned Idx = 0; Idx < FirstValue.getLocationOpCount(); ++Idx) {
2892 // If this op doesn't need to be joined because the values agree, use that
2893 // already-agreed value.
2894 if (!LocOpsToJoin.contains(V: Idx)) {
2895 NewDbgOps.push_back(Elt: FirstValue.getDbgOpID(Index: Idx));
2896 continue;
2897 }
2898
2899 std::optional<ValueIDNum> JoinedOpLoc =
2900 pickOperandPHILoc(DbgOpIdx: Idx, MBB, LiveOuts, MOutLocs, BlockOrders);
2901
2902 if (!JoinedOpLoc)
2903 return false;
2904
2905 NewDbgOps.push_back(Elt: DbgOpStore.insert(Op: *JoinedOpLoc));
2906 }
2907
2908 OutValues.append(RHS: NewDbgOps);
2909 return true;
2910}
2911
2912std::optional<ValueIDNum> InstrRefBasedLDV::pickOperandPHILoc(
2913 unsigned DbgOpIdx, const MachineBasicBlock &MBB, const LiveIdxT &LiveOuts,
2914 FuncValueTable &MOutLocs,
2915 const SmallVectorImpl<const MachineBasicBlock *> &BlockOrders) {
2916
2917 // Collect a set of locations from predecessor where its live-out value can
2918 // be found.
2919 SmallVector<SmallVector<LocIdx, 4>, 8> Locs;
2920 unsigned NumLocs = MTracker->getNumLocs();
2921
2922 for (const auto p : BlockOrders) {
2923 auto OutValIt = LiveOuts.find(Val: p);
2924 assert(OutValIt != LiveOuts.end());
2925 const DbgValue &OutVal = *OutValIt->second;
2926 DbgOpID OutValOpID = OutVal.getDbgOpID(Index: DbgOpIdx);
2927 DbgOp OutValOp = DbgOpStore.find(ID: OutValOpID);
2928 assert(!OutValOp.IsConst);
2929
2930 // Create new empty vector of locations.
2931 Locs.resize(N: Locs.size() + 1);
2932
2933 // If the live-in value is a def, find the locations where that value is
2934 // present. Do the same for VPHIs where we know the VPHI value.
2935 if (OutVal.Kind == DbgValue::Def ||
2936 (OutVal.Kind == DbgValue::VPHI && OutVal.BlockNo != MBB.getNumber() &&
2937 !OutValOp.isUndef())) {
2938 ValueIDNum ValToLookFor = OutValOp.ID;
2939 // Search the live-outs of the predecessor for the specified value.
2940 for (unsigned int I = 0; I < NumLocs; ++I) {
2941 if (MOutLocs[*p][I] == ValToLookFor)
2942 Locs.back().push_back(Elt: LocIdx(I));
2943 }
2944 } else {
2945 assert(OutVal.Kind == DbgValue::VPHI);
2946 // Otherwise: this is a VPHI on a backedge feeding back into itself, i.e.
2947 // a value that's live-through the whole loop. (It has to be a backedge,
2948 // because a block can't dominate itself). We can accept as a PHI location
2949 // any location where the other predecessors agree, _and_ the machine
2950 // locations feed back into themselves. Therefore, add all self-looping
2951 // machine-value PHI locations.
2952 for (unsigned int I = 0; I < NumLocs; ++I) {
2953 ValueIDNum MPHI(MBB.getNumber(), 0, LocIdx(I));
2954 if (MOutLocs[*p][I] == MPHI)
2955 Locs.back().push_back(Elt: LocIdx(I));
2956 }
2957 }
2958 }
2959 // We should have found locations for all predecessors, or returned.
2960 assert(Locs.size() == BlockOrders.size());
2961
2962 // Starting with the first set of locations, take the intersection with
2963 // subsequent sets.
2964 SmallVector<LocIdx, 4> CandidateLocs = Locs[0];
2965 for (unsigned int I = 1; I < Locs.size(); ++I) {
2966 auto &LocVec = Locs[I];
2967 SmallVector<LocIdx, 4> NewCandidates;
2968 std::set_intersection(first1: CandidateLocs.begin(), last1: CandidateLocs.end(),
2969 first2: LocVec.begin(), last2: LocVec.end(), result: std::inserter(x&: NewCandidates, i: NewCandidates.begin()));
2970 CandidateLocs = std::move(NewCandidates);
2971 }
2972 if (CandidateLocs.empty())
2973 return std::nullopt;
2974
2975 // We now have a set of LocIdxes that contain the right output value in
2976 // each of the predecessors. Pick the lowest; if there's a register loc,
2977 // that'll be it.
2978 LocIdx L = *CandidateLocs.begin();
2979
2980 // Return a PHI-value-number for the found location.
2981 ValueIDNum PHIVal = {(unsigned)MBB.getNumber(), 0, L};
2982 return PHIVal;
2983}
2984
2985bool InstrRefBasedLDV::vlocJoin(
2986 MachineBasicBlock &MBB, LiveIdxT &VLOCOutLocs,
2987 SmallPtrSet<const MachineBasicBlock *, 8> &BlocksToExplore,
2988 DbgValue &LiveIn) {
2989 LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n");
2990 bool Changed = false;
2991
2992 // Order predecessors by RPOT order, for exploring them in that order.
2993 SmallVector<MachineBasicBlock *, 8> BlockOrders(MBB.predecessors());
2994
2995 auto Cmp = [&](MachineBasicBlock *A, MachineBasicBlock *B) {
2996 return BBToOrder[A] < BBToOrder[B];
2997 };
2998
2999 llvm::sort(C&: BlockOrders, Comp: Cmp);
3000
3001 unsigned CurBlockRPONum = BBToOrder[&MBB];
3002
3003 // Collect all the incoming DbgValues for this variable, from predecessor
3004 // live-out values.
3005 SmallVector<InValueT, 8> Values;
3006 bool Bail = false;
3007 int BackEdgesStart = 0;
3008 for (auto *p : BlockOrders) {
3009 // If the predecessor isn't in scope / to be explored, we'll never be
3010 // able to join any locations.
3011 if (!BlocksToExplore.contains(Ptr: p)) {
3012 Bail = true;
3013 break;
3014 }
3015
3016 // All Live-outs will have been initialized.
3017 DbgValue &OutLoc = *VLOCOutLocs.find(Val: p)->second;
3018
3019 // Keep track of where back-edges begin in the Values vector. Relies on
3020 // BlockOrders being sorted by RPO.
3021 unsigned ThisBBRPONum = BBToOrder[p];
3022 if (ThisBBRPONum < CurBlockRPONum)
3023 ++BackEdgesStart;
3024
3025 Values.push_back(Elt: std::make_pair(x&: p, y: &OutLoc));
3026 }
3027
3028 // If there were no values, or one of the predecessors couldn't have a
3029 // value, then give up immediately. It's not safe to produce a live-in
3030 // value. Leave as whatever it was before.
3031 if (Bail || Values.size() == 0)
3032 return false;
3033
3034 // All (non-entry) blocks have at least one non-backedge predecessor.
3035 // Pick the variable value from the first of these, to compare against
3036 // all others.
3037 const DbgValue &FirstVal = *Values[0].second;
3038
3039 // If the old live-in value is not a PHI then either a) no PHI is needed
3040 // here, or b) we eliminated the PHI that was here. If so, we can just
3041 // propagate in the first parent's incoming value.
3042 if (LiveIn.Kind != DbgValue::VPHI || LiveIn.BlockNo != MBB.getNumber()) {
3043 Changed = LiveIn != FirstVal;
3044 if (Changed)
3045 LiveIn = FirstVal;
3046 return Changed;
3047 }
3048
3049 // Scan for variable values that can never be resolved: if they have
3050 // different DIExpressions, different indirectness, or are mixed constants /
3051 // non-constants.
3052 for (const auto &V : Values) {
3053 if (!V.second->Properties.isJoinable(Other: FirstVal.Properties))
3054 return false;
3055 if (V.second->Kind == DbgValue::NoVal)
3056 return false;
3057 if (!V.second->hasJoinableLocOps(Other: FirstVal))
3058 return false;
3059 }
3060
3061 // Try to eliminate this PHI. Do the incoming values all agree?
3062 bool Disagree = false;
3063 for (auto &V : Values) {
3064 if (*V.second == FirstVal)
3065 continue; // No disagreement.
3066
3067 // If both values are not equal but have equal non-empty IDs then they refer
3068 // to the same value from different sources (e.g. one is VPHI and the other
3069 // is Def), which does not cause disagreement.
3070 if (V.second->hasIdenticalValidLocOps(Other: FirstVal))
3071 continue;
3072
3073 // Eliminate if a backedge feeds a VPHI back into itself.
3074 if (V.second->Kind == DbgValue::VPHI &&
3075 V.second->BlockNo == MBB.getNumber() &&
3076 // Is this a backedge?
3077 std::distance(first: Values.begin(), last: &V) >= BackEdgesStart)
3078 continue;
3079
3080 Disagree = true;
3081 }
3082
3083 // No disagreement -> live-through value.
3084 if (!Disagree) {
3085 Changed = LiveIn != FirstVal;
3086 if (Changed)
3087 LiveIn = FirstVal;
3088 return Changed;
3089 } else {
3090 // Otherwise use a VPHI.
3091 DbgValue VPHI(MBB.getNumber(), FirstVal.Properties, DbgValue::VPHI);
3092 Changed = LiveIn != VPHI;
3093 if (Changed)
3094 LiveIn = VPHI;
3095 return Changed;
3096 }
3097}
3098
3099void InstrRefBasedLDV::getBlocksForScope(
3100 const DILocation *DILoc,
3101 SmallPtrSetImpl<const MachineBasicBlock *> &BlocksToExplore,
3102 const SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks) {
3103 // Get the set of "normal" in-lexical-scope blocks.
3104 LS.getMachineBasicBlocks(DL: DILoc, MBBs&: BlocksToExplore);
3105
3106 // VarLoc LiveDebugValues tracks variable locations that are defined in
3107 // blocks not in scope. This is something we could legitimately ignore, but
3108 // lets allow it for now for the sake of coverage.
3109 BlocksToExplore.insert_range(R: AssignBlocks);
3110
3111 // Storage for artificial blocks we intend to add to BlocksToExplore.
3112 DenseSet<const MachineBasicBlock *> ToAdd;
3113
3114 // To avoid needlessly dropping large volumes of variable locations, propagate
3115 // variables through aritifical blocks, i.e. those that don't have any
3116 // instructions in scope at all. To accurately replicate VarLoc
3117 // LiveDebugValues, this means exploring all artificial successors too.
3118 // Perform a depth-first-search to enumerate those blocks.
3119 for (const auto *MBB : BlocksToExplore) {
3120 // Depth-first-search state: each node is a block and which successor
3121 // we're currently exploring.
3122 SmallVector<std::pair<const MachineBasicBlock *,
3123 MachineBasicBlock::const_succ_iterator>,
3124 8>
3125 DFS;
3126
3127 // Find any artificial successors not already tracked.
3128 for (auto *succ : MBB->successors()) {
3129 if (BlocksToExplore.count(Ptr: succ))
3130 continue;
3131 if (!ArtificialBlocks.count(Ptr: succ))
3132 continue;
3133 ToAdd.insert(V: succ);
3134 DFS.push_back(Elt: {succ, succ->succ_begin()});
3135 }
3136
3137 // Search all those blocks, depth first.
3138 while (!DFS.empty()) {
3139 const MachineBasicBlock *CurBB = DFS.back().first;
3140 MachineBasicBlock::const_succ_iterator &CurSucc = DFS.back().second;
3141 // Walk back if we've explored this blocks successors to the end.
3142 if (CurSucc == CurBB->succ_end()) {
3143 DFS.pop_back();
3144 continue;
3145 }
3146
3147 // If the current successor is artificial and unexplored, descend into
3148 // it.
3149 if (!ToAdd.count(V: *CurSucc) && ArtificialBlocks.count(Ptr: *CurSucc)) {
3150 ToAdd.insert(V: *CurSucc);
3151 DFS.push_back(Elt: {*CurSucc, (*CurSucc)->succ_begin()});
3152 continue;
3153 }
3154
3155 ++CurSucc;
3156 }
3157 };
3158
3159 BlocksToExplore.insert_range(R&: ToAdd);
3160}
3161
3162void InstrRefBasedLDV::buildVLocValueMap(
3163 const DILocation *DILoc,
3164 const SmallSet<DebugVariableID, 4> &VarsWeCareAbout,
3165 SmallPtrSetImpl<MachineBasicBlock *> &AssignBlocks, LiveInsT &Output,
3166 FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
3167 SmallVectorImpl<VLocTracker> &AllTheVLocs) {
3168 // This method is much like buildMLocValueMap: but focuses on a single
3169 // LexicalScope at a time. Pick out a set of blocks and variables that are
3170 // to have their value assignments solved, then run our dataflow algorithm
3171 // until a fixedpoint is reached.
3172 std::priority_queue<unsigned int, std::vector<unsigned int>,
3173 std::greater<unsigned int>>
3174 Worklist, Pending;
3175 SmallPtrSet<MachineBasicBlock *, 16> OnWorklist, OnPending;
3176
3177 // The set of blocks we'll be examining.
3178 SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore;
3179
3180 // The order in which to examine them (RPO).
3181 SmallVector<MachineBasicBlock *, 16> BlockOrders;
3182 SmallVector<unsigned, 32> BlockOrderNums;
3183
3184 getBlocksForScope(DILoc, BlocksToExplore, AssignBlocks);
3185
3186 // Single block scope: not interesting! No propagation at all. Note that
3187 // this could probably go above ArtificialBlocks without damage, but
3188 // that then produces output differences from original-live-debug-values,
3189 // which propagates from a single block into many artificial ones.
3190 if (BlocksToExplore.size() == 1)
3191 return;
3192
3193 // Convert a const set to a non-const set. LexicalScopes
3194 // getMachineBasicBlocks returns const MBB pointers, IDF wants mutable ones.
3195 // (Neither of them mutate anything).
3196 SmallPtrSet<MachineBasicBlock *, 8> MutBlocksToExplore;
3197 for (const auto *MBB : BlocksToExplore)
3198 MutBlocksToExplore.insert(Ptr: const_cast<MachineBasicBlock *>(MBB));
3199
3200 // Picks out relevants blocks RPO order and sort them. Sort their
3201 // order-numbers and map back to MBB pointers later, to avoid repeated
3202 // DenseMap queries during comparisons.
3203 for (const auto *MBB : BlocksToExplore)
3204 BlockOrderNums.push_back(Elt: BBToOrder[MBB]);
3205
3206 llvm::sort(C&: BlockOrderNums);
3207 for (unsigned int I : BlockOrderNums)
3208 BlockOrders.push_back(Elt: OrderToBB[I]);
3209 BlockOrderNums.clear();
3210 unsigned NumBlocks = BlockOrders.size();
3211
3212 // Allocate some vectors for storing the live ins and live outs. Large.
3213 SmallVector<DbgValue, 32> LiveIns, LiveOuts;
3214 LiveIns.reserve(N: NumBlocks);
3215 LiveOuts.reserve(N: NumBlocks);
3216
3217 // Initialize all values to start as NoVals. This signifies "it's live
3218 // through, but we don't know what it is".
3219 DbgValueProperties EmptyProperties(EmptyExpr, false, false);
3220 for (unsigned int I = 0; I < NumBlocks; ++I) {
3221 DbgValue EmptyDbgValue(I, EmptyProperties, DbgValue::NoVal);
3222 LiveIns.push_back(Elt: EmptyDbgValue);
3223 LiveOuts.push_back(Elt: EmptyDbgValue);
3224 }
3225
3226 // Produce by-MBB indexes of live-in/live-outs, to ease lookup within
3227 // vlocJoin.
3228 LiveIdxT LiveOutIdx, LiveInIdx;
3229 LiveOutIdx.reserve(NumEntries: NumBlocks);
3230 LiveInIdx.reserve(NumEntries: NumBlocks);
3231 for (unsigned I = 0; I < NumBlocks; ++I) {
3232 LiveOutIdx[BlockOrders[I]] = &LiveOuts[I];
3233 LiveInIdx[BlockOrders[I]] = &LiveIns[I];
3234 }
3235
3236 // Loop over each variable and place PHIs for it, then propagate values
3237 // between blocks. This keeps the locality of working on one lexical scope at
3238 // at time, but avoids re-processing variable values because some other
3239 // variable has been assigned.
3240 for (DebugVariableID VarID : VarsWeCareAbout) {
3241 // Re-initialize live-ins and live-outs, to clear the remains of previous
3242 // variables live-ins / live-outs.
3243 for (unsigned int I = 0; I < NumBlocks; ++I) {
3244 DbgValue EmptyDbgValue(I, EmptyProperties, DbgValue::NoVal);
3245 LiveIns[I] = EmptyDbgValue;
3246 LiveOuts[I] = EmptyDbgValue;
3247 }
3248
3249 // Place PHIs for variable values, using the LLVM IDF calculator.
3250 // Collect the set of blocks where variables are def'd.
3251 SmallPtrSet<MachineBasicBlock *, 32> DefBlocks;
3252 for (const MachineBasicBlock *ExpMBB : BlocksToExplore) {
3253 auto &TransferFunc = AllTheVLocs[ExpMBB->getNumber()].Vars;
3254 if (TransferFunc.contains(Key: VarID))
3255 DefBlocks.insert(Ptr: const_cast<MachineBasicBlock *>(ExpMBB));
3256 }
3257
3258 SmallVector<MachineBasicBlock *, 32> PHIBlocks;
3259
3260 // Request the set of PHIs we should insert for this variable. If there's
3261 // only one value definition, things are very simple.
3262 if (DefBlocks.size() == 1) {
3263 placePHIsForSingleVarDefinition(InScopeBlocks: MutBlocksToExplore, MBB: *DefBlocks.begin(),
3264 AllTheVLocs, Var: VarID, Output);
3265 continue;
3266 }
3267
3268 // Otherwise: we need to place PHIs through SSA and propagate values.
3269 BlockPHIPlacement(AllBlocks: MutBlocksToExplore, DefBlocks, PHIBlocks);
3270
3271 // Insert PHIs into the per-block live-in tables for this variable.
3272 for (MachineBasicBlock *PHIMBB : PHIBlocks) {
3273 unsigned BlockNo = PHIMBB->getNumber();
3274 DbgValue *LiveIn = LiveInIdx[PHIMBB];
3275 *LiveIn = DbgValue(BlockNo, EmptyProperties, DbgValue::VPHI);
3276 }
3277
3278 for (auto *MBB : BlockOrders) {
3279 Worklist.push(x: BBToOrder[MBB]);
3280 OnWorklist.insert(Ptr: MBB);
3281 }
3282
3283 // Iterate over all the blocks we selected, propagating the variables value.
3284 // This loop does two things:
3285 // * Eliminates un-necessary VPHIs in vlocJoin,
3286 // * Evaluates the blocks transfer function (i.e. variable assignments) and
3287 // stores the result to the blocks live-outs.
3288 // Always evaluate the transfer function on the first iteration, and when
3289 // the live-ins change thereafter.
3290 bool FirstTrip = true;
3291 while (!Worklist.empty() || !Pending.empty()) {
3292 while (!Worklist.empty()) {
3293 auto *MBB = OrderToBB[Worklist.top()];
3294 CurBB = MBB->getNumber();
3295 Worklist.pop();
3296
3297 auto LiveInsIt = LiveInIdx.find(Val: MBB);
3298 assert(LiveInsIt != LiveInIdx.end());
3299 DbgValue *LiveIn = LiveInsIt->second;
3300
3301 // Join values from predecessors. Updates LiveInIdx, and writes output
3302 // into JoinedInLocs.
3303 bool InLocsChanged =
3304 vlocJoin(MBB&: *MBB, VLOCOutLocs&: LiveOutIdx, BlocksToExplore, LiveIn&: *LiveIn);
3305
3306 SmallVector<const MachineBasicBlock *, 8> Preds(MBB->predecessors());
3307
3308 // If this block's live-in value is a VPHI, try to pick a machine-value
3309 // for it. This makes the machine-value available and propagated
3310 // through all blocks by the time value propagation finishes. We can't
3311 // do this any earlier as it needs to read the block live-outs.
3312 if (LiveIn->Kind == DbgValue::VPHI && LiveIn->BlockNo == (int)CurBB) {
3313 // There's a small possibility that on a preceeding path, a VPHI is
3314 // eliminated and transitions from VPHI-with-location to
3315 // live-through-value. As a result, the selected location of any VPHI
3316 // might change, so we need to re-compute it on each iteration.
3317 SmallVector<DbgOpID> JoinedOps;
3318
3319 if (pickVPHILoc(OutValues&: JoinedOps, MBB: *MBB, LiveOuts: LiveOutIdx, MOutLocs, BlockOrders: Preds)) {
3320 bool NewLocPicked = !equal(LRange: LiveIn->getDbgOpIDs(), RRange&: JoinedOps);
3321 InLocsChanged |= NewLocPicked;
3322 if (NewLocPicked)
3323 LiveIn->setDbgOpIDs(JoinedOps);
3324 }
3325 }
3326
3327 if (!InLocsChanged && !FirstTrip)
3328 continue;
3329
3330 DbgValue *LiveOut = LiveOutIdx[MBB];
3331 bool OLChanged = false;
3332
3333 // Do transfer function.
3334 auto &VTracker = AllTheVLocs[MBB->getNumber()];
3335 auto TransferIt = VTracker.Vars.find(Key: VarID);
3336 if (TransferIt != VTracker.Vars.end()) {
3337 // Erase on empty transfer (DBG_VALUE $noreg).
3338 if (TransferIt->second.Kind == DbgValue::Undef) {
3339 DbgValue NewVal(MBB->getNumber(), EmptyProperties, DbgValue::NoVal);
3340 if (*LiveOut != NewVal) {
3341 *LiveOut = NewVal;
3342 OLChanged = true;
3343 }
3344 } else {
3345 // Insert new variable value; or overwrite.
3346 if (*LiveOut != TransferIt->second) {
3347 *LiveOut = TransferIt->second;
3348 OLChanged = true;
3349 }
3350 }
3351 } else {
3352 // Just copy live-ins to live-outs, for anything not transferred.
3353 if (*LiveOut != *LiveIn) {
3354 *LiveOut = *LiveIn;
3355 OLChanged = true;
3356 }
3357 }
3358
3359 // If no live-out value changed, there's no need to explore further.
3360 if (!OLChanged)
3361 continue;
3362
3363 // We should visit all successors. Ensure we'll visit any non-backedge
3364 // successors during this dataflow iteration; book backedge successors
3365 // to be visited next time around.
3366 for (auto *s : MBB->successors()) {
3367 // Ignore out of scope / not-to-be-explored successors.
3368 if (!LiveInIdx.contains(Val: s))
3369 continue;
3370
3371 unsigned Order = BBToOrder[s];
3372 if (Order > BBToOrder[MBB]) {
3373 if (OnWorklist.insert(Ptr: s).second)
3374 Worklist.push(x: Order);
3375 } else if (OnPending.insert(Ptr: s).second && (FirstTrip || OLChanged)) {
3376 Pending.push(x: Order);
3377 }
3378 }
3379 }
3380 Worklist.swap(pq&: Pending);
3381 std::swap(LHS&: OnWorklist, RHS&: OnPending);
3382 OnPending.clear();
3383 assert(Pending.empty());
3384 FirstTrip = false;
3385 }
3386
3387 // Save live-ins to output vector. Ignore any that are still marked as being
3388 // VPHIs with no location -- those are variables that we know the value of,
3389 // but are not actually available in the register file.
3390 for (auto *MBB : BlockOrders) {
3391 DbgValue *BlockLiveIn = LiveInIdx[MBB];
3392 if (BlockLiveIn->Kind == DbgValue::NoVal)
3393 continue;
3394 if (BlockLiveIn->isUnjoinedPHI())
3395 continue;
3396 if (BlockLiveIn->Kind == DbgValue::VPHI)
3397 BlockLiveIn->Kind = DbgValue::Def;
3398 [[maybe_unused]] auto &[Var, DILoc] = DVMap.lookupDVID(ID: VarID);
3399 assert(BlockLiveIn->Properties.DIExpr->getFragmentInfo() ==
3400 Var.getFragment() &&
3401 "Fragment info missing during value prop");
3402 Output[MBB->getNumber()].push_back(Elt: std::make_pair(x&: VarID, y&: *BlockLiveIn));
3403 }
3404 } // Per-variable loop.
3405
3406 BlockOrders.clear();
3407 BlocksToExplore.clear();
3408}
3409
3410void InstrRefBasedLDV::placePHIsForSingleVarDefinition(
3411 const SmallPtrSetImpl<MachineBasicBlock *> &InScopeBlocks,
3412 MachineBasicBlock *AssignMBB, SmallVectorImpl<VLocTracker> &AllTheVLocs,
3413 DebugVariableID VarID, LiveInsT &Output) {
3414 // If there is a single definition of the variable, then working out it's
3415 // value everywhere is very simple: it's every block dominated by the
3416 // definition. At the dominance frontier, the usual algorithm would:
3417 // * Place PHIs,
3418 // * Propagate values into them,
3419 // * Find there's no incoming variable value from the other incoming branches
3420 // of the dominance frontier,
3421 // * Specify there's no variable value in blocks past the frontier.
3422 // This is a common case, hence it's worth special-casing it.
3423
3424 // Pick out the variables value from the block transfer function.
3425 VLocTracker &VLocs = AllTheVLocs[AssignMBB->getNumber()];
3426 auto ValueIt = VLocs.Vars.find(Key: VarID);
3427 const DbgValue &Value = ValueIt->second;
3428
3429 // If it's an explicit assignment of "undef", that means there is no location
3430 // anyway, anywhere.
3431 if (Value.Kind == DbgValue::Undef)
3432 return;
3433
3434 // Assign the variable value to entry to each dominated block that's in scope.
3435 // Skip the definition block -- it's assigned the variable value in the middle
3436 // of the block somewhere.
3437 for (auto *ScopeBlock : InScopeBlocks) {
3438 if (!DomTree->properlyDominates(A: AssignMBB, B: ScopeBlock))
3439 continue;
3440
3441 Output[ScopeBlock->getNumber()].push_back(Elt: {VarID, Value});
3442 }
3443
3444 // All blocks that aren't dominated have no live-in value, thus no variable
3445 // value will be given to them.
3446}
3447
3448#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3449void InstrRefBasedLDV::dump_mloc_transfer(
3450 const MLocTransferMap &mloc_transfer) const {
3451 for (const auto &P : mloc_transfer) {
3452 std::string foo = MTracker->LocIdxToName(P.first);
3453 std::string bar = MTracker->IDAsString(P.second);
3454 dbgs() << "Loc " << foo << " --> " << bar << "\n";
3455 }
3456}
3457#endif
3458
3459void InstrRefBasedLDV::initialSetup(MachineFunction &MF) {
3460 // Build some useful data structures.
3461
3462 LLVMContext &Context = MF.getFunction().getContext();
3463 EmptyExpr = DIExpression::get(Context, Elements: {});
3464
3465 auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool {
3466 if (const DebugLoc &DL = MI.getDebugLoc())
3467 return DL.getLine() != 0;
3468 return false;
3469 };
3470
3471 // Collect a set of all the artificial blocks. Collect the size too, ilist
3472 // size calls are O(n).
3473 unsigned int Size = 0;
3474 for (auto &MBB : MF) {
3475 ++Size;
3476 if (none_of(Range: MBB.instrs(), P: hasNonArtificialLocation))
3477 ArtificialBlocks.insert(Ptr: &MBB);
3478 }
3479
3480 // Compute mappings of block <=> RPO order.
3481 ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
3482 unsigned int RPONumber = 0;
3483 OrderToBB.reserve(N: Size);
3484 BBToOrder.reserve(NumEntries: Size);
3485 BBNumToRPO.reserve(NumEntries: Size);
3486 auto processMBB = [&](MachineBasicBlock *MBB) {
3487 OrderToBB.push_back(Elt: MBB);
3488 BBToOrder[MBB] = RPONumber;
3489 BBNumToRPO[MBB->getNumber()] = RPONumber;
3490 ++RPONumber;
3491 };
3492 for (MachineBasicBlock *MBB : RPOT)
3493 processMBB(MBB);
3494 for (MachineBasicBlock &MBB : MF)
3495 if (!BBToOrder.contains(Val: &MBB))
3496 processMBB(&MBB);
3497
3498 // Order value substitutions by their "source" operand pair, for quick lookup.
3499 llvm::sort(C&: MF.DebugValueSubstitutions);
3500
3501#ifdef EXPENSIVE_CHECKS
3502 // As an expensive check, test whether there are any duplicate substitution
3503 // sources in the collection.
3504 if (MF.DebugValueSubstitutions.size() > 2) {
3505 for (auto It = MF.DebugValueSubstitutions.begin();
3506 It != std::prev(MF.DebugValueSubstitutions.end()); ++It) {
3507 assert(It->Src != std::next(It)->Src && "Duplicate variable location "
3508 "substitution seen");
3509 }
3510 }
3511#endif
3512}
3513
3514// Produce an "ejection map" for blocks, i.e., what's the highest-numbered
3515// lexical scope it's used in. When exploring in DFS order and we pass that
3516// scope, the block can be processed and any tracking information freed.
3517void InstrRefBasedLDV::makeDepthFirstEjectionMap(
3518 SmallVectorImpl<unsigned> &EjectionMap,
3519 const ScopeToDILocT &ScopeToDILocation,
3520 ScopeToAssignBlocksT &ScopeToAssignBlocks) {
3521 SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore;
3522 SmallVector<std::pair<LexicalScope *, ssize_t>, 4> WorkStack;
3523 auto *TopScope = LS.getCurrentFunctionScope();
3524
3525 // Unlike lexical scope explorers, we explore in reverse order, to find the
3526 // "last" lexical scope used for each block early.
3527 WorkStack.push_back(Elt: {TopScope, TopScope->getChildren().size() - 1});
3528
3529 while (!WorkStack.empty()) {
3530 auto &ScopePosition = WorkStack.back();
3531 LexicalScope *WS = ScopePosition.first;
3532 ssize_t ChildNum = ScopePosition.second--;
3533
3534 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
3535 if (ChildNum >= 0) {
3536 // If ChildNum is positive, there are remaining children to explore.
3537 // Push the child and its children-count onto the stack.
3538 auto &ChildScope = Children[ChildNum];
3539 WorkStack.push_back(
3540 Elt: std::make_pair(x: ChildScope, y: ChildScope->getChildren().size() - 1));
3541 } else {
3542 WorkStack.pop_back();
3543
3544 // We've explored all children and any later blocks: examine all blocks
3545 // in our scope. If they haven't yet had an ejection number set, then
3546 // this scope will be the last to use that block.
3547 auto DILocationIt = ScopeToDILocation.find(Val: WS);
3548 if (DILocationIt != ScopeToDILocation.end()) {
3549 getBlocksForScope(DILoc: DILocationIt->second, BlocksToExplore,
3550 AssignBlocks: ScopeToAssignBlocks.find(Val: WS)->second);
3551 for (const auto *MBB : BlocksToExplore) {
3552 unsigned BBNum = MBB->getNumber();
3553 if (EjectionMap[BBNum] == 0)
3554 EjectionMap[BBNum] = WS->getDFSOut();
3555 }
3556
3557 BlocksToExplore.clear();
3558 }
3559 }
3560 }
3561}
3562
3563bool InstrRefBasedLDV::depthFirstVLocAndEmit(
3564 unsigned MaxNumBlocks, const ScopeToDILocT &ScopeToDILocation,
3565 const ScopeToVarsT &ScopeToVars, ScopeToAssignBlocksT &ScopeToAssignBlocks,
3566 LiveInsT &Output, FuncValueTable &MOutLocs, FuncValueTable &MInLocs,
3567 SmallVectorImpl<VLocTracker> &AllTheVLocs, MachineFunction &MF,
3568 bool ShouldEmitDebugEntryValues) {
3569 TTracker = new TransferTracker(TII, MTracker, MF, DVMap, *TRI,
3570 CalleeSavedRegs, ShouldEmitDebugEntryValues);
3571 unsigned NumLocs = MTracker->getNumLocs();
3572 VTracker = nullptr;
3573
3574 // No scopes? No variable locations.
3575 if (!LS.getCurrentFunctionScope())
3576 return false;
3577
3578 // Build map from block number to the last scope that uses the block.
3579 SmallVector<unsigned, 16> EjectionMap;
3580 EjectionMap.resize(N: MaxNumBlocks, NV: 0);
3581 makeDepthFirstEjectionMap(EjectionMap, ScopeToDILocation,
3582 ScopeToAssignBlocks);
3583
3584 // Helper lambda for ejecting a block -- if nothing is going to use the block,
3585 // we can translate the variable location information into DBG_VALUEs and then
3586 // free all of InstrRefBasedLDV's data structures.
3587 auto EjectBlock = [&](MachineBasicBlock &MBB) -> void {
3588 unsigned BBNum = MBB.getNumber();
3589 AllTheVLocs[BBNum].clear();
3590
3591 // Prime the transfer-tracker, and then step through all the block
3592 // instructions, installing transfers.
3593 MTracker->reset();
3594 MTracker->loadFromArray(Locs&: MInLocs[MBB], NewCurBB: BBNum);
3595 TTracker->loadInlocs(MBB, MLocs&: MInLocs[MBB], DbgOpStore, VLocs: Output[BBNum], NumLocs);
3596
3597 CurBB = BBNum;
3598 CurInst = 1;
3599 for (auto &MI : MBB) {
3600 process(MI, MLiveOuts: &MOutLocs, MLiveIns: &MInLocs);
3601 TTracker->checkInstForNewValues(Inst: CurInst, pos: MI.getIterator());
3602 ++CurInst;
3603 }
3604
3605 // Free machine-location tables for this block.
3606 MInLocs.ejectTableForBlock(MBB);
3607 MOutLocs.ejectTableForBlock(MBB);
3608 // We don't need live-in variable values for this block either.
3609 Output[BBNum].clear();
3610 AllTheVLocs[BBNum].clear();
3611 };
3612
3613 SmallPtrSet<const MachineBasicBlock *, 8> BlocksToExplore;
3614 SmallVector<std::pair<LexicalScope *, ssize_t>, 4> WorkStack;
3615 WorkStack.push_back(Elt: {LS.getCurrentFunctionScope(), 0});
3616 unsigned HighestDFSIn = 0;
3617
3618 // Proceed to explore in depth first order.
3619 while (!WorkStack.empty()) {
3620 auto &ScopePosition = WorkStack.back();
3621 LexicalScope *WS = ScopePosition.first;
3622 ssize_t ChildNum = ScopePosition.second++;
3623
3624 // We obesrve scopes with children twice here, once descending in, once
3625 // ascending out of the scope nest. Use HighestDFSIn as a ratchet to ensure
3626 // we don't process a scope twice. Additionally, ignore scopes that don't
3627 // have a DILocation -- by proxy, this means we never tracked any variable
3628 // assignments in that scope.
3629 auto DILocIt = ScopeToDILocation.find(Val: WS);
3630 if (HighestDFSIn <= WS->getDFSIn() && DILocIt != ScopeToDILocation.end()) {
3631 const DILocation *DILoc = DILocIt->second;
3632 auto &VarsWeCareAbout = ScopeToVars.find(Val: WS)->second;
3633 auto &BlocksInScope = ScopeToAssignBlocks.find(Val: WS)->second;
3634
3635 buildVLocValueMap(DILoc, VarsWeCareAbout, AssignBlocks&: BlocksInScope, Output, MOutLocs,
3636 MInLocs, AllTheVLocs);
3637 }
3638
3639 HighestDFSIn = std::max(a: HighestDFSIn, b: WS->getDFSIn());
3640
3641 // Descend into any scope nests.
3642 const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
3643 if (ChildNum < (ssize_t)Children.size()) {
3644 // There are children to explore -- push onto stack and continue.
3645 auto &ChildScope = Children[ChildNum];
3646 WorkStack.push_back(Elt: std::make_pair(x: ChildScope, y: 0));
3647 } else {
3648 WorkStack.pop_back();
3649
3650 // We've explored a leaf, or have explored all the children of a scope.
3651 // Try to eject any blocks where this is the last scope it's relevant to.
3652 auto DILocationIt = ScopeToDILocation.find(Val: WS);
3653 if (DILocationIt == ScopeToDILocation.end())
3654 continue;
3655
3656 getBlocksForScope(DILoc: DILocationIt->second, BlocksToExplore,
3657 AssignBlocks: ScopeToAssignBlocks.find(Val: WS)->second);
3658 for (const auto *MBB : BlocksToExplore)
3659 if (WS->getDFSOut() == EjectionMap[MBB->getNumber()])
3660 EjectBlock(const_cast<MachineBasicBlock &>(*MBB));
3661
3662 BlocksToExplore.clear();
3663 }
3664 }
3665
3666 // Some artificial blocks may not have been ejected, meaning they're not
3667 // connected to an actual legitimate scope. This can technically happen
3668 // with things like the entry block. In theory, we shouldn't need to do
3669 // anything for such out-of-scope blocks, but for the sake of being similar
3670 // to VarLocBasedLDV, eject these too.
3671 for (auto *MBB : ArtificialBlocks)
3672 if (MInLocs.hasTableFor(MBB&: *MBB))
3673 EjectBlock(*MBB);
3674
3675 return emitTransfers();
3676}
3677
3678bool InstrRefBasedLDV::emitTransfers() {
3679 // Go through all the transfers recorded in the TransferTracker -- this is
3680 // both the live-ins to a block, and any movements of values that happen
3681 // in the middle.
3682 for (auto &P : TTracker->Transfers) {
3683 // We have to insert DBG_VALUEs in a consistent order, otherwise they
3684 // appear in DWARF in different orders. Use the order that they appear
3685 // when walking through each block / each instruction, stored in
3686 // DVMap.
3687 llvm::sort(C&: P.Insts, Comp: llvm::less_first());
3688
3689 // Insert either before or after the designated point...
3690 if (P.MBB) {
3691 MachineBasicBlock &MBB = *P.MBB;
3692 for (const auto &Pair : P.Insts)
3693 MBB.insert(I: P.Pos, M: Pair.second);
3694 } else {
3695 // Terminators, like tail calls, can clobber things. Don't try and place
3696 // transfers after them.
3697 if (P.Pos->isTerminator())
3698 continue;
3699
3700 MachineBasicBlock &MBB = *P.Pos->getParent();
3701 for (const auto &Pair : P.Insts)
3702 MBB.insertAfterBundle(I: P.Pos, MI: Pair.second);
3703 }
3704 }
3705
3706 return TTracker->Transfers.size() != 0;
3707}
3708
3709/// Calculate the liveness information for the given machine function and
3710/// extend ranges across basic blocks.
3711bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
3712 MachineDominatorTree *DomTree,
3713 bool ShouldEmitDebugEntryValues,
3714 unsigned InputBBLimit,
3715 unsigned InputDbgValLimit) {
3716 // No subprogram means this function contains no debuginfo.
3717 if (!MF.getFunction().getSubprogram())
3718 return false;
3719
3720 LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n");
3721
3722 this->DomTree = DomTree;
3723 TRI = MF.getSubtarget().getRegisterInfo();
3724 MRI = &MF.getRegInfo();
3725 TII = MF.getSubtarget().getInstrInfo();
3726 TFI = MF.getSubtarget().getFrameLowering();
3727 TFI->getCalleeSaves(MF, SavedRegs&: CalleeSavedRegs);
3728 MFI = &MF.getFrameInfo();
3729 LS.scanFunction(MF);
3730
3731 const auto &STI = MF.getSubtarget();
3732 AdjustsStackInCalls = MFI->adjustsStack() &&
3733 STI.getFrameLowering()->stackProbeFunctionModifiesSP();
3734 if (AdjustsStackInCalls)
3735 StackProbeSymbolName = STI.getTargetLowering()->getStackProbeSymbolName(MF);
3736
3737 MTracker =
3738 new MLocTracker(MF, *TII, *TRI, *MF.getSubtarget().getTargetLowering());
3739 VTracker = nullptr;
3740 TTracker = nullptr;
3741
3742 SmallVector<MLocTransferMap, 32> MLocTransfer;
3743 SmallVector<VLocTracker, 8> vlocs;
3744 LiveInsT SavedLiveIns;
3745
3746 int MaxNumBlocks = -1;
3747 for (auto &MBB : MF)
3748 MaxNumBlocks = std::max(a: MBB.getNumber(), b: MaxNumBlocks);
3749 assert(MaxNumBlocks >= 0);
3750 ++MaxNumBlocks;
3751
3752 initialSetup(MF);
3753
3754 MLocTransfer.resize(N: MaxNumBlocks);
3755 vlocs.resize(N: MaxNumBlocks, NV: VLocTracker(DVMap, OverlapFragments, EmptyExpr));
3756 SavedLiveIns.resize(N: MaxNumBlocks);
3757
3758 produceMLocTransferFunction(MF, MLocTransfer, MaxNumBlocks);
3759
3760 // Allocate and initialize two array-of-arrays for the live-in and live-out
3761 // machine values. The outer dimension is the block number; while the inner
3762 // dimension is a LocIdx from MLocTracker.
3763 unsigned NumLocs = MTracker->getNumLocs();
3764 FuncValueTable MOutLocs(MaxNumBlocks, NumLocs);
3765 FuncValueTable MInLocs(MaxNumBlocks, NumLocs);
3766
3767 // Solve the machine value dataflow problem using the MLocTransfer function,
3768 // storing the computed live-ins / live-outs into the array-of-arrays. We use
3769 // both live-ins and live-outs for decision making in the variable value
3770 // dataflow problem.
3771 buildMLocValueMap(MF, MInLocs, MOutLocs, MLocTransfer);
3772
3773 // Patch up debug phi numbers, turning unknown block-live-in values into
3774 // either live-through machine values, or PHIs.
3775 for (auto &DBG_PHI : DebugPHINumToValue) {
3776 // Identify unresolved block-live-ins.
3777 if (!DBG_PHI.ValueRead)
3778 continue;
3779
3780 ValueIDNum &Num = *DBG_PHI.ValueRead;
3781 if (!Num.isPHI())
3782 continue;
3783
3784 unsigned BlockNo = Num.getBlock();
3785 LocIdx LocNo = Num.getLoc();
3786 ValueIDNum ResolvedValue = MInLocs[BlockNo][LocNo.asU64()];
3787 // If there is no resolved value for this live-in then it is not directly
3788 // reachable from the entry block -- model it as a PHI on entry to this
3789 // block, which means we leave the ValueIDNum unchanged.
3790 if (ResolvedValue != ValueIDNum::EmptyValue)
3791 Num = ResolvedValue;
3792 }
3793 // Later, we'll be looking up ranges of instruction numbers.
3794 llvm::sort(C&: DebugPHINumToValue);
3795
3796 // Walk back through each block / instruction, collecting DBG_VALUE
3797 // instructions and recording what machine value their operands refer to.
3798 for (MachineBasicBlock *MBB : OrderToBB) {
3799 CurBB = MBB->getNumber();
3800 VTracker = &vlocs[CurBB];
3801 VTracker->MBB = MBB;
3802 MTracker->loadFromArray(Locs&: MInLocs[*MBB], NewCurBB: CurBB);
3803 CurInst = 1;
3804 for (auto &MI : *MBB) {
3805 process(MI, MLiveOuts: &MOutLocs, MLiveIns: &MInLocs);
3806 ++CurInst;
3807 }
3808 MTracker->reset();
3809 }
3810
3811 // Map from one LexicalScope to all the variables in that scope.
3812 ScopeToVarsT ScopeToVars;
3813
3814 // Map from One lexical scope to all blocks where assignments happen for
3815 // that scope.
3816 ScopeToAssignBlocksT ScopeToAssignBlocks;
3817
3818 // Store map of DILocations that describes scopes.
3819 ScopeToDILocT ScopeToDILocation;
3820
3821 // To mirror old LiveDebugValues, enumerate variables in RPOT order. Otherwise
3822 // the order is unimportant, it just has to be stable.
3823 unsigned VarAssignCount = 0;
3824 for (MachineBasicBlock *MBB : OrderToBB) {
3825 auto *VTracker = &vlocs[MBB->getNumber()];
3826 // Collect each variable with a DBG_VALUE in this block.
3827 for (auto &idx : VTracker->Vars) {
3828 DebugVariableID VarID = idx.first;
3829 const DILocation *ScopeLoc = VTracker->Scopes[VarID];
3830 assert(ScopeLoc != nullptr);
3831 auto *Scope = LS.findLexicalScope(DL: ScopeLoc);
3832
3833 // No insts in scope -> shouldn't have been recorded.
3834 assert(Scope != nullptr);
3835
3836 ScopeToVars[Scope].insert(V: VarID);
3837 ScopeToAssignBlocks[Scope].insert(Ptr: VTracker->MBB);
3838 ScopeToDILocation[Scope] = ScopeLoc;
3839 ++VarAssignCount;
3840 }
3841 }
3842
3843 bool Changed = false;
3844
3845 // If we have an extremely large number of variable assignments and blocks,
3846 // bail out at this point. We've burnt some time doing analysis already,
3847 // however we should cut our losses.
3848 if ((unsigned)MaxNumBlocks > InputBBLimit &&
3849 VarAssignCount > InputDbgValLimit) {
3850 LLVM_DEBUG(dbgs() << "Disabling InstrRefBasedLDV: " << MF.getName()
3851 << " has " << MaxNumBlocks << " basic blocks and "
3852 << VarAssignCount
3853 << " variable assignments, exceeding limits.\n");
3854 } else {
3855 // Optionally, solve the variable value problem and emit to blocks by using
3856 // a lexical-scope-depth search. It should be functionally identical to
3857 // the "else" block of this condition.
3858 Changed = depthFirstVLocAndEmit(
3859 MaxNumBlocks, ScopeToDILocation, ScopeToVars, ScopeToAssignBlocks,
3860 Output&: SavedLiveIns, MOutLocs, MInLocs, AllTheVLocs&: vlocs, MF, ShouldEmitDebugEntryValues);
3861 }
3862
3863 delete MTracker;
3864 delete TTracker;
3865 MTracker = nullptr;
3866 VTracker = nullptr;
3867 TTracker = nullptr;
3868
3869 ArtificialBlocks.clear();
3870 OrderToBB.clear();
3871 BBToOrder.clear();
3872 BBNumToRPO.clear();
3873 DebugInstrNumToInstr.clear();
3874 DebugPHINumToValue.clear();
3875 OverlapFragments.clear();
3876 SeenFragments.clear();
3877 SeenDbgPHIs.clear();
3878 DbgOpStore.clear();
3879 DVMap.clear();
3880
3881 return Changed;
3882}
3883
3884LDVImpl *llvm::makeInstrRefBasedLiveDebugValues() {
3885 return new InstrRefBasedLDV();
3886}
3887
3888namespace {
3889class LDVSSABlock;
3890class LDVSSAUpdater;
3891
3892// Pick a type to identify incoming block values as we construct SSA. We
3893// can't use anything more robust than an integer unfortunately, as SSAUpdater
3894// expects to zero-initialize the type.
3895typedef uint64_t BlockValueNum;
3896
3897/// Represents an SSA PHI node for the SSA updater class. Contains the block
3898/// this PHI is in, the value number it would have, and the expected incoming
3899/// values from parent blocks.
3900class LDVSSAPhi {
3901public:
3902 SmallVector<std::pair<LDVSSABlock *, BlockValueNum>, 4> IncomingValues;
3903 LDVSSABlock *ParentBlock;
3904 BlockValueNum PHIValNum;
3905 LDVSSAPhi(BlockValueNum PHIValNum, LDVSSABlock *ParentBlock)
3906 : ParentBlock(ParentBlock), PHIValNum(PHIValNum) {}
3907
3908 LDVSSABlock *getParent() { return ParentBlock; }
3909};
3910
3911/// Thin wrapper around a block predecessor iterator. Only difference from a
3912/// normal block iterator is that it dereferences to an LDVSSABlock.
3913class LDVSSABlockIterator {
3914public:
3915 MachineBasicBlock::pred_iterator PredIt;
3916 LDVSSAUpdater &Updater;
3917
3918 LDVSSABlockIterator(MachineBasicBlock::pred_iterator PredIt,
3919 LDVSSAUpdater &Updater)
3920 : PredIt(PredIt), Updater(Updater) {}
3921
3922 bool operator!=(const LDVSSABlockIterator &OtherIt) const {
3923 return OtherIt.PredIt != PredIt;
3924 }
3925
3926 LDVSSABlockIterator &operator++() {
3927 ++PredIt;
3928 return *this;
3929 }
3930
3931 LDVSSABlock *operator*();
3932};
3933
3934/// Thin wrapper around a block for SSA Updater interface. Necessary because
3935/// we need to track the PHI value(s) that we may have observed as necessary
3936/// in this block.
3937class LDVSSABlock {
3938public:
3939 MachineBasicBlock &BB;
3940 LDVSSAUpdater &Updater;
3941 using PHIListT = SmallVector<LDVSSAPhi, 1>;
3942 /// List of PHIs in this block. There should only ever be one.
3943 PHIListT PHIList;
3944
3945 LDVSSABlock(MachineBasicBlock &BB, LDVSSAUpdater &Updater)
3946 : BB(BB), Updater(Updater) {}
3947
3948 LDVSSABlockIterator succ_begin() {
3949 return LDVSSABlockIterator(BB.succ_begin(), Updater);
3950 }
3951
3952 LDVSSABlockIterator succ_end() {
3953 return LDVSSABlockIterator(BB.succ_end(), Updater);
3954 }
3955
3956 /// SSAUpdater has requested a PHI: create that within this block record.
3957 LDVSSAPhi *newPHI(BlockValueNum Value) {
3958 PHIList.emplace_back(Args&: Value, Args: this);
3959 return &PHIList.back();
3960 }
3961
3962 /// SSAUpdater wishes to know what PHIs already exist in this block.
3963 PHIListT &phis() { return PHIList; }
3964};
3965
3966/// Utility class for the SSAUpdater interface: tracks blocks, PHIs and values
3967/// while SSAUpdater is exploring the CFG. It's passed as a handle / baton to
3968// SSAUpdaterTraits<LDVSSAUpdater>.
3969class LDVSSAUpdater {
3970public:
3971 /// Map of value numbers to PHI records.
3972 DenseMap<BlockValueNum, LDVSSAPhi *> PHIs;
3973 /// Map of which blocks generate Undef values -- blocks that are not
3974 /// dominated by any Def.
3975 DenseMap<MachineBasicBlock *, BlockValueNum> PoisonMap;
3976 /// Map of machine blocks to our own records of them.
3977 DenseMap<MachineBasicBlock *, LDVSSABlock *> BlockMap;
3978 /// Machine location where any PHI must occur.
3979 LocIdx Loc;
3980 /// Table of live-in machine value numbers for blocks / locations.
3981 const FuncValueTable &MLiveIns;
3982
3983 LDVSSAUpdater(LocIdx L, const FuncValueTable &MLiveIns)
3984 : Loc(L), MLiveIns(MLiveIns) {}
3985
3986 void reset() {
3987 for (auto &Block : BlockMap)
3988 delete Block.second;
3989
3990 PHIs.clear();
3991 PoisonMap.clear();
3992 BlockMap.clear();
3993 }
3994
3995 ~LDVSSAUpdater() { reset(); }
3996
3997 /// For a given MBB, create a wrapper block for it. Stores it in the
3998 /// LDVSSAUpdater block map.
3999 LDVSSABlock *getSSALDVBlock(MachineBasicBlock *BB) {
4000 auto [It, Inserted] = BlockMap.try_emplace(Key: BB);
4001 if (Inserted)
4002 It->second = new LDVSSABlock(*BB, *this);
4003 return It->second;
4004 }
4005
4006 /// Find the live-in value number for the given block. Looks up the value at
4007 /// the PHI location on entry.
4008 BlockValueNum getValue(LDVSSABlock *LDVBB) {
4009 return MLiveIns[LDVBB->BB][Loc.asU64()].asU64();
4010 }
4011};
4012
4013LDVSSABlock *LDVSSABlockIterator::operator*() {
4014 return Updater.getSSALDVBlock(BB: *PredIt);
4015}
4016
4017#ifndef NDEBUG
4018
4019raw_ostream &operator<<(raw_ostream &out, const LDVSSAPhi &PHI) {
4020 out << "SSALDVPHI " << PHI.PHIValNum;
4021 return out;
4022}
4023
4024#endif
4025
4026} // namespace
4027
4028namespace llvm {
4029
4030/// Template specialization to give SSAUpdater access to CFG and value
4031/// information. SSAUpdater calls methods in these traits, passing in the
4032/// LDVSSAUpdater object, to learn about blocks and the values they define.
4033/// It also provides methods to create PHI nodes and track them.
4034template <> class SSAUpdaterTraits<LDVSSAUpdater> {
4035public:
4036 using BlkT = LDVSSABlock;
4037 using ValT = BlockValueNum;
4038 using PhiT = LDVSSAPhi;
4039 using BlkSucc_iterator = LDVSSABlockIterator;
4040
4041 // Methods to access block successors -- dereferencing to our wrapper class.
4042 static BlkSucc_iterator BlkSucc_begin(BlkT *BB) { return BB->succ_begin(); }
4043 static BlkSucc_iterator BlkSucc_end(BlkT *BB) { return BB->succ_end(); }
4044
4045 /// Iterator for PHI operands.
4046 class PHI_iterator {
4047 private:
4048 LDVSSAPhi *PHI;
4049 unsigned Idx;
4050
4051 public:
4052 explicit PHI_iterator(LDVSSAPhi *P) // begin iterator
4053 : PHI(P), Idx(0) {}
4054 PHI_iterator(LDVSSAPhi *P, bool) // end iterator
4055 : PHI(P), Idx(PHI->IncomingValues.size()) {}
4056
4057 PHI_iterator &operator++() {
4058 Idx++;
4059 return *this;
4060 }
4061 bool operator==(const PHI_iterator &X) const { return Idx == X.Idx; }
4062 bool operator!=(const PHI_iterator &X) const { return !operator==(X); }
4063
4064 BlockValueNum getIncomingValue() { return PHI->IncomingValues[Idx].second; }
4065
4066 LDVSSABlock *getIncomingBlock() { return PHI->IncomingValues[Idx].first; }
4067 };
4068
4069 static inline PHI_iterator PHI_begin(PhiT *PHI) { return PHI_iterator(PHI); }
4070
4071 static inline PHI_iterator PHI_end(PhiT *PHI) {
4072 return PHI_iterator(PHI, true);
4073 }
4074
4075 /// FindPredecessorBlocks - Put the predecessors of BB into the Preds
4076 /// vector.
4077 static void FindPredecessorBlocks(LDVSSABlock *BB,
4078 SmallVectorImpl<LDVSSABlock *> *Preds) {
4079 for (MachineBasicBlock *Pred : BB->BB.predecessors())
4080 Preds->push_back(Elt: BB->Updater.getSSALDVBlock(BB: Pred));
4081 }
4082
4083 /// GetPoisonVal - Normally creates an IMPLICIT_DEF instruction with a new
4084 /// register. For LiveDebugValues, represents a block identified as not having
4085 /// any DBG_PHI predecessors.
4086 static BlockValueNum GetPoisonVal(LDVSSABlock *BB, LDVSSAUpdater *Updater) {
4087 // Create a value number for this block -- it needs to be unique and in the
4088 // "poison" collection, so that we know it's not real. Use a number
4089 // representing a PHI into this block.
4090 BlockValueNum Num = ValueIDNum(BB->BB.getNumber(), 0, Updater->Loc).asU64();
4091 Updater->PoisonMap[&BB->BB] = Num;
4092 return Num;
4093 }
4094
4095 /// CreateEmptyPHI - Create a (representation of a) PHI in the given block.
4096 /// SSAUpdater will populate it with information about incoming values. The
4097 /// value number of this PHI is whatever the machine value number problem
4098 /// solution determined it to be. This includes non-phi values if SSAUpdater
4099 /// tries to create a PHI where the incoming values are identical.
4100 static BlockValueNum CreateEmptyPHI(LDVSSABlock *BB, unsigned NumPreds,
4101 LDVSSAUpdater *Updater) {
4102 BlockValueNum PHIValNum = Updater->getValue(LDVBB: BB);
4103 LDVSSAPhi *PHI = BB->newPHI(Value: PHIValNum);
4104 Updater->PHIs[PHIValNum] = PHI;
4105 return PHIValNum;
4106 }
4107
4108 /// AddPHIOperand - Add the specified value as an operand of the PHI for
4109 /// the specified predecessor block.
4110 static void AddPHIOperand(LDVSSAPhi *PHI, BlockValueNum Val, LDVSSABlock *Pred) {
4111 PHI->IncomingValues.push_back(Elt: std::make_pair(x&: Pred, y&: Val));
4112 }
4113
4114 /// ValueIsPHI - Check if the instruction that defines the specified value
4115 /// is a PHI instruction.
4116 static LDVSSAPhi *ValueIsPHI(BlockValueNum Val, LDVSSAUpdater *Updater) {
4117 return Updater->PHIs.lookup(Val);
4118 }
4119
4120 /// ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source
4121 /// operands, i.e., it was just added.
4122 static LDVSSAPhi *ValueIsNewPHI(BlockValueNum Val, LDVSSAUpdater *Updater) {
4123 LDVSSAPhi *PHI = ValueIsPHI(Val, Updater);
4124 if (PHI && PHI->IncomingValues.size() == 0)
4125 return PHI;
4126 return nullptr;
4127 }
4128
4129 /// GetPHIValue - For the specified PHI instruction, return the value
4130 /// that it defines.
4131 static BlockValueNum GetPHIValue(LDVSSAPhi *PHI) { return PHI->PHIValNum; }
4132};
4133
4134} // end namespace llvm
4135
4136std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIs(
4137 MachineFunction &MF, const FuncValueTable &MLiveOuts,
4138 const FuncValueTable &MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
4139 // This function will be called twice per DBG_INSTR_REF, and might end up
4140 // computing lots of SSA information: memoize it.
4141 auto SeenDbgPHIIt = SeenDbgPHIs.find(Val: std::make_pair(x: &Here, y&: InstrNum));
4142 if (SeenDbgPHIIt != SeenDbgPHIs.end())
4143 return SeenDbgPHIIt->second;
4144
4145 std::optional<ValueIDNum> Result =
4146 resolveDbgPHIsImpl(MF, MLiveOuts, MLiveIns, Here, InstrNum);
4147 SeenDbgPHIs.insert(KV: {std::make_pair(x: &Here, y&: InstrNum), Result});
4148 return Result;
4149}
4150
4151std::optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
4152 MachineFunction &MF, const FuncValueTable &MLiveOuts,
4153 const FuncValueTable &MLiveIns, MachineInstr &Here, uint64_t InstrNum) {
4154 // Pick out records of DBG_PHI instructions that have been observed. If there
4155 // are none, then we cannot compute a value number.
4156 auto RangePair = std::equal_range(first: DebugPHINumToValue.begin(),
4157 last: DebugPHINumToValue.end(), val: InstrNum);
4158 auto LowerIt = RangePair.first;
4159 auto UpperIt = RangePair.second;
4160
4161 // No DBG_PHI means there can be no location.
4162 if (LowerIt == UpperIt)
4163 return std::nullopt;
4164
4165 // If any DBG_PHIs referred to a location we didn't understand, don't try to
4166 // compute a value. There might be scenarios where we could recover a value
4167 // for some range of DBG_INSTR_REFs, but at this point we can have high
4168 // confidence that we've seen a bug.
4169 auto DBGPHIRange = make_range(x: LowerIt, y: UpperIt);
4170 for (const DebugPHIRecord &DBG_PHI : DBGPHIRange)
4171 if (!DBG_PHI.ValueRead)
4172 return std::nullopt;
4173
4174 // If there's only one DBG_PHI, then that is our value number.
4175 if (std::distance(first: LowerIt, last: UpperIt) == 1)
4176 return *LowerIt->ValueRead;
4177
4178 // Pick out the location (physreg, slot) where any PHIs must occur. It's
4179 // technically possible for us to merge values in different registers in each
4180 // block, but highly unlikely that LLVM will generate such code after register
4181 // allocation.
4182 LocIdx Loc = *LowerIt->ReadLoc;
4183
4184 // We have several DBG_PHIs, and a use position (the Here inst). All each
4185 // DBG_PHI does is identify a value at a program position. We can treat each
4186 // DBG_PHI like it's a Def of a value, and the use position is a Use of a
4187 // value, just like SSA. We use the bulk-standard LLVM SSA updater class to
4188 // determine which Def is used at the Use, and any PHIs that happen along
4189 // the way.
4190 // Adapted LLVM SSA Updater:
4191 LDVSSAUpdater Updater(Loc, MLiveIns);
4192 // Map of which Def or PHI is the current value in each block.
4193 DenseMap<LDVSSABlock *, BlockValueNum> AvailableValues;
4194 // Set of PHIs that we have created along the way.
4195 SmallVector<LDVSSAPhi *, 8> CreatedPHIs;
4196
4197 // Each existing DBG_PHI is a Def'd value under this model. Record these Defs
4198 // for the SSAUpdater.
4199 for (const auto &DBG_PHI : DBGPHIRange) {
4200 LDVSSABlock *Block = Updater.getSSALDVBlock(BB: DBG_PHI.MBB);
4201 const ValueIDNum &Num = *DBG_PHI.ValueRead;
4202 AvailableValues.insert(KV: std::make_pair(x&: Block, y: Num.asU64()));
4203 }
4204
4205 LDVSSABlock *HereBlock = Updater.getSSALDVBlock(BB: Here.getParent());
4206 const auto &AvailIt = AvailableValues.find(Val: HereBlock);
4207 if (AvailIt != AvailableValues.end()) {
4208 // Actually, we already know what the value is -- the Use is in the same
4209 // block as the Def.
4210 return ValueIDNum::fromU64(v: AvailIt->second);
4211 }
4212
4213 // Otherwise, we must use the SSA Updater. It will identify the value number
4214 // that we are to use, and the PHIs that must happen along the way.
4215 SSAUpdaterImpl<LDVSSAUpdater> Impl(&Updater, &AvailableValues, &CreatedPHIs);
4216 BlockValueNum ResultInt = Impl.GetValue(BB: Updater.getSSALDVBlock(BB: Here.getParent()));
4217 ValueIDNum Result = ValueIDNum::fromU64(v: ResultInt);
4218
4219 // We have the number for a PHI, or possibly live-through value, to be used
4220 // at this Use. There are a number of things we have to check about it though:
4221 // * Does any PHI use an 'Undef' (like an IMPLICIT_DEF) value? If so, this
4222 // Use was not completely dominated by DBG_PHIs and we should abort.
4223 // * Are the Defs or PHIs clobbered in a block? SSAUpdater isn't aware that
4224 // we've left SSA form. Validate that the inputs to each PHI are the
4225 // expected values.
4226 // * Is a PHI we've created actually a merging of values, or are all the
4227 // predecessor values the same, leading to a non-PHI machine value number?
4228 // (SSAUpdater doesn't know that either). Remap validated PHIs into the
4229 // the ValidatedValues collection below to sort this out.
4230 DenseMap<LDVSSABlock *, ValueIDNum> ValidatedValues;
4231
4232 // Define all the input DBG_PHI values in ValidatedValues.
4233 for (const auto &DBG_PHI : DBGPHIRange) {
4234 LDVSSABlock *Block = Updater.getSSALDVBlock(BB: DBG_PHI.MBB);
4235 const ValueIDNum &Num = *DBG_PHI.ValueRead;
4236 ValidatedValues.insert(KV: std::make_pair(x&: Block, y: Num));
4237 }
4238
4239 // Sort PHIs to validate into RPO-order.
4240 SmallVector<LDVSSAPhi *, 8> SortedPHIs(CreatedPHIs);
4241
4242 llvm::sort(C&: SortedPHIs, Comp: [&](LDVSSAPhi *A, LDVSSAPhi *B) {
4243 return BBToOrder[&A->getParent()->BB] < BBToOrder[&B->getParent()->BB];
4244 });
4245
4246 for (auto &PHI : SortedPHIs) {
4247 ValueIDNum ThisBlockValueNum = MLiveIns[PHI->ParentBlock->BB][Loc.asU64()];
4248
4249 // Are all these things actually defined?
4250 for (auto &PHIIt : PHI->IncomingValues) {
4251 // Any undef input means DBG_PHIs didn't dominate the use point.
4252 if (Updater.PoisonMap.contains(Val: &PHIIt.first->BB))
4253 return std::nullopt;
4254
4255 ValueIDNum ValueToCheck;
4256 const ValueTable &BlockLiveOuts = MLiveOuts[PHIIt.first->BB];
4257
4258 auto VVal = ValidatedValues.find(Val: PHIIt.first);
4259 if (VVal == ValidatedValues.end()) {
4260 // We cross a loop, and this is a backedge. LLVMs tail duplication
4261 // happens so late that DBG_PHI instructions should not be able to
4262 // migrate into loops -- meaning we can only be live-through this
4263 // loop.
4264 ValueToCheck = ThisBlockValueNum;
4265 } else {
4266 // Does the block have as a live-out, in the location we're examining,
4267 // the value that we expect? If not, it's been moved or clobbered.
4268 ValueToCheck = VVal->second;
4269 }
4270
4271 if (BlockLiveOuts[Loc.asU64()] != ValueToCheck)
4272 return std::nullopt;
4273 }
4274
4275 // Record this value as validated.
4276 ValidatedValues.insert(KV: {PHI->ParentBlock, ThisBlockValueNum});
4277 }
4278
4279 // All the PHIs are valid: we can return what the SSAUpdater said our value
4280 // number was.
4281 return Result;
4282}
4283