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