comparison graal/GraalCompiler/src/com/sun/c1x/opt/PhiSimplifier.java @ 2509:16b9a8b5ad39

Renamings Runtime=>GraalRuntime and Compiler=>GraalCompiler
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Apr 2011 11:50:44 +0200
parents graal/Compiler/src/com/sun/c1x/opt/PhiSimplifier.java@9ec15d6914ca
children
comparison
equal deleted inserted replaced
2508:fea94949e0a2 2509:16b9a8b5ad39
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.opt;
24
25 import com.sun.c1x.graph.IR;
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 final InstructionSubstituter subst;
38
39 public PhiSimplifier(IR ir) {
40 this.ir = ir;
41 this.subst = new InstructionSubstituter(ir);
42 ir.startBlock.iterateAnyOrder(this, false);
43 subst.finish();
44 }
45
46 /**
47 * This method is called for each block and processes any phi statements in the block.
48 * @param block the block to apply the simplification to
49 */
50 public void apply(BlockBegin block) {
51 FrameState state = block.stateBefore();
52 for (int i = 0; i < state.stackSize(); i++) {
53 simplify(state.stackAt(i));
54 }
55 for (int i = 0; i < state.localsSize(); i++) {
56 simplify(state.localAt(i));
57 }
58 }
59
60 Value simplify(Value x) {
61 if (x == null || !(x instanceof Phi)) {
62 return x;
63 }
64 Phi phi = (Phi) x;
65 if (phi.hasSubst()) {
66 // already substituted, but the subst could be a phi itself, so simplify
67 return simplify(subst.getSubst(phi));
68 } else if (phi.checkFlag(Value.Flag.PhiCannotSimplify)) {
69 // already tried, cannot simplify this phi
70 return phi;
71 } else if (phi.checkFlag(Value.Flag.PhiVisited)) {
72 // break cycles in phis
73 return phi;
74 } else if (phi.isIllegal()) {
75 // don't bother with illegals
76 return phi;
77 } else {
78 // attempt to simplify the phi by recursively simplifying its operands
79 phi.setFlag(Value.Flag.PhiVisited);
80 Value phiSubst = null;
81 int max = phi.inputCount();
82 boolean cannotSimplify = false;
83 for (int i = 0; i < max; i++) {
84 Value oldInstr = phi.inputAt(i);
85
86 if (oldInstr == null || oldInstr.isIllegal() || oldInstr.isDeadPhi()) {
87 // if one operand is illegal, make the entire phi illegal
88 phi.makeDead();
89 phi.clearFlag(Value.Flag.PhiVisited);
90 return phi;
91 }
92
93 Value newInstr = simplify(oldInstr);
94
95 if (newInstr == null || newInstr.isIllegal() || newInstr.isDeadPhi()) {
96 // if the subst instruction is illegal, make the entire phi illegal
97 phi.makeDead();
98 phi.clearFlag(Value.Flag.PhiVisited);
99 return phi;
100 }
101
102 // attempt to simplify this operand
103 if (!cannotSimplify) {
104
105 if (newInstr != phi && newInstr != phiSubst) {
106 if (phiSubst == null) {
107 phiSubst = newInstr;
108 continue;
109 }
110 // this phi cannot be simplified
111 cannotSimplify = true;
112 }
113 }
114 }
115 if (cannotSimplify) {
116 phi.setFlag(Value.Flag.PhiCannotSimplify);
117 phi.clearFlag(Value.Flag.PhiVisited);
118 return phi;
119 }
120
121 // successfully simplified the phi
122 assert phiSubst != null : "illegal phi function";
123 phi.clearFlag(Value.Flag.PhiVisited);
124 subst.setSubst(phi, phiSubst);
125 return phiSubst;
126 }
127 }
128 }