Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 1 | //===- ConstantPools.cpp - ConstantPool class -----------------------------===// |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the ConstantPool and AssemblerConstantPools classes. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 12 | |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 13 | #include "llvm/MC/ConstantPools.h" |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCDirectives.h" |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Casting.h" |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
Eugene Zelenko | 1d43552 | 2017-02-07 23:02:00 +0000 | [diff] [blame] | 21 | |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 22 | // |
| 23 | // ConstantPool implementation |
| 24 | // |
| 25 | // Emit the contents of the constant pool using the provided streamer. |
| 26 | void ConstantPool::emitEntries(MCStreamer &Streamer) { |
| 27 | if (Entries.empty()) |
| 28 | return; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 29 | Streamer.EmitDataRegion(MCDR_DataRegion); |
Benjamin Kramer | 7b4658f | 2016-06-26 14:49:00 +0000 | [diff] [blame] | 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); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 34 | } |
| 35 | Streamer.EmitDataRegion(MCDR_DataRegionEnd); |
| 36 | Entries.clear(); |
| 37 | } |
| 38 | |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 39 | const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 40 | unsigned Size, SMLoc Loc) { |
James Molloy | 5193c80 | 2017-05-22 09:42:01 +0000 | [diff] [blame] | 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 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 48 | MCSymbol *CPEntryLabel = Context.createTempSymbol(); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 49 | |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 50 | Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); |
James Molloy | 5193c80 | 2017-05-22 09:42:01 +0000 | [diff] [blame] | 51 | const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context); |
| 52 | if (C) |
| 53 | CachedEntries[C->getValue()] = SymRef; |
| 54 | return SymRef; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool ConstantPool::empty() { return Entries.empty(); } |
| 58 | |
James Molloy | 6110be9 | 2017-05-22 09:42:07 +0000 | [diff] [blame] | 59 | void ConstantPool::clearCache() { |
| 60 | CachedEntries.clear(); |
| 61 | } |
| 62 | |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 63 | // |
| 64 | // AssemblerConstantPools implementation |
| 65 | // |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 66 | ConstantPool *AssemblerConstantPools::getConstantPool(MCSection *Section) { |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 67 | ConstantPoolMapTy::iterator CP = ConstantPools.find(Section); |
| 68 | if (CP == ConstantPools.end()) |
| 69 | return nullptr; |
| 70 | |
| 71 | return &CP->second; |
| 72 | } |
| 73 | |
| 74 | ConstantPool & |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 75 | AssemblerConstantPools::getOrCreateConstantPool(MCSection *Section) { |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 76 | return ConstantPools[Section]; |
| 77 | } |
| 78 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 79 | static void emitConstantPool(MCStreamer &Streamer, MCSection *Section, |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 80 | ConstantPool &CP) { |
| 81 | if (!CP.empty()) { |
| 82 | Streamer.SwitchSection(Section); |
| 83 | CP.emitEntries(Streamer); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void AssemblerConstantPools::emitAll(MCStreamer &Streamer) { |
| 88 | // Dump contents of assembler constant pools. |
Benjamin Kramer | 7b4658f | 2016-06-26 14:49:00 +0000 | [diff] [blame] | 89 | for (auto &CPI : ConstantPools) { |
| 90 | MCSection *Section = CPI.first; |
| 91 | ConstantPool &CP = CPI.second; |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 92 | |
| 93 | emitConstantPool(Streamer, Section, CP); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 98 | MCSection *Section = Streamer.getCurrentSectionOnly(); |
Kristina Brooks | 12aaf96 | 2018-09-03 03:48:39 +0000 | [diff] [blame] | 99 | if (ConstantPool *CP = getConstantPool(Section)) |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 100 | emitConstantPool(Streamer, Section, *CP); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
James Molloy | 6110be9 | 2017-05-22 09:42:07 +0000 | [diff] [blame] | 103 | void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) { |
| 104 | MCSection *Section = Streamer.getCurrentSectionOnly(); |
Kristina Brooks | 12aaf96 | 2018-09-03 03:48:39 +0000 | [diff] [blame] | 105 | if (ConstantPool *CP = getConstantPool(Section)) |
James Molloy | 6110be9 | 2017-05-22 09:42:07 +0000 | [diff] [blame] | 106 | CP->clearCache(); |
James Molloy | 6110be9 | 2017-05-22 09:42:07 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 109 | const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer, |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 110 | const MCExpr *Expr, |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 111 | unsigned Size, SMLoc Loc) { |
Eric Christopher | 445c952 | 2016-10-14 05:47:37 +0000 | [diff] [blame] | 112 | MCSection *Section = Streamer.getCurrentSectionOnly(); |
David Peixotto | ae5ba76 | 2014-07-18 16:05:14 +0000 | [diff] [blame] | 113 | return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(), |
Oliver Stannard | 9327a75 | 2015-11-16 16:25:47 +0000 | [diff] [blame] | 114 | Size, Loc); |
Weiming Zhao | 8c89973 | 2014-06-18 18:17:25 +0000 | [diff] [blame] | 115 | } |