@@ -713,13 +713,20 @@ static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,
713713// active, or to be forced to operate as a module pass that cannot co-exist
714714// with an alias analysis such as GMR.
715715bool GlobalsAAResult::isNonEscapingGlobalNoAlias (const GlobalValue *GV,
716- const Value *V) {
716+ const Value *V,
717+ const Instruction *CtxI) {
717718 // In order to know that the underlying object cannot alias the
718719 // non-addr-taken global, we must know that it would have to be an escape.
719720 // Thus if the underlying object is a function argument, a load from
720721 // a global, or the return of a function, it cannot alias. We can also
721722 // recurse through PHI nodes and select nodes provided all of their inputs
722723 // 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+
723730 SmallPtrSet<const Value *, 8 > Visited;
724731 SmallVector<const Value *, 8 > Inputs;
725732 Visited.insert (V);
@@ -762,6 +769,14 @@ bool GlobalsAAResult::isNonEscapingGlobalNoAlias(const GlobalValue *GV,
762769 continue ;
763770 }
764771
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+
765780 // Recurse through a limited number of selects, loads and PHIs. This is an
766781 // arbitrary depth of 4, lower numbers could be used to fix compile time
767782 // issues if needed, but this is generally expected to be only be important
@@ -820,7 +835,7 @@ bool GlobalsAAResult::invalidate(Module &, const PreservedAnalyses &PA,
820835// / address of the global isn't taken.
821836AliasResult GlobalsAAResult::alias (const MemoryLocation &LocA,
822837 const MemoryLocation &LocB,
823- AAQueryInfo &AAQI, const Instruction *) {
838+ AAQueryInfo &AAQI, const Instruction *CtxI ) {
824839 // Get the base object these pointers point to.
825840 const Value *UV1 =
826841 getUnderlyingObject (LocA.Ptr ->stripPointerCastsForAliasAnalysis ());
@@ -856,7 +871,7 @@ AliasResult GlobalsAAResult::alias(const MemoryLocation &LocA,
856871 if ((GV1 || GV2) && GV1 != GV2) {
857872 const GlobalValue *GV = GV1 ? GV1 : GV2;
858873 const Value *UV = GV1 ? UV2 : UV1;
859- if (isNonEscapingGlobalNoAlias (GV, UV))
874+ if (isNonEscapingGlobalNoAlias (GV, UV, CtxI ))
860875 return AliasResult::NoAlias;
861876 }
862877
@@ -920,7 +935,7 @@ ModRefInfo GlobalsAAResult::getModRefInfoForArgument(const CallBase *Call,
920935 !all_of (Objects, [&](const Value *V) {
921936 return this ->alias (MemoryLocation::getBeforeOrAfter (V),
922937 MemoryLocation::getBeforeOrAfter (GV), AAQI,
923- nullptr ) == AliasResult::NoAlias;
938+ Call ) == AliasResult::NoAlias;
924939 }))
925940 return ConservativeResult;
926941
0 commit comments