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