On Sat, Nov 16, 2013 at 1:55 PM, Richard Sandiford <rdsandif...@googlemail.com> wrote: > Add tree_fits_shwi_p and tree_fits_uhwi_p. The implementations are taken > directly from host_integerp.
tree_ is a bit generic - you only ever return true for INTEGER_CSTs, so please use int_fits_[su]hwi_p please (mimicing int_fits_type_p). Ok with that change. Thanks, Richard. > Thanks, > Richard > > > gcc/ > * tree.h (tree_fits_shwi_p, tree_fits_uhwi_p): Declare. > * tree.c (tree_fits_shwi_p, tree_fits_uhwi_p): Define. > > Index: gcc/tree.h > =================================================================== > --- gcc/tree.h 2013-11-16 09:09:56.388037088 +0000 > +++ gcc/tree.h 2013-11-16 09:11:53.535874667 +0000 > @@ -3659,6 +3659,16 @@ extern int host_integerp (const_tree, in > ATTRIBUTE_PURE /* host_integerp is pure only when checking is disabled. */ > #endif > ; > +extern bool tree_fits_shwi_p (const_tree) > +#ifndef ENABLE_TREE_CHECKING > + ATTRIBUTE_PURE /* tree_fits_shwi_p is pure only when checking is disabled. > */ > +#endif > + ; > +extern bool tree_fits_uhwi_p (const_tree) > +#ifndef ENABLE_TREE_CHECKING > + ATTRIBUTE_PURE /* tree_fits_uhwi_p is pure only when checking is disabled. > */ > +#endif > + ; > extern HOST_WIDE_INT tree_low_cst (const_tree, int); > #if !defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 4003) > extern inline __attribute__ ((__gnu_inline__)) HOST_WIDE_INT > Index: gcc/tree.c > =================================================================== > --- gcc/tree.c 2013-11-16 09:09:56.388037088 +0000 > +++ gcc/tree.c 2013-11-16 09:11:53.534874659 +0000 > @@ -6990,6 +6990,32 @@ host_integerp (const_tree t, int pos) > || (pos && TREE_INT_CST_HIGH (t) == 0))); > } > > +/* Return true if T is an INTEGER_CST whose numerical value (extended > + according to TYPE_UNSIGNED) fits in a signed HOST_WIDE_INT. */ > + > +bool > +tree_fits_shwi_p (const_tree t) > +{ > + return (t != NULL_TREE > + && TREE_CODE (t) == INTEGER_CST > + && ((TREE_INT_CST_HIGH (t) == 0 > + && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0) > + || (TREE_INT_CST_HIGH (t) == -1 > + && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0 > + && !TYPE_UNSIGNED (TREE_TYPE (t))))); > +} > + > +/* Return true if T is an INTEGER_CST whose numerical value (extended > + according to TYPE_UNSIGNED) fits in an unsigned HOST_WIDE_INT. */ > + > +bool > +tree_fits_uhwi_p (const_tree t) > +{ > + return (t != NULL_TREE > + && TREE_CODE (t) == INTEGER_CST > + && TREE_INT_CST_HIGH (t) == 0); > +} > + > /* Return the HOST_WIDE_INT least significant bits of T if it is an > INTEGER_CST and there is no overflow. POS is nonzero if the result must > be non-negative. We must be able to satisfy the above conditions. */