blob: 3093169ba8b0010b637cbc39e02e8c6b075130f0 [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//
Justin Bogner8d0a0812016-09-02 01:21:37 +000010// This file defines an interface that allows bugpoint to choose different
11// combinations of optimizations to run on the selected input. Bugpoint will
Patrick Jenkins38a197a2006-08-15 16:41:52 +000012// 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>
Marshall Clowe9110d72017-02-16 14:37:03 +000024#include <random>
Patrick Jenkins38a197a2006-08-15 16:41:52 +000025using namespace llvm;
26
Justin Bogner1c039152016-09-06 17:18:22 +000027Error
28BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
Nick Lewycky8102d472008-02-14 05:01:46 +000029 setPassesToRun(AllPasses);
Dan Gohmanee051522009-07-16 15:30:09 +000030 outs() << "Starting bug finding procedure...\n\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +000031
Patrick Jenkins38a197a2006-08-15 16:41:52 +000032 // Creating a reference output if necessary
Justin Bogner1c039152016-09-06 17:18:22 +000033 if (Error E = initializeExecutionEnvironment())
34 return E;
Justin Bogner8d0a0812016-09-02 01:21:37 +000035
Dan Gohmanee051522009-07-16 15:30:09 +000036 outs() << "\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000037 if (ReferenceOutputFile.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +000038 outs() << "Generating reference output from raw program: \n";
Justin Bogner1c039152016-09-06 17:18:22 +000039 if (Error E = createReferenceFile(Program))
40 return E;
Patrick Jenkins38a197a2006-08-15 16:41:52 +000041 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000042
Marshall Clowe9110d72017-02-16 14:37:03 +000043 std::mt19937 randomness(std::random_device{}());
Chris Lattner6e81c962006-08-17 18:49:52 +000044 unsigned num = 1;
Justin Bogner8d0a0812016-09-02 01:21:37 +000045 while (1) {
Patrick Jenkins38a197a2006-08-15 16:41:52 +000046 //
47 // Step 1: Randomize the order of the optimizer passes.
48 //
Marshall Clowe9110d72017-02-16 14:37:03 +000049 std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
Justin Bogner8d0a0812016-09-02 01:21:37 +000050
Patrick Jenkins38a197a2006-08-15 16:41:52 +000051 //
52 // Step 2: Run optimizer passes on the program and check for success.
53 //
Dan Gohmanee051522009-07-16 15:30:09 +000054 outs() << "Running selected passes on program to test for crash: ";
Justin Bogner8d0a0812016-09-02 01:21:37 +000055 for (int i = 0, e = PassesToRun.size(); i != e; i++) {
Rafael Espindola33e81a82010-08-08 03:55:08 +000056 outs() << "-" << PassesToRun[i] << " ";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000057 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000058
Patrick Jenkins38a197a2006-08-15 16:41:52 +000059 std::string Filename;
Justin Bogner8d0a0812016-09-02 01:21:37 +000060 if (runPasses(Program, PassesToRun, Filename, false)) {
Dan Gohmanee051522009-07-16 15:30:09 +000061 outs() << "\n";
62 outs() << "Optimizer passes caused failure!\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +000063 return debugOptimizerCrash();
Chris Lattner6e81c962006-08-17 18:49:52 +000064 } else {
Dan Gohmanee051522009-07-16 15:30:09 +000065 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000066 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000067
Patrick Jenkins38a197a2006-08-15 16:41:52 +000068 //
69 // Step 3: Compile the optimized code.
70 //
Dan Gohmanee051522009-07-16 15:30:09 +000071 outs() << "Running the code generator to test for a crash: ";
Justin Bogner1c039152016-09-06 17:18:22 +000072 if (Error E = compileProgram(Program)) {
Dan Gohmanee051522009-07-16 15:30:09 +000073 outs() << "\n*** compileProgram threw an exception: ";
Justin Bogner1c039152016-09-06 17:18:22 +000074 outs() << toString(std::move(E));
75 return debugCodeGeneratorCrash();
Patrick Jenkins38a197a2006-08-15 16:41:52 +000076 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000077 outs() << '\n';
Justin Bogner8d0a0812016-09-02 01:21:37 +000078
Patrick Jenkins38a197a2006-08-15 16:41:52 +000079 //
Justin Bogner8d0a0812016-09-02 01:21:37 +000080 // Step 4: Run the program and compare its output to the reference
Patrick Jenkins38a197a2006-08-15 16:41:52 +000081 // output (created above).
82 //
Dan Gohmanee051522009-07-16 15:30:09 +000083 outs() << "*** Checking if passes caused miscompliation:\n";
Justin Bogner1c039152016-09-06 17:18:22 +000084 Expected<bool> Diff = diffProgram(Program, Filename, "", false);
85 if (Error E = Diff.takeError()) {
86 errs() << toString(std::move(E));
87 return debugCodeGeneratorCrash();
Nick Lewycky6ba630b2010-04-12 05:08:25 +000088 }
Justin Bogner1c039152016-09-06 17:18:22 +000089 if (*Diff) {
90 outs() << "\n*** diffProgram returned true!\n";
91 Error E = debugMiscompilation();
92 if (!E)
93 return Error::success();
Patrick Jenkins38a197a2006-08-15 16:41:52 +000094 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000095 outs() << "\n*** diff'd output matches!\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +000096
Rafael Espindola6ce17a42013-06-17 20:48:36 +000097 sys::fs::remove(Filename);
Justin Bogner8d0a0812016-09-02 01:21:37 +000098
Dan Gohmanee051522009-07-16 15:30:09 +000099 outs() << "\n\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000100 num++;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000101 } // end while
102
Chris Lattner6e81c962006-08-17 18:49:52 +0000103 // Unreachable.
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000104}