256 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 s<= x & C8; Should be transformed into:9; x s<= C10; Iff: isPowerOf2(C + 1)11; C must not be -1, but may be 0.12 13; NOTE: this pattern is not commutative!14 15declare i8 @gen8()16declare <2 x i8> @gen2x8()17declare <3 x i8> @gen3x8()18 19; ============================================================================ ;20; Basic positive tests21; ============================================================================ ;22 23define i1 @p0() {24; CHECK-LABEL: @p0(25; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()26; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], 427; CHECK-NEXT: ret i1 [[RET]]28;29 %x = call i8 @gen8()30 %tmp0 = and i8 %x, 331 %ret = icmp sle i8 %x, %tmp032 ret i1 %ret33}34 35; ============================================================================ ;36; Vector tests37; ============================================================================ ;38 39define <2 x i1> @p1_vec_splat() {40; CHECK-LABEL: @p1_vec_splat(41; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()42; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[X]], splat (i8 4)43; CHECK-NEXT: ret <2 x i1> [[RET]]44;45 %x = call <2 x i8> @gen2x8()46 %tmp0 = and <2 x i8> %x, <i8 3, i8 3>47 %ret = icmp sle <2 x i8> %x, %tmp048 ret <2 x i1> %ret49}50 51define <2 x i1> @p2_vec_nonsplat() {52; CHECK-LABEL: @p2_vec_nonsplat(53; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()54; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[X]], <i8 4, i8 16>55; CHECK-NEXT: ret <2 x i1> [[RET]]56;57 %x = call <2 x i8> @gen2x8()58 %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.59 %ret = icmp sle <2 x i8> %x, %tmp060 ret <2 x i1> %ret61}62 63define <2 x i1> @p2_vec_nonsplat_edgecase() {64; CHECK-LABEL: @p2_vec_nonsplat_edgecase(65; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()66; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[X]], <i8 4, i8 1>67; CHECK-NEXT: ret <2 x i1> [[RET]]68;69 %x = call <2 x i8> @gen2x8()70 %tmp0 = and <2 x i8> %x, <i8 3, i8 0>71 %ret = icmp sle <2 x i8> %x, %tmp072 ret <2 x i1> %ret73}74 75define <3 x i1> @p3_vec_splat_poison() {76; CHECK-LABEL: @p3_vec_splat_poison(77; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()78; CHECK-NEXT: [[RET:%.*]] = icmp slt <3 x i8> [[X]], splat (i8 4)79; CHECK-NEXT: ret <3 x i1> [[RET]]80;81 %x = call <3 x i8> @gen3x8()82 %tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>83 %ret = icmp sle <3 x i8> %x, %tmp084 ret <3 x i1> %ret85}86 87; ============================================================================ ;88; One-use tests. We don't care about multi-uses here.89; ============================================================================ ;90 91declare void @use8(i8)92 93define i1 @oneuse0() {94; CHECK-LABEL: @oneuse0(95; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()96; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 397; CHECK-NEXT: call void @use8(i8 [[TMP0]])98; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], 499; CHECK-NEXT: ret i1 [[RET]]100;101 %x = call i8 @gen8()102 %tmp0 = and i8 %x, 3103 call void @use8(i8 %tmp0)104 %ret = icmp sle i8 %x, %tmp0105 ret i1 %ret106}107 108; ============================================================================ ;109; Negative tests110; ============================================================================ ;111 112; Commutativity tests.113 114define i1 @c0(i8 %x) {115; CHECK-LABEL: @c0(116; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[X:%.*]], -1117; CHECK-NEXT: ret i1 [[RET]]118;119 %tmp0 = and i8 %x, 3120 %ret = icmp sle i8 %tmp0, %x ; swapped order121 ret i1 %ret122}123 124; ============================================================================ ;125; Rest of negative tests126; ============================================================================ ;127 128define i1 @n0() {129; CHECK-LABEL: @n0(130; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()131; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 4132; CHECK-NEXT: [[RET:%.*]] = icmp sle i8 [[X]], [[TMP0]]133; CHECK-NEXT: ret i1 [[RET]]134;135 %x = call i8 @gen8()136 %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.137 %ret = icmp sle i8 %x, %tmp0138 ret i1 %ret139}140 141define i1 @n1(i8 %y, i8 %notx) {142; CHECK-LABEL: @n1(143; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()144; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 3145; CHECK-NEXT: [[RET:%.*]] = icmp sle i8 [[TMP0]], [[NOTX:%.*]]146; CHECK-NEXT: ret i1 [[RET]]147;148 %x = call i8 @gen8()149 %tmp0 = and i8 %x, 3150 %ret = icmp sle i8 %tmp0, %notx ; not %x151 ret i1 %ret152}153 154define <2 x i1> @n2() {155; CHECK-LABEL: @n2(156; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()157; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 16>158; CHECK-NEXT: [[RET:%.*]] = icmp sle <2 x i8> [[X]], [[TMP0]]159; CHECK-NEXT: ret <2 x i1> [[RET]]160;161 %x = call <2 x i8> @gen2x8()162 %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.163 %ret = icmp sle <2 x i8> %x, %tmp0164 ret <2 x i1> %ret165}166 167; ============================================================================ ;168; Potential miscompiles.169; ============================================================================ ;170 171define i1 @pv(i8 %y) {172; CHECK-LABEL: @pv(173; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()174; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]175; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]176; CHECK-NEXT: [[RET:%.*]] = icmp sle i8 [[X]], [[TMP1]]177; CHECK-NEXT: ret i1 [[RET]]178;179 %x = call i8 @gen8()180 %tmp0 = lshr i8 -1, %y181 %tmp1 = and i8 %tmp0, %x182 %ret = icmp sle i8 %x, %tmp1183 ret i1 %ret184}185 186define <2 x i1> @n3_vec() {187; CHECK-LABEL: @n3_vec(188; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()189; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 -1>190; CHECK-NEXT: [[RET:%.*]] = icmp sle <2 x i8> [[X]], [[TMP0]]191; CHECK-NEXT: ret <2 x i1> [[RET]]192;193 %x = call <2 x i8> @gen2x8()194 %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>195 %ret = icmp sle <2 x i8> %x, %tmp0196 ret <2 x i1> %ret197}198 199define <3 x i1> @n4_vec() {200; CHECK-LABEL: @n4_vec(201; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()202; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X]], <i8 3, i8 poison, i8 -1>203; CHECK-NEXT: [[RET:%.*]] = icmp sle <3 x i8> [[X]], [[TMP0]]204; CHECK-NEXT: ret <3 x i1> [[RET]]205;206 %x = call <3 x i8> @gen3x8()207 %tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 -1>208 %ret = icmp sle <3 x i8> %x, %tmp0209 ret <3 x i1> %ret210}211 212; Commutativity tests with variable213 214define i1 @cv0_GOOD(i8 %y) {215; CHECK-LABEL: @cv0_GOOD(216; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()217; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]218; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]219; CHECK-NEXT: [[RET:%.*]] = icmp sle i8 [[X]], [[TMP1]]220; CHECK-NEXT: ret i1 [[RET]]221;222 %x = call i8 @gen8()223 %tmp0 = lshr i8 -1, %y224 %tmp1 = and i8 %tmp0, %x ; swapped order225 %ret = icmp sle i8 %x, %tmp1226 ret i1 %ret227}228 229define i1 @cv1(i8 %y) {230; CHECK-LABEL: @cv1(231; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()232; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]233; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]234; CHECK-NEXT: [[RET:%.*]] = icmp sle i8 [[TMP1]], [[X]]235; CHECK-NEXT: ret i1 [[RET]]236;237 %x = call i8 @gen8()238 %tmp0 = lshr i8 -1, %y239 %tmp1 = and i8 %x, %tmp0240 %ret = icmp sle i8 %tmp1, %x ; swapped order241 ret i1 %ret242}243 244define i1 @cv2(i8 %x, i8 %y) {245; CHECK-LABEL: @cv2(246; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]247; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X:%.*]]248; CHECK-NEXT: [[RET:%.*]] = icmp sle i8 [[TMP1]], [[X]]249; CHECK-NEXT: ret i1 [[RET]]250;251 %tmp0 = lshr i8 -1, %y252 %tmp1 = and i8 %tmp0, %x ; swapped order253 %ret = icmp sle i8 %tmp1, %x ; swapped order254 ret i1 %ret255}256