Skip to content

Add AoCO 2025 Day 01 Study Notes - #24

Merged
gapry merged 10 commits into
mainfrom
AoCO-2025-12-01
Jan 1, 2026
Merged

gapry merged 10 commits into
mainfrom
AoCO-2025-12-01

Conversation

@gapry

@gapry gapry commented Jan 1, 2026

Copy link
Copy Markdown
Owner

These notes are based on the post Why xor eax, eax?, which is Day 1 of the Advent of Compiler Optimisations 2025 Series by Matt Godbolt.

Copilot AI review requested due to automatic review settings January 1, 2026 02:07
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @gapry, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request adds comprehensive study notes that explore the compiler optimization technique involving the xor eax, eax instruction. It provides a comparative analysis of GCC and Clang's assembly output across various optimization levels, elucidates the x86-64 architecture's zero-extension mechanism, and illustrates how this optimization is applied to efficiently handle function return values and arguments.

Highlights

  • New Study Notes Added: Introduced a new set of study notes titled 'Why xor eax, eax?' as part of the Advent of Compiler Optimisations 2025 series.
  • Compiler Optimization Analysis: The notes provide a detailed comparison of assembly output generated by GCC and Clang at different optimization levels (-O0, -O1, -O2), highlighting instruction efficiency.
  • x86-64 Zero-Extension Explained: Explains the x86-64 architecture's zero-extension behavior, specifically how writing to a 32-bit register automatically clears the upper 32 bits of its corresponding 64-bit register.
  • Application of xorl: Demonstrates the use of the xorl instruction for efficiently zeroing out registers for function return values and argument passing, complete with LLDB examples.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds comprehensive study notes on compiler optimizations, specifically focusing on the xor eax, eax optimization. The content is well-structured and educational, covering different optimization levels and their effects on assembly output.

Issues Found:

  • Several typos throughout the document that need correction
  • Missing newline at end of file
  • Duplicate command line in one of the output blocks

Summary:
The technical content is accurate and provides valuable insights into compiler behavior. Once the typos are fixed, this will be a solid addition to the blog. All issues identified are minor formatting and spelling corrections that can be easily addressed with the provided suggestions.


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

lldb version 18.1.8
{% endhighlight %}

#### The different between `-O0`, `-O1`, and `-O2`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo in section heading. "different" should be "difference".

Suggested change
#### The different between `-O0`, `-O1`, and `-O2`
#### The difference between `-O0`, `-O1`, and `-O2`


It reduces the output from six instructions to three by removing the stack frame setup.

###### Use `-O2` as optimzing level

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo in section heading. "optimzing" should be "optimization".

Suggested change
###### Use `-O2` as optimzing level
###### Use `-O2` as optimization level

87 0 0 87 57 main.o
{% endhighlight %}

As you can see, `-02` and `-O1` are both produce three instructions.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo: "-02" should be "-O2" (capital O, not zero).

Suggested change
As you can see, `-02` and `-O1` are both produce three instructions.
As you can see, `-O2` and `-O1` are both produce three instructions.


As you can see, `-02` and `-O1` are both produce three instructions.
The only differences is that `-O2` changes from `movl` to `xorl`.
The reason is the instructon size. `xorl %eax, %eax` only use two bytes,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo: "instructon" should be "instruction".

Suggested change
The reason is the instructon size. `xorl %eax, %eax` only use two bytes,
The reason is the instruction size. `xorl %eax, %eax` only use two bytes,

Comment on lines +152 to +162
{% highlight bash %}
rm -f *.o; clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o

main.o: file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <main>:
0: 31 c0 xorl %eax, %eax
2: c3 retq
{% endhighlight %}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove duplicate command line from output. The command should only appear in the previous code block, not in the output.

Suggested change
{% highlight bash %}
rm -f *.o; clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
main.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <main>:
0: 31 c0 xorl %eax, %eax
2: c3 retq
{% endhighlight %}
{% highlight bash %}
main.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <main>:
0: 31 c0 xorl %eax, %eax
2: c3 retq
{% endhighlight %}

2: c3 retq
{% endhighlight %}

As we know, the x86-64 calling converstion requires the return value to be stored in the

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo: "converstion" should be "convention".

Suggested change
As we know, the x86-64 calling converstion requires the return value to be stored in the
As we know, the x86-64 calling convention requires the return value to be stored in the

| 5th | `%r8` | `%r8d` |
| 6th | `%r9` | `%r9d` |

The `xorl` optimization does not only apper for return values, you will also see it frequently

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo: "apper" should be "appear".

Suggested change
The `xorl` optimization does not only apper for return values, you will also see it frequently
The `xorl` optimization does not only appear for return values, you will also see it frequently

Comment on lines +308 to +309
## References
- [x64 architecture](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture) No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing newline at end of file to follow standard conventions.

Suggested change
## References
- [x64 architecture](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture)
## References
- [x64 architecture](

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds detailed study notes about the xor eax, eax compiler optimization, which is a great addition. The notes are well-structured and provide clear examples with different compilers and optimization levels. I've found a few typos and grammatical errors in the text. Addressing these will improve the clarity and professionalism of the post. I've left specific suggestions for these minor corrections.

lldb version 18.1.8
{% endhighlight %}

#### The different between `-O0`, `-O1`, and `-O2`

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a typo in the heading. 'different' should be 'difference'.

Suggested change
#### The different between `-O0`, `-O1`, and `-O2`
#### The difference between `-O0`, `-O1`, and `-O2`

'----------' '------------' '------------'
{% endhighlight %}

For now, we use `main.c` as input (shown below), apply different optimization level,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a minor grammatical error here. 'optimization level' should be plural, 'optimization levels', since you are applying different ones.

Suggested change
For now, we use `main.c` as input (shown below), apply different optimization level,
For now, we use `main.c` as input (shown below), apply different optimization levels,


It reduces the output from six instructions to three by removing the stack frame setup.

###### Use `-O2` as optimzing level

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a typo in 'optimizing'.

Suggested change
###### Use `-O2` as optimzing level
###### Use `-O2` as optimizing level

Comment on lines +141 to +145
As you can see, `-02` and `-O1` are both produce three instructions.
The only differences is that `-O2` changes from `movl` to `xorl`.
The reason is the instructon size. `xorl %eax, %eax` only use two bytes,
making it smaller than the five bytes `movl $0x0, %eax`.
Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

I've noticed a few typos and grammatical errors in this paragraph:

  • In the first sentence, -02 should be -O2, and 'are both produce' should be 'both produce'.
  • In the second sentence, 'differences is' should be 'difference is'.
  • In the third sentence, 'instructon' should be 'instruction', and 'use' should be 'uses'.
Suggested change
As you can see, `-02` and `-O1` are both produce three instructions.
The only differences is that `-O2` changes from `movl` to `xorl`.
The reason is the instructon size. `xorl %eax, %eax` only use two bytes,
making it smaller than the five bytes `movl $0x0, %eax`.
Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes.
As you can see, `-O2` and `-O1` both produce three instructions.
The only difference is that `-O2` changes from `movl` to `xorl`.
The reason is the instruction size. `xorl %eax, %eax` only uses two bytes,
making it smaller than the five bytes `movl $0x0, %eax`.
Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes.

2: c3 retq
{% endhighlight %}

You will find that the Clang's `-O1` output already use `xorl`, making it similar to GCC's `-O2`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Minor grammatical correction: 'use' should be 'uses'.

Suggested change
You will find that the Clang's `-O1` output already use `xorl`, making it similar to GCC's `-O2`.
You will find that the Clang's `-O1` output already uses `xorl`, making it similar to GCC's `-O2`.

Comment on lines +193 to +194
As we know, the x86-64 calling converstion requires the return value to be stored in the
64-bit `rax` register. However, we see that the compiler use the 32-bit `eax` register for the `xorl` instruction.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are a couple of minor corrections needed in this sentence:

  • 'converstion' should be 'convention'.
  • 'compiler use' should be 'compiler uses'.
Suggested change
As we know, the x86-64 calling converstion requires the return value to be stored in the
64-bit `rax` register. However, we see that the compiler use the 32-bit `eax` register for the `xorl` instruction.
As we know, the x86-64 calling convention requires the return value to be stored in the
64-bit `rax` register. However, we see that the compiler uses the 32-bit `eax` register for the `xorl` instruction.

{% endhighlight %}

According to the x86-64 System V ABI, the first six integer or pointer arguments are passed in
specific registers. To pass `0` to all of them, the compiler again use the `xorl` optimization to

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Minor grammatical correction: 'use' should be 'uses'.

Suggested change
specific registers. To pass `0` to all of them, the compiler again use the `xorl` optimization to
specific registers. To pass `0` to all of them, the compiler again uses the `xorl` optimization to

Comment on lines +304 to +306
The `xorl` optimization does not only apper for return values, you will also see it frequently
when a caller prepares arguments for a callee, As with the previous example, zeroing the 32-bit
version of these registers automatically zero-extends to the full 64-bit register.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

I found a typo and a punctuation issue here:

  • 'apper' should be 'appear'.
  • There should be a period after 'callee' to separate the two sentences.
Suggested change
The `xorl` optimization does not only apper for return values, you will also see it frequently
when a caller prepares arguments for a callee, As with the previous example, zeroing the 32-bit
version of these registers automatically zero-extends to the full 64-bit register.
The `xorl` optimization does not only appear for return values, you will also see it frequently
when a caller prepares arguments for a callee. As with the previous example, zeroing the 32-bit
version of these registers automatically zero-extends to the full 64-bit register.

version of these registers automatically zero-extends to the full 64-bit register.

## References
- [x64 architecture](https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture) No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The linked Microsoft documentation is for the Windows x64 calling convention. Since all the examples in this post are on Linux (Ubuntu), which uses the System V ABI, it would be more accurate to link to the System V ABI documentation. For example, you could link to the Wikipedia page or the official ABI document.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive study notes for Day 1 of the Advent of Compiler Optimisations 2025 series, exploring the "Why xor eax, eax?" compiler optimization technique. The notes provide practical examples demonstrating how different optimization levels affect assembly generation and includes hands-on verification using LLDB.

Key Changes:

  • Added detailed technical documentation about x86-64 compiler optimizations with practical examples
  • Demonstrated differences between -O0, -O1, and -O2 optimization levels in GCC and Clang
  • Explained zero-extension behavior in x86-64 architecture with debugging verification

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.

File Description
_posts/2026-01-01-Advent-of-Compiler-Optimisations-Study-Notes-01.md New 309-line study notes document covering xor eax optimization, including development environment setup, optimization level comparisons, register behavior explanations, and calling convention examples
_posts/2025-12-17-notes.md Removed draft notes file, likely replaced by the new comprehensive document

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

87 0 0 87 57 main.o
{% endhighlight %}

As you can see, `-02` and `-O1` are both produce three instructions.

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammatical error: "are both produce" should be "both produce".

Suggested change
As you can see, `-02` and `-O1` are both produce three instructions.
As you can see, `-02` and `-O1` both produce three instructions.

Copilot uses AI. Check for mistakes.
{% endhighlight %}

As you can see, `-02` and `-O1` are both produce three instructions.
The only differences is that `-O2` changes from `movl` to `xorl`.

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in "differences": "differences" should be singular "difference" in this context.

Suggested change
The only differences is that `-O2` changes from `movl` to `xorl`.
The only difference is that `-O2` changes from `movl` to `xorl`.

Copilot uses AI. Check for mistakes.

As you can see, `-02` and `-O1` are both produce three instructions.
The only differences is that `-O2` changes from `movl` to `xorl`.
The reason is the instructon size. `xorl %eax, %eax` only use two bytes,

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple issues on this line: "instructon" should be "instruction", and "use" should be "uses".

Suggested change
The reason is the instructon size. `xorl %eax, %eax` only use two bytes,
The reason is the instruction size. `xorl %eax, %eax` only uses two bytes,

Copilot uses AI. Check for mistakes.
2: c3 retq
{% endhighlight %}

As we know, the x86-64 calling converstion requires the return value to be stored in the

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "converstion" should be spelled "convention".

Suggested change
As we know, the x86-64 calling converstion requires the return value to be stored in the
As we know, the x86-64 calling convention requires the return value to be stored in the

Copilot uses AI. Check for mistakes.
| 5th | `%r8` | `%r8d` |
| 6th | `%r9` | `%r9d` |

The `xorl` optimization does not only apper for return values, you will also see it frequently

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "apper" should be spelled "appear".

Suggested change
The `xorl` optimization does not only apper for return values, you will also see it frequently
The `xorl` optimization does not only appear for return values, you will also see it frequently

Copilot uses AI. Check for mistakes.
lldb version 18.1.8
{% endhighlight %}

#### The different between `-O0`, `-O1`, and `-O2`

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in "different". The word "difference" should be used here instead of "different".

Suggested change
#### The different between `-O0`, `-O1`, and `-O2`
#### The difference between `-O0`, `-O1`, and `-O2`

Copilot uses AI. Check for mistakes.

It reduces the output from six instructions to three by removing the stack frame setup.

###### Use `-O2` as optimzing level

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "optimzing" should be spelled "optimization".

Suggested change
###### Use `-O2` as optimzing level
###### Use `-O2` as optimization level

Copilot uses AI. Check for mistakes.
@gapry
gapry merged commit 8487d77 into main Jan 1, 2026
1 check passed
@gapry
gapry deleted the AoCO-2025-12-01 branch January 1, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants