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