1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.c2.irTests;
import jdk.test.lib.Asserts;
import compiler.lib.ir_framework.*;
/*
* @test
* @bug 8267265
* @summary Test that Ideal transformations of AddINode* are being performed as expected.
* @library /test/lib /
* @run driver compiler.c2.irTests.AddINodeIdealizationTests
*/
public class AddINodeIdealizationTests {
public static void main(String[] args) {
TestFramework.run();
}
@Run(test = {"additions", "xMinusX", "test1",
"test2", "test3", "test4",
"test5", "test6", "test7",
"test8", "test9", "test10",
"test11", "test12", "test13",
"test14", "test15", "test16",
"test17", "test18", "test19",
"test20", "test21", "test22",
"test23", "test24", "test25"})
public void runMethod() {
int a = RunInfo.getRandom().nextInt();
int b = RunInfo.getRandom().nextInt();
int c = RunInfo.getRandom().nextInt();
int d = RunInfo.getRandom().nextInt();
int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;
assertResult(0, 0, 0, 0);
assertResult(a, b, c, d);
assertResult(min, min, min, min);
assertResult(max, max, max, max);
}
@DontCompile
public void assertResult(int a, int b, int c, int d) {
Asserts.assertEQ(((a+a) + (a+a)) , additions(a));
Asserts.assertEQ(0 , xMinusX(a));
Asserts.assertEQ(a + 1 + 2 , test1(a));
Asserts.assertEQ((a + 2021) + b , test2(a, b));
Asserts.assertEQ(a + (b + 2021) , test3(a, b));
Asserts.assertEQ((1 - a) + 2 , test4(a));
Asserts.assertEQ((a - b) + (c - d) , test5(a, b, c, d));
Asserts.assertEQ((a - b) + (b + c) , test6(a, b, c));
Asserts.assertEQ((a - b) + (c + b) , test7(a, b, c));
Asserts.assertEQ((a - b) + (b - c) , test8(a, b, c));
Asserts.assertEQ((a - b) + (c - a) , test9(a, b, c));
Asserts.assertEQ(a + (0 - b) , test10(a, b));
Asserts.assertEQ((0 - b) + a , test11(a, b));
Asserts.assertEQ((a - b) + b , test12(a, b));
Asserts.assertEQ(b + (a - b) , test13(a, b));
Asserts.assertEQ(a + 0 , test14(a));
Asserts.assertEQ(0 + a , test15(a));
Asserts.assertEQ(a*b + a*c , test16(a, b, c));
Asserts.assertEQ(a*b + b*c , test17(a, b, c));
Asserts.assertEQ(a*c + b*c , test18(a, b, c));
Asserts.assertEQ(a*b + c*a , test19(a, b, c));
Asserts.assertEQ((a - b) + 210 , test20(a, b));
Asserts.assertEQ((a - b) + 190 , test21(a, b));
Asserts.assertEQ((a - b) + 210 , test22(a, b));
Asserts.assertEQ((a - b) + 190 , test23(a, b));
Asserts.assertEQ(Math.max(a, b) + Math.min(a, b), test24(a, b));
Asserts.assertEQ(Math.min(a, b) + Math.max(a, b), test25(a, b));
}
@Test
@IR(counts = {IRNode.ADD, "2"})
// Checks (x + x) + (x + x) => a=(x + x); r=a+a
public int additions(int x) {
return (x + x) + (x + x);
}
@Test
@IR(failOn = {IRNode.ADD, IRNode.SUB})
// Checks (x - x) + (x - x) => 0
public int xMinusX(int x) {
return (x - x) + (x - x);
}
@Test
@IR(counts = {IRNode.ADD, "1"})
// Checks (x + c1) + c2 => x + c3 where c3 = c1 + c2
public int test1(int x) {
return (x + 1) + 2;
}
@Test
@IR(counts = {IRNode.ADD, "2"})
// Checks (x + c1) + y => (x + y) + c1
public int test2(int x, int y) {
return (x + 2021) + y;
}
@Test
@IR(counts = {IRNode.ADD, "2"})
// Checks x + (y + c1) => (x + y) + c1
public int test3(int x, int y) {
return x + (y + 2021);
}
@Test
@IR(failOn = {IRNode.ADD})
@IR(counts = {IRNode.SUB, "1"})
// Checks (c1 - x) + c2 => c3 - x where c3 = c1 + c2
public int test4(int x) {
return (1 - x) + 2;
}
@Test
@IR(counts = {IRNode.SUB, "1",
IRNode.ADD, "2",
})
// Checks (a - b) + (c - d) => (a + c) - (b + d)
public int test5(int a, int b, int c, int d) {
return (a - b) + (c - d);
}
@Test
@IR(failOn = {IRNode.SUB})
@IR(counts = {IRNode.ADD, "1"})
// Checks (a - b) + (b + c) => (a + c)
public int test6(int a, int b, int c) {
return (a - b) + (b + c);
}
@Test
@IR(failOn = {IRNode.SUB})
@IR(counts = {IRNode.ADD, "1"})
// Checks (a - b) + (c + b) => (a + c)
public int test7(int a, int b, int c) {
return (a - b) + (c + b);
}
@Test
@IR(failOn = {IRNode.ADD})
@IR(counts = {IRNode.SUB, "1"})
// Checks (a - b) + (b - c) => (a - c)
public int test8(int a, int b, int c) {
return (a - b) + (b - c);
}
@Test
@IR(failOn = {IRNode.ADD})
@IR(counts = {IRNode.SUB, "1"})
// Checks (a - b) + (c - a) => (c - b)
public int test9(int a, int b, int c) {
return (a - b) + (c - a);
}
@Test
@IR(failOn = {IRNode.ADD})
@IR(counts = {IRNode.SUB, "1"})
// Checks x + (0 - y) => (x - y)
public int test10(int x, int y) {
return x + (0 - y);
}
@Test
@IR(failOn = {IRNode.ADD})
@IR(counts = {IRNode.SUB, "1"})
// Checks (0 - y) + x => (x - y)
public int test11(int x, int y) {
return (0 - y) + x;
}
@Test
@IR(failOn = {IRNode.ADD, IRNode.SUB})
// Checks (x - y) + y => x
public int test12(int x, int y) {
return (x - y) + y;
}
@Test
@IR(failOn = {IRNode.ADD, IRNode.SUB})
// Checks y + (x - y) => x
public int test13(int x, int y) {
return y + (x - y);
}
@Test
@IR(failOn = {IRNode.ADD})
// Checks x + 0 => x
public int test14(int x) {
return x + 0;
}
@Test
@IR(failOn = {IRNode.ADD})
// Checks 0 + x => x
public int test15(int x) {
return 0 + x;
}
@Test
@IR(counts = {IRNode.MUL, "1",
IRNode.ADD, "1"
})
// Checks "a*b + a*c => a*(b+c)
public int test16(int a, int b, int c) {
return a*b + a*c;
}
@Test
@IR(counts = {IRNode.MUL, "1",
IRNode.ADD, "1"
})
// Checks a*b + b*c => b*(a+c)
public int test17(int a, int b, int c) {
return a*b + b*c;
}
@Test
@IR(counts = {IRNode.MUL, "1",
IRNode.ADD, "1"
})
// Checks a*c + b*c => (a+b)*c
public int test18(int a, int b, int c) {
return a*c + b*c;
}
@Test
@IR(counts = {IRNode.MUL, "1",
IRNode.ADD, "1"
})
// Checks a*b + c*a => a*(b+c)
public int test19(int a, int b, int c) {
return a*b + c*a;
}
@Test
@IR(counts = {IRNode.SUB_I, "1",
IRNode.ADD_I, "1",
IRNode.CON_I, "1"})
// Checks x + (con - y) => (x - y) + con
// where con > 0
public int test20(int x, int y) {
return x + (10 - y) + 200; // transformed to (x - y) + 210;
}
@Test
@IR(counts = {IRNode.SUB_I, "1",
IRNode.ADD_I, "1",
IRNode.CON_I, "1"})
// Checks x + (con - y) => (x - y) + con
// where con < 0
public int test21(int x, int y) {
return x + (-10 - y) + 200; // transformed to (x - y) + 190;
}
@Test
@IR(counts = {IRNode.SUB_I, "1",
IRNode.ADD_I, "1",
IRNode.CON_I, "1"})
// Checks (con - y) + x => (x - y) + con
// where con > 0
public int test22(int x, int y) {
return (10 - y) + x + 200; // transformed to (x - y) + 210;
}
@Test
@IR(counts = {IRNode.SUB_I, "1",
IRNode.ADD_I, "1",
IRNode.CON_I, "1"})
// Checks (con - y) + x => (x - y) + con
// where con < 0
public int test23(int x, int y) {
return x + (-10 - y) + 200; // transformed to (x - y) + 190;
}
@Test
@IR(failOn = { IRNode.MAX, IRNode.MIN })
@IR(counts = { IRNode.ADD, "1" })
// Checks Math.max(a, b) + Math.min(a, b) => a + b
public int test24(int a, int b) {
return Math.max(a, b) + Math.min(a, b);
}
@Test
@IR(failOn = { IRNode.MAX, IRNode.MIN })
@IR(counts = { IRNode.ADD, "1" })
// Checks Math.min(a, b) + Math.max(a, b) => a + b
public int test25(int a, int b) {
return Math.min(a, b) + Math.max(a, b);
}
}
|