[PATCH] D45343: [InstCombine] Always remove null check before free

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 7 05:13:18 PDT 2018


xbolva00 updated this revision to Diff 141487.

https://reviews.llvm.org/D45343

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/null-check-free.ll


Index: test/Transforms/InstCombine/null-check-free.ll
===================================================================
--- /dev/null
+++ test/Transforms/InstCombine/null-check-free.ll
@@ -0,0 +1,36 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+declare i8* @malloc(i32)
+
+declare i32 @free(...)
+
+define i8* @nullcheckfree() {
+; CHECK-LABEL: @nullcheckfree(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP:%.*]] = alloca i8*, align 8
+; CHECK-NEXT:    br i1 true, label [[IF_THEN:%.*]], label [[IF_END:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    [[TMP0:%.*]] = load i8*, i8** [[TMP]], align 8
+; CHECK-NEXT:    [[CALL1:%.*]] = call i32 bitcast (i32 (...)* @free to i32 (i8*)*)(i8* [[TMP0]])
+; CHECK-NEXT:    br label [[IF_END]]
+; CHECK:       if.end:
+; CHECK-NEXT:    [[TMP1:%.*]] = load i8*, i8** [[TMP]], align 8
+; CHECK-NEXT:    ret i8* [[TMP1]]
+;
+entry:
+  %tmp = alloca i8*, align 4
+  %call = call i8* @malloc(i32 10)
+  %tobool = icmp ne i8* %call, null
+  br i1 %tobool, label %if.then, label %if.end
+
+if.then:
+  %0 = load i8*, i8** %tmp, align 4
+  %call1 = call i32 bitcast (i32 (...)* @free to i32 (i8*)*)(i8* %0)
+  br label %if.end
+
+if.end:
+  %1 = load i8*, i8** %tmp, align 4
+  ret i8* %1
+
+}
\ No newline at end of file
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2327,15 +2327,14 @@
   if (isa<ConstantPointerNull>(Op))
     return eraseInstFromFunction(FI);
 
-  // If we optimize for code size, try to move the call to free before the null
-  // test so that simplify cfg can remove the empty block and dead code
-  // elimination the branch. I.e., helps to turn something like:
+  // Try to move the call to free before the null test so that simplify cfg can
+  // remove the empty block and dead code elimination the branch.
+  // I.e., helps to turn something like:
   // if (foo) free(foo);
   // into
   // free(foo);
-  if (MinimizeSize)
-    if (Instruction *I = tryToMoveFreeBeforeNullTest(FI))
-      return I;
+  if (Instruction *I = tryToMoveFreeBeforeNullTest(FI))
+    return I;
 
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45343.141487.patch
Type: text/x-patch
Size: 2339 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180407/a37ca27b/attachment.bin>


More information about the llvm-commits mailing list