This is the mail archive of the
mailing list for the GCC project.
Re: Why is unsigned type introduced in a simple case?
- From: pinskia at gmail dot com
- To: "Bin.Cheng" <amker dot cheng at gmail dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Mon, 14 Jul 2014 09:45:21 -0700
- Subject: Re: Why is unsigned type introduced in a simple case?
- Authentication-results: sourceware.org; auth=none
- References: <CAHFci2_pvVcXGwuC8aMH2=2Re-Fi5HLu0cFZxTDkvYxntZjDwg at mail dot gmail dot com>
> On Jul 14, 2014, at 8:57 AM, "Bin.Cheng" <firstname.lastname@example.org> wrote:
> For a simple example like below.
> f1 (int p, short i, short n)
> int sum = 0;
> sum += i;
This here is the same as i = i + 1; which is really
i = (short)(((int)i) + 1);
So it gets convert to using unsigned short to avoid undefined overflow ness of signed types (which is part of gcc's ir).
> while (i < n);
> return sum;
> When compiling with -O2 -fdump-tree-all options, GCC introduces
> unsigned type at the very beginning of gimple pass, for example, the
> dump for gimple pass is like below.
> f1 (int p, short int i, short int n)
> int D.4116;
> short int i.0;
> unsigned short i.1;
> unsigned short D.4119;
> int D.4120;
> int sum;
> sum = 0;
> D.4116 = (int) i;
> sum = D.4116 + sum;
> i.0 = i;
> i.1 = (unsigned short) i.0;
> D.4119 = i.1 + 1;
> i = (short int) D.4119;
> if (i < n) goto <D.4111>; else goto <D.4112>;
> D.4120 = sum;
> return D.4120;
> It uses i.1 to increase the induction variable and converts it back to
> signed type for comparison. Is it designed behavior? &why?