LLVM 20.0.0git
CtorUtils.cpp
Go to the documentation of this file.
1//===- CtorUtils.cpp - Helpers for working with global_ctors ----*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines functions that are used to process llvm.global_ctors.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/BitVector.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
18#include "llvm/IR/Module.h"
19#include "llvm/Support/Debug.h"
21#include <numeric>
22
23#define DEBUG_TYPE "ctor_utils"
24
25using namespace llvm;
26
27/// Given a specified llvm.global_ctors list, remove the listed elements.
28static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
29 // Filter out the initializer elements to remove.
30 ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
32 for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
33 if (!CtorsToRemove.test(I))
34 CAList.push_back(OldCA->getOperand(I));
35
36 // Create the new array initializer.
37 ArrayType *ATy =
38 ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
39 Constant *CA = ConstantArray::get(ATy, CAList);
40
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);
44 return;
45 }
46
47 // Create the new global and insert it next to the existing list.
48 GlobalVariable *NGV =
49 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
50 CA, "", GCL->getThreadLocalMode());
51 GCL->getParent()->insertGlobalVariable(GCL->getIterator(), NGV);
52 NGV->takeName(GCL);
53
54 // Nuke the old list, replacing any uses with the new one.
55 if (!GCL->use_empty())
56 GCL->replaceAllUsesWith(NGV);
57
58 GCL->eraseFromParent();
59}
60
61/// Given a llvm.global_ctors list that we can understand,
62/// return a list of the functions and null terminator as a vector.
63static std::vector<std::pair<uint32_t, Function *>>
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)));
72 }
73 return Result;
74}
75
76/// Find the llvm.global_ctors list.
78 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
79 if (!GV)
80 return nullptr;
81
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())
85 return nullptr;
86
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());
90 if (!CA)
91 return nullptr;
92
93 for (auto &V : CA->operands()) {
94 if (isa<ConstantAggregateZero>(V))
95 continue;
96 ConstantStruct *CS = cast<ConstantStruct>(V);
97 if (isa<ConstantPointerNull>(CS->getOperand(1)))
98 continue;
99
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)
103 return nullptr;
104 }
105 return GV;
106}
107
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.
111 Module &M, function_ref<bool(uint32_t, Function *)> ShouldRemove) {
112 GlobalVariable *GlobalCtors = findGlobalCtors(M);
113 if (!GlobalCtors)
114 return false;
115
116 std::vector<std::pair<uint32_t, Function *>> Ctors =
117 parseGlobalCtors(GlobalCtors);
118 if (Ctors.empty())
119 return false;
120
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;
128 });
129 for (unsigned CtorIndex : CtorsByPriority) {
130 const uint32_t Priority = Ctors[CtorIndex].first;
131 Function *F = Ctors[CtorIndex].second;
132 if (!F)
133 continue;
134
135 LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
136
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);
141 MadeChange = true;
142 continue;
143 }
144 }
145
146 if (!MadeChange)
147 return false;
148
149 removeGlobalCtors(GlobalCtors, CtorsToRemove);
150 return true;
151}
This file implements the BitVector class.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove)
Given a specified llvm.global_ctors list, remove the listed elements.
Definition: CtorUtils.cpp:28
static std::vector< std::pair< uint32_t, Function * > > parseGlobalCtors(GlobalVariable *GV)
Given a llvm.global_ctors list that we can understand, return a list of the functions and null termin...
Definition: CtorUtils.cpp:64
static GlobalVariable * findGlobalCtors(Module &M)
Find the llvm.global_ctors list.
Definition: CtorUtils.cpp:77
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
Value * RHS
Value * LHS
Type * getElementType() const
Definition: DerivedTypes.h:384
bool test(unsigned Idx) const
Definition: BitVector.h:461
BitVector & set()
Definition: BitVector.h:351
ConstantArray - Constant Array Declarations.
Definition: Constants.h:424
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1292
ArrayType * getType() const
Specialize the getType() method to always return an ArrayType, which reduces the amount of casting ne...
Definition: Constants.h:443
This is an important base class in LLVM.
Definition: Constant.h:42
LinkageTypes getLinkage() const
Definition: GlobalValue.h:546
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:271
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:485
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:481
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
void insertGlobalVariable(GlobalVariable *GV)
Insert global variable GV at the end of the global variable list and take ownership.
Definition: Module.h:583
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
op_range operands()
Definition: User.h:242
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
bool use_empty() const
Definition: Value.h:344
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition: ilist_node.h:132
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void stable_sort(R &&Range)
Definition: STLExtras.h:2020
bool optimizeGlobalCtorsList(Module &M, function_ref< bool(uint32_t, Function *)> ShouldRemove)
Call "ShouldRemove" for every entry in M's global_ctor list and remove the entries for which it retur...
Definition: CtorUtils.cpp:110
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163