Bug 47133 - code size opportunity for boolean expression evaluation
Summary: code size opportunity for boolean expression evaluation
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2010-12-31 08:05 UTC by Carrot
Modified: 2021-07-29 22:40 UTC (History)
1 user (show)

See Also:
Host:
Target: arm-eabi
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-01-07 14:29:29


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Carrot 2010-12-31 08:05:42 UTC
Compile the following code with options -march=armv7-a -mthumb -Os

struct S
{
  int f1, f2;
};

int t04(int x, struct S* p)
{
  return p->f1 == 9 && p->f2 == 0;
}

GCC 4.6 generates:

t04:
	ldr	r3, [r1, #0]
	cmp	r3, #9             // A
	bne	.L3
	ldr	r0, [r1, #4]
	rsbs	r0, r0, #1
	it	cc
	movcc	r0, #0
	bx	lr                 // C
.L3:
	movs	r0, #0             // B
	bx	lr


Instruction B can be moved before instruction A, and instruction C can be removed. 

t04:
	ldr	r3, [r1, #0]
        movs    r0, #0
	cmp	r3, #9            
	bne	.L3
	ldr	r0, [r1, #4]
	rsbs	r0, r0, #1
	it	cc
	movcc	r0, #0
.L3:
	bx	lr

When compiled to arm instructions, it has the same problem.

It should be enabled for code size optimization only because it may execute one more instruction run time.

Looks like an if-conversion opportunity.
Comment 1 Ian Bolton 2011-01-07 14:29:29 UTC
I have confirmed this on r168563 of trunk.
Comment 2 Andrew Pinski 2021-07-29 22:40:06 UTC
5.4+ produces:
        ldr     r3, [r1]
        cmp     r3, #9
        ittte   eq
        ldreq   r0, [r1, #4]
        clzeq   r0, r0
        lsreq   r0, r0, #5
        movne   r0, #0
        bx      lr