xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Comdat.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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 
280b57cec5SDimitry Andric LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) {
290b57cec5SDimitry Andric   return wrap(unwrap(M)->getOrInsertComdat(Name));
300b57cec5SDimitry Andric }
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric LLVMComdatRef LLVMGetComdat(LLVMValueRef V) {
330b57cec5SDimitry Andric   GlobalObject *G = unwrap<GlobalObject>(V);
340b57cec5SDimitry Andric   return wrap(G->getComdat());
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) {
380b57cec5SDimitry Andric   GlobalObject *G = unwrap<GlobalObject>(V);
390b57cec5SDimitry Andric   G->setComdat(unwrap(C));
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) {
430b57cec5SDimitry Andric   switch (unwrap(C)->getSelectionKind()) {
440b57cec5SDimitry Andric   case Comdat::Any:
450b57cec5SDimitry Andric     return LLVMAnyComdatSelectionKind;
460b57cec5SDimitry Andric   case Comdat::ExactMatch:
470b57cec5SDimitry Andric     return LLVMExactMatchComdatSelectionKind;
480b57cec5SDimitry Andric   case Comdat::Largest:
490b57cec5SDimitry Andric     return LLVMLargestComdatSelectionKind;
50*fe6060f1SDimitry Andric   case Comdat::NoDeduplicate:
51*fe6060f1SDimitry Andric     return LLVMNoDeduplicateComdatSelectionKind;
520b57cec5SDimitry Andric   case Comdat::SameSize:
530b57cec5SDimitry Andric     return LLVMSameSizeComdatSelectionKind;
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric   llvm_unreachable("Invalid Comdat SelectionKind!");
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) {
590b57cec5SDimitry Andric   Comdat *Cd = unwrap(C);
600b57cec5SDimitry Andric   switch (kind) {
610b57cec5SDimitry Andric   case LLVMAnyComdatSelectionKind:
620b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::Any);
630b57cec5SDimitry Andric     break;
640b57cec5SDimitry Andric   case LLVMExactMatchComdatSelectionKind:
650b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::ExactMatch);
660b57cec5SDimitry Andric     break;
670b57cec5SDimitry Andric   case LLVMLargestComdatSelectionKind:
680b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::Largest);
690b57cec5SDimitry Andric     break;
70*fe6060f1SDimitry Andric   case LLVMNoDeduplicateComdatSelectionKind:
71*fe6060f1SDimitry Andric     Cd->setSelectionKind(Comdat::NoDeduplicate);
720b57cec5SDimitry Andric     break;
730b57cec5SDimitry Andric   case LLVMSameSizeComdatSelectionKind:
740b57cec5SDimitry Andric     Cd->setSelectionKind(Comdat::SameSize);
750b57cec5SDimitry Andric     break;
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric }
78