Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT can generate redundant TEST operations #10742

Closed
Zhentar opened this issue Jul 21, 2018 · 1 comment · Fixed by #38586
Closed

JIT can generate redundant TEST operations #10742

Zhentar opened this issue Jul 21, 2018 · 1 comment · Fixed by #38586
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors optimization
Milestone

Comments

@Zhentar
Copy link

Zhentar commented Jul 21, 2018

The following C# code generates assembly which has what I understand to be a redundant test instruction:

private static int PopulatedByte(uint bytes, uint delimMask)
{
	var checkDelim = bytes ^ delimMask; //zero out matches, all others non-zero
	uint onlyDelimHasSign = Bmi1.AndNot( /*notted*/ checkDelim, checkDelim - INDEXOF_BYTEMULT32) & 0x80808080u;
	return onlyDelimHasSign == 0 ? -1 : ByteIndexThatIsntZero(onlyDelimHasSign);
}

private static int ByteIndexThatIsntZero(uint value)
{
	return (int)(Bmi1.TrailingZeroCount(value) >> 3);
}
	xor     r9d,0A0A0A0Ah
	lea     r10d,[r9-1010101h]
	andn    r9d,r9d,r10d
	and     r9d,80808080h
	test    r9d,r9d   ;not needed because  flags have already been set by preceding and
	je      M04_L07
	tzcnt   r9d,r9d
	shr     r9d,3
	jmp     M04_L08
M04_L07
	mov     r9d,0FFFFFFFFh
M04_L08

category:cq
theme:optimization
skill-level:beginner
cost:small

@saucecontrol
Copy link
Member

Another example here in a simple zero-terminated loop:

public void M(int i) {
    for (; i != 0; i--)
        Console.WriteLine("foo");
}

Current asm looks like:

C.M(Int32)
    L0000: push rsi
    L0001: sub rsp, 0x20
    L0005: mov esi, edx
    L0007: test esi, esi
    L0009: jz L0023
    L000b: mov rcx, 0x1389b488b28
    L0015: mov rcx, [rcx]
    L0018: call System.Console.WriteLine(System.String)
    L001d: dec esi
    L001f: test esi, esi  <-- ???
    L0021: jnz L000b
    L0023: add rsp, 0x20
    L0027: pop rsi
    L0028: ret

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors optimization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants