Skip to content

Instantly share code, notes, and snippets.

@tlewiscpp
Last active August 21, 2016 19:11
Show Gist options
  • Save tlewiscpp/8df16d3b75a423522f1da647e2aeff31 to your computer and use it in GitHub Desktop.
Save tlewiscpp/8df16d3b75a423522f1da647e2aeff31 to your computer and use it in GitHub Desktop.
Compiler Optimization Question
#!/bin/bash
mkdir normalBin
mkdir weirdBin
g++ -Wall -std=c++14 -O0 -S -o normalBin/normal-O0.s normal.cpp
g++ -Wall -std=c++14 -O1 -S -o normalBin/normal-O1.s normal.cpp
g++ -Wall -std=c++14 -O2 -S -o normalBin/normal-O2.s normal.cpp
g++ -Wall -std=c++14 -O3 -S -o normalBin/normal-O3.s normal.cpp
g++ -Wall -std=c++14 -Ofast -S -o normalBin/normal-Ofast.s normal.cpp
g++ -Wall -std=c++14 -Os -S -o normalBin/normal-Os.s normal.cpp
g++ -Wall -std=c++14 -O0 -S -o weirdBin/weird-O0.s weird.cpp
g++ -Wall -std=c++14 -O1 -S -o weirdBin/weird-O1.s weird.cpp
g++ -Wall -std=c++14 -O2 -S -o weirdBin/weird-O2.s weird.cpp
g++ -Wall -std=c++14 -O3 -S -o weirdBin/weird-O3.s weird.cpp
g++ -Wall -std=c++14 -Ofast -S -o weirdBin/weird-Ofast.s weird.cpp
g++ -Wall -std=c++14 -Os -S -o weirdBin/weird-Os.s weird.cpp
.file "normal.cpp"
.def __main; .scl 2; .type 32; .endef
.text
.global main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB0:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
.file "normal.cpp"
.def __main; .scl 2; .type 32; .endef
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB0:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
.file "normal.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB0:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
.file "normal.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB0:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
.file "normal.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB0:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
.file "normal.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB0:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
int main()
{
while (true) { }
return 0;
}
.file "weird.cpp"
.section .text$_ZN9EventLoop13movementStateEv,"x"
.linkonce discard
.align 2
.globl _ZN9EventLoop13movementStateEv
.def _ZN9EventLoop13movementStateEv; .scl 2; .type 32; .endef
.seh_proc _ZN9EventLoop13movementStateEv
_ZN9EventLoop13movementStateEv:
.LFB0:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
.seh_endprologue
movq %rcx, 16(%rbp)
movl $0, %eax
popq %rbp
ret
.seh_endproc
.globl Pigs
.bss
Pigs:
.space 1
.def __main; .scl 2; .type 32; .endef
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB1:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
call __main
.L5:
leaq Pigs(%rip), %rcx
call _ZN9EventLoop13movementStateEv
cmpl $3, %eax
setne %al
testb %al, %al
je .L4
jmp .L5
.L4:
movl $0, %eax
addq $32, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (GNU) 5.4.0"
.file "weird.cpp"
.def __main; .scl 2; .type 32; .endef
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB1:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.globl Pigs
.bss
Pigs:
.space 1
.ident "GCC: (GNU) 5.4.0"
.file "weird.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB1:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.globl Pigs
.bss
Pigs:
.space 1
.ident "GCC: (GNU) 5.4.0"
.file "weird.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB1:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.globl Pigs
.bss
Pigs:
.space 1
.ident "GCC: (GNU) 5.4.0"
.file "weird.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.p2align 4,,15
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB1:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.globl Pigs
.bss
Pigs:
.space 1
.ident "GCC: (GNU) 5.4.0"
.file "weird.cpp"
.def __main; .scl 2; .type 32; .endef
.section .text.startup,"x"
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
.LFB1:
subq $40, %rsp
.seh_stackalloc 40
.seh_endprologue
call __main
.L2:
jmp .L2
.seh_endproc
.globl Pigs
.bss
Pigs:
.space 1
.ident "GCC: (GNU) 5.4.0"
enum class MovementState {
Walking,
Running,
Swimming,
Flying
};
struct EventLoop {
MovementState movementState() {return MovementState::Walking;}
};
EventLoop Pigs;
int main()
{
while (Pigs.movementState() != MovementState::Flying) { }
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment