+
+namespace llvm {
+ /// Finds the stoppoint coressponding to this instruction, that is the
+ /// stoppoint that dominates this instruction
+ const DbgStopPointInst *findStopPoint(const Instruction *Inst)
+ {
+ if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
+ return DSI;
+
+ const BasicBlock *BB = Inst->getParent();
+ BasicBlock::const_iterator I = Inst, B;
+ do {
+ B = BB->begin();
+ // A BB consisting only of a terminator can't have a stoppoint.
+ if (I != B) {
+ do {
+ --I;
+ if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
+ return DSI;
+ } while (I != B);
+ }
+ // This BB didn't have a stoppoint: if there is only one
+ // predecessor, look for a stoppoint there.
+ // We could use getIDom(), but that would require dominator info.
+ BB = I->getParent()->getUniquePredecessor();
+ if (BB)
+ I = BB->getTerminator();
+ } while (BB != 0);
+ return 0;
+ }
+
+ /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
+ /// instruction in this Basic Block, and returns the stoppoint for it.
+ const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB)
+ {
+ for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
+ if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
+ return DSI;
+ }
+ // Fallback to looking for stoppoint of unique predecessor.
+ // Useful if this BB contains no stoppoints, but unique predecessor does.
+ BB = BB->getUniquePredecessor();
+ if (BB)
+ return findStopPoint(BB->getTerminator());
+ return 0;
+ }
+
+ /// Finds the dbg.declare intrinsic corresponding to this value if any.
+ /// It looks through pointer casts too.
+ const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts)
+ {
+ if (stripCasts) {
+ V = V->stripPointerCasts();
+ // Look for the bitcast.
+ for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
+ I != E; ++I) {
+ if (isa<BitCastInst>(I))
+ return findDbgDeclare(*I, false);
+ }
+ return 0;
+ }
+
+ // Find dbg.declare among uses of the instruction.
+ for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
+ I != E; ++I) {
+ if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
+ return DDI;
+ }
+ return 0;
+ }
+}
+
+/// dump - print compile unit.
+void DICompileUnit::dump() const {
+ cerr << " [" << dwarf::LanguageString(getLanguage()) << "] ";
+ cerr << " [" << getDirectory() << "/" << getFilename() << " ]";
+}
+
+/// dump - print type.
+void DIType::dump() const {
+ if (isNull()) return;
+ if (!getName().empty())
+ cerr << " [" << getName() << "] ";
+ unsigned Tag = getTag();
+ cerr << " [" << dwarf::TagString(Tag) << "] ";
+ // TODO : Print context
+ getCompileUnit().dump();
+ cerr << " ["
+ << getLineNumber() << ", "
+ << getSizeInBits() << ", "
+ << getAlignInBits() << ", "
+ << getOffsetInBits()
+ << "] ";
+ if (isPrivate())
+ cerr << " [private] ";
+ else if (isProtected())
+ cerr << " [protected] ";
+ if (isForwardDecl())
+ cerr << " [fwd] ";
+
+ if (isBasicType(Tag))
+ DIBasicType(GV).dump();
+ else if (isDerivedType(Tag))
+ DIDerivedType(GV).dump();
+ else if (isCompositeType(Tag))
+ DICompositeType(GV).dump();
+ else {
+ cerr << "Invalid DIType\n";
+ return;
+ }
+ cerr << "\n";
+}
+
+/// dump - print basic type.
+void DIBasicType::dump() const {
+ cerr << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
+
+}
+
+/// dump - print derived type.
+void DIDerivedType::dump() const {
+ cerr << "\n\t Derived From: "; getTypeDerivedFrom().dump();
+}
+
+/// dump - print composite type.
+void DICompositeType::dump() const {
+ DIArray A = getTypeArray();
+ if (A.isNull())
+ return;
+ cerr << " [" << A.getNumElements() << " elements]";
+}
+
+/// dump - print global.
+void DIGlobal::dump() const {
+
+ if (!getName().empty())
+ cerr << " [" << getName() << "] ";
+ unsigned Tag = getTag();
+ cerr << " [" << dwarf::TagString(Tag) << "] ";
+ // TODO : Print context
+ getCompileUnit().dump();
+ cerr << " [" << getLineNumber() << "] ";
+ if (isLocalToUnit())
+ cerr << " [local] ";
+ if (isDefinition())
+ cerr << " [def] ";
+
+ if (isGlobalVariable(Tag))
+ DIGlobalVariable(GV).dump();
+
+ cerr << "\n";
+}
+
+/// dump - print subprogram.
+void DISubprogram::dump() const {
+ DIGlobal::dump();
+}
+
+/// dump - print global variable.
+void DIGlobalVariable::dump() const {
+ cerr << " ["; getGlobal()->dump(); cerr << "] ";
+}
+
+/// dump - print variable.
+void DIVariable::dump() const {
+ if (!getName().empty())
+ cerr << " [" << getName() << "] ";
+ getCompileUnit().dump();
+ cerr << " [" << getLineNumber() << "] ";
+ getType().dump();
+ cerr << "\n";
+}