mirror of
https://github.com/RPCS3/llvm.git
synced 2026-08-27 21:00:01 -04:00
c852398cbc
This patch implements a optimization bisect feature, which will allow optimizations to be selectively disabled at compile time in order to track down test failures that are caused by incorrect optimizations. The bisection is enabled using a new command line option (-opt-bisect-limit). Individual passes that may be skipped call the OptBisect object (via an LLVMContext) to see if they should be skipped based on the bisect limit. A finer level of control (disabling individual transformations) can be managed through an addition OptBisect method, but this is not yet used. The skip checking in this implementation is based on (and replaces) the skipOptnoneFunction check. Where that check was being called, a new call has been inserted in its place which checks the bisect limit and the optnone attribute. A new function call has been added for module and SCC passes that behaves in a similar way. Differential Revision: http://reviews.llvm.org/D19172 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267022 91177308-0d34-0410-b5e6-96231b3b80d8
164 lines
5.0 KiB
C++
164 lines
5.0 KiB
C++
//===-- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
#include "llvm/ADT/SetVector.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/Instructions.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "llvm/Pass.h"
|
|
#include <algorithm>
|
|
using namespace llvm;
|
|
|
|
/// Make sure GV is visible from both modules. Delete is true if it is
|
|
/// being deleted from this module.
|
|
/// This also makes sure GV cannot be dropped so that references from
|
|
/// the split module remain valid.
|
|
static void makeVisible(GlobalValue &GV, bool Delete) {
|
|
bool Local = GV.hasLocalLinkage();
|
|
if (Local || Delete) {
|
|
GV.setLinkage(GlobalValue::ExternalLinkage);
|
|
if (Local)
|
|
GV.setVisibility(GlobalValue::HiddenVisibility);
|
|
return;
|
|
}
|
|
|
|
if (!GV.hasLinkOnceLinkage()) {
|
|
assert(!GV.isDiscardableIfUnused());
|
|
return;
|
|
}
|
|
|
|
// Map linkonce* to weak* so that llvm doesn't drop this GV.
|
|
switch(GV.getLinkage()) {
|
|
default:
|
|
llvm_unreachable("Unexpected linkage");
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
|
GV.setLinkage(GlobalValue::WeakAnyLinkage);
|
|
return;
|
|
case GlobalValue::LinkOnceODRLinkage:
|
|
GV.setLinkage(GlobalValue::WeakODRLinkage);
|
|
return;
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
/// @brief A pass to extract specific functions and their dependencies.
|
|
class GVExtractorPass : public ModulePass {
|
|
SetVector<GlobalValue *> Named;
|
|
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.
|
|
///
|
|
explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true)
|
|
: ModulePass(ID), Named(GVs.begin(), GVs.end()), deleteStuff(deleteS) {}
|
|
|
|
bool runOnModule(Module &M) override {
|
|
if (skipModule(M))
|
|
return false;
|
|
|
|
// 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();
|
|
I != E; ++I) {
|
|
bool Delete =
|
|
deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
|
|
if (!Delete) {
|
|
if (I->hasAvailableExternallyLinkage())
|
|
continue;
|
|
if (I->getName() == "llvm.global_ctors")
|
|
continue;
|
|
}
|
|
|
|
makeVisible(*I, Delete);
|
|
|
|
if (Delete) {
|
|
// Make this a declaration and drop it's comdat.
|
|
I->setInitializer(nullptr);
|
|
I->setComdat(nullptr);
|
|
}
|
|
}
|
|
|
|
// Visit the Functions.
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
|
bool Delete =
|
|
deleteStuff == (bool)Named.count(&*I) && !I->isDeclaration();
|
|
if (!Delete) {
|
|
if (I->hasAvailableExternallyLinkage())
|
|
continue;
|
|
}
|
|
|
|
makeVisible(*I, Delete);
|
|
|
|
if (Delete) {
|
|
// Make this a declaration and drop it's comdat.
|
|
I->deleteBody();
|
|
I->setComdat(nullptr);
|
|
}
|
|
}
|
|
|
|
// Visit the Aliases.
|
|
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
|
|
I != E;) {
|
|
Module::alias_iterator CurI = I;
|
|
++I;
|
|
|
|
bool Delete = deleteStuff == (bool)Named.count(&*CurI);
|
|
makeVisible(*CurI, Delete);
|
|
|
|
if (Delete) {
|
|
Type *Ty = CurI->getValueType();
|
|
|
|
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,
|
|
nullptr, CurI->getName());
|
|
|
|
}
|
|
CurI->replaceAllUsesWith(Declaration);
|
|
delete &*CurI;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
char GVExtractorPass::ID = 0;
|
|
}
|
|
|
|
ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue *> &GVs,
|
|
bool deleteFn) {
|
|
return new GVExtractorPass(GVs, deleteFn);
|
|
}
|