Mercurial > hg > graal-compiler
annotate graal/com.oracle.graal.replacements/src/com/oracle/graal/replacements/nodes/ArrayEqualsNode.java @ 14888:f6630873316b
Rename NodeLIRGenerator (and related classes and interfaces) to NodeLIRBuilder.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Mon, 31 Mar 2014 10:03:32 +0200 |
parents | 47e4d2e01c6e |
children | 882f4cb7cfcf |
rev | line source |
---|---|
13973 | 1 /* |
2 * Copyright (c) 2013, 2014, 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.oracle.graal.replacements.nodes; | |
24 | |
25 import com.oracle.graal.api.meta.*; | |
26 import com.oracle.graal.compiler.gen.*; | |
27 import com.oracle.graal.compiler.target.*; | |
28 import com.oracle.graal.graph.*; | |
29 import com.oracle.graal.graph.spi.*; | |
30 import com.oracle.graal.lir.*; | |
31 import com.oracle.graal.nodes.*; | |
14611
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
32 import com.oracle.graal.nodes.spi.*; |
13973 | 33 import com.oracle.graal.nodes.type.*; |
14611
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
34 import com.oracle.graal.nodes.util.*; |
13973 | 35 |
36 /** | |
37 * Compares two arrays with the same length. | |
38 */ | |
14611
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
39 public class ArrayEqualsNode extends FixedWithNextNode implements LIRGenLowerable, Canonicalizable, Virtualizable { |
13973 | 40 |
41 /** {@link Kind} of the arrays to compare. */ | |
42 private final Kind kind; | |
43 | |
44 /** One array to be tested for equality. */ | |
45 @Input private ValueNode array1; | |
46 | |
47 /** The other array to be tested for equality. */ | |
48 @Input private ValueNode array2; | |
49 | |
50 /** Length of both arrays. */ | |
51 @Input private ValueNode length; | |
52 | |
53 public ArrayEqualsNode(ValueNode array1, ValueNode array2, ValueNode length) { | |
54 super(StampFactory.forKind(Kind.Boolean)); | |
55 | |
56 assert array1.stamp().equals(array2.stamp()); | |
57 ObjectStamp stamp = (ObjectStamp) array1.stamp(); | |
58 ResolvedJavaType componentType = stamp.type().getComponentType(); | |
59 this.kind = componentType.getKind(); | |
60 | |
61 this.array1 = array1; | |
62 this.array2 = array2; | |
63 this.length = length; | |
64 } | |
65 | |
66 @Override | |
67 public Node canonical(CanonicalizerTool tool) { | |
14611
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
68 if (usages().isEmpty()) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
69 return null; |
13973 | 70 } |
14611
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
71 if (GraphUtil.unproxify(array1) == GraphUtil.unproxify(array2)) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
72 return ConstantNode.forBoolean(true, graph()); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
73 } |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
74 return this; |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
75 } |
13973 | 76 |
14611
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
77 public void virtualize(VirtualizerTool tool) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
78 State state1 = tool.getObjectState(array1); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
79 if (state1 != null) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
80 State state2 = tool.getObjectState(array2); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
81 if (state2 != null) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
82 if (state1.getVirtualObject() == state2.getVirtualObject()) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
83 // the same virtual objects will always have the same contents |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
84 tool.replaceWithValue(ConstantNode.forBoolean(true, graph())); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
85 } else if (state1.getVirtualObject().entryCount() == state2.getVirtualObject().entryCount()) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
86 int entryCount = state1.getVirtualObject().entryCount(); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
87 boolean allEqual = true; |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
88 for (int i = 0; i < entryCount; i++) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
89 ValueNode entry1 = state1.getEntry(i); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
90 ValueNode entry2 = state2.getEntry(i); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
91 if (entry1 != entry2) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
92 // the contents might be different |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
93 allEqual = false; |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
94 } |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
95 if (entry1.stamp().alwaysDistinct(entry2.stamp())) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
96 // the contents are different |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
97 tool.replaceWithValue(ConstantNode.forBoolean(false, graph())); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
98 return; |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
99 } |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
100 } |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
101 if (allEqual) { |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
102 tool.replaceWithValue(ConstantNode.forBoolean(true, graph())); |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
103 } |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
104 } |
1d853e629891
fixed canonicalization of ArrayEqualsNode, added Virtualizable implementation
Lukas Stadler <lukas.stadler@oracle.com>
parents:
14585
diff
changeset
|
105 } |
13973 | 106 } |
107 } | |
108 | |
109 @NodeIntrinsic | |
110 public static native boolean equals(boolean[] array1, boolean[] array2, int length); | |
111 | |
112 @NodeIntrinsic | |
113 public static native boolean equals(byte[] array1, byte[] array2, int length); | |
114 | |
115 @NodeIntrinsic | |
116 public static native boolean equals(char[] array1, char[] array2, int length); | |
117 | |
118 @NodeIntrinsic | |
119 public static native boolean equals(short[] array1, short[] array2, int length); | |
120 | |
121 @NodeIntrinsic | |
122 public static native boolean equals(int[] array1, int[] array2, int length); | |
123 | |
124 @NodeIntrinsic | |
125 public static native boolean equals(long[] array1, long[] array2, int length); | |
126 | |
127 @NodeIntrinsic | |
128 public static native boolean equals(float[] array1, float[] array2, int length); | |
129 | |
130 @NodeIntrinsic | |
131 public static native boolean equals(double[] array1, double[] array2, int length); | |
132 | |
133 @Override | |
14888
f6630873316b
Rename NodeLIRGenerator (and related classes and interfaces) to NodeLIRBuilder.
Josef Eisl <josef.eisl@jku.at>
parents:
14841
diff
changeset
|
134 public void generate(NodeLIRBuilder gen) { |
14003
0c38906450a0
Make conversion from Stamp to PlatformKind extensible by backend.
Roland Schatz <roland.schatz@oracle.com>
parents:
13973
diff
changeset
|
135 Variable result = gen.newVariable(Kind.Int); |
13973 | 136 gen.emitArrayEquals(kind, result, gen.operand(array1), gen.operand(array2), gen.operand(length)); |
137 gen.setResult(this, result); | |
138 } | |
139 } |