LLVM  14.0.0git
ConstantPools.cpp
Go to the documentation of this file.
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 
13 #include "llvm/MC/ConstantPools.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/Support/Casting.h"
19 
20 using namespace llvm;
21 
22 //
23 // ConstantPool implementation
24 //
25 // Emit the contents of the constant pool using the provided streamer.
27  if (Entries.empty())
28  return;
30  for (const ConstantPoolEntry &Entry : Entries) {
31  Streamer.emitValueToAlignment(Entry.Size); // align naturally
32  Streamer.emitLabel(Entry.Label);
33  Streamer.emitValue(Entry.Value, Entry.Size, Entry.Loc);
34  }
36  Entries.clear();
37 }
38 
40  unsigned Size, SMLoc Loc) {
41  const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
42 
43  // Check if there is existing entry for the same constant. If so, reuse it.
44  auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
45  if (Itr != CachedEntries.end())
46  return Itr->second;
47 
48  MCSymbol *CPEntryLabel = Context.createTempSymbol();
49 
50  Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
51  const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
52  if (C)
53  CachedEntries[C->getValue()] = SymRef;
54  return SymRef;
55 }
56 
57 bool ConstantPool::empty() { return Entries.empty(); }
58 
60  CachedEntries.clear();
61 }
62 
63 //
64 // AssemblerConstantPools implementation
65 //
66 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
67  ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
68  if (CP == ConstantPools.end())
69  return nullptr;
70 
71  return &CP->second;
72 }
73 
75 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
76  return ConstantPools[Section];
77 }
78 
79 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
80  ConstantPool &CP) {
81  if (!CP.empty()) {
82  Streamer.SwitchSection(Section);
83  CP.emitEntries(Streamer);
84  }
85 }
86 
88  // Dump contents of assembler constant pools.
89  for (auto &CPI : ConstantPools) {
90  MCSection *Section = CPI.first;
91  ConstantPool &CP = CPI.second;
92 
93  emitConstantPool(Streamer, Section, CP);
94  }
95 }
96 
99  if (ConstantPool *CP = getConstantPool(Section))
100  emitConstantPool(Streamer, Section, *CP);
101 }
102 
105  if (ConstantPool *CP = getConstantPool(Section))
106  CP->clearCache();
107 }
108 
110  const MCExpr *Expr,
111  unsigned Size, SMLoc Loc) {
113  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
114  Size, Loc);
115 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::MapVector< MCSection *, ConstantPool >::iterator
typename std::vector< std::pair< MCSection *, ConstantPool >> ::iterator iterator
Definition: MapVector.h:49
llvm::MCStreamer::emitDataRegion
virtual void emitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:486
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::ConstantPoolEntry
Definition: ConstantPools.h:31
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
MCDirectives.h
llvm::AssemblerConstantPools::clearCacheForCurrentSection
void clearCacheForCurrentSection(MCStreamer &Streamer)
Definition: ConstantPools.cpp:103
emitConstantPool
static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, ConstantPool &CP)
Definition: ConstantPools.cpp:79
llvm::ConstantPool
Definition: ConstantPools.h:43
llvm::MCStreamer::emitValue
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:177
llvm::AssemblerConstantPools::addEntry
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size, SMLoc Loc)
Definition: ConstantPools.cpp:109
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::MCDR_DataRegion
@ MCDR_DataRegion
.data_region
Definition: MCDirectives.h:59
llvm::ConstantPool::empty
bool empty()
Definition: ConstantPools.cpp:57
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
MCContext.h
llvm::MCStreamer::emitLabel
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:415
llvm::ConstantPool::emitEntries
void emitEntries(MCStreamer &Streamer)
Definition: ConstantPools.cpp:26
llvm::MCStreamer::getCurrentSectionOnly
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:376
llvm::MCConstantExpr
Definition: MCExpr.h:144
llvm::AssemblerConstantPools::emitAll
void emitAll(MCStreamer &Streamer)
Definition: ConstantPools.cpp:87
ConstantPools.h
llvm::MapVector::find
iterator find(const KeyT &Key)
Definition: MapVector.h:147
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::AssemblerConstantPools::emitForCurrentSection
void emitForCurrentSection(MCStreamer &Streamer)
Definition: ConstantPools.cpp:97
llvm::MapVector::end
iterator end()
Definition: MapVector.h:71
llvm::HexagonISD::CP
@ CP
Definition: HexagonISelLowering.h:53
Casting.h
llvm::ConstantPool::addEntry
const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc)
Definition: ConstantPools.cpp:39
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:385
llvm::ConstantPool::clearCache
void clearCache()
Definition: ConstantPools.cpp:59
llvm::MCStreamer::getContext
MCContext & getContext() const
Definition: MCStreamer.h:280
MCStreamer.h
llvm::MCStreamer::emitValueToAlignment
virtual void emitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCStreamer.cpp:1199
llvm::MCDR_DataRegionEnd
@ MCDR_DataRegionEnd
.end_data_region
Definition: MCDirectives.h:63
llvm::MCStreamer::SwitchSection
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:1212
MCExpr.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35