From 6100118a52828c0a824aea5492e4e5f9ec3ce5c5 Mon Sep 17 00:00:00 2001 From: Weiming Zhao Date: Fri, 4 Nov 2016 19:17:32 +0000 Subject: Fix 24560: assembler does not share constant pool for same constants Summary: This patch returns the same label if the CP entry with the same value has been created. Reviewers: eli.friedman, rengolin, jmolloy Subscribers: majnemer, jmolloy, llvm-commits Differential Revision: https://reviews.llvm.org/D25804 llvm-svn: 286006 --- llvm/lib/MC/ConstantPools.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'llvm/lib/MC/ConstantPools.cpp') diff --git a/llvm/lib/MC/ConstantPools.cpp b/llvm/lib/MC/ConstantPools.cpp index c52eec38090..9608c2c656b 100644 --- a/llvm/lib/MC/ConstantPools.cpp +++ b/llvm/lib/MC/ConstantPools.cpp @@ -36,10 +36,20 @@ void ConstantPool::emitEntries(MCStreamer &Streamer) { const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc) { + const MCConstantExpr *C = dyn_cast(Value); + + // Check if there is existing entry for the same constant. If so, reuse it. + auto Itr = C ? CachedEntries.find(C->getValue()) : CachedEntries.end(); + if (Itr != CachedEntries.end()) + return Itr->second; + MCSymbol *CPEntryLabel = Context.createTempSymbol(); Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); - return MCSymbolRefExpr::create(CPEntryLabel, Context); + const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context); + if (C) + CachedEntries[C->getValue()] = SymRef; + return SymRef; } bool ConstantPool::empty() { return Entries.empty(); } -- cgit v1.2.3