[PATCH] D52238: [CodeGen] Enable tail calls for functions with NonNull attributes.

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 18 08:51:36 PDT 2018


dmgreen created this revision.
dmgreen added reviewers: fhahn, efriedma, arsenm.
Herald added a subscriber: wdng.
Herald added a reviewer: javed.absar.

Adding NonNull as attributes to returned pointers has the unfortunate side effect of disabling tail calls. This
ignores the NonNull attribute, in the same way that we ignore the NoAlias attribute as it has no affect on the
calling convention.


https://reviews.llvm.org/D52238

Files:
  lib/CodeGen/Analysis.cpp
  lib/CodeGen/CodeGenPrepare.cpp
  lib/CodeGen/SelectionDAG/TargetLowering.cpp
  test/CodeGen/ARM/tail-call.ll


Index: test/CodeGen/ARM/tail-call.ll
===================================================================
--- test/CodeGen/ARM/tail-call.ll
+++ test/CodeGen/ARM/tail-call.ll
@@ -98,3 +98,15 @@
   %call = tail call i32 (i32, ...) @variadic(i32 %y, i64 %z, i64 %z)
   ret void
 }
+
+; Check that non-null attributes don't inhibit inlining.
+
+declare nonnull i8* @nonnull_callee(i8* %p, i32 %val)
+define i8* @nonnull_caller(i8* %p, i32 %val) {
+; CHECK-LABEL: nonnull_caller:
+; CHECK-TAIL: b nonnull_callee
+; CHECK-NO-TAIL: bl nonnull_callee
+entry:
+  %call = tail call i8* @nonnull_callee(i8* %p, i32 %val)
+  ret i8* %call
+}
Index: lib/CodeGen/SelectionDAG/TargetLowering.cpp
===================================================================
--- lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -55,10 +55,12 @@
   const Function &F = DAG.getMachineFunction().getFunction();
 
   // Conservatively require the attributes of the call to match those of
-  // the return. Ignore noalias because it doesn't affect the call sequence.
+  // the return. Ignore NoAlias and NonNull because they don't affect the
+  // call sequence.
   AttributeList CallerAttrs = F.getAttributes();
   if (AttrBuilder(CallerAttrs, AttributeList::ReturnIndex)
           .removeAttribute(Attribute::NoAlias)
+          .removeAttribute(Attribute::NonNull)
           .hasAttributes())
     return false;
 
Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -1866,15 +1866,6 @@
     CallInst *CI = TailCalls[i];
     CallSite CS(CI);
 
-    // Conservatively require the attributes of the call to match those of the
-    // return. Ignore noalias because it doesn't affect the call sequence.
-    AttributeList CalleeAttrs = CS.getAttributes();
-    if (AttrBuilder(CalleeAttrs, AttributeList::ReturnIndex)
-            .removeAttribute(Attribute::NoAlias) !=
-        AttrBuilder(CalleeAttrs, AttributeList::ReturnIndex)
-            .removeAttribute(Attribute::NoAlias))
-      continue;
-
     // Make sure the call instruction is followed by an unconditional branch to
     // the return block.
     BasicBlock *CallBB = CI->getParent();
Index: lib/CodeGen/Analysis.cpp
===================================================================
--- lib/CodeGen/Analysis.cpp
+++ lib/CodeGen/Analysis.cpp
@@ -519,10 +519,12 @@
   AttrBuilder CalleeAttrs(cast<CallInst>(I)->getAttributes(),
                           AttributeList::ReturnIndex);
 
-  // Noalias is completely benign as far as calling convention goes, it
-  // shouldn't affect whether the call is a tail call.
+  // NoAlias and NonNull are completely benign as far as calling convention
+  // goes, they shouldn't affect whether the call is a tail call.
   CallerAttrs.removeAttribute(Attribute::NoAlias);
   CalleeAttrs.removeAttribute(Attribute::NoAlias);
+  CallerAttrs.removeAttribute(Attribute::NonNull);
+  CalleeAttrs.removeAttribute(Attribute::NonNull);
 
   if (CallerAttrs.contains(Attribute::ZExt)) {
     if (!CalleeAttrs.contains(Attribute::ZExt))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52238.165987.patch
Type: text/x-patch
Size: 3177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180918/db5539cf/attachment-0001.bin>


More information about the llvm-commits mailing list