Skip to content

Commit

Permalink
Fix 24560: assembler does not share constant pool for same constants
Browse files Browse the repository at this point in the history
Summary: This patch returns the same label if the CP entry with the same value has been created.

Reviewers: eli.friedman, rengolin, jmolloy

Subscribers: majnemer, jmolloy, llvm-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286006 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Weiming Zhao committed Nov 4, 2016
1 parent 933679b commit 7e3119c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 74 deletions.
2 changes: 2 additions & 0 deletions include/llvm/MC/ConstantPools.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MCExpr;
class MCSection;
class MCStreamer;
class MCSymbol;
class MCSymbolRefExpr;

struct ConstantPoolEntry {
ConstantPoolEntry(MCSymbol *L, const MCExpr *Val, unsigned Sz, SMLoc Loc_)
Expand All @@ -40,6 +41,7 @@ struct ConstantPoolEntry {
class ConstantPool {
typedef SmallVector<ConstantPoolEntry, 4> EntryVecTy;
EntryVecTy Entries;
DenseMap<int64_t, const MCSymbolRefExpr *> CachedEntries;

public:
// Initialize a new empty constant pool
Expand Down
12 changes: 11 additions & 1 deletion lib/MC/ConstantPools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ 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);

// 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;

MCSymbol *CPEntryLabel = Context.createTempSymbol();

Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc));
return MCSymbolRefExpr::create(CPEntryLabel, Context);
const auto SymRef = MCSymbolRefExpr::create(CPEntryLabel, Context);
if (C)
CachedEntries[C->getValue()] = SymRef;
return SymRef;
}

bool ConstantPool::empty() { return Entries.empty(); }
Expand Down
8 changes: 1 addition & 7 deletions test/MC/AArch64/ldr-pseudo.s
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ f5:
adds x0, x0, #1
adds x0, x0, #1
ldr w0, =0x10004
// CHECK: ldr w0, .Ltmp[[TMP4:[0-9]+]]
// CHECK: ldr w0, .Ltmp[[TMP3:[0-9]+]]
adds x0, x0, #1
adds x0, x0, #1
adds x0, x0, #1
Expand Down Expand Up @@ -225,9 +225,6 @@ f18:
// CHECK: .p2align 2
// CHECK: .Ltmp[[TMP3]]
// CHECK: .word 65540
// CHECK: .p2align 2
// CHECK: .Ltmp[[TMP4]]
// CHECK: .word 65540

// CHECK: .section e,"ax",@progbits
// CHECK: .p2align 2
Expand Down Expand Up @@ -314,6 +311,3 @@ f18:
// CHECK: .p2align 2
// CHECK: .Ltmp[[TMP25]]
// CHECK: .word 3276900
// CHECK: .p2align 3
// CHECK: .Ltmp[[TMP26]]
// CHECK: .xword 3276900
87 changes: 42 additions & 45 deletions test/MC/ARM/ldr-pseudo-darwin.s
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ f5:
adds r0, r0, #1
adds r0, r0, #1
ldr r0, =0x10005
@ CHECK: ldr r0, Ltmp4
@ CHECK: ldr r0, Ltmp3
adds r0, r0, #1
adds r0, r0, #1
adds r0, r0, #1
Expand All @@ -61,7 +61,7 @@ f5:
@ CHECK-LABEL: f6:
f6:
ldr r0, =0x10006
@ CHECK: ldr r0, Ltmp5
@ CHECK: ldr r0, Ltmp4
adds r0, r0, #1
adds r0, r0, #1
adds r0, r0, #1
Expand All @@ -78,7 +78,7 @@ f7:
f8:
adds r0, r0, #1
ldr r0, =0x10007
@ CHECK: ldr r0, Ltmp6
@ CHECK: ldr r0, Ltmp5
adds r0, r0, #1
adds r0, r0, #1

Expand All @@ -91,21 +91,21 @@ f8:
@ CHECK-LABEL: f9:
f9:
ldr r0, =foo
@ CHECK: ldr r0, Ltmp7
@ CHECK: ldr r0, Ltmp6

@ load a symbol from another section
.section __TEXT,h,regular,pure_instructions
@ CHECK-LABEL: f10:
f10:
ldr r0, =f5
@ CHECK: ldr r0, Ltmp8
@ CHECK: ldr r0, Ltmp7

@ load a symbol from the same section
.section __TEXT,i,regular,pure_instructions
@ CHECK-LABEL: f11:
f11:
ldr r0, =f12
@ CHECK: ldr r0, Ltmp9
@ CHECK: ldr r0, Ltmp8

@ CHECK-LABEL: f12:
f12:
Expand All @@ -119,11 +119,11 @@ f13:
adds r0, r0, #1
adds r0, r0, #1
ldr r0, =0x10008
@ CHECK: ldr r0, Ltmp10
@ CHECK: ldr r0, Ltmp9
adds r0, r0, #1
adds r0, r0, #1
ldr r0, =bar
@ CHECK: ldr r0, Ltmp11
@ CHECK: ldr r0, Ltmp10
adds r0, r0, #1
adds r0, r0, #1
@
Expand All @@ -139,18 +139,18 @@ f13:
@ CHECK-LABEL: f14:
f14:
useit_in_a_macro
@ CHECK: ldr r0, Ltmp11
@ CHECK: ldr r0, Ltmp12
@ CHECK: ldr r0, Ltmp13

@ usage with expressions
.section __TEXT,l,regular,pure_instructions
@ CHECK-LABEL: f15:
f15:
ldr r0, =0x10001+9
@ CHECK: ldr r0, Ltmp14
@ CHECK: ldr r0, Ltmp13
adds r0, r0, #1
ldr r0, =bar+4
@ CHECK: ldr r0, Ltmp15
@ CHECK: ldr r0, Ltmp14
adds r0, r0, #1

@ transformation to mov
Expand All @@ -162,68 +162,68 @@ f16:
ldr r1, =0x1
@ CHECK-ARM: mov r1, #1
@ CHECK-ARMV5: mov r1, #1
@ CHECK-THUMB: ldr r1, Ltmp16
@ CHECK-THUMB: ldr r1, Ltmp15
@ CHECK-THUMB2: mov.w r1, #1
@ CHECK-BASELINE: movw r1, #1

@ Immediate is representable in A1 and T2 modified immediate only not movw
ldr r2, =0x120000
@ CHECK-ARM: mov r2, #1179648
@ CHECK-ARMV5: mov r2, #1179648
@ CHECK-THUMB: ldr r2, Ltmp17
@ CHECK-THUMB: ldr r2, Ltmp16
@ CHECK-THUMB2: mov.w r2, #1179648
@ CHECK-BASELINE: ldr r2, Ltmp16
@ CHECK-BASELINE: ldr r2, Ltmp15

@ Immediate can be represented only with movw instruction
ldr r3, =0x1234
@ CHECK-ARM: movw r3, #4660
@ CHECK-ARMV5: ldr r3, Ltmp16
@ CHECK-THUMB: ldr r3, Ltmp18
@ CHECK-ARMV5: ldr r3, Ltmp15
@ CHECK-THUMB: ldr r3, Ltmp17
@ CHECK-THUMB2: movw r3, #4660
@ CHECK-BASELINE: movw r3, #4660

@ Immediate can be represented only with T2 modified immediate
ldr r4, =0xabababab
@ CHECK-ARM: ldr r4, Ltmp16
@ CHECK-ARMV5: ldr r4, Ltmp17
@ CHECK-THUMB: ldr r4, Ltmp19
@ CHECK-ARM: ldr r4, Ltmp15
@ CHECK-ARMV5: ldr r4, Ltmp16
@ CHECK-THUMB: ldr r4, Ltmp18
@ CHECK-THUMB2: mov.w r4, #2880154539
@ CHECK-BASELINE: ldr r4, Ltmp17
@ CHECK-BASELINE: ldr r4, Ltmp16

@ Immediate can be represented only with A1 modified immediate
ldr r5, =0x1000000b
@ CHECK-ARM: mov r5, #268435467
@ CHECK-ARMV5: mov r5, #268435467
@ CHECK-THUMB: ldr r5, Ltmp20
@ CHECK-THUMB2: ldr r5, Ltmp16
@ CHECK-BASELINE: ldr r5, Ltmp18
@ CHECK-THUMB: ldr r5, Ltmp19
@ CHECK-THUMB2: ldr r5, Ltmp15
@ CHECK-BASELINE: ldr r5, Ltmp17

@ Negative numbers can be used with MVN or in Thumb2 with modified immediate
ldr r6, =-1
@ CHECK-ARM: mvn r6, #0
@ CHECK-ARMV5: mvn r6, #0
@ CHECK-THUMB: ldr r6, Ltmp21
@ CHECK-THUMB: ldr r6, Ltmp20
@ CHECK-THUMB2: mov.w r6, #-1
@ CHECK-BASELINE: ldr r6, Ltmp19
@ CHECK-BASELINE: ldr r6, Ltmp18
ldr r7, =-0x100
@ CHECK-ARM: mvn r7, #255
@ CHECK-ARMV5: mvn r7, #255
@ CHECK-THUMB: ldr r7, Ltmp22
@ CHECK-THUMB: ldr r7, Ltmp21
@ CHECK-THUMB2: mvn r7, #255
@ CHECK-BASELINE: ldr r7, Ltmp20
@ CHECK-BASELINE: ldr r7, Ltmp19

@ Constant expressions can be used
.equ expr, 0x10 + 0x10
ldr r0, = expr
@ CHECK-ARM: mov r0, #32
@ CHECK-ARMV5: mov r0, #32
@ CHECK-THUMB: ldr r0, Ltmp23
@ CHECK-THUMB: ldr r0, Ltmp22
@ CHECK-THUMB2: mov.w r0, #32
@ CHECK-BASELINE: movw r0, #32
ldr r1, = expr - 0x10
@ CHECK-ARM: mov r1, #16
@ CHECK-ARMV5: mov r1, #16
@ CHECK-THUMB: ldr r1, Ltmp24
@ CHECK-THUMB: ldr r1, Ltmp23
@ CHECK-THUMB2: mov.w r1, #16
@ CHECK-BASELINE: movw r1, #16

Expand All @@ -239,8 +239,8 @@ f17:
@ CHECK-ARM: mov r3, #32
@ CHECK-ARMV5: mov r2, #3
@ CHECK-ARMV5: mov r3, #32
@ CHECK-THUMB: ldr r2, Ltmp25
@ CHECK-THUMB: ldr r3, Ltmp26
@ CHECK-THUMB: ldr r2, Ltmp24
@ CHECK-THUMB: ldr r3, Ltmp22
@ CHECK-THUMB2: mov.w r2, #3
@ CHECK-THUMB2: mov.w r3, #32
@ CHECK-BASELINE: movw r2, #3
Expand Down Expand Up @@ -270,18 +270,15 @@ f17:
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp3:
@ CHECK: .long 65541
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp4:
@ CHECK: .long 65541
@ CHECK: .end_data_region

@ CHECK: .section __TEXT,e,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp5:
@ CHECK-LABEL: Ltmp4:
@ CHECK: .long 65542
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp6:
@ CHECK-LABEL: Ltmp5:
@ CHECK: .long 65543
@ CHECK: .end_data_region

Expand All @@ -291,50 +288,50 @@ f17:
@ CHECK: .section __TEXT,g,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp7:
@ CHECK-LABEL: Ltmp6:
@ CHECK: .long foo
@ CHECK: .end_data_region

@ CHECK: .section __TEXT,h,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp8:
@ CHECK-LABEL: Ltmp7:
@ CHECK: .long f5
@ CHECK: .end_data_region

@ CHECK: .section __TEXT,i,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp9:
@ CHECK-LABEL: Ltmp8:
@ CHECK: .long f12
@ CHECK: .end_data_region

@ CHECK: .section __TEXT,j,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp10:
@ CHECK-LABEL: Ltmp9:
@ CHECK: .long 65544
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp11:
@ CHECK-LABEL: Ltmp10:
@ CHECK: .long bar
@ CHECK: .end_data_region

@ CHECK: .section __TEXT,k,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp12:
@ CHECK-LABEL: Ltmp11:
@ CHECK: .long 65545
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp13:
@ CHECK-LABEL: Ltmp12:
@ CHECK: .long baz
@ CHECK: .end_data_region

@ CHECK: .section __TEXT,l,regular,pure_instructions
@ CHECK: .data_region
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp14:
@ CHECK-LABEL: Ltmp13:
@ CHECK: .long 65546
@ CHECK: .p2align 2
@ CHECK-LABEL: Ltmp15:
@ CHECK-LABEL: Ltmp14:
@ CHECK: .long bar+4
@ CHECK: .end_data_region
32 changes: 14 additions & 18 deletions test/MC/ARM/ldr-pseudo-wide.s
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ f1:
@ CHECK-DARWIN-THUMB2: ldr r0, Ltmp0

ldr.w r0, =0x10002
@ CHECK-ARM: ldr r0, .Ltmp[[TMP1:[0-9]+]]
@ CHECK-DARWIN-ARM: ldr r0, Ltmp1
@ CHECK-THUMB2: ldr.w r0, .Ltmp[[TMP1:[0-9]+]]
@ CHECK-DARWIN-THUMB2: ldr.w r0, Ltmp1
@ CHECK-ARM: ldr r0, .Ltmp[[TMP0]]
@ CHECK-DARWIN-ARM: ldr r0, Ltmp0
@ CHECK-THUMB2: ldr.w r0, .Ltmp[[TMP0:[0-9]+]]
@ CHECK-DARWIN-THUMB2: ldr.w r0, Ltmp0
@ CHECK-THUMB: error: instruction requires: thumb2
@ CHECK-THUMB-NEXT: ldr.w r0, =0x10002

@ CHECK-LABEL: f2:
f2:
ldr r0, =foo
@ CHECK-ARM: ldr r0, .Ltmp[[TMP2:[0-9]+]]
@ CHECK-DARWIN-ARM: ldr r0, Ltmp2
@ CHECK-THUMB2: ldr r0, .Ltmp[[TMP2:[0-9]+]]
@ CHECK-DARWIN-THUMB2: ldr r0, Ltmp2
@ CHECK-ARM: ldr r0, .Ltmp[[TMP1:[0-9]+]]
@ CHECK-DARWIN-ARM: ldr r0, Ltmp1
@ CHECK-THUMB2: ldr r0, .Ltmp[[TMP1:[0-9]+]]
@ CHECK-DARWIN-THUMB2: ldr r0, Ltmp1

ldr.w r0, =foo
@ CHECK-ARM: ldr r0, .Ltmp[[TMP3:[0-9]+]]
@ CHECK-DARWIN-ARM: ldr r0, Ltmp3
@ CHECK-THUMB2: ldr.w r0, .Ltmp[[TMP3:[0-9]+]]
@ CHECK-DARWIN-THUMB2: ldr.w r0, Ltmp3
@ CHECK-ARM: ldr r0, .Ltmp[[TMP2:[0-9]+]]
@ CHECK-DARWIN-ARM: ldr r0, Ltmp2
@ CHECK-THUMB2: ldr.w r0, .Ltmp[[TMP2:[0-9]+]]
@ CHECK-DARWIN-THUMB2: ldr.w r0, Ltmp2
@ CHECK-THUMB: error: instruction requires: thumb2
@ CHECK-THUMB-NEXT: ldr.w r0, =foo

Expand All @@ -55,17 +55,13 @@ f3:
@ CHECK: .Ltmp0:
@ CHECK-NEXT: .long 65538
@ CHECK: .Ltmp1:
@ CHECK-NEXT: .long 65538
@ CHECK: .Ltmp2:
@ CHECK-NEXT: .long foo
@ CHECK: .Ltmp3:
@ CHECK: .Ltmp2:
@ CHECK-NEXT: .long foo

@ CHECK-DARWIN: Ltmp0:
@ CHECK-DARWIN-NEXT: .long 65538
@ CHECK-DARWIN: Ltmp1:
@ CHECK-DARWIN-NEXT: .long 65538
@ CHECK-DARWIN: Ltmp2:
@ CHECK-DARWIN-NEXT: .long foo
@ CHECK-DARWIN: Ltmp3:
@ CHECK-DARWIN: Ltmp2:
@ CHECK-DARWIN-NEXT: .long foo
Loading

0 comments on commit 7e3119c

Please sign in to comment.