From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id DDB7C385840D; Tue, 9 Apr 2024 16:11:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DDB7C385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1712679070; bh=+H6PDvnEVG6nsEleA2tuvUsja0t3BfogvDT75APy4ps=; h=From:To:Subject:Date:From; b=CwiBCPj0qm46kI6GMw1iL9GF+aC5ARF7BLz61dfIssC7PuFS9sC84PzMPlAUUjYSd Ju9ayLpt6LF/8WwshCvlQx7aZojFgUW5aqwFOLJqAF8sXYjN7h6jBr9Pp3USPNrJqO 5iebmMKdpqqpBliUiU5iI7v0AB7ktHuk2qy4Ec/c= From: "antoshkka at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/114661] New: Bit operations not optimized to multiplication Date: Tue, 09 Apr 2024 16:11:10 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: antoshkka at gmail dot com 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: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter target_milestone Message-ID: 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=3D114661 Bug ID: 114661 Summary: Bit operations not optimized to multiplication Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Consider the example: unsigned mul(unsigned char c) { if (c > 3) __builtin_unreachable(); return c << 18 | c << 15 | c << 12 | c << 9 | c << 6 | c << 3 | c; } GCC with -O2 generates the following assembly: mul(unsigned char): movzx edi, dil lea edx, [rdi+rdi*8] lea eax, [0+rdx*8] mov ecx, edx sal edx, 15 or eax, edi sal ecx, 9 or eax, ecx or eax, edx ret However it could be optimized to just: mul(unsigned char): imul eax, edi, 299593 ret Compiling with -Os does not help. Godbolt playground: https://godbolt.org/z/YszzMbovK P.S.: without `c << 18 | c << 15 |` the bit operations are transformed to multiplication.=