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