blob: 8cba6b3281a53b9a01b1f320d9af51b02d52eb49 [file] [log] [blame]
Eugene Zelenko1d435522017-02-07 23:02:00 +00001//===- ConstantPools.cpp - ConstantPool class -----------------------------===//
Weiming Zhao8c899732014-06-18 18:17:25 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Weiming Zhao8c899732014-06-18 18:17:25 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the ConstantPool and AssemblerConstantPools classes.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko1d435522017-02-07 23:02:00 +000012
Chandler Carruthd9903882015-01-14 11:23:27 +000013#include "llvm/MC/ConstantPools.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000014#include "llvm/MC/MCContext.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000015#include "llvm/MC/MCDirectives.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCStreamer.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000018#include "llvm/Support/Casting.h"
Weiming Zhao8c899732014-06-18 18:17:25 +000019
20using namespace llvm;
Eugene Zelenko1d435522017-02-07 23:02:00 +000021
Weiming Zhao8c899732014-06-18 18:17:25 +000022//
23// ConstantPool implementation
24//
25// Emit the contents of the constant pool using the provided streamer.
26void ConstantPool::emitEntries(MCStreamer &Streamer) {
27 if (Entries.empty())
28 return;
Weiming Zhao8c899732014-06-18 18:17:25 +000029 Streamer.EmitDataRegion(MCDR_DataRegion);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000030 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);
Weiming Zhao8c899732014-06-18 18:17:25 +000034 }
35 Streamer.EmitDataRegion(MCDR_DataRegionEnd);
36 Entries.clear();
37}
38
David Peixottoae5ba762014-07-18 16:05:14 +000039const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,
Oliver Stannard9327a752015-11-16 16:25:47 +000040 unsigned Size, SMLoc Loc) {
James Molloy5193c802017-05-22 09:42:01 +000041 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
Jim Grosbach6f482002015-05-18 18:43:14 +000048 MCSymbol *CPEntryLabel = Context.createTempSymbol();
Weiming Zhao8c899732014-06-18 18:17:25 +000049
Oliver Stannard9327a752015-11-16 16:25:47 +000050 Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
James Molloy5193c802017-05-22 09:42:01 +000051 const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
52 if (C)
53 CachedEntries[C->getValue()] = SymRef;
54 return SymRef;
Weiming Zhao8c899732014-06-18 18:17:25 +000055}
56
57bool ConstantPool::empty() { return Entries.empty(); }
58
James Molloy6110be92017-05-22 09:42:07 +000059void ConstantPool::clearCache() {
60 CachedEntries.clear();
61}
62
Weiming Zhao8c899732014-06-18 18:17:25 +000063//
64// AssemblerConstantPools implementation
65//
Rafael Espindola0709a7b2015-05-21 19:20:38 +000066ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) {
Weiming Zhao8c899732014-06-18 18:17:25 +000067 ConstantPoolMapTy::iterator CP = ConstantPools.find(Section);
68 if (CP == ConstantPools.end())
69 return nullptr;
70
71 return &CP->second;
72}
73
74ConstantPool &
Rafael Espindola0709a7b2015-05-21 19:20:38 +000075AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) {
Weiming Zhao8c899732014-06-18 18:17:25 +000076 return ConstantPools[Section];
77}
78
Rafael Espindola0709a7b2015-05-21 19:20:38 +000079static void emitConstantPool(MCStreamer &Streamer, MCSection *Section,
Weiming Zhao8c899732014-06-18 18:17:25 +000080 ConstantPool &CP) {
81 if (!CP.empty()) {
82 Streamer.SwitchSection(Section);
83 CP.emitEntries(Streamer);
84 }
85}
86
87void AssemblerConstantPools::emitAll(MCStreamer &Streamer) {
88 // Dump contents of assembler constant pools.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +000089 for (auto &CPI : ConstantPools) {
90 MCSection *Section = CPI.first;
91 ConstantPool &CP = CPI.second;
Weiming Zhao8c899732014-06-18 18:17:25 +000092
93 emitConstantPool(Streamer, Section, CP);
94 }
95}
96
97void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
Eric Christopher445c9522016-10-14 05:47:37 +000098 MCSection *Section = Streamer.getCurrentSectionOnly();
Kristina Brooks12aaf962018-09-03 03:48:39 +000099 if (ConstantPool *CP = getConstantPool(Section))
Weiming Zhao8c899732014-06-18 18:17:25 +0000100 emitConstantPool(Streamer, Section, *CP);
Weiming Zhao8c899732014-06-18 18:17:25 +0000101}
102
James Molloy6110be92017-05-22 09:42:07 +0000103void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
104 MCSection *Section = Streamer.getCurrentSectionOnly();
Kristina Brooks12aaf962018-09-03 03:48:39 +0000105 if (ConstantPool *CP = getConstantPool(Section))
James Molloy6110be92017-05-22 09:42:07 +0000106 CP->clearCache();
James Molloy6110be92017-05-22 09:42:07 +0000107}
108
Weiming Zhao8c899732014-06-18 18:17:25 +0000109const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
David Peixottoae5ba762014-07-18 16:05:14 +0000110 const MCExpr *Expr,
Oliver Stannard9327a752015-11-16 16:25:47 +0000111 unsigned Size, SMLoc Loc) {
Eric Christopher445c9522016-10-14 05:47:37 +0000112 MCSection *Section = Streamer.getCurrentSectionOnly();
David Peixottoae5ba762014-07-18 16:05:14 +0000113 return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(),
Oliver Stannard9327a752015-11-16 16:25:47 +0000114 Size, Loc);
Weiming Zhao8c899732014-06-18 18:17:25 +0000115}