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,35 @@ +; 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 + +}