Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 1 | //===-- 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 Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 13 | #include "llvm/CodeGen/MachineFunction.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 14 | #include "llvm/IR/DebugInfo.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Debug.h" |
| 16 | #include "llvm/Target/TargetRegisterInfo.h" |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | #include <map> |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 19 | #include <set> |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 20 | |
| 21 | #define DEBUG_TYPE "dwarfdebug" |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 25 | // \brief If @MI is a DBG_VALUE with debug value described by a |
| 26 | // defined register, returns the number of this register. |
| 27 | // In the other case, returns 0. |
| 28 | static unsigned isDescribedByReg(const MachineInstr &MI) { |
| 29 | assert(MI.isDebugValue()); |
Adrian Prantl | b458dc2 | 2014-10-01 18:10:54 +0000 | [diff] [blame] | 30 | assert(MI.getNumOperands() == 3); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 31 | // If location of variable is described using a register (directly or |
| 32 | // indirecltly), this register is always a first operand. |
| 33 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
| 34 | } |
| 35 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 36 | void DbgValueHistoryMap::startInstrRange(const MDNode *Var, |
| 37 | const MachineInstr &MI) { |
| 38 | // Instruction range should start with a DBG_VALUE instruction for the |
| 39 | // variable. |
Adrian Prantl | b458dc2 | 2014-10-01 18:10:54 +0000 | [diff] [blame] | 40 | assert(MI.isDebugValue() && getEntireVariable(MI.getDebugVariable()) == Var); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 41 | auto &Ranges = VarInstrRanges[Var]; |
| 42 | if (!Ranges.empty() && Ranges.back().second == nullptr && |
| 43 | Ranges.back().first->isIdenticalTo(&MI)) { |
| 44 | DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" |
| 45 | << "\t" << Ranges.back().first << "\t" << MI << "\n"); |
| 46 | return; |
| 47 | } |
| 48 | Ranges.push_back(std::make_pair(&MI, nullptr)); |
| 49 | } |
| 50 | |
| 51 | void DbgValueHistoryMap::endInstrRange(const MDNode *Var, |
| 52 | const MachineInstr &MI) { |
| 53 | auto &Ranges = VarInstrRanges[Var]; |
| 54 | // Verify that the current instruction range is not yet closed. |
| 55 | assert(!Ranges.empty() && Ranges.back().second == nullptr); |
| 56 | // For now, instruction ranges are not allowed to cross basic block |
| 57 | // boundaries. |
| 58 | assert(Ranges.back().first->getParent() == MI.getParent()); |
| 59 | Ranges.back().second = &MI; |
| 60 | } |
| 61 | |
| 62 | unsigned DbgValueHistoryMap::getRegisterForVar(const MDNode *Var) const { |
| 63 | const auto &I = VarInstrRanges.find(Var); |
| 64 | if (I == VarInstrRanges.end()) |
| 65 | return 0; |
| 66 | const auto &Ranges = I->second; |
| 67 | if (Ranges.empty() || Ranges.back().second != nullptr) |
| 68 | return 0; |
| 69 | return isDescribedByReg(*Ranges.back().first); |
| 70 | } |
| 71 | |
| 72 | namespace { |
| 73 | // Maps physreg numbers to the variables they describe. |
| 74 | typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap; |
| 75 | } |
| 76 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 77 | // \brief Claim that @Var is not described by @RegNo anymore. |
| 78 | static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, |
| 79 | unsigned RegNo, const MDNode *Var) { |
| 80 | const auto &I = RegVars.find(RegNo); |
| 81 | assert(RegNo != 0U && I != RegVars.end()); |
| 82 | auto &VarSet = I->second; |
| 83 | const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var); |
| 84 | assert(VarPos != VarSet.end()); |
| 85 | VarSet.erase(VarPos); |
| 86 | // Don't keep empty sets in a map to keep it as small as possible. |
| 87 | if (VarSet.empty()) |
| 88 | RegVars.erase(I); |
| 89 | } |
| 90 | |
| 91 | // \brief Claim that @Var is now described by @RegNo. |
| 92 | static void addRegDescribedVar(RegDescribedVarsMap &RegVars, |
| 93 | unsigned RegNo, const MDNode *Var) { |
| 94 | assert(RegNo != 0U); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 95 | auto &VarSet = RegVars[RegNo]; |
| 96 | assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end()); |
| 97 | VarSet.push_back(Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // \brief Terminate the location range for variables described by register |
| 101 | // @RegNo by inserting @ClobberingInstr to their history. |
| 102 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo, |
| 103 | DbgValueHistoryMap &HistMap, |
| 104 | const MachineInstr &ClobberingInstr) { |
| 105 | const auto &I = RegVars.find(RegNo); |
| 106 | if (I == RegVars.end()) |
| 107 | return; |
| 108 | // Iterate over all variables described by this register and add this |
| 109 | // instruction to their history, clobbering it. |
| 110 | for (const auto &Var : I->second) |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 111 | HistMap.endInstrRange(Var, ClobberingInstr); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 112 | RegVars.erase(I); |
| 113 | } |
| 114 | |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 115 | // \brief Collect all registers clobbered by @MI and apply the functor |
| 116 | // @Func to their RegNo. |
| 117 | // @Func should be a functor with a void(unsigned) signature. We're |
| 118 | // not using std::function here for performance reasons. It has a |
| 119 | // small but measurable impact. By using a functor instead of a |
| 120 | // std::set& here, we can avoid the overhead of constructing |
| 121 | // temporaries in calculateDbgValueHistory, which has a significant |
| 122 | // performance impact. |
| 123 | template<typename Callable> |
| 124 | static void applyToClobberedRegisters(const MachineInstr &MI, |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 125 | const TargetRegisterInfo *TRI, |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 126 | Callable Func) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 127 | for (const MachineOperand &MO : MI.operands()) { |
| 128 | if (!MO.isReg() || !MO.isDef() || !MO.getReg()) |
| 129 | continue; |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 130 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI) |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 131 | Func(*AI); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 135 | // \brief Returns the first instruction in @MBB which corresponds to |
| 136 | // the function epilogue, or nullptr if @MBB doesn't contain an epilogue. |
| 137 | static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) { |
| 138 | auto LastMI = MBB.getLastNonDebugInstr(); |
| 139 | if (LastMI == MBB.end() || !LastMI->isReturn()) |
| 140 | return nullptr; |
| 141 | // Assume that epilogue starts with instruction having the same debug location |
| 142 | // as the return instruction. |
| 143 | DebugLoc LastLoc = LastMI->getDebugLoc(); |
| 144 | auto Res = LastMI; |
| 145 | for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)); I != MBB.rend(); |
| 146 | ++I) { |
| 147 | if (I->getDebugLoc() != LastLoc) |
| 148 | return Res; |
| 149 | Res = std::prev(I.base()); |
| 150 | } |
| 151 | // If all instructions have the same debug location, assume whole MBB is |
| 152 | // an epilogue. |
| 153 | return MBB.begin(); |
| 154 | } |
| 155 | |
| 156 | // \brief Collect registers that are modified in the function body (their |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 157 | // contents is changed outside of the prologue and epilogue). |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 158 | static void collectChangingRegs(const MachineFunction *MF, |
| 159 | const TargetRegisterInfo *TRI, |
| 160 | std::set<unsigned> &Regs) { |
| 161 | for (const auto &MBB : *MF) { |
| 162 | auto FirstEpilogueInst = getFirstEpilogueInst(MBB); |
Adrian Prantl | e2d6375 | 2014-08-06 18:41:19 +0000 | [diff] [blame] | 163 | |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 164 | for (const auto &MI : MBB) { |
Adrian Prantl | e2d6375 | 2014-08-06 18:41:19 +0000 | [diff] [blame] | 165 | if (&MI == FirstEpilogueInst) |
| 166 | break; |
| 167 | if (!MI.getFlag(MachineInstr::FrameSetup)) |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 168 | applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.insert(r); }); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 173 | void calculateDbgValueHistory(const MachineFunction *MF, |
| 174 | const TargetRegisterInfo *TRI, |
| 175 | DbgValueHistoryMap &Result) { |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 176 | std::set<unsigned> ChangingRegs; |
| 177 | collectChangingRegs(MF, TRI, ChangingRegs); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 178 | |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 179 | RegDescribedVarsMap RegVars; |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 180 | for (const auto &MBB : *MF) { |
| 181 | for (const auto &MI : MBB) { |
| 182 | if (!MI.isDebugValue()) { |
| 183 | // Not a DBG_VALUE instruction. It may clobber registers which describe |
| 184 | // some variables. |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 185 | applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) { |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 186 | if (ChangingRegs.count(RegNo)) |
| 187 | clobberRegisterUses(RegVars, RegNo, Result, MI); |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 188 | }); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 189 | continue; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 190 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 191 | |
Alexey Samsonov | f0e0cca | 2014-05-27 22:35:00 +0000 | [diff] [blame] | 192 | assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!"); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 193 | // Use the base variable (without any DW_OP_piece expressions) |
| 194 | // as index into History. The full variables including the |
| 195 | // piece expressions are attached to the MI. |
Adrian Prantl | b458dc2 | 2014-10-01 18:10:54 +0000 | [diff] [blame] | 196 | DIVariable Var = getEntireVariable(MI.getDebugVariable()); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 197 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 198 | if (unsigned PrevReg = Result.getRegisterForVar(Var)) |
| 199 | dropRegDescribedVar(RegVars, PrevReg, Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 200 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 201 | Result.startInstrRange(Var, MI); |
| 202 | |
| 203 | if (unsigned NewReg = isDescribedByReg(MI)) |
| 204 | addRegDescribedVar(RegVars, NewReg, Var); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 205 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 206 | |
| 207 | // Make sure locations for register-described variables are valid only |
| 208 | // until the end of the basic block (unless it's the last basic block, in |
| 209 | // which case let their liveness run off to the end of the function). |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 210 | if (!MBB.empty() && &MBB != &MF->back()) { |
| 211 | for (unsigned RegNo : ChangingRegs) |
| 212 | clobberRegisterUses(RegVars, RegNo, Result, MBB.back()); |
| 213 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | } |