Skip to content
–views–

LLVM Pipeline Tracing: How Clang Compiles C, Stage by Stage (Part 2)

Following the same trace.c through Clang's AST, LLVM IR, optimizer and machine code and comparing it with GCC.

17 min read

LLVM Pipeline Tracing: How Clang Compiles C, Stage by Stage (Part 2)
On this page

Part 2: Tracing LLVM

In Part 1 we followed one C program through GCC and watched it end up as mov w0, 19. Now, we take the same trace.c through LLVM by using Homebrew's clang 23.1.6 on an arm64 Mac. The source is the same so every difference we see comes from the compiler.

Screenshot 2026 11 25 at 1.08.11 PM

a) Stage 1: Preprocessing (Clang 23.1.6)

Bash
$CLANG -E trace.c -o trace.clang.i

Clang's preprocessor does exactly the same job as GCC's. It replaces macros, removes comments and keeps line numbers by leaving blank lines. The code part of the output matches GCC's output exactly.

The only difference is the linemarkers at the top:

GCC 16

Clang 23

First marker

# 0 "trace.c"

# 1 "trace.c"

Built in macros

# 0 "<built-in>"

# 1 "<built-in>" 1, # 1 "<built-in>" 3, # 477 "<built-in>" 3

Command line macros

# 0 "<command-line>"

# 1 "<command line>" 1

Return flags

none

# 1 "<built-in>" 2, # 1 "trace.c" 2

What the Clang markers mean:

  • The number after the filename is the line in that file where the text starts.

  • The trailing flag 1 means "entering this file", 2 means "returning to a file" and 3 means "system header".

  • # 477 "<built-in>" 3 says about 477 lines of predefined macros were pasted in. That number belongs to this exact Clang build and target.

The takeaway for the post is the C standard defines what preprocessing does so both compilers produce the same code. And, the bookkeeping in the linemarkers is each compiler's own choice.

b) Stage 2: LLVM IR (Clang 23.1.6, -O0)

Clang doesn't produce GENERIC and GIMPLE. It goes from its AST straight to LLVM IR which is a standalone language you can read, write and save as a .ll file. GCC's -O0 GIMPLE keeps variables as named values. And, on the other hand Clang's -O0 IR gives every local a stack slot.

Bash
$CLANG -O0 -S -emit-llvm trace.c -o trace.ll

1. Every local lives in memory.
%3 = alloca i32 reserves a 4 byte slot on the stack. store writes to it and load reads it back. So, here sum = a + b becomes like load a, load b, add them and store the result in sum's slot. This is deliberate. At -O0 Clang favors simple and debuggable code and the optimizer removes most of this memory traffic later.

2. Temporaries are numbered, and each is assigned once.
%7, %8, %9 are temporaries. Each one is defined exactly once which is the SSA rule from Part 1. The difference is that GCC's SSA form (at pass 24) was created during optimization while Temporaries are SSA from the start. Named locals stay in memory until SROA and mem2reg remove the memory traffic.

3. Types and overflow are explicit.
i32 is a 32 bit integer. So, add nsw means "no signed wrap" like C says signed overflow is undefined so the flag tells the optimizer it can assume overflow never happens. GCC's GIMPLE has no equivalent visible in its dump.

4. The if is an explicit branch between labeled blocks.
br i1 %13, label %14, label %17 means "if the condition is true, go to block 14, otherwise go to block 17". Every block ends in a terminator like either a br or a ret. The ; preds = %2 comments list which blocks can jump into each block. That's the bookkeeping GCC shows as goto <D.4604>.

5. main still calls calculate.
At -O0 nothing is folded or inlined so the main contains call i32 @calculate(i32 noundef 5, i32 noundef 7). GCC's -O0 GIMPLE also kept the call.

6. The attributes say "don't optimize".
optnone and noinline are attributes Clang adds at -O0. They tell the optimizer to leave these functions alone. The target-cpu="apple-m1" and tune-cpu="apple-m5" values show which Apple chip features the code targets.

Side by side with GCC's GIMPLE:

GCC GIMPLE (-O0)

LLVM IR (-O0)

Locals

named (sum, result)

alloca stack slots are loaded and stored

Addition

sum = a + b

%9 = add nsw i32 %7, %8

Branch

if (...) goto <D.4604>; else goto <D.4605>;

br i1 %13, label %14, label %17

Return

D.4606 = result; return D.4606;

ret i32 %18

Calls in main

x = calculate (5, 7);

call i32 @calculate(...)

c) Stage 3: Optimized LLVM IR (Clang 23.1.6, -O2)

Bash
$CLANG -O2 -S -emit-llvm trace.c -o trace.O2.ll

Both compilers end up with 19 in main but the path looks different.

1. main is just ret i32 19.
Same result as GCC's return 19. Clang inlined calculate into main and folded the constants (5 and 7) and returned the answer.

2. The if became a select.
This is the same idea as GCC's csel but at the IR level:

llvm

LLVM IR
%5 = icmp sgt i32 %3, 10%6 = add nsw i32 %4, -5%7 = select i1 %5, i32 %6, i32 %4

select picks one of two values based on a condition with no branch. Both values are computed and then one is chosen. GCC showed the same thing as if_then_else in RTL and only later became csel. Clang makes it visible earlier in the IR.

3. The comparison got rewritten.
Look at icmp sgt i32 %3, 10. The compare is against 10 and not 20. Clang worked out that sum is compared after doubling. Since %4 = sum * 2, sum * 2 > 20 is the same as sum > 10 so the compiler moved the comparison to before the shift. GCC kept cmp w0, 20 on the doubled value. only clang moved the comparison.

4. The shift and the subtraction are visible.
shl nsw i32 %3, 1 is the * 2 (strength reduction same as GCC's ashift). add nsw i32 %4, -5 is the subtraction, written as adding a negative number which is the same style GCC used in GENERIC.

5. calculate is still there, but it's marked.
It has memory(none) meaning it doesn't read or write memory and willreturn meaning it always returns. Those attributes let the optimizer delete or move calls freely. Notice main isn't calling it anymore so Clang could drop the function if it were internal.

6. Extra facts Clang adds.

  • range(i32 -2147483648, 2147483643) records the possible values of the result. That's a fact about the code and not something GCC prints in this dump.

  • !llvm.errno.tbaa is type based alias analysis metadata. It tells later passes which memory accesses can't overlap.

Side by side with GCC's -O2 result:

GCC 16 (-O2)

Clang 23 (-O2)

main

mov w0, 19; ret

ret i32 19

The if

csel (in RTL)

select (in IR)

Comparison

result > 20

sum > 10, moved before the shift

How the call went away

inlined

inlined

The results match but the stage where each compiler makes the change differs. Clang rewrites the comparison in the middle end. GCC keeps the comparison on the doubled value and only does the if to csel conversion late in RTL.

d) Stage 4: The optimizer pass pipeline (Clang 23.1.6, -O2)

Bash
$CLANG -O2 -c -mllvm -print-after-all -S trace.c -o /dev/null 2> passes.txtgrep -n "^; \*\*\* IR Dump" passes.txt | head -20

1. Passes run in a fixed pipeline one at a time.
Each line in the grep output is one pass. Now, look at the order here MemProfRemoveInfo, Annotation2MetadataPass, ForceFunctionAttrsPass and so on. Module passes (on [module]) work on the whole file. Function passes on calculate or main work on one function at a time.

2. Some of the passes are the ones we already saw in the output.

  • SROAPass (Scalar Replacement of Aggregates): this is what removes the alloca stack slots from Stage 2. It's the LLVM version of GCC turning named variables into SSA values.

  • PromotePass (mem2reg): it's the same job like promoting memory to registers in SSA form. It's the reason %3 = alloca i32 disappears.

  • SimplifyCFGPass: cleans up the control flow like the same kind of work as GCC's "Merging block" messages.

  • EarlyCSEPass: removes duplicate computations. This matches GCC's "remove redundant work" passes.

3. Module passes can change other functions.
IPSCCPPass and GlobalOptPass are interprocedural which means they look across function boundaries. And, that's how LLVM can learn calculate always gets 5 and 7 when called from main which is the constant propagation that leads to ret i32 19. GCC does this in its own inter procedural passes.

4. Passes are inspected one by one.
Now, notice that the order matters. SROAPass runs on calculate before EarlyCSEPass like the pipeline is ordered so each pass cleans up for the next. This is why compilers run many passes instead of one big optimization.

Comparison with GCC:

GCC 16

LLVM 23

Dump style

separate numbered files (.024t.ssa, .273t.optimized)

one file, a header for each pass

Pass count

about 250 passes between SSA and optimized

20 headers shown as the full list is much longer.

Memory-to-register

the SSA form is built during optimization

SROAPass and PromotePass do it explicitly

Note for the blog: I would show only a few passes instead of the full list. The interesting ones are SROAPass and PromotePass (memory to registers), SimplifyCFGPass (control flow) and IPSCCPPass which is constant propagation across functions.

Stage 4 (continued): What SROAPass did to calculate

This is the most useful output so far because it shows the memory to register step happening in front of us. Compare the dump with the Stage 2 -O0 IR:

-O0 (Stage 2)

After SROAPass (Stage 4)

4 alloca slots, plus store and load for every value

No alloca, no store, no load

%9 = add nsw i32 %7, %8 (after loading a and b)

%3 = add nsw i32 %0, %1 (uses the arguments directly)

%11 = mul nsw i32 %10, 2

%4 = mul nsw i32 %3, 2

br i1 %13, label %14, label %17, then store to %6

br i1 %5, label %6, label %8, no memory writes

Return value loaded from %6 at block 17

%9 = phi i32 [ %7, %6 ], [ %4, %2 ] picks the value

1. The alloca slots are gone.
Every store and load to a stack slot was replaced by a direct use of the SSA value. That's what SROAPass (Scalar Replacement of Aggregates) and PromotePass do means they move each scalar local out of memory and into an SSA name.

2. The phi appeared.
%9 = phi i32 [ %7, %6 ], [ %4, %2 ] says that if we came from block 6 (the if was taken) then use %7 and if we came from block 2 then use %4. This is the same PHI from GCC's SSA dump in Part 1 (result_1 = PHI <result_5(2), result_6(3)>). Once memory is gone then the phi is how the one remaining variable gets its value from either path.

3. The add now uses the arguments.
%3 = add nsw i32 %0, %1 is a + b straight from the function parameters. at -O0 Clang copied %0 and %1 into stack slots first so that copy is gone.

4. The comparison still says 20 here.
At this stage the compare is still icmp sgt i32 %4, 20, comparing the doubled value. The rewrite to sum > 10 we saw in the final -O2 output happens in a later pass so, this dump doesn't show it yet. Well, that's a good example of why we dump after every pass.

5. EarlyCSEPass changed nothing here.
The dump after EarlyCSEPass is identical because there were no duplicate computations to remove.

Comparison with GCC: GCC's SSA dump (.024t.ssa) showed the same SSA form with sum_4, result_5 and result_1 = PHI. The difference is that Clang's SROAPass visibly removes the memory operations as a separate pass so we can see the exact step where variables leave memory.

Stage 4 (continued): Finding where sgt 20 became sgt 10

The grep shows the change happens between line 779 and line 795 so it's in the pass whose dump starts just above line 795. Those compares are against %4 and %3 and this is the rewrite we saw in the final output.

What the grep tells us:

  • Lines 26 to 479: the compare is against %12, which is still the -O0 version with the loads from memory. This is the -O0 IR and the early passes.

  • Lines 499 to 779: the compare is icmp sgt i32 %4, 20. This is after SROAPass and most of the way through the pipeline and the doubled value is still in %4.

  • Lines 795 to 887: the compare is icmp sgt i32 %3, 10. Now it's against the sum itself so the multiply by 2 was moved after the compare.

What happened in between: the constant compare was rewritten to compare the sum with 10 instead of the doubled value with 20. That is the same arithmetic as sum * 2 > 20 is sum > 10 since 2 is positive. This is an instcombine style rewrite and it happens in a pass that runs after the ones we have looked at.

Stage 4 (continued): The pass that did the rewrite is InstCombinePass

The header just above line 795 is ; *** IR Dump After InstCombinePass on calculate ***. Well, that's the pass that changed sgt 20 into sgt 10.

What InstCombine does: it's LLVM's main peephole optimizer. It looks at small groups of instructions and replaces them with simpler equivalent ones. Here it recognized that (sum * 2) > 20 is the same as sum > 10 and rewrote the compare to use the sum directly. It also cleaned up the multiply that was no longer needed for the compare.

Why this matters for the comparison with GCC: GCC does this kind of simplification in several places (its fold and match.pd rules plus later RTL combine passes). But, in LLVM it's mostly one well known pass InstCombine that runs repeatedly throughout the pipeline.

Stage 4 (continued): The before and after of InstCombine

Here's the change, side by side:

Before InstCombine

After InstCombine

%5 = icmp sgt i32 %4, 20

%5 = icmp sgt i32 %3, 10

%4 = mul nsw i32 %3, 2 (named %4, before the compare)

%4 = shl nsw i32 %3, 1 (same value just cheaper shift)

%7 = sub nuw nsw i32 %4, 5

%7 = add nsw i32 %4, -5

1. The compare moved onto the sum.
sum * 2 > 20 became sum > 10. The compare now reads %3 directly so it no longer depends on the multiply. That's why the dump shows %5 comparing %3.

2. mul became shl.
mul nsw i32 %3, 2 became shl nsw i32 %3, 1. Same value and a shift is cheaper on most CPUs. This is the same strength reduction GCC showed as ashift in RTL.

3. sub became add with a negative constant.
sub nuw nsw %4, 5 became add nsw %4, -5. This is the same canonical form GCC used in GENERIC (result + -5). LLVM also dropped nuw in this step which is a small detail worth noting which is the flags are part of the IR and passes can change them.

4. The range attribute appeared.
range(i32 -2147483648, 2147483643) on calculate records that the result has a known range. That's new information the optimizer computed andnot something in the source.

5. The compare now happens before the doubling.
In the IR the comparison doesn't wait for the shift. Both results are still computed from %3 and the phi at block 8 still picks the right value.

Why this matters: the program is identical in behavior. Only the way it's written changed which is exactly what an optimizer should do.

Stage 4 (continued): Where the select appears

Bash
sed -n '795,813p' passes.txt | grep "IR Dump"

The first select appears in the dump after SimplifyCFGPass. Running sed -n '795,813p' passes.txt | grep "IR Dump" shows the header After SimplifyCFGPass on calculate.

What changed, in the IR:

Before, the branch and phi looked like this:

llvm

LLVM IR
br i1 %5, label %6, label %86: %7 = add nsw i32 %4, -5   br label %88: %9 = phi i32 [ %7, %6 ], [ %4, %2 ]

After, the same logic is one instruction:

llvm

LLVM IR
%6 = add nsw i32 %4, -5%7 = select i1 %5, i32 %6, i32 %4

Here, both values are computed and select picks one. This is the same if conversion we saw in GCC like GCC's RTL if_then_else became csel on ARM. Clang does it earlier at the IR level and the result is the same csel once it reaches the backend.

Two details here:

  • The select has no branch so the CPU never has to guess which way the if goes. That's the reason GCC's RTL csel mattered in Part 1.

  • The -O2 final output we saw earlier has exactly this select so this is where it was created.

d) Stage 5: Assembly (Clang 23.1.6, -O2, arm64)

Bash
$CLANG -O2 -S trace.c -o trace.O2.s

Clang's output and GCC's assembly from Part 1 are both short and both end in csel and mov w0, #19. The main differences are in the instruction choices and the layout.

1. The same arithmetic, in a different order.

  • add w8, w1, w0: sum = a + b. Clang reads the arguments in the opposite order from GCC (w1, w0) which is fine because addition is commutative.

  • lsl w9, w8, #1: sum * 2 which is the shift from InstCombine.

  • sub w10, w9, #5: result - 5 is computed ahead of time just like GCC.

  • cmp w8, #10: compares the sum with 10 like the rewrite we found in the IR.

  • csel w0, w10, w9, gt: picks w10 if greater otherwise w9. So, the same idea as GCC's csel w0, w1, w0, gt but with different registers.

2. Clang's register names are different.
GCC used w0 for almost everything. Clang spreads the work across w8, w9 and w10 and only puts the final answer in w0 which is the return register. That's register allocation making different choices. Both are correct.

3. Alignment and blocks.
.p2align 6 aligns the function to 64 bytes while GCC used 32 (.p2align 5). The ; %bb.0 and ; %bb.1 labels are Clang's basic block markers.

4. main is the same.
mov w0, #19 then ret just like GCC.

Side by side with GCC's trace.s:

GCC 16 (-O2)

Clang 23 (-O2)

Add

add w0, w0, w1

add w8, w1, w0

Shift

lsl w0, w0, 1

lsl w9, w8, #1

Subtract

sub w1, w0, #5

sub w10, w9, #5

Compare

cmp w0, 20

cmp w8, #10

Select

csel w0, w1, w0, gt

csel w0, w10, w9, gt

Main

mov w0, 19; ret

mov w0, #19; ret

Both compilers reach the same six instruction function. GCC's compare still uses 20 on the doubled value while Clang's uses 10 on the sum. So, both are correct.

e) Stage 6: Assembling (Clang 23.1.6, -O2 object, arm64)

Bash
$CLANG -O2 -c trace.c -o trace.clang.O2.ofile trace.clang.O2.onm trace.clang.O2.oobjdump -d trace.clang.O2.o

This matches Part 1's Stage 6 in shape like a short -O2 object with the same six instruction calculate and a two instruction main.

1. file: Mach-O 64 bit object arm64, same as GCC's.

2. nm: _calculate at 0 and _main at 0x18. The ltmp labels are temporary assembler labels as before. now, note that main is at 0x18 and not 0x20. GCC's main was at 0x20 because of its 32 byte alignment (.p2align 5). Clang's calculate is only 24 bytes long (6 instructions × 4) so main starts right after it. Clang's main is 4 byte aligned (.p2align 2) so it sits right after calculate with no padding.

3. objdump: the machine code.

  • 0b000028 is add w8, w1, w0 like the same instruction as in the assembly encoded in 4 bytes.

  • mov w0, #0x13 is 52800260 the same bytes GCC produced for mov w0, 19 in Part 1. The number 19 is still packed into the instruction.

  • Every instruction is 4 bytes so the addresses go up by 4 each time as on any ARM instruction.

4. Side by side with GCC's object file:

GCC 16 (-O2)

Clang 23 (-O2)

calculate size

6 instructions (24 bytes)

6 instructions (24 bytes)

main offset

0x20 (padded)

0x18 (no padding)

main encoding

52800260

52800260

csel encoding

1a80c020 (csel w0, w1, w0, gt)

1a89c140 (csel w0, w10, w9, gt)

The csel encodings differ because the registers differ even though both instructions do the same job. The main encoding is identical because it's the same instruction with the same operand.

f) Stage 7: Linking (Clang 23.1.6, -O2 executable)

Bash
$CLANG trace.clang.O2.o -o trace.clang./trace.clangecho $?file trace.clangnm trace.clangotool -L trace.clang

1. The exit code is 19. Both compilers' programs return the same number so the two pipelines end in the same place.

2. file: Mach-O 64 bit executable arm64 like the same kind of result as GCC's trace.

3. nm: real addresses. _calculate is at 0x100000340 and _main at 0x100000358. The gap is 0x18 (24 bytes) which is exactly calculate's size so there's no padding between the functions. GCC's gap was 0x20 because of its 32 byte alignment. The __mh_execute_header symbol is the same Mach-O header marker as in GCC's binary.

4. otool -L: only libSystem.B.dylib, the same macOS system library as GCC's program. The version numbers differ (1351.0.0 here and 1356.0.0 in the GCC binary). I haven't checked why so i am not drawing a conclusion from it.

Side by side with the GCC binary:

GCC 16

Clang 23

Exit code

19

19

_calculate

0x100000480

0x100000340

_main

0x1000004a0

0x100000358

Gap between functions

0x20 (32 bytes)

0x18 (24 bytes)

Linked library

libSystem.B.dylib

libSystem.B.dylib

The addresses differ because the two binaries have different code sizes before calculate and the gap between functions is what differs and it comes from each function's size and alignment. so, we don't compare the absolute addresses as if they meant something. The exit code and the library are the stable facts to highlight.

The whole Clang journey in one table:

Stage

Clang command

Output

What happened

1. Preprocessing

clang -E

trace.clang.i

Same code as GCC, different linemarkers

2. LLVM IR (-O0)

-S -emit-llvm

trace.ll

Every local in an alloca slot

3. Optimized IR

-O2 -S -emit-llvm

trace.O2.ll

main is ret i32 19; the if is a select

4. Passes

-mllvm -print-after-all

passes.txt

SROAPass removes memory; InstCombine rewrites sgt 20 to sgt 10

5. Assembly

-S

trace.O2.s

Six-instruction calculate, mov w0, #19 in main

6. Object

-c

trace.clang.O2.o

Same mov encoding as GCC

7. Executable

clang

trace.clang

Exit code 19, links libSystem

Putting the two pipelines side by side#

Both compilers start from the same C text and end at the same answer which is 19. The stages they use differ though. GCC makes its biggest simplifications in GENERIC, GIMPLE and RTL with the if-to-csel conversion happening late in the backend. Clang does most of its work in one long pipeline of IR passes where SROA removes the memory traffic, InstCombine rewrites the comparison and SimplifyCFG turns the branch into a select. By the time the backend runs there's a little left to decide.

Neither design is simply better. LLVM IR is readable and standalone which is why tools like Alive2 and MLIR can work on it directly. GCC's GIMPLE/RTL split is what lets one optimizer serve many languages and targets. Both choices show up the moment you dump the same program.

Btw a note on scope: everything here is LLVM 23.1.6 at -O2 on an arm64 Mac. Other versions, targets and optimization levels can make different choices and the pass names and output can change.

What's next#

Part 3 turns the optimizations off one at a time (-fno-inline, -fno-if-conversion, -fno-optimize-sibling-calls) and watches the output change in both compilers. If you have been wondering what -O2 really means so that's the next post. Stay tuned.

Enjoyed this? Get the next post by email.

Get new posts on compilers and systems in your inbox. No spam, unsubscribe anytime.

Comments

Be kind. Comments that break the rules may be removed.