If the frame pointer is an argument of the original pointer (which happens with opaque pointers), then we currently first replace the argument with undef, which will prevent later replacement of the old frame pointer with the new one.
Fix this by replacing arguments with some dummy instructions first, and then replacing those with undef later. This gives us a chance to replace the frame pointer before it becomes undef.
This is a big ugly, but I couldn't think of a better solution.
Please explain why we need to do this in the comment, so that future maintainers aren't forced to track the commit all the way back to this conversation to understand it. And then the comment on the replacement later can just be something like "all uses of the arguments should have been resolved by this point, so we can safely remove the dummy values".