From ac4d0bf7ed363534bdf609a2865db63d1010e18f Mon Sep 17 00:00:00 2001 From: Vsevolod Livinskiy Date: Fri, 10 Apr 2015 10:02:59 +0300 Subject: [PATCH] Fix for debug information in trunk. --- builtins.cpp | 20 +++++++------- ctx.cpp | 28 +++++++++++--------- module.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 94 insertions(+), 29 deletions(-) diff --git a/builtins.cpp b/builtins.cpp index 9cdadd70..972bedc5 100644 --- a/builtins.cpp +++ b/builtins.cpp @@ -928,7 +928,15 @@ lDefineConstantInt(const char *name, int val, llvm::Module *module, // have the DW_AT_artifical attribute. It's not clear if this // matters for anything though. -#if defined(LLVM_3_6) +#if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) + llvm::DIGlobalVariable var = m->diBuilder->createGlobalVariable( + name, + file, + 0 /* line */, + diType, + true /* static */, + sym->storagePtr); +#elif defined(LLVM_3_6) llvm::Constant *sym_const_storagePtr = llvm::dyn_cast(sym->storagePtr); Assert(sym_const_storagePtr); llvm::DIGlobalVariable var = m->diBuilder->createGlobalVariable( @@ -940,15 +948,7 @@ lDefineConstantInt(const char *name, int val, llvm::Module *module, diType, true /* static */, sym_const_storagePtr); -#elif defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) - llvm::DIGlobalVariable var = m->diBuilder->createGlobalVariable( - name, - file, - 0 /* line */, - diType, - true /* static */, - sym->storagePtr); -#else +#else // LLVM 3.7+ llvm::Constant *sym_const_storagePtr = llvm::dyn_cast(sym->storagePtr); Assert(sym_const_storagePtr); m->diBuilder->createGlobalVariable( diff --git a/ctx.cpp b/ctx.cpp index ae16014a..f73f6cf1 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -341,7 +341,7 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym, #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, diFile.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif @@ -352,7 +352,7 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym, #endif #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, scope.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif @@ -364,17 +364,21 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym, diSubprogramType = functionType->GetDIType(scope); #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, diSubprogramType.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif } -#if !defined(LLVM_3_2) && !defined(LLVM_3_3) +#if defined(LLVM_3_2) || defined(LLVM_3_3) + llvm::DIType diSubprogramType_n = diSubprogramType; +#elif defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) Assert(diSubprogramType.isCompositeType()); llvm::DICompositeType diSubprogramType_n = static_cast(diSubprogramType); -#else - llvm::DIType diSubprogramType_n = diSubprogramType; +#else // LLVM 3.7+ + Assert(llvm::isa(diSubprogramType)); + llvm::DICompositeType diSubprogramType_n = + llvm::cast(diSubprogramType); #endif std::string mangledName = llvmFunction->getName(); @@ -396,7 +400,7 @@ FunctionEmitContext::FunctionEmitContext(Function *func, Symbol *funSym, isOptimized, llvmFunction); #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, diSubprogram.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif @@ -1681,7 +1685,7 @@ FunctionEmitContext::StartScope() { #endif // LLVM 3.2, 3.3, 3.4 and 3.6+ #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, lexicalBlock.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif debugScopes.push_back(lexicalBlock); @@ -1714,7 +1718,7 @@ FunctionEmitContext::EmitVariableDebugInfo(Symbol *sym) { llvm::DIType diType = sym->type->GetDIType(scope); #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, diType.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif llvm::DIVariable var = @@ -1727,7 +1731,7 @@ FunctionEmitContext::EmitVariableDebugInfo(Symbol *sym) { true /* preserve through opts */); #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, var.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif #if !defined(LLVM_3_2) && !defined(LLVM_3_3) && !defined(LLVM_3_4) && !defined(LLVM_3_5)// LLVM 3.6+ @@ -1751,7 +1755,7 @@ FunctionEmitContext::EmitFunctionParameterDebugInfo(Symbol *sym, int argNum) { llvm::DIType diType = sym->type->GetDIType(scope); #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, diType.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif int flags = 0; @@ -1768,7 +1772,7 @@ FunctionEmitContext::EmitFunctionParameterDebugInfo(Symbol *sym, int argNum) { argNum+1); #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) AssertPos(currentPos, var.Verify()); -#else // LLVM 3.7 +#else // LLVM 3.7+ //comming soon #endif #if !defined(LLVM_3_2) && !defined(LLVM_3_3) && !defined(LLVM_3_4) && !defined(LLVM_3_5)// LLVM 3.6+ diff --git a/module.cpp b/module.cpp index 177605f3..41e2480a 100644 --- a/module.cpp +++ b/module.cpp @@ -189,22 +189,39 @@ lStripUnusedDebugInfo(llvm::Module *module) { // get the instruction`s debugging metadata llvm::MDNode *node = inst->getMetadata(llvm::LLVMContext::MD_dbg); while (node) { +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) llvm::DILocation dloc(node); +#else // LLVM 3.7+ + llvm::DILocation dloc(llvm::cast(node)); +#endif // get the scope of the current instruction`s location llvm::DIScope scope = dloc.getScope(); // node becomes NULL if this was the original location node = dloc.getOrigLocation(); // now following a chain of nested scopes while (!0) { +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) if (scope.isLexicalBlockFile()) scope = llvm::DILexicalBlockFile(scope).getScope(); else if (scope.isLexicalBlock()) scope = llvm::DILexicalBlock(scope).getContext(); else if (scope.isNameSpace()) scope = llvm::DINameSpace(scope).getContext(); +#else // LLVM 3.7+ + if (llvm::isa(scope)) + scope = llvm::DILexicalBlockFile(llvm::cast(scope)).getContext(); + else if (llvm::isa(scope)) + scope = llvm::DILexicalBlock(llvm::cast(scope)).getContext(); + else if (llvm::isa(scope)) + scope = llvm::DINameSpace(llvm::cast(scope)).getContext(); +#endif else break; } +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) if (scope.isSubprogram()) { +#else // LLVM 3.7+ + if (llvm::isa(scope)) { +#endif // good, the chain ended with a function; adding SPall.insert(scope); } @@ -215,10 +232,18 @@ lStripUnusedDebugInfo(llvm::Module *module) { if (llvm::NamedMDNode *cuNodes = module->getNamedMetadata("llvm.dbg.cu")) { for (unsigned i = 0, ie = cuNodes->getNumOperands(); i != ie; ++i) { llvm::MDNode *cuNode = cuNodes->getOperand(i); +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) llvm::DICompileUnit cu(cuNode); +#else // LLVM 3.7+ + llvm::DICompileUnit cu(llvm::cast(cuNode)); +#endif llvm::DIArray subprograms = cu.getSubprograms(); +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) if (subprograms.getNumElements() == 0) +#else // LLVM 3.7+ + if (subprograms.size() == 0) +#endif continue; #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) @@ -230,11 +255,17 @@ lStripUnusedDebugInfo(llvm::Module *module) { #endif // determine what functions of those extracted belong to the unit +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) for (unsigned j = 0, je = subprograms.getNumElements(); j != je; ++j) +#else // LLVM 3.7+ + for (unsigned j = 0, je = subprograms.size(); j != je; ++j) +#endif #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) SPset.insert(subprograms->getOperand(j)); -#else // LLVM 3.6+ +#elif defined(LLVM_3_6) SPset.insert(subprograms.getElement(j)); +#else // LLVM 3.7+ + SPset.insert(subprograms [j]); #endif std::set_intersection(SPall.begin(), SPall.end(), @@ -243,7 +274,11 @@ lStripUnusedDebugInfo(llvm::Module *module) { Debug(SourcePos(), "%d / %d functions left in module with debug " "info.", (int)usedSubprograms.size(), +#if defined (LLVM_3_2) || defined (LLVM_3_3)|| defined (LLVM_3_4)|| defined (LLVM_3_5) || (LLVM_3_6) (int)subprograms.getNumElements()); +#else // LLVM 3.7+ + (int)subprograms.size()); +#endif // We'd now like to replace the array of subprograms in the // compile unit with only the ones that actually have function @@ -298,15 +333,25 @@ lStripUnusedDebugInfo(llvm::Module *module) { cuNode->replaceOperandWith(9, replNode); #else // LLVM 3.6+ llvm::DIArray nodeSPs = cu.getSubprograms(); +#if defined(LLVM_3_6) Assert(nodeSPs.getNumElements() == subprograms.getNumElements()); for (int i = 0; i < (int)nodeSPs.getNumElements(); ++i) - Assert(nodeSPs.getElement(i) == subprograms.getElement(i)); - + Assert(nodeSPs.getElement(i) == subprograms.getElement(i)); +#else // LLVM 3.7+ + Assert(nodeSPs.size() == subprograms.size()); + for (int i = 0; i < (int)nodeSPs.size(); ++i) + Assert(nodeSPs [i] == subprograms [i]); +#endif // And now we can go and stuff it into the unit with some // confidence... llvm::MDNode *replNode = llvm::MDNode::get(module->getContext(), llvm::ArrayRef(usedSubprograms)); +#if defined(LLVM_3_6) cu.replaceSubprograms(llvm::DIArray(replNode)); +#else // LLVM 3.7+ + cu.replaceSubprograms(llvm::DIArray(llvm::cast(replNode))); + +#endif #endif } } @@ -655,7 +700,15 @@ Module::AddGlobalVariable(const std::string &name, const Type *type, Expr *initE if (diBuilder) { llvm::DIFile file = pos.GetDIFile(); -#if !defined(LLVM_3_2) && !defined(LLVM_3_3) && !defined(LLVM_3_4) && !defined(LLVM_3_5)// LLVM 3.6+ +#if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) + llvm::DIGlobalVariable var = diBuilder->createGlobalVariable( + name, + file, + pos.first_line, + sym->type->GetDIType(file), + (sym->storageClass == SC_STATIC), + sym->storagePtr); +#elif defined(LLVM_3_6) llvm::Constant *sym_const_storagePtr = llvm::dyn_cast(sym->storagePtr); Assert(sym_const_storagePtr); llvm::DIGlobalVariable var = diBuilder->createGlobalVariable( @@ -667,16 +720,24 @@ Module::AddGlobalVariable(const std::string &name, const Type *type, Expr *initE sym->type->GetDIType(file), (sym->storageClass == SC_STATIC), sym_const_storagePtr); -#else - llvm::DIGlobalVariable var = diBuilder->createGlobalVariable( +#else // LLVM 3.7+ + llvm::Constant *sym_const_storagePtr = llvm::dyn_cast(sym->storagePtr); + Assert(sym_const_storagePtr); + diBuilder->createGlobalVariable( + file, + name, name, file, pos.first_line, sym->type->GetDIType(file), (sym->storageClass == SC_STATIC), - sym->storagePtr); + sym_const_storagePtr); #endif +#if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) || defined(LLVM_3_6) Assert(var.Verify()); +#else // LLVM 3.7+ + //comming soon +#endif } }