xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Linker/IRMover.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- IRMover.h ------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LINKER_IRMOVER_H
10 #define LLVM_LINKER_IRMOVER_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/DenseSet.h"
14 #include "llvm/ADT/FunctionExtras.h"
15 #include "llvm/Support/Compiler.h"
16 #include <functional>
17 
18 namespace llvm {
19 class Error;
20 class GlobalValue;
21 class Metadata;
22 class Module;
23 class StructType;
24 class TrackingMDRef;
25 class Type;
26 
27 class IRMover {
28   struct StructTypeKeyInfo {
29     struct KeyTy {
30       ArrayRef<Type *> ETypes;
31       bool IsPacked;
32       LLVM_ABI KeyTy(ArrayRef<Type *> E, bool P);
33       LLVM_ABI KeyTy(const StructType *ST);
34       LLVM_ABI bool operator==(const KeyTy &that) const;
35       LLVM_ABI bool operator!=(const KeyTy &that) const;
36     };
37     LLVM_ABI static StructType *getEmptyKey();
38     LLVM_ABI static StructType *getTombstoneKey();
39     LLVM_ABI static unsigned getHashValue(const KeyTy &Key);
40     LLVM_ABI static unsigned getHashValue(const StructType *ST);
41     LLVM_ABI static bool isEqual(const KeyTy &LHS, const StructType *RHS);
42     LLVM_ABI static bool isEqual(const StructType *LHS, const StructType *RHS);
43   };
44 
45   /// Type of the Metadata map in \a ValueToValueMapTy.
46   typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT;
47 
48 public:
49   class IdentifiedStructTypeSet {
50     // The set of opaque types is the composite module.
51     DenseSet<StructType *> OpaqueStructTypes;
52 
53     // The set of identified but non opaque structures in the composite module.
54     DenseSet<StructType *, StructTypeKeyInfo> NonOpaqueStructTypes;
55 
56   public:
57     LLVM_ABI void addNonOpaque(StructType *Ty);
58     LLVM_ABI void switchToNonOpaque(StructType *Ty);
59     LLVM_ABI void addOpaque(StructType *Ty);
60     LLVM_ABI StructType *findNonOpaque(ArrayRef<Type *> ETypes, bool IsPacked);
61     LLVM_ABI bool hasType(StructType *Ty);
62   };
63 
64   LLVM_ABI IRMover(Module &M);
65 
66   typedef std::function<void(GlobalValue &)> ValueAdder;
67   using LazyCallback =
68       llvm::unique_function<void(GlobalValue &GV, ValueAdder Add)>;
69 
70   /// Move in the provide values in \p ValuesToLink from \p Src.
71   ///
72   /// - \p AddLazyFor is a call back that the IRMover will call when a global
73   ///   value is referenced by one of the ValuesToLink (transitively) but was
74   ///   not present in ValuesToLink. The GlobalValue and a ValueAdder callback
75   ///   are passed as an argument, and the callback is expected to be called
76   ///   if the GlobalValue needs to be added to the \p ValuesToLink and linked.
77   ///   Pass nullptr if there's no work to be done in such cases.
78   /// - \p IsPerformingImport is true when this IR link is to perform ThinLTO
79   ///   function importing from Src.
80   LLVM_ABI Error move(std::unique_ptr<Module> Src,
81                       ArrayRef<GlobalValue *> ValuesToLink,
82                       LazyCallback AddLazyFor, bool IsPerformingImport);
getModule()83   Module &getModule() { return Composite; }
84 
85 private:
86   Module &Composite;
87   IdentifiedStructTypeSet IdentifiedStructTypes;
88   MDMapT SharedMDs; ///< A Metadata map to use for all calls to \a move().
89 };
90 
91 } // End llvm namespace
92 
93 #endif
94