Skip to content

Commit

Permalink
[MC][ARM] Reuse symbol value in constant pool
Browse files Browse the repository at this point in the history
Fix #55816

Before this patch, MCConstantExpr were reused, but MCSymbolExpr were
not. To reuse symbol value, this patch added a DenseMap to record the
symbol value.

Differential Revision: https://reviews.llvm.org/D127113
  • Loading branch information
luxufan committed Jun 7, 2022
1 parent a3a4f03 commit a7b154a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion llvm/include/llvm/MC/ConstantPools.h
Expand Up @@ -43,7 +43,8 @@ struct ConstantPoolEntry {
class ConstantPool {
using EntryVecTy = SmallVector<ConstantPoolEntry, 4>;
EntryVecTy Entries;
std::map<int64_t, const MCSymbolRefExpr *> CachedEntries;
std::map<int64_t, const MCSymbolRefExpr *> CachedConstantEntries;
DenseMap<const MCSymbol *, const MCSymbolRefExpr *> CachedSymbolEntries;

public:
// Initialize a new empty constant pool
Expand Down
23 changes: 18 additions & 5 deletions llvm/lib/MC/ConstantPools.cpp
Expand Up @@ -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<MCConstantExpr>(Value);
const MCSymbolRefExpr *S = dyn_cast<MCSymbolRefExpr>(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();
}

//
Expand Down
8 changes: 2 additions & 6 deletions llvm/test/MC/ARM/ldr-pseudo-wide.s
Expand Up @@ -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

Expand All @@ -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

0 comments on commit a7b154a

Please sign in to comment.