llvm.org GIT mirror llvm / 1fc18d7
Fix BasicAA's recursion detection so that it doesn't pessimize queries in the case of a DAG, where a query reaches a node visited earlier, but it's not on a cycle. This avoids MayAlias results in cases where BasicAA is expected to return MustAlias or PartialAlias in order to protect TBAA. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@132609 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
3 changed file(s) with 97 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
3737 #define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
3838
3939 #include "llvm/Support/CallSite.h"
40 #include "llvm/ADT/DenseMap.h"
4041
4142 namespace llvm {
4243
487488 }
488489 };
489490
491 // Specialize DenseMapInfo for Location.
492 template<>
493 struct DenseMapInfo {
494 static inline AliasAnalysis::Location getEmptyKey() {
495 return
496 AliasAnalysis::Location(DenseMapInfo::getEmptyKey(),
497 0, 0);
498 }
499 static inline AliasAnalysis::Location getTombstoneKey() {
500 return
501 AliasAnalysis::Location(DenseMapInfo::getTombstoneKey(),
502 0, 0);
503 }
504 static unsigned getHashValue(const AliasAnalysis::Location &Val) {
505 return DenseMapInfo::getHashValue(Val.Ptr) ^
506 DenseMapInfo::getHashValue(Val.Size) ^
507 DenseMapInfo::getHashValue(Val.TBAATag);
508 }
509 static bool isEqual(const AliasAnalysis::Location &LHS,
510 const AliasAnalysis::Location &RHS) {
511 return LHS.Ptr == RHS.Ptr &&
512 LHS.Size == RHS.Size &&
513 LHS.TBAATag == RHS.TBAATag;
514 }
515 };
516
490517 /// isNoAliasCall - Return true if this pointer is returned by a noalias
491518 /// function.
492519 bool isNoAliasCall(const Value *V);
464464
465465 virtual AliasResult alias(const Location &LocA,
466466 const Location &LocB) {
467 assert(Visited.empty() && "Visited must be cleared after use!");
467 assert(AliasCache.empty() && "AliasCache must be cleared after use!");
468468 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
469469 "BasicAliasAnalysis doesn't support interprocedural queries.");
470470 AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.TBAATag,
471471 LocB.Ptr, LocB.Size, LocB.TBAATag);
472 Visited.clear();
472 AliasCache.clear();
473473 return Alias;
474474 }
475475
505505 }
506506
507507 private:
508 // Visited - Track instructions visited by a aliasPHI, aliasSelect(), and aliasGEP().
508 // AliasCache - Track alias queries to guard against recursion.
509 typedef std::pair LocPair;
510 typedef DenseMap AliasCacheTy;
511 AliasCacheTy AliasCache;
512
513 // Visited - Track instructions visited by pointsToConstantMemory.
509514 SmallPtrSet Visited;
510515
511516 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
821826 const MDNode *V2TBAAInfo,
822827 const Value *UnderlyingV1,
823828 const Value *UnderlyingV2) {
824 // If this GEP has been visited before, we're on a use-def cycle.
825 // Such cycles are only valid when PHI nodes are involved or in unreachable
826 // code. The visitPHI function catches cycles containing PHIs, but there
827 // could still be a cycle without PHIs in unreachable code.
828 if (!Visited.insert(GEP1))
829 return MayAlias;
830
831829 int64_t GEP1BaseOffset;
832830 SmallVector GEP1VariableIndices;
833831
968966 const MDNode *SITBAAInfo,
969967 const Value *V2, uint64_t V2Size,
970968 const MDNode *V2TBAAInfo) {
971 // If this select has been visited before, we're on a use-def cycle.
972 // Such cycles are only valid when PHI nodes are involved or in unreachable
973 // code. The visitPHI function catches cycles containing PHIs, but there
974 // could still be a cycle without PHIs in unreachable code.
975 if (!Visited.insert(SI))
976 return MayAlias;
977
978969 // If the values are Selects with the same condition, we can do a more precise
979970 // check: just check for aliases between the values on corresponding arms.
980971 if (const SelectInst *SI2 = dyn_cast(V2))
997988 if (Alias == MayAlias)
998989 return MayAlias;
999990
1000 // If V2 is visited, the recursive case will have been caught in the
1001 // above aliasCheck call, so these subsequent calls to aliasCheck
1002 // don't need to assume that V2 is being visited recursively.
1003 Visited.erase(V2);
1004
1005991 AliasResult ThisAlias =
1006992 aliasCheck(V2, V2Size, V2TBAAInfo, SI->getFalseValue(), SISize, SITBAAInfo);
1007993 return MergeAliasResults(ThisAlias, Alias);
10141000 const MDNode *PNTBAAInfo,
10151001 const Value *V2, uint64_t V2Size,
10161002 const MDNode *V2TBAAInfo) {
1017 // The PHI node has already been visited, avoid recursion any further.
1018 if (!Visited.insert(PN))
1019 return MayAlias;
1020
10211003 // If the values are PHIs in the same block, we can do a more precise
10221004 // as well as efficient check: just check for aliases between the values
10231005 // on corresponding edges.
10671049 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
10681050 Value *V = V1Srcs[i];
10691051
1070 // If V2 is visited, the recursive case will have been caught in the
1071 // above aliasCheck call, so these subsequent calls to aliasCheck
1072 // don't need to assume that V2 is being visited recursively.
1073 Visited.erase(V2);
1074
10751052 AliasResult ThisAlias = aliasCheck(V2, V2Size, V2TBAAInfo,
10761053 V, PNSize, PNTBAAInfo);
10771054 Alias = MergeAliasResults(ThisAlias, Alias);
11611138 (V2Size != UnknownSize && isObjectSmallerThan(O1, V2Size, *TD)))
11621139 return NoAlias;
11631140
1141 // Check the cache before climbing up use-def chains. This also terminates
1142 // otherwise infinitely recursive queries.
1143 LocPair Locs(Location(V1, V1Size, V1TBAAInfo),
1144 Location(V2, V2Size, V2TBAAInfo));
1145 if (V1 > V2)
1146 std::swap(Locs.first, Locs.second);
1147 std::pair Pair =
1148 AliasCache.insert(std::make_pair(Locs, MayAlias));
1149 if (!Pair.second)
1150 return Pair.first->second;
1151
11641152 // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
11651153 // GEP can't simplify, we don't even look at the PHI cases.
11661154 if (!isa(V1) && isa(V2)) {
11701158 }
11711159 if (const GEPOperator *GV1 = dyn_cast(V1)) {
11721160 AliasResult Result = aliasGEP(GV1, V1Size, V2, V2Size, V2TBAAInfo, O1, O2);
1173 if (Result != MayAlias) return Result;
1161 if (Result != MayAlias) return AliasCache[Locs] = Result;
11741162 }
11751163
11761164 if (isa(V2) && !isa(V1)) {
11801168 if (const PHINode *PN = dyn_cast(V1)) {
11811169 AliasResult Result = aliasPHI(PN, V1Size, V1TBAAInfo,
11821170 V2, V2Size, V2TBAAInfo);
1183 if (Result != MayAlias) return Result;
1171 if (Result != MayAlias) return AliasCache[Locs] = Result;
11841172 }
11851173
11861174 if (isa(V2) && !isa(V1)) {
11901178 if (const SelectInst *S1 = dyn_cast(V1)) {
11911179 AliasResult Result = aliasSelect(S1, V1Size, V1TBAAInfo,
11921180 V2, V2Size, V2TBAAInfo);
1193 if (Result != MayAlias) return Result;
1181 if (Result != MayAlias) return AliasCache[Locs] = Result;
11941182 }
11951183
11961184 // If both pointers are pointing into the same object and one of them
11991187 if (TD && O1 == O2)
12001188 if ((V1Size != UnknownSize && isObjectSize(O1, V1Size, *TD)) ||
12011189 (V2Size != UnknownSize && isObjectSize(O2, V2Size, *TD)))
1202 return PartialAlias;
1203
1204 return AliasAnalysis::alias(Location(V1, V1Size, V1TBAAInfo),
1205 Location(V2, V2Size, V2TBAAInfo));
1206 }
1190 return AliasCache[Locs] = PartialAlias;
1191
1192 AliasResult Result =
1193 AliasAnalysis::alias(Location(V1, V1Size, V1TBAAInfo),
1194 Location(V2, V2Size, V2TBAAInfo));
1195 return AliasCache[Locs] = Result;
1196 }
0 ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info |& FileCheck %s
1
2 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
3
4 ; BasicAA's guard against use-def cycles shouldn't prevent it from
5 ; analyzing use-def dags.
6
7 ; CHECK: MustAlias: i8* %base, i8* %phi
8 ; CHECK: MustAlias: i8* %phi, i8* %wwa
9 ; CHECK: MustAlias: i8* %phi, i8* %wwb
10 ; CHECK: MustAlias: i16* %bigbase, i8* %phi
11 define i8 @foo(i8* %base, i1 %x, i1 %w) {
12 entry:
13 br i1 %w, label %wa, label %wb
14 wa:
15 %wwa = bitcast i8* %base to i8*
16 br label %wc
17 wb:
18 %wwb = bitcast i8* %base to i8*
19 br label %wc
20 wc:
21 %first = phi i8* [ %wwa, %wa ], [ %wwb, %wb ]
22 %fc = bitcast i8* %first to i8*
23 br i1 %x, label %xa, label %xb
24 xa:
25 %xxa = bitcast i8* %fc to i8*
26 br label %xc
27 xb:
28 %xxb = bitcast i8* %fc to i8*
29 br label %xc
30 xc:
31 %phi = phi i8* [ %xxa, %xa ], [ %xxb, %xb ]
32
33 store i8 0, i8* %phi
34
35 %bigbase = bitcast i8* %base to i16*
36 store i16 -1, i16* %bigbase
37
38 %loaded = load i8* %phi
39 ret i8 %loaded
40 }