[match-and-simplify] operator-lists in expression

2014-11-10 Thread Prathamesh Kulkarni
Hi,
  This patch adds support for operator-lists to be used in expression.

I reuse operator-list as the iterator. This is not really valid since
user-defined operator-lists cannot be iterator in 'for', but it was
convenient to reuse operator-list as a 'for' iterator
and lower_for doesn't care about that.
eg:
(define_operator_list  list1 plus minus)

(simplify
  (list1 @x integer_zerop)
  (non_lvalue @x))

is wrapped into 'for' as: (lower_operator_list):
(for list1 (plus minus)
  (simplify
(list1 @x integer_zerop)
(non_lvalue @x)))

this is not really valid since we reject list1 to be used as iterator if
it were written by user.

Is this okay or should I introduce an explicit temporary iterator ?
so it gets lowered to something like:
(for tmp1 (list1)
  (simplify
(tmp1 @x integer_zerop)
(non_lvalue @x)))

* genmatch.c
  (fatal_at): New overloaded function.
  (simplify::oper_lists): New member.
  (simplify::simplify): Add default argument.
  (lower_commutative): Adjust call to simplify::simplify.
  (lower_opt_convert): Likewise.
  (lower_operator_list): New function.
  (lower): Call lower_operator_list.
  (parser::parsing_for_p): New member function.
  (parser::oper_lists): New member.
  (parser::parse_operation): Check for operator-list.
  (parser::parse_c_expr): Likewise.
  (parser::parse_simplify): Reset parser::oper_lists.
Adjust call to simplify::simplify.
  (parser::parser): Initialize parser::oper_lists.

* match-builtin.pd:
  Adjust patten to use SQRTs and POWs.

Thanks,
Prathamesh
Index: gcc/genmatch.c
===
--- gcc/genmatch.c	(revision 217284)
+++ gcc/genmatch.c	(working copy)
@@ -110,6 +110,18 @@
 #if GCC_VERSION >= 4001
 __attribute__((format (printf, 2, 3)))
 #endif
+fatal_at (source_location loc, const char *msg, ...)
+{
+  va_list ap;
+  va_start (ap, msg);
+  error_cb (NULL, CPP_DL_FATAL, 0, loc, 0, msg, &ap);
+  va_end (ap);
+}
+
+static void
+#if GCC_VERSION >= 4001
+__attribute__((format (printf, 2, 3)))
+#endif
 warning_at (const cpp_token *tk, const char *msg, ...)
 {
   va_list ap;
@@ -549,11 +561,11 @@
   simplify (operand *match_, source_location match_location_,
 	struct operand *result_, source_location result_location_,
 	vec ifexpr_vec_, vec > for_vec_,
-	cid_map_t *capture_ids_)
+	cid_map_t *capture_ids_, vec oper_lists_ = vNULL)
   : match (match_), match_location (match_location_),
   result (result_), result_location (result_location_),
   ifexpr_vec (ifexpr_vec_), for_vec (for_vec_),
-  capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1) {}
+  capture_ids (capture_ids_), capture_max (capture_ids_->elements () - 1), oper_lists (oper_lists_) {}
 
   /* The expression that is matched against the GENERIC or GIMPLE IL.  */
   operand *match;
@@ -572,6 +584,8 @@
   /* A map of capture identifiers to indexes.  */
   cid_map_t *capture_ids;
   int capture_max;
+  /* collected operator-list used in expression */
+  vec oper_lists;
 };
 
 /* Debugging routines for dumping the AST.  */
@@ -721,7 +735,7 @@
 {
   simplify *ns = new simplify (matchers[i], s->match_location,
    s->result, s->result_location, s->ifexpr_vec,
-   s->for_vec, s->capture_ids);
+   s->for_vec, s->capture_ids, s->oper_lists);
   simplifiers.safe_push (ns);
 }
 }
@@ -837,7 +851,7 @@
 {
   simplify *ns = new simplify (matchers[i], s->match_location,
    s->result, s->result_location, s->ifexpr_vec,
-   s->for_vec, s->capture_ids);
+   s->for_vec, s->capture_ids, s->oper_lists);
   simplifiers.safe_push (ns);
 }
 }
@@ -934,6 +948,38 @@
 simplifiers.safe_push (worklist[i]);
 }
 
+static void
+lower_operator_list (simplify *s, vec& simplifiers)
+{
+  vec oper_lists = s->oper_lists;
+  if (oper_lists == vNULL)
+{
+  simplifiers.safe_push (s);
+  return;
+} 
+
+  unsigned min = oper_lists[0]->substitutes.length ();
+  for (unsigned i = 1; i < oper_lists.length (); ++i)
+if (min > oper_lists[i]->substitutes.length ())
+  min = oper_lists[i]->substitutes.length ();
+
+  for (unsigned i = 0; i < oper_lists.length (); ++i)
+if (oper_lists[i]->substitutes.length () % min != 0)
+  fatal_at (s->match_location, "All user-defined identifiers must have a multiple number "
+   "of operator substittions of the smallest number of substitutions");
+
+  vec< vec > for_vec = vNULL;
+  for_vec.safe_push (oper_lists);
+
+  simplify ns (s->match, s->match_location,
+	   s->result, s->result_location,
+	   s->ifexpr_vec, for_vec,
+	   s->capture_ids);
+
+  lower_for (&ns, simplifiers);
+}
+
+
 /* Lower the AST for everything in SIMPLIFIERS.  */
 
 static void
@@ -947,14 +993,15 @@
   for (unsigned i = 0; i < out_simplifiers0.length (); ++i)
 lower_commutative (out_simplifiers0[i], out_simplifiers1);
 
+  auto_vec out_simplifiers2;
+  for (unsigned i = 0;  i < out_simplifiers1.length (); ++i)
+

Re: [match-and-simplify] operator-lists in expression

2014-12-01 Thread Richard Biener
On Tue, Nov 11, 2014 at 9:55 AM, Richard Biener
 wrote:
> On Mon, Nov 10, 2014 at 2:39 PM, Prathamesh Kulkarni
>  wrote:
>> Hi,
>>   This patch adds support for operator-lists to be used in expression.
>>
>> I reuse operator-list as the iterator. This is not really valid since
>> user-defined operator-lists cannot be iterator in 'for', but it was
>> convenient to reuse operator-list as a 'for' iterator
>> and lower_for doesn't care about that.
>> eg:
>> (define_operator_list  list1 plus minus)
>>
>> (simplify
>>   (list1 @x integer_zerop)
>>   (non_lvalue @x))
>>
>> is wrapped into 'for' as: (lower_operator_list):
>> (for list1 (plus minus)
>>   (simplify
>> (list1 @x integer_zerop)
>> (non_lvalue @x)))
>>
>> this is not really valid since we reject list1 to be used as iterator if
>> it were written by user.
>>
>> Is this okay or should I introduce an explicit temporary iterator ?
>
> No, it's ok to re-use it.
>
> I think you should get rid of the extra lowering step and instead
> in parse_simplify create the extra for directly when building
> a simplify (the multiple simplfy buildings really ask for factoring
> it out to a method in the parser class which has access to
> active_fors, active_ifs and friends).
>
> Also you use a vector to store operator_lists - this will gobble
> up duplicates.  It's probably better to use a pointer_hash 
> for this.
>
> Thanks for continuing to work on this!

I have picked this up now and did the suggested changes.  I've also
removed the restriction on the use in for as we have one case
where that's useful in match-builtins.pd already.  I have for now
restricted the operator lists to combine a little more than for
regular fors - namely I require the same number of operators.

I am testing the following patch on trunk and hope to commit it there
tomorrow and then bring it back to the branch via a merge.

Bootstrap running on x86_64-unknown-linux-gnu (testing not neccessary
on trunk as the generated files are unchanged).  Bootstrap / test on
the branch running as well.

Richard.

2014-12-01  Richard Biener  
Prathamesh Kulkarni  

* genmatch.c: Include hash-set.h.
(fatal_at): Add source_location overload.
(parser::record_operlist): New method.
(parser::push_simplify): Likewise.
(parser::oper_lists_set): New member.
(parser::oper_lists): Likewise.
(parser::parse_operation): Record seen operator list references.
(parser::parse_c_expr): Likewise.
(parser::parse_simplify): Init oper_lists_set and oper_lists
and use push_simplify.
(parser::parser): Init oper_lists_set and oper_lists.


> Richard.
>
>> so it gets lowered to something like:
>> (for tmp1 (list1)
>>   (simplify
>> (tmp1 @x integer_zerop)
>> (non_lvalue @x)))
>>
>> * genmatch.c
>>   (fatal_at): New overloaded function.
>>   (simplify::oper_lists): New member.
>>   (simplify::simplify): Add default argument.
>>   (lower_commutative): Adjust call to simplify::simplify.
>>   (lower_opt_convert): Likewise.
>>   (lower_operator_list): New function.
>>   (lower): Call lower_operator_list.
>>   (parser::parsing_for_p): New member function.
>>   (parser::oper_lists): New member.
>>   (parser::parse_operation): Check for operator-list.
>>   (parser::parse_c_expr): Likewise.
>>   (parser::parse_simplify): Reset parser::oper_lists.
>> Adjust call to simplify::simplify.
>>   (parser::parser): Initialize parser::oper_lists.
>>
>> * match-builtin.pd:
>>   Adjust patten to use SQRTs and POWs.
>>
>> Thanks,
>> Prathamesh


mas-oper-list-expr
Description: Binary data


Re: [match-and-simplify] operator-lists in expression

2014-11-11 Thread Richard Biener
On Mon, Nov 10, 2014 at 2:39 PM, Prathamesh Kulkarni
 wrote:
> Hi,
>   This patch adds support for operator-lists to be used in expression.
>
> I reuse operator-list as the iterator. This is not really valid since
> user-defined operator-lists cannot be iterator in 'for', but it was
> convenient to reuse operator-list as a 'for' iterator
> and lower_for doesn't care about that.
> eg:
> (define_operator_list  list1 plus minus)
>
> (simplify
>   (list1 @x integer_zerop)
>   (non_lvalue @x))
>
> is wrapped into 'for' as: (lower_operator_list):
> (for list1 (plus minus)
>   (simplify
> (list1 @x integer_zerop)
> (non_lvalue @x)))
>
> this is not really valid since we reject list1 to be used as iterator if
> it were written by user.
>
> Is this okay or should I introduce an explicit temporary iterator ?

No, it's ok to re-use it.

I think you should get rid of the extra lowering step and instead
in parse_simplify create the extra for directly when building
a simplify (the multiple simplfy buildings really ask for factoring
it out to a method in the parser class which has access to
active_fors, active_ifs and friends).

Also you use a vector to store operator_lists - this will gobble
up duplicates.  It's probably better to use a pointer_hash 
for this.

Thanks for continuing to work on this!

Richard.

> so it gets lowered to something like:
> (for tmp1 (list1)
>   (simplify
> (tmp1 @x integer_zerop)
> (non_lvalue @x)))
>
> * genmatch.c
>   (fatal_at): New overloaded function.
>   (simplify::oper_lists): New member.
>   (simplify::simplify): Add default argument.
>   (lower_commutative): Adjust call to simplify::simplify.
>   (lower_opt_convert): Likewise.
>   (lower_operator_list): New function.
>   (lower): Call lower_operator_list.
>   (parser::parsing_for_p): New member function.
>   (parser::oper_lists): New member.
>   (parser::parse_operation): Check for operator-list.
>   (parser::parse_c_expr): Likewise.
>   (parser::parse_simplify): Reset parser::oper_lists.
> Adjust call to simplify::simplify.
>   (parser::parser): Initialize parser::oper_lists.
>
> * match-builtin.pd:
>   Adjust patten to use SQRTs and POWs.
>
> Thanks,
> Prathamesh