xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/Comdat.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- llvm/IR/Comdat.h - Comdat definitions --------------------*- 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 /// @file
10 /// This file contains the declaration of the Comdat class, which represents a
11 /// single COMDAT in LLVM.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_COMDAT_H
16 #define LLVM_IR_COMDAT_H
17 
18 #include "llvm-c/Types.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/CBindingWrapping.h"
21 #include "llvm/Support/Compiler.h"
22 
23 namespace llvm {
24 
25 class GlobalObject;
26 class raw_ostream;
27 class StringRef;
28 template <typename ValueTy> class StringMapEntry;
29 
30 // This is a Name X SelectionKind pair. The reason for having this be an
31 // independent object instead of just adding the name and the SelectionKind
32 // to a GlobalObject is that it is invalid to have two Comdats with the same
33 // name but different SelectionKind. This structure makes that unrepresentable.
34 class Comdat {
35 public:
36   enum SelectionKind {
37     Any,           ///< The linker may choose any COMDAT.
38     ExactMatch,    ///< The data referenced by the COMDAT must be the same.
39     Largest,       ///< The linker will choose the largest COMDAT.
40     NoDeduplicate, ///< No deduplication is performed.
41     SameSize,      ///< The data referenced by the COMDAT must be the same size.
42   };
43 
44   Comdat(const Comdat &) = delete;
45   LLVM_ABI Comdat(Comdat &&C);
46 
getSelectionKind()47   SelectionKind getSelectionKind() const { return SK; }
setSelectionKind(SelectionKind Val)48   void setSelectionKind(SelectionKind Val) { SK = Val; }
49   LLVM_ABI StringRef getName() const;
50   LLVM_ABI void print(raw_ostream &OS, bool IsForDebug = false) const;
51   LLVM_ABI void dump() const;
getUsers()52   const SmallPtrSetImpl<GlobalObject *> &getUsers() const { return Users; }
53 
54 private:
55   friend class Module;
56   friend class GlobalObject;
57 
58   Comdat();
59   void addUser(GlobalObject *GO);
60   void removeUser(GlobalObject *GO);
61 
62   // Points to the map in Module.
63   StringMapEntry<Comdat> *Name = nullptr;
64   SelectionKind SK = Any;
65   // Globals using this comdat.
66   SmallPtrSet<GlobalObject *, 2> Users;
67 };
68 
69 // Create wrappers for C Binding types (see CBindingWrapping.h).
70 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Comdat, LLVMComdatRef)
71 
72 inline raw_ostream &operator<<(raw_ostream &OS, const Comdat &C) {
73   C.print(OS);
74   return OS;
75 }
76 
77 } // end namespace llvm
78 
79 #endif // LLVM_IR_COMDAT_H
80