106f32e7eSjoerg //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This program is an automated compiler debugger tool.  It is used to narrow
1006f32e7eSjoerg // down miscompilations and crash problems to a specific pass in the compiler,
1106f32e7eSjoerg // and the specific Module or Function input that is causing the problem.
1206f32e7eSjoerg //
1306f32e7eSjoerg //===----------------------------------------------------------------------===//
1406f32e7eSjoerg 
1506f32e7eSjoerg #include "BugDriver.h"
1606f32e7eSjoerg #include "ToolRunner.h"
1706f32e7eSjoerg #include "llvm/Config/llvm-config.h"
1806f32e7eSjoerg #include "llvm/IR/LLVMContext.h"
1906f32e7eSjoerg #include "llvm/IR/LegacyPassManager.h"
2006f32e7eSjoerg #include "llvm/IR/LegacyPassNameParser.h"
21*da58b97aSjoerg #include "llvm/InitializePasses.h"
2206f32e7eSjoerg #include "llvm/LinkAllIR.h"
2306f32e7eSjoerg #include "llvm/LinkAllPasses.h"
24*da58b97aSjoerg #include "llvm/Passes/PassPlugin.h"
2506f32e7eSjoerg #include "llvm/Support/CommandLine.h"
2606f32e7eSjoerg #include "llvm/Support/InitLLVM.h"
2706f32e7eSjoerg #include "llvm/Support/ManagedStatic.h"
2806f32e7eSjoerg #include "llvm/Support/PluginLoader.h"
2906f32e7eSjoerg #include "llvm/Support/PrettyStackTrace.h"
3006f32e7eSjoerg #include "llvm/Support/Process.h"
3106f32e7eSjoerg #include "llvm/Support/TargetSelect.h"
3206f32e7eSjoerg #include "llvm/Support/Valgrind.h"
3306f32e7eSjoerg #include "llvm/Transforms/IPO/AlwaysInliner.h"
3406f32e7eSjoerg #include "llvm/Transforms/IPO/PassManagerBuilder.h"
3506f32e7eSjoerg 
3606f32e7eSjoerg // Enable this macro to debug bugpoint itself.
3706f32e7eSjoerg //#define DEBUG_BUGPOINT 1
3806f32e7eSjoerg 
3906f32e7eSjoerg using namespace llvm;
4006f32e7eSjoerg 
4106f32e7eSjoerg static cl::opt<bool>
4206f32e7eSjoerg     FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
4306f32e7eSjoerg                                    "on program to find bugs"),
4406f32e7eSjoerg              cl::init(false));
4506f32e7eSjoerg 
4606f32e7eSjoerg static cl::list<std::string>
4706f32e7eSjoerg     InputFilenames(cl::Positional, cl::OneOrMore,
4806f32e7eSjoerg                    cl::desc("<input llvm ll/bc files>"));
4906f32e7eSjoerg 
5006f32e7eSjoerg static cl::opt<unsigned> TimeoutValue(
5106f32e7eSjoerg     "timeout", cl::init(300), cl::value_desc("seconds"),
5206f32e7eSjoerg     cl::desc("Number of seconds program is allowed to run before it "
5306f32e7eSjoerg              "is killed (default is 300s), 0 disables timeout"));
5406f32e7eSjoerg 
5506f32e7eSjoerg static cl::opt<int> MemoryLimit(
5606f32e7eSjoerg     "mlimit", cl::init(-1), cl::value_desc("MBytes"),
5706f32e7eSjoerg     cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
5806f32e7eSjoerg              "400MB (800MB under valgrind, 0 with sanitizers)."));
5906f32e7eSjoerg 
6006f32e7eSjoerg static cl::opt<bool>
6106f32e7eSjoerg     UseValgrind("enable-valgrind",
6206f32e7eSjoerg                 cl::desc("Run optimizations through valgrind"));
6306f32e7eSjoerg 
6406f32e7eSjoerg // The AnalysesList is automatically populated with registered Passes by the
6506f32e7eSjoerg // PassNameParser.
6606f32e7eSjoerg //
6706f32e7eSjoerg static cl::list<const PassInfo *, bool, PassNameParser>
6806f32e7eSjoerg     PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
6906f32e7eSjoerg 
7006f32e7eSjoerg static cl::opt<bool>
7106f32e7eSjoerg     StandardLinkOpts("std-link-opts",
7206f32e7eSjoerg                      cl::desc("Include the standard link time optimizations"));
7306f32e7eSjoerg 
7406f32e7eSjoerg static cl::opt<bool>
7506f32e7eSjoerg     OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
7606f32e7eSjoerg 
7706f32e7eSjoerg static cl::opt<bool>
7806f32e7eSjoerg     OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
7906f32e7eSjoerg 
8006f32e7eSjoerg static cl::opt<bool> OptLevelOs(
8106f32e7eSjoerg     "Os",
8206f32e7eSjoerg     cl::desc(
8306f32e7eSjoerg         "Like -O2 with extra optimizations for size. Similar to clang -Os"));
8406f32e7eSjoerg 
8506f32e7eSjoerg static cl::opt<bool>
8606f32e7eSjoerg OptLevelOz("Oz",
8706f32e7eSjoerg            cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
8806f32e7eSjoerg 
8906f32e7eSjoerg static cl::opt<bool>
9006f32e7eSjoerg     OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
9106f32e7eSjoerg 
9206f32e7eSjoerg static cl::opt<std::string>
9306f32e7eSjoerg     OverrideTriple("mtriple", cl::desc("Override target triple for module"));
9406f32e7eSjoerg 
9506f32e7eSjoerg /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
9606f32e7eSjoerg bool llvm::BugpointIsInterrupted = false;
9706f32e7eSjoerg 
9806f32e7eSjoerg #ifndef DEBUG_BUGPOINT
BugpointInterruptFunction()9906f32e7eSjoerg static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }
10006f32e7eSjoerg #endif
10106f32e7eSjoerg 
10206f32e7eSjoerg // Hack to capture a pass list.
10306f32e7eSjoerg namespace {
10406f32e7eSjoerg class AddToDriver : public legacy::FunctionPassManager {
10506f32e7eSjoerg   BugDriver &D;
10606f32e7eSjoerg 
10706f32e7eSjoerg public:
AddToDriver(BugDriver & _D)10806f32e7eSjoerg   AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
10906f32e7eSjoerg 
add(Pass * P)11006f32e7eSjoerg   void add(Pass *P) override {
11106f32e7eSjoerg     const void *ID = P->getPassID();
11206f32e7eSjoerg     const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
113*da58b97aSjoerg     D.addPass(std::string(PI->getPassArgument()));
11406f32e7eSjoerg   }
11506f32e7eSjoerg };
11606f32e7eSjoerg }
11706f32e7eSjoerg 
11806f32e7eSjoerg // This routine adds optimization passes based on selected optimization level,
11906f32e7eSjoerg // OptLevel.
12006f32e7eSjoerg //
12106f32e7eSjoerg // OptLevel - Optimization Level
AddOptimizationPasses(legacy::FunctionPassManager & FPM,unsigned OptLevel,unsigned SizeLevel)12206f32e7eSjoerg static void AddOptimizationPasses(legacy::FunctionPassManager &FPM,
12306f32e7eSjoerg                                   unsigned OptLevel,
12406f32e7eSjoerg                                   unsigned SizeLevel) {
12506f32e7eSjoerg   PassManagerBuilder Builder;
12606f32e7eSjoerg   Builder.OptLevel = OptLevel;
12706f32e7eSjoerg   Builder.SizeLevel = SizeLevel;
12806f32e7eSjoerg 
12906f32e7eSjoerg   if (OptLevel > 1)
13006f32e7eSjoerg     Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
13106f32e7eSjoerg   else
13206f32e7eSjoerg     Builder.Inliner = createAlwaysInlinerLegacyPass();
13306f32e7eSjoerg 
13406f32e7eSjoerg   Builder.populateFunctionPassManager(FPM);
13506f32e7eSjoerg   Builder.populateModulePassManager(FPM);
13606f32e7eSjoerg }
13706f32e7eSjoerg 
138*da58b97aSjoerg #define HANDLE_EXTENSION(Ext)                                                  \
139*da58b97aSjoerg   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
140*da58b97aSjoerg #include "llvm/Support/Extension.def"
14106f32e7eSjoerg 
main(int argc,char ** argv)14206f32e7eSjoerg int main(int argc, char **argv) {
14306f32e7eSjoerg #ifndef DEBUG_BUGPOINT
14406f32e7eSjoerg   InitLLVM X(argc, argv);
14506f32e7eSjoerg #endif
14606f32e7eSjoerg 
14706f32e7eSjoerg   // Initialize passes
14806f32e7eSjoerg   PassRegistry &Registry = *PassRegistry::getPassRegistry();
14906f32e7eSjoerg   initializeCore(Registry);
15006f32e7eSjoerg   initializeScalarOpts(Registry);
15106f32e7eSjoerg   initializeObjCARCOpts(Registry);
15206f32e7eSjoerg   initializeVectorization(Registry);
15306f32e7eSjoerg   initializeIPO(Registry);
15406f32e7eSjoerg   initializeAnalysis(Registry);
15506f32e7eSjoerg   initializeTransformUtils(Registry);
15606f32e7eSjoerg   initializeInstCombine(Registry);
15706f32e7eSjoerg   initializeAggressiveInstCombine(Registry);
15806f32e7eSjoerg   initializeInstrumentation(Registry);
15906f32e7eSjoerg   initializeTarget(Registry);
16006f32e7eSjoerg 
16106f32e7eSjoerg   if (std::getenv("bar") == (char*) -1) {
16206f32e7eSjoerg     InitializeAllTargets();
16306f32e7eSjoerg     InitializeAllTargetMCs();
16406f32e7eSjoerg     InitializeAllAsmPrinters();
16506f32e7eSjoerg     InitializeAllAsmParsers();
16606f32e7eSjoerg   }
16706f32e7eSjoerg 
16806f32e7eSjoerg   cl::ParseCommandLineOptions(argc, argv,
16906f32e7eSjoerg                               "LLVM automatic testcase reducer. See\nhttp://"
17006f32e7eSjoerg                               "llvm.org/cmds/bugpoint.html"
17106f32e7eSjoerg                               " for more information.\n");
17206f32e7eSjoerg #ifndef DEBUG_BUGPOINT
17306f32e7eSjoerg   sys::SetInterruptFunction(BugpointInterruptFunction);
17406f32e7eSjoerg #endif
17506f32e7eSjoerg 
17606f32e7eSjoerg   LLVMContext Context;
17706f32e7eSjoerg   // If we have an override, set it and then track the triple we want Modules
17806f32e7eSjoerg   // to use.
17906f32e7eSjoerg   if (!OverrideTriple.empty()) {
18006f32e7eSjoerg     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
18106f32e7eSjoerg     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
18206f32e7eSjoerg   }
18306f32e7eSjoerg 
18406f32e7eSjoerg   if (MemoryLimit < 0) {
18506f32e7eSjoerg     // Set the default MemoryLimit.  Be sure to update the flag's description if
18606f32e7eSjoerg     // you change this.
18706f32e7eSjoerg     if (sys::RunningOnValgrind() || UseValgrind)
18806f32e7eSjoerg       MemoryLimit = 800;
18906f32e7eSjoerg     else
19006f32e7eSjoerg       MemoryLimit = 400;
19106f32e7eSjoerg #if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD ||            \
19206f32e7eSjoerg      LLVM_THREAD_SANITIZER_BUILD)
19306f32e7eSjoerg     // Starting from kernel 4.9 memory allocated with mmap is counted against
19406f32e7eSjoerg     // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
19506f32e7eSjoerg     MemoryLimit = 0;
19606f32e7eSjoerg #endif
19706f32e7eSjoerg   }
19806f32e7eSjoerg 
19906f32e7eSjoerg   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
20006f32e7eSjoerg               Context);
20106f32e7eSjoerg   if (D.addSources(InputFilenames))
20206f32e7eSjoerg     return 1;
20306f32e7eSjoerg 
20406f32e7eSjoerg   AddToDriver PM(D);
20506f32e7eSjoerg 
20606f32e7eSjoerg   if (StandardLinkOpts) {
20706f32e7eSjoerg     PassManagerBuilder Builder;
20806f32e7eSjoerg     Builder.Inliner = createFunctionInliningPass();
20906f32e7eSjoerg     Builder.populateLTOPassManager(PM);
21006f32e7eSjoerg   }
21106f32e7eSjoerg 
21206f32e7eSjoerg   if (OptLevelO1)
21306f32e7eSjoerg     AddOptimizationPasses(PM, 1, 0);
21406f32e7eSjoerg   else if (OptLevelO2)
21506f32e7eSjoerg     AddOptimizationPasses(PM, 2, 0);
21606f32e7eSjoerg   else if (OptLevelO3)
21706f32e7eSjoerg     AddOptimizationPasses(PM, 3, 0);
21806f32e7eSjoerg   else if (OptLevelOs)
21906f32e7eSjoerg     AddOptimizationPasses(PM, 2, 1);
22006f32e7eSjoerg   else if (OptLevelOz)
22106f32e7eSjoerg     AddOptimizationPasses(PM, 2, 2);
22206f32e7eSjoerg 
22306f32e7eSjoerg   for (const PassInfo *PI : PassList)
224*da58b97aSjoerg     D.addPass(std::string(PI->getPassArgument()));
22506f32e7eSjoerg 
22606f32e7eSjoerg // Bugpoint has the ability of generating a plethora of core files, so to
22706f32e7eSjoerg // avoid filling up the disk, we prevent it
22806f32e7eSjoerg #ifndef DEBUG_BUGPOINT
22906f32e7eSjoerg   sys::Process::PreventCoreFiles();
23006f32e7eSjoerg #endif
23106f32e7eSjoerg 
232*da58b97aSjoerg // Needed to pull in symbols from statically linked extensions, including static
233*da58b97aSjoerg // registration. It is unused otherwise because bugpoint has no support for
234*da58b97aSjoerg // NewPM.
235*da58b97aSjoerg #define HANDLE_EXTENSION(Ext)                                                  \
236*da58b97aSjoerg   (void)get##Ext##PluginInfo();
237*da58b97aSjoerg #include "llvm/Support/Extension.def"
238*da58b97aSjoerg 
23906f32e7eSjoerg   if (Error E = D.run()) {
24006f32e7eSjoerg     errs() << toString(std::move(E));
24106f32e7eSjoerg     return 1;
24206f32e7eSjoerg   }
24306f32e7eSjoerg   return 0;
24406f32e7eSjoerg }
245