1; RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs < %s | FileCheck %s
2; RUN: opt -S -codegenprepare -mtriple=aarch64-linux %s | FileCheck --check-prefix=CHECK-CGP %s
3
4@A = global i32 zeroinitializer
5@B = global i32 zeroinitializer
6@C = global i32 zeroinitializer
7
8; Test that and is sunk into cmp block to form tbz.
9define i32 @and_sink1(i32 %a, i1 %c) {
10; CHECK-LABEL: and_sink1:
11; CHECK: tbz w1, #0
12; CHECK: str wzr, [x{{[0-9]+}}, :lo12:A]
13; CHECK: tbnz {{w[0-9]+}}, #2
14
15; CHECK-CGP-LABEL: @and_sink1(
16; CHECK-CGP-NOT: and i32
17  %and = and i32 %a, 4
18  br i1 %c, label %bb0, label %bb2
19bb0:
20; CHECK-CGP-LABEL: bb0:
21; CHECK-CGP: and i32
22; CHECK-CGP-NEXT: icmp eq i32
23; CHECK-CGP-NEXT: store
24; CHECK-CGP-NEXT: br
25  %cmp = icmp eq i32 %and, 0
26  store i32 0, i32* @A
27  br i1 %cmp, label %bb1, label %bb2
28bb1:
29  ret i32 1
30bb2:
31  ret i32 0
32}
33
34; Test that both 'and' and cmp get sunk to form tbz.
35define i32 @and_sink2(i32 %a, i1 %c, i1 %c2) {
36; CHECK-LABEL: and_sink2:
37; CHECK: str wzr, [x{{[0-9]+}}, :lo12:A]
38; CHECK: tbz w1, #0
39; CHECK: str wzr, [x{{[0-9]+}}, :lo12:B]
40; CHECK: tbz w2, #0
41; CHECK: str wzr, [x{{[0-9]+}}, :lo12:C]
42; CHECK: tbnz {{w[0-9]+}}, #2
43
44; CHECK-CGP-LABEL: @and_sink2(
45; CHECK-CGP-NOT: and i32
46  %and = and i32 %a, 4
47  store i32 0, i32* @A
48  br i1 %c, label %bb0, label %bb3
49bb0:
50; CHECK-CGP-LABEL: bb0:
51; CHECK-CGP-NOT: and i32
52; CHECK-CGP-NOT: icmp
53  %cmp = icmp eq i32 %and, 0
54  store i32 0, i32* @B
55  br i1 %c2, label %bb1, label %bb3
56bb1:
57; CHECK-CGP-LABEL: bb1:
58; CHECK-CGP: and i32
59; CHECK-CGP-NEXT: icmp eq i32
60; CHECK-CGP-NEXT: store
61; CHECK-CGP-NEXT: br
62  store i32 0, i32* @C
63  br i1 %cmp, label %bb2, label %bb0
64bb2:
65  ret i32 1
66bb3:
67  ret i32 0
68}
69
70; Test that 'and' is not sunk since cbz is a better alternative.
71define i32 @and_sink3(i32 %a) {
72; CHECK-LABEL: and_sink3:
73; CHECK: and [[REG:w[0-9]+]], w0, #0x3
74; CHECK: [[LOOP:.L[A-Z0-9_]+]]:
75; CHECK: str wzr, [x{{[0-9]+}}, :lo12:A]
76; CHECK: cbz [[REG]], [[LOOP]]
77
78; CHECK-CGP-LABEL: @and_sink3(
79; CHECK-CGP-NEXT: and i32
80  %and = and i32 %a, 3
81  br label %bb0
82bb0:
83; CHECK-CGP-LABEL: bb0:
84; CHECK-CGP-NOT: and i32
85  %cmp = icmp eq i32 %and, 0
86  store i32 0, i32* @A
87  br i1 %cmp, label %bb0, label %bb2
88bb2:
89  ret i32 0
90}
91