diff options
author | Akira Hatanaka <ahatanaka@apple.com> | 2019-08-13 01:23:06 +0000 |
---|---|---|
committer | Akira Hatanaka <ahatanaka@apple.com> | 2019-08-13 01:23:06 +0000 |
commit | 3c7c053145fa98a017d426b475a9cf9c001c9da1 (patch) | |
tree | 95f92886df75a73e72dcabef1f492564849f93e8 /llvm/lib/IR/AutoUpgrade.cpp | |
parent | 438315bf69c7dae68edd5c99f9672fffdc442a79 (diff) | |
download | bcm5719-llvm-3c7c053145fa98a017d426b475a9cf9c001c9da1.tar.gz bcm5719-llvm-3c7c053145fa98a017d426b475a9cf9c001c9da1.zip |
Do not call replaceAllUsesWith to upgrade calls to ARC runtime functions
to intrinsic calls
This fixes a bug in r368311.
It turns out that the ARC runtime functions in the IR can have pointer
parameter types that are not i8* or i8**. Instead of RAUWing normal
functions with intrinsics, manually bitcast the arguments before passing
them to the intrinsic functions and bitcast the return value back to the
type of the original call instruction.
This recommits r368634, which was reverted in r368637. The loop in the
patch was iterating over uses of a function and deleting function calls
inside it, which caused bots to crash.
rdar://problem/54125406
Differential Revision: https://reviews.llvm.org/D66047
llvm-svn: 368646
Diffstat (limited to 'llvm/lib/IR/AutoUpgrade.cpp')
-rw-r--r-- | llvm/lib/IR/AutoUpgrade.cpp | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp index 6d1305393e1..e8443aeb715 100644 --- a/llvm/lib/IR/AutoUpgrade.cpp +++ b/llvm/lib/IR/AutoUpgrade.cpp @@ -3855,6 +3855,8 @@ bool llvm::UpgradeRetainReleaseMarker(Module &M) { } void llvm::UpgradeARCRuntimeCalls(Module &M) { + // This lambda converts normal function calls to ARC runtime functions to + // intrinsic calls. auto UpgradeToIntrinsic = [&](const char *OldFunc, llvm::Intrinsic::ID IntrinsicFunc) { Function *Fn = M.getFunction(OldFunc); @@ -3863,11 +3865,44 @@ void llvm::UpgradeARCRuntimeCalls(Module &M) { return; Function *NewFn = llvm::Intrinsic::getDeclaration(&M, IntrinsicFunc); - Fn->replaceAllUsesWith(NewFn); - Fn->eraseFromParent(); + + for (auto I = Fn->user_begin(), E = Fn->user_end(); I != E;) { + CallInst *CI = dyn_cast<CallInst>(*I++); + if (!CI || CI->getCalledFunction() != Fn) + continue; + + IRBuilder<> Builder(CI->getParent(), CI->getIterator()); + FunctionType *NewFuncTy = NewFn->getFunctionType(); + SmallVector<Value *, 2> Args; + + for (unsigned I = 0, E = CI->getNumArgOperands(); I != E; ++I) { + Value *Arg = CI->getArgOperand(I); + // Bitcast argument to the parameter type of the new function if it's + // not a variadic argument. + if (I < NewFuncTy->getNumParams()) + Arg = Builder.CreateBitCast(Arg, NewFuncTy->getParamType(I)); + Args.push_back(Arg); + } + + // Create a call instruction that calls the new function. + CallInst *NewCall = Builder.CreateCall(NewFuncTy, NewFn, Args); + NewCall->setTailCallKind(cast<CallInst>(CI)->getTailCallKind()); + NewCall->setName(CI->getName()); + + // Bitcast the return value back to the type of the old call. + Value *NewRetVal = Builder.CreateBitCast(NewCall, CI->getType()); + + if (!CI->use_empty()) + CI->replaceAllUsesWith(NewRetVal); + CI->eraseFromParent(); + } + + if (Fn->use_empty()) + Fn->eraseFromParent(); }; - // Unconditionally convert "clang.arc.use" to "llvm.objc.clang.arc.use". + // Unconditionally convert a call to "clang.arc.use" to a call to + // "llvm.objc.clang.arc.use". UpgradeToIntrinsic("clang.arc.use", llvm::Intrinsic::objc_clang_arc_use); // Return if the bitcode doesn't have the arm64 retainAutoreleasedReturnValue |