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