[llvm] r239795 - [InstCombine] Propagate non-null facts to call parameters

Philip Reames listmail at philipreames.com
Mon Jun 15 17:43:55 PDT 2015


Author: reames
Date: Mon Jun 15 19:43:54 2015
New Revision: 239795

URL: http://llvm.org/viewvc/llvm-project?rev=239795&view=rev
Log:
[InstCombine] Propagate non-null facts to call parameters

If a parameter to a function is known non-null, use the existing parameter attributes to record that fact at the call site. This has no optimization benefit by itself - that I know of - but is an enabling change for http://reviews.llvm.org/D9129.

Differential Revision: http://reviews.llvm.org/D9132


Added:
    llvm/trunk/test/Transforms/InstCombine/nonnull-param.ll
Modified:
    llvm/trunk/include/llvm/IR/CallSite.h
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/trunk/test/CodeGen/NVPTX/intrin-nocapture.ll
    llvm/trunk/test/Transforms/Inline/byval-tail-call.ll
    llvm/trunk/test/Transforms/InstCombine/select.ll

Modified: llvm/trunk/include/llvm/IR/CallSite.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/CallSite.h?rev=239795&r1=239794&r2=239795&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/CallSite.h (original)
+++ llvm/trunk/include/llvm/IR/CallSite.h Mon Jun 15 19:43:54 2015
@@ -27,6 +27,7 @@
 #define LLVM_IR_CALLSITE_H
 
 #include "llvm/ADT/PointerIntPair.h"
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/IR/Attributes.h"
 #include "llvm/IR/CallingConv.h"
 #include "llvm/IR/Instructions.h"
@@ -150,6 +151,9 @@ public:
   }
 
   IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
+  iterator_range<IterTy> args() const {
+    return iterator_range<IterTy>(arg_begin(), arg_end());
+  }
   bool arg_empty() const { return arg_end() == arg_begin(); }
   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
 

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=239795&r1=239794&r2=239795&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Mon Jun 15 19:43:54 2015
@@ -1391,6 +1391,24 @@ static IntrinsicInst *FindInitTrampoline
 // visitCallSite - Improvements for call and invoke instructions.
 //
 Instruction *InstCombiner::visitCallSite(CallSite CS) {
+
+  // Mark any parameters that are known to be non-null with the nonnull
+  // attribute.  This is helpful for inlining calls to functions with null
+  // checks on their arguments.
+  unsigned ArgNo = 0;
+  for (Value *V : CS.args()) {
+    if (!CS.paramHasAttr(ArgNo+1, Attribute::NonNull) &&
+        isKnownNonNull(V)) {
+      AttributeSet AS = CS.getAttributes();
+      AS = AS.addAttribute(CS.getInstruction()->getContext(), ArgNo+1,
+                           Attribute::NonNull);
+      CS.setAttributes(AS);
+      return CS.getInstruction();
+    }
+    ArgNo++;
+  }
+  assert(ArgNo == CS.arg_size() && "sanity check");
+  
   if (isAllocLikeFn(CS.getInstruction(), TLI))
     return visitAllocSite(*CS.getInstruction());
 

Modified: llvm/trunk/test/CodeGen/NVPTX/intrin-nocapture.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/NVPTX/intrin-nocapture.ll?rev=239795&r1=239794&r2=239795&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/NVPTX/intrin-nocapture.ll (original)
+++ llvm/trunk/test/CodeGen/NVPTX/intrin-nocapture.ll Mon Jun 15 19:43:54 2015
@@ -11,7 +11,7 @@ declare i32 addrspace(1)* @llvm.nvvm.ptr
 ; CHECK: @bar
 define void @bar() {
   %t1 = alloca i32
-; CHECK: call i32 addrspace(1)* @llvm.nvvm.ptr.gen.to.global.p1i32.p0i32(i32* %t1)
+; CHECK: call i32 addrspace(1)* @llvm.nvvm.ptr.gen.to.global.p1i32.p0i32(i32* nonnull %t1)
 ; CHECK-NEXT: store i32 10, i32* %t1
   %t2 = call i32 addrspace(1)* @llvm.nvvm.ptr.gen.to.global.p1i32.p0i32(i32* %t1)
   store i32 10, i32* %t1

Modified: llvm/trunk/test/Transforms/Inline/byval-tail-call.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Inline/byval-tail-call.ll?rev=239795&r1=239794&r2=239795&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Inline/byval-tail-call.ll (original)
+++ llvm/trunk/test/Transforms/Inline/byval-tail-call.ll Mon Jun 15 19:43:54 2015
@@ -33,7 +33,7 @@ define void @frob(i32* %x) {
 ; CHECK: %[[POS:.*]] = alloca i32
 ; CHECK: %[[VAL:.*]] = load i32, i32* %x
 ; CHECK: store i32 %[[VAL]], i32* %[[POS]]
-; CHECK: {{^ *}}call void @ext(i32* %[[POS]]
+; CHECK: {{^ *}}call void @ext(i32* nonnull %[[POS]]
 ; CHECK: tail call void @ext(i32* null)
 ; CHECK: ret void
   tail call void @qux(i32* byval %x)

Added: llvm/trunk/test/Transforms/InstCombine/nonnull-param.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/nonnull-param.ll?rev=239795&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/nonnull-param.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/nonnull-param.ll Mon Jun 15 19:43:54 2015
@@ -0,0 +1,42 @@
+; RUN: opt -S -instcombine < %s | FileCheck %s
+
+declare void @callee(i8* %arg)
+
+; Positive test - arg is known non null
+define void @test(i8* nonnull %arg) {
+; CHECK-LABEL: @test
+; CHECK: call void @callee(i8* nonnull %arg)
+  call void @callee(i8* %arg)
+  ret void
+}
+
+
+; Negative test - arg is not known to be non null
+define void @test2(i8* %arg) {
+; CHECK-LABEL: @test2
+; CHECK: call void @callee(i8* %arg)
+  call void @callee(i8* %arg)
+  ret void
+}
+
+declare void @callee2(i8*, i8*, i8*)
+
+; Sanity check arg indexing
+define void @test3(i8* %arg1, i8* nonnull %arg2, i8* %arg3) {
+; CHECK-LABEL: @test3
+; CHECK: call void @callee2(i8* %arg1, i8* nonnull %arg2, i8* %arg3)
+  call void @callee2(i8* %arg1, i8* %arg2, i8* %arg3)
+  ret void
+}
+
+; Because of the way CallSite::paramHasAttribute looks at the callee 
+; directly, we will not set the attribute on the CallSite.  That's 
+; fine as long as all consumers use the same check. 
+define void @test4(i8* nonnull %arg) {
+; CHECK-LABEL: @test4
+; CHECK: call void @test4(i8* %arg)
+  call void @test4(i8* %arg)
+  ret void
+}
+
+

Modified: llvm/trunk/test/Transforms/InstCombine/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/select.ll?rev=239795&r1=239794&r2=239795&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/select.ll Mon Jun 15 19:43:54 2015
@@ -1265,7 +1265,7 @@ define i32 @test77(i1 %flag, i32* %x) {
 ; load does.
 ; CHECK-LABEL: @test77(
 ; CHECK: %[[A:.*]] = alloca i32, align 1
-; CHECK: call void @scribble_on_i32(i32* %[[A]])
+; CHECK: call void @scribble_on_i32(i32* nonnull %[[A]])
 ; CHECK: store i32 0, i32* %x
 ; CHECK: %[[P:.*]] = select i1 %flag, i32* %[[A]], i32* %x
 ; CHECK: load i32, i32* %[[P]]





More information about the llvm-commits mailing list