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