Consider the following code snippet static volatile short a[(2048)]; short foo (int len, int v) { int i; for (i = 0; i < len; i++) { a[i] = v; } return a[0]; }
This should generate a post_inc for accessing elements of a. The problem seems to be in the loop optimizer that fails to identify 'i' as an induction variable. For e.g the dump from 141r.loop2_invariant shows (insn 16 15 75 4 (set (reg/v:SI 103 [ i ]) (const_int 0 [0x0])) 161 {*arm_movsi_insn} (nil)) (insn 75 16 17 4 (set (reg/f:SI 118) (symbol_ref:SI ("^a") [flags 0x2] <var_decl 0xb7dc1000 a>)) -1 (nil)) (code_label 17 75 18 5 4 "" [0 uses]) (note 18 17 21 5 [bb 5] NOTE_INSN_BASIC_BLOCK) (insn 21 18 23 5 (set (reg:SI 110) (ashift:SI (reg/v:SI 103 [ i ]) (const_int 1 [0x1]))) 115 {*arm_shiftsi3} (nil)) (insn 23 21 25 5 (set (mem/s/v:HI (plus:SI (reg:SI 110) (reg/f:SI 118)) [3 a S2 A16]) (subreg/s/u:HI (reg:SI 102 [ pretmp.21 ]) 0)) 171 {*movhi_insn_arch4} (nil)) (insn 25 23 26 5 (set (reg/v:SI 103 [ i ]) (plus:SI (reg/v:SI 103 [ i ]) (const_int 1 [0x1]))) 4 {*arm_addsi3} (nil)) (insn 26 25 27 5 (set (reg:CC 24 cc) (compare:CC (reg/v:SI 106 [ len ]) (reg/v:SI 103 [ i ]))) 214 {*arm_cmpsi_insn} (nil)) (jump_insn 27 26 82 5 (set (pc) (if_then_else (gt (reg:CC 24 cc) (const_int 0 [0x0])) (label_ref:SI 82) (pc))) 224 {*arm_cond_branch} (expr_list:REG_BR_PROB (const_int 9900 [0x26ac]) (nil))) Here reg 103 i.e 'i' is incremented by 1 always and therefore 110 always increases by 2 which means that in each iteration the address in INSN 23 is 2 more than what it was in the previous iteration. If the access is converted into something like *118 = v 118 += 2, then the auto-inc-dec pass will be able to convert this into a post_inc Version Details: GNU C version 4.3.0 20070316 (experimental) (arm-none-eabi) compiled by GNU C version 3.4.6 (Ubuntu 3.4.6-1ubuntu2). GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096 -- Summary: Missed induction variable optimization Product: gcc Version: 4.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: pranav dot bhandarkar at gmail dot com GCC target triplet: arm-none-eabi http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32283