llvm.org GIT mirror llvm / f1f1a4f
Make it possible to use the linker without destroying the source module. This is so the source module can be linked to multiple other destination modules. For all that used LinkModules() before, they will continue to destroy the source module as before. This line, and those below, will be ignored-- M include/llvm/Linker.h M tools/bugpoint/Miscompilation.cpp M tools/bugpoint/BugDriver.cpp M tools/llvm-link/llvm-link.cpp M lib/Linker/LinkModules.cpp git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141606 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 8 years ago
5 changed file(s) with 73 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
5656 QuietWarnings = 2, ///< Don't print warnings to stderr.
5757 QuietErrors = 4 ///< Don't print errors to stderr.
5858 };
59
59
60 enum LinkerMode {
61 DestroySource = 0, // Allow source module to be destroyed.
62 PreserveSource = 1 // Preserve the source module.
63 };
64
6065 /// @}
6166 /// @name Constructors
6267 /// @{
244249 Module* Src, ///< Module linked into \p Dest
245250 std::string* ErrorMsg = 0 /// Error/diagnostic string
246251 ) {
247 return LinkModules(Composite, Src, ErrorMsg );
252 return LinkModules(Composite, Src, Linker::DestroySource, ErrorMsg );
248253 }
249254
250255 /// This is the heart of the linker. This method will take unconditional
258263 /// error.
259264 /// @returns True if an error occurs, false otherwise.
260265 /// @brief Generically link two modules together.
261 static bool LinkModules(Module* Dest, Module* Src, std::string* ErrorMsg);
266 static bool LinkModules(Module* Dest, Module* Src, unsigned Mode,
267 std::string* ErrorMsg);
262268
263269 /// This function looks through the Linker's LibPaths to find a library with
264270 /// the name \p Filename. If the library cannot be found, the returned path
1313 #include "llvm/Linker.h"
1414 #include "llvm/Constants.h"
1515 #include "llvm/DerivedTypes.h"
16 #include "llvm/Instructions.h"
1617 #include "llvm/Module.h"
18 #include "llvm/ADT/SmallPtrSet.h"
1719 #include "llvm/Support/raw_ostream.h"
1820 #include "llvm/Support/Path.h"
21 #include "llvm/Transforms/Utils/Cloning.h"
1922 #include "llvm/Transforms/Utils/ValueMapper.h"
2023 using namespace llvm;
2124
332335
333336 std::vector AppendingVars;
334337
338 unsigned Mode; // Mode to treat source module.
339
340 // Set of items not to link in from source.
341 SmallPtrSet DoNotLinkFromSource;
342
335343 public:
336344 std::string ErrorMsg;
337345
338 ModuleLinker(Module *dstM, Module *srcM) : DstM(dstM), SrcM(srcM) { }
346 ModuleLinker(Module *dstM, Module *srcM, unsigned mode)
347 : DstM(dstM), SrcM(srcM), Mode(mode) { }
339348
340349 bool run();
341350
595604 DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
596605 DstGV->eraseFromParent();
597606
598 // Zap the initializer in the source variable so we don't try to link it.
599 SrcGV->setInitializer(0);
600 SrcGV->setLinkage(GlobalValue::ExternalLinkage);
607 // Track the source variable so we don't try to link it.
608 DoNotLinkFromSource.insert(SrcGV);
609
601610 return false;
602611 }
603612
632641 // Make sure to remember this mapping.
633642 ValueMap[SGV] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGV->getType()));
634643
635 // Destroy the source global's initializer (and convert it to a prototype)
636 // so that we don't attempt to copy it over when processing global
637 // initializers.
638 SGV->setInitializer(0);
639 SGV->setLinkage(GlobalValue::ExternalLinkage);
644 // Track the source global so that we don't attempt to copy it over when
645 // processing global initializers.
646 DoNotLinkFromSource.insert(SGV);
647
640648 return false;
641649 }
642650 }
681689 // Make sure to remember this mapping.
682690 ValueMap[SF] = ConstantExpr::getBitCast(DGV, TypeMap.get(SF->getType()));
683691
684 // Remove the body from the source module so we don't attempt to remap it.
685 SF->deleteBody();
692 // Track the function from the source module so we don't attempt to remap
693 // it.
694 DoNotLinkFromSource.insert(SF);
695
686696 return false;
687697 }
688698 }
721731 // Make sure to remember this mapping.
722732 ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
723733
724 // Remove the body from the source module so we don't attempt to remap it.
725 SGA->setAliasee(0);
734 // Track the alias from the source module so we don't attempt to remap it.
735 DoNotLinkFromSource.insert(SGA);
736
726737 return false;
727738 }
728739 }
778789 // Loop over all of the globals in the src module, mapping them over as we go
779790 for (Module::const_global_iterator I = SrcM->global_begin(),
780791 E = SrcM->global_end(); I != E; ++I) {
781 if (!I->hasInitializer()) continue; // Only process initialized GV's.
792
793 // Only process initialized GV's or ones not already in dest.
794 if (!I->hasInitializer() || DoNotLinkFromSource.count(I)) continue;
782795
783796 // Grab destination global variable.
784797 GlobalVariable *DGV = cast(ValueMap[I]);
804817 ValueMap[I] = DI;
805818 }
806819
807 // Splice the body of the source function into the dest function.
808 Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
809
810 // At this point, all of the instructions and values of the function are now
811 // copied over. The only problem is that they are still referencing values in
812 // the Source function as operands. Loop through all of the operands of the
813 // functions and patch them up to point to the local versions.
814 for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
815 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
816 RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap);
817
820 if (Mode == Linker::DestroySource) {
821 // Splice the body of the source function into the dest function.
822 Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
823
824 // At this point, all of the instructions and values of the function are now
825 // copied over. The only problem is that they are still referencing values in
826 // the Source function as operands. Loop through all of the operands of the
827 // functions and patch them up to point to the local versions.
828 for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
829 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
830 RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap);
831
832 } else {
833 // Clone the body of the function into the dest function.
834 SmallVector Returns; // Ignore returns.
835 CloneFunctionInto(Dst, Src, ValueMap, false, Returns);
836 }
837
818838 // There is no need to map the arguments anymore.
819839 for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
820840 I != E; ++I)
821841 ValueMap.erase(I);
842
822843 }
823844
824845
825846 void ModuleLinker::linkAliasBodies() {
826847 for (Module::alias_iterator I = SrcM->alias_begin(), E = SrcM->alias_end();
827 I != E; ++I)
848 I != E; ++I) {
849 if (DoNotLinkFromSource.count(I))
850 continue;
828851 if (Constant *Aliasee = I->getAliasee()) {
829852 GlobalAlias *DA = cast(ValueMap[I]);
830853 DA->setAliasee(MapValue(Aliasee, ValueMap, RF_None, &TypeMap));
831854 }
855 }
832856 }
833857
834858 /// linkNamedMDNodes - Insert all of the named mdnodes in Src into the Dest
890914 StringRef ModuleId = SrcM->getModuleIdentifier();
891915 if (!ModuleId.empty())
892916 DstM->removeLibrary(sys::path::stem(ModuleId));
893
894917
895918 // Loop over all of the linked values to compute type mappings.
896919 computeTypeMapping();
927950 // Link in the function bodies that are defined in the source module into
928951 // DstM.
929952 for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
930 if (SF->isDeclaration()) continue; // No body if function is external.
953 // Skip if no body (function is external) or marked to skip.
954 if (SF->isDeclaration() || DoNotLinkFromSource.count(SF)) continue;
931955
932956 linkFunctionBody(cast(ValueMap[SF]), SF);
933957 }
956980 // error occurs, true is returned and ErrorMsg (if not null) is set to indicate
957981 // the problem. Upon failure, the Dest module could be in a modified state, and
958982 // shouldn't be relied on to be consistent.
959 bool Linker::LinkModules(Module *Dest, Module *Src, std::string *ErrorMsg) {
960 ModuleLinker TheLinker(Dest, Src);
983 bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode,
984 std::string *ErrorMsg) {
985 ModuleLinker TheLinker(Dest, Src, Mode);
961986 if (TheLinker.run()) {
962987 if (ErrorMsg) *ErrorMsg = TheLinker.ErrorMsg;
963988 return true;
126126
127127 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
128128 std::string ErrorMessage;
129 if (Linker::LinkModules(Program, M.get(), &ErrorMessage)) {
129 if (Linker::LinkModules(Program, M.get(), Linker::DestroySource,
130 &ErrorMessage)) {
130131 errs() << ToolName << ": error linking in '" << Filenames[i] << "': "
131132 << ErrorMessage << '\n';
132133 return true;
221221 M1 = CloneModule(M1);
222222 M2 = CloneModule(M2);
223223 }
224 if (Linker::LinkModules(M1, M2, &ErrorMsg)) {
224 if (Linker::LinkModules(M1, M2, Linker::DestroySource, &ErrorMsg)) {
225225 errs() << BD.getToolName() << ": Error linking modules together:"
226226 << ErrorMsg << '\n';
227227 exit(1);
395395 // Replace the current program with the loop extracted version, and try to
396396 // extract another loop.
397397 std::string ErrorMsg;
398 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted, &ErrorMsg)){
398 if (Linker::LinkModules(ToNotOptimize, ToOptimizeLoopExtracted,
399 Linker::DestroySource, &ErrorMsg)){
399400 errs() << BD.getToolName() << ": Error linking modules together:"
400401 << ErrorMsg << '\n';
401402 exit(1);
574575 I->getFunctionType()));
575576
576577 std::string ErrorMsg;
577 if (Linker::LinkModules(ProgClone, Extracted, &ErrorMsg)) {
578 if (Linker::LinkModules(ProgClone, Extracted, Linker::DestroySource,
579 &ErrorMsg)) {
578580 errs() << BD.getToolName() << ": Error linking modules together:"
579581 << ErrorMsg << '\n';
580582 exit(1);
102102
103103 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
104104
105 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
105 if (Linker::LinkModules(Composite.get(), M.get(), Linker::DestroySource,
106 &ErrorMessage)) {
106107 errs() << argv[0] << ": link error in '" << InputFilenames[i]
107108 << "': " << ErrorMessage << "\n";
108109 return 1;