Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-08-06 Thread Victor Tong via Gcc-patches
Thanks for the feedback. Here's the updated pattern:

  /* X - (X - Y) --> Y */
  (simplify
(minus (convert1? @0) (convert2? (minus@2 (convert3? @@0) @1)))
(if (ANY_INTEGRAL_TYPE_P (type)
&& TYPE_OVERFLOW_UNDEFINED(type)
&& !TYPE_OVERFLOW_SANITIZED(type)
&& ANY_INTEGRAL_TYPE_P (TREE_TYPE(@2))
&& TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@2))
&& !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@2))
&& ANY_INTEGRAL_TYPE_P (TREE_TYPE(@0))
&& TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@0))
&& !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@0))
&& TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
&& TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type))
(convert @1)))

I kept the TYPE_OVERFLOW_SANITIZED checks because I saw other patterns that 
leverage undefined overflows check for it. I think this new pattern shouldn't 
be applied if overflow sanitizer checks are enabled.

>> why is this testing TREE_TYPE (@0)?

I'm checking the type of @0 because I'm concerned that there could be a case 
where @0's type isn't an integer type with undefined overflow. I tried creating 
a test case and couldn't seem to create one where this is violated but I kept 
the checks to avoid causing a regression. If I'm being overcautious and you 
feel that the type checks on @0 aren't needed, I can remove them. I think the 
precision check on TREE_TYPE(@0) is needed to avoid truncation cases though.

>> Once we'd "inline" nop_convert genmatch would complain
about this.

Is someone working on inlining nop_convert? I'd like to avoid breaking someone 
else's work if that's being worked on right now.

I've also added some extra tests to cover this new pattern. I've attached a 
patch with my latest changes.


From: Richard Biener 
Sent: Wednesday, July 28, 2021 2:59 AM
To: Victor Tong 
Cc: Marc Glisse ; gcc-patches@gcc.gnu.org 

Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division 
followed by multiply [PR95176] 
 
On Tue, Jun 29, 2021 at 1:10 AM Victor Tong  wrote:
>
> Thanks Richard and Marc.
>
> I wrote the following test case to compare the outputs of fn1() and 
> fn1NoOpt() below with my extra pattern being applied. I tested the two 
> functions with all of the integers from INT_MIN to INT_MAX.
>
> long
> fn1 (int x)
> {
>   return 42L - (long)(42 - x);
> }
>
> #pragma GCC push_options
> #pragma GCC optimize ("O0")
> long
> fn1NoOpt (int x)
> {
>   volatile int y = (42 - x);
>   return 42L - (long)y;
> }
> #pragma GCC pop_options
>
> int main ()
> {
> for (long i=INT_MIN; i<=INT_MAX;i++)
> {
> auto valNoOpt = fn1NoOpt(i);
> auto valOpt = fn1(i);
> if (valNoOpt != valOpt)
> printf("valOpt=%ld, valNoOpt=%ld\n", valOpt, 
>valNoOpt);
> }
> return 0;
> }
>
> I saw that the return values of fn1() and fn1NoOpt() differed when the input 
> was between INT_MIN and INT_MIN+42 inclusive. When passing values in this 
> range to fn1NoOpt(), a signed overflow is triggered which causes the value to 
> differ (undefined behavior). This seems to go in line with what Marc 
> described and I think the transformation is correct in the scenario above. I 
> do think that type casts that result in truncation (i.e. from a higher 
> precision to a lower one) or with unsigned types will result in an incorrect 
> transformation so those scenarios need to be avoided.
>
> Given that the extra pattern I'm adding is taking advantage the undefined 
> behavior of signed integer overflow, I'm considering keeping the existing 
> nop_convert pattern in place and adding a new pattern to cover these new 
> cases. I'd also like to avoid touching nop_convert given that it's used in a 
> number of other patterns.
>
> This is the pattern I have currently:
>
>   (simplify
> (minus (convert1? @0) (convert2? (minus (convert3? @2) @1)))
> (if (operand_equal_p(@0, @2, 0)

The operand_equal_p should be reflected by using @0 in place of @2.

> && INTEGRAL_TYPE_P (type)
> && TYPE_OVERFLOW_UNDEFINED(type)
> && !TYPE_OVERFLOW_SANITIZED(type)
> && INTEGRAL_TYPE_P (TREE_TYPE(@1))
> && TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@1))
> && !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@1))
> && !TYPE_UNSIGNED (TREE_TYPE (@1))
> && !TYPE_UNSIGNED (type)

please group checks on common argument.  I think a single test
on INTEGRAL_TYPE_P (type) is enough, it could be ANY_INTEGRAL_TYPE_P
to include vector and complex types.

> && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type)
> && INTEGRAL_TYPE_P (TREE_TYPE(@0))
> && TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@0))

why is this testing TREE_TYPE (@0)?  The outer subtract is using 'type',
the inner subtract uses TREE_TYPE (@1) though you could place
a capture on the minus to make what you test more obvious, like

  (minus (convert1? @0) (convert2? (minus@3 (convert3? @2) @1)))

TYPE_OVERFLOW_

Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-08-22 Thread Victor Tong via Gcc-patches
Thanks for the feedback. I updated the pattern and it passes all tests 
(existing and the new ones I wrote). I added some brackets since there were 
some warnings about missing brackets on the || and &&. Here's the updated 
pattern:

  (simplify
(minus (convert1? @0) (convert2? (minus@2 (convert3? @@0) @1)))
(if (INTEGRAL_TYPE_P (type)
&& !TYPE_OVERFLOW_SANITIZED(type)
&& INTEGRAL_TYPE_P (TREE_TYPE(@2))
&& !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@2))
&& INTEGRAL_TYPE_P (TREE_TYPE(@0))
&& !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@0))
&& 
 (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@2)) ||
   (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)) &&
 (TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (TREE_TYPE (@2)) 
||
 (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE 
(@2)) &&
  !TYPE_UNSIGNED (TREE_TYPE (@0)))
 
(convert @1)))


>>> TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@2))

Did you mean > instead of <=? With the condition you proposed, that would 
trigger the optimization in cases where values may get truncated which I think 
should be avoided for this optimization.

>>> Maybe the new transform could be about scalars, and we could restrict the 
>>> old one to vectors, to simplify the code,

I tried limiting the existing pattern to vector types by changing it to the 
following:

  (simplify
   (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
   (if (VECTOR_TYPE_P(type))
   (view_convert @1)))
   
I found that the new pattern doesn't cover some cases. Specifically, it doesn't 
cover a case in pr92734-2.c:

unsigned
f10 (unsigned x, int y)
{
  unsigned a = (int) x - y;
  return x - a;
}

I think the pattern isn't triggering because of the !TYPE_UNSIGNED (TREE_TYPE 
(@0)) check. I'm slightly concerned that changing the new pattern to cover the 
existing cases would add complexity to the new pattern, making it difficult to 
understand.

I also think the new pattern could be simplified by removing the convert on @0. 
I don't think it's needed for the regression pattern that I was seeing, but I 
had added it to be more thorough so the pattern covers more cases. 

From: Richard Biener 
Sent: Monday, August 9, 2021 2:58 AM
To: Marc Glisse 
Cc: Victor Tong ; gcc-patches@gcc.gnu.org 

Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division 
followed by multiply [PR95176] 
 
On Sat, Aug 7, 2021 at 12:49 AM Marc Glisse  wrote:
>
> On Fri, 6 Aug 2021, Victor Tong wrote:
>
> > Thanks for the feedback. Here's the updated pattern:
> >
> >  /* X - (X - Y) --> Y */
> >  (simplify
> >    (minus (convert1? @0) (convert2? (minus@2 (convert3? @@0) @1)))
> >    (if (ANY_INTEGRAL_TYPE_P (type)
> >    && TYPE_OVERFLOW_UNDEFINED(type)
> >    && !TYPE_OVERFLOW_SANITIZED(type)
> >    && ANY_INTEGRAL_TYPE_P (TREE_TYPE(@2))
> >    && TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@2))
> >    && !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@2))
> >    && ANY_INTEGRAL_TYPE_P (TREE_TYPE(@0))
> >    && TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@0))
> >    && !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@0))
> >    && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
> >    && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type))
> >    (convert @1)))
> >
> > I kept the TYPE_OVERFLOW_SANITIZED checks because I saw other patterns that 
> > leverage undefined overflows check for it. I think this new pattern 
> > shouldn't be applied if overflow sanitizer checks are enabled.
> >
> >>> why is this testing TREE_TYPE (@0)?
> >
> > I'm checking the type of @0 because I'm concerned that there could be a 
> > case where @0's type isn't an integer type with undefined overflow. I tried 
> > creating a test case and couldn't seem to create one where this is violated 
> > but I kept the checks to avoid causing a regression. If I'm being 
> > overcautious and you feel that the type checks on @0 aren't needed, I can 
> > remove them. I think the precision check on TREE_TYPE(@0) is needed to 
> > avoid truncation cases though.
>
> It doesn't matter if @0 has undefined overflow, but it can matter that it
> be signed (yes, the 2 are correlated...) if it has the same precision as
> @2. Otherwise (int64_t)(-1u)-(int64_t)((int)(-1u)-0) is definitely not 0
> and it has type:int64_t, @2:int, @0:unsigned.
>
> Ignoring the sanitizer, the confusing double matching of constants, and
> restricting to scalars, I think the tightest condition (without vrp) that
> works is
>
> TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@2)) ||
>   TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)) &&
>    (TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (TREE_TYPE (@2)) ||
> TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@2)) &&
> !TYPE_UNSIGNED (TREE_TYPE (@0))
>    )
>
> (where implicitly undefined => signed) but of course it is ok to handle
> only a subset. It is too late for me to think abou

Re: [EXTERNAL] Re: [PATCH] Propagate get_nonzero_bits information in division [PR77980]

2021-08-30 Thread Victor Tong via Gcc-patches
Thanks Jeff. I've reached out to Roger to see if the fix would be better suited 
in CCP. If that isn't the right spot, I'll reach out to Aldy and Andrew about 
getting the fix in VRP/Ranger.


From: Jeff Law 
Sent: Sunday, August 22, 2021 8:11 PM
To: Victor Tong ; gcc-patches@gcc.gnu.org 
; pins...@gcc.gnu.org 
Subject: [EXTERNAL] Re: [PATCH] Propagate get_nonzero_bits information in 
division [PR77980] 
 


On 7/26/2021 6:45 PM, Victor Tong via Gcc-patches wrote:
> This change enables the "t1 != 0" check to be optimized away in this code:
>
> int x1 = 0;
> unsigned int x2 = 1;
>
> int main ()
> {
>  int t1 = x1*(1/(x2+x2));
>  if (t1 != 0) __builtin_abort();
>  return 0;
> }
>
> The change utilizes the VRP framework to propagate the get_nonzero_bits 
> information from the "x2+x2" expression to the "1/(x2+x2)" division 
> expression. Specifically, the framework knows that the least significant bit 
> of the "x2+x2" expression must be zero.
>
> The get_nonzero_bits information of the left hand side and right hand side of 
> expressions needed to be passed down to operator_div::wi_fold() in the VRP 
> framework. The majority of this change involves adding two additional 
> parameters to propagate this information. There are future opportunities to 
> use the non zero bit information to perform better optimizations in other 
> types of expressions.
>
> The changes were tested against x86_64-pc-linux-gnu and all tests in "make -k 
> check" passed.
>
> The original approach was to implement a match.pd pattern to support this but 
> the pattern wasn't being triggered. More context is available in: 
> https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fbugzilla%2Fshow_bug.cgi%3Fid%3D77980&data=04%7C01%7Cvitong%40microsoft.com%7C08dcd27e5482418d559708d965e3a826%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637652850726534114%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=S1WWWvuXwEIA20pmv%2Fn8kk6xlwv9VwsS%2BTl0jqEHf4w%3D&reserved=0
So you're going to want to sync with Aldy & Andrew as they're the 
experts on the Ranger design & implementation.  This hits the Ranger API 
as well as design questions about how best to tie in the nonzero_bits 
capabilities.

You might also want to reach out to Roger Sayle.  He's been poking 
around in a closely related area, though more focused on the bitwise 
conditional constant propagation rather than Ranger/VRP.  In fact, I 
just acked a patch of his that looks closely related.

https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgcc.gnu.org%2Fpipermail%2Fgcc-patches%2F2021-August%2F577888.html&data=04%7C01%7Cvitong%40microsoft.com%7C08dcd27e5482418d559708d965e3a826%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637652850726534114%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=gn1L1exTW5TjdV8CDBDqB0Z5a4V7EP2tBM2uTF2ihck%3D&reserved=0

Jeff

Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-06-02 Thread Victor Tong via Gcc-patches
Hi Richard,

Thanks for reviewing my patch. I did a search online and you're right -- there 
isn't a vector modulo instruction. I'll remove the X * (Y / X) --> Y - (Y % X) 
pattern and the existing X - (X / Y) * Y --> X % Y from triggering on vector 
types.

I looked into why the following pattern isn't triggering:

  (simplify
   (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
   (view_convert @1))

The nop_converts expand into tree_nop_conversion_p checks. In fn2() of the 
testsuite/gcc.dg/fold-minus-6.c, the expression during generic matching looks 
like: 

42 - (long int) (42 - 42 % x)

When looking at the right-hand side of the expression (the (long int) (42 - 42 
% x)), the tree_nop_conversion_p check fails because of the type precision 
difference. The expression inside of the cast has a 32-bit precision and the 
outer expression has a 64-bit precision.

I looked around at other patterns and it seems like nop_convert and 
view_convert are used because of underflow/overflow concerns. I'm not familiar 
with the two constructs. What's the difference between using them and checking 
TYPE_OVERFLOW_UNDEFINED? In the scenario above, since TYPE_OVERFLOW_UNDEFINED 
is true, the second pattern that I added (X - (X - Y) --> Y) gets triggered.

Thanks,
Victor


From: Richard Biener 
Sent: Tuesday, April 27, 2021 1:29 AM
To: Victor Tong 
Cc: gcc-patches@gcc.gnu.org 
Subject: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed 
by multiply [PR95176] 
 
On Thu, Apr 1, 2021 at 1:03 AM Victor Tong via Gcc-patches
 wrote:
>
> Hello,
>
> This patch fixes PR tree-optimization/95176. A new pattern in match.pd was 
> added to transform "a * (b / a)" --> "b - (b % a)". A new test case was also 
> added to cover this scenario.
>
> The new pattern interfered with the existing pattern of "X - (X / Y) * Y". In 
> some cases (such as in fn4() in gcc/testsuite/gcc.dg/fold-minus-6.c), the new 
> pattern is applied causing the existing pattern to no longer apply. This 
> results in worse code generation because the expression is left as "X - (X - 
> Y)". An additional subtraction pattern of "X - (X - Y) --> Y" was added to 
> this patch to avoid this regression.
>
> I also didn't remove the existing pattern because it triggered in more cases 
> than the new pattern because of a tree_invariant_p check that's inserted by 
> genmatch for the new pattern.

Yes, we do not handle using Y multiple times when it might contain
side-effects in GENERIC folding
(comments in genmatch suggest we can use save_expr but we don't
implement this [anymore]).

On GIMPLE there's also the issue that your new pattern creates a
complex expression which
makes it failed to be used by value-numbering for example where the
old pattern was OK
(eventually, if no conversion was required).

So indeed it looks OK to preserve both.

I wonder why you needed the

+/* X - (X - Y) --> Y */
+(simplify
+ (minus (convert1? @0) (convert2? (minus @@0 @1)))
+ (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) &&
TYPE_OVERFLOW_UNDEFINED(type))
+  (convert @1)))

pattern since it should be handled by

  /* Match patterns that allow contracting a plus-minus pair
 irrespective of overflow issues.  */
  /* (A +- B) - A   ->  +- B */
  /* (A +- B) -+ B  ->  A */
  /* A - (A +- B)   -> -+ B */
  /* A +- (B -+ A)  ->  +- B */

in particular

  (simplify
   (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
   (view_convert @1))

if there's supported cases missing I'd rather extend this pattern than
replicating it.

+/* X * (Y / X) is the same as Y - (Y % X).  */
+(simplify
+ (mult:c (convert1? @0) (convert2? (trunc_div @1 @@0)))
+ (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
+  (minus (convert @1) (convert (trunc_mod @1 @0)

note that if you're allowing vector types you have to use
(view_convert ...) in the
transform and you also need to make sure that the target can expand
the modulo - I suspect that's an issue with the existing pattern as well.
I don't know of any vector ISA that supports modulo (or integer
division, that is).
Restricting the patterns to integer types is probably the most
sensible solution.

Thanks,
Richard.

> I verified that all "make -k check" tests pass when targeting 
> x86_64-pc-linux-gnu.
>
> 2021-03-31  Victor Tong  
>
> gcc/ChangeLog:
>
> * match.pd: Two new patterns: One to optimize division followed by 
>multiply and the other to avoid a regression as explained above
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/20030807-10.c: Update existing test to look for a 
>subtraction because a shift is no longer emitted
> * gcc.dg/pr95176.c: New test to cover optimizing division foll

Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-07-19 Thread Victor Tong via Gcc-patches
Gentle ping.

From: Gcc-patches  on 
behalf of Victor Tong via Gcc-patches 
Sent: Monday, June 28, 2021 4:10 PM
To: Richard Biener ; Marc Glisse 

Cc: gcc-patches@gcc.gnu.org 
Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division 
followed by multiply [PR95176]

​Thanks Richard and Marc.

I wrote the following test case to compare the outputs of fn1() and fn1NoOpt() 
below with my extra pattern being applied. I tested the two functions with all 
of the integers from INT_MIN to INT_MAX.

long
fn1 (int x)
{
  return 42L - (long)(42 - x);
}

#pragma GCC push_options
#pragma GCC optimize ("O0")
long
fn1NoOpt (int x)
{
  volatile int y = (42 - x);
  return 42L - (long)y;
}
#pragma GCC pop_options

int main ()
{
for (long i=INT_MIN; i<=INT_MAX;i++)
{
auto valNoOpt = fn1NoOpt(i);
auto valOpt = fn1(i);
if (valNoOpt != valOpt)
printf("valOpt=%ld, valNoOpt=%ld\n", valOpt, valNoOpt);
}
return 0;
}

I saw that the return values of fn1() and fn1NoOpt() differed when the input 
was between INT_MIN and INT_MIN+42 inclusive. When passing values in this range 
to fn1NoOpt(), a signed overflow is triggered which causes the value to differ 
(undefined behavior). This seems to go in line with what Marc described and I 
think the transformation is correct in the scenario above. I do think that type 
casts that result in truncation (i.e. from a higher precision to a lower one) 
or with unsigned types will result in an incorrect transformation so those 
scenarios need to be avoided.

Given that the extra pattern I'm adding is taking advantage the undefined 
behavior of signed integer overflow, I'm considering keeping the existing 
nop_convert pattern in place and adding a new pattern to cover these new cases. 
I'd also like to avoid touching nop_convert given that it's used in a number of 
other patterns.

This is the pattern I have currently:

  (simplify
(minus (convert1? @0) (convert2? (minus (convert3? @2) @1)))
(if (operand_equal_p(@0, @2, 0)
&& INTEGRAL_TYPE_P (type)
&& TYPE_OVERFLOW_UNDEFINED(type)
&& !TYPE_OVERFLOW_SANITIZED(type)
&& INTEGRAL_TYPE_P (TREE_TYPE(@1))
&& TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@1))
&& !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@1))
&& !TYPE_UNSIGNED (TREE_TYPE (@1))
&& !TYPE_UNSIGNED (type)
&& TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type)
&& INTEGRAL_TYPE_P (TREE_TYPE(@0))
&& TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@0))
&& !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@0))
&& !TYPE_UNSIGNED (TREE_TYPE (@0))
&& TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
&& TREE_TYPE(@1) == TREE_TYPE(@2))
(convert @1)))

Is there a more concise/better way of writing the pattern? I was looking for 
similar checks in match.pd and I couldn't find anything that I could leverage.

I also kept my pattern to the specific scenario I'm seeing with the regression 
to lower the risk of something breaking. I've limited @1 and @2 to have the 
same type.

I'm also in favor of adding/running computer verification to make sure the 
transformation is legal. I've written some tests to verify that the pattern is 
being applied in the right scenarios and not being applied in others, but I 
think there are too many possibilities to manually write them all. Is there 
anything in GCC that can be used to verify that match.pd transformations are 
correct? I'm thinking of something like Alive 
https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FAliveToolkit%2Falive2&data=04%7C01%7Cvitong%40microsoft.com%7Cba7d8f9f9b774462148608d93a8a0471%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637605186726283785%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=ugx%2FTw58OPjLzamE9yqQThV5u4EfQ8JLrurnIy00AzQ%3D&reserved=0.

Thanks,
Victor



From: Richard Biener 
Sent: Monday, June 21, 2021 12:08 AM
To: Marc Glisse 
Cc: Victor Tong ; gcc-patches@gcc.gnu.org 

Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division 
followed by multiply [PR95176]

On Sat, Jun 19, 2021 at 7:05 PM Marc Glisse  wrote:
>
> On Fri, 18 Jun 2021, Richard Biener wrote:
>
> >> Option 2: Add a new pattern to support scenarios that the existing 
> >> nop_convert pattern bails out on.
> >>
> >> Existing pattern:
> >>
> >> (simplify
> >>(minus (nop_convert1? @0) (nop_convert2? (minus (nop_convert3? @@0) 
> >> @1)))
> >>(view_convert @1))
>
> I tried to check with a program when
>
> T3 x;
>

[PATCH] Propagate get_nonzero_bits information in division [PR77980]

2021-07-26 Thread Victor Tong via Gcc-patches
This change enables the "t1 != 0" check to be optimized away in this code:

int x1 = 0;
unsigned int x2 = 1;

int main ()
{
int t1 = x1*(1/(x2+x2));
if (t1 != 0) __builtin_abort();
return 0;
}

The change utilizes the VRP framework to propagate the get_nonzero_bits 
information from the "x2+x2" expression to the "1/(x2+x2)" division expression. 
Specifically, the framework knows that the least significant bit of the "x2+x2" 
expression must be zero.

The get_nonzero_bits information of the left hand side and right hand side of 
expressions needed to be passed down to operator_div::wi_fold() in the VRP 
framework. The majority of this change involves adding two additional 
parameters to propagate this information. There are future opportunities to use 
the non zero bit information to perform better optimizations in other types of 
expressions.

The changes were tested against x86_64-pc-linux-gnu and all tests in "make -k 
check" passed.

The original approach was to implement a match.pd pattern to support this but 
the pattern wasn't being triggered. More context is available in: 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77980

Thanks,
Victor

pr77980.patch
Description: pr77980.patch


[PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-03-31 Thread Victor Tong via Gcc-patches
Hello,

This patch fixes PR tree-optimization/95176. A new pattern in match.pd was 
added to transform "a * (b / a)" --> "b - (b % a)". A new test case was also 
added to cover this scenario.

The new pattern interfered with the existing pattern of "X - (X / Y) * Y". In 
some cases (such as in fn4() in gcc/testsuite/gcc.dg/fold-minus-6.c), the new 
pattern is applied causing the existing pattern to no longer apply. This 
results in worse code generation because the expression is left as "X - (X - 
Y)". An additional subtraction pattern of "X - (X - Y) --> Y" was added to this 
patch to avoid this regression.

I also didn't remove the existing pattern because it triggered in more cases 
than the new pattern because of a tree_invariant_p check that's inserted by 
genmatch for the new pattern.

I verified that all "make -k check" tests pass when targeting 
x86_64-pc-linux-gnu.

2021-03-31  Victor Tong   

gcc/ChangeLog:

* match.pd: Two new patterns: One to optimize division followed by 
multiply and the other to avoid a regression as explained above

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/20030807-10.c: Update existing test to look for a 
subtraction because a shift is no longer emitted
* gcc.dg/pr95176.c: New test to cover optimizing division followed by 
multiply

I don't have write access to the GCC repo but I've completed the FSF paperwork 
as I plan to make more contributions in the future. I'm looking for a 
sponsorship from an existing GCC maintainer before applying for write access.

Thanks,
Victor

pr95176.patch
Description: pr95176.patch


Patch ping for PR95176 fix

2021-04-12 Thread Victor Tong via Gcc-patches
Hello,

I'd like to ping this patch. It contains two new tree-opt patterns in match.pd.

[PATCH] tree-optimization: Optimize division followed by multiply [PR95176] 
(gnu.org)

Thanks,
Victor


Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-06-16 Thread Victor Tong via Gcc-patches
Hi Richard,

Thanks for the feedback. From what you said, I can think of two possible 
solutions (though I'm not sure if either is feasible/fully correct):

Option 1: Have the new X * (Y / X) --> Y - (Y % X) optimization only run in 
scenarios that don't interfere with the existing X - (X / Y) * Y --> X % Y 
optimization. 

This would involve checking the expression one level up to see if there's a 
subtraction that would trigger the existing optimization. I looked through the 
match.pd file and couldn't find a bail condition like this. It doesn't seem 
like there's a link from an expression to its parent expression one level up. 
This also feels a bit counter-intuitive since it would be doing the opposite of 
the bottom-up expression matching where the compiler would like to match a 
larger expression rather than a smaller one.

Option 2: Add a new pattern to support scenarios that the existing nop_convert 
pattern bails out on.

Existing pattern:

(simplify
   (minus (nop_convert1? @0) (nop_convert2? (minus (nop_convert3? @@0) @1)))
   (view_convert @1))

New pattern to add:

  /* X - (X - Y) --> Y */
  (simplify
  (minus @0 (convert? (minus @@0 @1)))
  (if (INTEGRAL_TYPE_P (type) 
&& TYPE_OVERFLOW_UNDEFINED(type)
&& INTEGRAL_TYPE_P (TREE_TYPE(@1))
&& TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@1))
&& !TYPE_UNSIGNED (TREE_TYPE (@1))
&& !TYPE_UNSIGNED (type)
&& TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
(convert @1)))

I think the truncation concerns that you brought up should be covered if the 
external expression type precision is greater than or equal to the internal 
expression type. There may be a sign extension operation (which is why the 
nop_convert check fails) but that shouldn't affect the value of the expression. 
And if the types involved are signed integers where overflow/underflow results 
in undefined behavior, the X - (X - Y) --> Y optimization should be legal.

Please correct me if I'm wrong with either one of these options, or if you can 
think of a better option to fix the regression.

Thanks,
Victor




From: Richard Biener 
Sent: Monday, June 7, 2021 1:25 AM
To: Victor Tong 
Cc: gcc-patches@gcc.gnu.org 
Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division 
followed by multiply [PR95176] 
 
On Wed, Jun 2, 2021 at 10:55 PM Victor Tong  wrote:
>
> Hi Richard,
>
> Thanks for reviewing my patch. I did a search online and you're right -- 
> there isn't a vector modulo instruction. I'll remove the X * (Y / X) --> Y - 
> (Y % X) pattern and the existing X - (X / Y) * Y --> X % Y from triggering on 
> vector types.
>
> I looked into why the following pattern isn't triggering:
>
>   (simplify
>    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
>    (view_convert @1))
>
> The nop_converts expand into tree_nop_conversion_p checks. In fn2() of the 
> testsuite/gcc.dg/fold-minus-6.c, the expression during generic matching looks 
> like:
>
> 42 - (long int) (42 - 42 % x)
>
> When looking at the right-hand side of the expression (the (long int) (42 - 
> 42 % x)), the tree_nop_conversion_p check fails because of the type precision 
> difference. The expression inside of the cast has a 32-bit precision and the 
> outer expression has a 64-bit precision.
>
> I looked around at other patterns and it seems like nop_convert and 
> view_convert are used because of underflow/overflow concerns. I'm not 
> familiar with the two constructs. What's the difference between using them 
> and checking TYPE_OVERFLOW_UNDEFINED? In the scenario above, since 
> TYPE_OVERFLOW_UNDEFINED is true, the second pattern that I added (X - (X - Y) 
> --> Y) gets triggered.

But TYPE_OVERFLOW_UNDEFINED is not a good condition here since the
conversion is the problematic one and
conversions have implementation defined behavior.  Now, the above does
not match because it wasn't designed to,
and for non-constant '42' it would have needed a (convert ...) around
the first @0 as well (matching of constants is
by value, not by value + type).

That said, your

+/* X - (X - Y) --> Y */
+(simplify
+ (minus (convert1? @0) (convert2? (minus @@0 @1)))
+ (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) &&
TYPE_OVERFLOW_UNDEFINED(type))
+  (convert @1)))

would match (int)x - (int)(x - y) where you assert the outer subtract
has undefined behavior
on overflow but the inner subtract could wrap and the (int) conversion
can be truncating
or widening.  Is that really always a valid transform then?

Richard.

> Thanks,
> Victor
>
>
> From: Richard Biener 
> Sent: Tuesday, April 27, 2021 1:29 AM
> To: Victor Tong 
> Cc: gcc-patches@gcc.gnu.org 
> Subject: [EXTERNAL] R

Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division followed by multiply [PR95176]

2021-06-28 Thread Victor Tong via Gcc-patches
​Thanks Richard and Marc.

I wrote the following test case to compare the outputs of fn1() and fn1NoOpt() 
below with my extra pattern being applied. I tested the two functions with all 
of the integers from INT_MIN to INT_MAX.

long
fn1 (int x)
{
  return 42L - (long)(42 - x);
}

#pragma GCC push_options
#pragma GCC optimize ("O0")
long
fn1NoOpt (int x)
{
  volatile int y = (42 - x);
  return 42L - (long)y;
}
#pragma GCC pop_options

int main ()
{
for (long i=INT_MIN; i<=INT_MAX;i++)
{
auto valNoOpt = fn1NoOpt(i);
auto valOpt = fn1(i);
if (valNoOpt != valOpt)
printf("valOpt=%ld, valNoOpt=%ld\n", valOpt, valNoOpt);
}
return 0;
}

I saw that the return values of fn1() and fn1NoOpt() differed when the input 
was between INT_MIN and INT_MIN+42 inclusive. When passing values in this range 
to fn1NoOpt(), a signed overflow is triggered which causes the value to differ 
(undefined behavior). This seems to go in line with what Marc described and I 
think the transformation is correct in the scenario above. I do think that type 
casts that result in truncation (i.e. from a higher precision to a lower one) 
or with unsigned types will result in an incorrect transformation so those 
scenarios need to be avoided.

Given that the extra pattern I'm adding is taking advantage the undefined 
behavior of signed integer overflow, I'm considering keeping the existing 
nop_convert pattern in place and adding a new pattern to cover these new cases. 
I'd also like to avoid touching nop_convert given that it's used in a number of 
other patterns.

This is the pattern I have currently:

  (simplify
    (minus (convert1? @0) (convert2? (minus (convert3? @2) @1)))
    (if (operand_equal_p(@0, @2, 0)
        && INTEGRAL_TYPE_P (type)
        && TYPE_OVERFLOW_UNDEFINED(type)
        && !TYPE_OVERFLOW_SANITIZED(type)
        && INTEGRAL_TYPE_P (TREE_TYPE(@1))
        && TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@1))
        && !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@1))
        && !TYPE_UNSIGNED (TREE_TYPE (@1))
        && !TYPE_UNSIGNED (type)
        && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type)
        && INTEGRAL_TYPE_P (TREE_TYPE(@0))
        && TYPE_OVERFLOW_UNDEFINED(TREE_TYPE(@0))
        && !TYPE_OVERFLOW_SANITIZED(TREE_TYPE(@0))
        && !TYPE_UNSIGNED (TREE_TYPE (@0))
        && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
        && TREE_TYPE(@1) == TREE_TYPE(@2))
    (convert @1)))

Is there a more concise/better way of writing the pattern? I was looking for 
similar checks in match.pd and I couldn't find anything that I could leverage.

I also kept my pattern to the specific scenario I'm seeing with the regression 
to lower the risk of something breaking. I've limited @1 and @2 to have the 
same type.

I'm also in favor of adding/running computer verification to make sure the 
transformation is legal. I've written some tests to verify that the pattern is 
being applied in the right scenarios and not being applied in others, but I 
think there are too many possibilities to manually write them all. Is there 
anything in GCC that can be used to verify that match.pd transformations are 
correct? I'm thinking of something like Alive 
https://github.com/AliveToolkit/alive2.

Thanks,
Victor



From: Richard Biener 
Sent: Monday, June 21, 2021 12:08 AM
To: Marc Glisse 
Cc: Victor Tong ; gcc-patches@gcc.gnu.org 

Subject: Re: [EXTERNAL] Re: [PATCH] tree-optimization: Optimize division 
followed by multiply [PR95176] 
 
On Sat, Jun 19, 2021 at 7:05 PM Marc Glisse  wrote:
>
> On Fri, 18 Jun 2021, Richard Biener wrote:
>
> >> Option 2: Add a new pattern to support scenarios that the existing 
> >> nop_convert pattern bails out on.
> >>
> >> Existing pattern:
> >>
> >> (simplify
> >>    (minus (nop_convert1? @0) (nop_convert2? (minus (nop_convert3? @@0) 
> >>@1)))
> >>    (view_convert @1))
>
> I tried to check with a program when
>
> T3 x;
> T1 y;
> (T2)x-(T2)((T1)x-y)
>
> can be safely replaced with
>
> (T2)y
>
> From the output, it looks like this is safe when T1 is at least as large
> as T2. It is wrong when T1 is unsigned and smaller than T2. And when T1 is
> signed and smaller than T2, it is ok if T3 is the same type as T1 (signed
> then) or has strictly less precision (any sign), and not in other cases.
>
> Note that this is when signed implies undefined overflow and unsigned
> implies wrapping, and I wouldn't put too much faith in this recently
> dusted program. And it doesn't say how to write the match.pd pattern with
> '?', "@@", disabling it if TYPE_OVERFLOW_SANITIZED, etc.
>
> Mostly, I wanted to say that if we are going to go handle more than
> nop_convert for more than just 1 or 2 easy transformations, I think some
> kind of computer verification would be useful, it would save a lot of time
> and headaches.

True.  I wonder if auto-generating such tests from match.pd rules would
be a good project to w