Root/b2/genredmap.pl

1#!/usr/bin/perl
2#
3# genredmap.pl - Redundacy detector map generator
4#
5# Copyright 2012 by Werner Almesberger
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12
13#
14# This script finds the relational operators op = f(opa, opb), for which
15# the following is true:
16#
17# forall A, B: (A op B) -> forall X: !(X opa A) -> !(X opa B)
18#
19# In other words: if, with X a variable and A and B constants, we have a
20# cascade of the form
21#
22# if (X opa A) ...;
23# else if (X opa B) ...;
24#
25# and A op B is true, then the second branch will never be taken.
26#
27
28%op = (
29    "lt" => sub { $_[0] < $_[1] },
30    "le" => sub { $_[0] <= $_[1] },
31    "eq" => sub { $_[0] == $_[1] },
32    "ge" => sub { $_[0] >= $_[1] },
33    "gt" => sub { $_[0] > $_[1] },
34    
35);
36
37@ops = ("lt", "le", "eq", "ge", "gt");
38
39$always = 0;
40
41$a = 2;
42@v = (0, 1, 2, 3, 4);
43
44for $opa (@ops) {
45#next unless $opa eq "lt";
46    for $opb (@ops) {
47#next unless $opb eq "lt";
48        $best = 0;
49        OP: for $op (@ops) {
50            $hit = 0;
51            for $b (@v) {
52#print "($opa, $opb) $a $op $b\n";
53                next unless $op{$op}->($a, $b);
54                for $x (@v) {
55#print " A: $x $opa $a\n";
56                    next if $op{$opa}->($x, $a);
57#print " B: $x $opb $b\n";
58                    next OP
59                        if $op{$opb}->($x, $b) != $always;
60                    $hit++;
61#print " HIT ($hit)\n";
62                }
63            }
64            if ($hit > $best) {
65                $best = $hit;
66                $best_op = $op;
67            }
68        }
69        print "\t[idx_$opa][idx_$opb] = rel_$best_op,\n" if $best;
70    }
71}
72

Archive Download this file

Branches:
master



interactive