[PM] Add (very skeletal) support to opt for running the new pass
[oota-llvm.git] / tools / opt / Passes.cpp
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
24   /// \brief No-op module pass which does nothing.
25 struct NoOpModulePass {
26   PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
27 };
28
29 } // End anonymous namespace.
30
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;
35
36   return false;
37 }
38
39 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
40   assert(isModulePassName(Name));
41   if (Name == "no-op-module") {
42     MPM.addPass(NoOpModulePass());
43     return true;
44   }
45   return false;
46 }
47
48 static bool parseModulePassPipeline(ModulePassManager &MPM,
49                                     StringRef &PipelineText) {
50   for (;;) {
51     // Parse nested pass managers by recursing.
52     if (PipelineText.startswith("module(")) {
53       PipelineText = PipelineText.substr(strlen("module("));
54       if (!parseModulePassPipeline(MPM, PipelineText))
55         return false;
56       assert(!PipelineText.empty() && PipelineText[0] == ')');
57       PipelineText = PipelineText.substr(1);
58     } else {
59       // Otherwise try to parse a pass name.
60       size_t End = PipelineText.find_first_of(",)");
61       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
62         return false;
63
64       PipelineText = PipelineText.substr(End);
65     }
66
67     if (PipelineText.empty() || PipelineText[0] == ')')
68       return true;
69
70     assert(PipelineText[0] == ',');
71     PipelineText = PipelineText.substr(1);
72   }
73 }
74
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);
82
83   // FIXME: Support parsing function pass manager nests.
84
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);
89
90   // FIXME: Support parsing function pass names.
91
92   return false;
93 }