llvm.org GIT mirror llvm / 4d882aa
A non-escaping malloc in the entry block is not unlike an alloca. Do dead-store elimination on them too. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142735 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
2 changed file(s) with 33 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
2323 #include "llvm/IntrinsicInst.h"
2424 #include "llvm/Pass.h"
2525 #include "llvm/Analysis/AliasAnalysis.h"
26 #include "llvm/Analysis/CaptureTracking.h"
2627 #include "llvm/Analysis/Dominators.h"
2728 #include "llvm/Analysis/MemoryBuiltins.h"
2829 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
254255
255256 static uint64_t getPointerSize(Value *V, AliasAnalysis &AA) {
256257 const TargetData *TD = AA.getTargetData();
258
259 if (CallInst *CI = dyn_cast(V)) {
260 assert(isMalloc(CI) && "Expected Malloc call!");
261 if (ConstantInt *C = dyn_cast(CI->getArgOperand(0)))
262 return C->getZExtValue();
263 return AliasAnalysis::UnknownSize;
264 }
265
257266 if (TD == 0)
258267 return AliasAnalysis::UnknownSize;
259268
264273 return AliasAnalysis::UnknownSize;
265274 }
266275
267 assert(isa(V) && "Expected AllocaInst or Argument!");
276 assert(isa(V) && "Expected AllocaInst, malloc call or Argument!");
268277 PointerType *PT = cast(V->getType());
269278 return TD->getTypeAllocSize(PT->getElementType());
270279 }
278287 return !GV->mayBeOverridden();
279288 if (const Argument *A = dyn_cast(V))
280289 return A->hasByValAttr();
290 if (isMalloc(V))
291 return true;
281292 return false;
282293 }
283294
587598
588599 // Find all of the alloca'd pointers in the entry block.
589600 BasicBlock *Entry = BB.getParent()->begin();
590 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I)
601 for (BasicBlock::iterator I = Entry->begin(), E = Entry->end(); I != E; ++I) {
591602 if (AllocaInst *AI = dyn_cast(I))
592603 DeadStackObjects.insert(AI);
604
605 // Okay, so these are dead heap objects, but if the pointer never escapes
606 // then it's leaked by this function anyways.
607 if (CallInst *CI = extractMallocCall(I))
608 if (!PointerMayBeCaptured(CI, true, true))
609 DeadStackObjects.insert(CI);
610 }
593611
594612 // Treat byval arguments the same, stores to them are dead at the end of the
595613 // function.
633651
634652 if (AllocaInst *A = dyn_cast(BBI)) {
635653 DeadStackObjects.erase(A);
654 continue;
655 }
656
657 if (CallInst *CI = extractMallocCall(BBI)) {
658 DeadStackObjects.erase(CI);
636659 continue;
637660 }
638661
250250 ; CHECK: call void @test19f
251251 }
252252
253 define void @test20() {
254 %m = call i8* @malloc(i32 24)
255 store i8 0, i8* %m
256 ret void
257 }
258 ; CHECK: @test20
259 ; CHECK-NOT: store
260 ; CHECK: ret void