@@ -713,13 +713,20 @@ static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,
713
713
// active, or to be forced to operate as a module pass that cannot co-exist
714
714
// with an alias analysis such as GMR.
715
715
bool GlobalsAAResult::isNonEscapingGlobalNoAlias (const GlobalValue *GV,
716
- const Value *V) {
716
+ const Value *V,
717
+ const Instruction *CtxI) {
717
718
// In order to know that the underlying object cannot alias the
718
719
// non-addr-taken global, we must know that it would have to be an escape.
719
720
// Thus if the underlying object is a function argument, a load from
720
721
// a global, or the return of a function, it cannot alias. We can also
721
722
// recurse through PHI nodes and select nodes provided all of their inputs
722
723
// resolve to one of these known-escaping roots.
724
+
725
+ // A non-addr-taken global cannot alias with any non-pointer value.
726
+ // Check this early and exit.
727
+ if (!V->getType ()->isPointerTy ())
728
+ return true ;
729
+
723
730
SmallPtrSet<const Value *, 8 > Visited;
724
731
SmallVector<const Value *, 8 > Inputs;
725
732
Visited.insert (V);
@@ -762,6 +769,14 @@ bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
762
769
continue ;
763
770
}
764
771
772
+ if (CtxI)
773
+ if (auto *CPN = dyn_cast<ConstantPointerNull>(Input)) {
774
+ // Null pointer cannot alias with a non-addr-taken global.
775
+ const Function *F = CtxI->getFunction ();
776
+ if (!NullPointerIsDefined (F, CPN->getType ()->getAddressSpace ()))
777
+ continue ;
778
+ }
779
+
765
780
// Recurse through a limited number of selects, loads and PHIs. This is an
766
781
// arbitrary depth of 4, lower numbers could be used to fix compile time
767
782
// issues if needed, but this is generally expected to be only be important
@@ -820,7 +835,7 @@ bool GlobalsAAResult::invalidate(Module &, const PreservedAnalyses &PA,
820
835
// / address of the global isn't taken.
821
836
AliasResult GlobalsAAResult::alias (const MemoryLocation &LocA,
822
837
const MemoryLocation &LocB,
823
- AAQueryInfo &AAQI, const Instruction *) {
838
+ AAQueryInfo &AAQI, const Instruction *CtxI ) {
824
839
// Get the base object these pointers point to.
825
840
const Value *UV1 =
826
841
getUnderlyingObject (LocA.Ptr ->stripPointerCastsForAliasAnalysis ());
@@ -856,7 +871,7 @@ AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
856
871
if ((GV1 || GV2) && GV1 != GV2) {
857
872
const GlobalValue *GV = GV1 ? GV1 : GV2;
858
873
const Value *UV = GV1 ? UV2 : UV1;
859
- if (isNonEscapingGlobalNoAlias (GV, UV))
874
+ if (isNonEscapingGlobalNoAlias (GV, UV, CtxI ))
860
875
return AliasResult::NoAlias;
861
876
}
862
877
@@ -920,7 +935,7 @@ ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
920
935
!all_of (Objects, [&](const Value *V) {
921
936
return this ->alias (MemoryLocation::getBeforeOrAfter (V),
922
937
MemoryLocation::getBeforeOrAfter (GV), AAQI,
923
- nullptr ) == AliasResult::NoAlias;
938
+ Call ) == AliasResult::NoAlias;
924
939
}))
925
940
return ConservativeResult;
926
941
0 commit comments