From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id DD87F3858D37 for ; Tue, 6 Feb 2024 11:41:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD87F3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DD87F3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707219675; cv=none; b=Z0njKfA6phx+JLn7NpJ6Tt95onWNnbPTX12HUsJSwookYU3Am1UfP0KK/cYw98xYcf7R1H3GbIZVnYkeNIv372eMJYVje1eJjwFf1uDHE+qJJ7RSgep3u6eK91pWiE9D8E3u/vjhX585lnTtPH/5HWuonGZop0zBZnCMa/0nB74= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707219675; c=relaxed/simple; bh=yhvcLWE8SoxUMYrL6ZJ+3OxZsSeQGFHF2uLtALKclz8=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:Message-ID:MIME-Version; b=jynASTowW0yxCs2R6vNpr1P1vKUkeSqLUhYeYAmuQ3Hh+dpSbqZEGdjnImsFB3VF8RRru/USnoKzt9YCaQMe66yb9sNwdTFdhL7D8WUL4czvHGQ0izZoXHSBXseX3ZERIuOeIDIAeAiWCIYuoHAlPNqcPbhWxRpkalVJJ+eyel8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.4.150] (unknown [10.168.4.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 942AF21F87; Tue, 6 Feb 2024 11:41:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707219671; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gng4KCKp4A8oV8UqjNULBYFs8WjPIAku+c+xuKvgxmM=; b=P0jKWRLQieycQOUf/luYwwOxhKmvLkPg/poZtRpSWFnCTfzHdN18Q9726PzwrIj5BRirCo wsaaL5cd6s3A0QlPvDECKpBx4BDmr2HHAvQehc2JdSyj6sEB9xUZxpMBZkGm2NhxqoxlTM 8bGQQryagkB3tVWLQvFVM1jVj2OVEQE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707219671; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gng4KCKp4A8oV8UqjNULBYFs8WjPIAku+c+xuKvgxmM=; b=SN5ue/RtFtYuPRYQ5OiBZTXAecVuChd9tEt/Xad7EJNo/LIWC7BvQs6hWlX8Q+Ut0+yIQ6 forAM4huP7gZe6Dg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707219671; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gng4KCKp4A8oV8UqjNULBYFs8WjPIAku+c+xuKvgxmM=; b=P0jKWRLQieycQOUf/luYwwOxhKmvLkPg/poZtRpSWFnCTfzHdN18Q9726PzwrIj5BRirCo wsaaL5cd6s3A0QlPvDECKpBx4BDmr2HHAvQehc2JdSyj6sEB9xUZxpMBZkGm2NhxqoxlTM 8bGQQryagkB3tVWLQvFVM1jVj2OVEQE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707219671; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=gng4KCKp4A8oV8UqjNULBYFs8WjPIAku+c+xuKvgxmM=; b=SN5ue/RtFtYuPRYQ5OiBZTXAecVuChd9tEt/Xad7EJNo/LIWC7BvQs6hWlX8Q+Ut0+yIQ6 forAM4huP7gZe6Dg== Date: Tue, 6 Feb 2024 12:41:11 +0100 (CET) From: Richard Biener To: Jakub Jelinek cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] tree-ssa-math-opts: Fix up convert_{mult,plusminus}_to_widen [PR113759] In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [-3.10 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCPT_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: X-Spam-Score: -3.10 X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Tue, 6 Feb 2024, Jakub Jelinek wrote: > Hi! > > On the following testcase we emit invalid stmt: > error: type mismatch in ?widen_mult_plus_expr? > 6 | foo (int c, int b) > | ^~~ > unsigned long > int > unsigned int > unsigned long > _31 = WIDEN_MULT_PLUS_EXPR ; > > The recent PR113560 r14-8680 changes tweaked convert_mult_to_widen, > but didn't change convert_plusminus_to_widen for the > TREE_TYPE (rhsN) != typeN cases, but looking at this, it was already > before that change quite weird. > > Earlier in those functions it determines actual_precision and from_unsignedN > and wants to use that precision and signedness for the operands and > it used build_and_insert_cast for that (which emits a cast stmt, even for > INTEGER_CSTs) and later on for INTEGER_CST arguments fold_converted them > to typeN (which is unclear to me why, because it seems to have assumed > that TREE_TYPE (rhsN) is typeN, for the actual_precision or from_unsignedN > cases it would be wrong except that build_and_insert_cast forced a SSA_NAME > and so it doesn't trigger anymore). > Now, since r14-8680 it is possible that rhsN also has some other type from > typeN and we again want to cast. > > The following patch changes this, so that for the differences in > actual_precision and/or from_unsignedN we actually update typeN and then use > it as the type to convert the arguments to if it isn't useless, for > INTEGER_CSTs by just fold_converting, otherwise using build_and_insert_cast. > And uses useless_type_conversion_p test so that we don't convert unless > necessary. Plus by doing that effectively also doing the important part of > the r14-8680 convert_mult_to_widen changes in convert_plusminus_to_widen. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? OK. Thanks for cleaning this up. Richard. > 2024-02-06 Jakub Jelinek > > PR tree-optimization/113759 > * tree-ssa-math-opts.cc (convert_mult_to_widen): If actual_precision > or from_unsignedN differs from properties of typeN, update typeN > to build_nonstandard_integer_type. If TREE_TYPE (rhsN) is not > uselessly convertible to typeN, convert it using fold_convert or > build_and_insert_cast depending on if rhsN is INTEGER_CST or not. > (convert_plusminus_to_widen): Likewise. > > * gcc.c-torture/compile/pr113759.c: New test. > > --- gcc/tree-ssa-math-opts.cc.jj 2024-02-02 11:26:43.730589763 +0100 > +++ gcc/tree-ssa-math-opts.cc 2024-02-05 10:23:16.068489814 +0100 > @@ -2865,25 +2865,25 @@ convert_mult_to_widen (gimple *stmt, gim > if (2 * actual_precision > TYPE_PRECISION (type)) > return false; > if (actual_precision != TYPE_PRECISION (type1) > - || from_unsigned1 != TYPE_UNSIGNED (type1) > - || (TREE_TYPE (rhs1) != type1 > - && TREE_CODE (rhs1) != INTEGER_CST)) > - rhs1 = build_and_insert_cast (gsi, loc, > - build_nonstandard_integer_type > - (actual_precision, from_unsigned1), rhs1); > + || from_unsigned1 != TYPE_UNSIGNED (type1)) > + type1 = build_nonstandard_integer_type (actual_precision, from_unsigned1); > + if (!useless_type_conversion_p (type1, TREE_TYPE (rhs1))) > + { > + if (TREE_CODE (rhs1) == INTEGER_CST) > + rhs1 = fold_convert (type1, rhs1); > + else > + rhs1 = build_and_insert_cast (gsi, loc, type1, rhs1); > + } > if (actual_precision != TYPE_PRECISION (type2) > - || from_unsigned2 != TYPE_UNSIGNED (type2) > - || (TREE_TYPE (rhs2) != type2 > - && TREE_CODE (rhs2) != INTEGER_CST)) > - rhs2 = build_and_insert_cast (gsi, loc, > - build_nonstandard_integer_type > - (actual_precision, from_unsigned2), rhs2); > - > - /* Handle constants. */ > - if (TREE_CODE (rhs1) == INTEGER_CST) > - rhs1 = fold_convert (type1, rhs1); > - if (TREE_CODE (rhs2) == INTEGER_CST) > - rhs2 = fold_convert (type2, rhs2); > + || from_unsigned2 != TYPE_UNSIGNED (type2)) > + type2 = build_nonstandard_integer_type (actual_precision, from_unsigned2); > + if (!useless_type_conversion_p (type2, TREE_TYPE (rhs2))) > + { > + if (TREE_CODE (rhs2) == INTEGER_CST) > + rhs2 = fold_convert (type2, rhs2); > + else > + rhs2 = build_and_insert_cast (gsi, loc, type2, rhs2); > + } > > gimple_assign_set_rhs1 (stmt, rhs1); > gimple_assign_set_rhs2 (stmt, rhs2); > @@ -3086,26 +3086,28 @@ convert_plusminus_to_widen (gimple_stmt_ > actual_precision = GET_MODE_PRECISION (actual_mode); > if (actual_precision != TYPE_PRECISION (type1) > || from_unsigned1 != TYPE_UNSIGNED (type1)) > - mult_rhs1 = build_and_insert_cast (gsi, loc, > - build_nonstandard_integer_type > - (actual_precision, from_unsigned1), > - mult_rhs1); > + type1 = build_nonstandard_integer_type (actual_precision, from_unsigned1); > + if (!useless_type_conversion_p (type1, TREE_TYPE (mult_rhs1))) > + { > + if (TREE_CODE (mult_rhs1) == INTEGER_CST) > + mult_rhs1 = fold_convert (type1, mult_rhs1); > + else > + mult_rhs1 = build_and_insert_cast (gsi, loc, type1, mult_rhs1); > + } > if (actual_precision != TYPE_PRECISION (type2) > || from_unsigned2 != TYPE_UNSIGNED (type2)) > - mult_rhs2 = build_and_insert_cast (gsi, loc, > - build_nonstandard_integer_type > - (actual_precision, from_unsigned2), > - mult_rhs2); > + type2 = build_nonstandard_integer_type (actual_precision, from_unsigned2); > + if (!useless_type_conversion_p (type2, TREE_TYPE (mult_rhs2))) > + { > + if (TREE_CODE (mult_rhs2) == INTEGER_CST) > + mult_rhs2 = fold_convert (type2, mult_rhs2); > + else > + mult_rhs2 = build_and_insert_cast (gsi, loc, type2, mult_rhs2); > + } > > if (!useless_type_conversion_p (type, TREE_TYPE (add_rhs))) > add_rhs = build_and_insert_cast (gsi, loc, type, add_rhs); > > - /* Handle constants. */ > - if (TREE_CODE (mult_rhs1) == INTEGER_CST) > - mult_rhs1 = fold_convert (type1, mult_rhs1); > - if (TREE_CODE (mult_rhs2) == INTEGER_CST) > - mult_rhs2 = fold_convert (type2, mult_rhs2); > - > gimple_assign_set_rhs_with_ops (gsi, wmult_code, mult_rhs1, mult_rhs2, > add_rhs); > update_stmt (gsi_stmt (*gsi)); > --- gcc/testsuite/gcc.c-torture/compile/pr113759.c.jj 2024-02-05 10:34:35.017036427 +0100 > +++ gcc/testsuite/gcc.c-torture/compile/pr113759.c 2024-02-05 10:34:24.682179591 +0100 > @@ -0,0 +1,20 @@ > +/* PR tree-optimization/113759 */ > + > +extern short t[]; > + > +int > +foo (int c, int b) > +{ > + if (b < 0) > + __builtin_unreachable (); > + if (c <= 0) > + __builtin_unreachable (); > + int d; > + for (; c >= 0; c--) > + { > + int a = b + c; > + d = t[a]; > + t[a] = 0; > + } > + return d; > +} > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)