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