LLVM 20.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
14#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCStreamer.h"
19
20using 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(std::make_pair(C->getValue(), Size));
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 =
54 CachedSymbolEntries.find(std::make_pair(&(S->getSymbol()), Size));
55 if (SItr != CachedSymbolEntries.end())
56 return SItr->second;
57 }
58
59 MCSymbol *CPEntryLabel = Context.createTempSymbol();
60
61 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
62 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
63 if (C)
64 CachedConstantEntries[std::make_pair(C->getValue(), Size)] = SymRef;
65 if (S)
66 CachedSymbolEntries[std::make_pair(&(S->getSymbol()), Size)] = SymRef;
67 return SymRef;
68}
69
70bool ConstantPool::empty() { return Entries.empty(); }
71
73 CachedConstantEntries.clear();
74 CachedSymbolEntries.clear();
75}
76
77//
78// AssemblerConstantPools implementation
79//
80ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
81 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
82 if (CP == ConstantPools.end())
83 return nullptr;
84
85 return &CP->second;
86}
87
89AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
90 return ConstantPools[Section];
91}
92
93static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
94 ConstantPool &CP) {
95 if (!CP.empty()) {
96 Streamer.switchSection(Section);
97 CP.emitEntries(Streamer);
98 }
99}
100
102 // Dump contents of assembler constant pools.
103 for (auto &CPI : ConstantPools) {
104 MCSection *Section = CPI.first;
105 ConstantPool &CP = CPI.second;
106
107 emitConstantPool(Streamer, Section, CP);
108 }
109}
110
112 MCSection *Section = Streamer.getCurrentSectionOnly();
113 if (ConstantPool *CP = getConstantPool(Section))
114 emitConstantPool(Streamer, Section, *CP);
115}
116
118 MCSection *Section = Streamer.getCurrentSectionOnly();
119 if (ConstantPool *CP = getConstantPool(Section))
120 CP->clearCache();
121}
122
124 const MCExpr *Expr,
125 unsigned Size, SMLoc Loc) {
126 MCSection *Section = Streamer.getCurrentSectionOnly();
127 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
128 Size, Loc);
129}
static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, ConstantPool &CP)
uint64_t Size
void emitAll(MCStreamer &Streamer)
void clearCacheForCurrentSection(MCStreamer &Streamer)
void emitForCurrentSection(MCStreamer &Streamer)
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size, SMLoc Loc)
const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc)
void emitEntries(MCStreamer &Streamer)
Context object for machine code objects.
Definition: MCContext.h:83
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:345
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCContext & getContext() const
Definition: MCStreamer.h:300
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:179
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:420
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.
virtual void emitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:470
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:400
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:411
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:398
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
typename VectorType::iterator iterator
Definition: MapVector.h:49
iterator end()
Definition: MapVector.h:71
iterator find(const KeyT &Key)
Definition: MapVector.h:167
Represents a location in source code.
Definition: SMLoc.h:23
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ MCDR_DataRegionEnd
.end_data_region
Definition: MCDirectives.h:66
@ MCDR_DataRegion
.data_region
Definition: MCDirectives.h:62
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39