#include "llvm/Transforms/Utils/CtorUtils.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <numeric>
#define DEBUG_TYPE "ctor_utils"
using namespace llvm;
static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
SmallVector<Constant *, 10> CAList;
for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
if (!CtorsToRemove.test(I))
CAList.push_back(OldCA->getOperand(I));
ArrayType *ATy =
ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
Constant *CA = ConstantArray::get(ATy, CAList);
if (CA->getType() == OldCA->getType()) {
GCL->setInitializer(CA);
return;
}
GlobalVariable *NGV =
new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
CA, "", GCL->getThreadLocalMode());
GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
NGV->takeName(GCL);
if (!GCL->use_empty()) {
Constant *V = NGV;
if (V->getType() != GCL->getType())
V = ConstantExpr::getBitCast(V, GCL->getType());
GCL->replaceAllUsesWith(V);
}
GCL->eraseFromParent();
}
static std::vector<std::pair<uint32_t, Function *>>
parseGlobalCtors(GlobalVariable *GV) {
ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
std::vector<std::pair<uint32_t, Function *>> Result;
Result.reserve(CA->getNumOperands());
for (auto &V : CA->operands()) {
ConstantStruct *CS = cast<ConstantStruct>(V);
Result.emplace_back(cast<ConstantInt>(CS->getOperand(0))->getZExtValue(),
dyn_cast<Function>(CS->getOperand(1)));
}
return Result;
}
static GlobalVariable *findGlobalCtors(Module &M) {
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
if (!GV)
return nullptr;
if (!GV->hasUniqueInitializer())
return nullptr;
ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
if (!CA)
return nullptr;
for (auto &V : CA->operands()) {
if (isa<ConstantAggregateZero>(V))
continue;
ConstantStruct *CS = cast<ConstantStruct>(V);
if (isa<ConstantPointerNull>(CS->getOperand(1)))
continue;
Function *F = dyn_cast<Function>(CS->getOperand(1));
if (!F || F->arg_size() != 0)
return nullptr;
}
return GV;
}
bool llvm::optimizeGlobalCtorsList(
Module &M, function_ref<bool(uint32_t, Function *)> ShouldRemove) {
GlobalVariable *GlobalCtors = findGlobalCtors(M);
if (!GlobalCtors)
return false;
std::vector<std::pair<uint32_t, Function *>> Ctors =
parseGlobalCtors(GlobalCtors);
if (Ctors.empty())
return false;
bool MadeChange = false;
BitVector CtorsToRemove(Ctors.size());
std::vector<size_t> CtorsByPriority(Ctors.size());
std::iota(CtorsByPriority.begin(), CtorsByPriority.end(), 0);
stable_sort(CtorsByPriority, [&](size_t LHS, size_t RHS) {
return Ctors[LHS].first < Ctors[RHS].first;
});
for (unsigned CtorIndex : CtorsByPriority) {
const uint32_t Priority = Ctors[CtorIndex].first;
Function *F = Ctors[CtorIndex].second;
if (!F)
continue;
LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
if (ShouldRemove(Priority, F)) {
Ctors[CtorIndex].second = nullptr;
CtorsToRemove.set(CtorIndex);
MadeChange = true;
continue;
}
}
if (!MadeChange)
return false;
removeGlobalCtors(GlobalCtors, CtorsToRemove);
return true;
}