Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/IR/LegacyPassNameParser.h
Line
Count
Source (jump to first uncovered line)
1
//===- LegacyPassNameParser.h -----------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains the PassNameParser and FilteredPassNameParser<> classes,
10
// which are used to add command line arguments to a utility for all of the
11
// passes that have been registered into the system.
12
//
13
// The PassNameParser class adds ALL passes linked into the system (that are
14
// creatable) as command line arguments to the tool (when instantiated with the
15
// appropriate command line option template).  The FilteredPassNameParser<>
16
// template is used for the same purposes as PassNameParser, except that it only
17
// includes passes that have a PassType that are compatible with the filter
18
// (which is the template argument).
19
//
20
// Note that this is part of the legacy pass manager infrastructure and will be
21
// (eventually) going away.
22
//
23
//===----------------------------------------------------------------------===//
24
25
#ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
26
#define LLVM_IR_LEGACYPASSNAMEPARSER_H
27
28
#include "llvm/ADT/STLExtras.h"
29
#include "llvm/Pass.h"
30
#include "llvm/Support/CommandLine.h"
31
#include "llvm/Support/ErrorHandling.h"
32
#include "llvm/Support/raw_ostream.h"
33
#include <cstring>
34
35
namespace llvm {
36
37
//===----------------------------------------------------------------------===//
38
// PassNameParser class - Make use of the pass registration mechanism to
39
// automatically add a command line argument to opt for each pass.
40
//
41
class PassNameParser : public PassRegistrationListener,
42
                       public cl::parser<const PassInfo*> {
43
public:
44
  PassNameParser(cl::Option &O);
45
  ~PassNameParser() override;
46
47
252k
  void initialize() {
48
252k
    cl::parser<const PassInfo*>::initialize();
49
252k
50
252k
    // Add all of the passes to the map that got initialized before 'this' did.
51
252k
    enumeratePasses();
52
252k
  }
53
54
  // ignorablePassImpl - Can be overriden in subclasses to refine the list of
55
  // which passes we want to include.
56
  //
57
71.1M
  virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
58
59
71.1M
  inline bool ignorablePass(const PassInfo *P) const {
60
71.1M
    // Ignore non-selectable and non-constructible passes!  Ignore
61
71.1M
    // non-optimizations.
62
71.1M
    return P->getPassArgument().empty() || 
P->getNormalCtor() == nullptr71.1M
||
63
71.1M
           
ignorablePassImpl(P)71.1M
;
64
71.1M
  }
65
66
  // Implement the PassRegistrationListener callbacks used to populate our map
67
  //
68
71.1M
  void passRegistered(const PassInfo *P) override {
69
71.1M
    if (ignorablePass(P)) 
return44
;
70
71.1M
    if (findOption(P->getPassArgument().data()) != getNumOptions()) {
71
0
      errs() << "Two passes with the same argument (-"
72
0
           << P->getPassArgument() << ") attempted to be registered!\n";
73
0
      llvm_unreachable(nullptr);
74
0
    }
75
71.1M
    addLiteralOption(P->getPassArgument().data(), P, P->getPassName().data());
76
71.1M
  }
77
264k
  void passEnumerate(const PassInfo *P) override { passRegistered(P); }
78
79
  // printOptionInfo - Print out information about this option.  Override the
80
  // default implementation to sort the table before we print...
81
4
  void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
82
4
    PassNameParser *PNP = const_cast<PassNameParser*>(this);
83
4
    array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValCompare);
84
4
    cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
85
4
  }
86
87
private:
88
  // ValCompare - Provide a sorting comparator for Values elements...
89
  static int ValCompare(const PassNameParser::OptionInfo *VT1,
90
6.81k
                        const PassNameParser::OptionInfo *VT2) {
91
6.81k
    return VT1->Name.compare(VT2->Name);
92
6.81k
  }
93
};
94
95
///===----------------------------------------------------------------------===//
96
/// FilteredPassNameParser class - Make use of the pass registration
97
/// mechanism to automatically add a command line argument to opt for
98
/// each pass that satisfies a filter criteria.  Filter should return
99
/// true for passes to be registered as command-line options.
100
///
101
template<typename Filter>
102
class FilteredPassNameParser : public PassNameParser {
103
private:
104
  Filter filter;
105
106
public:
107
  bool ignorablePassImpl(const PassInfo *P) const override {
108
    return !filter(*P);
109
  }
110
};
111
112
///===----------------------------------------------------------------------===//
113
/// PassArgFilter - A filter for use with PassNameFilterParser that only
114
/// accepts a Pass whose Arg matches certain strings.
115
///
116
/// Use like this:
117
///
118
/// extern const char AllowedPassArgs[] = "-anders_aa -dse";
119
///
120
/// static cl::list<
121
///   const PassInfo*,
122
///   bool,
123
///   FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
124
/// PassList(cl::desc("Passes available:"));
125
///
126
/// Only the -anders_aa and -dse options will be available to the user.
127
///
128
template<const char *Args>
129
class PassArgFilter {
130
public:
131
  bool operator()(const PassInfo &P) const {
132
    return StringRef(Args).contains(P.getPassArgument());
133
  }
134
};
135
136
} // End llvm namespace
137
138
#endif