1; RUN: opt < %s -sroa -S | FileCheck %s
2;
3; Make sure that SROA doesn't lose nonnull metadata
4; on loads from allocas that get optimized out.
5
6declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1)
7
8; Check that we do basic propagation of nonnull when rewriting.
9define i8* @propagate_nonnull(i32* %v) {
10; CHECK-LABEL: define i8* @propagate_nonnull(
11; CHECK-NEXT:  entry:
12; CHECK-NEXT:    %[[A:.*]] = alloca i8*
13; CHECK-NEXT:    %[[V_CAST:.*]] = bitcast i32* %v to i8*
14; CHECK-NEXT:    store i8* %[[V_CAST]], i8** %[[A]]
15; CHECK-NEXT:    %[[LOAD:.*]] = load volatile i8*, i8** %[[A]], align 8, !nonnull !0
16; CHECK-NEXT:    ret i8* %[[LOAD]]
17entry:
18  %a = alloca [2 x i8*]
19  %a.gep0 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 0
20  %a.gep1 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 1
21  %a.gep0.cast = bitcast i8** %a.gep0 to i32**
22  %a.gep1.cast = bitcast i8** %a.gep1 to i32**
23  store i32* %v, i32** %a.gep1.cast
24  store i32* null, i32** %a.gep0.cast
25  %load = load volatile i8*, i8** %a.gep1, !nonnull !0
26  ret i8* %load
27}
28
29define float* @turn_nonnull_into_assume(float** %arg) {
30; CHECK-LABEL: define float* @turn_nonnull_into_assume(
31; CHECK-NEXT:  entry:
32; CHECK-NEXT:    %[[RETURN:.*]] = load float*, float** %arg, align 8
33; CHECK-NEXT:    %[[ASSUME:.*]] = icmp ne float* %[[RETURN]], null
34; CHECK-NEXT:    call void @llvm.assume(i1 %[[ASSUME]])
35; CHECK-NEXT:    ret float* %[[RETURN]]
36entry:
37  %buf = alloca float*
38  %_arg_i8 = bitcast float** %arg to i8*
39  %_buf_i8 = bitcast float** %buf to i8*
40  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %_buf_i8, i8* align 8 %_arg_i8, i64 8, i1 false)
41  %ret = load float*, float** %buf, align 8, !nonnull !0
42  ret float* %ret
43}
44
45; Make sure we properly handle the !nonnull attribute when we convert
46; a pointer load to an integer load.
47; FIXME: While this doesn't do anythnig actively harmful today, it really
48; should propagate the !nonnull metadata to range metadata. The irony is, it
49; *does* initially, but then we lose that !range metadata before we finish
50; SROA.
51define i8* @propagate_nonnull_to_int() {
52; CHECK-LABEL: define i8* @propagate_nonnull_to_int(
53; CHECK-NEXT:  entry:
54; CHECK-NEXT:    %[[A:.*]] = alloca i8*
55; CHECK-NEXT:    store i8* inttoptr (i64 42 to i8*), i8** %[[A]]
56; CHECK-NEXT:    %[[LOAD:.*]] = load volatile i8*, i8** %[[A]]
57; CHECK-NEXT:    ret i8* %[[LOAD]]
58entry:
59  %a = alloca [2 x i8*]
60  %a.gep0 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 0
61  %a.gep1 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 1
62  %a.gep0.cast = bitcast i8** %a.gep0 to i64*
63  %a.gep1.cast = bitcast i8** %a.gep1 to i64*
64  store i64 42, i64* %a.gep1.cast
65  store i64 0, i64* %a.gep0.cast
66  %load = load volatile i8*, i8** %a.gep1, !nonnull !0
67  ret i8* %load
68}
69
70; Make sure we properly handle the !nonnull attribute when we convert
71; a pointer load to an integer load and immediately promote it to an SSA
72; register. This can fail in interesting ways due to the rewrite iteration of
73; SROA, resulting in PR32902.
74define i8* @propagate_nonnull_to_int_and_promote() {
75; CHECK-LABEL: define i8* @propagate_nonnull_to_int_and_promote(
76; CHECK-NEXT:  entry:
77; CHECK-NEXT:    ret i8* inttoptr (i64 42 to i8*)
78entry:
79  %a = alloca [2 x i8*], align 8
80  %a.gep0 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 0
81  %a.gep1 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 1
82  %a.gep0.cast = bitcast i8** %a.gep0 to i64*
83  %a.gep1.cast = bitcast i8** %a.gep1 to i64*
84  store i64 42, i64* %a.gep1.cast
85  store i64 0, i64* %a.gep0.cast
86  %load = load i8*, i8** %a.gep1, align 8, !nonnull !0
87  ret i8* %load
88}
89
90!0 = !{}
91