Skip to content

Instantly share code, notes, and snippets.

@hanazuki
Last active September 7, 2015 15:01
Show Gist options
  • Save hanazuki/26e16eff59cb8c7c06fa to your computer and use it in GitHub Desktop.
Save hanazuki/26e16eff59cb8c7c06fa to your computer and use it in GitHub Desktop.
INT_MIN / -1 Optimization
hanazuki@ring% clang++ --version
Debian clang version 3.0-6.2 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
hanazuki@ring% clang++ -S -O2 test.cc
#include<stdio.h>
#include<limits.h>
int main() {
int x = INT_MIN;
int y = 1;
printf("%d\n", x / y);
return 0;
}
.file "test-1.cc"
.text
.globl main
.align 16, 0x90
.type main,@function
main: # @main
.Ltmp2:
.cfi_startproc
# BB#0:
pushq %rbp
.Ltmp3:
.cfi_def_cfa_offset 16
.Ltmp4:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp5:
.cfi_def_cfa_register %rbp
movl $.L.str, %edi
movl $-2147483648, %esi # imm = 0xFFFFFFFF80000000
xorb %al, %al
callq printf
xorl %eax, %eax
popq %rbp
ret
.Ltmp6:
.size main, .Ltmp6-main
.Ltmp7:
.cfi_endproc
.Leh_func_end0:
.type .L.str,@object # @.str
.section .rodata.str1.1,"aMS",@progbits,1
.L.str:
.asciz "%d\n"
.size .L.str, 4
.section ".note.GNU-stack","",@progbits
#include<stdio.h>
#include<limits.h>
int main() {
int x = INT_MIN;
int y = -1;
printf("%d\n", x / y);
return 0;
}
.file "test.cc"
.text
.globl main
.align 16, 0x90
.type main,@function
main: # @main
.Ltmp2:
.cfi_startproc
# BB#0:
pushq %rbp
.Ltmp3:
.cfi_def_cfa_offset 16
.Ltmp4:
.cfi_offset %rbp, -16
movq %rsp, %rbp
.Ltmp5:
.cfi_def_cfa_register %rbp
movl $.L.str, %edi
xorb %al, %al
callq printf
xorl %eax, %eax
popq %rbp
ret
.Ltmp6:
.size main, .Ltmp6-main
.Ltmp7:
.cfi_endproc
.Leh_func_end0:
.type .L.str,@object # @.str
.section .rodata.str1.1,"aMS",@progbits,1
.L.str:
.asciz "%d\n"
.size .L.str, 4
.section ".note.GNU-stack","",@progbits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment