[PATCH] D59078: Fix missing memcpy, memmove and memset tail calls

Sanne Wouda via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 30 05:18:29 PDT 2019


sanwou01 updated this revision to Diff 227067.
sanwou01 retitled this revision from "memcpy is not tailcalled" to "Fix missing memcpy, memmove and memset tail calls".
sanwou01 edited the summary of this revision.
sanwou01 added a comment.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Fix crash and refactor for readability


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D59078/new/

https://reviews.llvm.org/D59078

Files:
  llvm/lib/CodeGen/Analysis.cpp
  llvm/test/CodeGen/AArch64/tailcall-bitcast-memcpy.ll


Index: llvm/test/CodeGen/AArch64/tailcall-bitcast-memcpy.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AArch64/tailcall-bitcast-memcpy.ll
@@ -0,0 +1,18 @@
+;RUN: llc %s -o - -verify-machineinstrs | FileCheck %s
+target triple = "aarch64-arm-none-eabi"
+
+;CHECK-LABEL: @wmemcpy
+;CHECK: lsl
+;CHECK-NOT: bl
+;CHECK-NOT: mov
+;CHECK-NOT: ldp
+;CHECK-NEXT: b memcpy
+define dso_local i32* @wmemcpy(i32* returned, i32* nocapture readonly, i64) local_unnamed_addr {
+  %4 = bitcast i32* %0 to i8*
+  %5 = bitcast i32* %1 to i8*
+  %6 = shl i64 %2, 2
+  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %4, i8* align 4 %5, i64 %6, i1 false)
+  ret i32* %0
+}
+
+declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i1)
Index: llvm/lib/CodeGen/Analysis.cpp
===================================================================
--- llvm/lib/CodeGen/Analysis.cpp
+++ llvm/lib/CodeGen/Analysis.cpp
@@ -611,6 +611,22 @@
   return CallerAttrs == CalleeAttrs;
 }
 
+/// Check whether B is a bitcast of a pointer type to another pointer type,
+/// which is equal to A.
+static bool isPointerBitcastEqualTo(const Value *A, const Value *B) {
+  assert(A); assert(B);
+
+  auto *BitCastIn = dyn_cast<BitCastInst>(B);
+
+  if (!BitCastIn)
+    return false;
+
+  if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
+    return false;
+
+  return A == BitCastIn->getOperand(0);
+}
+
 bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
                                            const Instruction *I,
                                            const ReturnInst *Ret,
@@ -643,7 +659,8 @@
           TLI.getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove")) ||
          (IID == Intrinsic::memset &&
           TLI.getLibcallName(RTLIB::MEMSET) == StringRef("memset"))) &&
-        RetVal == Call->getArgOperand(0))
+        (RetVal == Call->getArgOperand(0) ||
+         isPointerBitcastEqualTo(RetVal, Call->getArgOperand(0))))
       return true;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59078.227067.patch
Type: text/x-patch
Size: 2070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191030/f20e538b/attachment.bin>


More information about the llvm-commits mailing list