Remove the default clause from a fully-covering switch
[llvm-core.git] / lib / Transforms / Utils / CtorUtils.cpp
blob6642a97a29c2b30ce332ae26cbc16d63884ddbb9
1 //===- 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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/CtorUtils.h"
15 #include "llvm/ADT/BitVector.h"
16 #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"
22 #include "llvm/Support/raw_ostream.h"
24 #define DEBUG_TYPE "ctor_utils"
26 namespace llvm {
28 namespace {
29 /// Given a specified llvm.global_ctors list, remove the listed elements.
30 void 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));
38 // Create the new array initializer.
39 ArrayType *ATy =
40 ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
41 Constant *CA = ConstantArray::get(ATy, CAList);
43 // If we didn't change the number of elements, don't create a new GV.
44 if (CA->getType() == OldCA->getType()) {
45 GCL->setInitializer(CA);
46 return;
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());
53 GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
54 NGV->takeName(GCL);
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);
63 GCL->eraseFromParent();
66 /// Given a llvm.global_ctors list that we can understand,
67 /// return a list of the functions and null terminator as a vector.
68 std::vector<Function *> parseGlobalCtors(GlobalVariable *GV) {
69 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());
74 for (auto &V : CA->operands()) {
75 ConstantStruct *CS = cast<ConstantStruct>(V);
76 Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
78 return Result;
81 /// Find the llvm.global_ctors list, verifying that all initializers have an
82 /// init priority of 65535.
83 GlobalVariable *findGlobalCtors(Module &M) {
84 GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
85 if (!GV)
86 return nullptr;
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;
93 if (isa<ConstantAggregateZero>(GV->getInitializer()))
94 return GV;
95 ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
97 for (auto &V : CA->operands()) {
98 if (isa<ConstantAggregateZero>(V))
99 continue;
100 ConstantStruct *CS = cast<ConstantStruct>(V);
101 if (isa<ConstantPointerNull>(CS->getOperand(1)))
102 continue;
104 // Must have a function or null ptr.
105 if (!isa<Function>(CS->getOperand(1)))
106 return nullptr;
108 // Init priority must be standard.
109 ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
110 if (CI->getZExtValue() != 65535)
111 return nullptr;
114 return GV;
116 } // namespace
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.
120 bool optimizeGlobalCtorsList(Module &M,
121 function_ref<bool(Function *)> ShouldRemove) {
122 GlobalVariable *GlobalCtors = findGlobalCtors(M);
123 if (!GlobalCtors)
124 return false;
126 std::vector<Function *> Ctors = parseGlobalCtors(GlobalCtors);
127 if (Ctors.empty())
128 return false;
130 bool MadeChange = false;
132 // Loop over global ctors, optimizing them when we can.
133 unsigned NumCtors = Ctors.size();
134 BitVector CtorsToRemove(NumCtors);
135 for (unsigned i = 0; i != Ctors.size() && NumCtors > 0; ++i) {
136 Function *F = Ctors[i];
137 // Found a null terminator in the middle of the list, prune off the rest of
138 // the list.
139 if (!F)
140 continue;
142 DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
144 // We cannot simplify external ctor functions.
145 if (F->empty())
146 continue;
148 // If we can evaluate the ctor at compile time, do.
149 if (ShouldRemove(F)) {
150 Ctors[i] = nullptr;
151 CtorsToRemove.set(i);
152 NumCtors--;
153 MadeChange = true;
154 continue;
158 if (!MadeChange)
159 return false;
161 removeGlobalCtors(GlobalCtors, CtorsToRemove);
162 return true;
165 } // End llvm namespace