On Thu, 2026-05-14 at 13:01 +0200, Richard Biener wrote:
> 
> 
> > Am 14.05.2026 um 10:59 schrieb Xi Ruoyao <[email protected]>:
> > 
> > On Thu, 2026-05-14 at 09:51 +0200, Richard Biener wrote:
> > 
> > 
> > /* snip */
> > 
> > > > diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
> > > > index f524786f33b..ec3c9ab4f3d 100644
> > > > --- a/gcc/tree-scalar-evolution.cc
> > > > +++ b/gcc/tree-scalar-evolution.cc
> > > > @@ -1391,10 +1391,22 @@ simplify_peeled_chrec (class loop *loop, tree 
> > > > arg, tree init_cond)
> > > >      && wi::to_widest (init_cond) == wi::to_widest (left_before)
> > > >      && !scev_probably_wraps_p (NULL_TREE, left_before, right, NULL,
> > > >                     loop, false))
> > > > -    return build_polynomial_chrec (loop->num, init_cond,
> > > > -                       chrec_convert (TREE_TYPE (ev),
> > > > -                              right, NULL,
> > > > -                              false, NULL_TREE));
> > > > +    {
> > > > +      tree tp = TREE_TYPE (right);
> > > > +
> > > > +      /* We likely need a sign-extension to make things like
> > > > +         u8(6, 4, 2) => i32(6, 4, 2), instead of i32(6, 260, 514).
> > > > +         But u8(1, 129) shouldn't be i32(1, -127) either.  */
> > > > +      if (TYPE_UNSIGNED (tp)
> > > > +          && wi::to_widest (TYPE_MAX_VALUE (tp)) <
> > > > +         (wi::to_widest (init_cond) + wi::to_widest (right)))
> > > 
> > > I’d say we just want the TYPE_UNSIGNED check here, esp. it shouldn’t 
> > > depend on init_cond and also work for symbolic right.
> > 
> > So wouldn't doing so make u8(1, 129) => i32(1, -127) and misoptimize
> > that?  Or maybe we cannot have a loop iterating only twice here?
> 
> I think SCEV probably warp_p guarantees the unsigned IV doesn’t
> underflow when the step is interpreted as signed (underflow zero).  So
> yes, I think we know u8(1, 128) does not actually iterate.

I'd not pretend I really understand SCEV but after reading the code for
an hour my brain seems to agree.  And I've also bootstrapped & regtested
with a debug assert checking this.  So I'll simply use TYPE_UNSIGNED in
v3.

-- 
Xi Ruoyao <[email protected]>

Reply via email to