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