From 701923a60fdb7b321cfd8e6ff5d5d9195daaec0e Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 31 Oct 2021 11:46:18 -0400 Subject: [PATCH] [InstCombine] add tests for bitwise logic folds; NFC The extra uses are needed to prevent intermediate folds. Without that, there would be no coverage currently. The vector tests show an artificial limitation in the code. --- llvm/test/Transforms/InstCombine/and-or.ll | 138 +++++++++++++++++++++++++++++ llvm/test/Transforms/InstCombine/or.ll | 6 +- 2 files changed, 141 insertions(+), 3 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/and-or.ll b/llvm/test/Transforms/InstCombine/and-or.ll index 82d60ce..7996fd3 100644 --- a/llvm/test/Transforms/InstCombine/and-or.ll +++ b/llvm/test/Transforms/InstCombine/and-or.ll @@ -1,6 +1,9 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -instcombine -S | FileCheck %s +declare void @use(i8) +declare void @use_vec(<2 x i8>) + ; ((b | a) & C1) | (b & C2) -> (a & C1) | b iff C1 == ~C2 define i32 @or_and_not_constant_commute0(i32 %a, i32 %b) { @@ -68,6 +71,141 @@ define <2 x i7> @or_and_not_constant_commute0_splat(<2 x i7> %a, <2 x i7> %b) { ret <2 x i7> %t3 } +; ((x | N) & C1) | (x & C2) --> (x | N) & (C1 | C2) +; iff (C1 & C2) == 0 and (N & ~C1) == 0 + +define i8 @or_and_or_commute0(i8 %x) { +; CHECK-LABEL: @or_and_or_commute0( +; CHECK-NEXT: [[XN:%.*]] = or i8 [[X:%.*]], 16 +; CHECK-NEXT: call void @use(i8 [[XN]]) +; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], 59 +; CHECK-NEXT: call void @use(i8 [[X1]]) +; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64 +; CHECK-NEXT: call void @use(i8 [[X2]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], 123 +; CHECK-NEXT: ret i8 [[R]] +; + %xn = or i8 %x, 16 ; 0001_0000 + call void @use(i8 %xn) + %x1 = and i8 %xn, 59 ; 0011_1011 + call void @use(i8 %x1) + %x2 = and i8 %x, 64 ; 0100_0000 + call void @use(i8 %x2) + %r = or i8 %x1, %x2 + ret i8 %r +} + +define i8 @or_and_or_commute1(i8 %x) { +; CHECK-LABEL: @or_and_or_commute1( +; CHECK-NEXT: [[XN:%.*]] = or i8 [[X:%.*]], 16 +; CHECK-NEXT: call void @use(i8 [[XN]]) +; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], 59 +; CHECK-NEXT: call void @use(i8 [[X1]]) +; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64 +; CHECK-NEXT: call void @use(i8 [[X2]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], 123 +; CHECK-NEXT: ret i8 [[R]] +; + %xn = or i8 %x, 16 ; 0001_0000 + call void @use(i8 %xn) + %x1 = and i8 %xn, 59 ; 0011_1011 + call void @use(i8 %x1) + %x2 = and i8 %x, 64 ; 0100_0000 + call void @use(i8 %x2) + %r = or i8 %x2, %x1 + ret i8 %r +} + +define <2 x i8> @or_and_or_commute1_splat(<2 x i8> %x) { +; CHECK-LABEL: @or_and_or_commute1_splat( +; CHECK-NEXT: [[XN:%.*]] = or <2 x i8> [[X:%.*]], +; CHECK-NEXT: call void @use_vec(<2 x i8> [[XN]]) +; CHECK-NEXT: [[X1:%.*]] = and <2 x i8> [[XN]], +; CHECK-NEXT: call void @use_vec(<2 x i8> [[X1]]) +; CHECK-NEXT: [[X2:%.*]] = and <2 x i8> [[X]], +; CHECK-NEXT: call void @use_vec(<2 x i8> [[X2]]) +; CHECK-NEXT: [[R:%.*]] = or <2 x i8> [[X2]], [[X1]] +; CHECK-NEXT: ret <2 x i8> [[R]] +; + %xn = or <2 x i8> %x, + call void @use_vec(<2 x i8> %xn) + %x1 = and <2 x i8> %xn, + call void @use_vec(<2 x i8> %x1) + %x2 = and <2 x i8> %x, + call void @use_vec(<2 x i8> %x2) + %r = or <2 x i8> %x2, %x1 + ret <2 x i8> %r +} + +define i8 @or_and_or_commute2(i8 %x, i8 %y) { +; CHECK-LABEL: @or_and_or_commute2( +; CHECK-NEXT: [[N:%.*]] = lshr i8 [[Y:%.*]], 6 +; CHECK-NEXT: [[XN:%.*]] = or i8 [[N]], [[X:%.*]] +; CHECK-NEXT: call void @use(i8 [[XN]]) +; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], -69 +; CHECK-NEXT: call void @use(i8 [[X1]]) +; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64 +; CHECK-NEXT: call void @use(i8 [[X2]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], -5 +; CHECK-NEXT: ret i8 [[R]] +; + %n = lshr i8 %y, 6 + %xn = or i8 %n, %x + call void @use(i8 %xn) + %x1 = and i8 %xn, 187 + call void @use(i8 %x1) + %x2 = and i8 %x, 64 + call void @use(i8 %x2) + %r = or i8 %x1, %x2 + ret i8 %r +} + +define <2 x i8> @or_and_or_commute2_splat(<2 x i8> %x, <2 x i8> %y) { +; CHECK-LABEL: @or_and_or_commute2_splat( +; CHECK-NEXT: [[N:%.*]] = lshr <2 x i8> [[Y:%.*]], +; CHECK-NEXT: [[XN:%.*]] = or <2 x i8> [[N]], [[X:%.*]] +; CHECK-NEXT: call void @use_vec(<2 x i8> [[XN]]) +; CHECK-NEXT: [[X1:%.*]] = and <2 x i8> [[XN]], +; CHECK-NEXT: call void @use_vec(<2 x i8> [[X1]]) +; CHECK-NEXT: [[X2:%.*]] = and <2 x i8> [[X]], +; CHECK-NEXT: call void @use_vec(<2 x i8> [[X2]]) +; CHECK-NEXT: [[R:%.*]] = or <2 x i8> [[X1]], [[X2]] +; CHECK-NEXT: ret <2 x i8> [[R]] +; + %n = lshr <2 x i8> %y, + %xn = or <2 x i8> %n, %x + call void @use_vec(<2 x i8> %xn) + %x1 = and <2 x i8> %xn, + call void @use_vec(<2 x i8> %x1) + %x2 = and <2 x i8> %x, + call void @use_vec(<2 x i8> %x2) + %r = or <2 x i8> %x1, %x2 + ret <2 x i8> %r +} + +define i8 @or_and_or_commute3(i8 %x, i8 %y) { +; CHECK-LABEL: @or_and_or_commute3( +; CHECK-NEXT: [[N:%.*]] = lshr i8 [[Y:%.*]], 6 +; CHECK-NEXT: [[XN:%.*]] = or i8 [[N]], [[X:%.*]] +; CHECK-NEXT: call void @use(i8 [[XN]]) +; CHECK-NEXT: [[X1:%.*]] = and i8 [[XN]], -69 +; CHECK-NEXT: call void @use(i8 [[X1]]) +; CHECK-NEXT: [[X2:%.*]] = and i8 [[X]], 64 +; CHECK-NEXT: call void @use(i8 [[X2]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[XN]], -5 +; CHECK-NEXT: ret i8 [[R]] +; + %n = lshr i8 %y, 6 + %xn = or i8 %n, %x + call void @use(i8 %xn) + %x1 = and i8 %xn, 187 + call void @use(i8 %x1) + %x2 = and i8 %x, 64 + call void @use(i8 %x2) + %r = or i8 %x2, %x1 + ret i8 %r +} + ; Check variants of: ; and ({x}or X, Y), C --> {x}or X, (and Y, C) ; ...in the following 5 tests. diff --git a/llvm/test/Transforms/InstCombine/or.ll b/llvm/test/Transforms/InstCombine/or.ll index fcc6a0f..ef76fb0 100644 --- a/llvm/test/Transforms/InstCombine/or.ll +++ b/llvm/test/Transforms/InstCombine/or.ll @@ -341,9 +341,9 @@ define i32 @test30(i32 %A) { ; CHECK-NEXT: [[E:%.*]] = or i32 [[D]], 32962 ; CHECK-NEXT: ret i32 [[E]] ; - %B = or i32 %A, 32962 - %C = and i32 %A, -65536 - %D = and i32 %B, 40186 + %B = or i32 %A, 32962 ; 0b1000_0000_1100_0010 + %C = and i32 %A, -65536 ; 0xffff0000 + %D = and i32 %B, 40186 ; 0b1001_1100_1111_1010 %E = or i32 %D, %C ret i32 %E } -- 2.7.4