private void enforceSingleEntry() { Debug.debugMessage(getClass(), "Enforcing single entry in CFG", 3); ArrayList<Vertex> noPreds = new ArrayList<Vertex>(); for (Vertex v : cfg) { if (v.numOfPredecessors() == 0) { Debug.debugMessage(getClass(), v.getVertexID() + " is currently an entry vertex", 4); noPreds.add(v); } } while (noPreds.size() > 1) { int newVertexID = cfg.getNextVertexID(); cfg.addBasicBlock(newVertexID); Debug.debugMessage(getClass(), "Adding basic block " + newVertexID, 4); for (int i = 1; i <= MainProgramGenerator.Globals.getFanOut(); ++i) { if (!noPreds.isEmpty()) { Vertex v = noPreds.remove(noPreds.size() - 1); Debug.debugMessage(getClass(), "...and connecting it to " + v.getVertexID(), 4); if (i == 1) { cfg.addEdge(newVertexID, v.getVertexID(), BranchType.TAKEN); } else { cfg.addEdge(newVertexID, v.getVertexID(), BranchType.NOTTAKEN); } } } noPreds.add(cfg.getVertex(newVertexID)); } }
private void connectBranches(int noOfComponents, int unusedNodes) { if (noOfComponents > 1) { int destinationToRemove = random.nextInt(2); for (int i = 0; i < disconnectedBranches.size() - 3; i = i + 2) { if (random.nextBoolean()) { int sourceID = disconnectedBranches.get(i); int destinationRemovedID; if (destinationToRemove == 0) { destinationRemovedID = cfg.getVertex(sourceID).getNthSuccessor(destinationToRemove).getVertexID(); } else { destinationRemovedID = disconnectedBranches.get(i + 1); } Debug.debugMessage( getClass(), "Removing edge " + sourceID + "=>" + destinationRemovedID, 4); cfg.removeEdge(sourceID, destinationRemovedID); int newDestinationID = disconnectedBranches.get(i + 2); Debug.debugMessage(getClass(), "Adding edge " + sourceID + "=>" + newDestinationID, 4); cfg.addEdge(sourceID, newDestinationID, BranchType.TAKEN); sourceID = disconnectedBranches.get(i + 3); Debug.debugMessage( getClass(), "Adding edge " + sourceID + "=>" + destinationRemovedID, 4); cfg.addEdge(sourceID, destinationRemovedID, BranchType.TAKEN); } else { Debug.debugMessage( getClass(), "Adding edge " + disconnectedBranches.get(i + 1) + "=>" + disconnectedBranches.get(i + 2), 4); cfg.addEdge( disconnectedBranches.get(i + 1), disconnectedBranches.get(i + 2), BranchType.TAKEN); } } } if (unusedNodes > 0) { addNonBranchComponents(unusedNodes); Debug.debugMessage( getClass(), "Adding edge " + disconnectedBranches.get(disconnectedBranches.size() - 1) + "=>" + disconnectedVertices.get(0), 4); cfg.addEdge( disconnectedBranches.get(disconnectedBranches.size() - 1), disconnectedVertices.get(0), BranchType.TAKEN); } }
private void connectLoops() { for (int i = disconnectedLoops.getHeight() - 1; i > 0; --i) { Iterator<TreeVertex> it = disconnectedLoops.levelIterator(i); int childID = it.next().getVertexID(); int parentID = disconnectedLoops.getVertex(childID).getParentID(); Debug.debugMessage(getClass(), "Adding edge " + parentID + "=>" + childID, 4); cfg.addEdge(parentID, childID, BranchType.TAKEN); Debug.debugMessage( getClass(), "Adding edge " + childID + "=>" + cfg.getVertex(parentID).getNthSuccessor(0).getVertexID(), 4); cfg.addEdge( childID, cfg.getVertex(parentID).getNthSuccessor(0).getVertexID(), BranchType.TAKEN); Debug.debugMessage( getClass(), "Removing edge " + parentID + "=>" + cfg.getVertex(parentID).getNthSuccessor(0).getVertexID(), 4); cfg.removeEdge(parentID, cfg.getVertex(parentID).getNthSuccessor(0).getVertexID()); while (it.hasNext()) { int nextChildID = it.next().getVertexID(); int nextParentID = disconnectedLoops.getVertex(nextChildID).getParentID(); if (parentID == nextParentID) { Debug.debugMessage(getClass(), "Adding edge " + parentID + "=>" + nextChildID, 4); cfg.addEdge(parentID, nextChildID, BranchType.TAKEN); Debug.debugMessage(getClass(), "Adding edge " + nextChildID + "=>" + childID, 4); cfg.addEdge(nextChildID, childID, BranchType.TAKEN); Debug.debugMessage(getClass(), "Removing edge " + parentID + "=>" + childID, 4); cfg.removeEdge(parentID, childID); } else { Debug.debugMessage(getClass(), "Adding edge " + nextParentID + "=>" + nextChildID, 4); cfg.addEdge(nextParentID, nextChildID, BranchType.TAKEN); Debug.debugMessage( getClass(), "Adding edge " + nextChildID + "=>" + cfg.getVertex(nextParentID).getNthSuccessor(0).getVertexID(), 4); cfg.addEdge( nextChildID, cfg.getVertex(nextParentID).getNthSuccessor(0).getVertexID(), BranchType.TAKEN); Debug.debugMessage( getClass(), "Removing edge " + nextParentID + "=>" + cfg.getVertex(nextParentID).getNthSuccessor(0).getVertexID(), 4); cfg.removeEdge( nextParentID, cfg.getVertex(nextParentID).getNthSuccessor(0).getVertexID()); parentID = nextParentID; } childID = nextChildID; } } }