LexicalScopes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //===- LexicalScopes.cpp - Collecting lexical scope info ------------------===//
  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. #include "llvm/CodeGen/LexicalScopes.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineInstr.h"
  19. #include "llvm/IR/DebugInfo.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/FormattedStream.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "lexicalscopes"
  26. /// reset - Reset the instance so that it's prepared for another function.
  27. void LexicalScopes::reset() {
  28. MF = nullptr;
  29. CurrentFnLexicalScope = nullptr;
  30. LexicalScopeMap.clear();
  31. AbstractScopeMap.clear();
  32. InlinedLexicalScopeMap.clear();
  33. AbstractScopesList.clear();
  34. }
  35. /// initialize - Scan machine function and constuct lexical scope nest.
  36. void LexicalScopes::initialize(const MachineFunction &Fn) {
  37. // Don't attempt any lexical scope creation for a NoDebug compile unit.
  38. if (Fn.getFunction()->getSubprogram()->getUnit()->getEmissionKind() ==
  39. DICompileUnit::NoDebug)
  40. return;
  41. reset();
  42. MF = &Fn;
  43. SmallVector<InsnRange, 4> MIRanges;
  44. DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap;
  45. extractLexicalScopes(MIRanges, MI2ScopeMap);
  46. if (CurrentFnLexicalScope) {
  47. constructScopeNest(CurrentFnLexicalScope);
  48. assignInstructionRanges(MIRanges, MI2ScopeMap);
  49. }
  50. }
  51. /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
  52. /// for the given machine function.
  53. void LexicalScopes::extractLexicalScopes(
  54. SmallVectorImpl<InsnRange> &MIRanges,
  55. DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
  56. // Scan each instruction and create scopes. First build working set of scopes.
  57. for (const auto &MBB : *MF) {
  58. const MachineInstr *RangeBeginMI = nullptr;
  59. const MachineInstr *PrevMI = nullptr;
  60. const DILocation *PrevDL = nullptr;
  61. for (const auto &MInsn : MBB) {
  62. // Check if instruction has valid location information.
  63. const DILocation *MIDL = MInsn.getDebugLoc();
  64. if (!MIDL) {
  65. PrevMI = &MInsn;
  66. continue;
  67. }
  68. // If scope has not changed then skip this instruction.
  69. if (MIDL == PrevDL) {
  70. PrevMI = &MInsn;
  71. continue;
  72. }
  73. // Ignore DBG_VALUE. It does not contribute to any instruction in output.
  74. if (MInsn.isDebugValue())
  75. continue;
  76. if (RangeBeginMI) {
  77. // If we have already seen a beginning of an instruction range and
  78. // current instruction scope does not match scope of first instruction
  79. // in this range then create a new instruction range.
  80. InsnRange R(RangeBeginMI, PrevMI);
  81. MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
  82. MIRanges.push_back(R);
  83. }
  84. // This is a beginning of a new instruction range.
  85. RangeBeginMI = &MInsn;
  86. // Reset previous markers.
  87. PrevMI = &MInsn;
  88. PrevDL = MIDL;
  89. }
  90. // Create last instruction range.
  91. if (RangeBeginMI && PrevMI && PrevDL) {
  92. InsnRange R(RangeBeginMI, PrevMI);
  93. MIRanges.push_back(R);
  94. MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
  95. }
  96. }
  97. }
  98. /// findLexicalScope - Find lexical scope, either regular or inlined, for the
  99. /// given DebugLoc. Return NULL if not found.
  100. LexicalScope *LexicalScopes::findLexicalScope(const DILocation *DL) {
  101. DILocalScope *Scope = DL->getScope();
  102. if (!Scope)
  103. return nullptr;
  104. // The scope that we were created with could have an extra file - which
  105. // isn't what we care about in this case.
  106. Scope = Scope->getNonLexicalBlockFileScope();
  107. if (auto *IA = DL->getInlinedAt()) {
  108. auto I = InlinedLexicalScopeMap.find(std::make_pair(Scope, IA));
  109. return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr;
  110. }
  111. return findLexicalScope(Scope);
  112. }
  113. /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
  114. /// not available then create new lexical scope.
  115. LexicalScope *LexicalScopes::getOrCreateLexicalScope(const DILocalScope *Scope,
  116. const DILocation *IA) {
  117. if (IA) {
  118. // Skip scopes inlined from a NoDebug compile unit.
  119. if (Scope->getSubprogram()->getUnit()->getEmissionKind() ==
  120. DICompileUnit::NoDebug)
  121. return getOrCreateLexicalScope(IA);
  122. // Create an abstract scope for inlined function.
  123. getOrCreateAbstractScope(Scope);
  124. // Create an inlined scope for inlined function.
  125. return getOrCreateInlinedScope(Scope, IA);
  126. }
  127. return getOrCreateRegularScope(Scope);
  128. }
  129. /// getOrCreateRegularScope - Find or create a regular lexical scope.
  130. LexicalScope *
  131. LexicalScopes::getOrCreateRegularScope(const DILocalScope *Scope) {
  132. assert(Scope && "Invalid Scope encoding!");
  133. Scope = Scope->getNonLexicalBlockFileScope();
  134. auto I = LexicalScopeMap.find(Scope);
  135. if (I != LexicalScopeMap.end())
  136. return &I->second;
  137. // FIXME: Should the following dyn_cast be DILexicalBlock?
  138. LexicalScope *Parent = nullptr;
  139. if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
  140. Parent = getOrCreateLexicalScope(Block->getScope());
  141. I = LexicalScopeMap.emplace(std::piecewise_construct,
  142. std::forward_as_tuple(Scope),
  143. std::forward_as_tuple(Parent, Scope, nullptr,
  144. false)).first;
  145. if (!Parent) {
  146. assert(cast<DISubprogram>(Scope)->describes(MF->getFunction()));
  147. assert(!CurrentFnLexicalScope);
  148. CurrentFnLexicalScope = &I->second;
  149. }
  150. return &I->second;
  151. }
  152. /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
  153. LexicalScope *
  154. LexicalScopes::getOrCreateInlinedScope(const DILocalScope *Scope,
  155. const DILocation *InlinedAt) {
  156. assert(Scope && "Invalid Scope encoding!");
  157. Scope = Scope->getNonLexicalBlockFileScope();
  158. std::pair<const DILocalScope *, const DILocation *> P(Scope, InlinedAt);
  159. auto I = InlinedLexicalScopeMap.find(P);
  160. if (I != InlinedLexicalScopeMap.end())
  161. return &I->second;
  162. LexicalScope *Parent;
  163. if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
  164. Parent = getOrCreateInlinedScope(Block->getScope(), InlinedAt);
  165. else
  166. Parent = getOrCreateLexicalScope(InlinedAt);
  167. I = InlinedLexicalScopeMap
  168. .emplace(std::piecewise_construct, std::forward_as_tuple(P),
  169. std::forward_as_tuple(Parent, Scope, InlinedAt, false))
  170. .first;
  171. return &I->second;
  172. }
  173. /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
  174. LexicalScope *
  175. LexicalScopes::getOrCreateAbstractScope(const DILocalScope *Scope) {
  176. assert(Scope && "Invalid Scope encoding!");
  177. Scope = Scope->getNonLexicalBlockFileScope();
  178. auto I = AbstractScopeMap.find(Scope);
  179. if (I != AbstractScopeMap.end())
  180. return &I->second;
  181. // FIXME: Should the following isa be DILexicalBlock?
  182. LexicalScope *Parent = nullptr;
  183. if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
  184. Parent = getOrCreateAbstractScope(Block->getScope());
  185. I = AbstractScopeMap.emplace(std::piecewise_construct,
  186. std::forward_as_tuple(Scope),
  187. std::forward_as_tuple(Parent, Scope,
  188. nullptr, true)).first;
  189. if (isa<DISubprogram>(Scope))
  190. AbstractScopesList.push_back(&I->second);
  191. return &I->second;
  192. }
  193. /// constructScopeNest
  194. void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
  195. assert(Scope && "Unable to calculate scope dominance graph!");
  196. SmallVector<LexicalScope *, 4> WorkStack;
  197. WorkStack.push_back(Scope);
  198. unsigned Counter = 0;
  199. while (!WorkStack.empty()) {
  200. LexicalScope *WS = WorkStack.back();
  201. const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
  202. bool visitedChildren = false;
  203. for (auto &ChildScope : Children)
  204. if (!ChildScope->getDFSOut()) {
  205. WorkStack.push_back(ChildScope);
  206. visitedChildren = true;
  207. ChildScope->setDFSIn(++Counter);
  208. break;
  209. }
  210. if (!visitedChildren) {
  211. WorkStack.pop_back();
  212. WS->setDFSOut(++Counter);
  213. }
  214. }
  215. }
  216. /// assignInstructionRanges - Find ranges of instructions covered by each
  217. /// lexical scope.
  218. void LexicalScopes::assignInstructionRanges(
  219. SmallVectorImpl<InsnRange> &MIRanges,
  220. DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
  221. LexicalScope *PrevLexicalScope = nullptr;
  222. for (const auto &R : MIRanges) {
  223. LexicalScope *S = MI2ScopeMap.lookup(R.first);
  224. assert(S && "Lost LexicalScope for a machine instruction!");
  225. if (PrevLexicalScope && !PrevLexicalScope->dominates(S))
  226. PrevLexicalScope->closeInsnRange(S);
  227. S->openInsnRange(R.first);
  228. S->extendInsnRange(R.second);
  229. PrevLexicalScope = S;
  230. }
  231. if (PrevLexicalScope)
  232. PrevLexicalScope->closeInsnRange();
  233. }
  234. /// getMachineBasicBlocks - Populate given set using machine basic blocks which
  235. /// have machine instructions that belong to lexical scope identified by
  236. /// DebugLoc.
  237. void LexicalScopes::getMachineBasicBlocks(
  238. const DILocation *DL, SmallPtrSetImpl<const MachineBasicBlock *> &MBBs) {
  239. MBBs.clear();
  240. LexicalScope *Scope = getOrCreateLexicalScope(DL);
  241. if (!Scope)
  242. return;
  243. if (Scope == CurrentFnLexicalScope) {
  244. for (const auto &MBB : *MF)
  245. MBBs.insert(&MBB);
  246. return;
  247. }
  248. SmallVectorImpl<InsnRange> &InsnRanges = Scope->getRanges();
  249. for (auto &R : InsnRanges)
  250. MBBs.insert(R.first->getParent());
  251. }
  252. /// dominates - Return true if DebugLoc's lexical scope dominates at least one
  253. /// machine instruction's lexical scope in a given machine basic block.
  254. bool LexicalScopes::dominates(const DILocation *DL, MachineBasicBlock *MBB) {
  255. LexicalScope *Scope = getOrCreateLexicalScope(DL);
  256. if (!Scope)
  257. return false;
  258. // Current function scope covers all basic blocks in the function.
  259. if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
  260. return true;
  261. bool Result = false;
  262. for (auto &I : *MBB) {
  263. if (const DILocation *IDL = I.getDebugLoc())
  264. if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
  265. if (Scope->dominates(IScope))
  266. return true;
  267. }
  268. return Result;
  269. }
  270. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  271. LLVM_DUMP_METHOD void LexicalScope::dump(unsigned Indent) const {
  272. raw_ostream &err = dbgs();
  273. err.indent(Indent);
  274. err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
  275. const MDNode *N = Desc;
  276. err.indent(Indent);
  277. N->dump();
  278. if (AbstractScope)
  279. err << std::string(Indent, ' ') << "Abstract Scope\n";
  280. if (!Children.empty())
  281. err << std::string(Indent + 2, ' ') << "Children ...\n";
  282. for (unsigned i = 0, e = Children.size(); i != e; ++i)
  283. if (Children[i] != this)
  284. Children[i]->dump(Indent + 2);
  285. }
  286. #endif