|
| 1 | +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py |
| 2 | +; RUN: opt < %s -passes=instcombine -S | FileCheck %s |
| 3 | + |
| 4 | +; Tests for GitHub issue #97044 - Boolean instruction canonicalization |
| 5 | +; All expressions should optimise to the same canonical form: ~((y | z) ^ x) |
| 6 | + |
| 7 | +define i32 @test0_4way_or(i32 %x, i32 %y, i32 %z) { |
| 8 | +; CHECK-LABEL: @test0_4way_or( |
| 9 | +; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[Y:%.*]], [[Z:%.*]] |
| 10 | +; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP1]], [[X:%.*]] |
| 11 | +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2]], -1 |
| 12 | +; CHECK-NEXT: ret i32 [[TMP3]] |
| 13 | +; |
| 14 | + %not = xor i32 %z, -1 |
| 15 | + %and = and i32 %y, %not |
| 16 | + %and1 = and i32 %and, %x |
| 17 | + %not2 = xor i32 %y, -1 |
| 18 | + %and3 = and i32 %x, %not2 |
| 19 | + %and4 = and i32 %and3, %z |
| 20 | + %or = or i32 %and1, %and4 |
| 21 | + %not5 = xor i32 %x, -1 |
| 22 | + %not6 = xor i32 %y, -1 |
| 23 | + %and7 = and i32 %not5, %not6 |
| 24 | + %not8 = xor i32 %z, -1 |
| 25 | + %and9 = and i32 %and7, %not8 |
| 26 | + %or10 = or i32 %or, %and9 |
| 27 | + %and11 = and i32 %x, %y |
| 28 | + %and12 = and i32 %and11, %z |
| 29 | + %or13 = or i32 %or10, %and12 |
| 30 | + ret i32 %or13 |
| 31 | +} |
| 32 | + |
| 33 | +define i32 @test1_xor_pattern(i32 %x, i32 %y, i32 %z) { |
| 34 | +; CHECK-LABEL: @test1_xor_pattern( |
| 35 | +; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP1:%.*]], [[X:%.*]] |
| 36 | +; CHECK-NEXT: [[AND4_DEMORGAN:%.*]] = or i32 [[TMP2]], [[Z:%.*]] |
| 37 | +; CHECK-NEXT: [[AND8:%.*]] = and i32 [[Z]], [[TMP1]] |
| 38 | +; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[AND4_DEMORGAN]], -1 |
| 39 | +; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[AND8]], [[TMP4]] |
| 40 | +; CHECK-NEXT: ret i32 [[TMP3]] |
| 41 | +; |
| 42 | + %not = xor i32 %z, -1 |
| 43 | + %and = and i32 %x, %y |
| 44 | + %not1 = xor i32 %x, -1 |
| 45 | + %not2 = xor i32 %y, -1 |
| 46 | + %and3 = and i32 %not1, %not2 |
| 47 | + %or = or i32 %and, %and3 |
| 48 | + %and4 = and i32 %not, %or |
| 49 | + %and5 = and i32 %x, %y |
| 50 | + %and6 = and i32 %x, %not2 |
| 51 | + %or7 = or i32 %and5, %and6 |
| 52 | + %and8 = and i32 %z, %or7 |
| 53 | + %xor = xor i32 %and4, %and8 |
| 54 | + ret i32 %xor |
| 55 | +} |
| 56 | + |
| 57 | +define i32 @test2_nested_xor(i32 %x, i32 %y, i32 %z) { |
| 58 | +; CHECK-LABEL: @test2_nested_xor( |
| 59 | +; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2:%.*]], -1 |
| 60 | +; CHECK-NEXT: [[AND8:%.*]] = and i32 [[Z:%.*]], [[TMP3]] |
| 61 | +; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[X:%.*]], [[AND8]] |
| 62 | +; CHECK-NEXT: ret i32 [[TMP1]] |
| 63 | +; |
| 64 | + %and = and i32 %x, %y |
| 65 | + %not = xor i32 %x, -1 |
| 66 | + %not1 = xor i32 %y, -1 |
| 67 | + %and2 = and i32 %not, %not1 |
| 68 | + %or = or i32 %and, %and2 |
| 69 | + %and3 = and i32 %x, %y |
| 70 | + %not4 = xor i32 %y, -1 |
| 71 | + %and5 = and i32 %x, %not4 |
| 72 | + %or6 = or i32 %and3, %and5 |
| 73 | + %xor = xor i32 %or, %or6 |
| 74 | + %not7 = xor i32 %y, -1 |
| 75 | + %and8 = and i32 %z, %not7 |
| 76 | + %and9 = and i32 %xor, %and8 |
| 77 | + %xor10 = xor i32 %or, %and9 |
| 78 | + %xor11 = xor i32 %xor10, %y |
| 79 | + %xor12 = xor i32 %xor11, -1 |
| 80 | + ret i32 %xor12 |
| 81 | +} |
| 82 | + |
| 83 | +define i32 @test3_already_optimal(i32 %x, i32 %y, i32 %z) { |
| 84 | +; CHECK-LABEL: @test3_already_optimal( |
| 85 | +; CHECK-NEXT: [[OR:%.*]] = or i32 [[Y:%.*]], [[Z:%.*]] |
| 86 | +; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[OR]], [[X:%.*]] |
| 87 | +; CHECK-NEXT: [[NOT:%.*]] = xor i32 [[XOR]], -1 |
| 88 | +; CHECK-NEXT: ret i32 [[NOT]] |
| 89 | +; |
| 90 | + %or = or i32 %y, %z |
| 91 | + %xor = xor i32 %or, %x |
| 92 | + %not = xor i32 %xor, -1 |
| 93 | + ret i32 %not |
| 94 | +} |
0 commit comments