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.
34 StructType *StructTy =
35 cast<StructType>(GCL->getType()->getElementType()->getArrayElementType());
37 // Create the new init list.
38 std::vector<Constant *> CAList;
39 for (Function *F : Ctors) {
40 Type *Int32Ty = Type::getInt32Ty(GCL->getContext());
42 CSVals[0] = ConstantInt::get(Int32Ty, 65535);
45 CSVals[0] = ConstantInt::get(Int32Ty, 0x7fffffff);
46 CSVals[1] = Constant::getNullValue(StructTy->getElementType(1));
48 // FIXME: Only allow the 3-field form in LLVM 4.0.
49 size_t NumElts = StructTy->getNumElements();
51 CSVals[2] = Constant::getNullValue(StructTy->getElementType(2));
53 ConstantStruct::get(StructTy, makeArrayRef(CSVals, NumElts)));
56 // Create the array initializer.
58 ConstantArray::get(ArrayType::get(StructTy, CAList.size()), CAList);
60 // If we didn't change the number of elements, don't create a new GV.
61 if (CA->getType() == GCL->getInitializer()->getType()) {
62 GCL->setInitializer(CA);
66 // Create the new global and insert it next to the existing list.
68 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
69 CA, "", GCL->getThreadLocalMode());
70 GCL->getParent()->getGlobalList().insert(GCL, NGV);
73 // Nuke the old list, replacing any uses with the new one.
74 if (!GCL->use_empty()) {
76 if (V->getType() != GCL->getType())
77 V = ConstantExpr::getBitCast(V, GCL->getType());
78 GCL->replaceAllUsesWith(V);
80 GCL->eraseFromParent();
83 /// Given a llvm.global_ctors list that we can understand,
84 /// return a list of the functions and null terminator as a vector.
85 std::vector<Function*> parseGlobalCtors(GlobalVariable *GV) {
86 if (GV->getInitializer()->isNullValue())
87 return std::vector<Function *>();
88 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
89 std::vector<Function *> Result;
90 Result.reserve(CA->getNumOperands());
91 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
92 ConstantStruct *CS = cast<ConstantStruct>(*i);
93 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
98 /// Find the llvm.global_ctors list, verifying that all initializers have an
99 /// init priority of 65535.
100 GlobalVariable *findGlobalCtors(Module &M) {
101 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
105 // Verify that the initializer is simple enough for us to handle. We are
106 // only allowed to optimize the initializer if it is unique.
107 if (!GV->hasUniqueInitializer())
110 if (isa<ConstantAggregateZero>(GV->getInitializer()))
112 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
114 for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
115 if (isa<ConstantAggregateZero>(*i))
117 ConstantStruct *CS = cast<ConstantStruct>(*i);
118 if (isa<ConstantPointerNull>(CS->getOperand(1)))
121 // Must have a function or null ptr.
122 if (!isa<Function>(CS->getOperand(1)))
125 // Init priority must be standard.
126 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
127 if (CI->getZExtValue() != 65535)
135 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
136 /// entries for which it returns true. Return true if anything changed.
137 bool optimizeGlobalCtorsList(Module &M,
138 function_ref<bool(Function *)> ShouldRemove) {
139 GlobalVariable *GlobalCtors = findGlobalCtors(M);
143 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
147 bool MadeChange = false;
149 // Loop over global ctors, optimizing them when we can.
150 for (unsigned i = 0; i != Ctors.size(); ++i) {
151 Function *F = Ctors[i];
152 // Found a null terminator in the middle of the list, prune off the rest of
155 if (i != Ctors.size() - 1) {
161 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
163 // We cannot simplify external ctor functions.
167 // If we can evaluate the ctor at compile time, do.
168 if (ShouldRemove(F)) {
169 Ctors.erase(Ctors.begin() + i);
179 installGlobalCtors(GlobalCtors, Ctors);
183 } // End llvm namespace