[PATCH] D70140: Use nofree argument attributr hor heap-to-stack conversion

Stefan Stipanovic via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 17 12:42:28 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rGa516fbac5202: [Attributor] Use nofree argument attribute for heap-to-stack conversion (authored by sstefan1).

Changed prior to commit:
  https://reviews.llvm.org/D70140?vs=228945&id=229726#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D70140

Files:
  llvm/lib/Transforms/IPO/Attributor.cpp
  llvm/test/Transforms/FunctionAttrs/heap_to_stack.ll


Index: llvm/test/Transforms/FunctionAttrs/heap_to_stack.ll
===================================================================
--- llvm/test/Transforms/FunctionAttrs/heap_to_stack.ll
+++ llvm/test/Transforms/FunctionAttrs/heap_to_stack.ll
@@ -24,6 +24,13 @@
 
 declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture) nounwind
 
+; CHECK: @nofree_arg_only(i8* nocapture nofree %p1, i8* nocapture %p2)
+define void @nofree_arg_only(i8* %p1, i8* %p2) {
+  tail call void @free(i8* %p2)
+  tail call void @nofree_func(i8* %p1)
+  ret void
+}
+
 ; TEST 1 - negative, pointer freed in another function.
 
 define void @test1() {
@@ -59,6 +66,16 @@
   ret void
 }
 
+define void @test3a(i8* %p) {
+  %1 = tail call noalias i8* @malloc(i64 4)
+  ; CHECK: %1 = alloca i8, i64 4
+  ; CHECK-NEXT: tail call void @nofree_arg_only
+  tail call void @nofree_arg_only(i8* %1, i8* %p)
+  ; CHECK-NOT: @free(i8* %1)
+  tail call void @free(i8* %1)
+  ret void
+}
+
 declare noalias i8* @calloc(i64, i64)
 
 define void @test0() {
@@ -85,7 +102,7 @@
 ; TEST 5 - not all exit paths have a call to free, but all uses of malloc
 ; are in nofree functions and are not captured
 
-define void @test5(i32) {
+define void @test5(i32, i8* %p) {
   %2 = tail call noalias i8* @malloc(i64 4)
   ; CHECK: %2 = alloca i8, i64 4
   ; CHECK-NEXT: icmp eq i32 %0, 0
@@ -97,6 +114,7 @@
   br label %6
 
 5:                                                ; preds = %1
+  tail call void @nofree_arg_only(i8* %2, i8* %p)
   tail call void @free(i8* %2)
   ; CHECK-NOT: @free(i8* %2)
   br label %6
Index: llvm/lib/Transforms/IPO/Attributor.cpp
===================================================================
--- llvm/lib/Transforms/IPO/Attributor.cpp
+++ llvm/lib/Transforms/IPO/Attributor.cpp
@@ -4155,11 +4155,6 @@
         continue;
       }
 
-      // NOTE: Right now, if a function that has malloc pointer as an argument
-      // frees memory, we assume that the malloc pointer is freed.
-
-      // TODO: Add nofree callsite argument attribute to indicate that pointer
-      // argument is not freed.
       if (auto *CB = dyn_cast<CallBase>(UserI)) {
         if (!CB->isArgOperand(U))
           continue;
@@ -4180,15 +4175,16 @@
           continue;
         }
 
-        // If a function does not free memory we are fine
-        const auto &NoFreeAA =
-            A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(*CB));
-
         unsigned ArgNo = CB->getArgOperandNo(U);
+
         const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
             *this, IRPosition::callsite_argument(*CB, ArgNo));
 
-        if (!NoCaptureAA.isAssumedNoCapture() || !NoFreeAA.isAssumedNoFree()) {
+        // If a callsite argument use is nofree, we are fine.
+        const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
+            *this, IRPosition::callsite_argument(*CB, ArgNo));
+
+        if (!NoCaptureAA.isAssumedNoCapture() || !ArgNoFreeAA.isAssumedNoFree()) {
           LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
           ValidUsesOnly = false;
         }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70140.229726.patch
Type: text/x-patch
Size: 3061 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191117/9ed54dfe/attachment.bin>


More information about the llvm-commits mailing list