LLVM  9.0.0svn
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.EmitCodeAlignment(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 
98  MCSection *Section = Streamer.getCurrentSectionOnly();
99  if (ConstantPool *CP = getConstantPool(Section))
100  emitConstantPool(Streamer, Section, *CP);
101 }
102 
104  MCSection *Section = Streamer.getCurrentSectionOnly();
105  if (ConstantPool *CP = getConstantPool(Section))
106  CP->clearCache();
107 }
108 
110  const MCExpr *Expr,
111  unsigned Size, SMLoc Loc) {
112  MCSection *Section = Streamer.getCurrentSectionOnly();
113  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
114  Size, Loc);
115 }
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
LLVMContext & Context
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:321
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void push_back(const T &Elt)
Definition: SmallVector.h:211
MCContext & getContext() const
Definition: MCStreamer.h:250
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Context object for machine code objects.
Definition: MCContext.h:62
const MCExpr * addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc)
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:159
int64_t getValue() const
Definition: MCExpr.h:151
Streaming machine code generation interface.
Definition: MCStreamer.h:188
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:220
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
void emitForCurrentSection(MCStreamer &Streamer)
const MCExpr * addEntry(MCStreamer &Streamer, const MCExpr *Expr, unsigned Size, SMLoc Loc)
virtual void EmitDataRegion(MCDataRegionType Kind)
Note in the output the specified region Kind.
Definition: MCStreamer.h:449
void emitAll(MCStreamer &Streamer)
static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, ConstantPool &CP)
virtual void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit=0)
Emit nops until the byte alignment ByteAlignment is reached.
void emitEntries(MCStreamer &Streamer)
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:343
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
LLVM Value Representation.
Definition: Value.h:72
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:351
void clearCacheForCurrentSection(MCStreamer &Streamer)
Represents a location in source code.
Definition: SMLoc.h:23
.end_data_region
Definition: MCDirectives.h:61