Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
9.1 KiB

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