1 //===- Comdat.cpp - Implement Metadata classes ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Comdat class (including the C bindings).
11 //===----------------------------------------------------------------------===//
13 #include "llvm-c/Comdat.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/StringMapEntry.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/Comdat.h"
18 #include "llvm/IR/GlobalObject.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Value.h"
24 Comdat::Comdat(Comdat
&&C
) : Name(C
.Name
), SK(C
.SK
) {}
26 Comdat::Comdat() = default;
28 StringRef
Comdat::getName() const { return Name
->first(); }
30 void Comdat::addUser(GlobalObject
*GO
) { Users
.insert(GO
); }
32 void Comdat::removeUser(GlobalObject
*GO
) { Users
.erase(GO
); }
34 LLVMComdatRef
LLVMGetOrInsertComdat(LLVMModuleRef M
, const char *Name
) {
35 return wrap(unwrap(M
)->getOrInsertComdat(Name
));
38 LLVMComdatRef
LLVMGetComdat(LLVMValueRef V
) {
39 GlobalObject
*G
= unwrap
<GlobalObject
>(V
);
40 return wrap(G
->getComdat());
43 void LLVMSetComdat(LLVMValueRef V
, LLVMComdatRef C
) {
44 GlobalObject
*G
= unwrap
<GlobalObject
>(V
);
45 G
->setComdat(unwrap(C
));
48 LLVMComdatSelectionKind
LLVMGetComdatSelectionKind(LLVMComdatRef C
) {
49 switch (unwrap(C
)->getSelectionKind()) {
51 return LLVMAnyComdatSelectionKind
;
52 case Comdat::ExactMatch
:
53 return LLVMExactMatchComdatSelectionKind
;
55 return LLVMLargestComdatSelectionKind
;
56 case Comdat::NoDeduplicate
:
57 return LLVMNoDeduplicateComdatSelectionKind
;
58 case Comdat::SameSize
:
59 return LLVMSameSizeComdatSelectionKind
;
61 llvm_unreachable("Invalid Comdat SelectionKind!");
64 void LLVMSetComdatSelectionKind(LLVMComdatRef C
, LLVMComdatSelectionKind kind
) {
65 Comdat
*Cd
= unwrap(C
);
67 case LLVMAnyComdatSelectionKind
:
68 Cd
->setSelectionKind(Comdat::Any
);
70 case LLVMExactMatchComdatSelectionKind
:
71 Cd
->setSelectionKind(Comdat::ExactMatch
);
73 case LLVMLargestComdatSelectionKind
:
74 Cd
->setSelectionKind(Comdat::Largest
);
76 case LLVMNoDeduplicateComdatSelectionKind
:
77 Cd
->setSelectionKind(Comdat::NoDeduplicate
);
79 case LLVMSameSizeComdatSelectionKind
:
80 Cd
->setSelectionKind(Comdat::SameSize
);