Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1 | //===- Comdat.cpp - Implement Metadata classes ----------------------------===// |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Reid Kleckner | e66458a | 2018-03-14 18:33:53 +0000 | [diff] [blame] | 9 | // This file implements the Comdat class (including the C bindings). |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Reid Kleckner | e66458a | 2018-03-14 18:33:53 +0000 | [diff] [blame] | 13 | #include "llvm-c/Comdat.h" |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringMap.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
Reid Kleckner | e66458a | 2018-03-14 18:33:53 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Comdat.h" |
| 17 | #include "llvm/IR/GlobalObject.h" |
| 18 | #include "llvm/IR/Module.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 19 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 22 | Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {} |
| 23 | |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 24 | Comdat::Comdat() = default; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 25 | |
| 26 | StringRef Comdat::getName() const { return Name->first(); } |
Reid Kleckner | e66458a | 2018-03-14 18:33:53 +0000 | [diff] [blame] | 27 | |
| 28 | LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) { |
| 29 | return wrap(unwrap(M)->getOrInsertComdat(Name)); |
| 30 | } |
| 31 | |
| 32 | LLVMComdatRef LLVMGetComdat(LLVMValueRef V) { |
| 33 | GlobalObject *G = unwrap<GlobalObject>(V); |
| 34 | return wrap(G->getComdat()); |
| 35 | } |
| 36 | |
| 37 | void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) { |
| 38 | GlobalObject *G = unwrap<GlobalObject>(V); |
| 39 | G->setComdat(unwrap(C)); |
| 40 | } |
| 41 | |
| 42 | LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) { |
| 43 | switch (unwrap(C)->getSelectionKind()) { |
| 44 | case Comdat::Any: |
| 45 | return LLVMAnyComdatSelectionKind; |
| 46 | case Comdat::ExactMatch: |
| 47 | return LLVMExactMatchComdatSelectionKind; |
| 48 | case Comdat::Largest: |
| 49 | return LLVMLargestComdatSelectionKind; |
| 50 | case Comdat::NoDuplicates: |
| 51 | return LLVMNoDuplicatesComdatSelectionKind; |
| 52 | case Comdat::SameSize: |
| 53 | return LLVMSameSizeComdatSelectionKind; |
| 54 | } |
| 55 | llvm_unreachable("Invalid Comdat SelectionKind!"); |
| 56 | } |
| 57 | |
| 58 | void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) { |
| 59 | Comdat *Cd = unwrap(C); |
| 60 | switch (kind) { |
| 61 | case LLVMAnyComdatSelectionKind: |
| 62 | Cd->setSelectionKind(Comdat::Any); |
| 63 | break; |
| 64 | case LLVMExactMatchComdatSelectionKind: |
| 65 | Cd->setSelectionKind(Comdat::ExactMatch); |
| 66 | break; |
| 67 | case LLVMLargestComdatSelectionKind: |
| 68 | Cd->setSelectionKind(Comdat::Largest); |
| 69 | break; |
| 70 | case LLVMNoDuplicatesComdatSelectionKind: |
| 71 | Cd->setSelectionKind(Comdat::NoDuplicates); |
| 72 | break; |
| 73 | case LLVMSameSizeComdatSelectionKind: |
| 74 | Cd->setSelectionKind(Comdat::SameSize); |
| 75 | break; |
| 76 | } |
| 77 | } |