[PATCH] D75824: [InstSimplify] Don't simplify musttail calls

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 8 06:23:10 PDT 2020


nikic created this revision.
nikic added reviewers: jdoerfert, spatel, lebedev.ri.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
nikic added a child revision: D75815: [InstSimplify] Simplify calls with "returned" attribute.

As pointed out by @jdoerfert on D75815 <https://reviews.llvm.org/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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75824

Files:
  lib/Analysis/InstructionSimplify.cpp
  test/Transforms/InstSimplify/call.ll


Index: test/Transforms/InstSimplify/call.ll
===================================================================
--- test/Transforms/InstSimplify/call.ll
+++ test/Transforms/InstSimplify/call.ll
@@ -1007,3 +1007,30 @@
   %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
+}
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -5330,6 +5330,11 @@
 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 @@
     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:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75824.248994.patch
Type: text/x-patch
Size: 1983 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200308/1ac1d2d2/attachment.bin>


More information about the llvm-commits mailing list