diff options
| author | Chris Lattner <sabre@nondot.org> | 2007-04-14 00:20:02 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2007-04-14 00:20:02 +0000 |
| commit | efb33d28c6a407f65644604009e7cea56a29e668 (patch) | |
| tree | 336b04bb582cd2be919a39b7f1347e225b358fc8 /llvm/lib/Transforms/Scalar/InstructionCombining.cpp | |
| parent | a930c3d4e42337480e2177267dbb14c3f5dc4aea (diff) | |
| download | bcm5719-llvm-efb33d28c6a407f65644604009e7cea56a29e668.tar.gz bcm5719-llvm-efb33d28c6a407f65644604009e7cea56a29e668.zip | |
Implement PR1201 and test/Transforms/InstCombine/malloc-free-delete.ll
llvm-svn: 35981
Diffstat (limited to 'llvm/lib/Transforms/Scalar/InstructionCombining.cpp')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/InstructionCombining.cpp | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index ec27055f72e..17d2f93ca65 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/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 <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X - if (CastInst *CI = dyn_cast<CastInst>(Op)) - if (isa<PointerType>(CI->getOperand(0)->getType())) { - FI.setOperand(0, CI->getOperand(0)); - return &FI; - } - // free undef -> unreachable. if (isa<UndefValue>(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<ConstantPointerNull>(Op)) return EraseInstFromFunction(FI); + + // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X + if (BitCastInst *CI = dyn_cast<BitCastInst>(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<GetElementPtrInst>(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<MallocInst>(Op)) + if (MI->hasOneUse()) { + EraseInstFromFunction(FI); + return EraseInstFromFunction(*MI); + } return 0; } |

