When LICM fails us
xania.org·1d·
Discuss: Hacker News
Const Evaluation
Preview
Report Post

Written by me, proof-read by an LLM. Details at end.

Yesterday’s LICM post ended with the compiler pulling invariants like size() and get_range out of our loop - clean assembly, great performance. Job done, right?

Not quite. Let’s see how that optimisation can disappear.

Let’s say you had a const char *1, and wanted to write a function to return if there was an exclamation mark or not:

Here we’re relying on loop-invariant code motion (LICM) to move the strlen out of the loop, called once before we loop, and then the loop is:

.L4:
add rdi, 1                ; ++index
cmp BYTE PTR [rdi-1], 33  ; is string[index-1] a '!' ?
je .L5                    ; if so, jump to "return true"
.L2:
cmp rdi, rax              ; els...

Similar Posts

Loading similar posts...