d58acaf1f6d6d18e8687dd216f24d6e0c7c7d592
[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   static StringRef name() { return "NoOpModulePass"; }
28 };
29
30 } // End anonymous namespace.
31
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;
36
37   return false;
38 }
39
40 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
41   assert(isModulePassName(Name));
42   if (Name == "no-op-module") {
43     MPM.addPass(NoOpModulePass());
44     return true;
45   }
46   return false;
47 }
48
49 static bool parseModulePassPipeline(ModulePassManager &MPM,
50                                     StringRef &PipelineText) {
51   for (;;) {
52     // Parse nested pass managers by recursing.
53     if (PipelineText.startswith("module(")) {
54       PipelineText = PipelineText.substr(strlen("module("));
55       if (!parseModulePassPipeline(MPM, PipelineText))
56         return false;
57       assert(!PipelineText.empty() && PipelineText[0] == ')');
58       PipelineText = PipelineText.substr(1);
59     } else {
60       // Otherwise try to parse a pass name.
61       size_t End = PipelineText.find_first_of(",)");
62       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
63         return false;
64
65       PipelineText = PipelineText.substr(End);
66     }
67
68     if (PipelineText.empty() || PipelineText[0] == ')')
69       return true;
70
71     assert(PipelineText[0] == ',');
72     PipelineText = PipelineText.substr(1);
73   }
74 }
75
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);
83
84   // FIXME: Support parsing function pass manager nests.
85
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);
90
91   // FIXME: Support parsing function pass names.
92
93   return false;
94 }