Skip to content

improve zero-init code for x64. #52286

Description

@sandreenko

For sequences of zero-inits like:

        long l1 = 0;
        long l2 = 0;
        long l3 = 0;
        long l4 = 0;

on x64 we would generate:

IN000e: 000038 xor      r13d, r13d
IN000f: 00003B mov      qword ptr [rsp+58H], r13
IN0010: 000040 xor      ecx, ecx
IN0011: 000042 mov      qword ptr [rsp+50H], rcx
IN0012: 000047 xor      ecx, ecx
IN0013: 000049 mov      qword ptr [rsp+48H], rcx
IN0014: 00004E xor      ecx, ecx
IN0015: 000050 mov      qword ptr [rsp+40H], rcx
IN0016: 000055 xor      ecx, ecx
IN0017: 000057 mov      qword ptr [rsp+38H], rcx
IN0018: 00005C xor      ecx, ecx
IN0019: 00005E mov      qword ptr [rsp+30H], rcx
IN001a: 000063 xor      ecx, ecx
IN001b: 000065 mov      qword ptr [rsp+28H], rcx
IN001c: 00006A xor      ecx, ecx

and for sequences like:

        ManyShorts s = GetManyShorts(); // a struct with many shorts
        s.l1 = 0;
        s.l2 = 0;
        s.l3 = 0;
        s.l4 = 0;
        s.l5 = 0;

we would generate:

IN0003: 000027 mov      word  ptr [rsp+20H], 0
IN0004: 00002E mov      word  ptr [rsp+22H], 0
IN0005: 000035 mov      word  ptr [rsp+24H], 0
IN0006: 00003C mov      word  ptr [rsp+26H], 0
IN0007: 000043 mov      word  ptr [rsp+28H], 0
IN0008: 00004A mov      word  ptr [rsp+2AH], 0

when a good CQ would be to zero one register and reuse it after, like:

IN000e: 000038 xor      ecx, ecx
IN000f: 00003B mov      qword ptr [rsp+58H], rcx
IN0011: 000042 mov      qword ptr [rsp+50H], rcx
IN0013: 000049 mov      qword ptr [rsp+48H], rcx
IN0015: 000050 mov      qword ptr [rsp+40H], rcx
IN0017: 000057 mov      qword ptr [rsp+38H], rcx
IN0019: 00005E mov      qword ptr [rsp+30H], rcx
IN001b: 000065 mov      qword ptr [rsp+28H], rcx

maybe CSE or GTF_REUSE_REG_VAL can help with it.

The full test:
Program.txt
The jitdumps:
example.Test4.txt
example.Test3.txt

category:cq
theme:zero-init

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions