1 ; RUN: opt -passes=loop-load-elim -S < %s | FileCheck %s
3 ; Simple st->ld forwarding derived from a lexical backward dep.
5 ; for (unsigned i = 0; i < 100; i++)
6 ; A[i+1] = A[i] + B[i];
8 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
10 define void @f(ptr noalias nocapture %A, ptr noalias nocapture readonly %B, i64 %N) {
12 ; CHECK: %load_initial = load i32, ptr %A
15 for.body: ; preds = %for.body, %entry
16 ; CHECK: %store_forwarded = phi i32 [ %load_initial, %entry ], [ %add, %for.body ]
17 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
18 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv
19 %load = load i32, ptr %arrayidx, align 4
20 %arrayidx2 = getelementptr inbounds i32, ptr %B, i64 %indvars.iv
21 %load_1 = load i32, ptr %arrayidx2, align 4
22 ; CHECK: %add = add i32 %load_1, %store_forwarded
23 %add = add i32 %load_1, %load
24 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
25 %arrayidx_next = getelementptr inbounds i32, ptr %A, i64 %indvars.iv.next
26 store i32 %add, ptr %arrayidx_next, align 4
27 %exitcond = icmp eq i64 %indvars.iv.next, %N
28 br i1 %exitcond, label %for.end, label %for.body
30 for.end: ; preds = %for.body
34 ; Same but loop is descending.
36 ; for (unsigned i = N; i > 0; i--)
37 ; A[i-1] = A[i] + B[i];
38 define void @g(ptr noalias nocapture %A, ptr noalias nocapture readonly %B, i64 %N) {
40 ; CHECK: %0 = shl i64 %N, 2
41 ; CHECK: %scevgep = getelementptr i8, ptr %A, i64 %0
42 ; CHECK: %load_initial = load i32, ptr %scevgep, align 4
45 for.body: ; preds = %for.body, %entry
46 ; CHECK: %store_forwarded = phi i32 [ %load_initial, %entry ], [ %add, %for.body ]
47 %i.09 = phi i64 [ %sub, %for.body ], [ %N, %entry ]
48 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %i.09
49 %load = load i32, ptr %arrayidx, align 4
50 %arrayidx1 = getelementptr inbounds i32, ptr %B, i64 %i.09
51 %load_1 = load i32, ptr %arrayidx1, align 4
52 ; CHECK: %add = add i32 %load_1, %store_forwarded
53 %add = add i32 %load_1, %load
54 %sub = add i64 %i.09, -1
55 %arrayidx2 = getelementptr inbounds i32, ptr %A, i64 %sub
56 store i32 %add, ptr %arrayidx2, align 4
57 %cmp.not = icmp eq i64 %sub, 0
58 br i1 %cmp.not, label %for.end, label %for.body
60 for.end: ; preds = %for.body