diff options
author | johannst <johannes.stoelp@gmail.com> | 2020-03-19 22:14:18 +0100 |
---|---|---|
committer | johannst <johannes.stoelp@gmail.com> | 2020-03-19 22:14:18 +0100 |
commit | 6259d4ee6c06cc6ae4ce07484b75f7d327a6a52a (patch) | |
tree | fa2cd36fc421ed801459403024a70106641c0999 | |
parent | 72236e27a609df9b434ba14b213e8e44be8fd7fa (diff) | |
download | notes-6259d4ee6c06cc6ae4ce07484b75f7d327a6a52a.tar.gz notes-6259d4ee6c06cc6ae4ce07484b75f7d327a6a52a.zip |
added gcc
-rw-r--r-- | src/SUMMARY.md | 1 | ||||
-rw-r--r-- | src/gcc.md | 58 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 45c07b5..80aced1 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -24,3 +24,4 @@ - [c++filt](./c++filt.md) - [c++](./c++.md) - [glibc](./glibc.md) +- [gcc](./gcc.md) diff --git a/src/gcc.md b/src/gcc.md new file mode 100644 index 0000000..1381242 --- /dev/null +++ b/src/gcc.md @@ -0,0 +1,58 @@ +# gcc(1) + +## CLI + +### Preprocessing +While debugging can be helpful to just pre-process files. + +```bash +gcc -E [-dM] ... +``` +- `-E` run only preprocessor +- `-dM` list only `#define` statements + +## [Builtins][builtins] + +### `__builtin_expect(expr, cond)` +Give the compiler a hint which branch is hot, so it can lay out the code +accordingly to reduce number of jump instructions. +See on [compiler explorer](https://godbolt.org/z/MbTHAP). + +```bash +echo " +extern void foo(); +extern void bar(); +void run0(int x) { + if (__builtin_expect(x,0)) { foo(); } + else { bar(); } +} +void run1(int x) { + if (__builtin_expect(x,1)) { foo(); } + else { bar(); } +} +" | gcc -O2 -S -masm=intel -o /dev/stdout -xc - +``` + +Will generate something similar to the following. +- `run0`: `bar` is on the path without branch +- `run1`: `foo` is on the path without branch +```c +run0: + test edi, edi + jne .L4 + xor eax, eax + jmp bar +.L4: + xor eax, eax + jmp foo +run1: + test edi, edi + je .L6 + xor eax, eax + jmp foo +.L6: + xor eax, eax + jmp bar +``` + +[builtins]: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html |