From 6c189ecbe6d11583dc54289a2e8d8f35add01c82 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 13 Apr 2012 01:08:28 +0000 Subject: [PATCH] Use the new Use-aware dominates method to apply the objc runtime library return value optimization for phi uses. Even when the phi itself is not dominated, the specific use may be dominated. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154647 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/ObjCARC.cpp | 13 +++++-------- test/Transforms/ObjCARC/contract.ll | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/Transforms/Scalar/ObjCARC.cpp b/lib/Transforms/Scalar/ObjCARC.cpp index 62c4694616f..40b0b206168 100644 --- a/lib/Transforms/Scalar/ObjCARC.cpp +++ b/lib/Transforms/Scalar/ObjCARC.cpp @@ -4109,16 +4109,12 @@ bool ObjCARCContract::runOnFunction(Function &F) { Use &U = UI.getUse(); unsigned OperandNo = UI.getOperandNo(); ++UI; // Increment UI now, because we may unlink its element. - Instruction *UserInst = dyn_cast(U.getUser()); - if (!UserInst) - continue; - // FIXME: dominates should return true for unreachable UserInst. - if (DT->isReachableFromEntry(UserInst->getParent()) && - DT->dominates(Inst, UserInst)) { + if (DT->isReachableFromEntry(U) && + DT->dominates(Inst, U)) { Changed = true; Instruction *Replacement = Inst; Type *UseTy = U.get()->getType(); - if (PHINode *PHI = dyn_cast(UserInst)) { + if (PHINode *PHI = dyn_cast(U.getUser())) { // For PHI nodes, insert the bitcast in the predecessor block. unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo); @@ -4139,7 +4135,8 @@ bool ObjCARCContract::runOnFunction(Function &F) { } } else { if (Replacement->getType() != UseTy) - Replacement = new BitCastInst(Replacement, UseTy, "", UserInst); + Replacement = new BitCastInst(Replacement, UseTy, "", + cast(U.getUser())); U.set(Replacement); } } diff --git a/test/Transforms/ObjCARC/contract.ll b/test/Transforms/ObjCARC/contract.ll index 04ae3ca505f..c48f8a534fa 100644 --- a/test/Transforms/ObjCARC/contract.ll +++ b/test/Transforms/ObjCARC/contract.ll @@ -143,3 +143,21 @@ define i8* @test7(i8* %p) { %2 = tail call i8* @objc_autoreleaseReturnValue(i8* %p) ret i8* %p } + +; Do the return value substitution for PHI nodes too. + +; CHECK: define i8* @test8( +; CHECK: %retval = phi i8* [ %p, %if.then ], [ null, %entry ] +; CHECK: } +define i8* @test8(i1 %x, i8* %c) { +entry: + br i1 %x, label %return, label %if.then + +if.then: ; preds = %entry + %p = call i8* @objc_retain(i8* %c) nounwind + br label %return + +return: ; preds = %if.then, %entry + %retval = phi i8* [ %c, %if.then ], [ null, %entry ] + ret i8* %retval +} -- 2.34.1