Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,9 @@ class SelectionDAG {
/// stack arguments from being clobbered.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain);

std::pair<SDValue, SDValue> getMemcmp(SDValue Chain, const SDLoc &dl,
SDValue Dst, SDValue Src, SDValue Size,
const CallInst *CI);
/* \p CI if not null is the memset call being lowered.
* \p OverrideTailCall is an optional parameter that can be used to override
* the tail call optimization decision. */
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/CodeGen/SelectionDAGTargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace llvm {

class CallInst;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort forward declarations alphabetically

class SelectionDAG;

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -118,8 +119,7 @@ class SelectionDAGTargetInfo {
virtual std::pair<SDValue, SDValue>
EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain,
SDValue Op1, SDValue Op2, SDValue Op3,
MachinePointerInfo Op1PtrInfo,
MachinePointerInfo Op2PtrInfo) const {
const CallInst *CI) const {
return std::make_pair(SDValue(), SDValue());
}

Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/RuntimeLibcalls.td
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ foreach FPTy = ["F32", "F64", "F128", "PPCF128"] in {
}

// Memory
def MEMCMP : RuntimeLibcall;
def MEMCPY : RuntimeLibcall;
def MEMMOVE : RuntimeLibcall;
def MEMSET : RuntimeLibcall;
Expand Down Expand Up @@ -1739,12 +1740,14 @@ defset list<RuntimeLibcallImpl> PPCRuntimeLibcalls = {
}

defset list<RuntimeLibcallImpl> PPC64AIXCallList = {
def ___memcmp64 : RuntimeLibcallImpl<MEMCMP>;
def ___memmove64 : RuntimeLibcallImpl<MEMCPY>;
def ___memset64 : RuntimeLibcallImpl<MEMSET>;
def ___bzero64 : RuntimeLibcallImpl<BZERO>;
}

defset list<RuntimeLibcallImpl> PPC32AIXCallList = {
def ___memcmp : RuntimeLibcallImpl<MEMCMP>;
def ___memmove : RuntimeLibcallImpl<MEMMOVE>;
def ___memset : RuntimeLibcallImpl<MEMSET>;
def ___bzero : RuntimeLibcallImpl<BZERO>;
Expand Down
38 changes: 38 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8821,6 +8821,44 @@ static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI,
}
}

std::pair<SDValue, SDValue>
SelectionDAG::getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Mem0,
SDValue Mem1, SDValue Size, const CallInst *CI) {
const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
if (!LibCallName)
return {};

// Emit a library call.
auto GetEntry = [](Type *Ty, SDValue &SDV) {
TargetLowering::ArgListEntry E;
E.Ty = Ty;
E.Node = SDV;
return E;
};

PointerType *PT = PointerType::getUnqual(*getContext());
TargetLowering::ArgListTy Args = {
GetEntry(PT, Mem0), GetEntry(PT, Mem1),
GetEntry(getDataLayout().getIntPtrType(*getContext()), Size)};

TargetLowering::CallLoweringInfo CLI(*this);
bool IsTailCall = false;
bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
IsTailCall = CI && CI->isTailCall() &&
isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg);

CLI.setDebugLoc(dl)
.setChain(Chain)
.setLibCallee(
TLI->getLibcallCallingConv(RTLIB::MEMCMP),
Type::getInt32Ty(*getContext()),
getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
std::move(Args))
.setTailCall(IsTailCall);

return TLI->LowerCallTo(CLI);
}

SDValue SelectionDAG::getMemcpy(
SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9056,7 +9056,7 @@ bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
getValue(Size), MachinePointerInfo(LHS), MachinePointerInfo(RHS));
getValue(Size), &I);
if (Res.first.getNode()) {
processIntegerCallValue(I, Res.first, true);
PendingLoads.push_back(Res.second);
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Target/PowerPC/PPCSelectionDAGInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ bool PPCSelectionDAGInfo::isTargetStrictFPOpcode(unsigned Opcode) const {
return Opcode >= PPCISD::FIRST_STRICTFP_OPCODE &&
Opcode <= PPCISD::LAST_STRICTFP_OPCODE;
}

std::pair<SDValue, SDValue> PPCSelectionDAGInfo::EmitTargetCodeForMemcmp(
SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2,
SDValue Op3, const CallInst *CI) const {
return DAG.getMemcmp(Chain, dl, Op1, Op2, Op3, CI);
}
5 changes: 5 additions & 0 deletions llvm/lib/Target/PowerPC/PPCSelectionDAGInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class PPCSelectionDAGInfo : public SelectionDAGTargetInfo {
bool isTargetMemoryOpcode(unsigned Opcode) const override;

bool isTargetStrictFPOpcode(unsigned Opcode) const override;

std::pair<SDValue, SDValue>
EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain,
SDValue Op1, SDValue Op2, SDValue Op3,
const CallInst *CI) const;
};

} // namespace llvm
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/SystemZ/SystemZSelectionDAGInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ static SDValue addIPMSequence(const SDLoc &DL, SDValue CCReg,

std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemcmp(
SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1,
SDValue Src2, SDValue Size, MachinePointerInfo Op1PtrInfo,
MachinePointerInfo Op2PtrInfo) const {
SDValue Src2, SDValue Size, const CallInst *CI) const {
SDValue CCReg;
// Swap operands to invert CC == 1 vs. CC == 2 cases.
if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/SystemZ/SystemZSelectionDAGInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class SystemZSelectionDAGInfo : public SelectionDAGTargetInfo {
std::pair<SDValue, SDValue>
EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain,
SDValue Src1, SDValue Src2, SDValue Size,
MachinePointerInfo Op1PtrInfo,
MachinePointerInfo Op2PtrInfo) const override;
const CallInst *CI) const override;

std::pair<SDValue, SDValue>
EmitTargetCodeForMemchr(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain,
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/PowerPC/memintr32.ll
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ define i32 @memcmp_test(ptr nocapture noundef readonly %ptr1, ptr nocapture noun
; CHECK-AIX-32-P9-NEXT: mflr r0
; CHECK-AIX-32-P9-NEXT: stwu r1, -64(r1)
; CHECK-AIX-32-P9-NEXT: stw r0, 72(r1)
; CHECK-AIX-32-P9-NEXT: bl .memcmp[PR]
; CHECK-AIX-32-P9-NEXT: bl .___memcmp[PR]
; CHECK-AIX-32-P9-NEXT: nop
; CHECK-AIX-32-P9-NEXT: addi r1, r1, 64
; CHECK-AIX-32-P9-NEXT: lwz r0, 8(r1)
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/PowerPC/memintr64.ll
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ define noundef i32 @_Z11memcmp_testPKvS0_m(ptr noundef readonly captures(none) %
; CHECK-AIX-64-P9-NEXT: mflr r0
; CHECK-AIX-64-P9-NEXT: stdu r1, -112(r1)
; CHECK-AIX-64-P9-NEXT: std r0, 128(r1)
; CHECK-AIX-64-P9-NEXT: bl .memcmp[PR]
; CHECK-AIX-64-P9-NEXT: bl .___memcmp64[PR]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a tail call? Make sure both tail and non-tail cases are tested?

Copy link
Contributor Author

@diggerlin diggerlin Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a tail call? Make sure both tail and non-tail cases are tested?

you are correct, the IR in the test case is tail call, but in AIX, it is not tail callable when using a toc - the reason being you have to do a toc restore after returning from the definition of memcmp since it lives in another library and the call sequence modifies r2 to point to the defining modules toc.

; CHECK-AIX-64-P9-NEXT: nop
Comment on lines +42 to 43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diggerlin:
There should be no TOC modification/use for a call to a millicode routine. The nop is not necessary. Additionally, the branch can be bla. It seems that the tail call case should be tested after all?

Copy link
Contributor Author

@diggerlin diggerlin Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying. However, LLVM does not support AIX tail calls yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The triple underscore names allow the compiler to refer to the millicode functions symbolically. The linker will convert them to bla - it knows the constant addresses.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nop is unnecessary, tail call or not. Do we want to refine that at some point?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nop is something we should probably address at some point. I suspect for more than just millicode.

; CHECK-AIX-64-P9-NEXT: addi r1, r1, 112
; CHECK-AIX-64-P9-NEXT: ld r0, 16(r1)
Expand Down