Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the bugpoint internals that narrow down compilation crashes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "BugDriver.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 15 | #include "ListReducer.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "ToolRunner.h" |
| 17 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 18 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DerivedTypes.h" |
| 21 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LegacyPassManager.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/ValueSymbolTable.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Verifier.h" |
Misha Brukman | 0c2305b | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/FileUtilities.h" |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/Cloning.h" |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 31 | #include <set> |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 33 | |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | cl::opt<bool> |
| 36 | KeepMain("keep-main", |
| 37 | cl::desc("Force function reduction to keep main"), |
| 38 | cl::init(false)); |
Torok Edwin | 5bd3f7b | 2009-05-24 09:31:04 +0000 | [diff] [blame] | 39 | cl::opt<bool> |
| 40 | NoGlobalRM ("disable-global-remove", |
| 41 | cl::desc("Do not remove global variables"), |
| 42 | cl::init(false)); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 43 | |
| 44 | cl::opt<bool> |
| 45 | ReplaceFuncsWithNull("replace-funcs-with-null", |
| 46 | cl::desc("When stubbing functions, replace all uses will null"), |
| 47 | cl::init(false)); |
| 48 | cl::opt<bool> |
| 49 | DontReducePassList("disable-pass-list-reduction", |
| 50 | cl::desc("Skip pass list reduction steps"), |
| 51 | cl::init(false)); |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 54 | namespace llvm { |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 55 | class ReducePassList : public ListReducer<std::string> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 56 | BugDriver &BD; |
| 57 | public: |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 58 | ReducePassList(BugDriver &bd) : BD(bd) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 59 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 60 | // doTest - Return true iff running the "removed" passes succeeds, and |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 61 | // running the "Kept" passes fail when run on the output of the "removed" |
| 62 | // passes. If we return true, we update the current module of bugpoint. |
| 63 | // |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 64 | TestResult doTest(std::vector<std::string> &Removed, |
| 65 | std::vector<std::string> &Kept, |
| 66 | std::string &Error) override; |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 67 | }; |
| 68 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 70 | ReducePassList::TestResult |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 71 | ReducePassList::doTest(std::vector<std::string> &Prefix, |
| 72 | std::vector<std::string> &Suffix, |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 73 | std::string &Error) { |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 74 | std::string PrefixOutput; |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 75 | Module *OrigProgram = nullptr; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 76 | if (!Prefix.empty()) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 77 | outs() << "Checking to see if these passes crash: " |
| 78 | << getPassesString(Prefix) << ": "; |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 79 | if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput)) |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 80 | return KeepPrefix; |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 81 | |
| 82 | OrigProgram = BD.Program; |
| 83 | |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 84 | BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release(); |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 85 | if (BD.Program == nullptr) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 86 | errs() << BD.getToolName() << ": Error reading bitcode file '" |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 87 | << PrefixOutput << "'!\n"; |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 88 | exit(1); |
| 89 | } |
Rafael Espindola | bb87665 | 2013-06-17 19:33:18 +0000 | [diff] [blame] | 90 | sys::fs::remove(PrefixOutput); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 93 | outs() << "Checking to see if these passes crash: " |
| 94 | << getPassesString(Suffix) << ": "; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 95 | |
Rafael Espindola | 37302ea | 2010-08-05 02:16:32 +0000 | [diff] [blame] | 96 | if (BD.runPasses(BD.getProgram(), Suffix)) { |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 97 | delete OrigProgram; // The suffix crashes alone... |
| 98 | return KeepSuffix; |
| 99 | } |
| 100 | |
| 101 | // Nothing failed, restore state... |
Chris Lattner | a965631 | 2003-06-02 04:54:29 +0000 | [diff] [blame] | 102 | if (OrigProgram) { |
| 103 | delete BD.Program; |
| 104 | BD.Program = OrigProgram; |
| 105 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 106 | return NoFailure; |
| 107 | } |
| 108 | |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 109 | namespace { |
| 110 | /// ReduceCrashingGlobalVariables - This works by removing the global |
| 111 | /// variable's initializer and seeing if the program still crashes. If it |
| 112 | /// does, then we keep that program and try again. |
| 113 | /// |
| 114 | class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> { |
| 115 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 116 | bool (*TestFn)(const BugDriver &, Module *); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 117 | public: |
| 118 | ReduceCrashingGlobalVariables(BugDriver &bd, |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 119 | bool (*testFn)(const BugDriver &, Module *)) |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 120 | : BD(bd), TestFn(testFn) {} |
| 121 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 122 | TestResult doTest(std::vector<GlobalVariable*> &Prefix, |
| 123 | std::vector<GlobalVariable*> &Kept, |
| 124 | std::string &Error) override { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 125 | if (!Kept.empty() && TestGlobalVariables(Kept)) |
| 126 | return KeepSuffix; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 127 | if (!Prefix.empty() && TestGlobalVariables(Prefix)) |
| 128 | return KeepPrefix; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 129 | return NoFailure; |
| 130 | } |
| 131 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 132 | bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 133 | }; |
| 134 | } |
| 135 | |
| 136 | bool |
| 137 | ReduceCrashingGlobalVariables::TestGlobalVariables( |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 138 | std::vector<GlobalVariable*> &GVs) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 139 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 140 | ValueToValueMapTy VMap; |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 141 | Module *M = CloneModule(BD.getProgram(), VMap); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 142 | |
| 143 | // Convert list to set for fast lookup... |
| 144 | std::set<GlobalVariable*> GVSet; |
| 145 | |
| 146 | for (unsigned i = 0, e = GVs.size(); i != e; ++i) { |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 147 | GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 148 | assert(CMGV && "Global Variable not in module?!"); |
| 149 | GVSet.insert(CMGV); |
| 150 | } |
| 151 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 152 | outs() << "Checking for crash with only these global variables: "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 153 | PrintGlobalVariableList(GVs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 154 | outs() << ": "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 155 | |
| 156 | // Loop over and delete any global variables which we aren't supposed to be |
| 157 | // playing with... |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 158 | for (GlobalVariable &I : M->globals()) |
| 159 | if (I.hasInitializer() && !GVSet.count(&I)) { |
| 160 | I.setInitializer(nullptr); |
| 161 | I.setLinkage(GlobalValue::ExternalLinkage); |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Try running the hacked up program... |
| 165 | if (TestFn(BD, M)) { |
| 166 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 167 | |
| 168 | // Make sure to use global variable pointers that point into the now-current |
| 169 | // module. |
| 170 | GVs.assign(GVSet.begin(), GVSet.end()); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | delete M; |
| 175 | return false; |
| 176 | } |
| 177 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 178 | namespace { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 179 | /// ReduceCrashingFunctions reducer - This works by removing functions and |
| 180 | /// seeing if the program still crashes. If it does, then keep the newer, |
| 181 | /// smaller program. |
| 182 | /// |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 183 | class ReduceCrashingFunctions : public ListReducer<Function*> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 184 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 185 | bool (*TestFn)(const BugDriver &, Module *); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 186 | public: |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 187 | ReduceCrashingFunctions(BugDriver &bd, |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 188 | bool (*testFn)(const BugDriver &, Module *)) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 189 | : BD(bd), TestFn(testFn) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 190 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 191 | TestResult doTest(std::vector<Function*> &Prefix, |
| 192 | std::vector<Function*> &Kept, |
| 193 | std::string &Error) override { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 194 | if (!Kept.empty() && TestFuncs(Kept)) |
| 195 | return KeepSuffix; |
| 196 | if (!Prefix.empty() && TestFuncs(Prefix)) |
| 197 | return KeepPrefix; |
| 198 | return NoFailure; |
| 199 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 200 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 201 | bool TestFuncs(std::vector<Function*> &Prefix); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 202 | }; |
| 203 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 204 | |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 205 | static void RemoveFunctionReferences(Module *M, const char* Name) { |
| 206 | auto *UsedVar = M->getGlobalVariable(Name, true); |
| 207 | if (!UsedVar || !UsedVar->hasInitializer()) return; |
| 208 | if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) { |
| 209 | assert(UsedVar->use_empty()); |
| 210 | UsedVar->eraseFromParent(); |
| 211 | return; |
| 212 | } |
| 213 | auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer()); |
| 214 | std::vector<Constant*> Used; |
| 215 | for(Value *V : OldUsedVal->operand_values()) { |
| 216 | Constant *Op = cast<Constant>(V->stripPointerCasts()); |
| 217 | if(!Op->isNullValue()) { |
| 218 | Used.push_back(cast<Constant>(V)); |
| 219 | } |
| 220 | } |
| 221 | auto *NewValElemTy = OldUsedVal->getType()->getElementType(); |
| 222 | auto *NewValTy = ArrayType::get(NewValElemTy, Used.size()); |
| 223 | auto *NewUsedVal = ConstantArray::get(NewValTy, Used); |
| 224 | UsedVar->mutateType(NewUsedVal->getType()->getPointerTo()); |
| 225 | UsedVar->setInitializer(NewUsedVal); |
| 226 | } |
| 227 | |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 228 | bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) { |
Dmitri Gribenko | 6e0520e | 2013-09-02 01:18:56 +0000 | [diff] [blame] | 229 | // If main isn't present, claim there is no problem. |
| 230 | if (KeepMain && std::find(Funcs.begin(), Funcs.end(), |
| 231 | BD.getProgram()->getFunction("main")) == |
| 232 | Funcs.end()) |
Andrew Lenharth | ef9aa12 | 2006-03-05 22:21:36 +0000 | [diff] [blame] | 233 | return false; |
| 234 | |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 235 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 236 | ValueToValueMapTy VMap; |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 237 | Module *M = CloneModule(BD.getProgram(), VMap); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 239 | // Convert list to set for fast lookup... |
| 240 | std::set<Function*> Functions; |
| 241 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 242 | Function *CMF = cast<Function>(VMap[Funcs[i]]); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 243 | assert(CMF && "Function not in module?!"); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 244 | assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty"); |
Dan Gohman | bcad718 | 2009-03-06 02:16:23 +0000 | [diff] [blame] | 245 | assert(CMF->getName() == Funcs[i]->getName() && "wrong name"); |
Chris Lattner | de39f2b | 2003-04-24 22:54:06 +0000 | [diff] [blame] | 246 | Functions.insert(CMF); |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 249 | outs() << "Checking for crash with only these functions: "; |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 250 | PrintFunctionList(Funcs); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 251 | outs() << ": "; |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 252 | if (!ReplaceFuncsWithNull) { |
| 253 | // Loop over and delete any functions which we aren't supposed to be playing |
| 254 | // with... |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 255 | for (Function &I : *M) |
| 256 | if (!I.isDeclaration() && !Functions.count(&I)) |
| 257 | DeleteFunctionBody(&I); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 258 | } else { |
| 259 | std::vector<GlobalValue*> ToRemove; |
| 260 | // First, remove aliases to functions we're about to purge. |
| 261 | for (GlobalAlias &Alias : M->aliases()) { |
| 262 | Constant *Root = Alias.getAliasee()->stripPointerCasts(); |
| 263 | Function *F = dyn_cast<Function>(Root); |
| 264 | if (F) { |
| 265 | if (Functions.count(F)) |
| 266 | // We're keeping this function. |
| 267 | continue; |
| 268 | } else if (Root->isNullValue()) { |
| 269 | // This referenced a globalalias that we've already replaced, |
| 270 | // so we still need to replace this alias. |
| 271 | } else if (!F) { |
| 272 | // Not a function, therefore not something we mess with. |
| 273 | continue; |
| 274 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 275 | |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 276 | PointerType *Ty = cast<PointerType>(Alias.getType()); |
| 277 | Constant *Replacement = ConstantPointerNull::get(Ty); |
| 278 | Alias.replaceAllUsesWith(Replacement); |
| 279 | ToRemove.push_back(&Alias); |
| 280 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 281 | |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 282 | for (Function &I : *M) { |
| 283 | if (!I.isDeclaration() && !Functions.count(&I)) { |
| 284 | PointerType *Ty = cast<PointerType>(I.getType()); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 285 | Constant *Replacement = ConstantPointerNull::get(Ty); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 286 | I.replaceAllUsesWith(Replacement); |
| 287 | ToRemove.push_back(&I); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | for (auto *F : ToRemove) { |
| 292 | F->eraseFromParent(); |
| 293 | } |
| 294 | |
| 295 | // Finally, remove any null members from any global intrinsic. |
| 296 | RemoveFunctionReferences(M, "llvm.used"); |
| 297 | RemoveFunctionReferences(M, "llvm.compiler.used"); |
| 298 | } |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 299 | // Try running the hacked up program... |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 300 | if (TestFn(BD, M)) { |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 301 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 302 | |
| 303 | // Make sure to use function pointers that point into the now-current |
| 304 | // module. |
| 305 | Funcs.assign(Functions.begin(), Functions.end()); |
| 306 | return true; |
| 307 | } |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 308 | delete M; |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Chris Lattner | 16a4131 | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 1942f98 | 2004-02-18 21:29:46 +0000 | [diff] [blame] | 313 | namespace { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 314 | /// ReduceCrashingBlocks reducer - This works by setting the terminators of |
| 315 | /// all terminators except the specified basic blocks to a 'ret' instruction, |
| 316 | /// then running the simplify-cfg pass. This has the effect of chopping up |
| 317 | /// the CFG really fast which can reduce large functions quickly. |
| 318 | /// |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 319 | class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 320 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 321 | bool (*TestFn)(const BugDriver &, Module *); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 322 | public: |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 323 | ReduceCrashingBlocks(BugDriver &bd, |
| 324 | bool (*testFn)(const BugDriver &, Module *)) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 325 | : BD(bd), TestFn(testFn) {} |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 326 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 327 | TestResult doTest(std::vector<const BasicBlock*> &Prefix, |
| 328 | std::vector<const BasicBlock*> &Kept, |
| 329 | std::string &Error) override { |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 330 | if (!Kept.empty() && TestBlocks(Kept)) |
| 331 | return KeepSuffix; |
| 332 | if (!Prefix.empty() && TestBlocks(Prefix)) |
| 333 | return KeepPrefix; |
| 334 | return NoFailure; |
| 335 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 337 | bool TestBlocks(std::vector<const BasicBlock*> &Prefix); |
Chris Lattner | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 338 | }; |
| 339 | } |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 341 | bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) { |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 342 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 343 | ValueToValueMapTy VMap; |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 344 | Module *M = CloneModule(BD.getProgram(), VMap); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 345 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 346 | // Convert list to set for fast lookup... |
Nick Lewycky | b294e31 | 2009-04-04 09:39:23 +0000 | [diff] [blame] | 347 | SmallPtrSet<BasicBlock*, 8> Blocks; |
| 348 | for (unsigned i = 0, e = BBs.size(); i != e; ++i) |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 349 | Blocks.insert(cast<BasicBlock>(VMap[BBs[i]])); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 350 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 351 | outs() << "Checking for crash with only these blocks:"; |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 352 | unsigned NumPrint = Blocks.size(); |
| 353 | if (NumPrint > 10) NumPrint = 10; |
| 354 | for (unsigned i = 0, e = NumPrint; i != e; ++i) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 355 | outs() << " " << BBs[i]->getName(); |
Chris Lattner | 4f50ebd | 2003-10-27 04:44:59 +0000 | [diff] [blame] | 356 | if (NumPrint < Blocks.size()) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 357 | outs() << "... <" << Blocks.size() << " total>"; |
| 358 | outs() << ": "; |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 359 | |
| 360 | // Loop over and delete any hack up any blocks that are not listed... |
| 361 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 362 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 363 | if (!Blocks.count(&*BB) && BB->getTerminator()->getNumSuccessors()) { |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 364 | // Loop over all of the successors of this block, deleting any PHI nodes |
| 365 | // that might include it. |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 366 | for (succ_iterator SI = succ_begin(&*BB), E = succ_end(&*BB); SI != E; |
| 367 | ++SI) |
| 368 | (*SI)->removePredecessor(&*BB); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 7855a5c | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 370 | TerminatorInst *BBTerm = BB->getTerminator(); |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 371 | |
Dan Gohman | 34a2249 | 2010-06-07 20:19:26 +0000 | [diff] [blame] | 372 | if (!BB->getTerminator()->getType()->isVoidTy()) |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 373 | BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType())); |
Chris Lattner | 7b233af | 2003-11-22 02:10:38 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 7855a5c | 2008-04-28 00:04:58 +0000 | [diff] [blame] | 375 | // Replace the old terminator instruction. |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 376 | BB->getInstList().pop_back(); |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 377 | new UnreachableInst(BB->getContext(), &*BB); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // The CFG Simplifier pass may delete one of the basic blocks we are |
| 381 | // interested in. If it does we need to take the block out of the list. Make |
| 382 | // a "persistent mapping" by turning basic blocks into <function, name> pairs. |
| 383 | // This won't work well if blocks are unnamed, but that is just the risk we |
| 384 | // have to take. |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 385 | std::vector<std::pair<std::string, std::string> > BlockInfo; |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 386 | |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 387 | for (BasicBlock *BB : Blocks) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 388 | BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName()); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 389 | |
| 390 | // Now run the CFG simplify pass on the function... |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 391 | std::vector<std::string> Passes; |
| 392 | Passes.push_back("simplifycfg"); |
| 393 | Passes.push_back("verify"); |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 394 | std::unique_ptr<Module> New = BD.runPassesOn(M, Passes); |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 395 | delete M; |
| 396 | if (!New) { |
| 397 | errs() << "simplifycfg failed!\n"; |
| 398 | exit(1); |
| 399 | } |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 400 | M = New.release(); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 401 | |
| 402 | // Try running on the hacked up program... |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 403 | if (TestFn(BD, M)) { |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 404 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 405 | |
| 406 | // Make sure to use basic block pointers that point into the now-current |
| 407 | // module, and that they don't include any deleted blocks. |
| 408 | BBs.clear(); |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 409 | const ValueSymbolTable &GST = M->getValueSymbolTable(); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 410 | for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { |
Rafael Espindola | d1e241a | 2010-08-10 15:46:11 +0000 | [diff] [blame] | 411 | Function *F = cast<Function>(GST.lookup(BlockInfo[i].first)); |
| 412 | ValueSymbolTable &ST = F->getValueSymbolTable(); |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 413 | Value* V = ST.lookup(BlockInfo[i].second); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 414 | if (V && V->getType() == Type::getLabelTy(V->getContext())) |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 415 | BBs.push_back(cast<BasicBlock>(V)); |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 416 | } |
| 417 | return true; |
| 418 | } |
Chris Lattner | 327019b | 2004-02-18 21:24:48 +0000 | [diff] [blame] | 419 | delete M; // It didn't crash, try something else. |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 423 | namespace { |
| 424 | /// ReduceCrashingInstructions reducer - This works by removing the specified |
| 425 | /// non-terminator instructions and replacing them with undef. |
| 426 | /// |
| 427 | class ReduceCrashingInstructions : public ListReducer<const Instruction*> { |
| 428 | BugDriver &BD; |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 429 | bool (*TestFn)(const BugDriver &, Module *); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 430 | public: |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 431 | ReduceCrashingInstructions(BugDriver &bd, |
| 432 | bool (*testFn)(const BugDriver &, Module *)) |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 433 | : BD(bd), TestFn(testFn) {} |
| 434 | |
Craig Topper | e56917c | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 435 | TestResult doTest(std::vector<const Instruction*> &Prefix, |
| 436 | std::vector<const Instruction*> &Kept, |
| 437 | std::string &Error) override { |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 438 | if (!Kept.empty() && TestInsts(Kept)) |
| 439 | return KeepSuffix; |
| 440 | if (!Prefix.empty() && TestInsts(Prefix)) |
| 441 | return KeepPrefix; |
| 442 | return NoFailure; |
| 443 | } |
| 444 | |
| 445 | bool TestInsts(std::vector<const Instruction*> &Prefix); |
| 446 | }; |
| 447 | } |
| 448 | |
| 449 | bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*> |
| 450 | &Insts) { |
| 451 | // Clone the program to try hacking it apart... |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 452 | ValueToValueMapTy VMap; |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 453 | Module *M = CloneModule(BD.getProgram(), VMap); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 454 | |
| 455 | // Convert list to set for fast lookup... |
| 456 | SmallPtrSet<Instruction*, 64> Instructions; |
| 457 | for (unsigned i = 0, e = Insts.size(); i != e; ++i) { |
| 458 | assert(!isa<TerminatorInst>(Insts[i])); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 459 | Instructions.insert(cast<Instruction>(VMap[Insts[i]])); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 462 | outs() << "Checking for crash with only " << Instructions.size(); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 463 | if (Instructions.size() == 1) |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 464 | outs() << " instruction: "; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 465 | else |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 466 | outs() << " instructions: "; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 467 | |
| 468 | for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) |
| 469 | for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) |
| 470 | for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 471 | Instruction *Inst = &*I++; |
Eli Friedman | d4e02a5 | 2011-11-01 04:40:56 +0000 | [diff] [blame] | 472 | if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) && |
David Majnemer | ba275f9 | 2015-08-19 19:54:02 +0000 | [diff] [blame] | 473 | !Inst->isEHPad()) { |
Dan Gohman | 34a2249 | 2010-06-07 20:19:26 +0000 | [diff] [blame] | 474 | if (!Inst->getType()->isVoidTy()) |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 475 | Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); |
| 476 | Inst->eraseFromParent(); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // Verify that this is still valid. |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 481 | legacy::PassManager Passes; |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 482 | Passes.add(createVerifierPass()); |
| 483 | Passes.run(*M); |
| 484 | |
| 485 | // Try running on the hacked up program... |
| 486 | if (TestFn(BD, M)) { |
| 487 | BD.setNewProgram(M); // It crashed, keep the trimmed version... |
| 488 | |
| 489 | // Make sure to use instruction pointers that point into the now-current |
| 490 | // module, and that they don't include any deleted blocks. |
| 491 | Insts.clear(); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 492 | for (Instruction *Inst : Instructions) |
| 493 | Insts.push_back(Inst); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 494 | return true; |
| 495 | } |
| 496 | delete M; // It didn't crash, try something else. |
| 497 | return false; |
| 498 | } |
| 499 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 500 | /// DebugACrash - Given a predicate that determines whether a component crashes |
| 501 | /// on a program, try to destructively reduce the program while still keeping |
| 502 | /// the predicate true. |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 503 | static bool DebugACrash(BugDriver &BD, |
| 504 | bool (*TestFn)(const BugDriver &, Module *), |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 505 | std::string &Error) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 506 | // See if we can get away with nuking some of the global variable initializers |
Chris Lattner | 65e5f65 | 2003-04-25 00:53:05 +0000 | [diff] [blame] | 507 | // in the program... |
Torok Edwin | 5bd3f7b | 2009-05-24 09:31:04 +0000 | [diff] [blame] | 508 | if (!NoGlobalRM && |
| 509 | BD.getProgram()->global_begin() != BD.getProgram()->global_end()) { |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 510 | // Now try to reduce the number of global variable initializers in the |
| 511 | // module to something small. |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 512 | Module *M = CloneModule(BD.getProgram()); |
| 513 | bool DeletedInit = false; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 514 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 515 | for (Module::global_iterator I = M->global_begin(), E = M->global_end(); |
| 516 | I != E; ++I) |
| 517 | if (I->hasInitializer()) { |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 518 | I->setInitializer(nullptr); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 519 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 520 | DeletedInit = true; |
| 521 | } |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 522 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 523 | if (!DeletedInit) { |
| 524 | delete M; // No change made... |
| 525 | } else { |
| 526 | // See if the program still causes a crash... |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 527 | outs() << "\nChecking to see if we can delete global inits: "; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 528 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 529 | if (TestFn(BD, M)) { // Still crashes? |
| 530 | BD.setNewProgram(M); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 531 | outs() << "\n*** Able to remove all global initializers!\n"; |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 532 | } else { // No longer crashes? |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 533 | outs() << " - Removing all global inits hides problem!\n"; |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 534 | delete M; |
Bill Wendling | 3f83343 | 2006-10-25 18:36:14 +0000 | [diff] [blame] | 535 | |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 536 | std::vector<GlobalVariable*> GVs; |
| 537 | |
| 538 | for (Module::global_iterator I = BD.getProgram()->global_begin(), |
| 539 | E = BD.getProgram()->global_end(); I != E; ++I) |
| 540 | if (I->hasInitializer()) |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 541 | GVs.push_back(&*I); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 542 | |
| 543 | if (GVs.size() > 1 && !BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 544 | outs() << "\n*** Attempting to reduce the number of global " |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 545 | << "variables in the testcase\n"; |
| 546 | |
| 547 | unsigned OldSize = GVs.size(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 548 | ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error); |
| 549 | if (!Error.empty()) |
| 550 | return true; |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 551 | |
| 552 | if (GVs.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 553 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables"); |
Bill Wendling | 74a4a2a | 2006-10-27 20:18:06 +0000 | [diff] [blame] | 554 | } |
Bill Wendling | ce3afd6 | 2006-10-27 20:22:04 +0000 | [diff] [blame] | 555 | } |
Chris Lattner | 65e5f65 | 2003-04-25 00:53:05 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 559 | // Now try to reduce the number of functions in the module to something small. |
Chris Lattner | fd72bed | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 560 | std::vector<Function*> Functions; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 561 | for (Function &F : *BD.getProgram()) |
| 562 | if (!F.isDeclaration()) |
| 563 | Functions.push_back(&F); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 564 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 565 | if (Functions.size() > 1 && !BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 566 | outs() << "\n*** Attempting to reduce the number of functions " |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 567 | "in the testcase\n"; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 569 | unsigned OldSize = Functions.size(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 570 | ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 571 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 572 | if (Functions.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 573 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-function"); |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Chris Lattner | f32939b | 2003-04-24 23:51:38 +0000 | [diff] [blame] | 576 | // Attempt to delete entire basic blocks at a time to speed up |
| 577 | // convergence... this actually works by setting the terminator of the blocks |
| 578 | // to a return instruction then running simplifycfg, which can potentially |
| 579 | // shrinks the code dramatically quickly |
| 580 | // |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 581 | if (!DisableSimplifyCFG && !BugpointIsInterrupted) { |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 582 | std::vector<const BasicBlock*> Blocks; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 583 | for (Function &F : *BD.getProgram()) |
| 584 | for (BasicBlock &BB : F) |
| 585 | Blocks.push_back(&BB); |
Torok Edwin | 6ee6f73 | 2009-05-24 09:40:47 +0000 | [diff] [blame] | 586 | unsigned OldSize = Blocks.size(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 587 | ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error); |
Torok Edwin | 6ee6f73 | 2009-05-24 09:40:47 +0000 | [diff] [blame] | 588 | if (Blocks.size() < OldSize) |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 589 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks"); |
Chris Lattner | d1e2aae | 2003-08-05 15:51:05 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | d4e0474 | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 591 | |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 592 | // Attempt to delete instructions using bisection. This should help out nasty |
| 593 | // cases with large basic blocks where the problem is at one end. |
| 594 | if (!BugpointIsInterrupted) { |
| 595 | std::vector<const Instruction*> Insts; |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 596 | for (const Function &F : *BD.getProgram()) |
| 597 | for (const BasicBlock &BB : F) |
| 598 | for (const Instruction &I : BB) |
| 599 | if (!isa<TerminatorInst>(&I)) |
| 600 | Insts.push_back(&I); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 601 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 602 | ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error); |
Nick Lewycky | 6e090c9 | 2009-05-25 05:30:00 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Chris Lattner | 1d080f2 | 2003-04-24 22:24:58 +0000 | [diff] [blame] | 605 | // FIXME: This should use the list reducer to converge faster by deleting |
| 606 | // larger chunks of instructions at a time! |
Chris Lattner | 0ee372c | 2004-03-13 19:35:54 +0000 | [diff] [blame] | 607 | unsigned Simplification = 2; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 608 | do { |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 609 | if (BugpointIsInterrupted) break; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 610 | --Simplification; |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 611 | outs() << "\n*** Attempting to reduce testcase by deleting instruc" |
| 612 | << "tions: Simplification Level #" << Simplification << '\n'; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 613 | |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 614 | // Now that we have deleted the functions that are unnecessary for the |
| 615 | // program, try to remove instructions that are not necessary to cause the |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 616 | // crash. To do this, we loop through all of the instructions in the |
| 617 | // remaining functions, deleting them (replacing any values produced with |
| 618 | // nulls), and then running ADCE and SimplifyCFG. If the transformed input |
| 619 | // still triggers failure, keep deleting until we cannot trigger failure |
| 620 | // anymore. |
| 621 | // |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 622 | unsigned InstructionsToSkipBeforeDeleting = 0; |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 623 | TryAgain: |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 625 | // Loop over all of the (non-terminator) instructions remaining in the |
| 626 | // function, attempting to delete them. |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 627 | unsigned CurInstructionNum = 0; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 628 | for (Module::const_iterator FI = BD.getProgram()->begin(), |
| 629 | E = BD.getProgram()->end(); FI != E; ++FI) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 630 | if (!FI->isDeclaration()) |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 631 | for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E; |
| 632 | ++BI) |
| 633 | for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end(); |
Bill Wendling | 2990ec6 | 2011-12-25 06:56:22 +0000 | [diff] [blame] | 634 | I != E; ++I, ++CurInstructionNum) { |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 635 | if (InstructionsToSkipBeforeDeleting) { |
| 636 | --InstructionsToSkipBeforeDeleting; |
| 637 | } else { |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 638 | if (BugpointIsInterrupted) goto ExitLoops; |
| 639 | |
Eli Friedman | d4e02a5 | 2011-11-01 04:40:56 +0000 | [diff] [blame] | 640 | if (isa<LandingPadInst>(I)) |
| 641 | continue; |
| 642 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 643 | outs() << "Checking instruction: " << *I; |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 644 | std::unique_ptr<Module> M = |
Duncan P. N. Exon Smith | 306d16e | 2015-10-20 19:36:39 +0000 | [diff] [blame^] | 645 | BD.deleteInstructionFromProgram(&*I, Simplification); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 646 | |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 647 | // Find out if the pass still crashes on this pass... |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 648 | if (TestFn(BD, M.get())) { |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 649 | // Yup, it does, we delete the old module, and continue trying |
| 650 | // to reduce the testcase... |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 651 | BD.setNewProgram(M.release()); |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 652 | InstructionsToSkipBeforeDeleting = CurInstructionNum; |
| 653 | goto TryAgain; // I wish I had a multi-level break here! |
| 654 | } |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 655 | } |
Bill Wendling | 2990ec6 | 2011-12-25 06:56:22 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 94b2bfb | 2004-02-18 23:59:11 +0000 | [diff] [blame] | 657 | |
| 658 | if (InstructionsToSkipBeforeDeleting) { |
| 659 | InstructionsToSkipBeforeDeleting = 0; |
| 660 | goto TryAgain; |
| 661 | } |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 0eb5ce9 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 663 | } while (Simplification); |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 664 | ExitLoops: |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 665 | |
| 666 | // Try to clean up the testcase by running funcresolve and globaldce... |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 667 | if (!BugpointIsInterrupted) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 668 | outs() << "\n*** Attempting to perform final cleanups: "; |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 669 | Module *M = CloneModule(BD.getProgram()); |
Rafael Espindola | 28b351a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 670 | M = BD.performFinalCleanups(M, true).release(); |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 671 | |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 672 | // Find out if the pass still crashes on the cleaned up program... |
| 673 | if (TestFn(BD, M)) { |
| 674 | BD.setNewProgram(M); // Yup, it does, keep the reduced version... |
| 675 | } else { |
| 676 | delete M; |
| 677 | } |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 680 | BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified"); |
Chris Lattner | 514c02e | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 681 | |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 682 | return false; |
Chris Lattner | 73a6bdd | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 683 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 684 | |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 685 | static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) { |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 686 | return BD.runPasses(M); |
| 687 | } |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 688 | |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 689 | /// debugOptimizerCrash - This method is called when some pass crashes on input. |
| 690 | /// It attempts to prune down the testcase to something reasonable, and figure |
| 691 | /// out exactly which pass is crashing. |
| 692 | /// |
Patrick Jenkins | c46c038 | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 693 | bool BugDriver::debugOptimizerCrash(const std::string &ID) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 694 | outs() << "\n*** Debugging optimizer crash!\n"; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 695 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 696 | std::string Error; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 697 | // Reduce the list of passes which causes the optimizer to crash... |
JF Bastien | f87e20d | 2015-04-20 23:42:22 +0000 | [diff] [blame] | 698 | if (!BugpointIsInterrupted && !DontReducePassList) |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 699 | ReducePassList(*this).reduceList(PassesToRun, Error); |
| 700 | assert(Error.empty()); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 701 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 702 | outs() << "\n*** Found crashing pass" |
| 703 | << (PassesToRun.size() == 1 ? ": " : "es: ") |
| 704 | << getPassesString(PassesToRun) << '\n'; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 705 | |
Rafael Espindola | 594994a | 2010-07-28 18:12:30 +0000 | [diff] [blame] | 706 | EmitProgressBitcode(Program, ID); |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 707 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 708 | bool Success = DebugACrash(*this, TestForOptimizerCrash, Error); |
| 709 | assert(Error.empty()); |
| 710 | return Success; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Rafael Espindola | d1c7ef4 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 713 | static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) { |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 714 | std::string Error; |
| 715 | BD.compileProgram(M, &Error); |
| 716 | if (!Error.empty()) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 717 | errs() << "<crash>\n"; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 718 | return true; // Tool is still crashing. |
| 719 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 720 | errs() << '\n'; |
| 721 | return false; |
Chris Lattner | 8bda4c4 | 2004-02-18 23:26:28 +0000 | [diff] [blame] | 722 | } |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 723 | |
| 724 | /// debugCodeGeneratorCrash - This method is called when the code generator |
| 725 | /// crashes on an input. It attempts to reduce the input as much as possible |
| 726 | /// while still causing the code generator to crash. |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 727 | bool BugDriver::debugCodeGeneratorCrash(std::string &Error) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 728 | errs() << "*** Debugging code generator crash!\n"; |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 729 | |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 730 | return DebugACrash(*this, TestForCodeGenCrash, Error); |
Chris Lattner | ead1dff | 2004-02-18 21:02:04 +0000 | [diff] [blame] | 731 | } |