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