annotate graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/ir/Compare.java @ 3032:2433838f0414

Create null checks as guard nodes.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Mon, 20 Jun 2011 15:14:36 +0200
parents 80d27a8a022b
children 222f60eb09a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2861
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
1 /*
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
2 * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
4 *
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
7 * published by the Free Software Foundation.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
8 *
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
13 * accompanied this code).
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
14 *
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
18 *
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
21 * questions.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
22 */
2874
d90bf514d647 Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents: 2872
diff changeset
23 package com.oracle.max.graal.compiler.ir;
2861
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
24
2874
d90bf514d647 Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents: 2872
diff changeset
25 import com.oracle.max.graal.compiler.debug.*;
d90bf514d647 Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents: 2872
diff changeset
26 import com.oracle.max.graal.compiler.util.*;
d90bf514d647 Renamed packages.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents: 2872
diff changeset
27 import com.oracle.max.graal.graph.*;
2861
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
28 import com.sun.cri.ci.*;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
29
2899
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
30 /* (tw/gd) For high-level optimization purpose the compare node should be a boolean *value* (it is currently only a helper node)
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
31 * But in the back-end the comparison should not always be materialized (for example in x86 the comparison result will not be in a register but in a flag)
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
32 *
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
33 * Compare should probably be made a value (so that it can be canonicalized for example) and in later stages some Compare usage should be transformed
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
34 * into variants that do not materialize the value (CompareIf, CompareGuard...)
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
35 *
80d27a8a022b Fix on canonicalization plus canonicalization of shifts and integer arithmetics
Gilles Duboscq <gilles.duboscq@oracle.com>
parents: 2877
diff changeset
36 */
3032
2433838f0414 Create null checks as guard nodes.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents: 2899
diff changeset
37 public final class Compare extends BooleanNode {
2861
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
38
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
39 private static final int INPUT_COUNT = 2;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
40 private static final int INPUT_X = 0;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
41 private static final int INPUT_Y = 1;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
42
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
43 private static final int SUCCESSOR_COUNT = 0;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
44
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
45 @Override
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
46 protected int inputCount() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
47 return super.inputCount() + INPUT_COUNT;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
48 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
49
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
50 @Override
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
51 protected int successorCount() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
52 return super.successorCount() + SUCCESSOR_COUNT;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
53 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
54
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
55 /**
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
56 * The instruction that produces the first input to this comparison.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
57 */
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
58 public Value x() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
59 return (Value) inputs().get(super.inputCount() + INPUT_X);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
60 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
61
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
62 public Value setX(Value n) {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
63 return (Value) inputs().set(super.inputCount() + INPUT_X, n);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
64 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
65
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
66 /**
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
67 * The instruction that produces the second input to this comparison.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
68 */
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
69 public Value y() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
70 return (Value) inputs().get(super.inputCount() + INPUT_Y);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
71 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
72
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
73 public Value setY(Value n) {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
74 return (Value) inputs().set(super.inputCount() + INPUT_Y, n);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
75 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
76
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
77 Condition condition;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
78 boolean unorderedIsTrue;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
79
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
80 /**
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
81 * Constructs a new If instruction.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
82 * @param x the instruction producing the first input to the instruction
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
83 * @param condition the condition (comparison operation)
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
84 * @param y the instruction that produces the second input to this instruction
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
85 * @param graph
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
86 */
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
87 public Compare(Value x, Condition condition, Value y, Graph graph) {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
88 super(CiKind.Illegal, INPUT_COUNT, SUCCESSOR_COUNT, graph);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
89 assert (x == null && y == null) || Util.archKindsEqual(x, y);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
90 this.condition = condition;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
91 setX(x);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
92 setY(y);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
93 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
94
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
95 /**
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
96 * Gets the condition (comparison operation) for this instruction.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
97 * @return the condition
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
98 */
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
99 public Condition condition() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
100 return condition;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
101 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
102
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
103 /**
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
104 * Checks whether unordered inputs mean true or false.
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
105 * @return {@code true} if unordered inputs produce true
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
106 */
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
107 public boolean unorderedIsTrue() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
108 return unorderedIsTrue;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
109 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
110
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
111 /**
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
112 * Swaps the operands to this if and reverses the condition (e.g. > goes to <=).
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
113 * @see Condition#mirror()
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
114 */
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
115 public void swapOperands() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
116 condition = condition.mirror();
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
117 Value t = x();
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
118 setX(y());
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
119 setY(t);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
120 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
121
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
122 @Override
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
123 public void accept(ValueVisitor v) {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
124 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
125
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
126 @Override
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
127 public void print(LogStream out) {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
128 out.print("comp ").
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
129 print(x()).
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
130 print(' ').
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
131 print(condition().operator).
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
132 print(' ').
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
133 print(y());
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
134 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
135
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
136 @Override
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
137 public String shortName() {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
138 return "Comp " + condition.operator;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
139 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
140
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
141 @Override
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
142 public Node copy(Graph into) {
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
143 Compare x = new Compare(null, condition, null, into);
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
144 x.unorderedIsTrue = unorderedIsTrue;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
145 return x;
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
146 }
29d33aac5ae3 Added compare node.
Thomas Wuerthinger <thomas@wuerthinger.net>
parents:
diff changeset
147 }