Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the bugpoint internals that narrow down compilation crashes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "BugDriver.h" |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 15 | #include "ListReducer.h" |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 16 | #include "llvm/Constant.h" |
| 17 | #include "llvm/iTerminators.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/PassManager.h" |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 21 | #include "llvm/SymbolTable.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 22 | #include "llvm/Type.h" |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Verifier.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 24 | #include "llvm/Bytecode/Writer.h" |
| 25 | #include "llvm/Support/CFG.h" |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/Cloning.h" |
Misha Brukman | 3d9cafa | 2003-08-07 21:42:28 +0000 | [diff] [blame] | 28 | #include "Support/FileUtilities.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 29 | #include <fstream> |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 30 | #include <set> |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame^] | 31 | using namespace llvm; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 32 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | namespace llvm { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame^] | 34 | class DebugCrashes : public ListReducer<const PassInfo*> { |
| 35 | BugDriver &BD; |
| 36 | public: |
| 37 | DebugCrashes(BugDriver &bd) : BD(bd) {} |
| 38 | |
| 39 | // doTest - Return true iff running the "removed" passes succeeds, and |
| 40 | // running the "Kept" passes fail when run on the output of the "removed" |
| 41 | // passes. If we return true, we update the current module of bugpoint. |
| 42 | // |
| 43 | virtual TestResult doTest(std::vector<const PassInfo*> &Removed, |
| 44 | std::vector<const PassInfo*> &Kept); |
| 45 | }; |
| 46 | } |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 47 | |
| 48 | DebugCrashes::TestResult |
| 49 | DebugCrashes::doTest(std::vector<const PassInfo*> &Prefix, |
| 50 | std::vector<const PassInfo*> &Suffix) { |
| 51 | std::string PrefixOutput; |
Chris Lattner | b417c79 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 52 | Module *OrigProgram = 0; |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 53 | if (!Prefix.empty()) { |
| 54 | std::cout << "Checking to see if these passes crash: " |
| 55 | << getPassesString(Prefix) << ": "; |
| 56 | if (BD.runPasses(Prefix, PrefixOutput)) |
| 57 | return KeepPrefix; |
Chris Lattner | b417c79 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 58 | |
| 59 | OrigProgram = BD.Program; |
| 60 | |
| 61 | BD.Program = BD.ParseInputFile(PrefixOutput); |
| 62 | if (BD.Program == 0) { |
| 63 | std::cerr << BD.getToolName() << ": Error reading bytecode file '" |
| 64 | << PrefixOutput << "'!\n"; |
| 65 | exit(1); |
| 66 | } |
| 67 | removeFile(PrefixOutput); |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | std::cout << "Checking to see if these passes crash: " |
| 71 | << getPassesString(Suffix) << ": "; |
Chris Lattner | b417c79 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 72 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 73 | if (BD.runPasses(Suffix)) { |
| 74 | delete OrigProgram; // The suffix crashes alone... |
| 75 | return KeepSuffix; |
| 76 | } |
| 77 | |
| 78 | // Nothing failed, restore state... |
Chris Lattner | b417c79 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 79 | if (OrigProgram) { |
| 80 | delete BD.Program; |
| 81 | BD.Program = OrigProgram; |
| 82 | } |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 83 | return NoFailure; |
| 84 | } |
| 85 | |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame^] | 86 | namespace llvm { |
| 87 | class ReduceCrashingFunctions : public ListReducer<Function*> { |
| 88 | BugDriver &BD; |
| 89 | public: |
| 90 | ReduceCrashingFunctions(BugDriver &bd) : BD(bd) {} |
| 91 | |
| 92 | virtual TestResult doTest(std::vector<Function*> &Prefix, |
| 93 | std::vector<Function*> &Kept) { |
| 94 | if (!Kept.empty() && TestFuncs(Kept)) |
| 95 | return KeepSuffix; |
| 96 | if (!Prefix.empty() && TestFuncs(Prefix)) |
| 97 | return KeepPrefix; |
| 98 | return NoFailure; |
| 99 | } |
| 100 | |
| 101 | bool TestFuncs(std::vector<Function*> &Prefix); |
| 102 | }; |
| 103 | } |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 104 | |
| 105 | bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) { |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 106 | // Clone the program to try hacking it apart... |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 107 | Module *M = CloneModule(BD.Program); |
| 108 | |
| 109 | // Convert list to set for fast lookup... |
| 110 | std::set<Function*> Functions; |
| 111 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 112 | Function *CMF = M->getFunction(Funcs[i]->getName(), |
| 113 | Funcs[i]->getFunctionType()); |
| 114 | assert(CMF && "Function not in module?!"); |
Chris Lattner | f607b79 | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 115 | Functions.insert(CMF); |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::cout << "Checking for crash with only these functions:"; |
Chris Lattner | 73b96bd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 119 | unsigned NumPrint = Funcs.size(); |
| 120 | if (NumPrint > 10) NumPrint = 10; |
| 121 | for (unsigned i = 0; i != NumPrint; ++i) |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 122 | std::cout << " " << Funcs[i]->getName(); |
Chris Lattner | 73b96bd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 123 | if (NumPrint < Funcs.size()) |
| 124 | std::cout << "... <" << Funcs.size() << " total>"; |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 125 | std::cout << ": "; |
| 126 | |
| 127 | // Loop over and delete any functions which we aren't supposed to be playing |
| 128 | // with... |
| 129 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | f607b79 | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 130 | if (!I->isExternal() && !Functions.count(I)) |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 131 | DeleteFunctionBody(I); |
| 132 | |
| 133 | // Try running the hacked up program... |
| 134 | std::swap(BD.Program, M); |
| 135 | if (BD.runPasses(BD.PassesToRun)) { |
| 136 | delete M; // It crashed, keep the trimmed version... |
| 137 | |
| 138 | // Make sure to use function pointers that point into the now-current |
| 139 | // module. |
| 140 | Funcs.assign(Functions.begin(), Functions.end()); |
| 141 | return true; |
| 142 | } |
| 143 | delete BD.Program; // It didn't crash, revert... |
| 144 | BD.Program = M; |
| 145 | return false; |
| 146 | } |
| 147 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 148 | |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame^] | 149 | namespace llvm { |
| 150 | /// ReduceCrashingBlocks reducer - This works by setting the terminators of |
| 151 | /// all terminators except the specified basic blocks to a 'ret' instruction, |
| 152 | /// then running the simplify-cfg pass. This has the effect of chopping up |
| 153 | /// the CFG really fast which can reduce large functions quickly. |
| 154 | /// |
| 155 | class ReduceCrashingBlocks : public ListReducer<BasicBlock*> { |
| 156 | BugDriver &BD; |
| 157 | public: |
| 158 | ReduceCrashingBlocks(BugDriver &bd) : BD(bd) {} |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 159 | |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame^] | 160 | virtual TestResult doTest(std::vector<BasicBlock*> &Prefix, |
| 161 | std::vector<BasicBlock*> &Kept) { |
| 162 | if (!Kept.empty() && TestBlocks(Kept)) |
| 163 | return KeepSuffix; |
| 164 | if (!Prefix.empty() && TestBlocks(Prefix)) |
| 165 | return KeepPrefix; |
| 166 | return NoFailure; |
| 167 | } |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 168 | |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame^] | 169 | bool TestBlocks(std::vector<BasicBlock*> &Prefix); |
| 170 | }; |
| 171 | } |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 172 | |
| 173 | bool ReduceCrashingBlocks::TestBlocks(std::vector<BasicBlock*> &BBs) { |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 174 | // Clone the program to try hacking it apart... |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 175 | Module *M = CloneModule(BD.Program); |
| 176 | |
| 177 | // Convert list to set for fast lookup... |
| 178 | std::set<BasicBlock*> Blocks; |
| 179 | for (unsigned i = 0, e = BBs.size(); i != e; ++i) { |
| 180 | // Convert the basic block from the original module to the new module... |
| 181 | Function *F = BBs[i]->getParent(); |
| 182 | Function *CMF = M->getFunction(F->getName(), F->getFunctionType()); |
| 183 | assert(CMF && "Function not in module?!"); |
| 184 | |
| 185 | // Get the mapped basic block... |
| 186 | Function::iterator CBI = CMF->begin(); |
| 187 | std::advance(CBI, std::distance(F->begin(), Function::iterator(BBs[i]))); |
| 188 | Blocks.insert(CBI); |
| 189 | } |
| 190 | |
| 191 | std::cout << "Checking for crash with only these blocks:"; |
Chris Lattner | 73b96bd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 192 | unsigned NumPrint = Blocks.size(); |
| 193 | if (NumPrint > 10) NumPrint = 10; |
| 194 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 195 | std::cout << " " << BBs[i]->getName(); |
Chris Lattner | 73b96bd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 196 | if (NumPrint < Blocks.size()) |
| 197 | std::cout << "... <" << Blocks.size() << " total>"; |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 198 | std::cout << ": "; |
| 199 | |
| 200 | // Loop over and delete any hack up any blocks that are not listed... |
| 201 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 202 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
Chris Lattner | 8bc098b | 2003-11-22 02:10:38 +0000 | [diff] [blame] | 203 | if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) { |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 204 | // Loop over all of the successors of this block, deleting any PHI nodes |
| 205 | // that might include it. |
| 206 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 207 | (*SI)->removePredecessor(BB); |
| 208 | |
Chris Lattner | 8bc098b | 2003-11-22 02:10:38 +0000 | [diff] [blame] | 209 | if (BB->getTerminator()->getType() != Type::VoidTy) |
| 210 | BB->getTerminator()->replaceAllUsesWith( |
| 211 | Constant::getNullValue(BB->getTerminator()->getType())); |
| 212 | |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 213 | // Delete the old terminator instruction... |
| 214 | BB->getInstList().pop_back(); |
| 215 | |
| 216 | // Add a new return instruction of the appropriate type... |
| 217 | const Type *RetTy = BB->getParent()->getReturnType(); |
Chris Lattner | 8bc098b | 2003-11-22 02:10:38 +0000 | [diff] [blame] | 218 | new ReturnInst(RetTy == Type::VoidTy ? 0 : |
| 219 | Constant::getNullValue(RetTy), BB); |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // The CFG Simplifier pass may delete one of the basic blocks we are |
| 223 | // interested in. If it does we need to take the block out of the list. Make |
| 224 | // a "persistent mapping" by turning basic blocks into <function, name> pairs. |
| 225 | // This won't work well if blocks are unnamed, but that is just the risk we |
| 226 | // have to take. |
| 227 | std::vector<std::pair<Function*, std::string> > BlockInfo; |
| 228 | |
| 229 | for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end(); |
| 230 | I != E; ++I) |
| 231 | BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName())); |
| 232 | |
| 233 | // Now run the CFG simplify pass on the function... |
| 234 | PassManager Passes; |
| 235 | Passes.add(createCFGSimplificationPass()); |
| 236 | Passes.add(createVerifierPass()); |
| 237 | Passes.run(*M); |
| 238 | |
| 239 | // Try running on the hacked up program... |
| 240 | std::swap(BD.Program, M); |
| 241 | if (BD.runPasses(BD.PassesToRun)) { |
| 242 | delete M; // It crashed, keep the trimmed version... |
| 243 | |
| 244 | // Make sure to use basic block pointers that point into the now-current |
| 245 | // module, and that they don't include any deleted blocks. |
| 246 | BBs.clear(); |
| 247 | for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { |
| 248 | SymbolTable &ST = BlockInfo[i].first->getSymbolTable(); |
| 249 | SymbolTable::iterator I = ST.find(Type::LabelTy); |
| 250 | if (I != ST.end() && I->second.count(BlockInfo[i].second)) |
| 251 | BBs.push_back(cast<BasicBlock>(I->second[BlockInfo[i].second])); |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | delete BD.Program; // It didn't crash, revert... |
| 256 | BD.Program = M; |
| 257 | return false; |
| 258 | } |
| 259 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 260 | /// debugCrash - This method is called when some pass crashes on input. It |
| 261 | /// attempts to prune down the testcase to something reasonable, and figure |
| 262 | /// out exactly which pass is crashing. |
| 263 | /// |
| 264 | bool BugDriver::debugCrash() { |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 265 | bool AnyReduction = false; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 266 | std::cout << "\n*** Debugging optimizer crash!\n"; |
| 267 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 268 | // Reduce the list of passes which causes the optimizer to crash... |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 269 | unsigned OldSize = PassesToRun.size(); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 270 | DebugCrashes(*this).reduceList(PassesToRun); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 271 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 272 | std::cout << "\n*** Found crashing pass" |
| 273 | << (PassesToRun.size() == 1 ? ": " : "es: ") |
| 274 | << getPassesString(PassesToRun) << "\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 276 | EmitProgressBytecode("passinput"); |
Chris Lattner | 5f73e38 | 2003-04-25 00:53:05 +0000 | [diff] [blame] | 277 | |
| 278 | // See if we can get away with nuking all of the global variable initializers |
| 279 | // in the program... |
| 280 | if (Program->gbegin() != Program->gend()) { |
| 281 | Module *M = CloneModule(Program); |
| 282 | bool DeletedInit = false; |
| 283 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 284 | if (I->hasInitializer()) { |
| 285 | I->setInitializer(0); |
| 286 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 287 | DeletedInit = true; |
| 288 | } |
| 289 | |
| 290 | if (!DeletedInit) { |
| 291 | delete M; // No change made... |
| 292 | } else { |
| 293 | // See if the program still causes a crash... |
| 294 | std::cout << "\nChecking to see if we can delete global inits: "; |
| 295 | std::swap(Program, M); |
| 296 | if (runPasses(PassesToRun)) { // Still crashes? |
| 297 | AnyReduction = true; |
| 298 | delete M; |
| 299 | std::cout << "\n*** Able to remove all global initializers!\n"; |
| 300 | } else { // No longer crashes? |
| 301 | delete Program; // Restore program. |
| 302 | Program = M; |
| 303 | std::cout << " - Removing all global inits hides problem!\n"; |
| 304 | } |
| 305 | } |
| 306 | } |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 307 | |
| 308 | // Now try to reduce the number of functions in the module to something small. |
| 309 | std::vector<Function*> Functions; |
| 310 | for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I) |
| 311 | if (!I->isExternal()) |
| 312 | Functions.push_back(I); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 313 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 314 | if (Functions.size() > 1) { |
| 315 | std::cout << "\n*** Attempting to reduce the number of functions " |
| 316 | "in the testcase\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 317 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 318 | OldSize = Functions.size(); |
| 319 | ReduceCrashingFunctions(*this).reduceList(Functions); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 320 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 321 | if (Functions.size() < OldSize) { |
| 322 | EmitProgressBytecode("reduced-function"); |
| 323 | AnyReduction = true; |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Chris Lattner | 286921e | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 327 | // Attempt to delete entire basic blocks at a time to speed up |
| 328 | // convergence... this actually works by setting the terminator of the blocks |
| 329 | // to a return instruction then running simplifycfg, which can potentially |
| 330 | // shrinks the code dramatically quickly |
| 331 | // |
Chris Lattner | 47ae4a1 | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 332 | if (!DisableSimplifyCFG) { |
| 333 | std::vector<BasicBlock*> Blocks; |
| 334 | for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I) |
| 335 | for (Function::iterator FI = I->begin(), E = I->end(); FI != E; ++FI) |
| 336 | Blocks.push_back(FI); |
| 337 | ReduceCrashingBlocks(*this).reduceList(Blocks); |
| 338 | } |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 339 | |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 340 | // FIXME: This should use the list reducer to converge faster by deleting |
| 341 | // larger chunks of instructions at a time! |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 342 | unsigned Simplification = 4; |
| 343 | do { |
| 344 | --Simplification; |
| 345 | std::cout << "\n*** Attempting to reduce testcase by deleting instruc" |
| 346 | << "tions: Simplification Level #" << Simplification << "\n"; |
| 347 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 348 | // Now that we have deleted the functions that are unnecessary for the |
| 349 | // program, try to remove instructions that are not necessary to cause the |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 350 | // crash. To do this, we loop through all of the instructions in the |
| 351 | // remaining functions, deleting them (replacing any values produced with |
| 352 | // nulls), and then running ADCE and SimplifyCFG. If the transformed input |
| 353 | // still triggers failure, keep deleting until we cannot trigger failure |
| 354 | // anymore. |
| 355 | // |
| 356 | TryAgain: |
| 357 | |
| 358 | // Loop over all of the (non-terminator) instructions remaining in the |
| 359 | // function, attempting to delete them. |
| 360 | for (Module::iterator FI = Program->begin(), E = Program->end(); |
| 361 | FI != E; ++FI) |
| 362 | if (!FI->isExternal()) { |
| 363 | for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI) |
| 364 | for (BasicBlock::iterator I = BI->begin(), E = --BI->end(); |
| 365 | I != E; ++I) { |
| 366 | Module *M = deleteInstructionFromProgram(I, Simplification); |
| 367 | |
| 368 | // Make the function the current program... |
| 369 | std::swap(Program, M); |
| 370 | |
| 371 | // Find out if the pass still crashes on this pass... |
| 372 | std::cout << "Checking instruction '" << I->getName() << "': "; |
Chris Lattner | aae33f9 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 373 | if (runPasses(PassesToRun)) { |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 374 | // Yup, it does, we delete the old module, and continue trying to |
| 375 | // reduce the testcase... |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 376 | delete M; |
Chris Lattner | f607b79 | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 377 | AnyReduction = true; |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 378 | goto TryAgain; // I wish I had a multi-level break here! |
| 379 | } |
| 380 | |
| 381 | // This pass didn't crash without this instruction, try the next |
| 382 | // one. |
| 383 | delete Program; |
| 384 | Program = M; |
| 385 | } |
| 386 | } |
| 387 | } while (Simplification); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 388 | |
| 389 | // Try to clean up the testcase by running funcresolve and globaldce... |
Chris Lattner | 898e0e4 | 2003-06-25 04:13:52 +0000 | [diff] [blame] | 390 | std::cout << "\n*** Attempting to perform final cleanups: "; |
Chris Lattner | 417477d | 2003-11-05 21:15:19 +0000 | [diff] [blame] | 391 | Module *M = CloneModule(Program); |
Chris Lattner | fcb6ec0 | 2003-11-05 21:45:35 +0000 | [diff] [blame] | 392 | M = performFinalCleanups(M, true); |
Chris Lattner | 898e0e4 | 2003-06-25 04:13:52 +0000 | [diff] [blame] | 393 | std::swap(Program, M); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 898e0e4 | 2003-06-25 04:13:52 +0000 | [diff] [blame] | 395 | // Find out if the pass still crashes on the cleaned up program... |
| 396 | if (runPasses(PassesToRun)) { |
| 397 | // Yup, it does, keep the reduced version... |
| 398 | delete M; |
| 399 | AnyReduction = true; |
| 400 | } else { |
| 401 | delete Program; // Otherwise, restore the original module... |
| 402 | Program = M; |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | f607b79 | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 405 | if (AnyReduction) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 406 | EmitProgressBytecode("reduced-simplified"); |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 407 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 408 | return false; |
| 409 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 410 | |