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/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
22 #define DEBUG_TYPE "ctor_utils"
27 /// Given a specified llvm.global_ctors list, install the
29 void installGlobalCtors(GlobalVariable *GCL,
30 const std::vector<Function *> &Ctors) {
31 // If we made a change, reassemble the initializer list.
33 CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), 65535);
36 StructType *StructTy =
37 cast<StructType>(GCL->getType()->getElementType()->getArrayElementType());
39 // Create the new init list.
40 std::vector<Constant *> CAList;
41 for (unsigned i = 0, e = Ctors.size(); i != e; ++i) {
45 Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()), false);
46 PointerType *PFTy = PointerType::getUnqual(FTy);
47 CSVals[1] = Constant::getNullValue(PFTy);
49 ConstantInt::get(Type::getInt32Ty(GCL->getContext()), 0x7fffffff);
51 CAList.push_back(ConstantStruct::get(StructTy, CSVals));
54 // Create the array initializer.
56 ConstantArray::get(ArrayType::get(StructTy, CAList.size()), CAList);
58 // If we didn't change the number of elements, don't create a new GV.
59 if (CA->getType() == GCL->getInitializer()->getType()) {
60 GCL->setInitializer(CA);
64 // Create the new global and insert it next to the existing list.
66 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
67 CA, "", GCL->getThreadLocalMode());
68 GCL->getParent()->getGlobalList().insert(GCL, NGV);
71 // Nuke the old list, replacing any uses with the new one.
72 if (!GCL->use_empty()) {
74 if (V->getType() != GCL->getType())
75 V = ConstantExpr::getBitCast(V, GCL->getType());
76 GCL->replaceAllUsesWith(V);
78 GCL->eraseFromParent();
81 /// Given a llvm.global_ctors list that we can understand,
82 /// return a list of the functions and null terminator as a vector.
83 std::vector<Function*> parseGlobalCtors(GlobalVariable *GV) {
84 if (GV->getInitializer()->isNullValue())
85 return std::vector<Function *>();
86 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
87 std::vector<Function *> Result;
88 Result.reserve(CA->getNumOperands());
89 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
90 ConstantStruct *CS = cast<ConstantStruct>(*i);
91 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
96 /// Find the llvm.global_ctors list, verifying that all initializers have an
97 /// init priority of 65535.
98 GlobalVariable *findGlobalCtors(Module &M) {
99 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
103 // Verify that the initializer is simple enough for us to handle. We are
104 // only allowed to optimize the initializer if it is unique.
105 if (!GV->hasUniqueInitializer())
108 if (isa<ConstantAggregateZero>(GV->getInitializer()))
110 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
112 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
113 if (isa<ConstantAggregateZero>(*i))
115 ConstantStruct *CS = cast<ConstantStruct>(*i);
116 if (isa<ConstantPointerNull>(CS->getOperand(1)))
119 // Must have a function or null ptr.
120 if (!isa<Function>(CS->getOperand(1)))
123 // Init priority must be standard.
124 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
125 if (CI->getZExtValue() != 65535)
133 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
134 /// entries for which it returns true. Return true if anything changed.
135 bool optimizeGlobalCtorsList(Module &M, ShouldRemoveCtor ShouldRemove,
137 GlobalVariable *GlobalCtors = findGlobalCtors(M);
141 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
145 bool MadeChange = false;
147 // Loop over global ctors, optimizing them when we can.
148 for (unsigned i = 0; i != Ctors.size(); ++i) {
149 Function *F = Ctors[i];
150 // Found a null terminator in the middle of the list, prune off the rest of
153 if (i != Ctors.size() - 1) {
159 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
161 // We cannot simplify external ctor functions.
165 // If we can evaluate the ctor at compile time, do.
166 if (ShouldRemove(Context, F)) {
167 Ctors.erase(Ctors.begin() + i);
177 installGlobalCtors(GlobalCtors, Ctors);
181 } // End llvm namespace