141 lines · plain
1; RUN: opt < %s -passes=instcombine -S | FileCheck %s2 3@glob = global i32 04 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 anything12 %A = alloca i32, i32 %P13 ret ptr %A14}15 16; CHECK-LABEL: define ptr @test1(17; CHECK-NOT: call void @llvm.stackrestore18; CHECK: ret ptr19 20define void @test2(ptr %X) {21 call void @llvm.stackrestore( ptr %X ) ;; no allocas before return.22 ret void23}24 25; CHECK-LABEL: define void @test2(26; CHECK-NOT: call void @llvm.stackrestore27; CHECK: ret void28 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 %return33 34bb.preheader: ; preds = %entry35 %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 %bb39 40bb: ; preds = %bb, %bb.preheader41 %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 146 %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 ) nounwind53 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 %bb60 61return: ; preds = %bb, %entry62 ret void63}64 65; CHECK-LABEL: define void @foo(66; CHECK: %tmp = call ptr @llvm.stacksave.p0()67; CHECK: alloca i868; CHECK-NOT: stacksave69; CHECK: call void @bar(70; CHECK-NEXT: call void @llvm.stackrestore.p0(ptr %tmp)71; CHECK: ret void72 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 %loop80 81loop:82 %i = phi i32 [0, %entry], [%i1, %loop]83 %save1 = call ptr @llvm.stacksave()84 %argmem = alloca inalloca i3285 store i32 0, ptr %argmem86 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 @glob94 call void @llvm.stackrestore(ptr %save2)95 %i1 = add i32 1, %i96 %done = icmp eq i32 %i1, %c97 br i1 %done, label %loop, label %return98 99return:100 ret void101}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 i32108; CHECK: store i32 0, ptr %argmem109; 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 %return112; CHECK: ret void113 114define i32 @test4(i32 %m, ptr %a, ptr %b) {115entry:116 br label %for.body117 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 4123 %mul1 = mul nsw i32 %load1, %m124 %add1 = add nsw i32 %mul1, %x.012125 call void @llvm.stackrestore(ptr %0)126 %load2 = load i32, ptr %b, align 4127 %mul2 = mul nsw i32 %load2, %m128 %add2 = add nsw i32 %mul2, %add1129 call void @llvm.stackrestore(ptr %0)130 %inc = add nuw nsw i32 %i.011, 1131 %exitcond.not = icmp eq i32 %inc, 100132 br i1 %exitcond.not, label %for.cond.cleanup, label %for.body133 134for.cond.cleanup:135 ret i32 %add2136}137 138; CHECK-LABEL: define i32 @test4(139; CHECK-NOT: call void @llvm.stackrestore140; CHECK: ret i32 %add2141