Code enlargement
Frank Klemm
pfk@fuchs.offl.uni-jena.de
Sat Sep 1 08:56:00 GMT 2001
C-Code:
...1
if ( x == 0 ) {
...2
}
...3
return;
gcc2.95:
...1
cmpl $0,x
jne lbl
...2
lbl: ...3
ret
gcc3.0 tends to code this like (also with -Os):
...1
cmpl $0,x
je lbl1
lbl2: ...3
ret
lbl1: ...2 \ code
jmp lbl2 / junks
Problems:
- code is at least 2 byte longer (2 jumps instead of 1 jump)
- code often is 9 byte longer (distance becomes larger than
-128...+127 bytes, so a 'Jcc long' (6 byte) and 'JMP long'
(5 byte) is needed.
Solutions:
- avoid this trick with '-Os', may be also with '-O2', because speed
increase is very little, zero or below zero. Brnach prediction
seems to work very well.
- avoid trick especially if jump distance is too long compared with
the nontricky code.
- Insert the code junks as early as possible (after every jmp this
is possible, or before the function start). But be carefully to
avoid other code increases inside the function
- if code is small and target is Pentium Pro, II, III, 4, Duron, Athlon
often a conditional-mov works well (especially if the conditional
expression is highly inpredictable).
if ( expect (x>12,0) ) // condition is seldom true
if ( expect (x>12,1) ) // condition is very often true
The current expect() syntax don't allow to describe a condition
which is high unpredictable so that jumps should be avoided and cmov's
should be used if possible.
--
Frank Klemm
More information about the Gcc
mailing list