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(); }
29 } // End anonymous namespace.
31 // FIXME: Factor all of the parsing logic into a .def file that we include
32 // under different macros.
33 static bool isModulePassName(StringRef Name) {
34 if (Name == "no-op-module") return true;
39 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
40 assert(isModulePassName(Name));
41 if (Name == "no-op-module") {
42 MPM.addPass(NoOpModulePass());
48 static bool parseModulePassPipeline(ModulePassManager &MPM,
49 StringRef &PipelineText) {
51 // Parse nested pass managers by recursing.
52 if (PipelineText.startswith("module(")) {
53 PipelineText = PipelineText.substr(strlen("module("));
54 if (!parseModulePassPipeline(MPM, PipelineText))
56 assert(!PipelineText.empty() && PipelineText[0] == ')');
57 PipelineText = PipelineText.substr(1);
59 // Otherwise try to parse a pass name.
60 size_t End = PipelineText.find_first_of(",)");
61 if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
64 PipelineText = PipelineText.substr(End);
67 if (PipelineText.empty() || PipelineText[0] == ')')
70 assert(PipelineText[0] == ',');
71 PipelineText = PipelineText.substr(1);
75 // Primary pass pipeline description parsing routine.
76 // FIXME: Should this routine accept a TargetMachine or require the caller to
77 // pre-populate the analysis managers with target-specific stuff?
78 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
79 // Look at the first entry to figure out which layer to start parsing at.
80 if (PipelineText.startswith("module("))
81 return parseModulePassPipeline(MPM, PipelineText);
83 // FIXME: Support parsing function pass manager nests.
85 // This isn't a direct pass manager name, look for the end of a pass name.
86 StringRef FirstName = PipelineText.substr(0, PipelineText.find_first_of(","));
87 if (isModulePassName(FirstName))
88 return parseModulePassPipeline(MPM, PipelineText);
90 // FIXME: Support parsing function pass names.