283 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) + (-1)) != x8; Should be transformed into:9; x u> ((1 << y) + (-1))10; That is then later transformed into:11; (x >> y) != 012 13; This pattern is uncanonical, but we can not canonicalize it due to extra uses.14 15declare void @use8(i8)16declare void @use2i8(<2 x i8>)17declare void @use3i8(<3 x i8>)18 19; ============================================================================ ;20; Basic positive tests21; ============================================================================ ;22 23define i1 @p0(i8 %x, i8 %y) {24; CHECK-LABEL: @p0(25; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]26; CHECK-NEXT: call void @use8(i8 [[T0]])27; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr i8 [[X:%.*]], [[Y]]28; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X_HIGHBITS]], 029; CHECK-NEXT: ret i1 [[RET]]30;31 %t0 = shl i8 1, %y32 call void @use8(i8 %t0)33 %t1 = add i8 %t0, -134 %t2 = and i8 %t1, %x35 %ret = icmp ne i8 %t2, %x36 ret i1 %ret37}38 39; ============================================================================ ;40; Vector tests41; ============================================================================ ;42 43define <2 x i1> @p1_vec(<2 x i8> %x, <2 x i8> %y) {44; CHECK-LABEL: @p1_vec(45; CHECK-NEXT: [[T0:%.*]] = shl nuw <2 x i8> splat (i8 1), [[Y:%.*]]46; CHECK-NEXT: call void @use2i8(<2 x i8> [[T0]])47; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr <2 x i8> [[X:%.*]], [[Y]]48; CHECK-NEXT: [[RET:%.*]] = icmp ne <2 x i8> [[X_HIGHBITS]], zeroinitializer49; CHECK-NEXT: ret <2 x i1> [[RET]]50;51 %t0 = shl <2 x i8> <i8 1, i8 1>, %y52 call void @use2i8(<2 x i8> %t0)53 %t1 = add <2 x i8> %t0, <i8 -1, i8 -1>54 %t2 = and <2 x i8> %t1, %x55 %ret = icmp ne <2 x i8> %t2, %x56 ret <2 x i1> %ret57}58 59define <3 x i1> @p2_vec_poison0(<3 x i8> %x, <3 x i8> %y) {60; CHECK-LABEL: @p2_vec_poison0(61; CHECK-NEXT: [[T0:%.*]] = shl nuw <3 x i8> <i8 1, i8 poison, i8 1>, [[Y:%.*]]62; CHECK-NEXT: call void @use3i8(<3 x i8> [[T0]])63; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr <3 x i8> [[X:%.*]], [[Y]]64; CHECK-NEXT: [[RET:%.*]] = icmp ne <3 x i8> [[X_HIGHBITS]], zeroinitializer65; CHECK-NEXT: ret <3 x i1> [[RET]]66;67 %t0 = shl <3 x i8> <i8 1, i8 poison, i8 1>, %y68 call void @use3i8(<3 x i8> %t0)69 %t1 = add <3 x i8> %t0, <i8 -1, i8 -1, i8 -1>70 %t2 = and <3 x i8> %t1, %x71 %ret = icmp ne <3 x i8> %t2, %x72 ret <3 x i1> %ret73}74 75define <3 x i1> @p3_vec_poison0(<3 x i8> %x, <3 x i8> %y) {76; CHECK-LABEL: @p3_vec_poison0(77; CHECK-NEXT: [[T0:%.*]] = shl nuw <3 x i8> splat (i8 1), [[Y:%.*]]78; CHECK-NEXT: call void @use3i8(<3 x i8> [[T0]])79; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr <3 x i8> [[X:%.*]], [[Y]]80; CHECK-NEXT: [[RET:%.*]] = icmp ne <3 x i8> [[X_HIGHBITS]], zeroinitializer81; CHECK-NEXT: ret <3 x i1> [[RET]]82;83 %t0 = shl <3 x i8> <i8 1, i8 1, i8 1>, %y84 call void @use3i8(<3 x i8> %t0)85 %t1 = add <3 x i8> %t0, <i8 -1, i8 poison, i8 -1>86 %t2 = and <3 x i8> %t1, %x87 %ret = icmp ne <3 x i8> %t2, %x88 ret <3 x i1> %ret89}90 91define <3 x i1> @p4_vec_poison2(<3 x i8> %x, <3 x i8> %y) {92; CHECK-LABEL: @p4_vec_poison2(93; CHECK-NEXT: [[T0:%.*]] = shl nuw <3 x i8> <i8 1, i8 poison, i8 1>, [[Y:%.*]]94; CHECK-NEXT: call void @use3i8(<3 x i8> [[T0]])95; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr <3 x i8> [[X:%.*]], [[Y]]96; CHECK-NEXT: [[RET:%.*]] = icmp ne <3 x i8> [[X_HIGHBITS]], zeroinitializer97; CHECK-NEXT: ret <3 x i1> [[RET]]98;99 %t0 = shl <3 x i8> <i8 1, i8 poison, i8 1>, %y100 call void @use3i8(<3 x i8> %t0)101 %t1 = add <3 x i8> %t0, <i8 -1, i8 poison, i8 -1>102 %t2 = and <3 x i8> %t1, %x103 %ret = icmp ne <3 x i8> %t2, %x104 ret <3 x i1> %ret105}106 107; ============================================================================ ;108; Commutativity tests.109; ============================================================================ ;110 111declare i8 @gen8()112 113define i1 @c0(i8 %y) {114; CHECK-LABEL: @c0(115; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]116; CHECK-NEXT: call void @use8(i8 [[T0]])117; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()118; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr i8 [[X]], [[Y]]119; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X_HIGHBITS]], 0120; CHECK-NEXT: ret i1 [[RET]]121;122 %t0 = shl i8 1, %y123 call void @use8(i8 %t0)124 %t1 = add i8 %t0, -1125 %x = call i8 @gen8()126 %t2 = and i8 %x, %t1 ; swapped order127 %ret = icmp ne i8 %t2, %x128 ret i1 %ret129}130 131define i1 @c1(i8 %y) {132; CHECK-LABEL: @c1(133; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]134; CHECK-NEXT: call void @use8(i8 [[T0]])135; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()136; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr i8 [[X]], [[Y]]137; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X_HIGHBITS]], 0138; CHECK-NEXT: ret i1 [[RET]]139;140 %t0 = shl i8 1, %y141 call void @use8(i8 %t0)142 %t1 = add i8 %t0, -1143 %x = call i8 @gen8()144 %t2 = and i8 %t1, %x145 %ret = icmp ne i8 %x, %t2 ; swapped order146 ret i1 %ret147}148 149define i1 @c2(i8 %y) {150; CHECK-LABEL: @c2(151; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]152; CHECK-NEXT: call void @use8(i8 [[T0]])153; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()154; CHECK-NEXT: [[X_HIGHBITS:%.*]] = lshr i8 [[X]], [[Y]]155; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[X_HIGHBITS]], 0156; CHECK-NEXT: ret i1 [[RET]]157;158 %t0 = shl i8 1, %y159 call void @use8(i8 %t0)160 %t1 = add i8 %t0, -1161 %x = call i8 @gen8()162 %t2 = and i8 %x, %t1 ; swapped order163 %ret = icmp ne i8 %x, %t2 ; swapped order164 ret i1 %ret165}166 167; ============================================================================ ;168; One-use tests. We don't care about multi-uses here.169; ============================================================================ ;170 171define i1 @oneuse0(i8 %x, i8 %y) {172; CHECK-LABEL: @oneuse0(173; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]174; CHECK-NEXT: call void @use8(i8 [[T0]])175; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0]], -1176; CHECK-NEXT: call void @use8(i8 [[T1]])177; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X:%.*]], [[T1]]178; CHECK-NEXT: ret i1 [[RET]]179;180 %t0 = shl i8 1, %y181 call void @use8(i8 %t0) ; needed anyway182 %t1 = add i8 %t0, -1183 call void @use8(i8 %t1)184 %t2 = and i8 %t1, %x185 %ret = icmp ne i8 %t2, %x186 ret i1 %ret187}188 189define i1 @oneuse1(i8 %x, i8 %y) {190; CHECK-LABEL: @oneuse1(191; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]192; CHECK-NEXT: call void @use8(i8 [[T0]])193; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0]], -1194; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]195; CHECK-NEXT: call void @use8(i8 [[T2]])196; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X]], [[T1]]197; CHECK-NEXT: ret i1 [[RET]]198;199 %t0 = shl i8 1, %y200 call void @use8(i8 %t0) ; needed anyway201 %t1 = add i8 %t0, -1202 %t2 = and i8 %t1, %x203 call void @use8(i8 %t2)204 %ret = icmp ne i8 %t2, %x205 ret i1 %ret206}207 208define i1 @oneuse2(i8 %x, i8 %y) {209; CHECK-LABEL: @oneuse2(210; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]211; CHECK-NEXT: call void @use8(i8 [[T0]])212; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0]], -1213; CHECK-NEXT: call void @use8(i8 [[T1]])214; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]215; CHECK-NEXT: call void @use8(i8 [[T2]])216; CHECK-NEXT: [[RET:%.*]] = icmp ugt i8 [[X]], [[T1]]217; CHECK-NEXT: ret i1 [[RET]]218;219 %t0 = shl i8 1, %y220 call void @use8(i8 %t0)221 %t1 = add i8 %t0, -1222 call void @use8(i8 %t1)223 %t2 = and i8 %t1, %x224 call void @use8(i8 %t2)225 %ret = icmp ne i8 %t2, %x226 ret i1 %ret227}228 229; ============================================================================ ;230; Negative tests231; ============================================================================ ;232 233define i1 @n0(i8 %x, i8 %y, i8 %notx) {234; CHECK-LABEL: @n0(235; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]236; CHECK-NEXT: call void @use8(i8 [[T0]])237; CHECK-NEXT: [[T1:%.*]] = add i8 [[T0]], -1238; CHECK-NEXT: [[T2:%.*]] = and i8 [[T1]], [[X:%.*]]239; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[T2]], [[NOTX:%.*]]240; CHECK-NEXT: ret i1 [[RET]]241;242 %t0 = shl i8 1, %y243 call void @use8(i8 %t0)244 %t1 = add i8 %t0, -1245 %t2 = and i8 %t1, %x246 %ret = icmp ne i8 %t2, %notx ; not %x247 ret i1 %ret248}249 250define i1 @n1(i8 %x, i8 %y) {251; CHECK-LABEL: @n1(252; CHECK-NEXT: [[T0:%.*]] = shl nsw i8 -1, [[Y:%.*]]253; CHECK-NEXT: call void @use8(i8 [[T0]])254; CHECK-NEXT: [[TMP1:%.*]] = sub i8 0, [[T0]]255; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X:%.*]], [[TMP1]]256; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[TMP2]], 0257; CHECK-NEXT: ret i1 [[RET]]258;259 %t0 = shl i8 -1, %y ; not 1260 call void @use8(i8 %t0)261 %t1 = add i8 %t0, -1262 %t2 = and i8 %t1, %x263 %ret = icmp ne i8 %t2, %x264 ret i1 %ret265}266 267define i1 @n2(i8 %x, i8 %y) {268; CHECK-LABEL: @n2(269; CHECK-NEXT: [[T0:%.*]] = shl nuw i8 1, [[Y:%.*]]270; CHECK-NEXT: call void @use8(i8 [[T0]])271; CHECK-NEXT: [[TMP1:%.*]] = sub nuw i8 -2, [[T0]]272; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X:%.*]], [[TMP1]]273; CHECK-NEXT: [[RET:%.*]] = icmp ne i8 [[TMP2]], 0274; CHECK-NEXT: ret i1 [[RET]]275;276 %t0 = shl i8 1, %y277 call void @use8(i8 %t0)278 %t1 = add i8 %t0, 1 ; not -1279 %t2 = and i8 %t1, %x280 %ret = icmp ne i8 %t2, %x281 ret i1 %ret282}283