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