xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Comdat.cpp (revision 1fd87a682ad7442327078e1eeb63edc4258f9815)
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"
14*1fd87a68SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
15*1fd87a68SDimitry Andric #include "llvm/ADT/StringMapEntry.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
170b57cec5SDimitry Andric #include "llvm/IR/Comdat.h"
180b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h"
190b57cec5SDimitry Andric #include "llvm/IR/Module.h"
20*1fd87a68SDimitry Andric #include "llvm/IR/Value.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
Comdat(Comdat && C)240b57cec5SDimitry Andric Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {}
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric Comdat::Comdat() = default;
270b57cec5SDimitry Andric 
getName() const280b57cec5SDimitry Andric StringRef Comdat::getName() const { return Name->first(); }
290b57cec5SDimitry Andric 
addUser(GlobalObject * GO)3004eeddc0SDimitry Andric void Comdat::addUser(GlobalObject *GO) { Users.insert(GO); }
3104eeddc0SDimitry Andric 
removeUser(GlobalObject * GO)3204eeddc0SDimitry Andric void Comdat::removeUser(GlobalObject *GO) { Users.erase(GO); }
3304eeddc0SDimitry Andric 
LLVMGetOrInsertComdat(LLVMModuleRef M,const char * Name)340b57cec5SDimitry Andric LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) {
350b57cec5SDimitry Andric   return wrap(unwrap(M)->getOrInsertComdat(Name));
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
LLVMGetComdat(LLVMValueRef V)380b57cec5SDimitry Andric LLVMComdatRef LLVMGetComdat(LLVMValueRef V) {
390b57cec5SDimitry Andric   GlobalObject *G = unwrap<GlobalObject>(V);
400b57cec5SDimitry Andric   return wrap(G->getComdat());
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
LLVMSetComdat(LLVMValueRef V,LLVMComdatRef C)430b57cec5SDimitry Andric void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) {
440b57cec5SDimitry Andric   GlobalObject *G = unwrap<GlobalObject>(V);
450b57cec5SDimitry Andric   G->setComdat(unwrap(C));
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric 
LLVMGetComdatSelectionKind(LLVMComdatRef C)480b57cec5SDimitry Andric LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) {
490b57cec5SDimitry Andric   switch (unwrap(C)->getSelectionKind()) {
500b57cec5SDimitry Andric   case Comdat::Any:
510b57cec5SDimitry Andric     return LLVMAnyComdatSelectionKind;
520b57cec5SDimitry Andric   case Comdat::ExactMatch:
530b57cec5SDimitry Andric     return LLVMExactMatchComdatSelectionKind;
540b57cec5SDimitry Andric   case Comdat::Largest:
550b57cec5SDimitry Andric     return LLVMLargestComdatSelectionKind;
56fe6060f1SDimitry Andric   case Comdat::NoDeduplicate:
57fe6060f1SDimitry Andric     return LLVMNoDeduplicateComdatSelectionKind;
580b57cec5SDimitry Andric   case Comdat::SameSize:
590b57cec5SDimitry Andric     return LLVMSameSizeComdatSelectionKind;
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric   llvm_unreachable("Invalid Comdat SelectionKind!");
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
LLVMSetComdatSelectionKind(LLVMComdatRef C,LLVMComdatSelectionKind kind)640b57cec5SDimitry Andric void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) {
650b57cec5SDimitry Andric   Comdat *Cd = unwrap(C);
660b57cec5SDimitry Andric   switch (kind) {
670b57cec5SDimitry Andric   case LLVMAnyComdatSelectionKind:
680b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::Any);
690b57cec5SDimitry Andric     break;
700b57cec5SDimitry Andric   case LLVMExactMatchComdatSelectionKind:
710b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::ExactMatch);
720b57cec5SDimitry Andric     break;
730b57cec5SDimitry Andric   case LLVMLargestComdatSelectionKind:
740b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::Largest);
750b57cec5SDimitry Andric     break;
76fe6060f1SDimitry Andric   case LLVMNoDeduplicateComdatSelectionKind:
77fe6060f1SDimitry Andric     Cd->setSelectionKind(Comdat::NoDeduplicate);
780b57cec5SDimitry Andric     break;
790b57cec5SDimitry Andric   case LLVMSameSizeComdatSelectionKind:
800b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::SameSize);
810b57cec5SDimitry Andric     break;
820b57cec5SDimitry Andric   }
830b57cec5SDimitry Andric }
84