Kevin Brodsky | 9679849 | 2016-01-15 09:49:20 +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 transformation of Not/Not/And into Or/Not. |
| 36 | */ |
| 37 | |
| 38 | // Note: before the instruction_simplifier pass, Xor's are used instead of |
| 39 | // Not's (the simplification happens during the same pass). |
| 40 | /// CHECK-START-ARM64: int Main.$opt$noinline$andToOr(int, int) instruction_simplifier (before) |
| 41 | /// CHECK: <<P1:i\d+>> ParameterValue |
| 42 | /// CHECK: <<P2:i\d+>> ParameterValue |
| 43 | /// CHECK: <<CstM1:i\d+>> IntConstant -1 |
| 44 | /// CHECK: <<Not1:i\d+>> Xor [<<P1>>,<<CstM1>>] |
| 45 | /// CHECK: <<Not2:i\d+>> Xor [<<P2>>,<<CstM1>>] |
| 46 | /// CHECK: <<And:i\d+>> And [<<Not1>>,<<Not2>>] |
| 47 | /// CHECK: Return [<<And>>] |
| 48 | |
| 49 | /// CHECK-START-ARM64: int Main.$opt$noinline$andToOr(int, int) instruction_simplifier (after) |
| 50 | /// CHECK: <<P1:i\d+>> ParameterValue |
| 51 | /// CHECK: <<P2:i\d+>> ParameterValue |
| 52 | /// CHECK: <<Or:i\d+>> Or [<<P1>>,<<P2>>] |
| 53 | /// CHECK: <<Not:i\d+>> Not [<<Or>>] |
| 54 | /// CHECK: Return [<<Not>>] |
| 55 | |
| 56 | /// CHECK-START-ARM64: int Main.$opt$noinline$andToOr(int, int) instruction_simplifier (after) |
| 57 | /// CHECK: Not |
| 58 | /// CHECK-NOT: Not |
| 59 | /// CHECK-NOT: And |
| 60 | |
| 61 | public static int $opt$noinline$andToOr(int a, int b) { |
| 62 | if (doThrow) throw new Error(); |
| 63 | return ~a & ~b; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Test transformation of Not/Not/Or into And/Not. |
| 68 | */ |
| 69 | |
| 70 | // See note above. |
| 71 | // The second Xor has its arguments reversed for no obvious reason. |
| 72 | /// CHECK-START-ARM64: long Main.$opt$noinline$orToAnd(long, long) instruction_simplifier (before) |
| 73 | /// CHECK: <<P1:j\d+>> ParameterValue |
| 74 | /// CHECK: <<P2:j\d+>> ParameterValue |
| 75 | /// CHECK: <<CstM1:j\d+>> LongConstant -1 |
| 76 | /// CHECK: <<Not1:j\d+>> Xor [<<P1>>,<<CstM1>>] |
| 77 | /// CHECK: <<Not2:j\d+>> Xor [<<CstM1>>,<<P2>>] |
| 78 | /// CHECK: <<Or:j\d+>> Or [<<Not1>>,<<Not2>>] |
| 79 | /// CHECK: Return [<<Or>>] |
| 80 | |
| 81 | /// CHECK-START-ARM64: long Main.$opt$noinline$orToAnd(long, long) instruction_simplifier (after) |
| 82 | /// CHECK: <<P1:j\d+>> ParameterValue |
| 83 | /// CHECK: <<P2:j\d+>> ParameterValue |
| 84 | /// CHECK: <<And:j\d+>> And [<<P1>>,<<P2>>] |
| 85 | /// CHECK: <<Not:j\d+>> Not [<<And>>] |
| 86 | /// CHECK: Return [<<Not>>] |
| 87 | |
| 88 | /// CHECK-START-ARM64: long Main.$opt$noinline$orToAnd(long, long) instruction_simplifier (after) |
| 89 | /// CHECK: Not |
| 90 | /// CHECK-NOT: Not |
| 91 | /// CHECK-NOT: Or |
| 92 | |
| 93 | public static long $opt$noinline$orToAnd(long a, long b) { |
| 94 | if (doThrow) throw new Error(); |
| 95 | return ~a | ~b; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Test transformation of Not/Not/Xor into Xor. |
| 100 | */ |
| 101 | |
| 102 | // See first note above. |
| 103 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorToXor(int, int) instruction_simplifier (before) |
| 104 | /// CHECK: <<P1:i\d+>> ParameterValue |
| 105 | /// CHECK: <<P2:i\d+>> ParameterValue |
| 106 | /// CHECK: <<CstM1:i\d+>> IntConstant -1 |
| 107 | /// CHECK: <<Not1:i\d+>> Xor [<<P1>>,<<CstM1>>] |
| 108 | /// CHECK: <<Not2:i\d+>> Xor [<<P2>>,<<CstM1>>] |
| 109 | /// CHECK: <<Xor:i\d+>> Xor [<<Not1>>,<<Not2>>] |
| 110 | /// CHECK: Return [<<Xor>>] |
| 111 | |
| 112 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorToXor(int, int) instruction_simplifier (after) |
| 113 | /// CHECK: <<P1:i\d+>> ParameterValue |
| 114 | /// CHECK: <<P2:i\d+>> ParameterValue |
| 115 | /// CHECK: <<Xor:i\d+>> Xor [<<P1>>,<<P2>>] |
| 116 | /// CHECK: Return [<<Xor>>] |
| 117 | |
| 118 | /// CHECK-START-ARM64: int Main.$opt$noinline$notXorToXor(int, int) instruction_simplifier (after) |
| 119 | /// CHECK-NOT: Not |
| 120 | |
| 121 | public static int $opt$noinline$notXorToXor(int a, int b) { |
| 122 | if (doThrow) throw new Error(); |
| 123 | return ~a ^ ~b; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check that no transformation is done when one Not has multiple uses. |
| 128 | */ |
| 129 | |
| 130 | /// CHECK-START-ARM64: int Main.$opt$noinline$notMultipleUses(int, int) instruction_simplifier (before) |
| 131 | /// CHECK: <<P1:i\d+>> ParameterValue |
| 132 | /// CHECK: <<P2:i\d+>> ParameterValue |
| 133 | /// CHECK: <<CstM1:i\d+>> IntConstant -1 |
| 134 | /// CHECK: <<One:i\d+>> IntConstant 1 |
| 135 | /// CHECK: <<Not2:i\d+>> Xor [<<P2>>,<<CstM1>>] |
| 136 | /// CHECK: <<And2:i\d+>> And [<<Not2>>,<<One>>] |
| 137 | /// CHECK: <<Not1:i\d+>> Xor [<<P1>>,<<CstM1>>] |
| 138 | /// CHECK: <<And1:i\d+>> And [<<Not1>>,<<Not2>>] |
| 139 | /// CHECK: <<Add:i\d+>> Add [<<And2>>,<<And1>>] |
| 140 | /// CHECK: Return [<<Add>>] |
| 141 | |
| 142 | /// CHECK-START-ARM64: int Main.$opt$noinline$notMultipleUses(int, int) instruction_simplifier (after) |
| 143 | /// CHECK: <<P1:i\d+>> ParameterValue |
| 144 | /// CHECK: <<P2:i\d+>> ParameterValue |
| 145 | /// CHECK: <<One:i\d+>> IntConstant 1 |
| 146 | /// CHECK: <<Not2:i\d+>> Not [<<P2>>] |
| 147 | /// CHECK: <<And2:i\d+>> And [<<Not2>>,<<One>>] |
| 148 | /// CHECK: <<Not1:i\d+>> Not [<<P1>>] |
| 149 | /// CHECK: <<And1:i\d+>> And [<<Not1>>,<<Not2>>] |
| 150 | /// CHECK: <<Add:i\d+>> Add [<<And2>>,<<And1>>] |
| 151 | /// CHECK: Return [<<Add>>] |
| 152 | |
| 153 | /// CHECK-START-ARM64: int Main.$opt$noinline$notMultipleUses(int, int) instruction_simplifier (after) |
| 154 | /// CHECK-NOT: Or |
| 155 | |
| 156 | public static int $opt$noinline$notMultipleUses(int a, int b) { |
| 157 | if (doThrow) throw new Error(); |
| 158 | int tmp = ~b; |
| 159 | return (tmp & 0x1) + (~a & tmp); |
| 160 | } |
| 161 | |
| 162 | public static void main(String[] args) { |
| 163 | assertIntEquals(~0xff, $opt$noinline$andToOr(0xf, 0xff)); |
| 164 | assertLongEquals(~0xf, $opt$noinline$orToAnd(0xf, 0xff)); |
| 165 | assertIntEquals(0xf0, $opt$noinline$notXorToXor(0xf, 0xff)); |
| 166 | assertIntEquals(~0xff, $opt$noinline$notMultipleUses(0xf, 0xff)); |
| 167 | } |
| 168 | } |