[PATCH] D109988: Teach InstCombine to eliminate malloc-realloc-free triplets.

Owen Anderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 17 11:30:24 PDT 2021


resistor created this revision.
Herald added a subscriber: hiraditya.
resistor requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109988

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/alloc-realloc-free.ll


Index: llvm/test/Transforms/InstCombine/alloc-realloc-free.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/alloc-realloc-free.ll
@@ -0,0 +1,24 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --scrub-attributes
+; RUN: opt -S -instcombine < %s | FileCheck %s
+
+define dso_local void @test() local_unnamed_addr #0 {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    ret void
+;
+  %1 = tail call noalias align 16 dereferenceable_or_null(4) i8* @malloc(i64 4) #4
+  %2 = tail call align 16 dereferenceable_or_null(6) i8* @realloc(i8* %1, i64 6) #4
+  tail call void @free(i8* %2) #4
+  ret void
+}
+
+declare dso_local noalias noundef i8* @malloc(i64 noundef) local_unnamed_addr #1
+declare dso_local noalias noundef i8* @realloc(i8* nocapture, i64 noundef) local_unnamed_addr #2
+declare dso_local void @free(i8* nocapture noundef) local_unnamed_addr #2
+declare void @llvm.dbg.value(metadata, metadata, metadata) #3
+
+attributes #0 = { mustprogress nounwind uwtable willreturn }
+attributes #1 = { inaccessiblememonly mustprogress nofree nounwind willreturn }
+attributes #2 = { inaccessiblemem_or_argmemonly mustprogress nounwind willreturn }
+attributes #3 = { nofree nosync nounwind readnone speculatable willreturn }
+attributes #4 = { nounwind }
+
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2638,6 +2638,13 @@
           Users.emplace_back(I);
           continue;
         }
+
+        if (isReallocLikeFn(I, TLI, true)) {
+          Users.emplace_back(I);
+          Worklist.push_back(I);
+          continue;
+        }
+
         return false;
 
       case Instruction::Store: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109988.373290.patch
Type: text/x-patch
Size: 1911 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210917/460fb7b5/attachment.bin>


More information about the llvm-commits mailing list