llvm.org GIT mirror llvm / 75f2d37
Fix PR24469 resulting from r245025 and re-enable dead store elimination across basicblocks. PR24469 resulted because DeleteDeadInstruction in handleNonLocalStoreDeletion was deleting the next basic block iterator. Fixed the same by resetting the basic block iterator post call to DeleteDeadInstruction. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245195 91177308-0d34-0410-b5e6-96231b3b80d8 Karthik Bhat 4 years ago
3 changed file(s) with 439 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
1515 //===----------------------------------------------------------------------===//
1616
1717 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/ADT/DenseSet.h"
1819 #include "llvm/ADT/STLExtras.h"
1920 #include "llvm/ADT/SetVector.h"
2021 #include "llvm/ADT/Statistic.h"
2122 #include "llvm/Analysis/AliasAnalysis.h"
2223 #include "llvm/Analysis/CaptureTracking.h"
24 #include "llvm/Analysis/CFG.h"
2325 #include "llvm/Analysis/MemoryBuiltins.h"
2426 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
27 #include "llvm/Analysis/PostDominators.h"
2528 #include "llvm/Analysis/TargetLibraryInfo.h"
2629 #include "llvm/Analysis/ValueTracking.h"
2730 #include "llvm/IR/Constants.h"
4144
4245 STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
4346 STATISTIC(NumFastStores, "Number of stores deleted");
47 STATISTIC(NumCrossBlockStores, "Number of cross block stores deleted");
4448 STATISTIC(NumFastOther , "Number of other instrs removed");
4549
4650 namespace {
4852 AliasAnalysis *AA;
4953 MemoryDependenceAnalysis *MD;
5054 DominatorTree *DT;
55 PostDominatorTree *PDT;
5156 const TargetLibraryInfo *TLI;
52
57 SmallVector, 16> Candidates;
58 SetVector DeadStores;
59 SmallVector, 32>
60 BackEdges;
61 DenseSet> BackEdgesMap;
5362 static char ID; // Pass identification, replacement for typeid
54 DSE() : FunctionPass(ID), AA(nullptr), MD(nullptr), DT(nullptr) {
63 DSE()
64 : FunctionPass(ID), AA(nullptr), MD(nullptr), DT(nullptr),
65 PDT(nullptr) {
5566 initializeDSEPass(*PassRegistry::getPassRegistry());
67 }
68 // Return all stores in a given BasicBlock.
69 SmallVector getStores(BasicBlock *BB) {
70 SmallVector VecStores;
71 for (auto &BI : *BB) {
72 if (StoreInst *SI = dyn_cast(&BI))
73 VecStores.push_back(SI);
74 }
75 return VecStores;
76 }
77
78 // Get dfs in/out on the PDT and populate Candidates store list which
79 // is used to find potential dead stores for a given block
80 void populateCandidateStores(Function &F) {
81 for (auto &I : F) {
82 DomTreeNode *DTNode = PDT->getNode(&I);
83 if (!DTNode)
84 continue;
85 int DFSIn = DTNode->getDFSNumIn();
86 SmallVector VecStores = getStores(&I);
87 Candidates[DFSIn] = VecStores;
88 }
5689 }
5790
5891 bool runOnFunction(Function &F) override {
6396 MD = &getAnalysis();
6497 DT = &getAnalysis().getDomTree();
6598 TLI = &getAnalysis().getTLI();
66
99 PDT = &getAnalysis();
100 if (PDT->getRootNode()) {
101 int Count = PDT->getRootNode()->getDFSNumOut();
102 SmallVector VecStores;
103 Candidates.resize(Count + 1);
104 Candidates.assign(Count + 1, VecStores);
105
106 // If we have more than 1 block try to populate candidate store.
107 if (Count > 1) {
108 populateCandidateStores(F);
109 FindFunctionBackedges(F, BackEdges);
110 for (auto I : BackEdges)
111 BackEdgesMap.insert(I);
112 }
113 }
67114 bool Changed = false;
68115 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
69116 // Only check non-dead blocks. Dead blocks may have strange pointer
82129 void RemoveAccessedObjects(const MemoryLocation &LoadedLoc,
83130 SmallSetVector &DeadStackObjects,
84131 const DataLayout &DL);
85
132 void handleNonLocalStoreDeletion(StoreInst *SI, BasicBlock::iterator &BBI,
133 BasicBlock &CurBlock);
134 bool isSafeCandidateForDeletion(BasicBlock *SrcBlock, BasicBlock *SinkBlock,
135 StoreInst *SI);
136 void DeleteDeadInstruction(Instruction *I, MemoryDependenceAnalysis &MD,
137 const TargetLibraryInfo &TLI,
138 SmallSetVector *ValueSet = nullptr);
86139 void getAnalysisUsage(AnalysisUsage &AU) const override {
87140 AU.setPreservesCFG();
88141 AU.addRequired();
89142 AU.addRequired();
90143 AU.addRequired();
144 AU.addRequired();
91145 AU.addRequired();
92146 AU.addPreserved();
93147 AU.addPreserved();
94148 AU.addPreserved();
149 AU.addPreserved();
95150 }
96151 };
97152 }
101156 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
102157 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
103158 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
159 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
104160 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
105161 INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
106162
109165 //===----------------------------------------------------------------------===//
110166 // Helper functions
111167 //===----------------------------------------------------------------------===//
112
113 /// DeleteDeadInstruction - Delete this instruction. Before we do, go through
114 /// and zero out all the operands of this instruction. If any of them become
115 /// dead, delete them and the computation tree that feeds them.
116 ///
117 /// If ValueSet is non-null, remove any deleted instructions from it as well.
118 ///
119 static void DeleteDeadInstruction(Instruction *I,
120 MemoryDependenceAnalysis &MD,
121 const TargetLibraryInfo &TLI,
122 SmallSetVector *ValueSet = nullptr) {
123 SmallVector NowDeadInsts;
124
125 NowDeadInsts.push_back(I);
126 --NumFastOther;
127
128 // Before we touch this instruction, remove it from memdep!
129 do {
130 Instruction *DeadInst = NowDeadInsts.pop_back_val();
131 ++NumFastOther;
132
133 // This instruction is dead, zap it, in stages. Start by removing it from
134 // MemDep, which needs to know the operands and needs it to be in the
135 // function.
136 MD.removeInstruction(DeadInst);
137
138 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
139 Value *Op = DeadInst->getOperand(op);
140 DeadInst->setOperand(op, nullptr);
141
142 // If this operand just became dead, add it to the NowDeadInsts list.
143 if (!Op->use_empty()) continue;
144
145 if (Instruction *OpI = dyn_cast(Op))
146 if (isInstructionTriviallyDead(OpI, &TLI))
147 NowDeadInsts.push_back(OpI);
148 }
149
150 DeadInst->eraseFromParent();
151
152 if (ValueSet) ValueSet->remove(DeadInst);
153 } while (!NowDeadInsts.empty());
154 }
155
156168
157169 /// hasMemoryWrite - Does this instruction write some memory? This only returns
158170 /// true for things that we can analyze with other helpers below.
526538
527539 MemDepResult InstDep = MD->getDependency(Inst);
528540
529 // Ignore any store where we can't find a local dependence.
530 // FIXME: cross-block DSE would be fun. :)
531 if (!InstDep.isDef() && !InstDep.isClobber())
541 if (!InstDep.isDef() && !InstDep.isClobber() && !InstDep.isNonLocal())
532542 continue;
543 if (InstDep.isNonLocal()) {
544 if (!PDT->getRootNode())
545 continue;
546 if (StoreInst *SI = dyn_cast(Inst))
547 handleNonLocalStoreDeletion(SI, BBI, BB);
548 continue;
549 }
533550
534551 // Figure out what location is being stored to.
535552 MemoryLocation Loc = getLocForWrite(Inst, *AA);
703720 }
704721 }
705722
723 /// DeleteDeadInstruction - Delete this instruction. Before we do, go through
724 /// and zero out all the operands of this instruction. If any of them become
725 /// dead, delete them and the computation tree that feeds them.
726 /// If ValueSet is non-null, remove any deleted instructions from it as well.
727 void DSE::DeleteDeadInstruction(Instruction *I, MemoryDependenceAnalysis &MD,
728 const TargetLibraryInfo &TLI,
729 SmallSetVector *ValueSet) {
730 SmallVector NowDeadInsts;
731
732 NowDeadInsts.push_back(I);
733 --NumFastOther;
734
735 // Before we touch this instruction, remove it from memdep!
736 do {
737 Instruction *DeadInst = NowDeadInsts.pop_back_val();
738 ++NumFastOther;
739 if (StoreInst *SI = dyn_cast(DeadInst))
740 DeadStores.insert(SI);
741
742 // This instruction is dead, zap it, in stages. Start by removing it from
743 // MemDep, which needs to know the operands and needs it to be in the
744 // function.
745 MD.removeInstruction(DeadInst);
746
747 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
748 Value *Op = DeadInst->getOperand(op);
749 DeadInst->setOperand(op, nullptr);
750
751 // If this operand just became dead, add it to the NowDeadInsts list.
752 if (!Op->use_empty())
753 continue;
754
755 if (Instruction *OpI = dyn_cast(Op))
756 if (isInstructionTriviallyDead(OpI, &TLI))
757 NowDeadInsts.push_back(OpI);
758 }
759
760 DeadInst->eraseFromParent();
761
762 if (ValueSet)
763 ValueSet->remove(DeadInst);
764 } while (!NowDeadInsts.empty());
765 }
766
706767 /// HandleFree - Handle frees of entire structures whose dependency is a store
707768 /// to a field of that structure.
708769 bool DSE::HandleFree(CallInst *F) {
930991 return !AA->isNoAlias(StackLoc, LoadedLoc);
931992 });
932993 }
994
995 /// isSafeCandidateForDeletion- Check all paths from the SrcBlock till
996 /// SinkBlock to see if Store 'SI' is safe to be remove.
997 /// Returns true if the candidate store SI is safe to delete
998 /// else returns false.
999 bool DSE::isSafeCandidateForDeletion(BasicBlock *SrcBlock,
1000 BasicBlock *SinkBlock, StoreInst *SI) {
1001 SmallVector WorkList;
1002 SmallPtrSet Visited;
1003 BasicBlock::iterator BBI(SI);
1004
1005 // Check from the store till end of block and make sure we have no references
1006 // to memory stored by this Store Instruction.
1007 for (auto BI = ++BBI, BE = SrcBlock->end(); BI != BE; ++BI) {
1008 Instruction *I = BI;
1009 StoreInst *CSI = dyn_cast(I);
1010 if (CSI) {
1011 AliasResult R =
1012 AA->alias(MemoryLocation::get(SI), MemoryLocation::get(CSI));
1013 if (R == MustAlias)
1014 return true;
1015 } else {
1016 ModRefInfo Res = AA->getModRefInfo(I, MemoryLocation::get(SI));
1017 if (Res != MRI_NoModRef)
1018 return false;
1019 }
1020 }
1021
1022 // Add successors of the block to stack and start DFS.
1023 for (succ_iterator I = succ_begin(SrcBlock), E = succ_end(SrcBlock); I != E;
1024 ++I) {
1025 if (!Visited.insert(*I).second)
1026 continue;
1027 // A path with backedge may not be safe. Conservatively mark
1028 // this store unsafe.
1029 if (BackEdgesMap.count(std::make_pair(SrcBlock, *I)))
1030 return false;
1031 WorkList.push_back(*I);
1032 }
1033
1034 while (!WorkList.empty()) {
1035 BasicBlock *B = WorkList.pop_back_val();
1036 auto BI = B->begin();
1037 auto BE = B->end();
1038 for (; BI != BE; ++BI) {
1039 Instruction *I = BI;
1040 StoreInst *CSI = dyn_cast(I);
1041 if (CSI) {
1042 AliasResult R =
1043 AA->alias(MemoryLocation::get(SI), MemoryLocation::get(CSI));
1044 if (R == MustAlias)
1045 break;
1046 } else {
1047 ModRefInfo Res = AA->getModRefInfo(I, MemoryLocation::get(SI));
1048 if (Res != MRI_NoModRef)
1049 return false;
1050 }
1051 }
1052
1053 // If we reached the sink node or we found a block which has a stores that
1054 // overwrites the candidate block we need not look at their successors.
1055 if (B == SinkBlock || BI != BE)
1056 continue;
1057
1058 for (succ_iterator I = succ_begin(B), E = succ_end(B); I != E; ++I) {
1059 if (!Visited.insert(*I).second)
1060 continue;
1061 // A path with backedge may not be safe.Conservatively mark
1062 // this store unsafe.
1063 if (BackEdgesMap.count(std::make_pair(B, *I)))
1064 return false;
1065 WorkList.push_back(*I);
1066 }
1067 }
1068
1069 return true;
1070 }
1071
1072 /// handleNonLocalStoreDeletion - Handle non local dead store elimination.
1073 /// This works by finding candidate stores using PDT and then running DFS
1074 /// from candidate store block checking all paths to make sure the store is
1075 /// safe to delete.
1076 void DSE::handleNonLocalStoreDeletion(StoreInst *SI, BasicBlock::iterator &BBI,
1077 BasicBlock &CurBlock) {
1078 BasicBlock *BB = SI->getParent();
1079 Value *Pointer = SI->getPointerOperand();
1080 DomTreeNode *DTNode = PDT->getNode(BB);
1081 if (!DTNode)
1082 return;
1083
1084 int DFSNumIn = DTNode->getDFSNumIn();
1085 int DFSNumOut = DTNode->getDFSNumOut();
1086 for (int i = DFSNumIn + 1; i < DFSNumOut; ++i) {
1087 for (auto &I : Candidates[i]) {
1088 StoreInst *CandidateSI = I;
1089 if (DeadStores.count(CandidateSI))
1090 continue;
1091 Value *MemPtr = CandidateSI->getPointerOperand();
1092 if (!MemPtr)
1093 continue;
1094 if (Pointer->getType() != MemPtr->getType())
1095 continue;
1096 AliasResult R =
1097 AA->alias(MemoryLocation::get(SI), MemoryLocation::get(CandidateSI));
1098 if (R != MustAlias)
1099 continue;
1100 if (isSafeCandidateForDeletion(CandidateSI->getParent(), BB,
1101 CandidateSI)) {
1102 DeleteDeadInstruction(CandidateSI, *MD, *TLI);
1103 ++NumCrossBlockStores;
1104 // DeleteDeadInstruction can delete the current instruction in loop
1105 // cases, reset BBI.
1106 BBI = SI;
1107 if (BBI != CurBlock.begin())
1108 --BBI;
1109 }
1110 }
1111 }
1112 }
0 ; RUN: opt < %s -basicaa -dse -S | FileCheck %s
1 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2
3 @x = common global i32 0
4 @y = common global i32 0
5 @a = external global i32
6 @b = external global i32
7
8 define void @test_01(i32 %N) {
9 %1 = alloca i32
10 store i32 %N, i32* %1
11 store i32 10, i32* @x
12 %2 = load i32, i32* %1
13 %3 = icmp ne i32 %2, 0
14 br i1 %3, label %4, label %5
15
16 ;
17 store i32 5, i32* @x
18 br label %5
19
20 ;
21 store i32 15, i32* @x
22 ret void
23 }
24 ; CHECK-LABEL: @test_01(
25 ; CHECK-NOT: store i32 10, i32* @x
26 ; CHECK-NOT: store i32 5, i32* @x
27 ; CHECK: store i32 15, i32* @x
28
29
30 define void @test_02(i32 %N) {
31 %1 = alloca i32
32 store i32 %N, i32* %1
33 store i32 10, i32* @x
34 %2 = load i32, i32* %1
35 %3 = icmp ne i32 %2, 0
36 br i1 %3, label %4, label %5
37
38 ;
39 store i32 5, i32* @x
40 br label %7
41
42 ;
43 %6 = load i32, i32* @x
44 store i32 %6, i32* @y
45 br label %7
46
47 ;
48 store i32 15, i32* @x
49 ret void
50 }
51 ; CHECK-LABEL: @test_02(
52 ; CHECK: store i32 10, i32* @x
53 ; CHECK-NOT: store i32 5, i32* @x
54 ; CHECK: store i32 %6, i32* @y
55
56
57 define void @test_03(i32 %N) #0 {
58 %1 = alloca i32
59 store i32 %N, i32* %1
60 store i32 10, i32* @x
61 %2 = load i32, i32* %1
62 %3 = icmp ne i32 %2, 0
63 br i1 %3, label %4, label %6
64
65 ;
66 %5 = load i32, i32* @x
67 store i32 %5, i32* @y
68 br label %6
69
70 ;
71 store i32 15, i32* @x
72 ret void
73 }
74 ; CHECK-LABEL: @test_03(
75 ; CHECK: store i32 10, i32* @x
76 ; CHECK: store i32 %5, i32* @y
77 ; CHECK: store i32 15, i32* @x
78
79
80
81 ; Check we safely delete store i32 %g.02, i32* @b below.(PR24469)
82 define void @test_04() {
83 entry:
84 br i1 false, label %for.body, label %for.end.9
85
86 for.cond.1.loopexit: ; preds = %for.body
87 store i32 0, i32* @b
88 %0 = load i32, i32* @a
89 br i1 false, label %for.body, label %for.end.9
90
91 for.body: ; preds = %for.cond.1.loopexit, %entry
92 %g.02 = phi i32 [ undef, %entry ], [ %0, %for.cond.1.loopexit ]
93 store i32 %g.02, i32* @b
94 br label %for.cond.1.loopexit
95
96 for.end.9: ; preds = %for.cond.1.loopexit, %entry
97 ret void
98 }
99
100 ; CHECK-LABEL: @test_04(
101 ; CHECK-NOT: store i32 %g.02, i32* @b
102 ; CHECK-NOT: %g.02 = phi i32 [ undef, %entry ], [ %0, %for.cond.1.loopexit ]
103 ; CHECK-NOT: %0 = load i32, i32* @a
0 ; RUN: opt < %s -basicaa -dse -S | FileCheck %s
1 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2
3 @A = common global [100 x i32] zeroinitializer, align 16
4 @x = common global i32 0
5
6 ; Negative Test case-
7 ;void foo(int N) {
8 ; A[0] = N;
9 ; for(int i=0;i
10 ; A[i]+=i;
11 ; A[0] = 10;
12 ;}
13 ;; Stores should not be optimized away.
14
15 define void @test_01(i32 %N) #0 {
16 %1 = alloca i32
17 %i = alloca i32
18 store i32 %N, i32* %1
19 %2 = load i32, i32* %1
20 store i32 %2, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
21 store i32 0, i32* %i
22 br label %3
23
24 ;
25 %4 = load i32, i32* %i
26 %5 = load i32, i32* %1
27 %6 = icmp slt i32 %4, %5
28 br i1 %6, label %7, label %17
29
30 ;
31 %8 = load i32, i32* %i
32 %9 = load i32, i32* %i
33 %10 = sext i32 %9 to i64
34 %11 = getelementptr inbounds [100 x i32], [100 x i32]* @A, i32 0, i64 %10
35 %12 = load i32, i32* %11
36 %13 = add nsw i32 %12, %8
37 store i32 %13, i32* %11
38 br label %14
39
40 ;
41 %15 = load i32, i32* %i
42 %16 = add nsw i32 %15, 1
43 store i32 %16, i32* %i
44 br label %3
45
46 ;
47 store i32 10, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
48 ret void
49 }
50 ; CHECK-LABEL: @test_01(
51 ; CHECK: store i32 %2, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
52 ; CHECK: store i32 %13, i32* %11
53 ; CHECK: store i32 10, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
54
55
56 ; Postive Test case-
57 ;void foo(int N) {
58 ; A[0] = N;
59 ; for(int i=0;i
60 ; A[i]=i;
61 ; A[0] = 10;
62 ;}
63 ;; Stores should not be optimized away.
64 define void @test_02(i32 %N) #0 {
65 %1 = alloca i32
66 %i = alloca i32
67 store i32 %N, i32* %1
68 %2 = load i32, i32* %1
69 store i32 %2, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
70 store i32 0, i32* %i
71 br label %3
72
73 ;
74 %4 = load i32, i32* %i
75 %5 = load i32, i32* %1
76 %6 = icmp slt i32 %4, %5
77 br i1 %6, label %7, label %15
78
79 ;
80 %8 = load i32, i32* %i
81 %9 = load i32, i32* %i
82 %10 = sext i32 %9 to i64
83 %11 = getelementptr inbounds [100 x i32], [100 x i32]* @A, i32 0, i64 %10
84 store i32 %8, i32* %11
85 br label %12
86
87 ;
88 %13 = load i32, i32* %i
89 %14 = add nsw i32 %13, 1
90 store i32 %14, i32* %i
91 br label %3
92
93 ;
94 store i32 10, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
95 ret void
96 }
97
98 ; CHECK-LABEL: @test_02(
99 ; CHECK-NOT: store i32 %2, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
100 ; CHECK: store i32 %7, i32* %10
101 ; CHECK: store i32 10, i32* getelementptr inbounds ([100 x i32], [100 x i32]* @A, i32 0, i64 0)
102
103