diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp --- a/clang/lib/CodeGen/CGObjC.cpp +++ b/clang/lib/CodeGen/CGObjC.cpp @@ -2898,41 +2898,50 @@ llvm::Value *value, ValueTransform doAfterCall, ValueTransform doFallback) { - if (llvm::CallInst *call = dyn_cast(value)) { - CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); + CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); + if (llvm::CallInst *call = dyn_cast(value)) { // Place the retain immediately following the call. CGF.Builder.SetInsertPoint(call->getParent(), ++llvm::BasicBlock::iterator(call)); value = doAfterCall(CGF, value); - - CGF.Builder.restoreIP(ip); - return value; } else if (llvm::InvokeInst *invoke = dyn_cast(value)) { - CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); - // Place the retain at the beginning of the normal destination block. llvm::BasicBlock *BB = invoke->getNormalDest(); CGF.Builder.SetInsertPoint(BB, BB->begin()); value = doAfterCall(CGF, value); - CGF.Builder.restoreIP(ip); - return value; - // Bitcasts can arise because of related-result returns. Rewrite // the operand. } else if (llvm::BitCastInst *bitcast = dyn_cast(value)) { + // Change the insert point to avoid emitting the fall-back call after the + // bitcast. + CGF.Builder.SetInsertPoint(bitcast->getParent(), bitcast->getIterator()); llvm::Value *operand = bitcast->getOperand(0); operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback); bitcast->setOperand(0, operand); - return bitcast; - - // Generic fall-back case. + value = bitcast; } else { - // Retain using the non-block variant: we never need to do a copy - // of a block that's been returned to us. - return doFallback(CGF, value); + auto *phi = dyn_cast(value); + if (phi && phi->getNumIncomingValues() == 2 && + isa(phi->getIncomingValue(1)) && + isa(phi->getIncomingValue(0))) { + // Handle phi instructions that are generated when it's necessary to check + // whether the receiver of a message is null. + llvm::Value *inVal = phi->getIncomingValue(0); + inVal = emitARCOperationAfterCall(CGF, inVal, doAfterCall, doFallback); + phi->setIncomingValue(0, inVal); + value = phi; + } else { + // Generic fall-back case. + // Retain using the non-block variant: we never need to do a copy + // of a block that's been returned to us. + value = doFallback(CGF, value); + } } + + CGF.Builder.restoreIP(ip); + return value; } /// Given that the given expression is some sort of call (which does diff --git a/clang/test/CodeGenObjC/ns_consume_null_check.m b/clang/test/CodeGenObjC/ns_consume_null_check.m --- a/clang/test/CodeGenObjC/ns_consume_null_check.m +++ b/clang/test/CodeGenObjC/ns_consume_null_check.m @@ -7,6 +7,7 @@ @interface MyObject : NSObject - (char)isEqual:(id) __attribute__((ns_consumed)) object; - (_Complex float) asComplexWithArg: (id) __attribute__((ns_consumed)) object; ++(instancetype)m0:(id) __attribute__((ns_consumed)) object; @end MyObject *x; @@ -82,4 +83,15 @@ // CHECK: landingpad // CHECK: call void @llvm.objc.destroyWeak(i8** [[WEAKOBJ]]) [[NUW]] +void test2(id a) { + id obj = [MyObject m0:a]; +} + +// CHECK: define{{.*}} void @test2(i8* %[[A:.*]]) #0 { +// CHECK: %[[CALL:.*]] = call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend +// CHECK-NEXT: %[[V6:.*]] = {{.*}}call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[CALL]]) + +// CHECK: phi i8* [ %[[V6]], %{{.*}} ], [ null, %{{.*}} ] + + // CHECK: attributes [[NUW]] = { nounwind }