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