@@ -776,10 +776,9 @@ static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1,
776
776
ConstantInt *C2 =
777
777
dyn_cast<ConstantInt>(GEP2->getOperand (GEP2->getNumOperands () - 1 ));
778
778
779
- // If the last (struct) indices aren't constants, we can't say anything.
780
- // If they're identical, the other indices might be also be dynamically
781
- // equal, so the GEPs can alias.
782
- if (!C1 || !C2 || C1 == C2)
779
+ // If the last (struct) indices are constants and are equal, the other indices
780
+ // might be also be dynamically equal, so the GEPs can alias.
781
+ if (C1 && C2 && C1 == C2)
783
782
return MayAlias;
784
783
785
784
// Find the last-indexed type of the GEP, i.e., the type you'd get if
@@ -802,12 +801,43 @@ static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1,
802
801
IntermediateIndices.push_back (GEP1->getOperand (i + 1 ));
803
802
}
804
803
805
- StructType *LastIndexedStruct =
806
- dyn_cast<StructType>( GetElementPtrInst::getIndexedType (
807
- GEP1-> getSourceElementType (), IntermediateIndices) );
804
+ auto *Ty = GetElementPtrInst::getIndexedType (
805
+ GEP1-> getSourceElementType (), IntermediateIndices);
806
+ StructType *LastIndexedStruct = dyn_cast<StructType>(Ty );
808
807
809
- if (!LastIndexedStruct)
808
+ if (isa<SequentialType>(Ty)) {
809
+ // We know that:
810
+ // - both GEPs begin indexing from the exact same pointer;
811
+ // - the last indices in both GEPs are constants, indexing into a sequential
812
+ // type (array or pointer);
813
+ // - both GEPs only index through arrays prior to that.
814
+ //
815
+ // Because array indices greater than the number of elements are valid in
816
+ // GEPs, unless we know the intermediate indices are identical between
817
+ // GEP1 and GEP2 we cannot guarantee that the last indexed arrays don't
818
+ // partially overlap.
819
+ for (unsigned i = 0 , e = GEP1->getNumIndices () - 1 ; i != e; ++i)
820
+ if (GEP1->getOperand (i + 1 ) != GEP2->getOperand (i + 1 ))
821
+ return MayAlias;
822
+
823
+ // Now we know that the array/pointer that GEP1 indexes into and that
824
+ // that GEP2 indexes into must either precisely overlap or be disjoint.
825
+ // Because they cannot partially overlap and because fields in an array
826
+ // cannot overlap, if we can prove the final indices are different between
827
+ // GEP1 and GEP2, we can conclude GEP1 and GEP2 don't alias.
828
+
829
+ // If the last indices are constants, we've already checked they don't
830
+ // equal each other so we can exit early.
831
+ if (C1 && C2)
832
+ return NoAlias;
833
+ if (isKnownNonEqual (GEP1->getOperand (GEP1->getNumOperands () - 1 ),
834
+ GEP2->getOperand (GEP2->getNumOperands () - 1 ),
835
+ DL))
836
+ return NoAlias;
837
+ return MayAlias;
838
+ } else if (!LastIndexedStruct || !C1 || !C2) {
810
839
return MayAlias;
840
+ }
811
841
812
842
// We know that:
813
843
// - both GEPs begin indexing from the exact same pointer;
0 commit comments