diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2020-03-08 13:11:46 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-03-09 18:46:56 +0100 |
commit | 829d377a98fd3ee087935ea215677e49c8b51b27 (patch) | |
tree | bcdc0755696274441a732d86638afa62f41aaf40 | |
parent | 843a9778fcd5ef93804fc22de04af9ab8c8b20a9 (diff) | |
download | llvm-829d377a98fd3ee087935ea215677e49c8b51b27.zip llvm-829d377a98fd3ee087935ea215677e49c8b51b27.tar.gz llvm-829d377a98fd3ee087935ea215677e49c8b51b27.tar.bz2 |
[InstSimplify] Don't simplify musttail calls
As pointed out by jdoerfert on D75815, we must be careful when
simplifying musttail calls: We can only replace the return value
if we can eliminate the call entirely. As we can't make this
guarantee for all consumers of InstSimplify, this patch disables
simplification of musttail calls. Without this patch, musttail
simplification currently results in module verification errors.
Differential Revision: https://reviews.llvm.org/D75824
-rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 8 | ||||
-rw-r--r-- | llvm/test/Transforms/InstSimplify/call.ll | 27 |
2 files changed, 35 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 5ad09eb..3b80e535 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5330,6 +5330,11 @@ static Value *simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q) { Value *llvm::SimplifyCall(CallBase *Call, const SimplifyQuery &Q) { Value *Callee = Call->getCalledValue(); + // musttail calls can only be simplified if they are also DCEd. + // As we can't guarantee this here, don't simplify them. + if (Call->isMustTailCall()) + return nullptr; + // call undef -> undef // call null -> undef if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee)) @@ -5512,6 +5517,9 @@ Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ, break; case Instruction::Call: { Result = SimplifyCall(cast<CallInst>(I), Q); + // Don't perform known bits simplification below for musttail calls. + if (cast<CallInst>(I)->isMustTailCall()) + return Result; break; } case Instruction::Freeze: diff --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll index c5706ec..108de90 100644 --- a/llvm/test/Transforms/InstSimplify/call.ll +++ b/llvm/test/Transforms/InstSimplify/call.ll @@ -1007,3 +1007,30 @@ define i32 @returned_var_arg(i32 %arg) { %x = call i32 @passthru_i32(i32 %arg) ret i32 %x } + +define i32 @returned_const_int_arg_musttail(i32 %arg) { +; CHECK-LABEL: @returned_const_int_arg_musttail( +; CHECK-NEXT: [[X:%.*]] = musttail call i32 @passthru_i32(i32 42) +; CHECK-NEXT: ret i32 [[X]] +; + %x = musttail call i32 @passthru_i32(i32 42) + ret i32 %x +} + +define i32 @returned_var_arg_musttail(i32 %arg) { +; CHECK-LABEL: @returned_var_arg_musttail( +; CHECK-NEXT: [[X:%.*]] = musttail call i32 @passthru_i32(i32 [[ARG:%.*]]) +; CHECK-NEXT: ret i32 [[X]] +; + %x = musttail call i32 @passthru_i32(i32 %arg) + ret i32 %x +} + +define i32 @call_undef_musttail() { +; CHECK-LABEL: @call_undef_musttail( +; CHECK-NEXT: [[X:%.*]] = musttail call i32 undef() +; CHECK-NEXT: ret i32 [[X]] +; + %x = musttail call i32 undef() + ret i32 %x +} |