From 3385e50c43a644d471a6f1d305f8abe23a062a4d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 30 Jun 2009 22:43:32 -0500 Subject: [PATCH] compiler.cfg.branch-folding: fold away branches where both inputs are the same register. This results in 'push' being compiled slightly better --- .../cfg/branch-folding/branch-folding.factor | 30 +++++++++++++++++++ basis/compiler/cfg/optimizer/optimizer.factor | 5 ++++ 2 files changed, 35 insertions(+) create mode 100644 basis/compiler/cfg/branch-folding/branch-folding.factor diff --git a/basis/compiler/cfg/branch-folding/branch-folding.factor b/basis/compiler/cfg/branch-folding/branch-folding.factor new file mode 100644 index 0000000000..627db63c9f --- /dev/null +++ b/basis/compiler/cfg/branch-folding/branch-folding.factor @@ -0,0 +1,30 @@ +! Copyright (C) 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors combinators.short-circuit kernel sequences vectors +compiler.cfg.instructions compiler.cfg.rpo ; +IN: compiler.cfg.branch-folding + +! Fold comparisons where both inputs are the same. Predecessors must be +! recomputed after this + +: fold-branch? ( bb -- ? ) + instructions>> last { + [ ##compare-branch? ] + [ [ src1>> ] [ src2>> ] bi = ] + } 1&& ; + +: chosen-successor ( bb -- succ ) + [ instructions>> last cc>> { cc= cc<= cc>= } memq? 0 1 ? ] + [ successors>> ] + bi nth ; + +: fold-branch ( bb -- ) + dup chosen-successor 1vector >>successors + instructions>> [ pop* ] [ [ \ ##branch new-insn ] dip push ] bi ; + +: fold-branches ( cfg -- cfg' ) + dup [ + dup fold-branch? + [ fold-branch ] [ drop ] if + ] each-basic-block + f >>post-order ; \ No newline at end of file diff --git a/basis/compiler/cfg/optimizer/optimizer.factor b/basis/compiler/cfg/optimizer/optimizer.factor index d6b1221fef..84eb8a84d1 100644 --- a/basis/compiler/cfg/optimizer/optimizer.factor +++ b/basis/compiler/cfg/optimizer/optimizer.factor @@ -9,6 +9,7 @@ compiler.cfg.branch-splitting compiler.cfg.alias-analysis compiler.cfg.value-numbering compiler.cfg.dce +compiler.cfg.branch-folding compiler.cfg.write-barrier compiler.cfg.liveness compiler.cfg.rpo @@ -24,6 +25,8 @@ SYMBOL: check-optimizer? ] when ; : optimize-cfg ( cfg -- cfg' ) + ! Note that compute-predecessors has to be called several times. + ! The passes that need this document it. [ optimize-tail-calls compute-predecessors @@ -34,6 +37,8 @@ SYMBOL: check-optimizer? compute-liveness alias-analysis value-numbering + fold-branches + compute-predecessors eliminate-dead-code eliminate-write-barriers eliminate-phis