1 ; RUN: llc -mtriple=thumb-eabi < %s -o - | FileCheck %s
3 ; Check that stack addresses are generated using a single ADD
4 define void @test1(i8** %p) {
5 %x = alloca i8, align 1
6 %y = alloca i8, align 1
7 %z = alloca i8, align 1
8 ; CHECK: add r1, sp, #8
10 store volatile i8* %x, i8** %p, align 4
11 ; CHECK: add r1, sp, #4
13 store volatile i8* %y, i8** %p, align 4
16 store volatile i8* %z, i8** %p, align 4
20 ; Stack offsets larger than 1020 still need two ADDs
21 define void @test2([1024 x i8]** %p) {
22 %arr1 = alloca [1024 x i8], align 1
23 %arr2 = alloca [1024 x i8], align 1
24 ; CHECK: add r1, sp, #1020
27 store volatile [1024 x i8]* %arr1, [1024 x i8]** %p, align 4
30 store volatile [1024 x i8]* %arr2, [1024 x i8]** %p, align 4
34 ; If possible stack-based lrdb/ldrh are widened to use SP-based addressing
35 define i32 @test3() #0 {
36 %x = alloca i8, align 1
37 %y = alloca i8, align 1
39 %1 = load i8, i8* %x, align 1
40 ; CHECK: ldr r1, [sp, #4]
41 %2 = load i8, i8* %y, align 1
42 %3 = add nsw i8 %1, %2
43 %4 = zext i8 %3 to i32
47 define i32 @test4() #0 {
48 %x = alloca i16, align 2
49 %y = alloca i16, align 2
51 %1 = load i16, i16* %x, align 2
52 ; CHECK: ldr r1, [sp, #4]
53 %2 = load i16, i16* %y, align 2
54 %3 = add nsw i16 %1, %2
55 %4 = zext i16 %3 to i32
59 ; Don't widen if the value needs to be zero-extended
60 define zeroext i8 @test5() {
61 %x = alloca i8, align 1
63 ; CHECK: ldrb r0, [r0]
64 %1 = load i8, i8* %x, align 1
68 define zeroext i16 @test6() {
69 %x = alloca i16, align 2
71 ; CHECK: ldrh r0, [r0]
72 %1 = load i16, i16* %x, align 2
76 ; Accessing the bottom of a large array shouldn't require materializing a base
78 ; CHECK: movs [[REG:r[0-9]+]], #1
79 ; CHECK: str [[REG]], [sp, #16]
80 ; CHECK: str [[REG]], [sp, #4]
82 define void @test7() {
83 %arr = alloca [200 x i32], align 4
85 %arrayidx = getelementptr inbounds [200 x i32], [200 x i32]* %arr, i32 0, i32 1
86 store i32 1, i32* %arrayidx, align 4
88 %arrayidx1 = getelementptr inbounds [200 x i32], [200 x i32]* %arr, i32 0, i32 4
89 store i32 1, i32* %arrayidx1, align 4
94 ; Check that loads/stores with out-of-range offsets are handled correctly
95 define void @test8() {
96 %arr3 = alloca [224 x i32], align 4
97 %arr2 = alloca [224 x i32], align 4
98 %arr1 = alloca [224 x i32], align 4
100 ; CHECK: movs [[REG:r[0-9]+]], #1
101 ; CHECK-DAG: str [[REG]], [sp]
102 %arr1idx1 = getelementptr inbounds [224 x i32], [224 x i32]* %arr1, i32 0, i32 0
103 store i32 1, i32* %arr1idx1, align 4
105 ; Offset in range for sp-based store, but not for non-sp-based store
106 ; CHECK-DAG: str [[REG]], [sp, #128]
107 %arr1idx2 = getelementptr inbounds [224 x i32], [224 x i32]* %arr1, i32 0, i32 32
108 store i32 1, i32* %arr1idx2, align 4
110 ; CHECK-DAG: str [[REG]], [sp, #896]
111 %arr2idx1 = getelementptr inbounds [224 x i32], [224 x i32]* %arr2, i32 0, i32 0
112 store i32 1, i32* %arr2idx1, align 4
114 ; %arr2 is in range, but this element of it is not
115 ; CHECK-DAG: ldr [[RA:r[0-9]+]], .LCPI7_2
116 ; CHECK-DAG: add [[RA]], sp
117 ; CHECK-DAG: str [[REG]], [{{r[0-9]+}}]
118 %arr2idx2 = getelementptr inbounds [224 x i32], [224 x i32]* %arr2, i32 0, i32 32
119 store i32 1, i32* %arr2idx2, align 4
121 ; %arr3 is not in range
122 ; CHECK-DAG: ldr [[RB:r[0-9]+]], .LCPI7_3
123 ; CHECK-DAG: add [[RB]], sp
124 ; CHECK-DAG: str [[REG]], [{{r[0-9]+}}]
125 %arr3idx1 = getelementptr inbounds [224 x i32], [224 x i32]* %arr3, i32 0, i32 0
126 store i32 1, i32* %arr3idx1, align 4
128 ; CHECK-DAG: ldr [[RC:r[0-9]+]], .LCPI7_4
129 ; CHECK-DAG: add [[RC]], sp
130 ; CHECK-DAG: str [[REG]], [{{r[0-9]+}}]
131 %arr3idx2 = getelementptr inbounds [224 x i32], [224 x i32]* %arr3, i32 0, i32 32
132 store i32 1, i32* %arr3idx2, align 4