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