comparison graal/GraalCompiler/src/com/sun/c1x/gen/PhiSimplifier.java @ 2738:88123130ede6

Moved phi simplifier.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Fri, 20 May 2011 10:46:15 +0200
parents graal/GraalCompiler/src/com/sun/c1x/opt/PhiSimplifier.java@a2f62de90c76
children 0fe79e7435c3
comparison
equal deleted inserted replaced
2737:99c84a06bb64 2738:88123130ede6
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.gen;
24
25 import com.sun.c1x.graph.*;
26 import com.sun.c1x.ir.*;
27 import com.sun.c1x.value.*;
28
29 /**
30 * The {@code PhiSimplifier} class is a helper class that can reduce phi instructions.
31 *
32 * @author Ben L. Titzer
33 */
34 public final class PhiSimplifier implements BlockClosure {
35
36 final IR ir;
37
38 public PhiSimplifier(IR ir) {
39 this.ir = ir;
40 ir.getHIRStartBlock().iterateAnyOrder(this, false);
41 }
42
43 /**
44 * This method is called for each block and processes any phi statements in the block.
45 * @param block the block to apply the simplification to
46 */
47 public void apply(BlockBegin block) {
48 FrameState state = block.stateBefore();
49 for (int i = 0; i < state.stackSize(); i++) {
50 simplify(state.stackAt(i));
51 }
52 for (int i = 0; i < state.localsSize(); i++) {
53 simplify(state.localAt(i));
54 }
55 }
56
57 Value simplify(Value x) {
58 if (x == null || !(x instanceof Phi)) {
59 return x;
60 }
61 Phi phi = (Phi) x;
62 if (phi.checkFlag(Value.Flag.PhiCannotSimplify)) {
63 // already tried, cannot simplify this phi
64 return phi;
65 } else if (phi.checkFlag(Value.Flag.PhiVisited)) {
66 // break cycles in phis
67 return phi;
68 } else if (phi.isIllegal()) {
69 // don't bother with illegals
70 return phi;
71 } else {
72 // attempt to simplify the phi by recursively simplifying its operands
73 phi.setFlag(Value.Flag.PhiVisited);
74 Value phiSubst = null;
75 int max = phi.phiInputCount();
76 boolean cannotSimplify = false;
77 for (int i = 0; i < max; i++) {
78 Value oldInstr = phi.inputAt(i);
79
80 if (oldInstr == null || oldInstr.isIllegal() || oldInstr.isDeadPhi()) {
81 // if one operand is illegal, make the entire phi illegal
82 phi.makeDead();
83 phi.clearFlag(Value.Flag.PhiVisited);
84 return phi;
85 }
86
87 Value newInstr = simplify(oldInstr);
88
89 if (newInstr == null || newInstr.isIllegal() || newInstr.isDeadPhi()) {
90 // if the subst instruction is illegal, make the entire phi illegal
91 phi.makeDead();
92 phi.clearFlag(Value.Flag.PhiVisited);
93 return phi;
94 }
95
96 // attempt to simplify this operand
97 if (!cannotSimplify) {
98
99 if (newInstr != phi && newInstr != phiSubst) {
100 if (phiSubst == null) {
101 phiSubst = newInstr;
102 continue;
103 }
104 // this phi cannot be simplified
105 cannotSimplify = true;
106 }
107 }
108 }
109 if (cannotSimplify) {
110 phi.setFlag(Value.Flag.PhiCannotSimplify);
111 phi.clearFlag(Value.Flag.PhiVisited);
112 return phi;
113 }
114
115 // successfully simplified the phi
116 assert phiSubst != null : "illegal phi function";
117 phi.clearFlag(Value.Flag.PhiVisited);
118 return phiSubst;
119 }
120 }
121 }