Index: llvm/include/llvm/Transforms/Utils/ModuleUtils.h =================================================================== --- llvm/include/llvm/Transforms/Utils/ModuleUtils.h +++ llvm/include/llvm/Transforms/Utils/ModuleUtils.h @@ -13,6 +13,7 @@ #ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H #define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H +#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include // for std::pair @@ -24,6 +25,7 @@ class Function; class FunctionCallee; class GlobalValue; +class GlobalVariable; class Constant; class Value; class Type; @@ -78,6 +80,10 @@ /// Adds global values to the llvm.compiler.used list. void appendToCompilerUsed(Module &M, ArrayRef Values); +/// Replaces llvm.used or llvm.compiler.used list with a new set of values. +GlobalVariable *setUsedInitializer(GlobalVariable &V, + const SmallPtrSetImpl &Init); + /// Filter out potentially dead comdat functions where other entries keep the /// entire comdat group alive. /// Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -66,6 +66,7 @@ #include "llvm/Transforms/Utils/Evaluator.h" #include "llvm/Transforms/Utils/GlobalStatus.h" #include "llvm/Transforms/Utils/Local.h" +#include "llvm/Transforms/Utils/ModuleUtils.h" #include #include #include @@ -2311,42 +2312,6 @@ return EvalSuccess; } -static int compareNames(Constant *const *A, Constant *const *B) { - Value *AStripped = (*A)->stripPointerCasts(); - Value *BStripped = (*B)->stripPointerCasts(); - return AStripped->getName().compare(BStripped->getName()); -} - -static void setUsedInitializer(GlobalVariable &V, - const SmallPtrSetImpl &Init) { - if (Init.empty()) { - V.eraseFromParent(); - return; - } - - // Type of pointer to the array of pointers. - PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0); - - SmallVector UsedArray; - for (GlobalValue *GV : Init) { - Constant *Cast - = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy); - UsedArray.push_back(Cast); - } - // Sort to get deterministic order. - array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames); - ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size()); - - Module *M = V.getParent(); - V.removeFromParent(); - GlobalVariable *NV = - new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage, - ConstantArray::get(ATy, UsedArray), ""); - NV->takeName(&V); - NV->setSection("llvm.metadata"); - delete &V; -} - namespace { /// An easy to access representation of llvm.used and llvm.compiler.used. Index: llvm/lib/Transforms/Utils/ModuleUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/ModuleUtils.cpp +++ llvm/lib/Transforms/Utils/ModuleUtils.cpp @@ -111,6 +111,44 @@ appendToUsedList(M, "llvm.compiler.used", Values); } +static int compareNames(Constant *const *A, Constant *const *B) { + Value *AStripped = (*A)->stripPointerCasts(); + Value *BStripped = (*B)->stripPointerCasts(); + return AStripped->getName().compare(BStripped->getName()); +} + +GlobalVariable * +llvm::setUsedInitializer(GlobalVariable &V, + const SmallPtrSetImpl &Init) { + if (Init.empty()) { + V.eraseFromParent(); + return nullptr; + } + + // Type of pointer to the array of pointers. + PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0); + + SmallVector UsedArray; + for (GlobalValue *GV : Init) { + Constant *Cast = + ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy); + UsedArray.push_back(Cast); + } + // Sort to get deterministic order. + array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames); + ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size()); + + Module *M = V.getParent(); + V.removeFromParent(); + GlobalVariable *NV = + new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage, + ConstantArray::get(ATy, UsedArray), ""); + NV->takeName(&V); + NV->setSection("llvm.metadata"); + delete &V; + return NV; +} + FunctionCallee llvm::declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef InitArgTypes) {