diff --git a/llvm/include/llvm/MC/ConstantPools.h b/llvm/include/llvm/MC/ConstantPools.h index 9fe0cce8d68cb..7eac75362effd 100644 --- a/llvm/include/llvm/MC/ConstantPools.h +++ b/llvm/include/llvm/MC/ConstantPools.h @@ -43,7 +43,8 @@ struct ConstantPoolEntry { class ConstantPool { using EntryVecTy = SmallVector; EntryVecTy Entries; - std::map CachedEntries; + std::map CachedConstantEntries; + DenseMap CachedSymbolEntries; public: // Initialize a new empty constant pool diff --git a/llvm/lib/MC/ConstantPools.cpp b/llvm/lib/MC/ConstantPools.cpp index d8a08a4bd4390..476676953a374 100644 --- a/llvm/lib/MC/ConstantPools.cpp +++ b/llvm/lib/MC/ConstantPools.cpp @@ -39,25 +39,38 @@ void ConstantPool::emitEntries(MCStreamer &Streamer) { const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, unsigned Size, SMLoc Loc) { const MCConstantExpr *C = dyn_cast(Value); + const MCSymbolRefExpr *S = 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; + if (C) { + auto CItr = CachedConstantEntries.find(C->getValue()); + if (CItr != CachedConstantEntries.end()) + return CItr->second; + } + + // Check if there is existing entry for the same symbol. If so, reuse it. + if (S) { + auto SItr = CachedSymbolEntries.find(&(S->getSymbol())); + if (SItr != CachedSymbolEntries.end()) + return SItr->second; + } MCSymbol *CPEntryLabel = Context.createTempSymbol(); Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context); if (C) - CachedEntries[C->getValue()] = SymRef; + CachedConstantEntries[C->getValue()] = SymRef; + if (S) + CachedSymbolEntries[&(S->getSymbol())] = SymRef; return SymRef; } bool ConstantPool::empty() { return Entries.empty(); } void ConstantPool::clearCache() { - CachedEntries.clear(); + CachedConstantEntries.clear(); + CachedSymbolEntries.clear(); } // diff --git a/llvm/test/MC/ARM/ldr-pseudo-wide.s b/llvm/test/MC/ARM/ldr-pseudo-wide.s index b4deca58a6d69..c059ee024cff0 100644 --- a/llvm/test/MC/ARM/ldr-pseudo-wide.s +++ b/llvm/test/MC/ARM/ldr-pseudo-wide.s @@ -36,9 +36,9 @@ f2: ldr.w r0, =foo @ CHECK-ARM: ldr r0, .Ltmp[[TMP2:[0-9]+]] -@ CHECK-DARWIN-ARM: ldr r0, Ltmp2 +@ CHECK-DARWIN-ARM: ldr r0, Ltmp1 @ CHECK-THUMB2: ldr.w r0, .Ltmp[[TMP2:[0-9]+]] -@ CHECK-DARWIN-THUMB2: ldr.w r0, Ltmp2 +@ CHECK-DARWIN-THUMB2: ldr.w r0, Ltmp1 @ CHECK-THUMB: error: instruction requires: thumb2 @ CHECK-THUMB-NEXT: ldr.w r0, =foo @@ -56,12 +56,8 @@ f3: @ CHECK-NEXT: .long 65538 @ CHECK: .Ltmp1: @ CHECK-NEXT: .long foo -@ CHECK: .Ltmp2: -@ CHECK-NEXT: .long foo @ CHECK-DARWIN: Ltmp0: @ CHECK-DARWIN-NEXT: .long 65538 @ CHECK-DARWIN: Ltmp1: @ CHECK-DARWIN-NEXT: .long foo -@ CHECK-DARWIN: Ltmp2: -@ CHECK-DARWIN-NEXT: .long foo