From 8822db089f2596abd3a60590915797988f860549 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 29 Dec 2015 02:13:44 +0800 Subject: [PATCH] transforms.cfg_simplifier: implement. This allows error reporting in LocalAccessValidator to proceed even when the predecessor is an empty block with only a branch. --- artiq/compiler/module.py | 2 ++ artiq/compiler/transforms/__init__.py | 1 + artiq/compiler/transforms/cfg_simplifier.py | 22 +++++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 artiq/compiler/transforms/cfg_simplifier.py diff --git a/artiq/compiler/module.py b/artiq/compiler/module.py index 4bf5113bd..6d1976615 100644 --- a/artiq/compiler/module.py +++ b/artiq/compiler/module.py @@ -55,6 +55,7 @@ class Module: artiq_ir_generator = transforms.ARTIQIRGenerator(engine=self.engine, module_name=src.name, ref_period=ref_period) + cfg_simplifier = transforms.CFGSimplifier(engine=self.engine) dead_code_eliminator = transforms.DeadCodeEliminator(engine=self.engine) local_access_validator = validators.LocalAccessValidator(engine=self.engine) devirtualization = analyses.Devirtualization() @@ -70,6 +71,7 @@ class Module: devirtualization.visit(src.typedtree) self.artiq_ir = artiq_ir_generator.visit(src.typedtree) artiq_ir_generator.annotate_calls(devirtualization) + cfg_simplifier.process(self.artiq_ir) local_access_validator.process(self.artiq_ir) dead_code_eliminator.process(self.artiq_ir) interleaver.process(self.artiq_ir) diff --git a/artiq/compiler/transforms/__init__.py b/artiq/compiler/transforms/__init__.py index 665fd3ea1..d672d3604 100644 --- a/artiq/compiler/transforms/__init__.py +++ b/artiq/compiler/transforms/__init__.py @@ -4,5 +4,6 @@ from .int_monomorphizer import IntMonomorphizer from .iodelay_estimator import IODelayEstimator from .artiq_ir_generator import ARTIQIRGenerator from .dead_code_eliminator import DeadCodeEliminator +from .cfg_simplifier import CFGSimplifier from .llvm_ir_generator import LLVMIRGenerator from .interleaver import Interleaver diff --git a/artiq/compiler/transforms/cfg_simplifier.py b/artiq/compiler/transforms/cfg_simplifier.py new file mode 100644 index 000000000..913d37a6e --- /dev/null +++ b/artiq/compiler/transforms/cfg_simplifier.py @@ -0,0 +1,22 @@ +""" +:class:`CFGSimplifier` is a simple control flow graph +simplification transform: it removes empty basic blocks. +""" + +from .. import ir + +class CFGSimplifier: + def __init__(self, engine): + self.engine = engine + + def process(self, functions): + for func in functions: + self.process_function(func) + + def process_function(self, func): + for block in list(func.basic_blocks): + if len(block.instructions) == 1 and \ + isinstance(block.terminator(), ir.Branch): + successor, = block.successors() + block.replace_all_uses_with(successor) + block.erase()