LLVM API Documentation
00001 //===- LexicalScopes.cpp - Collecting lexical scope info ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements LexicalScopes analysis. 00011 // 00012 // This pass collects lexical scope information and maps machine instructions 00013 // to respective lexical scopes. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #define DEBUG_TYPE "lexicalscopes" 00018 #include "llvm/CodeGen/LexicalScopes.h" 00019 #include "llvm/CodeGen/MachineFunction.h" 00020 #include "llvm/CodeGen/MachineInstr.h" 00021 #include "llvm/DebugInfo.h" 00022 #include "llvm/IR/Function.h" 00023 #include "llvm/Support/Debug.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/FormattedStream.h" 00026 using namespace llvm; 00027 00028 LexicalScopes::~LexicalScopes() { 00029 releaseMemory(); 00030 } 00031 00032 /// releaseMemory - release memory. 00033 void LexicalScopes::releaseMemory() { 00034 MF = NULL; 00035 CurrentFnLexicalScope = NULL; 00036 DeleteContainerSeconds(LexicalScopeMap); 00037 DeleteContainerSeconds(AbstractScopeMap); 00038 InlinedLexicalScopeMap.clear(); 00039 AbstractScopesList.clear(); 00040 } 00041 00042 /// initialize - Scan machine function and constuct lexical scope nest. 00043 void LexicalScopes::initialize(const MachineFunction &Fn) { 00044 releaseMemory(); 00045 MF = &Fn; 00046 SmallVector<InsnRange, 4> MIRanges; 00047 DenseMap<const MachineInstr *, LexicalScope *> MI2ScopeMap; 00048 extractLexicalScopes(MIRanges, MI2ScopeMap); 00049 if (CurrentFnLexicalScope) { 00050 constructScopeNest(CurrentFnLexicalScope); 00051 assignInstructionRanges(MIRanges, MI2ScopeMap); 00052 } 00053 } 00054 00055 /// extractLexicalScopes - Extract instruction ranges for each lexical scopes 00056 /// for the given machine function. 00057 void LexicalScopes:: 00058 extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges, 00059 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) { 00060 00061 // Scan each instruction and create scopes. First build working set of scopes. 00062 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 00063 I != E; ++I) { 00064 const MachineInstr *RangeBeginMI = NULL; 00065 const MachineInstr *PrevMI = NULL; 00066 DebugLoc PrevDL; 00067 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end(); 00068 II != IE; ++II) { 00069 const MachineInstr *MInsn = II; 00070 00071 // Check if instruction has valid location information. 00072 const DebugLoc MIDL = MInsn->getDebugLoc(); 00073 if (MIDL.isUnknown()) { 00074 PrevMI = MInsn; 00075 continue; 00076 } 00077 00078 // If scope has not changed then skip this instruction. 00079 if (MIDL == PrevDL) { 00080 PrevMI = MInsn; 00081 continue; 00082 } 00083 00084 // Ignore DBG_VALUE. It does not contribute to any instruction in output. 00085 if (MInsn->isDebugValue()) 00086 continue; 00087 00088 if (RangeBeginMI) { 00089 // If we have already seen a beginning of an instruction range and 00090 // current instruction scope does not match scope of first instruction 00091 // in this range then create a new instruction range. 00092 InsnRange R(RangeBeginMI, PrevMI); 00093 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL); 00094 MIRanges.push_back(R); 00095 } 00096 00097 // This is a beginning of a new instruction range. 00098 RangeBeginMI = MInsn; 00099 00100 // Reset previous markers. 00101 PrevMI = MInsn; 00102 PrevDL = MIDL; 00103 } 00104 00105 // Create last instruction range. 00106 if (RangeBeginMI && PrevMI && !PrevDL.isUnknown()) { 00107 InsnRange R(RangeBeginMI, PrevMI); 00108 MIRanges.push_back(R); 00109 MI2ScopeMap[RangeBeginMI] = getOrCreateLexicalScope(PrevDL); 00110 } 00111 } 00112 } 00113 00114 /// findLexicalScope - Find lexical scope, either regular or inlined, for the 00115 /// given DebugLoc. Return NULL if not found. 00116 LexicalScope *LexicalScopes::findLexicalScope(DebugLoc DL) { 00117 MDNode *Scope = NULL; 00118 MDNode *IA = NULL; 00119 DL.getScopeAndInlinedAt(Scope, IA, MF->getFunction()->getContext()); 00120 if (!Scope) return NULL; 00121 00122 // The scope that we were created with could have an extra file - which 00123 // isn't what we care about in this case. 00124 DIDescriptor D = DIDescriptor(Scope); 00125 if (D.isLexicalBlockFile()) 00126 Scope = DILexicalBlockFile(Scope).getScope(); 00127 00128 if (IA) 00129 return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA)); 00130 return LexicalScopeMap.lookup(Scope); 00131 } 00132 00133 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If 00134 /// not available then create new lexical scope. 00135 LexicalScope *LexicalScopes::getOrCreateLexicalScope(DebugLoc DL) { 00136 MDNode *Scope = NULL; 00137 MDNode *InlinedAt = NULL; 00138 DL.getScopeAndInlinedAt(Scope, InlinedAt, MF->getFunction()->getContext()); 00139 00140 if (InlinedAt) { 00141 // Create an abstract scope for inlined function. 00142 getOrCreateAbstractScope(Scope); 00143 // Create an inlined scope for inlined function. 00144 return getOrCreateInlinedScope(Scope, InlinedAt); 00145 } 00146 00147 return getOrCreateRegularScope(Scope); 00148 } 00149 00150 /// getOrCreateRegularScope - Find or create a regular lexical scope. 00151 LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) { 00152 DIDescriptor D = DIDescriptor(Scope); 00153 if (D.isLexicalBlockFile()) { 00154 Scope = DILexicalBlockFile(Scope).getScope(); 00155 D = DIDescriptor(Scope); 00156 } 00157 00158 LexicalScope *WScope = LexicalScopeMap.lookup(Scope); 00159 if (WScope) 00160 return WScope; 00161 00162 LexicalScope *Parent = NULL; 00163 if (D.isLexicalBlock()) 00164 Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope)); 00165 WScope = new LexicalScope(Parent, DIDescriptor(Scope), NULL, false); 00166 LexicalScopeMap.insert(std::make_pair(Scope, WScope)); 00167 if (!Parent && DIDescriptor(Scope).isSubprogram() 00168 && DISubprogram(Scope).describes(MF->getFunction())) 00169 CurrentFnLexicalScope = WScope; 00170 00171 return WScope; 00172 } 00173 00174 /// getOrCreateInlinedScope - Find or create an inlined lexical scope. 00175 LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope, 00176 MDNode *InlinedAt) { 00177 LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt); 00178 if (InlinedScope) 00179 return InlinedScope; 00180 00181 DebugLoc InlinedLoc = DebugLoc::getFromDILocation(InlinedAt); 00182 InlinedScope = new LexicalScope(getOrCreateLexicalScope(InlinedLoc), 00183 DIDescriptor(Scope), InlinedAt, false); 00184 InlinedLexicalScopeMap[InlinedLoc] = InlinedScope; 00185 LexicalScopeMap[InlinedAt] = InlinedScope; 00186 return InlinedScope; 00187 } 00188 00189 /// getOrCreateAbstractScope - Find or create an abstract lexical scope. 00190 LexicalScope *LexicalScopes::getOrCreateAbstractScope(const MDNode *N) { 00191 assert(N && "Invalid Scope encoding!"); 00192 00193 DIDescriptor Scope(N); 00194 if (Scope.isLexicalBlockFile()) 00195 Scope = DILexicalBlockFile(Scope).getScope(); 00196 LexicalScope *AScope = AbstractScopeMap.lookup(N); 00197 if (AScope) 00198 return AScope; 00199 00200 LexicalScope *Parent = NULL; 00201 if (Scope.isLexicalBlock()) { 00202 DILexicalBlock DB(N); 00203 DIDescriptor ParentDesc = DB.getContext(); 00204 Parent = getOrCreateAbstractScope(ParentDesc); 00205 } 00206 AScope = new LexicalScope(Parent, DIDescriptor(N), NULL, true); 00207 AbstractScopeMap[N] = AScope; 00208 if (DIDescriptor(N).isSubprogram()) 00209 AbstractScopesList.push_back(AScope); 00210 return AScope; 00211 } 00212 00213 /// constructScopeNest 00214 void LexicalScopes::constructScopeNest(LexicalScope *Scope) { 00215 assert (Scope && "Unable to calculate scop edominance graph!"); 00216 SmallVector<LexicalScope *, 4> WorkStack; 00217 WorkStack.push_back(Scope); 00218 unsigned Counter = 0; 00219 while (!WorkStack.empty()) { 00220 LexicalScope *WS = WorkStack.back(); 00221 const SmallVector<LexicalScope *, 4> &Children = WS->getChildren(); 00222 bool visitedChildren = false; 00223 for (SmallVector<LexicalScope *, 4>::const_iterator SI = Children.begin(), 00224 SE = Children.end(); SI != SE; ++SI) { 00225 LexicalScope *ChildScope = *SI; 00226 if (!ChildScope->getDFSOut()) { 00227 WorkStack.push_back(ChildScope); 00228 visitedChildren = true; 00229 ChildScope->setDFSIn(++Counter); 00230 break; 00231 } 00232 } 00233 if (!visitedChildren) { 00234 WorkStack.pop_back(); 00235 WS->setDFSOut(++Counter); 00236 } 00237 } 00238 } 00239 00240 /// assignInstructionRanges - Find ranges of instructions covered by each 00241 /// lexical scope. 00242 void LexicalScopes:: 00243 assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges, 00244 DenseMap<const MachineInstr *, LexicalScope *> &MI2ScopeMap) 00245 { 00246 00247 LexicalScope *PrevLexicalScope = NULL; 00248 for (SmallVectorImpl<InsnRange>::const_iterator RI = MIRanges.begin(), 00249 RE = MIRanges.end(); RI != RE; ++RI) { 00250 const InsnRange &R = *RI; 00251 LexicalScope *S = MI2ScopeMap.lookup(R.first); 00252 assert (S && "Lost LexicalScope for a machine instruction!"); 00253 if (PrevLexicalScope && !PrevLexicalScope->dominates(S)) 00254 PrevLexicalScope->closeInsnRange(S); 00255 S->openInsnRange(R.first); 00256 S->extendInsnRange(R.second); 00257 PrevLexicalScope = S; 00258 } 00259 00260 if (PrevLexicalScope) 00261 PrevLexicalScope->closeInsnRange(); 00262 } 00263 00264 /// getMachineBasicBlocks - Populate given set using machine basic blocks which 00265 /// have machine instructions that belong to lexical scope identified by 00266 /// DebugLoc. 00267 void LexicalScopes:: 00268 getMachineBasicBlocks(DebugLoc DL, 00269 SmallPtrSet<const MachineBasicBlock*, 4> &MBBs) { 00270 MBBs.clear(); 00271 LexicalScope *Scope = getOrCreateLexicalScope(DL); 00272 if (!Scope) 00273 return; 00274 00275 if (Scope == CurrentFnLexicalScope) { 00276 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); 00277 I != E; ++I) 00278 MBBs.insert(I); 00279 return; 00280 } 00281 00282 SmallVector<InsnRange, 4> &InsnRanges = Scope->getRanges(); 00283 for (SmallVector<InsnRange, 4>::iterator I = InsnRanges.begin(), 00284 E = InsnRanges.end(); I != E; ++I) { 00285 InsnRange &R = *I; 00286 MBBs.insert(R.first->getParent()); 00287 } 00288 } 00289 00290 /// dominates - Return true if DebugLoc's lexical scope dominates at least one 00291 /// machine instruction's lexical scope in a given machine basic block. 00292 bool LexicalScopes::dominates(DebugLoc DL, MachineBasicBlock *MBB) { 00293 LexicalScope *Scope = getOrCreateLexicalScope(DL); 00294 if (!Scope) 00295 return false; 00296 00297 // Current function scope covers all basic blocks in the function. 00298 if (Scope == CurrentFnLexicalScope && MBB->getParent() == MF) 00299 return true; 00300 00301 bool Result = false; 00302 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); 00303 I != E; ++I) { 00304 DebugLoc IDL = I->getDebugLoc(); 00305 if (IDL.isUnknown()) 00306 continue; 00307 if (LexicalScope *IScope = getOrCreateLexicalScope(IDL)) 00308 if (Scope->dominates(IScope)) 00309 return true; 00310 } 00311 return Result; 00312 } 00313 00314 void LexicalScope::anchor() { } 00315 00316 /// dump - Print data structures. 00317 void LexicalScope::dump(unsigned Indent) const { 00318 #ifndef NDEBUG 00319 raw_ostream &err = dbgs(); 00320 err.indent(Indent); 00321 err << "DFSIn: " << DFSIn << " DFSOut: " << DFSOut << "\n"; 00322 const MDNode *N = Desc; 00323 err.indent(Indent); 00324 N->dump(); 00325 if (AbstractScope) 00326 err << std::string(Indent, ' ') << "Abstract Scope\n"; 00327 00328 if (!Children.empty()) 00329 err << std::string(Indent + 2, ' ') << "Children ...\n"; 00330 for (unsigned i = 0, e = Children.size(); i != e; ++i) 00331 if (Children[i] != this) 00332 Children[i]->dump(Indent + 2); 00333 #endif 00334 } 00335