@@ -487,7 +487,7 @@ static bool allCallersPassValidPointerForArgument(
487487
488488// Try to prove that all Calls to F do not modify the memory pointed to by Arg,
489489// using alias analysis local to each caller of F.
490- static bool isArgUnmodifiedByAllCalls (Argument *Arg,
490+ static bool isArgUnmodifiedByAllCalls (Argument *Arg, const LocationSize &Size,
491491 FunctionAnalysisManager &FAM) {
492492 for (User *U : Arg->getParent ()->users ()) {
493493
@@ -499,6 +499,9 @@ static bool isArgUnmodifiedByAllCalls(Argument *Arg,
499499 MemoryLocation Loc =
500500 MemoryLocation::getForArgument (Call, Arg->getArgNo (), nullptr );
501501
502+ // Refine the MemoryLocation Size using information from Loads of Arg.
503+ Loc = Loc.getWithNewSize (Size);
504+
502505 AAResults &AAR = FAM.getResult <AAManager>(*Call->getFunction ());
503506 // Bail as soon as we find a Call where Arg may be modified.
504507 if (isModSet (AAR.getModRefInfo (Call, Loc)))
@@ -743,10 +746,16 @@ static bool findArgParts(Argument *Arg, const DataLayout &DL, AAResults &AAR,
743746 // Okay, now we know that the argument is only used by load instructions, and
744747 // it is safe to unconditionally perform all of them.
745748
746- // If we can determine that no call to the Function modifies the memory
747- // pointed to by Arg, through alias analysis using actual arguments in the
749+ // If we can determine that no call to the Function modifies the memory region
750+ // accessed through Arg, through alias analysis using actual arguments in the
748751 // callers, we know that it is guaranteed to be safe to promote the argument.
749- if (isArgUnmodifiedByAllCalls (Arg, FAM))
752+
753+ // Compute the size of the memory region accessed by the Loads through Arg.
754+ LocationSize Size = LocationSize::precise (0 );
755+ for (LoadInst *Load : Loads) {
756+ Size = Size.unionWith (MemoryLocation::get (Load).Size );
757+ }
758+ if (isArgUnmodifiedByAllCalls (Arg, Size, FAM))
750759 return true ;
751760
752761 // Otherwise, use alias analysis to check if the pointer is guaranteed to not
0 commit comments