ConstantPools.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===- ConstantPools.cpp - ConstantPool class -----------------------------===//
  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 ConstantPool and AssemblerConstantPools classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/MC/ConstantPools.h"
  13. #include "llvm/MC/MCContext.h"
  14. #include "llvm/MC/MCDirectives.h"
  15. #include "llvm/MC/MCExpr.h"
  16. #include "llvm/MC/MCStreamer.h"
  17. #include "llvm/Support/Casting.h"
  18. using namespace llvm;
  19. //
  20. // ConstantPool implementation
  21. //
  22. // Emit the contents of the constant pool using the provided streamer.
  23. void ConstantPool::emitEntries(MCStreamer &Streamer) {
  24. if (Entries.empty())
  25. return;
  26. Streamer.emitDataRegion(MCDR_DataRegion);
  27. for (const ConstantPoolEntry &Entry : Entries) {
  28. Streamer.emitValueToAlignment(Align(Entry.Size)); // align naturally
  29. Streamer.emitLabel(Entry.Label);
  30. Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);
  31. }
  32. Streamer.emitDataRegion(MCDR_DataRegionEnd);
  33. Entries.clear();
  34. }
  35. const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
  36. unsigned Size, SMLoc Loc) {
  37. const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
  38. const MCSymbolRefExpr *S = dyn_cast<MCSymbolRefExpr>(Value);
  39. // Check if there is existing entry for the same constant. If so, reuse it.
  40. if (C) {
  41. auto CItr = CachedConstantEntries.find(C->getValue());
  42. if (CItr != CachedConstantEntries.end())
  43. return CItr->second;
  44. }
  45. // Check if there is existing entry for the same symbol. If so, reuse it.
  46. if (S) {
  47. auto SItr = CachedSymbolEntries.find(&(S->getSymbol()));
  48. if (SItr != CachedSymbolEntries.end())
  49. return SItr->second;
  50. }
  51. MCSymbol *CPEntryLabel = Context.createTempSymbol();
  52. Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
  53. const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
  54. if (C)
  55. CachedConstantEntries[C->getValue()] = SymRef;
  56. if (S)
  57. CachedSymbolEntries[&(S->getSymbol())] = SymRef;
  58. return SymRef;
  59. }
  60. bool ConstantPool::empty() { return Entries.empty(); }
  61. void ConstantPool::clearCache() {
  62. CachedConstantEntries.clear();
  63. CachedSymbolEntries.clear();
  64. }
  65. //
  66. // AssemblerConstantPools implementation
  67. //
  68. ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
  69. ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
  70. if (CP == ConstantPools.end())
  71. return nullptr;
  72. return &CP->second;
  73. }
  74. ConstantPool &
  75. AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
  76. return ConstantPools[Section];
  77. }
  78. static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
  79. ConstantPool &CP) {
  80. if (!CP.empty()) {
  81. Streamer.switchSection(Section);
  82. CP.emitEntries(Streamer);
  83. }
  84. }
  85. void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
  86. // Dump contents of assembler constant pools.
  87. for (auto &CPI : ConstantPools) {
  88. MCSection *Section = CPI.first;
  89. ConstantPool &CP = CPI.second;
  90. emitConstantPool(Streamer, Section, CP);
  91. }
  92. }
  93. void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
  94. MCSection *Section = Streamer.getCurrentSectionOnly();
  95. if (ConstantPool *CP = getConstantPool(Section))
  96. emitConstantPool(Streamer, Section, *CP);
  97. }
  98. void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
  99. MCSection *Section = Streamer.getCurrentSectionOnly();
  100. if (ConstantPool *CP = getConstantPool(Section))
  101. CP->clearCache();
  102. }
  103. const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
  104. const MCExpr *Expr,
  105. unsigned Size, SMLoc Loc) {
  106. MCSection *Section = Streamer.getCurrentSectionOnly();
  107. return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
  108. Size, Loc);
  109. }