Comdat.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "llvm-c/Comdat.h"
  13. #include "llvm/ADT/StringMap.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/IR/Comdat.h"
  16. #include "llvm/IR/GlobalObject.h"
  17. #include "llvm/IR/Module.h"
  18. using namespace llvm;
  19. Comdat::Comdat(Comdat &&C) : Name(C.Name), SK(C.SK) {}
  20. Comdat::Comdat() = default;
  21. StringRef Comdat::getName() const { return Name->first(); }
  22. LLVMComdatRef LLVMGetOrInsertComdat(LLVMModuleRef M, const char *Name) {
  23. return wrap(unwrap(M)->getOrInsertComdat(Name));
  24. }
  25. LLVMComdatRef LLVMGetComdat(LLVMValueRef V) {
  26. GlobalObject *G = unwrap<GlobalObject>(V);
  27. return wrap(G->getComdat());
  28. }
  29. void LLVMSetComdat(LLVMValueRef V, LLVMComdatRef C) {
  30. GlobalObject *G = unwrap<GlobalObject>(V);
  31. G->setComdat(unwrap(C));
  32. }
  33. LLVMComdatSelectionKind LLVMGetComdatSelectionKind(LLVMComdatRef C) {
  34. switch (unwrap(C)->getSelectionKind()) {
  35. case Comdat::Any:
  36. return LLVMAnyComdatSelectionKind;
  37. case Comdat::ExactMatch:
  38. return LLVMExactMatchComdatSelectionKind;
  39. case Comdat::Largest:
  40. return LLVMLargestComdatSelectionKind;
  41. case Comdat::NoDuplicates:
  42. return LLVMNoDuplicatesComdatSelectionKind;
  43. case Comdat::SameSize:
  44. return LLVMSameSizeComdatSelectionKind;
  45. }
  46. llvm_unreachable("Invalid Comdat SelectionKind!");
  47. }
  48. void LLVMSetComdatSelectionKind(LLVMComdatRef C, LLVMComdatSelectionKind kind) {
  49. Comdat *Cd = unwrap(C);
  50. switch (kind) {
  51. case LLVMAnyComdatSelectionKind:
  52. Cd->setSelectionKind(Comdat::Any);
  53. break;
  54. case LLVMExactMatchComdatSelectionKind:
  55. Cd->setSelectionKind(Comdat::ExactMatch);
  56. break;
  57. case LLVMLargestComdatSelectionKind:
  58. Cd->setSelectionKind(Comdat::Largest);
  59. break;
  60. case LLVMNoDuplicatesComdatSelectionKind:
  61. Cd->setSelectionKind(Comdat::NoDuplicates);
  62. break;
  63. case LLVMSameSizeComdatSelectionKind:
  64. Cd->setSelectionKind(Comdat::SameSize);
  65. break;
  66. }
  67. }