blob: 2c11d29f60d5696480a178e9b057db68e71a00a3 [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///
Chris Lattner7f275702006-08-17 18:49:52 +000032bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) {
Nick Lewycky1d1ef142008-02-14 05:01:46 +000033 setPassesToRun(AllPasses);
Dan Gohmanac95cc72009-07-16 15:30:09 +000034 outs() << "Starting bug finding procedure...\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000035
36 // Creating a reference output if necessary
37 if (initializeExecutionEnvironment()) return false;
Chris Lattner7f275702006-08-17 18:49:52 +000038
Dan Gohmanac95cc72009-07-16 15:30:09 +000039 outs() << "\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000040 if (ReferenceOutputFile.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000041 outs() << "Generating reference output from raw program: \n";
Chris Lattner7f275702006-08-17 18:49:52 +000042 if (!createReferenceFile(Program))
43 return false;
Patrick Jenkins032091d2006-08-15 16:41:52 +000044 }
45
46 srand(time(NULL));
Nick Lewycky1d1ef142008-02-14 05:01:46 +000047
Chris Lattner7f275702006-08-17 18:49:52 +000048 unsigned num = 1;
49 while(1) {
Patrick Jenkins032091d2006-08-15 16:41:52 +000050 //
51 // Step 1: Randomize the order of the optimizer passes.
52 //
Nick Lewycky1d1ef142008-02-14 05:01:46 +000053 std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
Patrick Jenkins032091d2006-08-15 16:41:52 +000054
55 //
56 // Step 2: Run optimizer passes on the program and check for success.
57 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000058 outs() << "Running selected passes on program to test for crash: ";
Nick Lewycky1d1ef142008-02-14 05:01:46 +000059 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000060 outs() << "-" << PassesToRun[i]->getPassArgument( )<< " ";
Patrick Jenkins032091d2006-08-15 16:41:52 +000061 }
Chris Lattner7f275702006-08-17 18:49:52 +000062
Patrick Jenkins032091d2006-08-15 16:41:52 +000063 std::string Filename;
Nick Lewycky1d1ef142008-02-14 05:01:46 +000064 if(runPasses(PassesToRun, Filename, false)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000065 outs() << "\n";
66 outs() << "Optimizer passes caused failure!\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000067 debugOptimizerCrash();
68 return true;
Chris Lattner7f275702006-08-17 18:49:52 +000069 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +000070 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000071 }
Nick Lewycky1d1ef142008-02-14 05:01:46 +000072
Patrick Jenkins032091d2006-08-15 16:41:52 +000073 //
74 // Step 3: Compile the optimized code.
75 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000076 outs() << "Running the code generator to test for a crash: ";
Patrick Jenkins032091d2006-08-15 16:41:52 +000077 try {
78 compileProgram(Program);
Dan Gohmanac95cc72009-07-16 15:30:09 +000079 outs() << '\n';
Patrick Jenkins032091d2006-08-15 16:41:52 +000080 } catch (ToolExecutionError &TEE) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000081 outs() << "\n*** compileProgram threw an exception: ";
82 outs() << TEE.what();
Patrick Jenkins032091d2006-08-15 16:41:52 +000083 return debugCodeGeneratorCrash();
84 }
Nick Lewycky1d1ef142008-02-14 05:01:46 +000085
Patrick Jenkins032091d2006-08-15 16:41:52 +000086 //
87 // Step 4: Run the program and compare its output to the reference
88 // output (created above).
89 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000090 outs() << "*** Checking if passes caused miscompliation:\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000091 try {
92 if (diffProgram(Filename, "", false)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000093 outs() << "\n*** diffProgram returned true!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000094 debugMiscompilation();
95 return true;
Chris Lattner7f275702006-08-17 18:49:52 +000096 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +000097 outs() << "\n*** diff'd output matches!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000098 }
99 } catch (ToolExecutionError &TEE) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000100 errs() << TEE.what();
Patrick Jenkins032091d2006-08-15 16:41:52 +0000101 debugCodeGeneratorCrash();
102 return true;
103 }
104
105 sys::Path(Filename).eraseFromDisk();
106
Dan Gohmanac95cc72009-07-16 15:30:09 +0000107 outs() << "\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +0000108 num++;
109 } //end while
110
Chris Lattner7f275702006-08-17 18:49:52 +0000111 // Unreachable.
Patrick Jenkins032091d2006-08-15 16:41:52 +0000112}