1 //===- CtorUtils.cpp - Helpers for working with global_ctors ----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines functions that are used to process llvm.global_ctors.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/Utils/CtorUtils.h"
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
23 #define DEBUG_TYPE "ctor_utils"
27 /// Given a specified llvm.global_ctors list, remove the listed elements.
28 static void removeGlobalCtors(GlobalVariable
*GCL
, const BitVector
&CtorsToRemove
) {
29 // Filter out the initializer elements to remove.
30 ConstantArray
*OldCA
= cast
<ConstantArray
>(GCL
->getInitializer());
31 SmallVector
<Constant
*, 10> CAList
;
32 for (unsigned I
= 0, E
= OldCA
->getNumOperands(); I
< E
; ++I
)
33 if (!CtorsToRemove
.test(I
))
34 CAList
.push_back(OldCA
->getOperand(I
));
36 // Create the new array initializer.
38 ArrayType::get(OldCA
->getType()->getElementType(), CAList
.size());
39 Constant
*CA
= ConstantArray::get(ATy
, CAList
);
41 // If we didn't change the number of elements, don't create a new GV.
42 if (CA
->getType() == OldCA
->getType()) {
43 GCL
->setInitializer(CA
);
47 // Create the new global and insert it next to the existing list.
49 new GlobalVariable(CA
->getType(), GCL
->isConstant(), GCL
->getLinkage(),
50 CA
, "", GCL
->getThreadLocalMode());
51 GCL
->getParent()->insertGlobalVariable(GCL
->getIterator(), NGV
);
54 // Nuke the old list, replacing any uses with the new one.
55 if (!GCL
->use_empty())
56 GCL
->replaceAllUsesWith(NGV
);
58 GCL
->eraseFromParent();
61 /// Given a llvm.global_ctors list that we can understand,
62 /// return a list of the functions and null terminator as a vector.
63 static std::vector
<std::pair
<uint32_t, Function
*>>
64 parseGlobalCtors(GlobalVariable
*GV
) {
65 ConstantArray
*CA
= cast
<ConstantArray
>(GV
->getInitializer());
66 std::vector
<std::pair
<uint32_t, Function
*>> Result
;
67 Result
.reserve(CA
->getNumOperands());
68 for (auto &V
: CA
->operands()) {
69 ConstantStruct
*CS
= cast
<ConstantStruct
>(V
);
70 Result
.emplace_back(cast
<ConstantInt
>(CS
->getOperand(0))->getZExtValue(),
71 dyn_cast
<Function
>(CS
->getOperand(1)));
76 /// Find the llvm.global_ctors list.
77 static GlobalVariable
*findGlobalCtors(Module
&M
) {
78 GlobalVariable
*GV
= M
.getGlobalVariable("llvm.global_ctors");
82 // Verify that the initializer is simple enough for us to handle. We are
83 // only allowed to optimize the initializer if it is unique.
84 if (!GV
->hasUniqueInitializer())
87 // If there are no ctors, then the initializer might be null/undef/poison.
88 // Ignore anything but an array.
89 ConstantArray
*CA
= dyn_cast
<ConstantArray
>(GV
->getInitializer());
93 for (auto &V
: CA
->operands()) {
94 if (isa
<ConstantAggregateZero
>(V
))
96 ConstantStruct
*CS
= cast
<ConstantStruct
>(V
);
97 if (isa
<ConstantPointerNull
>(CS
->getOperand(1)))
100 // Can only handle global constructors with no arguments.
101 Function
*F
= dyn_cast
<Function
>(CS
->getOperand(1));
102 if (!F
|| F
->arg_size() != 0)
108 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
109 /// entries for which it returns true. Return true if anything changed.
110 bool llvm::optimizeGlobalCtorsList(
111 Module
&M
, function_ref
<bool(uint32_t, Function
*)> ShouldRemove
) {
112 GlobalVariable
*GlobalCtors
= findGlobalCtors(M
);
116 std::vector
<std::pair
<uint32_t, Function
*>> Ctors
=
117 parseGlobalCtors(GlobalCtors
);
121 bool MadeChange
= false;
122 // Loop over global ctors, optimizing them when we can.
123 BitVector
CtorsToRemove(Ctors
.size());
124 std::vector
<size_t> CtorsByPriority(Ctors
.size());
125 std::iota(CtorsByPriority
.begin(), CtorsByPriority
.end(), 0);
126 stable_sort(CtorsByPriority
, [&](size_t LHS
, size_t RHS
) {
127 return Ctors
[LHS
].first
< Ctors
[RHS
].first
;
129 for (unsigned CtorIndex
: CtorsByPriority
) {
130 const uint32_t Priority
= Ctors
[CtorIndex
].first
;
131 Function
*F
= Ctors
[CtorIndex
].second
;
135 LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F
<< "\n");
137 // If we can evaluate the ctor at compile time, do.
138 if (ShouldRemove(Priority
, F
)) {
139 Ctors
[CtorIndex
].second
= nullptr;
140 CtorsToRemove
.set(CtorIndex
);
149 removeGlobalCtors(GlobalCtors
, CtorsToRemove
);