ConstantPools.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(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. // Check if there is existing entry for the same constant. If so, reuse it.
  39. auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
  40. if (Itr != CachedEntries.end())
  41. return Itr->second;
  42. MCSymbol *CPEntryLabel = Context.createTempSymbol();
  43. Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
  44. const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
  45. if (C)
  46. CachedEntries[C->getValue()] = SymRef;
  47. return SymRef;
  48. }
  49. bool ConstantPool::empty() { return Entries.empty(); }
  50. void ConstantPool::clearCache() {
  51. CachedEntries.clear();
  52. }
  53. //
  54. // AssemblerConstantPools implementation
  55. //
  56. ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
  57. ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
  58. if (CP == ConstantPools.end())
  59. return nullptr;
  60. return &CP->second;
  61. }
  62. ConstantPool &
  63. AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
  64. return ConstantPools[Section];
  65. }
  66. static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
  67. ConstantPool &CP) {
  68. if (!CP.empty()) {
  69. Streamer.SwitchSection(Section);
  70. CP.emitEntries(Streamer);
  71. }
  72. }
  73. void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
  74. // Dump contents of assembler constant pools.
  75. for (auto &CPI : ConstantPools) {
  76. MCSection *Section = CPI.first;
  77. ConstantPool &CP = CPI.second;
  78. emitConstantPool(Streamer, Section, CP);
  79. }
  80. }
  81. void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
  82. MCSection *Section = Streamer.getCurrentSectionOnly();
  83. if (ConstantPool *CP = getConstantPool(Section))
  84. emitConstantPool(Streamer, Section, *CP);
  85. }
  86. void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
  87. MCSection *Section = Streamer.getCurrentSectionOnly();
  88. if (ConstantPool *CP = getConstantPool(Section))
  89. CP->clearCache();
  90. }
  91. const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
  92. const MCExpr *Expr,
  93. unsigned Size, SMLoc Loc) {
  94. MCSection *Section = Streamer.getCurrentSectionOnly();
  95. return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
  96. Size, Loc);
  97. }