Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/CtorUtils.cpp
35271 views
//===- CtorUtils.cpp - Helpers for working with global_ctors ----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines functions that are used to process llvm.global_ctors.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Transforms/Utils/CtorUtils.h"13#include "llvm/ADT/BitVector.h"14#include "llvm/IR/Constants.h"15#include "llvm/IR/Function.h"16#include "llvm/IR/GlobalVariable.h"17#include "llvm/IR/Module.h"18#include "llvm/Support/Debug.h"19#include "llvm/Support/raw_ostream.h"20#include <numeric>2122#define DEBUG_TYPE "ctor_utils"2324using namespace llvm;2526/// Given a specified llvm.global_ctors list, remove the listed elements.27static void removeGlobalCtors(GlobalVariable *GCL, const BitVector &CtorsToRemove) {28// Filter out the initializer elements to remove.29ConstantArray *OldCA = cast<ConstantArray>(GCL->getInitializer());30SmallVector<Constant *, 10> CAList;31for (unsigned I = 0, E = OldCA->getNumOperands(); I < E; ++I)32if (!CtorsToRemove.test(I))33CAList.push_back(OldCA->getOperand(I));3435// Create the new array initializer.36ArrayType *ATy =37ArrayType::get(OldCA->getType()->getElementType(), CAList.size());38Constant *CA = ConstantArray::get(ATy, CAList);3940// If we didn't change the number of elements, don't create a new GV.41if (CA->getType() == OldCA->getType()) {42GCL->setInitializer(CA);43return;44}4546// Create the new global and insert it next to the existing list.47GlobalVariable *NGV =48new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(),49CA, "", GCL->getThreadLocalMode());50GCL->getParent()->insertGlobalVariable(GCL->getIterator(), NGV);51NGV->takeName(GCL);5253// Nuke the old list, replacing any uses with the new one.54if (!GCL->use_empty())55GCL->replaceAllUsesWith(NGV);5657GCL->eraseFromParent();58}5960/// Given a llvm.global_ctors list that we can understand,61/// return a list of the functions and null terminator as a vector.62static std::vector<std::pair<uint32_t, Function *>>63parseGlobalCtors(GlobalVariable *GV) {64ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());65std::vector<std::pair<uint32_t, Function *>> Result;66Result.reserve(CA->getNumOperands());67for (auto &V : CA->operands()) {68ConstantStruct *CS = cast<ConstantStruct>(V);69Result.emplace_back(cast<ConstantInt>(CS->getOperand(0))->getZExtValue(),70dyn_cast<Function>(CS->getOperand(1)));71}72return Result;73}7475/// Find the llvm.global_ctors list.76static GlobalVariable *findGlobalCtors(Module &M) {77GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");78if (!GV)79return nullptr;8081// Verify that the initializer is simple enough for us to handle. We are82// only allowed to optimize the initializer if it is unique.83if (!GV->hasUniqueInitializer())84return nullptr;8586// If there are no ctors, then the initializer might be null/undef/poison.87// Ignore anything but an array.88ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());89if (!CA)90return nullptr;9192for (auto &V : CA->operands()) {93if (isa<ConstantAggregateZero>(V))94continue;95ConstantStruct *CS = cast<ConstantStruct>(V);96if (isa<ConstantPointerNull>(CS->getOperand(1)))97continue;9899// Can only handle global constructors with no arguments.100Function *F = dyn_cast<Function>(CS->getOperand(1));101if (!F || F->arg_size() != 0)102return nullptr;103}104return GV;105}106107/// Call "ShouldRemove" for every entry in M's global_ctor list and remove the108/// entries for which it returns true. Return true if anything changed.109bool llvm::optimizeGlobalCtorsList(110Module &M, function_ref<bool(uint32_t, Function *)> ShouldRemove) {111GlobalVariable *GlobalCtors = findGlobalCtors(M);112if (!GlobalCtors)113return false;114115std::vector<std::pair<uint32_t, Function *>> Ctors =116parseGlobalCtors(GlobalCtors);117if (Ctors.empty())118return false;119120bool MadeChange = false;121// Loop over global ctors, optimizing them when we can.122BitVector CtorsToRemove(Ctors.size());123std::vector<size_t> CtorsByPriority(Ctors.size());124std::iota(CtorsByPriority.begin(), CtorsByPriority.end(), 0);125stable_sort(CtorsByPriority, [&](size_t LHS, size_t RHS) {126return Ctors[LHS].first < Ctors[RHS].first;127});128for (unsigned CtorIndex : CtorsByPriority) {129const uint32_t Priority = Ctors[CtorIndex].first;130Function *F = Ctors[CtorIndex].second;131if (!F)132continue;133134LLVM_DEBUG(dbgs() << "Optimizing Global Constructor: " << *F << "\n");135136// If we can evaluate the ctor at compile time, do.137if (ShouldRemove(Priority, F)) {138Ctors[CtorIndex].second = nullptr;139CtorsToRemove.set(CtorIndex);140MadeChange = true;141continue;142}143}144145if (!MadeChange)146return false;147148removeGlobalCtors(GlobalCtors, CtorsToRemove);149return true;150}151152153