http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52338
Bug #: 52338 Summary: shorter abs thumb2 code sequences Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target AssignedTo: unassig...@gcc.gnu.org ReportedBy: car...@google.com Target: arm-linux-androideabi Compile the following code with options -march=armv7-a -mthumb -Os char placex[] = {1, 2, 3, 4}; char placey[] = {5, 6, 7, 8}; int t0k(int b, int w) { int h1 = placex[b] - placex[w]; int h2 = placey[b] - placey[w]; if (h1 < 0) h1 = -h1; if (h2 < 0) h2 = -h2; return h1 + h2; } GCC 4.7 generates: t0k: ldr r3, .L2 push {r4, lr} ldrb r4, [r3, r0] @ zero_extendqisi2 adds r0, r3, r0 ldrb r2, [r3, r1] @ zero_extendqisi2 adds r3, r3, r1 ldrb r0, [r0, #4] @ zero_extendqisi2 ldrb r3, [r3, #4] @ zero_extendqisi2 subs r2, r4, r2 subs r3, r0, r3 eor r0, r2, r2, asr #31 // abs(h1) sub r0, r0, r2, asr #31 // abs(h1) cmp r3, #0 // abs(h2) it lt // abs(h2) rsblt r3, r3, #0 // abs(h2) adds r0, r0, r3 pop {r4, pc} It's interesting that gcc generats two different code sequence for abs(h1) and abs(h2). After carefully studying the insn pattern "*thumb2_abssi2", I got the impression that the selection of code sequence is depends on if the result register is same as source register. If they are same, the cmp code sequence is used, otherwise the pure arithmetic code sequence is used. But the cmp code sequence can also be used when the source is not the same as target register, and may result in smaller code size, like in this case. Another question is there any performance differences between these two different code sequences?