Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1 | //===- Passes.cpp - Parsing, selection, and running of passes -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// |
| 11 | /// This file provides the infrastructure to parse and build a custom pass |
| 12 | /// manager based on a commandline flag. It also provides helpers to aid in |
| 13 | /// analyzing, debugging, and testing pass structures. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "Passes.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LazyCallGraph.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 20 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 28 | /// \brief No-op module pass which does nothing. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 29 | struct NoOpModulePass { |
| 30 | PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); } |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 31 | static StringRef name() { return "NoOpModulePass"; } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 34 | /// \brief No-op function pass which does nothing. |
| 35 | struct NoOpFunctionPass { |
| 36 | PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); } |
| 37 | static StringRef name() { return "NoOpFunctionPass"; } |
| 38 | }; |
| 39 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 40 | } // End anonymous namespace. |
| 41 | |
| 42 | // FIXME: Factor all of the parsing logic into a .def file that we include |
| 43 | // under different macros. |
| 44 | static bool isModulePassName(StringRef Name) { |
| 45 | if (Name == "no-op-module") return true; |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 46 | if (Name == "print") return true; |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 47 | if (Name == "print-cg") return true; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 48 | |
| 49 | return false; |
| 50 | } |
| 51 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 52 | static bool isFunctionPassName(StringRef Name) { |
| 53 | if (Name == "no-op-function") return true; |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 54 | if (Name == "print") return true; |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 59 | static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 60 | if (Name == "no-op-module") { |
| 61 | MPM.addPass(NoOpModulePass()); |
| 62 | return true; |
| 63 | } |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 64 | if (Name == "print") { |
| 65 | MPM.addPass(PrintModulePass(dbgs())); |
| 66 | return true; |
| 67 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 68 | if (Name == "print-cg") { |
| 69 | MPM.addPass(LazyCallGraphPrinterPass(dbgs())); |
| 70 | return true; |
| 71 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 75 | static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) { |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 76 | if (Name == "no-op-function") { |
| 77 | FPM.addPass(NoOpFunctionPass()); |
| 78 | return true; |
| 79 | } |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 80 | if (Name == "print") { |
| 81 | FPM.addPass(PrintFunctionPass(dbgs())); |
| 82 | return true; |
| 83 | } |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
| 87 | static bool parseFunctionPassPipeline(FunctionPassManager &FPM, |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 88 | StringRef &PipelineText, |
| 89 | bool VerifyEachPass) { |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 90 | for (;;) { |
| 91 | // Parse nested pass managers by recursing. |
| 92 | if (PipelineText.startswith("function(")) { |
| 93 | FunctionPassManager NestedFPM; |
| 94 | |
| 95 | // Parse the inner pipeline inte the nested manager. |
| 96 | PipelineText = PipelineText.substr(strlen("function(")); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 97 | if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) || |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 98 | PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 99 | return false; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 100 | assert(PipelineText[0] == ')'); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 101 | PipelineText = PipelineText.substr(1); |
| 102 | |
| 103 | // Add the nested pass manager with the appropriate adaptor. |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame^] | 104 | FPM.addPass(std::move(NestedFPM)); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 105 | } else { |
| 106 | // Otherwise try to parse a pass name. |
| 107 | size_t End = PipelineText.find_first_of(",)"); |
| 108 | if (!parseFunctionPassName(FPM, PipelineText.substr(0, End))) |
| 109 | return false; |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 110 | if (VerifyEachPass) |
| 111 | FPM.addPass(VerifierPass()); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 112 | |
| 113 | PipelineText = PipelineText.substr(End); |
| 114 | } |
| 115 | |
| 116 | if (PipelineText.empty() || PipelineText[0] == ')') |
| 117 | return true; |
| 118 | |
| 119 | assert(PipelineText[0] == ','); |
| 120 | PipelineText = PipelineText.substr(1); |
| 121 | } |
| 122 | } |
| 123 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 124 | static bool parseModulePassPipeline(ModulePassManager &MPM, |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 125 | StringRef &PipelineText, |
| 126 | bool VerifyEachPass) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 127 | for (;;) { |
| 128 | // Parse nested pass managers by recursing. |
| 129 | if (PipelineText.startswith("module(")) { |
Chandler Carruth | 258dbb3 | 2014-01-11 12:06:47 +0000 | [diff] [blame] | 130 | ModulePassManager NestedMPM; |
| 131 | |
| 132 | // Parse the inner pipeline into the nested manager. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 133 | PipelineText = PipelineText.substr(strlen("module(")); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 134 | if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) || |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 135 | PipelineText.empty()) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 136 | return false; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 137 | assert(PipelineText[0] == ')'); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 138 | PipelineText = PipelineText.substr(1); |
Chandler Carruth | 258dbb3 | 2014-01-11 12:06:47 +0000 | [diff] [blame] | 139 | |
| 140 | // Now add the nested manager as a module pass. |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame^] | 141 | MPM.addPass(std::move(NestedMPM)); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 142 | } else if (PipelineText.startswith("function(")) { |
| 143 | FunctionPassManager NestedFPM; |
| 144 | |
| 145 | // Parse the inner pipeline inte the nested manager. |
| 146 | PipelineText = PipelineText.substr(strlen("function(")); |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 147 | if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) || |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 148 | PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 149 | return false; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 150 | assert(PipelineText[0] == ')'); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 151 | PipelineText = PipelineText.substr(1); |
| 152 | |
| 153 | // Add the nested pass manager with the appropriate adaptor. |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame^] | 154 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM))); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 155 | } else { |
| 156 | // Otherwise try to parse a pass name. |
| 157 | size_t End = PipelineText.find_first_of(",)"); |
| 158 | if (!parseModulePassName(MPM, PipelineText.substr(0, End))) |
| 159 | return false; |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 160 | if (VerifyEachPass) |
| 161 | MPM.addPass(VerifierPass()); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 162 | |
| 163 | PipelineText = PipelineText.substr(End); |
| 164 | } |
| 165 | |
| 166 | if (PipelineText.empty() || PipelineText[0] == ')') |
| 167 | return true; |
| 168 | |
| 169 | assert(PipelineText[0] == ','); |
| 170 | PipelineText = PipelineText.substr(1); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Primary pass pipeline description parsing routine. |
| 175 | // FIXME: Should this routine accept a TargetMachine or require the caller to |
| 176 | // pre-populate the analysis managers with target-specific stuff? |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 177 | bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText, |
| 178 | bool VerifyEachPass) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 179 | // Look at the first entry to figure out which layer to start parsing at. |
| 180 | if (PipelineText.startswith("module(")) |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 181 | return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) && |
| 182 | PipelineText.empty(); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 183 | if (PipelineText.startswith("function(")) { |
| 184 | FunctionPassManager FPM; |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 185 | if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) || |
| 186 | !PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 187 | return false; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame^] | 188 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 191 | |
| 192 | // This isn't a direct pass manager name, look for the end of a pass name. |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 193 | StringRef FirstName = |
| 194 | PipelineText.substr(0, PipelineText.find_first_of(",)")); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 195 | if (isModulePassName(FirstName)) |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 196 | return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) && |
| 197 | PipelineText.empty(); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 198 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 199 | if (isFunctionPassName(FirstName)) { |
| 200 | FunctionPassManager FPM; |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 201 | if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) || |
| 202 | !PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 203 | return false; |
Chandler Carruth | c3f3da3 | 2014-03-09 11:49:53 +0000 | [diff] [blame^] | 204 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 205 | return true; |
| 206 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 207 | |
| 208 | return false; |
| 209 | } |