1; RUN: opt < %s -instcombine -S | grep "call.*stackrestore" | count 1
2
3declare i8* @llvm.stacksave()
4declare void @llvm.stackrestore(i8*)
5
6;; Test that llvm.stackrestore is removed when possible.
7define i32* @test1(i32 %P) {
8	%tmp = call i8* @llvm.stacksave( )
9	call void @llvm.stackrestore( i8* %tmp ) ;; not restoring anything
10	%A = alloca i32, i32 %P
11	ret i32* %A
12}
13
14define void @test2(i8* %X) {
15	call void @llvm.stackrestore( i8* %X )  ;; no allocas before return.
16	ret void
17}
18
19define void @foo(i32 %size) nounwind  {
20entry:
21	%tmp118124 = icmp sgt i32 %size, 0		; <i1> [#uses=1]
22	br i1 %tmp118124, label %bb.preheader, label %return
23
24bb.preheader:		; preds = %entry
25	%tmp25 = add i32 %size, -1		; <i32> [#uses=1]
26	%tmp125 = icmp slt i32 %size, 1		; <i1> [#uses=1]
27	%smax = select i1 %tmp125, i32 1, i32 %size		; <i32> [#uses=1]
28	br label %bb
29
30bb:		; preds = %bb, %bb.preheader
31	%i.0.reg2mem.0 = phi i32 [ 0, %bb.preheader ], [ %indvar.next, %bb ]		; <i32> [#uses=2]
32	%tmp = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
33	%tmp23 = alloca i8, i32 %size		; <i8*> [#uses=2]
34	%tmp27 = getelementptr i8* %tmp23, i32 %tmp25		; <i8*> [#uses=1]
35	store i8 0, i8* %tmp27, align 1
36	%tmp28 = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
37	%tmp52 = alloca i8, i32 %size		; <i8*> [#uses=1]
38	%tmp53 = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
39	%tmp77 = alloca i8, i32 %size		; <i8*> [#uses=1]
40	%tmp78 = call i8* @llvm.stacksave( )		; <i8*> [#uses=1]
41	%tmp102 = alloca i8, i32 %size		; <i8*> [#uses=1]
42	call void @bar( i32 %i.0.reg2mem.0, i8* %tmp23, i8* %tmp52, i8* %tmp77, i8* %tmp102, i32 %size ) nounwind
43	call void @llvm.stackrestore( i8* %tmp78 )
44	call void @llvm.stackrestore( i8* %tmp53 )
45	call void @llvm.stackrestore( i8* %tmp28 )
46	call void @llvm.stackrestore( i8* %tmp )
47	%indvar.next = add i32 %i.0.reg2mem.0, 1		; <i32> [#uses=2]
48	%exitcond = icmp eq i32 %indvar.next, %smax		; <i1> [#uses=1]
49	br i1 %exitcond, label %return, label %bb
50
51return:		; preds = %bb, %entry
52	ret void
53}
54
55declare void @bar(i32, i8*, i8*, i8*, i8*, i32)
56
57