From 15230bbb9b1f69def9b0e1b41a097638c0fda734 Mon Sep 17 00:00:00 2001 From: johannst Date: Tue, 28 Apr 2020 09:11:18 +0000 Subject: deploy: fef4d6ff2ad9f48e6dccde0f061453e6a3ac624e --- development/gcc.html | 269 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 development/gcc.html (limited to 'development/gcc.html') diff --git a/development/gcc.html b/development/gcc.html new file mode 100644 index 0000000..bf457aa --- /dev/null +++ b/development/gcc.html @@ -0,0 +1,269 @@ + + + + + + gcc - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + + + + +
+
+

gcc(1)

+

CLI

+

Preprocessing

+

While debugging can be helpful to just pre-process files.

+
gcc -E [-dM] ...
+
+
    +
  • -E run only preprocessor
  • +
  • -dM list only #define statements
  • +
+

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.

+
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
  • +
+
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
+
+ +
+ + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3