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