Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify reducibility of control-flow graphs. #215

merged 5 commits into from May 27, 2018

Verify reducibility of control-flow graphs. #215

merged 5 commits into from May 27, 2018


Copy link

@dvander dvander commented May 27, 2018

SourcePawn is a structured language, which essentially means it does not have goto. Having a structured language leads to a very nice control-flow property called reducibility. A reducible control-flow graph guarantees an upper limit on the number of iterations it takes for many graph algorithms to reach a fixpoint. It also means some iterative algorithms can be replaced entirely with much simpler or more efficient variants.

While there are ways to make an irreducible graph reducible, there's an advantage to simply not allowing them: it verifies that the compiler is not generating unstructured control-flow. The language has no goto statements, so we shouldn't expect to see evidence of them in the bytecode.

There are many ways to determine reducibility. The implementation here is quite simple, but requires a bunch of preceding data structures:

  1. We need a postorder and reverse postorder traversal of the graph. Blocks are now stored in RPO order, which guarantees that all predecessors are visited before their successors.
  2. We need to compute dominators. A block A dominates block B if all paths to B must flow through A. An immediate dominator is the closest such dominating block. This is a fixpoint algorithm.
  3. We need a dominator tree. This is an efficient data structure for determining the A dom B relationship.
  4. Finally, we have a test for irreducibility. If an edge A -> B is a backwards edge (B occurs before A in RPO), then B must dominate A. If not, the graph is not reducible.

I read somewhere that step 2 is enough for determining irreducibility, because the number of iterations to reach a fixpoint should be <= 2 for reducible graphs. Indeed that is true for the corpus of .smx files I downloaded. But I can't find where I read this.

@dvander dvander merged commit 30e7678 into master May 27, 2018
2 checks passed
@dvander dvander deleted the cfg branch May 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant