Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.cpp -------------===// |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 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 | |
Yonghong Song | 61b189e | 2018-12-18 23:10:17 +0000 | [diff] [blame^] | 10 | #include "llvm/CodeGen/DbgEntityHistoryCalculator.h" |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 15 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineInstr.h" |
| 17 | #include "llvm/CodeGen/MachineOperand.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetLowering.h" |
| 19 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 20 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DebugInfoMetadata.h" |
| 22 | #include "llvm/IR/DebugLoc.h" |
| 23 | #include "llvm/MC/MCRegisterInfo.h" |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 16132e6 | 2015-03-23 18:07:13 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 26 | #include <cassert> |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 27 | #include <map> |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 28 | #include <utility> |
| 29 | |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 31 | |
| 32 | #define DEBUG_TYPE "dwarfdebug" |
| 33 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 34 | // If @MI is a DBG_VALUE with debug value described by a |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 35 | // defined register, returns the number of this register. |
| 36 | // In the other case, returns 0. |
| 37 | static unsigned isDescribedByReg(const MachineInstr &MI) { |
| 38 | assert(MI.isDebugValue()); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 39 | assert(MI.getNumOperands() == 4); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 40 | // If location of variable is described using a register (directly or |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 41 | // indirectly), this register is always a first operand. |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 42 | return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; |
| 43 | } |
| 44 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 45 | void DbgValueHistoryMap::startInstrRange(InlinedEntity Var, |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 46 | const MachineInstr &MI) { |
| 47 | // Instruction range should start with a DBG_VALUE instruction for the |
| 48 | // variable. |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 49 | assert(MI.isDebugValue() && "not a DBG_VALUE"); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 50 | auto &Ranges = VarInstrRanges[Var]; |
| 51 | if (!Ranges.empty() && Ranges.back().second == nullptr && |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 52 | Ranges.back().first->isIdenticalTo(MI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 53 | LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" |
| 54 | << "\t" << Ranges.back().first << "\t" << MI << "\n"); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | Ranges.push_back(std::make_pair(&MI, nullptr)); |
| 58 | } |
| 59 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 60 | void DbgValueHistoryMap::endInstrRange(InlinedEntity Var, |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 61 | const MachineInstr &MI) { |
| 62 | auto &Ranges = VarInstrRanges[Var]; |
| 63 | // Verify that the current instruction range is not yet closed. |
| 64 | assert(!Ranges.empty() && Ranges.back().second == nullptr); |
| 65 | // For now, instruction ranges are not allowed to cross basic block |
| 66 | // boundaries. |
| 67 | assert(Ranges.back().first->getParent() == MI.getParent()); |
| 68 | Ranges.back().second = &MI; |
| 69 | } |
| 70 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 71 | unsigned DbgValueHistoryMap::getRegisterForVar(InlinedEntity Var) const { |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 72 | const auto &I = VarInstrRanges.find(Var); |
| 73 | if (I == VarInstrRanges.end()) |
| 74 | return 0; |
| 75 | const auto &Ranges = I->second; |
| 76 | if (Ranges.empty() || Ranges.back().second != nullptr) |
| 77 | return 0; |
| 78 | return isDescribedByReg(*Ranges.back().first); |
| 79 | } |
| 80 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 81 | void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) { |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 82 | assert(MI.isDebugLabel() && "not a DBG_LABEL"); |
| 83 | LabelInstr[Label] = &MI; |
| 84 | } |
| 85 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 86 | namespace { |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 87 | |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 88 | // Maps physreg numbers to the variables they describe. |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 89 | using InlinedEntity = DbgValueHistoryMap::InlinedEntity; |
| 90 | using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>; |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 91 | |
| 92 | } // end anonymous namespace |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 93 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 94 | // Claim that @Var is not described by @RegNo anymore. |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 95 | static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo, |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 96 | InlinedEntity Var) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 97 | const auto &I = RegVars.find(RegNo); |
| 98 | assert(RegNo != 0U && I != RegVars.end()); |
| 99 | auto &VarSet = I->second; |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 100 | const auto &VarPos = llvm::find(VarSet, Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 101 | assert(VarPos != VarSet.end()); |
| 102 | VarSet.erase(VarPos); |
| 103 | // Don't keep empty sets in a map to keep it as small as possible. |
| 104 | if (VarSet.empty()) |
| 105 | RegVars.erase(I); |
| 106 | } |
| 107 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 108 | // Claim that @Var is now described by @RegNo. |
Duncan P. N. Exon Smith | 62e0f45 | 2015-04-15 22:29:27 +0000 | [diff] [blame] | 109 | static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo, |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 110 | InlinedEntity Var) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 111 | assert(RegNo != 0U); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 112 | auto &VarSet = RegVars[RegNo]; |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 113 | assert(!is_contained(VarSet, Var)); |
Alexey Samsonov | bb2990d | 2014-05-27 23:09:50 +0000 | [diff] [blame] | 114 | VarSet.push_back(Var); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 117 | // Terminate the location range for variables described by register at |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 118 | // @I by inserting @ClobberingInstr to their history. |
| 119 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, |
| 120 | RegDescribedVarsMap::iterator I, |
| 121 | DbgValueHistoryMap &HistMap, |
| 122 | const MachineInstr &ClobberingInstr) { |
| 123 | // Iterate over all variables described by this register and add this |
| 124 | // instruction to their history, clobbering it. |
| 125 | for (const auto &Var : I->second) |
| 126 | HistMap.endInstrRange(Var, ClobberingInstr); |
| 127 | RegVars.erase(I); |
| 128 | } |
| 129 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 130 | // Terminate the location range for variables described by register |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 131 | // @RegNo by inserting @ClobberingInstr to their history. |
| 132 | static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo, |
| 133 | DbgValueHistoryMap &HistMap, |
| 134 | const MachineInstr &ClobberingInstr) { |
| 135 | const auto &I = RegVars.find(RegNo); |
| 136 | if (I == RegVars.end()) |
| 137 | return; |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 138 | clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 141 | // Returns the first instruction in @MBB which corresponds to |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 142 | // the function epilogue, or nullptr if @MBB doesn't contain an epilogue. |
| 143 | static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) { |
| 144 | auto LastMI = MBB.getLastNonDebugInstr(); |
| 145 | if (LastMI == MBB.end() || !LastMI->isReturn()) |
| 146 | return nullptr; |
| 147 | // Assume that epilogue starts with instruction having the same debug location |
| 148 | // as the return instruction. |
| 149 | DebugLoc LastLoc = LastMI->getDebugLoc(); |
| 150 | auto Res = LastMI; |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 151 | for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(), |
| 152 | E = MBB.rend(); |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 153 | I != E; ++I) { |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 154 | if (I->getDebugLoc() != LastLoc) |
Duncan P. N. Exon Smith | 5bff511 | 2016-07-08 19:31:47 +0000 | [diff] [blame] | 155 | return &*Res; |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 156 | Res = &*I; |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 157 | } |
| 158 | // If all instructions have the same debug location, assume whole MBB is |
| 159 | // an epilogue. |
Duncan P. N. Exon Smith | 5bff511 | 2016-07-08 19:31:47 +0000 | [diff] [blame] | 160 | return &*MBB.begin(); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 163 | // Collect registers that are modified in the function body (their |
Adrian Prantl | 364d131 | 2014-08-06 18:41:24 +0000 | [diff] [blame] | 164 | // contents is changed outside of the prologue and epilogue). |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 165 | static void collectChangingRegs(const MachineFunction *MF, |
| 166 | const TargetRegisterInfo *TRI, |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 167 | BitVector &Regs) { |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 168 | for (const auto &MBB : *MF) { |
| 169 | auto FirstEpilogueInst = getFirstEpilogueInst(MBB); |
Adrian Prantl | e2d6375 | 2014-08-06 18:41:19 +0000 | [diff] [blame] | 170 | |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 171 | for (const auto &MI : MBB) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 172 | // Avoid looking at prologue or epilogue instructions. |
Adrian Prantl | e2d6375 | 2014-08-06 18:41:19 +0000 | [diff] [blame] | 173 | if (&MI == FirstEpilogueInst) |
| 174 | break; |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 175 | if (MI.getFlag(MachineInstr::FrameSetup)) |
| 176 | continue; |
| 177 | |
| 178 | // Look for register defs and register masks. Register masks are |
| 179 | // typically on calls and they clobber everything not in the mask. |
| 180 | for (const MachineOperand &MO : MI.operands()) { |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 181 | // Skip virtual registers since they are handled by the parent. |
| 182 | if (MO.isReg() && MO.isDef() && MO.getReg() && |
| 183 | !TRI->isVirtualRegister(MO.getReg())) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 184 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); |
| 185 | ++AI) |
| 186 | Regs.set(*AI); |
| 187 | } else if (MO.isRegMask()) { |
| 188 | Regs.setBitsNotInMask(MO.getRegMask()); |
| 189 | } |
| 190 | } |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 195 | void llvm::calculateDbgEntityHistory(const MachineFunction *MF, |
| 196 | const TargetRegisterInfo *TRI, |
| 197 | DbgValueHistoryMap &DbgValues, |
| 198 | DbgLabelInstrMap &DbgLabels) { |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 199 | BitVector ChangingRegs(TRI->getNumRegs()); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 200 | collectChangingRegs(MF, TRI, ChangingRegs); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 201 | |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 202 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
| 203 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 204 | RegDescribedVarsMap RegVars; |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 205 | for (const auto &MBB : *MF) { |
| 206 | for (const auto &MI : MBB) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 207 | if (!MI.isDebugInstr()) { |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 208 | // Not a DBG_VALUE instruction. It may clobber registers which describe |
| 209 | // some variables. |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 210 | for (const MachineOperand &MO : MI.operands()) { |
| 211 | if (MO.isReg() && MO.isDef() && MO.getReg()) { |
Adrian Prantl | d9cd4d5 | 2017-06-01 21:14:58 +0000 | [diff] [blame] | 212 | // Ignore call instructions that claim to clobber SP. The AArch64 |
| 213 | // backend does this for aggregate function arguments. |
| 214 | if (MI.isCall() && MO.getReg() == SP) |
| 215 | continue; |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 216 | // If this is a virtual register, only clobber it since it doesn't |
| 217 | // have aliases. |
| 218 | if (TRI->isVirtualRegister(MO.getReg())) |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 219 | clobberRegisterUses(RegVars, MO.getReg(), DbgValues, MI); |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 220 | // If this is a register def operand, it may end a debug value |
| 221 | // range. |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 222 | else { |
| 223 | for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); |
| 224 | ++AI) |
| 225 | if (ChangingRegs.test(*AI)) |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 226 | clobberRegisterUses(RegVars, *AI, DbgValues, MI); |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 227 | } |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 228 | } else if (MO.isRegMask()) { |
| 229 | // If this is a register mask operand, clobber all debug values in |
| 230 | // non-CSRs. |
Francis Visoiu Mistrih | b52e036 | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 231 | for (unsigned I : ChangingRegs.set_bits()) { |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 232 | // Don't consider SP to be clobbered by register masks. |
| 233 | if (unsigned(I) != SP && TRI->isPhysicalRegister(I) && |
| 234 | MO.clobbersPhysReg(I)) { |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 235 | clobberRegisterUses(RegVars, I, DbgValues, MI); |
Reid Kleckner | f6f04f8 | 2016-03-25 17:54:46 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 240 | continue; |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 241 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 242 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 243 | if (MI.isDebugValue()) { |
| 244 | assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!"); |
| 245 | // Use the base variable (without any DW_OP_piece expressions) |
| 246 | // as index into History. The full variables including the |
| 247 | // piece expressions are attached to the MI. |
| 248 | const DILocalVariable *RawVar = MI.getDebugVariable(); |
| 249 | assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) && |
| 250 | "Expected inlined-at fields to agree"); |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 251 | InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt()); |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 252 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 253 | if (unsigned PrevReg = DbgValues.getRegisterForVar(Var)) |
| 254 | dropRegDescribedVar(RegVars, PrevReg, Var); |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 255 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 256 | DbgValues.startInstrRange(Var, MI); |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 257 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 258 | if (unsigned NewReg = isDescribedByReg(MI)) |
| 259 | addRegDescribedVar(RegVars, NewReg, Var); |
| 260 | } else if (MI.isDebugLabel()) { |
| 261 | assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!"); |
| 262 | const DILabel *RawLabel = MI.getDebugLabel(); |
| 263 | assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) && |
| 264 | "Expected inlined-at fields to agree"); |
| 265 | // When collecting debug information for labels, there is no MCSymbol |
| 266 | // generated for it. So, we keep MachineInstr in DbgLabels in order |
| 267 | // to query MCSymbol afterward. |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 268 | InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt()); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 269 | DbgLabels.addInstr(L, MI); |
| 270 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 271 | } |
Alexey Samsonov | dfcaf9c | 2014-05-20 18:34:54 +0000 | [diff] [blame] | 272 | |
| 273 | // Make sure locations for register-described variables are valid only |
| 274 | // until the end of the basic block (unless it's the last basic block, in |
| 275 | // 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] | 276 | if (!MBB.empty() && &MBB != &MF->back()) { |
| 277 | for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) { |
| 278 | auto CurElem = I++; // CurElem can be erased below. |
Dominic Chen | 6ba1965 | 2016-08-11 17:52:40 +0000 | [diff] [blame] | 279 | if (TRI->isVirtualRegister(CurElem->first) || |
| 280 | ChangingRegs.test(CurElem->first)) |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 281 | clobberRegisterUses(RegVars, CurElem, DbgValues, MBB.back()); |
Benjamin Kramer | 6bf8af5 | 2014-10-06 15:31:04 +0000 | [diff] [blame] | 282 | } |
Alexey Samsonov | 8000e27 | 2014-06-09 21:53:47 +0000 | [diff] [blame] | 283 | } |
Alexey Samsonov | 414b6fb | 2014-04-30 21:34:11 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
Vedant Kumar | 7224c08 | 2018-06-01 22:33:15 +0000 | [diff] [blame] | 286 | |
| 287 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 288 | LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const { |
| 289 | dbgs() << "DbgValueHistoryMap:\n"; |
| 290 | for (const auto &VarRangePair : *this) { |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 291 | const InlinedEntity &Var = VarRangePair.first; |
Vedant Kumar | 7224c08 | 2018-06-01 22:33:15 +0000 | [diff] [blame] | 292 | const InstrRanges &Ranges = VarRangePair.second; |
| 293 | |
Hsiangkai Wang | 760c1ab | 2018-09-06 02:22:06 +0000 | [diff] [blame] | 294 | const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first); |
Vedant Kumar | 7224c08 | 2018-06-01 22:33:15 +0000 | [diff] [blame] | 295 | const DILocation *Location = Var.second; |
| 296 | |
| 297 | dbgs() << " - " << LocalVar->getName() << " at "; |
| 298 | |
| 299 | if (Location) |
| 300 | dbgs() << Location->getFilename() << ":" << Location->getLine() << ":" |
| 301 | << Location->getColumn(); |
| 302 | else |
| 303 | dbgs() << "<unknown location>"; |
| 304 | |
| 305 | dbgs() << " --\n"; |
| 306 | |
| 307 | for (const InstrRange &Range : Ranges) { |
| 308 | dbgs() << " Begin: " << *Range.first; |
| 309 | if (Range.second) |
| 310 | dbgs() << " End : " << *Range.second; |
| 311 | dbgs() << "\n"; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | #endif |