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 
22 #define DEBUG_TYPE "ctor_utils"
23 
24 using namespace llvm;
25 
26 /// Given a specified llvm.global_ctors list, remove the listed elements.
27 static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
28   // Filter out the initializer elements to remove.
29   ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
30   SmallVector<Constant *, 10> CAList;
31   for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
32     if (!CtorsToRemove.test(I))
33       CAList.push_back(OldCA->getOperand(I));
34 
35   // Create the new array initializer.
36   ArrayType *ATy =
37       ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
38   Constant *CA = ConstantArray::get(ATy, CAList);
39 
40   // If we didn't change the number of elements, don't create a new GV.
41   if (CA->getType() == OldCA->getType()) {
42     GCL->setInitializer(CA);
43     return;
44   }
45 
46   // Create the new global and insert it next to the existing list.
47   GlobalVariable *NGV =
48       new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
49                          CA, "", GCL->getThreadLocalMode());
50   GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
51   NGV->takeName(GCL);
52 
53   // Nuke the old list, replacing any uses with the new one.
54   if (!GCL->use_empty()) {
55     Constant *V = NGV;
56     if (V->getType() != GCL->getType())
57       V = ConstantExpr::getBitCast(V, GCL->getType());
58     GCL->replaceAllUsesWith(V);
59   }
60   GCL->eraseFromParent();
61 }
62 
63 /// Given a llvm.global_ctors list that we can understand,
64 /// return a list of the functions and null terminator as a vector.
65 static std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
66   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
67   std::vector<Function *> Result;
68   Result.reserve(CA->getNumOperands());
69   for (auto &V : CA->operands()) {
70     ConstantStruct *CS = cast<ConstantStruct>(V);
71     Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
72   }
73   return Result;
74 }
75 
76 /// Find the llvm.global_ctors list, verifying that all initializers have an
77 /// init priority of 65535.
78 static GlobalVariable *findGlobalCtors(Module &M) {
79   GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
80   if (!GV)
81     return nullptr;
82 
83   // Verify that the initializer is simple enough for us to handle. We are
84   // only allowed to optimize the initializer if it is unique.
85   if (!GV->hasUniqueInitializer())
86     return nullptr;
87 
88   // If there are no ctors, then the initializer might be null/undef/poison.
89   // Ignore anything but an array.
90   ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
91   if (!CA)
92     return nullptr;
93 
94   for (auto &V : CA->operands()) {
95     if (isa<ConstantAggregateZero>(V))
96       continue;
97     ConstantStruct *CS = cast<ConstantStruct>(V);
98     if (isa<ConstantPointerNull>(CS->getOperand(1)))
99       continue;
100 
101     // Can only handle global constructors with no arguments.
102     Function *F = dyn_cast<Function>(CS->getOperand(1));
103     if (!F || F->arg_size() != 0)
104       return nullptr;
105 
106     // Init priority must be standard.
107     ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
108     if (CI->getZExtValue() != 65535)
109       return nullptr;
110   }
111 
112   return GV;
113 }
114 
115 /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
116 /// entries for which it returns true.  Return true if anything changed.
117 bool llvm::optimizeGlobalCtorsList(
118     Module &M, function_ref<bool(Function *)> ShouldRemove) {
119   GlobalVariable *GlobalCtors = findGlobalCtors(M);
120   if (!GlobalCtors)
121     return false;
122 
123   std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
124   if (Ctors.empty())
125     return false;
126 
127   bool MadeChange = false;
128 
129   // Loop over global ctors, optimizing them when we can.
130   unsigned NumCtors = Ctors.size();
131   BitVector CtorsToRemove(NumCtors);
132   for (unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
133     Function *F = Ctors[i];
134     // Found a null terminator in the middle of the list, prune off the rest of
135     // the list.
136     if (!F)
137       continue;
138 
139     LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
140 
141     // We cannot simplify external ctor functions.
142     if (F->empty())
143       continue;
144 
145     // If we can evaluate the ctor at compile time, do.
146     if (ShouldRemove(F)) {
147       Ctors[i] = nullptr;
148       CtorsToRemove.set(i);
149       NumCtors--;
150       MadeChange = true;
151       continue;
152     }
153   }
154 
155   if (!MadeChange)
156     return false;
157 
158   removeGlobalCtors(GlobalCtors, CtorsToRemove);
159   return true;
160 }
161