[ThinLTO] Metadata linking for imported functions
[oota-llvm.git] / include / llvm / Linker / IRMover.h
1 //===- IRMover.h ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_LINKER_IRMOVER_H
11 #define LLVM_LINKER_IRMOVER_H
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include <functional>
16
17 namespace llvm {
18 class GlobalValue;
19 class MDNode;
20 class Module;
21 class StructType;
22 class Type;
23
24 class IRMover {
25   struct StructTypeKeyInfo {
26     struct KeyTy {
27       ArrayRef<Type *> ETypes;
28       bool IsPacked;
29       KeyTy(ArrayRef<Type *> E, bool P);
30       KeyTy(const StructType *ST);
31       bool operator==(const KeyTy &that) const;
32       bool operator!=(const KeyTy &that) const;
33     };
34     static StructType *getEmptyKey();
35     static StructType *getTombstoneKey();
36     static unsigned getHashValue(const KeyTy &Key);
37     static unsigned getHashValue(const StructType *ST);
38     static bool isEqual(const KeyTy &LHS, const StructType *RHS);
39     static bool isEqual(const StructType *LHS, const StructType *RHS);
40   };
41
42 public:
43   class IdentifiedStructTypeSet {
44     // The set of opaque types is the composite module.
45     DenseSet<StructType *> OpaqueStructTypes;
46
47     // The set of identified but non opaque structures in the composite module.
48     DenseSet<StructType *, StructTypeKeyInfo> NonOpaqueStructTypes;
49
50   public:
51     void addNonOpaque(StructType *Ty);
52     void switchToNonOpaque(StructType *Ty);
53     void addOpaque(StructType *Ty);
54     StructType *findNonOpaque(ArrayRef<Type *> ETypes, bool IsPacked);
55     bool hasType(StructType *Ty);
56   };
57
58   IRMover(Module &M);
59
60   typedef std::function<void(GlobalValue &)> ValueAdder;
61   /// Move in the provide values. The source is destroyed.
62   /// Returns true on error.
63   bool move(Module &Src, ArrayRef<GlobalValue *> ValuesToLink,
64             std::function<void(GlobalValue &GV, ValueAdder Add)> AddLazyFor,
65             DenseMap<unsigned, MDNode *> *ValIDToTempMDMap = nullptr,
66             bool IsMetadataLinkingPostpass = false);
67   Module &getModule() { return Composite; }
68
69 private:
70   Module &Composite;
71   IdentifiedStructTypeSet IdentifiedStructTypes;
72 };
73
74 } // End llvm namespace
75
76 #endif