diff --git a/include/llvm/Analysis/MemoryLocation.h b/include/llvm/Analysis/MemoryLocation.h index 6b680000312..3e9014d3488 100644 --- a/include/llvm/Analysis/MemoryLocation.h +++ b/include/llvm/Analysis/MemoryLocation.h @@ -109,7 +109,11 @@ public: /// Return a location representing a particular argument of a call. static MemoryLocation getForArgument(ImmutableCallSite CS, unsigned ArgIdx, - const TargetLibraryInfo &TLI); + const TargetLibraryInfo *TLI); + static MemoryLocation getForArgument(ImmutableCallSite CS, unsigned ArgIdx, + const TargetLibraryInfo &TLI) { + return getForArgument(CS, ArgIdx, &TLI); + } explicit MemoryLocation(const Value *Ptr = nullptr, LocationSize Size = UnknownSize, diff --git a/lib/Analysis/AliasSetTracker.cpp b/lib/Analysis/AliasSetTracker.cpp index 45d1b0d3e1c..d0997932260 100644 --- a/lib/Analysis/AliasSetTracker.cpp +++ b/lib/Analysis/AliasSetTracker.cpp @@ -452,6 +452,46 @@ void AliasSetTracker::add(Instruction *I) { return add(MSI); if (AnyMemTransferInst *MTI = dyn_cast(I)) return add(MTI); + + // Handle all calls with known mod/ref sets genericall + CallSite CS(I); + if (CS && CS.onlyAccessesArgMemory()) { + auto getAccessFromModRef = [](ModRefInfo MRI) { + if (isRefSet(MRI) && isModSet(MRI)) + return AliasSet::ModRefAccess; + else if (isModSet(MRI)) + return AliasSet::ModAccess; + else if (isRefSet(MRI)) + return AliasSet::RefAccess; + else + return AliasSet::NoAccess; + + }; + + ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(CS)); + + // Some intrinsics are marked as modifying memory for control flow + // modelling purposes, but don't actually modify any specific memory + // location. + using namespace PatternMatch; + if (I->use_empty() && match(I, m_Intrinsic())) + CallMask = clearMod(CallMask); + + for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) { + const Value *Arg = *AI; + if (!Arg->getType()->isPointerTy()) + continue; + unsigned ArgIdx = std::distance(CS.arg_begin(), AI); + MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx, + nullptr); + ModRefInfo ArgMask = AA.getArgModRefInfo(CS, ArgIdx); + ArgMask = intersectModRef(CallMask, ArgMask); + if (!isNoModRef(ArgMask)) + addPointer(ArgLoc, getAccessFromModRef(ArgMask)); + } + return; + } + return addUnknown(I); } diff --git a/lib/Analysis/MemoryLocation.cpp b/lib/Analysis/MemoryLocation.cpp index 43cebcd7a30..326944f2964 100644 --- a/lib/Analysis/MemoryLocation.cpp +++ b/lib/Analysis/MemoryLocation.cpp @@ -108,7 +108,7 @@ MemoryLocation MemoryLocation::getForDest(const AnyMemIntrinsic *MI) { MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS, unsigned ArgIdx, - const TargetLibraryInfo &TLI) { + const TargetLibraryInfo *TLI) { AAMDNodes AATags; CS->getAAMetadata(AATags); const Value *Arg = CS.getArgument(ArgIdx); @@ -163,8 +163,9 @@ MemoryLocation MemoryLocation::getForArgument(ImmutableCallSite CS, // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16 // whenever possible. LibFunc F; - if (CS.getCalledFunction() && TLI.getLibFunc(*CS.getCalledFunction(), F) && - F == LibFunc_memset_pattern16 && TLI.has(F)) { + if (TLI && CS.getCalledFunction() && + TLI->getLibFunc(*CS.getCalledFunction(), F) && + F == LibFunc_memset_pattern16 && TLI->has(F)) { assert((ArgIdx == 0 || ArgIdx == 1) && "Invalid argument index for memset_pattern16"); if (ArgIdx == 1) diff --git a/test/Analysis/AliasSet/argmemonly.ll b/test/Analysis/AliasSet/argmemonly.ll index 6302936f41b..58515d14101 100644 --- a/test/Analysis/AliasSet/argmemonly.ll +++ b/test/Analysis/AliasSet/argmemonly.ll @@ -4,10 +4,9 @@ @d = global i8 2, align 1 ; CHECK: Alias sets for function 'test_alloca_argmemonly': -; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 1 pointer values. +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 pointer values. ; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %a, 1) -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 1 Unknown instructions: call void @my_memcpy(i8* %d, i8* %s, i64 1) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod/Ref Pointers: (i8* %d, unknown), (i8* %s, unknown) define void @test_alloca_argmemonly(i8* %s, i8* %d) { entry: %a = alloca i8, align 1 @@ -17,9 +16,9 @@ entry: } ; CHECK: Alias sets for function 'test_readonly_arg' -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod/Ref Pointers: (i8* %s, 1) -; CHECK-NEXT: 1 Unknown instructions: call void @my_memcpy(i8* %d, i8* %s, i64 1) +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %d, unknown) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Ref Pointers: (i8* %s, unknown) define i8 @test_readonly_arg(i8* noalias %s, i8* noalias %d) { entry: call void @my_memcpy(i8* %d, i8* %s, i64 1) @@ -28,10 +27,9 @@ entry: } ; CHECK: Alias sets for function 'test_noalias_argmemonly': -; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 1 pointer values. +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 pointer values. ; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %a, 1) -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: Unknown instructions: call void @my_memmove(i8* %d, i8* %s, i64 1) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod/Ref Pointers: (i8* %d, unknown), (i8* %s, unknown) define void @test_noalias_argmemonly(i8* noalias %a, i8* %s, i8* %d) { entry: store i8 1, i8* %a, align 1 @@ -40,9 +38,9 @@ entry: } ; CHECK: Alias sets for function 'test5': -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 2 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 3] may alias, Mod/Ref Pointers: (i8* %a, 1), (i8* %b, 1) -; CHECK-NEXT: 1 Unknown instructions: call void @my_memcpy(i8* %b, i8* %a, i64 1) +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (i8* %a, unknown) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %b, unknown) define void @test5(i8* noalias %a, i8* noalias %b) { entry: store i8 1, i8* %a, align 1 @@ -52,9 +50,9 @@ entry: } ; CHECK: Alias sets for function 'test_argcollapse': -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 2 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 3] may alias, Mod/Ref Pointers: (i8* %a, 1), (i8* %b, 1) -; CHECK-NEXT: 1 Unknown instructions: call void @my_memmove(i8* %b, i8* %a, i64 1) +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (i8* %a, unknown) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (i8* %b, unknown) define void @test_argcollapse(i8* noalias %a, i8* noalias %b) { entry: store i8 1, i8* %a, align 1 @@ -64,10 +62,9 @@ entry: } ; CHECK: Alias sets for function 'test_memcpy1': -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 0 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 2 Unknown instructions: call void @my_memcpy(i8* %b, i8* %a, i64 1), call void @my_memcpy(i8* %a, i8* %b, i64 1) - +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (i8* %b, unknown) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (i8* %a, unknown) define void @test_memcpy1(i8* noalias %a, i8* noalias %b) { entry: call void @my_memcpy(i8* %b, i8* %a, i64 1) @@ -76,9 +73,8 @@ entry: } ; CHECK: Alias sets for function 'test_memset1': -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 0 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 1 Unknown instructions: call void @my_memset(i8* %a, i8 0, i64 1) +; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %a, unknown) define void @test_memset1() { entry: %a = alloca i8, align 1 @@ -87,9 +83,8 @@ entry: } ; CHECK: Alias sets for function 'test_memset2': -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 0 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 1 Unknown instructions: call void @my_memset(i8* %a, i8 0, i64 1) +; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %a, unknown) define void @test_memset2(i8* %a) { entry: call void @my_memset(i8* %a, i8 0, i64 1) @@ -97,9 +92,8 @@ entry: } ; CHECK: Alias sets for function 'test_memset3': -; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 0 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 2 Unknown instructions: call void @my_memset(i8* %a, i8 0, i64 1), call void @my_memset(i8* %b, i8 0, i64 1) +; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 2 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod Pointers: (i8* %a, unknown), (i8* %b, unknown) define void @test_memset3(i8* %a, i8* %b) { entry: call void @my_memset(i8* %a, i8 0, i64 1) @@ -107,12 +101,12 @@ entry: ret void } +;; PICKUP HERE + ; CHECK: Alias sets for function 'test_memset4': -; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 0 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 1 Unknown instructions: call void @my_memset(i8* %a, i8 0, i64 1) -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 1 Unknown instructions: call void @my_memset(i8* %b, i8 0, i64 1) +; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values. +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %a, unknown) +; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (i8* %b, unknown) define void @test_memset4(i8* noalias %a, i8* noalias %b) { entry: call void @my_memset(i8* %a, i8 0, i64 1) @@ -126,9 +120,7 @@ declare void @my_memmove(i8* nocapture, i8* nocapture readonly, i64) argmemonly ; CHECK: Alias sets for function 'test_attribute_intersect': -; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 1 pointer values. -; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] may alias, Mod/Ref -; CHECK-NEXT: 1 Unknown instructions: call void @attribute_intersect(i8* %a) +; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values. ; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Ref Pointers: (i8* %a, 1) define i8 @test_attribute_intersect(i8* noalias %a) { entry: diff --git a/test/Transforms/LICM/argmemonly-call.ll b/test/Transforms/LICM/argmemonly-call.ll index c66391bd11a..b9e0818e80b 100644 --- a/test/Transforms/LICM/argmemonly-call.ll +++ b/test/Transforms/LICM/argmemonly-call.ll @@ -114,11 +114,10 @@ loop: br label %loop } -; FIXME: argument aliasing should let us do this transform define void @test7(i32* noalias %loc, i32* noalias %loc2) { ; CHECK-LABEL: @test7 -; CHECK-LABEL: loop: ; CHECK: %val = load i32, i32* %loc2 +; CHECK-LABEL: loop: ; CHECK: @custom_memcpy br label %loop