blob: e42cce47ba0206652193f4627482461e2b78c7c6 [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>
22#include <iostream>
23using namespace llvm;
24
25/// runManyPasses - Take the specified pass list and create different
26/// combinations of passes to compile the program with. Compile the program with
27/// each set and mark test to see if it compiled correctly. If the passes
28/// compiled correctly output nothing and rearrange the passes into a new order.
29/// If the passes did not compile correctly, output the command required to
30/// recreate the failure. This returns true if a compiler error is found.
31///
32bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) {
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000033 setPassesToRun(AllPasses);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 std::cout << "Starting bug finding procedure...\n\n";
35
36 // Creating a reference output if necessary
37 if (initializeExecutionEnvironment()) return false;
38
39 std::cout << "\n";
40 if (ReferenceOutputFile.empty()) {
41 std::cout << "Generating reference output from raw program: \n";
42 if (!createReferenceFile(Program))
43 return false;
44 }
45
46 srand(time(NULL));
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000047
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 unsigned num = 1;
49 while(1) {
50 //
51 // Step 1: Randomize the order of the optimizer passes.
52 //
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000053 std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 //
56 // Step 2: Run optimizer passes on the program and check for success.
57 //
58 std::cout << "Running selected passes on program to test for crash: ";
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000059 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
60 std::cout << "-" << PassesToRun[i]->getPassArgument( )<< " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 }
62
63 std::string Filename;
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000064 if(runPasses(PassesToRun, Filename, false)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 std::cout << "\n";
66 std::cout << "Optimizer passes caused failure!\n\n";
67 debugOptimizerCrash();
68 return true;
69 } else {
70 std::cout << "Combination " << num << " optimized successfully!\n";
71 }
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000072
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 //
74 // Step 3: Compile the optimized code.
75 //
76 std::cout << "Running the code generator to test for a crash: ";
77 try {
78 compileProgram(Program);
79 std::cout << '\n';
80 } catch (ToolExecutionError &TEE) {
81 std::cout << "\n*** compileProgram threw an exception: ";
82 std::cout << TEE.what();
83 return debugCodeGeneratorCrash();
84 }
Nick Lewyckybcfb72a2008-02-14 05:01:46 +000085
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 //
87 // Step 4: Run the program and compare its output to the reference
88 // output (created above).
89 //
90 std::cout << "*** Checking if passes caused miscompliation:\n";
91 try {
92 if (diffProgram(Filename, "", false)) {
93 std::cout << "\n*** diffProgram returned true!\n";
94 debugMiscompilation();
95 return true;
96 } else {
97 std::cout << "\n*** diff'd output matches!\n";
98 }
99 } catch (ToolExecutionError &TEE) {
100 std::cerr << TEE.what();
101 debugCodeGeneratorCrash();
102 return true;
103 }
104
105 sys::Path(Filename).eraseFromDisk();
106
107 std::cout << "\n\n";
108 num++;
109 } //end while
110
111 // Unreachable.
112}