2006-08-15 16:41:52 +00:00
|
|
|
//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-15 16:41:52 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines an interface that allows bugpoint to choose different
|
|
|
|
// combinations of optimizations to run on the selected input. Bugpoint will
|
|
|
|
// run these optimizations and record the success/failure of each. This way
|
|
|
|
// we can hopefully spot bugs in the optimizations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-08-17 18:49:52 +00:00
|
|
|
|
2006-08-15 16:41:52 +00:00
|
|
|
#include "BugDriver.h"
|
|
|
|
#include "ToolRunner.h"
|
2006-08-17 18:49:52 +00:00
|
|
|
#include "llvm/Pass.h"
|
2009-08-23 22:45:37 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-08-15 16:41:52 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <ctime>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// runManyPasses - Take the specified pass list and create different
|
|
|
|
/// combinations of passes to compile the program with. Compile the program with
|
|
|
|
/// each set and mark test to see if it compiled correctly. If the passes
|
|
|
|
/// compiled correctly output nothing and rearrange the passes into a new order.
|
|
|
|
/// If the passes did not compile correctly, output the command required to
|
|
|
|
/// recreate the failure. This returns true if a compiler error is found.
|
|
|
|
///
|
2010-07-20 08:26:15 +00:00
|
|
|
bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses,
|
2010-04-12 05:08:25 +00:00
|
|
|
std::string &ErrMsg) {
|
2008-02-14 05:01:46 +00:00
|
|
|
setPassesToRun(AllPasses);
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Starting bug finding procedure...\n\n";
|
2006-08-15 16:41:52 +00:00
|
|
|
|
|
|
|
// Creating a reference output if necessary
|
|
|
|
if (initializeExecutionEnvironment()) return false;
|
2006-08-17 18:49:52 +00:00
|
|
|
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "\n";
|
2006-08-15 16:41:52 +00:00
|
|
|
if (ReferenceOutputFile.empty()) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Generating reference output from raw program: \n";
|
2006-08-17 18:49:52 +00:00
|
|
|
if (!createReferenceFile(Program))
|
|
|
|
return false;
|
2006-08-15 16:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
srand(time(NULL));
|
2008-02-14 05:01:46 +00:00
|
|
|
|
2006-08-17 18:49:52 +00:00
|
|
|
unsigned num = 1;
|
|
|
|
while(1) {
|
2006-08-15 16:41:52 +00:00
|
|
|
//
|
|
|
|
// Step 1: Randomize the order of the optimizer passes.
|
|
|
|
//
|
2008-02-14 05:01:46 +00:00
|
|
|
std::random_shuffle(PassesToRun.begin(), PassesToRun.end());
|
2006-08-15 16:41:52 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Step 2: Run optimizer passes on the program and check for success.
|
|
|
|
//
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Running selected passes on program to test for crash: ";
|
2008-02-14 05:01:46 +00:00
|
|
|
for(int i = 0, e = PassesToRun.size(); i != e; i++) {
|
2010-04-10 23:18:13 +00:00
|
|
|
outs() << "-" << PassesToRun[i]->getPassArgument() << " ";
|
2006-08-15 16:41:52 +00:00
|
|
|
}
|
2006-08-17 18:49:52 +00:00
|
|
|
|
2006-08-15 16:41:52 +00:00
|
|
|
std::string Filename;
|
2008-02-14 05:01:46 +00:00
|
|
|
if(runPasses(PassesToRun, Filename, false)) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "\n";
|
|
|
|
outs() << "Optimizer passes caused failure!\n\n";
|
2006-08-15 16:41:52 +00:00
|
|
|
debugOptimizerCrash();
|
|
|
|
return true;
|
2006-08-17 18:49:52 +00:00
|
|
|
} else {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Combination " << num << " optimized successfully!\n";
|
2006-08-15 16:41:52 +00:00
|
|
|
}
|
2008-02-14 05:01:46 +00:00
|
|
|
|
2006-08-15 16:41:52 +00:00
|
|
|
//
|
|
|
|
// Step 3: Compile the optimized code.
|
|
|
|
//
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "Running the code generator to test for a crash: ";
|
2010-04-12 05:08:25 +00:00
|
|
|
std::string Error;
|
|
|
|
compileProgram(Program, &Error);
|
|
|
|
if (!Error.empty()) {
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "\n*** compileProgram threw an exception: ";
|
2010-04-12 05:08:25 +00:00
|
|
|
outs() << Error;
|
|
|
|
return debugCodeGeneratorCrash(ErrMsg);
|
2006-08-15 16:41:52 +00:00
|
|
|
}
|
2010-04-12 05:08:25 +00:00
|
|
|
outs() << '\n';
|
2008-02-14 05:01:46 +00:00
|
|
|
|
2006-08-15 16:41:52 +00:00
|
|
|
//
|
|
|
|
// Step 4: Run the program and compare its output to the reference
|
|
|
|
// output (created above).
|
|
|
|
//
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "*** Checking if passes caused miscompliation:\n";
|
2010-04-12 05:08:25 +00:00
|
|
|
bool Diff = diffProgram(Filename, "", false, &Error);
|
|
|
|
if (Error.empty() && Diff) {
|
|
|
|
outs() << "\n*** diffProgram returned true!\n";
|
|
|
|
debugMiscompilation(&Error);
|
|
|
|
if (Error.empty())
|
2006-08-15 16:41:52 +00:00
|
|
|
return true;
|
2010-04-12 05:08:25 +00:00
|
|
|
}
|
|
|
|
if (!Error.empty()) {
|
|
|
|
errs() << Error;
|
|
|
|
debugCodeGeneratorCrash(ErrMsg);
|
2006-08-15 16:41:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-04-12 05:08:25 +00:00
|
|
|
outs() << "\n*** diff'd output matches!\n";
|
2006-08-15 16:41:52 +00:00
|
|
|
|
|
|
|
sys::Path(Filename).eraseFromDisk();
|
|
|
|
|
2009-07-16 15:30:09 +00:00
|
|
|
outs() << "\n\n";
|
2006-08-15 16:41:52 +00:00
|
|
|
num++;
|
|
|
|
} //end while
|
|
|
|
|
2006-08-17 18:49:52 +00:00
|
|
|
// Unreachable.
|
2006-08-15 16:41:52 +00:00
|
|
|
}
|