comparison graal/GraalCompiler/src/com/sun/c1x/ir/And.java @ 2853:0635ba16afe4

Reintroduce Logic subclasses, creating some Canonicalization related classes
author Gilles Duboscq <gilles.duboscq@oracle.com>
date Tue, 07 Jun 2011 15:52:55 +0200
parents
children b20f0a48fec3
comparison
equal deleted inserted replaced
2852:c6bdec623ef9 2853:0635ba16afe4
1 /*
2 * Copyright (c) 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.oracle.graal.graph.*;
26 import com.oracle.max.graal.opt.CanonicalizerPhase.CanonicalizerOp;
27 import com.sun.cri.bytecode.*;
28 import com.sun.cri.ci.*;
29
30 public final class And extends Logic {
31 private static final AndCanonicalizerOp CANONICALIZER = new AndCanonicalizerOp();
32
33 /**
34 * @param opcode
35 * @param x
36 * @param y
37 * @param graph
38 */
39 public And(CiKind kind, Value x, Value y, Graph graph) {
40 super(kind, kind == CiKind.Int ? Bytecodes.IAND : Bytecodes.LAND, x, y, graph);
41 }
42
43 @Override
44 public String shortName() {
45 return "&";
46 }
47
48 @Override
49 public Node copy(Graph into) {
50 And x = new And(kind, x(), y(), graph());
51 return x;
52 }
53
54 @SuppressWarnings("unchecked")
55 @Override
56 public <T extends Op> T lookup(Class<T> clazz) {
57 if (clazz == CanonicalizerOp.class) {
58 return (T) CANONICALIZER;
59 }
60 return super.lookup(clazz);
61 }
62
63 private static class AndCanonicalizerOp implements CanonicalizerOp {
64 @Override
65 public Node canonical(Node node) {
66 assert node instanceof And;
67 And and = (And) node;
68 CiKind kind = and.kind;
69 Graph graph = and.graph();
70 Value x = and.x();
71 Value y = and.y();
72 if (x == y) {
73 return x;
74 }
75 if (x.isConstant() && !y.isConstant()) {
76 and.swapOperands();
77 Value t = y;
78 y = x;
79 x = t;
80 }
81 if (x.isConstant()) {
82 if (kind == CiKind.Int) {
83 return Constant.forInt(x.asConstant().asInt() & y.asConstant().asInt(), graph);
84 } else {
85 assert kind == CiKind.Long;
86 return Constant.forLong(x.asConstant().asLong() & y.asConstant().asLong(), graph);
87 }
88 } else if (y.isConstant()) {
89 if (kind == CiKind.Int) {
90 int c = y.asConstant().asInt();
91 if (c == -1) {
92 return x;
93 }
94 if (c == 0) {
95 return Constant.forInt(0, graph);
96 }
97 } else {
98 assert kind == CiKind.Long;
99 long c = y.asConstant().asLong();
100 if (c == -1) {
101 return x;
102 }
103 if (c == 0) {
104 return Constant.forLong(0, graph);
105 }
106 }
107 }
108 return and;
109 }
110 }
111 }