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