xref: /aosp_15_r20/external/llvm/test/CodeGen/ARM/arm-and-tst-peephole.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: llc -mtriple=arm-eabi -arm-atomic-cfg-tidy=0 %s -o - | FileCheck -check-prefix=ARM %s
2*9880d681SAndroid Build Coastguard Worker; RUN: llc -mtriple=thumb-eabi -arm-atomic-cfg-tidy=0 %s -o - | FileCheck -check-prefix=THUMB %s
3*9880d681SAndroid Build Coastguard Worker; RUN: llc -mtriple=thumb-eabi -arm-atomic-cfg-tidy=0 -mcpu=arm1156t2-s -mattr=+thumb2 %s -o - \
4*9880d681SAndroid Build Coastguard Worker; RUN:   | FileCheck -check-prefix=T2 %s
5*9880d681SAndroid Build Coastguard Worker; RUN: llc -mtriple=thumbv8-eabi -arm-atomic-cfg-tidy=0 %s -o - | FileCheck -check-prefix=V8 %s
6*9880d681SAndroid Build Coastguard Worker
7*9880d681SAndroid Build Coastguard Worker; FIXME: The -march=thumb test doesn't change if -disable-peephole is specified.
8*9880d681SAndroid Build Coastguard Worker
9*9880d681SAndroid Build Coastguard Worker%struct.Foo = type { i8* }
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard Worker; ARM-LABEL:   foo:
12*9880d681SAndroid Build Coastguard Worker; THUMB-LABEL: foo:
13*9880d681SAndroid Build Coastguard Worker; T2-LABEL:    foo:
14*9880d681SAndroid Build Coastguard Workerdefine %struct.Foo* @foo(%struct.Foo* %this, i32 %acc) nounwind readonly align 2 {
15*9880d681SAndroid Build Coastguard Workerentry:
16*9880d681SAndroid Build Coastguard Worker  %scevgep = getelementptr %struct.Foo, %struct.Foo* %this, i32 1
17*9880d681SAndroid Build Coastguard Worker  br label %tailrecurse
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Workertailrecurse:                                      ; preds = %sw.bb, %entry
20*9880d681SAndroid Build Coastguard Worker  %lsr.iv2 = phi %struct.Foo* [ %scevgep3, %sw.bb ], [ %scevgep, %entry ]
21*9880d681SAndroid Build Coastguard Worker  %lsr.iv = phi i32 [ %lsr.iv.next, %sw.bb ], [ 1, %entry ]
22*9880d681SAndroid Build Coastguard Worker  %acc.tr = phi i32 [ %or, %sw.bb ], [ %acc, %entry ]
23*9880d681SAndroid Build Coastguard Worker  %lsr.iv24 = bitcast %struct.Foo* %lsr.iv2 to i8**
24*9880d681SAndroid Build Coastguard Worker  %scevgep5 = getelementptr i8*, i8** %lsr.iv24, i32 -1
25*9880d681SAndroid Build Coastguard Worker  %tmp2 = load i8*, i8** %scevgep5
26*9880d681SAndroid Build Coastguard Worker  %0 = ptrtoint i8* %tmp2 to i32
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker; ARM:      ands {{r[0-9]+}}, {{r[0-9]+}}, #3
29*9880d681SAndroid Build Coastguard Worker; ARM-NEXT: beq
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker; THUMB:      movs r[[R0:[0-9]+]], #3
32*9880d681SAndroid Build Coastguard Worker; THUMB-NEXT: ands r[[R0]], r
33*9880d681SAndroid Build Coastguard Worker; THUMB-NEXT: cmp r[[R0]], #0
34*9880d681SAndroid Build Coastguard Worker; THUMB-NEXT: beq
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker; T2:      ands {{r[0-9]+}}, {{r[0-9]+}}, #3
37*9880d681SAndroid Build Coastguard Worker; T2-NEXT: beq
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker  %and = and i32 %0, 3
40*9880d681SAndroid Build Coastguard Worker  %tst = icmp eq i32 %and, 0
41*9880d681SAndroid Build Coastguard Worker  br i1 %tst, label %sw.bb, label %tailrecurse.switch
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Workertailrecurse.switch:                               ; preds = %tailrecurse
44*9880d681SAndroid Build Coastguard Worker; V8-LABEL: %tailrecurse.switch
45*9880d681SAndroid Build Coastguard Worker; V8: cmp
46*9880d681SAndroid Build Coastguard Worker; V8-NEXT: beq
47*9880d681SAndroid Build Coastguard Worker; V8-NEXT: %tailrecurse.switch
48*9880d681SAndroid Build Coastguard Worker; V8: cmp
49*9880d681SAndroid Build Coastguard Worker; V8-NEXT: beq
50*9880d681SAndroid Build Coastguard Worker; V8-NEXT: %tailrecurse.switch
51*9880d681SAndroid Build Coastguard Worker; V8: cmp
52*9880d681SAndroid Build Coastguard Worker; V8-NEXT: beq
53*9880d681SAndroid Build Coastguard Worker; V8-NEXT: b
54*9880d681SAndroid Build Coastguard Worker; The trailing space in the last line checks that the branch is unconditional
55*9880d681SAndroid Build Coastguard Worker  switch i32 %and, label %sw.epilog [
56*9880d681SAndroid Build Coastguard Worker    i32 1, label %sw.bb
57*9880d681SAndroid Build Coastguard Worker    i32 3, label %sw.bb6
58*9880d681SAndroid Build Coastguard Worker    i32 2, label %sw.bb8
59*9880d681SAndroid Build Coastguard Worker  ], !prof !1
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Workersw.bb:                                            ; preds = %tailrecurse.switch, %tailrecurse
62*9880d681SAndroid Build Coastguard Worker  %shl = shl i32 %acc.tr, 1
63*9880d681SAndroid Build Coastguard Worker  %or = or i32 %and, %shl
64*9880d681SAndroid Build Coastguard Worker  %lsr.iv.next = add i32 %lsr.iv, 1
65*9880d681SAndroid Build Coastguard Worker  %scevgep3 = getelementptr %struct.Foo, %struct.Foo* %lsr.iv2, i32 1
66*9880d681SAndroid Build Coastguard Worker  br label %tailrecurse
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Workersw.bb6:                                           ; preds = %tailrecurse.switch
69*9880d681SAndroid Build Coastguard Worker  ret %struct.Foo* %lsr.iv2
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Workersw.bb8:                                           ; preds = %tailrecurse.switch
72*9880d681SAndroid Build Coastguard Worker  %tmp1 = add i32 %acc.tr, %lsr.iv
73*9880d681SAndroid Build Coastguard Worker  %add.ptr11 = getelementptr inbounds %struct.Foo, %struct.Foo* %this, i32 %tmp1
74*9880d681SAndroid Build Coastguard Worker  ret %struct.Foo* %add.ptr11
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Workersw.epilog:                                        ; preds = %tailrecurse.switch
77*9880d681SAndroid Build Coastguard Worker  ret %struct.Foo* undef
78*9880d681SAndroid Build Coastguard Worker}
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker; Another test that exercises the AND/TST peephole optimization and also
81*9880d681SAndroid Build Coastguard Worker; generates a predicated ANDS instruction. Check that the predicate is printed
82*9880d681SAndroid Build Coastguard Worker; after the "S" modifier on the instruction.
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker%struct.S = type { i8* (i8*)*, [1 x i8] }
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker; ARM-LABEL: bar:
87*9880d681SAndroid Build Coastguard Worker; THUMB-LABEL: bar:
88*9880d681SAndroid Build Coastguard Worker; T2-LABEL: bar:
89*9880d681SAndroid Build Coastguard Worker; V8-LABEL: bar:
90*9880d681SAndroid Build Coastguard Workerdefine internal zeroext i8 @bar(%struct.S* %x, %struct.S* nocapture %y) nounwind readonly {
91*9880d681SAndroid Build Coastguard Workerentry:
92*9880d681SAndroid Build Coastguard Worker  %0 = getelementptr inbounds %struct.S, %struct.S* %x, i32 0, i32 1, i32 0
93*9880d681SAndroid Build Coastguard Worker  %1 = load i8, i8* %0, align 1
94*9880d681SAndroid Build Coastguard Worker  %2 = zext i8 %1 to i32
95*9880d681SAndroid Build Coastguard Worker; ARM: ands
96*9880d681SAndroid Build Coastguard Worker; THUMB: ands
97*9880d681SAndroid Build Coastguard Worker; T2: ands
98*9880d681SAndroid Build Coastguard Worker; V8: ands
99*9880d681SAndroid Build Coastguard Worker; V8-NEXT: beq
100*9880d681SAndroid Build Coastguard Worker  %3 = and i32 %2, 112
101*9880d681SAndroid Build Coastguard Worker  %4 = icmp eq i32 %3, 0
102*9880d681SAndroid Build Coastguard Worker  br i1 %4, label %return, label %bb
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Workerbb:                                               ; preds = %entry
105*9880d681SAndroid Build Coastguard Worker; V8-NEXT: %bb
106*9880d681SAndroid Build Coastguard Worker  %5 = getelementptr inbounds %struct.S, %struct.S* %y, i32 0, i32 1, i32 0
107*9880d681SAndroid Build Coastguard Worker  %6 = load i8, i8* %5, align 1
108*9880d681SAndroid Build Coastguard Worker  %7 = zext i8 %6 to i32
109*9880d681SAndroid Build Coastguard Worker; ARM: andsne
110*9880d681SAndroid Build Coastguard Worker; THUMB: ands
111*9880d681SAndroid Build Coastguard Worker; T2: andsne
112*9880d681SAndroid Build Coastguard Worker; V8: ands
113*9880d681SAndroid Build Coastguard Worker; V8-NEXT: beq
114*9880d681SAndroid Build Coastguard Worker  %8 = and i32 %7, 112
115*9880d681SAndroid Build Coastguard Worker  %9 = icmp eq i32 %8, 0
116*9880d681SAndroid Build Coastguard Worker  br i1 %9, label %return, label %bb2
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Workerbb2:                                              ; preds = %bb
119*9880d681SAndroid Build Coastguard Worker; V8-NEXT: %bb2
120*9880d681SAndroid Build Coastguard Worker; V8-NEXT: cmp
121*9880d681SAndroid Build Coastguard Worker; V8-NEXT: it	ne
122*9880d681SAndroid Build Coastguard Worker; V8-NEXT: cmpne
123*9880d681SAndroid Build Coastguard Worker; V8-NEXT: bne
124*9880d681SAndroid Build Coastguard Worker  %10 = icmp eq i32 %3, 16
125*9880d681SAndroid Build Coastguard Worker  %11 = icmp eq i32 %8, 16
126*9880d681SAndroid Build Coastguard Worker  %or.cond = or i1 %10, %11
127*9880d681SAndroid Build Coastguard Worker  br i1 %or.cond, label %bb4, label %return
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Workerbb4:                                              ; preds = %bb2
130*9880d681SAndroid Build Coastguard Worker  %12 = ptrtoint %struct.S* %x to i32
131*9880d681SAndroid Build Coastguard Worker  %phitmp = trunc i32 %12 to i8
132*9880d681SAndroid Build Coastguard Worker  ret i8 %phitmp
133*9880d681SAndroid Build Coastguard Worker
134*9880d681SAndroid Build Coastguard Workerreturn:                                           ; preds = %bb2, %bb, %entry
135*9880d681SAndroid Build Coastguard Worker  ret i8 1
136*9880d681SAndroid Build Coastguard Worker}
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker
139*9880d681SAndroid Build Coastguard Worker; We were looking through multiple COPY instructions to find an AND we might
140*9880d681SAndroid Build Coastguard Worker; fold into a TST, but in doing so we changed the register being tested allowing
141*9880d681SAndroid Build Coastguard Worker; folding of unrelated tests (in this case, a TST against r1 was eliminated in
142*9880d681SAndroid Build Coastguard Worker; favour of an AND of r0).
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker; ARM-LABEL: test_tst_assessment:
145*9880d681SAndroid Build Coastguard Worker; THUMB-LABEL: test_tst_assessment:
146*9880d681SAndroid Build Coastguard Worker; T2-LABEL: test_tst_assessment:
147*9880d681SAndroid Build Coastguard Worker; V8-LABEL: test_tst_assessment:
148*9880d681SAndroid Build Coastguard Workerdefine i32 @test_tst_assessment(i1 %lhs, i1 %rhs) {
149*9880d681SAndroid Build Coastguard Worker  %lhs32 = zext i1 %lhs to i32
150*9880d681SAndroid Build Coastguard Worker  %rhs32 = zext i1 %rhs to i32
151*9880d681SAndroid Build Coastguard Worker  %diff = sub nsw i32 %lhs32, %rhs32
152*9880d681SAndroid Build Coastguard Worker; ARM: tst r1, #1
153*9880d681SAndroid Build Coastguard Worker; THUMB: movs [[RTMP:r[0-9]+]], #1
154*9880d681SAndroid Build Coastguard Worker; THUMB: tst r1, [[RTMP]]
155*9880d681SAndroid Build Coastguard Worker; T2: tst.w r1, #1
156*9880d681SAndroid Build Coastguard Worker; V8: tst.w r1, #1
157*9880d681SAndroid Build Coastguard Worker  ret i32 %diff
158*9880d681SAndroid Build Coastguard Worker}
159*9880d681SAndroid Build Coastguard Worker
160*9880d681SAndroid Build Coastguard Worker!1 = !{!"branch_weights", i32 1, i32 1, i32 3, i32 2 }
161