Bug 54011 - missed optimization opportunities for bool struct/class members
Summary: missed optimization opportunities for bool struct/class members
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.7.1
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2012-07-18 09:27 UTC by Dennis Lubert
Modified: 2021-07-23 21:52 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dennis Lubert 2012-07-18 09:27:32 UTC
Hi, when taking the following code (or on http://tinyurl.com/bs2qa3h to easier play with (leads to gcc explorer, might of course be inactive after some time))

#define BSET //:1
struct A
{
  bool b0 BSET;
  bool b1 BSET;
  bool b2 BSET;
  bool b3 BSET;
  bool b4 BSET;
  bool b5 BSET;
  bool b6 BSET;
  bool b7 BSET;
};
  //#define TO (__LINE__%2)
  #define TO 0
 void foo( A& a )
  {
    a.b0 = TO;
    a.b1 = TO;
    a.b2 = TO;
    a.b3 = TO;
   #if 1
    a.b4 = TO;
    a.b5 = TO;
    a.b6 = TO;
    a.b7 = TO;
    #endif
  }

I see some missed opportunities for optimization. While clang compiles this to:

	movq	$0, (%rdi)
	ret

gcc will compile this to:

	movb	$0, (%rdi)
	movb	$0, 1(%rdi)
	movb	$0, 2(%rdi)
	movb	$0, 3(%rdi)
	movb	$0, 4(%rdi)
	movb	$0, 5(%rdi)
	movb	$0, 6(%rdi)
	movb	$0, 7(%rdi)
	ret

What I think a good debugger should do is:

In case of bool being one byte, determine the longest "chain" of values being set, group them into useful operation sizes (2/4/8/16 bytes maybe) and create an appropriate value to store to that location. In the case of the bools being bitset members (above BSET define to try it out) gcc already seems to do this quite fine (things will get the appropriate and/or bitwise operations applied).
Comment 1 Richard Biener 2012-07-18 09:35:42 UTC
dup

*** This bug has been marked as a duplicate of bug 23684 ***
Comment 2 Dennis Lubert 2018-01-15 09:55:15 UTC
The duplicate has been closed as resolved, however the compilation of this example has not changed and is imho still a good opportunity for optimization.

Have a look on the gcc explorer with -O3 and gcc 7.3 : https://godbolt.org/g/qWvpSg
Comment 3 Andrew Pinski 2021-07-23 21:52:41 UTC
Fixed in GCC 8 and above.