Mercurial > hg > graal-jvmci-8
comparison graal/Compiler/src/com/sun/c1x/opt/Canonicalizer.java @ 2507:9ec15d6914ca
Pull over of compiler from maxine repository.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 27 Apr 2011 11:43:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2506:4a3bf8a5bf41 | 2507:9ec15d6914ca |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2011, 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 package com.sun.c1x.opt; | |
24 | |
25 import static com.sun.cri.bytecode.Bytecodes.*; | |
26 | |
27 import java.lang.reflect.*; | |
28 import java.util.*; | |
29 | |
30 import com.sun.c1x.*; | |
31 import com.sun.c1x.ir.*; | |
32 import com.sun.c1x.util.*; | |
33 import com.sun.cri.ci.*; | |
34 import com.sun.cri.ri.*; | |
35 | |
36 /** | |
37 * The {@code Canonicalizer} reduces instructions to a canonical form by folding constants, | |
38 * putting constants on the right side of commutative operators, simplifying conditionals, | |
39 * and several other transformations. | |
40 * | |
41 * @author Ben L. Titzer | |
42 */ | |
43 public class Canonicalizer extends DefaultValueVisitor { | |
44 | |
45 final RiRuntime runtime; | |
46 final RiMethod method; | |
47 final CiTarget target; | |
48 Value canonical; | |
49 List<Instruction> extra; | |
50 | |
51 public Canonicalizer(RiRuntime runtime, RiMethod method, CiTarget target) { | |
52 this.runtime = runtime; | |
53 this.method = method; | |
54 this.target = target; | |
55 } | |
56 | |
57 public Value canonicalize(Instruction original) { | |
58 this.canonical = original; | |
59 this.extra = null; | |
60 original.accept(this); | |
61 return this.canonical; | |
62 } | |
63 | |
64 public List<Instruction> extra() { | |
65 return extra; | |
66 } | |
67 | |
68 private <T extends Instruction> T addInstr(T x) { | |
69 if (extra == null) { | |
70 extra = new LinkedList<Instruction>(); | |
71 } | |
72 extra.add(x); | |
73 return x; | |
74 } | |
75 | |
76 private Constant intInstr(int v) { | |
77 return addInstr(Constant.forInt(v)); | |
78 } | |
79 | |
80 private Constant longInstr(long v) { | |
81 return addInstr(Constant.forLong(v)); | |
82 } | |
83 | |
84 private Constant wordInstr(long v) { | |
85 return addInstr(Constant.forWord(v)); | |
86 } | |
87 | |
88 private Value setCanonical(Value x) { | |
89 return canonical = x; | |
90 } | |
91 | |
92 private Value setIntConstant(int val) { | |
93 return canonical = Constant.forInt(val); | |
94 } | |
95 | |
96 private Value setConstant(CiConstant val) { | |
97 return canonical = new Constant(val); | |
98 } | |
99 | |
100 private Value setBooleanConstant(boolean val) { | |
101 return canonical = Constant.forBoolean(val); | |
102 } | |
103 | |
104 private Value setObjectConstant(Object val) { | |
105 if (C1XOptions.SupportObjectConstants) { | |
106 return canonical = Constant.forObject(val); | |
107 } | |
108 return canonical; | |
109 } | |
110 | |
111 private Value setLongConstant(long val) { | |
112 return canonical = Constant.forLong(val); | |
113 } | |
114 | |
115 private Value setFloatConstant(float val) { | |
116 return canonical = Constant.forFloat(val); | |
117 } | |
118 | |
119 private Value setDoubleConstant(double val) { | |
120 return canonical = Constant.forDouble(val); | |
121 } | |
122 | |
123 private Value setWordConstant(long val) { | |
124 return canonical = Constant.forDouble(val); | |
125 } | |
126 | |
127 private void moveConstantToRight(Op2 x) { | |
128 if (x.x().isConstant() && isCommutative(x.opcode)) { | |
129 x.swapOperands(); | |
130 } | |
131 } | |
132 | |
133 private void visitOp2(Op2 i) { | |
134 final Value x = i.x(); | |
135 final Value y = i.y(); | |
136 | |
137 if (x == y) { | |
138 // the left and right operands are the same value, try reducing some operations | |
139 switch (i.opcode) { | |
140 case ISUB: setIntConstant(0); return; | |
141 case LSUB: setLongConstant(0); return; | |
142 case IAND: // fall through | |
143 case LAND: // fall through | |
144 case IOR: // fall through | |
145 case LOR: setCanonical(x); return; | |
146 case IXOR: setIntConstant(0); return; | |
147 case LXOR: setLongConstant(0); return; | |
148 } | |
149 } | |
150 | |
151 CiKind kind = x.kind; | |
152 if (x.isConstant() && y.isConstant()) { | |
153 // both operands are constants, try constant folding | |
154 switch (kind) { | |
155 case Int: { | |
156 Integer val = foldIntOp2(i.opcode, x.asConstant().asInt(), y.asConstant().asInt()); | |
157 if (val != null) { | |
158 setIntConstant(val); // the operation was successfully folded to an int | |
159 return; | |
160 } | |
161 break; | |
162 } | |
163 case Long: { | |
164 Long val = foldLongOp2(i.opcode, x.asConstant().asLong(), y.asConstant().asLong()); | |
165 if (val != null) { | |
166 setLongConstant(val); // the operation was successfully folded to a long | |
167 return; | |
168 } | |
169 break; | |
170 } | |
171 case Float: { | |
172 if (C1XOptions.CanonicalizeFloatingPoint) { | |
173 // try to fold a floating point operation | |
174 Float val = foldFloatOp2(i.opcode, x.asConstant().asFloat(), y.asConstant().asFloat()); | |
175 if (val != null) { | |
176 setFloatConstant(val); // the operation was successfully folded to a float | |
177 return; | |
178 } | |
179 } | |
180 break; | |
181 } | |
182 case Double: { | |
183 if (C1XOptions.CanonicalizeFloatingPoint) { | |
184 // try to fold a floating point operation | |
185 Double val = foldDoubleOp2(i.opcode, x.asConstant().asDouble(), y.asConstant().asDouble()); | |
186 if (val != null) { | |
187 setDoubleConstant(val); // the operation was successfully folded to a double | |
188 return; | |
189 } | |
190 } | |
191 break; | |
192 } | |
193 case Word: { | |
194 CiConstant val = runtime.foldWordOperation(i.opcode, new CiMethodInvokeArguments() { | |
195 int argIndex; | |
196 @Override | |
197 public CiConstant nextArg() { | |
198 if (argIndex == 0) { | |
199 return x.asConstant(); | |
200 } | |
201 if (argIndex == 1) { | |
202 return y.asConstant(); | |
203 } | |
204 argIndex++; | |
205 return null; | |
206 } | |
207 }); | |
208 | |
209 if (val != null) { | |
210 setConstant(val); // the operation was successfully folded to a word | |
211 return; | |
212 } | |
213 break; | |
214 } | |
215 } | |
216 } | |
217 | |
218 // if there is a constant on the left and the operation is commutative, move it to the right | |
219 moveConstantToRight(i); | |
220 | |
221 if (i.y().isConstant()) { | |
222 // the right side is a constant, try strength reduction | |
223 switch (kind) { | |
224 case Int: { | |
225 if (reduceIntOp2(i, i.x(), i.y().asConstant().asInt()) != null) { | |
226 return; | |
227 } | |
228 break; | |
229 } | |
230 case Long: { | |
231 if (reduceLongOp2(i, i.x(), i.y().asConstant().asLong()) != null) { | |
232 return; | |
233 } | |
234 break; | |
235 } | |
236 case Word: { | |
237 if (reduceWordOp2(i, i.x(), i.y().asConstant().asLong()) != null) { | |
238 return; | |
239 } | |
240 break; | |
241 } | |
242 // XXX: note that other cases are possible, but harder | |
243 // floating point operations need to be extra careful | |
244 } | |
245 } | |
246 assert Util.archKindsEqual(i, canonical); | |
247 } | |
248 | |
249 private Value reduceIntOp2(Op2 original, Value x, int y) { | |
250 // attempt to reduce a binary operation with a constant on the right | |
251 int opcode = original.opcode; | |
252 switch (opcode) { | |
253 case IADD: return y == 0 ? setCanonical(x) : null; | |
254 case ISUB: return y == 0 ? setCanonical(x) : null; | |
255 case IMUL: { | |
256 if (y == 1) { | |
257 return setCanonical(x); | |
258 } | |
259 if (y > 0 && (y & y - 1) == 0 && C1XOptions.CanonicalizeMultipliesToShifts) { | |
260 // strength reduce multiply by power of 2 to shift operation | |
261 return setCanonical(new ShiftOp(ISHL, x, intInstr(CiUtil.log2(y)))); | |
262 } | |
263 return y == 0 ? setIntConstant(0) : null; | |
264 } | |
265 case IDIV: return y == 1 ? setCanonical(x) : null; | |
266 case IREM: return y == 1 ? setCanonical(x) : null; | |
267 case IAND: { | |
268 if (y == -1) { | |
269 return setCanonical(x); | |
270 } | |
271 return y == 0 ? setIntConstant(0) : null; | |
272 } | |
273 case IOR: { | |
274 if (y == -1) { | |
275 return setIntConstant(-1); | |
276 } | |
277 return y == 0 ? setCanonical(x) : null; | |
278 } | |
279 case IXOR: return y == 0 ? setCanonical(x) : null; | |
280 case ISHL: return reduceShift(false, opcode, IUSHR, x, y); | |
281 case ISHR: return reduceShift(false, opcode, 0, x, y); | |
282 case IUSHR: return reduceShift(false, opcode, ISHL, x, y); | |
283 } | |
284 return null; | |
285 } | |
286 | |
287 private Value reduceShift(boolean islong, int opcode, int reverse, Value x, long y) { | |
288 int mod = islong ? 0x3f : 0x1f; | |
289 long shift = y & mod; | |
290 if (shift == 0) { | |
291 return setCanonical(x); | |
292 } | |
293 if (x instanceof ShiftOp) { | |
294 // this is a chained shift operation ((e shift e) shift K) | |
295 ShiftOp s = (ShiftOp) x; | |
296 if (s.y().isConstant()) { | |
297 long z = s.y().asConstant().asLong(); | |
298 if (s.opcode == opcode) { | |
299 // this is a chained shift operation (e >> C >> K) | |
300 y = y + z; | |
301 shift = y & mod; | |
302 if (shift == 0) { | |
303 return setCanonical(s.x()); | |
304 } | |
305 // reduce to (e >> (C + K)) | |
306 return setCanonical(new ShiftOp(opcode, s.x(), intInstr((int) shift))); | |
307 } | |
308 if (s.opcode == reverse && y == z) { | |
309 // this is a chained shift of the form (e >> K << K) | |
310 if (islong) { | |
311 long mask = -1; | |
312 if (opcode == LUSHR) { | |
313 mask = mask >>> y; | |
314 } else { | |
315 mask = mask << y; | |
316 } | |
317 // reduce to (e & mask) | |
318 return setCanonical(new LogicOp(LAND, s.x(), longInstr(mask))); | |
319 } else { | |
320 int mask = -1; | |
321 if (opcode == IUSHR) { | |
322 mask = mask >>> y; | |
323 } else { | |
324 mask = mask << y; | |
325 } | |
326 return setCanonical(new LogicOp(IAND, s.x(), intInstr(mask))); | |
327 } | |
328 } | |
329 } | |
330 } | |
331 if (y != shift) { | |
332 // (y & mod) != y | |
333 return setCanonical(new ShiftOp(opcode, x, intInstr((int) shift))); | |
334 } | |
335 return null; | |
336 } | |
337 | |
338 private Value reduceLongOp2(Op2 original, Value x, long y) { | |
339 // attempt to reduce a binary operation with a constant on the right | |
340 int opcode = original.opcode; | |
341 switch (opcode) { | |
342 case LADD: return y == 0 ? setCanonical(x) : null; | |
343 case LSUB: return y == 0 ? setCanonical(x) : null; | |
344 case LMUL: { | |
345 if (y == 1) { | |
346 return setCanonical(x); | |
347 } | |
348 if (y > 0 && (y & y - 1) == 0 && C1XOptions.CanonicalizeMultipliesToShifts) { | |
349 // strength reduce multiply by power of 2 to shift operation | |
350 return setCanonical(new ShiftOp(LSHL, x, intInstr(CiUtil.log2(y)))); | |
351 } | |
352 return y == 0 ? setLongConstant(0) : null; | |
353 } | |
354 case LDIV: return y == 1 ? setCanonical(x) : null; | |
355 case LREM: return y == 1 ? setCanonical(x) : null; | |
356 case LAND: { | |
357 if (y == -1) { | |
358 return setCanonical(x); | |
359 } | |
360 return y == 0 ? setLongConstant(0) : null; | |
361 } | |
362 case LOR: { | |
363 if (y == -1) { | |
364 return setLongConstant(-1); | |
365 } | |
366 return y == 0 ? setCanonical(x) : null; | |
367 } | |
368 case LXOR: return y == 0 ? setCanonical(x) : null; | |
369 case LSHL: return reduceShift(true, opcode, LUSHR, x, y); | |
370 case LSHR: return reduceShift(true, opcode, 0, x, y); | |
371 case LUSHR: return reduceShift(true, opcode, LSHL, x, y); | |
372 } | |
373 return null; | |
374 } | |
375 | |
376 private Value reduceWordOp2(Op2 original, Value x, long y) { | |
377 if (y == 0) { | |
378 // Defer to arithmetic exception at runtime | |
379 return null; | |
380 } | |
381 // attempt to reduce a binary operation with a constant on the right | |
382 int opcode = original.opcode; | |
383 switch (opcode) { | |
384 case WDIVI: | |
385 case WDIV: { | |
386 if (y == 1) { | |
387 return setCanonical(x); | |
388 } | |
389 if (CiUtil.isPowerOf2(y)) { | |
390 return setCanonical(new ShiftOp(target.arch.is64bit() ? LUSHR : IUSHR, x, intInstr(CiUtil.log2(y)))); | |
391 } | |
392 break; | |
393 } | |
394 case WREMI: { | |
395 if (y == 1) { | |
396 return setCanonical(intInstr(0)); | |
397 } | |
398 if (CiUtil.isPowerOf2(y)) { | |
399 int mask = (int) y - 1; | |
400 if (target.arch.is64bit()) { | |
401 Convert l2i = new Convert(L2I, x, CiKind.Int); | |
402 addInstr(l2i); | |
403 return setCanonical(new LogicOp(IAND, l2i, intInstr(mask))); | |
404 } | |
405 return setCanonical(new LogicOp(CiKind.Int, IAND, x, intInstr(mask))); | |
406 } | |
407 break; | |
408 } | |
409 case WREM: { | |
410 if (y == 1) { | |
411 return setCanonical(wordInstr(0)); | |
412 } | |
413 if (CiUtil.isPowerOf2(y)) { | |
414 if (target.arch.is64bit()) { | |
415 long mask = y - 1L; | |
416 return setCanonical(new LogicOp(LAND, x, longInstr(mask))); | |
417 } | |
418 int mask = (int) y - 1; | |
419 return setCanonical(new LogicOp(IAND, x, intInstr(mask))); | |
420 } | |
421 break; | |
422 } | |
423 } | |
424 return null; | |
425 } | |
426 | |
427 private boolean inCurrentBlock(Value x) { | |
428 if (x instanceof Instruction) { | |
429 Instruction i = (Instruction) x; | |
430 int max = 4; // XXX: anything special about 4? seems like a tunable heuristic | |
431 while (max > 0 && i != null && !(i instanceof BlockEnd)) { | |
432 i = i.next(); | |
433 max--; | |
434 } | |
435 return i == null; | |
436 } | |
437 return true; | |
438 } | |
439 | |
440 private Value eliminateNarrowing(CiKind kind, Convert c) { | |
441 Value nv = null; | |
442 switch (c.opcode) { | |
443 case I2B: | |
444 if (kind == CiKind.Byte) { | |
445 nv = c.value(); | |
446 } | |
447 break; | |
448 case I2S: | |
449 if (kind == CiKind.Short || kind == CiKind.Byte) { | |
450 nv = c.value(); | |
451 } | |
452 break; | |
453 case I2C: | |
454 if (kind == CiKind.Char || kind == CiKind.Byte) { | |
455 nv = c.value(); | |
456 } | |
457 break; | |
458 } | |
459 return nv; | |
460 } | |
461 | |
462 @Override | |
463 public void visitLoadField(LoadField i) { | |
464 if (!i.isLoaded() || !C1XOptions.CanonicalizeConstantFields) { | |
465 return; | |
466 } | |
467 if (i.isStatic()) { | |
468 RiField field = i.field(); | |
469 CiConstant value = field.constantValue(null); | |
470 if (value != null) { | |
471 if (method.isClassInitializer()) { | |
472 // don't do canonicalization in the <clinit> method | |
473 return; | |
474 } | |
475 setConstant(value); | |
476 } | |
477 } else { | |
478 RiField field = i.field(); | |
479 if (i.object().isConstant()) { | |
480 CiConstant value = field.constantValue(i.object().asConstant()); | |
481 if (value != null) { | |
482 setConstant(value); | |
483 } | |
484 } | |
485 } | |
486 } | |
487 | |
488 @Override | |
489 public void visitStoreField(StoreField i) { | |
490 if (C1XOptions.CanonicalizeNarrowingInStores) { | |
491 // Eliminate narrowing conversions emitted by javac which are unnecessary when | |
492 // writing the value to a field that is packed | |
493 Value v = i.value(); | |
494 if (v instanceof Convert) { | |
495 Value nv = eliminateNarrowing(i.field().kind(), (Convert) v); | |
496 // limit this optimization to the current basic block | |
497 // XXX: why is this limited to the current block? | |
498 if (nv != null && inCurrentBlock(v)) { | |
499 setCanonical(new StoreField(i.object(), i.field(), nv, i.isStatic(), | |
500 i.stateBefore(), i.isLoaded())); | |
501 } | |
502 } | |
503 } | |
504 } | |
505 | |
506 @Override | |
507 public void visitArrayLength(ArrayLength i) { | |
508 // we can compute the length of the array statically if the object | |
509 // is a NewArray of a constant, or if the object is a constant reference | |
510 // (either by itself or loaded from a constant value field) | |
511 Value array = i.array(); | |
512 if (array instanceof NewArray) { | |
513 // the array is a NewArray; check if it has a constant length | |
514 NewArray newArray = (NewArray) array; | |
515 Value length = newArray.length(); | |
516 if (length instanceof Constant) { | |
517 // note that we don't use the Constant instruction itself | |
518 // as that would cause problems with liveness later | |
519 int actualLength = length.asConstant().asInt(); | |
520 setIntConstant(actualLength); | |
521 } | |
522 } else if (array instanceof LoadField) { | |
523 // the array is a load of a field; check if it is a constant | |
524 LoadField load = (LoadField) array; | |
525 CiConstant cons = load.constantValue(); | |
526 if (cons != null && cons.isNonNull()) { | |
527 setIntConstant(runtime.getArrayLength(cons)); | |
528 } | |
529 } else if (array.isConstant()) { | |
530 // the array itself is a constant object reference | |
531 CiConstant obj = array.asConstant(); | |
532 if (obj.isNonNull()) { | |
533 setIntConstant(runtime.getArrayLength(obj)); | |
534 } | |
535 } | |
536 } | |
537 | |
538 @Override | |
539 public void visitStoreIndexed(StoreIndexed i) { | |
540 Value array = i.array(); | |
541 Value value = i.value(); | |
542 if (C1XOptions.CanonicalizeNarrowingInStores) { | |
543 // Eliminate narrowing conversions emitted by javac which are unnecessary when | |
544 // writing the value to an array (which is packed) | |
545 Value v = value; | |
546 if (v instanceof Convert) { | |
547 Value nv = eliminateNarrowing(i.elementKind(), (Convert) v); | |
548 if (nv != null && inCurrentBlock(v)) { | |
549 setCanonical(new StoreIndexed(array, i.index(), i.length(), i.elementKind(), nv, i.stateBefore())); | |
550 } | |
551 } | |
552 } | |
553 if (C1XOptions.CanonicalizeArrayStoreChecks && i.elementKind() == CiKind.Object) { | |
554 if (value.isNullConstant()) { | |
555 i.eliminateStoreCheck(); | |
556 } else { | |
557 RiType exactType = array.exactType(); | |
558 if (exactType != null && exactType.isResolved()) { | |
559 if (exactType.componentType().superType() == null) { | |
560 // the exact type of the array is Object[] => no check is necessary | |
561 i.eliminateStoreCheck(); | |
562 } else { | |
563 RiType declaredType = value.declaredType(); | |
564 if (declaredType != null && declaredType.isResolved() && declaredType.isSubtypeOf(exactType.componentType())) { | |
565 // the value being stored has a known type | |
566 i.eliminateStoreCheck(); | |
567 } | |
568 } | |
569 } | |
570 } | |
571 } | |
572 if (i.index().isConstant() && i.length() != null && i.length().isConstant()) { | |
573 int index = i.index().asConstant().asInt(); | |
574 if (index >= 0 && index < i.length().asConstant().asInt()) { | |
575 i.eliminateBoundsCheck(); | |
576 } | |
577 } | |
578 } | |
579 | |
580 @Override | |
581 public void visitLoadIndexed(LoadIndexed i) { | |
582 if (i.index().isConstant() && i.length() != null && i.length().isConstant()) { | |
583 int index = i.index().asConstant().asInt(); | |
584 if (index >= 0 && index < i.length().asConstant().asInt()) { | |
585 i.eliminateBoundsCheck(); | |
586 } | |
587 } | |
588 } | |
589 | |
590 @Override | |
591 public void visitNegateOp(NegateOp i) { | |
592 CiKind vt = i.x().kind; | |
593 Value v = i.x(); | |
594 if (i.x().isConstant()) { | |
595 switch (vt) { | |
596 case Int: setIntConstant(-v.asConstant().asInt()); break; | |
597 case Long: setLongConstant(-v.asConstant().asLong()); break; | |
598 case Float: setFloatConstant(-v.asConstant().asFloat()); break; | |
599 case Double: setDoubleConstant(-v.asConstant().asDouble()); break; | |
600 } | |
601 } | |
602 assert vt == canonical.kind; | |
603 } | |
604 | |
605 @Override | |
606 public void visitArithmeticOp(ArithmeticOp i) { | |
607 visitOp2(i); | |
608 } | |
609 | |
610 @Override | |
611 public void visitShiftOp(ShiftOp i) { | |
612 visitOp2(i); | |
613 } | |
614 | |
615 @Override | |
616 public void visitLogicOp(LogicOp i) { | |
617 visitOp2(i); | |
618 } | |
619 | |
620 @Override | |
621 public void visitCompareOp(CompareOp i) { | |
622 if (i.kind.isVoid()) { | |
623 return; | |
624 } | |
625 // we can reduce a compare op if the two inputs are the same, | |
626 // or if both are constants | |
627 Value x = i.x(); | |
628 Value y = i.y(); | |
629 CiKind xt = x.kind; | |
630 if (x == y) { | |
631 // x and y are generated by the same instruction | |
632 switch (xt) { | |
633 case Long: setIntConstant(0); return; | |
634 case Float: | |
635 if (x.isConstant()) { | |
636 float xval = x.asConstant().asFloat(); // get the actual value of x (and y since x == y) | |
637 Integer val = foldFloatCompare(i.opcode, xval, xval); | |
638 assert val != null : "invalid opcode in float compare op"; | |
639 setIntConstant(val); | |
640 return; | |
641 } | |
642 break; | |
643 case Double: | |
644 if (x.isConstant()) { | |
645 double xval = x.asConstant().asDouble(); // get the actual value of x (and y since x == y) | |
646 Integer val = foldDoubleCompare(i.opcode, xval, xval); | |
647 assert val != null : "invalid opcode in double compare op"; | |
648 setIntConstant(val); | |
649 return; | |
650 } | |
651 break; | |
652 // note that there are no integer CompareOps | |
653 } | |
654 } | |
655 if (x.isConstant() && y.isConstant()) { | |
656 // both x and y are constants | |
657 switch (xt) { | |
658 case Long: | |
659 setIntConstant(foldLongCompare(x.asConstant().asLong(), y.asConstant().asLong())); | |
660 break; | |
661 case Float: { | |
662 Integer val = foldFloatCompare(i.opcode, x.asConstant().asFloat(), y.asConstant().asFloat()); | |
663 assert val != null : "invalid opcode in float compare op"; | |
664 setIntConstant(val); | |
665 break; | |
666 } | |
667 case Double: { | |
668 Integer val = foldDoubleCompare(i.opcode, x.asConstant().asDouble(), y.asConstant().asDouble()); | |
669 assert val != null : "invalid opcode in float compare op"; | |
670 setIntConstant(val); | |
671 break; | |
672 } | |
673 } | |
674 } | |
675 assert Util.archKindsEqual(i, canonical); | |
676 } | |
677 | |
678 @Override | |
679 public void visitIfOp(IfOp i) { | |
680 moveConstantToRight(i); | |
681 } | |
682 | |
683 @Override | |
684 public void visitConvert(Convert i) { | |
685 Value v = i.value(); | |
686 if (v.isConstant()) { | |
687 // fold conversions between primitive types | |
688 // Checkstyle: stop | |
689 switch (i.opcode) { | |
690 case I2B: setIntConstant ((byte) v.asConstant().asInt()); return; | |
691 case I2S: setIntConstant ((short) v.asConstant().asInt()); return; | |
692 case I2C: setIntConstant ((char) v.asConstant().asInt()); return; | |
693 case I2L: setLongConstant ( v.asConstant().asInt()); return; | |
694 case I2F: setFloatConstant ( v.asConstant().asInt()); return; | |
695 case L2I: setIntConstant ((int) v.asConstant().asLong()); return; | |
696 case L2F: setFloatConstant ( v.asConstant().asLong()); return; | |
697 case L2D: setDoubleConstant( v.asConstant().asLong()); return; | |
698 case F2D: setDoubleConstant( v.asConstant().asFloat()); return; | |
699 case F2I: setIntConstant ((int) v.asConstant().asFloat()); return; | |
700 case F2L: setLongConstant ((long) v.asConstant().asFloat()); return; | |
701 case D2F: setFloatConstant ((float) v.asConstant().asDouble()); return; | |
702 case D2I: setIntConstant ((int) v.asConstant().asDouble()); return; | |
703 case D2L: setLongConstant ((long) v.asConstant().asDouble()); return; | |
704 } | |
705 // Checkstyle: resume | |
706 } | |
707 | |
708 CiKind kind = CiKind.Illegal; | |
709 if (v instanceof LoadField) { | |
710 // remove redundant conversions from field loads of the correct type | |
711 kind = ((LoadField) v).field().kind(); | |
712 } else if (v instanceof LoadIndexed) { | |
713 // remove redundant conversions from array loads of the correct type | |
714 kind = ((LoadIndexed) v).elementKind(); | |
715 } else if (v instanceof Convert) { | |
716 // remove chained redundant conversions | |
717 Convert c = (Convert) v; | |
718 switch (c.opcode) { | |
719 case I2B: kind = CiKind.Byte; break; | |
720 case I2S: kind = CiKind.Short; break; | |
721 case I2C: kind = CiKind.Char; break; | |
722 } | |
723 } | |
724 | |
725 if (kind != CiKind.Illegal) { | |
726 // if any of the above matched | |
727 switch (i.opcode) { | |
728 case I2B: | |
729 if (kind == CiKind.Byte) { | |
730 setCanonical(v); | |
731 } | |
732 break; | |
733 case I2S: | |
734 if (kind == CiKind.Byte || kind == CiKind.Short) { | |
735 setCanonical(v); | |
736 } | |
737 break; | |
738 case I2C: | |
739 if (kind == CiKind.Char) { | |
740 setCanonical(v); | |
741 } | |
742 break; | |
743 } | |
744 } | |
745 | |
746 if (v instanceof Op2) { | |
747 // check if the operation was IAND with a constant; it may have narrowed the value already | |
748 Op2 op = (Op2) v; | |
749 // constant should be on right hand side if there is one | |
750 if (op.opcode == IAND && op.y().isConstant()) { | |
751 int safebits = 0; | |
752 int mask = op.y().asConstant().asInt(); | |
753 switch (i.opcode) { | |
754 case I2B: safebits = 0x7f; break; | |
755 case I2S: safebits = 0x7fff; break; | |
756 case I2C: safebits = 0xffff; break; | |
757 } | |
758 if (safebits != 0 && (mask & ~safebits) == 0) { | |
759 // the mask already cleared all the upper bits necessary. | |
760 setCanonical(v); | |
761 } | |
762 } | |
763 } | |
764 } | |
765 | |
766 @Override | |
767 public void visitNullCheck(NullCheck i) { | |
768 Value o = i.object(); | |
769 if (o.isNonNull()) { | |
770 // if the instruction producing the object was a new, no check is necessary | |
771 setCanonical(o); | |
772 } else if (o.isConstant()) { | |
773 // if the object is a constant, check if it is nonnull | |
774 CiConstant c = o.asConstant(); | |
775 if (c.kind.isObject() && !c.isNull()) { | |
776 setCanonical(o); | |
777 } | |
778 } | |
779 } | |
780 | |
781 @Override | |
782 public void visitInvoke(Invoke i) { | |
783 if (C1XOptions.CanonicalizeFoldableMethods) { | |
784 RiMethod method = i.target(); | |
785 if (method.isResolved()) { | |
786 // only try to fold resolved method invocations | |
787 CiConstant result = foldInvocation(runtime, i.target(), i.arguments()); | |
788 if (result != null) { | |
789 // folding was successful | |
790 setCanonical(new Constant(result)); | |
791 } | |
792 } | |
793 } | |
794 } | |
795 | |
796 @Override | |
797 public void visitCheckCast(CheckCast i) { | |
798 // we can remove a redundant check cast if it is an object constant or the exact type is known | |
799 if (i.targetClass().isResolved()) { | |
800 Value o = i.object(); | |
801 RiType type = o.exactType(); | |
802 if (type == null) { | |
803 type = o.declaredType(); | |
804 } | |
805 if (type != null && type.isResolved() && type.isSubtypeOf(i.targetClass())) { | |
806 // cast is redundant if exact type or declared type is already a subtype of the target type | |
807 setCanonical(o); | |
808 } | |
809 if (o.isConstant()) { | |
810 final CiConstant obj = o.asConstant(); | |
811 if (obj.isNull()) { | |
812 // checkcast of null is null | |
813 setCanonical(o); | |
814 } else if (C1XOptions.CanonicalizeObjectCheckCast) { | |
815 if (i.targetClass().isInstance(obj)) { | |
816 // fold the cast if it will succeed | |
817 setCanonical(o); | |
818 } | |
819 } | |
820 } | |
821 } | |
822 } | |
823 | |
824 @Override | |
825 public void visitInstanceOf(InstanceOf i) { | |
826 // we can fold an instanceof if it is an object constant or the exact type is known | |
827 if (i.targetClass().isResolved()) { | |
828 Value o = i.object(); | |
829 RiType exact = o.exactType(); | |
830 if (exact != null && exact.isResolved() && o.isNonNull()) { | |
831 setIntConstant(exact.isSubtypeOf(i.targetClass()) ? 1 : 0); | |
832 } else if (o.isConstant()) { | |
833 final CiConstant obj = o.asConstant(); | |
834 if (obj.isNull()) { | |
835 // instanceof of null is false | |
836 setIntConstant(0); | |
837 } else if (C1XOptions.CanonicalizeObjectInstanceOf) { | |
838 // fold the instanceof test | |
839 setIntConstant(i.targetClass().isInstance(obj) ? 1 : 0); | |
840 } | |
841 } | |
842 } | |
843 } | |
844 | |
845 @Override | |
846 public void visitIntrinsic(Intrinsic i) { | |
847 if (!C1XOptions.CanonicalizeIntrinsics) { | |
848 return; | |
849 } | |
850 if (!foldIntrinsic(i)) { | |
851 // folding did not work, try recognizing special intrinsics | |
852 reduceIntrinsic(i); | |
853 } | |
854 assert Util.archKindsEqual(i, canonical); | |
855 } | |
856 | |
857 private void reduceIntrinsic(Intrinsic i) { | |
858 Value[] args = i.arguments(); | |
859 C1XIntrinsic intrinsic = i.intrinsic(); | |
860 if (intrinsic == C1XIntrinsic.java_lang_Class$isInstance) { | |
861 // try to convert a call to Class.isInstance() into an InstanceOf | |
862 RiType type = getTypeOf(args[0]); | |
863 if (type != null) { | |
864 setCanonical(new InstanceOf(type, Constant.forObject(type.getEncoding(RiType.Representation.TypeInfo)), args[1], i.stateBefore())); | |
865 return; | |
866 } | |
867 } | |
868 if (intrinsic == C1XIntrinsic.java_lang_reflect_Array$newArray) { | |
869 // try to convert a call to Array.newInstance() into a NewObjectArray or NewTypeArray | |
870 RiType type = getTypeOf(args[0]); | |
871 if (type != null) { | |
872 if (type.kind() == CiKind.Object) { | |
873 setCanonical(new NewObjectArray(type, args[1], i.stateBefore())); | |
874 } else { | |
875 RiType elementType = runtime.asRiType(type.kind()); | |
876 setCanonical(new NewTypeArray(args[1], elementType, i.stateBefore())); | |
877 } | |
878 return; | |
879 } | |
880 } | |
881 assert Util.archKindsEqual(i, canonical); | |
882 } | |
883 | |
884 private boolean foldIntrinsic(Intrinsic i) { | |
885 Value[] args = i.arguments(); | |
886 for (Value arg : args) { | |
887 if (arg != null && !arg.isConstant()) { | |
888 // one input is not constant, give up | |
889 return true; | |
890 } | |
891 } | |
892 switch (i.intrinsic()) { | |
893 // do not use reflection here due to efficiency and potential bootstrap problems | |
894 case java_lang_Object$hashCode: { | |
895 Object object = argAsObject(args, 0); | |
896 if (object != null) { | |
897 setIntConstant(System.identityHashCode(object)); | |
898 } | |
899 return true; | |
900 } | |
901 case java_lang_Object$getClass: { | |
902 Object object = argAsObject(args, 0); | |
903 if (object != null) { | |
904 setObjectConstant(object.getClass()); | |
905 } | |
906 return true; | |
907 } | |
908 | |
909 // java.lang.Class | |
910 case java_lang_Class$isAssignableFrom: { | |
911 Class<?> javaClass = argAsClass(args, 0); | |
912 Class<?> otherClass = argAsClass(args, 1); | |
913 if (javaClass != null && otherClass != null) { | |
914 setBooleanConstant(javaClass.isAssignableFrom(otherClass)); | |
915 } | |
916 return true; | |
917 } | |
918 case java_lang_Class$isInstance: { | |
919 Class<?> javaClass = argAsClass(args, 0); | |
920 Object object = argAsObject(args, 1); | |
921 if (javaClass != null && object != null) { | |
922 setBooleanConstant(javaClass.isInstance(object)); | |
923 } | |
924 return true; | |
925 } | |
926 case java_lang_Class$getModifiers: { | |
927 Class<?> javaClass = argAsClass(args, 0); | |
928 if (javaClass != null) { | |
929 setIntConstant(javaClass.getModifiers()); | |
930 } | |
931 return true; | |
932 } | |
933 case java_lang_Class$isInterface: { | |
934 Class<?> javaClass = argAsClass(args, 0); | |
935 if (javaClass != null) { | |
936 setBooleanConstant(javaClass.isInterface()); | |
937 } | |
938 return true; | |
939 } | |
940 case java_lang_Class$isArray: { | |
941 Class<?> javaClass = argAsClass(args, 0); | |
942 if (javaClass != null) { | |
943 setBooleanConstant(javaClass.isArray()); | |
944 } | |
945 return true; | |
946 } | |
947 case java_lang_Class$isPrimitive: { | |
948 Class<?> javaClass = argAsClass(args, 0); | |
949 if (javaClass != null) { | |
950 setBooleanConstant(javaClass.isPrimitive()); | |
951 } | |
952 return true; | |
953 } | |
954 case java_lang_Class$getSuperclass: { | |
955 Class<?> javaClass = argAsClass(args, 0); | |
956 if (javaClass != null) { | |
957 setObjectConstant(javaClass.getSuperclass()); | |
958 } | |
959 return true; | |
960 } | |
961 case java_lang_Class$getComponentType: { | |
962 Class<?> javaClass = argAsClass(args, 0); | |
963 if (javaClass != null) { | |
964 setObjectConstant(javaClass.getComponentType()); | |
965 } | |
966 return true; | |
967 } | |
968 | |
969 // java.lang.Math | |
970 case java_lang_Math$abs: setDoubleConstant(Math.abs(argAsDouble(args, 0))); return true; | |
971 case java_lang_Math$sin: setDoubleConstant(Math.sin(argAsDouble(args, 0))); return true; | |
972 case java_lang_Math$cos: setDoubleConstant(Math.cos(argAsDouble(args, 0))); return true; | |
973 case java_lang_Math$tan: setDoubleConstant(Math.tan(argAsDouble(args, 0))); return true; | |
974 case java_lang_Math$atan2: setDoubleConstant(Math.atan2(argAsDouble(args, 0), argAsDouble(args, 2))); return true; | |
975 case java_lang_Math$sqrt: setDoubleConstant(Math.sqrt(argAsDouble(args, 0))); return true; | |
976 case java_lang_Math$log: setDoubleConstant(Math.log(argAsDouble(args, 0))); return true; | |
977 case java_lang_Math$log10: setDoubleConstant(Math.log10(argAsDouble(args, 0))); return true; | |
978 case java_lang_Math$pow: setDoubleConstant(Math.pow(argAsDouble(args, 0), argAsDouble(args, 2))); return true; | |
979 case java_lang_Math$exp: setDoubleConstant(Math.exp(argAsDouble(args, 0))); return true; | |
980 case java_lang_Math$min: setIntConstant(Math.min(argAsInt(args, 0), argAsInt(args, 1))); return true; | |
981 case java_lang_Math$max: setIntConstant(Math.max(argAsInt(args, 0), argAsInt(args, 1))); return true; | |
982 | |
983 // java.lang.Float | |
984 case java_lang_Float$floatToRawIntBits: setIntConstant(Float.floatToRawIntBits(argAsFloat(args, 0))); return true; | |
985 case java_lang_Float$floatToIntBits: setIntConstant(Float.floatToIntBits(argAsFloat(args, 0))); return true; | |
986 case java_lang_Float$intBitsToFloat: setFloatConstant(Float.intBitsToFloat(argAsInt(args, 0))); return true; | |
987 | |
988 // java.lang.Double | |
989 case java_lang_Double$doubleToRawLongBits: setLongConstant(Double.doubleToRawLongBits(argAsDouble(args, 0))); return true; | |
990 case java_lang_Double$doubleToLongBits: setLongConstant(Double.doubleToLongBits(argAsDouble(args, 0))); return true; | |
991 case java_lang_Double$longBitsToDouble: setDoubleConstant(Double.longBitsToDouble(argAsLong(args, 0))); return true; | |
992 | |
993 // java.lang.Integer | |
994 case java_lang_Integer$bitCount: setIntConstant(Integer.bitCount(argAsInt(args, 0))); return true; | |
995 case java_lang_Integer$reverseBytes: setIntConstant(Integer.reverseBytes(argAsInt(args, 0))); return true; | |
996 | |
997 // java.lang.Long | |
998 case java_lang_Long$bitCount: setIntConstant(Long.bitCount(argAsLong(args, 0))); return true; | |
999 case java_lang_Long$reverseBytes: setLongConstant(Long.reverseBytes(argAsLong(args, 0))); return true; | |
1000 | |
1001 // java.lang.System | |
1002 case java_lang_System$identityHashCode: { | |
1003 Object object = argAsObject(args, 0); | |
1004 if (object != null) { | |
1005 setIntConstant(System.identityHashCode(object)); | |
1006 } | |
1007 return true; | |
1008 } | |
1009 | |
1010 // java.lang.reflect.Array | |
1011 case java_lang_reflect_Array$getLength: { | |
1012 Object object = argAsObject(args, 0); | |
1013 if (object != null && object.getClass().isArray()) { | |
1014 setIntConstant(Array.getLength(object)); | |
1015 } | |
1016 return true; | |
1017 } | |
1018 } | |
1019 return false; | |
1020 } | |
1021 | |
1022 @Override | |
1023 public void visitIf(If i) { | |
1024 if (i.x().isConstant()) { | |
1025 // move constant to the right | |
1026 i.swapOperands(); | |
1027 } | |
1028 Value l = i.x(); | |
1029 Value r = i.y(); | |
1030 | |
1031 if (l == r && !l.kind.isFloatOrDouble()) { | |
1032 // this is a comparison of x op x | |
1033 // No opt for float/double due to NaN case | |
1034 reduceReflexiveIf(i); | |
1035 return; | |
1036 } | |
1037 | |
1038 CiKind rt = r.kind; | |
1039 | |
1040 Condition ifcond = i.condition(); | |
1041 if (l.isConstant() && r.isConstant()) { | |
1042 // fold comparisons between constants and convert to Goto | |
1043 Boolean result = ifcond.foldCondition(l.asConstant(), r.asConstant(), runtime); | |
1044 if (result != null) { | |
1045 setCanonical(new Goto(i.successor(result), i.stateAfter(), i.isSafepoint())); | |
1046 return; | |
1047 } | |
1048 } | |
1049 | |
1050 if (r.isConstant() && rt.isInt()) { | |
1051 // attempt to reduce comparisons with constant on right side | |
1052 if (l instanceof CompareOp) { | |
1053 // attempt to reduce If ((a cmp b) op const) | |
1054 reduceIfCompareOpConstant(i, r.asConstant()); | |
1055 } | |
1056 } | |
1057 | |
1058 if (isNullConstant(r) && l.isNonNull()) { | |
1059 // this is a comparison of null against something that is not null | |
1060 if (ifcond == Condition.EQ) { | |
1061 // new() == null is always false | |
1062 setCanonical(new Goto(i.falseSuccessor(), i.stateAfter(), i.isSafepoint())); | |
1063 } else if (ifcond == Condition.NE) { | |
1064 // new() != null is always true | |
1065 setCanonical(new Goto(i.trueSuccessor(), i.stateAfter(), i.isSafepoint())); | |
1066 } | |
1067 } | |
1068 } | |
1069 | |
1070 private boolean isNullConstant(Value r) { | |
1071 return r.isConstant() && r.asConstant().isNull(); | |
1072 } | |
1073 | |
1074 private void reduceIfCompareOpConstant(If i, CiConstant rtc) { | |
1075 Condition ifcond = i.condition(); | |
1076 Value l = i.x(); | |
1077 CompareOp cmp = (CompareOp) l; | |
1078 boolean unorderedIsLess = cmp.opcode == FCMPL || cmp.opcode == DCMPL; | |
1079 BlockBegin lssSucc = i.successor(ifcond.foldCondition(CiConstant.forInt(-1), rtc, runtime)); | |
1080 BlockBegin eqlSucc = i.successor(ifcond.foldCondition(CiConstant.forInt(0), rtc, runtime)); | |
1081 BlockBegin gtrSucc = i.successor(ifcond.foldCondition(CiConstant.forInt(1), rtc, runtime)); | |
1082 BlockBegin nanSucc = unorderedIsLess ? lssSucc : gtrSucc; | |
1083 // Note: At this point all successors (lssSucc, eqlSucc, gtrSucc, nanSucc) are | |
1084 // equal to x->tsux() or x->fsux(). Furthermore, nanSucc equals either | |
1085 // lssSucc or gtrSucc. | |
1086 if (lssSucc == eqlSucc && eqlSucc == gtrSucc) { | |
1087 // all successors identical => simplify to: Goto | |
1088 setCanonical(new Goto(lssSucc, i.stateAfter(), i.isSafepoint())); | |
1089 } else { | |
1090 // two successors differ and two successors are the same => simplify to: If (x cmp y) | |
1091 // determine new condition & successors | |
1092 Condition cond; | |
1093 BlockBegin tsux; | |
1094 BlockBegin fsux; | |
1095 if (lssSucc == eqlSucc) { | |
1096 cond = Condition.LE; | |
1097 tsux = lssSucc; | |
1098 fsux = gtrSucc; | |
1099 } else if (lssSucc == gtrSucc) { | |
1100 cond = Condition.NE; | |
1101 tsux = lssSucc; | |
1102 fsux = eqlSucc; | |
1103 } else if (eqlSucc == gtrSucc) { | |
1104 cond = Condition.GE; | |
1105 tsux = eqlSucc; | |
1106 fsux = lssSucc; | |
1107 } else { | |
1108 throw Util.shouldNotReachHere(); | |
1109 } | |
1110 // TODO: the state after is incorrect here: should it be preserved from the original if? | |
1111 If canon = new If(cmp.x(), cond, nanSucc == tsux, cmp.y(), tsux, fsux, cmp.stateBefore(), i.isSafepoint()); | |
1112 if (cmp.x() == cmp.y()) { | |
1113 // re-canonicalize the new if | |
1114 visitIf(canon); | |
1115 } else { | |
1116 setCanonical(canon); | |
1117 } | |
1118 } | |
1119 } | |
1120 | |
1121 private void reduceReflexiveIf(If i) { | |
1122 // simplify reflexive comparisons If (x op x) to Goto | |
1123 BlockBegin succ; | |
1124 switch (i.condition()) { | |
1125 case EQ: succ = i.successor(true); break; | |
1126 case NE: succ = i.successor(false); break; | |
1127 case LT: succ = i.successor(false); break; | |
1128 case LE: succ = i.successor(true); break; | |
1129 case GT: succ = i.successor(false); break; | |
1130 case GE: succ = i.successor(true); break; | |
1131 default: | |
1132 throw Util.shouldNotReachHere(); | |
1133 } | |
1134 setCanonical(new Goto(succ, i.stateAfter(), i.isSafepoint())); | |
1135 } | |
1136 | |
1137 @Override | |
1138 public void visitTableSwitch(TableSwitch i) { | |
1139 Value v = i.value(); | |
1140 if (v.isConstant()) { | |
1141 // fold a table switch over a constant by replacing it with a goto | |
1142 int val = v.asConstant().asInt(); | |
1143 BlockBegin succ = i.defaultSuccessor(); | |
1144 if (val >= i.lowKey() && val <= i.highKey()) { | |
1145 succ = i.successors().get(val - i.lowKey()); | |
1146 } | |
1147 setCanonical(new Goto(succ, i.stateAfter(), i.isSafepoint())); | |
1148 return; | |
1149 } | |
1150 int max = i.numberOfCases(); | |
1151 if (max == 0) { | |
1152 // replace switch with Goto | |
1153 if (v instanceof Instruction) { | |
1154 // TODO: is it necessary to add the instruction explicitly? | |
1155 addInstr((Instruction) v); | |
1156 } | |
1157 setCanonical(new Goto(i.defaultSuccessor(), i.stateAfter(), i.isSafepoint())); | |
1158 return; | |
1159 } | |
1160 if (max == 1) { | |
1161 // replace switch with If | |
1162 Constant key = intInstr(i.lowKey()); | |
1163 If newIf = new If(v, Condition.EQ, false, key, i.successors().get(0), i.defaultSuccessor(), null, i.isSafepoint()); | |
1164 newIf.setStateAfter(i.stateAfter()); | |
1165 setCanonical(newIf); | |
1166 } | |
1167 } | |
1168 | |
1169 @Override | |
1170 public void visitLookupSwitch(LookupSwitch i) { | |
1171 Value v = i.value(); | |
1172 if (v.isConstant()) { | |
1173 // fold a lookup switch over a constant by replacing it with a goto | |
1174 int val = v.asConstant().asInt(); | |
1175 BlockBegin succ = i.defaultSuccessor(); | |
1176 for (int j = 0; j < i.numberOfCases(); j++) { | |
1177 if (val == i.keyAt(j)) { | |
1178 succ = i.successors().get(j); | |
1179 break; | |
1180 } | |
1181 } | |
1182 setCanonical(new Goto(succ, i.stateAfter(), i.isSafepoint())); | |
1183 return; | |
1184 } | |
1185 int max = i.numberOfCases(); | |
1186 if (max == 0) { | |
1187 // replace switch with Goto | |
1188 if (v instanceof Instruction) { | |
1189 addInstr((Instruction) v); // the value expression may produce side effects | |
1190 } | |
1191 setCanonical(new Goto(i.defaultSuccessor(), i.stateAfter(), i.isSafepoint())); | |
1192 return; | |
1193 } | |
1194 if (max == 1) { | |
1195 // replace switch with If | |
1196 Constant key = intInstr(i.keyAt(0)); | |
1197 If newIf = new If(v, Condition.EQ, false, key, i.successors().get(0), i.defaultSuccessor(), null, i.isSafepoint()); | |
1198 newIf.setStateAfter(i.stateAfter()); | |
1199 setCanonical(newIf); | |
1200 } | |
1201 } | |
1202 | |
1203 private void visitUnsafeRawOp(UnsafeRawOp i) { | |
1204 if (i.base() instanceof ArithmeticOp) { | |
1205 // if the base is an arithmetic op, try reducing | |
1206 ArithmeticOp root = (ArithmeticOp) i.base(); | |
1207 if (!root.isLive() && root.opcode == LADD) { | |
1208 // match unsafe(x + y) if the x + y is not pinned | |
1209 // try reducing (x + y) and (y + x) | |
1210 Value y = root.y(); | |
1211 Value x = root.x(); | |
1212 if (reduceRawOp(i, x, y) || reduceRawOp(i, y, x)) { | |
1213 // the operation was reduced | |
1214 return; | |
1215 } | |
1216 if (y instanceof Convert) { | |
1217 // match unsafe(x + (long) y) | |
1218 Convert convert = (Convert) y; | |
1219 if (convert.opcode == I2L && convert.value().kind.isInt()) { | |
1220 // the conversion is redundant | |
1221 setUnsafeRawOp(i, x, convert.value(), 0); | |
1222 } | |
1223 } | |
1224 } | |
1225 } | |
1226 } | |
1227 | |
1228 private boolean reduceRawOp(UnsafeRawOp i, Value base, Value index) { | |
1229 if (index instanceof Convert) { | |
1230 // skip any conversion operations | |
1231 index = ((Convert) index).value(); | |
1232 } | |
1233 if (index instanceof ShiftOp) { | |
1234 // try to match the index as a shift by a constant | |
1235 ShiftOp shift = (ShiftOp) index; | |
1236 CiKind st = shift.y().kind; | |
1237 if (shift.y().isConstant() && st.isInt()) { | |
1238 int val = shift.y().asConstant().asInt(); | |
1239 switch (val) { | |
1240 case 0: // fall through | |
1241 case 1: // fall through | |
1242 case 2: // fall through | |
1243 case 3: return setUnsafeRawOp(i, base, shift.x(), val); | |
1244 } | |
1245 } | |
1246 } | |
1247 if (index instanceof ArithmeticOp) { | |
1248 // try to match the index as a multiply by a constant | |
1249 // note that this case will not happen if C1XOptions.CanonicalizeMultipliesToShifts is true | |
1250 ArithmeticOp arith = (ArithmeticOp) index; | |
1251 CiKind st = arith.y().kind; | |
1252 if (arith.opcode == IMUL && arith.y().isConstant() && st.isInt()) { | |
1253 int val = arith.y().asConstant().asInt(); | |
1254 switch (val) { | |
1255 case 1: return setUnsafeRawOp(i, base, arith.x(), 0); | |
1256 case 2: return setUnsafeRawOp(i, base, arith.x(), 1); | |
1257 case 4: return setUnsafeRawOp(i, base, arith.x(), 2); | |
1258 case 8: return setUnsafeRawOp(i, base, arith.x(), 3); | |
1259 } | |
1260 } | |
1261 } | |
1262 | |
1263 return false; | |
1264 } | |
1265 | |
1266 private boolean setUnsafeRawOp(UnsafeRawOp i, Value base, Value index, int log2scale) { | |
1267 i.setBase(base); | |
1268 i.setIndex(index); | |
1269 i.setLog2Scale(log2scale); | |
1270 return true; | |
1271 } | |
1272 | |
1273 @Override | |
1274 public void visitUnsafeGetRaw(UnsafeGetRaw i) { | |
1275 if (C1XOptions.CanonicalizeUnsafes) { | |
1276 visitUnsafeRawOp(i); | |
1277 } | |
1278 } | |
1279 | |
1280 @Override | |
1281 public void visitUnsafePutRaw(UnsafePutRaw i) { | |
1282 if (C1XOptions.CanonicalizeUnsafes) { | |
1283 visitUnsafeRawOp(i); | |
1284 } | |
1285 } | |
1286 | |
1287 private Object argAsObject(Value[] args, int index) { | |
1288 CiConstant c = args[index].asConstant(); | |
1289 if (c != null) { | |
1290 return runtime.asJavaObject(c); | |
1291 } | |
1292 return null; | |
1293 } | |
1294 | |
1295 private Class<?> argAsClass(Value[] args, int index) { | |
1296 CiConstant c = args[index].asConstant(); | |
1297 if (c != null) { | |
1298 return runtime.asJavaClass(c); | |
1299 } | |
1300 return null; | |
1301 } | |
1302 | |
1303 private double argAsDouble(Value[] args, int index) { | |
1304 return args[index].asConstant().asDouble(); | |
1305 } | |
1306 | |
1307 private float argAsFloat(Value[] args, int index) { | |
1308 return args[index].asConstant().asFloat(); | |
1309 } | |
1310 | |
1311 private int argAsInt(Value[] args, int index) { | |
1312 return args[index].asConstant().asInt(); | |
1313 } | |
1314 | |
1315 private long argAsLong(Value[] args, int index) { | |
1316 return args[index].asConstant().asLong(); | |
1317 } | |
1318 | |
1319 public static CiConstant foldInvocation(RiRuntime runtime, RiMethod method, final Value[] args) { | |
1320 CiConstant result = runtime.invoke(method, new CiMethodInvokeArguments() { | |
1321 int i; | |
1322 @Override | |
1323 public CiConstant nextArg() { | |
1324 if (i >= args.length) { | |
1325 return null; | |
1326 } | |
1327 Value arg = args[i++]; | |
1328 if (arg == null) { | |
1329 if (i >= args.length) { | |
1330 return null; | |
1331 } | |
1332 arg = args[i++]; | |
1333 assert arg != null; | |
1334 } | |
1335 return arg.isConstant() ? arg.asConstant() : null; | |
1336 } | |
1337 }); | |
1338 if (result != null) { | |
1339 C1XMetrics.MethodsFolded++; | |
1340 } | |
1341 return result; | |
1342 } | |
1343 | |
1344 @Override | |
1345 public void visitTypeEqualityCheck(TypeEqualityCheck i) { | |
1346 if (i.condition == Condition.EQ && i.left() == i.right()) { | |
1347 setCanonical(null); | |
1348 } | |
1349 } | |
1350 | |
1351 @Override | |
1352 public void visitBoundsCheck(BoundsCheck b) { | |
1353 Value index = b.index(); | |
1354 Value length = b.length(); | |
1355 | |
1356 if (index.isConstant() && length.isConstant()) { | |
1357 int i = index.asConstant().asInt(); | |
1358 int l = index.asConstant().asInt(); | |
1359 Condition c = b.condition; | |
1360 if (c.check(i, l)) { | |
1361 setCanonical(null); | |
1362 } | |
1363 } | |
1364 } | |
1365 | |
1366 private RiType getTypeOf(Value x) { | |
1367 if (x.isConstant()) { | |
1368 return runtime.getTypeOf(x.asConstant()); | |
1369 } | |
1370 return null; | |
1371 } | |
1372 } |