blob: 6642a97a29c2b30ce332ae26cbc16d63884ddbb9 [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"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Support/Debug.h"
Benjamin Kramer1f7c3282015-03-23 19:06:17 +000022#include "llvm/Support/raw_ostream.h"
Nico Weber4b2acde2014-05-02 18:35:25 +000023
24#define DEBUG_TYPE "ctor_utils"
25
26namespace llvm {
27
28namespace {
Reid Kleckner78927e82014-09-23 22:33:01 +000029/// Given a specified llvm.global_ctors list, remove the listed elements.
30void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
31 // Filter out the initializer elements to remove.
32 ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
33 SmallVector<Constant *, 10> CAList;
34 for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
35 if (!CtorsToRemove.test(I))
36 CAList.push_back(OldCA->getOperand(I));
Nico Weber4b2acde2014-05-02 18:35:25 +000037
Reid Kleckner78927e82014-09-23 22:33:01 +000038 // Create the new array initializer.
39 ArrayType *ATy =
40 ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
41 Constant *CA = ConstantArray::get(ATy, CAList);
Nico Weber4b2acde2014-05-02 18:35:25 +000042
43 // If we didn't change the number of elements, don't create a new GV.
Reid Kleckner78927e82014-09-23 22:33:01 +000044 if (CA->getType() == OldCA->getType()) {
Nico Weber4b2acde2014-05-02 18:35:25 +000045 GCL->setInitializer(CA);
46 return;
47 }
48
49 // Create the new global and insert it next to the existing list.
50 GlobalVariable *NGV =
51 new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
52 CA, "", GCL->getThreadLocalMode());
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +000053 GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
Nico Weber4b2acde2014-05-02 18:35:25 +000054 NGV->takeName(GCL);
55
56 // Nuke the old list, replacing any uses with the new one.
57 if (!GCL->use_empty()) {
58 Constant *V = NGV;
59 if (V->getType() != GCL->getType())
60 V = ConstantExpr::getBitCast(V, GCL->getType());
61 GCL->replaceAllUsesWith(V);
62 }
63 GCL->eraseFromParent();
64}
65
66/// Given a llvm.global_ctors list that we can understand,
67/// return a list of the functions and null terminator as a vector.
Reid Kleckner78927e82014-09-23 22:33:01 +000068std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
Nico Weber4b2acde2014-05-02 18:35:25 +000069 if (GV->getInitializer()->isNullValue())
70 return std::vector<Function *>();
71 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
72 std::vector<Function *> Result;
73 Result.reserve(CA->getNumOperands());
Davide Italiano738837e2016-10-22 01:21:24 +000074 for (auto &V : CA->operands()) {
75 ConstantStruct *CS = cast<ConstantStruct>(V);
Nico Weber4b2acde2014-05-02 18:35:25 +000076 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
77 }
78 return Result;
79}
80
81/// Find the llvm.global_ctors list, verifying that all initializers have an
82/// init priority of 65535.
83GlobalVariable *findGlobalCtors(Module &M) {
84 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
85 if (!GV)
86 return nullptr;
87
88 // Verify that the initializer is simple enough for us to handle. We are
89 // only allowed to optimize the initializer if it is unique.
90 if (!GV->hasUniqueInitializer())
91 return nullptr;
92
93 if (isa<ConstantAggregateZero>(GV->getInitializer()))
94 return GV;
95 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
96
Davide Italiano738837e2016-10-22 01:21:24 +000097 for (auto &V : CA->operands()) {
98 if (isa<ConstantAggregateZero>(V))
Nico Weber4b2acde2014-05-02 18:35:25 +000099 continue;
Davide Italiano738837e2016-10-22 01:21:24 +0000100 ConstantStruct *CS = cast<ConstantStruct>(V);
Nico Weber4b2acde2014-05-02 18:35:25 +0000101 if (isa<ConstantPointerNull>(CS->getOperand(1)))
102 continue;
103
104 // Must have a function or null ptr.
105 if (!isa<Function>(CS->getOperand(1)))
106 return nullptr;
107
108 // Init priority must be standard.
109 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
110 if (CI->getZExtValue() != 65535)
111 return nullptr;
112 }
113
114 return GV;
115}
116} // namespace
117
118/// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
119/// entries for which it returns true. Return true if anything changed.
Richard Smithc167d652014-05-06 01:44:26 +0000120bool optimizeGlobalCtorsList(Module &M,
121 function_ref<bool(Function *)> ShouldRemove) {
Nico Weber4b2acde2014-05-02 18:35:25 +0000122 GlobalVariable *GlobalCtors = findGlobalCtors(M);
123 if (!GlobalCtors)
124 return false;
125
126 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
127 if (Ctors.empty())
128 return false;
129
130 bool MadeChange = false;
131
132 // Loop over global ctors, optimizing them when we can.
Reid Kleckner78927e82014-09-23 22:33:01 +0000133 unsigned NumCtors = Ctors.size();
134 BitVector CtorsToRemove(NumCtors);
135 for (unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
Nico Weber4b2acde2014-05-02 18:35:25 +0000136 Function *F = Ctors[i];
137 // Found a null terminator in the middle of the list, prune off the rest of
138 // the list.
Reid Kleckner78927e82014-09-23 22:33:01 +0000139 if (!F)
140 continue;
141
Nico Weber4b2acde2014-05-02 18:35:25 +0000142 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
143
144 // We cannot simplify external ctor functions.
145 if (F->empty())
146 continue;
147
148 // If we can evaluate the ctor at compile time, do.
Richard Smithc167d652014-05-06 01:44:26 +0000149 if (ShouldRemove(F)) {
Reid Kleckner78927e82014-09-23 22:33:01 +0000150 Ctors[i] = nullptr;
151 CtorsToRemove.set(i);
152 NumCtors--;
Nico Weber4b2acde2014-05-02 18:35:25 +0000153 MadeChange = true;
Nico Weber4b2acde2014-05-02 18:35:25 +0000154 continue;
155 }
156 }
157
158 if (!MadeChange)
159 return false;
160
Reid Kleckner78927e82014-09-23 22:33:01 +0000161 removeGlobalCtors(GlobalCtors, CtorsToRemove);
Nico Weber4b2acde2014-05-02 18:35:25 +0000162 return true;
163}
164
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000165} // End llvm namespace