7575#include " llvm/Support/SHA1.h"
7676#include " llvm/Support/raw_ostream.h"
7777#include " llvm/TargetParser/Triple.h"
78+ #include " llvm/Transforms/Utils/ModuleUtils.h"
7879#include < algorithm>
7980#include < cassert>
8081#include < cstddef>
@@ -5850,25 +5851,25 @@ static const char *getSectionNameForCommandline(const Triple &T) {
58505851void llvm::embedBitcodeInModule (llvm::Module &M, llvm::MemoryBufferRef Buf,
58515852 bool EmbedBitcode, bool EmbedCmdline,
58525853 const std::vector<uint8_t > &CmdArgs) {
5853- // Save llvm.compiler.used and remove it.
5854- SmallVector<Constant *, 2 > UsedArray;
5855- SmallVector<GlobalValue *, 4 > UsedGlobals;
5856- GlobalVariable *Used = collectUsedGlobalVariables (M, UsedGlobals, true );
5857- Type *UsedElementType = Used ? Used->getValueType ()->getArrayElementType ()
5858- : PointerType::getUnqual (M.getContext ());
5859- for (auto *GV : UsedGlobals) {
5860- if (GV->getName () != " llvm.embedded.module" &&
5861- GV->getName () != " llvm.cmdline" )
5862- UsedArray.push_back (
5863- ConstantExpr::getPointerBitCastOrAddrSpaceCast (GV, UsedElementType));
5864- }
5865- if (Used)
5866- Used->eraseFromParent ();
58675854
58685855 // Embed the bitcode for the llvm module.
58695856 std::string Data;
58705857 ArrayRef<uint8_t > ModuleData;
58715858 Triple T (M.getTargetTriple ());
5859+ SmallVector<GlobalValue *, 2 > NewGlobals;
5860+
5861+ auto IsCmdOrBitcode = [&](Constant *C) {
5862+ GlobalVariable *GV = dyn_cast<GlobalVariable>(C);
5863+ StringRef Name = GV ? GV->getName () : " " ;
5864+ if (EmbedBitcode && Name == " llvm.embedded.module" )
5865+ return true ;
5866+ if (EmbedCmdline && Name == " llvm.cmdline" )
5867+ return true ;
5868+ return false ;
5869+ };
5870+
5871+ if (EmbedBitcode || EmbedCmdline)
5872+ removeFromUsedLists (M, IsCmdOrBitcode);
58725873
58735874 if (EmbedBitcode) {
58745875 if (Buf.getBufferSize () == 0 ||
@@ -5887,23 +5888,22 @@ void llvm::embedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
58875888 }
58885889 llvm::Constant *ModuleConstant =
58895890 llvm::ConstantDataArray::get (M.getContext (), ModuleData);
5890- llvm::GlobalVariable *GV = new llvm::GlobalVariable (
5891+ llvm::GlobalVariable *EmbeddedModule = new llvm::GlobalVariable (
58915892 M, ModuleConstant->getType (), true , llvm::GlobalValue::PrivateLinkage,
58925893 ModuleConstant);
5893- GV ->setSection (getSectionNameForBitcode (T));
5894+ EmbeddedModule ->setSection (getSectionNameForBitcode (T));
58945895 // Set alignment to 1 to prevent padding between two contributions from input
58955896 // sections after linking.
5896- GV->setAlignment (Align (1 ));
5897- UsedArray.push_back (
5898- ConstantExpr::getPointerBitCastOrAddrSpaceCast (GV, UsedElementType));
5897+ EmbeddedModule->setAlignment (Align (1 ));
5898+ NewGlobals.push_back (EmbeddedModule);
58995899 if (llvm::GlobalVariable *Old =
59005900 M.getGlobalVariable (" llvm.embedded.module" , true )) {
59015901 assert (Old->hasZeroLiveUses () &&
59025902 " llvm.embedded.module can only be used once in llvm.compiler.used" );
5903- GV ->takeName (Old);
5903+ EmbeddedModule ->takeName (Old);
59045904 Old->eraseFromParent ();
59055905 } else {
5906- GV ->setName (" llvm.embedded.module" );
5906+ EmbeddedModule ->setName (" llvm.embedded.module" );
59075907 }
59085908
59095909 // Skip if only bitcode needs to be embedded.
@@ -5913,30 +5913,20 @@ void llvm::embedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
59135913 CmdArgs.size ());
59145914 llvm::Constant *CmdConstant =
59155915 llvm::ConstantDataArray::get (M.getContext (), CmdData);
5916- GV = new llvm::GlobalVariable (M, CmdConstant->getType (), true ,
5917- llvm::GlobalValue::PrivateLinkage,
5918- CmdConstant);
5919- GV->setSection (getSectionNameForCommandline (T));
5920- GV->setAlignment (Align (1 ));
5921- UsedArray.push_back (
5922- ConstantExpr::getPointerBitCastOrAddrSpaceCast (GV, UsedElementType));
5916+ GlobalVariable *CmdLine = new llvm::GlobalVariable (
5917+ M, CmdConstant->getType (), true , llvm::GlobalValue::PrivateLinkage,
5918+ CmdConstant);
5919+ CmdLine->setSection (getSectionNameForCommandline (T));
5920+ CmdLine->setAlignment (Align (1 ));
59235921 if (llvm::GlobalVariable *Old = M.getGlobalVariable (" llvm.cmdline" , true )) {
59245922 assert (Old->hasZeroLiveUses () &&
59255923 " llvm.cmdline can only be used once in llvm.compiler.used" );
5926- GV ->takeName (Old);
5924+ CmdLine ->takeName (Old);
59275925 Old->eraseFromParent ();
59285926 } else {
5929- GV ->setName (" llvm.cmdline" );
5927+ CmdLine ->setName (" llvm.cmdline" );
59305928 }
5929+ NewGlobals.push_back (CmdLine);
5930+ appendToCompilerUsed (M, NewGlobals);
59315931 }
5932-
5933- if (UsedArray.empty ())
5934- return ;
5935-
5936- // Recreate llvm.compiler.used.
5937- ArrayType *ATy = ArrayType::get (UsedElementType, UsedArray.size ());
5938- auto *NewUsed = new GlobalVariable (
5939- M, ATy, false , llvm::GlobalValue::AppendingLinkage,
5940- llvm::ConstantArray::get (ATy, UsedArray), " llvm.compiler.used" );
5941- NewUsed->setSection (" llvm.metadata" );
59425932}
0 commit comments