This patch fixes a bug in emitARCOperationAfterCall where it inserts the fall-back call after a bitcast instruction and then replaces the bitcast's operand with the result of the fall-back call. The generated IR without this patch looks like this:
msgSend.call: ; preds = %entry %call = call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8* (i8*, i8*, i8*)*)(i8* %6, i8* %5, i8* %4) #4, !clang.arc.no_objc_arc_exceptions !11 br label %msgSend.cont msgSend.null-receiver: ; preds = %entry call void @llvm.objc.release(i8* %4) #1, !clang.imprecise_release !11 br label %msgSend.cont msgSend.cont: ; preds = %msgSend.null-receiver, %msgSend.call %8 = phi i8* [ %call, %msgSend.call ], [ null, %msgSend.null-receiver ] %9 = bitcast i8* %10 to %0* %10 = call i8* @llvm.objc.retain(i8* %8) #1
Notice that %9 = bitcast i8* %10 to %0* is taking operand %10 which is defined after it.
To fix the bug, this patch modifies the insert point to point to the bitcast instruction so that the fall-back call is inserted before the bitcast. In addition, it teaches the function to look at phi instructions that are generated when there is a check for a null receiver and insert the retainRV/claimRV instruction right after the call instead of inserting a fall-back call right after the phi instruction.
rdar://73360225