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 | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 18 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 19 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 26 | /// \brief No-op module pass which does nothing. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 27 | struct NoOpModulePass { |
| 28 | PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); } |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 29 | static StringRef name() { return "NoOpModulePass"; } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 32 | /// \brief No-op function pass which does nothing. |
| 33 | struct NoOpFunctionPass { |
| 34 | PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); } |
| 35 | static StringRef name() { return "NoOpFunctionPass"; } |
| 36 | }; |
| 37 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 38 | } // End anonymous namespace. |
| 39 | |
| 40 | // FIXME: Factor all of the parsing logic into a .def file that we include |
| 41 | // under different macros. |
| 42 | static bool isModulePassName(StringRef Name) { |
| 43 | if (Name == "no-op-module") return true; |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 44 | if (Name == "print") return true; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 45 | |
| 46 | return false; |
| 47 | } |
| 48 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 49 | static bool isFunctionPassName(StringRef Name) { |
| 50 | if (Name == "no-op-function") return true; |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 51 | if (Name == "print") return true; |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 56 | static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) { |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 57 | if (Name == "no-op-module") { |
| 58 | MPM.addPass(NoOpModulePass()); |
| 59 | return true; |
| 60 | } |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 61 | if (Name == "print") { |
| 62 | MPM.addPass(PrintModulePass(dbgs())); |
| 63 | return true; |
| 64 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 68 | static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) { |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 69 | if (Name == "no-op-function") { |
| 70 | FPM.addPass(NoOpFunctionPass()); |
| 71 | return true; |
| 72 | } |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 73 | if (Name == "print") { |
| 74 | FPM.addPass(PrintFunctionPass(dbgs())); |
| 75 | return true; |
| 76 | } |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
| 80 | static bool parseFunctionPassPipeline(FunctionPassManager &FPM, |
| 81 | StringRef &PipelineText) { |
| 82 | for (;;) { |
| 83 | // Parse nested pass managers by recursing. |
| 84 | if (PipelineText.startswith("function(")) { |
| 85 | FunctionPassManager NestedFPM; |
| 86 | |
| 87 | // Parse the inner pipeline inte the nested manager. |
| 88 | PipelineText = PipelineText.substr(strlen("function(")); |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 89 | if (!parseFunctionPassPipeline(NestedFPM, PipelineText) || |
| 90 | PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 91 | return false; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 92 | assert(PipelineText[0] == ')'); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 93 | PipelineText = PipelineText.substr(1); |
| 94 | |
| 95 | // Add the nested pass manager with the appropriate adaptor. |
| 96 | FPM.addPass(NestedFPM); |
| 97 | } else { |
| 98 | // Otherwise try to parse a pass name. |
| 99 | size_t End = PipelineText.find_first_of(",)"); |
| 100 | if (!parseFunctionPassName(FPM, PipelineText.substr(0, End))) |
| 101 | return false; |
| 102 | |
| 103 | PipelineText = PipelineText.substr(End); |
| 104 | } |
| 105 | |
| 106 | if (PipelineText.empty() || PipelineText[0] == ')') |
| 107 | return true; |
| 108 | |
| 109 | assert(PipelineText[0] == ','); |
| 110 | PipelineText = PipelineText.substr(1); |
| 111 | } |
| 112 | } |
| 113 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 114 | static bool parseModulePassPipeline(ModulePassManager &MPM, |
| 115 | StringRef &PipelineText) { |
| 116 | for (;;) { |
| 117 | // Parse nested pass managers by recursing. |
| 118 | if (PipelineText.startswith("module(")) { |
Chandler Carruth | 258dbb3 | 2014-01-11 12:06:47 +0000 | [diff] [blame] | 119 | ModulePassManager NestedMPM; |
| 120 | |
| 121 | // Parse the inner pipeline into the nested manager. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 122 | PipelineText = PipelineText.substr(strlen("module(")); |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 123 | if (!parseModulePassPipeline(NestedMPM, PipelineText) || |
| 124 | PipelineText.empty()) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 125 | return false; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 126 | assert(PipelineText[0] == ')'); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 127 | PipelineText = PipelineText.substr(1); |
Chandler Carruth | 258dbb3 | 2014-01-11 12:06:47 +0000 | [diff] [blame] | 128 | |
| 129 | // Now add the nested manager as a module pass. |
| 130 | MPM.addPass(NestedMPM); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 131 | } else if (PipelineText.startswith("function(")) { |
| 132 | FunctionPassManager NestedFPM; |
| 133 | |
| 134 | // Parse the inner pipeline inte the nested manager. |
| 135 | PipelineText = PipelineText.substr(strlen("function(")); |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 136 | if (!parseFunctionPassPipeline(NestedFPM, PipelineText) || |
| 137 | PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 138 | return false; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 139 | assert(PipelineText[0] == ')'); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 140 | PipelineText = PipelineText.substr(1); |
| 141 | |
| 142 | // Add the nested pass manager with the appropriate adaptor. |
| 143 | MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM)); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 144 | } else { |
| 145 | // Otherwise try to parse a pass name. |
| 146 | size_t End = PipelineText.find_first_of(",)"); |
| 147 | if (!parseModulePassName(MPM, PipelineText.substr(0, End))) |
| 148 | return false; |
| 149 | |
| 150 | PipelineText = PipelineText.substr(End); |
| 151 | } |
| 152 | |
| 153 | if (PipelineText.empty() || PipelineText[0] == ')') |
| 154 | return true; |
| 155 | |
| 156 | assert(PipelineText[0] == ','); |
| 157 | PipelineText = PipelineText.substr(1); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Primary pass pipeline description parsing routine. |
| 162 | // FIXME: Should this routine accept a TargetMachine or require the caller to |
| 163 | // pre-populate the analysis managers with target-specific stuff? |
| 164 | bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) { |
| 165 | // Look at the first entry to figure out which layer to start parsing at. |
| 166 | if (PipelineText.startswith("module(")) |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 167 | return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty(); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 168 | if (PipelineText.startswith("function(")) { |
| 169 | FunctionPassManager FPM; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 170 | if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 171 | return false; |
| 172 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM)); |
| 173 | return true; |
| 174 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 175 | |
| 176 | // 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] | 177 | StringRef FirstName = |
| 178 | PipelineText.substr(0, PipelineText.find_first_of(",)")); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 179 | if (isModulePassName(FirstName)) |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 180 | return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty(); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 181 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 182 | if (isFunctionPassName(FirstName)) { |
| 183 | FunctionPassManager FPM; |
Chandler Carruth | 6546cb6 | 2014-01-12 10:02:02 +0000 | [diff] [blame] | 184 | if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty()) |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 185 | return false; |
| 186 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM)); |
| 187 | return true; |
| 188 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 189 | |
| 190 | return false; |
| 191 | } |