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