Compilers are written as a pipeline: in particular, instruction selection and register allocation are different phases. GHC, for instance, uses maximal munch for instruction selection and a variety of register allocators. However, on x86-64 (for instance), register allocation constrains the particular instruction encodings, which affects the cost of some instructions.
Some infelicities:
In many cases, one can omit the REX prefix. However, this is not possible for
r8-r15.One can use a two-byte VEX prefix for certain registers, but others require a 3-byte VEX prefix.
The
testinstruction has a special encoding forraxwhen comparing to an integer. So encodingtestforraxspecifically saves one byte.The
loopinstruction decrementsrcxand performs a (short) conditional jump in one instruction.There are various constraints on register encodings when addressing. In particular,
rbpandr13must include a displacement andrspandr12always need a SIB byte. Thus if register allocation choosesrbp,r13,rsp, orr12then some encodings become longer.
All of these affect instruction length and thus code size, and in turn probably affect cache performance.
