r/Compilers • • Aug 09 '26

When do modern compilers still emit suboptimal code?

Do compilers for targets like x86/arm already emit essentially optimal code or are there still cases when hand optimized assembly beats the compiler (Perhaps even with PGO)? Aside from vectorization since this is known to be hard. Are the classic codegen stages like regalloc and Isel effectively optimal nowadays for most applications?

38 Upvotes

39 comments sorted by

View all comments

Show parent comment

9

u/Fosdran Aug 10 '26

I would argue that code optimization isn't even np-hard. It's non computable.

2

u/techy5000 Aug 10 '26

Which is NP-hard

2

u/wittierframe839 Aug 11 '26

Not the same thing

1

u/DankPhotoShopMemes Aug 12 '26

NP is the set of problems that can be solved in polynomial time by a nondeterministic turing machine. NP-hard is the set of problems that any NP problem can reduce to in polynomial time.

Though this means that NP-hard may have non-computable (more accurately, undecidable) problems, it doesn’t necessarily contain ALL undecidable problems. We also know explicit counterexamples, e.g. an NP problem that doesn’t reduce to a specific undecidable problem. So undecidable doesn’t imply NP-hard.

0

u/MithrilHuman Aug 11 '26

True, it’s like the halting problem. You can’t write a master program to say if a subject optimized program works.