Skip to content

Commit 82c6176

Browse files
committed
PowerPC: Stop reporting memcpy as an alias of memmove on AIX
Instead of reporting ___memmove as an implementation of memcpy, make it unavailable and let the lowering logic consider memmove as a fallback path. This avoids a special case 1:N mapping for libcall implementations.
1 parent 95bbaca commit 82c6176

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3572,6 +3572,8 @@ class LLVM_ABI TargetLoweringBase {
35723572
return Libcalls.getLibcallName(Call);
35733573
}
35743574

3575+
const char *getMemcpyName() const { return Libcalls.getMemcpyName(); }
3576+
35753577
/// Override the default CondCode to be used to test the result of the
35763578
/// comparison libcall against zero.
35773579
/// FIXME: This can't be merged with 'RuntimeLibcallsInfo' because of the ISD.

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ struct RuntimeLibcallsInfo {
8686
return ArrayRef(LibcallRoutineNames).drop_back();
8787
}
8888

89+
/// Return a function name compatible with RTLIB::MEMCPY, or nullptr if fully
90+
/// unsupported.
91+
const char *getMemcpyName() const {
92+
if (const char *Memcpy = getLibcallName(RTLIB::MEMCPY))
93+
return Memcpy;
94+
95+
// Fallback to memmove if memcpy isn't available.
96+
return getLibcallName(RTLIB::MEMMOVE);
97+
}
98+
8999
private:
90100
/// Stores the name each libcall.
91101
const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL + 1];

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,26 +672,30 @@ llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
672672
auto &TLI = *MIRBuilder.getMF().getSubtarget().getTargetLowering();
673673
RTLIB::Libcall RTLibcall;
674674
unsigned Opc = MI.getOpcode();
675+
const char *Name;
675676
switch (Opc) {
676677
case TargetOpcode::G_BZERO:
677678
RTLibcall = RTLIB::BZERO;
679+
Name = TLI.getLibcallName(RTLibcall);
678680
break;
679681
case TargetOpcode::G_MEMCPY:
680682
RTLibcall = RTLIB::MEMCPY;
683+
Name = TLI.getMemcpyName();
681684
Args[0].Flags[0].setReturned();
682685
break;
683686
case TargetOpcode::G_MEMMOVE:
684687
RTLibcall = RTLIB::MEMMOVE;
688+
Name = TLI.getLibcallName(RTLibcall);
685689
Args[0].Flags[0].setReturned();
686690
break;
687691
case TargetOpcode::G_MEMSET:
688692
RTLibcall = RTLIB::MEMSET;
693+
Name = TLI.getLibcallName(RTLibcall);
689694
Args[0].Flags[0].setReturned();
690695
break;
691696
default:
692697
llvm_unreachable("unsupported opcode");
693698
}
694-
const char *Name = TLI.getLibcallName(RTLibcall);
695699

696700
// Unsupported libcall on the target.
697701
if (!Name) {

llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ static bool canEmitLibcall(const TargetMachine *TM, Function *F,
231231
return TLI->getLibcallName(LC) != nullptr;
232232
}
233233

234+
static bool canEmitMemcpy(const TargetMachine *TM, Function *F) {
235+
// TODO: Should this consider the address space of the memcpy?
236+
if (!TM)
237+
return true;
238+
const TargetLowering *TLI = TM->getSubtargetImpl(*F)->getTargetLowering();
239+
return TLI->getMemcpyName() != nullptr;
240+
}
241+
234242
// Return a value appropriate for use with the memset_pattern16 libcall, if
235243
// possible and if we know how. (Adapted from equivalent helper in
236244
// LoopIdiomRecognize).
@@ -300,8 +308,7 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
300308
Function *ParentFunc = Memcpy->getFunction();
301309
const TargetTransformInfo &TTI = LookupTTI(*ParentFunc);
302310
if (shouldExpandMemIntrinsicWithSize(Memcpy->getLength(), TTI)) {
303-
if (UseMemIntrinsicLibFunc &&
304-
canEmitLibcall(TM, ParentFunc, RTLIB::MEMCPY))
311+
if (UseMemIntrinsicLibFunc && canEmitMemcpy(TM, ParentFunc))
305312
break;
306313

307314
// TODO: For optsize, emit the loop into a separate function

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8765,11 +8765,12 @@ SDValue SelectionDAG::getMemcpy(
87658765
// FIXME: pass in SDLoc
87668766
TargetLowering::CallLoweringInfo CLI(*this);
87678767
bool IsTailCall = false;
8768+
const char *MemCpyName = TLI->getMemcpyName();
8769+
87688770
if (OverrideTailCall.has_value()) {
87698771
IsTailCall = *OverrideTailCall;
87708772
} else {
8771-
bool LowersToMemcpy =
8772-
TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8773+
bool LowersToMemcpy = StringRef(MemCpyName) == StringRef("memcpy");
87738774
bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
87748775
IsTailCall = CI && CI->isTailCall() &&
87758776
isInTailCallPosition(*CI, getTarget(),
@@ -8778,11 +8779,11 @@ SDValue SelectionDAG::getMemcpy(
87788779

87798780
CLI.setDebugLoc(dl)
87808781
.setChain(Chain)
8781-
.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8782-
Dst.getValueType().getTypeForEVT(*getContext()),
8783-
getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8784-
TLI->getPointerTy(getDataLayout())),
8785-
std::move(Args))
8782+
.setLibCallee(
8783+
TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8784+
Dst.getValueType().getTypeForEVT(*getContext()),
8785+
getExternalSymbol(MemCpyName, TLI->getPointerTy(getDataLayout())),
8786+
std::move(Args))
87868787
.setDiscardResult()
87878788
.setTailCall(IsTailCall);
87888789

llvm/lib/IR/RuntimeLibcalls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT) {
325325

326326
if (TT.isOSAIX()) {
327327
bool isPPC64 = TT.isPPC64();
328-
setLibcallName(RTLIB::MEMCPY, isPPC64 ? "___memmove64" : "___memmove");
328+
setLibcallName(RTLIB::MEMCPY, nullptr);
329329
setLibcallName(RTLIB::MEMMOVE, isPPC64 ? "___memmove64" : "___memmove");
330330
setLibcallName(RTLIB::MEMSET, isPPC64 ? "___memset64" : "___memset");
331331
setLibcallName(RTLIB::BZERO, isPPC64 ? "___bzero64" : "___bzero");

0 commit comments

Comments
 (0)