218 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=instcombine -S | FileCheck %s3 4; https://bugs.llvm.org/show_bug.cgi?id=381235 6; Pattern:7; x & C u< x8; Should be transformed into:9; x u> C10; Iff: isPowerOf2(C + 1)11; C can be 0 and -1.12 13; ============================================================================ ;14; Basic positive tests15; ============================================================================ ;16 17define i1 @p0(i8 %x) {18; CHECK-LABEL: @p0(19; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X:%.*]], 320; CHECK-NEXT: ret i1 [[RET]]21;22 %tmp0 = and i8 %x, 323 %ret = icmp ult i8 %tmp0, %x24 ret i1 %ret25}26 27define i1 @pv(i8 %x, i8 %y) {28; CHECK-LABEL: @pv(29; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]30; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X:%.*]], [[TMP0]]31; CHECK-NEXT: ret i1 [[RET]]32;33 %tmp0 = lshr i8 -1, %y34 %tmp1 = and i8 %tmp0, %x35 %ret = icmp ult i8 %tmp1, %x36 ret i1 %ret37}38 39; ============================================================================ ;40; Vector tests41; ============================================================================ ;42 43define <2 x i1> @p1_vec_splat(<2 x i8> %x) {44; CHECK-LABEL: @p1_vec_splat(45; CHECK-NEXT: [[RET:%.*]] = icmp ugt <2 x i8> [[X:%.*]], splat (i8 3)46; CHECK-NEXT: ret <2 x i1> [[RET]]47;48 %tmp0 = and <2 x i8> %x, <i8 3, i8 3>49 %ret = icmp ult <2 x i8> %tmp0, %x50 ret <2 x i1> %ret51}52 53define <2 x i1> @p2_vec_nonsplat(<2 x i8> %x) {54; CHECK-LABEL: @p2_vec_nonsplat(55; CHECK-NEXT: [[RET:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 15>56; CHECK-NEXT: ret <2 x i1> [[RET]]57;58 %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.59 %ret = icmp ult <2 x i8> %tmp0, %x60 ret <2 x i1> %ret61}62 63define <2 x i1> @p2_vec_nonsplat_edgecase0(<2 x i8> %x) {64; CHECK-LABEL: @p2_vec_nonsplat_edgecase0(65; CHECK-NEXT: [[RET:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 0>66; CHECK-NEXT: ret <2 x i1> [[RET]]67;68 %tmp0 = and <2 x i8> %x, <i8 3, i8 0>69 %ret = icmp ult <2 x i8> %tmp0, %x70 ret <2 x i1> %ret71}72 73define <2 x i1> @p2_vec_nonsplat_edgecase1(<2 x i8> %x) {74; CHECK-LABEL: @p2_vec_nonsplat_edgecase1(75; CHECK-NEXT: [[RET:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 -1>76; CHECK-NEXT: ret <2 x i1> [[RET]]77;78 %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>79 %ret = icmp ult <2 x i8> %tmp0, %x80 ret <2 x i1> %ret81}82 83define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {84; CHECK-LABEL: @p3_vec_splat_poison(85; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 poison, i8 3>86; CHECK-NEXT: ret <3 x i1> [[RET]]87;88 %tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>89 %ret = icmp ult <3 x i8> %tmp0, %x90 ret <3 x i1> %ret91}92 93define <3 x i1> @p3_vec_nonsplat_poison(<3 x i8> %x) {94; CHECK-LABEL: @p3_vec_nonsplat_poison(95; CHECK-NEXT: [[RET:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 7, i8 31, i8 poison>96; CHECK-NEXT: ret <3 x i1> [[RET]]97;98 %tmp0 = and <3 x i8> %x, <i8 7, i8 31, i8 poison>99 %ret = icmp ult <3 x i8> %tmp0, %x100 ret <3 x i1> %ret101}102 103; ============================================================================ ;104; Commutativity tests.105; ============================================================================ ;106 107declare i8 @gen8()108 109; The pattern is not commutative. instsimplify will already take care of it.110define i1 @c0() {111; CHECK-LABEL: @c0(112; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()113; CHECK-NEXT: ret i1 false114;115 %x = call i8 @gen8()116 %tmp0 = and i8 %x, 3117 %ret = icmp ult i8 %x, %tmp0 ; swapped order118 ret i1 %ret119}120 121; ============================================================================ ;122; Commutativity tests with variable123; ============================================================================ ;124 125define i1 @cv0(i8 %y) {126; CHECK-LABEL: @cv0(127; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()128; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]129; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X]], [[TMP0]]130; CHECK-NEXT: ret i1 [[RET]]131;132 %x = call i8 @gen8()133 %tmp0 = lshr i8 -1, %y134 %tmp1 = and i8 %x, %tmp0 ; swapped order135 %ret = icmp ult i8 %tmp1, %x136 ret i1 %ret137}138 139define i1 @cv1(i8 %y) {140; CHECK-LABEL: @cv1(141; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()142; CHECK-NEXT: ret i1 false143;144 %x = call i8 @gen8()145 %tmp0 = lshr i8 -1, %y146 %tmp1 = and i8 %tmp0, %x147 %ret = icmp ult i8 %x, %tmp1 ; swapped order148 ret i1 %ret149}150 151define i1 @cv2(i8 %y) {152; CHECK-LABEL: @cv2(153; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()154; CHECK-NEXT: ret i1 false155;156 %x = call i8 @gen8()157 %tmp0 = lshr i8 -1, %y158 %tmp1 = and i8 %x, %tmp0 ; swapped order159 %ret = icmp ult i8 %x, %tmp1 ; swapped order160 ret i1 %ret161}162 163; ============================================================================ ;164; One-use tests. We don't care about multi-uses here.165; ============================================================================ ;166 167declare void @use8(i8)168 169define i1 @oneuse0(i8 %x) {170; CHECK-LABEL: @oneuse0(171; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3172; CHECK-NEXT: call void @use8(i8 [[TMP0]])173; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X]], 3174; CHECK-NEXT: ret i1 [[RET]]175;176 %tmp0 = and i8 %x, 3177 call void @use8(i8 %tmp0)178 %ret = icmp ult i8 %tmp0, %x179 ret i1 %ret180}181 182; ============================================================================ ;183; Negative tests184; ============================================================================ ;185 186define i1 @n0(i8 %x) {187; CHECK-LABEL: @n0(188; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -5189; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[TMP1]], 0190; CHECK-NEXT: ret i1 [[RET]]191;192 %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.193 %ret = icmp ult i8 %tmp0, %x194 ret i1 %ret195}196 197define i1 @n1(i8 %x, i8 %y, i8 %notx) {198; CHECK-LABEL: @n1(199; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3200; CHECK-NEXT: [[RET:%.*]] = icmp ult i8 [[TMP0]], [[NOTX:%.*]]201; CHECK-NEXT: ret i1 [[RET]]202;203 %tmp0 = and i8 %x, 3204 %ret = icmp ult i8 %tmp0, %notx ; not %x205 ret i1 %ret206}207 208define <2 x i1> @n2(<2 x i8> %x) {209; CHECK-LABEL: @n2(210; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -4, i8 -17>211; CHECK-NEXT: [[RET:%.*]] = icmp ne <2 x i8> [[TMP1]], zeroinitializer212; CHECK-NEXT: ret <2 x i1> [[RET]]213;214 %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.215 %ret = icmp ult <2 x i8> %tmp0, %x216 ret <2 x i1> %ret217}218