diff --git a/llvm/test/Transforms/FunctionAttrs/SCC1.ll b/llvm/test/Transforms/FunctionAttrs/SCC1.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/FunctionAttrs/SCC1.ll @@ -0,0 +1,169 @@ +; RUN: opt -S -functionattrs -enable-nonnull-arg-prop %s | FileCheck %s +; +; This is an evolved example to stress test SCC parameter attribute propagation. +; The SCC in this test is made up of the following six function, three of which +; are internal and three externally visible: +; +; static int* internal_ret0_nw(int *n0, int *w0); +; static int* internal_ret1_rw(int *r0, int *w0); +; static int* internal_ret1_rrw(int *r0, int *r1, int *w0); +; int* external_ret2_nrw(int *n0, int *r0, int *w0); +; int* external_sink_ret2_nrw(int *n0, int *r0, int *w0); +; int* external_source_ret2_nrw(int *n0, int *r0, int *w0); +; +; The top four functions call each other while the "sink" function will not +; call anything and the "source" function will not be called in this module. +; The names of the functions define the returned parameter (X for "_retX_"), +; as well as how the parameters are (transitively) used (n = readnone, +; r = readonly, w = writeonly). +; +; Currently we get the following statistics: +; 1 functionattrs - Number of functions marked as norecurse +; 6 functionattrs - Number of functions marked as nounwind +; 1 functionattrs - Number of arguments marked nocapture +; 1 functionattrs - Number of arguments marked readnone +; 1 functionattrs - Number of arguments marked readonly +; 1 functionattrs - Number of arguments marked returned +; -- +; 11 +; +; This basically describes that we only derived attributes for the "sink" +; function (except nounwind which is properly derived). +; +; +; What we should get is something along the lines of: +; 1 functionattrs - Number of functions marked as norecurse +; 6 functionattrs - Number of functions marked argmemonly +; 6 functionattrs - Number of functions marked as nounwind +; 16 functionattrs - Number of arguments marked nocapture +; 4 functionattrs - Number of arguments marked readnone +; 6 functionattrs - Number of arguments marked writeonly +; 6 functionattrs - Number of arguments marked readonly +; 6 functionattrs - Number of arguments marked returned +; -- +; 51 +; +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +; CHECK: define dso_local i32* @external_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) #[[NOUNWIND:[0-9]*]] +define dso_local i32* @external_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) { +entry: + %call = call i32* @internal_ret0_nw(i32* %n0, i32* %w0) + %call1 = call i32* @internal_ret1_rrw(i32* %r0, i32* %r0, i32* %w0) + %call2 = call i32* @external_sink_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) + %call3 = call i32* @internal_ret1_rw(i32* %r0, i32* %w0) + ret i32* %call3 +} + +; CHECK: define internal i32* @internal_ret0_nw(i32* %n0, i32* %w0) #[[NOUNWIND]] +define internal i32* @internal_ret0_nw(i32* %n0, i32* %w0) { +entry: + %r0 = alloca i32, align 4 + %r1 = alloca i32, align 4 + %tobool = icmp ne i32* %n0, null + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %entry + br label %return + +if.end: ; preds = %entry + store i32 3, i32* %r0, align 4 + store i32 5, i32* %r1, align 4 + store i32 1, i32* %w0, align 4 + %call = call i32* @internal_ret1_rrw(i32* %r0, i32* %r1, i32* %w0) + %call1 = call i32* @external_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) + %call2 = call i32* @external_ret2_nrw(i32* %n0, i32* %r1, i32* %w0) + %call3 = call i32* @external_sink_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) + %call4 = call i32* @external_sink_ret2_nrw(i32* %n0, i32* %r1, i32* %w0) + %call5 = call i32* @internal_ret0_nw(i32* %n0, i32* %w0) + br label %return + +return: ; preds = %if.end, %if.then + %retval.0 = phi i32* [ %call5, %if.end ], [ %n0, %if.then ] + ret i32* %retval.0 +} + +; CHECK: define internal i32* @internal_ret1_rrw(i32* %r0, i32* %r1, i32* %w0) #[[NOUNWIND]] +define internal i32* @internal_ret1_rrw(i32* %r0, i32* %r1, i32* %w0) { +entry: + %0 = load i32, i32* %r0, align 4 + %tobool = icmp ne i32 %0, 0 + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %entry + br label %return + +if.end: ; preds = %entry + %call = call i32* @internal_ret1_rw(i32* %r0, i32* %w0) + %1 = load i32, i32* %r0, align 4 + %2 = load i32, i32* %r1, align 4 + %add = add nsw i32 %1, %2 + store i32 %add, i32* %w0, align 4 + %call1 = call i32* @internal_ret1_rw(i32* %r1, i32* %w0) + %call2 = call i32* @internal_ret0_nw(i32* %r0, i32* %w0) + %call3 = call i32* @internal_ret0_nw(i32* %w0, i32* %w0) + %call4 = call i32* @external_ret2_nrw(i32* %r0, i32* %r1, i32* %w0) + %call5 = call i32* @external_ret2_nrw(i32* %r1, i32* %r0, i32* %w0) + %call6 = call i32* @external_sink_ret2_nrw(i32* %r0, i32* %r1, i32* %w0) + %call7 = call i32* @external_sink_ret2_nrw(i32* %r1, i32* %r0, i32* %w0) + %call8 = call i32* @internal_ret0_nw(i32* %r1, i32* %w0) + br label %return + +return: ; preds = %if.end, %if.then + %retval.0 = phi i32* [ %call8, %if.end ], [ %r1, %if.then ] + ret i32* %retval.0 +} + +; CHECK: define dso_local i32* @external_sink_ret2_nrw(i32* readnone %n0, i32* nocapture readonly %r0, i32* returned %w0) #[[NOREC_NOUNWIND:[0-9]*]] +define dso_local i32* @external_sink_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) { +entry: + %tobool = icmp ne i32* %n0, null + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %entry + br label %return + +if.end: ; preds = %entry + %0 = load i32, i32* %r0, align 4 + store i32 %0, i32* %w0, align 4 + br label %return + +return: ; preds = %if.end, %if.then + ret i32* %w0 +} + +; CHECK: define internal i32* @internal_ret1_rw(i32* %r0, i32* %w0) #[[NOUNWIND]] +define internal i32* @internal_ret1_rw(i32* %r0, i32* %w0) { +entry: + %0 = load i32, i32* %r0, align 4 + %tobool = icmp ne i32 %0, 0 + br i1 %tobool, label %if.end, label %if.then + +if.then: ; preds = %entry + br label %return + +if.end: ; preds = %entry + %call = call i32* @internal_ret1_rrw(i32* %r0, i32* %r0, i32* %w0) + %1 = load i32, i32* %r0, align 4 + store i32 %1, i32* %w0, align 4 + %call1 = call i32* @internal_ret0_nw(i32* %r0, i32* %w0) + %call2 = call i32* @internal_ret0_nw(i32* %w0, i32* %w0) + %call3 = call i32* @external_sink_ret2_nrw(i32* %r0, i32* %r0, i32* %w0) + %call4 = call i32* @external_ret2_nrw(i32* %r0, i32* %r0, i32* %w0) + br label %return + +return: ; preds = %if.end, %if.then + %retval.0 = phi i32* [ %call4, %if.end ], [ %w0, %if.then ] + ret i32* %retval.0 +} + +; CHECK: define dso_local i32* @external_source_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) #[[NOUNWIND]] +define dso_local i32* @external_source_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) { +entry: + %call = call i32* @external_sink_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) + %call1 = call i32* @external_ret2_nrw(i32* %n0, i32* %r0, i32* %w0) + ret i32* %call1 +} + +; CHECK: attributes #[[NOUNWIND]] = { nounwind } +; CHECK: attributes #[[NOREC_NOUNWIND]] = { norecurse nounwind } diff --git a/llvm/test/Transforms/FunctionAttrs/arg_nocapture.ll b/llvm/test/Transforms/FunctionAttrs/arg_nocapture.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/FunctionAttrs/arg_nocapture.ll @@ -0,0 +1,477 @@ +; RUN: opt -functionattrs -S < %s | FileCheck %s +; +; Test cases specifically designed for the "no-capture" argument attribute. +; We use FIXME's to indicate problems and missing attributes. +; +; TEST 1: comparison against NULL +; TEST 2: comparison against NULL in control flow +; TEST 3: singleton SCC +; TEST 4: singleton SCC with lots of nested recursive calls +; TEST 5: SCC with various calls, casts, and comparisons agains NULL +; TEST 6: call to external function, marked no-capture +; TEST 7: call to external var-args function, marked no-capture +; TEST 8: "captured" only through return +; TEST 9: "captured" only through return +; TEST 10: calls to "captured" only through return functions +; TEST 11: "captured" only through transitive return +; TEST 12: captured through write +; TEST 13: "captured" only through transitive return +; TEST 14: captured through write +; TEST 15: return argument or unknown call result +; TEST 16: not captured by readonly external function +; TEST 17: not captured by readonly external function if return chain is known +; +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +; TEST 1: +; +; int is_null_return(int *p) { +; return p == 0; +; } +; +; FIXME: no-capture missing for %p +; CHECK: define dso_local i32 @is_null_return(i32* readnone %p) +; +define dso_local i32 @is_null_return(i32* %p) #0 { +entry: + %cmp = icmp eq i32* %p, null + %conv = zext i1 %cmp to i32 + ret i32 %conv +} + +; TEST 2: +; +; int is_null_control(int *p) { +; if (p == 0) +; return 1; +; if (0 == p) +; return 1; +; return 0; +; } +; +; FIXME: no-capture missing for %p +; CHECK: define dso_local i32 @is_null_control(i32* readnone %p) +; +define dso_local i32 @is_null_control(i32* %p) #0 { +entry: + %retval = alloca i32, align 4 + %cmp = icmp eq i32* %p, null + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + store i32 1, i32* %retval, align 4 + br label %return + +if.end: ; preds = %entry + %cmp1 = icmp eq i32* null, %p + br i1 %cmp1, label %if.then2, label %if.end3 + +if.then2: ; preds = %if.end + store i32 1, i32* %retval, align 4 + br label %return + +if.end3: ; preds = %if.end + store i32 0, i32* %retval, align 4 + br label %return + +return: ; preds = %if.end3, %if.then2, %if.then + %0 = load i32, i32* %retval, align 4 + ret i32 %0 +} + +; TEST 3: +; +; double *srec0(double *a) { +; srec0(a); +; return 0; +; } +; +; CHECK: define dso_local noalias double* @srec0(double* nocapture readnone %a) +; +define dso_local double* @srec0(double* %a) #0 { +entry: + %call = call double* @srec0(double* %a) + ret double* null +} + +; TEST 4: +; +; int* srec16(int* a) { +; return srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(a)))))))))))))))); +; } +; +; Other arguments are possible here due to the no-return behavior. +; +; FIXME: no-return missing +; CHECK: define dso_local noalias nonnull i32* @srec16(i32* nocapture readnone %a) [[NoInlineNoUnwindReadnoneUwtable:#[0-9]*]] +; +define dso_local i32* @srec16(i32* %a) #0 { +entry: + %call = call i32* @srec16(i32* %a) + %call1 = call i32* @srec16(i32* %call) + %call2 = call i32* @srec16(i32* %call1) + %call3 = call i32* @srec16(i32* %call2) + %call4 = call i32* @srec16(i32* %call3) + %call5 = call i32* @srec16(i32* %call4) + %call6 = call i32* @srec16(i32* %call5) + %call7 = call i32* @srec16(i32* %call6) + %call8 = call i32* @srec16(i32* %call7) + %call9 = call i32* @srec16(i32* %call8) + %call10 = call i32* @srec16(i32* %call9) + %call11 = call i32* @srec16(i32* %call10) + %call12 = call i32* @srec16(i32* %call11) + %call13 = call i32* @srec16(i32* %call12) + %call14 = call i32* @srec16(i32* %call13) + %call15 = call i32* @srec16(i32* %call14) + ret i32* %call15 +} + +; TEST 5: +; +; FIXME: returned missing for %a +; FIXME: no-capture missing for %a +; CHECK: define dso_local float* @scc_A(i32* readnone %a) +; +; FIXME: returned missing for %a +; FIXME: no-capture missing for %a +; CHECK: define dso_local i64* @scc_B(double* readnone %a) +; +; FIXME: returned missing for %a +; FIXME: readnone missing for %s +; FIXME: no-capture missing for %a +; CHECK: define dso_local i8* @scc_C(i16* %a) +; +; float *scc_A(int *a) { +; return (float*)(a ? (int*)scc_A((int*)scc_B((double*)scc_C((short*)a))) : a); +; } +; +; long *scc_B(double *a) { +; return (long*)(a ? scc_C((short*)scc_B((double*)scc_A((int*)a))) : a); +; } +; +; void *scc_C(short *a) { +; return scc_A((int*)(scc_C(a) ? scc_B((double*)a) : scc_C(a))); +; } +; +define dso_local float* @scc_A(i32* %a) { +entry: + %tobool = icmp ne i32* %a, null + br i1 %tobool, label %cond.true, label %cond.false + +cond.true: ; preds = %entry + %0 = bitcast i32* %a to i16* + %call = call i8* @scc_C(i16* %0) + %1 = bitcast i8* %call to double* + %call1 = call i64* @scc_B(double* %1) + %2 = bitcast i64* %call1 to i32* + %call2 = call float* @scc_A(i32* %2) + %3 = bitcast float* %call2 to i32* + br label %cond.end + +cond.false: ; preds = %entry + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi i32* [ %3, %cond.true ], [ %a, %cond.false ] + %4 = bitcast i32* %cond to float* + ret float* %4 +} + +define dso_local i64* @scc_B(double* %a) { +entry: + %tobool = icmp ne double* %a, null + br i1 %tobool, label %cond.true, label %cond.false + +cond.true: ; preds = %entry + %0 = bitcast double* %a to i32* + %call = call float* @scc_A(i32* %0) + %1 = bitcast float* %call to double* + %call1 = call i64* @scc_B(double* %1) + %2 = bitcast i64* %call1 to i16* + %call2 = call i8* @scc_C(i16* %2) + br label %cond.end + +cond.false: ; preds = %entry + %3 = bitcast double* %a to i8* + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi i8* [ %call2, %cond.true ], [ %3, %cond.false ] + %4 = bitcast i8* %cond to i64* + ret i64* %4 +} + +define dso_local i8* @scc_C(i16* %a) { +entry: + %call = call i8* @scc_C(i16* %a) + %tobool = icmp ne i8* %call, null + br i1 %tobool, label %cond.true, label %cond.false + +cond.true: ; preds = %entry + %0 = bitcast i16* %a to double* + %call1 = call i64* @scc_B(double* %0) + %1 = bitcast i64* %call1 to i8* + br label %cond.end + +cond.false: ; preds = %entry + %call2 = call i8* @scc_C(i16* %a) + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi i8* [ %1, %cond.true ], [ %call2, %cond.false ] + %2 = bitcast i8* %cond to i32* + %call3 = call float* @scc_A(i32* %2) + %3 = bitcast float* %call3 to i8* + ret i8* %3 +} + + +; TEST 6: +; +; void external_no_capture(int /* no-capture */ *p); +; void test_external_no_capture(int *p) { +; external_no_capture(p); +; } +; +; CHECK: define dso_local void @test_external_no_capture(i32* nocapture %p) +; +declare dso_local void @external_no_capture(i32* nocapture) + +define dso_local void @test_external_no_capture(i32* %p) #0 { +entry: + call void @external_no_capture(i32* %p) + ret void +} + +; TEST 7: +; +; void test_var_arg_call(char *p, int a) { +; printf(p, a); +; } +; +; CHECK: define dso_local void @test_var_arg_call(i8* nocapture %p, i32 %a) +; +define dso_local void @test_var_arg_call(i8* %p, i32 %a) #0 { +entry: + %call = call i32 (i8*, ...) @printf(i8* %p, i32 %a) + ret void +} + +declare dso_local i32 @printf(i8* nocapture, ...) + + +; TEST 8: +; +; long *not_captured_but_returned_0(long *a) { +; *a1 = 0; +; return a; +; } +; +; There should *not* be a no-capture attribute on %a +; CHECK: define dso_local i64* @not_captured_but_returned_0(i64* returned %a) +; +define dso_local i64* @not_captured_but_returned_0(i64* %a) #0 { +entry: + store i64 0, i64* %a, align 8 + ret i64* %a +} + +; TEST 9: +; +; long *not_captured_but_returned_1(long *a) { +; *(a+1) = 1; +; return a + 1; +; } +; +; There should *not* be a no-capture attribute on %a +; CHECK: define dso_local nonnull i64* @not_captured_but_returned_1(i64* %a) +; +define dso_local i64* @not_captured_but_returned_1(i64* %a) #0 { +entry: + %add.ptr = getelementptr inbounds i64, i64* %a, i64 1 + store i64 1, i64* %add.ptr, align 8 + ret i64* %add.ptr +} + +; TEST 10: +; +; void test_not_captured_but_returned_calls(long *a) { +; not_captured_but_returned_0(a); +; not_captured_but_returned_1(a); +; } +; +; FIXME: no-capture missing for %a +; CHECK: define dso_local void @test_not_captured_but_returned_calls(i64* %a) +; +define dso_local void @test_not_captured_but_returned_calls(i64* %a) #0 { +entry: + %call = call i64* @not_captured_but_returned_0(i64* %a) + %call1 = call i64* @not_captured_but_returned_1(i64* %a) + ret void +} + +; TEST 11: +; +; long* negative_test_not_captured_but_returned_call_0a(long *a) { +; return not_captured_but_returned_0(a); +; } +; +; There should *not* be a no-capture attribute on %a +; CHECK: define dso_local i64* @negative_test_not_captured_but_returned_call_0a(i64* returned %a) +; +define dso_local i64* @negative_test_not_captured_but_returned_call_0a(i64* %a) #0 { +entry: + %call = call i64* @not_captured_but_returned_0(i64* %a) + ret i64* %call +} + +; TEST 12: +; +; void negative_test_not_captured_but_returned_call_0b(long *a) { +; *a = (long)not_captured_but_returned_0(a); +; } +; +; There should *not* be a no-capture attribute on %a +; CHECK: define dso_local void @negative_test_not_captured_but_returned_call_0b(i64* %a) +; +define dso_local void @negative_test_not_captured_but_returned_call_0b(i64* %a) #0 { +entry: + %call = call i64* @not_captured_but_returned_0(i64* %a) + %0 = ptrtoint i64* %call to i64 + store i64 %0, i64* %a, align 8 + ret void +} + +; TEST 13: +; +; long* negative_test_not_captured_but_returned_call_1a(long *a) { +; return not_captured_but_returned_1(a); +; } +; +; There should *not* be a no-capture attribute on %a +; CHECK: define dso_local nonnull i64* @negative_test_not_captured_but_returned_call_1a(i64* %a) +; +define dso_local i64* @negative_test_not_captured_but_returned_call_1a(i64* %a) #0 { +entry: + %call = call i64* @not_captured_but_returned_1(i64* %a) + ret i64* %call +} + +; TEST 14: +; +; void negative_test_not_captured_but_returned_call_1b(long *a) { +; *a = (long)not_captured_but_returned_1(a); +; } +; +; There should *not* be a no-capture attribute on %a +; CHECK: define dso_local void @negative_test_not_captured_but_returned_call_1b(i64* %a) +; +define dso_local void @negative_test_not_captured_but_returned_call_1b(i64* %a) #0 { +entry: + %call = call i64* @not_captured_but_returned_1(i64* %a) + %0 = ptrtoint i64* %call to i64 + store i64 %0, i64* %call, align 8 + ret void +} + +; TEST 15: +; +; int* ret_arg_or_unknown(int* b) { +; if (b == 0) +; return b; +; return unknown(); +; } +; +; Verify we do *not* assume b is returned or not captured. +; +; CHECK: define dso_local i32* @ret_arg_or_unknown(i32* readnone %b) +; CHECK: define dso_local i32* @ret_arg_or_unknown_through_phi(i32* readnone %b) +; +declare dso_local i32* @unknown() + +define dso_local i32* @ret_arg_or_unknown(i32* %b) #0 { +entry: + %cmp = icmp eq i32* %b, null + br i1 %cmp, label %ret_arg, label %ret_unknown + +ret_arg: + ret i32* %b + +ret_unknown: + %call = call i32* @unknown() + ret i32* %call +} + +define dso_local i32* @ret_arg_or_unknown_through_phi(i32* %b) #0 { +entry: + %cmp = icmp eq i32* %b, null + br i1 %cmp, label %ret_arg, label %ret_unknown + +ret_arg: + br label %r + +ret_unknown: + %call = call i32* @unknown() + br label %r + +r: + %phi = phi i32* [ %b, %ret_arg ], [ %call, %ret_unknown ] + ret i32* %phi +} + + +; TEST 16: +; +; CHECK: define dso_local void @not_captured_by_readonly_call(i32* nocapture %b) +; +declare dso_local i32* @readonly_unknown(i32*, i32*) readonly + +define dso_local void @not_captured_by_readonly_call(i32* %b) #0 { +entry: + %call = call i32* @readonly_unknown(i32* %b, i32* %b) + ret void +} + + +; TEST 17: +; +; Make sure the returned flag on %r is strong enough to justify nocapture on %b but **not** on %r. +; +; FIXME: The "returned" information is not propagated to the fullest extend causing us to miss "nocapture" on %b in the following: +; CHECK: define dso_local i32* @not_captured_by_readonly_call_not_returned_either1(i32* readonly %b, i32* readonly returned %r) +; +; CHECK: define dso_local i32* @not_captured_by_readonly_call_not_returned_either2(i32* readonly %b, i32* readonly returned %r) +; CHECK: define dso_local i32* @not_captured_by_readonly_call_not_returned_either3(i32* readonly %b, i32* readonly returned %r) +; +; FIXME: The "nounwind" information is not derived to the fullest extend causing us to miss "nocapture" on %b in the following: +; CHECK: define dso_local i32* @not_captured_by_readonly_call_not_returned_either4(i32* readonly %b, i32* readonly returned %r) +; +define dso_local i32* @not_captured_by_readonly_call_not_returned_either1(i32* %b, i32* returned %r) #0 { +entry: + %call = call i32* @readonly_unknown(i32* %b, i32* %r) nounwind + ret i32* %call +} + +declare dso_local i32* @readonly_unknown_r1a(i32*, i32* returned) readonly +define dso_local i32* @not_captured_by_readonly_call_not_returned_either2(i32* %b, i32* %r) #0 { +entry: + %call = call i32* @readonly_unknown_r1a(i32* %b, i32* %r) nounwind + ret i32* %call +} + +declare dso_local i32* @readonly_unknown_r1b(i32*, i32* returned) readonly nounwind +define dso_local i32* @not_captured_by_readonly_call_not_returned_either3(i32* %b, i32* %r) #0 { +entry: + %call = call i32* @readonly_unknown_r1b(i32* %b, i32* %r) + ret i32* %call +} + +define dso_local i32* @not_captured_by_readonly_call_not_returned_either4(i32* %b, i32* %r) #0 { +entry: + %call = call i32* @readonly_unknown_r1a(i32* %b, i32* %r) + ret i32* %call +} + +attributes #0 = { noinline nounwind uwtable } + +; CHECK: attributes [[NoInlineNoUnwindReadnoneUwtable]] = { noinline nounwind readnone uwtable } diff --git a/llvm/test/Transforms/FunctionAttrs/arg_returned.ll b/llvm/test/Transforms/FunctionAttrs/arg_returned.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/FunctionAttrs/arg_returned.ll @@ -0,0 +1,591 @@ +; RUN: opt -functionattrs -S < %s | FileCheck %s +; +; Test cases specifically designed for the "returned" argument attribute. +; We use FIXME's to indicate problems and missing attributes. +; +; TEST 1: SCC test returning an integer value argument +; TEST 2: the same SCC as in 1 returning a pointer value argument +; TEST 3: a singleton SCC with a lot of recursive calls +; TEST 4: address taken function with call to an external functions +; TEST 5: call to a function that might be redifined at link time +; TEST 6: returned argument goes through select and phi +; TEST 7: returned argument goes through recursion, select, and phi +; TEST 8: returned argument goes through bitcasts +; TEST 9: returned argument goes through select and phi interleaved with bitcasts +; TEST 10: return argument or argument or undef +; TEST 11: return undef or argument or argument +; TEST 12: return undef or argument or undef +; TEST 13: return argument or unknown call result + + +; TEST 1 +; +; CHECK: define dso_local i32 @sink_r0(i32 returned %r) [[NoInlineNoRecurseNoUnwindReadnoneUwtable:#[0-9]]] +; +; FIXME: returned on %r missing: +; CHECK: define dso_local i32 @scc_r1(i32 %a, i32 %r, i32 %b) [[NoInlineNoUnwindReadnoneUwtable:#[0-9]]] +; +; FIXME: returned on %r missing: +; CHECK: define dso_local i32 @scc_r2(i32 %a, i32 %b, i32 %r) [[NoInlineNoUnwindReadnoneUwtable]] +; +; int scc_r1(int a, int b, int r); +; int scc_r2(int a, int b, int r); +; +; __attribute__((noinline)) int sink_r0(int r) { +; return r; +; } +; +; __attribute__((noinline)) int scc_r1(int a, int r, int b) { +; return scc_r2(r, a, sink_r0(r)); +; } +; +; __attribute__((noinline)) int scc_r2(int a, int b, int r) { +; if (a > b) +; return scc_r2(b, a, sink_r0(r)); +; if (a < b) +; return scc_r1(sink_r0(b), scc_r2(scc_r1(a, b, r), scc_r1(a, scc_r2(r, r, r), r), scc_r2(a, b, r)), scc_r1(a, b, r)); +; return a == b ? r : scc_r2(a, b, r); +; } +; __attribute__((noinline)) int scc_rX(int a, int b, int r) { +; if (a > b) +; return scc_r2(b, a, sink_r0(r)); +; if (a < b) // V Diff to scc_r2 +; return scc_r1(sink_r0(b), scc_r2(scc_r1(a, b, r), scc_r1(a, scc_r2(r, r, r), r), scc_r1(a, b, r)), scc_r1(a, b, r)); +; return a == b ? r : scc_r2(a, b, r); +; } +; +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +define dso_local i32 @sink_r0(i32 %r) #0 { +entry: + ret i32 %r +} + +define dso_local i32 @scc_r1(i32 %a, i32 %r, i32 %b) #0 { +entry: + %call = call i32 @sink_r0(i32 %r) + %call1 = call i32 @scc_r2(i32 %r, i32 %a, i32 %call) + ret i32 %call1 +} + +define dso_local i32 @scc_r2(i32 %a, i32 %b, i32 %r) #0 { +entry: + %cmp = icmp sgt i32 %a, %b + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %call = call i32 @sink_r0(i32 %r) + %call1 = call i32 @scc_r2(i32 %b, i32 %a, i32 %call) + br label %return + +if.end: ; preds = %entry + %cmp2 = icmp slt i32 %a, %b + br i1 %cmp2, label %if.then3, label %if.end12 + +if.then3: ; preds = %if.end + %call4 = call i32 @sink_r0(i32 %b) + %call5 = call i32 @scc_r1(i32 %a, i32 %b, i32 %r) + %call6 = call i32 @scc_r2(i32 %r, i32 %r, i32 %r) + %call7 = call i32 @scc_r1(i32 %a, i32 %call6, i32 %r) + %call8 = call i32 @scc_r2(i32 %a, i32 %b, i32 %r) + %call9 = call i32 @scc_r2(i32 %call5, i32 %call7, i32 %call8) + %call10 = call i32 @scc_r1(i32 %a, i32 %b, i32 %r) + %call11 = call i32 @scc_r1(i32 %call4, i32 %call9, i32 %call10) + br label %return + +if.end12: ; preds = %if.end + %cmp13 = icmp eq i32 %a, %b + br i1 %cmp13, label %cond.true, label %cond.false + +cond.true: ; preds = %if.end12 + br label %cond.end + +cond.false: ; preds = %if.end12 + %call14 = call i32 @scc_r2(i32 %a, i32 %b, i32 %r) + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi i32 [ %r, %cond.true ], [ %call14, %cond.false ] + br label %return + +return: ; preds = %cond.end, %if.then3, %if.then + %retval.0 = phi i32 [ %call1, %if.then ], [ %call11, %if.then3 ], [ %cond, %cond.end ] + ret i32 %retval.0 +} + +define dso_local i32 @scc_rX(i32 %a, i32 %b, i32 %r) #0 { +entry: + %cmp = icmp sgt i32 %a, %b + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %call = call i32 @sink_r0(i32 %r) + %call1 = call i32 @scc_r2(i32 %b, i32 %a, i32 %call) + br label %return + +if.end: ; preds = %entry + %cmp2 = icmp slt i32 %a, %b + br i1 %cmp2, label %if.then3, label %if.end12 + +if.then3: ; preds = %if.end + %call4 = call i32 @sink_r0(i32 %b) + %call5 = call i32 @scc_r1(i32 %a, i32 %b, i32 %r) + %call6 = call i32 @scc_r2(i32 %r, i32 %r, i32 %r) + %call7 = call i32 @scc_r1(i32 %a, i32 %call6, i32 %r) + %call8 = call i32 @scc_r1(i32 %a, i32 %b, i32 %r) + %call9 = call i32 @scc_r2(i32 %call5, i32 %call7, i32 %call8) + %call10 = call i32 @scc_r1(i32 %a, i32 %b, i32 %r) + %call11 = call i32 @scc_r1(i32 %call4, i32 %call9, i32 %call10) + br label %return + +if.end12: ; preds = %if.end + %cmp13 = icmp eq i32 %a, %b + br i1 %cmp13, label %cond.true, label %cond.false + +cond.true: ; preds = %if.end12 + br label %cond.end + +cond.false: ; preds = %if.end12 + %call14 = call i32 @scc_r2(i32 %a, i32 %b, i32 %r) + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi i32 [ %r, %cond.true ], [ %call14, %cond.false ] + br label %return + +return: ; preds = %cond.end, %if.then3, %if.then + %retval.0 = phi i32 [ %call1, %if.then ], [ %call11, %if.then3 ], [ %cond, %cond.end ] + ret i32 %retval.0 +} + + +; TEST 2 +; +; CHECK: define dso_local double* @ptr_sink_r0(double* readnone returned %r) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +; FIXME: returned on %r missing: +; CHECK: define dso_local double* @ptr_scc_r1(double* %a, double* readnone %r, double* nocapture readnone %b) [[NoInlineNoUnwindReadnoneUwtable]] +; +; FIXME: returned on %r missing: +; CHECK: define dso_local double* @ptr_scc_r2(double* readnone %a, double* readnone %b, double* readnone %r) [[NoInlineNoUnwindReadnoneUwtable]] +; +; double* ptr_scc_r1(double* a, double* b, double* r); +; double* ptr_scc_r2(double* a, double* b, double* r); +; +; __attribute__((noinline)) double* ptr_sink_r0(double* r) { +; return r; +; } +; +; __attribute__((noinline)) double* ptr_scc_r1(double* a, double* r, double* b) { +; return ptr_scc_r2(r, a, ptr_sink_r0(r)); +; } +; +; __attribute__((noinline)) double* ptr_scc_r2(double* a, double* b, double* r) { +; if (a > b) +; return ptr_scc_r2(b, a, ptr_sink_r0(r)); +; if (a < b) +; return ptr_scc_r1(ptr_sink_r0(b), ptr_scc_r2(ptr_scc_r1(a, b, r), ptr_scc_r1(a, ptr_scc_r2(r, r, r), r), ptr_scc_r2(a, b, r)), ptr_scc_r1(a, b, r)); +; return a == b ? r : ptr_scc_r2(a, b, r); +; } +define dso_local double* @ptr_sink_r0(double* %r) #0 { +entry: + ret double* %r +} + +define dso_local double* @ptr_scc_r1(double* %a, double* %r, double* %b) #0 { +entry: + %call = call double* @ptr_sink_r0(double* %r) + %call1 = call double* @ptr_scc_r2(double* %r, double* %a, double* %call) + ret double* %call1 +} + +define dso_local double* @ptr_scc_r2(double* %a, double* %b, double* %r) #0 { +entry: + %cmp = icmp ugt double* %a, %b + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %call = call double* @ptr_sink_r0(double* %r) + %call1 = call double* @ptr_scc_r2(double* %b, double* %a, double* %call) + br label %return + +if.end: ; preds = %entry + %cmp2 = icmp ult double* %a, %b + br i1 %cmp2, label %if.then3, label %if.end12 + +if.then3: ; preds = %if.end + %call4 = call double* @ptr_sink_r0(double* %b) + %call5 = call double* @ptr_scc_r1(double* %a, double* %b, double* %r) + %call6 = call double* @ptr_scc_r2(double* %r, double* %r, double* %r) + %call7 = call double* @ptr_scc_r1(double* %a, double* %call6, double* %r) + %call8 = call double* @ptr_scc_r2(double* %a, double* %b, double* %r) + %call9 = call double* @ptr_scc_r2(double* %call5, double* %call7, double* %call8) + %call10 = call double* @ptr_scc_r1(double* %a, double* %b, double* %r) + %call11 = call double* @ptr_scc_r1(double* %call4, double* %call9, double* %call10) + br label %return + +if.end12: ; preds = %if.end + %cmp13 = icmp eq double* %a, %b + br i1 %cmp13, label %cond.true, label %cond.false + +cond.true: ; preds = %if.end12 + br label %cond.end + +cond.false: ; preds = %if.end12 + %call14 = call double* @ptr_scc_r2(double* %a, double* %b, double* %r) + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi double* [ %r, %cond.true ], [ %call14, %cond.false ] + br label %return + +return: ; preds = %cond.end, %if.then3, %if.then + %retval.0 = phi double* [ %call1, %if.then ], [ %call11, %if.then3 ], [ %cond, %cond.end ] + ret double* %retval.0 +} + + +; TEST 3 +; +; int* ret0(int *a) { +; return *a ? a : ret0(ret0(ret0(...ret0(a)...))); +; } +; +; FIXME: returned on %a missing: +; CHECK: define dso_local i32* @ret0(i32* readonly %a) [[NoInlineNoUnwindReadonlyUwtable:#[0-9]*]] +define dso_local i32* @ret0(i32* %a) #0 { +entry: + %v = load i32, i32* %a, align 4 + %tobool = icmp ne i32 %v, 0 + %call = call i32* @ret0(i32* %a) + %call1 = call i32* @ret0(i32* %call) + %call2 = call i32* @ret0(i32* %call1) + %call3 = call i32* @ret0(i32* %call2) + %call4 = call i32* @ret0(i32* %call3) + %call5 = call i32* @ret0(i32* %call4) + %call6 = call i32* @ret0(i32* %call5) + %call7 = call i32* @ret0(i32* %call6) + %call8 = call i32* @ret0(i32* %call7) + %call9 = call i32* @ret0(i32* %call8) + %call10 = call i32* @ret0(i32* %call9) + %call11 = call i32* @ret0(i32* %call10) + %call12 = call i32* @ret0(i32* %call11) + %call13 = call i32* @ret0(i32* %call12) + %call14 = call i32* @ret0(i32* %call13) + %call15 = call i32* @ret0(i32* %call14) + %call16 = call i32* @ret0(i32* %call15) + %call17 = call i32* @ret0(i32* %call16) + %sel = select i1 %tobool, i32* %a, i32* %call17 + ret i32* %sel +} + + +; TEST 4 +; +; void unknown_fn(void *); +; +; int* calls_unknown_fn(int *r) { +; unknown_fn(&calls_unknown_fn); +; return r; +; } +; +; CHECK: declare void @unknown_fn(i32* (i32*)*) [[NoInlineNoUnwindUwtable:#[0-9]*]] +; +; CHECK: define dso_local i32* @calls_unknown_fn(i32* readnone returned %r) [[NoInlineNoUnwindUwtable]] +; +declare void @unknown_fn(i32* (i32*)*) #0 + +define dso_local i32* @calls_unknown_fn(i32* %r) #0 { + tail call void @unknown_fn(i32* (i32*)* nonnull @calls_unknown_fn) + ret i32* %r +} + + +; TEST 5 +; +; int *maybe_redefined_fn(int *r) { +; return r; +; } +; +; int *calls_maybe_redefined_fn(int *r) { +; maybe_redefined_fn(r); +; return r; +; } +; +; Verify the maybe-redefined function is not annotated: +; CHECK: define linkonce_odr i32* @maybe_redefined_fn(i32* %r) [[NoInlineNoRecurseNoUnwindUwtable:#[0-9]*]] +; FIXME: We should not derive norecurse for potentially redefined functions! +; define linkonce_odr i32* @maybe_redefined_fn(i32* %r) [[NoInlineNoUnwindUwtable]] +; +; CHECK: define dso_local i32* @calls_maybe_redefined_fn(i32* returned %r) [[NoInlineNoRecurseNoUnwindUwtable]] +; FIXME: We should not derive norecurse for potentially redefined functions! +; define dso_local i32* @calls_maybe_redefined_fn(i32* returned %r) [[NoInlineNoUnwindUwtable]] +; +define linkonce_odr i32* @maybe_redefined_fn(i32* %r) #0 { +entry: + ret i32* %r +} + +define dso_local i32* @calls_maybe_redefined_fn(i32* %r) #0 { +entry: + %call = call i32* @maybe_redefined_fn(i32* %r) + ret i32* %r +} + + +; TEST 6 +; +; double select_and_phi(double b) { +; double x = b; +; if (b > 0) +; x = b; +; return b == 0? b : x; +; } +; +; FIXME: returned on %b missing: +; CHECK: define dso_local double @select_and_phi(double %b) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local double @select_and_phi(double %b) #0 { +entry: + %cmp = fcmp ogt double %b, 0.000000e+00 + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + br label %if.end + +if.end: ; preds = %if.then, %entry + %phi = phi double [ %b, %if.then ], [ %b, %entry ] + %cmp1 = fcmp oeq double %b, 0.000000e+00 + %sel = select i1 %cmp1, double %b, double %phi + ret double %sel +} + + +; TEST 7 +; +; double recursion_select_and_phi(int a, double b) { +; double x = b; +; if (a-- > 0) +; x = recursion_select_and_phi(a, b); +; return b == 0? b : x; +; } +; +; FIXME: returned on %b missing: +; CHECK: define dso_local double @recursion_select_and_phi(i32 %a, double %b) [[NoInlineNoUnwindReadnoneUwtable]] +; +define dso_local double @recursion_select_and_phi(i32 %a, double %b) #0 { +entry: + %dec = add nsw i32 %a, -1 + %cmp = icmp sgt i32 %a, 0 + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %call = call double @recursion_select_and_phi(i32 %dec, double %b) + br label %if.end + +if.end: ; preds = %if.then, %entry + %phi = phi double [ %call, %if.then ], [ %b, %entry ] + %cmp1 = fcmp oeq double %b, 0.000000e+00 + %sel = select i1 %cmp1, double %b, double %phi + ret double %sel +} + + +; TEST 8 +; +; double* bitcast(int* b) { +; return (double*)b; +; } +; +; FIXME: returned on %b missing: +; CHECK: define dso_local double* @bitcast(i32* readnone %b) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local double* @bitcast(i32* %b) #0 { +entry: + %bc0 = bitcast i32* %b to double* + ret double* %bc0 +} + + +; TEST 9 +; +; double* bitcasts_select_and_phi(int* b) { +; double* x = b; +; if (b == 0) +; x = b; +; return b != 0 ? b : x; +; } +; +; FIXME: returned on %b missing: +; CHECK: define dso_local double* @bitcasts_select_and_phi(i32* readnone %b) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local double* @bitcasts_select_and_phi(i32* %b) #0 { +entry: + %bc0 = bitcast i32* %b to double* + %cmp = icmp eq double* %bc0, null + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %bc1 = bitcast i32* %b to double* + br label %if.end + +if.end: ; preds = %if.then, %entry + %phi = phi double* [ %bc1, %if.then ], [ %bc0, %entry ] + %bc2 = bitcast double* %phi to i8* + %bc3 = bitcast i32* %b to i8* + %cmp2 = icmp ne double* %bc0, null + %sel = select i1 %cmp2, i8* %bc2, i8* %bc3 + %bc4 = bitcast i8* %sel to double* + ret double* %bc4 +} + + +; TEST 10 +; +; double* ret_arg_arg_undef(int* b) { +; if (b == 0) +; return (double*)b; +; if (b == 0) +; return (double*)b; +; /* return undef */ +; } +; +; CHECK: define dso_local double* @ret_arg_arg_undef(i32* readnone %b) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local double* @ret_arg_arg_undef(i32* %b) #0 { +entry: + %bc0 = bitcast i32* %b to double* + %cmp = icmp eq double* %bc0, null + br i1 %cmp, label %ret_arg0, label %if.end + +ret_arg0: + %bc1 = bitcast i32* %b to double* + ret double* %bc1 + +if.end: + br i1 %cmp, label %ret_arg1, label %ret_undef + +ret_arg1: + ret double* %bc0 + +ret_undef: + ret double *undef +} + + +; TEST 11 +; +; double* ret_undef_arg_arg(int* b) { +; if (b == 0) +; return (double*)b; +; if (b == 0) +; return (double*)b; +; /* return undef */ +; } +; +; CHECK: define dso_local double* @ret_undef_arg_arg(i32* readnone %b) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local double* @ret_undef_arg_arg(i32* %b) #0 { +entry: + %bc0 = bitcast i32* %b to double* + %cmp = icmp eq double* %bc0, null + br i1 %cmp, label %ret_undef, label %if.end + +ret_undef: + ret double *undef + +if.end: + br i1 %cmp, label %ret_arg0, label %ret_arg1 + +ret_arg0: + ret double* %bc0 + +ret_arg1: + %bc1 = bitcast i32* %b to double* + ret double* %bc1 +} + + +; TEST 12 +; +; double* ret_undef_arg_undef(int* b) { +; if (b == 0) +; /* return undef */ +; if (b == 0) +; return (double*)b; +; /* return undef */ +; } +; +; CHECK: define dso_local double* @ret_undef_arg_undef(i32* readnone %b) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local double* @ret_undef_arg_undef(i32* %b) #0 { +entry: + %bc0 = bitcast i32* %b to double* + %cmp = icmp eq double* %bc0, null + br i1 %cmp, label %ret_undef0, label %if.end + +ret_undef0: + ret double *undef + +if.end: + br i1 %cmp, label %ret_arg, label %ret_undef1 + +ret_arg: + ret double* %bc0 + +ret_undef1: + ret double *undef +} + +; TEST 13 +; +; int* ret_arg_or_unknown(int* b) { +; if (b == 0) +; return b; +; return unknown(b); +; } +; +; Verify we do not assume b is returned> +; +; CHECK: define dso_local i32* @ret_arg_or_unknown(i32* %b) +; CHECK: define dso_local i32* @ret_arg_or_unknown_through_phi(i32* %b) +; +declare dso_local i32* @unknown(i32*) + +define dso_local i32* @ret_arg_or_unknown(i32* %b) #0 { +entry: + %cmp = icmp eq i32* %b, null + br i1 %cmp, label %ret_arg, label %ret_unknown + +ret_arg: + ret i32* %b + +ret_unknown: + %call = call i32* @unknown(i32* %b) + ret i32* %call +} + +define dso_local i32* @ret_arg_or_unknown_through_phi(i32* %b) #0 { +entry: + %cmp = icmp eq i32* %b, null + br i1 %cmp, label %ret_arg, label %ret_unknown + +ret_arg: + br label %r + +ret_unknown: + %call = call i32* @unknown(i32* %b) + br label %r + +r: + %phi = phi i32* [ %b, %ret_arg ], [ %call, %ret_unknown ] + ret i32* %phi +} + +attributes #0 = { noinline nounwind uwtable } + +; CHECK-DAG: attributes [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] = { noinline norecurse nounwind readnone uwtable } +; CHECK-DAG: attributes [[NoInlineNoUnwindReadnoneUwtable]] = { noinline nounwind readnone uwtable } +; CHECK-DAG: attributes [[NoInlineNoUnwindReadonlyUwtable]] = { noinline nounwind readonly uwtable } +; CHECK-DAG: attributes [[NoInlineNoUnwindUwtable]] = { noinline nounwind uwtable } +; CHECK-DAG: attributes [[NoInlineNoRecurseNoUnwindUwtable]] = { noinline norecurse nounwind uwtable } diff --git a/llvm/test/Transforms/FunctionAttrs/fn_noreturn.ll b/llvm/test/Transforms/FunctionAttrs/fn_noreturn.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/FunctionAttrs/fn_noreturn.ll @@ -0,0 +1,132 @@ +; RUN: opt -functionattrs -S < %s | FileCheck %s +; +; Test cases specifically designed for the "no-return" function attribute. +; We use FIXME's to indicate problems and missing attributes. +; +; TEST 1: singleton SCC void return type +; TEST 2: singleton SCC int return type with a lot of recursive calls +; TEST 3: endless loop, no return instruction +; TEST 4: endless loop, dead return instruction +; TEST 5: all paths contain a no-return function call +; +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + + +; TEST 1 +; +; void srec0() { +; return srec0(); +; } +; +; FIXME: no-return missing +; CHECK: define dso_local void @srec0() [[NoInlineNoUnwindReadnoneUwtable:#[0-9]]] +; +define dso_local void @srec0() #0 { +entry: + call void @srec0() + ret void +} + + +; TEST 2 +; +; int srec16(int a) { +; return srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(srec16(a)))))))))))))))); +; } +; +; FIXME: no-return missing +; CHECK: define dso_local i32 @srec16(i32 %a) [[NoInlineNoUnwindReadnoneUwtable]] +; +define dso_local i32 @srec16(i32 %a) #0 { +entry: + %call = call i32 @srec16(i32 %a) + %call1 = call i32 @srec16(i32 %call) + %call2 = call i32 @srec16(i32 %call1) + %call3 = call i32 @srec16(i32 %call2) + %call4 = call i32 @srec16(i32 %call3) + %call5 = call i32 @srec16(i32 %call4) + %call6 = call i32 @srec16(i32 %call5) + %call7 = call i32 @srec16(i32 %call6) + %call8 = call i32 @srec16(i32 %call7) + %call9 = call i32 @srec16(i32 %call8) + %call10 = call i32 @srec16(i32 %call9) + %call11 = call i32 @srec16(i32 %call10) + %call12 = call i32 @srec16(i32 %call11) + %call13 = call i32 @srec16(i32 %call12) + %call14 = call i32 @srec16(i32 %call13) + %call15 = call i32 @srec16(i32 %call14) + ret i32 %call15 +} + + +; TEST 3 +; +; int endless_loop(int a) { +; while (1); +; } +; +; FIXME: no-return missing +; CHECK: define dso_local i32 @endless_loop(i32 %a) [[NoInlineNoRecurseNoUnwindReadnoneUwtable:#[0-9]]] +; +define dso_local i32 @endless_loop(i32 %a) #0 { +entry: + br label %while.body + +while.body: ; preds = %entry, %while.body + br label %while.body +} + + +; TEST 4 +; +; int endless_loop(int a) { +; while (1); +; return a; +; } +; +; FIXME: no-return missing +; CHECK: define dso_local i32 @dead_return(i32 returned %a) [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] +; +define dso_local i32 @dead_return(i32 %a) #0 { +entry: + br label %while.body + +while.body: ; preds = %entry, %while.body + br label %while.body + +return: ; No predecessors! + ret i32 %a +} + + +; TEST 5 +; +; int multiple_noreturn_calls(int a) { +; return a == 0 ? endless_loop(a) : srec16(a); +; } +; +; FIXME: no-return missing +; CHECK: define dso_local i32 @multiple_noreturn_calls(i32 %a) [[NoInlineNoUnwindReadnoneUwtable]] +; +define dso_local i32 @multiple_noreturn_calls(i32 %a) #0 { +entry: + %cmp = icmp eq i32 %a, 0 + br i1 %cmp, label %cond.true, label %cond.false + +cond.true: ; preds = %entry + %call = call i32 @endless_loop(i32 %a) + br label %cond.end + +cond.false: ; preds = %entry + %call1 = call i32 @srec16(i32 %a) + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %cond = phi i32 [ %call, %cond.true ], [ %call1, %cond.false ] + ret i32 %cond +} + +attributes #0 = { noinline nounwind uwtable } + +; CHECK-DAG: attributes [[NoInlineNoUnwindReadnoneUwtable]] = { noinline nounwind readnone uwtable } +; CHECK-DAG: attributes [[NoInlineNoRecurseNoUnwindReadnoneUwtable]] = { noinline norecurse nounwind readnone uwtable } diff --git a/llvm/test/Transforms/FunctionAttrs/readattrs.ll b/llvm/test/Transforms/FunctionAttrs/readattrs.ll --- a/llvm/test/Transforms/FunctionAttrs/readattrs.ll +++ b/llvm/test/Transforms/FunctionAttrs/readattrs.ll @@ -1,10 +1,11 @@ -; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -functionattrs -S | FileCheck %s ; RUN: opt < %s -aa-pipeline=basic-aa -passes='cgscc(function-attrs)' -S | FileCheck %s @x = global i32 0 declare void @test1_1(i8* %x1_1, i8* readonly %y1_1, ...) +; NOTE: readonly for %y1_2 would be OK here but not for the similar situation in test13. +; ; CHECK: define void @test1_2(i8* %x1_2, i8* readonly %y1_2, i8* %z1_2) define void @test1_2(i8* %x1_2, i8* %y1_2, i8* %z1_2) { call void (i8*, i8*, ...) @test1_1(i8* %x1_2, i8* %y1_2, i8* %z1_2) @@ -113,3 +114,18 @@ %load = load volatile i32, i32* %p ret i32 %load } + +; %escaped_then_written cannot be marked readonly even though the only direct use, +; in @test1_1, is marked readonly. However, @test1_1 can write the pointer into %addr, +; causing the store here to write actually %escaped_then_written. +; +; FIXME: This test currently exposes a bug! +; +; CHECK: define void @test13(i8* %ignored, i8* readonly %escaped_then_written) +define void @test13(i8* %ignored, i8* %escaped_then_written) { + %addr = alloca i8* + call void (i8*, i8*, ...) @test1_1(i8* %ignored, i8* %escaped_then_written, i8** %addr) + %addr.ld = load i8*, i8** %addr + store i8 0, i8* %addr.ld + ret void +}