Last active
April 21, 2016 02:06
-
-
Save wjlafrance/104cbc625175050ca569aedac37fe1f8 to your computer and use it in GitHub Desktop.
Testing whether use of local variables changes optimized machine code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <stdio.h> | |
int __attribute__((noinline)) foo(void) { | |
return 1; | |
} | |
int __attribute__((noinline)) bar(void) { | |
return 2; | |
} | |
int __attribute__((noinline)) baz(int a1, int a2) { | |
return a1 + a2; | |
} | |
int main() { | |
printf("%d\n", baz(foo(), bar())); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <stdio.h> | |
int __attribute__((noinline)) foo(void) { | |
return 1; | |
} | |
int __attribute__((noinline)) bar(void) { | |
return 2; | |
} | |
int __attribute__((noinline)) baz(int a1, int a2) { | |
return a1 + a2; | |
} | |
int main() { | |
int qux = foo(); | |
int quux = bar(); | |
int corge = baz(qux, quux); | |
printf("%d\n", corge); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clang -S -emit-llvm ex1.c | |
clang -S -emit-llvm ex2.c | |
clang -O -S -mllvm --x86-asm-syntax=intel ex1.c | |
clang -O -S -mllvm --x86-asm-syntax=intel ex2.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ uname -a | |
Linux massachusetts 4.4.0-18-generic #34-Ubuntu SMP Wed Apr 6 14:01:02 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux | |
$ clang -v | |
clang version 3.8.0-2ubuntu1 (tags/RELEASE_380/final) | |
Target: x86_64-pc-linux-gnu | |
Thread model: posix | |
InstalledDir: /usr/bin | |
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/5.3.1 | |
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.0.0 | |
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.1 | |
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0 | |
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.3.1 | |
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.0.0 | |
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.3.1 | |
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.0.0 | |
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.3.1 | |
Candidate multilib: .;@m64 | |
Selected multilib: .;@m64 | |
$ diff ex1.ll ex2.ll | |
1c1 | |
< ; ModuleID = 'ex1.c' | |
--- | |
> ; ModuleID = 'ex2.c' | |
31a32,34 | |
> %qux = alloca i32, align 4 | |
> %quux = alloca i32, align 4 | |
> %corge = alloca i32, align 4 | |
33a37 | |
> store i32 %2, i32* %qux, align 4 | |
35,36c39,45 | |
< %4 = call i32 @baz(i32 %2, i32 %3) | |
< %5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %4) | |
--- | |
> store i32 %3, i32* %quux, align 4 | |
> %4 = load i32, i32* %qux, align 4 | |
> %5 = load i32, i32* %quux, align 4 | |
> %6 = call i32 @baz(i32 %4, i32 %5) | |
> store i32 %6, i32* %corge, align 4 | |
> %7 = load i32, i32* %corge, align 4 | |
> %8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %7) | |
$ diff ex1.s ex2.s | |
3c3 | |
< .file "ex1.c" | |
--- | |
> .file "ex2.c" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment