1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

[AST] Generalize argument specific aliasing

AliasSetTracker has special case handling for memset, memcpy and memmove which pre-existed argmemonly on functions and readonly and writeonly on arguments. This patch generalizes it using the AA infrastructure to any call correctly annotated.

The motivation here is to cut down on confusion, not performance per se. For most instructions, there is a direct mapping to alias set. However, this is not guaranteed by the interface and was not in fact true for these three intrinsics *and only these three intrinsics*. I kept getting myself confused about this invariant, so I figured it would be good to clearly distinguish between a instructions and alias sets. Calls happened to be an easy target.

The nice side effect is that custom implementations of memset/memcpy/memmove - including wrappers discovered by IPO - can now be optimized the same as builts by LICM.

Note: The actual removal of the memset/memtransfer specific handling will happen in a follow on NFC patch.  It was originally part of this one, but separate for ease of review and rebase.

Differential Revision: https://reviews.llvm.org/D50730

llvm-svn: 341713
This commit is contained in:
Philip Reames 2018-09-07 21:36:11 +00:00
parent e1d575e20c
commit 6fce828d58
5 changed files with 78 additions and 42 deletions

View File

@ -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,

View File

@ -452,6 +452,46 @@ void AliasSetTracker::add(Instruction *I) {
return add(MSI);
if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(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<Intrinsic::invariant_start>()))
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);
}

View File

@ -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)

View File

@ -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:

View File

@ -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