blob: fd1f84b7286bdda1822b792ee31dce2cc0ef5a95 [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"
Patrick Jenkins032091d2006-08-15 16:41:52 +000020#include <algorithm>
21#include <ctime>
22using 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///
Chris Lattner7f275702006-08-17 18:49:52 +000031bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) {
Nick Lewycky1d1ef142008-02-14 05:01:46 +000032 setPassesToRun(AllPasses);
Dan Gohmanac95cc72009-07-16 15:30:09 +000033 outs() << "Starting bug finding procedure...\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000034
35 // Creating a reference output if necessary
36 if (initializeExecutionEnvironment()) return false;
Chris Lattner7f275702006-08-17 18:49:52 +000037
Dan Gohmanac95cc72009-07-16 15:30:09 +000038 outs() << "\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000039 if (ReferenceOutputFile.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000040 outs() << "Generating reference output from raw program: \n";
Chris Lattner7f275702006-08-17 18:49:52 +000041 if (!createReferenceFile(Program))
42 return false;
Patrick Jenkins032091d2006-08-15 16:41:52 +000043 }
44
45 srand(time(NULL));
Nick Lewycky1d1ef142008-02-14 05:01:46 +000046
Chris Lattner7f275702006-08-17 18:49:52 +000047 unsigned num = 1;
48 while(1) {
Patrick Jenkins032091d2006-08-15 16:41:52 +000049 //
50 // Step 1: Randomize the order of the optimizer passes.
51 //
Nick Lewycky1d1ef142008-02-14 05:01:46 +000052 std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
Patrick Jenkins032091d2006-08-15 16:41:52 +000053
54 //
55 // Step 2: Run optimizer passes on the program and check for success.
56 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000057 outs() << "Running selected passes on program to test for crash: ";
Nick Lewycky1d1ef142008-02-14 05:01:46 +000058 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000059 outs() << "-" << PassesToRun[i]->getPassArgument( )<< " ";
Patrick Jenkins032091d2006-08-15 16:41:52 +000060 }
Chris Lattner7f275702006-08-17 18:49:52 +000061
Patrick Jenkins032091d2006-08-15 16:41:52 +000062 std::string Filename;
Nick Lewycky1d1ef142008-02-14 05:01:46 +000063 if(runPasses(PassesToRun, Filename, false)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000064 outs() << "\n";
65 outs() << "Optimizer passes caused failure!\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000066 debugOptimizerCrash();
67 return true;
Chris Lattner7f275702006-08-17 18:49:52 +000068 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +000069 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000070 }
Nick Lewycky1d1ef142008-02-14 05:01:46 +000071
Patrick Jenkins032091d2006-08-15 16:41:52 +000072 //
73 // Step 3: Compile the optimized code.
74 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000075 outs() << "Running the code generator to test for a crash: ";
Patrick Jenkins032091d2006-08-15 16:41:52 +000076 try {
77 compileProgram(Program);
Dan Gohmanac95cc72009-07-16 15:30:09 +000078 outs() << '\n';
Patrick Jenkins032091d2006-08-15 16:41:52 +000079 } catch (ToolExecutionError &TEE) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000080 outs() << "\n*** compileProgram threw an exception: ";
81 outs() << TEE.what();
Patrick Jenkins032091d2006-08-15 16:41:52 +000082 return debugCodeGeneratorCrash();
83 }
Nick Lewycky1d1ef142008-02-14 05:01:46 +000084
Patrick Jenkins032091d2006-08-15 16:41:52 +000085 //
86 // Step 4: Run the program and compare its output to the reference
87 // output (created above).
88 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000089 outs() << "*** Checking if passes caused miscompliation:\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000090 try {
91 if (diffProgram(Filename, "", false)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000092 outs() << "\n*** diffProgram returned true!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000093 debugMiscompilation();
94 return true;
Chris Lattner7f275702006-08-17 18:49:52 +000095 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +000096 outs() << "\n*** diff'd output matches!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000097 }
98 } catch (ToolExecutionError &TEE) {
Dan Gohman65f57c22009-07-15 16:35:29 +000099 errs() << TEE.what();
Patrick Jenkins032091d2006-08-15 16:41:52 +0000100 debugCodeGeneratorCrash();
101 return true;
102 }
103
104 sys::Path(Filename).eraseFromDisk();
105
Dan Gohmanac95cc72009-07-16 15:30:09 +0000106 outs() << "\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +0000107 num++;
108 } //end while
109
Chris Lattner7f275702006-08-17 18:49:52 +0000110 // Unreachable.
Patrick Jenkins032091d2006-08-15 16:41:52 +0000111}