2002-11-21 06:28:10 +08:00
|
|
|
//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-21 06:28:10 +08:00
|
|
|
//
|
|
|
|
// This file defines the bugpoint internals that narrow down compilation crashes
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BugDriver.h"
|
2003-04-25 06:24:58 +08:00
|
|
|
#include "ListReducer.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "ToolRunner.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2015-11-06 08:12:50 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2016-09-02 09:21:37 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2016-10-26 02:44:13 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/ValueSymbolTable.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2003-08-08 05:19:30 +08:00
|
|
|
#include "llvm/Pass.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2003-04-25 07:51:38 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2003-04-25 06:24:58 +08:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2003-04-25 06:24:58 +08:00
|
|
|
#include <set>
|
2004-01-14 11:38:37 +08:00
|
|
|
using namespace llvm;
|
2002-11-21 06:28:10 +08:00
|
|
|
|
2006-03-06 06:21:36 +08:00
|
|
|
namespace {
|
2016-09-02 09:21:37 +08:00
|
|
|
cl::opt<bool> KeepMain("keep-main",
|
|
|
|
cl::desc("Force function reduction to keep main"),
|
|
|
|
cl::init(false));
|
|
|
|
cl::opt<bool> NoGlobalRM("disable-global-remove",
|
|
|
|
cl::desc("Do not remove global variables"),
|
|
|
|
cl::init(false));
|
|
|
|
|
|
|
|
cl::opt<bool> ReplaceFuncsWithNull(
|
|
|
|
"replace-funcs-with-null",
|
|
|
|
cl::desc("When stubbing functions, replace all uses will null"),
|
|
|
|
cl::init(false));
|
|
|
|
cl::opt<bool> DontReducePassList("disable-pass-list-reduction",
|
|
|
|
cl::desc("Skip pass list reduction steps"),
|
|
|
|
cl::init(false));
|
|
|
|
|
|
|
|
cl::opt<bool> NoNamedMDRM("disable-namedmd-remove",
|
|
|
|
cl::desc("Do not remove global named metadata"),
|
|
|
|
cl::init(false));
|
2016-10-26 02:44:13 +08:00
|
|
|
cl::opt<bool> NoStripDebugInfo("disable-strip-debuginfo",
|
|
|
|
cl::desc("Do not strip debug info metadata"),
|
|
|
|
cl::init(false));
|
|
|
|
cl::opt<bool> NoStripDebugTypeInfo("disable-strip-debug-types",
|
|
|
|
cl::desc("Do not strip debug type info metadata"),
|
|
|
|
cl::init(false));
|
2016-09-02 09:21:37 +08:00
|
|
|
cl::opt<bool> VerboseErrors("verbose-errors",
|
2016-07-16 07:15:06 +08:00
|
|
|
cl::desc("Print the output of crashing program"),
|
|
|
|
cl::init(false));
|
2006-03-06 06:21:36 +08:00
|
|
|
}
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
namespace llvm {
|
2016-09-02 09:21:37 +08:00
|
|
|
class ReducePassList : public ListReducer<std::string> {
|
|
|
|
BugDriver &BD;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ReducePassList(BugDriver &bd) : BD(bd) {}
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
// Return true iff running the "removed" passes succeeds, and running the
|
|
|
|
// "Kept" passes fail when run on the output of the "removed" passes. If we
|
|
|
|
// return true, we update the current module of bugpoint.
|
|
|
|
Expected<TestResult> doTest(std::vector<std::string> &Removed,
|
|
|
|
std::vector<std::string> &Kept) override;
|
2016-09-02 09:21:37 +08:00
|
|
|
};
|
2004-01-14 11:38:37 +08:00
|
|
|
}
|
2003-04-25 06:24:58 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<ReducePassList::TestResult>
|
2010-08-08 11:55:08 +08:00
|
|
|
ReducePassList::doTest(std::vector<std::string> &Prefix,
|
2016-09-07 01:18:22 +08:00
|
|
|
std::vector<std::string> &Suffix) {
|
2013-06-18 03:33:18 +08:00
|
|
|
std::string PrefixOutput;
|
2018-02-15 03:58:41 +08:00
|
|
|
std::unique_ptr<Module> OrigProgram;
|
2003-04-25 06:24:58 +08:00
|
|
|
if (!Prefix.empty()) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Checking to see if these passes crash: "
|
|
|
|
<< getPassesString(Prefix) << ": ";
|
2013-06-18 03:33:18 +08:00
|
|
|
if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput))
|
2003-04-25 06:24:58 +08:00
|
|
|
return KeepPrefix;
|
2003-06-02 12:54:29 +08:00
|
|
|
|
2018-02-15 03:58:41 +08:00
|
|
|
OrigProgram.reset(BD.Program);
|
2003-06-02 12:54:29 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release();
|
2014-04-25 12:24:47 +08:00
|
|
|
if (BD.Program == nullptr) {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << BD.getToolName() << ": Error reading bitcode file '"
|
2013-06-18 03:33:18 +08:00
|
|
|
<< PrefixOutput << "'!\n";
|
2003-06-02 12:54:29 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-06-18 03:33:18 +08:00
|
|
|
sys::fs::remove(PrefixOutput);
|
2003-04-25 06:24:58 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
outs() << "Checking to see if these passes crash: " << getPassesString(Suffix)
|
|
|
|
<< ": ";
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2018-02-15 03:58:41 +08:00
|
|
|
if (BD.runPasses(BD.getProgram(), Suffix))
|
|
|
|
return KeepSuffix; // The suffix crashes alone...
|
2003-04-25 06:24:58 +08:00
|
|
|
|
|
|
|
// Nothing failed, restore state...
|
2003-06-02 12:54:29 +08:00
|
|
|
if (OrigProgram) {
|
|
|
|
delete BD.Program;
|
2018-02-15 03:58:41 +08:00
|
|
|
BD.Program = OrigProgram.release();
|
2003-06-02 12:54:29 +08:00
|
|
|
}
|
2003-04-25 06:24:58 +08:00
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
2018-02-09 02:46:49 +08:00
|
|
|
using BugTester = bool (*)(const BugDriver &, Module *);
|
|
|
|
|
2006-10-26 02:36:14 +08:00
|
|
|
namespace {
|
2018-02-09 04:27:09 +08:00
|
|
|
/// ReduceCrashingGlobalInitializers - This works by removing global variable
|
|
|
|
/// initializers and seeing if the program still crashes. If it does, then we
|
|
|
|
/// keep that program and try again.
|
|
|
|
class ReduceCrashingGlobalInitializers : public ListReducer<GlobalVariable *> {
|
2016-09-02 09:21:37 +08:00
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2016-09-02 09:21:37 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-09 04:27:09 +08:00
|
|
|
ReduceCrashingGlobalInitializers(BugDriver &bd, BugTester testFn)
|
2006-10-26 02:36:14 +08:00
|
|
|
: BD(bd), TestFn(testFn) {}
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<GlobalVariable *> &Prefix,
|
|
|
|
std::vector<GlobalVariable *> &Kept) override {
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!Kept.empty() && TestGlobalVariables(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestGlobalVariables(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
2006-10-26 02:36:14 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool TestGlobalVariables(std::vector<GlobalVariable *> &GVs);
|
|
|
|
};
|
2006-10-26 02:36:14 +08:00
|
|
|
}
|
|
|
|
|
2018-02-09 04:27:09 +08:00
|
|
|
bool ReduceCrashingGlobalInitializers::TestGlobalVariables(
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<GlobalVariable *> &GVs) {
|
2006-10-26 02:36:14 +08:00
|
|
|
// Clone the program to try hacking it apart...
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2006-10-26 02:36:14 +08:00
|
|
|
|
|
|
|
// Convert list to set for fast lookup...
|
2016-09-02 09:21:37 +08:00
|
|
|
std::set<GlobalVariable *> GVSet;
|
2006-10-26 02:36:14 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
|
2016-09-02 09:21:37 +08:00
|
|
|
GlobalVariable *CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
|
2006-10-26 02:36:14 +08:00
|
|
|
assert(CMGV && "Global Variable not in module?!");
|
|
|
|
GVSet.insert(CMGV);
|
|
|
|
}
|
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Checking for crash with only these global variables: ";
|
2006-10-26 02:36:14 +08:00
|
|
|
PrintGlobalVariableList(GVs);
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << ": ";
|
2006-10-26 02:36:14 +08:00
|
|
|
|
|
|
|
// Loop over and delete any global variables which we aren't supposed to be
|
|
|
|
// playing with...
|
2015-10-21 03:36:39 +08:00
|
|
|
for (GlobalVariable &I : M->globals())
|
|
|
|
if (I.hasInitializer() && !GVSet.count(&I)) {
|
2015-11-27 03:23:49 +08:00
|
|
|
DeleteGlobalInitializer(&I);
|
2015-10-21 03:36:39 +08:00
|
|
|
I.setLinkage(GlobalValue::ExternalLinkage);
|
2016-06-16 07:20:12 +08:00
|
|
|
I.setComdat(nullptr);
|
2006-10-26 02:36:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try running the hacked up program...
|
2018-02-09 04:27:09 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2006-10-26 02:36:14 +08:00
|
|
|
|
|
|
|
// Make sure to use global variable pointers that point into the now-current
|
|
|
|
// module.
|
|
|
|
GVs.assign(GVSet.begin(), GVSet.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
namespace {
|
2016-09-02 09:21:37 +08:00
|
|
|
/// ReduceCrashingFunctions reducer - This works by removing functions and
|
|
|
|
/// seeing if the program still crashes. If it does, then keep the newer,
|
|
|
|
/// smaller program.
|
|
|
|
///
|
|
|
|
class ReduceCrashingFunctions : public ListReducer<Function *> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2016-09-02 09:21:37 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceCrashingFunctions(BugDriver &bd, BugTester testFn)
|
2004-02-19 07:26:28 +08:00
|
|
|
: BD(bd), TestFn(testFn) {}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<Function *> &Prefix,
|
|
|
|
std::vector<Function *> &Kept) override {
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!Kept.empty() && TestFuncs(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestFuncs(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool TestFuncs(std::vector<Function *> &Prefix);
|
|
|
|
};
|
2004-01-14 11:38:37 +08:00
|
|
|
}
|
2003-04-25 06:24:58 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
static void RemoveFunctionReferences(Module *M, const char *Name) {
|
2015-04-21 07:42:22 +08:00
|
|
|
auto *UsedVar = M->getGlobalVariable(Name, true);
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!UsedVar || !UsedVar->hasInitializer())
|
|
|
|
return;
|
2015-04-21 07:42:22 +08:00
|
|
|
if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) {
|
|
|
|
assert(UsedVar->use_empty());
|
|
|
|
UsedVar->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer());
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<Constant *> Used;
|
|
|
|
for (Value *V : OldUsedVal->operand_values()) {
|
2015-04-21 07:42:22 +08:00
|
|
|
Constant *Op = cast<Constant>(V->stripPointerCasts());
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!Op->isNullValue()) {
|
2015-04-21 07:42:22 +08:00
|
|
|
Used.push_back(cast<Constant>(V));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto *NewValElemTy = OldUsedVal->getType()->getElementType();
|
|
|
|
auto *NewValTy = ArrayType::get(NewValElemTy, Used.size());
|
|
|
|
auto *NewUsedVal = ConstantArray::get(NewValTy, Used);
|
|
|
|
UsedVar->mutateType(NewUsedVal->getType()->getPointerTo());
|
|
|
|
UsedVar->setInitializer(NewUsedVal);
|
|
|
|
}
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool ReduceCrashingFunctions::TestFuncs(std::vector<Function *> &Funcs) {
|
2013-09-02 09:18:56 +08:00
|
|
|
// If main isn't present, claim there is no problem.
|
2016-08-12 06:21:41 +08:00
|
|
|
if (KeepMain && !is_contained(Funcs, BD.getProgram()->getFunction("main")))
|
2006-03-06 06:21:36 +08:00
|
|
|
return false;
|
|
|
|
|
2003-10-11 01:57:28 +08:00
|
|
|
// Clone the program to try hacking it apart...
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 04:21:20 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2003-04-25 06:24:58 +08:00
|
|
|
// Convert list to set for fast lookup...
|
2016-09-02 09:21:37 +08:00
|
|
|
std::set<Function *> Functions;
|
2003-04-25 06:24:58 +08:00
|
|
|
for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
|
2010-06-24 08:33:28 +08:00
|
|
|
Function *CMF = cast<Function>(VMap[Funcs[i]]);
|
2003-04-25 06:24:58 +08:00
|
|
|
assert(CMF && "Function not in module?!");
|
2007-02-06 04:47:22 +08:00
|
|
|
assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
|
2009-03-06 10:16:23 +08:00
|
|
|
assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
|
2003-04-25 06:54:06 +08:00
|
|
|
Functions.insert(CMF);
|
2003-04-25 06:24:58 +08:00
|
|
|
}
|
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Checking for crash with only these functions: ";
|
2004-03-15 04:50:42 +08:00
|
|
|
PrintFunctionList(Funcs);
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << ": ";
|
2015-04-21 07:42:22 +08:00
|
|
|
if (!ReplaceFuncsWithNull) {
|
|
|
|
// Loop over and delete any functions which we aren't supposed to be playing
|
|
|
|
// with...
|
2015-10-21 03:36:39 +08:00
|
|
|
for (Function &I : *M)
|
|
|
|
if (!I.isDeclaration() && !Functions.count(&I))
|
|
|
|
DeleteFunctionBody(&I);
|
2015-04-21 07:42:22 +08:00
|
|
|
} else {
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<GlobalValue *> ToRemove;
|
2015-04-21 07:42:22 +08:00
|
|
|
// First, remove aliases to functions we're about to purge.
|
|
|
|
for (GlobalAlias &Alias : M->aliases()) {
|
2016-05-04 08:20:48 +08:00
|
|
|
GlobalObject *Root = Alias.getBaseObject();
|
|
|
|
Function *F = dyn_cast_or_null<Function>(Root);
|
2015-04-21 07:42:22 +08:00
|
|
|
if (F) {
|
|
|
|
if (Functions.count(F))
|
|
|
|
// We're keeping this function.
|
|
|
|
continue;
|
|
|
|
} else if (Root->isNullValue()) {
|
|
|
|
// This referenced a globalalias that we've already replaced,
|
|
|
|
// so we still need to replace this alias.
|
|
|
|
} else if (!F) {
|
|
|
|
// Not a function, therefore not something we mess with.
|
|
|
|
continue;
|
|
|
|
}
|
2003-04-25 06:24:58 +08:00
|
|
|
|
2015-04-21 07:42:22 +08:00
|
|
|
PointerType *Ty = cast<PointerType>(Alias.getType());
|
|
|
|
Constant *Replacement = ConstantPointerNull::get(Ty);
|
|
|
|
Alias.replaceAllUsesWith(Replacement);
|
|
|
|
ToRemove.push_back(&Alias);
|
|
|
|
}
|
|
|
|
|
2015-10-21 03:36:39 +08:00
|
|
|
for (Function &I : *M) {
|
|
|
|
if (!I.isDeclaration() && !Functions.count(&I)) {
|
|
|
|
PointerType *Ty = cast<PointerType>(I.getType());
|
2015-04-21 07:42:22 +08:00
|
|
|
Constant *Replacement = ConstantPointerNull::get(Ty);
|
2015-10-21 03:36:39 +08:00
|
|
|
I.replaceAllUsesWith(Replacement);
|
|
|
|
ToRemove.push_back(&I);
|
2015-04-21 07:42:22 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-25 06:24:58 +08:00
|
|
|
|
2015-04-21 07:42:22 +08:00
|
|
|
for (auto *F : ToRemove) {
|
|
|
|
F->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, remove any null members from any global intrinsic.
|
2018-02-15 04:21:20 +08:00
|
|
|
RemoveFunctionReferences(M.get(), "llvm.used");
|
|
|
|
RemoveFunctionReferences(M.get(), "llvm.compiler.used");
|
2015-04-21 07:42:22 +08:00
|
|
|
}
|
2003-04-25 06:24:58 +08:00
|
|
|
// Try running the hacked up program...
|
2018-02-15 04:21:20 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2003-04-25 06:24:58 +08:00
|
|
|
|
|
|
|
// Make sure to use function pointers that point into the now-current
|
|
|
|
// module.
|
|
|
|
Funcs.assign(Functions.begin(), Functions.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:29:46 +08:00
|
|
|
namespace {
|
2016-07-29 06:29:25 +08:00
|
|
|
/// Simplify the CFG without completely destroying it.
|
|
|
|
/// This is not well defined, but basically comes down to "try to eliminate
|
|
|
|
/// unreachable blocks and constant fold terminators without deciding that
|
|
|
|
/// certain undefined behavior cuts off the program at the legs".
|
|
|
|
void simpleSimplifyCfg(Function &F, SmallVectorImpl<BasicBlock *> &BBs) {
|
|
|
|
if (F.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto *BB : BBs) {
|
|
|
|
ConstantFoldTerminator(BB);
|
|
|
|
MergeBlockIntoPredecessor(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove unreachable blocks
|
|
|
|
// removeUnreachableBlocks can't be used here, it will turn various
|
|
|
|
// undefined behavior into unreachables, but bugpoint was the thing that
|
|
|
|
// generated the undefined behavior, and we don't want it to kill the entire
|
|
|
|
// program.
|
|
|
|
SmallPtrSet<BasicBlock *, 16> Visited;
|
|
|
|
for (auto *BB : depth_first(&F.getEntryBlock()))
|
|
|
|
Visited.insert(BB);
|
|
|
|
|
|
|
|
SmallVector<BasicBlock *, 16> Unreachable;
|
|
|
|
for (auto &BB : F)
|
|
|
|
if (!Visited.count(&BB))
|
|
|
|
Unreachable.push_back(&BB);
|
|
|
|
|
|
|
|
// The dead BB's may be in a dead cycle or otherwise have references to each
|
|
|
|
// other. Because of this, we have to drop all references first, then delete
|
|
|
|
// them all at once.
|
2016-09-02 09:21:37 +08:00
|
|
|
for (auto *BB : Unreachable) {
|
2016-07-29 06:29:25 +08:00
|
|
|
for (BasicBlock *Successor : successors(&*BB))
|
|
|
|
if (Visited.count(Successor))
|
|
|
|
Successor->removePredecessor(&*BB);
|
|
|
|
BB->dropAllReferences();
|
|
|
|
}
|
|
|
|
for (auto *BB : Unreachable)
|
|
|
|
BB->eraseFromParent();
|
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
/// ReduceCrashingBlocks reducer - This works by setting the terminators of
|
|
|
|
/// all terminators except the specified basic blocks to a 'ret' instruction,
|
|
|
|
/// then running the simplify-cfg pass. This has the effect of chopping up
|
|
|
|
/// the CFG really fast which can reduce large functions quickly.
|
|
|
|
///
|
|
|
|
class ReduceCrashingBlocks : public ListReducer<const BasicBlock *> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2016-09-02 09:21:37 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceCrashingBlocks(BugDriver &BD, BugTester testFn)
|
2016-07-29 06:29:25 +08:00
|
|
|
: BD(BD), TestFn(testFn) {}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<const BasicBlock *> &Prefix,
|
|
|
|
std::vector<const BasicBlock *> &Kept) override {
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!Kept.empty() && TestBlocks(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestBlocks(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool TestBlocks(std::vector<const BasicBlock *> &Prefix);
|
|
|
|
};
|
2004-01-14 11:38:37 +08:00
|
|
|
}
|
2003-04-25 07:51:38 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock *> &BBs) {
|
2003-10-11 01:57:28 +08:00
|
|
|
// Clone the program to try hacking it apart...
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2003-04-25 07:51:38 +08:00
|
|
|
// Convert list to set for fast lookup...
|
2016-09-02 09:21:37 +08:00
|
|
|
SmallPtrSet<BasicBlock *, 8> Blocks;
|
2009-04-04 17:39:23 +08:00
|
|
|
for (unsigned i = 0, e = BBs.size(); i != e; ++i)
|
2010-06-24 08:33:28 +08:00
|
|
|
Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
|
2003-04-25 07:51:38 +08:00
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Checking for crash with only these blocks:";
|
2003-10-27 12:44:59 +08:00
|
|
|
unsigned NumPrint = Blocks.size();
|
2016-09-02 09:21:37 +08:00
|
|
|
if (NumPrint > 10)
|
|
|
|
NumPrint = 10;
|
2003-10-27 12:44:59 +08:00
|
|
|
for (unsigned i = 0, e = NumPrint; i != e; ++i)
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << " " << BBs[i]->getName();
|
2003-10-27 12:44:59 +08:00
|
|
|
if (NumPrint < Blocks.size())
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "... <" << Blocks.size() << " total>";
|
|
|
|
outs() << ": ";
|
2003-04-25 07:51:38 +08:00
|
|
|
|
|
|
|
// Loop over and delete any hack up any blocks that are not listed...
|
2018-02-09 13:09:50 +08:00
|
|
|
for (Function &F : M->functions()) {
|
|
|
|
for (BasicBlock &BB : F) {
|
|
|
|
if (!Blocks.count(&BB) && BB.getTerminator()->getNumSuccessors()) {
|
2003-04-25 07:51:38 +08:00
|
|
|
// Loop over all of the successors of this block, deleting any PHI nodes
|
|
|
|
// that might include it.
|
2018-02-09 13:09:50 +08:00
|
|
|
for (BasicBlock *Succ : successors(&BB))
|
|
|
|
Succ->removePredecessor(&BB);
|
2003-04-25 07:51:38 +08:00
|
|
|
|
2018-02-09 13:09:50 +08:00
|
|
|
TerminatorInst *BBTerm = BB.getTerminator();
|
2016-06-29 08:15:35 +08:00
|
|
|
if (BBTerm->isEHPad() || BBTerm->getType()->isTokenTy())
|
2015-11-08 12:16:12 +08:00
|
|
|
continue;
|
2016-06-29 08:15:35 +08:00
|
|
|
if (!BBTerm->getType()->isVoidTy())
|
2009-08-01 04:28:14 +08:00
|
|
|
BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
|
2003-11-22 10:10:38 +08:00
|
|
|
|
2008-04-28 08:04:58 +08:00
|
|
|
// Replace the old terminator instruction.
|
2018-02-09 13:09:50 +08:00
|
|
|
BB.getInstList().pop_back();
|
|
|
|
new UnreachableInst(BB.getContext(), &BB);
|
2003-04-25 07:51:38 +08:00
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-25 07:51:38 +08:00
|
|
|
|
|
|
|
// The CFG Simplifier pass may delete one of the basic blocks we are
|
|
|
|
// interested in. If it does we need to take the block out of the list. Make
|
|
|
|
// a "persistent mapping" by turning basic blocks into <function, name> pairs.
|
|
|
|
// This won't work well if blocks are unnamed, but that is just the risk we
|
2018-02-09 13:09:50 +08:00
|
|
|
// have to take. FIXME: Can we just name the blocks?
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<std::pair<std::string, std::string>> BlockInfo;
|
2003-04-25 07:51:38 +08:00
|
|
|
|
2014-08-25 07:23:06 +08:00
|
|
|
for (BasicBlock *BB : Blocks)
|
2015-05-30 03:43:39 +08:00
|
|
|
BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName());
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2016-07-29 06:29:25 +08:00
|
|
|
SmallVector<BasicBlock *, 16> ToProcess;
|
2016-09-02 09:21:37 +08:00
|
|
|
for (auto &F : *M) {
|
2016-07-29 06:29:25 +08:00
|
|
|
for (auto &BB : F)
|
|
|
|
if (!Blocks.count(&BB))
|
|
|
|
ToProcess.push_back(&BB);
|
|
|
|
simpleSimplifyCfg(F, ToProcess);
|
|
|
|
ToProcess.clear();
|
|
|
|
}
|
|
|
|
// Verify we didn't break anything
|
2010-08-10 23:46:11 +08:00
|
|
|
std::vector<std::string> Passes;
|
|
|
|
Passes.push_back("verify");
|
2018-02-09 13:09:50 +08:00
|
|
|
std::unique_ptr<Module> New = BD.runPassesOn(M.get(), Passes);
|
2010-08-10 23:46:11 +08:00
|
|
|
if (!New) {
|
2016-07-29 06:29:25 +08:00
|
|
|
errs() << "verify failed!\n";
|
2010-08-10 23:46:11 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
M = std::move(New);
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2003-04-25 07:51:38 +08:00
|
|
|
// Try running on the hacked up program...
|
2018-02-09 13:09:50 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2003-04-25 07:51:38 +08:00
|
|
|
|
|
|
|
// Make sure to use basic block pointers that point into the now-current
|
|
|
|
// module, and that they don't include any deleted blocks.
|
|
|
|
BBs.clear();
|
2018-02-09 13:09:50 +08:00
|
|
|
const ValueSymbolTable &GST = BD.getProgram()->getValueSymbolTable();
|
|
|
|
for (const auto &BI : BlockInfo) {
|
|
|
|
Function *F = cast<Function>(GST.lookup(BI.first));
|
|
|
|
Value *V = F->getValueSymbolTable()->lookup(BI.second);
|
2009-08-14 05:58:54 +08:00
|
|
|
if (V && V->getType() == Type::getLabelTy(V->getContext()))
|
2007-02-06 04:47:22 +08:00
|
|
|
BBs.push_back(cast<BasicBlock>(V));
|
2003-04-25 07:51:38 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
// It didn't crash, try something else.
|
2003-04-25 07:51:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
namespace {
|
|
|
|
/// ReduceCrashingConditionals reducer - This works by changing
|
|
|
|
/// conditional branches to unconditional ones, then simplifying the CFG
|
|
|
|
/// This has the effect of chopping up the CFG really fast which can reduce
|
|
|
|
/// large functions quickly.
|
|
|
|
///
|
|
|
|
class ReduceCrashingConditionals : public ListReducer<const BasicBlock *> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
bool Direction;
|
|
|
|
|
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceCrashingConditionals(BugDriver &bd, BugTester testFn, bool Direction)
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
: BD(bd), TestFn(testFn), Direction(Direction) {}
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<const BasicBlock *> &Prefix,
|
|
|
|
std::vector<const BasicBlock *> &Kept) override {
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
if (!Kept.empty() && TestBlocks(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestBlocks(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestBlocks(std::vector<const BasicBlock *> &Prefix);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReduceCrashingConditionals::TestBlocks(
|
|
|
|
std::vector<const BasicBlock *> &BBs) {
|
|
|
|
// Clone the program to try hacking it apart...
|
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
|
|
|
|
// Convert list to set for fast lookup...
|
|
|
|
SmallPtrSet<const BasicBlock *, 8> Blocks;
|
2016-09-02 09:21:37 +08:00
|
|
|
for (const auto *BB : BBs)
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
Blocks.insert(cast<BasicBlock>(VMap[BB]));
|
|
|
|
|
|
|
|
outs() << "Checking for crash with changing conditionals to always jump to "
|
|
|
|
<< (Direction ? "true" : "false") << ":";
|
|
|
|
unsigned NumPrint = Blocks.size();
|
|
|
|
if (NumPrint > 10)
|
|
|
|
NumPrint = 10;
|
|
|
|
for (unsigned i = 0, e = NumPrint; i != e; ++i)
|
|
|
|
outs() << " " << BBs[i]->getName();
|
|
|
|
if (NumPrint < Blocks.size())
|
|
|
|
outs() << "... <" << Blocks.size() << " total>";
|
|
|
|
outs() << ": ";
|
|
|
|
|
|
|
|
// Loop over and delete any hack up any blocks that are not listed...
|
2016-09-02 09:21:37 +08:00
|
|
|
for (auto &F : *M)
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
for (auto &BB : F)
|
|
|
|
if (!Blocks.count(&BB)) {
|
|
|
|
auto *BR = dyn_cast<BranchInst>(BB.getTerminator());
|
|
|
|
if (!BR || !BR->isConditional())
|
|
|
|
continue;
|
|
|
|
if (Direction)
|
|
|
|
BR->setCondition(ConstantInt::getTrue(BR->getContext()));
|
|
|
|
else
|
|
|
|
BR->setCondition(ConstantInt::getFalse(BR->getContext()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following may destroy some blocks, so we save them first
|
|
|
|
std::vector<std::pair<std::string, std::string>> BlockInfo;
|
|
|
|
|
|
|
|
for (const BasicBlock *BB : Blocks)
|
|
|
|
BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName());
|
|
|
|
|
|
|
|
SmallVector<BasicBlock *, 16> ToProcess;
|
2016-09-02 09:21:37 +08:00
|
|
|
for (auto &F : *M) {
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
for (auto &BB : F)
|
|
|
|
if (!Blocks.count(&BB))
|
|
|
|
ToProcess.push_back(&BB);
|
|
|
|
simpleSimplifyCfg(F, ToProcess);
|
|
|
|
ToProcess.clear();
|
|
|
|
}
|
|
|
|
// Verify we didn't break anything
|
|
|
|
std::vector<std::string> Passes;
|
|
|
|
Passes.push_back("verify");
|
2018-02-09 13:09:50 +08:00
|
|
|
std::unique_ptr<Module> New = BD.runPassesOn(M.get(), Passes);
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
if (!New) {
|
|
|
|
errs() << "verify failed!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
M = std::move(New);
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
|
|
|
|
// Try running on the hacked up program...
|
2018-02-09 13:09:50 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
|
|
|
|
// Make sure to use basic block pointers that point into the now-current
|
|
|
|
// module, and that they don't include any deleted blocks.
|
|
|
|
BBs.clear();
|
2018-02-09 13:09:50 +08:00
|
|
|
const ValueSymbolTable &GST = BD.getProgram()->getValueSymbolTable();
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
for (auto &BI : BlockInfo) {
|
|
|
|
auto *F = cast<Function>(GST.lookup(BI.first));
|
2016-09-17 14:00:02 +08:00
|
|
|
Value *V = F->getValueSymbolTable()->lookup(BI.second);
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
if (V && V->getType() == Type::getLabelTy(V->getContext()))
|
|
|
|
BBs.push_back(cast<BasicBlock>(V));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
// It didn't crash, try something else.
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-29 06:29:25 +08:00
|
|
|
namespace {
|
|
|
|
/// SimplifyCFG reducer - This works by calling SimplifyCFG on each basic block
|
|
|
|
/// in the program.
|
|
|
|
|
|
|
|
class ReduceSimplifyCFG : public ListReducer<const BasicBlock *> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2016-07-29 06:29:25 +08:00
|
|
|
TargetTransformInfo TTI;
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2016-07-29 06:29:25 +08:00
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceSimplifyCFG(BugDriver &bd, BugTester testFn)
|
2016-09-02 09:21:37 +08:00
|
|
|
: BD(bd), TestFn(testFn), TTI(bd.getProgram()->getDataLayout()) {}
|
2016-07-29 06:29:25 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<const BasicBlock *> &Prefix,
|
|
|
|
std::vector<const BasicBlock *> &Kept) override {
|
2016-07-29 06:29:25 +08:00
|
|
|
if (!Kept.empty() && TestBlocks(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestBlocks(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestBlocks(std::vector<const BasicBlock *> &Prefix);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool ReduceSimplifyCFG::TestBlocks(std::vector<const BasicBlock *> &BBs) {
|
2016-07-29 06:29:25 +08:00
|
|
|
// Clone the program to try hacking it apart...
|
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2016-07-29 06:29:25 +08:00
|
|
|
|
|
|
|
// Convert list to set for fast lookup...
|
|
|
|
SmallPtrSet<const BasicBlock *, 8> Blocks;
|
2016-09-02 09:21:37 +08:00
|
|
|
for (const auto *BB : BBs)
|
2016-07-29 06:29:25 +08:00
|
|
|
Blocks.insert(cast<BasicBlock>(VMap[BB]));
|
|
|
|
|
|
|
|
outs() << "Checking for crash with CFG simplifying:";
|
|
|
|
unsigned NumPrint = Blocks.size();
|
|
|
|
if (NumPrint > 10)
|
|
|
|
NumPrint = 10;
|
|
|
|
for (unsigned i = 0, e = NumPrint; i != e; ++i)
|
|
|
|
outs() << " " << BBs[i]->getName();
|
|
|
|
if (NumPrint < Blocks.size())
|
|
|
|
outs() << "... <" << Blocks.size() << " total>";
|
|
|
|
outs() << ": ";
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
// The following may destroy some blocks, so we save them first
|
2016-07-29 06:29:25 +08:00
|
|
|
std::vector<std::pair<std::string, std::string>> BlockInfo;
|
|
|
|
|
|
|
|
for (const BasicBlock *BB : Blocks)
|
|
|
|
BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName());
|
|
|
|
|
|
|
|
// Loop over and delete any hack up any blocks that are not listed...
|
2016-09-02 09:21:37 +08:00
|
|
|
for (auto &F : *M)
|
|
|
|
// Loop over all of the basic blocks and remove them if they are unneeded.
|
|
|
|
for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
|
|
|
|
if (!Blocks.count(&*BBIt)) {
|
|
|
|
++BBIt;
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-05 04:26:25 +08:00
|
|
|
simplifyCFG(&*BBIt++, TTI);
|
2016-09-02 09:21:37 +08:00
|
|
|
}
|
2016-07-29 06:29:25 +08:00
|
|
|
// Verify we didn't break anything
|
|
|
|
std::vector<std::string> Passes;
|
|
|
|
Passes.push_back("verify");
|
2018-02-09 13:09:50 +08:00
|
|
|
std::unique_ptr<Module> New = BD.runPassesOn(M.get(), Passes);
|
2016-07-29 06:29:25 +08:00
|
|
|
if (!New) {
|
|
|
|
errs() << "verify failed!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
M = std::move(New);
|
2016-07-29 06:29:25 +08:00
|
|
|
|
|
|
|
// Try running on the hacked up program...
|
2018-02-09 13:09:50 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2016-07-29 06:29:25 +08:00
|
|
|
|
|
|
|
// Make sure to use basic block pointers that point into the now-current
|
|
|
|
// module, and that they don't include any deleted blocks.
|
|
|
|
BBs.clear();
|
2018-02-09 13:09:50 +08:00
|
|
|
const ValueSymbolTable &GST = BD.getProgram()->getValueSymbolTable();
|
2016-09-02 09:21:37 +08:00
|
|
|
for (auto &BI : BlockInfo) {
|
2016-07-29 06:29:25 +08:00
|
|
|
auto *F = cast<Function>(GST.lookup(BI.first));
|
2016-09-17 14:00:02 +08:00
|
|
|
Value *V = F->getValueSymbolTable()->lookup(BI.second);
|
2016-07-29 06:29:25 +08:00
|
|
|
if (V && V->getType() == Type::getLabelTy(V->getContext()))
|
|
|
|
BBs.push_back(cast<BasicBlock>(V));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-09 13:09:50 +08:00
|
|
|
// It didn't crash, try something else.
|
2016-07-29 06:29:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-25 13:30:00 +08:00
|
|
|
namespace {
|
2016-09-02 09:21:37 +08:00
|
|
|
/// ReduceCrashingInstructions reducer - This works by removing the specified
|
|
|
|
/// non-terminator instructions and replacing them with undef.
|
|
|
|
///
|
|
|
|
class ReduceCrashingInstructions : public ListReducer<const Instruction *> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2016-09-02 09:21:37 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceCrashingInstructions(BugDriver &bd, BugTester testFn)
|
2009-05-25 13:30:00 +08:00
|
|
|
: BD(bd), TestFn(testFn) {}
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<const Instruction *> &Prefix,
|
|
|
|
std::vector<const Instruction *> &Kept) override {
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!Kept.empty() && TestInsts(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestInsts(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
2009-05-25 13:30:00 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool TestInsts(std::vector<const Instruction *> &Prefix);
|
|
|
|
};
|
2009-05-25 13:30:00 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool ReduceCrashingInstructions::TestInsts(
|
|
|
|
std::vector<const Instruction *> &Insts) {
|
2009-05-25 13:30:00 +08:00
|
|
|
// Clone the program to try hacking it apart...
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 04:25:18 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2009-05-25 13:30:00 +08:00
|
|
|
|
|
|
|
// Convert list to set for fast lookup...
|
2016-09-02 09:21:37 +08:00
|
|
|
SmallPtrSet<Instruction *, 32> Instructions;
|
2009-05-25 13:30:00 +08:00
|
|
|
for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
|
|
|
|
assert(!isa<TerminatorInst>(Insts[i]));
|
2010-06-24 08:33:28 +08:00
|
|
|
Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
|
2009-05-25 13:30:00 +08:00
|
|
|
}
|
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Checking for crash with only " << Instructions.size();
|
2009-05-25 13:30:00 +08:00
|
|
|
if (Instructions.size() == 1)
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << " instruction: ";
|
2009-05-25 13:30:00 +08:00
|
|
|
else
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << " instructions: ";
|
2009-05-25 13:30:00 +08:00
|
|
|
|
|
|
|
for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
|
|
|
|
for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
|
|
|
|
for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
|
2015-10-21 03:36:39 +08:00
|
|
|
Instruction *Inst = &*I++;
|
2011-11-01 12:40:56 +08:00
|
|
|
if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
|
2017-03-08 04:28:59 +08:00
|
|
|
!Inst->isEHPad() && !Inst->getType()->isTokenTy() &&
|
|
|
|
!Inst->isSwiftError()) {
|
2016-06-29 08:15:35 +08:00
|
|
|
if (!Inst->getType()->isVoidTy())
|
2009-05-25 13:30:00 +08:00
|
|
|
Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
|
|
|
|
Inst->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that this is still valid.
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager Passes;
|
2016-10-19 00:24:43 +08:00
|
|
|
Passes.add(createVerifierPass(/*FatalErrors=*/false));
|
2009-05-25 13:30:00 +08:00
|
|
|
Passes.run(*M);
|
|
|
|
|
|
|
|
// Try running on the hacked up program...
|
2018-02-15 04:25:18 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2009-05-25 13:30:00 +08:00
|
|
|
|
|
|
|
// Make sure to use instruction pointers that point into the now-current
|
|
|
|
// module, and that they don't include any deleted blocks.
|
|
|
|
Insts.clear();
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *Inst : Instructions)
|
|
|
|
Insts.push_back(Inst);
|
2009-05-25 13:30:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-15 04:25:18 +08:00
|
|
|
// It didn't crash, try something else.
|
2009-05-25 13:30:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-06 08:12:50 +08:00
|
|
|
namespace {
|
|
|
|
// Reduce the list of Named Metadata nodes. We keep this as a list of
|
|
|
|
// names to avoid having to convert back and forth every time.
|
|
|
|
class ReduceCrashingNamedMD : public ListReducer<std::string> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2015-11-06 08:12:50 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceCrashingNamedMD(BugDriver &bd, BugTester testFn)
|
2015-11-06 08:12:50 +08:00
|
|
|
: BD(bd), TestFn(testFn) {}
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<std::string> &Prefix,
|
|
|
|
std::vector<std::string> &Kept) override {
|
2015-11-06 08:12:50 +08:00
|
|
|
if (!Kept.empty() && TestNamedMDs(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestNamedMDs(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestNamedMDs(std::vector<std::string> &NamedMDs);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &NamedMDs) {
|
|
|
|
|
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 04:53:38 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2015-11-06 08:12:50 +08:00
|
|
|
|
|
|
|
outs() << "Checking for crash with only these named metadata nodes:";
|
|
|
|
unsigned NumPrint = std::min<size_t>(NamedMDs.size(), 10);
|
|
|
|
for (unsigned i = 0, e = NumPrint; i != e; ++i)
|
|
|
|
outs() << " " << NamedMDs[i];
|
|
|
|
if (NumPrint < NamedMDs.size())
|
|
|
|
outs() << "... <" << NamedMDs.size() << " total>";
|
|
|
|
outs() << ": ";
|
|
|
|
|
|
|
|
// Make a StringMap for faster lookup
|
|
|
|
StringSet<> Names;
|
|
|
|
for (const std::string &Name : NamedMDs)
|
|
|
|
Names.insert(Name);
|
|
|
|
|
|
|
|
// First collect all the metadata to delete in a vector, then
|
|
|
|
// delete them all at once to avoid invalidating the iterator
|
|
|
|
std::vector<NamedMDNode *> ToDelete;
|
|
|
|
ToDelete.reserve(M->named_metadata_size() - Names.size());
|
|
|
|
for (auto &NamedMD : M->named_metadata())
|
2016-03-29 05:06:26 +08:00
|
|
|
// Always keep a nonempty llvm.dbg.cu because the Verifier would complain.
|
|
|
|
if (!Names.count(NamedMD.getName()) &&
|
|
|
|
(!(NamedMD.getName() == "llvm.dbg.cu" && NamedMD.getNumOperands() > 0)))
|
2015-11-06 08:12:50 +08:00
|
|
|
ToDelete.push_back(&NamedMD);
|
|
|
|
|
|
|
|
for (auto *NamedMD : ToDelete)
|
|
|
|
NamedMD->eraseFromParent();
|
|
|
|
|
|
|
|
// Verify that this is still valid.
|
|
|
|
legacy::PassManager Passes;
|
2016-10-19 00:24:43 +08:00
|
|
|
Passes.add(createVerifierPass(/*FatalErrors=*/false));
|
2015-11-06 08:12:50 +08:00
|
|
|
Passes.run(*M);
|
|
|
|
|
|
|
|
// Try running on the hacked up program...
|
2018-02-15 04:53:38 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2015-11-06 08:12:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Reduce the list of operands to named metadata nodes
|
|
|
|
class ReduceCrashingNamedMDOps : public ListReducer<const MDNode *> {
|
|
|
|
BugDriver &BD;
|
2018-02-09 02:46:49 +08:00
|
|
|
BugTester TestFn;
|
2015-11-06 08:12:50 +08:00
|
|
|
|
|
|
|
public:
|
2018-02-09 02:46:49 +08:00
|
|
|
ReduceCrashingNamedMDOps(BugDriver &bd, BugTester testFn)
|
2015-11-06 08:12:50 +08:00
|
|
|
: BD(bd), TestFn(testFn) {}
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> doTest(std::vector<const MDNode *> &Prefix,
|
|
|
|
std::vector<const MDNode *> &Kept) override {
|
2015-11-06 08:12:50 +08:00
|
|
|
if (!Kept.empty() && TestNamedMDOps(Kept))
|
|
|
|
return KeepSuffix;
|
|
|
|
if (!Prefix.empty() && TestNamedMDOps(Prefix))
|
|
|
|
return KeepPrefix;
|
|
|
|
return NoFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestNamedMDOps(std::vector<const MDNode *> &NamedMDOps);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReduceCrashingNamedMDOps::TestNamedMDOps(
|
|
|
|
std::vector<const MDNode *> &NamedMDOps) {
|
|
|
|
// Convert list to set for fast lookup...
|
2016-01-30 09:24:31 +08:00
|
|
|
SmallPtrSet<const MDNode *, 32> OldMDNodeOps;
|
2015-11-06 08:12:50 +08:00
|
|
|
for (unsigned i = 0, e = NamedMDOps.size(); i != e; ++i) {
|
|
|
|
OldMDNodeOps.insert(NamedMDOps[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
outs() << "Checking for crash with only " << OldMDNodeOps.size();
|
|
|
|
if (OldMDNodeOps.size() == 1)
|
|
|
|
outs() << " named metadata operand: ";
|
|
|
|
else
|
|
|
|
outs() << " named metadata operands: ";
|
|
|
|
|
|
|
|
ValueToValueMapTy VMap;
|
2018-02-15 04:59:39 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram(), VMap);
|
2015-11-06 08:12:50 +08:00
|
|
|
|
|
|
|
// This is a little wasteful. In the future it might be good if we could have
|
|
|
|
// these dropped during cloning.
|
|
|
|
for (auto &NamedMD : BD.getProgram()->named_metadata()) {
|
|
|
|
// Drop the old one and create a new one
|
|
|
|
M->eraseNamedMetadata(M->getNamedMetadata(NamedMD.getName()));
|
|
|
|
NamedMDNode *NewNamedMDNode =
|
|
|
|
M->getOrInsertNamedMetadata(NamedMD.getName());
|
|
|
|
for (MDNode *op : NamedMD.operands())
|
|
|
|
if (OldMDNodeOps.count(op))
|
|
|
|
NewNamedMDNode->addOperand(cast<MDNode>(MapMetadata(op, VMap)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that this is still valid.
|
|
|
|
legacy::PassManager Passes;
|
2016-10-19 00:24:43 +08:00
|
|
|
Passes.add(createVerifierPass(/*FatalErrors=*/false));
|
2015-11-06 08:12:50 +08:00
|
|
|
Passes.run(*M);
|
|
|
|
|
|
|
|
// Try running on the hacked up program...
|
2018-02-15 04:59:39 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
2015-11-06 08:12:50 +08:00
|
|
|
// Make sure to use instruction pointers that point into the now-current
|
|
|
|
// module, and that they don't include any deleted blocks.
|
|
|
|
NamedMDOps.clear();
|
|
|
|
for (const MDNode *Node : OldMDNodeOps)
|
2016-04-03 01:04:38 +08:00
|
|
|
NamedMDOps.push_back(cast<MDNode>(*VMap.getMappedMD(Node)));
|
2015-11-06 08:12:50 +08:00
|
|
|
|
2018-02-15 04:59:39 +08:00
|
|
|
BD.setNewProgram(M.release()); // It crashed, keep the trimmed version...
|
2015-11-06 08:12:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-15 04:59:39 +08:00
|
|
|
// It didn't crash, try something else.
|
2015-11-06 08:12:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-09 04:27:09 +08:00
|
|
|
/// Attempt to eliminate as many global initializers as possible.
|
2018-02-09 02:46:49 +08:00
|
|
|
static Error ReduceGlobalInitializers(BugDriver &BD, BugTester TestFn) {
|
2018-02-09 04:27:09 +08:00
|
|
|
Module *OrigM = BD.getProgram();
|
|
|
|
if (OrigM->global_empty())
|
|
|
|
return Error::success();
|
|
|
|
|
|
|
|
// Now try to reduce the number of global variable initializers in the
|
|
|
|
// module to something small.
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*OrigM);
|
2018-02-09 04:27:09 +08:00
|
|
|
bool DeletedInit = false;
|
|
|
|
|
|
|
|
for (GlobalVariable &GV : M->globals()) {
|
|
|
|
if (GV.hasInitializer()) {
|
|
|
|
DeleteGlobalInitializer(&GV);
|
|
|
|
GV.setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
GV.setComdat(nullptr);
|
|
|
|
DeletedInit = true;
|
2003-04-25 08:53:05 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-09 04:27:09 +08:00
|
|
|
|
|
|
|
if (!DeletedInit)
|
|
|
|
return Error::success();
|
|
|
|
|
|
|
|
// See if the program still causes a crash...
|
|
|
|
outs() << "\nChecking to see if we can delete global inits: ";
|
|
|
|
|
|
|
|
if (TestFn(BD, M.get())) { // Still crashes?
|
|
|
|
BD.setNewProgram(M.release());
|
|
|
|
outs() << "\n*** Able to remove all global initializers!\n";
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No longer crashes.
|
|
|
|
outs() << " - Removing all global inits hides problem!\n";
|
|
|
|
|
|
|
|
std::vector<GlobalVariable *> GVs;
|
|
|
|
for (GlobalVariable &GV : OrigM->globals())
|
|
|
|
if (GV.hasInitializer())
|
|
|
|
GVs.push_back(&GV);
|
|
|
|
|
|
|
|
if (GVs.size() > 1 && !BugpointIsInterrupted) {
|
|
|
|
outs() << "\n*** Attempting to reduce the number of global initializers "
|
|
|
|
<< "in the testcase\n";
|
|
|
|
|
|
|
|
unsigned OldSize = GVs.size();
|
|
|
|
Expected<bool> Result =
|
|
|
|
ReduceCrashingGlobalInitializers(BD, TestFn).reduceList(GVs);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
|
|
|
|
|
|
|
if (GVs.size() < OldSize)
|
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
|
|
|
|
}
|
2016-09-07 01:18:22 +08:00
|
|
|
return Error::success();
|
2016-06-29 08:43:18 +08:00
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2018-02-09 02:46:49 +08:00
|
|
|
static Error ReduceInsts(BugDriver &BD, BugTester TestFn) {
|
2009-05-25 13:30:00 +08:00
|
|
|
// Attempt to delete instructions using bisection. This should help out nasty
|
|
|
|
// cases with large basic blocks where the problem is at one end.
|
|
|
|
if (!BugpointIsInterrupted) {
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<const Instruction *> Insts;
|
2015-10-21 03:36:39 +08:00
|
|
|
for (const Function &F : *BD.getProgram())
|
|
|
|
for (const BasicBlock &BB : F)
|
|
|
|
for (const Instruction &I : BB)
|
|
|
|
if (!isa<TerminatorInst>(&I))
|
|
|
|
Insts.push_back(&I);
|
2009-05-25 13:30:00 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result =
|
|
|
|
ReduceCrashingInstructions(BD, TestFn).reduceList(Insts);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
2009-05-25 13:30:00 +08:00
|
|
|
}
|
|
|
|
|
2004-03-14 03:35:54 +08:00
|
|
|
unsigned Simplification = 2;
|
2003-01-23 10:48:33 +08:00
|
|
|
do {
|
2016-06-29 08:43:18 +08:00
|
|
|
if (BugpointIsInterrupted)
|
2016-09-07 01:18:22 +08:00
|
|
|
// TODO: Should we distinguish this with an "interrupted error"?
|
|
|
|
return Error::success();
|
2003-01-23 10:48:33 +08:00
|
|
|
--Simplification;
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n*** Attempting to reduce testcase by deleting instruc"
|
|
|
|
<< "tions: Simplification Level #" << Simplification << '\n';
|
2003-01-23 10:48:33 +08:00
|
|
|
|
2003-08-18 22:43:39 +08:00
|
|
|
// Now that we have deleted the functions that are unnecessary for the
|
|
|
|
// program, try to remove instructions that are not necessary to cause the
|
2003-01-23 10:48:33 +08:00
|
|
|
// crash. To do this, we loop through all of the instructions in the
|
|
|
|
// remaining functions, deleting them (replacing any values produced with
|
|
|
|
// nulls), and then running ADCE and SimplifyCFG. If the transformed input
|
|
|
|
// still triggers failure, keep deleting until we cannot trigger failure
|
|
|
|
// anymore.
|
|
|
|
//
|
2004-02-19 07:59:11 +08:00
|
|
|
unsigned InstructionsToSkipBeforeDeleting = 0;
|
2003-01-23 10:48:33 +08:00
|
|
|
TryAgain:
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2003-01-23 10:48:33 +08:00
|
|
|
// Loop over all of the (non-terminator) instructions remaining in the
|
|
|
|
// function, attempting to delete them.
|
2004-02-19 07:59:11 +08:00
|
|
|
unsigned CurInstructionNum = 0;
|
2004-02-19 07:26:28 +08:00
|
|
|
for (Module::const_iterator FI = BD.getProgram()->begin(),
|
2016-09-02 09:21:37 +08:00
|
|
|
E = BD.getProgram()->end();
|
|
|
|
FI != E; ++FI)
|
2007-01-31 04:08:39 +08:00
|
|
|
if (!FI->isDeclaration())
|
2004-02-19 07:26:28 +08:00
|
|
|
for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
|
|
|
|
++BI)
|
|
|
|
for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
|
2011-12-25 14:56:22 +08:00
|
|
|
I != E; ++I, ++CurInstructionNum) {
|
2004-02-19 07:59:11 +08:00
|
|
|
if (InstructionsToSkipBeforeDeleting) {
|
|
|
|
--InstructionsToSkipBeforeDeleting;
|
|
|
|
} else {
|
2016-06-29 08:43:18 +08:00
|
|
|
if (BugpointIsInterrupted)
|
2016-09-07 01:18:22 +08:00
|
|
|
// TODO: Should this be some kind of interrupted error?
|
|
|
|
return Error::success();
|
2005-08-02 10:16:17 +08:00
|
|
|
|
2017-03-08 04:28:59 +08:00
|
|
|
if (I->isEHPad() || I->getType()->isTokenTy() ||
|
|
|
|
I->isSwiftError())
|
2011-11-01 12:40:56 +08:00
|
|
|
continue;
|
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Checking instruction: " << *I;
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> M =
|
2015-10-21 03:36:39 +08:00
|
|
|
BD.deleteInstructionFromProgram(&*I, Simplification);
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2004-02-19 07:59:11 +08:00
|
|
|
// Find out if the pass still crashes on this pass...
|
2014-08-27 01:19:03 +08:00
|
|
|
if (TestFn(BD, M.get())) {
|
2004-02-19 07:59:11 +08:00
|
|
|
// Yup, it does, we delete the old module, and continue trying
|
|
|
|
// to reduce the testcase...
|
2014-08-27 01:19:03 +08:00
|
|
|
BD.setNewProgram(M.release());
|
2004-02-19 07:59:11 +08:00
|
|
|
InstructionsToSkipBeforeDeleting = CurInstructionNum;
|
2016-09-02 09:21:37 +08:00
|
|
|
goto TryAgain; // I wish I had a multi-level break here!
|
2004-02-19 07:59:11 +08:00
|
|
|
}
|
2003-01-23 10:48:33 +08:00
|
|
|
}
|
2011-12-25 14:56:22 +08:00
|
|
|
}
|
2004-02-19 07:59:11 +08:00
|
|
|
|
|
|
|
if (InstructionsToSkipBeforeDeleting) {
|
|
|
|
InstructionsToSkipBeforeDeleting = 0;
|
|
|
|
goto TryAgain;
|
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2003-01-23 10:48:33 +08:00
|
|
|
} while (Simplification);
|
2016-06-29 08:10:39 +08:00
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-instructions");
|
2016-09-07 01:18:22 +08:00
|
|
|
return Error::success();
|
2016-06-29 08:43:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// DebugACrash - Given a predicate that determines whether a component crashes
|
|
|
|
/// on a program, try to destructively reduce the program while still keeping
|
|
|
|
/// the predicate true.
|
2018-02-09 02:46:49 +08:00
|
|
|
static Error DebugACrash(BugDriver &BD, BugTester TestFn) {
|
2016-06-29 08:43:18 +08:00
|
|
|
// See if we can get away with nuking some of the global variable initializers
|
|
|
|
// in the program...
|
|
|
|
if (!NoGlobalRM)
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = ReduceGlobalInitializers(BD, TestFn))
|
|
|
|
return E;
|
2016-06-29 08:43:18 +08:00
|
|
|
|
|
|
|
// Now try to reduce the number of functions in the module to something small.
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<Function *> Functions;
|
2016-06-29 08:43:18 +08:00
|
|
|
for (Function &F : *BD.getProgram())
|
|
|
|
if (!F.isDeclaration())
|
|
|
|
Functions.push_back(&F);
|
|
|
|
|
|
|
|
if (Functions.size() > 1 && !BugpointIsInterrupted) {
|
|
|
|
outs() << "\n*** Attempting to reduce the number of functions "
|
2016-09-02 09:21:37 +08:00
|
|
|
"in the testcase\n";
|
2016-06-29 08:43:18 +08:00
|
|
|
|
|
|
|
unsigned OldSize = Functions.size();
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result =
|
|
|
|
ReduceCrashingFunctions(BD, TestFn).reduceList(Functions);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
2016-06-29 08:43:18 +08:00
|
|
|
|
|
|
|
if (Functions.size() < OldSize)
|
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
|
|
|
|
}
|
|
|
|
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
// Attempt to change conditional branches into unconditional branches to
|
|
|
|
// eliminate blocks.
|
|
|
|
if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<const BasicBlock *> Blocks;
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
for (Function &F : *BD.getProgram())
|
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
Blocks.push_back(&BB);
|
|
|
|
unsigned OldSize = Blocks.size();
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result =
|
|
|
|
ReduceCrashingConditionals(BD, TestFn, true).reduceList(Blocks);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
|
|
|
Result = ReduceCrashingConditionals(BD, TestFn, false).reduceList(Blocks);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
Make bugpoint transform conditional jumps into unconditional jumps.
Summary:
Add a pass to bugpoint to make it transform conditional jumps into unconditional jumps.
Often, bugpoint generates output that has large numbers of br undef jumps, where
one side is dead.
What is happening is two fold:
1. It never tries to just pick a direction for the jump, and just see what happens
<<<< this patch
2. SimplifyCFG no longer is a good match for bugpoint's usecase. It
does too much.
Even things in SimplifyCFG, like removeUnreachableBlocks, go to great
lengths to transform undefined behavior into blocks and kill large
parts of the CFG. This is great for regular code, not so much for
bugpoint, which often generates UB on purpose (store undef is a great
example).
<<<< a followup patch that is coming, to move simplifycfg into a
separate reduction pass, and move the existing reduceCrashingBlocks
pass to use simpleSimplifyCFG.
Both of these patches significantly reduce the size and complexity of bugpoint
generated testcases.
Reviewers: chandlerc, majnemer
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D22841
llvm-svn: 276884
2016-07-28 00:13:25 +08:00
|
|
|
if (Blocks.size() < OldSize)
|
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-conditionals");
|
|
|
|
}
|
|
|
|
|
2016-06-29 08:43:18 +08:00
|
|
|
// Attempt to delete entire basic blocks at a time to speed up
|
|
|
|
// convergence... this actually works by setting the terminator of the blocks
|
|
|
|
// to a return instruction then running simplifycfg, which can potentially
|
|
|
|
// shrinks the code dramatically quickly
|
|
|
|
//
|
|
|
|
if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<const BasicBlock *> Blocks;
|
2016-06-29 08:43:18 +08:00
|
|
|
for (Function &F : *BD.getProgram())
|
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
Blocks.push_back(&BB);
|
|
|
|
unsigned OldSize = Blocks.size();
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result = ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
2016-06-29 08:43:18 +08:00
|
|
|
if (Blocks.size() < OldSize)
|
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
|
|
|
|
}
|
|
|
|
|
2017-04-14 23:21:15 +08:00
|
|
|
if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
|
2016-09-02 09:21:37 +08:00
|
|
|
std::vector<const BasicBlock *> Blocks;
|
2016-07-29 06:29:25 +08:00
|
|
|
for (Function &F : *BD.getProgram())
|
|
|
|
for (BasicBlock &BB : F)
|
|
|
|
Blocks.push_back(&BB);
|
|
|
|
unsigned OldSize = Blocks.size();
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result = ReduceSimplifyCFG(BD, TestFn).reduceList(Blocks);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
2016-07-29 06:29:25 +08:00
|
|
|
if (Blocks.size() < OldSize)
|
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplifycfg");
|
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2016-06-29 08:43:18 +08:00
|
|
|
// Attempt to delete instructions using bisection. This should help out nasty
|
|
|
|
// cases with large basic blocks where the problem is at one end.
|
|
|
|
if (!BugpointIsInterrupted)
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = ReduceInsts(BD, TestFn))
|
|
|
|
return E;
|
2015-11-06 08:12:50 +08:00
|
|
|
|
2016-10-26 02:44:13 +08:00
|
|
|
// Attempt to strip debug info metadata.
|
|
|
|
auto stripMetadata = [&](std::function<bool(Module &)> strip) {
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram());
|
2016-10-26 02:44:13 +08:00
|
|
|
strip(*M);
|
|
|
|
if (TestFn(BD, M.get()))
|
|
|
|
BD.setNewProgram(M.release());
|
|
|
|
};
|
|
|
|
if (!NoStripDebugInfo && !BugpointIsInterrupted) {
|
|
|
|
outs() << "\n*** Attempting to strip the debug info: ";
|
|
|
|
stripMetadata(StripDebugInfo);
|
|
|
|
}
|
|
|
|
if (!NoStripDebugTypeInfo && !BugpointIsInterrupted) {
|
|
|
|
outs() << "\n*** Attempting to strip the debug type info: ";
|
|
|
|
stripMetadata(stripNonLineTableDebugInfo);
|
|
|
|
}
|
|
|
|
|
2015-11-06 08:12:50 +08:00
|
|
|
if (!NoNamedMDRM) {
|
|
|
|
if (!BugpointIsInterrupted) {
|
|
|
|
// Try to reduce the amount of global metadata (particularly debug info),
|
|
|
|
// by dropping global named metadata that anchors them
|
|
|
|
outs() << "\n*** Attempting to remove named metadata: ";
|
|
|
|
std::vector<std::string> NamedMDNames;
|
|
|
|
for (auto &NamedMD : BD.getProgram()->named_metadata())
|
|
|
|
NamedMDNames.push_back(NamedMD.getName().str());
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result =
|
|
|
|
ReduceCrashingNamedMD(BD, TestFn).reduceList(NamedMDNames);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
2015-11-06 08:12:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!BugpointIsInterrupted) {
|
|
|
|
// Now that we quickly dropped all the named metadata that doesn't
|
|
|
|
// contribute to the crash, bisect the operands of the remaining ones
|
|
|
|
std::vector<const MDNode *> NamedMDOps;
|
|
|
|
for (auto &NamedMD : BD.getProgram()->named_metadata())
|
2015-11-06 08:45:47 +08:00
|
|
|
for (auto op : NamedMD.operands())
|
|
|
|
NamedMDOps.push_back(op);
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> Result =
|
|
|
|
ReduceCrashingNamedMDOps(BD, TestFn).reduceList(NamedMDOps);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
2015-11-06 08:12:50 +08:00
|
|
|
}
|
2016-06-29 08:10:39 +08:00
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-named-md");
|
2015-11-06 08:12:50 +08:00
|
|
|
}
|
|
|
|
|
2003-03-01 00:13:20 +08:00
|
|
|
// Try to clean up the testcase by running funcresolve and globaldce...
|
2005-08-02 10:16:17 +08:00
|
|
|
if (!BugpointIsInterrupted) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n*** Attempting to perform final cleanups: ";
|
2018-02-15 03:50:40 +08:00
|
|
|
std::unique_ptr<Module> M = CloneModule(*BD.getProgram());
|
2018-02-09 13:09:50 +08:00
|
|
|
M = BD.performFinalCleanups(M.release(), true);
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2005-08-02 10:16:17 +08:00
|
|
|
// Find out if the pass still crashes on the cleaned up program...
|
2018-02-09 13:09:50 +08:00
|
|
|
if (M && TestFn(BD, M.get()))
|
|
|
|
BD.setNewProgram(M.release()); // Yup, it does, keep the reduced version...
|
2003-03-01 00:13:20 +08:00
|
|
|
}
|
|
|
|
|
2010-07-29 02:12:30 +08:00
|
|
|
BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
|
2003-03-01 00:13:20 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
return Error::success();
|
2004-02-19 07:26:28 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 11:00:22 +08:00
|
|
|
static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
|
2016-06-29 11:01:13 +08:00
|
|
|
return BD.runPasses(M, BD.getPassesToRun());
|
2002-11-21 06:28:10 +08:00
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2004-02-19 07:26:28 +08:00
|
|
|
/// debugOptimizerCrash - This method is called when some pass crashes on input.
|
|
|
|
/// It attempts to prune down the testcase to something reasonable, and figure
|
|
|
|
/// out exactly which pass is crashing.
|
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Error BugDriver::debugOptimizerCrash(const std::string &ID) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n*** Debugging optimizer crash!\n";
|
2004-02-19 07:26:28 +08:00
|
|
|
|
|
|
|
// Reduce the list of passes which causes the optimizer to crash...
|
2016-09-07 01:18:22 +08:00
|
|
|
if (!BugpointIsInterrupted && !DontReducePassList) {
|
|
|
|
Expected<bool> Result = ReducePassList(*this).reduceList(PassesToRun);
|
|
|
|
if (Error E = Result.takeError())
|
|
|
|
return E;
|
|
|
|
}
|
2004-02-19 07:26:28 +08:00
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n*** Found crashing pass"
|
|
|
|
<< (PassesToRun.size() == 1 ? ": " : "es: ")
|
|
|
|
<< getPassesString(PassesToRun) << '\n';
|
2004-02-19 05:02:04 +08:00
|
|
|
|
2010-07-29 02:12:30 +08:00
|
|
|
EmitProgressBitcode(Program, ID);
|
2004-02-19 07:26:28 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
return DebugACrash(*this, TestForOptimizerCrash);
|
2004-02-19 07:26:28 +08:00
|
|
|
}
|
|
|
|
|
2010-08-05 11:00:22 +08:00
|
|
|
static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = BD.compileProgram(M)) {
|
2016-07-16 07:15:06 +08:00
|
|
|
if (VerboseErrors)
|
2016-09-07 01:18:22 +08:00
|
|
|
errs() << toString(std::move(E)) << "\n";
|
|
|
|
else {
|
|
|
|
consumeError(std::move(E));
|
2016-07-16 07:15:06 +08:00
|
|
|
errs() << "<crash>\n";
|
2016-09-07 01:18:22 +08:00
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
return true; // Tool is still crashing.
|
2004-02-19 07:26:28 +08:00
|
|
|
}
|
2010-04-12 13:08:25 +08:00
|
|
|
errs() << '\n';
|
|
|
|
return false;
|
2004-02-19 07:26:28 +08:00
|
|
|
}
|
2004-02-19 05:02:04 +08:00
|
|
|
|
|
|
|
/// debugCodeGeneratorCrash - This method is called when the code generator
|
|
|
|
/// crashes on an input. It attempts to reduce the input as much as possible
|
|
|
|
/// while still causing the code generator to crash.
|
2016-09-07 01:18:22 +08:00
|
|
|
Error BugDriver::debugCodeGeneratorCrash() {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "*** Debugging code generator crash!\n";
|
2004-02-19 05:02:04 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
return DebugACrash(*this, TestForCodeGenCrash);
|
2004-02-19 05:02:04 +08:00
|
|
|
}
|