1 ; RUN: opt < %s -tailcallelim -verify-dom-info -S | FileCheck %s
4 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6 ; Several cases where tail call elimination should move the load above the call,
7 ; then eliminate the tail recursion.
11 @global = external global i32 ; <i32*> [#uses=1]
12 @extern_weak_global = extern_weak global i32 ; <i32*> [#uses=1]
15 ; This load can be moved above the call because the function won't write to it
16 ; and the call has no side effects.
17 define fastcc i32 @raise_load_1(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind readonly {
18 ; CHECK-LABEL: @raise_load_1(
20 ; CHECK: load i32, i32*
24 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
25 br i1 %tmp2, label %if, label %else
30 else: ; preds = %entry
31 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
32 %tmp8 = call fastcc i32 @raise_load_1(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
33 %tmp9 = load i32, i32* %a_arg ; <i32> [#uses=1]
34 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
39 ; This load can be moved above the call because the function won't write to it
40 ; and the load provably can't trap.
41 define fastcc i32 @raise_load_2(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
42 ; CHECK-LABEL: @raise_load_2(
44 ; CHECK: load i32, i32*
48 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
49 br i1 %tmp2, label %if, label %else
54 else: ; preds = %entry
55 %nullcheck = icmp eq i32* %a_arg, null ; <i1> [#uses=1]
56 br i1 %nullcheck, label %unwind, label %recurse
58 unwind: ; preds = %else
61 recurse: ; preds = %else
62 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
63 %tmp8 = call fastcc i32 @raise_load_2(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
64 %tmp9 = load i32, i32* @global ; <i32> [#uses=1]
65 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
70 ; This load can be safely moved above the call (even though it's from an
71 ; extern_weak global) because the call has no side effects.
72 define fastcc i32 @raise_load_3(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind readonly {
73 ; CHECK-LABEL: @raise_load_3(
75 ; CHECK: load i32, i32*
79 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
80 br i1 %tmp2, label %if, label %else
85 else: ; preds = %entry
86 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
87 %tmp8 = call fastcc i32 @raise_load_3(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
88 %tmp9 = load i32, i32* @extern_weak_global ; <i32> [#uses=1]
89 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
94 ; The second load can be safely moved above the call even though it's from an
95 ; unknown pointer (which normally means it might trap) because the first load
96 ; proves it doesn't trap.
97 define fastcc i32 @raise_load_4(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
98 ; CHECK-LABEL: @raise_load_4(
100 ; CHECK: load i32, i32*
101 ; CHECK-NEXT: load i32, i32*
105 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
106 br i1 %tmp2, label %if, label %else
111 else: ; preds = %entry
112 %nullcheck = icmp eq i32* %a_arg, null ; <i1> [#uses=1]
113 br i1 %nullcheck, label %unwind, label %recurse
115 unwind: ; preds = %else
118 recurse: ; preds = %else
119 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
120 %first = load i32, i32* %a_arg ; <i32> [#uses=1]
121 %tmp8 = call fastcc i32 @raise_load_4(i32* %a_arg, i32 %first, i32 %tmp7) ; <i32> [#uses=1]
122 %second = load i32, i32* %a_arg ; <i32> [#uses=1]
123 %tmp10 = add i32 %second, %tmp8 ; <i32> [#uses=1]
127 ; This load can be moved above the call because the function won't write to it
128 ; and the a_arg is dereferenceable.
129 define fastcc i32 @raise_load_5(i32* dereferenceable(4) %a_arg, i32 %a_len_arg, i32 %start_arg) readonly {
130 ; CHECK-LABEL: @raise_load_5(
132 ; CHECK: load i32, i32*
136 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
137 br i1 %tmp2, label %if, label %else
142 else: ; preds = %entry
143 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
144 %tmp8 = call fastcc i32 @raise_load_5(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
145 %tmp9 = load i32, i32* %a_arg ; <i32> [#uses=1]
146 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]
150 ; This load can be moved above the call because the function call does not write to the memory the load
151 ; is accessing and the load is safe to speculate.
152 define fastcc i32 @raise_load_6(i32* %a_arg, i32 %a_len_arg, i32 %start_arg) nounwind {
153 ; CHECK-LABEL: @raise_load_6(
155 ; CHECK: load i32, i32*
161 %tmp2 = icmp sge i32 %start_arg, %a_len_arg ; <i1> [#uses=1]
162 br i1 %tmp2, label %if, label %else
165 store i32 1, i32* %a_arg
168 else: ; preds = %entry
169 %tmp7 = add i32 %start_arg, 1 ; <i32> [#uses=1]
170 %tmp8 = call fastcc i32 @raise_load_6(i32* %a_arg, i32 %a_len_arg, i32 %tmp7) ; <i32> [#uses=1]
171 %tmp9 = load i32, i32* %s ; <i32> [#uses=1]
172 %tmp10 = add i32 %tmp9, %tmp8 ; <i32> [#uses=1]