14b2acde2SNico Weber //===- CtorUtils.cpp - Helpers for working with global_ctors ----*- C++ -*-===//
24b2acde2SNico Weber //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64b2acde2SNico Weber //
74b2acde2SNico Weber //===----------------------------------------------------------------------===//
84b2acde2SNico Weber //
94b2acde2SNico Weber // This file defines functions that are used to process llvm.global_ctors.
104b2acde2SNico Weber //
114b2acde2SNico Weber //===----------------------------------------------------------------------===//
124b2acde2SNico Weber 
134b2acde2SNico Weber #include "llvm/Transforms/Utils/CtorUtils.h"
141f7c328bSBenjamin Kramer #include "llvm/ADT/BitVector.h"
154b2acde2SNico Weber #include "llvm/IR/Constants.h"
164b2acde2SNico Weber #include "llvm/IR/Function.h"
174b2acde2SNico Weber #include "llvm/IR/GlobalVariable.h"
184b2acde2SNico Weber #include "llvm/IR/Module.h"
194b2acde2SNico Weber #include "llvm/Support/Debug.h"
201f7c328bSBenjamin Kramer #include "llvm/Support/raw_ostream.h"
21*badd088cSAlexander Shaposhnikov #include <numeric>
224b2acde2SNico Weber 
234b2acde2SNico Weber #define DEBUG_TYPE "ctor_utils"
244b2acde2SNico Weber 
25994a8451SReid Kleckner using namespace llvm;
264b2acde2SNico Weber 
2778927e88SReid Kleckner /// Given a specified llvm.global_ctors list, remove the listed elements.
removeGlobalCtors(GlobalVariable * GCL,const BitVector & CtorsToRemove)28994a8451SReid Kleckner static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {
2978927e88SReid Kleckner   // Filter out the initializer elements to remove.
3078927e88SReid Kleckner   ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());
3178927e88SReid Kleckner   SmallVector<Constant *, 10> CAList;
3278927e88SReid Kleckner   for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)
3378927e88SReid Kleckner     if (!CtorsToRemove.test(I))
3478927e88SReid Kleckner       CAList.push_back(OldCA->getOperand(I));
354b2acde2SNico Weber 
3678927e88SReid Kleckner   // Create the new array initializer.
3778927e88SReid Kleckner   ArrayType *ATy =
3878927e88SReid Kleckner       ArrayType::get(OldCA->getType()->getElementType(), CAList.size());
3978927e88SReid Kleckner   Constant *CA = ConstantArray::get(ATy, CAList);
404b2acde2SNico Weber 
414b2acde2SNico Weber   // If we didn't change the number of elements, don't create a new GV.
4278927e88SReid Kleckner   if (CA->getType() == OldCA->getType()) {
434b2acde2SNico Weber     GCL->setInitializer(CA);
444b2acde2SNico Weber     return;
454b2acde2SNico Weber   }
464b2acde2SNico Weber 
474b2acde2SNico Weber   // Create the new global and insert it next to the existing list.
484b2acde2SNico Weber   GlobalVariable *NGV =
494b2acde2SNico Weber       new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),
504b2acde2SNico Weber                          CA, "", GCL->getThreadLocalMode());
515b4c837cSDuncan P. N. Exon Smith   GCL->getParent()->getGlobalList().insert(GCL->getIterator(), NGV);
524b2acde2SNico Weber   NGV->takeName(GCL);
534b2acde2SNico Weber 
544b2acde2SNico Weber   // Nuke the old list, replacing any uses with the new one.
554b2acde2SNico Weber   if (!GCL->use_empty()) {
564b2acde2SNico Weber     Constant *V = NGV;
574b2acde2SNico Weber     if (V->getType() != GCL->getType())
584b2acde2SNico Weber       V = ConstantExpr::getBitCast(V, GCL->getType());
594b2acde2SNico Weber     GCL->replaceAllUsesWith(V);
604b2acde2SNico Weber   }
614b2acde2SNico Weber   GCL->eraseFromParent();
624b2acde2SNico Weber }
634b2acde2SNico Weber 
644b2acde2SNico Weber /// Given a llvm.global_ctors list that we can understand,
654b2acde2SNico Weber /// return a list of the functions and null terminator as a vector.
66*badd088cSAlexander Shaposhnikov static std::vector<std::pair<uint32_t, Function *>>
parseGlobalCtors(GlobalVariable * GV)67*badd088cSAlexander Shaposhnikov parseGlobalCtors(GlobalVariable *GV) {
684b2acde2SNico Weber   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
69*badd088cSAlexander Shaposhnikov   std::vector<std::pair<uint32_t, Function *>> Result;
704b2acde2SNico Weber   Result.reserve(CA->getNumOperands());
71738837eeSDavide Italiano   for (auto &V : CA->operands()) {
72738837eeSDavide Italiano     ConstantStruct *CS = cast<ConstantStruct>(V);
73*badd088cSAlexander Shaposhnikov     Result.emplace_back(cast<ConstantInt>(CS->getOperand(0))->getZExtValue(),
74*badd088cSAlexander Shaposhnikov                         dyn_cast<Function>(CS->getOperand(1)));
754b2acde2SNico Weber   }
764b2acde2SNico Weber   return Result;
774b2acde2SNico Weber }
784b2acde2SNico Weber 
79*badd088cSAlexander Shaposhnikov /// Find the llvm.global_ctors list.
findGlobalCtors(Module & M)80994a8451SReid Kleckner static GlobalVariable *findGlobalCtors(Module &M) {
814b2acde2SNico Weber   GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
824b2acde2SNico Weber   if (!GV)
834b2acde2SNico Weber     return nullptr;
844b2acde2SNico Weber 
854b2acde2SNico Weber   // Verify that the initializer is simple enough for us to handle. We are
864b2acde2SNico Weber   // only allowed to optimize the initializer if it is unique.
874b2acde2SNico Weber   if (!GV->hasUniqueInitializer())
884b2acde2SNico Weber     return nullptr;
894b2acde2SNico Weber 
90067c0350SNikita Popov   // If there are no ctors, then the initializer might be null/undef/poison.
91067c0350SNikita Popov   // Ignore anything but an array.
92067c0350SNikita Popov   ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
93067c0350SNikita Popov   if (!CA)
94067c0350SNikita Popov     return nullptr;
954b2acde2SNico Weber 
96738837eeSDavide Italiano   for (auto &V : CA->operands()) {
97738837eeSDavide Italiano     if (isa<ConstantAggregateZero>(V))
984b2acde2SNico Weber       continue;
99738837eeSDavide Italiano     ConstantStruct *CS = cast<ConstantStruct>(V);
1004b2acde2SNico Weber     if (isa<ConstantPointerNull>(CS->getOperand(1)))
1014b2acde2SNico Weber       continue;
1024b2acde2SNico Weber 
1034e652918SArthur Eubanks     // Can only handle global constructors with no arguments.
1044e652918SArthur Eubanks     Function *F = dyn_cast<Function>(CS->getOperand(1));
1054e652918SArthur Eubanks     if (!F || F->arg_size() != 0)
1064b2acde2SNico Weber       return nullptr;
1074b2acde2SNico Weber   }
1084b2acde2SNico Weber   return GV;
1094b2acde2SNico Weber }
1104b2acde2SNico Weber 
1114b2acde2SNico Weber /// Call "ShouldRemove" for every entry in M's global_ctor list and remove the
1124b2acde2SNico Weber /// entries for which it returns true.  Return true if anything changed.
optimizeGlobalCtorsList(Module & M,function_ref<bool (uint32_t,Function *)> ShouldRemove)113994a8451SReid Kleckner bool llvm::optimizeGlobalCtorsList(
114*badd088cSAlexander Shaposhnikov     Module &M, function_ref<bool(uint32_t, Function *)> ShouldRemove) {
1154b2acde2SNico Weber   GlobalVariable *GlobalCtors = findGlobalCtors(M);
1164b2acde2SNico Weber   if (!GlobalCtors)
1174b2acde2SNico Weber     return false;
1184b2acde2SNico Weber 
119*badd088cSAlexander Shaposhnikov   std::vector<std::pair<uint32_t, Function *>> Ctors =
120*badd088cSAlexander Shaposhnikov       parseGlobalCtors(GlobalCtors);
1214b2acde2SNico Weber   if (Ctors.empty())
1224b2acde2SNico Weber     return false;
1234b2acde2SNico Weber 
1244b2acde2SNico Weber   bool MadeChange = false;
1254b2acde2SNico Weber   // Loop over global ctors, optimizing them when we can.
126da823382SAlexander Shaposhnikov   BitVector CtorsToRemove(Ctors.size());
127*badd088cSAlexander Shaposhnikov   std::vector<size_t> CtorsByPriority(Ctors.size());
128*badd088cSAlexander Shaposhnikov   std::iota(CtorsByPriority.begin(), CtorsByPriority.end(), 0);
129*badd088cSAlexander Shaposhnikov   stable_sort(CtorsByPriority, [&](size_t LHS, size_t RHS) {
130*badd088cSAlexander Shaposhnikov     return Ctors[LHS].first < Ctors[RHS].first;
131*badd088cSAlexander Shaposhnikov   });
132*badd088cSAlexander Shaposhnikov   for (unsigned CtorIndex : CtorsByPriority) {
133*badd088cSAlexander Shaposhnikov     const uint32_t Priority = Ctors[CtorIndex].first;
134*badd088cSAlexander Shaposhnikov     Function *F = Ctors[CtorIndex].second;
13578927e88SReid Kleckner     if (!F)
13678927e88SReid Kleckner       continue;
13778927e88SReid Kleckner 
138d34e60caSNicola Zaghen     LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");
1394b2acde2SNico Weber 
1404b2acde2SNico Weber     // If we can evaluate the ctor at compile time, do.
141*badd088cSAlexander Shaposhnikov     if (ShouldRemove(Priority, F)) {
142*badd088cSAlexander Shaposhnikov       Ctors[CtorIndex].second = nullptr;
143*badd088cSAlexander Shaposhnikov       CtorsToRemove.set(CtorIndex);
1444b2acde2SNico Weber       MadeChange = true;
1454b2acde2SNico Weber       continue;
1464b2acde2SNico Weber     }
1474b2acde2SNico Weber   }
1484b2acde2SNico Weber 
1494b2acde2SNico Weber   if (!MadeChange)
1504b2acde2SNico Weber     return false;
1514b2acde2SNico Weber 
15278927e88SReid Kleckner   removeGlobalCtors(GlobalCtors, CtorsToRemove);
1534b2acde2SNico Weber   return true;
1544b2acde2SNico Weber }
155