[PATCH] D12779: [InstCombineCalls] Use isKnownNonNullAt() to check nullness of passing arguments at callsite

Chen Li via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 11 13:19:55 PDT 2015


chenli updated this revision to Diff 34577.
chenli added a comment.

This update fixed a issue when argument is not pointer type and should not be attached with nonnull attribute.


http://reviews.llvm.org/D12779

Files:
  lib/Analysis/ValueTracking.cpp
  lib/Transforms/InstCombine/InstCombineCalls.cpp
  test/Transforms/InstCombine/call_nonnull_arg.ll

Index: test/Transforms/InstCombine/call_nonnull_arg.ll
===================================================================
--- test/Transforms/InstCombine/call_nonnull_arg.ll
+++ test/Transforms/InstCombine/call_nonnull_arg.ll
@@ -0,0 +1,17 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+; InstCombine should mark null-checked argument as nonnull at callsite
+declare void @dummy(i32*)
+
+define void @test(i32* %a) {
+; CHECK-LABEL: @test
+; CHECK: call void @dummy(i32* nonnull %a)
+entry:
+  %cond = icmp eq i32* %a, null
+  br i1 %cond, label %is_null, label %not_null
+not_null:
+  call void @dummy(i32* %a)
+  ret void
+is_null:
+  unreachable
+}
Index: lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1536,8 +1536,8 @@
   // checks on their arguments.
   unsigned ArgNo = 0;
   for (Value *V : CS.args()) {
-    if (!CS.paramHasAttr(ArgNo+1, Attribute::NonNull) &&
-        isKnownNonNull(V)) {
+    if (V->getType()->isPointerTy() && !CS.paramHasAttr(ArgNo+1, Attribute::NonNull) &&
+        isKnownNonNullAt(V, CS.getInstruction(), DT, TLI)) {
       AttributeSet AS = CS.getAttributes();
       AS = AS.addAttribute(CS.getInstruction()->getContext(), ArgNo+1,
                            Attribute::NonNull);
Index: lib/Analysis/ValueTracking.cpp
===================================================================
--- lib/Analysis/ValueTracking.cpp
+++ lib/Analysis/ValueTracking.cpp
@@ -3220,6 +3220,8 @@
 
 /// Return true if we know that the specified value is never null.
 bool llvm::isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI) {
+  assert(V->getType()->isPointerTy() && "V must be pointer type");
+
   // Alloca never returns null, malloc might.
   if (isa<AllocaInst>(V)) return true;
 
@@ -3252,6 +3254,8 @@
 static bool isKnownNonNullFromDominatingCondition(const Value *V,
                                                   const Instruction *CtxI,
                                                   const DominatorTree *DT) {
+  assert(V->getType()->isPointerTy() && "V must be pointer type");
+
   unsigned NumUsesExplored = 0;
   for (auto U : V->users()) {
     // Avoid massive lists


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12779.34577.patch
Type: text/x-patch
Size: 2309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150911/2b42cea2/attachment.bin>


More information about the llvm-commits mailing list