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 22 using namespace llvm; 23 24 Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {} 25 26 Comdat::Comdat() = default; 27 28 StringRef Comdat::getName() const { return Name->first(); } 29 30 void Comdat::addUser(GlobalObject *GO) { Users.insert(GO); } 31 32 void Comdat::removeUser(GlobalObject *GO) { Users.erase(GO); } 33 34 LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) { 35 return wrap(unwrap(M)->getOrInsertComdat(Name)); 36 } 37 38 LLVMComdatRef LLVMGetComdat(LLVMValueRef V) { 39 GlobalObject *G = unwrap<GlobalObject>(V); 40 return wrap(G->getComdat()); 41 } 42 43 void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) { 44 GlobalObject *G = unwrap<GlobalObject>(V); 45 G->setComdat(unwrap(C)); 46 } 47 48 LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) { 49 switch (unwrap(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 64 void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) { 65 Comdat *Cd = unwrap(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