CHAPTER 12. CONDITIONAL JUMPS CHAPTER 12. CONDITIONAL JUMPS
12.3.4 MIPS.
Unfortunately, GCC 4.4.5 for MIPS is not very smart, either:
Listing 12.24: Optimizing GCC 4.4.5 (assembly output)
$LC0:
.ascii "it is not ten\000"
$LC1:
.ascii "it is ten\000"
f:
li $2,10 # 0xa
; compare $a0 and 10, jump if equal:
beq $4,$2,$L2
nop ; branch delay slot
; leave address of "it is not ten" string in $v0 and return:
lui $2,%hi($LC0)
j $31
addiu $2,$2,%lo($LC0)
$L2:
; leave address of "it is ten" string in $v0 and return:
lui $2,%hi($LC1)
j $31
addiu $2,$2,%lo($LC1)
12.3.5 Let’s rewrite it in anif/elseway
const char* f (int a)
{
if (a==10)
return "it is ten";
else
return "it is not ten";
};
Interestingly, optimizing GCC 4.8 for x86 was also able to useCMOVccin this case:
Listing 12.25: Optimizing GCC 4.8
.LC0:
.string "it is ten"
.LC1:
.string "it is not ten"
f:
.LFB0:
; compare input value with 10
cmp DWORD PTR [esp+4], 10
mov edx, OFFSET FLAT:.LC1 ; "it is not ten"
mov eax, OFFSET FLAT:.LC0 ; "it is ten"
; if comparison result is Not Equal, copy EDX value to EAX
; if not, do nothing
cmovne eax, edx
ret
Optimizing Keil in ARM mode generates code identical to listing.12.21.
But the optimizing MSVC 2012 is not that good (yet).
12.3.6 Conclusion.
Why optimizing compilers try to get rid of conditional jumps? Read here about it:33.1 on page 436.