diff --git a/llvm/include/llvm/MC/ConstantPools.h b/llvm/include/llvm/MC/ConstantPools.h index 7eac75362effd..ff21ccda07a83 100644 --- a/llvm/include/llvm/MC/ConstantPools.h +++ b/llvm/include/llvm/MC/ConstantPools.h @@ -43,8 +43,13 @@ struct ConstantPoolEntry { class ConstantPool { using EntryVecTy = SmallVector; EntryVecTy Entries; - std::map CachedConstantEntries; - DenseMap CachedSymbolEntries; + + // Caches of entries that already exist, indexed by their contents + // and also the size of the constant. + std::map, const MCSymbolRefExpr *> + CachedConstantEntries; + DenseMap, const MCSymbolRefExpr *> + CachedSymbolEntries; public: // Initialize a new empty constant pool diff --git a/llvm/lib/MC/ConstantPools.cpp b/llvm/lib/MC/ConstantPools.cpp index f895cc6413d74..824d2463f30fc 100644 --- a/llvm/lib/MC/ConstantPools.cpp +++ b/llvm/lib/MC/ConstantPools.cpp @@ -43,14 +43,15 @@ const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, // Check if there is existing entry for the same constant. If so, reuse it. if (C) { - auto CItr = CachedConstantEntries.find(C->getValue()); + auto CItr = CachedConstantEntries.find(std::make_pair(C->getValue(), Size)); 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())); + auto SItr = + CachedSymbolEntries.find(std::make_pair(&(S->getSymbol()), Size)); if (SItr != CachedSymbolEntries.end()) return SItr->second; } @@ -60,9 +61,9 @@ const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context); if (C) - CachedConstantEntries[C->getValue()] = SymRef; + CachedConstantEntries[std::make_pair(C->getValue(), Size)] = SymRef; if (S) - CachedSymbolEntries[&(S->getSymbol())] = SymRef; + CachedSymbolEntries[std::make_pair(&(S->getSymbol()), Size)] = SymRef; return SymRef; } diff --git a/llvm/test/MC/AArch64/constant-pool-sizes.s b/llvm/test/MC/AArch64/constant-pool-sizes.s new file mode 100644 index 0000000000000..279402af025f3 --- /dev/null +++ b/llvm/test/MC/AArch64/constant-pool-sizes.s @@ -0,0 +1,25 @@ +// RUN: llvm-mc -triple aarch64-none-linux-gnu %s | FileCheck %s + + ldr w0, =symbol + ldr x1, =symbol + + ldr w2, =1234567890 + ldr x3, =1234567890 + +// CHECK: ldr w0, .Ltmp0 +// CHECK: ldr x1, .Ltmp1 +// CHECK: ldr w2, .Ltmp2 +// CHECK: ldr x3, .Ltmp3 + +// CHECK: .p2align 2, 0x0 +// CHECK-NEXT:.Ltmp0: +// CHECK-NEXT: .word symbol +// CHECK: .p2align 3, 0x0 +// CHECK-NEXT:.Ltmp1: +// CHECK-NEXT: .xword symbol +// CHECK: .p2align 2, 0x0 +// CHECK-NEXT:.Ltmp2: +// CHECK-NEXT: .word 1234567890 +// CHECK: .p2align 3, 0x0 +// CHECK-NEXT:.Ltmp3: +// CHECK-NEXT: .xword 1234567890