[llvm] 35c63d6 - [GlobalISel][CallLowering] Look through bitcasts from constant function pointers.

Amara Emerson via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 7 15:33:10 PST 2020


Author: Amara Emerson
Date: 2020-02-07T15:32:54-08:00
New Revision: 35c63d66aaae65a7004d94e0a6668ff19612ba5e

URL: https://github.com/llvm/llvm-project/commit/35c63d66aaae65a7004d94e0a6668ff19612ba5e
DIFF: https://github.com/llvm/llvm-project/commit/35c63d66aaae65a7004d94e0a6668ff19612ba5e.diff

LOG: [GlobalISel][CallLowering] Look through bitcasts from constant function pointers.

Calls to ObjC's objc_msgSend function are done by bitcasting the function global
to the required function type signature. This patch looks through this bitcast
so that we can do a direct call with bl on arm64 instead of using an indirect blr.

Differential Revision: https://reviews.llvm.org/D74241

Added: 
    llvm/test/CodeGen/AArch64/GlobalISel/call-lowering-const-bitcast-func.ll

Modified: 
    llvm/lib/CodeGen/GlobalISel/CallLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/GlobalISel/CallLowering.cpp b/llvm/lib/CodeGen/GlobalISel/CallLowering.cpp
index 294e08978b37..752f26b76382 100644
--- a/llvm/lib/CodeGen/GlobalISel/CallLowering.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CallLowering.cpp
@@ -52,8 +52,18 @@ bool CallLowering::lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
 
   if (const Function *F = CS.getCalledFunction())
     Info.Callee = MachineOperand::CreateGA(F, 0);
-  else
-    Info.Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
+  else {
+    // Try looking through a bitcast from one function type to another.
+    // Commonly happens with calls to objc_msgSend().
+    const Value *CalleeV = CS.getCalledValue();
+    if (auto *BC = dyn_cast<ConstantExpr>(CalleeV)) {
+      if (const auto *F = dyn_cast<Function>(BC->getOperand(0))) {
+        Info.Callee = MachineOperand::CreateGA(F, 0);
+      }
+    } else {
+      Info.Callee = MachineOperand::CreateReg(GetCalleeReg(), false);
+    }
+  }
 
   Info.OrigRet = ArgInfo{ResRegs, CS.getType(), ISD::ArgFlagsTy{}};
   if (!Info.OrigRet.Ty->isVoidTy())

diff  --git a/llvm/test/CodeGen/AArch64/GlobalISel/call-lowering-const-bitcast-func.ll b/llvm/test/CodeGen/AArch64/GlobalISel/call-lowering-const-bitcast-func.ll
new file mode 100644
index 000000000000..07a6297ac76e
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/call-lowering-const-bitcast-func.ll
@@ -0,0 +1,14 @@
+; RUN: llc -O0 -global-isel -verify-machineinstrs %s -o - 2>&1 | FileCheck %s
+
+target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64-darwin-ios13.0"
+
+declare i8* @objc_msgSend(i8*, i8*, ...)
+define void @call_bitcast_ptr_const() {
+; CHECK-LABEL: @call_bitcast_ptr_const
+; CHECK: bl _objc_msgSend
+; CHECK-NOT: blr
+entry:
+  call void bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to void (i8*, i8*, [2 x i32], i32, float)*)(i8* undef, i8* undef, [2 x i32] zeroinitializer, i32 0, float 1.000000e+00)
+  ret void
+}


        


More information about the llvm-commits mailing list