comparison graal/Compiler/src/com/sun/c1x/ir/IfOp.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.ir;
24
25 import com.sun.c1x.debug.*;
26 import com.sun.c1x.util.*;
27 import com.sun.cri.bytecode.*;
28
29 /**
30 * The {@code IfOp} class represents a comparison that yields one of two values.
31 * Note that these nodes are not built directly from the bytecode but are introduced
32 * by conditional expression elimination.
33 *
34 * @author Ben L. Titzer
35 */
36 public final class IfOp extends Op2 {
37
38 Condition cond;
39 Value trueVal;
40 Value falseVal;
41
42 /**
43 * Constructs a new IfOp.
44 * @param x the instruction producing the first value to be compared
45 * @param cond the condition of the comparison
46 * @param y the instruction producing the second value to be compared
47 * @param tval the value produced if the condition is true
48 * @param fval the value produced if the condition is false
49 */
50 public IfOp(Value x, Condition cond, Value y, Value tval, Value fval) {
51 // TODO: return the appropriate bytecode IF_ICMPEQ, etc
52 super(tval.kind.meet(fval.kind), Bytecodes.ILLEGAL, x, y);
53 this.cond = cond;
54 this.trueVal = tval;
55 falseVal = fval;
56 }
57
58 /**
59 * Gets the condition of this if operation.
60 * @return the condition
61 */
62 public Condition condition() {
63 return cond;
64 }
65
66 /**
67 * Gets the instruction that produces the value if the comparison is true.
68 * @return the instruction producing the value upon true
69 */
70 public Value trueValue() {
71 return trueVal;
72 }
73
74 /**
75 * Gets the instruction that produces the value if the comparison is false.
76 * @return the instruction producing the value upon false
77 */
78 public Value falseValue() {
79 return falseVal;
80 }
81
82 /**
83 * Checks whether this comparison operator is commutative (i.e. it is either == or !=).
84 * @return {@code true} if this comparison is commutative
85 */
86 public boolean isCommutative() {
87 return cond == Condition.EQ || cond == Condition.NE;
88 }
89
90 @Override
91 public void inputValuesDo(ValueClosure closure) {
92 super.inputValuesDo(closure);
93 trueVal = closure.apply(trueVal);
94 falseVal = closure.apply(falseVal);
95 }
96
97 @Override
98 public void accept(ValueVisitor v) {
99 v.visitIfOp(this);
100 }
101
102 @Override
103 public int valueNumber() {
104 return Util.hash4(cond.hashCode(), x, y, trueVal, falseVal);
105 }
106
107 @Override
108 public boolean valueEqual(Instruction i) {
109 if (i instanceof IfOp) {
110 IfOp o = (IfOp) i;
111 return opcode == o.opcode && x == o.x && y == o.y && trueVal == o.trueVal && falseVal == o.falseVal;
112 }
113 return false;
114 }
115
116 @Override
117 public void print(LogStream out) {
118 out.print(x()).
119 print(' ').
120 print(condition().operator).
121 print(' ').
122 print(y()).
123 print(" ? ").
124 print(trueValue()).
125 print(" : ").
126 print(falseValue());
127 }
128 }