From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 11FAE3858D3C; Fri, 26 May 2023 08:49:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 11FAE3858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685090985; bh=04LQiI3RTHw9Skct4LD3apIgbwgrJ68CWG87e7fMF5E=; h=From:To:Subject:Date:In-Reply-To:References:From; b=HB0gvb1Hol52c4VO8VqlVoRV3WKFLk7dQneoPrwagnZHSymaesMnVHTtRvFrxkO5x MmHW6I4IKIvCjCwHc8G401FoLpn/gHEL5Il9BDNRE7US6158Xpzqc2flVsLbWrEE0j w45znurXbNSG5vkOK5jAAOhrRw0QEHnk5qrlUfcM= From: "gjl at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/109907] Missed optimization for bit extraction (uses shift instead of single bit-test) Date: Fri, 26 May 2023 08:49:38 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed 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: gjl at gcc dot gnu.org X-Bugzilla-Status: NEW 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: 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=3D109907 --- Comment #20 from Georg-Johann Lay --- Here is a testcase similar to the one from PR55181, where the first test is= for the sign bit: unsigned char lfsr32_mpp_sign (unsigned long number) { unsigned char b =3D 0; if (number & (1UL << 31)) b--; if (number & (1UL << 29)) b++; if (number & (1UL << 13)) b++; return b; } unsigned char lfsr32_ppp_sign (unsigned long number) { unsigned char b =3D 0; if (number & (1UL << 31)) b++; if (number & (1UL << 29)) b++; if (number & (1UL << 13)) b++; return b; } What then happens is: expr.cc::do_store_flag() expmed.cc::emit_store_flag_force() expmed.cc::emit_store_flag() expmed.cc::emit_store_flag_1() the latter then does: if (STORE_FLAG_VALUE =3D=3D 1 || normalizep) /* If we are supposed to produce a 0/1 value, we want to do a logical shift from the sign bit to the low-order bit; for a -1/0 value, we do an arithmetic shift. */ op0 =3D expand_shift (RSHIFT_EXPR, int_mode, op0, GET_MODE_BITSIZE (int_mode) - 1, subtarget, normalizep !=3D -1); "normalizep" is true because ops->type has a precision of 1, and STORE_FLAG_VALUE is the default of 1. Nowhere is there any cost computation or consideration whether extzv could = do the trick.=