llvm.org GIT mirror llvm / a0a7755
Revert "Add support for cross block dse. This patch enables dead stroe elimination across basicblocks." This reverts commit r245025, it caused PR24469. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@245172 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 4 years ago
3 changed file(s) with 51 addition(s) and 404 deletion(s). Raw diff Collapse all Expand all
1515 //===----------------------------------------------------------------------===//
1616
1717 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/ADT/DenseSet.h"
1918 #include "llvm/ADT/STLExtras.h"
2019 #include "llvm/ADT/SetVector.h"
2120 #include "llvm/ADT/Statistic.h"
2221 #include "llvm/Analysis/AliasAnalysis.h"
2322 #include "llvm/Analysis/CaptureTracking.h"
24 #include "llvm/Analysis/CFG.h"
2523 #include "llvm/Analysis/MemoryBuiltins.h"
2624 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
27 #include "llvm/Analysis/PostDominators.h"
2825 #include "llvm/Analysis/TargetLibraryInfo.h"
2926 #include "llvm/Analysis/ValueTracking.h"
3027 #include "llvm/IR/Constants.h"
4441
4542 STATISTIC(NumRedundantStores, "Number of redundant stores deleted");
4643 STATISTIC(NumFastStores, "Number of stores deleted");
47 STATISTIC(NumCrossBlockStores, "Number of cross block stores deleted");
4844 STATISTIC(NumFastOther , "Number of other instrs removed");
4945
5046 namespace {
5248 AliasAnalysis *AA;
5349 MemoryDependenceAnalysis *MD;
5450 DominatorTree *DT;
55 PostDominatorTree *PDT;
5651 const TargetLibraryInfo *TLI;
57 SmallVector, 16> Candidates;
58 SetVector DeadStores;
59 SmallVector, 32>
60 BackEdges;
61 DenseSet> BackEdgesMap;
52
6253 static char ID; // Pass identification, replacement for typeid
63 DSE()
64 : FunctionPass(ID), AA(nullptr), MD(nullptr), DT(nullptr),
65 PDT(nullptr) {
54 DSE() : FunctionPass(ID), AA(nullptr), MD(nullptr), DT(nullptr) {
6655 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 }
8956 }
9057
9158 bool runOnFunction(Function &F) override {
9663 MD = &getAnalysis();
9764 DT = &getAnalysis().getDomTree();
9865 TLI = &getAnalysis().getTLI();
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 }
66
11467 bool Changed = false;
11568 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
11669 // Only check non-dead blocks. Dead blocks may have strange pointer
12982 void RemoveAccessedObjects(const MemoryLocation &LoadedLoc,
13083 SmallSetVector &DeadStackObjects,
13184 const DataLayout &DL);
132 void handleNonLocalStoreDeletion(StoreInst *SI);
133 bool isSafeCandidateForDeletion(BasicBlock *SrcBlock, BasicBlock *SinkBlock,
134 StoreInst *SI);
135 void DeleteDeadInstruction(Instruction *I, MemoryDependenceAnalysis &MD,
136 const TargetLibraryInfo &TLI,
137 SmallSetVector *ValueSet = nullptr);
85
13886 void getAnalysisUsage(AnalysisUsage &AU) const override {
13987 AU.setPreservesCFG();
14088 AU.addRequired();
14189 AU.addRequired();
14290 AU.addRequired();
143 AU.addRequired();
14491 AU.addRequired();
14592 AU.addPreserved();
14693 AU.addPreserved();
14794 AU.addPreserved();
148 AU.addPreserved();
14995 }
15096 };
15197 }
155101 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
156102 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
157103 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
158 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
159104 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
160105 INITIALIZE_PASS_END(DSE, "dse", "Dead Store Elimination", false, false)
161106
164109 //===----------------------------------------------------------------------===//
165110 // Helper functions
166111 //===----------------------------------------------------------------------===//
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
167156
168157 /// hasMemoryWrite - Does this instruction write some memory? This only returns
169158 /// true for things that we can analyze with other helpers below.
537526
538527 MemDepResult InstDep = MD->getDependency(Inst);
539528
540 if (!InstDep.isDef() && !InstDep.isClobber() && !InstDep.isNonLocal())
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())
541532 continue;
542 if (InstDep.isNonLocal()) {
543 if (!PDT->getRootNode())
544 continue;
545 if (StoreInst *SI = dyn_cast(Inst))
546 handleNonLocalStoreDeletion(SI);
547 continue;
548 }
549533
550534 // Figure out what location is being stored to.
551535 MemoryLocation Loc = getLocForWrite(Inst, *AA);
719703 }
720704 }
721705
722 /// DeleteDeadInstruction - Delete this instruction. Before we do, go through
723 /// and zero out all the operands of this instruction. If any of them become
724 /// dead, delete them and the computation tree that feeds them.
725 /// If ValueSet is non-null, remove any deleted instructions from it as well.
726 void DSE::DeleteDeadInstruction(Instruction *I, MemoryDependenceAnalysis &MD,
727 const TargetLibraryInfo &TLI,
728 SmallSetVector *ValueSet) {
729 SmallVector NowDeadInsts;
730
731 NowDeadInsts.push_back(I);
732 --NumFastOther;
733
734 // Before we touch this instruction, remove it from memdep!
735 do {
736 Instruction *DeadInst = NowDeadInsts.pop_back_val();
737 ++NumFastOther;
738 if (StoreInst *SI = dyn_cast(DeadInst))
739 DeadStores.insert(SI);
740
741 // This instruction is dead, zap it, in stages. Start by removing it from
742 // MemDep, which needs to know the operands and needs it to be in the
743 // function.
744 MD.removeInstruction(DeadInst);
745
746 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
747 Value *Op = DeadInst->getOperand(op);
748 DeadInst->setOperand(op, nullptr);
749
750 // If this operand just became dead, add it to the NowDeadInsts list.
751 if (!Op->use_empty())
752 continue;
753
754 if (Instruction *OpI = dyn_cast(Op))
755 if (isInstructionTriviallyDead(OpI, &TLI))
756 NowDeadInsts.push_back(OpI);
757 }
758
759 DeadInst->eraseFromParent();
760
761 if (ValueSet)
762 ValueSet->remove(DeadInst);
763 } while (!NowDeadInsts.empty());
764 }
765
766706 /// HandleFree - Handle frees of entire structures whose dependency is a store
767707 /// to a field of that structure.
768708 bool DSE::HandleFree(CallInst *F) {
990930 return !AA->isNoAlias(StackLoc, LoadedLoc);
991931 });
992932 }
993
994 /// isSafeCandidateForDeletion- Check all paths from the SrcBlock till
995 /// SinkBlock to see if Store 'SI' is safe to be remove.
996 /// Returns true if the candidate store SI is safe to delete
997 /// else returns false.
998 bool DSE::isSafeCandidateForDeletion(BasicBlock *SrcBlock,
999 BasicBlock *SinkBlock, StoreInst *SI) {
1000 SmallVector WorkList;
1001 SmallPtrSet Visited;
1002 BasicBlock::iterator BBI(SI);
1003
1004 // Check from the store till end of block and make sure we have no references
1005 // to memory stored by this Store Instruction.
1006 for (auto BI = ++BBI, BE = SrcBlock->end(); BI != BE; ++BI) {
1007 Instruction *I = BI;
1008 StoreInst *CSI = dyn_cast(I);
1009 if (CSI) {
1010 AliasResult R =
1011 AA->alias(MemoryLocation::get(SI), MemoryLocation::get(CSI));
1012 if (R == MustAlias)
1013 return true;
1014 } else {
1015 ModRefInfo Res = AA->getModRefInfo(I, MemoryLocation::get(SI));
1016 if (Res != MRI_NoModRef)
1017 return false;
1018 }
1019 }
1020
1021 // Add successors of the block to stack and start DFS.
1022 for (succ_iterator I = succ_begin(SrcBlock), E = succ_end(SrcBlock); I != E;
1023 ++I) {
1024 if (!Visited.insert(*I).second)
1025 continue;
1026 // A path with backedge may not be safe. Conservatively mark
1027 // this store unsafe.
1028 if (BackEdgesMap.count(std::make_pair(SrcBlock, *I)))
1029 return false;
1030 WorkList.push_back(*I);
1031 }
1032
1033 while (!WorkList.empty()) {
1034 BasicBlock *B = WorkList.pop_back_val();
1035 auto BI = B->begin();
1036 auto BE = B->end();
1037 for (; BI != BE; ++BI) {
1038 Instruction *I = BI;
1039 StoreInst *CSI = dyn_cast(I);
1040 if (CSI) {
1041 AliasResult R =
1042 AA->alias(MemoryLocation::get(SI), MemoryLocation::get(CSI));
1043 if (R == MustAlias)
1044 break;
1045 } else {
1046 ModRefInfo Res = AA->getModRefInfo(I, MemoryLocation::get(SI));
1047 if (Res != MRI_NoModRef)
1048 return false;
1049 }
1050 }
1051
1052 // If we reached the sink node or we found a block which has a stores that
1053 // overwrites the candidate block we need not look at their successors.
1054 if (B == SinkBlock || BI != BE)
1055 continue;
1056
1057 for (succ_iterator I = succ_begin(B), E = succ_end(B); I != E; ++I) {
1058 if (!Visited.insert(*I).second)
1059 continue;
1060 // A path with backedge may not be safe.Conservatively mark
1061 // this store unsafe.
1062 if (BackEdgesMap.count(std::make_pair(B, *I)))
1063 return false;
1064 WorkList.push_back(*I);
1065 }
1066 }
1067
1068 return true;
1069 }
1070
1071 /// handleNonLocalStoreDeletion - Handle non local dead store elimination.
1072 /// This works by finding candidate stores using PDT and then running DFS
1073 /// from candidate store block checking all paths to make sure the store is
1074 /// safe to delete.
1075 void DSE::handleNonLocalStoreDeletion(StoreInst *SI) {
1076 BasicBlock *BB = SI->getParent();
1077 Value *Pointer = SI->getPointerOperand();
1078 DomTreeNode *DTNode = PDT->getNode(BB);
1079 if (!DTNode)
1080 return;
1081
1082 int DFSNumIn = DTNode->getDFSNumIn();
1083 int DFSNumOut = DTNode->getDFSNumOut();
1084 for (int i = DFSNumIn + 1; i < DFSNumOut; ++i) {
1085 for (auto &I : Candidates[i]) {
1086 StoreInst *CandidateSI = I;
1087 if (DeadStores.count(CandidateSI))
1088 continue;
1089 Value *MemPtr = CandidateSI->getPointerOperand();
1090 if (!MemPtr)
1091 continue;
1092 if (Pointer->getType() != MemPtr->getType())
1093 continue;
1094 AliasResult R =
1095 AA->alias(MemoryLocation::get(SI), MemoryLocation::get(CandidateSI));
1096 if (R != MustAlias)
1097 continue;
1098 if (isSafeCandidateForDeletion(CandidateSI->getParent(), BB,
1099 CandidateSI)) {
1100 DeleteDeadInstruction(CandidateSI, *MD, *TLI);
1101 ++NumCrossBlockStores;
1102 }
1103 }
1104 }
1105 }
+0
-76
test/Transforms/DeadStoreElimination/cross_block_dse.ll less more
None ; 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
6 define void @test_01(i32 %N) {
7 %1 = alloca i32
8 store i32 %N, i32* %1
9 store i32 10, i32* @x
10 %2 = load i32, i32* %1
11 %3 = icmp ne i32 %2, 0
12 br i1 %3, label %4, label %5
13
14 ;
15 store i32 5, i32* @x
16 br label %5
17
18 ;
19 store i32 15, i32* @x
20 ret void
21 }
22 ; CHECK-LABEL: @test_01(
23 ; CHECK-NOT: store i32 10, i32* @x
24 ; CHECK-NOT: store i32 5, i32* @x
25 ; CHECK: store i32 15, i32* @x
26
27
28 define void @test_02(i32 %N) {
29 %1 = alloca i32
30 store i32 %N, i32* %1
31 store i32 10, i32* @x
32 %2 = load i32, i32* %1
33 %3 = icmp ne i32 %2, 0
34 br i1 %3, label %4, label %5
35
36 ;
37 store i32 5, i32* @x
38 br label %7
39
40 ;
41 %6 = load i32, i32* @x
42 store i32 %6, i32* @y
43 br label %7
44
45 ;
46 store i32 15, i32* @x
47 ret void
48 }
49 ; CHECK-LABEL: @test_02(
50 ; CHECK: store i32 10, i32* @x
51 ; CHECK-NOT: store i32 5, i32* @x
52 ; CHECK: store i32 %6, i32* @y
53
54
55 define void @test_03(i32 %N) #0 {
56 %1 = alloca i32
57 store i32 %N, i32* %1
58 store i32 10, i32* @x
59 %2 = load i32, i32* %1
60 %3 = icmp ne i32 %2, 0
61 br i1 %3, label %4, label %6
62
63 ;
64 %5 = load i32, i32* @x
65 store i32 %5, i32* @y
66 br label %6
67
68 ;
69 store i32 15, i32* @x
70 ret void
71 }
72 ; CHECK-LABEL: @test_03(
73 ; CHECK: store i32 10, i32* @x
74 ; CHECK: store i32 %5, i32* @y
75 ; CHECK: store i32 15, i32* @x
+0
-104
test/Transforms/DeadStoreElimination/cross_block_dse_loop.ll less more
None ; 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