[PATCH] D149747: [InlineCost] Consider branches with !make.implicit metadata as free.

Denis Antrushin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 25 08:43:48 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG291223409c61: [InlineCost] Consider branches with !make.implicit metadata as free. (authored by dantrushin).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149747

Files:
  llvm/lib/Analysis/InlineCost.cpp
  llvm/test/Transforms/Inline/implicit-null-check.ll


Index: llvm/test/Transforms/Inline/implicit-null-check.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/Inline/implicit-null-check.ll
@@ -0,0 +1,24 @@
+; RUN: opt -passes=inline -inline-threshold=10 -S < %s | FileCheck %s
+
+declare void @foo()
+
+; CHECK-LABEL: @caller
+; CHECK-NOT:   %res = call i64 @callee(ptr %p)
+define i64 @caller(ptr %p) {
+  %res = call i64 @callee(ptr %p)
+  ret i64 %res
+}
+
+define i64 @callee(ptr %p) {
+  %null_check = icmp eq ptr %p, null
+  br i1 %null_check, label %is_null, label %non_null, !make.implicit !0
+
+is_null:
+  call void @foo()
+  ret i64 0
+
+non_null:
+  ret i64 1
+}
+
+!0 = !{}
Index: llvm/lib/Analysis/InlineCost.cpp
===================================================================
--- llvm/lib/Analysis/InlineCost.cpp
+++ llvm/lib/Analysis/InlineCost.cpp
@@ -1976,14 +1976,27 @@
     }
   }
 
+  auto isImplicitNullCheckCmp = [](const CmpInst &I) {
+    for (auto *User : I.users())
+      if (auto *Instr = dyn_cast<Instruction>(User))
+        if (!Instr->getMetadata(LLVMContext::MD_make_implicit))
+          return false;
+    return true;
+  };
+
   // If the comparison is an equality comparison with null, we can simplify it
   // if we know the value (argument) can't be null
-  if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1)) &&
-      isKnownNonNullInCallee(I.getOperand(0))) {
-    bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
-    SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
-                                      : ConstantInt::getFalse(I.getType());
-    return true;
+  if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1))) {
+    if (isKnownNonNullInCallee(I.getOperand(0))) {
+      bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
+      SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
+                                        : ConstantInt::getFalse(I.getType());
+      return true;
+    }
+    // Implicit null checks act as unconditional branches and their comparisons
+    // should be treated as simplified and free of cost.
+    if (isImplicitNullCheckCmp(I))
+      return true;
   }
   return handleSROA(I.getOperand(0), isa<ConstantPointerNull>(I.getOperand(1)));
 }
@@ -2265,6 +2278,7 @@
   // inliner more regular and predictable. Interestingly, conditional branches
   // which will fold away are also free.
   return BI.isUnconditional() || isa<ConstantInt>(BI.getCondition()) ||
+         BI.getMetadata(LLVMContext::MD_make_implicit) ||
          isa_and_nonnull<ConstantInt>(
              SimplifiedValues.lookup(BI.getCondition()));
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149747.525642.patch
Type: text/x-patch
Size: 2687 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230525/afdc1339/attachment.bin>


More information about the llvm-commits mailing list