mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-24 19:52:54 +01:00
Implement PR1201 and test/Transforms/InstCombine/malloc-free-delete.ll
llvm-svn: 35981
This commit is contained in:
parent
1d90754cf7
commit
6f64f54168
@ -8367,13 +8367,6 @@ Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
|
|||||||
Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
|
Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
|
||||||
Value *Op = FI.getOperand(0);
|
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.
|
// free undef -> unreachable.
|
||||||
if (isa<UndefValue>(Op)) {
|
if (isa<UndefValue>(Op)) {
|
||||||
// Insert a new store to null because we cannot modify the CFG here.
|
// Insert a new store to null because we cannot modify the CFG here.
|
||||||
@ -8387,6 +8380,28 @@ Instruction *InstCombiner::visitFreeInst(FreeInst &FI) {
|
|||||||
if (isa<ConstantPointerNull>(Op))
|
if (isa<ConstantPointerNull>(Op))
|
||||||
return EraseInstFromFunction(FI);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user