From 9244052ee186c91fff4626aea8afccb7d2d9a189 Mon Sep 17 00:00:00 2001 From: gapry Date: Tue, 31 Mar 2026 22:14:10 +0800 Subject: [PATCH 1/5] Posts: Add AoCO 2025 Day 09 Study Notes --- ...f-Compiler-Optimisations-Study-Notes-09.md | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md diff --git a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md new file mode 100644 index 0000000..626b2fe --- /dev/null +++ b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md @@ -0,0 +1,122 @@ +--- +tags: AoCO2025, Compiler, x86 +--- + +## Study Notes: Induction variables and loops, Advent of Compiler Optimisations 2025 + +These notes are based on the post [**Induction variables and loops**](https://xania.org/202512/09-induction-variables) and the YouTube video [**[AoCO 9/25] More Loops: Induction Variables**](https://www.youtube.com/watch?v=vZk7Br6Vh1U&list=PL2HVqYf7If8cY4wLk7JUQ2f0JXY_xMQm2&index=10) which are Day 9 of the [Advent of Compiler Optimisations 2025](https://xania.org/AoCO2025-archive) Series by [Matt Godbolt](https://xania.org/MattGodbolt). + +My notes focus on reproducing and verifying [Matt Godbolt](https://xania.org/MattGodbolt)'s teaching within a local development environment using `GNU toolchain` on `Ubuntu`. + +Written by me and assisted by AI, proofread by me and assisted by AI. + +#### Development Environment +```bash +$ lsb_release -d +Description: Ubuntu 24.04.3 LTS + +$ gcc -v +gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04.1) + +$ llvm-objdump -v +Ubuntu LLVM version 18.1.8 +``` + +## Introduction + +An + +## Part 01: Multiplication Approach + +$$ +a_n = a_1 + (n - 1) d +$$ + +```bash +$ cat main.c +``` + +```c +void sum(int* a, int n, int d) { + int cache = a[0]; + for (int i = 0; i < n; ++i) { + a[i] = cache + i * d; + } +} +``` + +```bash +$ gcc -O2 -c main.c +$ llvm-objdump -d --disassemble-symbols=sum --x86-asm-syntax=att main.o +``` + +```x86asm +0000000000000000 : + 0: f3 0f 1e fa endbr64 + 4: 8b 07 movl (%rdi), %eax ; Load a[0] into %eax outside the loop + 6: 85 f6 testl %esi, %esi ; Check if n <= 0 + 8: 7e 1b jle 0x25 ; If n <= 0, jump to return + a: 48 63 f6 movslq %esi, %rsi ; Sign-extend n to 64-bit for address calculation + d: 48 8d 0c b7 leaq (%rdi,%rsi,4), %rcx ; Calculate end address: a + (n * 4), store in %rcx +11: 0f 1f 80 00 00 00 00 nopl (%rax) ; Instruction alignment (no-op) for performance +; --- Loop Starts --- +18: 89 07 movl %eax, (%rdi) ; Write current value to a[i] +1a: 48 83 c7 04 addq $0x4, %rdi ; Pointer Increment: Advance %rdi to the next int +1e: 01 d0 addl %edx, %eax ; [Induction Variable] cache = cache + d +20: 48 39 cf cmpq %rcx, %rdi ; Check if current pointer %rdi has reached end address %rcx +23: 75 f3 jne 0x18 ; If not equal, jump back to 0x18 +; --- Loop Ends --- + +25: c3 retq +``` + +## Part 02 : Addition Approach + +$$ +a_{n + } = a_{n} + d +$$ + +```bash +$ cat main.c +``` + +```c +void sum(int* a, int n, int d) { + int cache = a[0]; + for (int i = 0; i < n; ++i) { + a[i] = cache; + cache += d; + } +} +``` + +```bash +$ gcc -O2 -c main.c +$ llvm-objdump -d --disassemble-symbols=sum --x86-asm-syntax=att main.o +``` + +```x86asm +main.o: file format elf64-x86-64 + +Disassembly of section .text: + +0000000000000000 : + 0: f3 0f 1e fa endbr64 + 4: 8b 07 movl (%rdi), %eax ; Load a[0] into %eax outside the loop + 6: 85 f6 testl %esi, %esi ; Check if n <= 0 + 8: 7e 1b jle 0x25 ; If n <= 0, jump to return + a: 48 63 f6 movslq %esi, %rsi ; Sign-extend n to 64-bit for address calculation + d: 48 8d 0c b7 leaq (%rdi,%rsi,4), %rcx ; Calculate end address: a + (n * 4), store in %rcx +11: 0f 1f 80 00 00 00 00 nopl (%rax) ; Instruction alignment (no-op) for performance +; --- Loop Starts --- +18: 89 07 movl %eax, (%rdi) ; Write current value to a[i] +1a: 48 83 c7 04 addq $0x4, %rdi ; Pointer Increment: Advance %rdi to the next int +1e: 01 d0 addl %edx, %eax ; [Induction Variable] cache = cache + d +20: 48 39 cf cmpq %rcx, %rdi ; Check if current pointer %rdi has reached end address %rcx +23: 75 f3 jne 0x18 ; If not equal, jump back to 0x18 +; --- Loop Ends --- +25: c3 retq +``` + +## Conclusion +We can see the disasm result is same. That is Induction Variables. \ No newline at end of file From a6e7afc85098c5fd63c007186012c2d9480c6b88 Mon Sep 17 00:00:00 2001 From: gapry Date: Tue, 31 Mar 2026 22:45:06 +0800 Subject: [PATCH 2/5] Posts: Add AoCO 2025 Day 09 Study Notes: update --- ...f-Compiler-Optimisations-Study-Notes-09.md | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md index 626b2fe..185a806 100644 --- a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md +++ b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md @@ -24,9 +24,23 @@ Ubuntu LLVM version 18.1.8 ## Introduction -An +This note introduces a loop optimization known as `Induction Variable Elimination` **[1]**. -## Part 01: Multiplication Approach +The core concept is analogous to an `arithmetic sequence` **[2]**. + +The closed-form expression $a_n = a_1 + (n - 1) d$ is mathematically equivalent to the + +recursive definition $a_{n + 1} = a_{n} + d$ + +To verify whether a compiler performs induction variable optimization, we compare two implementations: + +1. A loop using the formula $a_n = a_1 + (n - 1) d$ +2. A loop using the formula $a_{n + 1} = a_{n} + d$ + +If the compiler successfully applies this optimization, +the resulting disassembly for both implementations should be identical. + +## Part 01: Closed-Form Expression $$ a_n = a_1 + (n - 1) d @@ -70,10 +84,10 @@ $ llvm-objdump -d --disassemble-symbols=sum --x86-asm-syntax=att main.o 25: c3 retq ``` -## Part 02 : Addition Approach +## Part 02 : Recursive Definition $$ -a_{n + } = a_{n} + d +a_{n + 1} = a_{n} + d $$ ```bash @@ -119,4 +133,11 @@ Disassembly of section .text: ``` ## Conclusion -We can see the disasm result is same. That is Induction Variables. \ No newline at end of file +The disassembly results for both implementations are identical. +This confirms that the compiler successfully performs Induction Variable Elimination. +It recognizes the linear relationship within the loop and optimizes +the multiplication into an incremental addition. + +## References +1. https://en.wikipedia.org/wiki/Induction_variable +2. https://en.wikipedia.org/wiki/Arithmetic_progression \ No newline at end of file From 39ffda39204ee1b5e31c4f994a9a821dabc28bc3 Mon Sep 17 00:00:00 2001 From: gapry Date: Tue, 31 Mar 2026 22:48:57 +0800 Subject: [PATCH 3/5] Posts: Add AoCO 2025 Day 09 Study Notes: update --- ...026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md index 185a806..cc212ec 100644 --- a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md +++ b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md @@ -6,7 +6,7 @@ tags: AoCO2025, Compiler, x86 These notes are based on the post [**Induction variables and loops**](https://xania.org/202512/09-induction-variables) and the YouTube video [**[AoCO 9/25] More Loops: Induction Variables**](https://www.youtube.com/watch?v=vZk7Br6Vh1U&list=PL2HVqYf7If8cY4wLk7JUQ2f0JXY_xMQm2&index=10) which are Day 9 of the [Advent of Compiler Optimisations 2025](https://xania.org/AoCO2025-archive) Series by [Matt Godbolt](https://xania.org/MattGodbolt). -My notes focus on reproducing and verifying [Matt Godbolt](https://xania.org/MattGodbolt)'s teaching within a local development environment using `GNU toolchain` on `Ubuntu`. +My notes focus on reproducing and verifying [Matt Godbolt](https://xania.org/MattGodbolt)'s teaching within a local development environment using `GNU toolchain` and `LLVM toolchain` on `Ubuntu`. Written by me and assisted by AI, proofread by me and assisted by AI. From 4fcbb6050a0c7e390c19213adc8e5469b6f93384 Mon Sep 17 00:00:00 2001 From: gapry Date: Tue, 31 Mar 2026 22:50:54 +0800 Subject: [PATCH 4/5] Posts: Add AoCO 2025 Day 09 Study Notes: update --- ...26-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md index cc212ec..49d4050 100644 --- a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md +++ b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md @@ -29,8 +29,7 @@ This note introduces a loop optimization known as `Induction Variable Eliminatio The core concept is analogous to an `arithmetic sequence` **[2]**. The closed-form expression $a_n = a_1 + (n - 1) d$ is mathematically equivalent to the - -recursive definition $a_{n + 1} = a_{n} + d$ +recursive definition $a_{n + 1} = a_{n} + d$. To verify whether a compiler performs induction variable optimization, we compare two implementations: From 56673a50bda891a447fdb842562c8bceda2fd084 Mon Sep 17 00:00:00 2001 From: gapry Date: Tue, 31 Mar 2026 22:51:46 +0800 Subject: [PATCH 5/5] Posts: Add AoCO 2025 Day 09 Study Notes: update --- ...03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md index 49d4050..208094f 100644 --- a/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md +++ b/public/posts/2026/2026-03-31-Advent-of-Compiler-Optimisations-Study-Notes-09.md @@ -109,10 +109,6 @@ $ llvm-objdump -d --disassemble-symbols=sum --x86-asm-syntax=att main.o ``` ```x86asm -main.o: file format elf64-x86-64 - -Disassembly of section .text: - 0000000000000000 : 0: f3 0f 1e fa endbr64 4: 8b 07 movl (%rdi), %eax ; Load a[0] into %eax outside the loop @@ -139,4 +135,4 @@ the multiplication into an incremental addition. ## References 1. https://en.wikipedia.org/wiki/Induction_variable -2. https://en.wikipedia.org/wiki/Arithmetic_progression \ No newline at end of file +2. https://en.wikipedia.org/wiki/Arithmetic_progression