299 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 & ~(-1 << y) == x8; Should be transformed into:9; x u<= ~(-1 << y)10; That is then later transformed into:11; (x >> y) == 012 13; ============================================================================ ;14; Basic positive tests15; ============================================================================ ;16 17define i1 @p0(i8 %x, i8 %y) {18; CHECK-LABEL: @p0(19; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[X:%.*]], [[Y:%.*]]20; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 021; CHECK-NEXT: ret i1 [[RET]]22;23 %t0 = shl i8 -1, %y24 %t1 = xor i8 %t0, -125 %t2 = and i8 %t1, %x26 %ret = icmp eq i8 %t2, %x27 ret i1 %ret28}29 30; ============================================================================ ;31; Vector tests32; ============================================================================ ;33 34define <2 x i1> @p1_vec(<2 x i8> %x, <2 x i8> %y) {35; CHECK-LABEL: @p1_vec(36; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i8> [[X:%.*]], [[Y:%.*]]37; CHECK-NEXT: [[RET:%.*]] = icmp eq <2 x i8> [[TMP1]], zeroinitializer38; CHECK-NEXT: ret <2 x i1> [[RET]]39;40 %t0 = shl <2 x i8> <i8 -1, i8 -1>, %y41 %t1 = xor <2 x i8> %t0, <i8 -1, i8 -1>42 %t2 = and <2 x i8> %t1, %x43 %ret = icmp eq <2 x i8> %t2, %x44 ret <2 x i1> %ret45}46 47define <3 x i1> @p2_vec_poison0(<3 x i8> %x, <3 x i8> %y) {48; CHECK-LABEL: @p2_vec_poison0(49; CHECK-NEXT: [[TMP1:%.*]] = lshr <3 x i8> [[X:%.*]], [[Y:%.*]]50; CHECK-NEXT: [[RET:%.*]] = icmp eq <3 x i8> [[TMP1]], zeroinitializer51; CHECK-NEXT: ret <3 x i1> [[RET]]52;53 %t0 = shl <3 x i8> <i8 -1, i8 poison, i8 -1>, %y54 %t1 = xor <3 x i8> %t0, <i8 -1, i8 -1, i8 -1>55 %t2 = and <3 x i8> %t1, %x56 %ret = icmp eq <3 x i8> %t2, %x57 ret <3 x i1> %ret58}59 60define <3 x i1> @p3_vec_poison0(<3 x i8> %x, <3 x i8> %y) {61; CHECK-LABEL: @p3_vec_poison0(62; CHECK-NEXT: [[TMP1:%.*]] = lshr <3 x i8> [[X:%.*]], [[Y:%.*]]63; CHECK-NEXT: [[RET:%.*]] = icmp eq <3 x i8> [[TMP1]], zeroinitializer64; CHECK-NEXT: ret <3 x i1> [[RET]]65;66 %t0 = shl <3 x i8> <i8 -1, i8 -1, i8 -1>, %y67 %t1 = xor <3 x i8> %t0, <i8 -1, i8 poison, i8 -1>68 %t2 = and <3 x i8> %t1, %x69 %ret = icmp eq <3 x i8> %t2, %x70 ret <3 x i1> %ret71}72 73define <3 x i1> @p4_vec_poison2(<3 x i8> %x, <3 x i8> %y) {74; CHECK-LABEL: @p4_vec_poison2(75; CHECK-NEXT: [[TMP1:%.*]] = lshr <3 x i8> [[X:%.*]], [[Y:%.*]]76; CHECK-NEXT: [[RET:%.*]] = icmp eq <3 x i8> [[TMP1]], zeroinitializer77; CHECK-NEXT: ret <3 x i1> [[RET]]78;79 %t0 = shl <3 x i8> <i8 -1, i8 poison, i8 -1>, %y80 %t1 = xor <3 x i8> %t0, <i8 -1, i8 poison, i8 -1>81 %t2 = and <3 x i8> %t1, %x82 %ret = icmp eq <3 x i8> %t2, %x83 ret <3 x i1> %ret84}85 86; ============================================================================ ;87; Commutativity tests.88; ============================================================================ ;89 90declare i8 @gen8()91 92define i1 @c0(i8 %y) {93; CHECK-LABEL: @c0(94; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()95; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[X]], [[Y:%.*]]96; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 097; CHECK-NEXT: ret i1 [[RET]]98;99 %t0 = shl i8 -1, %y100 %t1 = xor i8 %t0, -1101 %x = call i8 @gen8()102 %t2 = and i8 %x, %t1 ; swapped order103 %ret = icmp eq i8 %t2, %x104 ret i1 %ret105}106 107define i1 @c1(i8 %y) {108; CHECK-LABEL: @c1(109; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()110; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[X]], [[Y:%.*]]111; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0112; CHECK-NEXT: ret i1 [[RET]]113;114 %t0 = shl i8 -1, %y115 %t1 = xor i8 %t0, -1116 %x = call i8 @gen8()117 %t2 = and i8 %t1, %x118 %ret = icmp eq i8 %x, %t2 ; swapped order119 ret i1 %ret120}121 122define i1 @c2(i8 %y) {123; CHECK-LABEL: @c2(124; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()125; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[X]], [[Y:%.*]]126; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0127; CHECK-NEXT: ret i1 [[RET]]128;129 %t0 = shl i8 -1, %y130 %t1 = xor i8 %t0, -1131 %x = call i8 @gen8()132 %t2 = and i8 %x, %t1 ; swapped order133 %ret = icmp eq i8 %x, %t2 ; swapped order134 ret i1 %ret135}136 137; ============================================================================ ;138; One-use tests. We don't care about multi-uses here.139; ============================================================================ ;140 141declare void @use8(i8)142 143define i1 @oneuse0(i8 %x, i8 %y) {144; CHECK-LABEL: @oneuse0(145; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]146; CHECK-NEXT: call void @use8(i8 [[T0]])147; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], [[T0]]148; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0149; CHECK-NEXT: ret i1 [[RET]]150;151 %t0 = shl i8 -1, %y152 call void @use8(i8 %t0)153 %t1 = xor i8 %t0, -1154 %t2 = and i8 %t1, %x155 %ret = icmp eq i8 %t2, %x156 ret i1 %ret157}158 159define i1 @oneuse1(i8 %x, i8 %y) {160; CHECK-LABEL: @oneuse1(161; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]162; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1163; CHECK-NEXT: call void @use8(i8 [[T1]])164; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], [[T0]]165; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0166; CHECK-NEXT: ret i1 [[RET]]167;168 %t0 = shl i8 -1, %y169 %t1 = xor i8 %t0, -1170 call void @use8(i8 %t1)171 %t2 = and i8 %t1, %x172 %ret = icmp eq i8 %t2, %x173 ret i1 %ret174}175 176define i1 @oneuse2(i8 %x, i8 %y) {177; CHECK-LABEL: @oneuse2(178; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]179; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1180; CHECK-NEXT: [[T2:%.*]] = and i8 [[X:%.*]], [[T1]]181; CHECK-NEXT: call void @use8(i8 [[T2]])182; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[T1]]183; CHECK-NEXT: ret i1 [[RET]]184;185 %t0 = shl i8 -1, %y186 %t1 = xor i8 %t0, -1187 %t2 = and i8 %t1, %x188 call void @use8(i8 %t2)189 %ret = icmp eq i8 %t2, %x190 ret i1 %ret191}192 193define i1 @oneuse3(i8 %x, i8 %y) {194; CHECK-LABEL: @oneuse3(195; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]196; CHECK-NEXT: call void @use8(i8 [[T0]])197; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1198; CHECK-NEXT: call void @use8(i8 [[T1]])199; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], [[T0]]200; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0201; CHECK-NEXT: ret i1 [[RET]]202;203 %t0 = shl i8 -1, %y204 call void @use8(i8 %t0)205 %t1 = xor i8 %t0, -1206 call void @use8(i8 %t1)207 %t2 = and i8 %t1, %x208 %ret = icmp eq i8 %t2, %x209 ret i1 %ret210}211 212define i1 @oneuse4(i8 %x, i8 %y) {213; CHECK-LABEL: @oneuse4(214; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]215; CHECK-NEXT: call void @use8(i8 [[T0]])216; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1217; CHECK-NEXT: [[T2:%.*]] = and i8 [[X:%.*]], [[T1]]218; CHECK-NEXT: call void @use8(i8 [[T2]])219; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[T1]]220; CHECK-NEXT: ret i1 [[RET]]221;222 %t0 = shl i8 -1, %y223 call void @use8(i8 %t0)224 %t1 = xor i8 %t0, -1225 %t2 = and i8 %t1, %x226 call void @use8(i8 %t2)227 %ret = icmp eq i8 %t2, %x228 ret i1 %ret229}230 231define i1 @oneuse5(i8 %x, i8 %y) {232; CHECK-LABEL: @oneuse5(233; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]234; CHECK-NEXT: call void @use8(i8 [[T0]])235; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1236; CHECK-NEXT: call void @use8(i8 [[T1]])237; CHECK-NEXT: [[T2:%.*]] = and i8 [[X:%.*]], [[T1]]238; CHECK-NEXT: call void @use8(i8 [[T2]])239; CHECK-NEXT: [[RET:%.*]] = icmp ule i8 [[X]], [[T1]]240; CHECK-NEXT: ret i1 [[RET]]241;242 %t0 = shl i8 -1, %y243 call void @use8(i8 %t0)244 %t1 = xor i8 %t0, -1245 call void @use8(i8 %t1)246 %t2 = and i8 %t1, %x247 call void @use8(i8 %t2)248 %ret = icmp eq i8 %t2, %x249 ret i1 %ret250}251 252; ============================================================================ ;253; Negative tests254; ============================================================================ ;255 256define i1 @n0(i8 %x, i8 %y, i8 %notx) {257; CHECK-LABEL: @n0(258; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]259; CHECK-NEXT: [[T1:%.*]] = xor i8 [[T0]], -1260; CHECK-NEXT: [[T2:%.*]] = and i8 [[X:%.*]], [[T1]]261; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[T2]], [[NOTX:%.*]]262; CHECK-NEXT: ret i1 [[RET]]263;264 %t0 = shl i8 -1, %y265 %t1 = xor i8 %t0, -1266 %t2 = and i8 %t1, %x267 %ret = icmp eq i8 %t2, %notx ; not %x268 ret i1 %ret269}270 271define i1 @n1(i8 %x, i8 %y) {272; CHECK-LABEL: @n1(273; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]274; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], [[T0]]275; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP1]], 0276; CHECK-NEXT: ret i1 [[RET]]277;278 %t0 = shl i8 1, %y ; not -1279 %t1 = xor i8 %t0, -1280 %t2 = and i8 %t1, %x281 %ret = icmp eq i8 %t2, %x282 ret i1 %ret283}284 285define i1 @n2(i8 %x, i8 %y) {286; CHECK-LABEL: @n2(287; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]288; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[T0]], -2289; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X:%.*]], [[TMP1]]290; CHECK-NEXT: [[RET:%.*]] = icmp eq i8 [[TMP2]], 0291; CHECK-NEXT: ret i1 [[RET]]292;293 %t0 = shl i8 -1, %y294 %t1 = xor i8 %t0, 1 ; not -1295 %t2 = and i8 %t1, %x296 %ret = icmp eq i8 %t2, %x297 ret i1 %ret298}299