10b57cec5SDimitry Andric //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the LiveDebugVariables analysis. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // Remove all DBG_VALUE instructions referencing virtual registers and replace 120b57cec5SDimitry Andric // them with a data structure tracking where live user variables are kept - in a 130b57cec5SDimitry Andric // virtual register or in a stack slot. 140b57cec5SDimitry Andric // 150b57cec5SDimitry Andric // Allow the data structure to be updated during register allocation when values 160b57cec5SDimitry Andric // are moved between registers and stack slots. Finally emit new DBG_VALUE 170b57cec5SDimitry Andric // instructions after register allocation is complete. 180b57cec5SDimitry Andric // 190b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric #include "LiveDebugVariables.h" 220b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 230b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 240b57cec5SDimitry Andric #include "llvm/ADT/IntervalMap.h" 250b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 260b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 270b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 280b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 290b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 300b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 320b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 330b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 370b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 380b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 390b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 410b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 420b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 430b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 440b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 450b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 460b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h" 470b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 480b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 490b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 500b57cec5SDimitry Andric #include "llvm/IR/Function.h" 510b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 52480093f4SDimitry Andric #include "llvm/InitializePasses.h" 530b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 540b57cec5SDimitry Andric #include "llvm/Pass.h" 550b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 560b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 570b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 580b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 590b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 600b57cec5SDimitry Andric #include <algorithm> 610b57cec5SDimitry Andric #include <cassert> 620b57cec5SDimitry Andric #include <iterator> 630b57cec5SDimitry Andric #include <memory> 640b57cec5SDimitry Andric #include <utility> 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric using namespace llvm; 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric #define DEBUG_TYPE "livedebugvars" 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric static cl::opt<bool> 710b57cec5SDimitry Andric EnableLDV("live-debug-variables", cl::init(true), 720b57cec5SDimitry Andric cl::desc("Enable the live debug variables pass"), cl::Hidden); 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); 750b57cec5SDimitry Andric STATISTIC(NumInsertedDebugLabels, "Number of DBG_LABELs inserted"); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric char LiveDebugVariables::ID = 0; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE, 800b57cec5SDimitry Andric "Debug Variable Analysis", false, false) 810b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 820b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 830b57cec5SDimitry Andric INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE, 840b57cec5SDimitry Andric "Debug Variable Analysis", false, false) 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { 870b57cec5SDimitry Andric AU.addRequired<MachineDominatorTree>(); 880b57cec5SDimitry Andric AU.addRequiredTransitive<LiveIntervals>(); 890b57cec5SDimitry Andric AU.setPreservesAll(); 900b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) { 940b57cec5SDimitry Andric initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric enum : unsigned { UndefLocNo = ~0U }; 980b57cec5SDimitry Andric 995ffd83dbSDimitry Andric /// Describes a debug variable value by location number and expression along 1005ffd83dbSDimitry Andric /// with some flags about the original usage of the location. 1015ffd83dbSDimitry Andric class DbgVariableValue { 1020b57cec5SDimitry Andric public: 1035ffd83dbSDimitry Andric DbgVariableValue(unsigned LocNo, bool WasIndirect, 1045ffd83dbSDimitry Andric const DIExpression &Expression) 1055ffd83dbSDimitry Andric : LocNo(LocNo), WasIndirect(WasIndirect), Expression(&Expression) { 1065ffd83dbSDimitry Andric assert(getLocNo() == LocNo && "location truncation"); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1095ffd83dbSDimitry Andric DbgVariableValue() : LocNo(0), WasIndirect(0) {} 1100b57cec5SDimitry Andric 1115ffd83dbSDimitry Andric const DIExpression *getExpression() const { return Expression; } 1125ffd83dbSDimitry Andric unsigned getLocNo() const { 1130b57cec5SDimitry Andric // Fix up the undef location number, which gets truncated. 1140b57cec5SDimitry Andric return LocNo == INT_MAX ? UndefLocNo : LocNo; 1150b57cec5SDimitry Andric } 1165ffd83dbSDimitry Andric bool getWasIndirect() const { return WasIndirect; } 1175ffd83dbSDimitry Andric bool isUndef() const { return getLocNo() == UndefLocNo; } 1180b57cec5SDimitry Andric 1195ffd83dbSDimitry Andric DbgVariableValue changeLocNo(unsigned NewLocNo) const { 1205ffd83dbSDimitry Andric return DbgVariableValue(NewLocNo, WasIndirect, *Expression); 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 1235ffd83dbSDimitry Andric friend inline bool operator==(const DbgVariableValue &LHS, 1245ffd83dbSDimitry Andric const DbgVariableValue &RHS) { 1255ffd83dbSDimitry Andric return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect && 1265ffd83dbSDimitry Andric LHS.Expression == RHS.Expression; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1295ffd83dbSDimitry Andric friend inline bool operator!=(const DbgVariableValue &LHS, 1305ffd83dbSDimitry Andric const DbgVariableValue &RHS) { 1310b57cec5SDimitry Andric return !(LHS == RHS); 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric private: 13513138422SDimitry Andric unsigned LocNo : 31; 13613138422SDimitry Andric unsigned WasIndirect : 1; 1375ffd83dbSDimitry Andric const DIExpression *Expression = nullptr; 1380b57cec5SDimitry Andric }; 1390b57cec5SDimitry Andric 1405ffd83dbSDimitry Andric /// Map of where a user value is live to that value. 1415ffd83dbSDimitry Andric using LocMap = IntervalMap<SlotIndex, DbgVariableValue, 4>; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric /// Map of stack slot offsets for spilled locations. 1440b57cec5SDimitry Andric /// Non-spilled locations are not added to the map. 1450b57cec5SDimitry Andric using SpillOffsetMap = DenseMap<unsigned, unsigned>; 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric namespace { 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric class LDVImpl; 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric /// A user value is a part of a debug info user variable. 1520b57cec5SDimitry Andric /// 1530b57cec5SDimitry Andric /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register 1540b57cec5SDimitry Andric /// holds part of a user variable. The part is identified by a byte offset. 155480093f4SDimitry Andric /// 156480093f4SDimitry Andric /// UserValues are grouped into equivalence classes for easier searching. Two 1575ffd83dbSDimitry Andric /// user values are related if they are held by the same virtual register. The 1585ffd83dbSDimitry Andric /// equivalence class is the transitive closure of that relation. 1590b57cec5SDimitry Andric class UserValue { 1600b57cec5SDimitry Andric const DILocalVariable *Variable; ///< The debug info variable we are part of. 1615ffd83dbSDimitry Andric /// The part of the variable we describe. 1625ffd83dbSDimitry Andric const Optional<DIExpression::FragmentInfo> Fragment; 1630b57cec5SDimitry Andric DebugLoc dl; ///< The debug location for the variable. This is 1640b57cec5SDimitry Andric ///< used by dwarf writer to find lexical scope. 165480093f4SDimitry Andric UserValue *leader; ///< Equivalence class leader. 166480093f4SDimitry Andric UserValue *next = nullptr; ///< Next value in equivalence class, or null. 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric /// Numbered locations referenced by locmap. 1690b57cec5SDimitry Andric SmallVector<MachineOperand, 4> locations; 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric /// Map of slot indices where this value is live. 1720b57cec5SDimitry Andric LocMap locInts; 1730b57cec5SDimitry Andric 17413138422SDimitry Andric /// Set of interval start indexes that have been trimmed to the 17513138422SDimitry Andric /// lexical scope. 17613138422SDimitry Andric SmallSet<SlotIndex, 2> trimmedDefs; 17713138422SDimitry Andric 1785ffd83dbSDimitry Andric /// Insert a DBG_VALUE into MBB at Idx for DbgValue. 1790b57cec5SDimitry Andric void insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, 1805ffd83dbSDimitry Andric SlotIndex StopIdx, DbgVariableValue DbgValue, 1815ffd83dbSDimitry Andric bool Spilled, unsigned SpillOffset, LiveIntervals &LIS, 1820b57cec5SDimitry Andric const TargetInstrInfo &TII, 1830b57cec5SDimitry Andric const TargetRegisterInfo &TRI); 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric /// Replace OldLocNo ranges with NewRegs ranges where NewRegs 1860b57cec5SDimitry Andric /// is live. Returns true if any changes were made. 1875ffd83dbSDimitry Andric bool splitLocation(unsigned OldLocNo, ArrayRef<Register> NewRegs, 1880b57cec5SDimitry Andric LiveIntervals &LIS); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric public: 1910b57cec5SDimitry Andric /// Create a new UserValue. 1925ffd83dbSDimitry Andric UserValue(const DILocalVariable *var, 1935ffd83dbSDimitry Andric Optional<DIExpression::FragmentInfo> Fragment, DebugLoc L, 1940b57cec5SDimitry Andric LocMap::Allocator &alloc) 1955ffd83dbSDimitry Andric : Variable(var), Fragment(Fragment), dl(std::move(L)), leader(this), 196480093f4SDimitry Andric locInts(alloc) {} 1970b57cec5SDimitry Andric 198480093f4SDimitry Andric /// Get the leader of this value's equivalence class. 199480093f4SDimitry Andric UserValue *getLeader() { 200480093f4SDimitry Andric UserValue *l = leader; 201480093f4SDimitry Andric while (l != l->leader) 202480093f4SDimitry Andric l = l->leader; 203480093f4SDimitry Andric return leader = l; 204480093f4SDimitry Andric } 205480093f4SDimitry Andric 206480093f4SDimitry Andric /// Return the next UserValue in the equivalence class. 207480093f4SDimitry Andric UserValue *getNext() const { return next; } 208480093f4SDimitry Andric 209480093f4SDimitry Andric /// Merge equivalence classes. 210480093f4SDimitry Andric static UserValue *merge(UserValue *L1, UserValue *L2) { 211480093f4SDimitry Andric L2 = L2->getLeader(); 212480093f4SDimitry Andric if (!L1) 213480093f4SDimitry Andric return L2; 214480093f4SDimitry Andric L1 = L1->getLeader(); 215480093f4SDimitry Andric if (L1 == L2) 216480093f4SDimitry Andric return L1; 217480093f4SDimitry Andric // Splice L2 before L1's members. 218480093f4SDimitry Andric UserValue *End = L2; 219480093f4SDimitry Andric while (End->next) { 220480093f4SDimitry Andric End->leader = L1; 221480093f4SDimitry Andric End = End->next; 222480093f4SDimitry Andric } 223480093f4SDimitry Andric End->leader = L1; 224480093f4SDimitry Andric End->next = L1->next; 225480093f4SDimitry Andric L1->next = L2; 226480093f4SDimitry Andric return L1; 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric /// Return the location number that matches Loc. 2300b57cec5SDimitry Andric /// 2310b57cec5SDimitry Andric /// For undef values we always return location number UndefLocNo without 2320b57cec5SDimitry Andric /// inserting anything in locations. Since locations is a vector and the 2330b57cec5SDimitry Andric /// location number is the position in the vector and UndefLocNo is ~0, 2340b57cec5SDimitry Andric /// we would need a very big vector to put the value at the right position. 2350b57cec5SDimitry Andric unsigned getLocationNo(const MachineOperand &LocMO) { 2360b57cec5SDimitry Andric if (LocMO.isReg()) { 2370b57cec5SDimitry Andric if (LocMO.getReg() == 0) 2380b57cec5SDimitry Andric return UndefLocNo; 2390b57cec5SDimitry Andric // For register locations we dont care about use/def and other flags. 2400b57cec5SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 2410b57cec5SDimitry Andric if (locations[i].isReg() && 2420b57cec5SDimitry Andric locations[i].getReg() == LocMO.getReg() && 2430b57cec5SDimitry Andric locations[i].getSubReg() == LocMO.getSubReg()) 2440b57cec5SDimitry Andric return i; 2450b57cec5SDimitry Andric } else 2460b57cec5SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 2470b57cec5SDimitry Andric if (LocMO.isIdenticalTo(locations[i])) 2480b57cec5SDimitry Andric return i; 2490b57cec5SDimitry Andric locations.push_back(LocMO); 2500b57cec5SDimitry Andric // We are storing a MachineOperand outside a MachineInstr. 2510b57cec5SDimitry Andric locations.back().clearParent(); 2520b57cec5SDimitry Andric // Don't store def operands. 2530b57cec5SDimitry Andric if (locations.back().isReg()) { 2540b57cec5SDimitry Andric if (locations.back().isDef()) 2550b57cec5SDimitry Andric locations.back().setIsDead(false); 2560b57cec5SDimitry Andric locations.back().setIsUse(); 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric return locations.size() - 1; 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric 261480093f4SDimitry Andric /// Remove (recycle) a location number. If \p LocNo still is used by the 262480093f4SDimitry Andric /// locInts nothing is done. 263480093f4SDimitry Andric void removeLocationIfUnused(unsigned LocNo) { 264480093f4SDimitry Andric // Bail out if LocNo still is used. 265480093f4SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 2665ffd83dbSDimitry Andric DbgVariableValue DbgValue = I.value(); 2675ffd83dbSDimitry Andric if (DbgValue.getLocNo() == LocNo) 268480093f4SDimitry Andric return; 269480093f4SDimitry Andric } 270480093f4SDimitry Andric // Remove the entry in the locations vector, and adjust all references to 271480093f4SDimitry Andric // location numbers above the removed entry. 272480093f4SDimitry Andric locations.erase(locations.begin() + LocNo); 273480093f4SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 2745ffd83dbSDimitry Andric DbgVariableValue DbgValue = I.value(); 2755ffd83dbSDimitry Andric if (!DbgValue.isUndef() && DbgValue.getLocNo() > LocNo) 2765ffd83dbSDimitry Andric I.setValueUnchecked(DbgValue.changeLocNo(DbgValue.getLocNo() - 1)); 277480093f4SDimitry Andric } 278480093f4SDimitry Andric } 279480093f4SDimitry Andric 2800b57cec5SDimitry Andric /// Ensure that all virtual register locations are mapped. 2810b57cec5SDimitry Andric void mapVirtRegs(LDVImpl *LDV); 2820b57cec5SDimitry Andric 2835ffd83dbSDimitry Andric /// Add a definition point to this user value. 2845ffd83dbSDimitry Andric void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect, 2855ffd83dbSDimitry Andric const DIExpression &Expr) { 2865ffd83dbSDimitry Andric DbgVariableValue DbgValue(getLocationNo(LocMO), IsIndirect, Expr); 2875ffd83dbSDimitry Andric // Add a singular (Idx,Idx) -> value mapping. 2880b57cec5SDimitry Andric LocMap::iterator I = locInts.find(Idx); 2890b57cec5SDimitry Andric if (!I.valid() || I.start() != Idx) 2905ffd83dbSDimitry Andric I.insert(Idx, Idx.getNextSlot(), DbgValue); 2910b57cec5SDimitry Andric else 2920b57cec5SDimitry Andric // A later DBG_VALUE at the same SlotIndex overrides the old location. 2935ffd83dbSDimitry Andric I.setValue(DbgValue); 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric /// Extend the current definition as far as possible down. 2970b57cec5SDimitry Andric /// 2980b57cec5SDimitry Andric /// Stop when meeting an existing def or when leaving the live 2990b57cec5SDimitry Andric /// range of VNI. End points where VNI is no longer live are added to Kills. 3000b57cec5SDimitry Andric /// 3010b57cec5SDimitry Andric /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a 3020b57cec5SDimitry Andric /// data-flow analysis to propagate them beyond basic block boundaries. 3030b57cec5SDimitry Andric /// 3040b57cec5SDimitry Andric /// \param Idx Starting point for the definition. 3055ffd83dbSDimitry Andric /// \param DbgValue value to propagate. 3060b57cec5SDimitry Andric /// \param LR Restrict liveness to where LR has the value VNI. May be null. 3070b57cec5SDimitry Andric /// \param VNI When LR is not null, this is the value to restrict to. 3080b57cec5SDimitry Andric /// \param [out] Kills Append end points of VNI's live range to Kills. 3090b57cec5SDimitry Andric /// \param LIS Live intervals analysis. 3105ffd83dbSDimitry Andric void extendDef(SlotIndex Idx, DbgVariableValue DbgValue, LiveRange *LR, 3115ffd83dbSDimitry Andric const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills, 3120b57cec5SDimitry Andric LiveIntervals &LIS); 3130b57cec5SDimitry Andric 3145ffd83dbSDimitry Andric /// The value in LI may be copies to other registers. Determine if 3150b57cec5SDimitry Andric /// any of the copies are available at the kill points, and add defs if 3160b57cec5SDimitry Andric /// possible. 3170b57cec5SDimitry Andric /// 3180b57cec5SDimitry Andric /// \param LI Scan for copies of the value in LI->reg. 3195ffd83dbSDimitry Andric /// \param DbgValue Location number of LI->reg, and DIExpression. 3205ffd83dbSDimitry Andric /// \param Kills Points where the range of DbgValue could be extended. 3215ffd83dbSDimitry Andric /// \param [in,out] NewDefs Append (Idx, DbgValue) of inserted defs here. 3220b57cec5SDimitry Andric void addDefsFromCopies( 3235ffd83dbSDimitry Andric LiveInterval *LI, DbgVariableValue DbgValue, 3240b57cec5SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 3255ffd83dbSDimitry Andric SmallVectorImpl<std::pair<SlotIndex, DbgVariableValue>> &NewDefs, 3260b57cec5SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS); 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric /// Compute the live intervals of all locations after collecting all their 3290b57cec5SDimitry Andric /// def points. 3300b57cec5SDimitry Andric void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, 3310b57cec5SDimitry Andric LiveIntervals &LIS, LexicalScopes &LS); 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric /// Replace OldReg ranges with NewRegs ranges where NewRegs is 3340b57cec5SDimitry Andric /// live. Returns true if any changes were made. 3355ffd83dbSDimitry Andric bool splitRegister(Register OldReg, ArrayRef<Register> NewRegs, 3360b57cec5SDimitry Andric LiveIntervals &LIS); 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric /// Rewrite virtual register locations according to the provided virtual 3390b57cec5SDimitry Andric /// register map. Record the stack slot offsets for the locations that 3400b57cec5SDimitry Andric /// were spilled. 3410b57cec5SDimitry Andric void rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF, 3420b57cec5SDimitry Andric const TargetInstrInfo &TII, 3430b57cec5SDimitry Andric const TargetRegisterInfo &TRI, 3440b57cec5SDimitry Andric SpillOffsetMap &SpillOffsets); 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric /// Recreate DBG_VALUE instruction from data structures. 3470b57cec5SDimitry Andric void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 3480b57cec5SDimitry Andric const TargetInstrInfo &TII, 3490b57cec5SDimitry Andric const TargetRegisterInfo &TRI, 3500b57cec5SDimitry Andric const SpillOffsetMap &SpillOffsets); 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric /// Return DebugLoc of this UserValue. 3530b57cec5SDimitry Andric DebugLoc getDebugLoc() { return dl;} 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric void print(raw_ostream &, const TargetRegisterInfo *); 3560b57cec5SDimitry Andric }; 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric /// A user label is a part of a debug info user label. 3590b57cec5SDimitry Andric class UserLabel { 3600b57cec5SDimitry Andric const DILabel *Label; ///< The debug info label we are part of. 3610b57cec5SDimitry Andric DebugLoc dl; ///< The debug location for the label. This is 3620b57cec5SDimitry Andric ///< used by dwarf writer to find lexical scope. 3630b57cec5SDimitry Andric SlotIndex loc; ///< Slot used by the debug label. 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric /// Insert a DBG_LABEL into MBB at Idx. 3660b57cec5SDimitry Andric void insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx, 3670b57cec5SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TII); 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric public: 3700b57cec5SDimitry Andric /// Create a new UserLabel. 3710b57cec5SDimitry Andric UserLabel(const DILabel *label, DebugLoc L, SlotIndex Idx) 3720b57cec5SDimitry Andric : Label(label), dl(std::move(L)), loc(Idx) {} 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric /// Does this UserLabel match the parameters? 3755ffd83dbSDimitry Andric bool matches(const DILabel *L, const DILocation *IA, 3760b57cec5SDimitry Andric const SlotIndex Index) const { 3770b57cec5SDimitry Andric return Label == L && dl->getInlinedAt() == IA && loc == Index; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric /// Recreate DBG_LABEL instruction from data structures. 3810b57cec5SDimitry Andric void emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric /// Return DebugLoc of this UserLabel. 3840b57cec5SDimitry Andric DebugLoc getDebugLoc() { return dl; } 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric void print(raw_ostream &, const TargetRegisterInfo *); 3870b57cec5SDimitry Andric }; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric /// Implementation of the LiveDebugVariables pass. 3900b57cec5SDimitry Andric class LDVImpl { 3910b57cec5SDimitry Andric LiveDebugVariables &pass; 3920b57cec5SDimitry Andric LocMap::Allocator allocator; 3930b57cec5SDimitry Andric MachineFunction *MF = nullptr; 3940b57cec5SDimitry Andric LiveIntervals *LIS; 3950b57cec5SDimitry Andric const TargetRegisterInfo *TRI; 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric /// Whether emitDebugValues is called. 3980b57cec5SDimitry Andric bool EmitDone = false; 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric /// Whether the machine function is modified during the pass. 4010b57cec5SDimitry Andric bool ModifiedMF = false; 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric /// All allocated UserValue instances. 4040b57cec5SDimitry Andric SmallVector<std::unique_ptr<UserValue>, 8> userValues; 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric /// All allocated UserLabel instances. 4070b57cec5SDimitry Andric SmallVector<std::unique_ptr<UserLabel>, 2> userLabels; 4080b57cec5SDimitry Andric 409480093f4SDimitry Andric /// Map virtual register to eq class leader. 410480093f4SDimitry Andric using VRMap = DenseMap<unsigned, UserValue *>; 411480093f4SDimitry Andric VRMap virtRegToEqClass; 4120b57cec5SDimitry Andric 4135ffd83dbSDimitry Andric /// Map to find existing UserValue instances. 4145ffd83dbSDimitry Andric using UVMap = DenseMap<DebugVariable, UserValue *>; 415480093f4SDimitry Andric UVMap userVarMap; 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric /// Find or create a UserValue. 4185ffd83dbSDimitry Andric UserValue *getUserValue(const DILocalVariable *Var, 4195ffd83dbSDimitry Andric Optional<DIExpression::FragmentInfo> Fragment, 4200b57cec5SDimitry Andric const DebugLoc &DL); 4210b57cec5SDimitry Andric 422480093f4SDimitry Andric /// Find the EC leader for VirtReg or null. 4235ffd83dbSDimitry Andric UserValue *lookupVirtReg(Register VirtReg); 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric /// Add DBG_VALUE instruction to our maps. 4260b57cec5SDimitry Andric /// 4270b57cec5SDimitry Andric /// \param MI DBG_VALUE instruction 4280b57cec5SDimitry Andric /// \param Idx Last valid SLotIndex before instruction. 4290b57cec5SDimitry Andric /// 4300b57cec5SDimitry Andric /// \returns True if the DBG_VALUE instruction should be deleted. 4310b57cec5SDimitry Andric bool handleDebugValue(MachineInstr &MI, SlotIndex Idx); 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric /// Add DBG_LABEL instruction to UserLabel. 4340b57cec5SDimitry Andric /// 4350b57cec5SDimitry Andric /// \param MI DBG_LABEL instruction 4360b57cec5SDimitry Andric /// \param Idx Last valid SlotIndex before instruction. 4370b57cec5SDimitry Andric /// 4380b57cec5SDimitry Andric /// \returns True if the DBG_LABEL instruction should be deleted. 4390b57cec5SDimitry Andric bool handleDebugLabel(MachineInstr &MI, SlotIndex Idx); 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric /// Collect and erase all DBG_VALUE instructions, adding a UserValue def 4420b57cec5SDimitry Andric /// for each instruction. 4430b57cec5SDimitry Andric /// 4440b57cec5SDimitry Andric /// \param mf MachineFunction to be scanned. 4450b57cec5SDimitry Andric /// 4460b57cec5SDimitry Andric /// \returns True if any debug values were found. 4470b57cec5SDimitry Andric bool collectDebugValues(MachineFunction &mf); 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric /// Compute the live intervals of all user values after collecting all 4500b57cec5SDimitry Andric /// their def points. 4510b57cec5SDimitry Andric void computeIntervals(); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric public: 4540b57cec5SDimitry Andric LDVImpl(LiveDebugVariables *ps) : pass(*ps) {} 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &mf); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric /// Release all memory. 4590b57cec5SDimitry Andric void clear() { 4600b57cec5SDimitry Andric MF = nullptr; 4610b57cec5SDimitry Andric userValues.clear(); 4620b57cec5SDimitry Andric userLabels.clear(); 463480093f4SDimitry Andric virtRegToEqClass.clear(); 464480093f4SDimitry Andric userVarMap.clear(); 4650b57cec5SDimitry Andric // Make sure we call emitDebugValues if the machine function was modified. 4660b57cec5SDimitry Andric assert((!ModifiedMF || EmitDone) && 4670b57cec5SDimitry Andric "Dbg values are not emitted in LDV"); 4680b57cec5SDimitry Andric EmitDone = false; 4690b57cec5SDimitry Andric ModifiedMF = false; 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric 472480093f4SDimitry Andric /// Map virtual register to an equivalence class. 4735ffd83dbSDimitry Andric void mapVirtReg(Register VirtReg, UserValue *EC); 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric /// Replace all references to OldReg with NewRegs. 4765ffd83dbSDimitry Andric void splitRegister(Register OldReg, ArrayRef<Register> NewRegs); 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric /// Recreate DBG_VALUE instruction from data structures. 4790b57cec5SDimitry Andric void emitDebugValues(VirtRegMap *VRM); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric void print(raw_ostream&); 4820b57cec5SDimitry Andric }; 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric } // end anonymous namespace 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4870b57cec5SDimitry Andric static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS, 4880b57cec5SDimitry Andric const LLVMContext &Ctx) { 4890b57cec5SDimitry Andric if (!DL) 4900b57cec5SDimitry Andric return; 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric auto *Scope = cast<DIScope>(DL.getScope()); 4930b57cec5SDimitry Andric // Omit the directory, because it's likely to be long and uninteresting. 4940b57cec5SDimitry Andric CommentOS << Scope->getFilename(); 4950b57cec5SDimitry Andric CommentOS << ':' << DL.getLine(); 4960b57cec5SDimitry Andric if (DL.getCol() != 0) 4970b57cec5SDimitry Andric CommentOS << ':' << DL.getCol(); 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric DebugLoc InlinedAtDL = DL.getInlinedAt(); 5000b57cec5SDimitry Andric if (!InlinedAtDL) 5010b57cec5SDimitry Andric return; 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric CommentOS << " @[ "; 5040b57cec5SDimitry Andric printDebugLoc(InlinedAtDL, CommentOS, Ctx); 5050b57cec5SDimitry Andric CommentOS << " ]"; 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric static void printExtendedName(raw_ostream &OS, const DINode *Node, 5090b57cec5SDimitry Andric const DILocation *DL) { 5100b57cec5SDimitry Andric const LLVMContext &Ctx = Node->getContext(); 5110b57cec5SDimitry Andric StringRef Res; 512480093f4SDimitry Andric unsigned Line = 0; 5130b57cec5SDimitry Andric if (const auto *V = dyn_cast<const DILocalVariable>(Node)) { 5140b57cec5SDimitry Andric Res = V->getName(); 5150b57cec5SDimitry Andric Line = V->getLine(); 5160b57cec5SDimitry Andric } else if (const auto *L = dyn_cast<const DILabel>(Node)) { 5170b57cec5SDimitry Andric Res = L->getName(); 5180b57cec5SDimitry Andric Line = L->getLine(); 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric if (!Res.empty()) 5220b57cec5SDimitry Andric OS << Res << "," << Line; 5230b57cec5SDimitry Andric auto *InlinedAt = DL ? DL->getInlinedAt() : nullptr; 5240b57cec5SDimitry Andric if (InlinedAt) { 5250b57cec5SDimitry Andric if (DebugLoc InlinedAtDL = InlinedAt) { 5260b57cec5SDimitry Andric OS << " @["; 5270b57cec5SDimitry Andric printDebugLoc(InlinedAtDL, OS, Ctx); 5280b57cec5SDimitry Andric OS << "]"; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { 5340b57cec5SDimitry Andric OS << "!\""; 5350b57cec5SDimitry Andric printExtendedName(OS, Variable, dl); 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric OS << "\"\t"; 5380b57cec5SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { 5390b57cec5SDimitry Andric OS << " [" << I.start() << ';' << I.stop() << "):"; 5400b57cec5SDimitry Andric if (I.value().isUndef()) 5410b57cec5SDimitry Andric OS << "undef"; 5420b57cec5SDimitry Andric else { 5435ffd83dbSDimitry Andric OS << I.value().getLocNo(); 5445ffd83dbSDimitry Andric if (I.value().getWasIndirect()) 54513138422SDimitry Andric OS << " ind"; 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) { 5490b57cec5SDimitry Andric OS << " Loc" << i << '='; 5500b57cec5SDimitry Andric locations[i].print(OS, TRI); 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric OS << '\n'; 5530b57cec5SDimitry Andric } 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric void UserLabel::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { 5560b57cec5SDimitry Andric OS << "!\""; 5570b57cec5SDimitry Andric printExtendedName(OS, Label, dl); 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric OS << "\"\t"; 5600b57cec5SDimitry Andric OS << loc; 5610b57cec5SDimitry Andric OS << '\n'; 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric void LDVImpl::print(raw_ostream &OS) { 5650b57cec5SDimitry Andric OS << "********** DEBUG VARIABLES **********\n"; 5660b57cec5SDimitry Andric for (auto &userValue : userValues) 5670b57cec5SDimitry Andric userValue->print(OS, TRI); 5680b57cec5SDimitry Andric OS << "********** DEBUG LABELS **********\n"; 5690b57cec5SDimitry Andric for (auto &userLabel : userLabels) 5700b57cec5SDimitry Andric userLabel->print(OS, TRI); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric #endif 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric void UserValue::mapVirtRegs(LDVImpl *LDV) { 5750b57cec5SDimitry Andric for (unsigned i = 0, e = locations.size(); i != e; ++i) 5760b57cec5SDimitry Andric if (locations[i].isReg() && 5778bcb0991SDimitry Andric Register::isVirtualRegister(locations[i].getReg())) 5780b57cec5SDimitry Andric LDV->mapVirtReg(locations[i].getReg(), this); 5790b57cec5SDimitry Andric } 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric UserValue *LDVImpl::getUserValue(const DILocalVariable *Var, 5825ffd83dbSDimitry Andric Optional<DIExpression::FragmentInfo> Fragment, 5835ffd83dbSDimitry Andric const DebugLoc &DL) { 5845ffd83dbSDimitry Andric // FIXME: Handle partially overlapping fragments. See 5855ffd83dbSDimitry Andric // https://reviews.llvm.org/D70121#1849741. 5865ffd83dbSDimitry Andric DebugVariable ID(Var, Fragment, DL->getInlinedAt()); 5875ffd83dbSDimitry Andric UserValue *&UV = userVarMap[ID]; 5885ffd83dbSDimitry Andric if (!UV) { 589480093f4SDimitry Andric userValues.push_back( 5905ffd83dbSDimitry Andric std::make_unique<UserValue>(Var, Fragment, DL, allocator)); 5915ffd83dbSDimitry Andric UV = userValues.back().get(); 5925ffd83dbSDimitry Andric } 593480093f4SDimitry Andric return UV; 594480093f4SDimitry Andric } 595480093f4SDimitry Andric 5965ffd83dbSDimitry Andric void LDVImpl::mapVirtReg(Register VirtReg, UserValue *EC) { 5978bcb0991SDimitry Andric assert(Register::isVirtualRegister(VirtReg) && "Only map VirtRegs"); 598480093f4SDimitry Andric UserValue *&Leader = virtRegToEqClass[VirtReg]; 599480093f4SDimitry Andric Leader = UserValue::merge(Leader, EC); 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6025ffd83dbSDimitry Andric UserValue *LDVImpl::lookupVirtReg(Register VirtReg) { 603480093f4SDimitry Andric if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) 604480093f4SDimitry Andric return UV->getLeader(); 6050b57cec5SDimitry Andric return nullptr; 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) { 6090b57cec5SDimitry Andric // DBG_VALUE loc, offset, variable 6100b57cec5SDimitry Andric if (MI.getNumOperands() != 4 || 6115ffd83dbSDimitry Andric !(MI.getDebugOffset().isReg() || MI.getDebugOffset().isImm()) || 6125ffd83dbSDimitry Andric !MI.getDebugVariableOp().isMetadata()) { 6130b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Can't handle " << MI); 6140b57cec5SDimitry Andric return false; 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric // Detect invalid DBG_VALUE instructions, with a debug-use of a virtual 6180b57cec5SDimitry Andric // register that hasn't been defined yet. If we do not remove those here, then 6190b57cec5SDimitry Andric // the re-insertion of the DBG_VALUE instruction after register allocation 6200b57cec5SDimitry Andric // will be incorrect. 6210b57cec5SDimitry Andric // TODO: If earlier passes are corrected to generate sane debug information 6220b57cec5SDimitry Andric // (and if the machine verifier is improved to catch this), then these checks 6230b57cec5SDimitry Andric // could be removed or replaced by asserts. 6240b57cec5SDimitry Andric bool Discard = false; 6255ffd83dbSDimitry Andric if (MI.getDebugOperand(0).isReg() && 6265ffd83dbSDimitry Andric Register::isVirtualRegister(MI.getDebugOperand(0).getReg())) { 6275ffd83dbSDimitry Andric const Register Reg = MI.getDebugOperand(0).getReg(); 6280b57cec5SDimitry Andric if (!LIS->hasInterval(Reg)) { 6290b57cec5SDimitry Andric // The DBG_VALUE is described by a virtual register that does not have a 6300b57cec5SDimitry Andric // live interval. Discard the DBG_VALUE. 6310b57cec5SDimitry Andric Discard = true; 6320b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Discarding debug info (no LIS interval): " << Idx 6330b57cec5SDimitry Andric << " " << MI); 6340b57cec5SDimitry Andric } else { 6350b57cec5SDimitry Andric // The DBG_VALUE is only valid if either Reg is live out from Idx, or Reg 6360b57cec5SDimitry Andric // is defined dead at Idx (where Idx is the slot index for the instruction 6370b57cec5SDimitry Andric // preceding the DBG_VALUE). 6380b57cec5SDimitry Andric const LiveInterval &LI = LIS->getInterval(Reg); 6390b57cec5SDimitry Andric LiveQueryResult LRQ = LI.Query(Idx); 6400b57cec5SDimitry Andric if (!LRQ.valueOutOrDead()) { 6410b57cec5SDimitry Andric // We have found a DBG_VALUE with the value in a virtual register that 6420b57cec5SDimitry Andric // is not live. Discard the DBG_VALUE. 6430b57cec5SDimitry Andric Discard = true; 6440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Discarding debug info (reg not live): " << Idx 6450b57cec5SDimitry Andric << " " << MI); 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric } 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric // Get or create the UserValue for (variable,offset) here. 6515ffd83dbSDimitry Andric bool IsIndirect = MI.isDebugOffsetImm(); 65213138422SDimitry Andric if (IsIndirect) 6535ffd83dbSDimitry Andric assert(MI.getDebugOffset().getImm() == 0 && 6545ffd83dbSDimitry Andric "DBG_VALUE with nonzero offset"); 6550b57cec5SDimitry Andric const DILocalVariable *Var = MI.getDebugVariable(); 6560b57cec5SDimitry Andric const DIExpression *Expr = MI.getDebugExpression(); 6575ffd83dbSDimitry Andric UserValue *UV = getUserValue(Var, Expr->getFragmentInfo(), MI.getDebugLoc()); 6580b57cec5SDimitry Andric if (!Discard) 6595ffd83dbSDimitry Andric UV->addDef(Idx, MI.getDebugOperand(0), IsIndirect, *Expr); 6600b57cec5SDimitry Andric else { 6610b57cec5SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(0U, false); 6620b57cec5SDimitry Andric MO.setIsDebug(); 6635ffd83dbSDimitry Andric UV->addDef(Idx, MO, false, *Expr); 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric return true; 6660b57cec5SDimitry Andric } 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric bool LDVImpl::handleDebugLabel(MachineInstr &MI, SlotIndex Idx) { 6690b57cec5SDimitry Andric // DBG_LABEL label 6700b57cec5SDimitry Andric if (MI.getNumOperands() != 1 || !MI.getOperand(0).isMetadata()) { 6710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Can't handle " << MI); 6720b57cec5SDimitry Andric return false; 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric // Get or create the UserLabel for label here. 6760b57cec5SDimitry Andric const DILabel *Label = MI.getDebugLabel(); 6770b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc(); 6780b57cec5SDimitry Andric bool Found = false; 6790b57cec5SDimitry Andric for (auto const &L : userLabels) { 6805ffd83dbSDimitry Andric if (L->matches(Label, DL->getInlinedAt(), Idx)) { 6810b57cec5SDimitry Andric Found = true; 6820b57cec5SDimitry Andric break; 6830b57cec5SDimitry Andric } 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric if (!Found) 6868bcb0991SDimitry Andric userLabels.push_back(std::make_unique<UserLabel>(Label, DL, Idx)); 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric return true; 6890b57cec5SDimitry Andric } 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric bool LDVImpl::collectDebugValues(MachineFunction &mf) { 6920b57cec5SDimitry Andric bool Changed = false; 6930b57cec5SDimitry Andric for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; 6940b57cec5SDimitry Andric ++MFI) { 6950b57cec5SDimitry Andric MachineBasicBlock *MBB = &*MFI; 6960b57cec5SDimitry Andric for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); 6970b57cec5SDimitry Andric MBBI != MBBE;) { 6980b57cec5SDimitry Andric // Use the first debug instruction in the sequence to get a SlotIndex 6990b57cec5SDimitry Andric // for following consecutive debug instructions. 7000b57cec5SDimitry Andric if (!MBBI->isDebugInstr()) { 7010b57cec5SDimitry Andric ++MBBI; 7020b57cec5SDimitry Andric continue; 7030b57cec5SDimitry Andric } 7040b57cec5SDimitry Andric // Debug instructions has no slot index. Use the previous 7050b57cec5SDimitry Andric // non-debug instruction's SlotIndex as its SlotIndex. 7060b57cec5SDimitry Andric SlotIndex Idx = 7070b57cec5SDimitry Andric MBBI == MBB->begin() 7080b57cec5SDimitry Andric ? LIS->getMBBStartIdx(MBB) 7090b57cec5SDimitry Andric : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot(); 7100b57cec5SDimitry Andric // Handle consecutive debug instructions with the same slot index. 7110b57cec5SDimitry Andric do { 7120b57cec5SDimitry Andric // Only handle DBG_VALUE in handleDebugValue(). Skip all other 7130b57cec5SDimitry Andric // kinds of debug instructions. 7140b57cec5SDimitry Andric if ((MBBI->isDebugValue() && handleDebugValue(*MBBI, Idx)) || 7150b57cec5SDimitry Andric (MBBI->isDebugLabel() && handleDebugLabel(*MBBI, Idx))) { 7160b57cec5SDimitry Andric MBBI = MBB->erase(MBBI); 7170b57cec5SDimitry Andric Changed = true; 7180b57cec5SDimitry Andric } else 7190b57cec5SDimitry Andric ++MBBI; 7200b57cec5SDimitry Andric } while (MBBI != MBBE && MBBI->isDebugInstr()); 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric return Changed; 7240b57cec5SDimitry Andric } 7250b57cec5SDimitry Andric 7265ffd83dbSDimitry Andric void UserValue::extendDef(SlotIndex Idx, DbgVariableValue DbgValue, LiveRange *LR, 7270b57cec5SDimitry Andric const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills, 7280b57cec5SDimitry Andric LiveIntervals &LIS) { 7290b57cec5SDimitry Andric SlotIndex Start = Idx; 7300b57cec5SDimitry Andric MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); 7310b57cec5SDimitry Andric SlotIndex Stop = LIS.getMBBEndIdx(MBB); 7320b57cec5SDimitry Andric LocMap::iterator I = locInts.find(Start); 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // Limit to VNI's live range. 7350b57cec5SDimitry Andric bool ToEnd = true; 7360b57cec5SDimitry Andric if (LR && VNI) { 7370b57cec5SDimitry Andric LiveInterval::Segment *Segment = LR->getSegmentContaining(Start); 7380b57cec5SDimitry Andric if (!Segment || Segment->valno != VNI) { 7390b57cec5SDimitry Andric if (Kills) 7400b57cec5SDimitry Andric Kills->push_back(Start); 7410b57cec5SDimitry Andric return; 7420b57cec5SDimitry Andric } 7430b57cec5SDimitry Andric if (Segment->end < Stop) { 7440b57cec5SDimitry Andric Stop = Segment->end; 7450b57cec5SDimitry Andric ToEnd = false; 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric // There could already be a short def at Start. 7500b57cec5SDimitry Andric if (I.valid() && I.start() <= Start) { 7510b57cec5SDimitry Andric // Stop when meeting a different location or an already extended interval. 7520b57cec5SDimitry Andric Start = Start.getNextSlot(); 7535ffd83dbSDimitry Andric if (I.value() != DbgValue || I.stop() != Start) 7540b57cec5SDimitry Andric return; 7550b57cec5SDimitry Andric // This is a one-slot placeholder. Just skip it. 7560b57cec5SDimitry Andric ++I; 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric // Limited by the next def. 7600b57cec5SDimitry Andric if (I.valid() && I.start() < Stop) 7610b57cec5SDimitry Andric Stop = I.start(); 7620b57cec5SDimitry Andric // Limited by VNI's live range. 7630b57cec5SDimitry Andric else if (!ToEnd && Kills) 7640b57cec5SDimitry Andric Kills->push_back(Stop); 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric if (Start < Stop) 7675ffd83dbSDimitry Andric I.insert(Start, Stop, DbgValue); 7680b57cec5SDimitry Andric } 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric void UserValue::addDefsFromCopies( 7715ffd83dbSDimitry Andric LiveInterval *LI, DbgVariableValue DbgValue, 7720b57cec5SDimitry Andric const SmallVectorImpl<SlotIndex> &Kills, 7735ffd83dbSDimitry Andric SmallVectorImpl<std::pair<SlotIndex, DbgVariableValue>> &NewDefs, 7740b57cec5SDimitry Andric MachineRegisterInfo &MRI, LiveIntervals &LIS) { 7750b57cec5SDimitry Andric if (Kills.empty()) 7760b57cec5SDimitry Andric return; 7770b57cec5SDimitry Andric // Don't track copies from physregs, there are too many uses. 7788bcb0991SDimitry Andric if (!Register::isVirtualRegister(LI->reg)) 7790b57cec5SDimitry Andric return; 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric // Collect all the (vreg, valno) pairs that are copies of LI. 7820b57cec5SDimitry Andric SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; 7830b57cec5SDimitry Andric for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) { 7840b57cec5SDimitry Andric MachineInstr *MI = MO.getParent(); 7850b57cec5SDimitry Andric // Copies of the full value. 7860b57cec5SDimitry Andric if (MO.getSubReg() || !MI->isCopy()) 7870b57cec5SDimitry Andric continue; 7888bcb0991SDimitry Andric Register DstReg = MI->getOperand(0).getReg(); 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric // Don't follow copies to physregs. These are usually setting up call 7910b57cec5SDimitry Andric // arguments, and the argument registers are always call clobbered. We are 7920b57cec5SDimitry Andric // better off in the source register which could be a callee-saved register, 7930b57cec5SDimitry Andric // or it could be spilled. 7948bcb0991SDimitry Andric if (!Register::isVirtualRegister(DstReg)) 7950b57cec5SDimitry Andric continue; 7960b57cec5SDimitry Andric 7975ffd83dbSDimitry Andric // Is the value extended to reach this copy? If not, another def may be 7985ffd83dbSDimitry Andric // blocking it, or we are looking at a wrong value of LI. 7990b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI); 8000b57cec5SDimitry Andric LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); 8015ffd83dbSDimitry Andric if (!I.valid() || I.value() != DbgValue) 8020b57cec5SDimitry Andric continue; 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric if (!LIS.hasInterval(DstReg)) 8050b57cec5SDimitry Andric continue; 8060b57cec5SDimitry Andric LiveInterval *DstLI = &LIS.getInterval(DstReg); 8070b57cec5SDimitry Andric const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); 8080b57cec5SDimitry Andric assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); 8090b57cec5SDimitry Andric CopyValues.push_back(std::make_pair(DstLI, DstVNI)); 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric 8120b57cec5SDimitry Andric if (CopyValues.empty()) 8130b57cec5SDimitry Andric return; 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI 8160b57cec5SDimitry Andric << '\n'); 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric // Try to add defs of the copied values for each kill point. 8190b57cec5SDimitry Andric for (unsigned i = 0, e = Kills.size(); i != e; ++i) { 8200b57cec5SDimitry Andric SlotIndex Idx = Kills[i]; 8210b57cec5SDimitry Andric for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { 8220b57cec5SDimitry Andric LiveInterval *DstLI = CopyValues[j].first; 8230b57cec5SDimitry Andric const VNInfo *DstVNI = CopyValues[j].second; 8240b57cec5SDimitry Andric if (DstLI->getVNInfoAt(Idx) != DstVNI) 8250b57cec5SDimitry Andric continue; 8260b57cec5SDimitry Andric // Check that there isn't already a def at Idx 8270b57cec5SDimitry Andric LocMap::iterator I = locInts.find(Idx); 8280b57cec5SDimitry Andric if (I.valid() && I.start() <= Idx) 8290b57cec5SDimitry Andric continue; 8300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" 8310b57cec5SDimitry Andric << DstVNI->id << " in " << *DstLI << '\n'); 8320b57cec5SDimitry Andric MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); 8330b57cec5SDimitry Andric assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); 8340b57cec5SDimitry Andric unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); 8355ffd83dbSDimitry Andric DbgVariableValue NewValue = DbgValue.changeLocNo(LocNo); 8365ffd83dbSDimitry Andric I.insert(Idx, Idx.getNextSlot(), NewValue); 8375ffd83dbSDimitry Andric NewDefs.push_back(std::make_pair(Idx, NewValue)); 8380b57cec5SDimitry Andric break; 8390b57cec5SDimitry Andric } 8400b57cec5SDimitry Andric } 8410b57cec5SDimitry Andric } 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric void UserValue::computeIntervals(MachineRegisterInfo &MRI, 8440b57cec5SDimitry Andric const TargetRegisterInfo &TRI, 8450b57cec5SDimitry Andric LiveIntervals &LIS, LexicalScopes &LS) { 8465ffd83dbSDimitry Andric SmallVector<std::pair<SlotIndex, DbgVariableValue>, 16> Defs; 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric // Collect all defs to be extended (Skipping undefs). 8490b57cec5SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) 8500b57cec5SDimitry Andric if (!I.value().isUndef()) 8510b57cec5SDimitry Andric Defs.push_back(std::make_pair(I.start(), I.value())); 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric // Extend all defs, and possibly add new ones along the way. 8540b57cec5SDimitry Andric for (unsigned i = 0; i != Defs.size(); ++i) { 8550b57cec5SDimitry Andric SlotIndex Idx = Defs[i].first; 8565ffd83dbSDimitry Andric DbgVariableValue DbgValue = Defs[i].second; 8575ffd83dbSDimitry Andric const MachineOperand &LocMO = locations[DbgValue.getLocNo()]; 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric if (!LocMO.isReg()) { 8605ffd83dbSDimitry Andric extendDef(Idx, DbgValue, nullptr, nullptr, nullptr, LIS); 8610b57cec5SDimitry Andric continue; 8620b57cec5SDimitry Andric } 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric // Register locations are constrained to where the register value is live. 8658bcb0991SDimitry Andric if (Register::isVirtualRegister(LocMO.getReg())) { 8660b57cec5SDimitry Andric LiveInterval *LI = nullptr; 8670b57cec5SDimitry Andric const VNInfo *VNI = nullptr; 8680b57cec5SDimitry Andric if (LIS.hasInterval(LocMO.getReg())) { 8690b57cec5SDimitry Andric LI = &LIS.getInterval(LocMO.getReg()); 8700b57cec5SDimitry Andric VNI = LI->getVNInfoAt(Idx); 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric SmallVector<SlotIndex, 16> Kills; 8735ffd83dbSDimitry Andric extendDef(Idx, DbgValue, LI, VNI, &Kills, LIS); 8740b57cec5SDimitry Andric // FIXME: Handle sub-registers in addDefsFromCopies. The problem is that 8750b57cec5SDimitry Andric // if the original location for example is %vreg0:sub_hi, and we find a 8760b57cec5SDimitry Andric // full register copy in addDefsFromCopies (at the moment it only handles 8770b57cec5SDimitry Andric // full register copies), then we must add the sub1 sub-register index to 8780b57cec5SDimitry Andric // the new location. However, that is only possible if the new virtual 8790b57cec5SDimitry Andric // register is of the same regclass (or if there is an equivalent 8800b57cec5SDimitry Andric // sub-register in that regclass). For now, simply skip handling copies if 8810b57cec5SDimitry Andric // a sub-register is involved. 8820b57cec5SDimitry Andric if (LI && !LocMO.getSubReg()) 8835ffd83dbSDimitry Andric addDefsFromCopies(LI, DbgValue, Kills, Defs, MRI, LIS); 8840b57cec5SDimitry Andric continue; 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric // For physregs, we only mark the start slot idx. DwarfDebug will see it 8880b57cec5SDimitry Andric // as if the DBG_VALUE is valid up until the end of the basic block, or 8890b57cec5SDimitry Andric // the next def of the physical register. So we do not need to extend the 8900b57cec5SDimitry Andric // range. It might actually happen that the DBG_VALUE is the last use of 8910b57cec5SDimitry Andric // the physical register (e.g. if this is an unused input argument to a 8920b57cec5SDimitry Andric // function). 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric // The computed intervals may extend beyond the range of the debug 8960b57cec5SDimitry Andric // location's lexical scope. In this case, splitting of an interval 8970b57cec5SDimitry Andric // can result in an interval outside of the scope being created, 8980b57cec5SDimitry Andric // causing extra unnecessary DBG_VALUEs to be emitted. To prevent 8990b57cec5SDimitry Andric // this, trim the intervals to the lexical scope. 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric LexicalScope *Scope = LS.findLexicalScope(dl); 9020b57cec5SDimitry Andric if (!Scope) 9030b57cec5SDimitry Andric return; 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric SlotIndex PrevEnd; 9060b57cec5SDimitry Andric LocMap::iterator I = locInts.begin(); 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric // Iterate over the lexical scope ranges. Each time round the loop 9090b57cec5SDimitry Andric // we check the intervals for overlap with the end of the previous 9100b57cec5SDimitry Andric // range and the start of the next. The first range is handled as 9110b57cec5SDimitry Andric // a special case where there is no PrevEnd. 9120b57cec5SDimitry Andric for (const InsnRange &Range : Scope->getRanges()) { 9130b57cec5SDimitry Andric SlotIndex RStart = LIS.getInstructionIndex(*Range.first); 9140b57cec5SDimitry Andric SlotIndex REnd = LIS.getInstructionIndex(*Range.second); 9150b57cec5SDimitry Andric 91613138422SDimitry Andric // Variable locations at the first instruction of a block should be 91713138422SDimitry Andric // based on the block's SlotIndex, not the first instruction's index. 91813138422SDimitry Andric if (Range.first == Range.first->getParent()->begin()) 91913138422SDimitry Andric RStart = LIS.getSlotIndexes()->getIndexBefore(*Range.first); 92013138422SDimitry Andric 9210b57cec5SDimitry Andric // At the start of each iteration I has been advanced so that 9220b57cec5SDimitry Andric // I.stop() >= PrevEnd. Check for overlap. 9230b57cec5SDimitry Andric if (PrevEnd && I.start() < PrevEnd) { 9240b57cec5SDimitry Andric SlotIndex IStop = I.stop(); 9255ffd83dbSDimitry Andric DbgVariableValue DbgValue = I.value(); 9260b57cec5SDimitry Andric 9270b57cec5SDimitry Andric // Stop overlaps previous end - trim the end of the interval to 9280b57cec5SDimitry Andric // the scope range. 9290b57cec5SDimitry Andric I.setStopUnchecked(PrevEnd); 9300b57cec5SDimitry Andric ++I; 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric // If the interval also overlaps the start of the "next" (i.e. 93313138422SDimitry Andric // current) range create a new interval for the remainder (which 93413138422SDimitry Andric // may be further trimmed). 9350b57cec5SDimitry Andric if (RStart < IStop) 9365ffd83dbSDimitry Andric I.insert(RStart, IStop, DbgValue); 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // Advance I so that I.stop() >= RStart, and check for overlap. 9400b57cec5SDimitry Andric I.advanceTo(RStart); 9410b57cec5SDimitry Andric if (!I.valid()) 9420b57cec5SDimitry Andric return; 9430b57cec5SDimitry Andric 94413138422SDimitry Andric if (I.start() < RStart) { 94513138422SDimitry Andric // Interval start overlaps range - trim to the scope range. 94613138422SDimitry Andric I.setStartUnchecked(RStart); 94713138422SDimitry Andric // Remember that this interval was trimmed. 94813138422SDimitry Andric trimmedDefs.insert(RStart); 94913138422SDimitry Andric } 95013138422SDimitry Andric 9510b57cec5SDimitry Andric // The end of a lexical scope range is the last instruction in the 9520b57cec5SDimitry Andric // range. To convert to an interval we need the index of the 9530b57cec5SDimitry Andric // instruction after it. 9540b57cec5SDimitry Andric REnd = REnd.getNextIndex(); 9550b57cec5SDimitry Andric 9560b57cec5SDimitry Andric // Advance I to first interval outside current range. 9570b57cec5SDimitry Andric I.advanceTo(REnd); 9580b57cec5SDimitry Andric if (!I.valid()) 9590b57cec5SDimitry Andric return; 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric PrevEnd = REnd; 9620b57cec5SDimitry Andric } 9630b57cec5SDimitry Andric 9640b57cec5SDimitry Andric // Check for overlap with end of final range. 9650b57cec5SDimitry Andric if (PrevEnd && I.start() < PrevEnd) 9660b57cec5SDimitry Andric I.setStopUnchecked(PrevEnd); 9670b57cec5SDimitry Andric } 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric void LDVImpl::computeIntervals() { 9700b57cec5SDimitry Andric LexicalScopes LS; 9710b57cec5SDimitry Andric LS.initialize(*MF); 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric for (unsigned i = 0, e = userValues.size(); i != e; ++i) { 9740b57cec5SDimitry Andric userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS); 9750b57cec5SDimitry Andric userValues[i]->mapVirtRegs(this); 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric } 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { 9800b57cec5SDimitry Andric clear(); 9810b57cec5SDimitry Andric MF = &mf; 9820b57cec5SDimitry Andric LIS = &pass.getAnalysis<LiveIntervals>(); 9830b57cec5SDimitry Andric TRI = mf.getSubtarget().getRegisterInfo(); 9840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " 9850b57cec5SDimitry Andric << mf.getName() << " **********\n"); 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric bool Changed = collectDebugValues(mf); 9880b57cec5SDimitry Andric computeIntervals(); 9890b57cec5SDimitry Andric LLVM_DEBUG(print(dbgs())); 9900b57cec5SDimitry Andric ModifiedMF = Changed; 9910b57cec5SDimitry Andric return Changed; 9920b57cec5SDimitry Andric } 9930b57cec5SDimitry Andric 9940b57cec5SDimitry Andric static void removeDebugValues(MachineFunction &mf) { 9950b57cec5SDimitry Andric for (MachineBasicBlock &MBB : mf) { 9960b57cec5SDimitry Andric for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) { 9970b57cec5SDimitry Andric if (!MBBI->isDebugValue()) { 9980b57cec5SDimitry Andric ++MBBI; 9990b57cec5SDimitry Andric continue; 10000b57cec5SDimitry Andric } 10010b57cec5SDimitry Andric MBBI = MBB.erase(MBBI); 10020b57cec5SDimitry Andric } 10030b57cec5SDimitry Andric } 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { 10070b57cec5SDimitry Andric if (!EnableLDV) 10080b57cec5SDimitry Andric return false; 10090b57cec5SDimitry Andric if (!mf.getFunction().getSubprogram()) { 10100b57cec5SDimitry Andric removeDebugValues(mf); 10110b57cec5SDimitry Andric return false; 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric if (!pImpl) 10140b57cec5SDimitry Andric pImpl = new LDVImpl(this); 10150b57cec5SDimitry Andric return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric void LiveDebugVariables::releaseMemory() { 10190b57cec5SDimitry Andric if (pImpl) 10200b57cec5SDimitry Andric static_cast<LDVImpl*>(pImpl)->clear(); 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric LiveDebugVariables::~LiveDebugVariables() { 10240b57cec5SDimitry Andric if (pImpl) 10250b57cec5SDimitry Andric delete static_cast<LDVImpl*>(pImpl); 10260b57cec5SDimitry Andric } 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 10290b57cec5SDimitry Andric // Live Range Splitting 10300b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric bool 10335ffd83dbSDimitry Andric UserValue::splitLocation(unsigned OldLocNo, ArrayRef<Register> NewRegs, 10340b57cec5SDimitry Andric LiveIntervals& LIS) { 10350b57cec5SDimitry Andric LLVM_DEBUG({ 10360b57cec5SDimitry Andric dbgs() << "Splitting Loc" << OldLocNo << '\t'; 10370b57cec5SDimitry Andric print(dbgs(), nullptr); 10380b57cec5SDimitry Andric }); 10390b57cec5SDimitry Andric bool DidChange = false; 10400b57cec5SDimitry Andric LocMap::iterator LocMapI; 10410b57cec5SDimitry Andric LocMapI.setMap(locInts); 10420b57cec5SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) { 10430b57cec5SDimitry Andric LiveInterval *LI = &LIS.getInterval(NewRegs[i]); 10440b57cec5SDimitry Andric if (LI->empty()) 10450b57cec5SDimitry Andric continue; 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric // Don't allocate the new LocNo until it is needed. 10480b57cec5SDimitry Andric unsigned NewLocNo = UndefLocNo; 10490b57cec5SDimitry Andric 10500b57cec5SDimitry Andric // Iterate over the overlaps between locInts and LI. 10510b57cec5SDimitry Andric LocMapI.find(LI->beginIndex()); 10520b57cec5SDimitry Andric if (!LocMapI.valid()) 10530b57cec5SDimitry Andric continue; 10540b57cec5SDimitry Andric LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); 10550b57cec5SDimitry Andric LiveInterval::iterator LIE = LI->end(); 10560b57cec5SDimitry Andric while (LocMapI.valid() && LII != LIE) { 10570b57cec5SDimitry Andric // At this point, we know that LocMapI.stop() > LII->start. 10580b57cec5SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 10590b57cec5SDimitry Andric if (LII == LIE) 10600b57cec5SDimitry Andric break; 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric // Now LII->end > LocMapI.start(). Do we have an overlap? 10635ffd83dbSDimitry Andric if (LocMapI.value().getLocNo() == OldLocNo && 10645ffd83dbSDimitry Andric LII->start < LocMapI.stop()) { 10650b57cec5SDimitry Andric // Overlapping correct location. Allocate NewLocNo now. 10660b57cec5SDimitry Andric if (NewLocNo == UndefLocNo) { 10670b57cec5SDimitry Andric MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); 10680b57cec5SDimitry Andric MO.setSubReg(locations[OldLocNo].getSubReg()); 10690b57cec5SDimitry Andric NewLocNo = getLocationNo(MO); 10700b57cec5SDimitry Andric DidChange = true; 10710b57cec5SDimitry Andric } 10720b57cec5SDimitry Andric 10730b57cec5SDimitry Andric SlotIndex LStart = LocMapI.start(); 10740b57cec5SDimitry Andric SlotIndex LStop = LocMapI.stop(); 10755ffd83dbSDimitry Andric DbgVariableValue OldDbgValue = LocMapI.value(); 10760b57cec5SDimitry Andric 10770b57cec5SDimitry Andric // Trim LocMapI down to the LII overlap. 10780b57cec5SDimitry Andric if (LStart < LII->start) 10790b57cec5SDimitry Andric LocMapI.setStartUnchecked(LII->start); 10800b57cec5SDimitry Andric if (LStop > LII->end) 10810b57cec5SDimitry Andric LocMapI.setStopUnchecked(LII->end); 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric // Change the value in the overlap. This may trigger coalescing. 10845ffd83dbSDimitry Andric LocMapI.setValue(OldDbgValue.changeLocNo(NewLocNo)); 10850b57cec5SDimitry Andric 10865ffd83dbSDimitry Andric // Re-insert any removed OldDbgValue ranges. 10870b57cec5SDimitry Andric if (LStart < LocMapI.start()) { 10885ffd83dbSDimitry Andric LocMapI.insert(LStart, LocMapI.start(), OldDbgValue); 10890b57cec5SDimitry Andric ++LocMapI; 10900b57cec5SDimitry Andric assert(LocMapI.valid() && "Unexpected coalescing"); 10910b57cec5SDimitry Andric } 10920b57cec5SDimitry Andric if (LStop > LocMapI.stop()) { 10930b57cec5SDimitry Andric ++LocMapI; 10945ffd83dbSDimitry Andric LocMapI.insert(LII->end, LStop, OldDbgValue); 10950b57cec5SDimitry Andric --LocMapI; 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric // Advance to the next overlap. 11000b57cec5SDimitry Andric if (LII->end < LocMapI.stop()) { 11010b57cec5SDimitry Andric if (++LII == LIE) 11020b57cec5SDimitry Andric break; 11030b57cec5SDimitry Andric LocMapI.advanceTo(LII->start); 11040b57cec5SDimitry Andric } else { 11050b57cec5SDimitry Andric ++LocMapI; 11060b57cec5SDimitry Andric if (!LocMapI.valid()) 11070b57cec5SDimitry Andric break; 11080b57cec5SDimitry Andric LII = LI->advanceTo(LII, LocMapI.start()); 11090b57cec5SDimitry Andric } 11100b57cec5SDimitry Andric } 11110b57cec5SDimitry Andric } 11120b57cec5SDimitry Andric 1113480093f4SDimitry Andric // Finally, remove OldLocNo unless it is still used by some interval in the 1114480093f4SDimitry Andric // locInts map. One case when OldLocNo still is in use is when the register 1115480093f4SDimitry Andric // has been spilled. In such situations the spilled register is kept as a 1116480093f4SDimitry Andric // location until rewriteLocations is called (VirtRegMap is mapping the old 1117480093f4SDimitry Andric // register to the spill slot). So for a while we can have locations that map 1118480093f4SDimitry Andric // to virtual registers that have been removed from both the MachineFunction 1119480093f4SDimitry Andric // and from LiveIntervals. 11205ffd83dbSDimitry Andric // 11215ffd83dbSDimitry Andric // We may also just be using the location for a value with a different 11225ffd83dbSDimitry Andric // expression. 1123480093f4SDimitry Andric removeLocationIfUnused(OldLocNo); 11240b57cec5SDimitry Andric 11250b57cec5SDimitry Andric LLVM_DEBUG({ 11260b57cec5SDimitry Andric dbgs() << "Split result: \t"; 11270b57cec5SDimitry Andric print(dbgs(), nullptr); 11280b57cec5SDimitry Andric }); 11290b57cec5SDimitry Andric return DidChange; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric bool 11335ffd83dbSDimitry Andric UserValue::splitRegister(Register OldReg, ArrayRef<Register> NewRegs, 11340b57cec5SDimitry Andric LiveIntervals &LIS) { 11350b57cec5SDimitry Andric bool DidChange = false; 11360b57cec5SDimitry Andric // Split locations referring to OldReg. Iterate backwards so splitLocation can 11370b57cec5SDimitry Andric // safely erase unused locations. 11380b57cec5SDimitry Andric for (unsigned i = locations.size(); i ; --i) { 11390b57cec5SDimitry Andric unsigned LocNo = i-1; 11400b57cec5SDimitry Andric const MachineOperand *Loc = &locations[LocNo]; 11410b57cec5SDimitry Andric if (!Loc->isReg() || Loc->getReg() != OldReg) 11420b57cec5SDimitry Andric continue; 11430b57cec5SDimitry Andric DidChange |= splitLocation(LocNo, NewRegs, LIS); 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric return DidChange; 11460b57cec5SDimitry Andric } 11470b57cec5SDimitry Andric 11485ffd83dbSDimitry Andric void LDVImpl::splitRegister(Register OldReg, ArrayRef<Register> NewRegs) { 11490b57cec5SDimitry Andric bool DidChange = false; 1150480093f4SDimitry Andric for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) 11510b57cec5SDimitry Andric DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS); 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric if (!DidChange) 11540b57cec5SDimitry Andric return; 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric // Map all of the new virtual registers. 1157480093f4SDimitry Andric UserValue *UV = lookupVirtReg(OldReg); 11580b57cec5SDimitry Andric for (unsigned i = 0; i != NewRegs.size(); ++i) 11590b57cec5SDimitry Andric mapVirtReg(NewRegs[i], UV); 11600b57cec5SDimitry Andric } 11610b57cec5SDimitry Andric 11620b57cec5SDimitry Andric void LiveDebugVariables:: 11635ffd83dbSDimitry Andric splitRegister(Register OldReg, ArrayRef<Register> NewRegs, LiveIntervals &LIS) { 11640b57cec5SDimitry Andric if (pImpl) 11650b57cec5SDimitry Andric static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric void UserValue::rewriteLocations(VirtRegMap &VRM, const MachineFunction &MF, 11690b57cec5SDimitry Andric const TargetInstrInfo &TII, 11700b57cec5SDimitry Andric const TargetRegisterInfo &TRI, 11710b57cec5SDimitry Andric SpillOffsetMap &SpillOffsets) { 11720b57cec5SDimitry Andric // Build a set of new locations with new numbers so we can coalesce our 11730b57cec5SDimitry Andric // IntervalMap if two vreg intervals collapse to the same physical location. 11740b57cec5SDimitry Andric // Use MapVector instead of SetVector because MapVector::insert returns the 11750b57cec5SDimitry Andric // position of the previously or newly inserted element. The boolean value 11760b57cec5SDimitry Andric // tracks if the location was produced by a spill. 11770b57cec5SDimitry Andric // FIXME: This will be problematic if we ever support direct and indirect 11780b57cec5SDimitry Andric // frame index locations, i.e. expressing both variables in memory and 11790b57cec5SDimitry Andric // 'int x, *px = &x'. The "spilled" bit must become part of the location. 11800b57cec5SDimitry Andric MapVector<MachineOperand, std::pair<bool, unsigned>> NewLocations; 11810b57cec5SDimitry Andric SmallVector<unsigned, 4> LocNoMap(locations.size()); 11820b57cec5SDimitry Andric for (unsigned I = 0, E = locations.size(); I != E; ++I) { 11830b57cec5SDimitry Andric bool Spilled = false; 11840b57cec5SDimitry Andric unsigned SpillOffset = 0; 11850b57cec5SDimitry Andric MachineOperand Loc = locations[I]; 11860b57cec5SDimitry Andric // Only virtual registers are rewritten. 11870b57cec5SDimitry Andric if (Loc.isReg() && Loc.getReg() && 11888bcb0991SDimitry Andric Register::isVirtualRegister(Loc.getReg())) { 11898bcb0991SDimitry Andric Register VirtReg = Loc.getReg(); 11900b57cec5SDimitry Andric if (VRM.isAssignedReg(VirtReg) && 11918bcb0991SDimitry Andric Register::isPhysicalRegister(VRM.getPhys(VirtReg))) { 11920b57cec5SDimitry Andric // This can create a %noreg operand in rare cases when the sub-register 11930b57cec5SDimitry Andric // index is no longer available. That means the user value is in a 11940b57cec5SDimitry Andric // non-existent sub-register, and %noreg is exactly what we want. 11950b57cec5SDimitry Andric Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); 11960b57cec5SDimitry Andric } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { 11970b57cec5SDimitry Andric // Retrieve the stack slot offset. 11980b57cec5SDimitry Andric unsigned SpillSize; 11990b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 12000b57cec5SDimitry Andric const TargetRegisterClass *TRC = MRI.getRegClass(VirtReg); 12010b57cec5SDimitry Andric bool Success = TII.getStackSlotRange(TRC, Loc.getSubReg(), SpillSize, 12020b57cec5SDimitry Andric SpillOffset, MF); 12030b57cec5SDimitry Andric 12040b57cec5SDimitry Andric // FIXME: Invalidate the location if the offset couldn't be calculated. 12050b57cec5SDimitry Andric (void)Success; 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); 12080b57cec5SDimitry Andric Spilled = true; 12090b57cec5SDimitry Andric } else { 12100b57cec5SDimitry Andric Loc.setReg(0); 12110b57cec5SDimitry Andric Loc.setSubReg(0); 12120b57cec5SDimitry Andric } 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric // Insert this location if it doesn't already exist and record a mapping 12160b57cec5SDimitry Andric // from the old number to the new number. 12170b57cec5SDimitry Andric auto InsertResult = NewLocations.insert({Loc, {Spilled, SpillOffset}}); 12180b57cec5SDimitry Andric unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first); 12190b57cec5SDimitry Andric LocNoMap[I] = NewLocNo; 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric // Rewrite the locations and record the stack slot offsets for spills. 12230b57cec5SDimitry Andric locations.clear(); 12240b57cec5SDimitry Andric SpillOffsets.clear(); 12250b57cec5SDimitry Andric for (auto &Pair : NewLocations) { 12260b57cec5SDimitry Andric bool Spilled; 12270b57cec5SDimitry Andric unsigned SpillOffset; 12280b57cec5SDimitry Andric std::tie(Spilled, SpillOffset) = Pair.second; 12290b57cec5SDimitry Andric locations.push_back(Pair.first); 12300b57cec5SDimitry Andric if (Spilled) { 12310b57cec5SDimitry Andric unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair); 12320b57cec5SDimitry Andric SpillOffsets[NewLocNo] = SpillOffset; 12330b57cec5SDimitry Andric } 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric // Update the interval map, but only coalesce left, since intervals to the 12370b57cec5SDimitry Andric // right use the old location numbers. This should merge two contiguous 12380b57cec5SDimitry Andric // DBG_VALUE intervals with different vregs that were allocated to the same 12390b57cec5SDimitry Andric // physical register. 12400b57cec5SDimitry Andric for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { 12415ffd83dbSDimitry Andric DbgVariableValue DbgValue = I.value(); 12420b57cec5SDimitry Andric // Undef values don't exist in locations (and thus not in LocNoMap either) 12430b57cec5SDimitry Andric // so skip over them. See getLocationNo(). 12445ffd83dbSDimitry Andric if (DbgValue.isUndef()) 12450b57cec5SDimitry Andric continue; 12465ffd83dbSDimitry Andric unsigned NewLocNo = LocNoMap[DbgValue.getLocNo()]; 12475ffd83dbSDimitry Andric I.setValueUnchecked(DbgValue.changeLocNo(NewLocNo)); 12480b57cec5SDimitry Andric I.setStart(I.start()); 12490b57cec5SDimitry Andric } 12500b57cec5SDimitry Andric } 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric /// Find an iterator for inserting a DBG_VALUE instruction. 12530b57cec5SDimitry Andric static MachineBasicBlock::iterator 12540b57cec5SDimitry Andric findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, 12550b57cec5SDimitry Andric LiveIntervals &LIS) { 12560b57cec5SDimitry Andric SlotIndex Start = LIS.getMBBStartIdx(MBB); 12570b57cec5SDimitry Andric Idx = Idx.getBaseIndex(); 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric // Try to find an insert location by going backwards from Idx. 12600b57cec5SDimitry Andric MachineInstr *MI; 12610b57cec5SDimitry Andric while (!(MI = LIS.getInstructionFromIndex(Idx))) { 12620b57cec5SDimitry Andric // We've reached the beginning of MBB. 12630b57cec5SDimitry Andric if (Idx == Start) { 12640b57cec5SDimitry Andric MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin()); 12650b57cec5SDimitry Andric return I; 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric Idx = Idx.getPrevIndex(); 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric 12700b57cec5SDimitry Andric // Don't insert anything after the first terminator, though. 12710b57cec5SDimitry Andric return MI->isTerminator() ? MBB->getFirstTerminator() : 12720b57cec5SDimitry Andric std::next(MachineBasicBlock::iterator(MI)); 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric 12750b57cec5SDimitry Andric /// Find an iterator for inserting the next DBG_VALUE instruction 12760b57cec5SDimitry Andric /// (or end if no more insert locations found). 12770b57cec5SDimitry Andric static MachineBasicBlock::iterator 12780b57cec5SDimitry Andric findNextInsertLocation(MachineBasicBlock *MBB, 12790b57cec5SDimitry Andric MachineBasicBlock::iterator I, 12800b57cec5SDimitry Andric SlotIndex StopIdx, MachineOperand &LocMO, 12810b57cec5SDimitry Andric LiveIntervals &LIS, 12820b57cec5SDimitry Andric const TargetRegisterInfo &TRI) { 12830b57cec5SDimitry Andric if (!LocMO.isReg()) 12840b57cec5SDimitry Andric return MBB->instr_end(); 12858bcb0991SDimitry Andric Register Reg = LocMO.getReg(); 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric // Find the next instruction in the MBB that define the register Reg. 12880b57cec5SDimitry Andric while (I != MBB->end() && !I->isTerminator()) { 12890b57cec5SDimitry Andric if (!LIS.isNotInMIMap(*I) && 12900b57cec5SDimitry Andric SlotIndex::isEarlierEqualInstr(StopIdx, LIS.getInstructionIndex(*I))) 12910b57cec5SDimitry Andric break; 12920b57cec5SDimitry Andric if (I->definesRegister(Reg, &TRI)) 12930b57cec5SDimitry Andric // The insert location is directly after the instruction/bundle. 12940b57cec5SDimitry Andric return std::next(I); 12950b57cec5SDimitry Andric ++I; 12960b57cec5SDimitry Andric } 12970b57cec5SDimitry Andric return MBB->end(); 12980b57cec5SDimitry Andric } 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx, 13015ffd83dbSDimitry Andric SlotIndex StopIdx, DbgVariableValue DbgValue, 13020b57cec5SDimitry Andric bool Spilled, unsigned SpillOffset, 13030b57cec5SDimitry Andric LiveIntervals &LIS, const TargetInstrInfo &TII, 13040b57cec5SDimitry Andric const TargetRegisterInfo &TRI) { 13050b57cec5SDimitry Andric SlotIndex MBBEndIdx = LIS.getMBBEndIdx(&*MBB); 13060b57cec5SDimitry Andric // Only search within the current MBB. 13070b57cec5SDimitry Andric StopIdx = (MBBEndIdx < StopIdx) ? MBBEndIdx : StopIdx; 13080b57cec5SDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, StartIdx, LIS); 13090b57cec5SDimitry Andric // Undef values don't exist in locations so create new "noreg" register MOs 13100b57cec5SDimitry Andric // for them. See getLocationNo(). 13115ffd83dbSDimitry Andric MachineOperand MO = 13125ffd83dbSDimitry Andric !DbgValue.isUndef() 13135ffd83dbSDimitry Andric ? locations[DbgValue.getLocNo()] 13145ffd83dbSDimitry Andric : MachineOperand::CreateReg( 13155ffd83dbSDimitry Andric /* Reg */ 0, /* isDef */ false, /* isImp */ false, 13160b57cec5SDimitry Andric /* isKill */ false, /* isDead */ false, 13170b57cec5SDimitry Andric /* isUndef */ false, /* isEarlyClobber */ false, 13180b57cec5SDimitry Andric /* SubReg */ 0, /* isDebug */ true); 13190b57cec5SDimitry Andric 13200b57cec5SDimitry Andric ++NumInsertedDebugValues; 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric assert(cast<DILocalVariable>(Variable) 13230b57cec5SDimitry Andric ->isValidLocationForIntrinsic(getDebugLoc()) && 13240b57cec5SDimitry Andric "Expected inlined-at fields to agree"); 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric // If the location was spilled, the new DBG_VALUE will be indirect. If the 13270b57cec5SDimitry Andric // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate 13280b57cec5SDimitry Andric // that the original virtual register was a pointer. Also, add the stack slot 13290b57cec5SDimitry Andric // offset for the spilled register to the expression. 13305ffd83dbSDimitry Andric const DIExpression *Expr = DbgValue.getExpression(); 133113138422SDimitry Andric uint8_t DIExprFlags = DIExpression::ApplyOffset; 13325ffd83dbSDimitry Andric bool IsIndirect = DbgValue.getWasIndirect(); 133313138422SDimitry Andric if (Spilled) { 133413138422SDimitry Andric if (IsIndirect) 133513138422SDimitry Andric DIExprFlags |= DIExpression::DerefAfter; 133613138422SDimitry Andric Expr = 133713138422SDimitry Andric DIExpression::prepend(Expr, DIExprFlags, SpillOffset); 133813138422SDimitry Andric IsIndirect = true; 133913138422SDimitry Andric } 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index"); 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric do { 13440b57cec5SDimitry Andric BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE), 134513138422SDimitry Andric IsIndirect, MO, Variable, Expr); 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric // Continue and insert DBG_VALUES after every redefinition of register 13480b57cec5SDimitry Andric // associated with the debug value within the range 13490b57cec5SDimitry Andric I = findNextInsertLocation(MBB, I, StopIdx, MO, LIS, TRI); 13500b57cec5SDimitry Andric } while (I != MBB->end()); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric void UserLabel::insertDebugLabel(MachineBasicBlock *MBB, SlotIndex Idx, 13540b57cec5SDimitry Andric LiveIntervals &LIS, 13550b57cec5SDimitry Andric const TargetInstrInfo &TII) { 13560b57cec5SDimitry Andric MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); 13570b57cec5SDimitry Andric ++NumInsertedDebugLabels; 13580b57cec5SDimitry Andric BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_LABEL)) 13590b57cec5SDimitry Andric .addMetadata(Label); 13600b57cec5SDimitry Andric } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, 13630b57cec5SDimitry Andric const TargetInstrInfo &TII, 13640b57cec5SDimitry Andric const TargetRegisterInfo &TRI, 13650b57cec5SDimitry Andric const SpillOffsetMap &SpillOffsets) { 13660b57cec5SDimitry Andric MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric for (LocMap::const_iterator I = locInts.begin(); I.valid();) { 13690b57cec5SDimitry Andric SlotIndex Start = I.start(); 13700b57cec5SDimitry Andric SlotIndex Stop = I.stop(); 13715ffd83dbSDimitry Andric DbgVariableValue DbgValue = I.value(); 13725ffd83dbSDimitry Andric auto SpillIt = !DbgValue.isUndef() ? SpillOffsets.find(DbgValue.getLocNo()) 13735ffd83dbSDimitry Andric : SpillOffsets.end(); 13740b57cec5SDimitry Andric bool Spilled = SpillIt != SpillOffsets.end(); 13750b57cec5SDimitry Andric unsigned SpillOffset = Spilled ? SpillIt->second : 0; 13760b57cec5SDimitry Andric 137713138422SDimitry Andric // If the interval start was trimmed to the lexical scope insert the 137813138422SDimitry Andric // DBG_VALUE at the previous index (otherwise it appears after the 137913138422SDimitry Andric // first instruction in the range). 138013138422SDimitry Andric if (trimmedDefs.count(Start)) 138113138422SDimitry Andric Start = Start.getPrevIndex(); 138213138422SDimitry Andric 13835ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "\t[" << Start << ';' << Stop 13845ffd83dbSDimitry Andric << "):" << DbgValue.getLocNo()); 13850b57cec5SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); 13860b57cec5SDimitry Andric SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB); 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); 13895ffd83dbSDimitry Andric insertDebugValue(&*MBB, Start, Stop, DbgValue, Spilled, SpillOffset, LIS, 13905ffd83dbSDimitry Andric TII, TRI); 13910b57cec5SDimitry Andric // This interval may span multiple basic blocks. 13920b57cec5SDimitry Andric // Insert a DBG_VALUE into each one. 13930b57cec5SDimitry Andric while (Stop > MBBEnd) { 13940b57cec5SDimitry Andric // Move to the next block. 13950b57cec5SDimitry Andric Start = MBBEnd; 13960b57cec5SDimitry Andric if (++MBB == MFEnd) 13970b57cec5SDimitry Andric break; 13980b57cec5SDimitry Andric MBBEnd = LIS.getMBBEndIdx(&*MBB); 13990b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB) << '-' << MBBEnd); 14005ffd83dbSDimitry Andric insertDebugValue(&*MBB, Start, Stop, DbgValue, Spilled, SpillOffset, LIS, 14015ffd83dbSDimitry Andric TII, TRI); 14020b57cec5SDimitry Andric } 14030b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << '\n'); 14040b57cec5SDimitry Andric if (MBB == MFEnd) 14050b57cec5SDimitry Andric break; 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric ++I; 14080b57cec5SDimitry Andric } 14090b57cec5SDimitry Andric } 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric void UserLabel::emitDebugLabel(LiveIntervals &LIS, const TargetInstrInfo &TII) { 14120b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\t" << loc); 14130b57cec5SDimitry Andric MachineFunction::iterator MBB = LIS.getMBBFromIndex(loc)->getIterator(); 14140b57cec5SDimitry Andric 14150b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << ' ' << printMBBReference(*MBB)); 14160b57cec5SDimitry Andric insertDebugLabel(&*MBB, loc, LIS, TII); 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << '\n'); 14190b57cec5SDimitry Andric } 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andric void LDVImpl::emitDebugValues(VirtRegMap *VRM) { 14220b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); 14230b57cec5SDimitry Andric if (!MF) 14240b57cec5SDimitry Andric return; 14250b57cec5SDimitry Andric const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 14260b57cec5SDimitry Andric SpillOffsetMap SpillOffsets; 14270b57cec5SDimitry Andric for (auto &userValue : userValues) { 14280b57cec5SDimitry Andric LLVM_DEBUG(userValue->print(dbgs(), TRI)); 14290b57cec5SDimitry Andric userValue->rewriteLocations(*VRM, *MF, *TII, *TRI, SpillOffsets); 14300b57cec5SDimitry Andric userValue->emitDebugValues(VRM, *LIS, *TII, *TRI, SpillOffsets); 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** EMITTING LIVE DEBUG LABELS **********\n"); 14330b57cec5SDimitry Andric for (auto &userLabel : userLabels) { 14340b57cec5SDimitry Andric LLVM_DEBUG(userLabel->print(dbgs(), TRI)); 14350b57cec5SDimitry Andric userLabel->emitDebugLabel(*LIS, *TII); 14360b57cec5SDimitry Andric } 14370b57cec5SDimitry Andric EmitDone = true; 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { 14410b57cec5SDimitry Andric if (pImpl) 14420b57cec5SDimitry Andric static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); 14430b57cec5SDimitry Andric } 14440b57cec5SDimitry Andric 14450b57cec5SDimitry Andric bool LiveDebugVariables::doInitialization(Module &M) { 14460b57cec5SDimitry Andric return Pass::doInitialization(M); 14470b57cec5SDimitry Andric } 14480b57cec5SDimitry Andric 14490b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 14500b57cec5SDimitry Andric LLVM_DUMP_METHOD void LiveDebugVariables::dump() const { 14510b57cec5SDimitry Andric if (pImpl) 14520b57cec5SDimitry Andric static_cast<LDVImpl*>(pImpl)->print(dbgs()); 14530b57cec5SDimitry Andric } 14540b57cec5SDimitry Andric #endif 1455