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

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 6 06:37:12 PDT 2018


xbolva00 updated this revision to Diff 141330.

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,34 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+declare i8* @malloc(i32)
+
+declare i32 @free(...) 
+
+define i8* @nullcheckfree() {
+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
+
+; CHECK-LABEL: define i8* @nullcheckfree(
+; CHECK:       entry:
+; CHECK-NEXT:    %tmp = alloca i8*, align 8
+; CHECK-NEXT:    br i1 true, label %if.then, label %if.end
+; CHECK:       if.then:                                          ; preds = %entry 
+; CHECK-NEXT:    %0 = load i8*, i8** %tmp, align 8
+; CHECK-NEXT:    %call1 = call i32 bitcast (i32 (...)* @free to i32 (i8*)*)(i8* %0)
+; CHECK-NEXT:    br label %if.end
+; CHECK:       if.end:                                           ; preds = %if.then, %entry
+; CHECK-NEXT:    %1 = load i8*, i8** %tmp, align 8
+; CHECK-NEXT:    ret i8* %1
+}
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.141330.patch
Type: text/x-patch
Size: 2335 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180406/ca15634b/attachment.bin>


More information about the llvm-commits mailing list