LLVM  8.0.1
ConstantPools.cpp
Go to the documentation of this file.
1 //===- ConstantPools.cpp - ConstantPool class -----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ConstantPool and AssemblerConstantPools classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/MC/ConstantPools.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Support/Casting.h"
20 
21 using namespace llvm;
22 
23 //
24 // ConstantPool implementation
25 //
26 // Emit the contents of the constant pool using the provided streamer.
28  if (Entries.empty())
29  return;
31  for (const ConstantPoolEntry &Entry : Entries) {
32  Streamer.EmitCodeAlignment(Entry.Size); // align naturally
33  Streamer.EmitLabel(Entry.Label);
34  Streamer.EmitValue(Entry.Value, Entry.Size, Entry.Loc);
35  }
37  Entries.clear();
38 }
39 
41  unsigned Size, SMLoc Loc) {
42  const MCConstantExpr *C = dyn_cast<MCConstantExpr>(Value);
43 
44  // Check if there is existing entry for the same constant. If so, reuse it.
45  auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end();
46  if (Itr != CachedEntries.end())
47  return Itr->second;
48 
49  MCSymbol *CPEntryLabel = Context.createTempSymbol();
50 
51  Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
52  const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
53  if (C)
54  CachedEntries[C->getValue()] = SymRef;
55  return SymRef;
56 }
57 
58 bool ConstantPool::empty() { return Entries.empty(); }
59 
61  CachedEntries.clear();
62 }
63 
64 //
65 // AssemblerConstantPools implementation
66 //
67 ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
68  ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
69  if (CP == ConstantPools.end())
70  return nullptr;
71 
72  return &CP->second;
73 }
74 
76 AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
77  return ConstantPools[Section];
78 }
79 
80 static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
81  ConstantPool &CP) {
82  if (!CP.empty()) {
83  Streamer.SwitchSection(Section);
84  CP.emitEntries(Streamer);
85  }
86 }
87 
89  // Dump contents of assembler constant pools.
90  for (auto &CPI : ConstantPools) {
91  MCSection *Section = CPI.first;
92  ConstantPool &CP = CPI.second;
93 
94  emitConstantPool(Streamer, Section, CP);
95  }
96 }
97 
99  MCSection *Section = Streamer.getCurrentSectionOnly();
100  if (ConstantPool *CP = getConstantPool(Section))
101  emitConstantPool(Streamer, Section, *CP);
102 }
103 
105  MCSection *Section = Streamer.getCurrentSectionOnly();
106  if (ConstantPool *CP = getConstantPool(Section))
107  CP->clearCache();
108 }
109 
111  const MCExpr *Expr,
112  unsigned Size, SMLoc Loc) {
113  MCSection *Section = Streamer.getCurrentSectionOnly();
114  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
115  Size, Loc);
116 }
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
LLVMContext & Context
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:323
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
void push_back(const T &Elt)
Definition: SmallVector.h:218
MCContext & getContext() const
Definition: MCStreamer.h:251
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
Context object for machine code objects.
Definition: MCContext.h:63
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:155
int64_t getValue() const
Definition: MCExpr.h:152
Streaming machine code generation interface.
Definition: MCStreamer.h:189
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:217
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:452
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:346
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
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:323
uint32_t Size
Definition: Profile.cpp:47
LLVM Value Representation.
Definition: Value.h:73
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:347
void clearCacheForCurrentSection(MCStreamer &Streamer)
Represents a location in source code.
Definition: SMLoc.h:24
.end_data_region
Definition: MCDirectives.h:61