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