Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created June 4, 2019 01:34
Show Gist options
  • Save kaityo256/8678fcaa8beeb54288b52567d8c224d7 to your computer and use it in GitHub Desktop.
Save kaityo256/8678fcaa8beeb54288b52567d8c224d7 to your computer and use it in GitHub Desktop.
Constant folding of array
#include <cstdio>
const int a[] = {1, 2, 3, 4};
int func(int b[4]) {
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += a[i] * b[i];
}
return sum;
}
int main() {
int b[] = {5, 6, 7, 8};
printf("%d\n", func(b));
}
@kaityo256
Copy link
Author

GCC and Clang can return an immediate value with -O3 -S.

main:
.LFB13:
  .cfi_startproc
  subq  $8, %rsp
  .cfi_def_cfa_offset 16
  movl  $70, %esi
  movl  $.LC1, %edi
  xorl  %eax, %eax
  call  printf
  xorl  %eax, %eax
  addq  $8, %rsp
  .cfi_def_cfa_offset 8
  ret

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