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