1 //===- Linker.h - Module Linker Interface -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LINKER_LINKER_H
11 #define LLVM_LINKER_LINKER_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/IR/DiagnosticInfo.h"
17 #include "llvm/IR/FunctionInfo.h"
24 /// This class provides the core functionality of linking in LLVM. It keeps a
25 /// pointer to the merged module so far. It doesn't take ownership of the
26 /// module since it is assumed that the user of this class will want to do
27 /// something with it after the linking.
30 struct StructTypeKeyInfo {
32 ArrayRef<Type *> ETypes;
34 KeyTy(ArrayRef<Type *> E, bool P);
35 KeyTy(const StructType *ST);
36 bool operator==(const KeyTy &that) const;
37 bool operator!=(const KeyTy &that) const;
39 static StructType *getEmptyKey();
40 static StructType *getTombstoneKey();
41 static unsigned getHashValue(const KeyTy &Key);
42 static unsigned getHashValue(const StructType *ST);
43 static bool isEqual(const KeyTy &LHS, const StructType *RHS);
44 static bool isEqual(const StructType *LHS, const StructType *RHS);
47 typedef DenseSet<StructType *, StructTypeKeyInfo> NonOpaqueStructTypeSet;
48 typedef DenseSet<StructType *> OpaqueStructTypeSet;
50 struct IdentifiedStructTypeSet {
51 // The set of opaque types is the composite module.
52 OpaqueStructTypeSet OpaqueStructTypes;
54 // The set of identified but non opaque structures in the composite module.
55 NonOpaqueStructTypeSet NonOpaqueStructTypes;
57 void addNonOpaque(StructType *Ty);
58 void switchToNonOpaque(StructType *Ty);
59 void addOpaque(StructType *Ty);
60 StructType *findNonOpaque(ArrayRef<Type *> ETypes, bool IsPacked);
61 bool hasType(StructType *Ty);
66 OverrideFromSrc = (1 << 0),
67 LinkOnlyNeeded = (1 << 1),
68 InternalizeLinkedSymbols = (1 << 2)
71 Linker(Module &M, DiagnosticHandlerFunction DiagnosticHandler);
73 /// \brief Link \p Src into the composite. The source is destroyed.
75 /// Passing OverrideSymbols as true will have symbols from Src
76 /// shadow those in the Dest.
77 /// For ThinLTO function importing/exporting the \p FunctionInfoIndex
78 /// is passed. If \p FunctionsToImport is provided, only the functions that
79 /// are part of the set will be imported from the source module.
81 /// Returns true on error.
82 bool linkInModule(Module &Src, unsigned Flags = Flags::None,
83 const FunctionInfoIndex *Index = nullptr,
84 DenseSet<const GlobalValue *> *FunctionsToImport = nullptr);
86 static bool linkModules(Module &Dest, Module &Src,
87 DiagnosticHandlerFunction DiagnosticHandler,
88 unsigned Flags = Flags::None);
90 DiagnosticHandlerFunction getDiagnosticHandler() const {
91 return DiagnosticHandler;
97 IdentifiedStructTypeSet IdentifiedStructTypes;
99 DiagnosticHandlerFunction DiagnosticHandler;
102 /// Create a new module with exported local functions renamed and promoted
104 std::unique_ptr<Module>
105 renameModuleForThinLTO(std::unique_ptr<Module> &M,
106 const FunctionInfoIndex *Index,
107 DiagnosticHandlerFunction DiagnosticHandler);
109 } // End llvm namespace