Skip to content

Commit

Permalink
[ARM] Clear the constant pool cache on explicit .ltorg directives
Browse files Browse the repository at this point in the history
Multiple ldr pseudoinstructions with the same constant value will
reuse the same constant pool entry. However, if the constant pool
is explicitly flushed with a .ltorg directive, we should not try
to reference constants in the previous pool any longer, since they
may be out of range.

This fixes assembling hand-written assembler source which repeatedly
loads the same constant value, across a binary size larger than the
pc-relative fixup range for ldr instructions (4096 bytes). Such
assembler source already uses explicit .ltorg instructions to emit
constant pools with regular intervals. However if we try to reuse
constants emitted in earlier pools, they end up out of range.

This makes the output of the testcase match what binutils gas does
(prior to this patch, it would fail to assemble).

Differential Revision: https://reviews.llvm.org/D32847

llvm-svn: 302416
  • Loading branch information
mstorsjo committed May 8, 2017
1 parent 7a28a3a commit fd4c158
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions llvm/include/llvm/MC/ConstantPools.h
Expand Up @@ -63,6 +63,8 @@ class ConstantPool {

// Return true if the constant pool is empty
bool empty();

void clearCache();
};

class AssemblerConstantPools {
Expand All @@ -86,6 +88,7 @@ class AssemblerConstantPools {
public:
void emitAll(MCStreamer &Streamer);
void emitForCurrentSection(MCStreamer &Streamer);
void clearCacheForCurrentSection(MCStreamer &Streamer);
const MCExpr *addEntry(MCStreamer &Streamer, const MCExpr *Expr,
unsigned Size, SMLoc Loc);

Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/MC/ConstantPools.cpp
Expand Up @@ -57,6 +57,10 @@ const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context,

bool ConstantPool::empty() { return Entries.empty(); }

void ConstantPool::clearCache() {
CachedEntries.clear();
}

//
// AssemblerConstantPools implementation
//
Expand Down Expand Up @@ -98,6 +102,13 @@ void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) {
}
}

void AssemblerConstantPools::clearCacheForCurrentSection(MCStreamer &Streamer) {
MCSection *Section = Streamer.getCurrentSectionOnly();
if (ConstantPool *CP = getConstantPool(Section)) {
CP->clearCache();
}
}

const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer,
const MCExpr *Expr,
unsigned Size, SMLoc Loc) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMTargetStreamer.cpp
Expand Up @@ -38,6 +38,7 @@ const MCExpr *ARMTargetStreamer::addConstantPoolEntry(const MCExpr *Expr, SMLoc

void ARMTargetStreamer::emitCurrentConstantPool() {
ConstantPools->emitForCurrentSection(Streamer);
ConstantPools->clearCacheForCurrentSection(Streamer);
}

// finish() - write out any non-empty assembler constant pools.
Expand Down
27 changes: 27 additions & 0 deletions llvm/test/MC/ARM/ltorg-range.s
@@ -0,0 +1,27 @@
@ RUN: llvm-mc -triple armv7-unknown-linux-gnueabi -filetype obj -o - %s \
@ RUN: | llvm-objdump -d - | FileCheck %s

ldr r0, =0x01020304
@ CHECK: ldr
.ltorg
@ CHECK: 0x01020304
ldr r0, =0x01020304
ldr r0, =0x01020304
ldr r0, =0x01020304
@ CHECK: ldr
@ CHECK: ldr
@ CHECK: ldr
.ltorg
@ CHECK: 0x01020304
.rep 1028
.word 0
.endr
@ CHECK: 0x00000000

ldr r0, =0x01020304
@ CHECK: ldr
.ltorg
@ CHECK: 0x01020304
.rep 1028
.word 0
.endr

0 comments on commit fd4c158

Please sign in to comment.