From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id D5EE03858415; Thu, 21 Dec 2023 17:50:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5EE03858415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1703181058; bh=qsb8aQ7sCC2b0xJjASdxZ0NgE2wZfDVA4mNG6swKuOY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Q3vPDHI35gnTPYxJaLWs/jttBTG8+w+Xl1wboRB0t2dM0+ILDJSBvsUqKDdjA2IeK miq/LYozwRimwLygkJYevbcxt4BRdbXoCtzKCMimqMsUEN1t6GfBS/jWh3qsipJIS1 OVkzrxBiQAlvz2FyKdQj64kUL7RaUKTTUCBiGkeA= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/113105] Missing optimzation: fold `div(v, a) * b + rem(v, a)` to `div(v, a) * (b - a) + v` Date: Thu, 21 Dec 2023 17:50:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: enhancement X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113105 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #3 from Jakub Jelinek --- I think with int v, a, b; ... v / a * b + v % a can be simplified into (int) (v / a * ((unsigned) b - a) + v), i.e. perform just the division in signed and everything else in corresponding unsigned type. Also, a question is if this is a useful optimization on targets where one instruction can compute both v / a and v % a together, because then the original has roughly one divmod insn, one multiplication and one addition, compared to the divmod insn from which only division is used, subtraction, multiplication and addition. Of course, if b - a can fold into a constant, it is different (but multiplication by constant is often done using shifts and additions and multiplication by b might be cheaper than by b - a. When v % a needs to be computed separately and especially when it is expens= ive, it can be obviously a win. >From the usual GIMPLE IL rules, both forms are 4 statements so equally good, but for the case where casts are needed, the replacement is more expensive. So, perhaps this shouldn't be done in match.pd, but during expansion or immediately before expansion, expanding to RTL both forms and comparing the costs.=