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