[PATCH] D60846: [ValueTracking] Improve isKnowNonZero for Ints

Dan Robertson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 17 18:04:07 PDT 2019


dlrobertson created this revision.
dlrobertson added reviewers: nikic, nlopes, mkazantsev.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
dlrobertson added a comment.

Would an NFS baseline tests patch be useful here?


dlrobertson added a comment.

s/NFS/NFC


Improve isKnownNonZero for integers in order to improve cttz
optimizations.


https://reviews.llvm.org/D60846

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/InstCombine/cttz.ll


Index: llvm/test/Transforms/InstCombine/cttz.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/cttz.ll
@@ -0,0 +1,31 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+declare i64 @llvm.cttz.i64(i64, i1)
+
+define i32 @test0(i64 %x) {
+; CHECK-LABEL: @test0(
+; CHECK-NEXT:  start:
+; CHECK-NEXT:    [[C:%.*]] = icmp eq i64 [[X:%.*]], 0
+; CHECK-NEXT:    br i1 [[C]], label [[EXIT:%.*]], label [[NON_ZERO:%.*]]
+; CHECK:       non_zero:
+; CHECK-NEXT:    [[CTZ:%.*]] = call i64 @llvm.cttz.i64(i64 [[X]], i1 false), !range !0
+; CHECK-NEXT:    [[CTZ32:%.*]] = trunc i64 [[CTZ]] to i32
+; CHECK-NEXT:    br label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ [[CTZ32]], [[NON_ZERO]] ], [ 0, [[START:%.*]] ]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+start:
+  %c = icmp eq i64 %x, 0
+  br i1 %c, label %exit, label %non_zero
+
+non_zero:
+  %ctz = call i64 @llvm.cttz.i64(i64 %x, i1 false)
+  %ctz32 = trunc i64 %ctz to i32
+  br label %exit
+
+exit:
+  %res = phi i32 [ %ctz32, %non_zero ], [ 0, %start ]
+  ret i32 %res
+}
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -1893,10 +1893,9 @@
   return false;
 }
 
-static bool isKnownNonNullFromDominatingCondition(const Value *V,
+static bool isKnownNonZeroFromDominatingCondition(const Value *V,
                                                   const Instruction *CtxI,
                                                   const DominatorTree *DT) {
-  assert(V->getType()->isPointerTy() && "V must be pointer type");
   assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull");
 
   if (!CtxI || !DT)
@@ -1909,14 +1908,15 @@
       break;
     NumUsesExplored++;
 
-    // If the value is used as an argument to a call or invoke, then argument
-    // attributes may provide an answer about null-ness.
-    if (auto CS = ImmutableCallSite(U))
-      if (auto *CalledFunc = CS.getCalledFunction())
-        for (const Argument &Arg : CalledFunc->args())
-          if (CS.getArgOperand(Arg.getArgNo()) == V &&
-              Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
-            return true;
+    // If the value is a pointer and used as an argument to a call or invoke,
+    // then argument attributes may provide an answer about null-ness.
+    if (V->getType()->isPointerTy())
+      if (auto CS = ImmutableCallSite(U))
+        if (auto *CalledFunc = CS.getCalledFunction())
+          for (const Argument &Arg : CalledFunc->args())
+            if (CS.getArgOperand(Arg.getArgNo()) == V &&
+                Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
+              return true;
 
     // Consider only compare instructions uniquely controlling a branch
     CmpInst::Predicate Pred;
@@ -2064,11 +2064,11 @@
   }
 
 
+  if (isKnownNonZeroFromDominatingCondition(V, Q.CxtI, Q.DT))
+    return true;
+
   // Check for recursive pointer simplifications.
   if (V->getType()->isPointerTy()) {
-    if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
-      return true;
-
     // Look through bitcast operations, GEPs, and int2ptr instructions as they
     // do not alter the value, or at least not the nullness property of the
     // value, e.g., int2ptr is allowed to zero/sign extend the value.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60846.195657.patch
Type: text/x-patch
Size: 3539 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190418/fe58bb86/attachment.bin>


More information about the llvm-commits mailing list