dotnet / jitutils

MIT License
146 stars 59 forks source link

Add FindRedundantTest() in AnalyzeAsm #329

Closed kunalspathak closed 3 years ago

kunalspathak commented 3 years ago

Added FindRedundantTest() method that looks for redundant test instruction in the dasm files. I have scoped it to limited jump instructions, but can be extended in the future. Used in https://github.com/dotnet/runtime/issues/53053.

kunalspathak commented 3 years ago

@dotnet/jit-contrib

kunalspathak commented 3 years ago

Would also be interesting to screen for cases where we have a non-move writing a register, followed by a test, followed by a jump.

In particular lea; test; jcc

I can add more patterns but I just restricted myself to handful of them for time sake. lea doesn't affect any flag, so wondering how can lea screening help? We won't be able to optimize test anyway in that case, right?

AndyAyersMS commented 3 years ago

how can lea screening help?

If we can find places where we're forming lea upstream of a jump we might want to reconsider and use a flag-setting instruction instead, as we may be happier overall since we can then omit the test.

For instance, if we have lea; test; jmp we might be better off with add; jump.

kunalspathak commented 3 years ago

For instance, if we have lea; test; jmp we might be better off with add; jump.

So although lea is faster than add, you are thinking that elimination of extra test will offset the degradation?

AndyAyersMS commented 3 years ago

Hard to say. Both lea; test; jcc and add; jcc will benefit from macro-fusion. But I suspect the latter is faster, provided we're only adding one thing in the lea.

kunalspathak commented 3 years ago

Just found 19 hits of lea like these:

; Assembly listing for method System.MathF:Round(float):float
[0053]       lea      ecx, [rsi+rsi]
[0054]       test     ecx, ecx
[0055]       jne      SHORT G_M2148_IG05