Skip to content

Commit bc991e7

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 4f991cc commit bc991e7

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 should be removed

llvm/include/llvm/IR/RuntimeLibcalls.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ struct RuntimeLibcallsInfo {
106106
SoftFloatCompareLibcallPredicates[Call] = Pred;
107107
}
108108

109+
/// Return a function name compatible with RTLIB::MEMCPY, or nullptr if fully
110+
/// unsupported.
111+
const char *getMemcpyName() const {
112+
if (const char *Memcpy = getLibcallName(RTLIB::MEMCPY))
113+
return Memcpy;
114+
115+
// Fallback to memmove if memcpy isn't available.
116+
return getLibcallName(RTLIB::MEMMOVE);
117+
}
118+
109119
private:
110120
/// Stores the name each libcall.
111121
const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL + 1] = {nullptr};

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
@@ -8776,11 +8776,12 @@ SDValue SelectionDAG::getMemcpy(
87768776
// FIXME: pass in SDLoc
87778777
TargetLowering::CallLoweringInfo CLI(*this);
87788778
bool IsTailCall = false;
8779+
const char *MemCpyName = TLI->getMemcpyName();
8780+
87798781
if (OverrideTailCall.has_value()) {
87808782
IsTailCall = *OverrideTailCall;
87818783
} else {
8782-
bool LowersToMemcpy =
8783-
TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8784+
bool LowersToMemcpy = StringRef(MemCpyName) == StringRef("memcpy");
87848785
bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
87858786
IsTailCall = CI && CI->isTailCall() &&
87868787
isInTailCallPosition(*CI, getTarget(),
@@ -8789,11 +8790,11 @@ SDValue SelectionDAG::getMemcpy(
87898790

87908791
CLI.setDebugLoc(dl)
87918792
.setChain(Chain)
8792-
.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8793-
Dst.getValueType().getTypeForEVT(*getContext()),
8794-
getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8795-
TLI->getPointerTy(getDataLayout())),
8796-
std::move(Args))
8793+
.setLibCallee(
8794+
TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8795+
Dst.getValueType().getTypeForEVT(*getContext()),
8796+
getExternalSymbol(MemCpyName, TLI->getPointerTy(getDataLayout())),
8797+
std::move(Args))
87978798
.setDiscardResult()
87988799
.setTailCall(IsTailCall);
87998800

llvm/lib/IR/RuntimeLibcalls.cpp

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

417417
if (TT.isOSAIX()) {
418418
bool isPPC64 = TT.isPPC64();
419-
setLibcallName(RTLIB::MEMCPY, isPPC64 ? "___memmove64" : "___memmove");
419+
setLibcallName(RTLIB::MEMCPY, nullptr);
420420
setLibcallName(RTLIB::MEMMOVE, isPPC64 ? "___memmove64" : "___memmove");
421421
setLibcallName(RTLIB::MEMSET, isPPC64 ? "___memset64" : "___memset");
422422
setLibcallName(RTLIB::BZERO, isPPC64 ? "___bzero64" : "___bzero");

0 commit comments

Comments
 (0)