2008-03-07 19:51:57 +00:00
|
|
|
//===-- ExtractGV.cpp - Global Value extraction pass ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass extracts global values
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2008-03-07 19:51:57 +00:00
|
|
|
#include "llvm/Pass.h"
|
2008-03-09 18:32:50 +00:00
|
|
|
#include <algorithm>
|
2008-03-07 19:51:57 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// @brief A pass to extract specific functions and their dependencies.
|
2009-10-25 06:33:48 +00:00
|
|
|
class GVExtractorPass : public ModulePass {
|
2010-08-26 00:22:55 +00:00
|
|
|
SetVector<GlobalValue *> Named;
|
2008-03-07 19:51:57 +00:00
|
|
|
bool deleteStuff;
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
|
|
|
/// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
|
|
|
|
/// specified function. Otherwise, it deletes as much of the module as
|
|
|
|
/// possible, except for the function specified.
|
|
|
|
///
|
2010-08-26 00:22:55 +00:00
|
|
|
explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
|
|
|
|
: ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
|
2008-03-07 19:51:57 +00:00
|
|
|
|
|
|
|
bool runOnModule(Module &M) {
|
2010-08-26 00:22:55 +00:00
|
|
|
// Visit the global inline asm.
|
|
|
|
if (!deleteStuff)
|
|
|
|
M.setModuleInlineAsm("");
|
|
|
|
|
|
|
|
// For simplicity, just give all GlobalValues ExternalLinkage. A trickier
|
|
|
|
// implementation could figure out which GlobalValues are actually
|
|
|
|
// referenced by the Named set, and which GlobalValues in the rest of
|
|
|
|
// the module are referenced by the NamedSet, and get away with leaving
|
|
|
|
// more internal and private things internal and private. But for now,
|
|
|
|
// be conservative and simple.
|
|
|
|
|
|
|
|
// Visit the GlobalVariables.
|
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
2010-09-23 17:25:06 +00:00
|
|
|
I != E; ++I) {
|
2012-10-29 01:59:03 +00:00
|
|
|
bool Delete =
|
|
|
|
deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
|
|
|
|
if (!Delete) {
|
2012-07-19 00:11:40 +00:00
|
|
|
if (I->hasAvailableExternallyLinkage())
|
|
|
|
continue;
|
|
|
|
if (I->getName() == "llvm.global_ctors")
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-09 14:38:09 +00:00
|
|
|
|
2013-03-04 22:40:44 +00:00
|
|
|
bool Local = I->isDiscardableIfUnused();
|
2012-10-29 01:59:03 +00:00
|
|
|
if (Local)
|
2010-09-23 17:25:06 +00:00
|
|
|
I->setVisibility(GlobalValue::HiddenVisibility);
|
2012-10-29 01:59:03 +00:00
|
|
|
|
|
|
|
if (Local || Delete)
|
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
|
|
|
|
if (Delete)
|
|
|
|
I->setInitializer(0);
|
2010-09-23 17:25:06 +00:00
|
|
|
}
|
2010-08-26 00:22:55 +00:00
|
|
|
|
|
|
|
// Visit the Functions.
|
2010-09-23 17:25:06 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2012-10-29 01:59:03 +00:00
|
|
|
bool Delete =
|
|
|
|
deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
|
|
|
|
if (!Delete) {
|
2012-07-19 00:11:40 +00:00
|
|
|
if (I->hasAvailableExternallyLinkage())
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-09 14:38:09 +00:00
|
|
|
|
2013-03-04 22:40:44 +00:00
|
|
|
bool Local = I->isDiscardableIfUnused();
|
2012-10-29 01:59:03 +00:00
|
|
|
if (Local)
|
2010-09-23 17:25:06 +00:00
|
|
|
I->setVisibility(GlobalValue::HiddenVisibility);
|
2012-10-29 01:59:03 +00:00
|
|
|
|
|
|
|
if (Local || Delete)
|
|
|
|
I->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
|
|
|
|
if (Delete)
|
|
|
|
I->deleteBody();
|
2010-09-23 17:25:06 +00:00
|
|
|
}
|
2008-03-07 19:51:57 +00:00
|
|
|
|
2012-10-29 00:27:55 +00:00
|
|
|
// Visit the Aliases.
|
|
|
|
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
|
|
|
|
I != E;) {
|
|
|
|
Module::alias_iterator CurI = I;
|
|
|
|
++I;
|
|
|
|
|
2013-03-04 22:40:44 +00:00
|
|
|
if (CurI->isDiscardableIfUnused()) {
|
2012-10-29 00:27:55 +00:00
|
|
|
CurI->setVisibility(GlobalValue::HiddenVisibility);
|
2012-10-29 01:59:03 +00:00
|
|
|
CurI->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
}
|
2012-10-29 00:27:55 +00:00
|
|
|
|
|
|
|
if (deleteStuff == (bool)Named.count(CurI)) {
|
|
|
|
Type *Ty = CurI->getType()->getElementType();
|
|
|
|
|
|
|
|
CurI->removeFromParent();
|
|
|
|
llvm::Value *Declaration;
|
|
|
|
if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
|
|
|
|
Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
|
|
|
|
CurI->getName(), &M);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Declaration =
|
|
|
|
new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
|
|
|
|
0, CurI->getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
CurI->replaceAllUsesWith(Declaration);
|
|
|
|
delete CurI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-07 19:51:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
char GVExtractorPass::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs,
|
2010-08-26 00:22:55 +00:00
|
|
|
bool deleteFn) {
|
|
|
|
return new GVExtractorPass(GVs, deleteFn);
|
2008-03-07 19:51:57 +00:00
|
|
|
}
|