llvm.org GIT mirror llvm / 241f653
Make GVN able to remove unnecessary calls to read-only functions again. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49842 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
4 changed file(s) with 64 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
133133 } else if (isa(QI)) {
134134 AliasAnalysis::ModRefBehavior result =
135135 AA.getModRefBehavior(CallSite::get(QI));
136 if (result != AliasAnalysis::DoesNotAccessMemory &&
137 result != AliasAnalysis::OnlyReadsMemory) {
136 if (result != AliasAnalysis::DoesNotAccessMemory) {
138137 if (!start && !block) {
139138 cachedResult.first = QI;
140139 cachedResult.second = true;
134134 DenseMap valueNumbering;
135135 DenseMap expressionNumbering;
136136 AliasAnalysis* AA;
137 MemoryDependenceAnalysis* MD;
137138
138139 uint32_t nextValueNumber;
139140
158159 void erase(Value* v);
159160 unsigned size();
160161 void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
162 void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
161163 };
162164 }
163165
431433
432434 return nextValueNumber++;
433435 }
436 } else if (AA->onlyReadsMemory(C)) {
437 Expression e = create_expression(C);
438
439 Instruction* dep = MD->getDependency(C);
440
441 if (dep == MemoryDependenceAnalysis::NonLocal ||
442 !isa(dep)) {
443 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
444 valueNumbering.insert(std::make_pair(V, nextValueNumber));
445
446 return nextValueNumber++;
447 }
448
449 CallInst* cdep = cast(dep);
450 Expression d_exp = create_expression(cdep);
451
452 if (e != d_exp) {
453 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
454 valueNumbering.insert(std::make_pair(V, nextValueNumber));
455
456 return nextValueNumber++;
457 } else {
458 uint32_t v = expressionNumbering[d_exp];
459 valueNumbering.insert(std::make_pair(V, v));
460 return v;
461 }
462
434463 } else {
435464 valueNumbering.insert(std::make_pair(V, nextValueNumber));
436465 return nextValueNumber++;
9921021 } else if (currAvail.test(num)) {
9931022 Value* repl = find_leader(currAvail, num);
9941023
995 if (CallInst* CI = dyn_cast(I)) {
996 AliasAnalysis& AA = getAnalysis();
997 if (!AA.doesNotAccessMemory(CI)) {
998 MemoryDependenceAnalysis& MD = getAnalysis();
999 if (cast(repl)->getParent() != CI->getParent() ||
1000 MD.getDependency(CI) != MD.getDependency(cast(repl))) {
1001 // There must be an intervening may-alias store, so nothing from
1002 // this point on will be able to be replaced with the preceding call
1003 currAvail.erase(repl);
1004 currAvail.insert(I);
1005
1006 return false;
1007 }
1008 }
1009 }
1010
10111024 // Remove it!
10121025 MemoryDependenceAnalysis& MD = getAnalysis();
10131026 MD.removeInstruction(I);
10291042 //
10301043 bool GVN::runOnFunction(Function& F) {
10311044 VN.setAliasAnalysis(&getAnalysis());
1045 VN.setMemDep(&getAnalysis());
10321046
10331047 bool changed = false;
10341048 bool shouldContinue = true;
+0
-33
test/Analysis/BasicAA/const-dce.ll less more
None ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestConst | count 2
1 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestPure | count 4
2 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestNone | count 4
3 @g = global i32 0 ; [#uses=1]
4
5 define i32 @test() {
6 entry:
7 %tmp0 = call i32 @TestConst( i32 5 ) readnone ; [#uses=1]
8 %tmp1 = call i32 @TestPure( i32 6 ) readonly ; [#uses=1]
9 %tmp2 = call i32 @TestNone( i32 7 ) ; [#uses=1]
10 store i32 1, i32* @g
11 %tmp3 = call i32 @TestConst( i32 5 ) readnone ; [#uses=1]
12 %tmp4 = call i32 @TestConst( i32 5 ) readnone ; [#uses=1]
13 %tmp5 = call i32 @TestPure( i32 6 ) readonly ; [#uses=1]
14 %tmp6 = call i32 @TestPure( i32 6 ) readonly ; [#uses=1]
15 %tmp7 = call i32 @TestNone( i32 7 ) ; [#uses=1]
16 %tmp8 = call i32 @TestNone( i32 7 ) ; [#uses=1]
17 %sum0 = add i32 %tmp0, %tmp1 ; [#uses=1]
18 %sum1 = add i32 %sum0, %tmp2 ; [#uses=1]
19 %sum2 = add i32 %sum1, %tmp3 ; [#uses=1]
20 %sum3 = add i32 %sum2, %tmp4 ; [#uses=1]
21 %sum4 = add i32 %sum3, %tmp5 ; [#uses=1]
22 %sum5 = add i32 %sum4, %tmp6 ; [#uses=1]
23 %sum6 = add i32 %sum5, %tmp7 ; [#uses=1]
24 %sum7 = add i32 %sum6, %tmp8 ; [#uses=1]
25 ret i32 %sum7
26 }
27
28 declare i32 @TestConst(i32) readnone
29
30 declare i32 @TestPure(i32) readonly
31
32 declare i32 @TestNone(i32)
0 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestConst | count 2
1 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestPure | count 3
2 ; RUN: llvm-as < %s | opt -basicaa -gvn | llvm-dis | grep TestNone | count 4
3 @g = global i32 0 ; [#uses=1]
4
5 define i32 @test() {
6 entry:
7 %tmp0 = call i32 @TestConst( i32 5 ) readnone ; [#uses=1]
8 %tmp1 = call i32 @TestPure( i32 6 ) readonly ; [#uses=1]
9 %tmp2 = call i32 @TestNone( i32 7 ) ; [#uses=1]
10 store i32 1, i32* @g
11 %tmp3 = call i32 @TestConst( i32 5 ) readnone ; [#uses=1]
12 %tmp4 = call i32 @TestConst( i32 5 ) readnone ; [#uses=1]
13 %tmp5 = call i32 @TestPure( i32 6 ) readonly ; [#uses=1]
14 %tmp6 = call i32 @TestPure( i32 6 ) readonly ; [#uses=1]
15 %tmp7 = call i32 @TestNone( i32 7 ) ; [#uses=1]
16 %tmp8 = call i32 @TestNone( i32 7 ) ; [#uses=1]
17 %sum0 = add i32 %tmp0, %tmp1 ; [#uses=1]
18 %sum1 = add i32 %sum0, %tmp2 ; [#uses=1]
19 %sum2 = add i32 %sum1, %tmp3 ; [#uses=1]
20 %sum3 = add i32 %sum2, %tmp4 ; [#uses=1]
21 %sum4 = add i32 %sum3, %tmp5 ; [#uses=1]
22 %sum5 = add i32 %sum4, %tmp6 ; [#uses=1]
23 %sum6 = add i32 %sum5, %tmp7 ; [#uses=1]
24 %sum7 = add i32 %sum6, %tmp8 ; [#uses=1]
25 ret i32 %sum7
26 }
27
28 declare i32 @TestConst(i32) readnone
29
30 declare i32 @TestPure(i32) readonly
31
32 declare i32 @TestNone(i32)