LLVM  16.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(Align(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  const MCSymbolRefExpr *S = dyn_cast<MCSymbolRefExpr>(Value);
43 
44  // Check if there is existing entry for the same constant. If so, reuse it.
45  if (C) {
46  auto CItr = CachedConstantEntries.find(C->getValue());
47  if (CItr != CachedConstantEntries.end())
48  return CItr->second;
49  }
50 
51  // Check if there is existing entry for the same symbol. If so, reuse it.
52  if (S) {
53  auto SItr = CachedSymbolEntries.find(&(S->getSymbol()));
54  if (SItr != CachedSymbolEntries.end())
55  return SItr->second;
56  }
57 
58  MCSymbol *CPEntryLabel = Context.createTempSymbol();
59 
60  Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
61  const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
62  if (C)
63  CachedConstantEntries[C->getValue()] = SymRef;
64  if (S)
65  CachedSymbolEntries[&(S->getSymbol())] = SymRef;
66  return SymRef;
67 }
68 
69 bool ConstantPool::empty() { return Entries.empty(); }
70 
72  CachedConstantEntries.clear();
73  CachedSymbolEntries.clear();
74 }
75 
76 //
77 // AssemblerConstantPools implementation
78 //
79 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
80  ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
81  if (CP == ConstantPools.end())
82  return nullptr;
83 
84  return &CP->second;
85 }
86 
88 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
89  return ConstantPools[Section];
90 }
91 
92 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
93  ConstantPool &CP) {
94  if (!CP.empty()) {
95  Streamer.switchSection(Section);
96  CP.emitEntries(Streamer);
97  }
98 }
99 
101  // Dump contents of assembler constant pools.
102  for (auto &CPI : ConstantPools) {
103  MCSection *Section = CPI.first;
104  ConstantPool &CP = CPI.second;
105 
106  emitConstantPool(Streamer, Section, CP);
107  }
108 }
109 
111  MCSection *Section = Streamer.getCurrentSectionOnly();
112  if (ConstantPool *CP = getConstantPool(Section))
113  emitConstantPool(Streamer, Section, *CP);
114 }
115 
117  MCSection *Section = Streamer.getCurrentSectionOnly();
118  if (ConstantPool *CP = getConstantPool(Section))
119  CP->clearCache();
120 }
121 
123  const MCExpr *Expr,
124  unsigned Size, SMLoc Loc) {
125  MCSection *Section = Streamer.getCurrentSectionOnly();
126  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
127  Size, Loc);
128 }
llvm::MapVector< MCSection *, ConstantPool >::iterator
typename std::vector< std::pair< MCSection *, ConstantPool >> ::iterator iterator
Definition: MapVector.h:50
llvm::MCStreamer::emitDataRegion
virtual void emitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:499
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:76
MCDirectives.h
llvm::AssemblerConstantPools::clearCacheForCurrentSection
void clearCacheForCurrentSection(MCStreamer &Streamer)
Definition: ConstantPools.cpp:116
emitConstantPool
static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, ConstantPool &CP)
Definition: ConstantPools.cpp:92
llvm::ConstantPool
Definition: ConstantPools.h:43
llvm::MCStreamer::emitValue
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:178
llvm::AssemblerConstantPools::addEntry
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size, SMLoc Loc)
Definition: ConstantPools.cpp:122
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:212
llvm::MCDR_DataRegion
@ MCDR_DataRegion
.data_region
Definition: MCDirectives.h:61
llvm::ConstantPool::empty
bool empty()
Definition: ConstantPools.cpp:69
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:421
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:1232
llvm::ConstantPool::emitEntries
void emitEntries(MCStreamer &Streamer)
Definition: ConstantPools.cpp:26
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::MCStreamer::getCurrentSectionOnly
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:389
llvm::MCConstantExpr
Definition: MCExpr.h:144
llvm::AssemblerConstantPools::emitAll
void emitAll(MCStreamer &Streamer)
Definition: ConstantPools.cpp:100
ConstantPools.h
llvm::MapVector::find
iterator find(const KeyT &Key)
Definition: MapVector.h:148
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
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:110
llvm::MCStreamer::emitValueToAlignment
virtual void emitValueToAlignment(Align Alignment, 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:1220
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MapVector::end
iterator end()
Definition: MapVector.h:72
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:386
llvm::ConstantPool::clearCache
void clearCache()
Definition: ConstantPools.cpp:71
llvm::MCStreamer::getContext
MCContext & getContext() const
Definition: MCStreamer.h:293
MCStreamer.h
llvm::MCDR_DataRegionEnd
@ MCDR_DataRegionEnd
.end_data_region
Definition: MCDirectives.h:65
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