blob: fd1f84b7286bdda1822b792ee31dce2cc0ef5a95 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15//===----------------------------------------------------------------------===//
16
17#include "BugDriver.h"
18#include "ToolRunner.h"
19#include "llvm/Pass.h"
20#include <algorithm>
21#include <ctime>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
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.
30///
31bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) {
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000032 setPassesToRun(AllPasses);
Dan Gohmanb714fab2009-07-16 15:30:09 +000033 outs() << "Starting bug finding procedure...\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034
35 // Creating a reference output if necessary
36 if (initializeExecutionEnvironment()) return false;
37
Dan Gohmanb714fab2009-07-16 15:30:09 +000038 outs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 if (ReferenceOutputFile.empty()) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000040 outs() << "Generating reference output from raw program: \n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 if (!createReferenceFile(Program))
42 return false;
43 }
44
45 srand(time(NULL));
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000046
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 unsigned num = 1;
48 while(1) {
49 //
50 // Step 1: Randomize the order of the optimizer passes.
51 //
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000052 std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 //
55 // Step 2: Run optimizer passes on the program and check for success.
56 //
Dan Gohmanb714fab2009-07-16 15:30:09 +000057 outs() << "Running selected passes on program to test for crash: ";
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000058 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000059 outs() << "-" << PassesToRun[i]->getPassArgument( )<< " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 }
61
62 std::string Filename;
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000063 if(runPasses(PassesToRun, Filename, false)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000064 outs() << "\n";
65 outs() << "Optimizer passes caused failure!\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 debugOptimizerCrash();
67 return true;
68 } else {
Dan Gohmanb714fab2009-07-16 15:30:09 +000069 outs() << "Combination " << num << " optimized successfully!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 }
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 //
73 // Step 3: Compile the optimized code.
74 //
Dan Gohmanb714fab2009-07-16 15:30:09 +000075 outs() << "Running the code generator to test for a crash: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 try {
77 compileProgram(Program);
Dan Gohmanb714fab2009-07-16 15:30:09 +000078 outs() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 } catch (ToolExecutionError &TEE) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000080 outs() << "\n*** compileProgram threw an exception: ";
81 outs() << TEE.what();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 return debugCodeGeneratorCrash();
83 }
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 //
86 // Step 4: Run the program and compare its output to the reference
87 // output (created above).
88 //
Dan Gohmanb714fab2009-07-16 15:30:09 +000089 outs() << "*** Checking if passes caused miscompliation:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 try {
91 if (diffProgram(Filename, "", false)) {
Dan Gohmanb714fab2009-07-16 15:30:09 +000092 outs() << "\n*** diffProgram returned true!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 debugMiscompilation();
94 return true;
95 } else {
Dan Gohmanb714fab2009-07-16 15:30:09 +000096 outs() << "\n*** diff'd output matches!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 }
98 } catch (ToolExecutionError &TEE) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +000099 errs() << TEE.what();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 debugCodeGeneratorCrash();
101 return true;
102 }
103
104 sys::Path(Filename).eraseFromDisk();
105
Dan Gohmanb714fab2009-07-16 15:30:09 +0000106 outs() << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 num++;
108 } //end while
109
110 // Unreachable.
111}