http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56925
Bug #: 56925 Summary: SRA should take into account likelihood of statements being executed Classification: Unclassified Product: gcc Version: 4.9.0 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: josh.m.con...@gmail.com In the following code: struct stype { unsigned int pad:4; unsigned int val:4; }; void bar (void); void baz (void); int x, y; unsigned int foo (struct stype input) { if (__builtin_expect (x, 0)) return input.val; if (__builtin_expect (y, 0)) return input.val + 1; return 0; } When compiled with -O2, SRA moves the read of input.val to the top of the function: ;; Function foo (foo, funcdef_no=0, decl_uid=4988, cgraph_uid=0) Candidate (4987): input Rejected (4999): not aggregate: y.1 Rejected (4993): not aggregate: x.0 Created a replacement for input offset: 4, size: 4: input$val ... <bb 2>: input$val_14 = input.val; x.0_3 = x; _4 = __builtin_expect (x.0_3, 0); if (_4 != 0) goto <bb 3>; else goto <bb 4>; ... Which means that the critical path for this function now executes an extra instruction. It would be nice if SRA would take into account the likelihood of statement execution when deciding whether to apply the transformation. We currently verify that there are at least two reads -- perhaps we should check that there are at least two reads that are likely to occur. This can be seen in sub-optimal codegen for ARM, where a bitfield extract (ubfx) is moved out of unlikely code into the critical path: foo: movw r3, #:lower16:x ubfx r2, r0, #4, #4 movt r3, #:upper16:x ldr r3, [r3] cmp r3, #0 bne .L6 ...