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) { |
| 53 | assert(isModulePassName(Name)); |
| 54 | if (Name == "no-op-module") { |
| 55 | MPM.addPass(NoOpModulePass()); |
| 56 | return true; |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame^] | 61 | static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) { |
| 62 | assert(isFunctionPassName(Name)); |
| 63 | if (Name == "no-op-function") { |
| 64 | FPM.addPass(NoOpFunctionPass()); |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | static bool parseFunctionPassPipeline(FunctionPassManager &FPM, |
| 71 | StringRef &PipelineText) { |
| 72 | for (;;) { |
| 73 | // Parse nested pass managers by recursing. |
| 74 | if (PipelineText.startswith("function(")) { |
| 75 | FunctionPassManager NestedFPM; |
| 76 | |
| 77 | // Parse the inner pipeline inte the nested manager. |
| 78 | PipelineText = PipelineText.substr(strlen("function(")); |
| 79 | if (!parseFunctionPassPipeline(NestedFPM, PipelineText)) |
| 80 | return false; |
| 81 | assert(!PipelineText.empty() && PipelineText[0] == ')'); |
| 82 | PipelineText = PipelineText.substr(1); |
| 83 | |
| 84 | // Add the nested pass manager with the appropriate adaptor. |
| 85 | FPM.addPass(NestedFPM); |
| 86 | } else { |
| 87 | // Otherwise try to parse a pass name. |
| 88 | size_t End = PipelineText.find_first_of(",)"); |
| 89 | if (!parseFunctionPassName(FPM, PipelineText.substr(0, End))) |
| 90 | return false; |
| 91 | |
| 92 | PipelineText = PipelineText.substr(End); |
| 93 | } |
| 94 | |
| 95 | if (PipelineText.empty() || PipelineText[0] == ')') |
| 96 | return true; |
| 97 | |
| 98 | assert(PipelineText[0] == ','); |
| 99 | PipelineText = PipelineText.substr(1); |
| 100 | } |
| 101 | } |
| 102 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 103 | static bool parseModulePassPipeline(ModulePassManager &MPM, |
| 104 | StringRef &PipelineText) { |
| 105 | for (;;) { |
| 106 | // Parse nested pass managers by recursing. |
| 107 | if (PipelineText.startswith("module(")) { |
Chandler Carruth | 258dbb3 | 2014-01-11 12:06:47 +0000 | [diff] [blame] | 108 | ModulePassManager NestedMPM; |
| 109 | |
| 110 | // Parse the inner pipeline into the nested manager. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 111 | PipelineText = PipelineText.substr(strlen("module(")); |
Chandler Carruth | 258dbb3 | 2014-01-11 12:06:47 +0000 | [diff] [blame] | 112 | if (!parseModulePassPipeline(NestedMPM, PipelineText)) |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 113 | return false; |
| 114 | assert(!PipelineText.empty() && PipelineText[0] == ')'); |
| 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(")); |
| 124 | if (!parseFunctionPassPipeline(NestedFPM, PipelineText)) |
| 125 | return false; |
| 126 | assert(!PipelineText.empty() && PipelineText[0] == ')'); |
| 127 | PipelineText = PipelineText.substr(1); |
| 128 | |
| 129 | // Add the nested pass manager with the appropriate adaptor. |
| 130 | MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM)); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 131 | } else { |
| 132 | // Otherwise try to parse a pass name. |
| 133 | size_t End = PipelineText.find_first_of(",)"); |
| 134 | if (!parseModulePassName(MPM, PipelineText.substr(0, End))) |
| 135 | return false; |
| 136 | |
| 137 | PipelineText = PipelineText.substr(End); |
| 138 | } |
| 139 | |
| 140 | if (PipelineText.empty() || PipelineText[0] == ')') |
| 141 | return true; |
| 142 | |
| 143 | assert(PipelineText[0] == ','); |
| 144 | PipelineText = PipelineText.substr(1); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Primary pass pipeline description parsing routine. |
| 149 | // FIXME: Should this routine accept a TargetMachine or require the caller to |
| 150 | // pre-populate the analysis managers with target-specific stuff? |
| 151 | bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) { |
| 152 | // Look at the first entry to figure out which layer to start parsing at. |
| 153 | if (PipelineText.startswith("module(")) |
| 154 | return parseModulePassPipeline(MPM, PipelineText); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame^] | 155 | if (PipelineText.startswith("function(")) { |
| 156 | FunctionPassManager FPM; |
| 157 | if (!parseFunctionPassPipeline(FPM, PipelineText)) |
| 158 | return false; |
| 159 | MPM.addPass(createModuleToFunctionPassAdaptor(FPM)); |
| 160 | return true; |
| 161 | } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 162 | |
| 163 | // This isn't a direct pass manager name, look for the end of a pass name. |
| 164 | StringRef FirstName = PipelineText.substr(0, PipelineText.find_first_of(",")); |
| 165 | if (isModulePassName(FirstName)) |
| 166 | return parseModulePassPipeline(MPM, PipelineText); |
| 167 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame^] | 168 | if (isFunctionPassName(FirstName)) { |
| 169 | FunctionPassManager FPM; |
| 170 | if (!parseFunctionPassPipeline(FPM, PipelineText)) |
| 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 | return false; |
| 177 | } |