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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help