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