Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 73a6bdd | 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 | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 15 | #include "ListReducer.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "ToolRunner.h" |
| 17 | #include "llvm/ADT/SmallPtrSet.h" |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 19 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/DerivedTypes.h" |
| 22 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 23 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/ValueSymbolTable.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Verifier.h" |
Misha Brukman | 0c2305b | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 32 | #include <set> |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 34 | |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | cl::opt<bool> |
| 37 | KeepMain("keep-main", |
| 38 | cl::desc("Force function reduction to keep main"), |
| 39 | cl::init(false)); |
Torok Edwin | 5bd3f7b | 2009-05-24 09:31:04 +0000 | [diff] [blame] | 40 | cl::opt<bool> |
| 41 | NoGlobalRM ("disable-global-remove", |
| 42 | cl::desc("Do not remove global variables"), |
| 43 | cl::init(false)); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 44 | |
| 45 | cl::opt<bool> |
| 46 | ReplaceFuncsWithNull("replace-funcs-with-null", |
| 47 | cl::desc("When stubbing functions, replace all uses will null"), |
| 48 | cl::init(false)); |
| 49 | cl::opt<bool> |
| 50 | DontReducePassList("disable-pass-list-reduction", |
| 51 | cl::desc("Skip pass list reduction steps"), |
| 52 | cl::init(false)); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 53 | |
| 54 | cl::opt<bool> NoNamedMDRM("disable-namedmd-remove", |
| 55 | cl::desc("Do not remove global named metadata"), |
| 56 | cl::init(false)); |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 59 | namespace llvm { |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 60 | class ReducePassList : public ListReducer<std::string> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 61 | BugDriver &BD; |
| 62 | public: |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 63 | ReducePassList(BugDriver &bd) : BD(bd) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 64 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 65 | // doTest - Return true iff running the "removed" passes succeeds, and |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 66 | // running the "Kept" passes fail when run on the output of the "removed" |
| 67 | // passes. If we return true, we update the current module of bugpoint. |
| 68 | // |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 69 | TestResult doTest(std::vector<std::string> &Removed, |
| 70 | std::vector<std::string> &Kept, |
| 71 | std::string &Error) override; |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 72 | }; |
| 73 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 75 | ReducePassList::TestResult |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 76 | ReducePassList::doTest(std::vector<std::string> &Prefix, |
| 77 | std::vector<std::string> &Suffix, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 78 | std::string &Error) { |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 79 | std::string PrefixOutput; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 80 | Module *OrigProgram = nullptr; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 81 | if (!Prefix.empty()) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 82 | outs() << "Checking to see if these passes crash: " |
| 83 | << getPassesString(Prefix) << ": "; |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 84 | if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput)) |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 85 | return KeepPrefix; |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 86 | |
| 87 | OrigProgram = BD.Program; |
| 88 | |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 89 | BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 90 | if (BD.Program == nullptr) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 91 | errs() << BD.getToolName() << ": Error reading bitcode file '" |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 92 | << PrefixOutput << "'!\n"; |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 93 | exit(1); |
| 94 | } |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 95 | sys::fs::remove(PrefixOutput); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 98 | outs() << "Checking to see if these passes crash: " |
| 99 | << getPassesString(Suffix) << ": "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 100 | |
Rafael Espindola | 37302ea | 2010-08-05 02:16:32 +0000 | [diff] [blame] | 101 | if (BD.runPasses(BD.getProgram(), Suffix)) { |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 102 | delete OrigProgram; // The suffix crashes alone... |
| 103 | return KeepSuffix; |
| 104 | } |
| 105 | |
| 106 | // Nothing failed, restore state... |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 107 | if (OrigProgram) { |
| 108 | delete BD.Program; |
| 109 | BD.Program = OrigProgram; |
| 110 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 111 | return NoFailure; |
| 112 | } |
| 113 | |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 114 | namespace { |
| 115 | /// ReduceCrashingGlobalVariables - This works by removing the global |
| 116 | /// variable's initializer and seeing if the program still crashes. If it |
| 117 | /// does, then we keep that program and try again. |
| 118 | /// |
| 119 | class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> { |
| 120 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 121 | bool (*TestFn)(const BugDriver &, Module *); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 122 | public: |
| 123 | ReduceCrashingGlobalVariables(BugDriver &bd, |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 124 | bool (*testFn)(const BugDriver &, Module *)) |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 125 | : BD(bd), TestFn(testFn) {} |
| 126 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 127 | TestResult doTest(std::vector<GlobalVariable*> &Prefix, |
| 128 | std::vector<GlobalVariable*> &Kept, |
| 129 | std::string &Error) override { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 130 | if (!Kept.empty() && TestGlobalVariables(Kept)) |
| 131 | return KeepSuffix; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 132 | if (!Prefix.empty() && TestGlobalVariables(Prefix)) |
| 133 | return KeepPrefix; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 134 | return NoFailure; |
| 135 | } |
| 136 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 137 | bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 138 | }; |
| 139 | } |
| 140 | |
| 141 | bool |
| 142 | ReduceCrashingGlobalVariables::TestGlobalVariables( |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 143 | std::vector<GlobalVariable*> &GVs) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 144 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 145 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 146 | Module *M = CloneModule(BD.getProgram(), VMap).release(); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 147 | |
| 148 | // Convert list to set for fast lookup... |
| 149 | std::set<GlobalVariable*> GVSet; |
| 150 | |
| 151 | for (unsigned i = 0, e = GVs.size(); i != e; ++i) { |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 152 | GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 153 | assert(CMGV && "Global Variable not in module?!"); |
| 154 | GVSet.insert(CMGV); |
| 155 | } |
| 156 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 157 | outs() << "Checking for crash with only these global variables: "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 158 | PrintGlobalVariableList(GVs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 159 | outs() << ": "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 160 | |
| 161 | // Loop over and delete any global variables which we aren't supposed to be |
| 162 | // playing with... |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 163 | for (GlobalVariable &I : M->globals()) |
| 164 | if (I.hasInitializer() && !GVSet.count(&I)) { |
Hal Finkel | 28ad2b4 | 2015-11-26 19:23:49 +0000 | [diff] [blame] | 165 | DeleteGlobalInitializer(&I); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 166 | I.setLinkage(GlobalValue::ExternalLinkage); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Try running the hacked up program... |
| 170 | if (TestFn(BD, M)) { |
| 171 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 172 | |
| 173 | // Make sure to use global variable pointers that point into the now-current |
| 174 | // module. |
| 175 | GVs.assign(GVSet.begin(), GVSet.end()); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | delete M; |
| 180 | return false; |
| 181 | } |
| 182 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 183 | namespace { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 184 | /// ReduceCrashingFunctions reducer - This works by removing functions and |
| 185 | /// seeing if the program still crashes. If it does, then keep the newer, |
| 186 | /// smaller program. |
| 187 | /// |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 188 | class ReduceCrashingFunctions : public ListReducer<Function*> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 189 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 190 | bool (*TestFn)(const BugDriver &, Module *); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 191 | public: |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 192 | ReduceCrashingFunctions(BugDriver &bd, |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 193 | bool (*testFn)(const BugDriver &, Module *)) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 194 | : BD(bd), TestFn(testFn) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 195 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 196 | TestResult doTest(std::vector<Function*> &Prefix, |
| 197 | std::vector<Function*> &Kept, |
| 198 | std::string &Error) override { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 199 | if (!Kept.empty() && TestFuncs(Kept)) |
| 200 | return KeepSuffix; |
| 201 | if (!Prefix.empty() && TestFuncs(Prefix)) |
| 202 | return KeepPrefix; |
| 203 | return NoFailure; |
| 204 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 205 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 206 | bool TestFuncs(std::vector<Function*> &Prefix); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 207 | }; |
| 208 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 209 | |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 210 | static void RemoveFunctionReferences(Module *M, const char* Name) { |
| 211 | auto *UsedVar = M->getGlobalVariable(Name, true); |
| 212 | if (!UsedVar || !UsedVar->hasInitializer()) return; |
| 213 | if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) { |
| 214 | assert(UsedVar->use_empty()); |
| 215 | UsedVar->eraseFromParent(); |
| 216 | return; |
| 217 | } |
| 218 | auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer()); |
| 219 | std::vector<Constant*> Used; |
| 220 | for(Value *V : OldUsedVal->operand_values()) { |
| 221 | Constant *Op = cast<Constant>(V->stripPointerCasts()); |
| 222 | if(!Op->isNullValue()) { |
| 223 | Used.push_back(cast<Constant>(V)); |
| 224 | } |
| 225 | } |
| 226 | auto *NewValElemTy = OldUsedVal->getType()->getElementType(); |
| 227 | auto *NewValTy = ArrayType::get(NewValElemTy, Used.size()); |
| 228 | auto *NewUsedVal = ConstantArray::get(NewValTy, Used); |
| 229 | UsedVar->mutateType(NewUsedVal->getType()->getPointerTo()); |
| 230 | UsedVar->setInitializer(NewUsedVal); |
| 231 | } |
| 232 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 233 | bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) { |
Dmitri Gribenko | 6e0520e | 2013-09-02 01:18:56 +0000 | [diff] [blame] | 234 | // If main isn't present, claim there is no problem. |
| 235 | if (KeepMain && std::find(Funcs.begin(), Funcs.end(), |
| 236 | BD.getProgram()->getFunction("main")) == |
| 237 | Funcs.end()) |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 238 | return false; |
| 239 | |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 240 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 241 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 242 | Module *M = CloneModule(BD.getProgram(), VMap).release(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 244 | // Convert list to set for fast lookup... |
| 245 | std::set<Function*> Functions; |
| 246 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 247 | Function *CMF = cast<Function>(VMap[Funcs[i]]); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 248 | assert(CMF && "Function not in module?!"); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 249 | assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty"); |
Dan Gohman | bcad718 | 2009-03-06 02:16:23 +0000 | [diff] [blame] | 250 | assert(CMF->getName() == Funcs[i]->getName() && "wrong name"); |
Chris Lattner | de39f2b | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 251 | Functions.insert(CMF); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 254 | outs() << "Checking for crash with only these functions: "; |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 255 | PrintFunctionList(Funcs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 256 | outs() << ": "; |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 257 | if (!ReplaceFuncsWithNull) { |
| 258 | // Loop over and delete any functions which we aren't supposed to be playing |
| 259 | // with... |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 260 | for (Function &I : *M) |
| 261 | if (!I.isDeclaration() && !Functions.count(&I)) |
| 262 | DeleteFunctionBody(&I); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 263 | } else { |
| 264 | std::vector<GlobalValue*> ToRemove; |
| 265 | // First, remove aliases to functions we're about to purge. |
| 266 | for (GlobalAlias &Alias : M->aliases()) { |
| 267 | Constant *Root = Alias.getAliasee()->stripPointerCasts(); |
| 268 | Function *F = dyn_cast<Function>(Root); |
| 269 | if (F) { |
| 270 | if (Functions.count(F)) |
| 271 | // We're keeping this function. |
| 272 | continue; |
| 273 | } else if (Root->isNullValue()) { |
| 274 | // This referenced a globalalias that we've already replaced, |
| 275 | // so we still need to replace this alias. |
| 276 | } else if (!F) { |
| 277 | // Not a function, therefore not something we mess with. |
| 278 | continue; |
| 279 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 280 | |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 281 | PointerType *Ty = cast<PointerType>(Alias.getType()); |
| 282 | Constant *Replacement = ConstantPointerNull::get(Ty); |
| 283 | Alias.replaceAllUsesWith(Replacement); |
| 284 | ToRemove.push_back(&Alias); |
| 285 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 286 | |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 287 | for (Function &I : *M) { |
| 288 | if (!I.isDeclaration() && !Functions.count(&I)) { |
| 289 | PointerType *Ty = cast<PointerType>(I.getType()); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 290 | Constant *Replacement = ConstantPointerNull::get(Ty); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 291 | I.replaceAllUsesWith(Replacement); |
| 292 | ToRemove.push_back(&I); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | for (auto *F : ToRemove) { |
| 297 | F->eraseFromParent(); |
| 298 | } |
| 299 | |
| 300 | // Finally, remove any null members from any global intrinsic. |
| 301 | RemoveFunctionReferences(M, "llvm.used"); |
| 302 | RemoveFunctionReferences(M, "llvm.compiler.used"); |
| 303 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 304 | // Try running the hacked up program... |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 305 | if (TestFn(BD, M)) { |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 306 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 307 | |
| 308 | // Make sure to use function pointers that point into the now-current |
| 309 | // module. |
| 310 | Funcs.assign(Functions.begin(), Functions.end()); |
| 311 | return true; |
| 312 | } |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 313 | delete M; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 314 | return false; |
| 315 | } |
| 316 | |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 1942f98 | 2004-02-18 21:29:46 +0000 | [diff] [blame] | 318 | namespace { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 319 | /// ReduceCrashingBlocks reducer - This works by setting the terminators of |
| 320 | /// all terminators except the specified basic blocks to a 'ret' instruction, |
| 321 | /// then running the simplify-cfg pass. This has the effect of chopping up |
| 322 | /// the CFG really fast which can reduce large functions quickly. |
| 323 | /// |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 324 | class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 325 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 326 | bool (*TestFn)(const BugDriver &, Module *); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 327 | public: |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 328 | ReduceCrashingBlocks(BugDriver &bd, |
| 329 | bool (*testFn)(const BugDriver &, Module *)) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 330 | : BD(bd), TestFn(testFn) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 331 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 332 | TestResult doTest(std::vector<const BasicBlock*> &Prefix, |
| 333 | std::vector<const BasicBlock*> &Kept, |
| 334 | std::string &Error) override { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 335 | if (!Kept.empty() && TestBlocks(Kept)) |
| 336 | return KeepSuffix; |
| 337 | if (!Prefix.empty() && TestBlocks(Prefix)) |
| 338 | return KeepPrefix; |
| 339 | return NoFailure; |
| 340 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 342 | bool TestBlocks(std::vector<const BasicBlock*> &Prefix); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 343 | }; |
| 344 | } |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 346 | bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) { |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 347 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 348 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 349 | Module *M = CloneModule(BD.getProgram(), VMap).release(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 350 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 351 | // Convert list to set for fast lookup... |
Nick Lewycky | b294e31 | 2009-04-04 09:39:23 +0000 | [diff] [blame] | 352 | SmallPtrSet<BasicBlock*, 8> Blocks; |
| 353 | for (unsigned i = 0, e = BBs.size(); i != e; ++i) |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 354 | Blocks.insert(cast<BasicBlock>(VMap[BBs[i]])); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 355 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 356 | outs() << "Checking for crash with only these blocks:"; |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 357 | unsigned NumPrint = Blocks.size(); |
| 358 | if (NumPrint > 10) NumPrint = 10; |
| 359 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 360 | outs() << " " << BBs[i]->getName(); |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 361 | if (NumPrint < Blocks.size()) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 362 | outs() << "... <" << Blocks.size() << " total>"; |
| 363 | outs() << ": "; |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 364 | |
| 365 | // Loop over and delete any hack up any blocks that are not listed... |
| 366 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 367 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 368 | if (!Blocks.count(&*BB) && BB->getTerminator()->getNumSuccessors()) { |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 369 | // Loop over all of the successors of this block, deleting any PHI nodes |
| 370 | // that might include it. |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 371 | for (succ_iterator SI = succ_begin(&*BB), E = succ_end(&*BB); SI != E; |
| 372 | ++SI) |
| 373 | (*SI)->removePredecessor(&*BB); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 7855a5c | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 375 | TerminatorInst *BBTerm = BB->getTerminator(); |
David Majnemer | 189d7da | 2015-11-08 04:16:12 +0000 | [diff] [blame] | 376 | if (BBTerm->isEHPad()) |
| 377 | continue; |
| 378 | if (!BBTerm->getType()->isVoidTy() && !BBTerm->getType()->isTokenTy()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 379 | BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType())); |
Chris Lattner | 7b233af | 2003-11-22 02:10:38 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 7855a5c | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 381 | // Replace the old terminator instruction. |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 382 | BB->getInstList().pop_back(); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 383 | new UnreachableInst(BB->getContext(), &*BB); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // The CFG Simplifier pass may delete one of the basic blocks we are |
| 387 | // interested in. If it does we need to take the block out of the list. Make |
| 388 | // a "persistent mapping" by turning basic blocks into <function, name> pairs. |
| 389 | // This won't work well if blocks are unnamed, but that is just the risk we |
| 390 | // have to take. |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 391 | std::vector<std::pair<std::string, std::string> > BlockInfo; |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 392 | |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 393 | for (BasicBlock *BB : Blocks) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 394 | BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName()); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 395 | |
| 396 | // Now run the CFG simplify pass on the function... |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 397 | std::vector<std::string> Passes; |
| 398 | Passes.push_back("simplifycfg"); |
| 399 | Passes.push_back("verify"); |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 400 | std::unique_ptr<Module> New = BD.runPassesOn(M, Passes); |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 401 | delete M; |
| 402 | if (!New) { |
| 403 | errs() << "simplifycfg failed!\n"; |
| 404 | exit(1); |
| 405 | } |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 406 | M = New.release(); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 407 | |
| 408 | // Try running on the hacked up program... |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 409 | if (TestFn(BD, M)) { |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 410 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 411 | |
| 412 | // Make sure to use basic block pointers that point into the now-current |
| 413 | // module, and that they don't include any deleted blocks. |
| 414 | BBs.clear(); |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 415 | const ValueSymbolTable &GST = M->getValueSymbolTable(); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 416 | for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 417 | Function *F = cast<Function>(GST.lookup(BlockInfo[i].first)); |
| 418 | ValueSymbolTable &ST = F->getValueSymbolTable(); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 419 | Value* V = ST.lookup(BlockInfo[i].second); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 420 | if (V && V->getType() == Type::getLabelTy(V->getContext())) |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 421 | BBs.push_back(cast<BasicBlock>(V)); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 422 | } |
| 423 | return true; |
| 424 | } |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 425 | delete M; // It didn't crash, try something else. |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 429 | namespace { |
| 430 | /// ReduceCrashingInstructions reducer - This works by removing the specified |
| 431 | /// non-terminator instructions and replacing them with undef. |
| 432 | /// |
| 433 | class ReduceCrashingInstructions : public ListReducer<const Instruction*> { |
| 434 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 435 | bool (*TestFn)(const BugDriver &, Module *); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 436 | public: |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 437 | ReduceCrashingInstructions(BugDriver &bd, |
| 438 | bool (*testFn)(const BugDriver &, Module *)) |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 439 | : BD(bd), TestFn(testFn) {} |
| 440 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 441 | TestResult doTest(std::vector<const Instruction*> &Prefix, |
| 442 | std::vector<const Instruction*> &Kept, |
| 443 | std::string &Error) override { |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 444 | if (!Kept.empty() && TestInsts(Kept)) |
| 445 | return KeepSuffix; |
| 446 | if (!Prefix.empty() && TestInsts(Prefix)) |
| 447 | return KeepPrefix; |
| 448 | return NoFailure; |
| 449 | } |
| 450 | |
| 451 | bool TestInsts(std::vector<const Instruction*> &Prefix); |
| 452 | }; |
| 453 | } |
| 454 | |
| 455 | bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*> |
| 456 | &Insts) { |
| 457 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 458 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 459 | Module *M = CloneModule(BD.getProgram(), VMap).release(); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 460 | |
| 461 | // Convert list to set for fast lookup... |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 462 | SmallPtrSet<Instruction*, 32> Instructions; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 463 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 464 | assert(!isa<TerminatorInst>(Insts[i])); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 465 | Instructions.insert(cast<Instruction>(VMap[Insts[i]])); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 468 | outs() << "Checking for crash with only " << Instructions.size(); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 469 | if (Instructions.size() == 1) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 470 | outs() << " instruction: "; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 471 | else |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 472 | outs() << " instructions: "; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 473 | |
| 474 | for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) |
| 475 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) |
| 476 | for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 477 | Instruction *Inst = &*I++; |
Eli Friedman | d4e02a5 | 2011-11-01 04:40:56 +0000 | [diff] [blame] | 478 | if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) && |
David Majnemer | ba275f9 | 2015-08-19 19:54:02 +0000 | [diff] [blame] | 479 | !Inst->isEHPad()) { |
David Majnemer | 189d7da | 2015-11-08 04:16:12 +0000 | [diff] [blame] | 480 | if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy()) |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 481 | Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); |
| 482 | Inst->eraseFromParent(); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // Verify that this is still valid. |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 487 | legacy::PassManager Passes; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 488 | Passes.add(createVerifierPass()); |
| 489 | Passes.run(*M); |
| 490 | |
| 491 | // Try running on the hacked up program... |
| 492 | if (TestFn(BD, M)) { |
| 493 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 494 | |
| 495 | // Make sure to use instruction pointers that point into the now-current |
| 496 | // module, and that they don't include any deleted blocks. |
| 497 | Insts.clear(); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 498 | for (Instruction *Inst : Instructions) |
| 499 | Insts.push_back(Inst); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 500 | return true; |
| 501 | } |
| 502 | delete M; // It didn't crash, try something else. |
| 503 | return false; |
| 504 | } |
| 505 | |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 506 | namespace { |
| 507 | // Reduce the list of Named Metadata nodes. We keep this as a list of |
| 508 | // names to avoid having to convert back and forth every time. |
| 509 | class ReduceCrashingNamedMD : public ListReducer<std::string> { |
| 510 | BugDriver &BD; |
| 511 | bool (*TestFn)(const BugDriver &, Module *); |
| 512 | |
| 513 | public: |
| 514 | ReduceCrashingNamedMD(BugDriver &bd, |
| 515 | bool (*testFn)(const BugDriver &, Module *)) |
| 516 | : BD(bd), TestFn(testFn) {} |
| 517 | |
| 518 | TestResult doTest(std::vector<std::string> &Prefix, |
| 519 | std::vector<std::string> &Kept, |
| 520 | std::string &Error) override { |
| 521 | if (!Kept.empty() && TestNamedMDs(Kept)) |
| 522 | return KeepSuffix; |
| 523 | if (!Prefix.empty() && TestNamedMDs(Prefix)) |
| 524 | return KeepPrefix; |
| 525 | return NoFailure; |
| 526 | } |
| 527 | |
| 528 | bool TestNamedMDs(std::vector<std::string> &NamedMDs); |
| 529 | }; |
| 530 | } |
| 531 | |
| 532 | bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &NamedMDs) { |
| 533 | |
| 534 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 535 | Module *M = CloneModule(BD.getProgram(), VMap).release(); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 536 | |
| 537 | outs() << "Checking for crash with only these named metadata nodes:"; |
| 538 | unsigned NumPrint = std::min<size_t>(NamedMDs.size(), 10); |
| 539 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
| 540 | outs() << " " << NamedMDs[i]; |
| 541 | if (NumPrint < NamedMDs.size()) |
| 542 | outs() << "... <" << NamedMDs.size() << " total>"; |
| 543 | outs() << ": "; |
| 544 | |
| 545 | // Make a StringMap for faster lookup |
| 546 | StringSet<> Names; |
| 547 | for (const std::string &Name : NamedMDs) |
| 548 | Names.insert(Name); |
| 549 | |
| 550 | // First collect all the metadata to delete in a vector, then |
| 551 | // delete them all at once to avoid invalidating the iterator |
| 552 | std::vector<NamedMDNode *> ToDelete; |
| 553 | ToDelete.reserve(M->named_metadata_size() - Names.size()); |
| 554 | for (auto &NamedMD : M->named_metadata()) |
Adrian Prantl | faebbb0 | 2016-03-28 21:06:26 +0000 | [diff] [blame] | 555 | // Always keep a nonempty llvm.dbg.cu because the Verifier would complain. |
| 556 | if (!Names.count(NamedMD.getName()) && |
| 557 | (!(NamedMD.getName() == "llvm.dbg.cu" && NamedMD.getNumOperands() > 0))) |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 558 | ToDelete.push_back(&NamedMD); |
| 559 | |
| 560 | for (auto *NamedMD : ToDelete) |
| 561 | NamedMD->eraseFromParent(); |
| 562 | |
| 563 | // Verify that this is still valid. |
| 564 | legacy::PassManager Passes; |
| 565 | Passes.add(createVerifierPass()); |
| 566 | Passes.run(*M); |
| 567 | |
| 568 | // Try running on the hacked up program... |
| 569 | if (TestFn(BD, M)) { |
| 570 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 571 | return true; |
| 572 | } |
| 573 | delete M; // It didn't crash, try something else. |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | namespace { |
| 578 | // Reduce the list of operands to named metadata nodes |
| 579 | class ReduceCrashingNamedMDOps : public ListReducer<const MDNode *> { |
| 580 | BugDriver &BD; |
| 581 | bool (*TestFn)(const BugDriver &, Module *); |
| 582 | |
| 583 | public: |
| 584 | ReduceCrashingNamedMDOps(BugDriver &bd, |
| 585 | bool (*testFn)(const BugDriver &, Module *)) |
| 586 | : BD(bd), TestFn(testFn) {} |
| 587 | |
| 588 | TestResult doTest(std::vector<const MDNode *> &Prefix, |
| 589 | std::vector<const MDNode *> &Kept, |
| 590 | std::string &Error) override { |
| 591 | if (!Kept.empty() && TestNamedMDOps(Kept)) |
| 592 | return KeepSuffix; |
| 593 | if (!Prefix.empty() && TestNamedMDOps(Prefix)) |
| 594 | return KeepPrefix; |
| 595 | return NoFailure; |
| 596 | } |
| 597 | |
| 598 | bool TestNamedMDOps(std::vector<const MDNode *> &NamedMDOps); |
| 599 | }; |
| 600 | } |
| 601 | |
| 602 | bool ReduceCrashingNamedMDOps::TestNamedMDOps( |
| 603 | std::vector<const MDNode *> &NamedMDOps) { |
| 604 | // Convert list to set for fast lookup... |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 605 | SmallPtrSet<const MDNode *, 32> OldMDNodeOps; |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 606 | for (unsigned i = 0, e = NamedMDOps.size(); i != e; ++i) { |
| 607 | OldMDNodeOps.insert(NamedMDOps[i]); |
| 608 | } |
| 609 | |
| 610 | outs() << "Checking for crash with only " << OldMDNodeOps.size(); |
| 611 | if (OldMDNodeOps.size() == 1) |
| 612 | outs() << " named metadata operand: "; |
| 613 | else |
| 614 | outs() << " named metadata operands: "; |
| 615 | |
| 616 | ValueToValueMapTy VMap; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 617 | Module *M = CloneModule(BD.getProgram(), VMap).release(); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 618 | |
| 619 | // This is a little wasteful. In the future it might be good if we could have |
| 620 | // these dropped during cloning. |
| 621 | for (auto &NamedMD : BD.getProgram()->named_metadata()) { |
| 622 | // Drop the old one and create a new one |
| 623 | M->eraseNamedMetadata(M->getNamedMetadata(NamedMD.getName())); |
| 624 | NamedMDNode *NewNamedMDNode = |
| 625 | M->getOrInsertNamedMetadata(NamedMD.getName()); |
| 626 | for (MDNode *op : NamedMD.operands()) |
| 627 | if (OldMDNodeOps.count(op)) |
| 628 | NewNamedMDNode->addOperand(cast<MDNode>(MapMetadata(op, VMap))); |
| 629 | } |
| 630 | |
| 631 | // Verify that this is still valid. |
| 632 | legacy::PassManager Passes; |
| 633 | Passes.add(createVerifierPass()); |
| 634 | Passes.run(*M); |
| 635 | |
| 636 | // Try running on the hacked up program... |
| 637 | if (TestFn(BD, M)) { |
| 638 | // Make sure to use instruction pointers that point into the now-current |
| 639 | // module, and that they don't include any deleted blocks. |
| 640 | NamedMDOps.clear(); |
| 641 | for (const MDNode *Node : OldMDNodeOps) |
Duncan P. N. Exon Smith | da4a56d | 2016-04-02 17:04:38 +0000 | [diff] [blame^] | 642 | NamedMDOps.push_back(cast<MDNode>(*VMap.getMappedMD(Node))); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 643 | |
| 644 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 645 | return true; |
| 646 | } |
| 647 | delete M; // It didn't crash, try something else. |
| 648 | return false; |
| 649 | } |
| 650 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 651 | /// DebugACrash - Given a predicate that determines whether a component crashes |
| 652 | /// on a program, try to destructively reduce the program while still keeping |
| 653 | /// the predicate true. |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 654 | static bool DebugACrash(BugDriver &BD, |
| 655 | bool (*TestFn)(const BugDriver &, Module *), |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 656 | std::string &Error) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 657 | // See if we can get away with nuking some of the global variable initializers |
Chris Lattner | 65e5f65 | 2003-04-25 00:53:05 +0000 | [diff] [blame] | 658 | // in the program... |
Torok Edwin | 5bd3f7b | 2009-05-24 09:31:04 +0000 | [diff] [blame] | 659 | if (!NoGlobalRM && |
| 660 | BD.getProgram()->global_begin() != BD.getProgram()->global_end()) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 661 | // Now try to reduce the number of global variable initializers in the |
| 662 | // module to something small. |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 663 | Module *M = CloneModule(BD.getProgram()).release(); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 664 | bool DeletedInit = false; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 665 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 666 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 667 | I != E; ++I) |
| 668 | if (I->hasInitializer()) { |
Hal Finkel | 28ad2b4 | 2015-11-26 19:23:49 +0000 | [diff] [blame] | 669 | DeleteGlobalInitializer(&*I); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 670 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 671 | DeletedInit = true; |
| 672 | } |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 673 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 674 | if (!DeletedInit) { |
| 675 | delete M; // No change made... |
| 676 | } else { |
| 677 | // See if the program still causes a crash... |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 678 | outs() << "\nChecking to see if we can delete global inits: "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 679 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 680 | if (TestFn(BD, M)) { // Still crashes? |
| 681 | BD.setNewProgram(M); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 682 | outs() << "\n*** Able to remove all global initializers!\n"; |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 683 | } else { // No longer crashes? |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 684 | outs() << " - Removing all global inits hides problem!\n"; |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 685 | delete M; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 686 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 687 | std::vector<GlobalVariable*> GVs; |
| 688 | |
| 689 | for (Module::global_iterator I = BD.getProgram()->global_begin(), |
| 690 | E = BD.getProgram()->global_end(); I != E; ++I) |
| 691 | if (I->hasInitializer()) |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 692 | GVs.push_back(&*I); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 693 | |
| 694 | if (GVs.size() > 1 && !BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 695 | outs() << "\n*** Attempting to reduce the number of global " |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 696 | << "variables in the testcase\n"; |
| 697 | |
| 698 | unsigned OldSize = GVs.size(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 699 | ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error); |
| 700 | if (!Error.empty()) |
| 701 | return true; |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 702 | |
| 703 | if (GVs.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 704 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables"); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 705 | } |
Bill Wendling | ce3afd6 | 2006-10-27 20:22:04 +0000 | [diff] [blame] | 706 | } |
Chris Lattner | 65e5f65 | 2003-04-25 00:53:05 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 710 | // Now try to reduce the number of functions in the module to something small. |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 711 | std::vector<Function*> Functions; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 712 | for (Function &F : *BD.getProgram()) |
| 713 | if (!F.isDeclaration()) |
| 714 | Functions.push_back(&F); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 715 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 716 | if (Functions.size() > 1 && !BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 717 | outs() << "\n*** Attempting to reduce the number of functions " |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 718 | "in the testcase\n"; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 720 | unsigned OldSize = Functions.size(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 721 | ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 722 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 723 | if (Functions.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 724 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-function"); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 727 | // Attempt to delete entire basic blocks at a time to speed up |
| 728 | // convergence... this actually works by setting the terminator of the blocks |
| 729 | // to a return instruction then running simplifycfg, which can potentially |
| 730 | // shrinks the code dramatically quickly |
| 731 | // |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 732 | if (!DisableSimplifyCFG && !BugpointIsInterrupted) { |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 733 | std::vector<const BasicBlock*> Blocks; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 734 | for (Function &F : *BD.getProgram()) |
| 735 | for (BasicBlock &BB : F) |
| 736 | Blocks.push_back(&BB); |
Torok Edwin | 6ee6f73 | 2009-05-24 09:40:47 +0000 | [diff] [blame] | 737 | unsigned OldSize = Blocks.size(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 738 | ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error); |
Torok Edwin | 6ee6f73 | 2009-05-24 09:40:47 +0000 | [diff] [blame] | 739 | if (Blocks.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 740 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks"); |
Chris Lattner | d1e2aae | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 741 | } |
Chris Lattner | d4e0474 | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 742 | |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 743 | // Attempt to delete instructions using bisection. This should help out nasty |
| 744 | // cases with large basic blocks where the problem is at one end. |
| 745 | if (!BugpointIsInterrupted) { |
| 746 | std::vector<const Instruction*> Insts; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 747 | for (const Function &F : *BD.getProgram()) |
| 748 | for (const BasicBlock &BB : F) |
| 749 | for (const Instruction &I : BB) |
| 750 | if (!isa<TerminatorInst>(&I)) |
| 751 | Insts.push_back(&I); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 752 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 753 | ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 756 | // FIXME: This should use the list reducer to converge faster by deleting |
| 757 | // larger chunks of instructions at a time! |
Chris Lattner | 0ee372c | 2004-03-13 19:35:54 +0000 | [diff] [blame] | 758 | unsigned Simplification = 2; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 759 | do { |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 760 | if (BugpointIsInterrupted) break; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 761 | --Simplification; |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 762 | outs() << "\n*** Attempting to reduce testcase by deleting instruc" |
| 763 | << "tions: Simplification Level #" << Simplification << '\n'; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 764 | |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 765 | // Now that we have deleted the functions that are unnecessary for the |
| 766 | // program, try to remove instructions that are not necessary to cause the |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 767 | // crash. To do this, we loop through all of the instructions in the |
| 768 | // remaining functions, deleting them (replacing any values produced with |
| 769 | // nulls), and then running ADCE and SimplifyCFG. If the transformed input |
| 770 | // still triggers failure, keep deleting until we cannot trigger failure |
| 771 | // anymore. |
| 772 | // |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 773 | unsigned InstructionsToSkipBeforeDeleting = 0; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 774 | TryAgain: |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 776 | // Loop over all of the (non-terminator) instructions remaining in the |
| 777 | // function, attempting to delete them. |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 778 | unsigned CurInstructionNum = 0; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 779 | for (Module::const_iterator FI = BD.getProgram()->begin(), |
| 780 | E = BD.getProgram()->end(); FI != E; ++FI) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 781 | if (!FI->isDeclaration()) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 782 | for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E; |
| 783 | ++BI) |
| 784 | for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end(); |
Bill Wendling | 2990ec6 | 2011-12-25 06:56:22 +0000 | [diff] [blame] | 785 | I != E; ++I, ++CurInstructionNum) { |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 786 | if (InstructionsToSkipBeforeDeleting) { |
| 787 | --InstructionsToSkipBeforeDeleting; |
| 788 | } else { |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 789 | if (BugpointIsInterrupted) goto ExitLoops; |
| 790 | |
David Majnemer | 189d7da | 2015-11-08 04:16:12 +0000 | [diff] [blame] | 791 | if (I->isEHPad() || I->getType()->isTokenTy()) |
Eli Friedman | d4e02a5 | 2011-11-01 04:40:56 +0000 | [diff] [blame] | 792 | continue; |
| 793 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 794 | outs() << "Checking instruction: " << *I; |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 795 | std::unique_ptr<Module> M = |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 796 | BD.deleteInstructionFromProgram(&*I, Simplification); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 798 | // Find out if the pass still crashes on this pass... |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 799 | if (TestFn(BD, M.get())) { |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 800 | // Yup, it does, we delete the old module, and continue trying |
| 801 | // to reduce the testcase... |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 802 | BD.setNewProgram(M.release()); |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 803 | InstructionsToSkipBeforeDeleting = CurInstructionNum; |
| 804 | goto TryAgain; // I wish I had a multi-level break here! |
| 805 | } |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 806 | } |
Bill Wendling | 2990ec6 | 2011-12-25 06:56:22 +0000 | [diff] [blame] | 807 | } |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 808 | |
| 809 | if (InstructionsToSkipBeforeDeleting) { |
| 810 | InstructionsToSkipBeforeDeleting = 0; |
| 811 | goto TryAgain; |
| 812 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 814 | } while (Simplification); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 815 | |
| 816 | if (!NoNamedMDRM) { |
| 817 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-instructions"); |
| 818 | |
| 819 | if (!BugpointIsInterrupted) { |
| 820 | // Try to reduce the amount of global metadata (particularly debug info), |
| 821 | // by dropping global named metadata that anchors them |
| 822 | outs() << "\n*** Attempting to remove named metadata: "; |
| 823 | std::vector<std::string> NamedMDNames; |
| 824 | for (auto &NamedMD : BD.getProgram()->named_metadata()) |
| 825 | NamedMDNames.push_back(NamedMD.getName().str()); |
| 826 | ReduceCrashingNamedMD(BD, TestFn).reduceList(NamedMDNames, Error); |
| 827 | } |
| 828 | |
| 829 | if (!BugpointIsInterrupted) { |
| 830 | // Now that we quickly dropped all the named metadata that doesn't |
| 831 | // contribute to the crash, bisect the operands of the remaining ones |
| 832 | std::vector<const MDNode *> NamedMDOps; |
| 833 | for (auto &NamedMD : BD.getProgram()->named_metadata()) |
Keno Fischer | 256df86 | 2015-11-06 00:45:47 +0000 | [diff] [blame] | 834 | for (auto op : NamedMD.operands()) |
| 835 | NamedMDOps.push_back(op); |
Keno Fischer | 34ca831 | 2015-11-06 00:12:50 +0000 | [diff] [blame] | 836 | ReduceCrashingNamedMDOps(BD, TestFn).reduceList(NamedMDOps, Error); |
| 837 | } |
| 838 | } |
| 839 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 840 | ExitLoops: |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 841 | |
| 842 | // Try to clean up the testcase by running funcresolve and globaldce... |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 843 | if (!BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 844 | outs() << "\n*** Attempting to perform final cleanups: "; |
Rafael Espindola | cab951d | 2015-12-08 23:57:17 +0000 | [diff] [blame] | 845 | Module *M = CloneModule(BD.getProgram()).release(); |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 846 | M = BD.performFinalCleanups(M, true).release(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 847 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 848 | // Find out if the pass still crashes on the cleaned up program... |
| 849 | if (TestFn(BD, M)) { |
| 850 | BD.setNewProgram(M); // Yup, it does, keep the reduced version... |
| 851 | } else { |
| 852 | delete M; |
| 853 | } |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 856 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified"); |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 857 | |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 858 | return false; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 859 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 860 | |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 861 | static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) { |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 862 | return BD.runPasses(M); |
| 863 | } |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 865 | /// debugOptimizerCrash - This method is called when some pass crashes on input. |
| 866 | /// It attempts to prune down the testcase to something reasonable, and figure |
| 867 | /// out exactly which pass is crashing. |
| 868 | /// |
Patrick Jenkins | c46c038 | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 869 | bool BugDriver::debugOptimizerCrash(const std::string &ID) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 870 | outs() << "\n*** Debugging optimizer crash!\n"; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 871 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 872 | std::string Error; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 873 | // Reduce the list of passes which causes the optimizer to crash... |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 874 | if (!BugpointIsInterrupted && !DontReducePassList) |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 875 | ReducePassList(*this).reduceList(PassesToRun, Error); |
| 876 | assert(Error.empty()); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 877 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 878 | outs() << "\n*** Found crashing pass" |
| 879 | << (PassesToRun.size() == 1 ? ": " : "es: ") |
| 880 | << getPassesString(PassesToRun) << '\n'; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 881 | |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 882 | EmitProgressBitcode(Program, ID); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 883 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 884 | bool Success = DebugACrash(*this, TestForOptimizerCrash, Error); |
| 885 | assert(Error.empty()); |
| 886 | return Success; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 889 | static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) { |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 890 | std::string Error; |
| 891 | BD.compileProgram(M, &Error); |
| 892 | if (!Error.empty()) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 893 | errs() << "<crash>\n"; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 894 | return true; // Tool is still crashing. |
| 895 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 896 | errs() << '\n'; |
| 897 | return false; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 898 | } |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 899 | |
| 900 | /// debugCodeGeneratorCrash - This method is called when the code generator |
| 901 | /// crashes on an input. It attempts to reduce the input as much as possible |
| 902 | /// while still causing the code generator to crash. |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 903 | bool BugDriver::debugCodeGeneratorCrash(std::string &Error) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 904 | errs() << "*** Debugging code generator crash!\n"; |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 905 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 906 | return DebugACrash(*this, TestForCodeGenCrash, Error); |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 907 | } |