xref: /llvm-project/llvm/test/Transforms/InstCombine/select-of-symmetric-selects.ll (revision c034c44362a5dda93a8049d452625c59b76f7169)
1*c034c443STim Gymnich; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2*c034c443STim Gymnich; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3*c034c443STim Gymnich
4*c034c443STim Gymnichdefine i32 @select_of_symmetric_selects(i32 %a, i32 %b, i1 %c1, i1 %c2) {
5*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects(
6*c034c443STim Gymnich; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[C1:%.*]], [[C2:%.*]]
7*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP1]], i32 [[B:%.*]], i32 [[A:%.*]]
8*c034c443STim Gymnich; CHECK-NEXT:    ret i32 [[RET]]
9*c034c443STim Gymnich;
10*c034c443STim Gymnich  %sel1 = select i1 %c1, i32 %a, i32 %b
11*c034c443STim Gymnich  %sel2 = select i1 %c1, i32 %b, i32 %a
12*c034c443STim Gymnich  %ret = select i1 %c2, i32 %sel1, i32 %sel2
13*c034c443STim Gymnich  ret i32 %ret
14*c034c443STim Gymnich}
15*c034c443STim Gymnich
16*c034c443STim Gymnichdefine i32 @select_of_symmetric_selects_negative1(i32 %a, i32 %b, i1 %c1, i1 %c2) {
17*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_negative1(
18*c034c443STim Gymnich; CHECK-NEXT:    [[SEL1:%.*]] = select i1 [[C1:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]
19*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[C2:%.*]], i32 [[SEL1]], i32 [[A]]
20*c034c443STim Gymnich; CHECK-NEXT:    ret i32 [[RET]]
21*c034c443STim Gymnich;
22*c034c443STim Gymnich  %sel1 = select i1 %c1, i32 %a, i32 %b
23*c034c443STim Gymnich  %sel2 = select i1 %c2, i32 %b, i32 %a
24*c034c443STim Gymnich  %ret = select i1 %c2, i32 %sel1, i32 %sel2
25*c034c443STim Gymnich  ret i32 %ret
26*c034c443STim Gymnich}
27*c034c443STim Gymnich
28*c034c443STim Gymnichdefine i32 @select_of_symmetric_selects_negative2(i32 %a, i32 %b, i32 %c, i1 %c1, i1 %c2) {
29*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_negative2(
30*c034c443STim Gymnich; CHECK-NEXT:    [[SEL1:%.*]] = select i1 [[C1:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]
31*c034c443STim Gymnich; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C1]], i32 [[B]], i32 [[C:%.*]]
32*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[C2:%.*]], i32 [[SEL1]], i32 [[SEL2]]
33*c034c443STim Gymnich; CHECK-NEXT:    ret i32 [[RET]]
34*c034c443STim Gymnich;
35*c034c443STim Gymnich  %sel1 = select i1 %c1, i32 %a, i32 %b
36*c034c443STim Gymnich  %sel2 = select i1 %c1, i32 %b, i32 %c
37*c034c443STim Gymnich  %ret = select i1 %c2, i32 %sel1, i32 %sel2
38*c034c443STim Gymnich  ret i32 %ret
39*c034c443STim Gymnich}
40*c034c443STim Gymnich
41*c034c443STim Gymnichdeclare void @use(i32)
42*c034c443STim Gymnich
43*c034c443STim Gymnichdefine i32 @select_of_symmetric_selects_multi_use1(i32 %a, i32 %b, i1 %c1, i1 %c2) {
44*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_multi_use1(
45*c034c443STim Gymnich; CHECK-NEXT:    [[SEL1:%.*]] = select i1 [[C1:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]
46*c034c443STim Gymnich; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C1]], i32 [[B]], i32 [[A]]
47*c034c443STim Gymnich; CHECK-NEXT:    call void @use(i32 [[SEL2]])
48*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[C2:%.*]], i32 [[SEL1]], i32 [[SEL2]]
49*c034c443STim Gymnich; CHECK-NEXT:    ret i32 [[RET]]
50*c034c443STim Gymnich;
51*c034c443STim Gymnich  %sel1 = select i1 %c1, i32 %a, i32 %b
52*c034c443STim Gymnich  %sel2 = select i1 %c1, i32 %b, i32 %a
53*c034c443STim Gymnich  call void @use(i32 %sel2)
54*c034c443STim Gymnich  %ret = select i1 %c2, i32 %sel1, i32 %sel2
55*c034c443STim Gymnich  ret i32 %ret
56*c034c443STim Gymnich}
57*c034c443STim Gymnich
58*c034c443STim Gymnichdefine i32 @select_of_symmetric_selects_multi_use2(i32 %a, i32 %b, i1 %c1, i1 %c2) {
59*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_multi_use2(
60*c034c443STim Gymnich; CHECK-NEXT:    [[SEL1:%.*]] = select i1 [[C1:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]
61*c034c443STim Gymnich; CHECK-NEXT:    call void @use(i32 [[SEL1]])
62*c034c443STim Gymnich; CHECK-NEXT:    [[SEL2:%.*]] = select i1 [[C1]], i32 [[B]], i32 [[A]]
63*c034c443STim Gymnich; CHECK-NEXT:    call void @use(i32 [[SEL2]])
64*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[C2:%.*]], i32 [[SEL1]], i32 [[SEL2]]
65*c034c443STim Gymnich; CHECK-NEXT:    ret i32 [[RET]]
66*c034c443STim Gymnich;
67*c034c443STim Gymnich  %sel1 = select i1 %c1, i32 %a, i32 %b
68*c034c443STim Gymnich  call void @use(i32 %sel1)
69*c034c443STim Gymnich  %sel2 = select i1 %c1, i32 %b, i32 %a
70*c034c443STim Gymnich  call void @use(i32 %sel2)
71*c034c443STim Gymnich  %ret = select i1 %c2, i32 %sel1, i32 %sel2
72*c034c443STim Gymnich  ret i32 %ret
73*c034c443STim Gymnich}
74*c034c443STim Gymnich
75*c034c443STim Gymnichdefine i32 @select_of_symmetric_selects_commuted(i32 %a, i32 %b, i1 %c1, i1 %c2) {
76*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_commuted(
77*c034c443STim Gymnich; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[C1:%.*]], [[C2:%.*]]
78*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP1]], i32 [[A:%.*]], i32 [[B:%.*]]
79*c034c443STim Gymnich; CHECK-NEXT:    ret i32 [[RET]]
80*c034c443STim Gymnich;
81*c034c443STim Gymnich  %sel1 = select i1 %c1, i32 %a, i32 %b
82*c034c443STim Gymnich  %sel2 = select i1 %c1, i32 %b, i32 %a
83*c034c443STim Gymnich  %ret = select i1 %c2, i32 %sel2, i32 %sel1
84*c034c443STim Gymnich  ret i32 %ret
85*c034c443STim Gymnich}
86*c034c443STim Gymnich
87*c034c443STim Gymnichdefine <4 x i32> @select_of_symmetric_selects_vector1(<4 x i32> %a, <4 x i32> %b, i1 %c1, i1 %c2) {
88*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_vector1(
89*c034c443STim Gymnich; CHECK-NEXT:    [[TMP1:%.*]] = xor i1 [[C1:%.*]], [[C2:%.*]]
90*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[TMP1]], <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]]
91*c034c443STim Gymnich; CHECK-NEXT:    ret <4 x i32> [[RET]]
92*c034c443STim Gymnich;
93*c034c443STim Gymnich  %sel1 = select i1 %c1, <4 x i32> %a, <4 x i32> %b
94*c034c443STim Gymnich  %sel2 = select i1 %c1, <4 x i32> %b, <4 x i32> %a
95*c034c443STim Gymnich  %ret = select i1 %c2, <4 x i32> %sel2, <4 x i32> %sel1
96*c034c443STim Gymnich  ret <4 x i32> %ret
97*c034c443STim Gymnich}
98*c034c443STim Gymnich
99*c034c443STim Gymnichdefine <4 x i32> @select_of_symmetric_selects_vector2(<4 x i32> %a, <4 x i32> %b, <4 x i1> %c1, <4 x i1> %c2) {
100*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_vector2(
101*c034c443STim Gymnich; CHECK-NEXT:    [[TMP1:%.*]] = xor <4 x i1> [[C1:%.*]], [[C2:%.*]]
102*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> [[A:%.*]], <4 x i32> [[B:%.*]]
103*c034c443STim Gymnich; CHECK-NEXT:    ret <4 x i32> [[RET]]
104*c034c443STim Gymnich;
105*c034c443STim Gymnich  %sel1 = select <4 x i1> %c1, <4 x i32> %a, <4 x i32> %b
106*c034c443STim Gymnich  %sel2 = select <4 x i1> %c1, <4 x i32> %b, <4 x i32> %a
107*c034c443STim Gymnich  %ret = select <4 x i1> %c2, <4 x i32> %sel2, <4 x i32> %sel1
108*c034c443STim Gymnich  ret <4 x i32> %ret
109*c034c443STim Gymnich}
110*c034c443STim Gymnich
111*c034c443STim Gymnichdefine <2 x i32> @select_of_symmetric_selects_vector3(<2 x i32> %a, <2 x i32> %b, <2 x i1> %c1, i1 %c2) {
112*c034c443STim Gymnich; CHECK-LABEL: @select_of_symmetric_selects_vector3(
113*c034c443STim Gymnich; CHECK-NEXT:    [[SEL1:%.*]] = select <2 x i1> [[C1:%.*]], <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]]
114*c034c443STim Gymnich; CHECK-NEXT:    [[SEL2:%.*]] = select <2 x i1> [[C1]], <2 x i32> [[B]], <2 x i32> [[A]]
115*c034c443STim Gymnich; CHECK-NEXT:    [[RET:%.*]] = select i1 [[C2:%.*]], <2 x i32> [[SEL1]], <2 x i32> [[SEL2]]
116*c034c443STim Gymnich; CHECK-NEXT:    ret <2 x i32> [[RET]]
117*c034c443STim Gymnich;
118*c034c443STim Gymnich  %sel1 = select <2 x i1> %c1, <2 x i32> %a, <2 x i32> %b
119*c034c443STim Gymnich  %sel2 = select <2 x i1> %c1, <2 x i32> %b, <2 x i32> %a
120*c034c443STim Gymnich  %ret = select i1 %c2, <2 x i32> %sel1, <2 x i32> %sel2
121*c034c443STim Gymnich  ret <2 x i32> %ret
122*c034c443STim Gymnich  }
123