blob: a291f9fb0f99b6e9d78f6b2cef8afc1c902a4d6c [file] [log] [blame]
Patrick Jenkins032091d2006-08-15 16:41:52 +00001//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Jenkins032091d2006-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 Lattner7f275702006-08-17 18:49:52 +000016
Patrick Jenkins032091d2006-08-15 16:41:52 +000017#include "BugDriver.h"
18#include "ToolRunner.h"
Chris Lattner7f275702006-08-17 18:49:52 +000019#include "llvm/Pass.h"
Chris Lattner74382b72009-08-23 22:45:37 +000020#include "llvm/Support/raw_ostream.h"
Patrick Jenkins032091d2006-08-15 16:41:52 +000021#include <algorithm>
22#include <ctime>
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///
Rafael Espindola8261dfe2010-08-08 03:55:08 +000032bool BugDriver::runManyPasses(const std::vector<std::string> &AllPasses,
Nick Lewycky22ff7482010-04-12 05:08:25 +000033 std::string &ErrMsg) {
Nick Lewycky1d1ef142008-02-14 05:01:46 +000034 setPassesToRun(AllPasses);
Dan Gohmanac95cc72009-07-16 15:30:09 +000035 outs() << "Starting bug finding procedure...\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000036
37 // Creating a reference output if necessary
38 if (initializeExecutionEnvironment()) return false;
Chris Lattner7f275702006-08-17 18:49:52 +000039
Dan Gohmanac95cc72009-07-16 15:30:09 +000040 outs() << "\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000041 if (ReferenceOutputFile.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000042 outs() << "Generating reference output from raw program: \n";
Chris Lattner7f275702006-08-17 18:49:52 +000043 if (!createReferenceFile(Program))
44 return false;
Patrick Jenkins032091d2006-08-15 16:41:52 +000045 }
46
47 srand(time(NULL));
Nick Lewycky1d1ef142008-02-14 05:01:46 +000048
Chris Lattner7f275702006-08-17 18:49:52 +000049 unsigned num = 1;
50 while(1) {
Patrick Jenkins032091d2006-08-15 16:41:52 +000051 //
52 // Step 1: Randomize the order of the optimizer passes.
53 //
Nick Lewycky1d1ef142008-02-14 05:01:46 +000054 std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
Patrick Jenkins032091d2006-08-15 16:41:52 +000055
56 //
57 // Step 2: Run optimizer passes on the program and check for success.
58 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000059 outs() << "Running selected passes on program to test for crash: ";
Nick Lewycky1d1ef142008-02-14 05:01:46 +000060 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
Rafael Espindola8261dfe2010-08-08 03:55:08 +000061 outs() << "-" << PassesToRun[i] << " ";
Patrick Jenkins032091d2006-08-15 16:41:52 +000062 }
Chris Lattner7f275702006-08-17 18:49:52 +000063
Patrick Jenkins032091d2006-08-15 16:41:52 +000064 std::string Filename;
Rafael Espindolaca356af2010-08-05 00:29:04 +000065 if(runPasses(Program, PassesToRun, Filename, false)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000066 outs() << "\n";
67 outs() << "Optimizer passes caused failure!\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000068 debugOptimizerCrash();
69 return true;
Chris Lattner7f275702006-08-17 18:49:52 +000070 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +000071 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000072 }
Nick Lewycky1d1ef142008-02-14 05:01:46 +000073
Patrick Jenkins032091d2006-08-15 16:41:52 +000074 //
75 // Step 3: Compile the optimized code.
76 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000077 outs() << "Running the code generator to test for a crash: ";
Nick Lewycky22ff7482010-04-12 05:08:25 +000078 std::string Error;
79 compileProgram(Program, &Error);
80 if (!Error.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000081 outs() << "\n*** compileProgram threw an exception: ";
Nick Lewycky22ff7482010-04-12 05:08:25 +000082 outs() << Error;
83 return debugCodeGeneratorCrash(ErrMsg);
Patrick Jenkins032091d2006-08-15 16:41:52 +000084 }
Nick Lewycky22ff7482010-04-12 05:08:25 +000085 outs() << '\n';
Nick Lewycky1d1ef142008-02-14 05:01:46 +000086
Patrick Jenkins032091d2006-08-15 16:41:52 +000087 //
88 // Step 4: Run the program and compare its output to the reference
89 // output (created above).
90 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000091 outs() << "*** Checking if passes caused miscompliation:\n";
Rafael Espindola10757dd2010-07-30 14:19:00 +000092 bool Diff = diffProgram(Program, Filename, "", false, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +000093 if (Error.empty() && Diff) {
94 outs() << "\n*** diffProgram returned true!\n";
95 debugMiscompilation(&Error);
96 if (Error.empty())
Patrick Jenkins032091d2006-08-15 16:41:52 +000097 return true;
Nick Lewycky22ff7482010-04-12 05:08:25 +000098 }
99 if (!Error.empty()) {
100 errs() << Error;
101 debugCodeGeneratorCrash(ErrMsg);
Patrick Jenkins032091d2006-08-15 16:41:52 +0000102 return true;
103 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000104 outs() << "\n*** diff'd output matches!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +0000105
106 sys::Path(Filename).eraseFromDisk();
107
Dan Gohmanac95cc72009-07-16 15:30:09 +0000108 outs() << "\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +0000109 num++;
110 } //end while
111
Chris Lattner7f275702006-08-17 18:49:52 +0000112 // Unreachable.
Patrick Jenkins032091d2006-08-15 16:41:52 +0000113}