2
0

LexicalScopes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. reset();
  38. MF = &Fn;
  39. SmallVector<InsnRange, 4> MIRanges;
  40. DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap;
  41. extractLexicalScopes(MIRanges, MI2ScopeMap);
  42. if (CurrentFnLexicalScope) {
  43. constructScopeNest(CurrentFnLexicalScope);
  44. assignInstructionRanges(MIRanges, MI2ScopeMap);
  45. }
  46. }
  47. /// extractLexicalScopes - Extract instruction ranges for each lexical scopes
  48. /// for the given machine function.
  49. void LexicalScopes::extractLexicalScopes(
  50. SmallVectorImpl<InsnRange> &MIRanges,
  51. DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
  52. // Scan each instruction and create scopes. First build working set of scopes.
  53. for (const auto &MBB : *MF) {
  54. const MachineInstr *RangeBeginMI = nullptr;
  55. const MachineInstr *PrevMI = nullptr;
  56. const DILocation *PrevDL = nullptr;
  57. for (const auto &MInsn : MBB) {
  58. // Check if instruction has valid location information.
  59. const DILocation *MIDL = MInsn.getDebugLoc();
  60. if (!MIDL) {
  61. PrevMI = &MInsn;
  62. continue;
  63. }
  64. // If scope has not changed then skip this instruction.
  65. if (MIDL == PrevDL) {
  66. PrevMI = &MInsn;
  67. continue;
  68. }
  69. // Ignore DBG_VALUE. It does not contribute to any instruction in output.
  70. if (MInsn.isDebugValue())
  71. continue;
  72. if (RangeBeginMI) {
  73. // If we have already seen a beginning of an instruction range and
  74. // current instruction scope does not match scope of first instruction
  75. // in this range then create a new instruction range.
  76. InsnRange R(RangeBeginMI, PrevMI);
  77. MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
  78. MIRanges.push_back(R);
  79. }
  80. // This is a beginning of a new instruction range.
  81. RangeBeginMI = &MInsn;
  82. // Reset previous markers.
  83. PrevMI = &MInsn;
  84. PrevDL = MIDL;
  85. }
  86. // Create last instruction range.
  87. if (RangeBeginMI && PrevMI && PrevDL) {
  88. InsnRange R(RangeBeginMI, PrevMI);
  89. MIRanges.push_back(R);
  90. MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL);
  91. }
  92. }
  93. }
  94. /// findLexicalScope - Find lexical scope, either regular or inlined, for the
  95. /// given DebugLoc. Return NULL if not found.
  96. LexicalScope *LexicalScopes::findLexicalScope(const DILocation *DL) {
  97. DILocalScope *Scope = DL->getScope();
  98. if (!Scope)
  99. return nullptr;
  100. // The scope that we were created with could have an extra file - which
  101. // isn't what we care about in this case.
  102. if (auto *File = dyn_cast<DILexicalBlockFile>(Scope))
  103. Scope = File->getScope();
  104. if (auto *IA = DL->getInlinedAt()) {
  105. auto I = InlinedLexicalScopeMap.find(std::make_pair(Scope, IA));
  106. return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr;
  107. }
  108. return findLexicalScope(Scope);
  109. }
  110. /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
  111. /// not available then create new lexical scope.
  112. LexicalScope *LexicalScopes::getOrCreateLexicalScope(const DILocalScope *Scope,
  113. const DILocation *IA) {
  114. if (IA) {
  115. // Create an abstract scope for inlined function.
  116. getOrCreateAbstractScope(Scope);
  117. // Create an inlined scope for inlined function.
  118. return getOrCreateInlinedScope(Scope, IA);
  119. }
  120. return getOrCreateRegularScope(Scope);
  121. }
  122. /// getOrCreateRegularScope - Find or create a regular lexical scope.
  123. LexicalScope *
  124. LexicalScopes::getOrCreateRegularScope(const DILocalScope *Scope) {
  125. if (auto *File = dyn_cast<DILexicalBlockFile>(Scope))
  126. Scope = File->getScope();
  127. auto I = LexicalScopeMap.find(Scope);
  128. if (I != LexicalScopeMap.end())
  129. return &I->second;
  130. // FIXME: Should the following dyn_cast be DILexicalBlock?
  131. LexicalScope *Parent = nullptr;
  132. if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
  133. Parent = getOrCreateLexicalScope(Block->getScope());
  134. I = LexicalScopeMap.emplace(std::piecewise_construct,
  135. std::forward_as_tuple(Scope),
  136. std::forward_as_tuple(Parent, Scope, nullptr,
  137. false)).first;
  138. if (!Parent) {
  139. assert(cast<DISubprogram>(Scope)->describes(MF->getFunction()));
  140. assert(!CurrentFnLexicalScope);
  141. CurrentFnLexicalScope = &I->second;
  142. }
  143. return &I->second;
  144. }
  145. /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
  146. LexicalScope *
  147. LexicalScopes::getOrCreateInlinedScope(const DILocalScope *Scope,
  148. const DILocation *InlinedAt) {
  149. std::pair<const DILocalScope *, const DILocation *> P(Scope, InlinedAt);
  150. auto I = InlinedLexicalScopeMap.find(P);
  151. if (I != InlinedLexicalScopeMap.end())
  152. return &I->second;
  153. LexicalScope *Parent;
  154. if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
  155. Parent = getOrCreateInlinedScope(Block->getScope(), InlinedAt);
  156. else
  157. Parent = getOrCreateLexicalScope(InlinedAt);
  158. I = InlinedLexicalScopeMap.emplace(std::piecewise_construct,
  159. std::forward_as_tuple(P),
  160. std::forward_as_tuple(Parent, Scope,
  161. InlinedAt, false))
  162. .first;
  163. return &I->second;
  164. }
  165. /// getOrCreateAbstractScope - Find or create an abstract lexical scope.
  166. LexicalScope *
  167. LexicalScopes::getOrCreateAbstractScope(const DILocalScope *Scope) {
  168. assert(Scope && "Invalid Scope encoding!");
  169. if (auto *File = dyn_cast<DILexicalBlockFile>(Scope))
  170. Scope = File->getScope();
  171. auto I = AbstractScopeMap.find(Scope);
  172. if (I != AbstractScopeMap.end())
  173. return &I->second;
  174. // FIXME: Should the following isa be DILexicalBlock?
  175. LexicalScope *Parent = nullptr;
  176. if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
  177. Parent = getOrCreateAbstractScope(Block->getScope());
  178. I = AbstractScopeMap.emplace(std::piecewise_construct,
  179. std::forward_as_tuple(Scope),
  180. std::forward_as_tuple(Parent, Scope,
  181. nullptr, true)).first;
  182. if (isa<DISubprogram>(Scope))
  183. AbstractScopesList.push_back(&I->second);
  184. return &I->second;
  185. }
  186. /// constructScopeNest
  187. void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
  188. assert(Scope && "Unable to calculate scope dominance graph!");
  189. SmallVector<LexicalScope *, 4> WorkStack;
  190. WorkStack.push_back(Scope);
  191. unsigned Counter = 0;
  192. while (!WorkStack.empty()) {
  193. LexicalScope *WS = WorkStack.back();
  194. const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
  195. bool visitedChildren = false;
  196. for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
  197. SE = Children.end();
  198. SI != SE; ++SI) {
  199. LexicalScope *ChildScope = *SI;
  200. if (!ChildScope->getDFSOut()) {
  201. WorkStack.push_back(ChildScope);
  202. visitedChildren = true;
  203. ChildScope->setDFSIn(++Counter);
  204. break;
  205. }
  206. }
  207. if (!visitedChildren) {
  208. WorkStack.pop_back();
  209. WS->setDFSOut(++Counter);
  210. }
  211. }
  212. }
  213. /// assignInstructionRanges - Find ranges of instructions covered by each
  214. /// lexical scope.
  215. void LexicalScopes::assignInstructionRanges(
  216. SmallVectorImpl<InsnRange> &MIRanges,
  217. DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) {
  218. LexicalScope *PrevLexicalScope = nullptr;
  219. for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(),
  220. RE = MIRanges.end();
  221. RI != RE; ++RI) {
  222. const InsnRange &R = *RI;
  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 (SmallVectorImpl<InsnRange>::iterator I = InsnRanges.begin(),
  250. E = InsnRanges.end();
  251. I != E; ++I) {
  252. InsnRange &R = *I;
  253. MBBs.insert(R.first->getParent());
  254. }
  255. }
  256. /// dominates - Return true if DebugLoc's lexical scope dominates at least one
  257. /// machine instruction's lexical scope in a given machine basic block.
  258. bool LexicalScopes::dominates(const DILocation *DL, MachineBasicBlock *MBB) {
  259. LexicalScope *Scope = getOrCreateLexicalScope(DL);
  260. if (!Scope)
  261. return false;
  262. // Current function scope covers all basic blocks in the function.
  263. if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF)
  264. return true;
  265. bool Result = false;
  266. for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
  267. ++I) {
  268. if (const DILocation *IDL = I->getDebugLoc())
  269. if (LexicalScope *IScope = getOrCreateLexicalScope(IDL))
  270. if (Scope->dominates(IScope))
  271. return true;
  272. }
  273. return Result;
  274. }
  275. /// dump - Print data structures.
  276. void LexicalScope::dump(unsigned Indent) const {
  277. #ifndef NDEBUG
  278. raw_ostream &err = dbgs();
  279. err.indent(Indent);
  280. err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n";
  281. const MDNode *N = Desc;
  282. err.indent(Indent);
  283. N->dump();
  284. if (AbstractScope)
  285. err << std::string(Indent, ' ') << "Abstract Scope\n";
  286. if (!Children.empty())
  287. err << std::string(Indent + 2, ' ') << "Children ...\n";
  288. for (unsigned i = 0, e = Children.size(); i != e; ++i)
  289. if (Children[i] != this)
  290. Children[i]->dump(Indent + 2);
  291. #endif
  292. }