xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Utils/CtorUtils.cpp (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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 
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"
21 #include <numeric>
22 
23 #define DEBUG_TYPE "ctor_utils"
24 
25 using namespace llvm;
26 
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));
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()->getGlobalList().insert(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     Constant *V = NGV;
57     if (V->getType() != GCL->getType())
58       V = ConstantExpr::getBitCast(V, GCL->getType());
59     GCL->replaceAllUsesWith(V);
60   }
61   GCL->eraseFromParent();
62 }
63 
64 /// Given a llvm.global_ctors list that we can understand,
65 /// return a list of the functions and null terminator as a vector.
66 static std::vector<std::pair<uint32_t, Function *>>
67 parseGlobalCtors(GlobalVariable *GV) {
68   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
69   std::vector<std::pair<uint32_t, Function *>> Result;
70   Result.reserve(CA->getNumOperands());
71   for (auto &V : CA->operands()) {
72     ConstantStruct *CS = cast<ConstantStruct>(V);
73     Result.emplace_back(cast<ConstantInt>(CS->getOperand(0))->getZExtValue(),
74                         dyn_cast<Function>(CS->getOperand(1)));
75   }
76   return Result;
77 }
78 
79 /// Find the llvm.global_ctors list.
80 static GlobalVariable *findGlobalCtors(Module &M) {
81   GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
82   if (!GV)
83     return nullptr;
84 
85   // Verify that the initializer is simple enough for us to handle. We are
86   // only allowed to optimize the initializer if it is unique.
87   if (!GV->hasUniqueInitializer())
88     return nullptr;
89 
90   // If there are no ctors, then the initializer might be null/undef/poison.
91   // Ignore anything but an array.
92   ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
93   if (!CA)
94     return nullptr;
95 
96   for (auto &V : CA->operands()) {
97     if (isa<ConstantAggregateZero>(V))
98       continue;
99     ConstantStruct *CS = cast<ConstantStruct>(V);
100     if (isa<ConstantPointerNull>(CS->getOperand(1)))
101       continue;
102 
103     // Can only handle global constructors with no arguments.
104     Function *F = dyn_cast<Function>(CS->getOperand(1));
105     if (!F || F->arg_size() != 0)
106       return nullptr;
107   }
108   return GV;
109 }
110 
111 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
112 /// entries for which it returns true.  Return true if anything changed.
113 bool llvm::optimizeGlobalCtorsList(
114     Module &M, function_ref<bool(uint32_t, Function *)> ShouldRemove) {
115   GlobalVariable *GlobalCtors = findGlobalCtors(M);
116   if (!GlobalCtors)
117     return false;
118 
119   std::vector<std::pair<uint32_t, Function *>> Ctors =
120       parseGlobalCtors(GlobalCtors);
121   if (Ctors.empty())
122     return false;
123 
124   bool MadeChange = false;
125   // Loop over global ctors, optimizing them when we can.
126   BitVector CtorsToRemove(Ctors.size());
127   std::vector<size_t> CtorsByPriority(Ctors.size());
128   std::iota(CtorsByPriority.begin(), CtorsByPriority.end(), 0);
129   stable_sort(CtorsByPriority, [&](size_t LHS, size_t RHS) {
130     return Ctors[LHS].first < Ctors[RHS].first;
131   });
132   for (unsigned CtorIndex : CtorsByPriority) {
133     const uint32_t Priority = Ctors[CtorIndex].first;
134     Function *F = Ctors[CtorIndex].second;
135     if (!F)
136       continue;
137 
138     LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
139 
140     // If we can evaluate the ctor at compile time, do.
141     if (ShouldRemove(Priority, F)) {
142       Ctors[CtorIndex].second = nullptr;
143       CtorsToRemove.set(CtorIndex);
144       MadeChange = true;
145       continue;
146     }
147   }
148 
149   if (!MadeChange)
150     return false;
151 
152   removeGlobalCtors(GlobalCtors, CtorsToRemove);
153   return true;
154 }
155