A non-escaping malloc in the entry block is not unlike an alloca. Do dead-store

elimination on them too.

llvm-svn: 142735
This commit is contained in:
Nick Lewycky 2011-10-22 21:59:35 +00:00
parent ce8bfeadff
commit 25e5f6896b
2 changed files with 33 additions and 2 deletions

View File

@ -24,6 +24,7 @@
#include "llvm/IntrinsicInst.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
@ -255,6 +256,14 @@ static Value *getStoredPointerOperand(Instruction *I) {
static uint64_t getPointerSize(Value *V, AliasAnalysis &AA) {
const TargetData *TD = AA.getTargetData();
if (CallInst *CI = dyn_cast<CallInst>(V)) {
assert(isMalloc(CI) && "Expected Malloc call!");
if (ConstantInt *C = dyn_cast<ConstantInt>(CI->getArgOperand(0)))
return C->getZExtValue();
return AliasAnalysis::UnknownSize;
}
if (TD == 0)
return AliasAnalysis::UnknownSize;
@ -265,7 +274,7 @@ static uint64_t getPointerSize(Value *V, AliasAnalysis &AA) {
return AliasAnalysis::UnknownSize;
}
assert(isa<Argument>(V) && "Expected AllocaInst or Argument!");
assert(isa<Argument>(V) && "Expected AllocaInst, malloc call or Argument!");
PointerType *PT = cast<PointerType>(V->getType());
return TD->getTypeAllocSize(PT->getElementType());
}
@ -279,6 +288,8 @@ static bool isObjectPointerWithTrustworthySize(const Value *V) {
return !GV->mayBeOverridden();
if (const Argument *A = dyn_cast<Argument>(V))
return A->hasByValAttr();
if (isMalloc(V))
return true;
return false;
}
@ -588,10 +599,17 @@ bool DSE::handleEndBlock(BasicBlock &BB) {
// Find all of the alloca'd pointers in the entry block.
BasicBlock *Entry = BB.getParent()->begin();
for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) {
if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
DeadStackObjects.insert(AI);
// Okay, so these are dead heap objects, but if the pointer never escapes
// then it's leaked by this function anyways.
if (CallInst *CI = extractMallocCall(I))
if (!PointerMayBeCaptured(CI, true, true))
DeadStackObjects.insert(CI);
}
// Treat byval arguments the same, stores to them are dead at the end of the
// function.
for (Function::arg_iterator AI = BB.getParent()->arg_begin(),
@ -637,6 +655,11 @@ bool DSE::handleEndBlock(BasicBlock &BB) {
continue;
}
if (CallInst *CI = extractMallocCall(BBI)) {
DeadStackObjects.erase(CI);
continue;
}
if (CallSite CS = cast<Value>(BBI)) {
// If this call does not access memory, it can't be loading any of our
// pointers.

View File

@ -251,3 +251,11 @@ bb:
; CHECK: call void @test19f
}
define void @test20() {
%m = call i8* @malloc(i32 24)
store i8 0, i8* %m
ret void
}
; CHECK: @test20
; CHECK-NOT: store
; CHECK: ret void