| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 1 | //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===// |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// This pass implements a data flow analysis that propagates debug location |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 10 | /// information by inserting additional DBG_VALUE insts into the machine |
| 11 | /// instruction stream. Before running, each DBG_VALUE inst corresponds to a |
| 12 | /// source assignment of a variable. Afterwards, a DBG_VALUE inst specifies a |
| 13 | /// variable location for the current basic block (see SourceLevelDebugging.rst). |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 14 | /// |
| 15 | /// This is a separate pass from DbgValueHistoryCalculator to facilitate |
| 16 | /// testing and improve modularity. |
| 17 | /// |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 18 | /// Each variable location is represented by a VarLoc object that identifies the |
| 19 | /// source variable, its current machine-location, and the DBG_VALUE inst that |
| 20 | /// specifies the location. Each VarLoc is indexed in the (function-scope) |
| 21 | /// VarLocMap, giving each VarLoc a unique index. Rather than operate directly |
| 22 | /// on machine locations, the dataflow analysis in this pass identifies |
| 23 | /// locations by their index in the VarLocMap, meaning all the variable |
| 24 | /// locations in a block can be described by a sparse vector of VarLocMap |
| 25 | /// indexes. |
| 26 | /// |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/PostOrderIterator.h" |
| 31 | #include "llvm/ADT/SmallPtrSet.h" |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallSet.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallVector.h" |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SparseBitVector.h" |
| Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/UniqueVector.h" |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/LexicalScopes.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineFunction.h" |
| 41 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/MachineInstr.h" |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineMemOperand.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/MachineOperand.h" |
| 46 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/RegisterScavenging.h" |
| David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 48 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 49 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/TargetLowering.h" |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/TargetPassConfig.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 53 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 54 | #include "llvm/Config/llvm-config.h" |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 55 | #include "llvm/IR/DIBuilder.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 56 | #include "llvm/IR/DebugInfoMetadata.h" |
| 57 | #include "llvm/IR/DebugLoc.h" |
| 58 | #include "llvm/IR/Function.h" |
| 59 | #include "llvm/IR/Module.h" |
| Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 60 | #include "llvm/InitializePasses.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 61 | #include "llvm/MC/MCRegisterInfo.h" |
| 62 | #include "llvm/Pass.h" |
| 63 | #include "llvm/Support/Casting.h" |
| 64 | #include "llvm/Support/Compiler.h" |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 65 | #include "llvm/Support/Debug.h" |
| 66 | #include "llvm/Support/raw_ostream.h" |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 67 | #include <algorithm> |
| 68 | #include <cassert> |
| 69 | #include <cstdint> |
| 70 | #include <functional> |
| Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 71 | #include <queue> |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 72 | #include <tuple> |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 73 | #include <utility> |
| 74 | #include <vector> |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 75 | |
| 76 | using namespace llvm; |
| 77 | |
| Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 78 | #define DEBUG_TYPE "livedebugvalues" |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 79 | |
| 80 | STATISTIC(NumInserted, "Number of DBG_VALUE instructions inserted"); |
| Jeremy Morse | 0ae5498 | 2019-08-23 16:33:42 +0000 | [diff] [blame] | 81 | STATISTIC(NumRemoved, "Number of DBG_VALUE instructions removed"); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 82 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 83 | // If @MI is a DBG_VALUE with debug value described by a defined |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 84 | // register, returns the number of this register. In the other case, returns 0. |
| Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 85 | static Register isDbgValueDescribedByReg(const MachineInstr &MI) { |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 86 | assert(MI.isDebugValue() && "expected a DBG_VALUE"); |
| 87 | assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); |
| 88 | // If location of variable is described using a register (directly |
| 89 | // or indirectly), this register is always a first operand. |
| Matt Arsenault | e3a676e | 2019-06-24 15:50:29 +0000 | [diff] [blame] | 90 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : Register(); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 93 | /// If \p Op is a stack or frame register return true, otherwise return false. |
| 94 | /// This is used to avoid basing the debug entry values on the registers, since |
| 95 | /// we do not support it at the moment. |
| 96 | static bool isRegOtherThanSPAndFP(const MachineOperand &Op, |
| 97 | const MachineInstr &MI, |
| 98 | const TargetRegisterInfo *TRI) { |
| 99 | if (!Op.isReg()) |
| 100 | return false; |
| 101 | |
| 102 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 103 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
| 104 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
| 105 | Register FP = TRI->getFrameRegister(*MF); |
| 106 | Register Reg = Op.getReg(); |
| 107 | |
| 108 | return Reg && Reg != SP && Reg != FP; |
| 109 | } |
| 110 | |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 111 | namespace { |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 112 | |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 113 | using DefinedRegsSet = SmallSet<Register, 32>; |
| 114 | |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 115 | class LiveDebugValues : public MachineFunctionPass { |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 116 | private: |
| 117 | const TargetRegisterInfo *TRI; |
| 118 | const TargetInstrInfo *TII; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 119 | const TargetFrameLowering *TFI; |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 120 | BitVector CalleeSavedRegs; |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 121 | LexicalScopes LS; |
| 122 | |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 123 | enum struct TransferKind { TransferCopy, TransferSpill, TransferRestore }; |
| 124 | |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 125 | /// Keeps track of lexical scopes associated with a user value's source |
| 126 | /// location. |
| 127 | class UserValueScopes { |
| 128 | DebugLoc DL; |
| 129 | LexicalScopes &LS; |
| 130 | SmallPtrSet<const MachineBasicBlock *, 4> LBlocks; |
| 131 | |
| 132 | public: |
| 133 | UserValueScopes(DebugLoc D, LexicalScopes &L) : DL(std::move(D)), LS(L) {} |
| 134 | |
| 135 | /// Return true if current scope dominates at least one machine |
| 136 | /// instruction in a given machine basic block. |
| 137 | bool dominates(MachineBasicBlock *MBB) { |
| 138 | if (LBlocks.empty()) |
| 139 | LS.getMachineBasicBlocks(DL, LBlocks); |
| 140 | return LBlocks.count(MBB) != 0 || LS.dominates(DL, MBB); |
| 141 | } |
| 142 | }; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 143 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 144 | using FragmentInfo = DIExpression::FragmentInfo; |
| 145 | using OptFragmentInfo = Optional<DIExpression::FragmentInfo>; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 146 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 147 | /// A pair of debug variable and value location. |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 148 | struct VarLoc { |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 149 | // The location at which a spilled variable resides. It consists of a |
| 150 | // register and an offset. |
| 151 | struct SpillLoc { |
| 152 | unsigned SpillBase; |
| 153 | int SpillOffset; |
| 154 | bool operator==(const SpillLoc &Other) const { |
| 155 | return SpillBase == Other.SpillBase && SpillOffset == Other.SpillOffset; |
| 156 | } |
| 157 | }; |
| 158 | |
| Jeremy Morse | 337a7cb | 2019-09-04 11:09:05 +0000 | [diff] [blame] | 159 | /// Identity of the variable at this location. |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 160 | const DebugVariable Var; |
| Jeremy Morse | 337a7cb | 2019-09-04 11:09:05 +0000 | [diff] [blame] | 161 | |
| 162 | /// The expression applied to this location. |
| 163 | const DIExpression *Expr; |
| 164 | |
| 165 | /// DBG_VALUE to clone var/expr information from if this location |
| 166 | /// is moved. |
| 167 | const MachineInstr &MI; |
| 168 | |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 169 | mutable UserValueScopes UVS; |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 170 | enum VarLocKind { |
| 171 | InvalidKind = 0, |
| 172 | RegisterKind, |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 173 | SpillLocKind, |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 174 | ImmediateKind, |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 175 | EntryValueKind, |
| 176 | EntryValueBackupKind, |
| 177 | EntryValueCopyBackupKind |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 178 | } Kind = InvalidKind; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 179 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 180 | /// The value location. Stored separately to avoid repeatedly |
| 181 | /// extracting it from MI. |
| 182 | union { |
| Adrian Prantl | 359846f | 2017-07-28 23:25:51 +0000 | [diff] [blame] | 183 | uint64_t RegNo; |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 184 | SpillLoc SpillLocation; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 185 | uint64_t Hash; |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 186 | int64_t Immediate; |
| 187 | const ConstantFP *FPImm; |
| 188 | const ConstantInt *CImm; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 189 | } Loc; |
| 190 | |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 191 | VarLoc(const MachineInstr &MI, LexicalScopes &LS) |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 192 | : Var(MI.getDebugVariable(), MI.getDebugExpression(), |
| 193 | MI.getDebugLoc()->getInlinedAt()), |
| 194 | Expr(MI.getDebugExpression()), MI(MI), UVS(MI.getDebugLoc(), LS) { |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 195 | static_assert((sizeof(Loc) == sizeof(uint64_t)), |
| 196 | "hash does not cover all members of Loc"); |
| 197 | assert(MI.isDebugValue() && "not a DBG_VALUE"); |
| 198 | assert(MI.getNumOperands() == 4 && "malformed DBG_VALUE"); |
| Adrian Prantl | 0069873 | 2016-05-25 22:37:29 +0000 | [diff] [blame] | 199 | if (int RegNo = isDbgValueDescribedByReg(MI)) { |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 200 | Kind = RegisterKind; |
| Adrian Prantl | 359846f | 2017-07-28 23:25:51 +0000 | [diff] [blame] | 201 | Loc.RegNo = RegNo; |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 202 | } else if (MI.getOperand(0).isImm()) { |
| 203 | Kind = ImmediateKind; |
| 204 | Loc.Immediate = MI.getOperand(0).getImm(); |
| 205 | } else if (MI.getOperand(0).isFPImm()) { |
| 206 | Kind = ImmediateKind; |
| 207 | Loc.FPImm = MI.getOperand(0).getFPImm(); |
| 208 | } else if (MI.getOperand(0).isCImm()) { |
| 209 | Kind = ImmediateKind; |
| 210 | Loc.CImm = MI.getOperand(0).getCImm(); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 211 | } |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 212 | |
| 213 | // We create the debug entry values from the factory functions rather than |
| 214 | // from this ctor. |
| 215 | assert(Kind != EntryValueKind && !isEntryBackupLoc()); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 218 | /// Take the variable and machine-location in DBG_VALUE MI, and build an |
| 219 | /// entry location using the given expression. |
| 220 | static VarLoc CreateEntryLoc(const MachineInstr &MI, LexicalScopes &LS, |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 221 | const DIExpression *EntryExpr, unsigned Reg) { |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 222 | VarLoc VL(MI, LS); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 223 | assert(VL.Kind == RegisterKind); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 224 | VL.Kind = EntryValueKind; |
| 225 | VL.Expr = EntryExpr; |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 226 | VL.Loc.RegNo = Reg; |
| 227 | return VL; |
| 228 | } |
| 229 | |
| 230 | /// Take the variable and machine-location from the DBG_VALUE (from the |
| 231 | /// function entry), and build an entry value backup location. The backup |
| 232 | /// location will turn into the normal location if the backup is valid at |
| 233 | /// the time of the primary location clobbering. |
| 234 | static VarLoc CreateEntryBackupLoc(const MachineInstr &MI, |
| 235 | LexicalScopes &LS, |
| 236 | const DIExpression *EntryExpr) { |
| 237 | VarLoc VL(MI, LS); |
| 238 | assert(VL.Kind == RegisterKind); |
| 239 | VL.Kind = EntryValueBackupKind; |
| 240 | VL.Expr = EntryExpr; |
| 241 | return VL; |
| 242 | } |
| 243 | |
| 244 | /// Take the variable and machine-location from the DBG_VALUE (from the |
| 245 | /// function entry), and build a copy of an entry value backup location by |
| 246 | /// setting the register location to NewReg. |
| 247 | static VarLoc CreateEntryCopyBackupLoc(const MachineInstr &MI, |
| 248 | LexicalScopes &LS, |
| 249 | const DIExpression *EntryExpr, |
| 250 | unsigned NewReg) { |
| 251 | VarLoc VL(MI, LS); |
| 252 | assert(VL.Kind == RegisterKind); |
| 253 | VL.Kind = EntryValueCopyBackupKind; |
| 254 | VL.Expr = EntryExpr; |
| 255 | VL.Loc.RegNo = NewReg; |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 256 | return VL; |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 259 | /// Copy the register location in DBG_VALUE MI, updating the register to |
| 260 | /// be NewReg. |
| 261 | static VarLoc CreateCopyLoc(const MachineInstr &MI, LexicalScopes &LS, |
| 262 | unsigned NewReg) { |
| 263 | VarLoc VL(MI, LS); |
| 264 | assert(VL.Kind == RegisterKind); |
| 265 | VL.Loc.RegNo = NewReg; |
| 266 | return VL; |
| 267 | } |
| 268 | |
| 269 | /// Take the variable described by DBG_VALUE MI, and create a VarLoc |
| 270 | /// locating it in the specified spill location. |
| 271 | static VarLoc CreateSpillLoc(const MachineInstr &MI, unsigned SpillBase, |
| 272 | int SpillOffset, LexicalScopes &LS) { |
| 273 | VarLoc VL(MI, LS); |
| 274 | assert(VL.Kind == RegisterKind); |
| 275 | VL.Kind = SpillLocKind; |
| 276 | VL.Loc.SpillLocation = {SpillBase, SpillOffset}; |
| 277 | return VL; |
| 278 | } |
| 279 | |
| 280 | /// Create a DBG_VALUE representing this VarLoc in the given function. |
| 281 | /// Copies variable-specific information such as DILocalVariable and |
| 282 | /// inlining information from the original DBG_VALUE instruction, which may |
| 283 | /// have been several transfers ago. |
| 284 | MachineInstr *BuildDbgValue(MachineFunction &MF) const { |
| 285 | const DebugLoc &DbgLoc = MI.getDebugLoc(); |
| 286 | bool Indirect = MI.isIndirectDebugValue(); |
| 287 | const auto &IID = MI.getDesc(); |
| 288 | const DILocalVariable *Var = MI.getDebugVariable(); |
| 289 | const DIExpression *DIExpr = MI.getDebugExpression(); |
| 290 | |
| 291 | switch (Kind) { |
| 292 | case EntryValueKind: |
| 293 | // An entry value is a register location -- but with an updated |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 294 | // expression. The register location of such DBG_VALUE is always the one |
| 295 | // from the entry DBG_VALUE, it does not matter if the entry value was |
| 296 | // copied in to another register due to some optimizations. |
| 297 | return BuildMI(MF, DbgLoc, IID, Indirect, MI.getOperand(0).getReg(), |
| 298 | Var, Expr); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 299 | case RegisterKind: |
| 300 | // Register locations are like the source DBG_VALUE, but with the |
| 301 | // register number from this VarLoc. |
| 302 | return BuildMI(MF, DbgLoc, IID, Indirect, Loc.RegNo, Var, DIExpr); |
| 303 | case SpillLocKind: { |
| 304 | // Spills are indirect DBG_VALUEs, with a base register and offset. |
| 305 | // Use the original DBG_VALUEs expression to build the spilt location |
| 306 | // on top of. FIXME: spill locations created before this pass runs |
| 307 | // are not recognized, and not handled here. |
| 308 | auto *SpillExpr = DIExpression::prepend( |
| 309 | DIExpr, DIExpression::ApplyOffset, Loc.SpillLocation.SpillOffset); |
| 310 | unsigned Base = Loc.SpillLocation.SpillBase; |
| 311 | return BuildMI(MF, DbgLoc, IID, true, Base, Var, SpillExpr); |
| 312 | } |
| 313 | case ImmediateKind: { |
| 314 | MachineOperand MO = MI.getOperand(0); |
| 315 | return BuildMI(MF, DbgLoc, IID, Indirect, MO, Var, DIExpr); |
| 316 | } |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 317 | case EntryValueBackupKind: |
| 318 | case EntryValueCopyBackupKind: |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 319 | case InvalidKind: |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 320 | llvm_unreachable( |
| 321 | "Tried to produce DBG_VALUE for invalid or backup VarLoc"); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 322 | } |
| Simon Pilgrim | 84f5cd7 | 2019-10-04 12:45:27 +0000 | [diff] [blame] | 323 | llvm_unreachable("Unrecognized LiveDebugValues.VarLoc.Kind enum"); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// Is the Loc field a constant or constant object? |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 327 | bool isConstant() const { return Kind == ImmediateKind; } |
| 328 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 329 | /// Check if the Loc field is an entry backup location. |
| 330 | bool isEntryBackupLoc() const { |
| 331 | return Kind == EntryValueBackupKind || Kind == EntryValueCopyBackupKind; |
| 332 | } |
| 333 | |
| 334 | /// If this variable is described by a register holding the entry value, |
| 335 | /// return it, otherwise return 0. |
| 336 | unsigned getEntryValueBackupReg() const { |
| 337 | if (Kind == EntryValueBackupKind) |
| 338 | return Loc.RegNo; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /// If this variable is described by a register holding the copy of the |
| 343 | /// entry value, return it, otherwise return 0. |
| 344 | unsigned getEntryValueCopyBackupReg() const { |
| 345 | if (Kind == EntryValueCopyBackupKind) |
| 346 | return Loc.RegNo; |
| 347 | return 0; |
| 348 | } |
| 349 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 350 | /// If this variable is described by a register, return it, |
| 351 | /// otherwise return 0. |
| 352 | unsigned isDescribedByReg() const { |
| 353 | if (Kind == RegisterKind) |
| Adrian Prantl | 359846f | 2017-07-28 23:25:51 +0000 | [diff] [blame] | 354 | return Loc.RegNo; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 358 | /// Determine whether the lexical scope of this value's debug location |
| 359 | /// dominates MBB. |
| 360 | bool dominates(MachineBasicBlock &MBB) const { return UVS.dominates(&MBB); } |
| 361 | |
| Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 362 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 363 | // TRI can be null. |
| 364 | void dump(const TargetRegisterInfo *TRI, raw_ostream &Out = dbgs()) const { |
| 365 | dbgs() << "VarLoc("; |
| 366 | switch (Kind) { |
| 367 | case RegisterKind: |
| 368 | case EntryValueKind: |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 369 | case EntryValueBackupKind: |
| 370 | case EntryValueCopyBackupKind: |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 371 | dbgs() << printReg(Loc.RegNo, TRI); |
| 372 | break; |
| 373 | case SpillLocKind: |
| 374 | dbgs() << printReg(Loc.SpillLocation.SpillBase, TRI); |
| 375 | dbgs() << "[" << Loc.SpillLocation.SpillOffset << "]"; |
| 376 | break; |
| 377 | case ImmediateKind: |
| 378 | dbgs() << Loc.Immediate; |
| 379 | break; |
| 380 | case InvalidKind: |
| 381 | llvm_unreachable("Invalid VarLoc in dump method"); |
| 382 | } |
| 383 | |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 384 | dbgs() << ", \"" << Var.getVariable()->getName() << "\", " << *Expr |
| 385 | << ", "; |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 386 | if (Var.getInlinedAt()) |
| 387 | dbgs() << "!" << Var.getInlinedAt()->getMetadataID() << ")\n"; |
| 388 | else |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 389 | dbgs() << "(null))"; |
| 390 | |
| 391 | if (isEntryBackupLoc()) |
| 392 | dbgs() << " (backup loc)\n"; |
| 393 | else |
| 394 | dbgs() << "\n"; |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 395 | } |
| Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 396 | #endif |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 397 | |
| 398 | bool operator==(const VarLoc &Other) const { |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 399 | return Kind == Other.Kind && Var == Other.Var && |
| Jeremy Morse | 337a7cb | 2019-09-04 11:09:05 +0000 | [diff] [blame] | 400 | Loc.Hash == Other.Loc.Hash && Expr == Other.Expr; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 403 | /// This operator guarantees that VarLocs are sorted by Variable first. |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 404 | bool operator<(const VarLoc &Other) const { |
| Jeremy Morse | 337a7cb | 2019-09-04 11:09:05 +0000 | [diff] [blame] | 405 | return std::tie(Var, Kind, Loc.Hash, Expr) < |
| 406 | std::tie(Other.Var, Other.Kind, Other.Loc.Hash, Other.Expr); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 407 | } |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 408 | }; |
| 409 | |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 410 | using VarLocMap = UniqueVector<VarLoc>; |
| 411 | using VarLocSet = SparseBitVector<>; |
| 412 | using VarLocInMBB = SmallDenseMap<const MachineBasicBlock *, VarLocSet>; |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 413 | struct TransferDebugPair { |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 414 | MachineInstr *TransferInst; /// Instruction where this transfer occurs. |
| 415 | unsigned LocationID; /// Location number for the transfer dest. |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 416 | }; |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 417 | using TransferMap = SmallVector<TransferDebugPair, 4>; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 418 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 419 | // Types for recording sets of variable fragments that overlap. For a given |
| 420 | // local variable, we record all other fragments of that variable that could |
| 421 | // overlap it, to reduce search time. |
| 422 | using FragmentOfVar = |
| 423 | std::pair<const DILocalVariable *, DIExpression::FragmentInfo>; |
| 424 | using OverlapMap = |
| 425 | DenseMap<FragmentOfVar, SmallVector<DIExpression::FragmentInfo, 1>>; |
| 426 | |
| 427 | // Helper while building OverlapMap, a map of all fragments seen for a given |
| 428 | // DILocalVariable. |
| 429 | using VarToFragments = |
| 430 | DenseMap<const DILocalVariable *, SmallSet<FragmentInfo, 4>>; |
| 431 | |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 432 | /// This holds the working set of currently open ranges. For fast |
| 433 | /// access, this is done both as a set of VarLocIDs, and a map of |
| 434 | /// DebugVariable to recent VarLocID. Note that a DBG_VALUE ends all |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 435 | /// previous open ranges for the same variable. In addition, we keep |
| 436 | /// two different maps (Vars/EntryValuesBackupVars), so erase/insert |
| 437 | /// methods act differently depending on whether a VarLoc is primary |
| 438 | /// location or backup one. In the case the VarLoc is backup location |
| 439 | /// we will erase/insert from the EntryValuesBackupVars map, otherwise |
| 440 | /// we perform the operation on the Vars. |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 441 | class OpenRangesSet { |
| 442 | VarLocSet VarLocs; |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 443 | // Map the DebugVariable to recent primary location ID. |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 444 | SmallDenseMap<DebugVariable, unsigned, 8> Vars; |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 445 | // Map the DebugVariable to recent backup location ID. |
| 446 | SmallDenseMap<DebugVariable, unsigned, 8> EntryValuesBackupVars; |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 447 | OverlapMap &OverlappingFragments; |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 448 | |
| 449 | public: |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 450 | OpenRangesSet(OverlapMap &_OLapMap) : OverlappingFragments(_OLapMap) {} |
| 451 | |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 452 | const VarLocSet &getVarLocs() const { return VarLocs; } |
| 453 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 454 | /// Terminate all open ranges for VL.Var by removing it from the set. |
| 455 | void erase(const VarLoc &VL); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 456 | |
| 457 | /// Terminate all open ranges listed in \c KillSet by removing |
| 458 | /// them from the set. |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 459 | void erase(const VarLocSet &KillSet, const VarLocMap &VarLocIDs); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 460 | |
| 461 | /// Insert a new range into the set. |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 462 | void insert(unsigned VarLocID, const VarLoc &VL); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 463 | |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 464 | /// Insert a set of ranges. |
| 465 | void insertFromLocSet(const VarLocSet &ToLoad, const VarLocMap &Map) { |
| 466 | for (unsigned Id : ToLoad) { |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 467 | const VarLoc &VarL = Map[Id]; |
| 468 | insert(Id, VarL); |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 472 | llvm::Optional<unsigned> getEntryValueBackup(DebugVariable Var); |
| 473 | |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 474 | /// Empty the set. |
| 475 | void clear() { |
| 476 | VarLocs.clear(); |
| 477 | Vars.clear(); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 478 | EntryValuesBackupVars.clear(); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /// Return whether the set is empty or not. |
| 482 | bool empty() const { |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 483 | assert(Vars.empty() == EntryValuesBackupVars.empty() && |
| 484 | Vars.empty() == VarLocs.empty() && |
| 485 | "open ranges are inconsistent"); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 486 | return VarLocs.empty(); |
| 487 | } |
| 488 | }; |
| 489 | |
| Jeremy Morse | 5d9cd3b | 2019-09-06 10:08:22 +0000 | [diff] [blame] | 490 | /// Tests whether this instruction is a spill to a stack location. |
| 491 | bool isSpillInstruction(const MachineInstr &MI, MachineFunction *MF); |
| 492 | |
| 493 | /// Decide if @MI is a spill instruction and return true if it is. We use 2 |
| 494 | /// criteria to make this decision: |
| 495 | /// - Is this instruction a store to a spill slot? |
| 496 | /// - Is there a register operand that is both used and killed? |
| 497 | /// TODO: Store optimization can fold spills into other stores (including |
| 498 | /// other spills). We do not handle this yet (more than one memory operand). |
| 499 | bool isLocationSpill(const MachineInstr &MI, MachineFunction *MF, |
| 500 | unsigned &Reg); |
| 501 | |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 502 | /// Returns true if the given machine instruction is a debug value which we |
| 503 | /// can emit entry values for. |
| 504 | /// |
| 505 | /// Currently, we generate debug entry values only for parameters that are |
| 506 | /// unmodified throughout the function and located in a register. |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 507 | bool isEntryValueCandidate(const MachineInstr &MI, |
| 508 | const DefinedRegsSet &Regs) const; |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 509 | |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 510 | /// If a given instruction is identified as a spill, return the spill location |
| 511 | /// and set \p Reg to the spilled register. |
| 512 | Optional<VarLoc::SpillLoc> isRestoreInstruction(const MachineInstr &MI, |
| 513 | MachineFunction *MF, |
| 514 | unsigned &Reg); |
| 515 | /// Given a spill instruction, extract the register and offset used to |
| 516 | /// address the spill location in a target independent way. |
| 517 | VarLoc::SpillLoc extractSpillBaseRegAndOffset(const MachineInstr &MI); |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 518 | void insertTransferDebugPair(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| 519 | TransferMap &Transfers, VarLocMap &VarLocIDs, |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 520 | unsigned OldVarID, TransferKind Kind, |
| 521 | unsigned NewReg = 0); |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 522 | |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 523 | void transferDebugValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 524 | VarLocMap &VarLocIDs); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 525 | void transferSpillOrRestoreInst(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| 526 | VarLocMap &VarLocIDs, TransferMap &Transfers); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 527 | bool removeEntryValue(const MachineInstr &MI, OpenRangesSet &OpenRanges, |
| 528 | VarLocMap &VarLocIDs, const VarLoc &EntryVL); |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 529 | void emitEntryValues(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| 530 | VarLocMap &VarLocIDs, TransferMap &Transfers, |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 531 | SparseBitVector<> &KillSet); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 532 | void recordEntryValue(const MachineInstr &MI, |
| 533 | const DefinedRegsSet &DefinedRegs, |
| 534 | OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs); |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 535 | void transferRegisterCopy(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| 536 | VarLocMap &VarLocIDs, TransferMap &Transfers); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 537 | void transferRegisterDef(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 538 | VarLocMap &VarLocIDs, TransferMap &Transfers); |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 539 | bool transferTerminator(MachineBasicBlock *MBB, OpenRangesSet &OpenRanges, |
| 540 | VarLocInMBB &OutLocs, const VarLocMap &VarLocIDs); |
| Nikola Prica | 441ad62 | 2019-05-27 13:51:30 +0000 | [diff] [blame] | 541 | |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 542 | void process(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 543 | VarLocMap &VarLocIDs, TransferMap &Transfers); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 544 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 545 | void accumulateFragmentMap(MachineInstr &MI, VarToFragments &SeenFragments, |
| 546 | OverlapMap &OLapMap); |
| 547 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 548 | bool join(MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 549 | const VarLocMap &VarLocIDs, |
| Vedant Kumar | 8c46668 | 2018-10-05 21:44:15 +0000 | [diff] [blame] | 550 | SmallPtrSet<const MachineBasicBlock *, 16> &Visited, |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 551 | SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks, |
| 552 | VarLocInMBB &PendingInLocs); |
| 553 | |
| 554 | /// Create DBG_VALUE insts for inlocs that have been propagated but |
| 555 | /// had their instruction creation deferred. |
| 556 | void flushPendingLocs(VarLocInMBB &PendingInLocs, VarLocMap &VarLocIDs); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 557 | |
| 558 | bool ExtendRanges(MachineFunction &MF); |
| 559 | |
| 560 | public: |
| 561 | static char ID; |
| 562 | |
| 563 | /// Default construct and initialize the pass. |
| 564 | LiveDebugValues(); |
| 565 | |
| 566 | /// Tell the pass manager which passes we depend on and what |
| 567 | /// information we preserve. |
| 568 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 569 | |
| Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 570 | MachineFunctionProperties getRequiredProperties() const override { |
| 571 | return MachineFunctionProperties().set( |
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 572 | MachineFunctionProperties::Property::NoVRegs); |
| Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 575 | /// Print to ostream with a message. |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 576 | void printVarLocInMBB(const MachineFunction &MF, const VarLocInMBB &V, |
| 577 | const VarLocMap &VarLocIDs, const char *msg, |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 578 | raw_ostream &Out) const; |
| 579 | |
| 580 | /// Calculate the liveness information for the given machine function. |
| 581 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 582 | }; |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 583 | |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 584 | } // end anonymous namespace |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 585 | |
| 586 | //===----------------------------------------------------------------------===// |
| 587 | // Implementation |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | |
| 590 | char LiveDebugValues::ID = 0; |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 591 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 592 | char &llvm::LiveDebugValuesID = LiveDebugValues::ID; |
| Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 593 | |
| Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 594 | INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 595 | false, false) |
| 596 | |
| 597 | /// Default construct and initialize the pass. |
| 598 | LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) { |
| 599 | initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry()); |
| 600 | } |
| 601 | |
| 602 | /// Tell the pass manager which passes we depend on and what information we |
| 603 | /// preserve. |
| 604 | void LiveDebugValues::getAnalysisUsage(AnalysisUsage &AU) const { |
| Matt Arsenault | b1630a1 | 2016-06-08 05:18:01 +0000 | [diff] [blame] | 605 | AU.setPreservesCFG(); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 606 | MachineFunctionPass::getAnalysisUsage(AU); |
| 607 | } |
| 608 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 609 | /// Erase a variable from the set of open ranges, and additionally erase any |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 610 | /// fragments that may overlap it. If the VarLoc is a buckup location, erase |
| 611 | /// the variable from the EntryValuesBackupVars set, indicating we should stop |
| 612 | /// tracking its backup entry location. Otherwise, if the VarLoc is primary |
| 613 | /// location, erase the variable from the Vars set. |
| 614 | void LiveDebugValues::OpenRangesSet::erase(const VarLoc &VL) { |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 615 | // Erasure helper. |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 616 | auto DoErase = [VL, this](DebugVariable VarToErase) { |
| 617 | auto *EraseFrom = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; |
| 618 | auto It = EraseFrom->find(VarToErase); |
| 619 | if (It != EraseFrom->end()) { |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 620 | unsigned ID = It->second; |
| 621 | VarLocs.reset(ID); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 622 | EraseFrom->erase(It); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 623 | } |
| 624 | }; |
| 625 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 626 | DebugVariable Var = VL.Var; |
| 627 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 628 | // Erase the variable/fragment that ends here. |
| 629 | DoErase(Var); |
| 630 | |
| 631 | // Extract the fragment. Interpret an empty fragment as one that covers all |
| 632 | // possible bits. |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 633 | FragmentInfo ThisFragment = Var.getFragmentOrDefault(); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 634 | |
| 635 | // There may be fragments that overlap the designated fragment. Look them up |
| 636 | // in the pre-computed overlap map, and erase them too. |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 637 | auto MapIt = OverlappingFragments.find({Var.getVariable(), ThisFragment}); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 638 | if (MapIt != OverlappingFragments.end()) { |
| 639 | for (auto Fragment : MapIt->second) { |
| 640 | LiveDebugValues::OptFragmentInfo FragmentHolder; |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 641 | if (!DebugVariable::isDefaultFragment(Fragment)) |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 642 | FragmentHolder = LiveDebugValues::OptFragmentInfo(Fragment); |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 643 | DoErase({Var.getVariable(), FragmentHolder, Var.getInlinedAt()}); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 648 | void LiveDebugValues::OpenRangesSet::erase(const VarLocSet &KillSet, |
| 649 | const VarLocMap &VarLocIDs) { |
| 650 | VarLocs.intersectWithComplement(KillSet); |
| 651 | for (unsigned ID : KillSet) { |
| 652 | const VarLoc *VL = &VarLocIDs[ID]; |
| 653 | auto *EraseFrom = VL->isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; |
| 654 | EraseFrom->erase(VL->Var); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void LiveDebugValues::OpenRangesSet::insert(unsigned VarLocID, |
| 659 | const VarLoc &VL) { |
| 660 | auto *InsertInto = VL.isEntryBackupLoc() ? &EntryValuesBackupVars : &Vars; |
| 661 | VarLocs.set(VarLocID); |
| 662 | InsertInto->insert({VL.Var, VarLocID}); |
| 663 | } |
| 664 | |
| 665 | /// Return the Loc ID of an entry value backup location, if it exists for the |
| 666 | /// variable. |
| 667 | llvm::Optional<unsigned> |
| 668 | LiveDebugValues::OpenRangesSet::getEntryValueBackup(DebugVariable Var) { |
| 669 | auto It = EntryValuesBackupVars.find(Var); |
| 670 | if (It != EntryValuesBackupVars.end()) |
| 671 | return It->second; |
| 672 | |
| 673 | return llvm::None; |
| 674 | } |
| 675 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 676 | //===----------------------------------------------------------------------===// |
| 677 | // Debug Range Extension Implementation |
| 678 | //===----------------------------------------------------------------------===// |
| 679 | |
| Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 680 | #ifndef NDEBUG |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 681 | void LiveDebugValues::printVarLocInMBB(const MachineFunction &MF, |
| 682 | const VarLocInMBB &V, |
| 683 | const VarLocMap &VarLocIDs, |
| 684 | const char *msg, |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 685 | raw_ostream &Out) const { |
| Keith Walker | f83a19f | 2016-09-20 16:04:31 +0000 | [diff] [blame] | 686 | Out << '\n' << msg << '\n'; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 687 | for (const MachineBasicBlock &BB : MF) { |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 688 | const VarLocSet &L = V.lookup(&BB); |
| 689 | if (L.empty()) |
| 690 | continue; |
| 691 | Out << "MBB: " << BB.getNumber() << ":\n"; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 692 | for (unsigned VLL : L) { |
| 693 | const VarLoc &VL = VarLocIDs[VLL]; |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 694 | Out << " Var: " << VL.Var.getVariable()->getName(); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 695 | Out << " MI: "; |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 696 | VL.dump(TRI, Out); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | Out << "\n"; |
| 700 | } |
| Matthias Braun | 194ded5 | 2017-01-28 06:53:55 +0000 | [diff] [blame] | 701 | #endif |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 702 | |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 703 | LiveDebugValues::VarLoc::SpillLoc |
| 704 | LiveDebugValues::extractSpillBaseRegAndOffset(const MachineInstr &MI) { |
| Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 705 | assert(MI.hasOneMemOperand() && |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 706 | "Spill instruction does not have exactly one memory operand?"); |
| 707 | auto MMOI = MI.memoperands_begin(); |
| 708 | const PseudoSourceValue *PVal = (*MMOI)->getPseudoValue(); |
| 709 | assert(PVal->kind() == PseudoSourceValue::FixedStack && |
| 710 | "Inconsistent memory operand in spill instruction"); |
| 711 | int FI = cast<FixedStackPseudoSourceValue>(PVal)->getFrameIndex(); |
| 712 | const MachineBasicBlock *MBB = MI.getParent(); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 713 | unsigned Reg; |
| 714 | int Offset = TFI->getFrameIndexReference(*MBB->getParent(), FI, Reg); |
| 715 | return {Reg, Offset}; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 718 | /// Try to salvage the debug entry value if we encounter a new debug value |
| 719 | /// describing the same parameter, otherwise stop tracking the value. Return |
| 720 | /// true if we should stop tracking the entry value, otherwise return false. |
| 721 | bool LiveDebugValues::removeEntryValue(const MachineInstr &MI, |
| 722 | OpenRangesSet &OpenRanges, |
| 723 | VarLocMap &VarLocIDs, |
| 724 | const VarLoc &EntryVL) { |
| 725 | // Skip the DBG_VALUE which is the debug entry value itself. |
| 726 | if (MI.isIdenticalTo(EntryVL.MI)) |
| 727 | return false; |
| 728 | |
| 729 | // If the parameter's location is not register location, we can not track |
| 730 | // the entry value any more. In addition, if the debug expression from the |
| 731 | // DBG_VALUE is not empty, we can assume the parameter's value has changed |
| 732 | // indicating that we should stop tracking its entry value as well. |
| 733 | if (!MI.getOperand(0).isReg() || |
| 734 | MI.getDebugExpression()->getNumElements() != 0) |
| 735 | return true; |
| 736 | |
| 737 | // If the DBG_VALUE comes from a copy instruction that copies the entry value, |
| 738 | // it means the parameter's value has not changed and we should be able to use |
| 739 | // its entry value. |
| 740 | bool TrySalvageEntryValue = false; |
| 741 | Register Reg = MI.getOperand(0).getReg(); |
| 742 | auto I = std::next(MI.getReverseIterator()); |
| 743 | const MachineOperand *SrcRegOp, *DestRegOp; |
| 744 | if (I != MI.getParent()->rend()) { |
| 745 | // TODO: Try to keep tracking of an entry value if we encounter a propagated |
| 746 | // DBG_VALUE describing the copy of the entry value. (Propagated entry value |
| 747 | // does not indicate the parameter modification.) |
| 748 | auto DestSrc = TII->isCopyInstr(*I); |
| 749 | if (!DestSrc) |
| 750 | return true; |
| 751 | |
| 752 | SrcRegOp = DestSrc->Source; |
| 753 | DestRegOp = DestSrc->Destination; |
| 754 | if (Reg != DestRegOp->getReg()) |
| 755 | return true; |
| 756 | TrySalvageEntryValue = true; |
| 757 | } |
| 758 | |
| 759 | if (TrySalvageEntryValue) { |
| 760 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| 761 | const VarLoc &VL = VarLocIDs[ID]; |
| 762 | if (!VL.isEntryBackupLoc()) |
| 763 | continue; |
| 764 | |
| 765 | if (VL.getEntryValueCopyBackupReg() == Reg && |
| 766 | VL.MI.getOperand(0).getReg() == SrcRegOp->getReg()) |
| 767 | return false; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | return true; |
| 772 | } |
| 773 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 774 | /// End all previous ranges related to @MI and start a new range from @MI |
| 775 | /// if it is a DBG_VALUE instr. |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 776 | void LiveDebugValues::transferDebugValue(const MachineInstr &MI, |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 777 | OpenRangesSet &OpenRanges, |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 778 | VarLocMap &VarLocIDs) { |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 779 | if (!MI.isDebugValue()) |
| 780 | return; |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 781 | const DILocalVariable *Var = MI.getDebugVariable(); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 782 | const DIExpression *Expr = MI.getDebugExpression(); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 783 | const DILocation *DebugLoc = MI.getDebugLoc(); |
| 784 | const DILocation *InlinedAt = DebugLoc->getInlinedAt(); |
| 785 | assert(Var->isValidLocationForIntrinsic(DebugLoc) && |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 786 | "Expected inlined-at fields to agree"); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 787 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 788 | DebugVariable V(Var, Expr, InlinedAt); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 789 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 790 | // Check if this DBG_VALUE indicates a parameter's value changing. |
| 791 | // If that is the case, we should stop tracking its entry value. |
| 792 | auto EntryValBackupID = OpenRanges.getEntryValueBackup(V); |
| 793 | if (Var->isParameter() && EntryValBackupID) { |
| 794 | const VarLoc &EntryVL = VarLocIDs[*EntryValBackupID]; |
| 795 | if (removeEntryValue(MI, OpenRanges, VarLocIDs, EntryVL)) { |
| 796 | LLVM_DEBUG(dbgs() << "Deleting a DBG entry value because of: "; |
| 797 | MI.print(dbgs(), /*IsStandalone*/ false, |
| 798 | /*SkipOpers*/ false, /*SkipDebugLoc*/ false, |
| 799 | /*AddNewLine*/ true, TII)); |
| 800 | OpenRanges.erase(EntryVL); |
| 801 | } |
| 802 | } |
| 803 | |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 804 | unsigned ID; |
| Djordje Todorovic | 774eabd | 2019-06-27 18:12:04 +0000 | [diff] [blame] | 805 | if (isDbgValueDescribedByReg(MI) || MI.getOperand(0).isImm() || |
| 806 | MI.getOperand(0).isFPImm() || MI.getOperand(0).isCImm()) { |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 807 | // Use normal VarLoc constructor for registers and immediates. |
| Djordje Todorovic | 774eabd | 2019-06-27 18:12:04 +0000 | [diff] [blame] | 808 | VarLoc VL(MI, LS); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 809 | // End all previous ranges of VL.Var. |
| 810 | OpenRanges.erase(VL); |
| 811 | |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 812 | ID = VarLocIDs.insert(VL); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 813 | // Add the VarLoc to OpenRanges from this DBG_VALUE. |
| 814 | OpenRanges.insert(ID, VL); |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 815 | } else if (MI.hasOneMemOperand()) { |
| Jeremy Morse | 8b59348 | 2019-08-16 10:04:17 +0000 | [diff] [blame] | 816 | llvm_unreachable("DBG_VALUE with mem operand encountered after regalloc?"); |
| Jeremy Morse | bcff417 | 2019-06-10 15:23:46 +0000 | [diff] [blame] | 817 | } else { |
| 818 | // This must be an undefined location. We should leave OpenRanges closed. |
| 819 | assert(MI.getOperand(0).isReg() && MI.getOperand(0).getReg() == 0 && |
| 820 | "Unexpected non-undef DBG_VALUE encountered"); |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 821 | } |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 824 | /// Turn the entry value backup locations into primary locations. |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 825 | void LiveDebugValues::emitEntryValues(MachineInstr &MI, |
| 826 | OpenRangesSet &OpenRanges, |
| 827 | VarLocMap &VarLocIDs, |
| 828 | TransferMap &Transfers, |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 829 | SparseBitVector<> &KillSet) { |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 830 | for (unsigned ID : KillSet) { |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 831 | if (!VarLocIDs[ID].Var.getVariable()->isParameter()) |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 832 | continue; |
| 833 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 834 | auto DebugVar = VarLocIDs[ID].Var; |
| 835 | auto EntryValBackupID = OpenRanges.getEntryValueBackup(DebugVar); |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 836 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 837 | // If the parameter has the entry value backup, it means we should |
| 838 | // be able to use its entry value. |
| 839 | if (!EntryValBackupID) |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 840 | continue; |
| 841 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 842 | const VarLoc &EntryVL = VarLocIDs[*EntryValBackupID]; |
| 843 | VarLoc EntryLoc = |
| 844 | VarLoc::CreateEntryLoc(EntryVL.MI, LS, EntryVL.Expr, EntryVL.Loc.RegNo); |
| 845 | unsigned EntryValueID = VarLocIDs.insert(EntryLoc); |
| 846 | Transfers.push_back({&MI, EntryValueID}); |
| 847 | OpenRanges.insert(EntryValueID, EntryLoc); |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 851 | /// Create new TransferDebugPair and insert it in \p Transfers. The VarLoc |
| 852 | /// with \p OldVarID should be deleted form \p OpenRanges and replaced with |
| 853 | /// new VarLoc. If \p NewReg is different than default zero value then the |
| 854 | /// new location will be register location created by the copy like instruction, |
| 855 | /// otherwise it is variable's location on the stack. |
| 856 | void LiveDebugValues::insertTransferDebugPair( |
| 857 | MachineInstr &MI, OpenRangesSet &OpenRanges, TransferMap &Transfers, |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 858 | VarLocMap &VarLocIDs, unsigned OldVarID, TransferKind Kind, |
| 859 | unsigned NewReg) { |
| Petar Jovanovic | aa28b6d | 2019-05-23 13:49:06 +0000 | [diff] [blame] | 860 | const MachineInstr *DebugInstr = &VarLocIDs[OldVarID].MI; |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 861 | |
| Djordje Todorovic | 52b231e | 2019-12-05 12:21:51 +0100 | [diff] [blame] | 862 | auto ProcessVarLoc = [&MI, &OpenRanges, &Transfers, &VarLocIDs](VarLoc &VL) { |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 863 | unsigned LocId = VarLocIDs.insert(VL); |
| Nikola Prica | 2d0106a | 2019-06-03 09:48:29 +0000 | [diff] [blame] | 864 | |
| 865 | // Close this variable's previous location range. |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 866 | OpenRanges.erase(VL); |
| Nikola Prica | 2d0106a | 2019-06-03 09:48:29 +0000 | [diff] [blame] | 867 | |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 868 | // Record the new location as an open range, and a postponed transfer |
| 869 | // inserting a DBG_VALUE for this location. |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 870 | OpenRanges.insert(LocId, VL); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 871 | TransferDebugPair MIP = {&MI, LocId}; |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 872 | Transfers.push_back(MIP); |
| 873 | }; |
| 874 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 875 | // End all previous ranges of VL.Var. |
| 876 | OpenRanges.erase(VarLocIDs[OldVarID]); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 877 | switch (Kind) { |
| 878 | case TransferKind::TransferCopy: { |
| 879 | assert(NewReg && |
| 880 | "No register supplied when handling a copy of a debug value"); |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 881 | // Create a DBG_VALUE instruction to describe the Var in its new |
| 882 | // register location. |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 883 | VarLoc VL = VarLoc::CreateCopyLoc(*DebugInstr, LS, NewReg); |
| 884 | ProcessVarLoc(VL); |
| 885 | LLVM_DEBUG({ |
| 886 | dbgs() << "Creating VarLoc for register copy:"; |
| 887 | VL.dump(TRI); |
| 888 | }); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | case TransferKind::TransferSpill: { |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 892 | // Create a DBG_VALUE instruction to describe the Var in its spilled |
| 893 | // location. |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 894 | VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 895 | VarLoc VL = VarLoc::CreateSpillLoc(*DebugInstr, SpillLocation.SpillBase, |
| 896 | SpillLocation.SpillOffset, LS); |
| 897 | ProcessVarLoc(VL); |
| 898 | LLVM_DEBUG({ |
| 899 | dbgs() << "Creating VarLoc for spill:"; |
| 900 | VL.dump(TRI); |
| 901 | }); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 902 | return; |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 903 | } |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 904 | case TransferKind::TransferRestore: { |
| 905 | assert(NewReg && |
| 906 | "No register supplied when handling a restore of a debug value"); |
| Jeremy Morse | 8b59348 | 2019-08-16 10:04:17 +0000 | [diff] [blame] | 907 | // DebugInstr refers to the pre-spill location, therefore we can reuse |
| 908 | // its expression. |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 909 | VarLoc VL = VarLoc::CreateCopyLoc(*DebugInstr, LS, NewReg); |
| 910 | ProcessVarLoc(VL); |
| 911 | LLVM_DEBUG({ |
| 912 | dbgs() << "Creating VarLoc for restore:"; |
| 913 | VL.dump(TRI); |
| 914 | }); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 915 | return; |
| 916 | } |
| 917 | } |
| 918 | llvm_unreachable("Invalid transfer kind"); |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 921 | /// A definition of a register may mark the end of a range. |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 922 | void LiveDebugValues::transferRegisterDef( |
| 923 | MachineInstr &MI, OpenRangesSet &OpenRanges, VarLocMap &VarLocIDs, |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 924 | TransferMap &Transfers) { |
| Tom Weaver | f5147765 | 2020-01-24 16:29:05 +0000 | [diff] [blame] | 925 | |
| 926 | // Meta Instructions do not affect the debug liveness of any register they |
| 927 | // define. |
| 928 | if (MI.isMetaInstruction()) |
| 929 | return; |
| 930 | |
| Justin Bogner | fdf9bf4 | 2017-10-10 23:50:49 +0000 | [diff] [blame] | 931 | MachineFunction *MF = MI.getMF(); |
| Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 932 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
| 933 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 934 | SparseBitVector<> KillSet; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 935 | for (const MachineOperand &MO : MI.operands()) { |
| Adrian Prantl | ea8880b | 2017-03-03 01:08:25 +0000 | [diff] [blame] | 936 | // Determine whether the operand is a register def. Assume that call |
| 937 | // instructions never clobber SP, because some backends (e.g., AArch64) |
| 938 | // never list SP in the regmask. |
| Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 939 | if (MO.isReg() && MO.isDef() && MO.getReg() && |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 940 | Register::isPhysicalRegister(MO.getReg()) && |
| Adrian Prantl | ea8880b | 2017-03-03 01:08:25 +0000 | [diff] [blame] | 941 | !(MI.isCall() && MO.getReg() == SP)) { |
| Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 942 | // Remove ranges of all aliased registers. |
| 943 | for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 944 | for (unsigned ID : OpenRanges.getVarLocs()) |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 945 | if (VarLocIDs[ID].isDescribedByReg() == *RAI) |
| 946 | KillSet.set(ID); |
| Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 947 | } else if (MO.isRegMask()) { |
| 948 | // Remove ranges of all clobbered registers. Register masks don't usually |
| 949 | // list SP as preserved. While the debug info may be off for an |
| 950 | // instruction or two around callee-cleanup calls, transferring the |
| 951 | // DEBUG_VALUE across the call is still a better user experience. |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 952 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 953 | unsigned Reg = VarLocIDs[ID].isDescribedByReg(); |
| 954 | if (Reg && Reg != SP && MO.clobbersPhysReg(Reg)) |
| 955 | KillSet.set(ID); |
| 956 | } |
| Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 957 | } |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 958 | } |
| Adrian Prantl | 7509d54 | 2016-05-26 21:42:47 +0000 | [diff] [blame] | 959 | OpenRanges.erase(KillSet, VarLocIDs); |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 960 | |
| 961 | if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) { |
| 962 | auto &TM = TPC->getTM<TargetMachine>(); |
| Djordje Todorovic | 97ed706 | 2020-02-12 11:54:29 +0100 | [diff] [blame] | 963 | if (TM.Options.EnableDebugEntryValues) |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 964 | emitEntryValues(MI, OpenRanges, VarLocIDs, Transfers, KillSet); |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 965 | } |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 968 | bool LiveDebugValues::isSpillInstruction(const MachineInstr &MI, |
| Jeremy Morse | 5d9cd3b | 2019-09-06 10:08:22 +0000 | [diff] [blame] | 969 | MachineFunction *MF) { |
| Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 970 | // TODO: Handle multiple stores folded into one. |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 971 | if (!MI.hasOneMemOperand()) |
| 972 | return false; |
| 973 | |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 974 | if (!MI.getSpillSize(TII) && !MI.getFoldedSpillSize(TII)) |
| 975 | return false; // This is not a spill instruction, since no valid size was |
| 976 | // returned from either function. |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 977 | |
| Jeremy Morse | 5d9cd3b | 2019-09-06 10:08:22 +0000 | [diff] [blame] | 978 | return true; |
| 979 | } |
| 980 | |
| 981 | bool LiveDebugValues::isLocationSpill(const MachineInstr &MI, |
| 982 | MachineFunction *MF, unsigned &Reg) { |
| 983 | if (!isSpillInstruction(MI, MF)) |
| 984 | return false; |
| 985 | |
| Petar Jovanovic | 0b464e4 | 2018-01-16 14:46:05 +0000 | [diff] [blame] | 986 | auto isKilledReg = [&](const MachineOperand MO, unsigned &Reg) { |
| 987 | if (!MO.isReg() || !MO.isUse()) { |
| 988 | Reg = 0; |
| 989 | return false; |
| 990 | } |
| 991 | Reg = MO.getReg(); |
| 992 | return MO.isKill(); |
| 993 | }; |
| 994 | |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 995 | for (const MachineOperand &MO : MI.operands()) { |
| Petar Jovanovic | 0b464e4 | 2018-01-16 14:46:05 +0000 | [diff] [blame] | 996 | // In a spill instruction generated by the InlineSpiller the spilled |
| 997 | // register has its kill flag set. |
| 998 | if (isKilledReg(MO, Reg)) |
| 999 | return true; |
| 1000 | if (Reg != 0) { |
| 1001 | // Check whether next instruction kills the spilled register. |
| 1002 | // FIXME: Current solution does not cover search for killed register in |
| 1003 | // bundles and instructions further down the chain. |
| 1004 | auto NextI = std::next(MI.getIterator()); |
| 1005 | // Skip next instruction that points to basic block end iterator. |
| 1006 | if (MI.getParent()->end() == NextI) |
| 1007 | continue; |
| 1008 | unsigned RegNext; |
| 1009 | for (const MachineOperand &MONext : NextI->operands()) { |
| 1010 | // Return true if we came across the register from the |
| 1011 | // previous spill instruction that is killed in NextI. |
| 1012 | if (isKilledReg(MONext, RegNext) && RegNext == Reg) |
| 1013 | return true; |
| 1014 | } |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| Petar Jovanovic | 0b464e4 | 2018-01-16 14:46:05 +0000 | [diff] [blame] | 1017 | // Return false if we didn't find spilled register. |
| 1018 | return false; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1021 | Optional<LiveDebugValues::VarLoc::SpillLoc> |
| 1022 | LiveDebugValues::isRestoreInstruction(const MachineInstr &MI, |
| 1023 | MachineFunction *MF, unsigned &Reg) { |
| 1024 | if (!MI.hasOneMemOperand()) |
| 1025 | return None; |
| 1026 | |
| 1027 | // FIXME: Handle folded restore instructions with more than one memory |
| 1028 | // operand. |
| 1029 | if (MI.getRestoreSize(TII)) { |
| 1030 | Reg = MI.getOperand(0).getReg(); |
| 1031 | return extractSpillBaseRegAndOffset(MI); |
| 1032 | } |
| 1033 | return None; |
| 1034 | } |
| 1035 | |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1036 | /// A spilled register may indicate that we have to end the current range of |
| 1037 | /// a variable and create a new one for the spill location. |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1038 | /// A restored register may indicate the reverse situation. |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1039 | /// We don't want to insert any instructions in process(), so we just create |
| 1040 | /// the DBG_VALUE without inserting it and keep track of it in \p Transfers. |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1041 | /// It will be inserted into the BB when we're done iterating over the |
| 1042 | /// instructions. |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1043 | void LiveDebugValues::transferSpillOrRestoreInst(MachineInstr &MI, |
| 1044 | OpenRangesSet &OpenRanges, |
| 1045 | VarLocMap &VarLocIDs, |
| 1046 | TransferMap &Transfers) { |
| Wolfgang Pieb | facd052 | 2019-01-30 20:37:14 +0000 | [diff] [blame] | 1047 | MachineFunction *MF = MI.getMF(); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1048 | TransferKind TKind; |
| 1049 | unsigned Reg; |
| 1050 | Optional<VarLoc::SpillLoc> Loc; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1051 | |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1052 | LLVM_DEBUG(dbgs() << "Examining instruction: "; MI.dump();); |
| 1053 | |
| Jeremy Morse | 5d9cd3b | 2019-09-06 10:08:22 +0000 | [diff] [blame] | 1054 | // First, if there are any DBG_VALUEs pointing at a spill slot that is |
| 1055 | // written to, then close the variable location. The value in memory |
| 1056 | // will have changed. |
| 1057 | VarLocSet KillSet; |
| 1058 | if (isSpillInstruction(MI, MF)) { |
| 1059 | Loc = extractSpillBaseRegAndOffset(MI); |
| 1060 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| 1061 | const VarLoc &VL = VarLocIDs[ID]; |
| 1062 | if (VL.Kind == VarLoc::SpillLocKind && VL.Loc.SpillLocation == *Loc) { |
| 1063 | // This location is overwritten by the current instruction -- terminate |
| 1064 | // the open range, and insert an explicit DBG_VALUE $noreg. |
| 1065 | // |
| 1066 | // Doing this at a later stage would require re-interpreting all |
| 1067 | // DBG_VALUes and DIExpressions to identify whether they point at |
| 1068 | // memory, and then analysing all memory writes to see if they |
| 1069 | // overwrite that memory, which is expensive. |
| 1070 | // |
| 1071 | // At this stage, we already know which DBG_VALUEs are for spills and |
| 1072 | // where they are located; it's best to fix handle overwrites now. |
| 1073 | KillSet.set(ID); |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 1074 | VarLoc UndefVL = VarLoc::CreateCopyLoc(VL.MI, LS, 0); |
| 1075 | unsigned UndefLocID = VarLocIDs.insert(UndefVL); |
| 1076 | Transfers.push_back({&MI, UndefLocID}); |
| Jeremy Morse | 5d9cd3b | 2019-09-06 10:08:22 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | OpenRanges.erase(KillSet, VarLocIDs); |
| 1080 | } |
| 1081 | |
| 1082 | // Try to recognise spill and restore instructions that may create a new |
| 1083 | // variable location. |
| 1084 | if (isLocationSpill(MI, MF, Reg)) { |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1085 | TKind = TransferKind::TransferSpill; |
| 1086 | LLVM_DEBUG(dbgs() << "Recognized as spill: "; MI.dump();); |
| 1087 | LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) |
| 1088 | << "\n"); |
| 1089 | } else { |
| 1090 | if (!(Loc = isRestoreInstruction(MI, MF, Reg))) |
| 1091 | return; |
| 1092 | TKind = TransferKind::TransferRestore; |
| 1093 | LLVM_DEBUG(dbgs() << "Recognized as restore: "; MI.dump();); |
| 1094 | LLVM_DEBUG(dbgs() << "Register: " << Reg << " " << printReg(Reg, TRI) |
| 1095 | << "\n"); |
| 1096 | } |
| 1097 | // Check if the register or spill location is the location of a debug value. |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1098 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1099 | if (TKind == TransferKind::TransferSpill && |
| Jeremy Morse | 8b59348 | 2019-08-16 10:04:17 +0000 | [diff] [blame] | 1100 | VarLocIDs[ID].isDescribedByReg() == Reg) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1101 | LLVM_DEBUG(dbgs() << "Spilling Register " << printReg(Reg, TRI) << '(' |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1102 | << VarLocIDs[ID].Var.getVariable()->getName() << ")\n"); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1103 | } else if (TKind == TransferKind::TransferRestore && |
| Jeremy Morse | ca0e4b3 | 2019-08-29 11:20:54 +0000 | [diff] [blame] | 1104 | VarLocIDs[ID].Kind == VarLoc::SpillLocKind && |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1105 | VarLocIDs[ID].Loc.SpillLocation == *Loc) { |
| 1106 | LLVM_DEBUG(dbgs() << "Restoring Register " << printReg(Reg, TRI) << '(' |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1107 | << VarLocIDs[ID].Var.getVariable()->getName() << ")\n"); |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1108 | } else |
| 1109 | continue; |
| 1110 | insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, ID, TKind, |
| 1111 | Reg); |
| 1112 | return; |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1115 | |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1116 | /// If \p MI is a register copy instruction, that copies a previously tracked |
| 1117 | /// value from one register to another register that is callee saved, we |
| 1118 | /// create new DBG_VALUE instruction described with copy destination register. |
| 1119 | void LiveDebugValues::transferRegisterCopy(MachineInstr &MI, |
| 1120 | OpenRangesSet &OpenRanges, |
| 1121 | VarLocMap &VarLocIDs, |
| 1122 | TransferMap &Transfers) { |
| Djordje Todorovic | 8d2ccd1 | 2019-11-08 11:19:58 +0100 | [diff] [blame] | 1123 | auto DestSrc = TII->isCopyInstr(MI); |
| 1124 | if (!DestSrc) |
| 1125 | return; |
| 1126 | |
| 1127 | const MachineOperand *DestRegOp = DestSrc->Destination; |
| 1128 | const MachineOperand *SrcRegOp = DestSrc->Source; |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1129 | |
| 1130 | if (!DestRegOp->isDef()) |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1131 | return; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1132 | |
| David Stenberg | 2a3dc6b | 2019-10-25 11:21:11 +0200 | [diff] [blame] | 1133 | auto isCalleeSavedReg = [&](unsigned Reg) { |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1134 | for (MCRegAliasIterator RAI(Reg, TRI, true); RAI.isValid(); ++RAI) |
| 1135 | if (CalleeSavedRegs.test(*RAI)) |
| 1136 | return true; |
| 1137 | return false; |
| 1138 | }; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1139 | |
| Simon Pilgrim | 3842b94 | 2019-10-31 17:58:15 +0000 | [diff] [blame] | 1140 | Register SrcReg = SrcRegOp->getReg(); |
| 1141 | Register DestReg = DestRegOp->getReg(); |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1142 | |
| 1143 | // We want to recognize instructions where destination register is callee |
| 1144 | // saved register. If register that could be clobbered by the call is |
| 1145 | // included, there would be a great chance that it is going to be clobbered |
| 1146 | // soon. It is more likely that previous register location, which is callee |
| 1147 | // saved, is going to stay unclobbered longer, even if it is killed. |
| David Stenberg | 2a3dc6b | 2019-10-25 11:21:11 +0200 | [diff] [blame] | 1148 | if (!isCalleeSavedReg(DestReg)) |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1149 | return; |
| 1150 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1151 | // Remember an entry value movement. If we encounter a new debug value of |
| 1152 | // a parameter describing only a moving of the value around, rather then |
| 1153 | // modifying it, we are still able to use the entry value if needed. |
| 1154 | if (isRegOtherThanSPAndFP(*DestRegOp, MI, TRI)) { |
| 1155 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| 1156 | if (VarLocIDs[ID].getEntryValueBackupReg() == SrcReg) { |
| 1157 | LLVM_DEBUG(dbgs() << "Copy of the entry value: "; MI.dump();); |
| 1158 | VarLoc EntryValLocCopyBackup = VarLoc::CreateEntryCopyBackupLoc( |
| 1159 | VarLocIDs[ID].MI, LS, VarLocIDs[ID].Expr, DestReg); |
| 1160 | |
| 1161 | // Stop tracking the original entry value. |
| 1162 | OpenRanges.erase(VarLocIDs[ID]); |
| 1163 | |
| 1164 | // Start tracking the entry value copy. |
| 1165 | unsigned EntryValCopyLocID = VarLocIDs.insert(EntryValLocCopyBackup); |
| 1166 | OpenRanges.insert(EntryValCopyLocID, EntryValLocCopyBackup); |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | if (!SrcRegOp->isKill()) |
| 1173 | return; |
| 1174 | |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1175 | for (unsigned ID : OpenRanges.getVarLocs()) { |
| 1176 | if (VarLocIDs[ID].isDescribedByReg() == SrcReg) { |
| 1177 | insertTransferDebugPair(MI, OpenRanges, Transfers, VarLocIDs, ID, |
| Wolfgang Pieb | 90d856c | 2019-02-04 20:42:45 +0000 | [diff] [blame] | 1178 | TransferKind::TransferCopy, DestReg); |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1179 | return; |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1184 | /// Terminate all open ranges at the end of the current basic block. |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1185 | bool LiveDebugValues::transferTerminator(MachineBasicBlock *CurMBB, |
| 1186 | OpenRangesSet &OpenRanges, |
| 1187 | VarLocInMBB &OutLocs, |
| 1188 | const VarLocMap &VarLocIDs) { |
| Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 1189 | bool Changed = false; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1190 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1191 | LLVM_DEBUG(for (unsigned ID |
| 1192 | : OpenRanges.getVarLocs()) { |
| 1193 | // Copy OpenRanges to OutLocs, if not already present. |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1194 | dbgs() << "Add to OutLocs in MBB #" << CurMBB->getNumber() << ": "; |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 1195 | VarLocIDs[ID].dump(TRI); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1196 | }); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1197 | VarLocSet &VLS = OutLocs[CurMBB]; |
| Jeremy Morse | 0ae5498 | 2019-08-23 16:33:42 +0000 | [diff] [blame] | 1198 | Changed = VLS != OpenRanges.getVarLocs(); |
| Nikola Prica | 2d0106a | 2019-06-03 09:48:29 +0000 | [diff] [blame] | 1199 | // New OutLocs set may be different due to spill, restore or register |
| 1200 | // copy instruction processing. |
| 1201 | if (Changed) |
| 1202 | VLS = OpenRanges.getVarLocs(); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1203 | OpenRanges.clear(); |
| Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 1204 | return Changed; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1207 | /// Accumulate a mapping between each DILocalVariable fragment and other |
| 1208 | /// fragments of that DILocalVariable which overlap. This reduces work during |
| 1209 | /// the data-flow stage from "Find any overlapping fragments" to "Check if the |
| 1210 | /// known-to-overlap fragments are present". |
| 1211 | /// \param MI A previously unprocessed DEBUG_VALUE instruction to analyze for |
| 1212 | /// fragment usage. |
| 1213 | /// \param SeenFragments Map from DILocalVariable to all fragments of that |
| 1214 | /// Variable which are known to exist. |
| 1215 | /// \param OverlappingFragments The overlap map being constructed, from one |
| 1216 | /// Var/Fragment pair to a vector of fragments known to overlap. |
| 1217 | void LiveDebugValues::accumulateFragmentMap(MachineInstr &MI, |
| 1218 | VarToFragments &SeenFragments, |
| 1219 | OverlapMap &OverlappingFragments) { |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1220 | DebugVariable MIVar(MI.getDebugVariable(), MI.getDebugExpression(), |
| 1221 | MI.getDebugLoc()->getInlinedAt()); |
| 1222 | FragmentInfo ThisFragment = MIVar.getFragmentOrDefault(); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1223 | |
| 1224 | // If this is the first sighting of this variable, then we are guaranteed |
| 1225 | // there are currently no overlapping fragments either. Initialize the set |
| 1226 | // of seen fragments, record no overlaps for the current one, and return. |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1227 | auto SeenIt = SeenFragments.find(MIVar.getVariable()); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1228 | if (SeenIt == SeenFragments.end()) { |
| 1229 | SmallSet<FragmentInfo, 4> OneFragment; |
| 1230 | OneFragment.insert(ThisFragment); |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1231 | SeenFragments.insert({MIVar.getVariable(), OneFragment}); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1232 | |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1233 | OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | // If this particular Variable/Fragment pair already exists in the overlap |
| 1238 | // map, it has already been accounted for. |
| 1239 | auto IsInOLapMap = |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1240 | OverlappingFragments.insert({{MIVar.getVariable(), ThisFragment}, {}}); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1241 | if (!IsInOLapMap.second) |
| 1242 | return; |
| 1243 | |
| 1244 | auto &ThisFragmentsOverlaps = IsInOLapMap.first->second; |
| 1245 | auto &AllSeenFragments = SeenIt->second; |
| 1246 | |
| 1247 | // Otherwise, examine all other seen fragments for this variable, with "this" |
| 1248 | // fragment being a previously unseen fragment. Record any pair of |
| 1249 | // overlapping fragments. |
| 1250 | for (auto &ASeenFragment : AllSeenFragments) { |
| 1251 | // Does this previously seen fragment overlap? |
| 1252 | if (DIExpression::fragmentsOverlap(ThisFragment, ASeenFragment)) { |
| 1253 | // Yes: Mark the current fragment as being overlapped. |
| 1254 | ThisFragmentsOverlaps.push_back(ASeenFragment); |
| 1255 | // Mark the previously seen fragment as being overlapped by the current |
| 1256 | // one. |
| 1257 | auto ASeenFragmentsOverlaps = |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1258 | OverlappingFragments.find({MIVar.getVariable(), ASeenFragment}); |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1259 | assert(ASeenFragmentsOverlaps != OverlappingFragments.end() && |
| 1260 | "Previously seen var fragment has no vector of overlaps"); |
| 1261 | ASeenFragmentsOverlaps->second.push_back(ThisFragment); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | AllSeenFragments.insert(ThisFragment); |
| 1266 | } |
| 1267 | |
| Djordje Todorovic | 8c99a54 | 2019-10-24 10:08:43 +0200 | [diff] [blame] | 1268 | /// This routine creates OpenRanges. |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1269 | void LiveDebugValues::process(MachineInstr &MI, OpenRangesSet &OpenRanges, |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1270 | VarLocMap &VarLocIDs, TransferMap &Transfers) { |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1271 | transferDebugValue(MI, OpenRanges, VarLocIDs); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1272 | transferRegisterDef(MI, OpenRanges, VarLocIDs, Transfers); |
| Jeremy Morse | 313d2ce | 2019-08-29 10:53:29 +0000 | [diff] [blame] | 1273 | transferRegisterCopy(MI, OpenRanges, VarLocIDs, Transfers); |
| 1274 | transferSpillOrRestoreInst(MI, OpenRanges, VarLocIDs, Transfers); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /// This routine joins the analysis results of all incoming edges in @MBB by |
| 1278 | /// inserting a new DBG_VALUE instruction at the start of the @MBB - if the same |
| 1279 | /// source variable in all the predecessors of @MBB reside in the same location. |
| Vedant Kumar | 8c46668 | 2018-10-05 21:44:15 +0000 | [diff] [blame] | 1280 | bool LiveDebugValues::join( |
| 1281 | MachineBasicBlock &MBB, VarLocInMBB &OutLocs, VarLocInMBB &InLocs, |
| 1282 | const VarLocMap &VarLocIDs, |
| 1283 | SmallPtrSet<const MachineBasicBlock *, 16> &Visited, |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1284 | SmallPtrSetImpl<const MachineBasicBlock *> &ArtificialBlocks, |
| 1285 | VarLocInMBB &PendingInLocs) { |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1286 | LLVM_DEBUG(dbgs() << "join MBB: " << MBB.getNumber() << "\n"); |
| Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 1287 | bool Changed = false; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1288 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1289 | VarLocSet InLocsT; // Temporary incoming locations. |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1290 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1291 | // For all predecessors of this MBB, find the set of VarLocs that |
| 1292 | // can be joined. |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1293 | int NumVisited = 0; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1294 | for (auto p : MBB.predecessors()) { |
| Jeremy Morse | 313d2ce | 2019-08-29 10:53:29 +0000 | [diff] [blame] | 1295 | // Ignore backedges if we have not visited the predecessor yet. As the |
| 1296 | // predecessor hasn't yet had locations propagated into it, most locations |
| 1297 | // will not yet be valid, so treat them as all being uninitialized and |
| 1298 | // potentially valid. If a location guessed to be correct here is |
| 1299 | // invalidated later, we will remove it when we revisit this block. |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1300 | if (!Visited.count(p)) { |
| 1301 | LLVM_DEBUG(dbgs() << " ignoring unvisited pred MBB: " << p->getNumber() |
| 1302 | << "\n"); |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1303 | continue; |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1304 | } |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1305 | auto OL = OutLocs.find(p); |
| 1306 | // Join is null in case of empty OutLocs from any of the pred. |
| 1307 | if (OL == OutLocs.end()) |
| Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 1308 | return false; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1309 | |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1310 | // Just copy over the Out locs to incoming locs for the first visited |
| 1311 | // predecessor, and for all other predecessors join the Out locs. |
| 1312 | if (!NumVisited) |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1313 | InLocsT = OL->second; |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1314 | else |
| 1315 | InLocsT &= OL->second; |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1316 | |
| 1317 | LLVM_DEBUG({ |
| 1318 | if (!InLocsT.empty()) { |
| 1319 | for (auto ID : InLocsT) |
| 1320 | dbgs() << " gathered candidate incoming var: " |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1321 | << VarLocIDs[ID].Var.getVariable()->getName() << "\n"; |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1322 | } |
| 1323 | }); |
| 1324 | |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1325 | NumVisited++; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 1328 | // Filter out DBG_VALUES that are out of scope. |
| 1329 | VarLocSet KillSet; |
| Vedant Kumar | 8c46668 | 2018-10-05 21:44:15 +0000 | [diff] [blame] | 1330 | bool IsArtificial = ArtificialBlocks.count(&MBB); |
| 1331 | if (!IsArtificial) { |
| 1332 | for (auto ID : InLocsT) { |
| 1333 | if (!VarLocIDs[ID].dominates(MBB)) { |
| 1334 | KillSet.set(ID); |
| 1335 | LLVM_DEBUG({ |
| stozer | 269a9af | 2019-12-03 12:24:41 +0000 | [diff] [blame] | 1336 | auto Name = VarLocIDs[ID].Var.getVariable()->getName(); |
| Vedant Kumar | 8c46668 | 2018-10-05 21:44:15 +0000 | [diff] [blame] | 1337 | dbgs() << " killing " << Name << ", it doesn't dominate MBB\n"; |
| 1338 | }); |
| 1339 | } |
| Vedant Kumar | 9b55838 | 2018-10-05 21:44:00 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 1342 | InLocsT.intersectWithComplement(KillSet); |
| 1343 | |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1344 | // As we are processing blocks in reverse post-order we |
| 1345 | // should have processed at least one predecessor, unless it |
| 1346 | // is the entry block which has no predecessor. |
| 1347 | assert((NumVisited || MBB.pred_empty()) && |
| 1348 | "Should have processed at least one predecessor"); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1349 | |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1350 | VarLocSet &ILS = InLocs[&MBB]; |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1351 | VarLocSet &Pending = PendingInLocs[&MBB]; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1352 | |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1353 | // New locations will have DBG_VALUE insts inserted at the start of the |
| 1354 | // block, after location propagation has finished. Record the insertions |
| 1355 | // that we need to perform in the Pending set. |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1356 | VarLocSet Diff = InLocsT; |
| 1357 | Diff.intersectWithComplement(ILS); |
| 1358 | for (auto ID : Diff) { |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1359 | Pending.set(ID); |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1360 | ILS.set(ID); |
| 1361 | ++NumInserted; |
| 1362 | Changed = true; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1363 | } |
| Jeremy Morse | 0ae5498 | 2019-08-23 16:33:42 +0000 | [diff] [blame] | 1364 | |
| 1365 | // We may have lost locations by learning about a predecessor that either |
| 1366 | // loses or moves a variable. Find any locations in ILS that are not in the |
| 1367 | // new in-locations, and delete those. |
| 1368 | VarLocSet Removed = ILS; |
| 1369 | Removed.intersectWithComplement(InLocsT); |
| 1370 | for (auto ID : Removed) { |
| 1371 | Pending.reset(ID); |
| 1372 | ILS.reset(ID); |
| 1373 | ++NumRemoved; |
| 1374 | Changed = true; |
| 1375 | } |
| 1376 | |
| Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 1377 | return Changed; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1380 | void LiveDebugValues::flushPendingLocs(VarLocInMBB &PendingInLocs, |
| 1381 | VarLocMap &VarLocIDs) { |
| 1382 | // PendingInLocs records all locations propagated into blocks, which have |
| 1383 | // not had DBG_VALUE insts created. Go through and create those insts now. |
| 1384 | for (auto &Iter : PendingInLocs) { |
| 1385 | // Map is keyed on a constant pointer, unwrap it so we can insert insts. |
| 1386 | auto &MBB = const_cast<MachineBasicBlock &>(*Iter.first); |
| 1387 | VarLocSet &Pending = Iter.second; |
| 1388 | |
| 1389 | for (unsigned ID : Pending) { |
| 1390 | // The ID location is live-in to MBB -- work out what kind of machine |
| 1391 | // location it is and create a DBG_VALUE. |
| 1392 | const VarLoc &DiffIt = VarLocIDs[ID]; |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1393 | if (DiffIt.isEntryBackupLoc()) |
| 1394 | continue; |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 1395 | MachineInstr *MI = DiffIt.BuildDbgValue(*MBB.getParent()); |
| 1396 | MBB.insert(MBB.instr_begin(), MI); |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1397 | |
| Jeremy Morse | c8c5f2a | 2019-09-04 10:18:03 +0000 | [diff] [blame] | 1398 | (void)MI; |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1399 | LLVM_DEBUG(dbgs() << "Inserted: "; MI->dump();); |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 1404 | bool LiveDebugValues::isEntryValueCandidate( |
| 1405 | const MachineInstr &MI, const DefinedRegsSet &DefinedRegs) const { |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1406 | assert(MI.isDebugValue() && "This must be DBG_VALUE."); |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 1407 | |
| 1408 | // TODO: Add support for local variables that are expressed in terms of |
| 1409 | // parameters entry values. |
| 1410 | // TODO: Add support for modified arguments that can be expressed |
| 1411 | // by using its entry value. |
| 1412 | auto *DIVar = MI.getDebugVariable(); |
| Djordje Todorovic | 979592a | 2019-11-20 12:20:53 +0100 | [diff] [blame] | 1413 | if (!DIVar->isParameter()) |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 1414 | return false; |
| 1415 | |
| 1416 | // Do not consider parameters that belong to an inlined function. |
| 1417 | if (MI.getDebugLoc()->getInlinedAt()) |
| 1418 | return false; |
| 1419 | |
| 1420 | // Do not consider indirect debug values (TODO: explain why). |
| 1421 | if (MI.isIndirectDebugValue()) |
| 1422 | return false; |
| 1423 | |
| 1424 | // Only consider parameters that are described using registers. Parameters |
| 1425 | // that are passed on the stack are not yet supported, so ignore debug |
| 1426 | // values that are described by the frame or stack pointer. |
| 1427 | if (!isRegOtherThanSPAndFP(MI.getOperand(0), MI, TRI)) |
| 1428 | return false; |
| 1429 | |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 1430 | // If a parameter's value has been propagated from the caller, then the |
| 1431 | // parameter's DBG_VALUE may be described using a register defined by some |
| 1432 | // instruction in the entry block, in which case we shouldn't create an |
| 1433 | // entry value. |
| 1434 | if (DefinedRegs.count(MI.getOperand(0).getReg())) |
| 1435 | return false; |
| 1436 | |
| David Stenberg | 5c7cc6f | 2019-12-13 10:37:53 +0100 | [diff] [blame] | 1437 | // TODO: Add support for parameters that have a pre-existing debug expressions |
| 1438 | // (e.g. fragments, or indirect parameters using DW_OP_deref). |
| 1439 | if (MI.getDebugExpression()->getNumElements() > 0) |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 1440 | return false; |
| 1441 | |
| 1442 | return true; |
| 1443 | } |
| 1444 | |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 1445 | /// Collect all register defines (including aliases) for the given instruction. |
| 1446 | static void collectRegDefs(const MachineInstr &MI, DefinedRegsSet &Regs, |
| 1447 | const TargetRegisterInfo *TRI) { |
| 1448 | for (const MachineOperand &MO : MI.operands()) |
| 1449 | if (MO.isReg() && MO.isDef() && MO.getReg()) |
| 1450 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI) |
| 1451 | Regs.insert(*AI); |
| 1452 | } |
| 1453 | |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1454 | /// This routine records the entry values of function parameters. The values |
| 1455 | /// could be used as backup values. If we loose the track of some unmodified |
| 1456 | /// parameters, the backup values will be used as a primary locations. |
| 1457 | void LiveDebugValues::recordEntryValue(const MachineInstr &MI, |
| 1458 | const DefinedRegsSet &DefinedRegs, |
| 1459 | OpenRangesSet &OpenRanges, |
| 1460 | VarLocMap &VarLocIDs) { |
| 1461 | if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) { |
| 1462 | auto &TM = TPC->getTM<TargetMachine>(); |
| Djordje Todorovic | 97ed706 | 2020-02-12 11:54:29 +0100 | [diff] [blame] | 1463 | if (!TM.Options.EnableDebugEntryValues) |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1464 | return; |
| 1465 | } |
| 1466 | |
| 1467 | DebugVariable V(MI.getDebugVariable(), MI.getDebugExpression(), |
| 1468 | MI.getDebugLoc()->getInlinedAt()); |
| 1469 | |
| 1470 | if (!isEntryValueCandidate(MI, DefinedRegs) || |
| 1471 | OpenRanges.getEntryValueBackup(V)) |
| 1472 | return; |
| 1473 | |
| 1474 | LLVM_DEBUG(dbgs() << "Creating the backup entry location: "; MI.dump();); |
| 1475 | |
| 1476 | // Create the entry value and use it as a backup location until it is |
| 1477 | // valid. It is valid until a parameter is not changed. |
| 1478 | DIExpression *NewExpr = |
| 1479 | DIExpression::prepend(MI.getDebugExpression(), DIExpression::EntryValue); |
| 1480 | VarLoc EntryValLocAsBackup = VarLoc::CreateEntryBackupLoc(MI, LS, NewExpr); |
| 1481 | unsigned EntryValLocID = VarLocIDs.insert(EntryValLocAsBackup); |
| 1482 | OpenRanges.insert(EntryValLocID, EntryValLocAsBackup); |
| 1483 | } |
| 1484 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1485 | /// Calculate the liveness information for the given machine function and |
| 1486 | /// extend ranges across basic blocks. |
| 1487 | bool LiveDebugValues::ExtendRanges(MachineFunction &MF) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1488 | LLVM_DEBUG(dbgs() << "\nDebug Range Extension\n"); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1489 | |
| 1490 | bool Changed = false; |
| Daniel Berlin | ca4d93a | 2016-01-10 03:25:42 +0000 | [diff] [blame] | 1491 | bool OLChanged = false; |
| 1492 | bool MBBJoined = false; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1493 | |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1494 | VarLocMap VarLocIDs; // Map VarLoc<>unique ID for use in bitvectors. |
| Djordje Todorovic | 8c99a54 | 2019-10-24 10:08:43 +0200 | [diff] [blame] | 1495 | OverlapMap OverlapFragments; // Map of overlapping variable fragments. |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1496 | OpenRangesSet OpenRanges(OverlapFragments); |
| 1497 | // Ranges that are open until end of bb. |
| 1498 | VarLocInMBB OutLocs; // Ranges that exist beyond bb. |
| 1499 | VarLocInMBB InLocs; // Ranges that are incoming after joining. |
| Djordje Todorovic | 8c99a54 | 2019-10-24 10:08:43 +0200 | [diff] [blame] | 1500 | TransferMap Transfers; // DBG_VALUEs associated with transfers (such as |
| 1501 | // spills, copies and restores). |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1502 | VarLocInMBB PendingInLocs; // Ranges that are incoming after joining, but |
| 1503 | // that we have deferred creating DBG_VALUE insts |
| 1504 | // for immediately. |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1505 | |
| 1506 | VarToFragments SeenFragments; |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1507 | |
| Vedant Kumar | 8c46668 | 2018-10-05 21:44:15 +0000 | [diff] [blame] | 1508 | // Blocks which are artificial, i.e. blocks which exclusively contain |
| 1509 | // instructions without locations, or with line 0 locations. |
| 1510 | SmallPtrSet<const MachineBasicBlock *, 16> ArtificialBlocks; |
| 1511 | |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1512 | DenseMap<unsigned int, MachineBasicBlock *> OrderToBB; |
| 1513 | DenseMap<MachineBasicBlock *, unsigned int> BBToOrder; |
| 1514 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1515 | std::greater<unsigned int>> |
| 1516 | Worklist; |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1517 | std::priority_queue<unsigned int, std::vector<unsigned int>, |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1518 | std::greater<unsigned int>> |
| 1519 | Pending; |
| 1520 | |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 1521 | // Set of register defines that are seen when traversing the entry block |
| 1522 | // looking for debug entry value candidates. |
| 1523 | DefinedRegsSet DefinedRegs; |
| 1524 | |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 1525 | // Only in the case of entry MBB collect DBG_VALUEs representing |
| 1526 | // function parameters in order to generate debug entry values for them. |
| David Stenberg | 4fec44c | 2019-11-13 10:36:13 +0100 | [diff] [blame] | 1527 | MachineBasicBlock &First_MBB = *(MF.begin()); |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 1528 | for (auto &MI : First_MBB) { |
| 1529 | collectRegDefs(MI, DefinedRegs, TRI); |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1530 | if (MI.isDebugValue()) |
| 1531 | recordEntryValue(MI, DefinedRegs, OpenRanges, VarLocIDs); |
| David Stenberg | 5e646ff | 2019-11-13 10:37:53 +0100 | [diff] [blame] | 1532 | } |
| Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame] | 1533 | |
| Jeremy Morse | 313d2ce | 2019-08-29 10:53:29 +0000 | [diff] [blame] | 1534 | // Initialize per-block structures and scan for fragment overlaps. |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1535 | for (auto &MBB : MF) { |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1536 | PendingInLocs[&MBB] = VarLocSet(); |
| Jeremy Morse | 313d2ce | 2019-08-29 10:53:29 +0000 | [diff] [blame] | 1537 | |
| 1538 | for (auto &MI : MBB) { |
| 1539 | if (MI.isDebugValue()) |
| 1540 | accumulateFragmentMap(MI, SeenFragments, OverlapFragments); |
| 1541 | } |
| Jeremy Morse | bf2b2f0 | 2019-06-13 12:51:57 +0000 | [diff] [blame] | 1542 | } |
| Adrian Prantl | 6ee02c7 | 2016-05-25 22:21:12 +0000 | [diff] [blame] | 1543 | |
| Vedant Kumar | 8c46668 | 2018-10-05 21:44:15 +0000 | [diff] [blame] | 1544 | auto hasNonArtificialLocation = [](const MachineInstr &MI) -> bool { |
| 1545 | if (const DebugLoc &DL = MI.getDebugLoc()) |
| 1546 | return DL.getLine() != 0; |
| 1547 | return false; |
| 1548 | }; |
| 1549 | for (auto &MBB : MF) |
| 1550 | if (none_of(MBB.instrs(), hasNonArtificialLocation)) |
| 1551 | ArtificialBlocks.insert(&MBB); |
| 1552 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1553 | LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, |
| 1554 | "OutLocs after initialization", dbgs())); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1555 | |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1556 | ReversePostOrderTraversal<MachineFunction *> RPOT(&MF); |
| 1557 | unsigned int RPONumber = 0; |
| 1558 | for (auto RI = RPOT.begin(), RE = RPOT.end(); RI != RE; ++RI) { |
| 1559 | OrderToBB[RPONumber] = *RI; |
| 1560 | BBToOrder[*RI] = RPONumber; |
| 1561 | Worklist.push(RPONumber); |
| 1562 | ++RPONumber; |
| 1563 | } |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1564 | // This is a standard "union of predecessor outs" dataflow problem. |
| Petar Jovanovic | be2e80a | 2018-07-13 08:24:26 +0000 | [diff] [blame] | 1565 | // To solve it, we perform join() and process() using the two worklist method |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1566 | // until the ranges converge. |
| 1567 | // Ranges have converged when both worklists are empty. |
| Keith Walker | 83ebef5 | 2016-09-27 16:46:07 +0000 | [diff] [blame] | 1568 | SmallPtrSet<const MachineBasicBlock *, 16> Visited; |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1569 | while (!Worklist.empty() || !Pending.empty()) { |
| 1570 | // We track what is on the pending worklist to avoid inserting the same |
| 1571 | // thing twice. We could avoid this with a custom priority queue, but this |
| 1572 | // is probably not worth it. |
| 1573 | SmallPtrSet<MachineBasicBlock *, 16> OnPending; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1574 | LLVM_DEBUG(dbgs() << "Processing Worklist\n"); |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1575 | while (!Worklist.empty()) { |
| 1576 | MachineBasicBlock *MBB = OrderToBB[Worklist.top()]; |
| 1577 | Worklist.pop(); |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1578 | MBBJoined = join(*MBB, OutLocs, InLocs, VarLocIDs, Visited, |
| 1579 | ArtificialBlocks, PendingInLocs); |
| Jeremy Morse | 313d2ce | 2019-08-29 10:53:29 +0000 | [diff] [blame] | 1580 | MBBJoined |= Visited.insert(MBB).second; |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1581 | if (MBBJoined) { |
| 1582 | MBBJoined = false; |
| 1583 | Changed = true; |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1584 | // Now that we have started to extend ranges across BBs we need to |
| Djordje Todorovic | 8c99a54 | 2019-10-24 10:08:43 +0200 | [diff] [blame] | 1585 | // examine spill, copy and restore instructions to see whether they |
| 1586 | // operate with registers that correspond to user variables. |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1587 | // First load any pending inlocs. |
| 1588 | OpenRanges.insertFromLocSet(PendingInLocs[MBB], VarLocIDs); |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1589 | for (auto &MI : *MBB) |
| Djordje Todorovic | 4b4ede4 | 2019-12-03 14:18:02 +0100 | [diff] [blame] | 1590 | process(MI, OpenRanges, VarLocIDs, Transfers); |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1591 | OLChanged |= transferTerminator(MBB, OpenRanges, OutLocs, VarLocIDs); |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1592 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1593 | LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, |
| 1594 | "OutLocs after propagating", dbgs())); |
| 1595 | LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, |
| 1596 | "InLocs after propagating", dbgs())); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1597 | |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1598 | if (OLChanged) { |
| 1599 | OLChanged = false; |
| 1600 | for (auto s : MBB->successors()) |
| Benjamin Kramer | 4dea8f5 | 2016-06-17 18:59:41 +0000 | [diff] [blame] | 1601 | if (OnPending.insert(s).second) { |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1602 | Pending.push(BBToOrder[s]); |
| 1603 | } |
| 1604 | } |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1605 | } |
| 1606 | } |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1607 | Worklist.swap(Pending); |
| 1608 | // At this point, pending must be empty, since it was just the empty |
| 1609 | // worklist |
| 1610 | assert(Pending.empty() && "Pending should be empty"); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1611 | } |
| Daniel Berlin | 7256059 | 2016-01-10 18:08:32 +0000 | [diff] [blame] | 1612 | |
| Jeremy Morse | 0ca48de2 | 2019-10-04 09:38:05 +0000 | [diff] [blame] | 1613 | // Add any DBG_VALUE instructions created by location transfers. |
| 1614 | for (auto &TR : Transfers) { |
| Jeremy Morse | 61800a7 | 2019-10-04 10:53:47 +0000 | [diff] [blame] | 1615 | MachineBasicBlock *MBB = TR.TransferInst->getParent(); |
| 1616 | const VarLoc &VL = VarLocIDs[TR.LocationID]; |
| 1617 | MachineInstr *MI = VL.BuildDbgValue(MF); |
| 1618 | MBB->insertAfterBundle(TR.TransferInst->getIterator(), MI); |
| Jeremy Morse | 0ca48de2 | 2019-10-04 09:38:05 +0000 | [diff] [blame] | 1619 | } |
| 1620 | Transfers.clear(); |
| 1621 | |
| Jeremy Morse | 67443c3 | 2019-08-21 09:22:31 +0000 | [diff] [blame] | 1622 | // Deferred inlocs will not have had any DBG_VALUE insts created; do |
| 1623 | // that now. |
| 1624 | flushPendingLocs(PendingInLocs, VarLocIDs); |
| 1625 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1626 | LLVM_DEBUG(printVarLocInMBB(MF, OutLocs, VarLocIDs, "Final OutLocs", dbgs())); |
| 1627 | LLVM_DEBUG(printVarLocInMBB(MF, InLocs, VarLocIDs, "Final InLocs", dbgs())); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1628 | return Changed; |
| 1629 | } |
| 1630 | |
| 1631 | bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1632 | if (!MF.getFunction().getSubprogram()) |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 1633 | // LiveDebugValues will already have removed all DBG_VALUEs. |
| 1634 | return false; |
| 1635 | |
| Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 1636 | // Skip functions from NoDebug compilation units. |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1637 | if (MF.getFunction().getSubprogram()->getUnit()->getEmissionKind() == |
| Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 1638 | DICompileUnit::NoDebug) |
| 1639 | return false; |
| 1640 | |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1641 | TRI = MF.getSubtarget().getRegisterInfo(); |
| 1642 | TII = MF.getSubtarget().getInstrInfo(); |
| Wolfgang Pieb | 399dcfa | 2017-02-14 19:08:45 +0000 | [diff] [blame] | 1643 | TFI = MF.getSubtarget().getFrameLowering(); |
| Sander de Smalen | d6a7da8 | 2019-10-29 12:49:34 +0000 | [diff] [blame] | 1644 | TFI->getCalleeSaves(MF, CalleeSavedRegs); |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 1645 | LS.initialize(MF); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1646 | |
| Adrian Prantl | 7f5866c | 2016-09-28 17:51:14 +0000 | [diff] [blame] | 1647 | bool Changed = ExtendRanges(MF); |
| Vikram TV | 859ad29 | 2015-12-16 11:09:48 +0000 | [diff] [blame] | 1648 | return Changed; |
| 1649 | } |