From b77161e60bce7b4416319defe5f141f14fd375c4 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Fri, 14 Jul 2023 10:01:39 +0200 Subject: [PATCH] Provide extra checking for phi argument access from edge The following adds checking that the edge we query an associated PHI arg for is related to the PHI node. Triggered by questionable code in one of my reviews. * gimple.h (gimple_phi_arg): New const overload. (gimple_phi_arg_def): Make gimple arg const. (gimple_phi_arg_def_from_edge): New inline function. * tree-phinodes.h (gimple_phi_arg_imm_use_ptr_from_edge): Likewise. * tree-ssa-operands.h (PHI_ARG_DEF_FROM_EDGE): Direct to new inline function. (PHI_ARG_DEF_PTR_FROM_EDGE): Likewise. --- gcc/gimple.h | 25 ++++++++++++++++++++++++- gcc/tree-phinodes.h | 7 +++++++ gcc/tree-ssa-operands.h | 4 ++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/gcc/gimple.h b/gcc/gimple.h index daf55242f68..d3750f95d79 100644 --- a/gcc/gimple.h +++ b/gcc/gimple.h @@ -4633,6 +4633,13 @@ gimple_phi_arg (const gphi *gs, unsigned index) return &(gs->args[index]); } +inline const phi_arg_d * +gimple_phi_arg (const gimple *gs, unsigned index) +{ + const gphi *phi_stmt = as_a (gs); + return gimple_phi_arg (phi_stmt, index); +} + inline struct phi_arg_d * gimple_phi_arg (gimple *gs, unsigned index) { @@ -4678,11 +4685,27 @@ gimple_phi_arg_def (const gphi *gs, size_t index) } inline tree -gimple_phi_arg_def (gimple *gs, size_t index) +gimple_phi_arg_def (const gimple *gs, size_t index) { return gimple_phi_arg (gs, index)->def; } +/* Return the tree operand for the argument associated with + edge E of PHI node GS. */ + +inline tree +gimple_phi_arg_def_from_edge (const gphi *gs, const_edge e) +{ + gcc_checking_assert (e->dest == gimple_bb (gs)); + return gimple_phi_arg (gs, e->dest_idx)->def; +} + +inline tree +gimple_phi_arg_def_from_edge (const gimple *gs, const_edge e) +{ + gcc_checking_assert (e->dest == gimple_bb (gs)); + return gimple_phi_arg (gs, e->dest_idx)->def; +} /* Return a pointer to the tree operand for argument I of phi node PHI. */ diff --git a/gcc/tree-phinodes.h b/gcc/tree-phinodes.h index 932a461e987..be114e317b4 100644 --- a/gcc/tree-phinodes.h +++ b/gcc/tree-phinodes.h @@ -37,6 +37,13 @@ gimple_phi_arg_imm_use_ptr (gimple *gs, int i) return &gimple_phi_arg (gs, i)->imm_use; } +inline use_operand_p +gimple_phi_arg_imm_use_ptr_from_edge (gimple *gs, const_edge e) +{ + gcc_checking_assert (e->dest == gimple_bb (gs)); + return &gimple_phi_arg (gs, e->dest_idx)->imm_use; +} + /* Return the phi argument which contains the specified use. */ inline int diff --git a/gcc/tree-ssa-operands.h b/gcc/tree-ssa-operands.h index ae36bcdb893..c7b74e046d0 100644 --- a/gcc/tree-ssa-operands.h +++ b/gcc/tree-ssa-operands.h @@ -83,9 +83,9 @@ struct GTY(()) ssa_operands { #define SET_PHI_ARG_DEF(PHI, I, V) \ SET_USE (PHI_ARG_DEF_PTR ((PHI), (I)), (V)) #define PHI_ARG_DEF_FROM_EDGE(PHI, E) \ - PHI_ARG_DEF ((PHI), (E)->dest_idx) + gimple_phi_arg_def_from_edge ((PHI), (E)) #define PHI_ARG_DEF_PTR_FROM_EDGE(PHI, E) \ - PHI_ARG_DEF_PTR ((PHI), (E)->dest_idx) + gimple_phi_arg_imm_use_ptr_from_edge ((PHI), (E)) #define PHI_ARG_INDEX_FROM_USE(USE) phi_arg_index_from_use (USE)