1 //===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an interface that allows bugpoint to choose different
11 // combinations of optimizations to run on the selected input. Bugpoint will
12 // run these optimizations and record the success/failure of each. This way
13 // we can hopefully spot bugs in the optimizations.
15 //===----------------------------------------------------------------------===//
17 #include "BugDriver.h"
18 #include "ToolRunner.h"
19 #include "llvm/Pass.h"
24 /// runManyPasses - Take the specified pass list and create different
25 /// combinations of passes to compile the program with. Compile the program with
26 /// each set and mark test to see if it compiled correctly. If the passes
27 /// compiled correctly output nothing and rearrange the passes into a new order.
28 /// If the passes did not compile correctly, output the command required to
29 /// recreate the failure. This returns true if a compiler error is found.
31 bool BugDriver::runManyPasses(const std::vector
<const PassInfo
*> &AllPasses
) {
32 setPassesToRun(AllPasses
);
33 outs() << "Starting bug finding procedure...\n\n";
35 // Creating a reference output if necessary
36 if (initializeExecutionEnvironment()) return false;
39 if (ReferenceOutputFile
.empty()) {
40 outs() << "Generating reference output from raw program: \n";
41 if (!createReferenceFile(Program
))
50 // Step 1: Randomize the order of the optimizer passes.
52 std::random_shuffle(PassesToRun
.begin(), PassesToRun
.end());
55 // Step 2: Run optimizer passes on the program and check for success.
57 outs() << "Running selected passes on program to test for crash: ";
58 for(int i
= 0, e
= PassesToRun
.size(); i
!= e
; i
++) {
59 outs() << "-" << PassesToRun
[i
]->getPassArgument( )<< " ";
63 if(runPasses(PassesToRun
, Filename
, false)) {
65 outs() << "Optimizer passes caused failure!\n\n";
66 debugOptimizerCrash();
69 outs() << "Combination " << num
<< " optimized successfully!\n";
73 // Step 3: Compile the optimized code.
75 outs() << "Running the code generator to test for a crash: ";
77 compileProgram(Program
);
79 } catch (ToolExecutionError
&TEE
) {
80 outs() << "\n*** compileProgram threw an exception: ";
82 return debugCodeGeneratorCrash();
86 // Step 4: Run the program and compare its output to the reference
87 // output (created above).
89 outs() << "*** Checking if passes caused miscompliation:\n";
91 if (diffProgram(Filename
, "", false)) {
92 outs() << "\n*** diffProgram returned true!\n";
93 debugMiscompilation();
96 outs() << "\n*** diff'd output matches!\n";
98 } catch (ToolExecutionError
&TEE
) {
100 debugCodeGeneratorCrash();
104 sys::Path(Filename
).eraseFromDisk();