}
}
-const char *
+StringRef
DIDescriptor::getStringField(unsigned Elt) const {
if (DbgNode == 0)
- return NULL;
+ return StringRef();
if (Elt < DbgNode->getNumElements())
- if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt))) {
- if (MDS->getLength() == 0)
- return NULL;
- return MDS->getString().data();
- }
+ if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
+ return MDS->getString();
- return NULL;
+ return StringRef();
}
uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
bool DICompileUnit::Verify() const {
if (isNull())
return false;
- const char *N = getFilename();
- if (!N)
+ StringRef N = getFilename();
+ if (N.empty())
return false;
// It is possible that directory and produce string is empty.
return true;
if (isNull())
return false;
+ if (getDisplayName().empty())
+ return false;
+
if (getContext().isNull())
return false;
Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
Tag == dwarf::DW_TAG_restrict_type) {
DIType BaseType = getTypeDerivedFrom();
+ // If this type is not derived from any type then take conservative
+ // approach.
+ if (BaseType.isNull())
+ return getSizeInBits();
if (BaseType.isDerivedType())
return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
else
/// information for the function F.
bool DISubprogram::describes(const Function *F) {
assert (F && "Invalid function");
- const char *Name = getLinkageName();
- if (!Name)
+ StringRef Name = getLinkageName();
+ if (Name.empty())
Name = getName();
- if (strcmp(F->getName().data(), Name) == 0)
+ if (F->getName() == Name)
return true;
return false;
}
-const char *DIScope::getFilename() const {
+StringRef DIScope::getFilename() const {
if (isLexicalBlock())
return DILexicalBlock(DbgNode).getFilename();
else if (isSubprogram())
return DICompileUnit(DbgNode).getFilename();
else
assert (0 && "Invalid DIScope!");
- return NULL;
+ return StringRef();
}
-const char *DIScope::getDirectory() const {
+StringRef DIScope::getDirectory() const {
if (isLexicalBlock())
return DILexicalBlock(DbgNode).getDirectory();
else if (isSubprogram())
return DICompileUnit(DbgNode).getDirectory();
else
assert (0 && "Invalid DIScope!");
- return NULL;
+ return StringRef();
}
//===----------------------------------------------------------------------===//
void DIType::dump() const {
if (isNull()) return;
- if (const char *Res = getName())
+ StringRef Res = getName();
+ if (!Res.empty())
errs() << " [" << Res << "] ";
unsigned Tag = getTag();
/// dump - Print global.
void DIGlobal::dump() const {
- if (const char *Res = getName())
+ StringRef Res = getName();
+ if (!Res.empty())
errs() << " [" << Res << "] ";
unsigned Tag = getTag();
/// dump - Print subprogram.
void DISubprogram::dump() const {
- if (const char *Res = getName())
+ StringRef Res = getName();
+ if (!Res.empty())
errs() << " [" << Res << "] ";
unsigned Tag = getTag();
/// dump - Print variable.
void DIVariable::dump() const {
- if (const char *Res = getName())
+ StringRef Res = getName();
+ if (!Res.empty())
errs() << " [" << Res << "] ";
getCompileUnit().dump();
//===----------------------------------------------------------------------===//
DIFactory::DIFactory(Module &m)
- : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
- RegionStartFn(0), RegionEndFn(0),
- DeclareFn(0) {
+ : M(m), VMContext(M.getContext()), DeclareFn(0) {
EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
}
StringRef Producer,
bool isMain,
bool isOptimized,
- const char *Flags,
+ StringRef Flags,
unsigned RunTimeVer) {
Value *Elts[] = {
GetTagConstant(dwarf::DW_TAG_compile_unit),
DICompileUnit CompileUnit,
unsigned LineNo, DIType Type,
bool isLocalToUnit,
- bool isDefinition) {
+ bool isDefinition,
+ unsigned VK, unsigned VIndex,
+ DIType ContainingType) {
Value *Elts[] = {
GetTagConstant(dwarf::DW_TAG_subprogram),
ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Type.getNode(),
ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
- ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
+ ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
+ ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
+ ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
+ ContainingType.getNode()
+ };
+ return DISubprogram(MDNode::get(VMContext, &Elts[0], 14));
+}
+
+/// CreateSubprogramDefinition - Create new subprogram descriptor for the
+/// given declaration.
+DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
+ if (SPDeclaration.isDefinition())
+ return DISubprogram(SPDeclaration.getNode());
+
+ MDNode *DeclNode = SPDeclaration.getNode();
+ Value *Elts[] = {
+ GetTagConstant(dwarf::DW_TAG_subprogram),
+ llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
+ DeclNode->getElement(2), // Context
+ DeclNode->getElement(3), // Name
+ DeclNode->getElement(4), // DisplayName
+ DeclNode->getElement(5), // LinkageName
+ DeclNode->getElement(6), // CompileUnit
+ DeclNode->getElement(7), // LineNo
+ DeclNode->getElement(8), // Type
+ DeclNode->getElement(9), // isLocalToUnit
+ ConstantInt::get(Type::getInt1Ty(VMContext), true),
+ DeclNode->getElement(11), // Virtuality
+ DeclNode->getElement(12), // VIndex
+ DeclNode->getElement(13) // Containting Type
};
- return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
+ return DISubprogram(MDNode::get(VMContext, &Elts[0], 14));
}
/// CreateGlobalVariable - Create a new descriptor for the specified global.
return DILocation(MDNode::get(VMContext, &Elts[0], 4));
}
+/// CreateLocation - Creates a debug info location.
+DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
+ DIScope S, MDNode *OrigLoc) {
+ Value *Elts[] = {
+ ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
+ ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
+ S.getNode(),
+ OrigLoc
+ };
+ return DILocation(MDNode::get(VMContext, &Elts[0], 4));
+}
//===----------------------------------------------------------------------===//
// DIFactory: Routines for inserting code into a function
//===----------------------------------------------------------------------===//
-/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
-/// inserting it at the end of the specified basic block.
-void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
- unsigned ColNo, BasicBlock *BB) {
-
- // Lazily construct llvm.dbg.stoppoint function.
- if (!StopPointFn)
- StopPointFn = llvm::Intrinsic::getDeclaration(&M,
- llvm::Intrinsic::dbg_stoppoint);
-
- // Invoke llvm.dbg.stoppoint
- Value *Args[] = {
- ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
- ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
- CU.getNode()
- };
- CallInst::Create(StopPointFn, Args, Args+3, "", BB);
-}
-
-/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
-/// mark the start of the specified subprogram.
-void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
- // Lazily construct llvm.dbg.func.start.
- if (!FuncStartFn)
- FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
-
- // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
- CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
-}
-
-/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
-/// mark the start of a region for the specified scoping descriptor.
-void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
- // Lazily construct llvm.dbg.region.start function.
- if (!RegionStartFn)
- RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
-
- // Call llvm.dbg.func.start.
- CallInst::Create(RegionStartFn, D.getNode(), "", BB);
-}
-
-/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
-/// mark the end of a region for the specified scoping descriptor.
-void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
- // Lazily construct llvm.dbg.region.end function.
- if (!RegionEndFn)
- RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
-
- // Call llvm.dbg.region.end.
- CallInst::Create(RegionEndFn, D.getNode(), "", BB);
-}
-
/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
-void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
+Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Instruction *InsertBefore) {
// Cast the storage to a {}* for the call to llvm.dbg.declare.
Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertBefore);
DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
Value *Args[] = { Storage, D.getNode() };
- CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
+ return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
}
/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
-void DIFactory::InsertDeclare(Value *Storage, DIVariable D,
+Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
BasicBlock *InsertAtEnd) {
// Cast the storage to a {}* for the call to llvm.dbg.declare.
Storage = new BitCastInst(Storage, EmptyStructPtr, "", InsertAtEnd);
DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
Value *Args[] = { Storage, D.getNode() };
- CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
+ return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
+}
+
+/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
+Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
+ DIVariable D,
+ Instruction *InsertBefore) {
+ assert(V && "no value passed to dbg.value");
+ assert(Offset->getType() == Type::getInt64Ty(V->getContext()) &&
+ "offset must be i64");
+ if (!ValueFn)
+ ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
+
+ Value *Elts[] = { V };
+ Value *Args[] = { MDNode::get(V->getContext(), Elts, 1), Offset,
+ D.getNode() };
+ return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
}
+/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
+Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset,
+ DIVariable D,
+ BasicBlock *InsertAtEnd) {
+ assert(V && "no value passed to dbg.value");
+ assert(Offset->getType() == Type::getInt64Ty(V->getContext()) &&
+ "offset must be i64");
+ if (!ValueFn)
+ ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
+
+ Value *Elts[] = { V };
+ Value *Args[] = { MDNode::get(V->getContext(), Elts, 1), Offset,
+ D.getNode() };
+ return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
+}
//===----------------------------------------------------------------------===//
// DebugInfoFinder implementations.
/// processModule - Process entire module and collect debug info.
void DebugInfoFinder::processModule(Module &M) {
-#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
MetadataContext &TheMetadata = M.getContext().getMetadata();
unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
-#endif
+
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
++BI) {
- if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
- processStopPoint(SPI);
- else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
- processFuncStart(FSI);
- else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
- processRegionStart(DRS);
- else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
- processRegionEnd(DRE);
- else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
+ if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
processDeclare(DDI);
-#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
- else if (MDDbgKind) {
- if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI)) {
- DILocation Loc(L);
- DIScope S(Loc.getScope().getNode());
- if (S.isCompileUnit())
- addCompileUnit(DICompileUnit(S.getNode()));
- else if (S.isSubprogram())
- processSubprogram(DISubprogram(S.getNode()));
- else if (S.isLexicalBlock())
- processLexicalBlock(DILexicalBlock(S.getNode()));
- }
- }
-#endif
+ else if (MDDbgKind)
+ if (MDNode *L = TheMetadata.getMD(MDDbgKind, BI))
+ processLocation(DILocation(L));
}
NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
}
}
+/// processLocation - Process DILocation.
+void DebugInfoFinder::processLocation(DILocation Loc) {
+ if (Loc.isNull()) return;
+ DIScope S(Loc.getScope().getNode());
+ if (S.isNull()) return;
+ if (S.isCompileUnit())
+ addCompileUnit(DICompileUnit(S.getNode()));
+ else if (S.isSubprogram())
+ processSubprogram(DISubprogram(S.getNode()));
+ else if (S.isLexicalBlock())
+ processLexicalBlock(DILexicalBlock(S.getNode()));
+ processLocation(Loc.getOrigLocation());
+}
+
/// processType - Process DIType.
void DebugInfoFinder::processType(DIType DT) {
if (!addType(DT))
processType(SP.getType());
}
-/// processStopPoint - Process DbgStopPointInst.
-void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
- MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
- addCompileUnit(DICompileUnit(Context));
-}
-
-/// processFuncStart - Process DbgFuncStartInst.
-void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
- MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
- processSubprogram(DISubprogram(SP));
-}
-
-/// processRegionStart - Process DbgRegionStart.
-void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
- MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
- processSubprogram(DISubprogram(SP));
-}
-
-/// processRegionEnd - Process DbgRegionEnd.
-void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
- MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
- processSubprogram(DISubprogram(SP));
-}
-
/// processDeclare - Process DbgDeclareInst.
void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
DIVariable DV(cast<MDNode>(DDI->getVariable()));
if (!DIGV) return false;
DIGlobalVariable Var(cast<MDNode>(DIGV));
- if (const char *D = Var.getDisplayName())
+ StringRef D = Var.getDisplayName();
+ if (!D.empty())
DisplayName = D;
LineNo = Var.getLineNumber();
Unit = Var.getCompileUnit();
if (!DDI) return false;
DIVariable Var(cast<MDNode>(DDI->getVariable()));
- if (const char *D = Var.getName())
+ StringRef D = Var.getName();
+ if (!D.empty())
DisplayName = D;
LineNo = Var.getLineNumber();
Unit = Var.getCompileUnit();
TypeD = Var.getType();
}
- if (const char *T = TypeD.getName())
+ StringRef T = TypeD.getName();
+ if (!T.empty())
Type = T;
- if (const char *F = Unit.getFilename())
+ StringRef F = Unit.getFilename();
+ if (!F.empty())
File = F;
- if (const char *D = Unit.getDirectory())
+ StringRef D = Unit.getDirectory();
+ if (!D.empty())
Dir = D;
return true;
}
return DebugLoc::get(Id);
}
- /// isInlinedFnStart - Return true if FSI is starting an inlined function.
- bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
- DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
- if (Subprogram.describes(CurrentFn))
- return false;
-
- return true;
+ /// getDISubprogram - Find subprogram that is enclosing this scope.
+ DISubprogram getDISubprogram(MDNode *Scope) {
+ DIDescriptor D(Scope);
+ if (D.isNull())
+ return DISubprogram();
+
+ if (D.isCompileUnit())
+ return DISubprogram();
+
+ if (D.isSubprogram())
+ return DISubprogram(Scope);
+
+ if (D.isLexicalBlock())
+ return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
+
+ return DISubprogram();
}
- /// isInlinedFnEnd - Return true if REI is ending an inlined function.
- bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
- DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
- if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
- return false;
-
- return true;
+ /// getDICompositeType - Find underlying composite type.
+ DICompositeType getDICompositeType(DIType T) {
+ if (T.isNull())
+ return DICompositeType();
+
+ if (T.isCompositeType())
+ return DICompositeType(T.getNode());
+
+ if (T.isDerivedType())
+ return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
+
+ return DICompositeType();
}
}