I know your question concerns whether "irreplaceable code" could exist, but still think a patent on the overall function performed is the only way to keep it from being replaced, all else being equal.
Even if the replacement was identical code, it could be a legitimate "new work". I did a controller once that had to keep a bunch (128) of outputs updated based on various flags, counters, etc, in a 1 ms interrupt routine. Straightforward code did not begin to fit into the number of cycles available. After a day or two of sheer panic, I wrote it all out as boolean logic and used logic minimization on that (a plan B involving FPGA's was in the back of my mind). The trusty Karnaugh map cranked out a much much smaller yet equivalent bit of ( completely indecipherable) logic that just barely fit into the cycles a 8051 can provide. (The 8051 is an 8-bit cpu, then maybe 0.7 MIPS) I had some fun with the comments.
If a "black box" spec was written for that system, I would expect the exact same core sequence of code would result, just because only the truly minimum solution would fit. It would be clean code, independantly copyrightable, but a stupid patent on that particular trick would force a quicker CPU to be used.
You have an interesting question, and I look forward to reading what the saner adult denizens will have to say about it :)