This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH 2/3] Simplify wrapped binops
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: Robin Dapp <rdapp at linux dot vnet dot ibm dot com>
- Cc: "Bin.Cheng" <amker dot cheng at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 27 Jun 2017 14:14:30 +0200
- Subject: Re: [PATCH 2/3] Simplify wrapped binops
- Authentication-results: sourceware.org; auth=none
- References: <5790A709.4060804@linux.vnet.ibm.com> <CAFiYyc1s699beSC9bpDGxY_2ppenDLoP3nbjYSnThF1cp2YHDA@mail.gmail.com> <d6e24bfd-fc3e-4160-fe27-db3eda5b857c@linux.vnet.ibm.com> <CAFiYyc1AcFG8JzoO3LYW9iyGsPj+7Kv17weZp+U3vTaeiqdhKA@mail.gmail.com> <260c4925-29e2-d50a-871e-397e2f9f4efb@linux.vnet.ibm.com> <CAFiYyc2_WX+3vzX27iRO9byK4zoc8N43F-d=Cg2xXoHwLRTgGQ@mail.gmail.com> <CAHFci299r3v3jbaZn0pzKPmk+yvF=Hwnbq+hxYGJCz-y8Qx0jg@mail.gmail.com> <803f5629-2a68-db02-a3a1-16fbe656f242@linux.vnet.ibm.com> <CAHFci2-zFEjPa4TokPca8EDF4zM5PU+0B8maFKa189=Pyrcj7Q@mail.gmail.com> <57eda9e0-8fbb-7508-f41e-d98000314012@linux.vnet.ibm.com> <CAHFci2-VJu5SbWzm43uozxRq2L=4Sxp0L573y-vPeK69RX_iaA@mail.gmail.com> <bb91b7d9-2d8d-72af-1741-bb5d97e64466@linux.vnet.ibm.com> <CAHFci2_GZb1k9DSrX1MR2ZUSLFQRxxAhRr4ywkfv0_PUrzn0VQ@mail.gmail.com> <3bb74016-ea97-53ba-348e-51113418e37c@linux.vnet.ibm.com> <CAFiYyc3HTUTAt9ETYqW9LB3i82XzYmN33jXCtWNXq8U0tF1biQ@mail.gmail.com> <3d769452-cd24-1f7b-6fb2-b556a51be55b@linux.vnet.ibm.com>
On Wed, Jun 21, 2017 at 1:44 PM, Robin Dapp <rdapp@linux.vnet.ibm.com> wrote:
>> use INTEGRAL_TYPE_P.
>
> Done.
>
>> but you do not actually _use_ vr_outer. Do you think that if
>> vr_outer is a VR_RANGE then the outer operation may not
>> possibly have wrapped? That's a false conclusion.
>
> These were remains of a previous version. vr_outer is indeed not needed
> anymore; removed.
>
>> wi::add overload with the overflow flag? ISTR you want to handle "negative"
>> unsigned constants somehow, but then I don't see how the above works.
>> I'd say if wmin/wmax interpreted as signed are positive and then using
>> a signed op to add w1 results in a still positive number you're fine
>> (you don't seem
>> to restrict the widening cast to either zero- or sign-extending).
>
> Changed to using wi:add overload now.
>
> In essence, three cases are being handled:
> - wrapped_range --> do not simplify
> - !wrapped_range && ovf ("negative" unsigned) --> simplify and combine
> with sign extension in the outer type
> - !wrapped_range && !ovf ("positive" unsigned) --> simplify and combine
> with zero extension in the outer type.
Let's split this and look at the simpler case:
+/* ((T)(A)) +- CST -> (T)(A +- CST) */
+#if GIMPLE
+ (for outer_op (plus minus)
+ (simplify
+ (outer_op (convert SSA_NAME@0) INTEGER_CST@2)
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && INTEGRAL_TYPE_P (type)
+ && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
+ /* Perform binary operation inside the cast if the constant fits
+ and there is no overflow. */
+ (with
+ {
+ bool wrapped_range = true;
+ tree cst_inner = NULL_TREE;
+ enum value_range_type vr = VR_VARYING;
+ tree inner_type = TREE_TYPE (@0);
+
+ if (int_fits_type_p (@2, inner_type))
do
&& get_range_info (...) == VR_RANGE)
here. That avoids vr and its initialization and you get all of the "work" when
you know it will eventually succeed.
+ {
+ cst_inner = fold_convert (inner_type, @2);
ideally you'd use a wide-int here and defer the tree allocation to the result
wide_int wi = wi::from (@2, TYPE_PRECISION (inner_type),
TYPE_SIGN (inner_type));
+ wide_int wmin0, wmax0;
+ wide_int w1 = cst_inner;
+ signop sgn = TYPE_SIGN (inner_type);
+ vr = get_range_info (@0, &wmin0, &wmax0);
+
+ if (vr == VR_RANGE)
+ {
+ bool min_ovf;
+ wi::add (wmin0, w1, sgn, &min_ovf);
+
+ bool max_ovf;
+ wi::add (wmax0, w1, sgn, &max_ovf);
So I guess we never run into the outer_op == minus case as the above is
clearly wrong for that?
The comment above says "if there is no overflow" but below you allow
min_ovf && max_ovf without any further explanation.
+ wrapped_range = (min_ovf && !max_ovf) || (!min_ovf && max_ovf);
+ }
+ }
+ }
+ (if (cst_inner && !wrapped_range)
+ (convert (outer_op @0 { cst_inner; })))
thus
(if ((min_ovf && !max_ovf) || ....)
(convert (outer_op @0 { wide_int_to_tree (inner_type, w1); } ))))
try to keep vertical spacing in patterns minimal -- I belive that patterns
should be small enough to fit in a terminal window (24 lines).
Richard.
+ ))))
+#endif
> Regards
> Robin