Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2327,15 +2327,14 @@ if (isa(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; } 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 +}