Skip to content

Commit

Permalink
CFG can use Instr[] to build
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Mar 3, 2015
1 parent ac3bc8a commit 5ad4c5c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
15 changes: 14 additions & 1 deletion core/src/main/java/org/jruby/ir/IRScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public CFG buildCFG() {
}

CFG newCFG = new CFG(this);
newCFG.build(getInstrs());
newCFG.build(prepareBuildInstructions(getInstrs()));
// Clear out instruction list after CFG has been built.
instrList = null;

Expand All @@ -456,6 +456,19 @@ public CFG buildCFG() {
return newCFG;
}

private Instr[] prepareBuildInstructions(List<Instr> instructions) {
int length = instructions.size();
Instr[] linearizedInstrArray = instructions.toArray(new Instr[length]);
for (int ipc = 0; ipc < length; ipc++) {
Instr i = linearizedInstrArray[ipc];
i.setIPC(ipc);

if (i instanceof LabelInstr) ((LabelInstr) i).getLabel().setTargetPC(ipc + 1);
}

return linearizedInstrArray;
}

protected void setCFG(CFG cfg) {
this.cfg = cfg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public BasicBlock(CFG cfg, Label label) {
private void initInstrs() {
instrs = new ArrayList<>();
if (RubyInstanceConfig.IR_COMPILER_DEBUG || RubyInstanceConfig.IR_VISUALIZER) {
IRManager irManager = cfg.getScope().getManager();
IRManager irManager = cfg.getManager();
InstructionsListener listener = irManager.getInstructionsListener();
if (listener != null) {
instrs = new InstructionsListenerDecorator(instrs, listener);
Expand Down
39 changes: 10 additions & 29 deletions core/src/main/java/org/jruby/ir/representations/CFG.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jruby.dirgra.DirectedGraph;
import org.jruby.dirgra.Edge;
import org.jruby.ir.IRClosure;
import org.jruby.ir.IRManager;
import org.jruby.ir.IRScope;
import org.jruby.ir.Operation;
import org.jruby.ir.instructions.*;
Expand Down Expand Up @@ -69,6 +70,10 @@ public int getNextBBID() {
return nextBBId;
}

public IRManager getManager() {
return scope.getManager();
}

public int getMaxNodeID() {
return nextBBId;
}
Expand Down Expand Up @@ -152,26 +157,10 @@ public Iterable<Edge<BasicBlock>> getIncomingEdges(BasicBlock block) {
return graph.findVertexFor(block).getIncomingEdges();
}

public BasicBlock getIncomingSource(BasicBlock block) {
return graph.findVertexFor(block).getIncomingSourceData();
}

public BasicBlock getIncomingSourceOfType(BasicBlock block, Object type) {
return graph.findVertexFor(block).getIncomingSourceDataOfType(type);
}

public Edge<BasicBlock> getIncomingEdgeOfType(BasicBlock block, Object type) {
return graph.findVertexFor(block).getIncomingEdgeOfType(type);
}

public Edge<BasicBlock> getOutgoingEdgeOfType(BasicBlock block, Object type) {
return graph.findVertexFor(block).getOutgoingEdgeOfType(type);
}

public BasicBlock getOutgoingDestination(BasicBlock block) {
return graph.findVertexFor(block).getOutgoingDestinationData();
}

public BasicBlock getOutgoingDestinationOfType(BasicBlock block, Object type) {
return graph.findVertexFor(block).getOutgoingDestinationDataOfType(type);
}
Expand All @@ -192,10 +181,6 @@ public Collection<Edge<BasicBlock>> getOutgoingEdges(BasicBlock block) {
return graph.findVertexFor(block).getOutgoingEdges();
}

public Iterable<Edge<BasicBlock>> getOutgoingEdgesNotOfType(BasicBlock block, Object type) {
return graph.findVertexFor(block).getOutgoingEdgesNotOfType(type);
}

public BasicBlock getRescuerBBFor(BasicBlock block) {
return rescuerMap.get(block);
}
Expand Down Expand Up @@ -224,7 +209,7 @@ public void setRescuerBB(BasicBlock block, BasicBlock rescuerBlock) {
/**
* Build the Control Flow Graph
*/
public DirectedGraph<BasicBlock> build(List<Instr> instrs) {
public DirectedGraph<BasicBlock> build(Instr[] instrs) {
// Map of label & basic blocks which are waiting for a bb with that label
Map<Label, List<BasicBlock>> forwardRefs = new HashMap<>();

Expand All @@ -251,7 +236,7 @@ public DirectedGraph<BasicBlock> build(List<Instr> instrs) {
BasicBlock newBB;
boolean bbEnded = false;
boolean nextBBIsFallThrough = true;
for (Instr i : instrs) {
for (Instr i: instrs) {
// System.out.println("Processing: " + i);
Operation iop = i.getOperation();
if (iop == Operation.LABEL) {
Expand Down Expand Up @@ -422,10 +407,6 @@ public void addBasicBlock(BasicBlock bb) {
postOrderList = null;
}

public void removeEdge(Edge edge) {
graph.removeEdge(edge);
}

public void removeAllOutgoingEdgesForBB(BasicBlock b) {
graph.findVertexFor(b).removeAllOutgoingEdges();
}
Expand Down Expand Up @@ -550,7 +531,7 @@ private void optimize(List<BasicBlock> returnBBs) {
//
// If a jump intervenes in 'x', skip over it and if merge succeeds,
// delete the jump.
List<Edge> toRemove = new ArrayList<>();
List<Edge<BasicBlock>> toRemove = new ArrayList<>();
for (BasicBlock retBB: returnBBs) {
List<Instr> rbInstrs = retBB.getInstrs();
Instr first = rbInstrs.get(0);
Expand Down Expand Up @@ -587,7 +568,7 @@ private void optimize(List<BasicBlock> returnBBs) {
}
}
}
for (Edge edge: toRemove) {
for (Edge<BasicBlock> edge: toRemove) {
graph.removeEdge(edge);
}

Expand Down Expand Up @@ -615,7 +596,7 @@ private void optimize(List<BasicBlock> returnBBs) {
}

if (!toRemove.isEmpty()) {
for (Edge edge: toRemove) {
for (Edge<BasicBlock> edge: toRemove) {
graph.removeEdge(edge);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static void fixupList(CFG cfg, BasicBlock[] list, int listSize) {
BasicBlock current = list[i];

if (current.isExitBB()) { // exit not last
current.addInstr(new ReturnInstr(cfg.getScope().getManager().getNil()));
current.addInstr(new ReturnInstr(cfg.getManager().getNil()));
continue;
}

Expand Down

0 comments on commit 5ad4c5c

Please sign in to comment.