1 //===- PrintPasses.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/IR/PrintPasses.h" 10 #include "llvm/Support/CommandLine.h" 11 #include <unordered_set> 12 13 using namespace llvm; 14 15 // Print IR out before/after specified passes. 16 static cl::list<std::string> 17 PrintBefore("print-before", 18 llvm::cl::desc("Print IR before specified passes"), 19 cl::CommaSeparated, cl::Hidden); 20 21 static cl::list<std::string> 22 PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"), 23 cl::CommaSeparated, cl::Hidden); 24 25 static cl::opt<bool> PrintBeforeAll("print-before-all", 26 llvm::cl::desc("Print IR before each pass"), 27 cl::init(false), cl::Hidden); 28 static cl::opt<bool> PrintAfterAll("print-after-all", 29 llvm::cl::desc("Print IR after each pass"), 30 cl::init(false), cl::Hidden); 31 32 static cl::opt<bool> 33 PrintModuleScope("print-module-scope", 34 cl::desc("When printing IR for print-[before|after]{-all} " 35 "always print a module IR"), 36 cl::init(false), cl::Hidden); 37 38 static cl::list<std::string> 39 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"), 40 cl::desc("Only print IR for functions whose name " 41 "match this for all print-[before|after][-all] " 42 "options"), 43 cl::CommaSeparated, cl::Hidden); 44 45 /// This is a helper to determine whether to print IR before or 46 /// after a pass. 47 48 bool llvm::shouldPrintBeforeSomePass() { 49 return PrintBeforeAll || !PrintBefore.empty(); 50 } 51 52 bool llvm::shouldPrintAfterSomePass() { 53 return PrintAfterAll || !PrintAfter.empty(); 54 } 55 56 static bool shouldPrintBeforeOrAfterPass(StringRef PassID, 57 ArrayRef<std::string> PassesToPrint) { 58 for (auto &Pass : PassesToPrint) { 59 if (Pass == PassID) 60 return true; 61 } 62 return false; 63 } 64 65 bool llvm::shouldPrintBeforeAll() { return PrintBeforeAll; } 66 67 bool llvm::shouldPrintAfterAll() { return PrintAfterAll; } 68 69 bool llvm::shouldPrintBeforePass(StringRef PassID) { 70 return PrintBeforeAll || shouldPrintBeforeOrAfterPass(PassID, PrintBefore); 71 } 72 73 bool llvm::shouldPrintAfterPass(StringRef PassID) { 74 return PrintAfterAll || shouldPrintBeforeOrAfterPass(PassID, PrintAfter); 75 } 76 77 std::vector<std::string> llvm::printBeforePasses() { 78 return std::vector<std::string>(PrintBefore); 79 } 80 81 std::vector<std::string> llvm::printAfterPasses() { 82 return std::vector<std::string>(PrintAfter); 83 } 84 85 bool llvm::forcePrintModuleIR() { return PrintModuleScope; } 86 87 bool llvm::isFunctionInPrintList(StringRef FunctionName) { 88 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(), 89 PrintFuncsList.end()); 90 return PrintFuncNames.empty() || 91 PrintFuncNames.count(std::string(FunctionName)); 92 } 93