blob: 6f41d3030d0032f5d652b96110507de964cdf37c [file] [log] [blame]
Chris Lattner73a6bdd2002-11-20 22:28:10 +00001//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// 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.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner73a6bdd2002-11-20 22:28:10 +00009//
10// This file defines the bugpoint internals that narrow down compilation crashes
11//
12//===----------------------------------------------------------------------===//
13
14#include "BugDriver.h"
Chris Lattner1d080f22003-04-24 22:24:58 +000015#include "ListReducer.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "ToolRunner.h"
17#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000018#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Instructions.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/ValueSymbolTable.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000025#include "llvm/IR/Verifier.h"
Misha Brukman0c2305b2003-08-07 21:19:30 +000026#include "llvm/Pass.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/FileUtilities.h"
Chris Lattnerf32939b2003-04-24 23:51:38 +000029#include "llvm/Transforms/Scalar.h"
Chris Lattner1d080f22003-04-24 22:24:58 +000030#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner1d080f22003-04-24 22:24:58 +000031#include <set>
Chris Lattner2f1aa112004-01-14 03:38:37 +000032using namespace llvm;
Chris Lattner73a6bdd2002-11-20 22:28:10 +000033
Andrew Lenharthef9aa122006-03-05 22:21:36 +000034namespace {
35 cl::opt<bool>
36 KeepMain("keep-main",
37 cl::desc("Force function reduction to keep main"),
38 cl::init(false));
Torok Edwin5bd3f7b2009-05-24 09:31:04 +000039 cl::opt<bool>
40 NoGlobalRM ("disable-global-remove",
41 cl::desc("Do not remove global variables"),
42 cl::init(false));
JF Bastienf87e20d2015-04-20 23:42:22 +000043
44 cl::opt<bool>
45 ReplaceFuncsWithNull("replace-funcs-with-null",
46 cl::desc("When stubbing functions, replace all uses will null"),
47 cl::init(false));
48 cl::opt<bool>
49 DontReducePassList("disable-pass-list-reduction",
50 cl::desc("Skip pass list reduction steps"),
51 cl::init(false));
Andrew Lenharthef9aa122006-03-05 22:21:36 +000052}
53
Brian Gaeke960707c2003-11-11 22:41:34 +000054namespace llvm {
Rafael Espindola33e81a82010-08-08 03:55:08 +000055 class ReducePassList : public ListReducer<std::string> {
Chris Lattner2f1aa112004-01-14 03:38:37 +000056 BugDriver &BD;
57 public:
Chris Lattner327019b2004-02-18 21:24:48 +000058 ReducePassList(BugDriver &bd) : BD(bd) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +000059
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000060 // doTest - Return true iff running the "removed" passes succeeds, and
Chris Lattner2f1aa112004-01-14 03:38:37 +000061 // running the "Kept" passes fail when run on the output of the "removed"
62 // passes. If we return true, we update the current module of bugpoint.
63 //
Craig Toppere56917c2014-03-08 08:27:28 +000064 TestResult doTest(std::vector<std::string> &Removed,
65 std::vector<std::string> &Kept,
66 std::string &Error) override;
Chris Lattner2f1aa112004-01-14 03:38:37 +000067 };
68}
Chris Lattner1d080f22003-04-24 22:24:58 +000069
Chris Lattner327019b2004-02-18 21:24:48 +000070ReducePassList::TestResult
Rafael Espindola33e81a82010-08-08 03:55:08 +000071ReducePassList::doTest(std::vector<std::string> &Prefix,
72 std::vector<std::string> &Suffix,
Nick Lewycky6ba630b2010-04-12 05:08:25 +000073 std::string &Error) {
Rafael Espindolabb876652013-06-17 19:33:18 +000074 std::string PrefixOutput;
Craig Toppere6cb63e2014-04-25 04:24:47 +000075 Module *OrigProgram = nullptr;
Chris Lattner1d080f22003-04-24 22:24:58 +000076 if (!Prefix.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +000077 outs() << "Checking to see if these passes crash: "
78 << getPassesString(Prefix) << ": ";
Rafael Espindolabb876652013-06-17 19:33:18 +000079 if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput))
Chris Lattner1d080f22003-04-24 22:24:58 +000080 return KeepPrefix;
Chris Lattnera9656312003-06-02 04:54:29 +000081
82 OrigProgram = BD.Program;
83
Rafael Espindola28b351a2014-08-26 17:19:03 +000084 BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release();
Craig Toppere6cb63e2014-04-25 04:24:47 +000085 if (BD.Program == nullptr) {
Dan Gohmand8db3762009-07-15 16:35:29 +000086 errs() << BD.getToolName() << ": Error reading bitcode file '"
Rafael Espindolabb876652013-06-17 19:33:18 +000087 << PrefixOutput << "'!\n";
Chris Lattnera9656312003-06-02 04:54:29 +000088 exit(1);
89 }
Rafael Espindolabb876652013-06-17 19:33:18 +000090 sys::fs::remove(PrefixOutput);
Chris Lattner1d080f22003-04-24 22:24:58 +000091 }
92
Dan Gohmanee051522009-07-16 15:30:09 +000093 outs() << "Checking to see if these passes crash: "
94 << getPassesString(Suffix) << ": ";
Misha Brukman650ba8e2005-04-22 00:00:37 +000095
Rafael Espindola37302ea2010-08-05 02:16:32 +000096 if (BD.runPasses(BD.getProgram(), Suffix)) {
Chris Lattner1d080f22003-04-24 22:24:58 +000097 delete OrigProgram; // The suffix crashes alone...
98 return KeepSuffix;
99 }
100
101 // Nothing failed, restore state...
Chris Lattnera9656312003-06-02 04:54:29 +0000102 if (OrigProgram) {
103 delete BD.Program;
104 BD.Program = OrigProgram;
105 }
Chris Lattner1d080f22003-04-24 22:24:58 +0000106 return NoFailure;
107}
108
Bill Wendling3f833432006-10-25 18:36:14 +0000109namespace {
110 /// ReduceCrashingGlobalVariables - This works by removing the global
111 /// variable's initializer and seeing if the program still crashes. If it
112 /// does, then we keep that program and try again.
113 ///
114 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> {
115 BugDriver &BD;
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000116 bool (*TestFn)(const BugDriver &, Module *);
Bill Wendling3f833432006-10-25 18:36:14 +0000117 public:
118 ReduceCrashingGlobalVariables(BugDriver &bd,
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000119 bool (*testFn)(const BugDriver &, Module *))
Bill Wendling3f833432006-10-25 18:36:14 +0000120 : BD(bd), TestFn(testFn) {}
121
Craig Toppere56917c2014-03-08 08:27:28 +0000122 TestResult doTest(std::vector<GlobalVariable*> &Prefix,
123 std::vector<GlobalVariable*> &Kept,
124 std::string &Error) override {
Bill Wendling3f833432006-10-25 18:36:14 +0000125 if (!Kept.empty() && TestGlobalVariables(Kept))
126 return KeepSuffix;
Bill Wendling3f833432006-10-25 18:36:14 +0000127 if (!Prefix.empty() && TestGlobalVariables(Prefix))
128 return KeepPrefix;
Bill Wendling3f833432006-10-25 18:36:14 +0000129 return NoFailure;
130 }
131
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000132 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
Bill Wendling3f833432006-10-25 18:36:14 +0000133 };
134}
135
136bool
137ReduceCrashingGlobalVariables::TestGlobalVariables(
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000138 std::vector<GlobalVariable*> &GVs) {
Bill Wendling3f833432006-10-25 18:36:14 +0000139 // Clone the program to try hacking it apart...
Rafael Espindola229e38f2010-10-13 01:36:30 +0000140 ValueToValueMapTy VMap;
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000141 Module *M = CloneModule(BD.getProgram(), VMap);
Bill Wendling3f833432006-10-25 18:36:14 +0000142
143 // Convert list to set for fast lookup...
144 std::set<GlobalVariable*> GVSet;
145
146 for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000147 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
Bill Wendling3f833432006-10-25 18:36:14 +0000148 assert(CMGV && "Global Variable not in module?!");
149 GVSet.insert(CMGV);
150 }
151
Dan Gohmanee051522009-07-16 15:30:09 +0000152 outs() << "Checking for crash with only these global variables: ";
Bill Wendling3f833432006-10-25 18:36:14 +0000153 PrintGlobalVariableList(GVs);
Dan Gohmanee051522009-07-16 15:30:09 +0000154 outs() << ": ";
Bill Wendling3f833432006-10-25 18:36:14 +0000155
156 // Loop over and delete any global variables which we aren't supposed to be
157 // playing with...
158 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
159 I != E; ++I)
Nick Lewycky4099c7c2009-05-25 06:29:56 +0000160 if (I->hasInitializer() && !GVSet.count(I)) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000161 I->setInitializer(nullptr);
Bill Wendling3f833432006-10-25 18:36:14 +0000162 I->setLinkage(GlobalValue::ExternalLinkage);
163 }
164
165 // Try running the hacked up program...
166 if (TestFn(BD, M)) {
167 BD.setNewProgram(M); // It crashed, keep the trimmed version...
168
169 // Make sure to use global variable pointers that point into the now-current
170 // module.
171 GVs.assign(GVSet.begin(), GVSet.end());
172 return true;
173 }
174
175 delete M;
176 return false;
177}
178
David Blaikiea379b1812011-12-20 02:50:00 +0000179namespace {
Bill Wendling3f833432006-10-25 18:36:14 +0000180 /// ReduceCrashingFunctions reducer - This works by removing functions and
181 /// seeing if the program still crashes. If it does, then keep the newer,
182 /// smaller program.
183 ///
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000184 class ReduceCrashingFunctions : public ListReducer<Function*> {
Chris Lattner2f1aa112004-01-14 03:38:37 +0000185 BugDriver &BD;
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000186 bool (*TestFn)(const BugDriver &, Module *);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000187 public:
Chris Lattner8bda4c42004-02-18 23:26:28 +0000188 ReduceCrashingFunctions(BugDriver &bd,
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000189 bool (*testFn)(const BugDriver &, Module *))
Chris Lattner8bda4c42004-02-18 23:26:28 +0000190 : BD(bd), TestFn(testFn) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000191
Craig Toppere56917c2014-03-08 08:27:28 +0000192 TestResult doTest(std::vector<Function*> &Prefix,
193 std::vector<Function*> &Kept,
194 std::string &Error) override {
Chris Lattner2f1aa112004-01-14 03:38:37 +0000195 if (!Kept.empty() && TestFuncs(Kept))
196 return KeepSuffix;
197 if (!Prefix.empty() && TestFuncs(Prefix))
198 return KeepPrefix;
199 return NoFailure;
200 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000201
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000202 bool TestFuncs(std::vector<Function*> &Prefix);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000203 };
204}
Chris Lattner1d080f22003-04-24 22:24:58 +0000205
JF Bastienf87e20d2015-04-20 23:42:22 +0000206static void RemoveFunctionReferences(Module *M, const char* Name) {
207 auto *UsedVar = M->getGlobalVariable(Name, true);
208 if (!UsedVar || !UsedVar->hasInitializer()) return;
209 if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) {
210 assert(UsedVar->use_empty());
211 UsedVar->eraseFromParent();
212 return;
213 }
214 auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer());
215 std::vector<Constant*> Used;
216 for(Value *V : OldUsedVal->operand_values()) {
217 Constant *Op = cast<Constant>(V->stripPointerCasts());
218 if(!Op->isNullValue()) {
219 Used.push_back(cast<Constant>(V));
220 }
221 }
222 auto *NewValElemTy = OldUsedVal->getType()->getElementType();
223 auto *NewValTy = ArrayType::get(NewValElemTy, Used.size());
224 auto *NewUsedVal = ConstantArray::get(NewValTy, Used);
225 UsedVar->mutateType(NewUsedVal->getType()->getPointerTo());
226 UsedVar->setInitializer(NewUsedVal);
227}
228
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000229bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Dmitri Gribenko6e0520e2013-09-02 01:18:56 +0000230 // If main isn't present, claim there is no problem.
231 if (KeepMain && std::find(Funcs.begin(), Funcs.end(),
232 BD.getProgram()->getFunction("main")) ==
233 Funcs.end())
Andrew Lenharthef9aa122006-03-05 22:21:36 +0000234 return false;
235
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000236 // Clone the program to try hacking it apart...
Rafael Espindola229e38f2010-10-13 01:36:30 +0000237 ValueToValueMapTy VMap;
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000238 Module *M = CloneModule(BD.getProgram(), VMap);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000239
Chris Lattner1d080f22003-04-24 22:24:58 +0000240 // Convert list to set for fast lookup...
241 std::set<Function*> Functions;
242 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000243 Function *CMF = cast<Function>(VMap[Funcs[i]]);
Chris Lattner1d080f22003-04-24 22:24:58 +0000244 assert(CMF && "Function not in module?!");
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000245 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
Dan Gohmanbcad7182009-03-06 02:16:23 +0000246 assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
Chris Lattnerde39f2b2003-04-24 22:54:06 +0000247 Functions.insert(CMF);
Chris Lattner1d080f22003-04-24 22:24:58 +0000248 }
249
Dan Gohmanee051522009-07-16 15:30:09 +0000250 outs() << "Checking for crash with only these functions: ";
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000251 PrintFunctionList(Funcs);
Dan Gohmanee051522009-07-16 15:30:09 +0000252 outs() << ": ";
JF Bastienf87e20d2015-04-20 23:42:22 +0000253 if (!ReplaceFuncsWithNull) {
254 // Loop over and delete any functions which we aren't supposed to be playing
255 // with...
256 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
257 if (!I->isDeclaration() && !Functions.count(I))
258 DeleteFunctionBody(I);
259 } else {
260 std::vector<GlobalValue*> ToRemove;
261 // First, remove aliases to functions we're about to purge.
262 for (GlobalAlias &Alias : M->aliases()) {
263 Constant *Root = Alias.getAliasee()->stripPointerCasts();
264 Function *F = dyn_cast<Function>(Root);
265 if (F) {
266 if (Functions.count(F))
267 // We're keeping this function.
268 continue;
269 } else if (Root->isNullValue()) {
270 // This referenced a globalalias that we've already replaced,
271 // so we still need to replace this alias.
272 } else if (!F) {
273 // Not a function, therefore not something we mess with.
274 continue;
275 }
Chris Lattner1d080f22003-04-24 22:24:58 +0000276
JF Bastienf87e20d2015-04-20 23:42:22 +0000277 PointerType *Ty = cast<PointerType>(Alias.getType());
278 Constant *Replacement = ConstantPointerNull::get(Ty);
279 Alias.replaceAllUsesWith(Replacement);
280 ToRemove.push_back(&Alias);
281 }
Chris Lattner1d080f22003-04-24 22:24:58 +0000282
JF Bastienf87e20d2015-04-20 23:42:22 +0000283 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
284 if (!I->isDeclaration() && !Functions.count(I)) {
285 PointerType *Ty = cast<PointerType>(I->getType());
286 Constant *Replacement = ConstantPointerNull::get(Ty);
287 I->replaceAllUsesWith(Replacement);
288 ToRemove.push_back(I);
289 }
290 }
291
292 for (auto *F : ToRemove) {
293 F->eraseFromParent();
294 }
295
296 // Finally, remove any null members from any global intrinsic.
297 RemoveFunctionReferences(M, "llvm.used");
298 RemoveFunctionReferences(M, "llvm.compiler.used");
299 }
Chris Lattner1d080f22003-04-24 22:24:58 +0000300 // Try running the hacked up program...
Chris Lattner8bda4c42004-02-18 23:26:28 +0000301 if (TestFn(BD, M)) {
Chris Lattner327019b2004-02-18 21:24:48 +0000302 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattner1d080f22003-04-24 22:24:58 +0000303
304 // Make sure to use function pointers that point into the now-current
305 // module.
306 Funcs.assign(Functions.begin(), Functions.end());
307 return true;
308 }
Chris Lattner327019b2004-02-18 21:24:48 +0000309 delete M;
Chris Lattner1d080f22003-04-24 22:24:58 +0000310 return false;
311}
312
Chris Lattner16a41312003-04-24 17:02:17 +0000313
Chris Lattner1942f982004-02-18 21:29:46 +0000314namespace {
Chris Lattner2f1aa112004-01-14 03:38:37 +0000315 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
316 /// all terminators except the specified basic blocks to a 'ret' instruction,
317 /// then running the simplify-cfg pass. This has the effect of chopping up
318 /// the CFG really fast which can reduce large functions quickly.
319 ///
Chris Lattner8bda4c42004-02-18 23:26:28 +0000320 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
Chris Lattner2f1aa112004-01-14 03:38:37 +0000321 BugDriver &BD;
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000322 bool (*TestFn)(const BugDriver &, Module *);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000323 public:
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000324 ReduceCrashingBlocks(BugDriver &bd,
325 bool (*testFn)(const BugDriver &, Module *))
Chris Lattner8bda4c42004-02-18 23:26:28 +0000326 : BD(bd), TestFn(testFn) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000327
Craig Toppere56917c2014-03-08 08:27:28 +0000328 TestResult doTest(std::vector<const BasicBlock*> &Prefix,
329 std::vector<const BasicBlock*> &Kept,
330 std::string &Error) override {
Chris Lattner2f1aa112004-01-14 03:38:37 +0000331 if (!Kept.empty() && TestBlocks(Kept))
332 return KeepSuffix;
333 if (!Prefix.empty() && TestBlocks(Prefix))
334 return KeepPrefix;
335 return NoFailure;
336 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000337
Chris Lattner8bda4c42004-02-18 23:26:28 +0000338 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
Chris Lattner2f1aa112004-01-14 03:38:37 +0000339 };
340}
Chris Lattnerf32939b2003-04-24 23:51:38 +0000341
Chris Lattner8bda4c42004-02-18 23:26:28 +0000342bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000343 // Clone the program to try hacking it apart...
Rafael Espindola229e38f2010-10-13 01:36:30 +0000344 ValueToValueMapTy VMap;
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000345 Module *M = CloneModule(BD.getProgram(), VMap);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000346
Chris Lattnerf32939b2003-04-24 23:51:38 +0000347 // Convert list to set for fast lookup...
Nick Lewyckyb294e312009-04-04 09:39:23 +0000348 SmallPtrSet<BasicBlock*, 8> Blocks;
349 for (unsigned i = 0, e = BBs.size(); i != e; ++i)
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000350 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
Chris Lattnerf32939b2003-04-24 23:51:38 +0000351
Dan Gohmanee051522009-07-16 15:30:09 +0000352 outs() << "Checking for crash with only these blocks:";
Chris Lattner4f50ebd2003-10-27 04:44:59 +0000353 unsigned NumPrint = Blocks.size();
354 if (NumPrint > 10) NumPrint = 10;
355 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Dan Gohmanee051522009-07-16 15:30:09 +0000356 outs() << " " << BBs[i]->getName();
Chris Lattner4f50ebd2003-10-27 04:44:59 +0000357 if (NumPrint < Blocks.size())
Dan Gohmanee051522009-07-16 15:30:09 +0000358 outs() << "... <" << Blocks.size() << " total>";
359 outs() << ": ";
Chris Lattnerf32939b2003-04-24 23:51:38 +0000360
361 // Loop over and delete any hack up any blocks that are not listed...
362 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
363 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner7b233af2003-11-22 02:10:38 +0000364 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattnerf32939b2003-04-24 23:51:38 +0000365 // Loop over all of the successors of this block, deleting any PHI nodes
366 // that might include it.
Duncan P. N. Exon Smith6c990152014-07-21 17:06:51 +0000367 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
368 (*SI)->removePredecessor(BB);
Chris Lattnerf32939b2003-04-24 23:51:38 +0000369
Chris Lattner7855a5c2008-04-28 00:04:58 +0000370 TerminatorInst *BBTerm = BB->getTerminator();
JF Bastienf87e20d2015-04-20 23:42:22 +0000371
Dan Gohman34a22492010-06-07 20:19:26 +0000372 if (!BB->getTerminator()->getType()->isVoidTy())
Owen Anderson5a1acd92009-07-31 20:28:14 +0000373 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
Chris Lattner7b233af2003-11-22 02:10:38 +0000374
Chris Lattner7855a5c2008-04-28 00:04:58 +0000375 // Replace the old terminator instruction.
Chris Lattnerf32939b2003-04-24 23:51:38 +0000376 BB->getInstList().pop_back();
Owen Anderson55f1c092009-08-13 21:58:54 +0000377 new UnreachableInst(BB->getContext(), BB);
Chris Lattnerf32939b2003-04-24 23:51:38 +0000378 }
379
380 // The CFG Simplifier pass may delete one of the basic blocks we are
381 // interested in. If it does we need to take the block out of the list. Make
382 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
383 // This won't work well if blocks are unnamed, but that is just the risk we
384 // have to take.
Rafael Espindolad1e241a2010-08-10 15:46:11 +0000385 std::vector<std::pair<std::string, std::string> > BlockInfo;
Chris Lattnerf32939b2003-04-24 23:51:38 +0000386
Craig Topper46276792014-08-24 23:23:06 +0000387 for (BasicBlock *BB : Blocks)
388 BlockInfo.push_back(std::make_pair(BB->getParent()->getName(),
389 BB->getName()));
Chris Lattnerf32939b2003-04-24 23:51:38 +0000390
391 // Now run the CFG simplify pass on the function...
Rafael Espindolad1e241a2010-08-10 15:46:11 +0000392 std::vector<std::string> Passes;
393 Passes.push_back("simplifycfg");
394 Passes.push_back("verify");
Rafael Espindola28b351a2014-08-26 17:19:03 +0000395 std::unique_ptr<Module> New = BD.runPassesOn(M, Passes);
Rafael Espindolad1e241a2010-08-10 15:46:11 +0000396 delete M;
397 if (!New) {
398 errs() << "simplifycfg failed!\n";
399 exit(1);
400 }
Rafael Espindola28b351a2014-08-26 17:19:03 +0000401 M = New.release();
Chris Lattnerf32939b2003-04-24 23:51:38 +0000402
403 // Try running on the hacked up program...
Chris Lattner8bda4c42004-02-18 23:26:28 +0000404 if (TestFn(BD, M)) {
Chris Lattner327019b2004-02-18 21:24:48 +0000405 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattnerf32939b2003-04-24 23:51:38 +0000406
407 // Make sure to use basic block pointers that point into the now-current
408 // module, and that they don't include any deleted blocks.
409 BBs.clear();
Rafael Espindolad1e241a2010-08-10 15:46:11 +0000410 const ValueSymbolTable &GST = M->getValueSymbolTable();
Chris Lattnerf32939b2003-04-24 23:51:38 +0000411 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
Rafael Espindolad1e241a2010-08-10 15:46:11 +0000412 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first));
413 ValueSymbolTable &ST = F->getValueSymbolTable();
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000414 Value* V = ST.lookup(BlockInfo[i].second);
Owen Anderson55f1c092009-08-13 21:58:54 +0000415 if (V && V->getType() == Type::getLabelTy(V->getContext()))
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000416 BBs.push_back(cast<BasicBlock>(V));
Chris Lattnerf32939b2003-04-24 23:51:38 +0000417 }
418 return true;
419 }
Chris Lattner327019b2004-02-18 21:24:48 +0000420 delete M; // It didn't crash, try something else.
Chris Lattnerf32939b2003-04-24 23:51:38 +0000421 return false;
422}
423
Nick Lewycky6e090c92009-05-25 05:30:00 +0000424namespace {
425 /// ReduceCrashingInstructions reducer - This works by removing the specified
426 /// non-terminator instructions and replacing them with undef.
427 ///
428 class ReduceCrashingInstructions : public ListReducer<const Instruction*> {
429 BugDriver &BD;
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000430 bool (*TestFn)(const BugDriver &, Module *);
Nick Lewycky6e090c92009-05-25 05:30:00 +0000431 public:
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000432 ReduceCrashingInstructions(BugDriver &bd,
433 bool (*testFn)(const BugDriver &, Module *))
Nick Lewycky6e090c92009-05-25 05:30:00 +0000434 : BD(bd), TestFn(testFn) {}
435
Craig Toppere56917c2014-03-08 08:27:28 +0000436 TestResult doTest(std::vector<const Instruction*> &Prefix,
437 std::vector<const Instruction*> &Kept,
438 std::string &Error) override {
Nick Lewycky6e090c92009-05-25 05:30:00 +0000439 if (!Kept.empty() && TestInsts(Kept))
440 return KeepSuffix;
441 if (!Prefix.empty() && TestInsts(Prefix))
442 return KeepPrefix;
443 return NoFailure;
444 }
445
446 bool TestInsts(std::vector<const Instruction*> &Prefix);
447 };
448}
449
450bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
451 &Insts) {
452 // Clone the program to try hacking it apart...
Rafael Espindola229e38f2010-10-13 01:36:30 +0000453 ValueToValueMapTy VMap;
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000454 Module *M = CloneModule(BD.getProgram(), VMap);
Nick Lewycky6e090c92009-05-25 05:30:00 +0000455
456 // Convert list to set for fast lookup...
457 SmallPtrSet<Instruction*, 64> Instructions;
458 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
459 assert(!isa<TerminatorInst>(Insts[i]));
Devang Patel0dc3c2d2010-06-24 00:33:28 +0000460 Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
Nick Lewycky6e090c92009-05-25 05:30:00 +0000461 }
462
Dan Gohmanee051522009-07-16 15:30:09 +0000463 outs() << "Checking for crash with only " << Instructions.size();
Nick Lewycky6e090c92009-05-25 05:30:00 +0000464 if (Instructions.size() == 1)
Dan Gohmanee051522009-07-16 15:30:09 +0000465 outs() << " instruction: ";
Nick Lewycky6e090c92009-05-25 05:30:00 +0000466 else
Dan Gohmanee051522009-07-16 15:30:09 +0000467 outs() << " instructions: ";
Nick Lewycky6e090c92009-05-25 05:30:00 +0000468
469 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
470 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
471 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
472 Instruction *Inst = I++;
Eli Friedmand4e02a52011-11-01 04:40:56 +0000473 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
474 !isa<LandingPadInst>(Inst)) {
Dan Gohman34a22492010-06-07 20:19:26 +0000475 if (!Inst->getType()->isVoidTy())
Nick Lewycky6e090c92009-05-25 05:30:00 +0000476 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
477 Inst->eraseFromParent();
478 }
479 }
480
481 // Verify that this is still valid.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000482 legacy::PassManager Passes;
Nick Lewycky6e090c92009-05-25 05:30:00 +0000483 Passes.add(createVerifierPass());
484 Passes.run(*M);
485
486 // Try running on the hacked up program...
487 if (TestFn(BD, M)) {
488 BD.setNewProgram(M); // It crashed, keep the trimmed version...
489
490 // Make sure to use instruction pointers that point into the now-current
491 // module, and that they don't include any deleted blocks.
492 Insts.clear();
Craig Topper46276792014-08-24 23:23:06 +0000493 for (Instruction *Inst : Instructions)
494 Insts.push_back(Inst);
Nick Lewycky6e090c92009-05-25 05:30:00 +0000495 return true;
496 }
497 delete M; // It didn't crash, try something else.
498 return false;
499}
500
Chris Lattner8bda4c42004-02-18 23:26:28 +0000501/// DebugACrash - Given a predicate that determines whether a component crashes
502/// on a program, try to destructively reduce the program while still keeping
503/// the predicate true.
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000504static bool DebugACrash(BugDriver &BD,
505 bool (*TestFn)(const BugDriver &, Module *),
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000506 std::string &Error) {
Bill Wendling3f833432006-10-25 18:36:14 +0000507 // See if we can get away with nuking some of the global variable initializers
Chris Lattner65e5f652003-04-25 00:53:05 +0000508 // in the program...
Torok Edwin5bd3f7b2009-05-24 09:31:04 +0000509 if (!NoGlobalRM &&
510 BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
Bill Wendling3f833432006-10-25 18:36:14 +0000511 // Now try to reduce the number of global variable initializers in the
512 // module to something small.
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000513 Module *M = CloneModule(BD.getProgram());
514 bool DeletedInit = false;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000515
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000516 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
517 I != E; ++I)
518 if (I->hasInitializer()) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000519 I->setInitializer(nullptr);
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000520 I->setLinkage(GlobalValue::ExternalLinkage);
521 DeletedInit = true;
522 }
Bill Wendling3f833432006-10-25 18:36:14 +0000523
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000524 if (!DeletedInit) {
525 delete M; // No change made...
526 } else {
527 // See if the program still causes a crash...
Dan Gohmanee051522009-07-16 15:30:09 +0000528 outs() << "\nChecking to see if we can delete global inits: ";
Bill Wendling3f833432006-10-25 18:36:14 +0000529
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000530 if (TestFn(BD, M)) { // Still crashes?
531 BD.setNewProgram(M);
Dan Gohmanee051522009-07-16 15:30:09 +0000532 outs() << "\n*** Able to remove all global initializers!\n";
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000533 } else { // No longer crashes?
Dan Gohmanee051522009-07-16 15:30:09 +0000534 outs() << " - Removing all global inits hides problem!\n";
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000535 delete M;
Bill Wendling3f833432006-10-25 18:36:14 +0000536
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000537 std::vector<GlobalVariable*> GVs;
538
539 for (Module::global_iterator I = BD.getProgram()->global_begin(),
540 E = BD.getProgram()->global_end(); I != E; ++I)
541 if (I->hasInitializer())
542 GVs.push_back(I);
543
544 if (GVs.size() > 1 && !BugpointIsInterrupted) {
Dan Gohmanee051522009-07-16 15:30:09 +0000545 outs() << "\n*** Attempting to reduce the number of global "
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000546 << "variables in the testcase\n";
547
548 unsigned OldSize = GVs.size();
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000549 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
550 if (!Error.empty())
551 return true;
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000552
553 if (GVs.size() < OldSize)
Rafael Espindola594994a2010-07-28 18:12:30 +0000554 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
Bill Wendling74a4a2a2006-10-27 20:18:06 +0000555 }
Bill Wendlingce3afd62006-10-27 20:22:04 +0000556 }
Chris Lattner65e5f652003-04-25 00:53:05 +0000557 }
558 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000559
Chris Lattner1d080f22003-04-24 22:24:58 +0000560 // Now try to reduce the number of functions in the module to something small.
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000561 std::vector<Function*> Functions;
562 for (Module::iterator I = BD.getProgram()->begin(),
Chris Lattner8bda4c42004-02-18 23:26:28 +0000563 E = BD.getProgram()->end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000564 if (!I->isDeclaration())
Chris Lattner1d080f22003-04-24 22:24:58 +0000565 Functions.push_back(I);
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000566
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000567 if (Functions.size() > 1 && !BugpointIsInterrupted) {
Dan Gohmanee051522009-07-16 15:30:09 +0000568 outs() << "\n*** Attempting to reduce the number of functions "
Chris Lattner1d080f22003-04-24 22:24:58 +0000569 "in the testcase\n";
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000570
Chris Lattner8bda4c42004-02-18 23:26:28 +0000571 unsigned OldSize = Functions.size();
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000572 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000573
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000574 if (Functions.size() < OldSize)
Rafael Espindola594994a2010-07-28 18:12:30 +0000575 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000576 }
577
Chris Lattnerf32939b2003-04-24 23:51:38 +0000578 // Attempt to delete entire basic blocks at a time to speed up
579 // convergence... this actually works by setting the terminator of the blocks
580 // to a return instruction then running simplifycfg, which can potentially
581 // shrinks the code dramatically quickly
582 //
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000583 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
Chris Lattner8bda4c42004-02-18 23:26:28 +0000584 std::vector<const BasicBlock*> Blocks;
585 for (Module::const_iterator I = BD.getProgram()->begin(),
586 E = BD.getProgram()->end(); I != E; ++I)
587 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
Chris Lattnerd1e2aae2003-08-05 15:51:05 +0000588 Blocks.push_back(FI);
Torok Edwin6ee6f732009-05-24 09:40:47 +0000589 unsigned OldSize = Blocks.size();
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000590 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
Torok Edwin6ee6f732009-05-24 09:40:47 +0000591 if (Blocks.size() < OldSize)
Rafael Espindola594994a2010-07-28 18:12:30 +0000592 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
Chris Lattnerd1e2aae2003-08-05 15:51:05 +0000593 }
Chris Lattnerd4e04742002-12-23 23:49:59 +0000594
Nick Lewycky6e090c92009-05-25 05:30:00 +0000595 // Attempt to delete instructions using bisection. This should help out nasty
596 // cases with large basic blocks where the problem is at one end.
597 if (!BugpointIsInterrupted) {
598 std::vector<const Instruction*> Insts;
599 for (Module::const_iterator MI = BD.getProgram()->begin(),
600 ME = BD.getProgram()->end(); MI != ME; ++MI)
601 for (Function::const_iterator FI = MI->begin(), FE = MI->end(); FI != FE;
602 ++FI)
603 for (BasicBlock::const_iterator I = FI->begin(), E = FI->end();
604 I != E; ++I)
605 if (!isa<TerminatorInst>(I))
606 Insts.push_back(I);
607
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000608 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
Nick Lewycky6e090c92009-05-25 05:30:00 +0000609 }
610
Chris Lattner1d080f22003-04-24 22:24:58 +0000611 // FIXME: This should use the list reducer to converge faster by deleting
612 // larger chunks of instructions at a time!
Chris Lattner0ee372c2004-03-13 19:35:54 +0000613 unsigned Simplification = 2;
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000614 do {
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000615 if (BugpointIsInterrupted) break;
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000616 --Simplification;
Dan Gohmanee051522009-07-16 15:30:09 +0000617 outs() << "\n*** Attempting to reduce testcase by deleting instruc"
618 << "tions: Simplification Level #" << Simplification << '\n';
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000619
Misha Brukman7eb05a12003-08-18 14:43:39 +0000620 // Now that we have deleted the functions that are unnecessary for the
621 // program, try to remove instructions that are not necessary to cause the
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000622 // crash. To do this, we loop through all of the instructions in the
623 // remaining functions, deleting them (replacing any values produced with
624 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
625 // still triggers failure, keep deleting until we cannot trigger failure
626 // anymore.
627 //
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000628 unsigned InstructionsToSkipBeforeDeleting = 0;
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000629 TryAgain:
Misha Brukman650ba8e2005-04-22 00:00:37 +0000630
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000631 // Loop over all of the (non-terminator) instructions remaining in the
632 // function, attempting to delete them.
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000633 unsigned CurInstructionNum = 0;
Chris Lattner8bda4c42004-02-18 23:26:28 +0000634 for (Module::const_iterator FI = BD.getProgram()->begin(),
635 E = BD.getProgram()->end(); FI != E; ++FI)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000636 if (!FI->isDeclaration())
Chris Lattner8bda4c42004-02-18 23:26:28 +0000637 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
638 ++BI)
639 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
Bill Wendling2990ec62011-12-25 06:56:22 +0000640 I != E; ++I, ++CurInstructionNum) {
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000641 if (InstructionsToSkipBeforeDeleting) {
642 --InstructionsToSkipBeforeDeleting;
643 } else {
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000644 if (BugpointIsInterrupted) goto ExitLoops;
645
Eli Friedmand4e02a52011-11-01 04:40:56 +0000646 if (isa<LandingPadInst>(I))
647 continue;
648
Dan Gohmanee051522009-07-16 15:30:09 +0000649 outs() << "Checking instruction: " << *I;
Rafael Espindola28b351a2014-08-26 17:19:03 +0000650 std::unique_ptr<Module> M =
651 BD.deleteInstructionFromProgram(I, Simplification);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000652
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000653 // Find out if the pass still crashes on this pass...
Rafael Espindola28b351a2014-08-26 17:19:03 +0000654 if (TestFn(BD, M.get())) {
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000655 // Yup, it does, we delete the old module, and continue trying
656 // to reduce the testcase...
Rafael Espindola28b351a2014-08-26 17:19:03 +0000657 BD.setNewProgram(M.release());
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000658 InstructionsToSkipBeforeDeleting = CurInstructionNum;
659 goto TryAgain; // I wish I had a multi-level break here!
660 }
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000661 }
Bill Wendling2990ec62011-12-25 06:56:22 +0000662 }
Chris Lattner94b2bfb2004-02-18 23:59:11 +0000663
664 if (InstructionsToSkipBeforeDeleting) {
665 InstructionsToSkipBeforeDeleting = 0;
666 goto TryAgain;
667 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000668
Chris Lattner0eb5ce92003-01-23 02:48:33 +0000669 } while (Simplification);
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000670ExitLoops:
Chris Lattner514c02e2003-02-28 16:13:20 +0000671
672 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000673 if (!BugpointIsInterrupted) {
Dan Gohmanee051522009-07-16 15:30:09 +0000674 outs() << "\n*** Attempting to perform final cleanups: ";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000675 Module *M = CloneModule(BD.getProgram());
Rafael Espindola28b351a2014-08-26 17:19:03 +0000676 M = BD.performFinalCleanups(M, true).release();
Misha Brukman650ba8e2005-04-22 00:00:37 +0000677
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000678 // Find out if the pass still crashes on the cleaned up program...
679 if (TestFn(BD, M)) {
680 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
681 } else {
682 delete M;
683 }
Chris Lattner514c02e2003-02-28 16:13:20 +0000684 }
685
Rafael Espindola594994a2010-07-28 18:12:30 +0000686 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
Chris Lattner514c02e2003-02-28 16:13:20 +0000687
Misha Brukman650ba8e2005-04-22 00:00:37 +0000688 return false;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000689}
Brian Gaeke960707c2003-11-11 22:41:34 +0000690
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000691static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
Chris Lattner8bda4c42004-02-18 23:26:28 +0000692 return BD.runPasses(M);
693}
Chris Lattneread1dff2004-02-18 21:02:04 +0000694
Chris Lattner8bda4c42004-02-18 23:26:28 +0000695/// debugOptimizerCrash - This method is called when some pass crashes on input.
696/// It attempts to prune down the testcase to something reasonable, and figure
697/// out exactly which pass is crashing.
698///
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000699bool BugDriver::debugOptimizerCrash(const std::string &ID) {
Dan Gohmanee051522009-07-16 15:30:09 +0000700 outs() << "\n*** Debugging optimizer crash!\n";
Chris Lattner8bda4c42004-02-18 23:26:28 +0000701
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000702 std::string Error;
Chris Lattner8bda4c42004-02-18 23:26:28 +0000703 // Reduce the list of passes which causes the optimizer to crash...
JF Bastienf87e20d2015-04-20 23:42:22 +0000704 if (!BugpointIsInterrupted && !DontReducePassList)
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000705 ReducePassList(*this).reduceList(PassesToRun, Error);
706 assert(Error.empty());
Chris Lattner8bda4c42004-02-18 23:26:28 +0000707
Dan Gohmanee051522009-07-16 15:30:09 +0000708 outs() << "\n*** Found crashing pass"
709 << (PassesToRun.size() == 1 ? ": " : "es: ")
710 << getPassesString(PassesToRun) << '\n';
Chris Lattner8bda4c42004-02-18 23:26:28 +0000711
Rafael Espindola594994a2010-07-28 18:12:30 +0000712 EmitProgressBitcode(Program, ID);
Chris Lattner8bda4c42004-02-18 23:26:28 +0000713
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000714 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
715 assert(Error.empty());
716 return Success;
Chris Lattner8bda4c42004-02-18 23:26:28 +0000717}
718
Rafael Espindolad1c7ef42010-08-05 03:00:22 +0000719static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000720 std::string Error;
721 BD.compileProgram(M, &Error);
722 if (!Error.empty()) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000723 errs() << "<crash>\n";
Chris Lattner8bda4c42004-02-18 23:26:28 +0000724 return true; // Tool is still crashing.
725 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000726 errs() << '\n';
727 return false;
Chris Lattner8bda4c42004-02-18 23:26:28 +0000728}
Chris Lattneread1dff2004-02-18 21:02:04 +0000729
730/// debugCodeGeneratorCrash - This method is called when the code generator
731/// crashes on an input. It attempts to reduce the input as much as possible
732/// while still causing the code generator to crash.
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000733bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000734 errs() << "*** Debugging code generator crash!\n";
Chris Lattneread1dff2004-02-18 21:02:04 +0000735
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000736 return DebugACrash(*this, TestForCodeGenCrash, Error);
Chris Lattneread1dff2004-02-18 21:02:04 +0000737}