[Refactor] TAddNode.Simplify rearranging
Summary
This merge request seeks to simplify some of the optimisations in taddnode.simplify
by detecting whether one of the operands is an integer constant and ensuring it is always on the right-hand side for a commutative operation (i.e. addition, multiplication, bitwise AND, bitwise OR and bitwise XOR, but not subtraction). This results in the removal of duplicate code and hence increased maintainability.
System
- Processor architecture: All
Relevant logs and/or screenshots
There are a couple of noted cases where the generated code is slightly different, one in dateutils
and one in zdeflate
. In the case of zdeflate
, the register allocator switches some registers around. For dateutils
, some code executes in a different order, but the end result is the same - before:
.section .text.n_dateutils_$$_datetimetojuliandate$tdatetime$$double,"ax"
...
subl $3,%eax
addl %edx,%eax
movslq %ecx,%rcx ; ---- BLOCK 1 START ----
movq %rcx,%r8
movq %rcx,%rdx
sarq $63,%rdx
andq $3,%rdx
addq %rdx,%r8
sarq $2,%r8 ; ---- BLOCK 1 END----
cltq ; ---- BLOCK 2 START ----
imulq $153,%rax
leaq 2(%rax),%r9
movq $7378697629483820647,%rax
imulq %r9
sarq $1,%rdx
shrq $63,%r9
addq %r9,%rdx
movzwl 32(%rsp),%eax
addq %rdx,%rax
imulq $365,%rcx,%rdx
addq %rdx,%rax ; ---- BLOCK 2 END----
leaq (%r8,%rax),%r9
...
After:
.section .text.n_dateutils_$$_datetimetojuliandate$tdatetime$$double,"ax"
...
subl $3,%eax
addl %edx,%eax
cltq ; ---- BLOCK 2 START----
imulq $153,%rax
leaq 2(%rax),%r8
movq $7378697629483820647,%rax
imulq %r8
sarq $1,%rdx
shrq $63,%r8
addq %r8,%rdx
movzwl 32(%rsp),%eax
addq %rdx,%rax
movslq %ecx,%rcx ; ---- BLOCK 1 START---- (crossover start)
imulq $365,%rcx,%rdx
leaq (%rax,%rdx),%r8 ; ---- BLOCK 2 END---- (crossover end)
movq %rcx,%rax
movq %rcx,%rdx
sarq $63,%rdx
andq $3,%rdx
addq %rdx,%rax
sarq $2,%rax ; ---- BLOCK 1 END----
leaq (%r8,%rax),%r9
...
Additional notes
- With the
dateutils
example, there is no chance of side-effects because one of the operands has to be an integer constant for the node branch swap to take place, so if the other node is a function call, for example, it cannot affect the outcome. - Under x86_64-win64, -O4, the compiler is 512 bytes smaller.
Edited by J. Gareth "Kit" Moreton