1 ; RUN: opt %loadNPMPolly -polly-invariant-load-hoisting=true '-passes=print<polly-function-scops>' -disable-output < %s 2>&1 | FileCheck %s
2 ; RUN: opt %loadNPMPolly -S -passes=polly-codegen -polly-invariant-load-hoisting=true < %s | FileCheck %s --check-prefix=IR
3 ; RUN: opt %loadNPMPolly -S -passes=polly-codegen -polly-invariant-load-hoisting=true --polly-overflow-tracking=always < %s | FileCheck %s --check-prefix=IRA
5 ; As (p + q) can overflow we have to check that we load from
6 ; I[p + q] only if it does not.
8 ; CHECK: Invariant Accesses: {
9 ; CHECK-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
10 ; CHECK-NEXT: [N, p, q] -> { Stmt_for_body[i0] -> MemRef_I[p + q] };
11 ; CHECK-NEXT: Execution Context: [N, p, q] -> { : N > 0 and -2147483648 - p <= q <= 2147483647 - p }
12 ; CHECK-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
13 ; CHECK-NEXT: [N, p, q] -> { Stmt_for_body[i0] -> MemRef_tmp1[0] };
14 ; CHECK-NEXT: Execution Context: [N, p, q] -> { : N > 0 }
17 ; IR: polly.preload.merge:
18 ; IR-NEXT: %polly.preload.tmp1.merge = phi ptr [ %polly.access.I.load, %polly.preload.exec ], [ null, %polly.preload.cond ]
19 ; IR-NEXT: store ptr %polly.preload.tmp1.merge, ptr %tmp1.preload.s2a
20 ; IR-NEXT: %12 = sext i32 %N to i64
21 ; IR-NEXT: %13 = icmp sge i64 %12, 1
22 ; IR-NEXT: %14 = sext i32 %q to i64
23 ; IR-NEXT: %15 = sext i32 %p to i64
24 ; IR-NEXT: %16 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %15, i64 %14)
25 ; IR-NEXT: %.obit4 = extractvalue { i64, i1 } %16, 1
26 ; IR-NEXT: %polly.overflow.state5 = or i1 false, %.obit4
27 ; IR-NEXT: %.res6 = extractvalue { i64, i1 } %16, 0
28 ; IR-NEXT: %17 = icmp sle i64 %.res6, 2147483647
29 ; IR-NEXT: %18 = and i1 %13, %17
30 ; IR-NEXT: %19 = sext i32 %q to i64
31 ; IR-NEXT: %20 = sext i32 %p to i64
32 ; IR-NEXT: %21 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %20, i64 %19)
33 ; IR-NEXT: %.obit7 = extractvalue { i64, i1 } %21, 1
34 ; IR-NEXT: %polly.overflow.state8 = or i1 %polly.overflow.state5, %.obit7
35 ; IR-NEXT: %.res9 = extractvalue { i64, i1 } %21, 0
36 ; IR-NEXT: %22 = icmp sge i64 %.res9, -2147483648
37 ; IR-NEXT: %23 = and i1 %18, %22
38 ; IR-NEXT: %polly.preload.cond.overflown10 = xor i1 %polly.overflow.state8, true
39 ; IR-NEXT: %polly.preload.cond.result11 = and i1 %23, %polly.preload.cond.overflown10
40 ; IR-NEXT: br label %polly.preload.cond12
42 ; IR: polly.preload.cond12:
43 ; IR-NEXT: br i1 %polly.preload.cond.result11
45 ; IR: polly.preload.exec14:
46 ; IR-NEXT: %polly.access.polly.preload.tmp1.merge = getelementptr i32, ptr %polly.preload.tmp1.merge, i64 0
47 ; IR-NEXT: %polly.access.polly.preload.tmp1.merge.load = load i32, ptr %polly.access.polly.preload.tmp1.merge, align 4
49 ; IRA: polly.preload.merge:
50 ; IRA-NEXT: %polly.preload.tmp1.merge = phi ptr [ %polly.access.I.load, %polly.preload.exec ], [ null, %polly.preload.cond ]
51 ; IRA-NEXT: store ptr %polly.preload.tmp1.merge, ptr %tmp1.preload.s2a
52 ; IRA-NEXT: %12 = sext i32 %N to i64
53 ; IRA-NEXT: %13 = icmp sge i64 %12, 1
54 ; IRA-NEXT: %14 = sext i32 %q to i64
55 ; IRA-NEXT: %15 = sext i32 %p to i64
56 ; IRA-NEXT: %16 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %15, i64 %14)
57 ; IRA-NEXT: %.obit5 = extractvalue { i64, i1 } %16, 1
58 ; IRA-NEXT: %.res6 = extractvalue { i64, i1 } %16, 0
59 ; IRA-NEXT: %17 = icmp sle i64 %.res6, 2147483647
60 ; IRA-NEXT: %18 = and i1 %13, %17
61 ; IRA-NEXT: %19 = sext i32 %q to i64
62 ; IRA-NEXT: %20 = sext i32 %p to i64
63 ; IRA-NEXT: %21 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %20, i64 %19)
64 ; IRA-NEXT: %.obit7 = extractvalue { i64, i1 } %21, 1
65 ; IRA-NEXT: %.res8 = extractvalue { i64, i1 } %21, 0
66 ; IRA-NEXT: %22 = icmp sge i64 %.res8, -2147483648
67 ; IRA-NEXT: %23 = and i1 %18, %22
68 ; IRA-NEXT: %polly.preload.cond.overflown9 = xor i1 %.obit7, true
69 ; IRA-NEXT: %polly.preload.cond.result10 = and i1 %23, %polly.preload.cond.overflown9
70 ; IRA-NEXT: br label %polly.preload.cond11
72 ; IRA: polly.preload.cond11:
73 ; IRA-NEXT: br i1 %polly.preload.cond.result10
75 ; IRA: polly.preload.exec13:
76 ; IRA-NEXT: %polly.access.polly.preload.tmp1.merge = getelementptr i32, ptr %polly.preload.tmp1.merge, i64 0
77 ; IRA-NEXT: %polly.access.polly.preload.tmp1.merge.load = load i32, ptr %polly.access.polly.preload.tmp1.merge, align 4
79 ; void f(int **I, int *A, int N, int p, int q) {
80 ; for (int i = 0; i < N; i++)
84 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
86 define void @f(ptr %I, ptr %A, i32 %N, i32 %p, i32 %q) {
88 %tmp = sext i32 %N to i64
91 for.cond: ; preds = %for.inc, %entry
92 %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
93 %cmp = icmp slt i64 %indvars.iv, %tmp
94 br i1 %cmp, label %for.body, label %for.end
96 for.body: ; preds = %for.cond
98 %idxprom = sext i32 %add to i64
99 %arrayidx = getelementptr inbounds ptr, ptr %I, i64 %idxprom
100 %tmp1 = load ptr, ptr %arrayidx, align 8
101 %tmp2 = load i32, ptr %tmp1, align 4
102 %arrayidx2 = getelementptr inbounds i32, ptr %A, i64 %indvars.iv
103 store i32 %tmp2, ptr %arrayidx2, align 4
106 for.inc: ; preds = %for.body
107 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
110 for.end: ; preds = %for.cond