1 //===- Passes.cpp - Parsing, selection, and running of passes -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
18 #include "llvm/IR/PassManager.h"
24 /// \brief No-op module pass which does nothing.
25 struct NoOpModulePass {
26 PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
27 static StringRef name() { return "NoOpModulePass"; }
30 } // End anonymous namespace.
32 // FIXME: Factor all of the parsing logic into a .def file that we include
33 // under different macros.
34 static bool isModulePassName(StringRef Name) {
35 if (Name == "no-op-module") return true;
40 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
41 assert(isModulePassName(Name));
42 if (Name == "no-op-module") {
43 MPM.addPass(NoOpModulePass());
49 static bool parseModulePassPipeline(ModulePassManager &MPM,
50 StringRef &PipelineText) {
52 // Parse nested pass managers by recursing.
53 if (PipelineText.startswith("module(")) {
54 PipelineText = PipelineText.substr(strlen("module("));
55 if (!parseModulePassPipeline(MPM, PipelineText))
57 assert(!PipelineText.empty() && PipelineText[0] == ')');
58 PipelineText = PipelineText.substr(1);
60 // Otherwise try to parse a pass name.
61 size_t End = PipelineText.find_first_of(",)");
62 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
65 PipelineText = PipelineText.substr(End);
68 if (PipelineText.empty() || PipelineText[0] == ')')
71 assert(PipelineText[0] == ',');
72 PipelineText = PipelineText.substr(1);
76 // Primary pass pipeline description parsing routine.
77 // FIXME: Should this routine accept a TargetMachine or require the caller to
78 // pre-populate the analysis managers with target-specific stuff?
79 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
80 // Look at the first entry to figure out which layer to start parsing at.
81 if (PipelineText.startswith("module("))
82 return parseModulePassPipeline(MPM, PipelineText);
84 // FIXME: Support parsing function pass manager nests.
86 // This isn't a direct pass manager name, look for the end of a pass name.
87 StringRef FirstName = PipelineText.substr(0, PipelineText.find_first_of(","));
88 if (isModulePassName(FirstName))
89 return parseModulePassPipeline(MPM, PipelineText);
91 // FIXME: Support parsing function pass names.