blob: 82b67c293102773d66776a417237616ac9e0f928 [file] [log] [blame]
Nico Weber4b2acde2014-05-02 18:35:25 +00001//===- CtorUtils.cpp - Helpers for working with global_ctors ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines functions that are used to process llvm.global_ctors.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Utils/CtorUtils.h"
Benjamin Kramer1f7c3282015-03-23 19:06:17 +000015#include "llvm/ADT/BitVector.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/GlobalVariable.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000019#include "llvm/IR/Module.h"
20#include "llvm/Support/Debug.h"
Benjamin Kramer1f7c3282015-03-23 19:06:17 +000021#include "llvm/Support/raw_ostream.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000022
23#define DEBUG_TYPE "ctor_utils"
24
25namespace llvm {
26
27namespace {
Reid Kleckner78927e82014-09-23 22:33:01 +000028/// Given a specified llvm.global_ctors list, remove the listed elements.
29void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
30 // Filter out the initializer elements to remove.
31 ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
32 SmallVector<Constant *, 10> CAList;
33 for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
34 if (!CtorsToRemove.test(I))
35 CAList.push_back(OldCA->getOperand(I));
Nico Weber4b2acde2014-05-02 18:35:25 +000036
Reid Kleckner78927e82014-09-23 22:33:01 +000037 // Create the new array initializer.
38 ArrayType *ATy =
39 ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
40 Constant *CA = ConstantArray::get(ATy, CAList);
Nico Weber4b2acde2014-05-02 18:35:25 +000041
42 // If we didn't change the number of elements, don't create a new GV.
Reid Kleckner78927e82014-09-23 22:33:01 +000043 if (CA->getType() == OldCA->getType()) {
Nico Weber4b2acde2014-05-02 18:35:25 +000044 GCL->setInitializer(CA);
45 return;
46 }
47
48 // Create the new global and insert it next to the existing list.
49 GlobalVariable *NGV =
50 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
51 CA, "", GCL->getThreadLocalMode());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000052 GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
Nico Weber4b2acde2014-05-02 18:35:25 +000053 NGV->takeName(GCL);
54
55 // Nuke the old list, replacing any uses with the new one.
56 if (!GCL->use_empty()) {
57 Constant *V = NGV;
58 if (V->getType() != GCL->getType())
59 V = ConstantExpr::getBitCast(V, GCL->getType());
60 GCL->replaceAllUsesWith(V);
61 }
62 GCL->eraseFromParent();
63}
64
65/// Given a llvm.global_ctors list that we can understand,
66/// return a list of the functions and null terminator as a vector.
Reid Kleckner78927e82014-09-23 22:33:01 +000067std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
Nico Weber4b2acde2014-05-02 18:35:25 +000068 if (GV->getInitializer()->isNullValue())
69 return std::vector<Function *>();
70 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
71 std::vector<Function *> Result;
72 Result.reserve(CA->getNumOperands());
Davide Italiano738837e2016-10-22 01:21:24 +000073 for (auto &V : CA->operands()) {
74 ConstantStruct *CS = cast<ConstantStruct>(V);
Nico Weber4b2acde2014-05-02 18:35:25 +000075 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
76 }
77 return Result;
78}
79
80/// Find the llvm.global_ctors list, verifying that all initializers have an
81/// init priority of 65535.
82GlobalVariable *findGlobalCtors(Module &M) {
83 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
84 if (!GV)
85 return nullptr;
86
87 // Verify that the initializer is simple enough for us to handle. We are
88 // only allowed to optimize the initializer if it is unique.
89 if (!GV->hasUniqueInitializer())
90 return nullptr;
91
92 if (isa<ConstantAggregateZero>(GV->getInitializer()))
93 return GV;
94 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
95
Davide Italiano738837e2016-10-22 01:21:24 +000096 for (auto &V : CA->operands()) {
97 if (isa<ConstantAggregateZero>(V))
Nico Weber4b2acde2014-05-02 18:35:25 +000098 continue;
Davide Italiano738837e2016-10-22 01:21:24 +000099 ConstantStruct *CS = cast<ConstantStruct>(V);
Nico Weber4b2acde2014-05-02 18:35:25 +0000100 if (isa<ConstantPointerNull>(CS->getOperand(1)))
101 continue;
102
103 // Must have a function or null ptr.
104 if (!isa<Function>(CS->getOperand(1)))
105 return nullptr;
106
107 // Init priority must be standard.
108 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
109 if (CI->getZExtValue() != 65535)
110 return nullptr;
111 }
112
113 return GV;
114}
115} // namespace
116
117/// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
118/// entries for which it returns true. Return true if anything changed.
Richard Smithc167d652014-05-06 01:44:26 +0000119bool optimizeGlobalCtorsList(Module &M,
120 function_ref<bool(Function *)> ShouldRemove) {
Nico Weber4b2acde2014-05-02 18:35:25 +0000121 GlobalVariable *GlobalCtors = findGlobalCtors(M);
122 if (!GlobalCtors)
123 return false;
124
125 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
126 if (Ctors.empty())
127 return false;
128
129 bool MadeChange = false;
130
131 // Loop over global ctors, optimizing them when we can.
Reid Kleckner78927e82014-09-23 22:33:01 +0000132 unsigned NumCtors = Ctors.size();
133 BitVector CtorsToRemove(NumCtors);
134 for (unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
Nico Weber4b2acde2014-05-02 18:35:25 +0000135 Function *F = Ctors[i];
136 // Found a null terminator in the middle of the list, prune off the rest of
137 // the list.
Reid Kleckner78927e82014-09-23 22:33:01 +0000138 if (!F)
139 continue;
140
Nico Weber4b2acde2014-05-02 18:35:25 +0000141 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
142
143 // We cannot simplify external ctor functions.
144 if (F->empty())
145 continue;
146
147 // If we can evaluate the ctor at compile time, do.
Richard Smithc167d652014-05-06 01:44:26 +0000148 if (ShouldRemove(F)) {
Reid Kleckner78927e82014-09-23 22:33:01 +0000149 Ctors[i] = nullptr;
150 CtorsToRemove.set(i);
151 NumCtors--;
Nico Weber4b2acde2014-05-02 18:35:25 +0000152 MadeChange = true;
Nico Weber4b2acde2014-05-02 18:35:25 +0000153 continue;
154 }
155 }
156
157 if (!MadeChange)
158 return false;
159
Reid Kleckner78927e82014-09-23 22:33:01 +0000160 removeGlobalCtors(GlobalCtors, CtorsToRemove);
Nico Weber4b2acde2014-05-02 18:35:25 +0000161 return true;
162}
163
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000164} // End llvm namespace