1 ; RUN: opt < %s -tailcallelim -S | not grep call 2 ; PR4323 3 4 ; Several cases where tail call elimination should move the load above the call, 5 ; then eliminate the tail recursion. 6 7 8 @global = external global i32 ; <i32*> [#uses=1] 9 @extern_weak_global = extern_weak global i32 ; <i32*> [#uses=1] 10 11 12 ; This load can be moved above the call because the function won't write to it 13 ; and the call has no side effects. 14 define fastcc i32 @raise_load_1(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind readonly { 15 entry: 16 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1] 17 br i1 %tmp2, label %if, label %else 18 19 if: ; preds = %entry 20 ret i32 0 21 22 else: ; preds = %entry 23 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1] 24 %tmp8 = call fastcc i32 @raise_load_1(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1] 25 %tmp9 = load i32* %a_arg ; <i32> [#uses=1] 26 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1] 27 ret i32 %tmp10 28 } 29 30 31 ; This load can be moved above the call because the function won't write to it 32 ; and the load provably can't trap. 33 define fastcc i32 @raise_load_2(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly { 34 entry: 35 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1] 36 br i1 %tmp2, label %if, label %else 37 38 if: ; preds = %entry 39 ret i32 0 40 41 else: ; preds = %entry 42 %nullcheck = icmp eq i32* %a_arg, null ; <i1> [#uses=1] 43 br i1 %nullcheck, label %unwind, label %recurse 44 45 unwind: ; preds = %else 46 unreachable 47 48 recurse: ; preds = %else 49 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1] 50 %tmp8 = call fastcc i32 @raise_load_2(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1] 51 %tmp9 = load i32* @global ; <i32> [#uses=1] 52 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1] 53 ret i32 %tmp10 54 } 55 56 57 ; This load can be safely moved above the call (even though it's from an 58 ; extern_weak global) because the call has no side effects. 59 define fastcc i32 @raise_load_3(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind readonly { 60 entry: 61 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1] 62 br i1 %tmp2, label %if, label %else 63 64 if: ; preds = %entry 65 ret i32 0 66 67 else: ; preds = %entry 68 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1] 69 %tmp8 = call fastcc i32 @raise_load_3(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1] 70 %tmp9 = load i32* @extern_weak_global ; <i32> [#uses=1] 71 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1] 72 ret i32 %tmp10 73 } 74 75 76 ; The second load can be safely moved above the call even though it's from an 77 ; unknown pointer (which normally means it might trap) because the first load 78 ; proves it doesn't trap. 79 define fastcc i32 @raise_load_4(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly { 80 entry: 81 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1] 82 br i1 %tmp2, label %if, label %else 83 84 if: ; preds = %entry 85 ret i32 0 86 87 else: ; preds = %entry 88 %nullcheck = icmp eq i32* %a_arg, null ; <i1> [#uses=1] 89 br i1 %nullcheck, label %unwind, label %recurse 90 91 unwind: ; preds = %else 92 unreachable 93 94 recurse: ; preds = %else 95 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1] 96 %first = load i32* %a_arg ; <i32> [#uses=1] 97 %tmp8 = call fastcc i32 @raise_load_4(i32* %a_arg, i32 %first, i32 %tmp7) ; <i32> [#uses=1] 98 %second = load i32* %a_arg ; <i32> [#uses=1] 99 %tmp10 = add i32 %second, %tmp8 ; <i32> [#uses=1] 100 ret i32 %tmp10 101 } 102