Hi,

Prathamesh Kulkarni <bilbotheelffri...@gmail.com> skribis:

> Example:
> /* x & 0 -> 0 */
> (match_and_simplify
>   (bit_and @0 @1)
>   if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && (@1 == integer_zero_node))
>   { integer_zero_node; })
>
> /* x & -1 -> x */
> (match_and_simplify
>   (bit_and @0 @1)
>   if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>      && (@1 == integer_minus_one_node)
>   @0)

(Apologies if this has already been discussed/decided before.)

With my Scheme background, two things come to mind:

  1. Wouldn’t it be nicer to have a single language, rather than
     intermingle C expressions in the middle of the s-expression
     language?

  2. Wouldn’t it be useful to allow multiple clauses in a single
     ‘match’?

Here’s a hypothetical simplification pass one could write:

  ;; Match tree node ‘exp’ against a series of patterns, and return
  ;; a (possibly identical) tree node.
  (match exp
    ((bitwise-and first second)
     (if (and (integral-type? (tree-type first))
              (eq? integer-zero-node second))
         integer-zero-node    ; simplify to zero
         exp))                ; return ‘exp’ unchanged
    ((bitwise-and first second)
     (if (and (integral-type? (tree-type first))
              (eq? integer-minus-one-node second))
         first
         exp))
    (else                     ; no simplification pattern matched
     exp))

The language for expression rewriting (the ‘if’ expressions above) could
consist of very few constructs directly translatable to C + tree.

As an example, Guile’s compiler simplification passes look very much
like this [0], built around a generic pattern matcher [1].  Pattern
matching in MELT should also be a good source of inspiration, obviously [2].

Thanks,
Ludo’.

[0] 
http://git.savannah.gnu.org/cgit/guile.git/tree/module/language/tree-il/peval.scm#n1082
[1] http://www.gnu.org/software/guile/manual/html_node/Pattern-Matching.html
[2] http://gcc-melt.org/tutomeltimplem.html

Reply via email to