llvm.org GIT mirror llvm / 6665b0e
Teach BasicAA how to analyze Select instructions, and make it more aggressive on PHI instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85158 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 149 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
216216
217217 private:
218218 // VisitedPHIs - Track PHI nodes visited by a aliasCheck() call.
219 SmallPtrSetPHINode*, 16> VisitedPHIs;
219 SmallPtrSetValue*, 16> VisitedPHIs;
220220
221221 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
222222 // against another.
227227 // against another.
228228 AliasResult aliasPHI(const PHINode *PN, unsigned PNSize,
229229 const Value *V2, unsigned V2Size);
230
231 /// aliasSelect - Disambiguate a Select instruction against another value.
232 AliasResult aliasSelect(const SelectInst *SI, unsigned SISize,
233 const Value *V2, unsigned V2Size);
230234
231235 AliasResult aliasCheck(const Value *V1, unsigned V1Size,
232236 const Value *V2, unsigned V2Size);
518522 return MayAlias;
519523 }
520524
525 // aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select instruction
526 // against another.
527 AliasAnalysis::AliasResult
528 BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize,
529 const Value *V2, unsigned V2Size) {
530 // If the values are Selects with the same condition, we can do a more precise
531 // check: just check for aliases between the values on corresponding arms.
532 if (const SelectInst *SI2 = dyn_cast(V2))
533 if (SI->getCondition() == SI2->getCondition()) {
534 AliasResult Alias =
535 aliasCheck(SI->getTrueValue(), SISize,
536 SI2->getTrueValue(), V2Size);
537 if (Alias == MayAlias)
538 return MayAlias;
539 AliasResult ThisAlias =
540 aliasCheck(SI->getFalseValue(), SISize,
541 SI2->getFalseValue(), V2Size);
542 if (ThisAlias != Alias)
543 return MayAlias;
544 return Alias;
545 }
546
547 // If both arms of the Select node NoAlias or MustAlias V2, then returns
548 // NoAlias / MustAlias. Otherwise, returns MayAlias.
549 AliasResult Alias =
550 aliasCheck(SI->getTrueValue(), SISize, V2, V2Size);
551 if (Alias == MayAlias)
552 return MayAlias;
553 AliasResult ThisAlias =
554 aliasCheck(SI->getFalseValue(), SISize, V2, V2Size);
555 if (ThisAlias != Alias)
556 return MayAlias;
557 return Alias;
558 }
559
521560 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
522561 // against another.
523562 AliasAnalysis::AliasResult
526565 // The PHI node has already been visited, avoid recursion any further.
527566 if (!VisitedPHIs.insert(PN))
528567 return MayAlias;
568
569 // If the values are PHIs in the same block, we can do a more precise
570 // as well as efficient check: just check for aliases between the values
571 // on corresponding edges.
572 if (const PHINode *PN2 = dyn_cast(V2))
573 if (PN2->getParent() == PN->getParent()) {
574 AliasResult Alias =
575 aliasCheck(PN->getIncomingValue(0), PNSize,
576 PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)),
577 V2Size);
578 if (Alias == MayAlias)
579 return MayAlias;
580 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
581 AliasResult ThisAlias =
582 aliasCheck(PN->getIncomingValue(i), PNSize,
583 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
584 V2Size);
585 if (ThisAlias != Alias)
586 return MayAlias;
587 }
588 return Alias;
589 }
529590
530591 SmallPtrSet UniqueSrc;
531592 SmallVector V1Srcs;
541602 V1Srcs.push_back(PV1);
542603 }
543604
544 AliasResult Alias = aliasCheck(V1Srcs[0], PNSize, V2, V2Size);
605 AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize);
545606 // Early exit if the check of the first PHI source against V2 is MayAlias.
546607 // Other results are not possible.
547608 if (Alias == MayAlias)
551612 // NoAlias / MustAlias. Otherwise, returns MayAlias.
552613 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
553614 Value *V = V1Srcs[i];
615
616 // If V2 is a PHI, the recursive case will have been caught in the
617 // above aliasCheck call, so these subsequent calls to aliasCheck
618 // don't need to assume that V2 is being visited recursively.
619 VisitedPHIs.erase(V2);
620
554621 AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize);
555622 if (ThisAlias != Alias || ThisAlias == MayAlias)
556623 return MayAlias;
626693 }
627694 if (const PHINode *PN = dyn_cast(V1))
628695 return aliasPHI(PN, V1Size, V2, V2Size);
696
697 if (isa(V2) && !isa(V1)) {
698 std::swap(V1, V2);
699 std::swap(V1Size, V2Size);
700 }
701 if (const SelectInst *S1 = dyn_cast(V1))
702 return aliasSelect(S1, V1Size, V2, V2Size);
629703
630704 return MayAlias;
631705 }
0 ; RUN: opt < %s -aa-eval -print-all-alias-modref-info -disable-output \
1 ; RUN: |& grep {NoAlias: double\\* \[%\]a, double\\* \[%\]b\$} | count 4
2
3 ; BasicAA should detect NoAliases in PHIs and Selects.
4
5 ; Two PHIs in the same block.
6 define void @foo(i1 %m, double* noalias %x, double* noalias %y) {
7 entry:
8 br i1 %m, label %true, label %false
9
10 true:
11 br label %exit
12
13 false:
14 br label %exit
15
16 exit:
17 %a = phi double* [ %x, %true ], [ %y, %false ]
18 %b = phi double* [ %x, %false ], [ %y, %true ]
19 volatile store double 0.0, double* %a
20 volatile store double 1.0, double* %b
21 ret void
22 }
23
24 ; Two selects with the same condition.
25 define void @bar(i1 %m, double* noalias %x, double* noalias %y) {
26 entry:
27 %a = select i1 %m, double* %x, double* %y
28 %b = select i1 %m, double* %y, double* %x
29 volatile store double 0.000000e+00, double* %a
30 volatile store double 1.000000e+00, double* %b
31 ret void
32 }
33
34 ; Two PHIs with disjoint sets of inputs.
35 define void @qux(i1 %m, double* noalias %x, double* noalias %y,
36 i1 %n, double* noalias %v, double* noalias %w) {
37 entry:
38 br i1 %m, label %true, label %false
39
40 true:
41 br label %exit
42
43 false:
44 br label %exit
45
46 exit:
47 %a = phi double* [ %x, %true ], [ %y, %false ]
48 br i1 %n, label %ntrue, label %nfalse
49
50 ntrue:
51 br label %nexit
52
53 nfalse:
54 br label %nexit
55
56 nexit:
57 %b = phi double* [ %v, %ntrue ], [ %w, %nfalse ]
58 volatile store double 0.0, double* %a
59 volatile store double 1.0, double* %b
60 ret void
61 }
62
63 ; Two selects with disjoint sets of arms.
64 define void @fin(i1 %m, double* noalias %x, double* noalias %y,
65 i1 %n, double* noalias %v, double* noalias %w) {
66 entry:
67 %a = select i1 %m, double* %x, double* %y
68 %b = select i1 %n, double* %v, double* %w
69 volatile store double 0.000000e+00, double* %a
70 volatile store double 1.000000e+00, double* %b
71 ret void
72 }