blob: 450d1541384914d731f01688b353fde1a342ae1e [file] [log] [blame]
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00001//===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp -------------===//
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#include "DbgValueHistoryCalculator.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000011#include "llvm/ADT/SmallVector.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000012#include "llvm/CodeGen/MachineBasicBlock.h"
13#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/Support/Debug.h"
15#include "llvm/Target/TargetRegisterInfo.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000016#include <algorithm>
17#include <map>
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000018
19#define DEBUG_TYPE "dwarfdebug"
20
21namespace llvm {
22
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000023namespace {
24// Maps physreg numbers to the variables they describe.
25typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap;
26}
27
28// \brief If @MI is a DBG_VALUE with debug value described by a
29// defined register, returns the number of this register.
30// In the other case, returns 0.
31static unsigned isDescribedByReg(const MachineInstr &MI) {
32 assert(MI.isDebugValue());
33 assert(MI.getNumOperands() == 3);
34 // If location of variable is described using a register (directly or
35 // indirecltly), this register is always a first operand.
36 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
37}
38
39// \brief Claim that @Var is not described by @RegNo anymore.
40static void dropRegDescribedVar(RegDescribedVarsMap &RegVars,
41 unsigned RegNo, const MDNode *Var) {
42 const auto &I = RegVars.find(RegNo);
43 assert(RegNo != 0U && I != RegVars.end());
44 auto &VarSet = I->second;
45 const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
46 assert(VarPos != VarSet.end());
47 VarSet.erase(VarPos);
48 // Don't keep empty sets in a map to keep it as small as possible.
49 if (VarSet.empty())
50 RegVars.erase(I);
51}
52
53// \brief Claim that @Var is now described by @RegNo.
54static void addRegDescribedVar(RegDescribedVarsMap &RegVars,
55 unsigned RegNo, const MDNode *Var) {
56 assert(RegNo != 0U);
57 RegVars[RegNo].push_back(Var);
58}
59
60static void clobberVariableLocation(SmallVectorImpl<const MachineInstr *> &VarHistory,
61 const MachineInstr &ClobberingInstr) {
62 assert(!VarHistory.empty());
63 // DBG_VALUE we're clobbering should belong to the same MBB.
64 assert(VarHistory.back()->isDebugValue());
65 assert(VarHistory.back()->getParent() == ClobberingInstr.getParent());
66 VarHistory.push_back(&ClobberingInstr);
67}
68
69// \brief Terminate the location range for variables described by register
70// @RegNo by inserting @ClobberingInstr to their history.
71static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
72 DbgValueHistoryMap &HistMap,
73 const MachineInstr &ClobberingInstr) {
74 const auto &I = RegVars.find(RegNo);
75 if (I == RegVars.end())
76 return;
77 // Iterate over all variables described by this register and add this
78 // instruction to their history, clobbering it.
79 for (const auto &Var : I->second)
80 clobberVariableLocation(HistMap[Var], ClobberingInstr);
81 RegVars.erase(I);
82}
83
84// \brief Terminate the location range for all variables, described by registers
85// clobbered by @MI.
86static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
87 const MachineInstr &MI,
88 const TargetRegisterInfo *TRI,
89 DbgValueHistoryMap &HistMap) {
90 for (const MachineOperand &MO : MI.operands()) {
91 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
92 continue;
93 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
94 ++AI) {
95 unsigned RegNo = *AI;
96 clobberRegisterUses(RegVars, RegNo, HistMap, MI);
97 }
98 }
99}
100
101// \brief Terminate the location range for all register-described variables
102// by inserting @ClobberingInstr to their history.
103static void clobberAllRegistersUses(RegDescribedVarsMap &RegVars,
104 DbgValueHistoryMap &HistMap,
105 const MachineInstr &ClobberingInstr) {
106 for (const auto &I : RegVars)
107 for (const auto &Var : I.second)
108 clobberVariableLocation(HistMap[Var], ClobberingInstr);
109 RegVars.clear();
110}
111
112// \brief Update the register that describes location of @Var in @RegVars map.
113static void
114updateRegForVariable(RegDescribedVarsMap &RegVars, const MDNode *Var,
115 const SmallVectorImpl<const MachineInstr *> &VarHistory,
116 const MachineInstr &MI) {
117 if (!VarHistory.empty()) {
118 const MachineInstr &Prev = *VarHistory.back();
119 // Check if Var is currently described by a register by instruction in the
120 // same basic block.
121 if (Prev.isDebugValue() && Prev.getDebugVariable() == Var &&
122 Prev.getParent() == MI.getParent()) {
123 if (unsigned PrevReg = isDescribedByReg(Prev))
124 dropRegDescribedVar(RegVars, PrevReg, Var);
125 }
126 }
127
128 assert(MI.getDebugVariable() == Var);
129 if (unsigned MIReg = isDescribedByReg(MI))
130 addRegDescribedVar(RegVars, MIReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000131}
132
133void calculateDbgValueHistory(const MachineFunction *MF,
134 const TargetRegisterInfo *TRI,
135 DbgValueHistoryMap &Result) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000136 RegDescribedVarsMap RegVars;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000137
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000138 for (const auto &MBB : *MF) {
139 for (const auto &MI : MBB) {
140 if (!MI.isDebugValue()) {
141 // Not a DBG_VALUE instruction. It may clobber registers which describe
142 // some variables.
143 clobberRegisterUses(RegVars, MI, TRI, Result);
144 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000145 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000146
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000147 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000148 const MDNode *Var = MI.getDebugVariable();
149 auto &History = Result[Var];
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000150
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000151 if (!History.empty() && History.back()->isIdenticalTo(&MI)) {
152 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
153 << "\t" << History.back() << "\t" << MI << "\n");
154 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000155 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000156
157 updateRegForVariable(RegVars, Var, History, MI);
158 History.push_back(&MI);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000159 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000160
161 // Make sure locations for register-described variables are valid only
162 // until the end of the basic block (unless it's the last basic block, in
163 // which case let their liveness run off to the end of the function).
164 if (!MBB.empty() && &MBB != &MF->back())
165 clobberAllRegistersUses(RegVars, Result, MBB.back());
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000166 }
167}
168
169}