diff --git a/llvm/lib/Analysis/CFGPrinter.cpp b/llvm/lib/Analysis/CFGPrinter.cpp --- a/llvm/lib/Analysis/CFGPrinter.cpp +++ b/llvm/lib/Analysis/CFGPrinter.cpp @@ -96,6 +96,8 @@ } bool runOnFunction(Function &F) override { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return false; auto *BPI = &getAnalysis().getBPI(); auto *BFI = &getAnalysis().getBFI(); viewCFG(F, BFI, BPI, getMaxFreq(F, BFI)); @@ -111,13 +113,15 @@ AU.setPreservesAll(); } }; -} +} // namespace char CFGViewerLegacyPass::ID = 0; INITIALIZE_PASS(CFGViewerLegacyPass, "view-cfg", "View CFG of function", false, true) PreservedAnalyses CFGViewerPass::run(Function &F, FunctionAnalysisManager &AM) { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return PreservedAnalyses::all(); auto *BFI = &AM.getResult(F); auto *BPI = &AM.getResult(F); viewCFG(F, BFI, BPI, getMaxFreq(F, BFI)); @@ -132,6 +136,8 @@ } bool runOnFunction(Function &F) override { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return false; auto *BPI = &getAnalysis().getBPI(); auto *BFI = &getAnalysis().getBFI(); viewCFG(F, BFI, BPI, getMaxFreq(F, BFI), /*CFGOnly=*/true); @@ -147,7 +153,7 @@ AU.setPreservesAll(); } }; -} +} // namespace char CFGOnlyViewerLegacyPass::ID = 0; INITIALIZE_PASS(CFGOnlyViewerLegacyPass, "view-cfg-only", @@ -155,6 +161,8 @@ PreservedAnalyses CFGOnlyViewerPass::run(Function &F, FunctionAnalysisManager &AM) { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return PreservedAnalyses::all(); auto *BFI = &AM.getResult(F); auto *BPI = &AM.getResult(F); viewCFG(F, BFI, BPI, getMaxFreq(F, BFI), /*CFGOnly=*/true); @@ -169,6 +177,8 @@ } bool runOnFunction(Function &F) override { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return false; auto *BPI = &getAnalysis().getBPI(); auto *BFI = &getAnalysis().getBFI(); writeCFGToDotFile(F, BFI, BPI, getMaxFreq(F, BFI)); @@ -184,7 +194,7 @@ AU.setPreservesAll(); } }; -} +} // namespace char CFGPrinterLegacyPass::ID = 0; INITIALIZE_PASS(CFGPrinterLegacyPass, "dot-cfg", @@ -192,6 +202,8 @@ PreservedAnalyses CFGPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return PreservedAnalyses::all(); auto *BFI = &AM.getResult(F); auto *BPI = &AM.getResult(F); writeCFGToDotFile(F, BFI, BPI, getMaxFreq(F, BFI)); @@ -206,6 +218,8 @@ } bool runOnFunction(Function &F) override { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return false; auto *BPI = &getAnalysis().getBPI(); auto *BFI = &getAnalysis().getBFI(); writeCFGToDotFile(F, BFI, BPI, getMaxFreq(F, BFI), /*CFGOnly=*/true); @@ -220,7 +234,7 @@ AU.setPreservesAll(); } }; -} +} // namespace char CFGOnlyPrinterLegacyPass::ID = 0; INITIALIZE_PASS(CFGOnlyPrinterLegacyPass, "dot-cfg-only", @@ -229,6 +243,8 @@ PreservedAnalyses CFGOnlyPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { + if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) + return PreservedAnalyses::all(); auto *BFI = &AM.getResult(F); auto *BPI = &AM.getResult(F); writeCFGToDotFile(F, BFI, BPI, getMaxFreq(F, BFI), /*CFGOnly=*/true); diff --git a/llvm/test/Other/cfg-printer-filter.ll b/llvm/test/Other/cfg-printer-filter.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Other/cfg-printer-filter.ll @@ -0,0 +1,40 @@ +;RUN: opt < %s -dot-cfg -cfg-dot-filename-prefix=%t -cfg-func-name=f 2>/dev/null +;RUN: FileCheck %s -input-file=%t.f.dot -check-prefix=F +;RUN: FileCheck %s -input-file=%t.func.dot -check-prefix=Func +;RUN: not test -f %t.other.dot + +;RUN: opt < %s -dot-cfg-only -cfg-dot-filename-prefix=%t-only -cfg-func-name=f 2>/dev/null +;RUN: FileCheck %s -input-file=%t-only.f.dot -check-prefix=F +;RUN: FileCheck %s -input-file=%t-only.func.dot -check-prefix=Func +;RUN: not test -f %t-only.other.dot + +;END. + +; F: digraph "CFG for 'f' function" +define void @f(i32) { +entry: + %check = icmp sgt i32 %0, 0 + br i1 %check, label %if, label %exit +if: ; preds = %entry + br label %exit +exit: ; preds = %entry, %if + ret void +} + +; Func: digraph "CFG for 'func' function" +define void @func(i32) { +entry: + %check = icmp sgt i32 %0, 0 + br label %exit +exit: ; preds = %entry + ret void +} + +define void @other(i32) { +entry: + %check = icmp sgt i32 %0, 0 + br label %exit +exit: ; preds = %entry + ret void +} +