1; RUN: opt < %s -basicaa -gvn -enable-load-pre -S | FileCheck %s
2; RUN: opt < %s -aa-pipeline=basic-aa -passes=gvn -enable-load-pre -S | FileCheck %s
3
4target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
5target triple = "aarch64--linux-gnu"
6
7define double @foo(i32 %stat, i32 %i, double** %p) {
8; CHECK-LABEL: @foo(
9entry:
10  switch i32 %stat, label %sw.default [
11    i32 0, label %sw.bb
12    i32 1, label %sw.bb
13    i32 2, label %sw.bb2
14  ]
15
16sw.bb:                                            ; preds = %entry, %entry
17  %idxprom = sext i32 %i to i64
18  %arrayidx = getelementptr inbounds double*, double** %p, i64 0
19  %0 = load double*, double** %arrayidx, align 8
20  %arrayidx1 = getelementptr inbounds double, double* %0, i64 %idxprom
21  %1 = load double, double* %arrayidx1, align 8
22  %sub = fsub double %1, 1.000000e+00
23  %cmp = fcmp olt double %sub, 0.000000e+00
24  br i1 %cmp, label %if.then, label %if.end
25
26if.then:                                          ; preds = %sw.bb
27  br label %return
28
29if.end:                                           ; preds = %sw.bb
30  br label %sw.bb2
31
32sw.bb2:                                           ; preds = %if.end, %entry
33  %idxprom3 = sext i32 %i to i64
34  %arrayidx4 = getelementptr inbounds double*, double** %p, i64 0
35  %2 = load double*, double** %arrayidx4, align 8
36  %arrayidx5 = getelementptr inbounds double, double* %2, i64 %idxprom3
37  %3 = load double, double* %arrayidx5, align 8
38; CHECK: sw.bb2:
39; CHECK-NOT: sext
40; CHECK-NEXT: phi double [
41; CHECK-NOT: load
42  %sub6 = fsub double 3.000000e+00, %3
43  br label %return
44
45sw.default:                                       ; preds = %entry
46  br label %return
47
48return:                                           ; preds = %sw.default, %sw.bb2, %if.then
49  %retval.0 = phi double [ 0.000000e+00, %sw.default ], [ %sub6, %sw.bb2 ], [ %sub, %if.then ]
50  ret double %retval.0
51}
52
53; The load causes the GEP's operands to be PREd earlier than normal. The
54; resulting sext ends up in pre.dest and in the GVN system before that BB is
55; actually processed. Make sure we can deal with the situation.
56
57define void @test_shortcut_safe(i1 %tst, i32 %p1, i32* %a) {
58; CHECK-LABEL: define void @test_shortcut_safe
59; CHECK: [[SEXT1:%.*]] = sext i32 %p1 to i64
60; CHECK: [[PHI1:%.*]] = phi i64 [ [[SEXT1]], {{%.*}} ], [ [[PHI2:%.*]], {{%.*}} ]
61; CHECK: [[SEXT2:%.*]] = sext i32 %p1 to i64
62; CHECK: [[PHI2]] = phi i64 [ [[SEXT2]], {{.*}} ], [ [[PHI1]], {{%.*}} ]
63; CHECK: getelementptr inbounds i32, i32* %a, i64 [[PHI2]]
64
65  br i1 %tst, label %sext1, label %pre.dest
66
67pre.dest:
68  br label %sext.use
69
70sext1:
71  %idxprom = sext i32 %p1 to i64
72  br label %sext.use
73
74sext.use:
75  %idxprom2 = sext i32 %p1 to i64
76  %arrayidx3 = getelementptr inbounds i32, i32* %a, i64 %idxprom2
77  %val = load i32, i32* %arrayidx3, align 4
78  tail call void (i32) @g(i32 %val)
79  br label %pre.dest
80}
81
82declare void @g(i32)
83