From 6fe5541003e8781af88120db69ca6b7cc498d841 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 14 Apr 2007 00:20:02 +0000 Subject: [PATCH] Implement PR1201 and test/Transforms/InstCombine/malloc-free-delete.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35981 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index ec27055f72e..17d2f93ca65 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -8367,13 +8367,6 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { Value *Op = FI.getOperand(0); - // Change free * (cast * X to *) into free * X - if (CastInst *CI = dyn_cast(Op)) - if (isa(CI->getOperand(0)->getType())) { - FI.setOperand(0, CI->getOperand(0)); - return &FI; - } - // free undef -> unreachable. if (isa(Op)) { // Insert a new store to null because we cannot modify the CFG here. @@ -8381,11 +8374,33 @@ Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { UndefValue::get(PointerType::get(Type::Int1Ty)), &FI); return EraseInstFromFunction(FI); } - + // If we have 'free null' delete the instruction. This can happen in stl code // when lots of inlining happens. if (isa(Op)) return EraseInstFromFunction(FI); + + // Change free * (cast * X to *) into free * X + if (BitCastInst *CI = dyn_cast(Op)) { + FI.setOperand(0, CI->getOperand(0)); + return &FI; + } + + // Change free (gep X, 0,0,0,0) into free(X) + if (GetElementPtrInst *GEPI = dyn_cast(Op)) { + if (GEPI->hasAllZeroIndices()) { + AddToWorkList(GEPI); + FI.setOperand(0, GEPI->getOperand(0)); + return &FI; + } + } + + // Change free(malloc) into nothing, if the malloc has a single use. + if (MallocInst *MI = dyn_cast(Op)) + if (MI->hasOneUse()) { + EraseInstFromFunction(FI); + return EraseInstFromFunction(*MI); + } return 0; }