xref: /llvm-project/llvm/test/MC/RISCV/align.s (revision b27f86b40b20942c0e809128214b43d6edde365a)
1# The file testing Nop insertion with R_RISCV_ALIGN for relaxation.
2
3# Relaxation enabled:
4# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+relax < %s \
5# RUN:     | llvm-objdump -d -M no-aliases - \
6# RUN:     | FileCheck -check-prefix=RELAX-INST %s
7# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+relax < %s \
8# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=RELAX-RELOC %s
9
10# Relaxation disabled:
11# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=-relax < %s \
12# RUN:     | llvm-objdump -d -M no-aliases - \
13# RUN:     | FileCheck -check-prefix=NORELAX-INST %s
14# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=-relax < %s \
15# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=NORELAX-RELOC %s
16
17# Relaxation enabled with C extension:
18# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c,+relax < %s \
19# RUN:     | llvm-objdump -d -M no-aliases - \
20# RUN:     | FileCheck -check-prefix=C-OR-ZCA-EXT-RELAX-INST %s
21# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c,+relax < %s \
22# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=C-OR-ZCA-EXT-RELAX-RELOC %s
23
24# Relaxation enabled with Zca extension:
25# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+zca,+relax < %s \
26# RUN:     | llvm-objdump --mattr=+zca -d -M no-aliases - \
27# RUN:     | FileCheck -check-prefix=C-OR-ZCA-EXT-RELAX-INST %s
28# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+zca,+relax < %s \
29# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=C-OR-ZCA-EXT-RELAX-RELOC %s
30
31# Relaxation disabled with C extension:
32# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c,-relax < %s \
33# RUN:     | llvm-objdump -d -M no-aliases - \
34# RUN:     | FileCheck -check-prefix=C-OR-ZCA-EXT-NORELAX-INST %s
35# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c,-relax < %s \
36# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=C-OR-ZCA-EXT-NORELAX-RELOC %s
37
38# Relaxation disabled with ZCA extension:
39# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+zca,-relax < %s \
40# RUN:     | llvm-objdump --mattr=+zca -d -M no-aliases - \
41# RUN:     | FileCheck -check-prefix=C-OR-ZCA-EXT-NORELAX-INST %s
42# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+zca,-relax < %s \
43# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=C-OR-ZCA-EXT-NORELAX-RELOC %s
44
45# We need to insert N-MinNopSize bytes NOPs and R_RISCV_ALIGN relocation
46# type for .align N directive when linker relaxation enabled.
47# Linker could satisfy alignment by removing NOPs after linker relaxation.
48
49# The first R_RISCV_ALIGN come from
50# MCELFStreamer::InitSections() emitCodeAlignment(getTextSectionAligntment()).
51# C-OR-ZCA-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2
52# C-OR-ZCA-EXT-RELAX-INST:  c.nop
53test:
54	.p2align 2
55# If the +c extension is enabled, the text section will be 2-byte aligned, so
56# one c.nop instruction is sufficient.
57# C-OR-ZCA-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0x2
58# C-OR-ZCA-EXT-RELAX-INST-NOT:  c.nop
59	bne     zero, a0, .LBB0_2
60	mv	a0, zero
61	.p2align 3
62# RELAX-RELOC: R_RISCV_ALIGN - 0x4
63# RELAX-INST:  addi    zero, zero, 0
64# C-OR-ZCA-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x6
65# C-OR-ZCA-EXT-RELAX-INST:  c.nop
66# C-OR-ZCA-EXT-RELAX-INST:  addi    zero, zero, 0
67# C-OR-ZCA-EXT-NORELAX-INST: addi    zero, zero, 0
68	add	a0, a0, a1
69	.align 4
70.LBB0_2:
71# RELAX-RELOC: R_RISCV_ALIGN - 0xC
72# RELAX-INST:  addi    zero, zero, 0
73# RELAX-INST:  addi    zero, zero, 0
74# RELAX-INST:  addi    zero, zero, 0
75# NORELAX-INST: addi    zero, zero, 0
76# C-OR-ZCA-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0xE
77# C-OR-ZCA-EXT-RELAX-INST:  addi    zero, zero, 0
78# C-OR-ZCA-EXT-RELAX-INST:  addi    zero, zero, 0
79# C-OR-ZCA-EXT-RELAX-INST:  addi    zero, zero, 0
80# C-OR-ZCA-EXT-RELAX-INST:  c.nop
81# C-EXT-INST: addi    zero, zero, 0
82# C-EXT-INST: c.nop
83	add	a0, a0, a1
84	.p2align 3
85.constant_pool:
86.long	3126770193
87# RELAX-RELOC: R_RISCV_ALIGN - 0x4
88# RELAX-INST:  addi    zero, zero, 0
89# NORELAX-INST: addi    zero, zero, 0
90# C-OR-ZCA-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x6
91# C-OR-ZCA-EXT-RELAX-INST:  addi    zero, zero, 0
92# C-OR-ZCA-EXT-RELAX-INST-NOT:  c.nop
93# C-EXT-INST: addi    zero, zero, 0
94# C-EXT-INST: c.nop
95	add	a0, a0, a1
96# Alignment directive with specific padding value 0x01.
97# We will not emit R_RISCV_ALIGN in this case.
98# The behavior is the same as GNU assembler.
99	.p2align 4, 1
100# RELAX-RELOC-NOT: R_RISCV_ALIGN - 0xC
101# RELAX-INST:  0101
102# RELAX-INST:  0101
103# C-OR-ZCA-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0xE
104# C-OR-ZCA-EXT-RELAX-INST:  0101
105# C-EXT-INST:  0101
106	ret
107# NORELAX-RELOC-NOT: R_RISCV
108# C-OR-ZCA-EXT-NORELAX-RELOC-NOT: R_RISCV
109# Code alignment of a byte size less than the size of a nop must be treated
110# as no alignment. This used to trigger a fatal error with relaxation enabled
111# as the calculation to emit the worst-case sequence of nops would overflow.
112	.p2align        1
113	add	a0, a0, a1
114	.p2align        0
115	add	a0, a0, a1
116# We only need to insert R_RISCV_ALIGN for code section
117# when the linker relaxation enabled.
118        .data
119	.p2align        3
120# RELAX-RELOC-NOT: R_RISCV_ALIGN
121# C-OR-ZCA-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN
122data1:
123	.word 7
124	.p2align        4
125# RELAX-RELOC-NOT: R_RISCV_ALIGN
126# C-OR-ZCA-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN
127data2:
128	.word 9
129# Check that the initial alignment is properly handled when using .option to
130# disable the C extension. This used to crash.
131# C-OR-ZCA-EXT-RELAX-INST:      <.text2>:
132# C-OR-ZCA-EXT-RELAX-INST-NEXT: add a0, a0, a1
133	.section .text2, "x"
134	.option norvc
135	.balign 4
136	add	a0, a0, a1
137