#include "llvm/Transforms/IPO/GlobalDCE.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/TypeMetadataUtils.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/CtorUtils.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
using namespace llvm;
#define DEBUG_TYPE "globaldce"
static cl::opt<bool>
ClEnableVFE("enable-vfe", cl::Hidden, cl::init(true),
cl::desc("Enable virtual function elimination"));
STATISTIC(NumAliases , "Number of global aliases removed");
STATISTIC(NumFunctions, "Number of functions removed");
STATISTIC(NumIFuncs, "Number of indirect functions removed");
STATISTIC(NumVariables, "Number of global variables removed");
STATISTIC(NumVFuncs, "Number of virtual functions removed");
namespace {
class GlobalDCELegacyPass : public ModulePass {
public:
static char ID; GlobalDCELegacyPass() : ModulePass(ID) {
initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry());
}
bool runOnModule(Module &M) override {
if (skipModule(M))
return false;
FunctionAnalysisManager DummyFAM;
ModuleAnalysisManager DummyMAM;
DummyMAM.registerPass(
[&] { return FunctionAnalysisManagerModuleProxy(DummyFAM); });
auto PA = Impl.run(M, DummyMAM);
return !PA.areAllPreserved();
}
private:
GlobalDCEPass Impl;
};
}
char GlobalDCELegacyPass::ID = 0;
INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce",
"Dead Global Elimination", false, false)
ModulePass *llvm::createGlobalDCEPass() {
return new GlobalDCELegacyPass();
}
static bool isEmptyFunction(Function *F) {
if (F->isDeclaration())
return false;
BasicBlock &Entry = F->getEntryBlock();
for (auto &I : Entry) {
if (I.isDebugOrPseudoInst())
continue;
if (auto *RI = dyn_cast<ReturnInst>(&I))
return !RI->getReturnValue();
break;
}
return false;
}
void GlobalDCEPass::ComputeDependencies(Value *V,
SmallPtrSetImpl<GlobalValue *> &Deps) {
if (auto *I = dyn_cast<Instruction>(V)) {
Function *Parent = I->getParent()->getParent();
Deps.insert(Parent);
} else if (auto *GV = dyn_cast<GlobalValue>(V)) {
Deps.insert(GV);
} else if (auto *CE = dyn_cast<Constant>(V)) {
auto Where = ConstantDependenciesCache.find(CE);
if (Where != ConstantDependenciesCache.end()) {
auto const &K = Where->second;
Deps.insert(K.begin(), K.end());
} else {
SmallPtrSetImpl<GlobalValue *> &LocalDeps = ConstantDependenciesCache[CE];
for (User *CEUser : CE->users())
ComputeDependencies(CEUser, LocalDeps);
Deps.insert(LocalDeps.begin(), LocalDeps.end());
}
}
}
void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) {
SmallPtrSet<GlobalValue *, 8> Deps;
for (User *User : GV.users())
ComputeDependencies(User, Deps);
Deps.erase(&GV); for (GlobalValue *GVU : Deps) {
if (VFESafeVTables.count(GVU) && isa<Function>(&GV)) {
LLVM_DEBUG(dbgs() << "Ignoring dep " << GVU->getName() << " -> "
<< GV.getName() << "\n");
continue;
}
GVDependencies[GVU].insert(&GV);
}
}
void GlobalDCEPass::MarkLive(GlobalValue &GV,
SmallVectorImpl<GlobalValue *> *Updates) {
auto const Ret = AliveGlobals.insert(&GV);
if (!Ret.second)
return;
if (Updates)
Updates->push_back(&GV);
if (Comdat *C = GV.getComdat()) {
for (auto &&CM : make_range(ComdatMembers.equal_range(C))) {
MarkLive(*CM.second, Updates); }
}
}
void GlobalDCEPass::ScanVTables(Module &M) {
SmallVector<MDNode *, 2> Types;
LLVM_DEBUG(dbgs() << "Building type info -> vtable map\n");
auto *LTOPostLinkMD =
cast_or_null<ConstantAsMetadata>(M.getModuleFlag("LTOPostLink"));
bool LTOPostLink =
LTOPostLinkMD &&
(cast<ConstantInt>(LTOPostLinkMD->getValue())->getZExtValue() != 0);
for (GlobalVariable &GV : M.globals()) {
Types.clear();
GV.getMetadata(LLVMContext::MD_type, Types);
if (GV.isDeclaration() || Types.empty())
continue;
for (MDNode *Type : Types) {
Metadata *TypeID = Type->getOperand(1).get();
uint64_t Offset =
cast<ConstantInt>(
cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
->getZExtValue();
TypeIdMap[TypeID].insert(std::make_pair(&GV, Offset));
}
if (auto GO = dyn_cast<GlobalObject>(&GV)) {
GlobalObject::VCallVisibility TypeVis = GO->getVCallVisibility();
if (TypeVis == GlobalObject::VCallVisibilityTranslationUnit ||
(LTOPostLink &&
TypeVis == GlobalObject::VCallVisibilityLinkageUnit)) {
LLVM_DEBUG(dbgs() << GV.getName() << " is safe for VFE\n");
VFESafeVTables.insert(&GV);
}
}
}
}
void GlobalDCEPass::ScanVTableLoad(Function *Caller, Metadata *TypeId,
uint64_t CallOffset) {
for (auto &VTableInfo : TypeIdMap[TypeId]) {
GlobalVariable *VTable = VTableInfo.first;
uint64_t VTableOffset = VTableInfo.second;
Constant *Ptr =
getPointerAtOffset(VTable->getInitializer(), VTableOffset + CallOffset,
*Caller->getParent(), VTable);
if (!Ptr) {
LLVM_DEBUG(dbgs() << "can't find pointer in vtable!\n");
VFESafeVTables.erase(VTable);
continue;
}
auto Callee = dyn_cast<Function>(Ptr->stripPointerCasts());
if (!Callee) {
LLVM_DEBUG(dbgs() << "vtable entry is not function pointer!\n");
VFESafeVTables.erase(VTable);
continue;
}
LLVM_DEBUG(dbgs() << "vfunc dep " << Caller->getName() << " -> "
<< Callee->getName() << "\n");
GVDependencies[Caller].insert(Callee);
}
}
void GlobalDCEPass::ScanTypeCheckedLoadIntrinsics(Module &M) {
LLVM_DEBUG(dbgs() << "Scanning type.checked.load intrinsics\n");
Function *TypeCheckedLoadFunc =
M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
if (!TypeCheckedLoadFunc)
return;
for (auto U : TypeCheckedLoadFunc->users()) {
auto CI = dyn_cast<CallInst>(U);
if (!CI)
continue;
auto *Offset = dyn_cast<ConstantInt>(CI->getArgOperand(1));
Value *TypeIdValue = CI->getArgOperand(2);
auto *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata();
if (Offset) {
ScanVTableLoad(CI->getFunction(), TypeId, Offset->getZExtValue());
} else {
for (auto &VTableInfo : TypeIdMap[TypeId]) {
VFESafeVTables.erase(VTableInfo.first);
}
}
}
}
void GlobalDCEPass::AddVirtualFunctionDependencies(Module &M) {
if (!ClEnableVFE)
return;
auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
M.getModuleFlag("Virtual Function Elim"));
if (!Val || Val->getZExtValue() == 0)
return;
ScanVTables(M);
if (VFESafeVTables.empty())
return;
ScanTypeCheckedLoadIntrinsics(M);
LLVM_DEBUG(
dbgs() << "VFE safe vtables:\n";
for (auto *VTable : VFESafeVTables)
dbgs() << " " << VTable->getName() << "\n";
);
}
PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &MAM) {
bool Changed = false;
Changed |= optimizeGlobalCtorsList(
M, [](uint32_t, Function *F) { return isEmptyFunction(F); });
for (Function &F : M)
if (Comdat *C = F.getComdat())
ComdatMembers.insert(std::make_pair(C, &F));
for (GlobalVariable &GV : M.globals())
if (Comdat *C = GV.getComdat())
ComdatMembers.insert(std::make_pair(C, &GV));
for (GlobalAlias &GA : M.aliases())
if (Comdat *C = GA.getComdat())
ComdatMembers.insert(std::make_pair(C, &GA));
AddVirtualFunctionDependencies(M);
for (GlobalObject &GO : M.global_objects()) {
GO.removeDeadConstantUsers();
if (!GO.isDeclaration())
if (!GO.isDiscardableIfUnused())
MarkLive(GO);
UpdateGVDependencies(GO);
}
for (GlobalAlias &GA : M.aliases()) {
GA.removeDeadConstantUsers();
if (!GA.isDiscardableIfUnused())
MarkLive(GA);
UpdateGVDependencies(GA);
}
for (GlobalIFunc &GIF : M.ifuncs()) {
GIF.removeDeadConstantUsers();
if (!GIF.isDiscardableIfUnused())
MarkLive(GIF);
UpdateGVDependencies(GIF);
}
SmallVector<GlobalValue *, 8> NewLiveGVs{AliveGlobals.begin(),
AliveGlobals.end()};
while (!NewLiveGVs.empty()) {
GlobalValue *LGV = NewLiveGVs.pop_back_val();
for (auto *GVD : GVDependencies[LGV])
MarkLive(*GVD, &NewLiveGVs);
}
std::vector<GlobalVariable *> DeadGlobalVars; for (GlobalVariable &GV : M.globals())
if (!AliveGlobals.count(&GV)) {
DeadGlobalVars.push_back(&GV); if (GV.hasInitializer()) {
Constant *Init = GV.getInitializer();
GV.setInitializer(nullptr);
if (isSafeToDestroyConstant(Init))
Init->destroyConstant();
}
}
std::vector<Function *> DeadFunctions;
for (Function &F : M)
if (!AliveGlobals.count(&F)) {
DeadFunctions.push_back(&F); if (!F.isDeclaration())
F.deleteBody();
}
std::vector<GlobalAlias*> DeadAliases;
for (GlobalAlias &GA : M.aliases())
if (!AliveGlobals.count(&GA)) {
DeadAliases.push_back(&GA);
GA.setAliasee(nullptr);
}
std::vector<GlobalIFunc*> DeadIFuncs;
for (GlobalIFunc &GIF : M.ifuncs())
if (!AliveGlobals.count(&GIF)) {
DeadIFuncs.push_back(&GIF);
GIF.setResolver(nullptr);
}
auto EraseUnusedGlobalValue = [&](GlobalValue *GV) {
GV->removeDeadConstantUsers();
GV->eraseFromParent();
Changed = true;
};
NumFunctions += DeadFunctions.size();
for (Function *F : DeadFunctions) {
if (!F->use_empty()) {
++NumVFuncs;
replaceRelativePointerUsersWithZero(F);
F->replaceNonMetadataUsesWith(ConstantPointerNull::get(F->getType()));
}
EraseUnusedGlobalValue(F);
}
NumVariables += DeadGlobalVars.size();
for (GlobalVariable *GV : DeadGlobalVars)
EraseUnusedGlobalValue(GV);
NumAliases += DeadAliases.size();
for (GlobalAlias *GA : DeadAliases)
EraseUnusedGlobalValue(GA);
NumIFuncs += DeadIFuncs.size();
for (GlobalIFunc *GIF : DeadIFuncs)
EraseUnusedGlobalValue(GIF);
AliveGlobals.clear();
ConstantDependenciesCache.clear();
GVDependencies.clear();
ComdatMembers.clear();
TypeIdMap.clear();
VFESafeVTables.clear();
if (Changed)
return PreservedAnalyses::none();
return PreservedAnalyses::all();
}