+ bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
+ };
+}
+
+bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
+ // Clone the program to try hacking it apart...
+ Module *M = CloneModule(BD.getProgram());
+
+ // Convert list to set for fast lookup...
+ std::set<BasicBlock*> Blocks;
+ for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
+ // Convert the basic block from the original module to the new module...
+ const Function *F = BBs[i]->getParent();
+ Function *CMF = M->getFunction(F->getName());
+ assert(CMF && "Function not in module?!");
+ assert(CMF->getFunctionType() == F->getFunctionType() && "wrong type?");
+
+ // Get the mapped basic block...
+ Function::iterator CBI = CMF->begin();
+ std::advance(CBI, std::distance(F->begin(),
+ Function::const_iterator(BBs[i])));
+ Blocks.insert(CBI);
+ }
+
+ std::cout << "Checking for crash with only these blocks:";
+ unsigned NumPrint = Blocks.size();
+ if (NumPrint > 10) NumPrint = 10;
+ for (unsigned i = 0, e = NumPrint; i != e; ++i)
+ std::cout << " " << BBs[i]->getName();
+ if (NumPrint < Blocks.size())
+ std::cout << "... <" << Blocks.size() << " total>";
+ std::cout << ": ";
+
+ // Loop over and delete any hack up any blocks that are not listed...
+ for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
+ for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
+ if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
+ // Loop over all of the successors of this block, deleting any PHI nodes
+ // that might include it.
+ for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
+ (*SI)->removePredecessor(BB);
+
+ TerminatorInst *BBTerm = BB->getTerminator();
+
+ if (isa<StructType>(BBTerm->getType()))
+ BBTerm->replaceAllUsesWith(UndefValue::get(BBTerm->getType()));
+ else if (BB->getTerminator()->getType() != Type::VoidTy)
+ BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
+
+ // Replace the old terminator instruction.
+ BB->getInstList().pop_back();
+ new UnreachableInst(BB);
+ }
+
+ // 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
+ // have to take.
+ std::vector<std::pair<Function*, std::string> > BlockInfo;
+
+ for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
+ I != E; ++I)
+ BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
+
+ // Now run the CFG simplify pass on the function...
+ PassManager Passes;
+ Passes.add(createCFGSimplificationPass());
+ Passes.add(createVerifierPass());
+ Passes.run(*M);
+
+ // Try running on the hacked up program...
+ if (TestFn(BD, M)) {
+ BD.setNewProgram(M); // It crashed, keep the trimmed version...
+
+ // 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();
+ for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
+ ValueSymbolTable &ST = BlockInfo[i].first->getValueSymbolTable();
+ Value* V = ST.lookup(BlockInfo[i].second);
+ if (V && V->getType() == Type::LabelTy)
+ BBs.push_back(cast<BasicBlock>(V));
+ }
+ return true;
+ }
+ delete M; // It didn't crash, try something else.
+ return false;
+}
+
+/// 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.
+static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
+ // See if we can get away with nuking some of the global variable initializers
+ // in the program...
+ if (BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
+ // Now try to reduce the number of global variable initializers in the
+ // module to something small.
+ Module *M = CloneModule(BD.getProgram());
+ bool DeletedInit = false;
+
+ for (Module::global_iterator I = M->global_begin(), E = M->global_end();
+ I != E; ++I)
+ if (I->hasInitializer()) {
+ I->setInitializer(0);
+ I->setLinkage(GlobalValue::ExternalLinkage);
+ DeletedInit = true;
+ }
+
+ if (!DeletedInit) {
+ delete M; // No change made...
+ } else {
+ // See if the program still causes a crash...
+ std::cout << "\nChecking to see if we can delete global inits: ";
+
+ if (TestFn(BD, M)) { // Still crashes?
+ BD.setNewProgram(M);
+ std::cout << "\n*** Able to remove all global initializers!\n";
+ } else { // No longer crashes?
+ std::cout << " - Removing all global inits hides problem!\n";
+ delete M;
+
+ std::vector<GlobalVariable*> GVs;
+
+ for (Module::global_iterator I = BD.getProgram()->global_begin(),
+ E = BD.getProgram()->global_end(); I != E; ++I)
+ if (I->hasInitializer())
+ GVs.push_back(I);
+
+ if (GVs.size() > 1 && !BugpointIsInterrupted) {
+ std::cout << "\n*** Attempting to reduce the number of global "
+ << "variables in the testcase\n";
+
+ unsigned OldSize = GVs.size();
+ ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs);
+
+ if (GVs.size() < OldSize)
+ BD.EmitProgressBitcode("reduced-global-variables");
+ }
+ }
+ }
+ }