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" |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/BitVector.h" |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallVector.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 14 | #include "llvm/CodeGen/MachineFunction.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfo.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetLowering.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetRegisterInfo.h" |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetSubtargetInfo.h" |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <map> |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 24 | |
| 25 | #define DEBUG_TYPE "dwarfdebug" |
| 26 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 27 | // \brief If @MI is a DBG_VALUE with debug value described by a |
| 28 | // defined register, returns the number of this register. |
| 29 | // In the other case, returns 0. |
| 30 | static unsigned isDescribedByReg(const MachineInstr &MI) { |
| 31 | assert(MI.isDebugValue()); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 32 | assert(MI.getNumOperands() == 4); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 33 | // If location of variable is described using a register (directly or |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 34 | // indirectly), this register is always a first operand. |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 35 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
| 36 | } |
| 37 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 38 | void DbgValueHistoryMap::startInstrRange(InlinedVariable Var, |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 39 | const MachineInstr &MI) { |
| 40 | // Instruction range should start with a DBG_VALUE instruction for the |
| 41 | // variable. |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 42 | assert(MI.isDebugValue() && "not a DBG_VALUE"); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 43 | auto &Ranges = VarInstrRanges[Var]; |
| 44 | if (!Ranges.empty() && Ranges.back().second == nullptr && |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 45 | Ranges.back().first->isIdenticalTo(MI)) { |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 46 | DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" |
| 47 | << "\t" << Ranges.back().first << "\t" << MI << "\n"); |
| 48 | return; |
| 49 | } |
| 50 | Ranges.push_back(std::make_pair(&MI, nullptr)); |
| 51 | } |
| 52 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 53 | void DbgValueHistoryMap::endInstrRange(InlinedVariable Var, |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 54 | const MachineInstr &MI) { |
| 55 | auto &Ranges = VarInstrRanges[Var]; |
| 56 | // Verify that the current instruction range is not yet closed. |
| 57 | assert(!Ranges.empty() && Ranges.back().second == nullptr); |
| 58 | // For now, instruction ranges are not allowed to cross basic block |
| 59 | // boundaries. |
| 60 | assert(Ranges.back().first->getParent() == MI.getParent()); |
| 61 | Ranges.back().second = &MI; |
| 62 | } |
| 63 | |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 64 | unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const { |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 65 | const auto &I = VarInstrRanges.find(Var); |
| 66 | if (I == VarInstrRanges.end()) |
| 67 | return 0; |
| 68 | const auto &Ranges = I->second; |
| 69 | if (Ranges.empty() || Ranges.back().second != nullptr) |
| 70 | return 0; |
| 71 | return isDescribedByReg(*Ranges.back().first); |
| 72 | } |
| 73 | |
| 74 | namespace { |
| 75 | // Maps physreg numbers to the variables they describe. |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 76 | typedef DbgValueHistoryMap::InlinedVariable InlinedVariable; |
| 77 | typedef std::map<unsigned, SmallVector<InlinedVariable, 1>> RegDescribedVarsMap; |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 80 | // \brief Claim that @Var is not described by @RegNo anymore. |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 81 | static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo, |
| 82 | InlinedVariable Var) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 83 | const auto &I = RegVars.find(RegNo); |
| 84 | assert(RegNo != 0U && I != RegVars.end()); |
| 85 | auto &VarSet = I->second; |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 86 | const auto &VarPos = find(VarSet, Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 87 | assert(VarPos != VarSet.end()); |
| 88 | VarSet.erase(VarPos); |
| 89 | // Don't keep empty sets in a map to keep it as small as possible. |
| 90 | if (VarSet.empty()) |
| 91 | RegVars.erase(I); |
| 92 | } |
| 93 | |
| 94 | // \brief Claim that @Var is now described by @RegNo. |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 95 | static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo, |
| 96 | InlinedVariable Var) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 97 | assert(RegNo != 0U); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 98 | auto &VarSet = RegVars[RegNo]; |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 99 | assert(!is_contained(VarSet, Var)); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 100 | VarSet.push_back(Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 103 | // \brief Terminate the location range for variables described by register at |
| 104 | // @I by inserting @ClobberingInstr to their history. |
| 105 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, |
| 106 | RegDescribedVarsMap::iterator I, |
| 107 | DbgValueHistoryMap &HistMap, |
| 108 | const MachineInstr &ClobberingInstr) { |
| 109 | // Iterate over all variables described by this register and add this |
| 110 | // instruction to their history, clobbering it. |
| 111 | for (const auto &Var : I->second) |
| 112 | HistMap.endInstrRange(Var, ClobberingInstr); |
| 113 | RegVars.erase(I); |
| 114 | } |
| 115 | |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 116 | // \brief Terminate the location range for variables described by register |
| 117 | // @RegNo by inserting @ClobberingInstr to their history. |
| 118 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo, |
| 119 | DbgValueHistoryMap &HistMap, |
| 120 | const MachineInstr &ClobberingInstr) { |
| 121 | const auto &I = RegVars.find(RegNo); |
| 122 | if (I == RegVars.end()) |
| 123 | return; |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 124 | clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 127 | // \brief Returns the first instruction in @MBB which corresponds to |
| 128 | // the function epilogue, or nullptr if @MBB doesn't contain an epilogue. |
| 129 | static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) { |
| 130 | auto LastMI = MBB.getLastNonDebugInstr(); |
| 131 | if (LastMI == MBB.end() || !LastMI->isReturn()) |
| 132 | return nullptr; |
| 133 | // Assume that epilogue starts with instruction having the same debug location |
| 134 | // as the return instruction. |
| 135 | DebugLoc LastLoc = LastMI->getDebugLoc(); |
| 136 | auto Res = LastMI; |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 137 | for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(), |
| 138 | E = MBB.rend(); |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 139 | I != E; ++I) { |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 140 | if (I->getDebugLoc() != LastLoc) |
Duncan P. N. Exon Smith | 5bff511 | 2016-07-08 19:31:47 +0000 | [diff] [blame] | 141 | return &*Res; |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 142 | Res = &*I; |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 143 | } |
| 144 | // If all instructions have the same debug location, assume whole MBB is |
| 145 | // an epilogue. |
Duncan P. N. Exon Smith | 5bff511 | 2016-07-08 19:31:47 +0000 | [diff] [blame] | 146 | return &*MBB.begin(); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // \brief Collect registers that are modified in the function body (their |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 150 | // contents is changed outside of the prologue and epilogue). |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 151 | static void collectChangingRegs(const MachineFunction *MF, |
| 152 | const TargetRegisterInfo *TRI, |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 153 | BitVector &Regs) { |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 154 | for (const auto &MBB : *MF) { |
| 155 | auto FirstEpilogueInst = getFirstEpilogueInst(MBB); |
Adrian Prantl | e2d6375 | 2014-08-06 18:41:19 +0000 | [diff] [blame] | 156 | |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 157 | for (const auto &MI : MBB) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 158 | // Avoid looking at prologue or epilogue instructions. |
Adrian Prantl | e2d6375 | 2014-08-06 18:41:19 +0000 | [diff] [blame] | 159 | if (&MI == FirstEpilogueInst) |
| 160 | break; |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 161 | if (MI.getFlag(MachineInstr::FrameSetup)) |
| 162 | continue; |
| 163 | |
| 164 | // Look for register defs and register masks. Register masks are |
| 165 | // typically on calls and they clobber everything not in the mask. |
| 166 | for (const MachineOperand &MO : MI.operands()) { |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 167 | // Skip virtual registers since they are handled by the parent. |
| 168 | if (MO.isReg() && MO.isDef() && MO.getReg() && |
| 169 | !TRI->isVirtualRegister(MO.getReg())) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 170 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); |
| 171 | ++AI) |
| 172 | Regs.set(*AI); |
| 173 | } else if (MO.isRegMask()) { |
| 174 | Regs.setBitsNotInMask(MO.getRegMask()); |
| 175 | } |
| 176 | } |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 181 | void llvm::calculateDbgValueHistory(const MachineFunction *MF, |
| 182 | const TargetRegisterInfo *TRI, |
| 183 | DbgValueHistoryMap &Result) { |
| 184 | BitVector ChangingRegs(TRI->getNumRegs()); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 185 | collectChangingRegs(MF, TRI, ChangingRegs); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 186 | |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 187 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
| 188 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 189 | RegDescribedVarsMap RegVars; |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 190 | for (const auto &MBB : *MF) { |
| 191 | for (const auto &MI : MBB) { |
| 192 | if (!MI.isDebugValue()) { |
| 193 | // Not a DBG_VALUE instruction. It may clobber registers which describe |
| 194 | // some variables. |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 195 | for (const MachineOperand &MO : MI.operands()) { |
| 196 | if (MO.isReg() && MO.isDef() && MO.getReg()) { |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 197 | // If this is a virtual register, only clobber it since it doesn't |
| 198 | // have aliases. |
| 199 | if (TRI->isVirtualRegister(MO.getReg())) |
| 200 | clobberRegisterUses(RegVars, MO.getReg(), Result, MI); |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 201 | // If this is a register def operand, it may end a debug value |
| 202 | // range. |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 203 | else { |
| 204 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); |
| 205 | ++AI) |
| 206 | if (ChangingRegs.test(*AI)) |
| 207 | clobberRegisterUses(RegVars, *AI, Result, MI); |
| 208 | } |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 209 | } else if (MO.isRegMask()) { |
| 210 | // If this is a register mask operand, clobber all debug values in |
| 211 | // non-CSRs. |
| 212 | for (int I = ChangingRegs.find_first(); I != -1; |
| 213 | I = ChangingRegs.find_next(I)) { |
| 214 | // Don't consider SP to be clobbered by register masks. |
| 215 | if (unsigned(I) != SP && TRI->isPhysicalRegister(I) && |
| 216 | MO.clobbersPhysReg(I)) { |
| 217 | clobberRegisterUses(RegVars, I, Result, MI); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 222 | continue; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 223 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 224 | |
Alexey Samsonov | f0e0cca | 2014-05-27 22:35:00 +0000 | [diff] [blame] | 225 | assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!"); |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 226 | // Use the base variable (without any DW_OP_piece expressions) |
| 227 | // as index into History. The full variables including the |
| 228 | // piece expressions are attached to the MI. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 229 | const DILocalVariable *RawVar = MI.getDebugVariable(); |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 230 | assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) && |
Duncan P. N. Exon Smith | 3bef6a3 | 2015-04-03 19:20:26 +0000 | [diff] [blame] | 231 | "Expected inlined-at fields to agree"); |
Duncan P. N. Exon Smith | 78a9527 | 2015-04-16 22:12:59 +0000 | [diff] [blame] | 232 | InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt()); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 233 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 234 | if (unsigned PrevReg = Result.getRegisterForVar(Var)) |
| 235 | dropRegDescribedVar(RegVars, PrevReg, Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 236 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 237 | Result.startInstrRange(Var, MI); |
| 238 | |
| 239 | if (unsigned NewReg = isDescribedByReg(MI)) |
| 240 | addRegDescribedVar(RegVars, NewReg, Var); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 241 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 242 | |
| 243 | // Make sure locations for register-described variables are valid only |
| 244 | // until the end of the basic block (unless it's the last basic block, in |
| 245 | // which case let their liveness run off to the end of the function). |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 246 | if (!MBB.empty() && &MBB != &MF->back()) { |
| 247 | for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) { |
| 248 | auto CurElem = I++; // CurElem can be erased below. |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 249 | if (TRI->isVirtualRegister(CurElem->first) || |
| 250 | ChangingRegs.test(CurElem->first)) |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 251 | clobberRegisterUses(RegVars, CurElem, Result, MBB.back()); |
| 252 | } |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 253 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 254 | } |
| 255 | } |