llvm.org GIT mirror llvm / 681a33e
Another BasicAA fix. If a value does not alias a GEP's base pointer, then it cannot alias the GEP. GEP pointer alias rule states this clearly: A pointer value formed from a getelementptr instruction is associated with the addresses associated with the first operand of the getelementptr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84079 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 10 years ago
2 changed file(s) with 78 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
427427 SmallVector GEPOperands;
428428 const Value *BasePtr = GetGEPOperands(V1, GEPOperands);
429429
430 AliasResult R = aliasCheck(BasePtr, V1Size, V2, V2Size);
431 if (R == MustAlias) {
432 // If there is at least one non-zero constant index, we know they cannot
433 // alias.
434 bool ConstantFound = false;
435 bool AllZerosFound = true;
436 for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
437 if (const Constant *C = dyn_cast(GEPOperands[i])) {
438 if (!C->isNullValue()) {
439 ConstantFound = true;
440 AllZerosFound = false;
441 break;
442 }
443 } else {
430 AliasResult R = aliasCheck(BasePtr, ~0U, V2, V2Size);
431 if (R != MustAlias)
432 // If V2 may alias GEP base pointer, conservatively returns MayAlias.
433 // If V2 is known not to alias GEP base pointer, then the two values
434 // cannot alias per GEP semantics: "A pointer value formed from a
435 // getelementptr instruction is associated with the addresses associated
436 // with the first operand of the getelementptr".
437 return R;
438
439 // If there is at least one non-zero constant index, we know they cannot
440 // alias.
441 bool ConstantFound = false;
442 bool AllZerosFound = true;
443 for (unsigned i = 0, e = GEPOperands.size(); i != e; ++i)
444 if (const Constant *C = dyn_cast(GEPOperands[i])) {
445 if (!C->isNullValue()) {
446 ConstantFound = true;
444447 AllZerosFound = false;
448 break;
445449 }
446
447 // If we have getelementptr , 0, 0, 0, 0, ... and V2 must aliases
448 // the ptr, the end result is a must alias also.
449 if (AllZerosFound)
450 return MustAlias;
451
452 if (ConstantFound) {
453 if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
450 } else {
451 AllZerosFound = false;
452 }
453
454 // If we have getelementptr , 0, 0, 0, 0, ... and V2 must aliases
455 // the ptr, the end result is a must alias also.
456 if (AllZerosFound)
457 return MustAlias;
458
459 if (ConstantFound) {
460 if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
461 return NoAlias;
462
463 // Otherwise we have to check to see that the distance is more than
464 // the size of the argument... build an index vector that is equal to
465 // the arguments provided, except substitute 0's for any variable
466 // indexes we find...
467 if (TD &&
468 cast(BasePtr->getType())->getElementType()->isSized()) {
469 for (unsigned i = 0; i != GEPOperands.size(); ++i)
470 if (!isa(GEPOperands[i]))
471 GEPOperands[i] = Constant::getNullValue(GEPOperands[i]->getType());
472 int64_t Offset = TD->getIndexedOffset(BasePtr->getType(),
473 &GEPOperands[0],
474 GEPOperands.size());
475
476 if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
454477 return NoAlias;
455
456 // Otherwise we have to check to see that the distance is more than
457 // the size of the argument... build an index vector that is equal to
458 // the arguments provided, except substitute 0's for any variable
459 // indexes we find...
460 if (TD &&
461 cast(BasePtr->getType())->getElementType()->isSized()) {
462 for (unsigned i = 0; i != GEPOperands.size(); ++i)
463 if (!isa(GEPOperands[i]))
464 GEPOperands[i] = Constant::getNullValue(GEPOperands[i]->getType());
465 int64_t Offset =
466 TD->getIndexedOffset(BasePtr->getType(),
467 &GEPOperands[0],
468 GEPOperands.size());
469
470 if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
471 return NoAlias;
472 }
473478 }
474479 }
475480
491496 Value *PV1 = PN->getIncomingValue(i);
492497 if (isa(PV1))
493498 // If any of the source itself is a PHI, return MayAlias conservatively
494 // to avoid compile time explosion.
499 // to avoid compile time explosion. The worst possible case is if both
500 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
501 // and 'n' are the number of PHI sources.
495502 return MayAlias;
496503 if (UniqueSrc.insert(PV1))
497504 V1Srcs.push_back(PV1);
0 ; RUN: opt < %s -aa-eval -print-all-alias-modref-info -disable-output |& grep {NoAlias:.*%P,.*@Z}
1 ; If GEP base doesn't alias Z, then GEP doesn't alias Z.
2 ; rdar://7282591
3
4 @Y = common global i32 0
5 @Z = common global i32 0
6
7 define void @foo(i32 %cond) nounwind ssp {
8 entry:
9 %a = alloca i32
10 %tmp = icmp ne i32 %cond, 0
11 br i1 %tmp, label %bb, label %bb1
12
13 bb:
14 %b = getelementptr i32* %a, i32 0
15 br label %bb2
16
17 bb1:
18 br label %bb2
19
20 bb2:
21 %P = phi i32* [ %b, %bb ], [ @Y, %bb1 ]
22 %tmp1 = load i32* @Z, align 4
23 store i32 123, i32* %P, align 4
24 %tmp2 = load i32* @Z, align 4
25 br label %return
26
27 return:
28 ret void
29 }