diff --git a/llvm/lib/Analysis/MemoryLocation.cpp b/llvm/lib/Analysis/MemoryLocation.cpp --- a/llvm/lib/Analysis/MemoryLocation.cpp +++ b/llvm/lib/Analysis/MemoryLocation.cpp @@ -223,6 +223,18 @@ } return MemoryLocation(Arg, Size, AATags); } + case LibFunc_strncpy: { + assert((ArgIdx == 0 || ArgIdx == 1) && + "Invalid argument index for strncpy"); + LocationSize Size = LocationSize::afterPointer(); + if (const auto *Len = dyn_cast(Call->getArgOperand(2))) { + // strncpy is guaranteed to write Len bytes, but only reads up to Len + // bytes. + Size = ArgIdx == 0 ? LocationSize::precise(Len->getZExtValue()) + : LocationSize::upperBound(Len->getZExtValue()); + } + return MemoryLocation(Arg, Size, AATags); + } case LibFunc_memset_pattern16: assert((ArgIdx == 0 || ArgIdx == 1) && "Invalid argument index for memset_pattern16"); diff --git a/llvm/test/Analysis/BasicAA/libfuncs.ll b/llvm/test/Analysis/BasicAA/libfuncs.ll --- a/llvm/test/Analysis/BasicAA/libfuncs.ll +++ b/llvm/test/Analysis/BasicAA/libfuncs.ll @@ -136,9 +136,9 @@ ; CHECK-NEXT: Just Ref: Ptr: i8* %b <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) ; CHECK-NEXT: Just Mod: Ptr: i8* %res <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) ; CHECK-NEXT: Just Mod: Ptr: i8* %a.gep.1 <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) -; CHECK-NEXT: Just Mod: Ptr: i8* %a.gep.5 <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) +; CHECK-NEXT: NoModRef: Ptr: i8* %a.gep.5 <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) ; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.1 <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) -; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.5 <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) +; CHECK-NEXT: NoModRef: Ptr: i8* %b.gep.5 <-> %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4) ; entry: %res = tail call i8* @strncpy(i8* %a, i8* %b, i64 4)