Currently, InstCombine aggressively merge GEPs because it can help backends make
better decision with complex addressing modes. However, in some cases, merging
GEPs can cause more instructions. This change will skip merging GEPs if a
non-free GEP is used in multiple basic blocks.
For the test-case (unit_skip_gep_merge.ll), this patch change code generation (-O3) for AArch64 :
from :
foo: orr w8, wzr, #0x18 madd x8, x2, x8, x0 ldr x8, [x8, #8] str x8, [x4] tbz w1, #0, .LBB0_2 // %bb.1: orr w8, wzr, #0x18 madd x8, x2, x8, x0 ldr x8, [x8, #16] str x8, [x3] .LBB0_2: ret
to :
foo: orr w8, wzr, #0x18 madd x8, x2, x8, x0 ldr x9, [x8, #8] str x9, [x4] tbz w1, #0, .LBB0_2 // %bb.1: ldr x8, [x8, #16] str x8, [x3] .LBB0_2: ret
For array GEPs, we have a check to try to make sure we aren't making a GEP more expensive (see EndsWithSequential etc.) Maybe instead of adding a different kind of check, it would make more sense to extend the existing check to struct GEPs?