Skip to content

Commit 507efbc

Browse files
committed
[MC] Fold A-B when A is a pending label or A/B are separated by a MCFillFragment
When the MCAssembler is non-null and the MCAsmLayout is null, we can fold A-B in these additional cases: * when A is a pending label (will be reassigned to a real fragment in flushPendingLabels()) * A and B are separated by a MCFillFragment with a constant size
1 parent ada137a commit 507efbc

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

llvm/lib/MC/MCExpr.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,30 @@ static void AttemptToFoldSymbolOffsetDifference(
658658
// Try to find a constant displacement from FA to FB, add the displacement
659659
// between the offset in FA of SA and the offset in FB of SB.
660660
int64_t Displacement = SA.getOffset() - SB.getOffset();
661+
bool Found = false;
661662
for (auto FI = FB->getIterator(), FE = SecA.end(); FI != FE; ++FI) {
663+
auto DF = dyn_cast<MCDataFragment>(FI);
662664
if (&*FI == FA) {
663-
Addend += Displacement;
664-
return FinalizeFolding();
665+
Found = true;
666+
break;
665667
}
666668

667-
if (FI->getKind() != MCFragment::FT_Data)
669+
int64_t Num;
670+
if (DF) {
671+
Displacement += DF->getContents().size();
672+
} else if (auto *FF = dyn_cast<MCFillFragment>(FI);
673+
FF && FF->getNumValues().evaluateAsAbsolute(Num)) {
674+
Displacement += Num * FF->getValueSize();
675+
} else {
668676
return;
669-
Displacement += cast<MCDataFragment>(FI)->getContents().size();
677+
}
678+
}
679+
// If FA is found or if FA is a dummy fragment not in the fragment list,
680+
// (which means SA is a pending label (see flushPendingLabels)), we can
681+
// resolve the difference.
682+
if (Found || isa<MCDummyFragment>(FA)) {
683+
Addend += Displacement;
684+
FinalizeFolding();
670685
}
671686
}
672687
}

llvm/test/MC/ARM/directive-if-subtraction.s

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: llvm-mc -triple armv7a-linux-gnueabihf %s -filetype=obj -o /dev/null 2>&1 | FileCheck --check-prefix=OBJ --allow-empty %s
2-
// RUN: not llvm-mc -triple armv7a-linux-gnueabihf %s -o /dev/null 2>&1 | FileCheck --check-prefix=ASM %s
2+
// RUN: not llvm-mc -triple armv7a-linux-gnueabihf %s -o /dev/null 2>&1 | FileCheck --check-prefix=ASM %s --implicit-check-not=error:
33
// RUN: llvm-mc -triple armv7a-linux-gnueabihf %s -filetype=obj -o - | llvm-objdump -d - | FileCheck --check-prefix=DISASM %s
44

55
nop
@@ -32,7 +32,6 @@ orr r1, r1, #2
3232
.space 4
3333
nop
3434
.if . - 9997b == 4
35-
// ARM-ERR:[[@LINE-1]]:5: error: expected absolute expression
3635
.endif
3736

3837
9997:

llvm/test/MC/AsmParser/assembler-expressions.s

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.data
55

66
# OBJDATA: Contents of section .data
7-
# OBJDATA-NEXT: 0000 aa01aa05 06ff
7+
# OBJDATA-NEXT: 0000 aa01aa05 06000000 08ff
88

99
foo1:
1010
# ASM-ERR: :[[#@LINE+1]]:5: error: expected absolute expression
@@ -32,6 +32,16 @@ foo3:
3232
.byte 7
3333
.endif
3434

35+
foo4:
36+
.byte 0
37+
.space 2
38+
# ASM-ERR: :[[#@LINE+1]]:5: error: expected absolute expression
39+
.if . - foo4 == 3
40+
.byte 8
41+
.else
42+
.byte 9
43+
.endif
44+
3545
.byte 0xff
3646

3747
# nop is a fixed size instruction so this should pass.

llvm/test/MC/RISCV/cfi-advance.s

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# RUN: llvm-dwarfdump --debug-frame %t.o 2>&1 \
44
# RUN: | FileCheck -check-prefix=CHECK-DWARFDUMP %s
55

6-
# CHECK: 0x26 R_RISCV_SET8 - 0x0
7-
# CHECK-NEXT: 0x26 R_RISCV_SUB8 - 0x0
8-
# CHECK-NEXT: 0x2A R_RISCV_SET16 - 0x0
9-
# CHECK-NEXT: 0x2A R_RISCV_SUB16 - 0x0
10-
# CHECK-NEXT: 0x2F R_RISCV_SET32 - 0x0
6+
# CHECK: .rela.eh_frame {
7+
# CHECK-NEXT: 0x1C R_RISCV_32_PCREL - 0x0
8+
# CHECK-NEXT: 0x20 R_RISCV_ADD32 - 0x0
9+
# CHECK-NEXT: 0x20 R_RISCV_SUB32 - 0x0
10+
# CHECK-NEXT: }
1111
# CHECK-DWARFDUMP: DW_CFA_advance_loc1
1212
# CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset
1313
# CHECK-DWARFDUMP-NEXT: DW_CFA_advance_loc2

0 commit comments

Comments
 (0)