2002-11-21 06:28:10 +08:00
|
|
|
//===- ExtractFunction.cpp - Extract a function from Program --------------===//
|
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
|
|
|
//
|
2004-03-15 04:50:42 +08:00
|
|
|
// This file implements several methods that are used to extract functions,
|
|
|
|
// loops, or portions of a module from the rest of the module.
|
2002-11-21 06:28:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BugDriver.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/LLVMContext.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"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2003-09-11 05:11:42 +08:00
|
|
|
#include "llvm/Pass.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/Transforms/Utils/CodeExtractor.h"
|
2004-04-03 00:28:32 +08:00
|
|
|
#include <set>
|
2003-11-23 12:51:05 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "bugpoint"
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
namespace llvm {
|
2003-11-23 12:51:05 +08:00
|
|
|
bool DisableSimplifyCFG = false;
|
2009-09-08 03:26:11 +08:00
|
|
|
extern cl::opt<std::string> OutputPrefix;
|
2003-11-12 06:41:34 +08:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-04-26 06:08:12 +08:00
|
|
|
namespace {
|
|
|
|
cl::opt<bool>
|
|
|
|
NoDCE ("disable-dce",
|
|
|
|
cl::desc("Do not use the -dce pass to reduce testcases"));
|
2003-08-05 23:51:05 +08:00
|
|
|
cl::opt<bool, true>
|
|
|
|
NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
|
2003-04-26 06:08:12 +08:00
|
|
|
cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
|
2012-02-22 09:43:47 +08:00
|
|
|
|
|
|
|
Function* globalInitUsesExternalBA(GlobalVariable* GV) {
|
|
|
|
if (!GV->hasInitializer())
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2012-02-22 09:43:47 +08:00
|
|
|
|
|
|
|
Constant *I = GV->getInitializer();
|
|
|
|
|
|
|
|
// walk the values used by the initializer
|
|
|
|
// (and recurse into things like ConstantExpr)
|
|
|
|
std::vector<Constant*> Todo;
|
|
|
|
std::set<Constant*> Done;
|
|
|
|
Todo.push_back(I);
|
|
|
|
|
|
|
|
while (!Todo.empty()) {
|
|
|
|
Constant* V = Todo.back();
|
|
|
|
Todo.pop_back();
|
|
|
|
Done.insert(V);
|
|
|
|
|
|
|
|
if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) {
|
|
|
|
Function *F = BA->getFunction();
|
|
|
|
if (F->isDeclaration())
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) {
|
|
|
|
Constant *C = dyn_cast<Constant>(*i);
|
|
|
|
if (C && !isa<GlobalValue>(C) && !Done.count(C))
|
|
|
|
Todo.push_back(C);
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2012-02-22 09:43:47 +08:00
|
|
|
}
|
|
|
|
} // end anonymous namespace
|
2002-11-21 06:28:10 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
BugDriver::deleteInstructionFromProgram(const Instruction *I,
|
|
|
|
unsigned Simplification) {
|
2010-08-10 23:46:11 +08:00
|
|
|
// FIXME, use vmap?
|
2015-12-09 07:57:17 +08:00
|
|
|
Module *Clone = CloneModule(Program).release();
|
2003-01-23 10:48:33 +08:00
|
|
|
|
2004-02-19 05:50:26 +08:00
|
|
|
const BasicBlock *PBB = I->getParent();
|
|
|
|
const Function *PF = PBB->getParent();
|
2003-01-23 10:48:33 +08:00
|
|
|
|
2010-08-10 23:46:11 +08:00
|
|
|
Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn
|
2004-02-19 05:50:26 +08:00
|
|
|
std::advance(RFI, std::distance(PF->getParent()->begin(),
|
|
|
|
Module::const_iterator(PF)));
|
2003-01-23 10:48:33 +08:00
|
|
|
|
|
|
|
Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
|
2004-02-19 05:50:26 +08:00
|
|
|
std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
|
2003-01-23 10:48:33 +08:00
|
|
|
|
|
|
|
BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
|
2004-02-19 05:50:26 +08:00
|
|
|
std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
|
2015-10-21 03:36:39 +08:00
|
|
|
Instruction *TheInst = &*RI; // Got the corresponding instruction!
|
2003-01-23 10:48:33 +08:00
|
|
|
|
|
|
|
// If this instruction produces a value, replace any users with null values
|
2010-06-08 04:19:26 +08:00
|
|
|
if (!TheInst->getType()->isVoidTy())
|
2009-08-01 04:28:14 +08:00
|
|
|
TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
|
2003-01-23 10:48:33 +08:00
|
|
|
|
|
|
|
// Remove the instruction from the program.
|
2004-02-19 05:50:26 +08:00
|
|
|
TheInst->getParent()->getInstList().erase(TheInst);
|
2003-01-23 10:48:33 +08:00
|
|
|
|
2003-04-25 06:53:24 +08:00
|
|
|
// Spiff up the output a little bit.
|
2010-08-10 23:46:11 +08:00
|
|
|
std::vector<std::string> Passes;
|
2003-10-23 23:42:55 +08:00
|
|
|
|
2010-08-10 23:46:11 +08:00
|
|
|
/// Can we get rid of the -disable-* options?
|
2003-04-26 06:08:12 +08:00
|
|
|
if (Simplification > 1 && !NoDCE)
|
2010-08-10 23:46:11 +08:00
|
|
|
Passes.push_back("dce");
|
2003-08-05 23:51:05 +08:00
|
|
|
if (Simplification && !DisableSimplifyCFG)
|
2010-08-10 23:46:11 +08:00
|
|
|
Passes.push_back("simplifycfg"); // Delete dead control flow
|
|
|
|
|
|
|
|
Passes.push_back("verify");
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> New = runPassesOn(Clone, Passes);
|
2010-08-10 23:46:11 +08:00
|
|
|
delete Clone;
|
|
|
|
if (!New) {
|
|
|
|
errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return New;
|
2003-01-23 10:48:33 +08:00
|
|
|
}
|
2003-03-01 00:13:20 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
|
2003-05-22 03:41:31 +08:00
|
|
|
// Make all functions external, so GlobalDCE doesn't delete them...
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2010-08-08 11:55:08 +08:00
|
|
|
std::vector<std::string> CleanupPasses;
|
|
|
|
CleanupPasses.push_back("globaldce");
|
2003-11-06 05:45:35 +08:00
|
|
|
|
2003-11-23 12:51:05 +08:00
|
|
|
if (MayModifySemantics)
|
2010-08-08 11:55:08 +08:00
|
|
|
CleanupPasses.push_back("deadarghaX0r");
|
2003-11-23 12:51:05 +08:00
|
|
|
else
|
2010-08-08 11:55:08 +08:00
|
|
|
CleanupPasses.push_back("deadargelim");
|
2003-11-06 05:45:35 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> New = runPassesOn(M, CleanupPasses);
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!New) {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n";
|
2014-08-27 01:19:03 +08:00
|
|
|
return nullptr;
|
2003-11-06 05:45:35 +08:00
|
|
|
}
|
2004-03-15 05:17:22 +08:00
|
|
|
delete M;
|
|
|
|
return New;
|
2003-03-01 00:13:20 +08:00
|
|
|
}
|
2004-03-15 03:27:19 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> BugDriver::extractLoop(Module *M) {
|
2010-08-08 11:55:08 +08:00
|
|
|
std::vector<std::string> LoopExtractPasses;
|
|
|
|
LoopExtractPasses.push_back("loop-extract-single");
|
2004-03-15 04:02:07 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> NewM = runPassesOn(M, LoopExtractPasses);
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!NewM) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "*** Loop extraction failed: ";
|
2010-07-29 02:12:30 +08:00
|
|
|
EmitProgressBitcode(M, "loopextraction", true);
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "*** Sorry. :( Please report a bug!\n";
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2004-03-15 04:02:07 +08:00
|
|
|
}
|
2004-03-15 05:17:22 +08:00
|
|
|
|
|
|
|
// Check to see if we created any new functions. If not, no loops were
|
2004-11-19 03:40:13 +08:00
|
|
|
// extracted and we should return null. Limit the number of loops we extract
|
|
|
|
// to avoid taking forever.
|
|
|
|
static unsigned NumExtracted = 32;
|
2004-11-16 14:31:38 +08:00
|
|
|
if (M->size() == NewM->size() || --NumExtracted == 0) {
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2004-11-16 14:31:38 +08:00
|
|
|
} else {
|
|
|
|
assert(M->size() < NewM->size() && "Loop extract removed functions?");
|
|
|
|
Module::iterator MI = NewM->begin();
|
|
|
|
for (unsigned i = 0, e = M->size(); i != e; ++i)
|
|
|
|
++MI;
|
2004-03-15 05:17:22 +08:00
|
|
|
}
|
2005-04-22 08:00:37 +08:00
|
|
|
|
2004-03-15 05:17:22 +08:00
|
|
|
return NewM;
|
2004-03-15 04:02:07 +08:00
|
|
|
}
|
|
|
|
|
2015-11-27 03:23:49 +08:00
|
|
|
static void eliminateAliases(GlobalValue *GV) {
|
|
|
|
// First, check whether a GlobalAlias references this definition.
|
|
|
|
// GlobalAlias MAY NOT reference declarations.
|
|
|
|
for (;;) {
|
|
|
|
// 1. Find aliases
|
|
|
|
SmallVector<GlobalAlias*,1> aliases;
|
|
|
|
Module *M = GV->getParent();
|
|
|
|
for (Module::alias_iterator I=M->alias_begin(), E=M->alias_end(); I!=E; ++I)
|
|
|
|
if (I->getAliasee()->stripPointerCasts() == GV)
|
|
|
|
aliases.push_back(&*I);
|
|
|
|
if (aliases.empty())
|
|
|
|
break;
|
|
|
|
// 2. Resolve aliases
|
|
|
|
for (unsigned i=0, e=aliases.size(); i<e; ++i) {
|
|
|
|
aliases[i]->replaceAllUsesWith(aliases[i]->getAliasee());
|
|
|
|
aliases[i]->eraseFromParent();
|
|
|
|
}
|
|
|
|
// 3. Repeat until no more aliases found; there might
|
|
|
|
// be an alias to an alias...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// DeleteGlobalInitializer - "Remove" the global variable by deleting its initializer,
|
|
|
|
// making it external.
|
|
|
|
//
|
|
|
|
void llvm::DeleteGlobalInitializer(GlobalVariable *GV) {
|
|
|
|
eliminateAliases(GV);
|
|
|
|
GV->setInitializer(nullptr);
|
|
|
|
}
|
2004-03-15 04:02:07 +08:00
|
|
|
|
2004-03-15 03:27:19 +08:00
|
|
|
// DeleteFunctionBody - "Remove" the function by deleting all of its basic
|
|
|
|
// blocks, making it external.
|
|
|
|
//
|
|
|
|
void llvm::DeleteFunctionBody(Function *F) {
|
2015-11-27 03:23:49 +08:00
|
|
|
eliminateAliases(F);
|
|
|
|
|
2004-03-15 03:27:19 +08:00
|
|
|
// delete the body of the function...
|
|
|
|
F->deleteBody();
|
2007-01-31 04:08:39 +08:00
|
|
|
assert(F->isDeclaration() && "This didn't make the function external!");
|
2004-03-15 03:27:19 +08:00
|
|
|
}
|
|
|
|
|
2006-03-09 07:55:38 +08:00
|
|
|
/// GetTorInit - Given a list of entries for static ctors/dtors, return them
|
|
|
|
/// as a constant array.
|
|
|
|
static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
|
|
|
|
assert(!TorList.empty() && "Don't create empty tor list!");
|
|
|
|
std::vector<Constant*> ArrayElts;
|
2011-07-12 22:06:48 +08:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext());
|
2015-02-26 09:10:49 +08:00
|
|
|
|
2011-07-18 12:54:35 +08:00
|
|
|
StructType *STy =
|
2015-02-26 09:10:49 +08:00
|
|
|
StructType::get(Int32Ty, TorList[0].first->getType(), nullptr);
|
2006-03-09 07:55:38 +08:00
|
|
|
for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
|
2011-06-20 12:01:31 +08:00
|
|
|
Constant *Elts[] = {
|
|
|
|
ConstantInt::get(Int32Ty, TorList[i].second),
|
|
|
|
TorList[i].first
|
|
|
|
};
|
|
|
|
ArrayElts.push_back(ConstantStruct::get(STy, Elts));
|
2006-03-09 07:55:38 +08:00
|
|
|
}
|
2009-07-30 06:17:13 +08:00
|
|
|
return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
|
2006-03-09 07:55:38 +08:00
|
|
|
ArrayElts.size()),
|
|
|
|
ArrayElts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and
|
|
|
|
/// M1 has all of the global variables. If M2 contains any functions that are
|
|
|
|
/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and
|
|
|
|
/// prune appropriate entries out of M1s list.
|
2009-04-22 23:57:18 +08:00
|
|
|
static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2,
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy &VMap) {
|
2006-03-09 07:55:38 +08:00
|
|
|
GlobalVariable *GV = M1->getNamedGlobal(GlobalName);
|
2009-01-16 04:18:42 +08:00
|
|
|
if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() ||
|
2006-03-09 07:55:38 +08:00
|
|
|
!GV->use_empty()) return;
|
|
|
|
|
|
|
|
std::vector<std::pair<Function*, int> > M1Tors, M2Tors;
|
|
|
|
ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
|
|
|
|
if (!InitList) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
|
|
|
|
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
|
|
|
|
if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
|
|
|
|
|
|
|
|
if (CS->getOperand(1)->isNullValue())
|
|
|
|
break; // Found a null terminator, stop here.
|
|
|
|
|
2006-10-20 15:07:24 +08:00
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
|
|
|
|
int Priority = CI ? CI->getSExtValue() : 0;
|
2006-03-09 07:55:38 +08:00
|
|
|
|
|
|
|
Constant *FP = CS->getOperand(1);
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
|
2006-11-27 09:05:10 +08:00
|
|
|
if (CE->isCast())
|
2006-03-09 07:55:38 +08:00
|
|
|
FP = CE->getOperand(0);
|
|
|
|
if (Function *F = dyn_cast<Function>(FP)) {
|
2007-01-31 04:08:39 +08:00
|
|
|
if (!F->isDeclaration())
|
2006-03-09 07:55:38 +08:00
|
|
|
M1Tors.push_back(std::make_pair(F, Priority));
|
|
|
|
else {
|
|
|
|
// Map to M2's version of the function.
|
2010-06-24 08:33:28 +08:00
|
|
|
F = cast<Function>(VMap[F]);
|
2006-03-09 07:55:38 +08:00
|
|
|
M2Tors.push_back(std::make_pair(F, Priority));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GV->eraseFromParent();
|
|
|
|
if (!M1Tors.empty()) {
|
|
|
|
Constant *M1Init = GetTorInit(M1Tors);
|
2009-07-09 03:03:57 +08:00
|
|
|
new GlobalVariable(*M1, M1Init->getType(), false,
|
2009-07-08 09:26:06 +08:00
|
|
|
GlobalValue::AppendingLinkage,
|
2009-07-09 03:03:57 +08:00
|
|
|
M1Init, GlobalName);
|
2006-03-09 07:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GV = M2->getNamedGlobal(GlobalName);
|
|
|
|
assert(GV && "Not a clone of M1?");
|
|
|
|
assert(GV->use_empty() && "llvm.ctors shouldn't have uses!");
|
|
|
|
|
|
|
|
GV->eraseFromParent();
|
|
|
|
if (!M2Tors.empty()) {
|
|
|
|
Constant *M2Init = GetTorInit(M2Tors);
|
2009-07-09 03:03:57 +08:00
|
|
|
new GlobalVariable(*M2, M2Init->getType(), false,
|
2009-07-08 09:26:06 +08:00
|
|
|
GlobalValue::AppendingLinkage,
|
2009-07-09 03:03:57 +08:00
|
|
|
M2Init, GlobalName);
|
2006-03-09 07:55:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 08:34:10 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy &VMap) {
|
2004-03-15 03:27:19 +08:00
|
|
|
// Make sure functions & globals are all external so that linkage
|
|
|
|
// between the two modules will work.
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
2006-03-09 07:55:38 +08:00
|
|
|
for (Module::global_iterator I = M->global_begin(), E = M->global_end();
|
2008-07-09 00:38:42 +08:00
|
|
|
I != E; ++I) {
|
2009-07-26 08:34:27 +08:00
|
|
|
if (I->hasName() && I->getName()[0] == '\01')
|
|
|
|
I->setName(I->getName().substr(1));
|
2004-03-15 03:27:19 +08:00
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
2008-07-09 00:38:42 +08:00
|
|
|
}
|
2004-03-15 03:27:19 +08:00
|
|
|
|
2010-10-13 09:36:30 +08:00
|
|
|
ValueToValueMapTy NewVMap;
|
2015-12-09 08:34:10 +08:00
|
|
|
std::unique_ptr<Module> New = CloneModule(M, NewVMap);
|
2004-03-15 03:27:19 +08:00
|
|
|
|
2006-11-09 14:24:56 +08:00
|
|
|
// Remove the Test functions from the Safe module
|
2009-04-22 23:57:18 +08:00
|
|
|
std::set<Function *> TestFunctions;
|
2004-03-15 03:27:19 +08:00
|
|
|
for (unsigned i = 0, e = F.size(); i != e; ++i) {
|
2010-06-24 08:33:28 +08:00
|
|
|
Function *TNOF = cast<Function>(VMap[F[i]]);
|
2009-07-16 00:35:29 +08:00
|
|
|
DEBUG(errs() << "Removing function ");
|
2014-01-09 10:29:41 +08:00
|
|
|
DEBUG(TNOF->printAsOperand(errs(), false));
|
2009-07-16 00:35:29 +08:00
|
|
|
DEBUG(errs() << "\n");
|
2010-06-24 08:33:28 +08:00
|
|
|
TestFunctions.insert(cast<Function>(NewVMap[TNOF]));
|
2006-11-09 14:24:56 +08:00
|
|
|
DeleteFunctionBody(TNOF); // Function is now external in this module!
|
2006-07-28 09:19:28 +08:00
|
|
|
}
|
|
|
|
|
2006-03-09 07:55:38 +08:00
|
|
|
|
2006-11-09 14:24:56 +08:00
|
|
|
// Remove the Safe functions from the Test module
|
2015-10-21 03:36:39 +08:00
|
|
|
for (Function &I : *New)
|
|
|
|
if (!TestFunctions.count(&I))
|
|
|
|
DeleteFunctionBody(&I);
|
2006-07-28 09:19:28 +08:00
|
|
|
|
2012-02-22 09:43:47 +08:00
|
|
|
// Try to split the global initializers evenly
|
2015-10-21 03:36:39 +08:00
|
|
|
for (GlobalVariable &I : M->globals()) {
|
|
|
|
GlobalVariable *GV = cast<GlobalVariable>(NewVMap[&I]);
|
|
|
|
if (Function *TestFn = globalInitUsesExternalBA(&I)) {
|
2012-02-22 09:43:47 +08:00
|
|
|
if (Function *SafeFn = globalInitUsesExternalBA(GV)) {
|
|
|
|
errs() << "*** Error: when reducing functions, encountered "
|
|
|
|
"the global '";
|
2014-01-09 10:29:41 +08:00
|
|
|
GV->printAsOperand(errs(), false);
|
2012-02-22 09:43:47 +08:00
|
|
|
errs() << "' with an initializer that references blockaddresses "
|
|
|
|
"from safe function '" << SafeFn->getName()
|
|
|
|
<< "' and from test function '" << TestFn->getName() << "'.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-11-27 03:23:49 +08:00
|
|
|
DeleteGlobalInitializer(&I); // Delete the initializer to make it external
|
2012-02-22 09:43:47 +08:00
|
|
|
} else {
|
|
|
|
// If we keep it in the safe module, then delete it in the test module
|
2015-11-27 03:23:49 +08:00
|
|
|
DeleteGlobalInitializer(GV);
|
2012-02-22 09:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-09 07:55:38 +08:00
|
|
|
// Make sure that there is a global ctor/dtor array in both halves of the
|
|
|
|
// module if they both have static ctor/dtor functions.
|
2015-12-09 08:34:10 +08:00
|
|
|
SplitStaticCtorDtor("llvm.global_ctors", M, New.get(), NewVMap);
|
|
|
|
SplitStaticCtorDtor("llvm.global_dtors", M, New.get(), NewVMap);
|
|
|
|
|
2004-03-15 03:27:19 +08:00
|
|
|
return New;
|
|
|
|
}
|
2004-05-12 05:54:13 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic Block Extraction Code
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
BugDriver::extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
|
|
|
|
Module *M) {
|
2013-06-18 02:48:59 +08:00
|
|
|
SmallString<128> Filename;
|
|
|
|
int FD;
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code EC = sys::fs::createUniqueFile(
|
2013-07-06 05:01:08 +08:00
|
|
|
OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
|
2013-06-18 02:48:59 +08:00
|
|
|
if (EC) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "*** Basic Block extraction failed!\n";
|
2013-06-18 02:48:59 +08:00
|
|
|
errs() << "Error creating temporary file: " << EC.message() << "\n";
|
2010-07-29 02:12:30 +08:00
|
|
|
EmitProgressBitcode(M, "basicblockextractfail", true);
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2007-11-14 14:47:06 +08:00
|
|
|
}
|
2013-06-18 02:48:59 +08:00
|
|
|
sys::RemoveFileOnSignal(Filename);
|
2007-11-14 14:47:06 +08:00
|
|
|
|
2013-06-18 02:48:59 +08:00
|
|
|
tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD);
|
2007-11-14 14:47:06 +08:00
|
|
|
for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
2008-01-08 12:26:20 +08:00
|
|
|
// If the BB doesn't have a name, give it one so we have something to key
|
|
|
|
// off of.
|
|
|
|
if (!BB->hasName()) BB->setName("tmpbb");
|
2011-11-16 00:27:03 +08:00
|
|
|
BlocksToNotExtractFile.os() << BB->getParent()->getName() << " "
|
2010-09-01 22:20:41 +08:00
|
|
|
<< BB->getName() << "\n";
|
2007-11-14 14:47:06 +08:00
|
|
|
}
|
2010-09-01 22:20:41 +08:00
|
|
|
BlocksToNotExtractFile.os().close();
|
|
|
|
if (BlocksToNotExtractFile.os().has_error()) {
|
2013-06-18 02:48:59 +08:00
|
|
|
errs() << "Error writing list of blocks to not extract\n";
|
2010-08-21 00:59:15 +08:00
|
|
|
EmitProgressBitcode(M, "basicblockextractfail", true);
|
2010-09-01 22:20:41 +08:00
|
|
|
BlocksToNotExtractFile.os().clear_error();
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2010-08-21 00:59:15 +08:00
|
|
|
}
|
|
|
|
BlocksToNotExtractFile.keep();
|
2007-11-14 14:47:06 +08:00
|
|
|
|
2013-06-18 02:48:59 +08:00
|
|
|
std::string uniqueFN = "--extract-blocks-file=";
|
|
|
|
uniqueFN += Filename.str();
|
2010-01-29 02:04:38 +08:00
|
|
|
const char *ExtraArg = uniqueFN.c_str();
|
2007-11-14 14:47:06 +08:00
|
|
|
|
2010-08-08 11:55:08 +08:00
|
|
|
std::vector<std::string> PI;
|
|
|
|
PI.push_back("extract-blocks");
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
|
2007-11-14 14:47:06 +08:00
|
|
|
|
2013-06-18 02:48:59 +08:00
|
|
|
sys::fs::remove(Filename.c_str());
|
2007-11-14 14:47:06 +08:00
|
|
|
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!Ret) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "*** Basic Block extraction failed, please report a bug!\n";
|
2010-07-29 02:12:30 +08:00
|
|
|
EmitProgressBitcode(M, "basicblockextractfail", true);
|
2004-08-12 10:36:50 +08:00
|
|
|
}
|
2004-05-12 05:54:13 +08:00
|
|
|
return Ret;
|
|
|
|
}
|