Better_Software_Header_Mobile Better_Software_Header_Web

Find what you need - explore our website and developer resources

Understanding Type-Based Alias Analysis in C and C++

How type systems shape compiler optimizations

void modifyValues(int *a, short *b) {
  *a = 42;
  *b = 3;
  *a += 5;
}
mov rax, qword ptr [rbp - 8]
mov dword ptr [rax], 42  ; *a = 42

mov rax, qword ptr [rbp - 16]
mov word ptr [rax], 3    ; *b = 3

mov rax, qword ptr [rbp - 8]
mov ecx, dword ptr [rax]
add ecx, 5               ; *a += 5;
mov word ptr [rsi], 3
mov dword ptr [rdi], 47
void modifyValues(int *a, short *b) {
  *b = 3;
  *a = 47;
}
void Foo(float *v, int *n) {
  for (int i = 0; i < *n; i++)
    v[i] += 1.0f;
}
void Foo(float v[], float *c, int n) {
  for (int i = 0; i < n; i++)
    v[i] = *c + 1.0f;
}
Foo(float*, int*):
    movsx rax, DWORD PTR [rsi]
    test eax, eax
    jle .L1
    movss xmm1, DWORD PTR .LC0[rip]
    lea rax, [rdi+rax*4]
.L3:
    movss xmm0, DWORD PTR [rdi]
    add rdi, 4
    addss xmm0, xmm1
    movss DWORD PTR [rdi-4], xmm0
    cmp rdi, rax
    jne .L3
.L1:
    ret
Foo(float*, float*, int):
    test edx, edx
    jle .L1
    movsx rdx, edx
    movss xmm1, DWORD PTR .LC0[rip]
    lea rax, [rdi+rdx*4]
.L3:
    movss xmm0, DWORD PTR [rsi]
    add rdi, 4
    addss xmm0, xmm1
    movss DWORD PTR [rdi-4], xmm0
    cmp rdi, rax
    jne .L3
movsx rax, DWORD PTR [rsi]
movss xmm0, DWORD PTR [rsi]
void Foo(float* restrict v, float* c, int n) {
  for (int i = 0; i < n; i++)
    v[i] = *c + 1.0f;
}
Foo:
test edx, edx
jle .L1
movsx rdx, edx
movss xmm0, DWORD PTR .LC0[rip]
lea rax, [rdi+rdx*4]
and edx, 1
je .L3
movss DWORD PTR [rdi], xmm0
add rdi, 4
and edx, 1
je .L11
.L3:
movss DWORD PTR [rdi], xmm0
add rdi, 8
movss DWORD PTR [rdi-4], xmm0
cmp rdi, rax
je .L11
movss xmm0, DWORD PTR .LC0[rip]  ; Load 1.0f into xmm0
addss xmm0, DWORD PTR [rsi]      ; Add *c to xmm0

About KDAB

ShivamKunwar

Shivam Kunwar

Software Engineer

Sign up for the KDAB Newsletter

Learn Modern C++

Learn more

Need help with performance issues?

Get in touch