On Thu, 29 Oct 2026, Richard Biener wrote:
> On Thu, 29 Oct 2026, Andrea Pinski wrote:
>
> > On Thu, Oct 29, 2026 at 1:55 AM Andrea Pinski
> > <[email protected]> wrote:
> > >
> > > On Thu, Oct 29, 2026 at 1:42 AM Richard Biener <[email protected]> wrote:
> > > >
> > > > This fixes const correctness for several predicates, accessors, and
> > > > utility
> > > > functions in gimple.h and gimple.cc. It also adds some missing
> > > > pieces for consistency.
>
> [...]
>
> > > > +
> > > > + return (const tree *) ((const char *) gs + off);
> > > > +}
> > > > +
> > > > +inline tree *
> > > > +gimple_ops (gimple *gs)
> > > > +{
> > > > + return const_cast <tree *> (gimple_ops (const_cast <const gimple *>
> > > > (gs)));
> > > > }
> > >
> > > This seems like not a good idea. I am not a fan of having any
> > > const_cast here, especially ones that cast it both ways in the same
> > > line.
> > > Is there a better way of implementing this?
>
> The intent is to avoid repeating the complex arithmetic, easing
> maintainance. I'll put a comment before the double-const-cast.
I'm going to push with that now, re-bootstrapped on
x86_64-unknown-linux-gnu, testing in progress.
Somebody C++-literate can improve it if desired.
Richard.
> > > Maybe the better question is can we change/fix up all the places that
> > > use gimple_ops? It seems like only gimple_op_ptr and gimple_set_op
> > > uses it for a non-const form. Both cases which already have a check
> > > for gimple_has_ops/bounds.
> > > Maybe this will work:
> > > ```
> > > template<class T, class rt = conditional<std::is_convertible<T, const
> > > gimple*>::value, const tree*, tree*>>
> > > inline rt
> > > gimple_ops (T *gs)
> >
> > I totally messed this up:
> > template<class T,
> > class rt = std::conditional<std::is_const<T>::value,
> > const tree, tree>>::type
> > inline rt *
> > gimple_ops (T *gs)
> > {
> > size_t off;
> > typedef std::conditional<std::is_const<T>::value, const char,
> > char>>::type *cptr;
> >
> > I don't do much meta-programming as you can see :).
>
> Ugh. Not sure I like that more - I can't even understand it enough
> to say it's correct ...
>
> Richard.
>
> > Thanks,
> > Andrea
> >
> > >
> > > /* All the tuples have their operand vector at the very bottom
> > > of the structure. Note that those structures that do not
> > > have an operand vector have a zero offset. */
> > > off = gimple_ops_offset_[gimple_statement_structure (gs)];
> > > gcc_gimple_checking_assert (off != 0);
> > >
> > > return (rt) ((cptr) gs + off);
> > > }
> > > ```
> > > instead
> > > >
> > > >
> > > > @@ -2617,7 +2631,7 @@ gimple_op (const gimple *gs, unsigned i)
> > > > if (gimple_has_ops (gs))
> > > > {
> > > > gcc_gimple_checking_assert (i < gimple_num_ops (gs));
> > > > - return gimple_ops (const_cast<gimple *> (gs))[i];
> > > > + return gimple_ops (gs)[i];
> > > > }
> > > > else
> > > > return NULL_TREE;
> > > > @@ -2843,12 +2857,18 @@ gimple_assign_rhs3 (const gimple *gs)
> > > > /* Return a pointer to the third operand on the RHS of assignment
> > > > statement GS. */
> > > >
> > > > +inline tree *
> > > > +gimple_assign_rhs3_ptr (gassign *gs)
> > > > +{
> > > > + gcc_gimple_checking_assert (gimple_num_ops (gs) >= 4);
> > > > + return &gs->op[3];
> > > > +}
> > > > +
> > > > inline tree *
> > > > gimple_assign_rhs3_ptr (gimple *gs)
> > > > {
> > > > gassign *ass = GIMPLE_CHECK2<gassign *> (gs);
> > > > - gcc_gimple_checking_assert (gimple_num_ops (gs) >= 4);
> > > > - return &ass->op[3];
> > > > + return gimple_assign_rhs3_ptr (ass);
> > > > }
> > > >
> > > >
> > > > @@ -3493,7 +3513,7 @@ gimple_call_set_from_thunk (gcall *s, bool
> > > > from_thunk_p)
> > > > /* Return true if GIMPLE_CALL S is a jump from a thunk. */
> > > >
> > > > inline bool
> > > > -gimple_call_from_thunk_p (gcall *s)
> > > > +gimple_call_from_thunk_p (const gcall *s)
> > > > {
> > > > return (s->subcode & GF_CALL_FROM_THUNK) != 0;
> > > > }
> > > > @@ -3598,7 +3618,7 @@ gimple_call_set_expected_throw (gcall *s, bool
> > > > expected_throw_p)
> > > > propagating an exception than by other means. */
> > > >
> > > > static inline bool
> > > > -gimple_call_expected_throw_p (gcall *s)
> > > > +gimple_call_expected_throw_p (const gcall *s)
> > > > {
> > > > return (gimple_call_flags (s) & ECF_XTHROW) != 0;
> > > > }
> > > > @@ -3620,15 +3640,15 @@ gimple_call_set_alloca_for_var (gcall *s, bool
> > > > for_var)
> > > > /* Return true of S is a call to builtin_alloca emitted for VLA
> > > > objects. */
> > > >
> > > > inline bool
> > > > -gimple_call_alloca_for_var_p (gcall *s)
> > > > +gimple_call_alloca_for_var_p (const gcall *s)
> > > > {
> > > > return (s->subcode & GF_CALL_ALLOCA_FOR_VAR) != 0;
> > > > }
> > > >
> > > > inline bool
> > > > -gimple_call_alloca_for_var_p (gimple *s)
> > > > +gimple_call_alloca_for_var_p (const gimple *s)
> > > > {
> > > > - const gcall *gc = GIMPLE_CHECK2<gcall *> (s);
> > > > + const gcall *gc = GIMPLE_CHECK2<const gcall *> (s);
> > > > return (gc->subcode & GF_CALL_ALLOCA_FOR_VAR) != 0;
> > > > }
> > > >
> > > > @@ -3647,7 +3667,7 @@ gimple_call_set_by_descriptor (gcall *s, bool
> > > > by_descriptor_p)
> > > > /* Return true if S is a by-descriptor call. */
> > > >
> > > > inline bool
> > > > -gimple_call_by_descriptor_p (gcall *s)
> > > > +gimple_call_by_descriptor_p (const gcall *s)
> > > > {
> > > > return (s->subcode & GF_CALL_BY_DESCRIPTOR) != 0;
> > > > }
> > > > @@ -3655,7 +3675,7 @@ gimple_call_by_descriptor_p (gcall *s)
> > > > /* Copy all the GF_CALL_* flags from ORIG_CALL to DEST_CALL. */
> > > >
> > > > inline void
> > > > -gimple_call_copy_flags (gcall *dest_call, gcall *orig_call)
> > > > +gimple_call_copy_flags (gcall *dest_call, const gcall *orig_call)
> > > > {
> > > > dest_call->subcode = orig_call->subcode;
> > > > }
> > > > @@ -4593,9 +4613,11 @@ gimple_wce_cleanup_ptr (gimple *gs)
> > > > /* Return the cleanup sequence for cleanup statement GS. */
> > > >
> > > > inline gimple_seq
> > > > -gimple_wce_cleanup (gimple *gs)
> > > > +gimple_wce_cleanup (const gimple *gs)
> > > > {
> > > > - return *gimple_wce_cleanup_ptr (gs);
> > > > + const gimple_statement_wce *wce_stmt
> > > > + = as_a <const gimple_statement_wce *> (gs);
> > > > + return wce_stmt->cleanup;
> > > > }
> > > >
> > > >
> > > > @@ -4810,7 +4832,7 @@ gimple_phi_arg_location (const gphi *phi, size_t
> > > > i)
> > > > /* Return the source location of the argument on edge E of phi node
> > > > PHI. */
> > > >
> > > > inline location_t
> > > > -gimple_phi_arg_location_from_edge (gphi *phi, edge e)
> > > > +gimple_phi_arg_location_from_edge (const gphi *phi, edge e)
> > > > {
> > > > return gimple_phi_arg (phi, e->dest_idx)->locus;
> > > > }
> > > > @@ -5121,7 +5143,7 @@ gimple_debug_bind_reset_value (gimple *dbg)
> > > > value. */
> > > >
> > > > inline bool
> > > > -gimple_debug_bind_has_value_p (gimple *dbg)
> > > > +gimple_debug_bind_has_value_p (const gimple *dbg)
> > > > {
> > > > GIMPLE_CHECK (dbg, GIMPLE_DEBUG);
> > > > gcc_gimple_checking_assert (gimple_debug_bind_p (dbg));
> > > > @@ -7066,7 +7088,7 @@ gimple_alloc_kind (enum gimple_code code)
> > > > by annotate_all_with_location. */
> > > >
> > > > inline bool
> > > > -gimple_do_not_emit_location_p (gimple *g)
> > > > +gimple_do_not_emit_location_p (const gimple *g)
> > > > {
> > > > return gimple_plf (g, GF_PLF_1);
> > > > }
> > > >
> > > > base-commit: 0091900417570a0d70c9d382ec4491dbe12fb01f
> > > > --
> > > > 2.51.0
> >
>
>
--
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)