Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 1 | //===- LiveDebugVariables.cpp - Tracking debug info variables -------------===// |
| 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 file implements the LiveDebugVariables analysis. |
| 11 | // |
| 12 | // Remove all DBG_VALUE instructions referencing virtual registers and replace |
| 13 | // them with a data structure tracking where live user variables are kept - in a |
| 14 | // virtual register or in a stack slot. |
| 15 | // |
| 16 | // Allow the data structure to be updated during register allocation when values |
| 17 | // are moved between registers and stack slots. Finally emit new DBG_VALUE |
| 18 | // instructions after register allocation is complete. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "LiveDebugVariables.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/ArrayRef.h" |
| 24 | #include "llvm/ADT/DenseMap.h" |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/IntervalMap.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Devang Patel | b456866 | 2011-08-04 18:45:38 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringRef.h" |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/LexicalScopes.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/LiveInterval.h" |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineInstr.h" |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineOperand.h" |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/SlotIndexes.h" |
Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/VirtRegMap.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 43 | #include "llvm/IR/DebugInfoMetadata.h" |
| 44 | #include "llvm/IR/DebugLoc.h" |
| 45 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Metadata.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 47 | #include "llvm/MC/MCRegisterInfo.h" |
| 48 | #include "llvm/Pass.h" |
| 49 | #include "llvm/Support/Casting.h" |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 50 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Compiler.h" |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 53 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 54 | #include "llvm/Target/TargetInstrInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 55 | #include "llvm/Target/TargetOpcodes.h" |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 56 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetSubtargetInfo.h" |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 58 | #include <algorithm> |
| 59 | #include <cassert> |
| 60 | #include <iterator> |
David Blaikie | 2b1dfa7 | 2014-04-21 20:37:07 +0000 | [diff] [blame] | 61 | #include <memory> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 62 | #include <utility> |
David Blaikie | 2b1dfa7 | 2014-04-21 20:37:07 +0000 | [diff] [blame] | 63 | |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 64 | using namespace llvm; |
| 65 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 66 | #define DEBUG_TYPE "livedebugvars" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 67 | |
Devang Patel | acbee0b | 2011-01-07 22:33:41 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
Jakob Stoklund Olesen | 74ded57 | 2011-01-12 23:36:21 +0000 | [diff] [blame] | 69 | EnableLDV("live-debug-variables", cl::init(true), |
Devang Patel | acbee0b | 2011-01-07 22:33:41 +0000 | [diff] [blame] | 70 | cl::desc("Enable the live debug variables pass"), cl::Hidden); |
| 71 | |
Devang Patel | b456866 | 2011-08-04 18:45:38 +0000 | [diff] [blame] | 72 | STATISTIC(NumInsertedDebugValues, "Number of DBG_VALUEs inserted"); |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 73 | |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 74 | char LiveDebugVariables::ID = 0; |
| 75 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 76 | INITIALIZE_PASS_BEGIN(LiveDebugVariables, DEBUG_TYPE, |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 77 | "Debug Variable Analysis", false, false) |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 78 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 79 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 80 | INITIALIZE_PASS_END(LiveDebugVariables, DEBUG_TYPE, |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 81 | "Debug Variable Analysis", false, false) |
| 82 | |
| 83 | void LiveDebugVariables::getAnalysisUsage(AnalysisUsage &AU) const { |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 84 | AU.addRequired<MachineDominatorTree>(); |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 85 | AU.addRequiredTransitive<LiveIntervals>(); |
| 86 | AU.setPreservesAll(); |
| 87 | MachineFunctionPass::getAnalysisUsage(AU); |
| 88 | } |
| 89 | |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 90 | LiveDebugVariables::LiveDebugVariables() : MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 91 | initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); |
| 92 | } |
| 93 | |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 94 | enum : unsigned { UndefLocNo = ~0U }; |
| 95 | |
| 96 | /// Describes a location by number along with some flags about the original |
| 97 | /// usage of the location. |
| 98 | class DbgValueLocation { |
| 99 | public: |
| 100 | DbgValueLocation(unsigned LocNo, bool WasIndirect) |
| 101 | : LocNo(LocNo), WasIndirect(WasIndirect) { |
| 102 | static_assert(sizeof(*this) == sizeof(unsigned), "bad bitfield packing"); |
| 103 | assert(locNo() == LocNo && "location truncation"); |
| 104 | } |
| 105 | |
| 106 | DbgValueLocation() : LocNo(0), WasIndirect(0) {} |
| 107 | |
| 108 | unsigned locNo() const { |
| 109 | // Fix up the undef location number, which gets truncated. |
| 110 | return LocNo == INT_MAX ? UndefLocNo : LocNo; |
| 111 | } |
| 112 | bool wasIndirect() const { return WasIndirect; } |
| 113 | bool isUndef() const { return locNo() == UndefLocNo; } |
| 114 | |
| 115 | DbgValueLocation changeLocNo(unsigned NewLocNo) const { |
| 116 | return DbgValueLocation(NewLocNo, WasIndirect); |
| 117 | } |
| 118 | |
Reid Kleckner | b4569de7 | 2017-10-03 18:30:11 +0000 | [diff] [blame^] | 119 | friend inline bool operator==(const DbgValueLocation &LHS, |
| 120 | const DbgValueLocation &RHS) { |
| 121 | return LHS.LocNo == RHS.LocNo && LHS.WasIndirect == RHS.WasIndirect; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 122 | } |
Reid Kleckner | b4569de7 | 2017-10-03 18:30:11 +0000 | [diff] [blame^] | 123 | |
| 124 | friend inline bool operator!=(const DbgValueLocation &LHS, |
| 125 | const DbgValueLocation &RHS) { |
| 126 | return !(LHS == RHS); |
| 127 | } |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | unsigned LocNo : 31; |
| 131 | unsigned WasIndirect : 1; |
| 132 | }; |
| 133 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 134 | /// LocMap - Map of where a user value is live, and its location. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 135 | using LocMap = IntervalMap<SlotIndex, DbgValueLocation, 4>; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 136 | |
| 137 | namespace { |
| 138 | |
| 139 | class LDVImpl; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 140 | |
| 141 | /// UserValue - A user value is a part of a debug info user variable. |
| 142 | /// |
| 143 | /// A DBG_VALUE instruction notes that (a sub-register of) a virtual register |
| 144 | /// holds part of a user variable. The part is identified by a byte offset. |
| 145 | /// |
| 146 | /// UserValues are grouped into equivalence classes for easier searching. Two |
| 147 | /// user values are related if they refer to the same variable, or if they are |
| 148 | /// held by the same virtual register. The equivalence class is the transitive |
| 149 | /// closure of that relation. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 150 | class UserValue { |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 151 | const DILocalVariable *Variable; ///< The debug info variable we are part of. |
| 152 | const DIExpression *Expression; ///< Any complex address expression. |
Devang Patel | 26ffa01 | 2011-02-04 01:43:25 +0000 | [diff] [blame] | 153 | DebugLoc dl; ///< The debug location for the variable. This is |
| 154 | ///< used by dwarf writer to find lexical scope. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 155 | UserValue *leader; ///< Equivalence class leader. |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 156 | UserValue *next = nullptr; ///< Next value in equivalence class, or null. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 157 | |
| 158 | /// Numbered locations referenced by locmap. |
Jakob Stoklund Olesen | 9adf5e0 | 2011-01-09 05:33:21 +0000 | [diff] [blame] | 159 | SmallVector<MachineOperand, 4> locations; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 160 | |
| 161 | /// Map of slot indices where this value is live. |
| 162 | LocMap locInts; |
| 163 | |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 164 | /// Set of interval start indexes that have been trimmed to the |
| 165 | /// lexical scope. |
| 166 | SmallSet<SlotIndex, 2> trimmedDefs; |
| 167 | |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 168 | /// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 169 | void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 170 | DbgValueLocation Loc, bool Spilled, LiveIntervals &LIS, |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 171 | const TargetInstrInfo &TII); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 172 | |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 173 | /// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs |
| 174 | /// is live. Returns true if any changes were made. |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 175 | bool splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, |
| 176 | LiveIntervals &LIS); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 177 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 178 | public: |
| 179 | /// UserValue - Create a new UserValue. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 180 | UserValue(const DILocalVariable *var, const DIExpression *expr, DebugLoc L, |
| 181 | LocMap::Allocator &alloc) |
| 182 | : Variable(var), Expression(expr), dl(std::move(L)), leader(this), |
| 183 | locInts(alloc) {} |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 184 | |
| 185 | /// getLeader - Get the leader of this value's equivalence class. |
| 186 | UserValue *getLeader() { |
| 187 | UserValue *l = leader; |
| 188 | while (l != l->leader) |
| 189 | l = l->leader; |
| 190 | return leader = l; |
| 191 | } |
| 192 | |
| 193 | /// getNext - Return the next UserValue in the equivalence class. |
| 194 | UserValue *getNext() const { return next; } |
| 195 | |
Devang Patel | 338e432 | 2011-07-06 23:09:51 +0000 | [diff] [blame] | 196 | /// match - Does this UserValue match the parameters? |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 197 | bool match(const DILocalVariable *Var, const DIExpression *Expr, |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 198 | const DILocation *IA) const { |
| 199 | // FIXME: The fragment should be part of the equivalence class, but not |
| 200 | // other things in the expression like stack values. |
| 201 | return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /// merge - Merge equivalence classes. |
| 205 | static UserValue *merge(UserValue *L1, UserValue *L2) { |
| 206 | L2 = L2->getLeader(); |
| 207 | if (!L1) |
| 208 | return L2; |
| 209 | L1 = L1->getLeader(); |
| 210 | if (L1 == L2) |
| 211 | return L1; |
| 212 | // Splice L2 before L1's members. |
| 213 | UserValue *End = L2; |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 214 | while (End->next) { |
| 215 | End->leader = L1; |
| 216 | End = End->next; |
| 217 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 218 | End->leader = L1; |
| 219 | End->next = L1->next; |
| 220 | L1->next = L2; |
| 221 | return L1; |
| 222 | } |
| 223 | |
| 224 | /// getLocationNo - Return the location number that matches Loc. |
Jakob Stoklund Olesen | 9adf5e0 | 2011-01-09 05:33:21 +0000 | [diff] [blame] | 225 | unsigned getLocationNo(const MachineOperand &LocMO) { |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 226 | if (LocMO.isReg()) { |
| 227 | if (LocMO.getReg() == 0) |
Reid Kleckner | eed0973 | 2017-09-15 22:08:50 +0000 | [diff] [blame] | 228 | return UndefLocNo; |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 229 | // For register locations we dont care about use/def and other flags. |
| 230 | for (unsigned i = 0, e = locations.size(); i != e; ++i) |
| 231 | if (locations[i].isReg() && |
| 232 | locations[i].getReg() == LocMO.getReg() && |
| 233 | locations[i].getSubReg() == LocMO.getSubReg()) |
| 234 | return i; |
| 235 | } else |
| 236 | for (unsigned i = 0, e = locations.size(); i != e; ++i) |
| 237 | if (LocMO.isIdenticalTo(locations[i])) |
| 238 | return i; |
Jakob Stoklund Olesen | 9adf5e0 | 2011-01-09 05:33:21 +0000 | [diff] [blame] | 239 | locations.push_back(LocMO); |
| 240 | // We are storing a MachineOperand outside a MachineInstr. |
| 241 | locations.back().clearParent(); |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 242 | // Don't store def operands. |
| 243 | if (locations.back().isReg()) |
| 244 | locations.back().setIsUse(); |
Jakob Stoklund Olesen | 9adf5e0 | 2011-01-09 05:33:21 +0000 | [diff] [blame] | 245 | return locations.size() - 1; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 248 | /// mapVirtRegs - Ensure that all virtual register locations are mapped. |
| 249 | void mapVirtRegs(LDVImpl *LDV); |
| 250 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 251 | /// addDef - Add a definition point to this value. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 252 | void addDef(SlotIndex Idx, const MachineOperand &LocMO, bool IsIndirect) { |
| 253 | DbgValueLocation Loc(getLocationNo(LocMO), IsIndirect); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 254 | // Add a singular (Idx,Idx) -> Loc mapping. |
| 255 | LocMap::iterator I = locInts.find(Idx); |
| 256 | if (!I.valid() || I.start() != Idx) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 257 | I.insert(Idx, Idx.getNextSlot(), Loc); |
Jakob Stoklund Olesen | 2539af6 | 2011-08-03 23:44:31 +0000 | [diff] [blame] | 258 | else |
| 259 | // A later DBG_VALUE at the same SlotIndex overrides the old location. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 260 | I.setValue(Loc); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Adrian Prantl | f8d10ce | 2016-09-28 21:34:23 +0000 | [diff] [blame] | 263 | /// extendDef - Extend the current definition as far as possible down. |
| 264 | /// Stop when meeting an existing def or when leaving the live |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 265 | /// range of VNI. |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 266 | /// End points where VNI is no longer live are added to Kills. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 267 | /// @param Idx Starting point for the definition. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 268 | /// @param Loc Location number to propagate. |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 269 | /// @param LR Restrict liveness to where LR has the value VNI. May be null. |
| 270 | /// @param VNI When LR is not null, this is the value to restrict to. |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 271 | /// @param Kills Append end points of VNI's live range to Kills. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 272 | /// @param LIS Live intervals analysis. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 273 | void extendDef(SlotIndex Idx, DbgValueLocation Loc, |
Matthias Braun | 34e1be9 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 274 | LiveRange *LR, const VNInfo *VNI, |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 275 | SmallVectorImpl<SlotIndex> *Kills, |
Adrian Prantl | f8d10ce | 2016-09-28 21:34:23 +0000 | [diff] [blame] | 276 | LiveIntervals &LIS); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 277 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 278 | /// addDefsFromCopies - The value in LI/LocNo may be copies to other |
| 279 | /// registers. Determine if any of the copies are available at the kill |
| 280 | /// points, and add defs if possible. |
| 281 | /// @param LI Scan for copies of the value in LI->reg. |
| 282 | /// @param LocNo Location number of LI->reg. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 283 | /// @param WasIndirect Indicates if the original use of LI->reg was indirect |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 284 | /// @param Kills Points where the range of LocNo could be extended. |
| 285 | /// @param NewDefs Append (Idx, LocNo) of inserted defs here. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 286 | void addDefsFromCopies( |
| 287 | LiveInterval *LI, unsigned LocNo, bool WasIndirect, |
| 288 | const SmallVectorImpl<SlotIndex> &Kills, |
| 289 | SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs, |
| 290 | MachineRegisterInfo &MRI, LiveIntervals &LIS); |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 291 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 292 | /// computeIntervals - Compute the live intervals of all locations after |
| 293 | /// collecting all their def points. |
Jakob Stoklund Olesen | 3244963 | 2012-06-22 17:15:32 +0000 | [diff] [blame] | 294 | void computeIntervals(MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 295 | LiveIntervals &LIS, LexicalScopes &LS); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 296 | |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 297 | /// splitRegister - Replace OldReg ranges with NewRegs ranges where NewRegs is |
| 298 | /// live. Returns true if any changes were made. |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 299 | bool splitRegister(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, |
| 300 | LiveIntervals &LIS); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 301 | |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 302 | /// rewriteLocations - Rewrite virtual register locations according to the |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 303 | /// provided virtual register map. Record which locations were spilled. |
| 304 | void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI, |
| 305 | BitVector &SpilledLocations); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 306 | |
Eric Christopher | bc67170 | 2013-02-13 02:29:18 +0000 | [diff] [blame] | 307 | /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 308 | void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, |
| 309 | const TargetInstrInfo &TRI, |
| 310 | const BitVector &SpilledLocations); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 311 | |
Devang Patel | f9e2ae9 | 2011-09-13 18:40:53 +0000 | [diff] [blame] | 312 | /// getDebugLoc - Return DebugLoc of this UserValue. |
| 313 | DebugLoc getDebugLoc() { return dl;} |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 314 | |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 315 | void print(raw_ostream &, const TargetRegisterInfo *); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 316 | }; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 317 | |
| 318 | /// LDVImpl - Implementation of the LiveDebugVariables pass. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 319 | class LDVImpl { |
| 320 | LiveDebugVariables &pass; |
| 321 | LocMap::Allocator allocator; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 322 | MachineFunction *MF = nullptr; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 323 | LiveIntervals *LIS; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 324 | const TargetRegisterInfo *TRI; |
| 325 | |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 326 | /// Whether emitDebugValues is called. |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 327 | bool EmitDone = false; |
| 328 | |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 329 | /// Whether the machine function is modified during the pass. |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 330 | bool ModifiedMF = false; |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 331 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 332 | /// userValues - All allocated UserValue instances. |
David Blaikie | 2b1dfa7 | 2014-04-21 20:37:07 +0000 | [diff] [blame] | 333 | SmallVector<std::unique_ptr<UserValue>, 8> userValues; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 334 | |
| 335 | /// Map virtual register to eq class leader. |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 336 | using VRMap = DenseMap<unsigned, UserValue *>; |
Jakob Stoklund Olesen | 922e1fa | 2010-12-03 22:25:09 +0000 | [diff] [blame] | 337 | VRMap virtRegToEqClass; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 338 | |
| 339 | /// Map user variable to eq class leader. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 340 | using UVMap = DenseMap<const DILocalVariable *, UserValue *>; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 341 | UVMap userVarMap; |
| 342 | |
| 343 | /// getUserValue - Find or create a UserValue. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 344 | UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr, |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 345 | const DebugLoc &DL); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 346 | |
Jakob Stoklund Olesen | 9ec2011 | 2010-12-02 18:15:44 +0000 | [diff] [blame] | 347 | /// lookupVirtReg - Find the EC leader for VirtReg or null. |
| 348 | UserValue *lookupVirtReg(unsigned VirtReg); |
| 349 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 350 | /// handleDebugValue - Add DBG_VALUE instruction to our maps. |
| 351 | /// @param MI DBG_VALUE instruction |
| 352 | /// @param Idx Last valid SLotIndex before instruction. |
| 353 | /// @return True if the DBG_VALUE instruction should be deleted. |
Duncan P. N. Exon Smith | fb612ac | 2016-06-30 23:13:38 +0000 | [diff] [blame] | 354 | bool handleDebugValue(MachineInstr &MI, SlotIndex Idx); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 355 | |
| 356 | /// collectDebugValues - Collect and erase all DBG_VALUE instructions, adding |
| 357 | /// a UserValue def for each instruction. |
| 358 | /// @param mf MachineFunction to be scanned. |
| 359 | /// @return True if any debug values were found. |
| 360 | bool collectDebugValues(MachineFunction &mf); |
| 361 | |
| 362 | /// computeIntervals - Compute the live intervals of all user values after |
| 363 | /// collecting all their def points. |
| 364 | void computeIntervals(); |
| 365 | |
| 366 | public: |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 367 | LDVImpl(LiveDebugVariables *ps) : pass(*ps) {} |
| 368 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 369 | bool runOnMachineFunction(MachineFunction &mf); |
| 370 | |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 371 | /// clear - Release all memory. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 372 | void clear() { |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 373 | MF = nullptr; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 374 | userValues.clear(); |
Jakob Stoklund Olesen | 922e1fa | 2010-12-03 22:25:09 +0000 | [diff] [blame] | 375 | virtRegToEqClass.clear(); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 376 | userVarMap.clear(); |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 377 | // Make sure we call emitDebugValues if the machine function was modified. |
| 378 | assert((!ModifiedMF || EmitDone) && |
| 379 | "Dbg values are not emitted in LDV"); |
| 380 | EmitDone = false; |
| 381 | ModifiedMF = false; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 384 | /// mapVirtReg - Map virtual register to an equivalence class. |
| 385 | void mapVirtReg(unsigned VirtReg, UserValue *EC); |
| 386 | |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 387 | /// splitRegister - Replace all references to OldReg with NewRegs. |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 388 | void splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 389 | |
Eric Christopher | bc67170 | 2013-02-13 02:29:18 +0000 | [diff] [blame] | 390 | /// emitDebugValues - Recreate DBG_VALUE instruction from data structures. |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 391 | void emitDebugValues(VirtRegMap *VRM); |
| 392 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 393 | void print(raw_ostream&); |
| 394 | }; |
Eugene Zelenko | 5df3d89 | 2017-08-24 21:21:39 +0000 | [diff] [blame] | 395 | |
| 396 | } // end anonymous namespace |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 397 | |
Don Hinton | 53eb637 | 2017-09-27 21:19:56 +0000 | [diff] [blame] | 398 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 399 | static void printDebugLoc(const DebugLoc &DL, raw_ostream &CommentOS, |
Duncan P. N. Exon Smith | 32e7f28 | 2015-04-14 02:09:32 +0000 | [diff] [blame] | 400 | const LLVMContext &Ctx) { |
| 401 | if (!DL) |
| 402 | return; |
| 403 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 404 | auto *Scope = cast<DIScope>(DL.getScope()); |
Duncan P. N. Exon Smith | 32e7f28 | 2015-04-14 02:09:32 +0000 | [diff] [blame] | 405 | // Omit the directory, because it's likely to be long and uninteresting. |
Duncan P. N. Exon Smith | b273d06 | 2015-04-16 01:37:00 +0000 | [diff] [blame] | 406 | CommentOS << Scope->getFilename(); |
Duncan P. N. Exon Smith | 32e7f28 | 2015-04-14 02:09:32 +0000 | [diff] [blame] | 407 | CommentOS << ':' << DL.getLine(); |
| 408 | if (DL.getCol() != 0) |
| 409 | CommentOS << ':' << DL.getCol(); |
| 410 | |
| 411 | DebugLoc InlinedAtDL = DL.getInlinedAt(); |
| 412 | if (!InlinedAtDL) |
| 413 | return; |
| 414 | |
| 415 | CommentOS << " @[ "; |
| 416 | printDebugLoc(InlinedAtDL, CommentOS, Ctx); |
| 417 | CommentOS << " ]"; |
| 418 | } |
| 419 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 420 | static void printExtendedName(raw_ostream &OS, const DILocalVariable *V, |
| 421 | const DILocation *DL) { |
Duncan P. N. Exon Smith | 32e7f28 | 2015-04-14 02:09:32 +0000 | [diff] [blame] | 422 | const LLVMContext &Ctx = V->getContext(); |
| 423 | StringRef Res = V->getName(); |
| 424 | if (!Res.empty()) |
| 425 | OS << Res << "," << V->getLine(); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 426 | if (auto *InlinedAt = DL->getInlinedAt()) { |
Duncan P. N. Exon Smith | 32e7f28 | 2015-04-14 02:09:32 +0000 | [diff] [blame] | 427 | if (DebugLoc InlinedAtDL = InlinedAt) { |
| 428 | OS << " @["; |
| 429 | printDebugLoc(InlinedAtDL, OS, Ctx); |
| 430 | OS << "]"; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 435 | void UserValue::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 436 | auto *DV = cast<DILocalVariable>(Variable); |
Frederic Riss | e6bb187 | 2014-08-07 20:04:00 +0000 | [diff] [blame] | 437 | OS << "!\""; |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 438 | printExtendedName(OS, DV, dl); |
Duncan P. N. Exon Smith | 32e7f28 | 2015-04-14 02:09:32 +0000 | [diff] [blame] | 439 | |
Devang Patel | 6c1ed31 | 2011-08-09 01:03:35 +0000 | [diff] [blame] | 440 | OS << "\"\t"; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 441 | for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) { |
| 442 | OS << " [" << I.start() << ';' << I.stop() << "):"; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 443 | if (I.value().isUndef()) |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 444 | OS << "undef"; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 445 | else { |
| 446 | OS << I.value().locNo(); |
| 447 | if (I.value().wasIndirect()) |
| 448 | OS << " ind"; |
| 449 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 450 | } |
Jakob Stoklund Olesen | c86fe05 | 2011-05-06 17:59:59 +0000 | [diff] [blame] | 451 | for (unsigned i = 0, e = locations.size(); i != e; ++i) { |
| 452 | OS << " Loc" << i << '='; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 453 | locations[i].print(OS, TRI); |
Jakob Stoklund Olesen | c86fe05 | 2011-05-06 17:59:59 +0000 | [diff] [blame] | 454 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 455 | OS << '\n'; |
| 456 | } |
| 457 | |
| 458 | void LDVImpl::print(raw_ostream &OS) { |
| 459 | OS << "********** DEBUG VARIABLES **********\n"; |
| 460 | for (unsigned i = 0, e = userValues.size(); i != e; ++i) |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 461 | userValues[i]->print(OS, TRI); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 462 | } |
Florian Hahn | 6b3216a | 2017-07-31 10:07:49 +0000 | [diff] [blame] | 463 | #endif |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 464 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 465 | void UserValue::mapVirtRegs(LDVImpl *LDV) { |
| 466 | for (unsigned i = 0, e = locations.size(); i != e; ++i) |
| 467 | if (locations[i].isReg() && |
| 468 | TargetRegisterInfo::isVirtualRegister(locations[i].getReg())) |
| 469 | LDV->mapVirtReg(locations[i].getReg(), this); |
| 470 | } |
| 471 | |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 472 | UserValue *LDVImpl::getUserValue(const DILocalVariable *Var, |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 473 | const DIExpression *Expr, const DebugLoc &DL) { |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 474 | UserValue *&Leader = userVarMap[Var]; |
| 475 | if (Leader) { |
| 476 | UserValue *UV = Leader->getLeader(); |
| 477 | Leader = UV; |
| 478 | for (; UV; UV = UV->getNext()) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 479 | if (UV->match(Var, Expr, DL->getInlinedAt())) |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 480 | return UV; |
| 481 | } |
| 482 | |
David Blaikie | 2b1dfa7 | 2014-04-21 20:37:07 +0000 | [diff] [blame] | 483 | userValues.push_back( |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 484 | llvm::make_unique<UserValue>(Var, Expr, DL, allocator)); |
David Blaikie | 2b1dfa7 | 2014-04-21 20:37:07 +0000 | [diff] [blame] | 485 | UserValue *UV = userValues.back().get(); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 486 | Leader = UserValue::merge(Leader, UV); |
| 487 | return UV; |
| 488 | } |
| 489 | |
| 490 | void LDVImpl::mapVirtReg(unsigned VirtReg, UserValue *EC) { |
| 491 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && "Only map VirtRegs"); |
Jakob Stoklund Olesen | 922e1fa | 2010-12-03 22:25:09 +0000 | [diff] [blame] | 492 | UserValue *&Leader = virtRegToEqClass[VirtReg]; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 493 | Leader = UserValue::merge(Leader, EC); |
| 494 | } |
| 495 | |
Jakob Stoklund Olesen | 9ec2011 | 2010-12-02 18:15:44 +0000 | [diff] [blame] | 496 | UserValue *LDVImpl::lookupVirtReg(unsigned VirtReg) { |
Jakob Stoklund Olesen | 922e1fa | 2010-12-03 22:25:09 +0000 | [diff] [blame] | 497 | if (UserValue *UV = virtRegToEqClass.lookup(VirtReg)) |
Jakob Stoklund Olesen | 9ec2011 | 2010-12-02 18:15:44 +0000 | [diff] [blame] | 498 | return UV->getLeader(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 499 | return nullptr; |
Jakob Stoklund Olesen | 9ec2011 | 2010-12-02 18:15:44 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Duncan P. N. Exon Smith | fb612ac | 2016-06-30 23:13:38 +0000 | [diff] [blame] | 502 | bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) { |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 503 | // DBG_VALUE loc, offset, variable |
Duncan P. N. Exon Smith | fb612ac | 2016-06-30 23:13:38 +0000 | [diff] [blame] | 504 | if (MI.getNumOperands() != 4 || |
| 505 | !(MI.getOperand(1).isReg() || MI.getOperand(1).isImm()) || |
| 506 | !MI.getOperand(2).isMetadata()) { |
| 507 | DEBUG(dbgs() << "Can't handle " << MI); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 508 | return false; |
| 509 | } |
| 510 | |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 511 | // Get or create the UserValue for (variable,offset) here. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 512 | bool IsIndirect = MI.getOperand(1).isImm(); |
Adrian Prantl | b2811e5 | 2017-07-28 23:06:50 +0000 | [diff] [blame] | 513 | if (IsIndirect) |
| 514 | assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset"); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 515 | const DILocalVariable *Var = MI.getDebugVariable(); |
| 516 | const DIExpression *Expr = MI.getDebugExpression(); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 517 | UserValue *UV = |
| 518 | getUserValue(Var, Expr, MI.getDebugLoc()); |
| 519 | UV->addDef(Idx, MI.getOperand(0), IsIndirect); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 520 | return true; |
| 521 | } |
| 522 | |
| 523 | bool LDVImpl::collectDebugValues(MachineFunction &mf) { |
| 524 | bool Changed = false; |
| 525 | for (MachineFunction::iterator MFI = mf.begin(), MFE = mf.end(); MFI != MFE; |
| 526 | ++MFI) { |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 527 | MachineBasicBlock *MBB = &*MFI; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 528 | for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end(); |
| 529 | MBBI != MBBE;) { |
| 530 | if (!MBBI->isDebugValue()) { |
| 531 | ++MBBI; |
| 532 | continue; |
| 533 | } |
| 534 | // DBG_VALUE has no slot index, use the previous instruction instead. |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 535 | SlotIndex Idx = |
| 536 | MBBI == MBB->begin() |
| 537 | ? LIS->getMBBStartIdx(MBB) |
| 538 | : LIS->getInstructionIndex(*std::prev(MBBI)).getRegSlot(); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 539 | // Handle consecutive DBG_VALUE instructions with the same slot index. |
| 540 | do { |
Duncan P. N. Exon Smith | fb612ac | 2016-06-30 23:13:38 +0000 | [diff] [blame] | 541 | if (handleDebugValue(*MBBI, Idx)) { |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 542 | MBBI = MBB->erase(MBBI); |
| 543 | Changed = true; |
| 544 | } else |
| 545 | ++MBBI; |
| 546 | } while (MBBI != MBBE && MBBI->isDebugValue()); |
| 547 | } |
| 548 | } |
| 549 | return Changed; |
| 550 | } |
| 551 | |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 552 | /// We only propagate DBG_VALUES locally here. LiveDebugValues performs a |
| 553 | /// data-flow analysis to propagate them beyond basic block boundaries. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 554 | void UserValue::extendDef(SlotIndex Idx, DbgValueLocation Loc, LiveRange *LR, |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 555 | const VNInfo *VNI, SmallVectorImpl<SlotIndex> *Kills, |
Adrian Prantl | f8d10ce | 2016-09-28 21:34:23 +0000 | [diff] [blame] | 556 | LiveIntervals &LIS) { |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 557 | SlotIndex Start = Idx; |
| 558 | MachineBasicBlock *MBB = LIS.getMBBFromIndex(Start); |
| 559 | SlotIndex Stop = LIS.getMBBEndIdx(MBB); |
| 560 | LocMap::iterator I = locInts.find(Start); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 561 | |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 562 | // Limit to VNI's live range. |
| 563 | bool ToEnd = true; |
| 564 | if (LR && VNI) { |
| 565 | LiveInterval::Segment *Segment = LR->getSegmentContaining(Start); |
| 566 | if (!Segment || Segment->valno != VNI) { |
| 567 | if (Kills) |
| 568 | Kills->push_back(Start); |
| 569 | return; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 570 | } |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 571 | if (Segment->end < Stop) { |
| 572 | Stop = Segment->end; |
| 573 | ToEnd = false; |
| 574 | } |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 575 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 576 | |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 577 | // There could already be a short def at Start. |
| 578 | if (I.valid() && I.start() <= Start) { |
| 579 | // Stop when meeting a different location or an already extended interval. |
| 580 | Start = Start.getNextSlot(); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 581 | if (I.value() != Loc || I.stop() != Start) |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 582 | return; |
| 583 | // This is a one-slot placeholder. Just skip it. |
| 584 | ++I; |
| 585 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 586 | |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 587 | // Limited by the next def. |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 588 | if (I.valid() && I.start() < Stop) { |
| 589 | Stop = I.start(); |
| 590 | ToEnd = false; |
| 591 | } |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 592 | // Limited by VNI's live range. |
| 593 | else if (!ToEnd && Kills) |
| 594 | Kills->push_back(Stop); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 595 | |
Adrian Prantl | ce85813 | 2015-12-21 20:03:00 +0000 | [diff] [blame] | 596 | if (Start < Stop) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 597 | I.insert(Start, Stop, Loc); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 600 | void UserValue::addDefsFromCopies( |
| 601 | LiveInterval *LI, unsigned LocNo, bool WasIndirect, |
| 602 | const SmallVectorImpl<SlotIndex> &Kills, |
| 603 | SmallVectorImpl<std::pair<SlotIndex, DbgValueLocation>> &NewDefs, |
| 604 | MachineRegisterInfo &MRI, LiveIntervals &LIS) { |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 605 | if (Kills.empty()) |
| 606 | return; |
| 607 | // Don't track copies from physregs, there are too many uses. |
| 608 | if (!TargetRegisterInfo::isVirtualRegister(LI->reg)) |
| 609 | return; |
| 610 | |
| 611 | // Collect all the (vreg, valno) pairs that are copies of LI. |
| 612 | SmallVector<std::pair<LiveInterval*, const VNInfo*>, 8> CopyValues; |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 613 | for (MachineOperand &MO : MRI.use_nodbg_operands(LI->reg)) { |
| 614 | MachineInstr *MI = MO.getParent(); |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 615 | // Copies of the full value. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 616 | if (MO.getSubReg() || !MI->isCopy()) |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 617 | continue; |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 618 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 619 | |
Jakob Stoklund Olesen | ec0ac3c | 2011-03-22 22:33:08 +0000 | [diff] [blame] | 620 | // Don't follow copies to physregs. These are usually setting up call |
| 621 | // arguments, and the argument registers are always call clobbered. We are |
| 622 | // better off in the source register which could be a callee-saved register, |
| 623 | // or it could be spilled. |
| 624 | if (!TargetRegisterInfo::isVirtualRegister(DstReg)) |
| 625 | continue; |
| 626 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 627 | // Is LocNo extended to reach this copy? If not, another def may be blocking |
| 628 | // it, or we are looking at a wrong value of LI. |
Duncan P. N. Exon Smith | 3ac9cc6 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 629 | SlotIndex Idx = LIS.getInstructionIndex(*MI); |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 630 | LocMap::iterator I = locInts.find(Idx.getRegSlot(true)); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 631 | if (!I.valid() || I.value().locNo() != LocNo) |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 632 | continue; |
| 633 | |
| 634 | if (!LIS.hasInterval(DstReg)) |
| 635 | continue; |
| 636 | LiveInterval *DstLI = &LIS.getInterval(DstReg); |
Jakob Stoklund Olesen | 90b5e56 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 637 | const VNInfo *DstVNI = DstLI->getVNInfoAt(Idx.getRegSlot()); |
| 638 | assert(DstVNI && DstVNI->def == Idx.getRegSlot() && "Bad copy value"); |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 639 | CopyValues.push_back(std::make_pair(DstLI, DstVNI)); |
| 640 | } |
| 641 | |
| 642 | if (CopyValues.empty()) |
| 643 | return; |
| 644 | |
| 645 | DEBUG(dbgs() << "Got " << CopyValues.size() << " copies of " << *LI << '\n'); |
| 646 | |
| 647 | // Try to add defs of the copied values for each kill point. |
| 648 | for (unsigned i = 0, e = Kills.size(); i != e; ++i) { |
| 649 | SlotIndex Idx = Kills[i]; |
| 650 | for (unsigned j = 0, e = CopyValues.size(); j != e; ++j) { |
| 651 | LiveInterval *DstLI = CopyValues[j].first; |
| 652 | const VNInfo *DstVNI = CopyValues[j].second; |
| 653 | if (DstLI->getVNInfoAt(Idx) != DstVNI) |
| 654 | continue; |
| 655 | // Check that there isn't already a def at Idx |
| 656 | LocMap::iterator I = locInts.find(Idx); |
| 657 | if (I.valid() && I.start() <= Idx) |
| 658 | continue; |
| 659 | DEBUG(dbgs() << "Kill at " << Idx << " covered by valno #" |
| 660 | << DstVNI->id << " in " << *DstLI << '\n'); |
| 661 | MachineInstr *CopyMI = LIS.getInstructionFromIndex(DstVNI->def); |
| 662 | assert(CopyMI && CopyMI->isCopy() && "Bad copy value"); |
| 663 | unsigned LocNo = getLocationNo(CopyMI->getOperand(0)); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 664 | DbgValueLocation NewLoc(LocNo, WasIndirect); |
| 665 | I.insert(Idx, Idx.getNextSlot(), NewLoc); |
| 666 | NewDefs.push_back(std::make_pair(Idx, NewLoc)); |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 672 | void UserValue::computeIntervals(MachineRegisterInfo &MRI, |
| 673 | const TargetRegisterInfo &TRI, |
| 674 | LiveIntervals &LIS, LexicalScopes &LS) { |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 675 | SmallVector<std::pair<SlotIndex, DbgValueLocation>, 16> Defs; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 676 | |
| 677 | // Collect all defs to be extended (Skipping undefs). |
| 678 | for (LocMap::const_iterator I = locInts.begin(); I.valid(); ++I) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 679 | if (!I.value().isUndef()) |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 680 | Defs.push_back(std::make_pair(I.start(), I.value())); |
| 681 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 682 | // Extend all defs, and possibly add new ones along the way. |
| 683 | for (unsigned i = 0; i != Defs.size(); ++i) { |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 684 | SlotIndex Idx = Defs[i].first; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 685 | DbgValueLocation Loc = Defs[i].second; |
| 686 | const MachineOperand &LocMO = locations[Loc.locNo()]; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 687 | |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 688 | if (!LocMO.isReg()) { |
| 689 | extendDef(Idx, Loc, nullptr, nullptr, nullptr, LIS); |
Jakob Stoklund Olesen | 3244963 | 2012-06-22 17:15:32 +0000 | [diff] [blame] | 690 | continue; |
| 691 | } |
| 692 | |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 693 | // Register locations are constrained to where the register value is live. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 694 | if (TargetRegisterInfo::isVirtualRegister(LocMO.getReg())) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 695 | LiveInterval *LI = nullptr; |
| 696 | const VNInfo *VNI = nullptr; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 697 | if (LIS.hasInterval(LocMO.getReg())) { |
| 698 | LI = &LIS.getInterval(LocMO.getReg()); |
Jakob Stoklund Olesen | 48a1647 | 2012-06-22 18:51:35 +0000 | [diff] [blame] | 699 | VNI = LI->getVNInfoAt(Idx); |
| 700 | } |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 701 | SmallVector<SlotIndex, 16> Kills; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 702 | extendDef(Idx, Loc, LI, VNI, &Kills, LIS); |
Jakob Stoklund Olesen | 48a1647 | 2012-06-22 18:51:35 +0000 | [diff] [blame] | 703 | if (LI) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 704 | addDefsFromCopies(LI, Loc.locNo(), Loc.wasIndirect(), Kills, Defs, MRI, |
| 705 | LIS); |
Jakob Stoklund Olesen | 3244963 | 2012-06-22 17:15:32 +0000 | [diff] [blame] | 706 | continue; |
| 707 | } |
| 708 | |
Bjorn Pettersson | 715a5ef | 2017-09-28 13:10:06 +0000 | [diff] [blame] | 709 | // For physregs, we only mark the start slot idx. DwarfDebug will see it |
| 710 | // as if the DBG_VALUE is valid up until the end of the basic block, or |
| 711 | // the next def of the physical register. So we do not need to extend the |
| 712 | // range. It might actually happen that the DBG_VALUE is the last use of |
| 713 | // the physical register (e.g. if this is an unused input argument to a |
| 714 | // function). |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 717 | // Erase all the undefs. |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 718 | for (LocMap::iterator I = locInts.begin(); I.valid();) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 719 | if (I.value().isUndef()) |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 720 | I.erase(); |
| 721 | else |
| 722 | ++I; |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 723 | |
| 724 | // The computed intervals may extend beyond the range of the debug |
| 725 | // location's lexical scope. In this case, splitting of an interval |
| 726 | // can result in an interval outside of the scope being created, |
| 727 | // causing extra unnecessary DBG_VALUEs to be emitted. To prevent |
| 728 | // this, trim the intervals to the lexical scope. |
| 729 | |
| 730 | LexicalScope *Scope = LS.findLexicalScope(dl); |
| 731 | if (!Scope) |
| 732 | return; |
| 733 | |
| 734 | SlotIndex PrevEnd; |
| 735 | LocMap::iterator I = locInts.begin(); |
| 736 | |
| 737 | // Iterate over the lexical scope ranges. Each time round the loop |
| 738 | // we check the intervals for overlap with the end of the previous |
| 739 | // range and the start of the next. The first range is handled as |
| 740 | // a special case where there is no PrevEnd. |
| 741 | for (const InsnRange &Range : Scope->getRanges()) { |
| 742 | SlotIndex RStart = LIS.getInstructionIndex(*Range.first); |
| 743 | SlotIndex REnd = LIS.getInstructionIndex(*Range.second); |
| 744 | |
| 745 | // At the start of each iteration I has been advanced so that |
| 746 | // I.stop() >= PrevEnd. Check for overlap. |
| 747 | if (PrevEnd && I.start() < PrevEnd) { |
| 748 | SlotIndex IStop = I.stop(); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 749 | DbgValueLocation Loc = I.value(); |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 750 | |
| 751 | // Stop overlaps previous end - trim the end of the interval to |
| 752 | // the scope range. |
| 753 | I.setStopUnchecked(PrevEnd); |
| 754 | ++I; |
| 755 | |
| 756 | // If the interval also overlaps the start of the "next" (i.e. |
| 757 | // current) range create a new interval for the remainder (which |
| 758 | // may be further trimmed). |
| 759 | if (RStart < IStop) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 760 | I.insert(RStart, IStop, Loc); |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // Advance I so that I.stop() >= RStart, and check for overlap. |
| 764 | I.advanceTo(RStart); |
| 765 | if (!I.valid()) |
| 766 | return; |
| 767 | |
| 768 | if (I.start() < RStart) { |
| 769 | // Interval start overlaps range - trim to the scope range. |
| 770 | I.setStartUnchecked(RStart); |
| 771 | // Remember that this interval was trimmed. |
| 772 | trimmedDefs.insert(RStart); |
| 773 | } |
| 774 | |
| 775 | // The end of a lexical scope range is the last instruction in the |
| 776 | // range. To convert to an interval we need the index of the |
| 777 | // instruction after it. |
| 778 | REnd = REnd.getNextIndex(); |
| 779 | |
| 780 | // Advance I to first interval outside current range. |
| 781 | I.advanceTo(REnd); |
| 782 | if (!I.valid()) |
| 783 | return; |
| 784 | |
| 785 | PrevEnd = REnd; |
| 786 | } |
| 787 | |
| 788 | // Check for overlap with end of final range. |
| 789 | if (PrevEnd && I.start() < PrevEnd) |
| 790 | I.setStopUnchecked(PrevEnd); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | void LDVImpl::computeIntervals() { |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 794 | LexicalScopes LS; |
| 795 | LS.initialize(*MF); |
| 796 | |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 797 | for (unsigned i = 0, e = userValues.size(); i != e; ++i) { |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 798 | userValues[i]->computeIntervals(MF->getRegInfo(), *TRI, *LIS, LS); |
Jakob Stoklund Olesen | 816f5f4 | 2011-03-18 21:42:19 +0000 | [diff] [blame] | 799 | userValues[i]->mapVirtRegs(this); |
| 800 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | bool LDVImpl::runOnMachineFunction(MachineFunction &mf) { |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 804 | clear(); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 805 | MF = &mf; |
| 806 | LIS = &pass.getAnalysis<LiveIntervals>(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 807 | TRI = mf.getSubtarget().getRegisterInfo(); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 808 | DEBUG(dbgs() << "********** COMPUTING LIVE DEBUG VARIABLES: " |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 809 | << mf.getName() << " **********\n"); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 810 | |
| 811 | bool Changed = collectDebugValues(mf); |
| 812 | computeIntervals(); |
| 813 | DEBUG(print(dbgs())); |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 814 | ModifiedMF = Changed; |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 815 | return Changed; |
| 816 | } |
| 817 | |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 818 | static void removeDebugValues(MachineFunction &mf) { |
| 819 | for (MachineBasicBlock &MBB : mf) { |
| 820 | for (auto MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ) { |
| 821 | if (!MBBI->isDebugValue()) { |
| 822 | ++MBBI; |
| 823 | continue; |
| 824 | } |
| 825 | MBBI = MBB.erase(MBBI); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 830 | bool LiveDebugVariables::runOnMachineFunction(MachineFunction &mf) { |
Devang Patel | acbee0b | 2011-01-07 22:33:41 +0000 | [diff] [blame] | 831 | if (!EnableLDV) |
| 832 | return false; |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 833 | if (!mf.getFunction()->getSubprogram()) { |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 834 | removeDebugValues(mf); |
| 835 | return false; |
| 836 | } |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 837 | if (!pImpl) |
| 838 | pImpl = new LDVImpl(this); |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 839 | return static_cast<LDVImpl*>(pImpl)->runOnMachineFunction(mf); |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void LiveDebugVariables::releaseMemory() { |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 843 | if (pImpl) |
Jakob Stoklund Olesen | 4be0bd7 | 2010-12-02 00:37:37 +0000 | [diff] [blame] | 844 | static_cast<LDVImpl*>(pImpl)->clear(); |
| 845 | } |
| 846 | |
| 847 | LiveDebugVariables::~LiveDebugVariables() { |
| 848 | if (pImpl) |
| 849 | delete static_cast<LDVImpl*>(pImpl); |
Jakob Stoklund Olesen | d4900a6 | 2010-11-30 02:17:10 +0000 | [diff] [blame] | 850 | } |
Jakob Stoklund Olesen | 9ec2011 | 2010-12-02 18:15:44 +0000 | [diff] [blame] | 851 | |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 852 | //===----------------------------------------------------------------------===// |
| 853 | // Live Range Splitting |
| 854 | //===----------------------------------------------------------------------===// |
| 855 | |
| 856 | bool |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 857 | UserValue::splitLocation(unsigned OldLocNo, ArrayRef<unsigned> NewRegs, |
| 858 | LiveIntervals& LIS) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 859 | DEBUG({ |
| 860 | dbgs() << "Splitting Loc" << OldLocNo << '\t'; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 861 | print(dbgs(), nullptr); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 862 | }); |
| 863 | bool DidChange = false; |
| 864 | LocMap::iterator LocMapI; |
| 865 | LocMapI.setMap(locInts); |
| 866 | for (unsigned i = 0; i != NewRegs.size(); ++i) { |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 867 | LiveInterval *LI = &LIS.getInterval(NewRegs[i]); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 868 | if (LI->empty()) |
| 869 | continue; |
| 870 | |
| 871 | // Don't allocate the new LocNo until it is needed. |
Reid Kleckner | eed0973 | 2017-09-15 22:08:50 +0000 | [diff] [blame] | 872 | unsigned NewLocNo = UndefLocNo; |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 873 | |
| 874 | // Iterate over the overlaps between locInts and LI. |
| 875 | LocMapI.find(LI->beginIndex()); |
| 876 | if (!LocMapI.valid()) |
| 877 | continue; |
| 878 | LiveInterval::iterator LII = LI->advanceTo(LI->begin(), LocMapI.start()); |
| 879 | LiveInterval::iterator LIE = LI->end(); |
| 880 | while (LocMapI.valid() && LII != LIE) { |
| 881 | // At this point, we know that LocMapI.stop() > LII->start. |
| 882 | LII = LI->advanceTo(LII, LocMapI.start()); |
| 883 | if (LII == LIE) |
| 884 | break; |
| 885 | |
| 886 | // Now LII->end > LocMapI.start(). Do we have an overlap? |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 887 | if (LocMapI.value().locNo() == OldLocNo && LII->start < LocMapI.stop()) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 888 | // Overlapping correct location. Allocate NewLocNo now. |
Reid Kleckner | eed0973 | 2017-09-15 22:08:50 +0000 | [diff] [blame] | 889 | if (NewLocNo == UndefLocNo) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 890 | MachineOperand MO = MachineOperand::CreateReg(LI->reg, false); |
| 891 | MO.setSubReg(locations[OldLocNo].getSubReg()); |
| 892 | NewLocNo = getLocationNo(MO); |
| 893 | DidChange = true; |
| 894 | } |
| 895 | |
| 896 | SlotIndex LStart = LocMapI.start(); |
| 897 | SlotIndex LStop = LocMapI.stop(); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 898 | DbgValueLocation OldLoc = LocMapI.value(); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 899 | |
| 900 | // Trim LocMapI down to the LII overlap. |
| 901 | if (LStart < LII->start) |
| 902 | LocMapI.setStartUnchecked(LII->start); |
| 903 | if (LStop > LII->end) |
| 904 | LocMapI.setStopUnchecked(LII->end); |
| 905 | |
| 906 | // Change the value in the overlap. This may trigger coalescing. |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 907 | LocMapI.setValue(OldLoc.changeLocNo(NewLocNo)); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 908 | |
| 909 | // Re-insert any removed OldLocNo ranges. |
| 910 | if (LStart < LocMapI.start()) { |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 911 | LocMapI.insert(LStart, LocMapI.start(), OldLoc); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 912 | ++LocMapI; |
| 913 | assert(LocMapI.valid() && "Unexpected coalescing"); |
| 914 | } |
| 915 | if (LStop > LocMapI.stop()) { |
| 916 | ++LocMapI; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 917 | LocMapI.insert(LII->end, LStop, OldLoc); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 918 | --LocMapI; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // Advance to the next overlap. |
| 923 | if (LII->end < LocMapI.stop()) { |
| 924 | if (++LII == LIE) |
| 925 | break; |
| 926 | LocMapI.advanceTo(LII->start); |
| 927 | } else { |
| 928 | ++LocMapI; |
| 929 | if (!LocMapI.valid()) |
| 930 | break; |
| 931 | LII = LI->advanceTo(LII, LocMapI.start()); |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | // Finally, remove any remaining OldLocNo intervals and OldLocNo itself. |
| 937 | locations.erase(locations.begin() + OldLocNo); |
| 938 | LocMapI.goToBegin(); |
| 939 | while (LocMapI.valid()) { |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 940 | DbgValueLocation v = LocMapI.value(); |
| 941 | if (v.locNo() == OldLocNo) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 942 | DEBUG(dbgs() << "Erasing [" << LocMapI.start() << ';' |
| 943 | << LocMapI.stop() << ")\n"); |
| 944 | LocMapI.erase(); |
| 945 | } else { |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 946 | if (v.locNo() > OldLocNo) |
| 947 | LocMapI.setValueUnchecked(v.changeLocNo(v.locNo() - 1)); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 948 | ++LocMapI; |
| 949 | } |
| 950 | } |
| 951 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 952 | DEBUG({dbgs() << "Split result: \t"; print(dbgs(), nullptr);}); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 953 | return DidChange; |
| 954 | } |
| 955 | |
| 956 | bool |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 957 | UserValue::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, |
| 958 | LiveIntervals &LIS) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 959 | bool DidChange = false; |
Jakob Stoklund Olesen | 57c8f58 | 2011-05-06 19:31:19 +0000 | [diff] [blame] | 960 | // Split locations referring to OldReg. Iterate backwards so splitLocation can |
Eric Christopher | be153e6 | 2012-03-15 21:33:35 +0000 | [diff] [blame] | 961 | // safely erase unused locations. |
Jakob Stoklund Olesen | 57c8f58 | 2011-05-06 19:31:19 +0000 | [diff] [blame] | 962 | for (unsigned i = locations.size(); i ; --i) { |
| 963 | unsigned LocNo = i-1; |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 964 | const MachineOperand *Loc = &locations[LocNo]; |
| 965 | if (!Loc->isReg() || Loc->getReg() != OldReg) |
| 966 | continue; |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 967 | DidChange |= splitLocation(LocNo, NewRegs, LIS); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 968 | } |
| 969 | return DidChange; |
| 970 | } |
| 971 | |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 972 | void LDVImpl::splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 973 | bool DidChange = false; |
| 974 | for (UserValue *UV = lookupVirtReg(OldReg); UV; UV = UV->getNext()) |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 975 | DidChange |= UV->splitRegister(OldReg, NewRegs, *LIS); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 976 | |
| 977 | if (!DidChange) |
| 978 | return; |
| 979 | |
| 980 | // Map all of the new virtual registers. |
| 981 | UserValue *UV = lookupVirtReg(OldReg); |
| 982 | for (unsigned i = 0; i != NewRegs.size(); ++i) |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 983 | mapVirtReg(NewRegs[i], UV); |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | void LiveDebugVariables:: |
Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 987 | splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) { |
Jakob Stoklund Olesen | f8da028 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 988 | if (pImpl) |
| 989 | static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs); |
| 990 | } |
| 991 | |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 992 | void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI, |
| 993 | BitVector &SpilledLocations) { |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 994 | // Build a set of new locations with new numbers so we can coalesce our |
| 995 | // IntervalMap if two vreg intervals collapse to the same physical location. |
| 996 | // Use MapVector instead of SetVector because MapVector::insert returns the |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 997 | // position of the previously or newly inserted element. The boolean value |
| 998 | // tracks if the location was produced by a spill. |
| 999 | // FIXME: This will be problematic if we ever support direct and indirect |
| 1000 | // frame index locations, i.e. expressing both variables in memory and |
| 1001 | // 'int x, *px = &x'. The "spilled" bit must become part of the location. |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1002 | MapVector<MachineOperand, bool> NewLocations; |
| 1003 | SmallVector<unsigned, 4> LocNoMap(locations.size()); |
| 1004 | for (unsigned I = 0, E = locations.size(); I != E; ++I) { |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1005 | bool Spilled = false; |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1006 | MachineOperand Loc = locations[I]; |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1007 | // Only virtual registers are rewritten. |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1008 | if (Loc.isReg() && Loc.getReg() && |
| 1009 | TargetRegisterInfo::isVirtualRegister(Loc.getReg())) { |
| 1010 | unsigned VirtReg = Loc.getReg(); |
| 1011 | if (VRM.isAssignedReg(VirtReg) && |
| 1012 | TargetRegisterInfo::isPhysicalRegister(VRM.getPhys(VirtReg))) { |
| 1013 | // This can create a %noreg operand in rare cases when the sub-register |
| 1014 | // index is no longer available. That means the user value is in a |
| 1015 | // non-existent sub-register, and %noreg is exactly what we want. |
| 1016 | Loc.substPhysReg(VRM.getPhys(VirtReg), TRI); |
| 1017 | } else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) { |
| 1018 | // FIXME: Translate SubIdx to a stackslot offset. |
| 1019 | Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg)); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1020 | Spilled = true; |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1021 | } else { |
| 1022 | Loc.setReg(0); |
| 1023 | Loc.setSubReg(0); |
| 1024 | } |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1025 | } |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Insert this location if it doesn't already exist and record a mapping |
| 1028 | // from the old number to the new number. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1029 | auto InsertResult = NewLocations.insert({Loc, Spilled}); |
| 1030 | unsigned NewLocNo = std::distance(NewLocations.begin(), InsertResult.first); |
| 1031 | LocNoMap[I] = NewLocNo; |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1034 | // Rewrite the locations and record which ones were spill slots. |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1035 | locations.clear(); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1036 | SpilledLocations.clear(); |
| 1037 | SpilledLocations.resize(NewLocations.size()); |
| 1038 | for (auto &Pair : NewLocations) { |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1039 | locations.push_back(Pair.first); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1040 | if (Pair.second) { |
| 1041 | unsigned NewLocNo = std::distance(&*NewLocations.begin(), &Pair); |
| 1042 | SpilledLocations.set(NewLocNo); |
| 1043 | } |
| 1044 | } |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Update the interval map, but only coalesce left, since intervals to the |
| 1047 | // right use the old location numbers. This should merge two contiguous |
| 1048 | // DBG_VALUE intervals with different vregs that were allocated to the same |
| 1049 | // physical register. |
| 1050 | for (LocMap::iterator I = locInts.begin(); I.valid(); ++I) { |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1051 | DbgValueLocation Loc = I.value(); |
| 1052 | unsigned NewLocNo = LocNoMap[Loc.locNo()]; |
| 1053 | I.setValueUnchecked(Loc.changeLocNo(NewLocNo)); |
Reid Kleckner | 92687d4 | 2017-09-20 17:32:54 +0000 | [diff] [blame] | 1054 | I.setStart(I.start()); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1055 | } |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Devang Patel | 26ffa01 | 2011-02-04 01:43:25 +0000 | [diff] [blame] | 1058 | /// findInsertLocation - Find an iterator for inserting a DBG_VALUE |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1059 | /// instruction. |
| 1060 | static MachineBasicBlock::iterator |
Devang Patel | 26ffa01 | 2011-02-04 01:43:25 +0000 | [diff] [blame] | 1061 | findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx, |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1062 | LiveIntervals &LIS) { |
| 1063 | SlotIndex Start = LIS.getMBBStartIdx(MBB); |
| 1064 | Idx = Idx.getBaseIndex(); |
| 1065 | |
| 1066 | // Try to find an insert location by going backwards from Idx. |
| 1067 | MachineInstr *MI; |
| 1068 | while (!(MI = LIS.getInstructionFromIndex(Idx))) { |
| 1069 | // We've reached the beginning of MBB. |
| 1070 | if (Idx == Start) { |
Keith Walker | 830a8c1 | 2016-09-16 14:07:29 +0000 | [diff] [blame] | 1071 | MachineBasicBlock::iterator I = MBB->SkipPHIsLabelsAndDebug(MBB->begin()); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1072 | return I; |
| 1073 | } |
| 1074 | Idx = Idx.getPrevIndex(); |
| 1075 | } |
Devang Patel | 26ffa01 | 2011-02-04 01:43:25 +0000 | [diff] [blame] | 1076 | |
Jakob Stoklund Olesen | 088b30a | 2011-01-13 23:35:53 +0000 | [diff] [blame] | 1077 | // Don't insert anything after the first terminator, though. |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1078 | return MI->isTerminator() ? MBB->getFirstTerminator() : |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1079 | std::next(MachineBasicBlock::iterator(MI)); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1083 | DbgValueLocation Loc, bool Spilled, |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1084 | LiveIntervals &LIS, |
| 1085 | const TargetInstrInfo &TII) { |
Devang Patel | 26ffa01 | 2011-02-04 01:43:25 +0000 | [diff] [blame] | 1086 | MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1087 | MachineOperand &MO = locations[Loc.locNo()]; |
Devang Patel | eabc3cea | 2011-08-04 20:42:11 +0000 | [diff] [blame] | 1088 | ++NumInsertedDebugValues; |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1089 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1090 | assert(cast<DILocalVariable>(Variable) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 1091 | ->isValidLocationForIntrinsic(getDebugLoc()) && |
Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 1092 | "Expected inlined-at fields to agree"); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1093 | |
| 1094 | // If the location was spilled, the new DBG_VALUE will be indirect. If the |
| 1095 | // original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate |
| 1096 | // that the original virtual register was a pointer. |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1097 | const DIExpression *Expr = Expression; |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1098 | bool IsIndirect = Loc.wasIndirect(); |
| 1099 | if (Spilled) { |
| 1100 | if (IsIndirect) |
| 1101 | Expr = DIExpression::prepend(Expr, DIExpression::WithDeref); |
| 1102 | IsIndirect = true; |
| 1103 | } |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1104 | |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1105 | assert((!Spilled || MO.isFI()) && "a spilled location must be a frame index"); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1106 | |
| 1107 | MachineInstrBuilder MIB = |
| 1108 | BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE)) |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1109 | .add(MO); |
| 1110 | if (IsIndirect) |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1111 | MIB.addImm(0U); |
Adrian Prantl | 418d1d1 | 2013-07-09 20:28:37 +0000 | [diff] [blame] | 1112 | else |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1113 | MIB.addReg(0U, RegState::Debug); |
| 1114 | MIB.addMetadata(Variable).addMetadata(Expr); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1117 | void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS, |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1118 | const TargetInstrInfo &TII, |
| 1119 | const BitVector &SpilledLocations) { |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1120 | MachineFunction::iterator MFEnd = VRM->getMachineFunction().end(); |
| 1121 | |
| 1122 | for (LocMap::const_iterator I = locInts.begin(); I.valid();) { |
| 1123 | SlotIndex Start = I.start(); |
| 1124 | SlotIndex Stop = I.stop(); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1125 | DbgValueLocation Loc = I.value(); |
| 1126 | bool Spilled = !Loc.isUndef() ? SpilledLocations.test(Loc.locNo()) : false; |
Robert Lougher | 10f740d | 2017-08-03 11:54:02 +0000 | [diff] [blame] | 1127 | |
| 1128 | // If the interval start was trimmed to the lexical scope insert the |
| 1129 | // DBG_VALUE at the previous index (otherwise it appears after the |
| 1130 | // first instruction in the range). |
| 1131 | if (trimmedDefs.count(Start)) |
| 1132 | Start = Start.getPrevIndex(); |
| 1133 | |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1134 | DEBUG(dbgs() << "\t[" << Start << ';' << Stop << "):" << Loc.locNo()); |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 1135 | MachineFunction::iterator MBB = LIS.getMBBFromIndex(Start)->getIterator(); |
| 1136 | SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1137 | |
| 1138 | DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1139 | insertDebugValue(&*MBB, Start, Loc, Spilled, LIS, TII); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1140 | // This interval may span multiple basic blocks. |
| 1141 | // Insert a DBG_VALUE into each one. |
| 1142 | while(Stop > MBBEnd) { |
| 1143 | // Move to the next block. |
| 1144 | Start = MBBEnd; |
| 1145 | if (++MBB == MFEnd) |
| 1146 | break; |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 1147 | MBBEnd = LIS.getMBBEndIdx(&*MBB); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1148 | DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd); |
Reid Kleckner | 04e25e0 | 2017-10-03 17:59:02 +0000 | [diff] [blame] | 1149 | insertDebugValue(&*MBB, Start, Loc, Spilled, LIS, TII); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1150 | } |
| 1151 | DEBUG(dbgs() << '\n'); |
| 1152 | if (MBB == MFEnd) |
| 1153 | break; |
| 1154 | |
| 1155 | ++I; |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | void LDVImpl::emitDebugValues(VirtRegMap *VRM) { |
| 1160 | DEBUG(dbgs() << "********** EMITTING LIVE DEBUG VARIABLES **********\n"); |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 1161 | if (!MF) |
| 1162 | return; |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1163 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1164 | BitVector SpilledLocations; |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1165 | for (unsigned i = 0, e = userValues.size(); i != e; ++i) { |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 1166 | DEBUG(userValues[i]->print(dbgs(), TRI)); |
Reid Kleckner | 4e04028 | 2017-09-20 18:19:08 +0000 | [diff] [blame] | 1167 | userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations); |
| 1168 | userValues[i]->emitDebugValues(VRM, *LIS, *TII, SpilledLocations); |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1169 | } |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 1170 | EmitDone = true; |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | void LiveDebugVariables::emitDebugValues(VirtRegMap *VRM) { |
Manman Ren | 7a4c8a7 | 2013-02-13 20:23:48 +0000 | [diff] [blame] | 1174 | if (pImpl) |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1175 | static_cast<LDVImpl*>(pImpl)->emitDebugValues(VRM); |
| 1176 | } |
| 1177 | |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 1178 | bool LiveDebugVariables::doInitialization(Module &M) { |
David Blaikie | 2f04011 | 2014-07-25 16:10:16 +0000 | [diff] [blame] | 1179 | return Pass::doInitialization(M); |
| 1180 | } |
Jakob Stoklund Olesen | afc2bc2 | 2010-12-03 21:47:10 +0000 | [diff] [blame] | 1181 | |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 1182 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Sam Clegg | 705f798 | 2017-06-21 22:19:17 +0000 | [diff] [blame] | 1183 | LLVM_DUMP_METHOD void LiveDebugVariables::dump() const { |
Jakob Stoklund Olesen | 9ec2011 | 2010-12-02 18:15:44 +0000 | [diff] [blame] | 1184 | if (pImpl) |
| 1185 | static_cast<LDVImpl*>(pImpl)->print(dbgs()); |
| 1186 | } |
| 1187 | #endif |