On Mon, Sep 15, 2025 at 8:03 AM Eric Botcazou <botca...@adacore.com> wrote: > > Hi, > > For parameters passed by reference, the Ada compiler sets TREE_THIS_NOTRAP on > their dereference to prevent tree_could_trap_p from returning true and then > causing a new basic block to be created for every access to them (given that > in Ada the -fnon-call-exceptions flag is enabled by default). > > However, when the subprogram is inlined, this TREE_THIS_NOTRAP flag cannot be > blindly preserved because the call may pass the dereference of a pointer as > the argument: even if the compiler generates a check that the pointer is not > null just before, preserving the TREE_THIS_NOTRAP flag could cause an access > to be hoisted before the check; therefore it gets cleared for parameters. > > Now that's suboptimal if the argument is an object (DECL_P) because accessing > it through the dereference of the parameter cannot trap, which causes MEM_REFs > of the form MEM_REF [&DECL] to be considered as trapping in the case where the > nominal subtype of DECL is self-referential. > > Bootstrapped/regtested on x86-64/Linux, OK for the mainline?
Do we need to ensure that, for the MEM_REF case at least, the DECL is of appropriate size with respect to the TREE_TYPE of the MEM_REF and the offset (TREE_OPERAND (*tp, 1))? That is, consider ptr = &too_small_object; if (object-of-OK-size) foo (*(larget-type *)ptr); if we set TREE_THIS_NOTRAP that makes an inlined access OK to be hoisted above the object-of-OK-size check. It might also be a if (object-writable) check and an inlined store, where we'd have to consider a readonly decl? Richard. > > 2025-09-15 Eric Botcazou <ebotca...@adacore.com> > > * tree-inline.cc (maybe_copy_this_notrap): New function. Copy the > TREE_THIS_NOTRAP flag for parameters if the argument is DECL_P. > (remap_gimple_op_r): Call maybe_copy_this_notrap. > (copy_tree_body_r): Likewise. > > -- > Eric Botcazou