-// ---------- Define Printers for module and function passes ------------
-namespace {
-
-struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
- static char ID;
- const PassInfo *PassToPrint;
- raw_ostream &Out;
- std::string PassName;
-
- CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
- CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
- std::string PassToPrintName = PassToPrint->getPassName();
- PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
- }
-
- virtual bool runOnSCC(CallGraphSCC &SCC) {
- if (!Quiet)
- Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-
- // Get and print pass...
- for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
- Function *F = (*I)->getFunction();
- if (F)
- getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
- F->getParent());
- }
- return false;
- }
-
- virtual const char *getPassName() const { return PassName.c_str(); }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequiredID(PassToPrint->getTypeInfo());
- AU.setPreservesAll();
- }
-};
-
-char CallGraphSCCPassPrinter::ID = 0;
-
-struct ModulePassPrinter : public ModulePass {
- static char ID;
- const PassInfo *PassToPrint;
- raw_ostream &Out;
- std::string PassName;
-
- ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
- : ModulePass(ID), PassToPrint(PI), Out(out) {
- std::string PassToPrintName = PassToPrint->getPassName();
- PassName = "ModulePass Printer: " + PassToPrintName;
- }
-
- virtual bool runOnModule(Module &M) {
- if (!Quiet)
- Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-
- // Get and print pass...
- getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
- return false;
- }
-
- virtual const char *getPassName() const { return PassName.c_str(); }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequiredID(PassToPrint->getTypeInfo());
- AU.setPreservesAll();
- }
-};
-
-char ModulePassPrinter::ID = 0;
-struct FunctionPassPrinter : public FunctionPass {
- const PassInfo *PassToPrint;
- raw_ostream &Out;
- static char ID;
- std::string PassName;
-
- FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
- : FunctionPass(ID), PassToPrint(PI), Out(out) {
- std::string PassToPrintName = PassToPrint->getPassName();
- PassName = "FunctionPass Printer: " + PassToPrintName;
- }
-
- virtual bool runOnFunction(Function &F) {
- if (!Quiet)
- Out << "Printing analysis '" << PassToPrint->getPassName()
- << "' for function '" << F.getName() << "':\n";
-
- // Get and print pass...
- getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
- F.getParent());
- return false;
- }
-
- virtual const char *getPassName() const { return PassName.c_str(); }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequiredID(PassToPrint->getTypeInfo());
- AU.setPreservesAll();
- }
-};
-
-char FunctionPassPrinter::ID = 0;
-
-struct LoopPassPrinter : public LoopPass {
- static char ID;
- const PassInfo *PassToPrint;
- raw_ostream &Out;
- std::string PassName;
-
- LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
- LoopPass(ID), PassToPrint(PI), Out(out) {
- std::string PassToPrintName = PassToPrint->getPassName();
- PassName = "LoopPass Printer: " + PassToPrintName;
- }
-
-
- virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
- if (!Quiet)
- Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-
- // Get and print pass...
- getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
- L->getHeader()->getParent()->getParent());
- return false;
- }
-
- virtual const char *getPassName() const { return PassName.c_str(); }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequiredID(PassToPrint->getTypeInfo());
- AU.setPreservesAll();
- }
-};
-
-char LoopPassPrinter::ID = 0;
-
-struct RegionPassPrinter : public RegionPass {
- static char ID;
- const PassInfo *PassToPrint;
- raw_ostream &Out;
- std::string PassName;
-
- RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
- PassToPrint(PI), Out(out) {
- std::string PassToPrintName = PassToPrint->getPassName();
- PassName = "RegionPass Printer: " + PassToPrintName;
- }
-
- virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
- if (!Quiet) {
- Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
- << "region: '" << R->getNameStr() << "' in function '"
- << R->getEntry()->getParent()->getName() << "':\n";
- }
- // Get and print pass...
- getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
- R->getEntry()->getParent()->getParent());
- return false;
- }
-
- virtual const char *getPassName() const { return PassName.c_str(); }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequiredID(PassToPrint->getTypeInfo());
- AU.setPreservesAll();
- }
-};
-
-char RegionPassPrinter::ID = 0;
-
-struct BasicBlockPassPrinter : public BasicBlockPass {
- const PassInfo *PassToPrint;
- raw_ostream &Out;
- static char ID;
- std::string PassName;
-
- BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
- : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
- std::string PassToPrintName = PassToPrint->getPassName();
- PassName = "BasicBlockPass Printer: " + PassToPrintName;
- }
-
- virtual bool runOnBasicBlock(BasicBlock &BB) {
- if (!Quiet)
- Out << "Printing Analysis info for BasicBlock '" << BB.getName()
- << "': Pass " << PassToPrint->getPassName() << ":\n";
-
- // Get and print pass...
- getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
- BB.getParent()->getParent());
- return false;
- }
-
- virtual const char *getPassName() const { return PassName.c_str(); }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequiredID(PassToPrint->getTypeInfo());
- AU.setPreservesAll();
- }
-};
-
-char BasicBlockPassPrinter::ID = 0;
-
-struct BreakpointPrinter : public ModulePass {
- raw_ostream &Out;
- static char ID;
- DITypeIdentifierMap TypeIdentifierMap;
-
- BreakpointPrinter(raw_ostream &out)
- : ModulePass(ID), Out(out) {
- }
-
- void getContextName(DIDescriptor Context, std::string &N) {
- if (Context.isNameSpace()) {
- DINameSpace NS(Context);
- if (!NS.getName().empty()) {
- getContextName(NS.getContext(), N);
- N = N + NS.getName().str() + "::";
- }
- } else if (Context.isType()) {
- DIType TY(Context);
- if (!TY.getName().empty()) {
- getContextName(TY.getContext().resolve(TypeIdentifierMap), N);
- N = N + TY.getName().str() + "::";
- }
- }
- }
-
- virtual bool runOnModule(Module &M) {
- TypeIdentifierMap.clear();
- NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
- if (CU_Nodes)
- TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
-
- StringSet<> Processed;
- if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
- for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
- std::string Name;
- DISubprogram SP(NMD->getOperand(i));
- assert((!SP || SP.isSubprogram()) &&
- "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
- if (!SP)
- continue;
- getContextName(SP.getContext().resolve(TypeIdentifierMap), Name);
- Name = Name + SP.getDisplayName().str();
- if (!Name.empty() && Processed.insert(Name)) {
- Out << Name << "\n";
- }
- }
- return false;
- }
-
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.setPreservesAll();
- }
-};