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/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/IR/Comdat.h"
17 #include "llvm/IR/GlobalObject.h"
18 #include "llvm/IR/Module.h"
22 Comdat::Comdat(Comdat
&&C
) : Name(C
.Name
), SK(C
.SK
) {}
24 Comdat::Comdat() = default;
26 StringRef
Comdat::getName() const { return Name
->first(); }
28 LLVMComdatRef
LLVMGetOrInsertComdat(LLVMModuleRef M
, const char *Name
) {
29 return wrap(unwrap(M
)->getOrInsertComdat(Name
));
32 LLVMComdatRef
LLVMGetComdat(LLVMValueRef V
) {
33 GlobalObject
*G
= unwrap
<GlobalObject
>(V
);
34 return wrap(G
->getComdat());
37 void LLVMSetComdat(LLVMValueRef V
, LLVMComdatRef C
) {
38 GlobalObject
*G
= unwrap
<GlobalObject
>(V
);
39 G
->setComdat(unwrap(C
));
42 LLVMComdatSelectionKind
LLVMGetComdatSelectionKind(LLVMComdatRef C
) {
43 switch (unwrap(C
)->getSelectionKind()) {
45 return LLVMAnyComdatSelectionKind
;
46 case Comdat::ExactMatch
:
47 return LLVMExactMatchComdatSelectionKind
;
49 return LLVMLargestComdatSelectionKind
;
50 case Comdat::NoDuplicates
:
51 return LLVMNoDuplicatesComdatSelectionKind
;
52 case Comdat::SameSize
:
53 return LLVMSameSizeComdatSelectionKind
;
55 llvm_unreachable("Invalid Comdat SelectionKind!");
58 void LLVMSetComdatSelectionKind(LLVMComdatRef C
, LLVMComdatSelectionKind kind
) {
59 Comdat
*Cd
= unwrap(C
);
61 case LLVMAnyComdatSelectionKind
:
62 Cd
->setSelectionKind(Comdat::Any
);
64 case LLVMExactMatchComdatSelectionKind
:
65 Cd
->setSelectionKind(Comdat::ExactMatch
);
67 case LLVMLargestComdatSelectionKind
:
68 Cd
->setSelectionKind(Comdat::Largest
);
70 case LLVMNoDuplicatesComdatSelectionKind
:
71 Cd
->setSelectionKind(Comdat::NoDuplicates
);
73 case LLVMSameSizeComdatSelectionKind
:
74 Cd
->setSelectionKind(Comdat::SameSize
);