Using AliasAnalysis for memmove lowering

SystemZ can implement a memmove (overlapping memcpy) if it knows the order in memory of the Dst and Src addresses. If there is no way to tell, a comparison can me done and then one of two instructions can be used, either going “left to right” or “right to left” in memory.

It would be nice to use AliasAnalysis and simplify this in the cases where this can be known at compile time, and skip the comparison step. In SelectionDAG::getMemmove(), AAInfo and BatchAA are available, so I started experimenting with it, but it seems I am not quite doing this right. I can’t get the Offset difference between Dst and Src that I am looking for. If I use this:

SDValue SelectionDAG::getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst,
SDValue Src, SDValue Size, Align Alignment,
bool isVol, const CallInst *CI,
std::optional OverrideTailCall,
MachinePointerInfo DstPtrInfo,
MachinePointerInfo SrcPtrInfo,
const AAMDNodes &AAInfo,
BatchAAResults *BatchAA) {
...
const Value *DstV = dyn_cast_if_present<const Value *>(DstPtrInfo.V);
const Value *SrcV = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
if (DstV && SrcV) {
AliasResult AAR = BatchAA->alias(MemoryLocation::getBeforeOrAfter(DstV, AAInfo),
MemoryLocation::getBeforeOrAfter(SrcV, AAInfo));
...

, I get just a lot of MayAlias for everything.

If I provide a constant size to the MemoryLocation, there seem to be a slight difference in the AAR: just very rarely I see “PartialAlias”, but even then there is no Offset value.

So my question is: how could BatchAA be used here to provide the needed answer to the question of the order of Dst and Src in memory?

@MaskRay @nikic @paulwalker-arm @MacDue @chill

Pretty sure AA currently can’t do this.

Can you just analyze the pointer values directly? I would expect that most cases where the transform is practical involve a constant offset between the source and destination pointers.

Yes, AA is not suitable for this. You’ll want to stripAndAccumulateConstantOffsets() to a common base and compare the offsets.