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