122347Spst//===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===// 222347Spst// 322347Spst// The LLVM Compiler Infrastructure 422347Spst// 522347Spst// This file is distributed under the University of Illinois Open Source 622347Spst// License. See LICENSE.TXT for details. 729964Sache// 829964Sache//===----------------------------------------------------------------------===// 922347Spst// 1022347Spst// This file contains the PassNameParser and FilteredPassNameParser<> classes, 1122347Spst// which are used to add command line arguments to a utility for all of the 1222347Spst// passes that have been registered into the system. 1322347Spst// 1422347Spst// The PassNameParser class adds ALL passes linked into the system (that are 1522347Spst// creatable) as command line arguments to the tool (when instantiated with the 1622347Spst// appropriate command line option template). The FilteredPassNameParser<> 1722347Spst// template is used for the same purposes as PassNameParser, except that it only 1822347Spst// includes passes that have a PassType that are compatible with the filter 1922347Spst// (which is the template argument). 2022347Spst// 2129964Sache//===----------------------------------------------------------------------===// 2229964Sache 2329964Sache#ifndef LLVM_SUPPORT_PASSNAMEPARSER_H 2422347Spst#define LLVM_SUPPORT_PASSNAMEPARSER_H 2522347Spst 2622347Spst#include "llvm/ADT/STLExtras.h" 2722347Spst#include "llvm/Pass.h" 2822347Spst#include "llvm/Support/CommandLine.h" 2922347Spst#include "llvm/Support/ErrorHandling.h" 3022347Spst#include "llvm/Support/raw_ostream.h" 3122347Spst#include <cstring> 3222347Spst 3322347Spstnamespace llvm { 3422347Spst 3522347Spst//===----------------------------------------------------------------------===// 3622347Spst// PassNameParser class - Make use of the pass registration mechanism to 3722347Spst// automatically add a command line argument to opt for each pass. 3822347Spst// 3922347Spstclass PassNameParser : public PassRegistrationListener, 4022347Spst public cl::parser<const PassInfo*> { 4122347Spst cl::Option *Opt; 4222347Spstpublic: 4322347Spst PassNameParser() : Opt(0) {} 4422347Spst virtual ~PassNameParser(); 4522347Spst 4622347Spst void initialize(cl::Option &O) { 4722347Spst Opt = &O; 4822347Spst cl::parser<const PassInfo*>::initialize(O); 4922347Spst 5022347Spst // Add all of the passes to the map that got initialized before 'this' did. 5122347Spst enumeratePasses(); 5222347Spst } 5322347Spst 5422347Spst // ignorablePassImpl - Can be overriden in subclasses to refine the list of 5522347Spst // which passes we want to include. 5622347Spst // 5722347Spst virtual bool ignorablePassImpl(const PassInfo *P) const { return false; } 5822347Spst 5922347Spst inline bool ignorablePass(const PassInfo *P) const { 6022347Spst // Ignore non-selectable and non-constructible passes! Ignore 6122347Spst // non-optimizations. 6222347Spst return P->getPassArgument() == 0 || *P->getPassArgument() == 0 || 6322347Spst P->getNormalCtor() == 0 || ignorablePassImpl(P); 6422347Spst } 6522347Spst 6622347Spst // Implement the PassRegistrationListener callbacks used to populate our map 6722347Spst // 6822347Spst virtual void passRegistered(const PassInfo *P) { 6922347Spst if (ignorablePass(P) || !Opt) return; 7022347Spst if (findOption(P->getPassArgument()) != getNumOptions()) { 7122347Spst errs() << "Two passes with the same argument (-" 7222347Spst << P->getPassArgument() << ") attempted to be registered!\n"; 7322347Spst llvm_unreachable(0); 7429964Sache } 7522347Spst addLiteralOption(P->getPassArgument(), P, P->getPassName()); 7622347Spst } 7722347Spst virtual void passEnumerate(const PassInfo *P) { passRegistered(P); } 7822347Spst 7922347Spst // printOptionInfo - Print out information about this option. Override the 8022347Spst // default implementation to sort the table before we print... 8122347Spst virtual void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const { 8222347Spst PassNameParser *PNP = const_cast<PassNameParser*>(this); 8322347Spst array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan); 8422347Spst cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth); 8522347Spst } 8629964Sache 8722347Spstprivate: 8822347Spst // ValLessThan - Provide a sorting comparator for Values elements... 8922347Spst static int ValLessThan(const PassNameParser::OptionInfo *VT1, 9022347Spst const PassNameParser::OptionInfo *VT2) { 9122347Spst return std::strcmp(VT1->Name, VT2->Name); 9222347Spst } 9322347Spst}; 9422347Spst 9522347Spst///===----------------------------------------------------------------------===// 9622347Spst/// FilteredPassNameParser class - Make use of the pass registration 9722347Spst/// mechanism to automatically add a command line argument to opt for 9822347Spst/// each pass that satisfies a filter criteria. Filter should return 9922347Spst/// true for passes to be registered as command-line options. 10022347Spst/// 10122347Spsttemplate<typename Filter> 10222347Spstclass FilteredPassNameParser : public PassNameParser { 10322347Spstprivate: 10422347Spst Filter filter; 10522347Spst 10622347Spstpublic: 10722347Spst bool ignorablePassImpl(const PassInfo *P) const { return !filter(*P); } 10822347Spst}; 10922347Spst 11022347Spst///===----------------------------------------------------------------------===// 11122347Spst/// PassArgFilter - A filter for use with PassNameFilterParser that only 11222347Spst/// accepts a Pass whose Arg matches certain strings. 11322347Spst/// 11422347Spst/// Use like this: 11522347Spst/// 11622347Spst/// extern const char AllowedPassArgs[] = "-anders_aa -dse"; 11722347Spst/// 11822347Spst/// static cl::list< 11922347Spst/// const PassInfo*, 12022347Spst/// bool, 12122347Spst/// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > > 12222347Spst/// PassList(cl::desc("Passes available:")); 12322347Spst/// 12422347Spst/// Only the -anders_aa and -dse options will be available to the user. 12522347Spst/// 12622347Spsttemplate<const char *Args> 12722347Spstclass PassArgFilter { 12822347Spstpublic: 12922347Spst bool operator()(const PassInfo &P) const { 13022347Spst return(std::strstr(Args, P.getPassArgument())); 13122347Spst } 13222347Spst}; 13322347Spst 13422347Spst} // End llvm namespace 13522347Spst 13622347Spst#endif 13722347Spst