This whole thing seems fragile, but I've never looked at this code before this.
We gather a set of white-listed instructions in isAllocSiteRemovable() and then replace/erase them. But we don't know in general if the instructions in the set have uses amongst themselves, so order of deletion makes a difference.
There's already a special-case for the llvm.objectsize intrinsic, so add another for llvm.invariant.end.
Should fix:
https://bugs.llvm.org/show_bug.cgi?id=43723
For reference - these are the potential intrinsics that we can encounter in the code below. The 'default' case says we can't see anything outside of this set.