23 #define DEBUG_TYPE "ctor_utils" 29 void removeGlobalCtors(GlobalVariable *GCL,
const BitVector &CtorsToRemove) {
31 ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
32 SmallVector<Constant *, 10> CAList;
33 for (
unsigned I = 0,
E = OldCA->getNumOperands();
I <
E; ++
I)
34 if (!CtorsToRemove.test(
I))
35 CAList.push_back(OldCA->getOperand(
I));
43 if (CA->getType() == OldCA->getType()) {
44 GCL->setInitializer(CA);
50 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
51 CA,
"", GCL->getThreadLocalMode());
52 GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
56 if (!GCL->use_empty()) {
58 if (V->getType() != GCL->getType())
62 GCL->eraseFromParent();
67 std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
68 if (GV->getInitializer()->isNullValue())
69 return std::vector<Function *>();
70 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
71 std::vector<Function *>
Result;
72 Result.reserve(CA->getNumOperands());
73 for (
auto &V : CA->operands()) {
74 ConstantStruct *CS = cast<ConstantStruct>(V);
75 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
82 GlobalVariable *findGlobalCtors(Module &M) {
83 GlobalVariable *GV = M.getGlobalVariable(
"llvm.global_ctors");
89 if (!GV->hasUniqueInitializer())
92 if (isa<ConstantAggregateZero>(GV->getInitializer()))
94 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
96 for (
auto &V : CA->operands()) {
97 if (isa<ConstantAggregateZero>(V))
99 ConstantStruct *CS = cast<ConstantStruct>(V);
100 if (isa<ConstantPointerNull>(CS->getOperand(1)))
104 if (!isa<Function>(CS->getOperand(1)))
108 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
109 if (CI->getZExtValue() != 65535)
125 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
129 bool MadeChange =
false;
132 unsigned NumCtors = Ctors.size();
134 for (
unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
141 LLVM_DEBUG(
dbgs() <<
"Optimizing Global Constructor: " << *F <<
"\n");
148 if (ShouldRemove(F)) {
150 CtorsToRemove.
set(i);
160 removeGlobalCtors(GlobalCtors, CtorsToRemove);
Compute iterated dominance frontiers using a linear time algorithm.
A Module instance is used to store all the information related to an LLVM module. ...
An efficient, type-erasing, non-owning reference to a callable.
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
bool optimizeGlobalCtorsList(Module &M, function_ref< bool(Function *)> ShouldRemove)
Call "ShouldRemove" for every entry in M's global_ctor list and remove the entries for which it retur...