Skip to content

Commit

Permalink
[MC] Fold A-B when A is a pending label or A/B are separated by a MCF…
Browse files Browse the repository at this point in the history
…illFragment

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
  • Loading branch information
MaskRay committed Jun 19, 2023
1 parent ada137a commit 507efbc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
23 changes: 19 additions & 4 deletions llvm/lib/MC/MCExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,15 +658,30 @@ static void AttemptToFoldSymbolOffsetDifference(
// Try to find a constant displacement from FA to FB, add the displacement
// between the offset in FA of SA and the offset in FB of SB.
int64_t Displacement = SA.getOffset() - SB.getOffset();
bool Found = false;
for (auto FI = FB->getIterator(), FE = SecA.end(); FI != FE; ++FI) {
auto DF = dyn_cast<MCDataFragment>(FI);
if (&*FI == FA) {
Addend += Displacement;
return FinalizeFolding();
Found = true;
break;
}

if (FI->getKind() != MCFragment::FT_Data)
int64_t Num;
if (DF) {
Displacement += DF->getContents().size();
} else if (auto *FF = dyn_cast<MCFillFragment>(FI);
FF && FF->getNumValues().evaluateAsAbsolute(Num)) {
Displacement += Num * FF->getValueSize();
} else {
return;
Displacement += cast<MCDataFragment>(FI)->getContents().size();
}
}
// If FA is found or if FA is a dummy fragment not in the fragment list,
// (which means SA is a pending label (see flushPendingLabels)), we can
// resolve the difference.
if (Found || isa<MCDummyFragment>(FA)) {
Addend += Displacement;
FinalizeFolding();
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/MC/ARM/directive-if-subtraction.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: llvm-mc -triple armv7a-linux-gnueabihf %s -filetype=obj -o /dev/null 2>&1 | FileCheck --check-prefix=OBJ --allow-empty %s
// RUN: not llvm-mc -triple armv7a-linux-gnueabihf %s -o /dev/null 2>&1 | FileCheck --check-prefix=ASM %s
// RUN: not llvm-mc -triple armv7a-linux-gnueabihf %s -o /dev/null 2>&1 | FileCheck --check-prefix=ASM %s --implicit-check-not=error:
// RUN: llvm-mc -triple armv7a-linux-gnueabihf %s -filetype=obj -o - | llvm-objdump -d - | FileCheck --check-prefix=DISASM %s

nop
Expand Down Expand Up @@ -32,7 +32,6 @@ orr r1, r1, #2
.space 4
nop
.if . - 9997b == 4
// ARM-ERR:[[@LINE-1]]:5: error: expected absolute expression
.endif

9997:
Expand Down
12 changes: 11 additions & 1 deletion llvm/test/MC/AsmParser/assembler-expressions.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.data

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

foo1:
# ASM-ERR: :[[#@LINE+1]]:5: error: expected absolute expression
Expand Down Expand Up @@ -32,6 +32,16 @@ foo3:
.byte 7
.endif

foo4:
.byte 0
.space 2
# ASM-ERR: :[[#@LINE+1]]:5: error: expected absolute expression
.if . - foo4 == 3
.byte 8
.else
.byte 9
.endif

.byte 0xff

# nop is a fixed size instruction so this should pass.
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/MC/RISCV/cfi-advance.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# RUN: llvm-dwarfdump --debug-frame %t.o 2>&1 \
# RUN: | FileCheck -check-prefix=CHECK-DWARFDUMP %s

# CHECK: 0x26 R_RISCV_SET8 - 0x0
# CHECK-NEXT: 0x26 R_RISCV_SUB8 - 0x0
# CHECK-NEXT: 0x2A R_RISCV_SET16 - 0x0
# CHECK-NEXT: 0x2A R_RISCV_SUB16 - 0x0
# CHECK-NEXT: 0x2F R_RISCV_SET32 - 0x0
# CHECK: .rela.eh_frame {
# CHECK-NEXT: 0x1C R_RISCV_32_PCREL - 0x0
# CHECK-NEXT: 0x20 R_RISCV_ADD32 - 0x0
# CHECK-NEXT: 0x20 R_RISCV_SUB32 - 0x0
# CHECK-NEXT: }
# CHECK-DWARFDUMP: DW_CFA_advance_loc1
# CHECK-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset
# CHECK-DWARFDUMP-NEXT: DW_CFA_advance_loc2
Expand Down

0 comments on commit 507efbc

Please sign in to comment.