xref: /llvm-project/llvm/test/Transforms/InstCombine/stacksaverestore.ll (revision 25bc999d1fb2efccc3ece398550af738aea7d310)
1; RUN: opt < %s -passes=instcombine -S | FileCheck %s
2
3@glob = global i32 0
4
5declare ptr @llvm.stacksave()
6declare void @llvm.stackrestore(ptr)
7
8;; Test that llvm.stackrestore is removed when possible.
9define ptr @test1(i32 %P) {
10	%tmp = call ptr @llvm.stacksave( )
11	call void @llvm.stackrestore( ptr %tmp ) ;; not restoring anything
12	%A = alloca i32, i32 %P
13	ret ptr %A
14}
15
16; CHECK-LABEL: define ptr @test1(
17; CHECK-NOT: call void @llvm.stackrestore
18; CHECK: ret ptr
19
20define void @test2(ptr %X) {
21	call void @llvm.stackrestore( ptr %X )  ;; no allocas before return.
22	ret void
23}
24
25; CHECK-LABEL: define void @test2(
26; CHECK-NOT: call void @llvm.stackrestore
27; CHECK: ret void
28
29define void @foo(i32 %size) nounwind  {
30entry:
31	%tmp118124 = icmp sgt i32 %size, 0		; <i1> [#uses=1]
32	br i1 %tmp118124, label %bb.preheader, label %return
33
34bb.preheader:		; preds = %entry
35	%tmp25 = add i32 %size, -1		; <i32> [#uses=1]
36	%tmp125 = icmp slt i32 %size, 1		; <i1> [#uses=1]
37	%smax = select i1 %tmp125, i32 1, i32 %size		; <i32> [#uses=1]
38	br label %bb
39
40bb:		; preds = %bb, %bb.preheader
41	%i.0.reg2mem.0 = phi i32 [ 0, %bb.preheader ], [ %indvar.next, %bb ]		; <i32> [#uses=2]
42	%tmp = call ptr @llvm.stacksave( )		; <ptr> [#uses=1]
43	%tmp23 = alloca i8, i32 %size		; <ptr> [#uses=2]
44	%tmp27 = getelementptr i8, ptr %tmp23, i32 %tmp25		; <ptr> [#uses=1]
45	store i8 0, ptr %tmp27, align 1
46	%tmp28 = call ptr @llvm.stacksave( )		; <ptr> [#uses=1]
47	%tmp52 = alloca i8, i32 %size		; <ptr> [#uses=1]
48	%tmp53 = call ptr @llvm.stacksave( )		; <ptr> [#uses=1]
49	%tmp77 = alloca i8, i32 %size		; <ptr> [#uses=1]
50	%tmp78 = call ptr @llvm.stacksave( )		; <ptr> [#uses=1]
51	%tmp102 = alloca i8, i32 %size		; <ptr> [#uses=1]
52	call void @bar( i32 %i.0.reg2mem.0, ptr %tmp23, ptr %tmp52, ptr %tmp77, ptr %tmp102, i32 %size ) nounwind
53	call void @llvm.stackrestore( ptr %tmp78 )
54	call void @llvm.stackrestore( ptr %tmp53 )
55	call void @llvm.stackrestore( ptr %tmp28 )
56	call void @llvm.stackrestore( ptr %tmp )
57	%indvar.next = add i32 %i.0.reg2mem.0, 1		; <i32> [#uses=2]
58	%exitcond = icmp eq i32 %indvar.next, %smax		; <i1> [#uses=1]
59	br i1 %exitcond, label %return, label %bb
60
61return:		; preds = %bb, %entry
62	ret void
63}
64
65; CHECK-LABEL: define void @foo(
66; CHECK: %tmp = call ptr @llvm.stacksave.p0()
67; CHECK: alloca i8
68; CHECK-NOT: stacksave
69; CHECK: call void @bar(
70; CHECK-NEXT: call void @llvm.stackrestore.p0(ptr %tmp)
71; CHECK: ret void
72
73declare void @bar(i32, ptr, ptr, ptr, ptr, i32)
74
75declare void @inalloca_callee(ptr inalloca(i32))
76
77define void @test3(i32 %c) {
78entry:
79  br label %loop
80
81loop:
82  %i = phi i32 [0, %entry], [%i1, %loop]
83  %save1 = call ptr @llvm.stacksave()
84  %argmem = alloca inalloca i32
85  store i32 0, ptr %argmem
86  call void @inalloca_callee(ptr inalloca(i32) %argmem)
87
88  ; This restore cannot be deleted, the restore below does not make it dead.
89  call void @llvm.stackrestore(ptr %save1)
90
91  ; FIXME: We should be able to remove this save/restore pair, but we don't.
92  %save2 = call ptr @llvm.stacksave()
93  store i32 0, ptr @glob
94  call void @llvm.stackrestore(ptr %save2)
95  %i1 = add i32 1, %i
96  %done = icmp eq i32 %i1, %c
97  br i1 %done, label %loop, label %return
98
99return:
100  ret void
101}
102
103; CHECK-LABEL: define void @test3(
104; CHECK: loop:
105; CHECK: %i = phi i32 [ 0, %entry ], [ %i1, %loop ]
106; CHECK: %save1 = call ptr @llvm.stacksave.p0()
107; CHECK: %argmem = alloca inalloca i32
108; CHECK: store i32 0, ptr %argmem
109; CHECK: call void @inalloca_callee(ptr {{.*}} inalloca(i32) %argmem)
110; CHECK: call void @llvm.stackrestore.p0(ptr %save1)
111; CHECK: br i1 %done, label %loop, label %return
112; CHECK: ret void
113
114define i32 @test4(i32 %m, ptr %a, ptr %b) {
115entry:
116  br label %for.body
117
118for.body:
119  %x.012 = phi i32 [ 0, %entry ], [ %add2, %for.body ]
120  %i.011 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
121  %0 = call ptr @llvm.stacksave()
122  %load1 = load i32, ptr %a, align 4
123  %mul1 = mul nsw i32 %load1, %m
124  %add1 = add nsw i32 %mul1, %x.012
125  call void @llvm.stackrestore(ptr %0)
126  %load2 = load i32, ptr %b, align 4
127  %mul2 = mul nsw i32 %load2, %m
128  %add2 = add nsw i32 %mul2, %add1
129  call void @llvm.stackrestore(ptr %0)
130  %inc = add nuw nsw i32 %i.011, 1
131  %exitcond.not = icmp eq i32 %inc, 100
132  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
133
134for.cond.cleanup:
135  ret i32 %add2
136}
137
138; CHECK-LABEL: define i32 @test4(
139; CHECK-NOT: call void @llvm.stackrestore
140; CHECK: ret i32 %add2
141