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