Mercurial > hg > truffle
comparison graal/GraalCompiler/src/com/sun/c1x/graph/CriticalEdgeFinder.java @ 2779:93ec3f067420
Changed CriticalEdgeFinder to use LIRBlock.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 25 May 2011 11:04:59 +0200 |
parents | 27512ea6bbcb |
children | 79dda81dd337 |
comparison
equal
deleted
inserted
replaced
2778:2ac7b30b7290 | 2779:93ec3f067420 |
---|---|
22 */ | 22 */ |
23 package com.sun.c1x.graph; | 23 package com.sun.c1x.graph; |
24 | 24 |
25 import java.util.*; | 25 import java.util.*; |
26 | 26 |
27 import com.oracle.graal.graph.*; | |
27 import com.sun.c1x.*; | 28 import com.sun.c1x.*; |
28 import com.sun.c1x.debug.*; | 29 import com.sun.c1x.debug.*; |
29 import com.sun.c1x.ir.*; | 30 import com.sun.c1x.ir.*; |
31 import com.sun.c1x.lir.*; | |
30 | 32 |
31 /** | 33 /** |
32 * This class finds and splits "critical" edges in the control flow graph. | 34 * This class finds and splits "critical" edges in the control flow graph. |
33 * An edge between two blocks {@code A} and {@code B} is "critical" if {@code A} | 35 * An edge between two blocks {@code A} and {@code B} is "critical" if {@code A} |
34 * has more than one successor and {@code B} has more than one predecessor. Such | 36 * has more than one successor and {@code B} has more than one predecessor. Such |
35 * edges are split by adding a block between the two blocks. | 37 * edges are split by adding a block between the two blocks. |
36 */ | 38 */ |
37 public class CriticalEdgeFinder implements BlockClosure { | 39 public class CriticalEdgeFinder { |
38 | 40 |
39 private final IR ir; | 41 private final List<LIRBlock> lirBlocks; |
42 private final Graph graph; | |
40 | 43 |
41 /** | 44 /** |
42 * The graph edges represented as a map from source to target nodes. | 45 * The graph edges represented as a map from source to target nodes. |
43 * Using a linked hash map makes compilation tracing more deterministic and thus eases debugging. | 46 * Using a linked hash map makes compilation tracing more deterministic and thus eases debugging. |
44 */ | 47 */ |
45 private Map<BlockBegin, Set<BlockBegin>> edges = C1XOptions.DetailedAsserts ? | 48 private Map<LIRBlock, Set<LIRBlock>> edges = C1XOptions.DetailedAsserts ? |
46 new LinkedHashMap<BlockBegin, Set<BlockBegin>>() : | 49 new LinkedHashMap<LIRBlock, Set<LIRBlock>>() : |
47 new HashMap<BlockBegin, Set<BlockBegin>>(); | 50 new HashMap<LIRBlock, Set<LIRBlock>>(); |
48 | 51 |
49 public CriticalEdgeFinder(IR ir) { | 52 public CriticalEdgeFinder(List<LIRBlock> lirBlocks, Graph graph) { |
50 this.ir = ir; | 53 this.lirBlocks = lirBlocks; |
54 this.graph = graph; | |
55 for (LIRBlock block : lirBlocks) { | |
56 apply(block); | |
57 } | |
58 | |
51 } | 59 } |
52 | 60 |
53 public void apply(BlockBegin block) { | 61 private void apply(LIRBlock block) { |
54 BlockEnd end = block.end(); | 62 if (block.numberOfSux() >= 2) { |
55 if (end.blockSuccessorCount() >= 2) { | 63 for (LIRBlock succ : block.blockSuccessors()) { |
56 for (BlockBegin succ : end.blockSuccessors()) { | |
57 if (succ.numberOfPreds() >= 2) { | 64 if (succ.numberOfPreds() >= 2) { |
58 // TODO: (tw) probably we don't have to make it a critical edge if succ only contains the _same_ predecessor multiple times. | 65 // TODO: (tw) probably we don't have to make it a critical edge if succ only contains the _same_ predecessor multiple times. |
59 recordCriticalEdge(block, succ); | 66 recordCriticalEdge(block, succ); |
60 } | 67 } |
61 } | 68 } |
62 } | 69 } |
63 } | 70 } |
64 | 71 |
65 private void recordCriticalEdge(BlockBegin block, BlockBegin succ) { | 72 private void recordCriticalEdge(LIRBlock block, LIRBlock succ) { |
66 if (!edges.containsKey(block)) { | 73 if (!edges.containsKey(block)) { |
67 edges.put(block, new HashSet<BlockBegin>()); | 74 edges.put(block, new HashSet<LIRBlock>()); |
68 } | 75 } |
69 | 76 |
70 edges.get(block).add(succ); | 77 edges.get(block).add(succ); |
71 } | 78 } |
72 | 79 |
73 public void splitCriticalEdges() { | 80 public void splitCriticalEdges() { |
74 for (Map.Entry<BlockBegin, Set<BlockBegin>> entry : edges.entrySet()) { | 81 for (Map.Entry<LIRBlock, Set<LIRBlock>> entry : edges.entrySet()) { |
75 BlockBegin from = entry.getKey(); | 82 LIRBlock from = entry.getKey(); |
76 for (BlockBegin to : entry.getValue()) { | 83 for (LIRBlock to : entry.getValue()) { |
77 BlockBegin split = ir.splitEdge(from, to); | 84 LIRBlock split = splitEdge(from, to); |
78 if (C1XOptions.PrintHIR) { | 85 if (C1XOptions.PrintHIR) { |
79 TTY.println("Split edge between block %d and block %d, creating new block %d", from.blockID, to.blockID, split.blockID); | 86 TTY.println("Split edge between block %d and block %d, creating new block %d", from.blockID(), to.blockID(), split.blockID()); |
80 } | 87 } |
81 } | 88 } |
82 } | 89 } |
83 } | 90 } |
91 | |
92 | |
93 /** | |
94 * Creates and inserts a new block between this block and the specified successor, | |
95 * altering the successor and predecessor lists of involved blocks appropriately. | |
96 * @param source the source of the edge | |
97 * @param target the successor before which to insert a block | |
98 * @return the new block inserted | |
99 */ | |
100 public LIRBlock splitEdge(LIRBlock source, LIRBlock target) { | |
101 | |
102 int backEdgeIndex = target.blockPredecessors().indexOf(source); | |
103 | |
104 // create new successor and mark it for special block order treatment | |
105 LIRBlock newSucc = new LIRBlock(lirBlocks.size()); | |
106 lirBlocks.add(newSucc); | |
107 | |
108 List<Integer> removePhiInputs = null; | |
109 for (int i = backEdgeIndex + 1; i < target.blockPredecessors().size(); ++i) { | |
110 if (target.blockPredecessors().get(i) == source) { | |
111 if (removePhiInputs == null) { | |
112 removePhiInputs = new ArrayList<Integer>(4); | |
113 } | |
114 removePhiInputs.add(i); | |
115 } | |
116 } | |
117 | |
118 // This goto is not a safepoint. | |
119 Goto e = new Goto(target.getInstructions().get(0), graph); | |
120 newSucc.getInstructions().add(e); | |
121 //e.reorderSuccessor(0, backEdgeIndex); | |
122 | |
123 // link predecessor to new block | |
124 ((BlockEnd) source.getInstructions().get(source.getInstructions().size() - 1)).successors().replace(target.getInstructions().get(0), newSucc.getInstructions().get(0)); | |
125 /* if (removePhiInputs != null && removePhiInputs.size() > 0) { | |
126 | |
127 for (Node n : target.getInstructions().get(0).usages()) { | |
128 if (n instanceof Phi) { | |
129 Phi phi = (Phi) n; | |
130 int correction = 0; | |
131 for (int index : removePhiInputs) { | |
132 phi.removeInput(index - correction); | |
133 correction++; | |
134 } | |
135 } | |
136 } | |
137 }*/ | |
138 | |
139 source.substituteSuccessor(target, newSucc); | |
140 target.substitutePredecessor(source, newSucc); | |
141 newSucc.blockPredecessors().add(source); | |
142 newSucc.blockSuccessors().add(target); | |
143 | |
144 return newSucc; | |
145 } | |
84 } | 146 } |