1.14. CONDITIONAL JUMPS
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 1.130: 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.1.126.
But the optimizing MSVC 2012 is not that good (yet).
Conclusion
Whyoptimizingcompilerstrytogetridofconditionaljumps? Readhereaboutit:2.10.1onpage466.
1.14.4 Getting minimal and maximal values.
32-bit
int my_max(int a, int b)
{
if (a>b)
return a;
else
return b;
};
int my_min(int a, int b)
{
if (a<b)
return a;
else
return b;
};
Listing 1.131: Non-optimizing MSVC 2013
_a$ = 8
_b$ = 12
_my_min PROC
push ebp
mov ebp, esp
mov eax, DWORD PTR _a$[ebp]
; compare A and B:
cmp eax, DWORD PTR _b$[ebp]
; jump, if A is greater or equal to B: