LexicalScopes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LexicalScopes.cpp - Collecting lexical scope info --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements LexicalScopes analysis.
  15. //
  16. // This pass collects lexical scope information and maps machine instructions
  17. // to respective lexical scopes.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_CODEGEN_LEXICALSCOPES_H
  21. #define LLVM_CODEGEN_LEXICALSCOPES_H
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/IR/DebugInfoMetadata.h"
  27. #include <cassert>
  28. #include <unordered_map>
  29. #include <utility>
  30. namespace llvm {
  31. class MachineBasicBlock;
  32. class MachineFunction;
  33. class MachineInstr;
  34. class MDNode;
  35. //===----------------------------------------------------------------------===//
  36. /// InsnRange - This is used to track range of instructions with identical
  37. /// lexical scope.
  38. ///
  39. using InsnRange = std::pair<const MachineInstr *, const MachineInstr *>;
  40. //===----------------------------------------------------------------------===//
  41. /// LexicalScope - This class is used to track scope information.
  42. ///
  43. class LexicalScope {
  44. public:
  45. LexicalScope(LexicalScope *P, const DILocalScope *D, const DILocation *I,
  46. bool A)
  47. : Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A) {
  48. assert(D);
  49. assert(D->getSubprogram()->getUnit()->getEmissionKind() !=
  50. DICompileUnit::NoDebug &&
  51. "Don't build lexical scopes for non-debug locations");
  52. assert(D->isResolved() && "Expected resolved node");
  53. assert((!I || I->isResolved()) && "Expected resolved node");
  54. if (Parent)
  55. Parent->addChild(this);
  56. }
  57. // Accessors.
  58. LexicalScope *getParent() const { return Parent; }
  59. const MDNode *getDesc() const { return Desc; }
  60. const DILocation *getInlinedAt() const { return InlinedAtLocation; }
  61. const DILocalScope *getScopeNode() const { return Desc; }
  62. bool isAbstractScope() const { return AbstractScope; }
  63. SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
  64. SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
  65. /// addChild - Add a child scope.
  66. void addChild(LexicalScope *S) { Children.push_back(S); }
  67. /// openInsnRange - This scope covers instruction range starting from MI.
  68. void openInsnRange(const MachineInstr *MI) {
  69. if (!FirstInsn)
  70. FirstInsn = MI;
  71. if (Parent)
  72. Parent->openInsnRange(MI);
  73. }
  74. /// extendInsnRange - Extend the current instruction range covered by
  75. /// this scope.
  76. void extendInsnRange(const MachineInstr *MI) {
  77. assert(FirstInsn && "MI Range is not open!");
  78. LastInsn = MI;
  79. if (Parent)
  80. Parent->extendInsnRange(MI);
  81. }
  82. /// closeInsnRange - Create a range based on FirstInsn and LastInsn collected
  83. /// until now. This is used when a new scope is encountered while walking
  84. /// machine instructions.
  85. void closeInsnRange(LexicalScope *NewScope = nullptr) {
  86. assert(LastInsn && "Last insn missing!");
  87. Ranges.push_back(InsnRange(FirstInsn, LastInsn));
  88. FirstInsn = nullptr;
  89. LastInsn = nullptr;
  90. // If Parent dominates NewScope then do not close Parent's instruction
  91. // range.
  92. if (Parent && (!NewScope || !Parent->dominates(NewScope)))
  93. Parent->closeInsnRange(NewScope);
  94. }
  95. /// dominates - Return true if current scope dominates given lexical scope.
  96. bool dominates(const LexicalScope *S) const {
  97. if (S == this)
  98. return true;
  99. if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
  100. return true;
  101. return false;
  102. }
  103. // Depth First Search support to walk and manipulate LexicalScope hierarchy.
  104. unsigned getDFSOut() const { return DFSOut; }
  105. void setDFSOut(unsigned O) { DFSOut = O; }
  106. unsigned getDFSIn() const { return DFSIn; }
  107. void setDFSIn(unsigned I) { DFSIn = I; }
  108. /// dump - print lexical scope.
  109. void dump(unsigned Indent = 0) const;
  110. private:
  111. LexicalScope *Parent; // Parent to this scope.
  112. const DILocalScope *Desc; // Debug info descriptor.
  113. const DILocation *InlinedAtLocation; // Location at which this
  114. // scope is inlined.
  115. bool AbstractScope; // Abstract Scope
  116. SmallVector<LexicalScope *, 4> Children; // Scopes defined in scope.
  117. // Contents not owned.
  118. SmallVector<InsnRange, 4> Ranges;
  119. const MachineInstr *LastInsn = nullptr; // Last instruction of this scope.
  120. const MachineInstr *FirstInsn = nullptr; // First instruction of this scope.
  121. unsigned DFSIn = 0; // In & Out Depth use to determine scope nesting.
  122. unsigned DFSOut = 0;
  123. };
  124. //===----------------------------------------------------------------------===//
  125. /// LexicalScopes - This class provides interface to collect and use lexical
  126. /// scoping information from machine instruction.
  127. ///
  128. class LexicalScopes {
  129. public:
  130. LexicalScopes() = default;
  131. /// initialize - Scan machine function and constuct lexical scope nest, resets
  132. /// the instance if necessary.
  133. void initialize(const MachineFunction &);
  134. /// releaseMemory - release memory.
  135. void reset();
  136. /// empty - Return true if there is any lexical scope information available.
  137. bool empty() { return CurrentFnLexicalScope == nullptr; }
  138. /// getCurrentFunctionScope - Return lexical scope for the current function.
  139. LexicalScope *getCurrentFunctionScope() const {
  140. return CurrentFnLexicalScope;
  141. }
  142. /// getMachineBasicBlocks - Populate given set using machine basic blocks
  143. /// which have machine instructions that belong to lexical scope identified by
  144. /// DebugLoc.
  145. void getMachineBasicBlocks(const DILocation *DL,
  146. SmallPtrSetImpl<const MachineBasicBlock *> &MBBs);
  147. /// Return true if DebugLoc's lexical scope dominates at least one machine
  148. /// instruction's lexical scope in a given machine basic block.
  149. bool dominates(const DILocation *DL, MachineBasicBlock *MBB);
  150. /// findLexicalScope - Find lexical scope, either regular or inlined, for the
  151. /// given DebugLoc. Return NULL if not found.
  152. LexicalScope *findLexicalScope(const DILocation *DL);
  153. /// getAbstractScopesList - Return a reference to list of abstract scopes.
  154. ArrayRef<LexicalScope *> getAbstractScopesList() const {
  155. return AbstractScopesList;
  156. }
  157. /// findAbstractScope - Find an abstract scope or return null.
  158. LexicalScope *findAbstractScope(const DILocalScope *N) {
  159. auto I = AbstractScopeMap.find(N);
  160. return I != AbstractScopeMap.end() ? &I->second : nullptr;
  161. }
  162. /// findInlinedScope - Find an inlined scope for the given scope/inlined-at.
  163. LexicalScope *findInlinedScope(const DILocalScope *N, const DILocation *IA) {
  164. auto I = InlinedLexicalScopeMap.find(std::make_pair(N, IA));
  165. return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr;
  166. }
  167. /// findLexicalScope - Find regular lexical scope or return null.
  168. LexicalScope *findLexicalScope(const DILocalScope *N) {
  169. auto I = LexicalScopeMap.find(N);
  170. return I != LexicalScopeMap.end() ? &I->second : nullptr;
  171. }
  172. /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
  173. LexicalScope *getOrCreateAbstractScope(const DILocalScope *Scope);
  174. private:
  175. /// getOrCreateLexicalScope - Find lexical scope for the given Scope/IA. If
  176. /// not available then create new lexical scope.
  177. LexicalScope *getOrCreateLexicalScope(const DILocalScope *Scope,
  178. const DILocation *IA = nullptr);
  179. LexicalScope *getOrCreateLexicalScope(const DILocation *DL) {
  180. return DL ? getOrCreateLexicalScope(DL->getScope(), DL->getInlinedAt())
  181. : nullptr;
  182. }
  183. /// getOrCreateRegularScope - Find or create a regular lexical scope.
  184. LexicalScope *getOrCreateRegularScope(const DILocalScope *Scope);
  185. /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
  186. LexicalScope *getOrCreateInlinedScope(const DILocalScope *Scope,
  187. const DILocation *InlinedAt);
  188. /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
  189. /// for the given machine function.
  190. void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
  191. DenseMap<const MachineInstr *, LexicalScope *> &M);
  192. void constructScopeNest(LexicalScope *Scope);
  193. void
  194. assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
  195. DenseMap<const MachineInstr *, LexicalScope *> &M);
  196. const MachineFunction *MF = nullptr;
  197. /// LexicalScopeMap - Tracks the scopes in the current function.
  198. // Use an unordered_map to ensure value pointer validity over insertion.
  199. std::unordered_map<const DILocalScope *, LexicalScope> LexicalScopeMap;
  200. /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
  201. /// function.
  202. std::unordered_map<std::pair<const DILocalScope *, const DILocation *>,
  203. LexicalScope,
  204. pair_hash<const DILocalScope *, const DILocation *>>
  205. InlinedLexicalScopeMap;
  206. /// AbstractScopeMap - These scopes are not included LexicalScopeMap.
  207. // Use an unordered_map to ensure value pointer validity over insertion.
  208. std::unordered_map<const DILocalScope *, LexicalScope> AbstractScopeMap;
  209. /// AbstractScopesList - Tracks abstract scopes constructed while processing
  210. /// a function.
  211. SmallVector<LexicalScope *, 4> AbstractScopesList;
  212. /// CurrentFnLexicalScope - Top level scope for the current function.
  213. ///
  214. LexicalScope *CurrentFnLexicalScope = nullptr;
  215. /// Map a location to the set of basic blocks it dominates. This is a cache
  216. /// for \ref LexicalScopes::getMachineBasicBlocks results.
  217. using BlockSetT = SmallPtrSet<const MachineBasicBlock *, 4>;
  218. DenseMap<const DILocation *, std::unique_ptr<BlockSetT>> DominatedBlocks;
  219. };
  220. } // end namespace llvm
  221. #endif // LLVM_CODEGEN_LEXICALSCOPES_H
  222. #ifdef __GNUC__
  223. #pragma GCC diagnostic pop
  224. #endif