blob: ec4d0b29d749d9437b1dd4d462659971564dde47 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-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 Lattneraae33f92003-04-24 22:24:58 +000015#include "ListReducer.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000016#include "ToolRunner.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/Analysis/Verifier.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/ValueSymbolTable.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000024#include "llvm/Pass.h"
25#include "llvm/PassManager.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000026#include "llvm/Support/CFG.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/FileUtilities.h"
Rafael Espindolaf656a1d2013-06-17 19:21:38 +000029#include "llvm/Support/PathV1.h"
Chris Lattner286921e2003-04-24 23:51:38 +000030#include "llvm/Transforms/Scalar.h"
Chris Lattneraae33f92003-04-24 22:24:58 +000031#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattneraae33f92003-04-24 22:24:58 +000032#include <set>
Chris Lattnerfa761832004-01-14 03:38:37 +000033using namespace llvm;
Chris Lattnerafade922002-11-20 22:28:10 +000034
Andrew Lenharth7c0a9372006-03-05 22:21:36 +000035namespace {
36 cl::opt<bool>
37 KeepMain("keep-main",
38 cl::desc("Force function reduction to keep main"),
39 cl::init(false));
Torok Edwin23d471f2009-05-24 09:31:04 +000040 cl::opt<bool>
41 NoGlobalRM ("disable-global-remove",
42 cl::desc("Do not remove global variables"),
43 cl::init(false));
Andrew Lenharth7c0a9372006-03-05 22:21:36 +000044}
45
Brian Gaeked0fde302003-11-11 22:41:34 +000046namespace llvm {
Rafael Espindola8261dfe2010-08-08 03:55:08 +000047 class ReducePassList : public ListReducer<std::string> {
Chris Lattnerfa761832004-01-14 03:38:37 +000048 BugDriver &BD;
49 public:
Chris Lattner06905db2004-02-18 21:24:48 +000050 ReducePassList(BugDriver &bd) : BD(bd) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000051
Sylvestre Ledru94c22712012-09-27 10:14:43 +000052 // doTest - Return true iff running the "removed" passes succeeds, and
Chris Lattnerfa761832004-01-14 03:38:37 +000053 // running the "Kept" passes fail when run on the output of the "removed"
54 // passes. If we return true, we update the current module of bugpoint.
55 //
Rafael Espindola8261dfe2010-08-08 03:55:08 +000056 virtual TestResult doTest(std::vector<std::string> &Removed,
57 std::vector<std::string> &Kept,
Nick Lewycky22ff7482010-04-12 05:08:25 +000058 std::string &Error);
Chris Lattnerfa761832004-01-14 03:38:37 +000059 };
60}
Chris Lattneraae33f92003-04-24 22:24:58 +000061
Chris Lattner06905db2004-02-18 21:24:48 +000062ReducePassList::TestResult
Rafael Espindola8261dfe2010-08-08 03:55:08 +000063ReducePassList::doTest(std::vector<std::string> &Prefix,
64 std::vector<std::string> &Suffix,
Nick Lewycky22ff7482010-04-12 05:08:25 +000065 std::string &Error) {
Reid Spencer5f767602004-12-16 23:04:20 +000066 sys::Path PrefixOutput;
Chris Lattnerb417c792003-06-02 04:54:29 +000067 Module *OrigProgram = 0;
Chris Lattneraae33f92003-04-24 22:24:58 +000068 if (!Prefix.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000069 outs() << "Checking to see if these passes crash: "
70 << getPassesString(Prefix) << ": ";
Reid Spencer5f767602004-12-16 23:04:20 +000071 std::string PfxOutput;
Rafael Espindolaca356af2010-08-05 00:29:04 +000072 if (BD.runPasses(BD.getProgram(), Prefix, PfxOutput))
Chris Lattneraae33f92003-04-24 22:24:58 +000073 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000074
Reid Spencerdd04df02005-07-07 23:21:43 +000075 PrefixOutput.set(PfxOutput);
Chris Lattnerb417c792003-06-02 04:54:29 +000076 OrigProgram = BD.Program;
77
Chris Lattner74382b72009-08-23 22:45:37 +000078 BD.Program = ParseInputFile(PrefixOutput.str(), BD.getContext());
Chris Lattnerb417c792003-06-02 04:54:29 +000079 if (BD.Program == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +000080 errs() << BD.getToolName() << ": Error reading bitcode file '"
Chris Lattner74382b72009-08-23 22:45:37 +000081 << PrefixOutput.str() << "'!\n";
Chris Lattnerb417c792003-06-02 04:54:29 +000082 exit(1);
83 }
Reid Spencera229c5c2005-07-08 03:08:58 +000084 PrefixOutput.eraseFromDisk();
Chris Lattneraae33f92003-04-24 22:24:58 +000085 }
86
Dan Gohmanac95cc72009-07-16 15:30:09 +000087 outs() << "Checking to see if these passes crash: "
88 << getPassesString(Suffix) << ": ";
Misha Brukman3da94ae2005-04-22 00:00:37 +000089
Rafael Espindola5d8cace2010-08-05 02:16:32 +000090 if (BD.runPasses(BD.getProgram(), Suffix)) {
Chris Lattneraae33f92003-04-24 22:24:58 +000091 delete OrigProgram; // The suffix crashes alone...
92 return KeepSuffix;
93 }
94
95 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000096 if (OrigProgram) {
97 delete BD.Program;
98 BD.Program = OrigProgram;
99 }
Chris Lattneraae33f92003-04-24 22:24:58 +0000100 return NoFailure;
101}
102
Bill Wendling4e3be892006-10-25 18:36:14 +0000103namespace {
104 /// ReduceCrashingGlobalVariables - This works by removing the global
105 /// variable's initializer and seeing if the program still crashes. If it
106 /// does, then we keep that program and try again.
107 ///
108 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> {
109 BugDriver &BD;
Rafael Espindola248d1c62010-08-05 03:00:22 +0000110 bool (*TestFn)(const BugDriver &, Module *);
Bill Wendling4e3be892006-10-25 18:36:14 +0000111 public:
112 ReduceCrashingGlobalVariables(BugDriver &bd,
Rafael Espindola248d1c62010-08-05 03:00:22 +0000113 bool (*testFn)(const BugDriver &, Module *))
Bill Wendling4e3be892006-10-25 18:36:14 +0000114 : BD(bd), TestFn(testFn) {}
115
Nick Lewycky22ff7482010-04-12 05:08:25 +0000116 virtual TestResult doTest(std::vector<GlobalVariable*> &Prefix,
117 std::vector<GlobalVariable*> &Kept,
118 std::string &Error) {
Bill Wendling4e3be892006-10-25 18:36:14 +0000119 if (!Kept.empty() && TestGlobalVariables(Kept))
120 return KeepSuffix;
Bill Wendling4e3be892006-10-25 18:36:14 +0000121 if (!Prefix.empty() && TestGlobalVariables(Prefix))
122 return KeepPrefix;
Bill Wendling4e3be892006-10-25 18:36:14 +0000123 return NoFailure;
124 }
125
Nick Lewycky22ff7482010-04-12 05:08:25 +0000126 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
Bill Wendling4e3be892006-10-25 18:36:14 +0000127 };
128}
129
130bool
131ReduceCrashingGlobalVariables::TestGlobalVariables(
Nick Lewycky22ff7482010-04-12 05:08:25 +0000132 std::vector<GlobalVariable*> &GVs) {
Bill Wendling4e3be892006-10-25 18:36:14 +0000133 // Clone the program to try hacking it apart...
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000134 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000135 Module *M = CloneModule(BD.getProgram(), VMap);
Bill Wendling4e3be892006-10-25 18:36:14 +0000136
137 // Convert list to set for fast lookup...
138 std::set<GlobalVariable*> GVSet;
139
140 for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
Devang Patele9916a32010-06-24 00:33:28 +0000141 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
Bill Wendling4e3be892006-10-25 18:36:14 +0000142 assert(CMGV && "Global Variable not in module?!");
143 GVSet.insert(CMGV);
144 }
145
Dan Gohmanac95cc72009-07-16 15:30:09 +0000146 outs() << "Checking for crash with only these global variables: ";
Bill Wendling4e3be892006-10-25 18:36:14 +0000147 PrintGlobalVariableList(GVs);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000148 outs() << ": ";
Bill Wendling4e3be892006-10-25 18:36:14 +0000149
150 // Loop over and delete any global variables which we aren't supposed to be
151 // playing with...
152 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
153 I != E; ++I)
Nick Lewycky028839d2009-05-25 06:29:56 +0000154 if (I->hasInitializer() && !GVSet.count(I)) {
Bill Wendling4e3be892006-10-25 18:36:14 +0000155 I->setInitializer(0);
156 I->setLinkage(GlobalValue::ExternalLinkage);
157 }
158
159 // Try running the hacked up program...
160 if (TestFn(BD, M)) {
161 BD.setNewProgram(M); // It crashed, keep the trimmed version...
162
163 // Make sure to use global variable pointers that point into the now-current
164 // module.
165 GVs.assign(GVSet.begin(), GVSet.end());
166 return true;
167 }
168
169 delete M;
170 return false;
171}
172
David Blaikie2d24e2a2011-12-20 02:50:00 +0000173namespace {
Bill Wendling4e3be892006-10-25 18:36:14 +0000174 /// ReduceCrashingFunctions reducer - This works by removing functions and
175 /// seeing if the program still crashes. If it does, then keep the newer,
176 /// smaller program.
177 ///
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000178 class ReduceCrashingFunctions : public ListReducer<Function*> {
Chris Lattnerfa761832004-01-14 03:38:37 +0000179 BugDriver &BD;
Rafael Espindola248d1c62010-08-05 03:00:22 +0000180 bool (*TestFn)(const BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000181 public:
Chris Lattner8b189272004-02-18 23:26:28 +0000182 ReduceCrashingFunctions(BugDriver &bd,
Rafael Espindola248d1c62010-08-05 03:00:22 +0000183 bool (*testFn)(const BugDriver &, Module *))
Chris Lattner8b189272004-02-18 23:26:28 +0000184 : BD(bd), TestFn(testFn) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000185
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000186 virtual TestResult doTest(std::vector<Function*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000187 std::vector<Function*> &Kept,
188 std::string &Error) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000189 if (!Kept.empty() && TestFuncs(Kept))
190 return KeepSuffix;
191 if (!Prefix.empty() && TestFuncs(Prefix))
192 return KeepPrefix;
193 return NoFailure;
194 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000195
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000196 bool TestFuncs(std::vector<Function*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000197 };
198}
Chris Lattneraae33f92003-04-24 22:24:58 +0000199
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000200bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Andrew Lenharth7c0a9372006-03-05 22:21:36 +0000201
202 //if main isn't present, claim there is no problem
Bill Wendling4e3be892006-10-25 18:36:14 +0000203 if (KeepMain && find(Funcs.begin(), Funcs.end(),
Reid Spencer688b0492007-02-05 21:19:13 +0000204 BD.getProgram()->getFunction("main")) == Funcs.end())
Andrew Lenharth7c0a9372006-03-05 22:21:36 +0000205 return false;
206
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000207 // Clone the program to try hacking it apart...
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000208 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000209 Module *M = CloneModule(BD.getProgram(), VMap);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000210
Chris Lattneraae33f92003-04-24 22:24:58 +0000211 // Convert list to set for fast lookup...
212 std::set<Function*> Functions;
213 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
Devang Patele9916a32010-06-24 00:33:28 +0000214 Function *CMF = cast<Function>(VMap[Funcs[i]]);
Chris Lattneraae33f92003-04-24 22:24:58 +0000215 assert(CMF && "Function not in module?!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000216 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
Dan Gohmanf6dd0eb2009-03-06 02:16:23 +0000217 assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
Chris Lattnerf607b792003-04-24 22:54:06 +0000218 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000219 }
220
Dan Gohmanac95cc72009-07-16 15:30:09 +0000221 outs() << "Checking for crash with only these functions: ";
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000222 PrintFunctionList(Funcs);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000223 outs() << ": ";
Chris Lattneraae33f92003-04-24 22:24:58 +0000224
225 // Loop over and delete any functions which we aren't supposed to be playing
226 // with...
227 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000228 if (!I->isDeclaration() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000229 DeleteFunctionBody(I);
230
231 // Try running the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000232 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000233 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattneraae33f92003-04-24 22:24:58 +0000234
235 // Make sure to use function pointers that point into the now-current
236 // module.
237 Funcs.assign(Functions.begin(), Functions.end());
238 return true;
239 }
Chris Lattner06905db2004-02-18 21:24:48 +0000240 delete M;
Chris Lattneraae33f92003-04-24 22:24:58 +0000241 return false;
242}
243
Chris Lattner640f22e2003-04-24 17:02:17 +0000244
Chris Lattnerf913f402004-02-18 21:29:46 +0000245namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000246 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
247 /// all terminators except the specified basic blocks to a 'ret' instruction,
248 /// then running the simplify-cfg pass. This has the effect of chopping up
249 /// the CFG really fast which can reduce large functions quickly.
250 ///
Chris Lattner8b189272004-02-18 23:26:28 +0000251 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
Chris Lattnerfa761832004-01-14 03:38:37 +0000252 BugDriver &BD;
Rafael Espindola248d1c62010-08-05 03:00:22 +0000253 bool (*TestFn)(const BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000254 public:
Rafael Espindola248d1c62010-08-05 03:00:22 +0000255 ReduceCrashingBlocks(BugDriver &bd,
256 bool (*testFn)(const BugDriver &, Module *))
Chris Lattner8b189272004-02-18 23:26:28 +0000257 : BD(bd), TestFn(testFn) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000258
Chris Lattner8b189272004-02-18 23:26:28 +0000259 virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000260 std::vector<const BasicBlock*> &Kept,
261 std::string &Error) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000262 if (!Kept.empty() && TestBlocks(Kept))
263 return KeepSuffix;
264 if (!Prefix.empty() && TestBlocks(Prefix))
265 return KeepPrefix;
266 return NoFailure;
267 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000268
Chris Lattner8b189272004-02-18 23:26:28 +0000269 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000270 };
271}
Chris Lattner286921e2003-04-24 23:51:38 +0000272
Chris Lattner8b189272004-02-18 23:26:28 +0000273bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000274 // Clone the program to try hacking it apart...
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000275 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000276 Module *M = CloneModule(BD.getProgram(), VMap);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000277
Chris Lattner286921e2003-04-24 23:51:38 +0000278 // Convert list to set for fast lookup...
Nick Lewyckye0325902009-04-04 09:39:23 +0000279 SmallPtrSet<BasicBlock*, 8> Blocks;
280 for (unsigned i = 0, e = BBs.size(); i != e; ++i)
Devang Patele9916a32010-06-24 00:33:28 +0000281 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
Chris Lattner286921e2003-04-24 23:51:38 +0000282
Dan Gohmanac95cc72009-07-16 15:30:09 +0000283 outs() << "Checking for crash with only these blocks:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000284 unsigned NumPrint = Blocks.size();
285 if (NumPrint > 10) NumPrint = 10;
286 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000287 outs() << " " << BBs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000288 if (NumPrint < Blocks.size())
Dan Gohmanac95cc72009-07-16 15:30:09 +0000289 outs() << "... <" << Blocks.size() << " total>";
290 outs() << ": ";
Chris Lattner286921e2003-04-24 23:51:38 +0000291
292 // Loop over and delete any hack up any blocks that are not listed...
293 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
294 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner8bc098b2003-11-22 02:10:38 +0000295 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattner286921e2003-04-24 23:51:38 +0000296 // Loop over all of the successors of this block, deleting any PHI nodes
297 // that might include it.
298 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
299 (*SI)->removePredecessor(BB);
300
Chris Lattnere78109e2008-04-28 00:04:58 +0000301 TerminatorInst *BBTerm = BB->getTerminator();
302
Dan Gohmane49a13e2010-06-07 20:19:26 +0000303 if (!BB->getTerminator()->getType()->isVoidTy())
Owen Andersona7235ea2009-07-31 20:28:14 +0000304 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
Chris Lattner8bc098b2003-11-22 02:10:38 +0000305
Chris Lattnere78109e2008-04-28 00:04:58 +0000306 // Replace the old terminator instruction.
Chris Lattner286921e2003-04-24 23:51:38 +0000307 BB->getInstList().pop_back();
Owen Anderson1d0be152009-08-13 21:58:54 +0000308 new UnreachableInst(BB->getContext(), BB);
Chris Lattner286921e2003-04-24 23:51:38 +0000309 }
310
311 // The CFG Simplifier pass may delete one of the basic blocks we are
312 // interested in. If it does we need to take the block out of the list. Make
313 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
314 // This won't work well if blocks are unnamed, but that is just the risk we
315 // have to take.
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000316 std::vector<std::pair<std::string, std::string> > BlockInfo;
Chris Lattner286921e2003-04-24 23:51:38 +0000317
Nick Lewyckye0325902009-04-04 09:39:23 +0000318 for (SmallPtrSet<BasicBlock*, 8>::iterator I = Blocks.begin(),
319 E = Blocks.end(); I != E; ++I)
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000320 BlockInfo.push_back(std::make_pair((*I)->getParent()->getName(),
321 (*I)->getName()));
Chris Lattner286921e2003-04-24 23:51:38 +0000322
323 // Now run the CFG simplify pass on the function...
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000324 std::vector<std::string> Passes;
325 Passes.push_back("simplifycfg");
326 Passes.push_back("verify");
327 Module *New = BD.runPassesOn(M, Passes);
328 delete M;
329 if (!New) {
330 errs() << "simplifycfg failed!\n";
331 exit(1);
332 }
333 M = New;
Chris Lattner286921e2003-04-24 23:51:38 +0000334
335 // Try running on the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000336 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000337 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattner286921e2003-04-24 23:51:38 +0000338
339 // Make sure to use basic block pointers that point into the now-current
340 // module, and that they don't include any deleted blocks.
341 BBs.clear();
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000342 const ValueSymbolTable &GST = M->getValueSymbolTable();
Chris Lattner286921e2003-04-24 23:51:38 +0000343 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000344 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first));
345 ValueSymbolTable &ST = F->getValueSymbolTable();
Reid Spenceref9b9a72007-02-05 20:47:22 +0000346 Value* V = ST.lookup(BlockInfo[i].second);
Owen Anderson1d0be152009-08-13 21:58:54 +0000347 if (V && V->getType() == Type::getLabelTy(V->getContext()))
Reid Spenceref9b9a72007-02-05 20:47:22 +0000348 BBs.push_back(cast<BasicBlock>(V));
Chris Lattner286921e2003-04-24 23:51:38 +0000349 }
350 return true;
351 }
Chris Lattner06905db2004-02-18 21:24:48 +0000352 delete M; // It didn't crash, try something else.
Chris Lattner286921e2003-04-24 23:51:38 +0000353 return false;
354}
355
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000356namespace {
357 /// ReduceCrashingInstructions reducer - This works by removing the specified
358 /// non-terminator instructions and replacing them with undef.
359 ///
360 class ReduceCrashingInstructions : public ListReducer<const Instruction*> {
361 BugDriver &BD;
Rafael Espindola248d1c62010-08-05 03:00:22 +0000362 bool (*TestFn)(const BugDriver &, Module *);
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000363 public:
Rafael Espindola248d1c62010-08-05 03:00:22 +0000364 ReduceCrashingInstructions(BugDriver &bd,
365 bool (*testFn)(const BugDriver &, Module *))
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000366 : BD(bd), TestFn(testFn) {}
367
368 virtual TestResult doTest(std::vector<const Instruction*> &Prefix,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000369 std::vector<const Instruction*> &Kept,
370 std::string &Error) {
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000371 if (!Kept.empty() && TestInsts(Kept))
372 return KeepSuffix;
373 if (!Prefix.empty() && TestInsts(Prefix))
374 return KeepPrefix;
375 return NoFailure;
376 }
377
378 bool TestInsts(std::vector<const Instruction*> &Prefix);
379 };
380}
381
382bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
383 &Insts) {
384 // Clone the program to try hacking it apart...
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000385 ValueToValueMapTy VMap;
Devang Patele9916a32010-06-24 00:33:28 +0000386 Module *M = CloneModule(BD.getProgram(), VMap);
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000387
388 // Convert list to set for fast lookup...
389 SmallPtrSet<Instruction*, 64> Instructions;
390 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
391 assert(!isa<TerminatorInst>(Insts[i]));
Devang Patele9916a32010-06-24 00:33:28 +0000392 Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000393 }
394
Dan Gohmanac95cc72009-07-16 15:30:09 +0000395 outs() << "Checking for crash with only " << Instructions.size();
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000396 if (Instructions.size() == 1)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000397 outs() << " instruction: ";
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000398 else
Dan Gohmanac95cc72009-07-16 15:30:09 +0000399 outs() << " instructions: ";
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000400
401 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
402 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
403 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
404 Instruction *Inst = I++;
Eli Friedman597362a2011-11-01 04:40:56 +0000405 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
406 !isa<LandingPadInst>(Inst)) {
Dan Gohmane49a13e2010-06-07 20:19:26 +0000407 if (!Inst->getType()->isVoidTy())
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000408 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
409 Inst->eraseFromParent();
410 }
411 }
412
413 // Verify that this is still valid.
414 PassManager Passes;
415 Passes.add(createVerifierPass());
416 Passes.run(*M);
417
418 // Try running on the hacked up program...
419 if (TestFn(BD, M)) {
420 BD.setNewProgram(M); // It crashed, keep the trimmed version...
421
422 // Make sure to use instruction pointers that point into the now-current
423 // module, and that they don't include any deleted blocks.
424 Insts.clear();
425 for (SmallPtrSet<Instruction*, 64>::const_iterator I = Instructions.begin(),
426 E = Instructions.end(); I != E; ++I)
427 Insts.push_back(*I);
428 return true;
429 }
430 delete M; // It didn't crash, try something else.
431 return false;
432}
433
Chris Lattner8b189272004-02-18 23:26:28 +0000434/// DebugACrash - Given a predicate that determines whether a component crashes
435/// on a program, try to destructively reduce the program while still keeping
436/// the predicate true.
Rafael Espindola248d1c62010-08-05 03:00:22 +0000437static bool DebugACrash(BugDriver &BD,
438 bool (*TestFn)(const BugDriver &, Module *),
Nick Lewycky22ff7482010-04-12 05:08:25 +0000439 std::string &Error) {
Bill Wendling4e3be892006-10-25 18:36:14 +0000440 // See if we can get away with nuking some of the global variable initializers
Chris Lattner5f73e382003-04-25 00:53:05 +0000441 // in the program...
Torok Edwin23d471f2009-05-24 09:31:04 +0000442 if (!NoGlobalRM &&
443 BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
Bill Wendling4e3be892006-10-25 18:36:14 +0000444 // Now try to reduce the number of global variable initializers in the
445 // module to something small.
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000446 Module *M = CloneModule(BD.getProgram());
447 bool DeletedInit = false;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000448
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000449 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
450 I != E; ++I)
451 if (I->hasInitializer()) {
452 I->setInitializer(0);
453 I->setLinkage(GlobalValue::ExternalLinkage);
454 DeletedInit = true;
455 }
Bill Wendling4e3be892006-10-25 18:36:14 +0000456
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000457 if (!DeletedInit) {
458 delete M; // No change made...
459 } else {
460 // See if the program still causes a crash...
Dan Gohmanac95cc72009-07-16 15:30:09 +0000461 outs() << "\nChecking to see if we can delete global inits: ";
Bill Wendling4e3be892006-10-25 18:36:14 +0000462
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000463 if (TestFn(BD, M)) { // Still crashes?
464 BD.setNewProgram(M);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000465 outs() << "\n*** Able to remove all global initializers!\n";
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000466 } else { // No longer crashes?
Dan Gohmanac95cc72009-07-16 15:30:09 +0000467 outs() << " - Removing all global inits hides problem!\n";
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000468 delete M;
Bill Wendling4e3be892006-10-25 18:36:14 +0000469
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000470 std::vector<GlobalVariable*> GVs;
471
472 for (Module::global_iterator I = BD.getProgram()->global_begin(),
473 E = BD.getProgram()->global_end(); I != E; ++I)
474 if (I->hasInitializer())
475 GVs.push_back(I);
476
477 if (GVs.size() > 1 && !BugpointIsInterrupted) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000478 outs() << "\n*** Attempting to reduce the number of global "
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000479 << "variables in the testcase\n";
480
481 unsigned OldSize = GVs.size();
Nick Lewycky22ff7482010-04-12 05:08:25 +0000482 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
483 if (!Error.empty())
484 return true;
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000485
486 if (GVs.size() < OldSize)
Rafael Espindolabae1b712010-07-28 18:12:30 +0000487 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
Bill Wendlingb3d83a32006-10-27 20:18:06 +0000488 }
Bill Wendling22706e82006-10-27 20:22:04 +0000489 }
Chris Lattner5f73e382003-04-25 00:53:05 +0000490 }
491 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000492
Chris Lattneraae33f92003-04-24 22:24:58 +0000493 // Now try to reduce the number of functions in the module to something small.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000494 std::vector<Function*> Functions;
495 for (Module::iterator I = BD.getProgram()->begin(),
Chris Lattner8b189272004-02-18 23:26:28 +0000496 E = BD.getProgram()->end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000497 if (!I->isDeclaration())
Chris Lattneraae33f92003-04-24 22:24:58 +0000498 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000499
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000500 if (Functions.size() > 1 && !BugpointIsInterrupted) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000501 outs() << "\n*** Attempting to reduce the number of functions "
Chris Lattneraae33f92003-04-24 22:24:58 +0000502 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000503
Chris Lattner8b189272004-02-18 23:26:28 +0000504 unsigned OldSize = Functions.size();
Nick Lewycky22ff7482010-04-12 05:08:25 +0000505 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
Chris Lattnerafade922002-11-20 22:28:10 +0000506
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000507 if (Functions.size() < OldSize)
Rafael Espindolabae1b712010-07-28 18:12:30 +0000508 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
Chris Lattnerafade922002-11-20 22:28:10 +0000509 }
510
Chris Lattner286921e2003-04-24 23:51:38 +0000511 // Attempt to delete entire basic blocks at a time to speed up
512 // convergence... this actually works by setting the terminator of the blocks
513 // to a return instruction then running simplifycfg, which can potentially
514 // shrinks the code dramatically quickly
515 //
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000516 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
Chris Lattner8b189272004-02-18 23:26:28 +0000517 std::vector<const BasicBlock*> Blocks;
518 for (Module::const_iterator I = BD.getProgram()->begin(),
519 E = BD.getProgram()->end(); I != E; ++I)
520 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
Chris Lattner47ae4a12003-08-05 15:51:05 +0000521 Blocks.push_back(FI);
Torok Edwin2c235012009-05-24 09:40:47 +0000522 unsigned OldSize = Blocks.size();
Nick Lewycky22ff7482010-04-12 05:08:25 +0000523 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
Torok Edwin2c235012009-05-24 09:40:47 +0000524 if (Blocks.size() < OldSize)
Rafael Espindolabae1b712010-07-28 18:12:30 +0000525 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
Chris Lattner47ae4a12003-08-05 15:51:05 +0000526 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000527
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000528 // Attempt to delete instructions using bisection. This should help out nasty
529 // cases with large basic blocks where the problem is at one end.
530 if (!BugpointIsInterrupted) {
531 std::vector<const Instruction*> Insts;
532 for (Module::const_iterator MI = BD.getProgram()->begin(),
533 ME = BD.getProgram()->end(); MI != ME; ++MI)
534 for (Function::const_iterator FI = MI->begin(), FE = MI->end(); FI != FE;
535 ++FI)
536 for (BasicBlock::const_iterator I = FI->begin(), E = FI->end();
537 I != E; ++I)
538 if (!isa<TerminatorInst>(I))
539 Insts.push_back(I);
540
Nick Lewycky22ff7482010-04-12 05:08:25 +0000541 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
Nick Lewycky4c8f9af2009-05-25 05:30:00 +0000542 }
543
Chris Lattneraae33f92003-04-24 22:24:58 +0000544 // FIXME: This should use the list reducer to converge faster by deleting
545 // larger chunks of instructions at a time!
Chris Lattnerb2c180f2004-03-13 19:35:54 +0000546 unsigned Simplification = 2;
Chris Lattner65207852003-01-23 02:48:33 +0000547 do {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000548 if (BugpointIsInterrupted) break;
Chris Lattner65207852003-01-23 02:48:33 +0000549 --Simplification;
Dan Gohmanac95cc72009-07-16 15:30:09 +0000550 outs() << "\n*** Attempting to reduce testcase by deleting instruc"
551 << "tions: Simplification Level #" << Simplification << '\n';
Chris Lattner65207852003-01-23 02:48:33 +0000552
Misha Brukman5560c9d2003-08-18 14:43:39 +0000553 // Now that we have deleted the functions that are unnecessary for the
554 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000555 // crash. To do this, we loop through all of the instructions in the
556 // remaining functions, deleting them (replacing any values produced with
557 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
558 // still triggers failure, keep deleting until we cannot trigger failure
559 // anymore.
560 //
Chris Lattnerf66d9062004-02-18 23:59:11 +0000561 unsigned InstructionsToSkipBeforeDeleting = 0;
Chris Lattner65207852003-01-23 02:48:33 +0000562 TryAgain:
Misha Brukman3da94ae2005-04-22 00:00:37 +0000563
Chris Lattner65207852003-01-23 02:48:33 +0000564 // Loop over all of the (non-terminator) instructions remaining in the
565 // function, attempting to delete them.
Chris Lattnerf66d9062004-02-18 23:59:11 +0000566 unsigned CurInstructionNum = 0;
Chris Lattner8b189272004-02-18 23:26:28 +0000567 for (Module::const_iterator FI = BD.getProgram()->begin(),
568 E = BD.getProgram()->end(); FI != E; ++FI)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000569 if (!FI->isDeclaration())
Chris Lattner8b189272004-02-18 23:26:28 +0000570 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
571 ++BI)
572 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
Bill Wendling467ef212011-12-25 06:56:22 +0000573 I != E; ++I, ++CurInstructionNum) {
Chris Lattnerf66d9062004-02-18 23:59:11 +0000574 if (InstructionsToSkipBeforeDeleting) {
575 --InstructionsToSkipBeforeDeleting;
576 } else {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000577 if (BugpointIsInterrupted) goto ExitLoops;
578
Eli Friedman597362a2011-11-01 04:40:56 +0000579 if (isa<LandingPadInst>(I))
580 continue;
581
Dan Gohmanac95cc72009-07-16 15:30:09 +0000582 outs() << "Checking instruction: " << *I;
Chris Lattnerf66d9062004-02-18 23:59:11 +0000583 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000584
Chris Lattnerf66d9062004-02-18 23:59:11 +0000585 // Find out if the pass still crashes on this pass...
586 if (TestFn(BD, M)) {
587 // Yup, it does, we delete the old module, and continue trying
588 // to reduce the testcase...
589 BD.setNewProgram(M);
Chris Lattnerf66d9062004-02-18 23:59:11 +0000590 InstructionsToSkipBeforeDeleting = CurInstructionNum;
591 goto TryAgain; // I wish I had a multi-level break here!
592 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000593
Chris Lattnerf66d9062004-02-18 23:59:11 +0000594 // This pass didn't crash without this instruction, try the next
595 // one.
596 delete M;
Chris Lattner65207852003-01-23 02:48:33 +0000597 }
Bill Wendling467ef212011-12-25 06:56:22 +0000598 }
Chris Lattnerf66d9062004-02-18 23:59:11 +0000599
600 if (InstructionsToSkipBeforeDeleting) {
601 InstructionsToSkipBeforeDeleting = 0;
602 goto TryAgain;
603 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000604
Chris Lattner65207852003-01-23 02:48:33 +0000605 } while (Simplification);
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000606ExitLoops:
Chris Lattnerba386d92003-02-28 16:13:20 +0000607
608 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000609 if (!BugpointIsInterrupted) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000610 outs() << "\n*** Attempting to perform final cleanups: ";
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000611 Module *M = CloneModule(BD.getProgram());
612 M = BD.performFinalCleanups(M, true);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000613
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000614 // Find out if the pass still crashes on the cleaned up program...
615 if (TestFn(BD, M)) {
616 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
617 } else {
618 delete M;
619 }
Chris Lattnerba386d92003-02-28 16:13:20 +0000620 }
621
Rafael Espindolabae1b712010-07-28 18:12:30 +0000622 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000623
Misha Brukman3da94ae2005-04-22 00:00:37 +0000624 return false;
Chris Lattnerafade922002-11-20 22:28:10 +0000625}
Brian Gaeked0fde302003-11-11 22:41:34 +0000626
Rafael Espindola248d1c62010-08-05 03:00:22 +0000627static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
Chris Lattner8b189272004-02-18 23:26:28 +0000628 return BD.runPasses(M);
629}
Chris Lattner02526262004-02-18 21:02:04 +0000630
Chris Lattner8b189272004-02-18 23:26:28 +0000631/// debugOptimizerCrash - This method is called when some pass crashes on input.
632/// It attempts to prune down the testcase to something reasonable, and figure
633/// out exactly which pass is crashing.
634///
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000635bool BugDriver::debugOptimizerCrash(const std::string &ID) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000636 outs() << "\n*** Debugging optimizer crash!\n";
Chris Lattner8b189272004-02-18 23:26:28 +0000637
Nick Lewycky22ff7482010-04-12 05:08:25 +0000638 std::string Error;
Chris Lattner8b189272004-02-18 23:26:28 +0000639 // Reduce the list of passes which causes the optimizer to crash...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000640 if (!BugpointIsInterrupted)
Nick Lewycky22ff7482010-04-12 05:08:25 +0000641 ReducePassList(*this).reduceList(PassesToRun, Error);
642 assert(Error.empty());
Chris Lattner8b189272004-02-18 23:26:28 +0000643
Dan Gohmanac95cc72009-07-16 15:30:09 +0000644 outs() << "\n*** Found crashing pass"
645 << (PassesToRun.size() == 1 ? ": " : "es: ")
646 << getPassesString(PassesToRun) << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000647
Rafael Espindolabae1b712010-07-28 18:12:30 +0000648 EmitProgressBitcode(Program, ID);
Chris Lattner8b189272004-02-18 23:26:28 +0000649
Nick Lewycky22ff7482010-04-12 05:08:25 +0000650 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
651 assert(Error.empty());
652 return Success;
Chris Lattner8b189272004-02-18 23:26:28 +0000653}
654
Rafael Espindola248d1c62010-08-05 03:00:22 +0000655static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000656 std::string Error;
657 BD.compileProgram(M, &Error);
658 if (!Error.empty()) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000659 errs() << "<crash>\n";
Chris Lattner8b189272004-02-18 23:26:28 +0000660 return true; // Tool is still crashing.
661 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000662 errs() << '\n';
663 return false;
Chris Lattner8b189272004-02-18 23:26:28 +0000664}
Chris Lattner02526262004-02-18 21:02:04 +0000665
666/// debugCodeGeneratorCrash - This method is called when the code generator
667/// crashes on an input. It attempts to reduce the input as much as possible
668/// while still causing the code generator to crash.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000669bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000670 errs() << "*** Debugging code generator crash!\n";
Chris Lattner02526262004-02-18 21:02:04 +0000671
Nick Lewycky22ff7482010-04-12 05:08:25 +0000672 return DebugACrash(*this, TestForCodeGenCrash, Error);
Chris Lattner02526262004-02-18 21:02:04 +0000673}