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