18
18
#include " llvm/ADT/SmallVector.h"
19
19
#include " llvm/Analysis/AliasAnalysis.h"
20
20
#include " llvm/Analysis/CaptureTracking.h"
21
+ #include " llvm/Analysis/Dominators.h"
21
22
#include " llvm/Analysis/InstructionSimplify.h"
22
23
#include " llvm/Analysis/MemoryBuiltins.h"
23
24
#include " llvm/Analysis/ValueTracking.h"
38
39
#include < algorithm>
39
40
using namespace llvm ;
40
41
42
+ // / Cutoff after which to stop analysing a set of phi nodes potentially involved
43
+ // / in a cycle. Because we are analysing 'through' phi nodes we need to be
44
+ // / careful with value equivalence. We use dominance to make sure a value cannot
45
+ // / be involved in a cycle.
46
+ const unsigned MaxNumPhiBBsValueDominanceCheck = 20 ;
47
+
41
48
// ===----------------------------------------------------------------------===//
42
49
// Useful predicates
43
50
// ===----------------------------------------------------------------------===//
@@ -403,42 +410,6 @@ DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
403
410
return V;
404
411
}
405
412
406
- // / GetIndexDifference - Dest and Src are the variable indices from two
407
- // / decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
408
- // / pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
409
- // / difference between the two pointers.
410
- static void GetIndexDifference (SmallVectorImpl<VariableGEPIndex> &Dest,
411
- const SmallVectorImpl<VariableGEPIndex> &Src) {
412
- if (Src.empty ()) return ;
413
-
414
- for (unsigned i = 0 , e = Src.size (); i != e; ++i) {
415
- const Value *V = Src[i].V ;
416
- ExtensionKind Extension = Src[i].Extension ;
417
- int64_t Scale = Src[i].Scale ;
418
-
419
- // Find V in Dest. This is N^2, but pointer indices almost never have more
420
- // than a few variable indexes.
421
- for (unsigned j = 0 , e = Dest.size (); j != e; ++j) {
422
- if (Dest[j].V != V || Dest[j].Extension != Extension) continue ;
423
-
424
- // If we found it, subtract off Scale V's from the entry in Dest. If it
425
- // goes to zero, remove the entry.
426
- if (Dest[j].Scale != Scale)
427
- Dest[j].Scale -= Scale;
428
- else
429
- Dest.erase (Dest.begin ()+j);
430
- Scale = 0 ;
431
- break ;
432
- }
433
-
434
- // If we didn't consume this entry, add it to the end of the Dest list.
435
- if (Scale) {
436
- VariableGEPIndex Entry = { V, Extension, -Scale };
437
- Dest.push_back (Entry);
438
- }
439
- }
440
- }
441
-
442
413
// ===----------------------------------------------------------------------===//
443
414
// BasicAliasAnalysis Pass
444
415
// ===----------------------------------------------------------------------===//
@@ -482,6 +453,7 @@ namespace {
482
453
483
454
virtual AliasResult alias (const Location &LocA,
484
455
const Location &LocB) {
456
+ DT = 0 ;
485
457
assert (AliasCache.empty () && " AliasCache must be cleared after use!" );
486
458
assert (notDifferentParent (LocA.Ptr , LocB.Ptr ) &&
487
459
" BasicAliasAnalysis doesn't support interprocedural queries." );
@@ -492,6 +464,7 @@ namespace {
492
464
// SmallDenseMap if it ever grows larger.
493
465
// FIXME: This should really be shrink_to_inline_capacity_and_clear().
494
466
AliasCache.shrink_and_clear ();
467
+ VisitedPhiBBs.clear ();
495
468
return Alias;
496
469
}
497
470
@@ -532,9 +505,41 @@ namespace {
532
505
typedef SmallDenseMap<LocPair, AliasResult, 8 > AliasCacheTy;
533
506
AliasCacheTy AliasCache;
534
507
508
+ // / \brief Track phi nodes we have visited. When interpret "Value" pointer
509
+ // / equality as value equality we need to make sure that the "Value" is not
510
+ // / part of a cycle. Otherwise, two uses could come from different
511
+ // / "iterations" of a cycle and see different values for the same "Value"
512
+ // / pointer.
513
+ // / The following example shows the problem:
514
+ // / %p = phi(%alloca1, %addr2)
515
+ // / %l = load %ptr
516
+ // / %addr1 = gep, %alloca2, 0, %l
517
+ // / %addr2 = gep %alloca2, 0, (%l + 1)
518
+ // / alias(%p, %addr1) -> MayAlias !
519
+ // / store %l, ...
520
+ SmallPtrSet<const BasicBlock*, 8 > VisitedPhiBBs;
521
+
535
522
// Visited - Track instructions visited by pointsToConstantMemory.
536
523
SmallPtrSet<const Value*, 16 > Visited;
537
524
525
+ // We use the dominator tree to check values can't be part of a cycle.
526
+ DominatorTree *DT;
527
+
528
+ // / \brief Check whether two Values can be considered equivalent.
529
+ // /
530
+ // / In addition to pointer equivalence of \p V1 and \p V2 this checks
531
+ // / whether they can not be part of a cycle in the value graph by looking at
532
+ // / all visited phi nodes an making sure that the value dominates all of
533
+ // / them.
534
+ bool isValueEqual (const Value *V1, const Value *V2);
535
+
536
+ // / \brief Dest and Src are the variable indices from two decomposed
537
+ // / GetElementPtr instructions GEP1 and GEP2 which have common base
538
+ // / pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
539
+ // / difference between the two pointers.
540
+ void GetIndexDifference (SmallVectorImpl<VariableGEPIndex> &Dest,
541
+ const SmallVectorImpl<VariableGEPIndex> &Src);
542
+
538
543
// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
539
544
// instruction against another.
540
545
AliasResult aliasGEP (const GEPOperator *V1, uint64_t V1Size,
@@ -1094,6 +1099,10 @@ BasicAliasAnalysis::aliasPHI(const PHINode *PN, uint64_t PNSize,
1094
1099
const MDNode *PNTBAAInfo,
1095
1100
const Value *V2, uint64_t V2Size,
1096
1101
const MDNode *V2TBAAInfo) {
1102
+ // Track phi nodes we have visited. We use this information when we determine
1103
+ // value equivalence.
1104
+ VisitedPhiBBs.insert (PN->getParent ());
1105
+
1097
1106
// If the values are PHIs in the same block, we can do a more precise
1098
1107
// as well as efficient check: just check for aliases between the values
1099
1108
// on corresponding edges.
@@ -1187,7 +1196,7 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, uint64_t V1Size,
1187
1196
V2 = V2->stripPointerCasts ();
1188
1197
1189
1198
// Are we checking for alias of the same value?
1190
- if (V1 == V2 ) return MustAlias;
1199
+ if (isValueEqual (V1, V2) ) return MustAlias;
1191
1200
1192
1201
if (!V1->getType ()->isPointerTy () || !V2->getType ()->isPointerTy ())
1193
1202
return NoAlias; // Scalars cannot alias each other
@@ -1307,3 +1316,69 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, uint64_t V1Size,
1307
1316
Location (V2, V2Size, V2TBAAInfo));
1308
1317
return AliasCache[Locs] = Result;
1309
1318
}
1319
+
1320
+ bool BasicAliasAnalysis::isValueEqual (const Value *V, const Value *V2) {
1321
+ if (V != V2)
1322
+ return false ;
1323
+
1324
+ const Instruction *Inst = dyn_cast<Instruction>(V);
1325
+ if (!Inst)
1326
+ return true ;
1327
+
1328
+ // Use the dominance if available.
1329
+ DT = getAnalysisIfAvailable<DominatorTree>();
1330
+ if (DT) {
1331
+ if (VisitedPhiBBs.size () > MaxNumPhiBBsValueDominanceCheck)
1332
+ return false ;
1333
+
1334
+ // Make sure that the visited phis are dominated by the Value.
1335
+ for (SmallPtrSet<const BasicBlock *, 8 >::iterator
1336
+ PI = VisitedPhiBBs.begin (),
1337
+ PE = VisitedPhiBBs.end ();
1338
+ PI != PE; ++PI)
1339
+ if (!DT->dominates (Inst, *PI))
1340
+ return false ;
1341
+ return true ;
1342
+ }
1343
+
1344
+ return false ;
1345
+ }
1346
+
1347
+ // / GetIndexDifference - Dest and Src are the variable indices from two
1348
+ // / decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
1349
+ // / pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
1350
+ // / difference between the two pointers.
1351
+ void BasicAliasAnalysis::GetIndexDifference (
1352
+ SmallVectorImpl<VariableGEPIndex> &Dest,
1353
+ const SmallVectorImpl<VariableGEPIndex> &Src) {
1354
+ if (Src.empty ())
1355
+ return ;
1356
+
1357
+ for (unsigned i = 0 , e = Src.size (); i != e; ++i) {
1358
+ const Value *V = Src[i].V ;
1359
+ ExtensionKind Extension = Src[i].Extension ;
1360
+ int64_t Scale = Src[i].Scale ;
1361
+
1362
+ // Find V in Dest. This is N^2, but pointer indices almost never have more
1363
+ // than a few variable indexes.
1364
+ for (unsigned j = 0 , e = Dest.size (); j != e; ++j) {
1365
+ if (!isValueEqual (Dest[j].V , V) || Dest[j].Extension != Extension)
1366
+ continue ;
1367
+
1368
+ // If we found it, subtract off Scale V's from the entry in Dest. If it
1369
+ // goes to zero, remove the entry.
1370
+ if (Dest[j].Scale != Scale)
1371
+ Dest[j].Scale -= Scale;
1372
+ else
1373
+ Dest.erase (Dest.begin () + j);
1374
+ Scale = 0 ;
1375
+ break ;
1376
+ }
1377
+
1378
+ // If we didn't consume this entry, add it to the end of the Dest list.
1379
+ if (Scale) {
1380
+ VariableGEPIndex Entry = { V, Extension, -Scale };
1381
+ Dest.push_back (Entry);
1382
+ }
1383
+ }
1384
+ }
0 commit comments