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-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-EXT-RELAX-RELOC %s
23
24# Relaxation disabled with C extension:
25# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c,-relax < %s \
26# RUN:     | llvm-objdump -d -M no-aliases - \
27# RUN:     | FileCheck -check-prefix=C-EXT-NORELAX-INST %s
28# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+c,-relax < %s \
29# RUN:     | llvm-readobj -r - | FileCheck -check-prefix=C-EXT-NORELAX-RELOC %s
30
31# We need to insert N-MinNopSize bytes NOPs and R_RISCV_ALIGN relocation
32# type for .align N directive when linker relaxation enabled.
33# Linker could satisfy alignment by removing NOPs after linker relaxation.
34
35# The first R_RISCV_ALIGN come from
36# MCELFStreamer::InitSections() emitCodeAlignment(4).
37# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2
38# C-EXT-RELAX-INST:  c.nop
39test:
40	.p2align 2
41# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2
42# C-EXT-RELAX-INST:  c.nop
43	bne     zero, a0, .LBB0_2
44	mv	a0, zero
45	.p2align 3
46# RELAX-RELOC: R_RISCV_ALIGN - 0x4
47# RELAX-INST:  addi    zero, zero, 0
48# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x6
49# C-EXT-RELAX-INST:  addi    zero, zero, 0
50# C-EXT-RELAX-INST:  c.nop
51# C-EXT-NORELAX-INST: addi    zero, zero, 0
52	add	a0, a0, a1
53	.align 4
54.LBB0_2:
55# RELAX-RELOC: R_RISCV_ALIGN - 0xC
56# RELAX-INST:  addi    zero, zero, 0
57# RELAX-INST:  addi    zero, zero, 0
58# RELAX-INST:  addi    zero, zero, 0
59# NORELAX-INST: addi    zero, zero, 0
60# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0xE
61# C-EXT-RELAX-INST:  addi    zero, zero, 0
62# C-EXT-RELAX-INST:  addi    zero, zero, 0
63# C-EXT-RELAX-INST:  addi    zero, zero, 0
64# C-EXT-RELAX-INST:  c.nop
65# C-EXT-INST: addi    zero, zero, 0
66# C-EXT-INST: c.nop
67	add	a0, a0, a1
68	.p2align 3
69.constant_pool:
70.long	3126770193
71# RELAX-RELOC: R_RISCV_ALIGN - 0x4
72# RELAX-INST:  addi    zero, zero, 0
73# NORELAX-INST: addi    zero, zero, 0
74# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x6
75# C-EXT-RELAX-INST:  addi    zero, zero, 0
76# C-EXT-RELAX-INST:  c.nop
77# C-EXT-INST: addi    zero, zero, 0
78# C-EXT-INST: c.nop
79	add	a0, a0, a1
80# Alignment directive with specific padding value 0x01.
81# We will not emit R_RISCV_ALIGN in this case.
82# The behavior is the same as GNU assembler.
83	.p2align 4, 1
84# RELAX-RELOC-NOT: R_RISCV_ALIGN - 0xC
85# RELAX-INST:  01 01
86# RELAX-INST:  01 01
87# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0xE
88# C-EXT-RELAX-INST:  01 01
89# C-EXT-INST:  01 01
90	ret
91# NORELAX-RELOC-NOT: R_RISCV
92# C-EXT-NORELAX-RELOC-NOT: R_RISCV
93# Code alignment of a byte size less than the size of a nop must be treated
94# as no alignment. This used to trigger a fatal error with relaxation enabled
95# as the calculation to emit the worst-case sequence of nops would overflow.
96	.p2align        1
97	add	a0, a0, a1
98	.p2align        0
99	add	a0, a0, a1
100# We only need to insert R_RISCV_ALIGN for code section
101# when the linker relaxation enabled.
102        .data
103	.p2align        3
104# RELAX-RELOC-NOT: R_RISCV_ALIGN
105# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN
106data1:
107	.word 7
108	.p2align        4
109# RELAX-RELOC-NOT: R_RISCV_ALIGN
110# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN
111data2:
112	.word 9
113