Kevin Brodsky | 9ff0d20 | 2016-01-11 13:43:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | public class Main { |
| 18 | |
| 19 | // A dummy value to defeat inlining of these routines. |
| 20 | static boolean doThrow = false; |
| 21 | |
| 22 | public static void assertIntEquals(int expected, int result) { |
| 23 | if (expected != result) { |
| 24 | throw new Error("Expected: " + expected + ", found: " + result); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | public static void assertLongEquals(long expected, long result) { |
| 29 | if (expected != result) { |
| 30 | throw new Error("Expected: " + expected + ", found: " + result); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Test merging of `NOT+AND` into `BIC`. |
| 36 | */ |
| 37 | |
| 38 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAnd(int, int) instruction_simplifier_arm64 (before) |
| 39 | /// CHECK: <<Base:i\d+>> ParameterValue |
| 40 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 41 | /// CHECK: <<Not:i\d+>> Not [<<Mask>>] |
| 42 | /// CHECK: <<Op:i\d+>> And [<<Base>>,<<Not>>] |
| 43 | /// CHECK: Return [<<Op>>] |
| 44 | |
| 45 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAnd(int, int) instruction_simplifier_arm64 (after) |
| 46 | /// CHECK: <<Base:i\d+>> ParameterValue |
| 47 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 48 | /// CHECK: <<NegOp:i\d+>> Arm64BitwiseNegatedRight [<<Base>>,<<Mask>>] kind:And |
| 49 | /// CHECK: Return [<<NegOp>>] |
| 50 | |
| 51 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAnd(int, int) instruction_simplifier_arm64 (after) |
| 52 | /// CHECK-NOT: Not |
| 53 | /// CHECK-NOT: And |
| 54 | |
| 55 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAnd(int, int) disassembly (after) |
| 56 | /// CHECK: bic w{{\d+}}, w{{\d+}}, w{{\d+}} |
| 57 | |
| 58 | public static int $opt$noinline$notAnd(int base, int mask) { |
| 59 | if (doThrow) throw new Error(); |
| 60 | return base & ~mask; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Test merging of `NOT+ORR` into `ORN`. |
| 65 | */ |
| 66 | |
| 67 | /// CHECK-START-ARM64: long Main.$opt$noinline$notOr(long, long) instruction_simplifier_arm64 (before) |
| 68 | /// CHECK: <<Base:j\d+>> ParameterValue |
| 69 | /// CHECK: <<Mask:j\d+>> ParameterValue |
| 70 | /// CHECK: <<Not:j\d+>> Not [<<Mask>>] |
| 71 | /// CHECK: <<Op:j\d+>> Or [<<Base>>,<<Not>>] |
| 72 | /// CHECK: Return [<<Op>>] |
| 73 | |
| 74 | /// CHECK-START-ARM64: long Main.$opt$noinline$notOr(long, long) instruction_simplifier_arm64 (after) |
| 75 | /// CHECK: <<Base:j\d+>> ParameterValue |
| 76 | /// CHECK: <<Mask:j\d+>> ParameterValue |
| 77 | /// CHECK: <<NegOp:j\d+>> Arm64BitwiseNegatedRight [<<Base>>,<<Mask>>] kind:Or |
| 78 | /// CHECK: Return [<<NegOp>>] |
| 79 | |
| 80 | /// CHECK-START-ARM64: long Main.$opt$noinline$notOr(long, long) instruction_simplifier_arm64 (after) |
| 81 | /// CHECK-NOT: Not |
| 82 | /// CHECK-NOT: Or |
| 83 | |
| 84 | /// CHECK-START-ARM64: long Main.$opt$noinline$notOr(long, long) disassembly (after) |
| 85 | /// CHECK: orn x{{\d+}}, x{{\d+}}, x{{\d+}} |
| 86 | |
| 87 | public static long $opt$noinline$notOr(long base, long mask) { |
| 88 | if (doThrow) throw new Error(); |
| 89 | return base | ~mask; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Test merging of `NOT+EOR` into `EON`. |
| 94 | */ |
| 95 | |
| 96 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXor(int, int) instruction_simplifier_arm64 (before) |
| 97 | /// CHECK: <<Base:i\d+>> ParameterValue |
| 98 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 99 | /// CHECK: <<Not:i\d+>> Not [<<Mask>>] |
| 100 | /// CHECK: <<Op:i\d+>> Xor [<<Base>>,<<Not>>] |
| 101 | /// CHECK: Return [<<Op>>] |
| 102 | |
| 103 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXor(int, int) instruction_simplifier_arm64 (after) |
| 104 | /// CHECK: <<Base:i\d+>> ParameterValue |
| 105 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 106 | /// CHECK: <<NegOp:i\d+>> Arm64BitwiseNegatedRight [<<Base>>,<<Mask>>] kind:Xor |
| 107 | /// CHECK: Return [<<NegOp>>] |
| 108 | |
| 109 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXor(int, int) instruction_simplifier_arm64 (after) |
| 110 | /// CHECK-NOT: Not |
| 111 | /// CHECK-NOT: Xor |
| 112 | |
| 113 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXor(int, int) disassembly (after) |
| 114 | /// CHECK: eon w{{\d+}}, w{{\d+}}, w{{\d+}} |
| 115 | |
| 116 | public static int $opt$noinline$notXor(int base, int mask) { |
| 117 | if (doThrow) throw new Error(); |
| 118 | return base ^ ~mask; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Check that the transformation is also done when the base is a constant. |
| 123 | */ |
| 124 | |
| 125 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorConstant(int) instruction_simplifier_arm64 (before) |
| 126 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 127 | /// CHECK: <<Constant:i\d+>> IntConstant |
| 128 | /// CHECK: <<Not:i\d+>> Not [<<Mask>>] |
| 129 | /// CHECK: <<Op:i\d+>> Xor [<<Not>>,<<Constant>>] |
| 130 | /// CHECK: Return [<<Op>>] |
| 131 | |
| 132 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorConstant(int) instruction_simplifier_arm64 (after) |
| 133 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 134 | /// CHECK: <<Constant:i\d+>> IntConstant |
| 135 | /// CHECK: <<NegOp:i\d+>> Arm64BitwiseNegatedRight [<<Constant>>,<<Mask>>] kind:Xor |
| 136 | /// CHECK: Return [<<NegOp>>] |
| 137 | |
| 138 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorConstant(int) instruction_simplifier_arm64 (after) |
| 139 | /// CHECK-NOT: Not |
| 140 | /// CHECK-NOT: Xor |
| 141 | |
| 142 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorConstant(int) disassembly (after) |
| 143 | /// CHECK: mov <<Reg:w\d+>>, #0xf |
| 144 | /// CHECK: eon w{{\d+}}, <<Reg>>, w{{\d+}} |
| 145 | |
| 146 | public static int $opt$noinline$notXorConstant(int mask) { |
| 147 | if (doThrow) throw new Error(); |
| 148 | return 0xf ^ ~mask; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check that no transformation is done when Not has multiple uses. |
| 153 | */ |
| 154 | |
| 155 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAndMultipleUses(int, int) instruction_simplifier_arm64 (before) |
| 156 | /// CHECK: <<Base:i\d+>> ParameterValue |
| 157 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 158 | /// CHECK: <<One:i\d+>> IntConstant |
| 159 | /// CHECK: <<Not:i\d+>> Not [<<Mask>>] |
| 160 | /// CHECK: <<Op1:i\d+>> And [<<Not>>,<<One>>] |
| 161 | /// CHECK: <<Op2:i\d+>> And [<<Base>>,<<Not>>] |
| 162 | /// CHECK: <<Add:i\d+>> Add [<<Op1>>,<<Op2>>] |
| 163 | /// CHECK: Return [<<Add>>] |
| 164 | |
| 165 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAndMultipleUses(int, int) instruction_simplifier_arm64 (after) |
| 166 | /// CHECK: <<Base:i\d+>> ParameterValue |
| 167 | /// CHECK: <<Mask:i\d+>> ParameterValue |
| 168 | /// CHECK: <<One:i\d+>> IntConstant |
| 169 | /// CHECK: <<Not:i\d+>> Not [<<Mask>>] |
| 170 | /// CHECK: <<Op1:i\d+>> And [<<Not>>,<<One>>] |
| 171 | /// CHECK: <<Op2:i\d+>> And [<<Base>>,<<Not>>] |
| 172 | /// CHECK: <<Add:i\d+>> Add [<<Op1>>,<<Op2>>] |
| 173 | /// CHECK: Return [<<Add>>] |
| 174 | |
| 175 | /// CHECK-START-ARM64: int Main.$opt$noinline$notAndMultipleUses(int, int) instruction_simplifier_arm64 (after) |
| 176 | /// CHECK-NOT: Arm64BitwiseNegatedRight |
| 177 | |
| 178 | public static int $opt$noinline$notAndMultipleUses(int base, int mask) { |
| 179 | if (doThrow) throw new Error(); |
| 180 | int tmp = ~mask; |
| 181 | return (tmp & 0x1) + (base & tmp); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Check that no transformation is done when both inputs are Not's. |
| 186 | */ |
| 187 | |
| 188 | // We don't check the instructions before the pass, since if De Morgan's laws |
| 189 | // have been applied then Not/Not/Or is replaced by And/Not. |
| 190 | |
| 191 | /// CHECK-START-ARM64: int Main.$opt$noinline$deMorganOr(int, int) instruction_simplifier_arm64 (after) |
| 192 | /// CHECK-NOT: Arm64BitwiseNegatedRight |
| 193 | |
| 194 | public static int $opt$noinline$deMorganOr(int a, int b) { |
| 195 | if (doThrow) throw new Error(); |
| 196 | return ~a | ~b; |
| 197 | } |
| 198 | |
| 199 | public static void main(String[] args) { |
| 200 | assertIntEquals(0xe, $opt$noinline$notAnd(0xf, 0x1)); |
| 201 | assertLongEquals(~0x0, $opt$noinline$notOr(0xf, 0x1)); |
| 202 | assertIntEquals(~0xe, $opt$noinline$notXor(0xf, 0x1)); |
| 203 | assertIntEquals(~0xe, $opt$noinline$notXorConstant(0x1)); |
| 204 | assertIntEquals(0xe, $opt$noinline$notAndMultipleUses(0xf, 0x1)); |
| 205 | assertIntEquals(~0x1, $opt$noinline$deMorganOr(0x3, 0x1)); |
| 206 | } |
| 207 | } |