xref: /llvm-project-15.0.7/llvm/lib/IR/Comdat.cpp (revision 4abfe47e)
1 //===- Comdat.cpp - Implement Metadata classes ----------------------------===//
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 // This file implements the Comdat class (including the C bindings).
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
19 
20 using namespace llvm;
21 
22 Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {}
23 
24 Comdat::Comdat() = default;
25 
26 StringRef Comdat::getName() const { return Name->first(); }
27 
28 void Comdat::addUser(GlobalObject *GO) { Users.insert(GO); }
29 
30 void Comdat::removeUser(GlobalObject *GO) { Users.erase(GO); }
31 
32 LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) {
33   return wrap(unwrap(M)->getOrInsertComdat(Name));
34 }
35 
36 LLVMComdatRef LLVMGetComdat(LLVMValueRef V) {
37   GlobalObject *G = unwrap<GlobalObject>(V);
38   return wrap(G->getComdat());
39 }
40 
41 void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) {
42   GlobalObject *G = unwrap<GlobalObject>(V);
43   G->setComdat(unwrap(C));
44 }
45 
46 LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) {
47   switch (unwrap(C)->getSelectionKind()) {
48   case Comdat::Any:
49     return LLVMAnyComdatSelectionKind;
50   case Comdat::ExactMatch:
51     return LLVMExactMatchComdatSelectionKind;
52   case Comdat::Largest:
53     return LLVMLargestComdatSelectionKind;
54   case Comdat::NoDeduplicate:
55     return LLVMNoDeduplicateComdatSelectionKind;
56   case Comdat::SameSize:
57     return LLVMSameSizeComdatSelectionKind;
58   }
59   llvm_unreachable("Invalid Comdat SelectionKind!");
60 }
61 
62 void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) {
63   Comdat *Cd = unwrap(C);
64   switch (kind) {
65   case LLVMAnyComdatSelectionKind:
66     Cd->setSelectionKind(Comdat::Any);
67     break;
68   case LLVMExactMatchComdatSelectionKind:
69     Cd->setSelectionKind(Comdat::ExactMatch);
70     break;
71   case LLVMLargestComdatSelectionKind:
72     Cd->setSelectionKind(Comdat::Largest);
73     break;
74   case LLVMNoDeduplicateComdatSelectionKind:
75     Cd->setSelectionKind(Comdat::NoDeduplicate);
76     break;
77   case LLVMSameSizeComdatSelectionKind:
78     Cd->setSelectionKind(Comdat::SameSize);
79     break;
80   }
81 }
82