xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Comdat.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
10b57cec5SDimitry Andric //===- Comdat.cpp - Implement Metadata classes ----------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the Comdat class (including the C bindings).
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm-c/Comdat.h"
140b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
150b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
160b57cec5SDimitry Andric #include "llvm/IR/Comdat.h"
170b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h"
180b57cec5SDimitry Andric #include "llvm/IR/Module.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {}
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric Comdat::Comdat() = default;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric StringRef Comdat::getName() const { return Name->first(); }
270b57cec5SDimitry Andric 
28*04eeddc0SDimitry Andric void Comdat::addUser(GlobalObject *GO) { Users.insert(GO); }
29*04eeddc0SDimitry Andric 
30*04eeddc0SDimitry Andric void Comdat::removeUser(GlobalObject *GO) { Users.erase(GO); }
31*04eeddc0SDimitry Andric 
320b57cec5SDimitry Andric LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) {
330b57cec5SDimitry Andric   return wrap(unwrap(M)->getOrInsertComdat(Name));
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric LLVMComdatRef LLVMGetComdat(LLVMValueRef V) {
370b57cec5SDimitry Andric   GlobalObject *G = unwrap<GlobalObject>(V);
380b57cec5SDimitry Andric   return wrap(G->getComdat());
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) {
420b57cec5SDimitry Andric   GlobalObject *G = unwrap<GlobalObject>(V);
430b57cec5SDimitry Andric   G->setComdat(unwrap(C));
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) {
470b57cec5SDimitry Andric   switch (unwrap(C)->getSelectionKind()) {
480b57cec5SDimitry Andric   case Comdat::Any:
490b57cec5SDimitry Andric     return LLVMAnyComdatSelectionKind;
500b57cec5SDimitry Andric   case Comdat::ExactMatch:
510b57cec5SDimitry Andric     return LLVMExactMatchComdatSelectionKind;
520b57cec5SDimitry Andric   case Comdat::Largest:
530b57cec5SDimitry Andric     return LLVMLargestComdatSelectionKind;
54fe6060f1SDimitry Andric   case Comdat::NoDeduplicate:
55fe6060f1SDimitry Andric     return LLVMNoDeduplicateComdatSelectionKind;
560b57cec5SDimitry Andric   case Comdat::SameSize:
570b57cec5SDimitry Andric     return LLVMSameSizeComdatSelectionKind;
580b57cec5SDimitry Andric   }
590b57cec5SDimitry Andric   llvm_unreachable("Invalid Comdat SelectionKind!");
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) {
630b57cec5SDimitry Andric   Comdat *Cd = unwrap(C);
640b57cec5SDimitry Andric   switch (kind) {
650b57cec5SDimitry Andric   case LLVMAnyComdatSelectionKind:
660b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::Any);
670b57cec5SDimitry Andric     break;
680b57cec5SDimitry Andric   case LLVMExactMatchComdatSelectionKind:
690b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::ExactMatch);
700b57cec5SDimitry Andric     break;
710b57cec5SDimitry Andric   case LLVMLargestComdatSelectionKind:
720b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::Largest);
730b57cec5SDimitry Andric     break;
74fe6060f1SDimitry Andric   case LLVMNoDeduplicateComdatSelectionKind:
75fe6060f1SDimitry Andric     Cd->setSelectionKind(Comdat::NoDeduplicate);
760b57cec5SDimitry Andric     break;
770b57cec5SDimitry Andric   case LLVMSameSizeComdatSelectionKind:
780b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::SameSize);
790b57cec5SDimitry Andric     break;
800b57cec5SDimitry Andric   }
810b57cec5SDimitry Andric }
82