[llvm-commits] two-byte return optimization for AMD

Nick Lewycky nicholas at mxc.ca
Sun Dec 7 10:28:34 PST 2008


Dan Gohman wrote:
> Hi NIck,
> 
> The AMD family 10h aka Barcelona optimization guide recommends using
> "ret 0" for this purpose, which is a three-byte ret.  The guide for  
> earlier AMD
> processors does recommend "rep ret" though.  I don't know how much
> difference this makes.

The Intel manual states that rep is only defined for the string 
instructions, making "rep ret" an illegal instruction. It still works on 
every X86 CPU and is one byte cheaper than a three byte ret. Regardless, 
this should only be used on AuthenticAMD CPUs.

> addAssemblyEmitter isn't an ideal place to add the pass because it's not
> used on the JIT path.  I think it would be appropriate to add a new
> TargetMachine hook here.

Thanks. I moved it to addPreEmitPass, as you suggested on IRC.

>  > +    MachineInstr &MI = MBB.front();
>  > +    if (MI.getOpcode() == X86::RET) {
> 
> It might be useful to skip past any labels or IMPLICIT_DEF at the  
> beginning
> of the block, since those don't produce any actual instructions.

Good catch! That improved the number of two-byte rets.

Is there a better way to look for labels than testing the opcode against 
DBG_LABEL, EH_LABEL and GC_LABEL? I don't see any 'isLabel()' method on 
TII for example.

>  > +      BuildMI(MBB, MBB.begin(), TII->get(X86::REP_RET));
>  > +      MI.eraseFromParent();
> 
> You can use MI.setDesc(TII->get(X86::REP_RET)) to change the
> instruction in-place.

That's much better.

Thanks for the review!

Nick

> Dan
> 
> On Dec 7, 2008, at 12:19 AM, Nick Lewycky wrote:
> 
>> The AMD optimization manual suggests avoiding branches to 'ret'  
>> instructions, preferring to emit 'rep; ret'. The attached patch  
>> implements this.
>>
>> This doesn't apply very often across llvm-test. The largest is  
>> kimwitu++ which produces 28 of these two-byte rets. The performance  
>> impact seems to be less than noise, at least on my system.
>>
>> Please review!
>>
>> Nick
>> <x86-twobyteret.patch>_______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: x86-twobyteret.patch
Type: text/x-diff
Size: 9985 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20081207/a8736f59/attachment.patch>


More information about the llvm-commits mailing list