xref: /llvm-project/llvm/test/Analysis/LoopAccessAnalysis/memcheck-wrapping-pointers.ll (revision 7bfcf93527782f1ebf83880f35e981665308d89c)
1; RUN: opt -passes='print<access-info>' %s -disable-output 2>&1 | FileCheck %s
2
3target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
4
5; i and i + 1 can overflow in the following kernel:
6; void test1(unsigned long long x, int *a, int *b) {
7;  for (unsigned i = 0; i < x; ++i)
8;    b[i] = a[i+1] + 1;
9; }
10;
11; If accesses to a and b can alias, we need to emit a run-time alias check
12; between accesses to a and b. However, when i and i + 1 can wrap, their
13; SCEV expression is not an AddRec. We need to create SCEV predicates and
14; coerce the expressions to AddRecs in order to be able to emit the run-time
15; alias check.
16;
17; The accesses at b[i] and a[i+1] correspond to the addresses %arrayidx and
18; %arrayidx4 in the test. The SCEV expressions for these are:
19;  ((4 * (zext i32 {1,+,1}<%for.body> to i64))<nuw><nsw> + %a)<nsw>
20;  ((4 * (zext i32 {0,+,1}<%for.body> to i64))<nuw><nsw> + %b)<nsw>
21;
22; The transformed expressions are:
23;  i64 {(4 + %a),+,4}<%for.body>
24;  i64 {(4 + %b),+,4}<%for.body>
25
26; CHECK-LABEL: test1
27; CHECK:         Memory dependences are safe with run-time checks
28; CHECK-NEXT:    Dependences:
29; CHECK-NEXT:    Run-time memory checks:
30; CHECK-NEXT:    Check 0:
31; CHECK-NEXT:      Comparing group
32; CHECK-NEXT:        %arrayidx4 = getelementptr inbounds i32, ptr %b, i64 %conv11
33; CHECK-NEXT:      Against group
34; CHECK-NEXT:        %arrayidx = getelementptr inbounds i32, ptr %a, i64 %idxprom
35; CHECK-NEXT:    Grouped accesses:
36; CHECK-NEXT:      Group
37; CHECK-NEXT:        (Low: %b High: ((4 * (1 umax %x)) + %b))
38; CHECK-NEXT:          Member: {%b,+,4}<%for.body>
39; CHECK-NEXT:      Group
40; CHECK-NEXT:        (Low: (4 + %a) High: (4 + (4 * (1 umax %x)) + %a))
41; CHECK-NEXT:          Member: {(4 + %a),+,4}<%for.body>
42; CHECK:         Non vectorizable stores to invariant address were not found in loop.
43; CHECK-NEXT:    SCEV assumptions:
44; CHECK-NEXT:    {1,+,1}<%for.body> Added Flags: <nusw>
45; CHECK:         Expressions re-written:
46; CHECK-NEXT:    [PSE]  %arrayidx = getelementptr inbounds i32, ptr %a, i64 %idxprom:
47; CHECK-NEXT:      ((4 * (zext i32 {1,+,1}<%for.body> to i64))<nuw><nsw> + %a)<nuw>
48; CHECK-NEXT:      --> {(4 + %a),+,4}<%for.body>
49; CHECK-NEXT:    [PSE]  %arrayidx4 = getelementptr inbounds i32, ptr %b, i64 %conv11:
50; CHECK-NEXT:      ((4 * (zext i32 {0,+,1}<%for.body> to i64))<nuw><nsw> + %b)<nuw>
51; CHECK-NEXT:      --> {%b,+,4}<%for.body>
52define void @test1(i64 %x, ptr %a, ptr %b) {
53entry:
54  br label %for.body
55
56for.body:                                         ; preds = %for.body.preheader, %for.body
57  %conv11 = phi i64 [ %conv, %for.body ], [ 0, %entry ]
58  %i.010 = phi i32 [ %add, %for.body ], [ 0, %entry ]
59  %add = add i32 %i.010, 1
60  %idxprom = zext i32 %add to i64
61  %arrayidx = getelementptr inbounds i32, ptr %a, i64 %idxprom
62  %ld = load i32, ptr %arrayidx, align 4
63  %add2 = add nsw i32 %ld, 1
64  %arrayidx4 = getelementptr inbounds i32, ptr %b, i64 %conv11
65  store i32 %add2, ptr %arrayidx4, align 4
66  %conv = zext i32 %add to i64
67  %cmp = icmp ult i64 %conv, %x
68  br i1 %cmp, label %for.body, label %exit
69
70exit:
71  ret void
72}
73
74; i can overflow in the following kernel:
75; void test2(unsigned long long x, int *a) {
76;   for (unsigned i = 0; i < x; ++i)
77;     a[i] = a[i] + 1;
78; }
79;
80; We need to check that i doesn't wrap, but we don't need a run-time alias
81; check. We also need an extra no-wrap check to get the backedge taken count.
82
83; CHECK-LABEL: test2
84; CHECK: Memory dependences are safe
85; CHECK: SCEV assumptions:
86; CHECK-NEXT:   {1,+,1}<%for.body> Added Flags: <nusw>
87 define void @test2(i64 %x, ptr %a) {
88entry:
89  br label %for.body
90
91for.body:
92  %conv11 = phi i64 [ %conv, %for.body ], [ 0, %entry ]
93  %i.010 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
94  %arrayidx = getelementptr inbounds i32, ptr %a, i64 %conv11
95  %ld = load i32, ptr %arrayidx, align 4
96  %add = add nsw i32 %ld, 1
97  store i32 %add, ptr %arrayidx, align 4
98  %inc = add i32 %i.010, 1
99  %conv = zext i32 %inc to i64
100  %cmp = icmp ult i64 %conv, %x
101  br i1 %cmp, label %for.body, label %exit
102
103exit:
104  ret void
105}
106