1 //===- CtorUtils.cpp - Helpers for working with global_ctors ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines functions that are used to process llvm.global_ctors.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/Transforms/Utils/CtorUtils.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/Debug.h"
23 #define DEBUG_TYPE "ctor_utils"
28 /// Given a specified llvm.global_ctors list, remove the listed elements.
29 void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
30 // Filter out the initializer elements to remove.
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));
37 // Create the new array initializer.
39 ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
40 Constant *CA = ConstantArray::get(ATy, CAList);
42 // If we didn't change the number of elements, don't create a new GV.
43 if (CA->getType() == OldCA->getType()) {
44 GCL->setInitializer(CA);
48 // Create the new global and insert it next to the existing list.
50 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
51 CA, "", GCL->getThreadLocalMode());
52 GCL->getParent()->getGlobalList().insert(GCL, NGV);
55 // Nuke the old list, replacing any uses with the new one.
56 if (!GCL->use_empty()) {
58 if (V->getType() != GCL->getType())
59 V = ConstantExpr::getBitCast(V, GCL->getType());
60 GCL->replaceAllUsesWith(V);
62 GCL->eraseFromParent();
65 /// Given a llvm.global_ctors list that we can understand,
66 /// return a list of the functions and null terminator as a vector.
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 (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
74 ConstantStruct *CS = cast<ConstantStruct>(*i);
75 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
80 /// Find the llvm.global_ctors list, verifying that all initializers have an
81 /// init priority of 65535.
82 GlobalVariable *findGlobalCtors(Module &M) {
83 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
87 // Verify that the initializer is simple enough for us to handle. We are
88 // only allowed to optimize the initializer if it is unique.
89 if (!GV->hasUniqueInitializer())
92 if (isa<ConstantAggregateZero>(GV->getInitializer()))
94 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
96 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
97 if (isa<ConstantAggregateZero>(*i))
99 ConstantStruct *CS = cast<ConstantStruct>(*i);
100 if (isa<ConstantPointerNull>(CS->getOperand(1)))
103 // Must have a function or null ptr.
104 if (!isa<Function>(CS->getOperand(1)))
107 // Init priority must be standard.
108 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
109 if (CI->getZExtValue() != 65535)
117 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
118 /// entries for which it returns true. Return true if anything changed.
119 bool optimizeGlobalCtorsList(Module &M,
120 function_ref<bool(Function *)> ShouldRemove) {
121 GlobalVariable *GlobalCtors = findGlobalCtors(M);
125 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
129 bool MadeChange = false;
131 // Loop over global ctors, optimizing them when we can.
132 unsigned NumCtors = Ctors.size();
133 BitVector CtorsToRemove(NumCtors);
134 for (unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
135 Function *F = Ctors[i];
136 // Found a null terminator in the middle of the list, prune off the rest of
141 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
143 // We cannot simplify external ctor functions.
147 // If we can evaluate the ctor at compile time, do.
148 if (ShouldRemove(F)) {
150 CtorsToRemove.set(i);
160 removeGlobalCtors(GlobalCtors, CtorsToRemove);
164 } // End llvm namespace