From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 322453858C54 for ; Mon, 26 Feb 2024 16:02:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 322453858C54 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 322453858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708963379; cv=none; b=kC+vaRO8rkgpnBIYjKpSx6ZRIWKpe55hyRlPSzOboqKtUpWDNpt+aFbfe3mCwz/0gQub4o4gpkXXwegbr5EWv+8FkKIV6TF3yKX4wtUj5PzCXVV4/5vx+DiVHq8IM6xCJJzUVLmAqMsMSXYTRskSIG7y5T+I/wHFa9fs2DbAvXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708963379; c=relaxed/simple; bh=T5tepdnBhXQ3sthjP1Kod2/aEgBFplE7D3FTT9xPA8s=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=RiQgheo4xdI0qO3X4cBe65mmEyWeuMjmKS34wZrPF6r7pXP5yNJLgj2WhOtPMCv3zdaJScg2HvbNc1I/teDYtFTGf/x7BqdGZG2I28V4qucC9HU6vKEUjX6vpap9TmNLrNPAfGJ7a390UPT+hmJK1kYpsV83et6C3miZpbFBbuE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708963377; h=from:from:reply-to:reply-to:subject:subject: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=eIHDCEfLINPLUNPX1b+BRDx/aN9kH8gH4cSHCikLxIU=; b=N2f8CyEhgeIMmpwcvfsVj2mkEgxX87l9+0UH56Rhuh0B2wY2Mehy2jK3W6ndm0rOO0mA0Y 6uDCTG90Ohk/nbho+CRkSyEGQ2IOefEEuIDIjfnHVDBH/88s5gqOPQrlPCWfF6UOuaetvH AVNyD2K7WpGJ1EsoYNjJGE3HwH6sM7E= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-352-hjNTjF9oNM2zfZiVjMYPlw-1; Mon, 26 Feb 2024 11:02:53 -0500 X-MC-Unique: hjNTjF9oNM2zfZiVjMYPlw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6BD2A185A786; Mon, 26 Feb 2024 16:02:53 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F3D11C060B1; Mon, 26 Feb 2024 16:02:53 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 41QG2om81361037 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 26 Feb 2024 17:02:51 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41QG2nNv1361036; Mon, 26 Feb 2024 17:02:49 +0100 Date: Mon, 26 Feb 2024 17:02:49 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] tree-optimization/114074 - CHREC multiplication and undefined overflow Message-ID: Reply-To: Jakub Jelinek References: <20240226141510.C27103858C33@sourceware.org> MIME-Version: 1.0 In-Reply-To: <20240226141510.C27103858C33@sourceware.org> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,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 Mon, Feb 26, 2024 at 03:15:02PM +0100, Richard Biener wrote: > When folding a multiply CHRECs are handled like {a, +, b} * c > is {a*c, +, b*c} but that isn't generally correct when overflow > invokes undefined behavior. The following uses unsigned arithmetic > unless either a is zero or a and b have the same sign. > > I've used simple early outs for INTEGER_CSTs and otherwise use > a range-query since we lack a tree_expr_nonpositive_p. > > Bootstrapped and tested on x86_64-unknown-linux-gnu. > > I'm not sure it's worth using ranger, there might be also more > cases where the integer multiply is OK, say when abs (A) > abs (B). > But also {-2, +, 2}, but not for {1, +, -1} for example. So, given that we found that get_range_pos_neg is not what you want, I think the patch is ok, except a minor nit > @@ -428,10 +434,41 @@ chrec_fold_multiply (tree type, > if (integer_zerop (op1)) > return build_int_cst (type, 0); > > - return build_polynomial_chrec > - (CHREC_VARIABLE (op0), > - chrec_fold_multiply (type, CHREC_LEFT (op0), op1), > - chrec_fold_multiply (type, CHREC_RIGHT (op0), op1)); > + /* When overflow is undefined and CHREC_LEFT/RIGHT do not have the > + same sign or CHREC_LEFT is zero then folding the multiply into > + the addition does not have the same behavior on overflow. Use > + unsigned arithmetic in that case. */ > + value_range rl, rr; > + if (!ANY_INTEGRAL_TYPE_P (type) > + || TYPE_OVERFLOW_WRAPS (type) > + || integer_zerop (CHREC_LEFT (op0)) > + || (TREE_CODE (CHREC_LEFT (op0)) == INTEGER_CST > + && TREE_CODE (CHREC_RIGHT (op0)) == INTEGER_CST > + && (tree_int_cst_sgn (CHREC_LEFT (op0)) > + == tree_int_cst_sgn (CHREC_RIGHT (op0)))) > + || (get_range_query (cfun)->range_of_expr (rl, CHREC_LEFT (op0)) > + && !rl.undefined_p () > + && (rl.nonpositive_p () || rl.nonnegative_p ()) > + && get_range_query (cfun)->range_of_expr (rr, CHREC_RIGHT (op0)) > + && !rr.undefined_p () > + && ((rl.nonpositive_p () && rr.nonpositive_p ()) > + || (rl.nonnegative_p () && rr.nonnegative_p ())))) > + return build_polynomial_chrec > + (CHREC_VARIABLE (op0), > + chrec_fold_multiply (type, CHREC_LEFT (op0), op1), > + chrec_fold_multiply (type, CHREC_RIGHT (op0), op1)); > + else > + { > + tree utype = unsigned_type_for (type); > + op1 = chrec_convert_rhs (utype, op1); > + tree tem = build_polynomial_chrec > + (CHREC_VARIABLE (op0), > + chrec_fold_multiply > + (utype, chrec_convert_rhs (utype, CHREC_LEFT (op0)), op1), > + chrec_fold_multiply > + (utype, chrec_convert_rhs (utype, CHREC_RIGHT (op0)), op1)); > + return chrec_convert_rhs (type, tem); When you touch these, can you please rewrite it to more readable code with temporaries, instead of the ugly calls with ( on different line from the function name? { tree left = chrec_fold_multiply (type, CHREC_LEFT (op0), op1); tree right = chrec_fold_multiply (type, CHREC_RIGHT (op0), op1); return build_polynomial_chrec (CHREC_VARIABLE (op0), left, right); } and tree left = chrec_convert_rhs (utype, CHREC_LEFT (op0)); left = chrec_fold_multiply (utype, left, op1); tree right = chrec_convert_rhs (utype, CHREC_RIGHT (op0)); right = chrec_fold_multiply (utype, right, op1); tree tem = build_polynomial_chrec (CHREC_VARIABLE (op0), left, right); return chrec_convert_rhs (type, tem); ? Jakub