From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1666) id 304673858415; Tue, 30 Apr 2024 11:05:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 304673858415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1714475117; bh=eqS7PSvw1XqsSdJdVhbr5l0Ww+nNuAIML3oXG9Y9pW0=; h=From:To:Subject:Date:From; b=JF+GG6p89Bb1Q+moLh7QVDDdTadLJc9G49IL1i4Fisjuh3eSZx/VoHg0uluC0bhE4 dcEvmE+W11Feu/31xQZCjn5CEtwkWmI1Ciy7ZodpHxIHROotWvQVSl+PxDj5W+wsvP 3lZshAkljlWACfqy+hwpIBBS4L6Hm9Gla9Zm2GEQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Richard Biener To: gcc-cvs@gcc.gnu.org Subject: [gcc r15-67] middle-end/13421 - -ftrapv vs. POINTER_DIFF_EXPR X-Act-Checkin: gcc X-Git-Author: Richard Biener X-Git-Refname: refs/heads/master X-Git-Oldrev: 6c6b70f07208ca14ba783933988c04c6fc2fff42 X-Git-Newrev: 667c19de86b33648f5f4599f589a5e02adbb35cb Message-Id: <20240430110517.304673858415@sourceware.org> Date: Tue, 30 Apr 2024 11:05:17 +0000 (GMT) List-Id: https://gcc.gnu.org/g:667c19de86b33648f5f4599f589a5e02adbb35cb commit r15-67-g667c19de86b33648f5f4599f589a5e02adbb35cb Author: Richard Biener Date: Tue Apr 16 14:05:35 2024 +0200 middle-end/13421 - -ftrapv vs. POINTER_DIFF_EXPR Currently we expand POINTER_DIFF_EXPR using subv_optab when -ftrapv (but -fsanitize=undefined does nothing). That's not consistent with the behavior of POINTER_PLUS_EXPR which never uses addv_optab with -ftrapv. Both are because of the way we select whether to use the trapping or the non-trapping optab - we look at the result type of the expression and check trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type); the bugreport correctly complains that -ftrapv affects pointer subtraction (there's no -ftrapv-pointer). Now that we have POINTER_DIFF_EXPR we can honor that appropriately. The patch moves both POINTER_DIFF_EXPR and POINTER_PLUS_EXPR handling so they will never consider trapping (or saturating) optabs. PR middle-end/13421 * optabs-tree.cc (optab_for_tree_code): Do not consider {add,sub}v or {us,ss}{add,sub} optabs for POINTER_DIFF_EXPR or POINTER_PLUS_EXPR. Diff: --- gcc/optabs-tree.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gcc/optabs-tree.cc b/gcc/optabs-tree.cc index e7bd0d10892..b69a5bc3676 100644 --- a/gcc/optabs-tree.cc +++ b/gcc/optabs-tree.cc @@ -135,6 +135,12 @@ optab_for_tree_code (enum tree_code code, const_tree type, case MIN_EXPR: return TYPE_UNSIGNED (type) ? umin_optab : smin_optab; + case POINTER_PLUS_EXPR: + return add_optab; + + case POINTER_DIFF_EXPR: + return sub_optab; + case REALIGN_LOAD_EXPR: return vec_realign_load_optab; @@ -249,13 +255,11 @@ optab_for_tree_code (enum tree_code code, const_tree type, trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type); switch (code) { - case POINTER_PLUS_EXPR: case PLUS_EXPR: if (TYPE_SATURATING (type)) return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab; return trapv ? addv_optab : add_optab; - case POINTER_DIFF_EXPR: case MINUS_EXPR: if (TYPE_SATURATING (type)) return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;