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/Transforms/Utils/CtorUtils.h"
15 #include "llvm/ADT/BitVector.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"
22 #include "llvm/Support/raw_ostream.h"
24 #define DEBUG_TYPE "ctor_utils"
29 /// Given a specified llvm.global_ctors list, remove the listed elements.
30 void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
31 // Filter out the initializer elements to remove.
32 ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
33 SmallVector<Constant *, 10> CAList;
34 for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
35 if (!CtorsToRemove.test(I))
36 CAList.push_back(OldCA->getOperand(I));
38 // Create the new array initializer.
40 ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
41 Constant *CA = ConstantArray::get(ATy, CAList);
43 // If we didn't change the number of elements, don't create a new GV.
44 if (CA->getType() == OldCA->getType()) {
45 GCL->setInitializer(CA);
49 // Create the new global and insert it next to the existing list.
51 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
52 CA, "", GCL->getThreadLocalMode());
53 GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
56 // Nuke the old list, replacing any uses with the new one.
57 if (!GCL->use_empty()) {
59 if (V->getType() != GCL->getType())
60 V = ConstantExpr::getBitCast(V, GCL->getType());
61 GCL->replaceAllUsesWith(V);
63 GCL->eraseFromParent();
66 /// Given a llvm.global_ctors list that we can understand,
67 /// return a list of the functions and null terminator as a vector.
68 std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
69 if (GV->getInitializer()->isNullValue())
70 return std::vector<Function *>();
71 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
72 std::vector<Function *> Result;
73 Result.reserve(CA->getNumOperands());
74 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
75 ConstantStruct *CS = cast<ConstantStruct>(*i);
76 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
81 /// Find the llvm.global_ctors list, verifying that all initializers have an
82 /// init priority of 65535.
83 GlobalVariable *findGlobalCtors(Module &M) {
84 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
88 // Verify that the initializer is simple enough for us to handle. We are
89 // only allowed to optimize the initializer if it is unique.
90 if (!GV->hasUniqueInitializer())
93 if (isa<ConstantAggregateZero>(GV->getInitializer()))
95 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
97 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
98 if (isa<ConstantAggregateZero>(*i))
100 ConstantStruct *CS = cast<ConstantStruct>(*i);
101 if (isa<ConstantPointerNull>(CS->getOperand(1)))
104 // Must have a function or null ptr.
105 if (!isa<Function>(CS->getOperand(1)))
108 // Init priority must be standard.
109 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
110 if (CI->getZExtValue() != 65535)
118 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
119 /// entries for which it returns true. Return true if anything changed.
120 bool optimizeGlobalCtorsList(Module &M,
121 function_ref<bool(Function *)> ShouldRemove) {
122 GlobalVariable *GlobalCtors = findGlobalCtors(M);
126 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
130 bool MadeChange = false;
132 // Loop over global ctors, optimizing them when we can.
133 unsigned NumCtors = Ctors.size();
134 BitVector CtorsToRemove(NumCtors);
135 for (unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
136 Function *F = Ctors[i];
137 // Found a null terminator in the middle of the list, prune off the rest of
142 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
144 // We cannot simplify external ctor functions.
148 // If we can evaluate the ctor at compile time, do.
149 if (ShouldRemove(F)) {
151 CtorsToRemove.set(i);
161 removeGlobalCtors(GlobalCtors, CtorsToRemove);
165 } // End llvm namespace