llvm.org GIT mirror llvm / ffb9f96
Merging r198290: ------------------------------------------------------------------------ r198290 | aschwaighofer | 2014-01-01 22:31:36 -0500 (Wed, 01 Jan 2014) | 23 lines BasicAA: Fix value equality and phi cycles When there are cycles in the value graph we have to be careful interpreting "Value*" identity as "value" equivalence. We interpret the value of a phi node as the value of its operands. When we check for value equivalence now we make sure that the "Value*" dominates all cycles (phis). %0 = phi [%noaliasval, %addr2] %l = load %ptr %addr1 = gep @a, 0, %l %addr2 = gep @a, 0, (%l + 1) store %ptr ... Before this patch we would return NoAlias for (%0, %addr1) which is wrong because the value of the load is from different iterations of the loop. Tested on x86_64 -mavx at O3 and O3 -flto with no performance or compile time regressions. PR18068 radar://15653794 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_34@206051 91177308-0d34-0410-b5e6-96231b3b80d8 Tom Stellard 6 years ago
8 changed file(s) with 204 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
1717 #include "llvm/ADT/SmallVector.h"
1818 #include "llvm/Analysis/AliasAnalysis.h"
1919 #include "llvm/Analysis/CaptureTracking.h"
20 #include "llvm/Analysis/Dominators.h"
2021 #include "llvm/Analysis/InstructionSimplify.h"
2122 #include "llvm/Analysis/MemoryBuiltins.h"
2223 #include "llvm/Analysis/ValueTracking.h"
3637 #include "llvm/Target/TargetLibraryInfo.h"
3738 #include
3839 using namespace llvm;
40
41 /// Cutoff after which to stop analysing a set of phi nodes potentially involved
42 /// in a cycle. Because we are analysing 'through' phi nodes we need to be
43 /// careful with value equivalence. We use dominance to make sure a value cannot
44 /// be involved in a cycle.
45 const unsigned MaxNumPhiBBsValueDominanceCheck = 20;
3946
4047 //===----------------------------------------------------------------------===//
4148 // Useful predicates
402409 return V;
403410 }
404411
405 /// GetIndexDifference - Dest and Src are the variable indices from two
406 /// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
407 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
408 /// difference between the two pointers.
409 static void GetIndexDifference(SmallVectorImpl &Dest,
410 const SmallVectorImpl &Src) {
411 if (Src.empty()) return;
412
413 for (unsigned i = 0, e = Src.size(); i != e; ++i) {
414 const Value *V = Src[i].V;
415 ExtensionKind Extension = Src[i].Extension;
416 int64_t Scale = Src[i].Scale;
417
418 // Find V in Dest. This is N^2, but pointer indices almost never have more
419 // than a few variable indexes.
420 for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
421 if (Dest[j].V != V || Dest[j].Extension != Extension) continue;
422
423 // If we found it, subtract off Scale V's from the entry in Dest. If it
424 // goes to zero, remove the entry.
425 if (Dest[j].Scale != Scale)
426 Dest[j].Scale -= Scale;
427 else
428 Dest.erase(Dest.begin()+j);
429 Scale = 0;
430 break;
431 }
432
433 // If we didn't consume this entry, add it to the end of the Dest list.
434 if (Scale) {
435 VariableGEPIndex Entry = { V, Extension, -Scale };
436 Dest.push_back(Entry);
437 }
438 }
439 }
440
441412 //===----------------------------------------------------------------------===//
442413 // BasicAliasAnalysis Pass
443414 //===----------------------------------------------------------------------===//
481452
482453 virtual AliasResult alias(const Location &LocA,
483454 const Location &LocB) {
455 DT = 0;
484456 assert(AliasCache.empty() && "AliasCache must be cleared after use!");
485457 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
486458 "BasicAliasAnalysis doesn't support interprocedural queries.");
491463 // SmallDenseMap if it ever grows larger.
492464 // FIXME: This should really be shrink_to_inline_capacity_and_clear().
493465 AliasCache.shrink_and_clear();
466 VisitedPhiBBs.clear();
494467 return Alias;
495468 }
496469
531504 typedef SmallDenseMap AliasCacheTy;
532505 AliasCacheTy AliasCache;
533506
507 /// \brief Track phi nodes we have visited. When interpret "Value" pointer
508 /// equality as value equality we need to make sure that the "Value" is not
509 /// part of a cycle. Otherwise, two uses could come from different
510 /// "iterations" of a cycle and see different values for the same "Value"
511 /// pointer.
512 /// The following example shows the problem:
513 /// %p = phi(%alloca1, %addr2)
514 /// %l = load %ptr
515 /// %addr1 = gep, %alloca2, 0, %l
516 /// %addr2 = gep %alloca2, 0, (%l + 1)
517 /// alias(%p, %addr1) -> MayAlias !
518 /// store %l, ...
519 SmallPtrSet VisitedPhiBBs;
520
534521 // Visited - Track instructions visited by pointsToConstantMemory.
535522 SmallPtrSet Visited;
523
524 // We use the dominator tree to check values can't be part of a cycle.
525 DominatorTree *DT;
526
527 /// \brief Check whether two Values can be considered equivalent.
528 ///
529 /// In addition to pointer equivalence of \p V1 and \p V2 this checks
530 /// whether they can not be part of a cycle in the value graph by looking at
531 /// all visited phi nodes an making sure that the value dominates all of
532 /// them.
533 bool isValueEqual(const Value *V1, const Value *V2);
534
535 /// \brief Dest and Src are the variable indices from two decomposed
536 /// GetElementPtr instructions GEP1 and GEP2 which have common base
537 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
538 /// difference between the two pointers.
539 void GetIndexDifference(SmallVectorImpl &Dest,
540 const SmallVectorImpl &Src);
536541
537542 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
538543 // instruction against another.
10931098 const MDNode *PNTBAAInfo,
10941099 const Value *V2, uint64_t V2Size,
10951100 const MDNode *V2TBAAInfo) {
1101 // Track phi nodes we have visited. We use this information when we determine
1102 // value equivalence.
1103 VisitedPhiBBs.insert(PN->getParent());
1104
10961105 // If the values are PHIs in the same block, we can do a more precise
10971106 // as well as efficient check: just check for aliases between the values
10981107 // on corresponding edges.
11861195 V2 = V2->stripPointerCasts();
11871196
11881197 // Are we checking for alias of the same value?
1189 if (V1 == V2) return MustAlias;
1198 if (isValueEqual(V1, V2)) return MustAlias;
11901199
11911200 if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
11921201 return NoAlias; // Scalars cannot alias each other
13061315 Location(V2, V2Size, V2TBAAInfo));
13071316 return AliasCache[Locs] = Result;
13081317 }
1318
1319 bool BasicAliasAnalysis::isValueEqual(const Value *V, const Value *V2) {
1320 if (V != V2)
1321 return false;
1322
1323 const Instruction *Inst = dyn_cast(V);
1324 if (!Inst)
1325 return true;
1326
1327 // Use the dominance if available.
1328 DT = getAnalysisIfAvailable();
1329 if (DT) {
1330 if (VisitedPhiBBs.size() > MaxNumPhiBBsValueDominanceCheck)
1331 return false;
1332
1333 // Make sure that the visited phis are dominated by the Value.
1334 for (SmallPtrSet::iterator
1335 PI = VisitedPhiBBs.begin(),
1336 PE = VisitedPhiBBs.end();
1337 PI != PE; ++PI)
1338 if (!DT->dominates(Inst, *PI))
1339 return false;
1340 return true;
1341 }
1342
1343 return false;
1344 }
1345
1346 /// GetIndexDifference - Dest and Src are the variable indices from two
1347 /// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
1348 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
1349 /// difference between the two pointers.
1350 void BasicAliasAnalysis::GetIndexDifference(
1351 SmallVectorImpl &Dest,
1352 const SmallVectorImpl &Src) {
1353 if (Src.empty())
1354 return;
1355
1356 for (unsigned i = 0, e = Src.size(); i != e; ++i) {
1357 const Value *V = Src[i].V;
1358 ExtensionKind Extension = Src[i].Extension;
1359 int64_t Scale = Src[i].Scale;
1360
1361 // Find V in Dest. This is N^2, but pointer indices almost never have more
1362 // than a few variable indexes.
1363 for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
1364 if (!isValueEqual(Dest[j].V, V) || Dest[j].Extension != Extension)
1365 continue;
1366
1367 // If we found it, subtract off Scale V's from the entry in Dest. If it
1368 // goes to zero, remove the entry.
1369 if (Dest[j].Scale != Scale)
1370 Dest[j].Scale -= Scale;
1371 else
1372 Dest.erase(Dest.begin() + j);
1373 Scale = 0;
1374 break;
1375 }
1376
1377 // If we didn't consume this entry, add it to the end of the Dest list.
1378 if (Scale) {
1379 VariableGEPIndex Entry = { V, Extension, -Scale };
1380 Dest.push_back(Entry);
1381 }
1382 }
1383 }
None ; RUN: opt < %s -basicaa -aa-eval -disable-output 2>&1 | FileCheck %s
0 ; RUN: opt < %s -domtree -basicaa -aa-eval -disable-output 2>&1 | FileCheck %s
11 ; TEST that A[1][0] may alias A[0][i].
22 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
33
0 ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
1 ; RUN: opt < %s -domtree -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s --check-prefix=DOM
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-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
15 ; rdar://7282591
26
37 @X = common global i32 0
48 @Y = common global i32 0
59 @Z = common global i32 0
610
11 ; CHECK-LABEL: foo
712 ; CHECK: NoAlias: i32* %P, i32* @Z
13
14 ; DOM-LABEL: foo
15 ; DOM: NoAlias: i32* %P, i32* @Z
816
917 define void @foo(i32 %cond) nounwind {
1018 entry:
2836 return:
2937 ret void
3038 }
39
40 ; Pointers can vary in between iterations of loops.
41 ; PR18068
42
43 ; CHECK-LABEL: pr18068
44 ; CHECK: MayAlias: i32* %0, i32* %arrayidx5
45
46 ; DOM-LABEL: pr18068
47 ; DOM: MayAlias: i32* %0, i32* %arrayidx5
48
49 define i32 @pr18068(i32* %jj7, i32* %j) {
50 entry:
51 %oa5 = alloca [100 x i32], align 16
52 br label %codeRepl
53
54 codeRepl:
55 %0 = phi i32* [ %arrayidx13, %for.body ], [ %j, %entry ]
56 %targetBlock = call i1 @cond(i32* %jj7)
57 br i1 %targetBlock, label %for.body, label %bye
58
59 for.body:
60 %1 = load i32* %jj7, align 4
61 %idxprom4 = zext i32 %1 to i64
62 %arrayidx5 = getelementptr inbounds [100 x i32]* %oa5, i64 0, i64 %idxprom4
63 %2 = load i32* %arrayidx5, align 4
64 %sub6 = sub i32 %2, 6
65 store i32 %sub6, i32* %arrayidx5, align 4
66 ; %0 and %arrayidx5 can alias! It is not safe to DSE the above store.
67 %3 = load i32* %0, align 4
68 store i32 %3, i32* %arrayidx5, align 4
69 %sub11 = add i32 %1, -1
70 %idxprom12 = zext i32 %sub11 to i64
71 %arrayidx13 = getelementptr inbounds [100 x i32]* %oa5, i64 0, i64 %idxprom12
72 call void @inc(i32* %jj7)
73 br label %codeRepl
74
75 bye:
76 %.reload = load i32* %jj7, align 4
77 ret i32 %.reload
78 }
79
80 declare i1 @cond(i32*)
81
82 declare void @inc(i32*)
83
84
0 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-f128:128:128-v128:128:128-n32:64"
11 target triple = "powerpc64-bgq-linux"
2 ; RUN: opt < %s -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
2 ; RUN: opt < %s -domtree -basicaa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
33
44 @X = external global [16000 x double], align 32
55 @Y = external global [16000 x double], align 32
None ; RUN: opt < %s -basicaa -globalsmodref-aa -gvn -S | FileCheck %s
0 ; RUN: opt < %s -domtree -basicaa -globalsmodref-aa -gvn -S | FileCheck %s
11
22 @X = internal global i32 4 ; [#uses=1]
33
None ; RUN: opt -S -basicaa -objc-arc < %s | FileCheck %s
0 ; RUN: opt -S -domtree -basicaa -objc-arc < %s | FileCheck %s
11
22 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"
33 target triple = "x86_64-apple-darwin11.0.0"
None ; RUN: opt -S -basicaa -objc-arc < %s | FileCheck %s
0 ; RUN: opt -S -domtree -basicaa -objc-arc < %s | FileCheck %s
11 ; rdar://11434915
22
33 ; Delete the weak calls and replace them with just the net retain.
2525 Release=""
2626 Release_no_dot=""
2727 RC=""
28 DOT=""
2829 Triple=""
2930 use_gzip="no"
3031 do_checkout="yes"
4445 echo ""
4546 echo " -release X.Y The release number to test."
4647 echo " -rc NUM The pre-release candidate number."
48 echo " -dot NUM The dot release to test e.g. X.Y.DOT_NUM [default: 0]"
4749 echo " -final The final release candidate."
4850 echo " -triple TRIPLE The target triple for this machine."
4951 echo " -j NUM Number of compile jobs to run. [default: 3]"
7577 -final | --final )
7678 RC=final
7779 ;;
80 -dot | --dot )
81 shift
82 DOT="$1"
83 if [ $DOT -eq 0 ]; then
84 DOT=""
85 fi
86 ;;
7887 -triple | --triple )
7988 shift
8089 Triple="$1"
135144 esac
136145 shift
137146 done
147
148 if [ -n "$DOT" ]; then
149 Release="$Release.$DOT"
150 fi
138151
139152 # Check required arguments.
140153 if [ -z "$Release" ]; then
216229 check_program_exists 'objdump'
217230 fi
218231
232 function get_svn_tag() {
233 case $1 in
234 # llvm and clang are the only projects currently doing dot releases.
235 llvm | cfe)
236 if [ -z $DOT ]; then
237 SvnTag="$RC"
238 else
239 SvnTag="dot$DOT-$RC"
240 fi
241 ;;
242 *)
243 SvnTag="$RC"
244 ;;
245 esac
246 }
247
219248 # Make sure that the URLs are valid.
220249 function check_valid_urls() {
221250 for proj in $projects ; do
222251 echo "# Validating $proj SVN URL"
223252
224 if ! svn ls $Base_url/$proj/tags/RELEASE_$Release_no_dot/$RC > /dev/null 2>&1 ; then
253 get_svn_tag "$proj"
254 if ! svn ls $Base_url/$proj/tags/RELEASE_$Release_no_dot/$SvnTag > /dev/null 2>&1 ; then
225255 echo "llvm $Release release candidate $RC doesn't exist!"
226256 exit 1
227257 fi
234264
235265 for proj in $projects ; do
236266 echo "# Exporting $proj $Release-RC$RC sources"
237 if ! svn export -q $Base_url/$proj/tags/RELEASE_$Release_no_dot/$RC $proj.src ; then
267 get_svn_tag "$proj"
268 if ! svn export -q $Base_url/$proj/tags/RELEASE_$Release_no_dot/$SvnTag $proj.src ; then
238269 echo "error: failed to export $proj project"
239270 exit 1
240271 fi