LexicalScopes.h 9.8 KB

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