blob: e2941f6f43aea81251af67d2dfe3607bba2b0032 [file] [log] [blame]
Patrick Jenkins38a197a2006-08-15 16:41:52 +00001//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Patrick Jenkins38a197a2006-08-15 16:41:52 +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//===----------------------------------------------------------------------===//
Chris Lattner6e81c962006-08-17 18:49:52 +000016
Patrick Jenkins38a197a2006-08-15 16:41:52 +000017#include "BugDriver.h"
18#include "ToolRunner.h"
Chris Lattner6e81c962006-08-17 18:49:52 +000019#include "llvm/Pass.h"
Rafael Espindola6ce17a42013-06-17 20:48:36 +000020#include "llvm/Support/FileSystem.h"
Chris Lattnerc521f542009-08-23 22:45:37 +000021#include "llvm/Support/raw_ostream.h"
Patrick Jenkins38a197a2006-08-15 16:41:52 +000022#include <algorithm>
23#include <ctime>
24using namespace llvm;
25
26/// runManyPasses - Take the specified pass list and create different
27/// combinations of passes to compile the program with. Compile the program with
28/// each set and mark test to see if it compiled correctly. If the passes
29/// compiled correctly output nothing and rearrange the passes into a new order.
30/// If the passes did not compile correctly, output the command required to
31/// recreate the failure. This returns true if a compiler error is found.
32///
Rafael Espindola33e81a82010-08-08 03:55:08 +000033bool BugDriver::runManyPasses(const std::vector<std::string> &AllPasses,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000034 std::string &ErrMsg) {
Nick Lewycky8102d472008-02-14 05:01:46 +000035 setPassesToRun(AllPasses);
Dan Gohmanee051522009-07-16 15:30:09 +000036 outs() << "Starting bug finding procedure...\n\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000037
38 // Creating a reference output if necessary
39 if (initializeExecutionEnvironment()) return false;
Chris Lattner6e81c962006-08-17 18:49:52 +000040
Dan Gohmanee051522009-07-16 15:30:09 +000041 outs() << "\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000042 if (ReferenceOutputFile.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +000043 outs() << "Generating reference output from raw program: \n";
Chris Lattner6e81c962006-08-17 18:49:52 +000044 if (!createReferenceFile(Program))
45 return false;
Patrick Jenkins38a197a2006-08-15 16:41:52 +000046 }
47
48 srand(time(NULL));
Nick Lewycky8102d472008-02-14 05:01:46 +000049
Chris Lattner6e81c962006-08-17 18:49:52 +000050 unsigned num = 1;
51 while(1) {
Patrick Jenkins38a197a2006-08-15 16:41:52 +000052 //
53 // Step 1: Randomize the order of the optimizer passes.
54 //
Nick Lewycky8102d472008-02-14 05:01:46 +000055 std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
Patrick Jenkins38a197a2006-08-15 16:41:52 +000056
57 //
58 // Step 2: Run optimizer passes on the program and check for success.
59 //
Dan Gohmanee051522009-07-16 15:30:09 +000060 outs() << "Running selected passes on program to test for crash: ";
Nick Lewycky8102d472008-02-14 05:01:46 +000061 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
Rafael Espindola33e81a82010-08-08 03:55:08 +000062 outs() << "-" << PassesToRun[i] << " ";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000063 }
Chris Lattner6e81c962006-08-17 18:49:52 +000064
Patrick Jenkins38a197a2006-08-15 16:41:52 +000065 std::string Filename;
Rafael Espindola315190b2010-08-05 00:29:04 +000066 if(runPasses(Program, PassesToRun, Filename, false)) {
Dan Gohmanee051522009-07-16 15:30:09 +000067 outs() << "\n";
68 outs() << "Optimizer passes caused failure!\n\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000069 debugOptimizerCrash();
70 return true;
Chris Lattner6e81c962006-08-17 18:49:52 +000071 } else {
Dan Gohmanee051522009-07-16 15:30:09 +000072 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000073 }
Nick Lewycky8102d472008-02-14 05:01:46 +000074
Patrick Jenkins38a197a2006-08-15 16:41:52 +000075 //
76 // Step 3: Compile the optimized code.
77 //
Dan Gohmanee051522009-07-16 15:30:09 +000078 outs() << "Running the code generator to test for a crash: ";
Nick Lewycky6ba630b2010-04-12 05:08:25 +000079 std::string Error;
80 compileProgram(Program, &Error);
81 if (!Error.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +000082 outs() << "\n*** compileProgram threw an exception: ";
Nick Lewycky6ba630b2010-04-12 05:08:25 +000083 outs() << Error;
84 return debugCodeGeneratorCrash(ErrMsg);
Patrick Jenkins38a197a2006-08-15 16:41:52 +000085 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000086 outs() << '\n';
Nick Lewycky8102d472008-02-14 05:01:46 +000087
Patrick Jenkins38a197a2006-08-15 16:41:52 +000088 //
89 // Step 4: Run the program and compare its output to the reference
90 // output (created above).
91 //
Dan Gohmanee051522009-07-16 15:30:09 +000092 outs() << "*** Checking if passes caused miscompliation:\n";
Rafael Espindolac89b1ef2010-07-30 14:19:00 +000093 bool Diff = diffProgram(Program, Filename, "", false, &Error);
Nick Lewycky6ba630b2010-04-12 05:08:25 +000094 if (Error.empty() && Diff) {
95 outs() << "\n*** diffProgram returned true!\n";
96 debugMiscompilation(&Error);
97 if (Error.empty())
Patrick Jenkins38a197a2006-08-15 16:41:52 +000098 return true;
Nick Lewycky6ba630b2010-04-12 05:08:25 +000099 }
100 if (!Error.empty()) {
101 errs() << Error;
102 debugCodeGeneratorCrash(ErrMsg);
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000103 return true;
104 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000105 outs() << "\n*** diff'd output matches!\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000106
Rafael Espindola6ce17a42013-06-17 20:48:36 +0000107 sys::fs::remove(Filename);
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000108
Dan Gohmanee051522009-07-16 15:30:09 +0000109 outs() << "\n\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000110 num++;
111 } //end while
112
Chris Lattner6e81c962006-08-17 18:49:52 +0000113 // Unreachable.
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000114}