36 #define DEBUG_TYPE "lexicalscopes"
41 CurrentFnLexicalScope =
nullptr;
42 LexicalScopeMap.clear();
43 AbstractScopeMap.clear();
44 InlinedLexicalScopeMap.clear();
45 AbstractScopesList.clear();
58 extractLexicalScopes(MIRanges, MI2ScopeMap);
59 if (CurrentFnLexicalScope) {
60 constructScopeNest(CurrentFnLexicalScope);
61 assignInstructionRanges(MIRanges, MI2ScopeMap);
67 void LexicalScopes::extractLexicalScopes(
71 for (
const auto &MBB : *MF) {
75 for (
const auto &MInsn : MBB) {
91 if (MInsn.isMetaInstruction())
99 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
104 RangeBeginMI = &MInsn;
112 if (RangeBeginMI && PrevMI && PrevDL) {
115 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
131 if (
auto *IA = DL->getInlinedAt()) {
132 auto I = InlinedLexicalScopeMap.find(std::make_pair(Scope, IA));
133 return I != InlinedLexicalScopeMap.end() ? &
I->second :
nullptr;
146 return getOrCreateLexicalScope(IA);
150 return getOrCreateInlinedScope(Scope, IA);
153 return getOrCreateRegularScope(Scope);
158 LexicalScopes::getOrCreateRegularScope(
const DILocalScope *Scope) {
159 assert(Scope &&
"Invalid Scope encoding!");
162 auto I = LexicalScopeMap.find(Scope);
163 if (
I != LexicalScopeMap.end())
168 if (
auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
169 Parent = getOrCreateLexicalScope(
Block->getScope());
170 I = LexicalScopeMap.emplace(std::piecewise_construct,
171 std::forward_as_tuple(Scope),
172 std::forward_as_tuple(Parent, Scope,
nullptr,
176 assert(cast<DISubprogram>(Scope)->describes(MF->getFunction()));
177 assert(!CurrentFnLexicalScope);
178 CurrentFnLexicalScope = &
I->second;
186 LexicalScopes::getOrCreateInlinedScope(
const DILocalScope *Scope,
188 assert(Scope &&
"Invalid Scope encoding!");
190 std::pair<const DILocalScope *, const DILocation *>
P(Scope, InlinedAt);
191 auto I = InlinedLexicalScopeMap.find(
P);
192 if (
I != InlinedLexicalScopeMap.end())
196 if (
auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
197 Parent = getOrCreateInlinedScope(
Block->getScope(), InlinedAt);
199 Parent = getOrCreateLexicalScope(InlinedAt);
201 I = InlinedLexicalScopeMap
202 .emplace(std::piecewise_construct, std::forward_as_tuple(
P),
203 std::forward_as_tuple(Parent, Scope, InlinedAt,
false))
211 assert(Scope &&
"Invalid Scope encoding!");
213 auto I = AbstractScopeMap.find(Scope);
214 if (
I != AbstractScopeMap.end())
219 if (
auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
222 I = AbstractScopeMap.emplace(std::piecewise_construct,
223 std::forward_as_tuple(Scope),
224 std::forward_as_tuple(Parent, Scope,
225 nullptr,
true)).first;
226 if (isa<DISubprogram>(Scope))
227 AbstractScopesList.push_back(&
I->second);
232 void LexicalScopes::constructScopeNest(
LexicalScope *Scope) {
233 assert(Scope &&
"Unable to calculate scope dominance graph!");
237 while (!WorkStack.
empty()) {
240 bool visitedChildren =
false;
241 for (
auto &ChildScope : Children)
242 if (!ChildScope->getDFSOut()) {
244 visitedChildren =
true;
245 ChildScope->setDFSIn(++Counter);
248 if (!visitedChildren) {
257 void LexicalScopes::assignInstructionRanges(
261 for (
const auto &R : MIRanges) {
263 assert(S &&
"Lost LexicalScope for a machine instruction!");
264 if (PrevLexicalScope && !PrevLexicalScope->
dominates(S))
268 PrevLexicalScope = S;
271 if (PrevLexicalScope)
285 if (Scope == CurrentFnLexicalScope) {
286 for (
const auto &MBB : *MF)
292 for (
auto &R : InsnRanges)
293 MBBs.
insert(R.first->getParent());
304 if (Scope == CurrentFnLexicalScope && MBB->
getParent() == MF)
308 for (
auto &
I : *MBB) {
310 if (
LexicalScope *IScope = getOrCreateLexicalScope(IDL))
317 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
321 err <<
"DFSIn: " << DFSIn <<
" DFSOut: " << DFSOut <<
"\n";
326 err << std::string(Indent,
' ') <<
"Abstract Scope\n";
328 if (!Children.empty())
329 err << std::string(Indent + 2,
' ') <<
"Children ...\n";
330 for (
unsigned i = 0, e = Children.size(); i != e; ++i)
331 if (Children[i] !=
this)
332 Children[i]->dump(Indent + 2);
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void openInsnRange(const MachineInstr *MI)
openInsnRange - This scope covers instruction range starting from MI.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
void extendInsnRange(const MachineInstr *MI)
extendInsnRange - Extend the current instruction range covered by this scope.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
bool dominates(const DILocation *DL, MachineBasicBlock *MBB)
dominates - Return true if DebugLoc's lexical scope dominates at least one machine instruction's lexi...
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
SmallVectorImpl< InsnRange > & getRanges()
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
LexicalScope - This class is used to track scope information.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
DILocalScope * getNonLexicalBlockFileScope() const
Get the first non DILexicalBlockFile scope of this scope.
LexicalScope * getOrCreateAbstractScope(const DILocalScope *Scope)
getOrCreateAbstractScope - Find or create an abstract lexical scope.
SmallVectorImpl< LexicalScope * > & getChildren()
void setDFSOut(unsigned O)
void reset()
releaseMemory - release memory.
LLVM_NODISCARD bool empty() const
void initialize(const MachineFunction &)
initialize - Scan machine function and constuct lexical scope nest, resets the instance if necessary...
LexicalScope * findLexicalScope(const DILocation *DL)
findLexicalScope - Find lexical scope, either regular or inlined, for the given DebugLoc.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool dominates(const LexicalScope *S) const
dominates - Return true if current scope dominates given lexical scope.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void getMachineBasicBlocks(const DILocation *DL, SmallPtrSetImpl< const MachineBasicBlock * > &MBBs)
getMachineBasicBlocks - Populate given set using machine basic blocks which have machine instructions...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Representation of each machine instruction.
void closeInsnRange(LexicalScope *NewScope=nullptr)
closeInsnRange - Create a range based on FirstInsn and LastInsn collected until now.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
void dump(unsigned Indent=0) const
dump - print lexical scope.
std::pair< const MachineInstr *, const MachineInstr * > InsnRange
InsnRange - This is used to track range of instructions with identical lexical scope.