1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.core.test; 26 27 import org.graalvm.compiler.nodes.StructuredGraph; 28 import org.graalvm.compiler.nodes.extended.IntegerSwitchNode; 29 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 30 import org.junit.Test; 31 32 public class SwitchCanonicalizerTest extends GraalCompilerTest { 33 34 public int divByPowerOf2(int n) { 35 switch (n / 8) { 36 case Integer.MAX_VALUE / 8 + 1: 37 return hashCode(); 38 default: 39 return 1; 40 } 41 } 42 43 @Test 44 public void testDivByPowerOf2() { 45 shouldFoldSwitch("divByPowerOf2"); 46 } 47 48 public int divByNonPowerOf2(int n) { 49 switch (n / 7) { 50 case Integer.MAX_VALUE / 7 + 1: 51 return hashCode(); 52 default: 53 return 1; 54 } 55 } 56 57 @Test 58 public void testDivByNonPowerOf2() { 59 shouldFoldSwitch("divByNonPowerOf2"); 60 } 61 62 public int remByPowerOf2(int n) { 63 switch (n % 8) { 64 case 9: 65 return hashCode(); 66 default: 67 return 1; 68 } 69 } 70 71 @Test 72 public void testRemByPowerOf2() { 73 shouldFoldSwitch("remByPowerOf2"); 74 } 75 76 public int remByPowerOf2PositiveX(int n) { 77 int n0 = n > 0 ? 8 : 9; 78 switch (n0 % 8) { 79 case 9: 80 return hashCode(); 81 default: 82 return 1; 83 } 84 } 85 86 @Test 87 public void testRemByPowerOf2PositiveX() { 88 shouldFoldSwitch("remByPowerOf2PositiveX"); 89 } 90 91 public int remByPowerOf2NegativeX(int n) { 92 int n0 = n > 0 ? -8 : -9; 93 switch (n0 % 8) { 94 case 9: 95 return hashCode(); 96 default: 97 return 1; 98 } 99 } 100 101 @Test 102 public void testRemByPowerOf2NegativeX() { 103 shouldFoldSwitch("remByPowerOf2NegativeX"); 104 } 105 106 public int remByNonPowerOf2(int n) { 107 switch (n % 7) { 108 case 9: 109 return hashCode(); 110 default: 111 return 1; 112 } 113 } 114 115 @Test 116 public void testRemByNonPowerOf2() { 117 shouldFoldSwitch("remByNonPowerOf2"); 118 } 119 120 private void shouldFoldSwitch(String methodName) { 121 StructuredGraph graph = parseForCompile(getResolvedJavaMethod(methodName)); 122 new CanonicalizerPhase().apply(graph, getDefaultHighTierContext()); 123 assertFalse(graph.hasNode(IntegerSwitchNode.TYPE)); 124 } 125 126 }