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