xref: /llvm-project/llvm/test/CodeGen/AArch64/shrink-wrapping-vla.ll (revision db158c7c830807caeeb0691739c41f1d522029e9)
1; Test shrink wrapping placement is correct with respect to calls to llvm.{stacksave,stackrestore}
2
3; void f(int n, int x[]) {
4;   if (n < 0)
5;     return;
6;
7;  int a[n];
8;
9;  for (int i = 0; i < n; i++)
10;    a[i] = x[n - i - 1];
11;
12;  for (int i = 0; i < n; i++)
13;    x[i] = a[i] + 1;
14; }
15;
16; RUN: llc -mtriple aarch64-linux %s -o - | FileCheck %s
17
18define dso_local void @f(i32 %n, ptr nocapture %x) uwtable {
19entry:
20  %cmp = icmp slt i32 %n, 0
21  br i1 %cmp, label %return, label %if.end
22
23if.end:                                           ; preds = %entry
24  %0 = zext i32 %n to i64
25  %1 = tail call ptr @llvm.stacksave()
26  %vla = alloca i32, i64 %0, align 16
27  %cmp132 = icmp eq i32 %n, 0
28  br i1 %cmp132, label %for.cond.cleanup8, label %for.body.lr.ph
29
30for.body.lr.ph:                                   ; preds = %if.end
31  %sub = add i32 %n, -1
32  br label %for.body
33
34for.cond6.preheader:                              ; preds = %for.body
35  %cmp730 = icmp sgt i32 %n, 0
36  br i1 %cmp730, label %for.body9, label %for.cond.cleanup8
37
38for.body:                                         ; preds = %for.body, %for.body.lr.ph
39  %indvars.iv34 = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next35, %for.body ]
40  %2 = trunc i64 %indvars.iv34 to i32
41  %sub2 = sub i32 %sub, %2
42  %idxprom = sext i32 %sub2 to i64
43  %arrayidx = getelementptr inbounds i32, ptr %x, i64 %idxprom
44  %3 = load i32, ptr %arrayidx, align 4
45  %arrayidx4 = getelementptr inbounds i32, ptr %vla, i64 %indvars.iv34
46  store i32 %3, ptr %arrayidx4, align 4
47  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
48  %exitcond37 = icmp eq i64 %indvars.iv.next35, %0
49  br i1 %exitcond37, label %for.cond6.preheader, label %for.body
50
51for.cond.cleanup8:                                ; preds = %for.body9, %if.end, %for.cond6.preheader
52  tail call void @llvm.stackrestore(ptr %1)
53  br label %return
54
55for.body9:                                        ; preds = %for.cond6.preheader, %for.body9
56  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body9 ], [ 0, %for.cond6.preheader ]
57  %arrayidx11 = getelementptr inbounds i32, ptr %vla, i64 %indvars.iv
58  %4 = load i32, ptr %arrayidx11, align 4
59  %add = add nsw i32 %4, 1
60  %arrayidx13 = getelementptr inbounds i32, ptr %x, i64 %indvars.iv
61  store i32 %add, ptr %arrayidx13, align 4
62  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
63  %exitcond = icmp eq i64 %indvars.iv.next, %0
64  br i1 %exitcond, label %for.cond.cleanup8, label %for.body9
65
66return:                                           ; preds = %entry, %for.cond.cleanup8
67  ret void
68}
69
70; Function Attrs: nounwind
71declare ptr @llvm.stacksave()
72
73; Function Attrs: nounwind
74declare void @llvm.stackrestore(ptr)
75
76; Check that llvm.stackrestore() happens before CSRs are popped off the stack
77
78; CHECK-LABEL: f
79
80; CHECK:      stp x29, x30, [sp, #-16]!
81; CHECK-NEXT: .cfi_def_cfa_offset 16
82; CHECK-NEXT: mov x29, sp
83; CHECK-NEXT: .cfi_def_cfa w29, 16
84; CHECK-NEXT: .cfi_offset w30, -8
85; CHECK-NEXT: .cfi_offset w29, -16
86
87
88; VLA allocation
89; CHECK: ubfiz	x8, x0, #2, #32
90; CHECK: mov	x9, sp
91; CHECK: mov	[[SAVE:x[0-9]+]], sp
92; CHECK: add	x8, x8, #15
93; CHECK: and	[[X1:x[0-9]+]], [[X1]], #0x7fffffff0
94; Saving the SP via llvm.stacksave()
95; CHECK: sub	[[X1]], [[X2:x[0-9]+]], [[X1]]
96
97; The next instruction comes from llvm.stackrestore()
98; CHECK:      mov sp, [[SAVE]]
99; Epilogue
100; CHECK-NEXT: mov sp, x29
101; CHECK-NEXT: .cfi_def_cfa wsp, 16
102; CHECK-NEXT: ldp x29, x30, [sp], #16
103; CHECK-NEXT: .cfi_def_cfa_offset 0
104; CHECK-NEXT: .cfi_restore w30
105; CHECK-NEXT: .cfi_restore w29
106; CHECK-NEXT:  ret
107