llvm.org GIT mirror llvm / 336b88d
Do simple cross-block DSE when we encounter a free statement. Fixes PR11240. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@143808 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 7 years ago
2 changed file(s) with 83 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
3333 #include "llvm/Support/Debug.h"
3434 #include "llvm/ADT/SmallPtrSet.h"
3535 #include "llvm/ADT/Statistic.h"
36 #include "llvm/ADT/STLExtras.h"
3637 using namespace llvm;
3738
3839 STATISTIC(NumFastStores, "Number of stores deleted");
4243 struct DSE : public FunctionPass {
4344 AliasAnalysis *AA;
4445 MemoryDependenceAnalysis *MD;
46 DominatorTree *DT;
4547
4648 static char ID; // Pass identification, replacement for typeid
47 DSE() : FunctionPass(ID), AA(0), MD(0) {
49 DSE() : FunctionPass(ID), AA(0), MD(0), DT(0) {
4850 initializeDSEPass(*PassRegistry::getPassRegistry());
4951 }
5052
5153 virtual bool runOnFunction(Function &F) {
5254 AA = &getAnalysis();
5355 MD = &getAnalysis();
54 DominatorTree &DT = getAnalysis();
56 DT = &getAnalysis();
5557
5658 bool Changed = false;
5759 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
5860 // Only check non-dead blocks. Dead blocks may have strange pointer
5961 // cycles that will confuse alias analysis.
60 if (DT.isReachableFromEntry(I))
62 if (DT->isReachableFromEntry(I))
6163 Changed |= runOnBasicBlock(*I);
6264
63 AA = 0; MD = 0;
65 AA = 0; MD = 0; DT = 0;
6466 return Changed;
6567 }
6668
548550 return MadeChange;
549551 }
550552
553 /// Find all blocks that will unconditionally lead to the block BB and append
554 /// them to F.
555 static void FindUnconditionalPreds(SmallVectorImpl &Blocks,
556 BasicBlock *BB, DominatorTree *DT) {
557 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
558 BasicBlock *Pred = *I;
559 TerminatorInst *PredTI = Pred->getTerminator();
560 if (PredTI->getNumSuccessors() != 1)
561 continue;
562
563 if (DT->isReachableFromEntry(Pred))
564 Blocks.push_back(Pred);
565 }
566 }
567
551568 /// HandleFree - Handle frees of entire structures whose dependency is a store
552569 /// to a field of that structure.
553570 bool DSE::HandleFree(CallInst *F) {
554571 bool MadeChange = false;
555572
556 MemDepResult Dep = MD->getDependency(F);
557
558 while (Dep.isDef() || Dep.isClobber()) {
559 Instruction *Dependency = Dep.getInst();
560 if (!hasMemoryWrite(Dependency) || !isRemovable(Dependency))
561 return MadeChange;
562
563 Value *DepPointer =
564 GetUnderlyingObject(getStoredPointerOperand(Dependency));
565
566 // Check for aliasing.
567 if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
568 return MadeChange;
569
570 // DCE instructions only used to calculate that store
571 DeleteDeadInstruction(Dependency, *MD);
572 ++NumFastStores;
573 MadeChange = true;
574
575 // Inst's old Dependency is now deleted. Compute the next dependency,
576 // which may also be dead, as in
577 // s[0] = 0;
578 // s[1] = 0; // This has just been deleted.
579 // free(s);
580 Dep = MD->getDependency(F);
581 };
573 AliasAnalysis::Location Loc = AliasAnalysis::Location(F->getOperand(0));
574 SmallVector Blocks;
575 Blocks.push_back(F->getParent());
576
577 while (!Blocks.empty()) {
578 BasicBlock *BB = Blocks.pop_back_val();
579 Instruction *InstPt = BB->getTerminator();
580 if (BB == F->getParent()) InstPt = F;
581
582 MemDepResult Dep = MD->getPointerDependencyFrom(Loc, false, InstPt, BB);
583 while (Dep.isDef() || Dep.isClobber()) {
584 Instruction *Dependency = Dep.getInst();
585 if (!hasMemoryWrite(Dependency) || !isRemovable(Dependency))
586 break;
587
588 Value *DepPointer =
589 GetUnderlyingObject(getStoredPointerOperand(Dependency));
590
591 // Check for aliasing.
592 if (!AA->isMustAlias(F->getArgOperand(0), DepPointer))
593 break;
594
595 Instruction *Next = llvm::next(BasicBlock::iterator(Dependency));
596
597 // DCE instructions only used to calculate that store
598 DeleteDeadInstruction(Dependency, *MD);
599 ++NumFastStores;
600 MadeChange = true;
601
602 // Inst's old Dependency is now deleted. Compute the next dependency,
603 // which may also be dead, as in
604 // s[0] = 0;
605 // s[1] = 0; // This has just been deleted.
606 // free(s);
607 Dep = MD->getPointerDependencyFrom(Loc, false, Next, BB);
608 }
609
610 if (Dep.isNonLocal())
611 FindUnconditionalPreds(Blocks, BB, DT);
612 }
582613
583614 return MadeChange;
584615 }
0 ; RUN: opt < %s -basicaa -dse -S | FileCheck %s
11
22 target datalayout = "e-p:64:64:64"
3
4 declare void @free(i8* nocapture)
5 declare noalias i8* @malloc(i64)
36
47 ; CHECK: @test
58 ; CHECK-NEXT: bitcast
2528 ret void
2629 }
2730
28 ; CHECK: @test4
31 ; CHECK: @test3
2932 ; CHECK-NOT: store
3033 ; CHECK: ret void
31 define void @test4() {
34 define void @test3() {
3235 %m = call i8* @malloc(i64 24)
3336 store i8 0, i8* %m
3437 %m1 = getelementptr i8* %m, i64 1
3740 ret void
3841 }
3942
40 declare void @free(i8*)
41 declare i8* @malloc(i64)
43 ; PR11240
44 ; CHECK: @test4
45 ; CHECK-NOT: store
46 ; CHECK: ret void
47 define void @test4(i1 %x) nounwind {
48 entry:
49 %alloc1 = tail call noalias i8* @malloc(i64 4) nounwind
50 br i1 %x, label %skipinit1, label %init1
51
52 init1:
53 store i8 1, i8* %alloc1
54 br label %skipinit1
55
56 skipinit1:
57 tail call void @free(i8* %alloc1) nounwind
58 ret void
59 }