This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Transformation of relational expression
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: Dmytro Sheyko <dmytro dot sheyko at gmail dot com>
- Cc: GCC Development <gcc at gcc dot gnu dot org>
- Date: Tue, 23 Feb 2016 12:53:51 +0100
- Subject: Re: Transformation of relational expression
- Authentication-results: sourceware.org; auth=none
- References: <CANVEV5VnWB9cSrrgtQkaJ=e+mSULMJUUN_3vfnTUjKKMuVHBkQ at mail dot gmail dot com>
On Tue, Feb 23, 2016 at 11:39 AM, Dmytro Sheyko <dmytro.sheyko@gmail.com> wrote:
> Hello,
>
> I am compiling code like this:
>
> int test(int x, int eq, int lt, int gt) {
> return x < 2000 ? lt : x > 2000 ? gt : eq;
> }
>
> and expect that compiler would generate one CMP instruction for both
> comparisons:
>
> cmpl $2000, %edi
> jl .L37
> jne .L38
> ...
>
> however result is quite different:
>
> cmpl $1999, %edi
> jle .L37
> cmpl $2000, %edi
> jne .L38
>
> For some reason, compiler transforms (x < 2000) to (x <= 1999). Though
> such transformation looks semantically legal, not sure it is really
> necessary here. Especially taking into account that it impedes
> subsequent optimizations.
>
> It seems that 'maybe_canonicalize_comparison' from gcc/fold-const.c is
> responsible for such transformation (I am using 5.3.0, as for trunk it
> seems that some part of such logic is moved to gcc/match.pd).
>
> I wonder why such transformation is necessary. What benefits does it offer?
Canonicalization with x <= 1999 and thus simplifying of x < 2000 && x <= 1999
for example.
I don't see how we can get a single compare at GIMPLE so it must be RTL
maybe in compare-elim.c doing the recognition.
Richard.
> Thank you,
> Dmytro