Class ControlFlowGraph

java.lang.Object
net.covers1624.coffeegrinder.bytecode.flow.ControlFlowGraph

public class ControlFlowGraph extends Object
Holds the control flow graph. A separate graph is computed for each BlockContainer at the start of block transforms (before loop detection)

Created by covers1624 on 19/4/21.

  • Field Details

    • container

      public final BlockContainer container
      The container for which the ControlFlowGraph was created.

      This may differ from the container currently holding a block, because a transform could have moved the block since the CFG was created.

    • cfg

      public final ControlFlowNode[] cfg
      Nodes array, indexed by original block index.

      Originally cfg[i].block == container.blocks.get(i) but may have since been moved/reordered by transforms.

    • nodes

      public final Map<Block,ControlFlowNode> nodes
      A map of Block to ControlFlowNode.

      Unlike the cfg array, this can be used to discover control flow nodes even after blocks were moved/reordered by transforms.

    • directExits

      public final BitSet directExits
      directExits.get(i) == true if cfg[i] directly contains a branch/leave instruction leaving container.
    • reachableExits

      public final BitSet reachableExits
      reachableExits.get(i) == true if there is a path from cfg[i] to a node not dominated by cfg[i], or if there is a path from cfg[i] to a branch/leave instruction leaving the container.
  • Constructor Details

    • ControlFlowGraph

      public ControlFlowGraph(BlockContainer container)
      Constructs a control flow graph for the blocks in the given block container.

      Return statements, exceptions, or branches leaving the block container are not modeled by the control flow graph.

      Parameters:
      container - The container.
  • Method Details

    • getNode

      public ControlFlowNode getNode(Block block)
      Gets the control flow node for the block.

      Precondition: the block belonged to the container at the start of the block transforms (where the control flow graph was created).

      Parameters:
      block - The block to get the ControlFlowNode for.
      Returns:
      The ControlFlowNode.
    • hasReachableExit

      public boolean hasReachableExit(ControlFlowNode node)
      Returns true if there is a control flow path from node to one of the following: - Branch or leave instruction leaving this.container. - Branch instruction within this container to another node that is not dominated by node.

      If this function returns false, the only way control flow can leave the set of nodes dominated by node is by executing a return; or throw; instruction.

      Parameters:
      node - The node to check.
      Returns:
      If the node has a reachable exit.
    • hasDirectExit

      public boolean hasDirectExit(ControlFlowNode node)
      Gets whether the control flow node directly contains a branch/leave instruction exiting the container.
      Parameters:
      node - The node to check.
      Returns:
      If the node has a direct exit.
    • resetVisited

      public void resetVisited()
      Resets all ControlFlowNode.visited flags for all nodes in this graph.