Hi rsmith, chandlerc, nlewycky,

This is the Clang companion patch to http://reviews.llvm.org/D4449 which adds a 
dereferencable function parameter attribute (like the nonnull attribute, but 
specifying that the pointer is dereferencable like the value of an alloca is 
dereferencable). This patch causes the dereferencable attribute to appear on 
reference parameters (and return values) just like nonnull does currently.

As mentioned in the llvm patch summary, my motivating use case is C++ 
references that are passes as function parameters and used in loops. For 
example,

void foo(int * __restrict__ a, int * __restrict__ b, int &c, int n) {
  for (int i = 0; i < n; ++i)
    if (a[i] > 0)
      a[i] = c*b[i];
}

Currently, we generate a load of 'c' in every loop iteration because we can't 
prove that there is no control dependence on the dereferencability of 'c' from 
the if condition. But because 'c' is a reference, we know it must point to 
something, and since nothing else in the loop can alias it, it is safe to load 
it in the preheader.

http://reviews.llvm.org/D4450

Files:
  lib/CodeGen/CGCall.cpp

Index: lib/CodeGen/CGCall.cpp
===================================================================
--- lib/CodeGen/CGCall.cpp
+++ lib/CodeGen/CGCall.cpp
@@ -1200,8 +1200,10 @@
     llvm_unreachable("Invalid ABI kind for return argument");
   }
 
-  if (RetTy->isReferenceType())
+  if (RetTy->isReferenceType()) {
+    RetAttrs.addAttribute(llvm::Attribute::Dereferencable);
     RetAttrs.addAttribute(llvm::Attribute::NonNull);
+  }
 
   if (RetAttrs.hasAttributes())
     PAL.push_back(llvm::
@@ -1291,8 +1293,10 @@
     }
     }
 
-    if (ParamType->isReferenceType())
+    if (ParamType->isReferenceType()) {
+      Attrs.addAttribute(llvm::Attribute::Dereferencable);
       Attrs.addAttribute(llvm::Attribute::NonNull);
+    }
 
     if (Attrs.hasAttributes())
       PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs));
Index: lib/CodeGen/CGCall.cpp
===================================================================
--- lib/CodeGen/CGCall.cpp
+++ lib/CodeGen/CGCall.cpp
@@ -1200,8 +1200,10 @@
     llvm_unreachable("Invalid ABI kind for return argument");
   }
 
-  if (RetTy->isReferenceType())
+  if (RetTy->isReferenceType()) {
+    RetAttrs.addAttribute(llvm::Attribute::Dereferencable);
     RetAttrs.addAttribute(llvm::Attribute::NonNull);
+  }
 
   if (RetAttrs.hasAttributes())
     PAL.push_back(llvm::
@@ -1291,8 +1293,10 @@
     }
     }
 
-    if (ParamType->isReferenceType())
+    if (ParamType->isReferenceType()) {
+      Attrs.addAttribute(llvm::Attribute::Dereferencable);
       Attrs.addAttribute(llvm::Attribute::NonNull);
+    }
 
     if (Attrs.hasAttributes())
       PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs));
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to