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