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