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