llvm.org GIT mirror llvm / 2b47592
Optimistically analyse Phi cycles Analyse Phis under the starting assumption that they are NoAlias. Recursively look at their inputs. If they MayAlias/MustAlias there must be an input that makes them so. Addresses bug 14351. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169788 91177308-0d34-0410-b5e6-96231b3b80d8 Arnold Schwaighofer 7 years ago
2 changed file(s) with 76 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
10631063 Location(V2, V2Size, V2TBAAInfo));
10641064 if (PN > V2)
10651065 std::swap(Locs.first, Locs.second);
1066
1067 // Find the first incoming phi value not from its parent.
1068 unsigned f = 0;
1069 while (PN->getIncomingBlock(f) == PN->getParent() &&
1070 f < PN->getNumIncomingValues()-1)
1071 ++f;
1072
1073 AliasResult Alias =
1074 aliasCheck(PN->getIncomingValue(f), PNSize, PNTBAAInfo,
1075 PN2->getIncomingValueForBlock(PN->getIncomingBlock(f)),
1076 V2Size, V2TBAAInfo);
1077 if (Alias == MayAlias)
1078 return MayAlias;
1079
1080 // If the first source of the PHI nodes NoAlias and the other inputs are
1081 // the PHI node itself through some amount of recursion this does not add
1082 // any new information so just return NoAlias.
1083 // bb:
1084 // ptr = ptr2 + 1
1085 // loop:
1086 // ptr_phi = phi [bb, ptr], [loop, ptr_plus_one]
1087 // ptr2_phi = phi [bb, ptr2], [loop, ptr2_plus_one]
1088 // ...
1089 // ptr_plus_one = gep ptr_phi, 1
1090 // ptr2_plus_one = gep ptr2_phi, 1
1091 // We assume for the recursion that the the phis (ptr_phi, ptr2_phi) do
1092 // not alias each other.
1093 bool ArePhisAssumedNoAlias = false;
1094 AliasResult OrigAliasResult = NoAlias;
1095 if (Alias == NoAlias) {
1096 // Pretend the phis do not alias.
1097 assert(AliasCache.count(Locs) &&
1098 "There must exist an entry for the phi node");
1099 OrigAliasResult = AliasCache[Locs];
1100 AliasCache[Locs] = NoAlias;
1101 ArePhisAssumedNoAlias = true;
1102 }
1066 // Analyse the PHIs' inputs under the assumption that the PHIs are
1067 // NoAlias.
1068 // If the PHIs are May/MustAlias there must be (recursively) an input
1069 // operand from outside the PHIs' cycle that is MayAlias/MustAlias or
1070 // there must be an operation on the PHIs within the PHIs' value cycle
1071 // that causes a MayAlias.
1072 // Pretend the phis do not alias.
1073 AliasResult Alias = NoAlias;
1074 assert(AliasCache.count(Locs) &&
1075 "There must exist an entry for the phi node");
1076 AliasResult OrigAliasResult = AliasCache[Locs];
1077 AliasCache[Locs] = NoAlias;
11031078
11041079 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1105 if (i == f)
1106 continue;
1107
11081080 AliasResult ThisAlias =
11091081 aliasCheck(PN->getIncomingValue(i), PNSize, PNTBAAInfo,
11101082 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
11151087 }
11161088
11171089 // Reset if speculation failed.
1118 if (ArePhisAssumedNoAlias && Alias != NoAlias)
1090 if (Alias != NoAlias)
11191091 AliasCache[Locs] = OrigAliasResult;
11201092
11211093 return Alias;
33 ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
44
55 ; ptr_phi and ptr2_phi do not alias.
6 ; CHECK: test_noalias_1
67 ; CHECK: NoAlias: i32* %ptr2_phi, i32* %ptr_phi
7
8 define i32 @test_noalias(i32* %ptr2, i32 %count, i32* %coeff) {
8 define i32 @test_noalias_1(i32* %ptr2, i32 %count, i32* %coeff) {
99 entry:
1010 %ptr = getelementptr inbounds i32* %ptr2, i64 1
1111 br label %while.body
3030 the_exit:
3131 ret i32 %add
3232 }
33
34 ; CHECK: test_noalias_2
35 ; CHECK: NoAlias: i32* %ptr_outer_phi, i32* %ptr_outer_phi2
36 ; CHECK: NoAlias: i32* %ptr2_phi, i32* %ptr_phi
37 define i32 @test_noalias_2(i32* %ptr2, i32 %count, i32* %coeff) {
38 entry:
39 %ptr = getelementptr inbounds i32* %ptr2, i64 1
40 br label %outer.while.header
41
42 outer.while.header:
43 %ptr_outer_phi = phi i32* [%ptr_inc_outer, %outer.while.backedge], [ %ptr, %entry]
44 %ptr_outer_phi2 = phi i32* [%ptr2_inc_outer, %outer.while.backedge], [ %ptr2, %entry]
45 %num.outer = phi i32 [ %count, %entry ], [ %dec.outer, %outer.while.backedge ]
46 br label %while.body
47
48 while.body:
49 %num = phi i32 [ %count, %outer.while.header ], [ %dec, %while.body ]
50 %ptr_phi = phi i32* [ %ptr_outer_phi, %outer.while.header ], [ %ptr_inc, %while.body ]
51 %ptr2_phi = phi i32* [ %ptr_outer_phi2, %outer.while.header ], [ %ptr2_inc, %while.body ]
52 %result.09 = phi i32 [ 0 , %outer.while.header ], [ %add, %while.body ]
53 %dec = add nsw i32 %num, -1
54 %0 = load i32* %ptr_phi, align 4
55 store i32 %0, i32* %ptr2_phi, align 4
56 %1 = load i32* %coeff, align 4
57 %2 = load i32* %ptr_phi, align 4
58 %mul = mul nsw i32 %1, %2
59 %add = add nsw i32 %mul, %result.09
60 %tobool = icmp eq i32 %dec, 0
61 %ptr_inc = getelementptr inbounds i32* %ptr_phi, i64 1
62 %ptr2_inc = getelementptr inbounds i32* %ptr2_phi, i64 1
63 br i1 %tobool, label %outer.while.backedge, label %while.body
64
65 outer.while.backedge:
66 %ptr_inc_outer = getelementptr inbounds i32* %ptr_phi, i64 1
67 %ptr2_inc_outer = getelementptr inbounds i32* %ptr2_phi, i64 1
68 %dec.outer = add nsw i32 %num.outer, -1
69 %br.cond = icmp eq i32 %dec.outer, 0
70 br i1 %br.cond, label %the_exit, label %outer.while.header
71
72 the_exit:
73 ret i32 %add
74 }
75
76 ; CHECK: test_noalias_3
77 ; CHECK: MayAlias: i8* %ptr2_phi, i8* %ptr_phi
78 define i32 @test_noalias_3(i8* noalias %x, i8* noalias %y, i8* noalias %z,
79 i32 %count) {
80 entry:
81 br label %while.body
82
83 while.body:
84 %num = phi i32 [ %count, %entry ], [ %dec, %while.body ]
85 %ptr_phi = phi i8* [ %x, %entry ], [ %z, %while.body ]
86 %ptr2_phi = phi i8* [ %y, %entry ], [ %ptr_phi, %while.body ]
87 %dec = add nsw i32 %num, -1
88 %tobool = icmp eq i32 %dec, 0
89 br i1 %tobool, label %the_exit, label %while.body
90
91 the_exit:
92 ret i32 1
93 }