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