Add a dereferenceable attribute
[oota-llvm.git] / test / Transforms / LICM / hoist-deref-load.ll
1 ; RUN: opt -S -basicaa -licm < %s | FileCheck %s
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 ; This test represents the following function:
6 ; void test1(int * __restrict__ a, int * __restrict__ b, int &c, int n) {
7 ;   for (int i = 0; i < n; ++i)
8 ;     if (a[i] > 0)
9 ;       a[i] = c*b[i];
10 ; }
11 ; and we want to hoist the load of %c out of the loop. This can be done only
12 ; because the dereferenceable attribute is on %c.
13
14 ; CHECK-LABEL: @test1
15 ; CHECK: load i32* %c, align 4
16 ; CHECK: for.body:
17
18 define void @test1(i32* noalias nocapture %a, i32* noalias nocapture readonly %b, i32* nocapture readonly nonnull dereferenceable(4) %c, i32 %n) #0 {
19 entry:
20   %cmp11 = icmp sgt i32 %n, 0
21   br i1 %cmp11, label %for.body, label %for.end
22
23 for.body:                                         ; preds = %entry, %for.inc
24   %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
25   %arrayidx = getelementptr inbounds i32* %a, i64 %indvars.iv
26   %0 = load i32* %arrayidx, align 4
27   %cmp1 = icmp sgt i32 %0, 0
28   br i1 %cmp1, label %if.then, label %for.inc
29
30 if.then:                                          ; preds = %for.body
31   %1 = load i32* %c, align 4
32   %arrayidx3 = getelementptr inbounds i32* %b, i64 %indvars.iv
33   %2 = load i32* %arrayidx3, align 4
34   %mul = mul nsw i32 %2, %1
35   store i32 %mul, i32* %arrayidx, align 4
36   br label %for.inc
37
38 for.inc:                                          ; preds = %for.body, %if.then
39   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
40   %lftr.wideiv = trunc i64 %indvars.iv.next to i32
41   %exitcond = icmp eq i32 %lftr.wideiv, %n
42   br i1 %exitcond, label %for.end, label %for.body
43
44 for.end:                                          ; preds = %for.inc, %entry
45   ret void
46 }
47
48 ; This is the same as @test1, but without the dereferenceable attribute on %c.
49 ; Without this attribute, we should not hoist the load of %c.
50
51 ; CHECK-LABEL: @test2
52 ; CHECK: if.then:
53 ; CHECK: load i32* %c, align 4
54
55 define void @test2(i32* noalias nocapture %a, i32* noalias nocapture readonly %b, i32* nocapture readonly nonnull %c, i32 %n) #0 {
56 entry:
57   %cmp11 = icmp sgt i32 %n, 0
58   br i1 %cmp11, label %for.body, label %for.end
59
60 for.body:                                         ; preds = %entry, %for.inc
61   %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
62   %arrayidx = getelementptr inbounds i32* %a, i64 %indvars.iv
63   %0 = load i32* %arrayidx, align 4
64   %cmp1 = icmp sgt i32 %0, 0
65   br i1 %cmp1, label %if.then, label %for.inc
66
67 if.then:                                          ; preds = %for.body
68   %1 = load i32* %c, align 4
69   %arrayidx3 = getelementptr inbounds i32* %b, i64 %indvars.iv
70   %2 = load i32* %arrayidx3, align 4
71   %mul = mul nsw i32 %2, %1
72   store i32 %mul, i32* %arrayidx, align 4
73   br label %for.inc
74
75 for.inc:                                          ; preds = %for.body, %if.then
76   %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
77   %lftr.wideiv = trunc i64 %indvars.iv.next to i32
78   %exitcond = icmp eq i32 %lftr.wideiv, %n
79   br i1 %exitcond, label %for.end, label %for.body
80
81 for.end:                                          ; preds = %for.inc, %entry
82   ret void
83 }
84
85 attributes #0 = { nounwind uwtable }
86