Comdat.cpp 2.5 KB

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