Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 1 | //===- LexicalScopes.cpp - Collecting lexical scope info ------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements LexicalScopes analysis. |
| 10 | // |
| 11 | // This pass collects lexical scope information and maps machine instructions |
| 12 | // to respective lexical scopes. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LexicalScopes.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineInstr.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 22 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DebugInfoMetadata.h" |
Reid Kleckner | 1d7b413 | 2019-10-19 00:22:07 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Metadata.h" |
| 26 | #include "llvm/Support/Casting.h" |
| 27 | #include "llvm/Support/Compiler.h" |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | 5db84df | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include <cassert> |
| 31 | #include <string> |
| 32 | #include <tuple> |
| 33 | #include <utility> |
| 34 | |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "lexicalscopes" |
| 38 | |
Eric Christopher | b7dee8a | 2013-11-20 00:54:28 +0000 | [diff] [blame] | 39 | /// reset - Reset the instance so that it's prepared for another function. |
| 40 | void LexicalScopes::reset() { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 41 | MF = nullptr; |
| 42 | CurrentFnLexicalScope = nullptr; |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 43 | LexicalScopeMap.clear(); |
| 44 | AbstractScopeMap.clear(); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 45 | InlinedLexicalScopeMap.clear(); |
| 46 | AbstractScopesList.clear(); |
Vedant Kumar | 1987626 | 2020-05-28 15:57:11 -0700 | [diff] [blame] | 47 | DominatedBlocks.clear(); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /// initialize - Scan machine function and constuct lexical scope nest. |
| 51 | void LexicalScopes::initialize(const MachineFunction &Fn) { |
Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 52 | reset(); |
Teresa Johnson | 76b5b74 | 2017-02-17 00:21:19 +0000 | [diff] [blame] | 53 | // Don't attempt any lexical scope creation for a NoDebug compile unit. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 54 | if (Fn.getFunction().getSubprogram()->getUnit()->getEmissionKind() == |
Teresa Johnson | 76b5b74 | 2017-02-17 00:21:19 +0000 | [diff] [blame] | 55 | DICompileUnit::NoDebug) |
| 56 | return; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 57 | MF = &Fn; |
| 58 | SmallVector<InsnRange, 4> MIRanges; |
| 59 | DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap; |
| 60 | extractLexicalScopes(MIRanges, MI2ScopeMap); |
| 61 | if (CurrentFnLexicalScope) { |
| 62 | constructScopeNest(CurrentFnLexicalScope); |
| 63 | assignInstructionRanges(MIRanges, MI2ScopeMap); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// extractLexicalScopes - Extract instruction ranges for each lexical scopes |
| 68 | /// for the given machine function. |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 69 | void LexicalScopes::extractLexicalScopes( |
| 70 | SmallVectorImpl<InsnRange> &MIRanges, |
| 71 | DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) { |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 72 | // Scan each instruction and create scopes. First build working set of scopes. |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 73 | for (const auto &MBB : *MF) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 74 | const MachineInstr *RangeBeginMI = nullptr; |
| 75 | const MachineInstr *PrevMI = nullptr; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 76 | const DILocation *PrevDL = nullptr; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 77 | for (const auto &MInsn : MBB) { |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 78 | // Check if instruction has valid location information. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 79 | const DILocation *MIDL = MInsn.getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 80 | if (!MIDL) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 81 | PrevMI = &MInsn; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 82 | continue; |
| 83 | } |
| 84 | |
| 85 | // If scope has not changed then skip this instruction. |
| 86 | if (MIDL == PrevDL) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 87 | PrevMI = &MInsn; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 88 | continue; |
| 89 | } |
| 90 | |
Adrian Prantl | fb31da1 | 2017-05-22 20:47:09 +0000 | [diff] [blame] | 91 | // Ignore DBG_VALUE and similar instruction that do not contribute to any |
| 92 | // instruction in the output. |
| 93 | if (MInsn.isMetaInstruction()) |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 94 | continue; |
| 95 | |
| 96 | if (RangeBeginMI) { |
| 97 | // If we have already seen a beginning of an instruction range and |
| 98 | // current instruction scope does not match scope of first instruction |
| 99 | // in this range then create a new instruction range. |
| 100 | InsnRange R(RangeBeginMI, PrevMI); |
| 101 | MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL); |
| 102 | MIRanges.push_back(R); |
| 103 | } |
| 104 | |
| 105 | // This is a beginning of a new instruction range. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 106 | RangeBeginMI = &MInsn; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 107 | |
| 108 | // Reset previous markers. |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 109 | PrevMI = &MInsn; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 110 | PrevDL = MIDL; |
| 111 | } |
| 112 | |
| 113 | // Create last instruction range. |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 114 | if (RangeBeginMI && PrevMI && PrevDL) { |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 115 | InsnRange R(RangeBeginMI, PrevMI); |
| 116 | MIRanges.push_back(R); |
| 117 | MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /// findLexicalScope - Find lexical scope, either regular or inlined, for the |
| 123 | /// given DebugLoc. Return NULL if not found. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 124 | LexicalScope *LexicalScopes::findLexicalScope(const DILocation *DL) { |
| 125 | DILocalScope *Scope = DL->getScope(); |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 126 | if (!Scope) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 127 | return nullptr; |
Eric Christopher | 6647b83 | 2011-10-11 22:59:11 +0000 | [diff] [blame] | 128 | |
| 129 | // The scope that we were created with could have an extra file - which |
| 130 | // isn't what we care about in this case. |
Amjad Aboud | a5ba991 | 2016-04-21 16:58:49 +0000 | [diff] [blame] | 131 | Scope = Scope->getNonLexicalBlockFileScope(); |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 132 | |
Duncan P. N. Exon Smith | 5a227ff | 2015-03-30 21:54:46 +0000 | [diff] [blame] | 133 | if (auto *IA = DL->getInlinedAt()) { |
David Blaikie | 9b8c8cd | 2014-05-14 01:08:28 +0000 | [diff] [blame] | 134 | auto I = InlinedLexicalScopeMap.find(std::make_pair(Scope, IA)); |
| 135 | return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr; |
| 136 | } |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 137 | return findLexicalScope(Scope); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If |
| 141 | /// not available then create new lexical scope. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 142 | LexicalScope *LexicalScopes::getOrCreateLexicalScope(const DILocalScope *Scope, |
| 143 | const DILocation *IA) { |
Duncan P. N. Exon Smith | 82eba74 | 2015-03-30 23:47:26 +0000 | [diff] [blame] | 144 | if (IA) { |
Teresa Johnson | 76b5b74 | 2017-02-17 00:21:19 +0000 | [diff] [blame] | 145 | // Skip scopes inlined from a NoDebug compile unit. |
| 146 | if (Scope->getSubprogram()->getUnit()->getEmissionKind() == |
| 147 | DICompileUnit::NoDebug) |
| 148 | return getOrCreateLexicalScope(IA); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 149 | // Create an abstract scope for inlined function. |
| 150 | getOrCreateAbstractScope(Scope); |
| 151 | // Create an inlined scope for inlined function. |
Duncan P. N. Exon Smith | 82eba74 | 2015-03-30 23:47:26 +0000 | [diff] [blame] | 152 | return getOrCreateInlinedScope(Scope, IA); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 153 | } |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 154 | |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 155 | return getOrCreateRegularScope(Scope); |
| 156 | } |
| 157 | |
| 158 | /// getOrCreateRegularScope - Find or create a regular lexical scope. |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 159 | LexicalScope * |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 160 | LexicalScopes::getOrCreateRegularScope(const DILocalScope *Scope) { |
Amjad Aboud | a5ba991 | 2016-04-21 16:58:49 +0000 | [diff] [blame] | 161 | assert(Scope && "Invalid Scope encoding!"); |
| 162 | Scope = Scope->getNonLexicalBlockFileScope(); |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 163 | |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 164 | auto I = LexicalScopeMap.find(Scope); |
| 165 | if (I != LexicalScopeMap.end()) |
| 166 | return &I->second; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 167 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 168 | // FIXME: Should the following dyn_cast be DILexicalBlock? |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 169 | LexicalScope *Parent = nullptr; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 170 | if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope)) |
Duncan P. N. Exon Smith | 82eba74 | 2015-03-30 23:47:26 +0000 | [diff] [blame] | 171 | Parent = getOrCreateLexicalScope(Block->getScope()); |
Aaron Ballman | f17583e | 2015-02-16 18:21:19 +0000 | [diff] [blame] | 172 | I = LexicalScopeMap.emplace(std::piecewise_construct, |
| 173 | std::forward_as_tuple(Scope), |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 174 | std::forward_as_tuple(Parent, Scope, nullptr, |
| 175 | false)).first; |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 176 | |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 177 | if (!Parent) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 178 | assert(cast<DISubprogram>(Scope)->describes(&MF->getFunction())); |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 179 | assert(!CurrentFnLexicalScope); |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 180 | CurrentFnLexicalScope = &I->second; |
David Blaikie | 3dfe478 | 2014-10-14 18:22:52 +0000 | [diff] [blame] | 181 | } |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 182 | |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 183 | return &I->second; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /// getOrCreateInlinedScope - Find or create an inlined lexical scope. |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 187 | LexicalScope * |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 188 | LexicalScopes::getOrCreateInlinedScope(const DILocalScope *Scope, |
| 189 | const DILocation *InlinedAt) { |
Amjad Aboud | a5ba991 | 2016-04-21 16:58:49 +0000 | [diff] [blame] | 190 | assert(Scope && "Invalid Scope encoding!"); |
| 191 | Scope = Scope->getNonLexicalBlockFileScope(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 192 | std::pair<const DILocalScope *, const DILocation *> P(Scope, InlinedAt); |
David Blaikie | 9b8c8cd | 2014-05-14 01:08:28 +0000 | [diff] [blame] | 193 | auto I = InlinedLexicalScopeMap.find(P); |
| 194 | if (I != InlinedLexicalScopeMap.end()) |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 195 | return &I->second; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 196 | |
David Blaikie | 9b8c8cd | 2014-05-14 01:08:28 +0000 | [diff] [blame] | 197 | LexicalScope *Parent; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 198 | if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope)) |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 199 | Parent = getOrCreateInlinedScope(Block->getScope(), InlinedAt); |
David Blaikie | 9b8c8cd | 2014-05-14 01:08:28 +0000 | [diff] [blame] | 200 | else |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 201 | Parent = getOrCreateLexicalScope(InlinedAt); |
David Blaikie | 9b8c8cd | 2014-05-14 01:08:28 +0000 | [diff] [blame] | 202 | |
Eric Christopher | 14303d1 | 2017-02-14 19:43:50 +0000 | [diff] [blame] | 203 | I = InlinedLexicalScopeMap |
| 204 | .emplace(std::piecewise_construct, std::forward_as_tuple(P), |
| 205 | std::forward_as_tuple(Parent, Scope, InlinedAt, false)) |
Aaron Ballman | f17583e | 2015-02-16 18:21:19 +0000 | [diff] [blame] | 206 | .first; |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 207 | return &I->second; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /// getOrCreateAbstractScope - Find or create an abstract lexical scope. |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 211 | LexicalScope * |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 212 | LexicalScopes::getOrCreateAbstractScope(const DILocalScope *Scope) { |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 213 | assert(Scope && "Invalid Scope encoding!"); |
Amjad Aboud | a5ba991 | 2016-04-21 16:58:49 +0000 | [diff] [blame] | 214 | Scope = Scope->getNonLexicalBlockFileScope(); |
David Blaikie | ea86226 | 2014-05-25 18:11:35 +0000 | [diff] [blame] | 215 | auto I = AbstractScopeMap.find(Scope); |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 216 | if (I != AbstractScopeMap.end()) |
| 217 | return &I->second; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 218 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 219 | // FIXME: Should the following isa be DILexicalBlock? |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 220 | LexicalScope *Parent = nullptr; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 221 | if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope)) |
Duncan P. N. Exon Smith | 33af7a8 | 2015-03-30 23:21:21 +0000 | [diff] [blame] | 222 | Parent = getOrCreateAbstractScope(Block->getScope()); |
| 223 | |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 224 | I = AbstractScopeMap.emplace(std::piecewise_construct, |
David Blaikie | ea86226 | 2014-05-25 18:11:35 +0000 | [diff] [blame] | 225 | std::forward_as_tuple(Scope), |
| 226 | std::forward_as_tuple(Parent, Scope, |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 227 | nullptr, true)).first; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 228 | if (isa<DISubprogram>(Scope)) |
David Blaikie | 2f143e0 | 2014-05-08 22:24:51 +0000 | [diff] [blame] | 229 | AbstractScopesList.push_back(&I->second); |
| 230 | return &I->second; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Jan-Willem Maessen | 3610d31 | 2020-06-08 18:38:16 +0100 | [diff] [blame^] | 233 | /// constructScopeNest - Traverse the Scope tree depth-first, storing |
| 234 | /// traversal state in WorkStack and recording the depth-first |
| 235 | /// numbering (setDFSIn, setDFSOut) for edge classification. |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 236 | void LexicalScopes::constructScopeNest(LexicalScope *Scope) { |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 237 | assert(Scope && "Unable to calculate scope dominance graph!"); |
Jan-Willem Maessen | 3610d31 | 2020-06-08 18:38:16 +0100 | [diff] [blame^] | 238 | SmallVector<std::pair<LexicalScope *, size_t>, 4> WorkStack; |
| 239 | WorkStack.push_back(std::make_pair(Scope, 0)); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 240 | unsigned Counter = 0; |
| 241 | while (!WorkStack.empty()) { |
Jan-Willem Maessen | 3610d31 | 2020-06-08 18:38:16 +0100 | [diff] [blame^] | 242 | auto &ScopePosition = WorkStack.back(); |
| 243 | LexicalScope *WS = ScopePosition.first; |
| 244 | size_t ChildNum = ScopePosition.second++; |
Craig Topper | 80170e5 | 2013-07-03 04:30:58 +0000 | [diff] [blame] | 245 | const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren(); |
Jan-Willem Maessen | 3610d31 | 2020-06-08 18:38:16 +0100 | [diff] [blame^] | 246 | if (ChildNum < Children.size()) { |
| 247 | auto &ChildScope = Children[ChildNum]; |
| 248 | WorkStack.push_back(std::make_pair(ChildScope, 0)); |
| 249 | ChildScope->setDFSIn(++Counter); |
| 250 | } else { |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 251 | WorkStack.pop_back(); |
| 252 | WS->setDFSOut(++Counter); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Devang Patel | 784077e | 2011-08-10 23:58:09 +0000 | [diff] [blame] | 257 | /// assignInstructionRanges - Find ranges of instructions covered by each |
| 258 | /// lexical scope. |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 259 | void LexicalScopes::assignInstructionRanges( |
| 260 | SmallVectorImpl<InsnRange> &MIRanges, |
| 261 | DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 262 | LexicalScope *PrevLexicalScope = nullptr; |
Adrian Prantl | 16b2ace | 2016-09-28 17:31:17 +0000 | [diff] [blame] | 263 | for (const auto &R : MIRanges) { |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 264 | LexicalScope *S = MI2ScopeMap.lookup(R.first); |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 265 | assert(S && "Lost LexicalScope for a machine instruction!"); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 266 | if (PrevLexicalScope && !PrevLexicalScope->dominates(S)) |
| 267 | PrevLexicalScope->closeInsnRange(S); |
| 268 | S->openInsnRange(R.first); |
| 269 | S->extendInsnRange(R.second); |
| 270 | PrevLexicalScope = S; |
| 271 | } |
| 272 | |
| 273 | if (PrevLexicalScope) |
| 274 | PrevLexicalScope->closeInsnRange(); |
| 275 | } |
| 276 | |
| 277 | /// getMachineBasicBlocks - Populate given set using machine basic blocks which |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 278 | /// have machine instructions that belong to lexical scope identified by |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 279 | /// DebugLoc. |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 280 | void LexicalScopes::getMachineBasicBlocks( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 281 | const DILocation *DL, SmallPtrSetImpl<const MachineBasicBlock *> &MBBs) { |
Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 282 | assert(MF && "Method called on a uninitialized LexicalScopes object!"); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 283 | MBBs.clear(); |
Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 284 | |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 285 | LexicalScope *Scope = getOrCreateLexicalScope(DL); |
| 286 | if (!Scope) |
| 287 | return; |
Eric Christopher | 6211e4b | 2013-11-20 00:54:19 +0000 | [diff] [blame] | 288 | |
Devang Patel | db4374a | 2011-08-12 18:01:34 +0000 | [diff] [blame] | 289 | if (Scope == CurrentFnLexicalScope) { |
Alexey Samsonov | 41b977d | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 290 | for (const auto &MBB : *MF) |
| 291 | MBBs.insert(&MBB); |
Devang Patel | db4374a | 2011-08-12 18:01:34 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
Jeremy Morse | 6af859dcc | 2020-02-28 10:41:23 +0000 | [diff] [blame] | 295 | // The scope ranges can cover multiple basic blocks in each span. Iterate over |
| 296 | // all blocks (in the order they are in the function) until we reach the one |
| 297 | // containing the end of the span. |
Craig Topper | 80170e5 | 2013-07-03 04:30:58 +0000 | [diff] [blame] | 298 | SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges(); |
Adrian Prantl | 16b2ace | 2016-09-28 17:31:17 +0000 | [diff] [blame] | 299 | for (auto &R : InsnRanges) |
Jeremy Morse | 6af859dcc | 2020-02-28 10:41:23 +0000 | [diff] [blame] | 300 | for (auto CurMBBIt = R.first->getParent()->getIterator(), |
| 301 | EndBBIt = std::next(R.second->getParent()->getIterator()); |
| 302 | CurMBBIt != EndBBIt; CurMBBIt++) |
| 303 | MBBs.insert(&*CurMBBIt); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 306 | bool LexicalScopes::dominates(const DILocation *DL, MachineBasicBlock *MBB) { |
Wolfgang Pieb | e018bbd | 2017-07-19 19:36:40 +0000 | [diff] [blame] | 307 | assert(MF && "Unexpected uninitialized LexicalScopes object!"); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 308 | LexicalScope *Scope = getOrCreateLexicalScope(DL); |
| 309 | if (!Scope) |
| 310 | return false; |
Devang Patel | db4374a | 2011-08-12 18:01:34 +0000 | [diff] [blame] | 311 | |
| 312 | // Current function scope covers all basic blocks in the function. |
| 313 | if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF) |
| 314 | return true; |
| 315 | |
Jeremy Morse | 6af859dcc | 2020-02-28 10:41:23 +0000 | [diff] [blame] | 316 | // Fetch all the blocks in DLs scope. Because the range / block list also |
Vedant Kumar | 1987626 | 2020-05-28 15:57:11 -0700 | [diff] [blame] | 317 | // contain any subscopes, any instruction that DL dominates can be found in |
| 318 | // the block set. |
| 319 | // |
| 320 | // Cache the set of fetched blocks to avoid repeatedly recomputing the set in |
| 321 | // the LiveDebugValues pass. |
| 322 | std::unique_ptr<BlockSetT> &Set = DominatedBlocks[DL]; |
| 323 | if (!Set) { |
| 324 | Set = std::make_unique<BlockSetT>(); |
| 325 | getMachineBasicBlocks(DL, *Set); |
| 326 | } |
| 327 | return Set->count(MBB) != 0; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 330 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 331 | LLVM_DUMP_METHOD void LexicalScope::dump(unsigned Indent) const { |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 332 | raw_ostream &err = dbgs(); |
Manman Ren | e498b25 | 2013-02-02 00:02:03 +0000 | [diff] [blame] | 333 | err.indent(Indent); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 334 | err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n"; |
| 335 | const MDNode *N = Desc; |
Manman Ren | e498b25 | 2013-02-02 00:02:03 +0000 | [diff] [blame] | 336 | err.indent(Indent); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 337 | N->dump(); |
| 338 | if (AbstractScope) |
Manman Ren | e498b25 | 2013-02-02 00:02:03 +0000 | [diff] [blame] | 339 | err << std::string(Indent, ' ') << "Abstract Scope\n"; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 340 | |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 341 | if (!Children.empty()) |
Manman Ren | e498b25 | 2013-02-02 00:02:03 +0000 | [diff] [blame] | 342 | err << std::string(Indent + 2, ' ') << "Children ...\n"; |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 343 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
| 344 | if (Children[i] != this) |
Manman Ren | e498b25 | 2013-02-02 00:02:03 +0000 | [diff] [blame] | 345 | Children[i]->dump(Indent + 2); |
Devang Patel | e1649c3 | 2011-08-10 19:04:06 +0000 | [diff] [blame] | 346 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 347 | #endif |