llvm.org GIT mirror llvm / 43122e2
Utility functions for appending to llvm.used/llvm.compiler.used. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285143 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 2 years ago
7 changed file(s) with 80 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
4343 }
4444 };
4545 DenseMap ProfileDataMap;
46 std::vector<Value *> UsedVars;
46 std::vector<GlobalValue *> UsedVars;
4747 std::vector ReferencedNames;
4848 GlobalVariable *NamesVar;
4949 size_t NamesSize;
5858 /// the list of public globals in the module.
5959 bool nameUnamedGlobals(Module &M);
6060
61 /// \brief Adds global values to the llvm.used list.
62 void appendToUsed(Module &M, ArrayRef Values);
63
64 /// \brief Adds global values to the llvm.compiler.used list.
65 void appendToCompilerUsed(Module &M, ArrayRef Values);
66
6167 } // End llvm namespace
6268
6369 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
5757 #include "llvm/Transforms/IPO/Internalize.h"
5858 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
5959 #include "llvm/Transforms/ObjCARC.h"
60 #include "llvm/Transforms/Utils/ModuleUtils.h"
6061 #include
6162 using namespace llvm;
6263
363364 void LTOCodeGenerator::preserveDiscardableGVs(
364365 Module &TheModule,
365366 llvm::function_ref mustPreserveGV) {
366 SetVector UsedValuesSet;
367 if (GlobalVariable *LLVMUsed =
368 TheModule.getGlobalVariable("llvm.compiler.used")) {
369 ConstantArray *Inits = cast(LLVMUsed->getInitializer());
370 for (auto &V : Inits->operands())
371 UsedValuesSet.insert(cast(&V));
372 LLVMUsed->eraseFromParent();
373 }
374 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext());
367 std::vector Used;
375368 auto mayPreserveGlobal = [&](GlobalValue &GV) {
376369 if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
377 !mustPreserveGV(GV))
370 !mustPreserveGV(GV))
378371 return;
379372 if (GV.hasAvailableExternallyLinkage())
380373 return emitWarning(
383376 if (GV.hasInternalLinkage())
384377 return emitWarning((Twine("Linker asked to preserve internal global: '") +
385378 GV.getName() + "'").str());
386 UsedValuesSet.insert(ConstantExpr::getBitCast(&GV, i8PTy));
379 Used.push_back(&GV);
387380 };
388381 for (auto &GV : TheModule)
389382 mayPreserveGlobal(GV);
392385 for (auto &GV : TheModule.aliases())
393386 mayPreserveGlobal(GV);
394387
395 if (UsedValuesSet.empty())
396 return;
397
398 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesSet.size());
399 auto *LLVMUsed = new llvm::GlobalVariable(
400 TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage,
401 llvm::ConstantArray::get(ATy, UsedValuesSet.getArrayRef()),
402 "llvm.compiler.used");
403 LLVMUsed->setSection("llvm.metadata");
388 if (Used.empty())
389 return;
390
391 appendToCompilerUsed(TheModule, Used);
404392 }
405393
406394 void LTOCodeGenerator::applyScopeRestrictions() {
1717 #include "llvm/Target/TargetLowering.h"
1818 #include "llvm/Target/TargetSubtargetInfo.h"
1919 #include "llvm/Transforms/IPO/Internalize.h"
20 #include "llvm/Transforms/Utils/ModuleUtils.h"
2021
2122 using namespace llvm;
2223
2728 public:
2829 PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
2930 const TargetMachine &TM,
30 SmallPtrSetImplGlobalValue *> &LLVMUsed)
31 std::vector<GlobalValue *> &LLVMUsed)
3132 : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
3233
33 void findInModule(const Module &TheModule) {
34 void findInModule(Module &TheModule) {
3435 initializeLibCalls(TheModule);
35 for (const Function &F : TheModule)
36 for (Function &F : TheModule)
3637 findLibCallsAndAsm(F);
37 for (const GlobalVariable &GV : TheModule.globals())
38 for (GlobalVariable &GV : TheModule.globals())
3839 findLibCallsAndAsm(GV);
39 for (const GlobalAlias &GA : TheModule.aliases())
40 for (GlobalAlias &GA : TheModule.aliases())
4041 findLibCallsAndAsm(GA);
4142 }
4243
5051 StringSet<> Libcalls;
5152
5253 // Output
53 SmallPtrSetImplGlobalValue *> &LLVMUsed;
54 std::vector<GlobalValue *> &LLVMUsed;
5455
5556 // Collect names of runtime library functions. User-defined functions with the
5657 // same names are added to llvm.compiler.used to prevent them from being
8586 }
8687 }
8788
88 void findLibCallsAndAsm(const GlobalValue &GV) {
89 void findLibCallsAndAsm(GlobalValue &GV) {
8990 // There are no restrictions to apply to declarations.
9091 if (GV.isDeclaration())
9192 return;
99100 // optimizations like -globalopt, causing problems when later optimizations
100101 // add new library calls (e.g., llvm.memset => memset and printf => puts).
101102 // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
102 if (isa(GV) && Libcalls.count(GV.getName()))
103 LLVMUsed.insert(&GV);
103 if (isa(GV) && Libcalls.count(GV.getName())) {
104 LLVMUsed.push_back(&GV);
105 return;
106 }
104107
105108 SmallString<64> Buffer;
106109 TM.getNameWithPrefix(Buffer, &GV, Mangler);
107110 if (AsmUndefinedRefs.count(Buffer))
108 LLVMUsed.insert(&GV);
111 LLVMUsed.push_back(&GV);
109112 }
110113 };
111114
113116
114117 void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
115118 const StringSet<> &AsmUndefinedRefs) {
116 SmallPtrSet> UsedValues;
119 std::vector> UsedValues;
117120 PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
118121 .findInModule(TheModule);
119122
120123 if (UsedValues.empty())
121124 return;
122125
123 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext());
124 std::vector UsedValuesList;
125 for (const auto *GV : UsedValues) {
126 Constant *c =
127 ConstantExpr::getBitCast(const_cast(GV), i8PTy);
128 UsedValuesList.push_back(c);
129 }
130
131 GlobalVariable *LLVMUsed = TheModule.getGlobalVariable("llvm.compiler.used");
132 if (LLVMUsed) {
133 ConstantArray *Inits = cast(LLVMUsed->getInitializer());
134 for (auto &V : Inits->operands())
135 UsedValuesList.push_back(cast(&V));
136 LLVMUsed->eraseFromParent();
137 }
138
139 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesList.size());
140 LLVMUsed = new llvm::GlobalVariable(
141 TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage,
142 llvm::ConstantArray::get(ATy, UsedValuesList), "llvm.compiler.used");
143
144 LLVMUsed->setSection("llvm.metadata");
126 appendToCompilerUsed(TheModule, UsedValues);
145127 }
16231623 StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy, nullptr);
16241624
16251625 // Keep the list of "Liveness" GV created to be added to llvm.compiler.used
1626 SmallVector<Constant *, 16> LivenessGlobals;
1626 SmallVector<GlobalValue *, 16> LivenessGlobals;
16271627 LivenessGlobals.reserve(n);
16281628
16291629 for (size_t i = 0; i < n; i++) {
16461646 M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder,
16471647 Twine("__asan_binder_") + GVName);
16481648 Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
1649 LivenessGlobals.push_back(
1650 ConstantExpr::getBitCast(Liveness, IRB.getInt8PtrTy()));
1651 }
1652
1653 if (!LivenessGlobals.empty()) {
1654 // Update llvm.compiler.used, adding the new liveness globals. This is
1655 // needed so that during LTO these variables stay alive. The alternative
1656 // would be to have the linker handling the LTO symbols, but libLTO
1657 // current
1658 // API does not expose access to the section for each symbol.
1659 if (GlobalVariable *LLVMUsed =
1660 M.getGlobalVariable("llvm.compiler.used")) {
1661 ConstantArray *Inits = cast(LLVMUsed->getInitializer());
1662 for (auto &V : Inits->operands())
1663 LivenessGlobals.push_back(cast(&V));
1664 LLVMUsed->eraseFromParent();
1665 }
1666 llvm::ArrayType *ATy =
1667 llvm::ArrayType::get(IRB.getInt8PtrTy(), LivenessGlobals.size());
1668 auto *LLVMUsed = new llvm::GlobalVariable(
1669 M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1670 llvm::ConstantArray::get(ATy, LivenessGlobals), "llvm.compiler.used");
1671 LLVMUsed->setSection("llvm.metadata");
1672 }
1649 LivenessGlobals.push_back(Liveness);
1650 }
1651
1652 // Update llvm.compiler.used, adding the new liveness globals. This is
1653 // needed so that during LTO these variables stay alive. The alternative
1654 // would be to have the linker handling the LTO symbols, but libLTO
1655 // current API does not expose access to the section for each symbol.
1656 if (!LivenessGlobals.empty())
1657 appendToCompilerUsed(M, LivenessGlobals);
16731658 } else {
16741659 // On all other platfoms, we just emit an array of global metadata
16751660 // structures.
552552 }
553553
554554 void InstrProfiling::emitUses() {
555 if (UsedVars.empty())
556 return;
557
558 GlobalVariable *LLVMUsed = M->getGlobalVariable("llvm.used");
559 std::vector MergedVars;
560 if (LLVMUsed) {
561 // Collect the existing members of llvm.used.
562 ConstantArray *Inits = cast(LLVMUsed->getInitializer());
563 for (unsigned I = 0, E = Inits->getNumOperands(); I != E; ++I)
564 MergedVars.push_back(Inits->getOperand(I));
565 LLVMUsed->eraseFromParent();
566 }
567
568 Type *i8PTy = Type::getInt8PtrTy(M->getContext());
569 // Add uses for our data.
570 for (auto *Value : UsedVars)
571 MergedVars.push_back(
572 ConstantExpr::getBitCast(cast(Value), i8PTy));
573
574 // Recreate llvm.used.
575 ArrayType *ATy = ArrayType::get(i8PTy, MergedVars.size());
576 LLVMUsed =
577 new GlobalVariable(*M, ATy, false, GlobalValue::AppendingLinkage,
578 ConstantArray::get(ATy, MergedVars), "llvm.used");
579 LLVMUsed->setSection("llvm.metadata");
555 if (!UsedVars.empty())
556 appendToUsed(*M, UsedVars);
580557 }
581558
582559 void InstrProfiling::emitInitialization() {
8888 appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
8989 }
9090
91 static void appendToUsedList(Module &M, StringRef Name, ArrayRef Values) {
92 GlobalVariable *GV = M.getGlobalVariable(Name);
93 SmallPtrSet InitAsSet;
94 SmallVector Init;
95 if (GV) {
96 ConstantArray *CA = dyn_cast(GV->getInitializer());
97 for (auto &Op : CA->operands()) {
98 Constant *C = cast_or_null(Op);
99 if (InitAsSet.insert(C).second)
100 Init.push_back(C);
101 }
102 GV->eraseFromParent();
103 }
104
105 Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
106 for (auto *V : Values) {
107 Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
108 if (InitAsSet.insert(C).second)
109 Init.push_back(C);
110 }
111
112 if (Init.empty())
113 return;
114
115 ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
116 GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
117 ConstantArray::get(ATy, Init), Name);
118 GV->setSection("llvm.metadata");
119 }
120
121 void llvm::appendToUsed(Module &M, ArrayRef Values) {
122 appendToUsedList(M, "llvm.used", Values);
123 }
124
125 void llvm::appendToCompilerUsed(Module &M, ArrayRef Values) {
126 appendToUsedList(M, "llvm.compiler.used", Values);
127 }
128
91129 Function *llvm::checkSanitizerInterfaceFunction(Constant *FuncOrBitcast) {
92130 if (isa(FuncOrBitcast))
93131 return cast(FuncOrBitcast);