001/* 002 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.jtt.optimize; 024 025import java.util.*; 026 027import org.junit.*; 028 029import com.oracle.graal.jtt.*; 030 031/* 032 * Tests optimization of switches. 033 */ 034public class Switch02 extends JTTTest { 035 private static char staticCharVal = 0; 036 private static short staticShortVal = 0; 037 private static byte staticByteVal = 0; 038 039 public static int test(int arg) { 040 switch (arg) { 041 case 1: 042 return 2; 043 default: 044 return 1; 045 } 046 } 047 048 public static int test2char(char arg) { 049 int result = 392123; 050 Object x = null; 051 char val = staticCharVal != 0 ? staticCharVal : arg; 052 switch (val) { 053 case (char) 0xFFFF: 054 result = 23212 / val; 055 break; 056 case (char) 0xFFFF - 3: 057 result = 932991439 / val; 058 break; 059 case (char) 0xFFFF - 6: 060 result = 47329561 / val; 061 break; 062 case (char) 0xFFFF - 9: 063 result = 1950976984 / val; 064 break; 065 case (char) 0xFFFF - 10: 066 result = 97105581 / val; 067 switch (result) { 068 case 1: 069 result = 321; 070 break; 071 default: 072 result = 2391; 073 break; 074 } 075 break; 076 case (char) 0xFFFF - 12: 077 result = 99757362 / val; 078 break; 079 case (char) 0xFFFF - 15: 080 result = 912573 / val; 081 x = new LinkedList<>(); 082 break; 083 case (char) 0xFFFF - 18: 084 x = new HashSet<>(); 085 result = 876765 / val; 086 break; 087 case (char) 0xFFFF - 19: 088 result = 75442917 / val; 089 break; 090 case (char) 0xFFFF - 21: 091 result = 858112498 / val; 092 x = new HashMap<>(); 093 break; 094 default: 095 result = 34324341 / val; 096 } 097 result = result + (x == null ? 0 : x.hashCode()); 098 return result; 099 } 100 101 public static int test2short(short arg) { 102 int result = 392123; 103 Object x = null; 104 short val = staticShortVal != 0 ? staticShortVal : arg; 105 switch (val) { 106 case (short) -0x7FFF: 107 result = 23212 / val; 108 break; 109 case (short) -0x7FFF + 3: 110 result = 932991439 / val; 111 break; 112 case (short) -0x7FFF + 6: 113 result = 47329561 / val; 114 break; 115 case (short) -0x7FFF + 9: 116 result = 1950976984 / val; 117 break; 118 case (short) -0x7FFF + 10: 119 result = 97105581 / val; 120 switch (result) { 121 case 1: 122 result = 321; 123 break; 124 default: 125 result = 2391; 126 break; 127 } 128 break; 129 case (short) -0x7FFF + 12: 130 result = 99757362 / val; 131 break; 132 case (short) -0x7FFF + 15: 133 result = 912573 / val; 134 x = new LinkedList<>(); 135 break; 136 case (short) -0x7FFF + 18: 137 x = new HashSet<>(); 138 result = 876765 / val; 139 break; 140 case (short) -0x7FFF + 19: 141 result = 75442917 / val; 142 break; 143 case (short) -0x7FFF + 21: 144 result = 858112498 / val; 145 x = new HashMap<>(); 146 break; 147 default: 148 result = 34324341 / val; 149 } 150 result = result + (x == null ? 0 : x.hashCode()); 151 return result; 152 } 153 154 public static int test2byte(byte arg) { 155 int result = 392123; 156 Object x = null; 157 byte val = staticByteVal != 0 ? staticByteVal : arg; 158 switch (val) { 159 case (byte) -0x7F: 160 result = 23212 / val; 161 break; 162 case (byte) -0x7F + 3: 163 result = 932991439 / val; 164 break; 165 case (byte) -0x7F + 6: 166 result = 47329561 / val; 167 break; 168 case (byte) -0x7F + 9: 169 result = 1950976984 / val; 170 break; 171 case (byte) -0x7F + 10: 172 result = 97105581 / val; 173 switch (result) { 174 case 1: 175 result = 321; 176 break; 177 default: 178 result = 2391; 179 break; 180 } 181 break; 182 case (byte) -0x7F + 12: 183 result = 99757362 / val; 184 break; 185 case (byte) -0x7F + 15: 186 result = 912573 / val; 187 x = new LinkedList<>(); 188 break; 189 case (byte) -0x7F + 18: 190 x = new HashSet<>(); 191 result = 876765 / val; 192 break; 193 case (byte) -0x7F + 19: 194 result = 75442917 / val; 195 break; 196 case (byte) -0x7F + 20: 197 result = 856261268 / val; 198 break; 199 case (byte) -0x7F + 21: 200 result = 858112498 / val; 201 x = new HashMap<>(); 202 break; 203 default: 204 result = 34324341 / val; 205 } 206 result = result + (x == null ? 0 : x.hashCode()); 207 return result; 208 } 209 210 @Test 211 public void run0() throws Throwable { 212 runTest("test", 0); 213 } 214 215 @Test 216 public void run1() throws Throwable { 217 runTest("test", 1); 218 } 219 220 @Test 221 public void run2() throws Throwable { 222 runTest("test2char", (char) (0x0)); 223 runTest("test2char", (char) (0xFFFF)); 224 runTest("test2char", (char) (0xFFFF - 21)); // miss 225 runTest("test2char", (char) (0xFFFF - 22)); // hit 226 runTest("test2char", (char) (0xFFFF - 23)); // miss (out of bound) 227 228 staticCharVal = (char) 0xFFFF; 229 runTest("test2char", (char) 0); 230 staticCharVal = (char) (0xFFFF - 21); 231 runTest("test2char", (char) 0xFFFF); 232 staticCharVal = (char) (0xFFFF - 22); 233 runTest("test2char", (char) 0xFFFF); 234 staticCharVal = (char) (0xFFFF - 23); 235 runTest("test2char", (char) 0xFFFF); 236 } 237 238 @Test 239 public void run3() throws Throwable { 240 runTest("test2short", (short) 0x0); 241 runTest("test2short", (short) -0x7FFF); 242 runTest("test2short", (short) (-0x7FFF + 21)); // Miss 243 runTest("test2short", (short) (-0x7FFF + 22)); // hit 244 runTest("test2short", (short) (-0x7FFF + 23)); // miss (out of bound) 245 runTest("test2short", (short) 0x7FFF); // miss (out of bound) 246 247 staticShortVal = (short) -0x7FFF; 248 runTest("test2short", (short) 0); 249 staticShortVal = (short) (-0x7FFF + 21); 250 runTest("test2short", (short) 0); 251 staticShortVal = (short) (-0x7FFF + 22); 252 runTest("test2short", (short) 0); 253 staticShortVal = (short) (-0x7FFF + 23); 254 runTest("test2short", (short) 0); 255 staticShortVal = (short) 0x7FFF; 256 runTest("test2short", (short) 0); 257 } 258 259 @Test 260 public void run4() throws Throwable { 261 runTest("test2byte", (byte) 0); 262 runTest("test2byte", (byte) -0x7F); 263 runTest("test2byte", (byte) (-0x7F + 21)); // Miss 264 runTest("test2byte", (byte) (-0x7F + 22)); // hit 265 runTest("test2byte", (byte) (-0x7F + 23)); // miss (out of bound) 266 runTest("test2byte", (byte) 0x7F); // miss (out of bound) 267 268 staticByteVal = (byte) -0x7F; 269 runTest("test2short", (short) 0); 270 staticByteVal = (byte) (-0x7F + 21); 271 runTest("test2short", (short) 0); 272 staticByteVal = (byte) (-0x7F + 22); 273 runTest("test2short", (short) 0); 274 staticByteVal = (byte) (-0x7F + 23); 275 runTest("test2short", (short) 0); 276 staticByteVal = (byte) 0x7F; 277 runTest("test2short", (short) 0); 278 } 279}