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