• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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]], !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 i64
55; CHECK-NEXT:    store i64 42, i64* %[[A]]
56; CHECK-NEXT:    %[[LOAD:.*]] = load volatile i64, i64* %[[A]]
57; CHECK-NEXT:    %[[CAST:.*]] = inttoptr i64 %[[LOAD]] to i8*
58; CHECK-NEXT:    ret i8* %[[CAST]]
59entry:
60  %a = alloca [2 x i8*]
61  %a.gep0 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 0
62  %a.gep1 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 1
63  %a.gep0.cast = bitcast i8** %a.gep0 to i64*
64  %a.gep1.cast = bitcast i8** %a.gep1 to i64*
65  store i64 42, i64* %a.gep1.cast
66  store i64 0, i64* %a.gep0.cast
67  %load = load volatile i8*, i8** %a.gep1, !nonnull !0
68  ret i8* %load
69}
70
71; Make sure we properly handle the !nonnull attribute when we convert
72; a pointer load to an integer load and immediately promote it to an SSA
73; register. This can fail in interesting ways due to the rewrite iteration of
74; SROA, resulting in PR32902.
75define i8* @propagate_nonnull_to_int_and_promote() {
76; CHECK-LABEL: define i8* @propagate_nonnull_to_int_and_promote(
77; CHECK-NEXT:  entry:
78; CHECK-NEXT:    %[[PROMOTED_VALUE:.*]] = inttoptr i64 42 to i8*
79; CHECK-NEXT:    ret i8* %[[PROMOTED_VALUE]]
80entry:
81  %a = alloca [2 x i8*], align 8
82  %a.gep0 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 0
83  %a.gep1 = getelementptr [2 x i8*], [2 x i8*]* %a, i32 0, i32 1
84  %a.gep0.cast = bitcast i8** %a.gep0 to i64*
85  %a.gep1.cast = bitcast i8** %a.gep1 to i64*
86  store i64 42, i64* %a.gep1.cast
87  store i64 0, i64* %a.gep0.cast
88  %load = load i8*, i8** %a.gep1, align 8, !nonnull !0
89  ret i8* %load
90}
91
92!0 = !{}
93