Bug 22199 - fold does not optimize (int)ABS_EXPR<(long long)(int_var)>
Summary: fold does not optimize (int)ABS_EXPR<(long long)(int_var)>
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.0
: P2 enhancement
Target Milestone: 9.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: 19987 64946
  Show dependency treegraph
 
Reported: 2005-06-27 15:04 UTC by Thomas Koenig
Modified: 2021-06-03 03:37 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-09-24 16:22:48


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2005-06-27 15:04:44 UTC
$ cat labs.c
extern long int labs (long int __x) __attribute__ ((__const__));

int main()
{
    int a,b;
    foo(&a, &b);
    if (labs(a) > b)
        return 1;
    else
        return 0;
}

is translated with

$ gcc -O3 -fdump-tree-optimized -S labs.c

into

<bb 0>:
  foo (&a, &b);
  return (int) (ABS_EXPR <(long int) a> > (long int) b);

The casts aren't necessary in this case.
Comment 1 Andrew Pinski 2005-06-27 15:17:41 UTC
A better example as on 32bit targets long and int are the same size which removes the casts:
extern long long int llabs (long long int __x) __attribute__ ((__const__));

int main()
{
    int a,b;
    foo(&a, &b);
    if (llabs(a) > b)
        return 1;
    else
        return 0;
}

Or just as good:
extern int abs (int __x) __attribute__ ((__const__));


void foo(short *, short*);

int main()
{
    short a,b;
    foo(&a, &b);
    if (abs(a) > b)
        return 1;
    else
        return 0;
}

Though only the first one will have different asm and will be better optimized.
Comment 2 Andrew Pinski 2005-09-24 16:22:48 UTC
Changing the summary to be more reflect what this bug is about.
Comment 3 Andrew Pinski 2005-12-24 20:15:07 UTC
Hmm, actually this is not really valid to do except when -fwrapv is supplied as (int)ABS_EXPR<(long long) 0x80000000 > is defined to be 0 but ABS_EXPR< (int)0x80000000> is undefined.
Comment 4 Andrew Pinski 2021-06-03 03:35:41 UTC
There is ABSU now.
Which we use now:
  a.0_1 = a;
  _2 = ABSU_EXPR <a.0_1>;
  _3 = (long int) _2;
  b.1_4 = b;

So fixed.
Comment 5 Andrew Pinski 2021-06-03 03:37:43 UTC
Fixed with r9-1281 by the way.