Home | History | Annotate | Download | only in bugpoint
      1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
      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 //
     10 // This program is an automated compiler debugger tool.  It is used to narrow
     11 // down miscompilations and crash problems to a specific pass in the compiler,
     12 // and the specific Module or Function input that is causing the problem.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #include "BugDriver.h"
     17 #include "ToolRunner.h"
     18 #include "llvm/IR/LLVMContext.h"
     19 #include "llvm/IR/LegacyPassManager.h"
     20 #include "llvm/IR/LegacyPassNameParser.h"
     21 #include "llvm/LinkAllIR.h"
     22 #include "llvm/LinkAllPasses.h"
     23 #include "llvm/Support/CommandLine.h"
     24 #include "llvm/Support/ManagedStatic.h"
     25 #include "llvm/Support/PluginLoader.h"
     26 #include "llvm/Support/PrettyStackTrace.h"
     27 #include "llvm/Support/Process.h"
     28 #include "llvm/Support/Signals.h"
     29 #include "llvm/Support/Valgrind.h"
     30 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
     31 
     32 //Enable this macro to debug bugpoint itself.
     33 //#define DEBUG_BUGPOINT 1
     34 
     35 using namespace llvm;
     36 
     37 static cl::opt<bool>
     38 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
     39                                "on program to find bugs"), cl::init(false));
     40 
     41 static cl::list<std::string>
     42 InputFilenames(cl::Positional, cl::OneOrMore,
     43                cl::desc("<input llvm ll/bc files>"));
     44 
     45 static cl::opt<unsigned>
     46 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
     47              cl::desc("Number of seconds program is allowed to run before it "
     48                       "is killed (default is 300s), 0 disables timeout"));
     49 
     50 static cl::opt<int>
     51 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
     52             cl::desc("Maximum amount of memory to use. 0 disables check."
     53                      " Defaults to 400MB (800MB under valgrind)."));
     54 
     55 static cl::opt<bool>
     56 UseValgrind("enable-valgrind",
     57             cl::desc("Run optimizations through valgrind"));
     58 
     59 // The AnalysesList is automatically populated with registered Passes by the
     60 // PassNameParser.
     61 //
     62 static cl::list<const PassInfo*, bool, PassNameParser>
     63 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
     64 
     65 static cl::opt<bool>
     66 StandardLinkOpts("std-link-opts",
     67                  cl::desc("Include the standard link time optimizations"));
     68 
     69 static cl::opt<bool>
     70 OptLevelO1("O1",
     71            cl::desc("Optimization level 1. Identical to 'opt -O1'"));
     72 
     73 static cl::opt<bool>
     74 OptLevelO2("O2",
     75            cl::desc("Optimization level 2. Identical to 'opt -O2'"));
     76 
     77 static cl::opt<bool>
     78 OptLevelO3("O3",
     79            cl::desc("Optimization level 3. Identical to 'opt -O3'"));
     80 
     81 static cl::opt<std::string>
     82 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
     83 
     84 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
     85 bool llvm::BugpointIsInterrupted = false;
     86 
     87 #ifndef DEBUG_BUGPOINT
     88 static void BugpointInterruptFunction() {
     89   BugpointIsInterrupted = true;
     90 }
     91 #endif
     92 
     93 // Hack to capture a pass list.
     94 namespace {
     95   class AddToDriver : public legacy::FunctionPassManager {
     96     BugDriver &D;
     97   public:
     98     AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
     99 
    100     void add(Pass *P) override {
    101       const void *ID = P->getPassID();
    102       const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
    103       D.addPass(PI->getPassArgument());
    104     }
    105   };
    106 }
    107 
    108 #ifdef LINK_POLLY_INTO_TOOLS
    109 namespace polly {
    110 void initializePollyPasses(llvm::PassRegistry &Registry);
    111 }
    112 #endif
    113 
    114 int main(int argc, char **argv) {
    115 #ifndef DEBUG_BUGPOINT
    116   llvm::sys::PrintStackTraceOnErrorSignal();
    117   llvm::PrettyStackTraceProgram X(argc, argv);
    118   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
    119 #endif
    120 
    121   // Initialize passes
    122   PassRegistry &Registry = *PassRegistry::getPassRegistry();
    123   initializeCore(Registry);
    124   initializeScalarOpts(Registry);
    125   initializeObjCARCOpts(Registry);
    126   initializeVectorization(Registry);
    127   initializeIPO(Registry);
    128   initializeAnalysis(Registry);
    129   initializeTransformUtils(Registry);
    130   initializeInstCombine(Registry);
    131   initializeInstrumentation(Registry);
    132   initializeTarget(Registry);
    133 
    134 #ifdef LINK_POLLY_INTO_TOOLS
    135   polly::initializePollyPasses(Registry);
    136 #endif
    137 
    138   cl::ParseCommandLineOptions(argc, argv,
    139                               "LLVM automatic testcase reducer. See\nhttp://"
    140                               "llvm.org/cmds/bugpoint.html"
    141                               " for more information.\n");
    142 #ifndef DEBUG_BUGPOINT
    143   sys::SetInterruptFunction(BugpointInterruptFunction);
    144 #endif
    145 
    146   LLVMContext& Context = getGlobalContext();
    147   // If we have an override, set it and then track the triple we want Modules
    148   // to use.
    149   if (!OverrideTriple.empty()) {
    150     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
    151     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
    152   }
    153 
    154   if (MemoryLimit < 0) {
    155     // Set the default MemoryLimit.  Be sure to update the flag's description if
    156     // you change this.
    157     if (sys::RunningOnValgrind() || UseValgrind)
    158       MemoryLimit = 800;
    159     else
    160       MemoryLimit = 400;
    161   }
    162 
    163   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
    164               UseValgrind, Context);
    165   if (D.addSources(InputFilenames)) return 1;
    166 
    167   AddToDriver PM(D);
    168 
    169   if (StandardLinkOpts) {
    170     PassManagerBuilder Builder;
    171     Builder.Inliner = createFunctionInliningPass();
    172     Builder.populateLTOPassManager(PM);
    173   }
    174 
    175   if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
    176     PassManagerBuilder Builder;
    177     if (OptLevelO1)
    178       Builder.Inliner = createAlwaysInlinerPass();
    179     else if (OptLevelO2)
    180       Builder.Inliner = createFunctionInliningPass(225);
    181     else
    182       Builder.Inliner = createFunctionInliningPass(275);
    183     Builder.populateFunctionPassManager(PM);
    184     Builder.populateModulePassManager(PM);
    185   }
    186 
    187   for (const PassInfo *PI : PassList)
    188     D.addPass(PI->getPassArgument());
    189 
    190   // Bugpoint has the ability of generating a plethora of core files, so to
    191   // avoid filling up the disk, we prevent it
    192 #ifndef DEBUG_BUGPOINT
    193   sys::Process::PreventCoreFiles();
    194 #endif
    195 
    196   std::string Error;
    197   bool Failure = D.run(Error);
    198   if (!Error.empty()) {
    199     errs() << Error;
    200     return 1;
    201   }
    202   return Failure;
    203 }
    204