Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 1 | //===-- FindBugs.cpp - Run Many Different Optimizations -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 7 | // |
| 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 Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 16 | |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 17 | #include "BugDriver.h" |
| 18 | #include "ToolRunner.h" |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <ctime> |
| 23 | using 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 | /// |
Owen Anderson | 9e08100 | 2010-07-20 06:52:42 +0000 | [diff] [blame] | 32 | bool |
| 33 | BugDriver::runManyPasses(const std::vector<const StaticPassInfo*> &AllPasses, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 34 | std::string &ErrMsg) { |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 35 | setPassesToRun(AllPasses); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 36 | outs() << "Starting bug finding procedure...\n\n"; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 37 | |
| 38 | // Creating a reference output if necessary |
| 39 | if (initializeExecutionEnvironment()) return false; |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 40 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 41 | outs() << "\n"; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 42 | if (ReferenceOutputFile.empty()) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 43 | outs() << "Generating reference output from raw program: \n"; |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 44 | if (!createReferenceFile(Program)) |
| 45 | return false; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | srand(time(NULL)); |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 50 | unsigned num = 1; |
| 51 | while(1) { |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 52 | // |
| 53 | // Step 1: Randomize the order of the optimizer passes. |
| 54 | // |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 55 | std::random_shuffle(PassesToRun.begin(), PassesToRun.end()); |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 56 | |
| 57 | // |
| 58 | // Step 2: Run optimizer passes on the program and check for success. |
| 59 | // |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 60 | outs() << "Running selected passes on program to test for crash: "; |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 61 | for(int i = 0, e = PassesToRun.size(); i != e; i++) { |
Nick Lewycky | 16350f8 | 2010-04-10 23:18:13 +0000 | [diff] [blame] | 62 | outs() << "-" << PassesToRun[i]->getPassArgument() << " "; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 64 | |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 65 | std::string Filename; |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 66 | if(runPasses(PassesToRun, Filename, false)) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 67 | outs() << "\n"; |
| 68 | outs() << "Optimizer passes caused failure!\n\n"; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 69 | debugOptimizerCrash(); |
| 70 | return true; |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 71 | } else { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 72 | outs() << "Combination " << num << " optimized successfully!\n"; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 73 | } |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 74 | |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 75 | // |
| 76 | // Step 3: Compile the optimized code. |
| 77 | // |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 78 | outs() << "Running the code generator to test for a crash: "; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 79 | std::string Error; |
| 80 | compileProgram(Program, &Error); |
| 81 | if (!Error.empty()) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 82 | outs() << "\n*** compileProgram threw an exception: "; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 83 | outs() << Error; |
| 84 | return debugCodeGeneratorCrash(ErrMsg); |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 85 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 86 | outs() << '\n'; |
Nick Lewycky | 1d1ef14 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 87 | |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 88 | // |
| 89 | // Step 4: Run the program and compare its output to the reference |
| 90 | // output (created above). |
| 91 | // |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 92 | outs() << "*** Checking if passes caused miscompliation:\n"; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 93 | bool Diff = diffProgram(Filename, "", false, &Error); |
| 94 | if (Error.empty() && Diff) { |
| 95 | outs() << "\n*** diffProgram returned true!\n"; |
| 96 | debugMiscompilation(&Error); |
| 97 | if (Error.empty()) |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 98 | return true; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 99 | } |
| 100 | if (!Error.empty()) { |
| 101 | errs() << Error; |
| 102 | debugCodeGeneratorCrash(ErrMsg); |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 105 | outs() << "\n*** diff'd output matches!\n"; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 106 | |
| 107 | sys::Path(Filename).eraseFromDisk(); |
| 108 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 109 | outs() << "\n\n"; |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 110 | num++; |
| 111 | } //end while |
| 112 | |
Chris Lattner | 7f27570 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 113 | // Unreachable. |
Patrick Jenkins | 032091d | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 114 | } |