diff --git a/llvm/include/llvm/MC/ConstantPools.h b/llvm/include/llvm/MC/ConstantPools.h --- a/llvm/include/llvm/MC/ConstantPools.h +++ b/llvm/include/llvm/MC/ConstantPools.h @@ -43,7 +43,8 @@ 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 --- a/llvm/lib/MC/ConstantPools.cpp +++ b/llvm/lib/MC/ConstantPools.cpp @@ -39,25 +39,36 @@ 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; + auto CItr = C ? CachedConstantEntries.find(C->getValue()) + : CachedConstantEntries.end(); + if (CItr != CachedConstantEntries.end()) + return CItr->second; + + // Check if there is existing entry for the same symbol. If so, reuse if. + auto SItr = S ? CachedSymbolEntries.find(&(S->getSymbol())) + : CachedSymbolEntries.end(); + 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 --- a/llvm/test/MC/ARM/ldr-pseudo-wide.s +++ b/llvm/test/MC/ARM/ldr-pseudo-wide.s @@ -36,9 +36,9 @@ 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 @@ @ 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