LLVM  6.0.0svn
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 }
104 
106  MCSection *Section = Streamer.getCurrentSectionOnly();
107  if (ConstantPool *CP = getConstantPool(Section)) {
108  CP->clearCache();
109  }
110 }
111 
113  const MCExpr *Expr,
114  unsigned Size, SMLoc Loc) {
115  MCSection *Section = Streamer.getCurrentSectionOnly();
116  return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
117  Size, Loc);
118 }
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void push_back(const T &Elt)
Definition: SmallVector.h:212
LLVMContext & Context
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
Compute iterated dominance frontiers using a linear time algorithm.
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
MCContext & getContext() const
Definition: MCStreamer.h:234
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:59
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:123
int64_t getValue() const
Definition: MCExpr.h:151
Streaming machine code generation interface.
Definition: MCStreamer.h:169
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:215
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:937
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:422
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.
Definition: MCStreamer.cpp:928
void emitEntries(MCStreamer &Streamer)
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:316
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
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
LLVM Value Representation.
Definition: Value.h:73
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:300
void clearCacheForCurrentSection(MCStreamer &Streamer)
Represents a location in source code.
Definition: SMLoc.h:24
.end_data_region
Definition: MCDirectives.h:61