Bug 18030

Summary: OR is inefficient in 2-bit bitfield
Product: gcc Reporter: Kazu Hirata <kazu>
Component: middle-endAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: enhancement CC: gcc-bugs
Priority: P2 Keywords: missed-optimization, patch
Version: unknown   
Target Milestone: 4.1.0   
Host: Target: i686-pc-linux-gnu
Build: Known to work:
Known to fail: Last reconfirmed: 2004-10-16 03:11:16
Bug Depends on:    
Bug Blocks: 19466, 17652    

Description Kazu Hirata 2004-10-16 03:04:14 UTC
Consider:

struct B {
  unsigned bit : 2;
};

void
plus (struct B *b)
{
  b->bit |= 1;
}

./cc1 -O2 -fomit-frame-pointer -mregparm=3 generates

plus:
	movb	(%eax), %cl
	andl	$3, %ecx
	orl	$1, %ecx
	andl	$3, %ecx
	movl	(%eax), %edx
	andl	$-4, %edx
	orl	%ecx, %edx
	movl	%edx, (%eax)
	ret

Note that we could just do "orl $1, (%eax)"
Comment 1 Andrew Pinski 2004-10-16 03:11:16 UTC
For PPC:
struct B {
  unsigned :30;
  unsigned bit : 2;
};

Confirmed.
Comment 2 Kazu Hirata 2004-10-16 15:49:20 UTC
A patch here:
http://gcc.gnu.org/ml/gcc-patches/2004-10/msg01333.html
Comment 3 Giovanni Bajo 2004-10-17 11:26:58 UTC
Approved for 4.1.
Comment 4 CVS Commits 2005-03-02 16:05:31 UTC
Subject: Bug 18030

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	kazu@gcc.gnu.org	2005-03-02 16:05:00

Modified files:
	gcc            : ChangeLog expr.c 

Log message:
	PR middle-end/18029 and PR middle-end/18030
	* expr.c (optimize_bitfield_assignment_op): Add a special case
	to handle BIT_IOR_EXPR and BIT_XOR_EXPR.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.7632&r2=2.7633
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/expr.c.diff?cvsroot=gcc&r1=1.778&r2=1.779

Comment 5 Kazu Hirata 2005-03-02 16:08:11 UTC
Just checked in a patch.