use repmovsb when optimizing forminsize

llvm-svn: 300960
This commit is contained in:
Clement Courbet 2017-04-21 09:20:55 +00:00
parent 203fc17797
commit d5f6182bec
2 changed files with 57 additions and 8 deletions

View File

@ -181,6 +181,24 @@ SDValue X86SelectionDAGInfo::EmitTargetCodeForMemset(
return Chain; return Chain;
} }
namespace {
// Represents a cover of a buffer of SizeVal bytes with blocks of size
// AVT, as well as how many bytes remain (BytesLeft is always smaller than
// the block size).
struct RepMovsRepeats {
RepMovsRepeats(const uint64_t SizeVal, const MVT& AVT) {
const unsigned UBytes = AVT.getSizeInBits() / 8;
Count = SizeVal / UBytes;
BytesLeft = SizeVal % UBytes;
}
unsigned Count;
unsigned BytesLeft;
};
} // namespace
SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy( SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
SDValue Size, unsigned Align, bool isVolatile, bool AlwaysInline, SDValue Size, unsigned Align, bool isVolatile, bool AlwaysInline,
@ -231,14 +249,18 @@ SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
// QWORD aligned // QWORD aligned
AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32; AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
unsigned UBytes = AVT.getSizeInBits() / 8; RepMovsRepeats Repeats(SizeVal, AVT);
unsigned CountVal = SizeVal / UBytes; if (Repeats.BytesLeft > 0 &&
SDValue Count = DAG.getIntPtrConstant(CountVal, dl); DAG.getMachineFunction().getFunction()->optForMinSize()) {
unsigned BytesLeft = SizeVal % UBytes; // When agressively optimizing for size, avoid generating the code to handle
// BytesLeft.
AVT = MVT::i8;
Repeats = RepMovsRepeats(SizeVal, AVT);
}
SDValue InFlag; SDValue InFlag;
Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX, Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
Count, InFlag); DAG.getIntPtrConstant(Repeats.Count, dl), InFlag);
InFlag = Chain.getValue(1); InFlag = Chain.getValue(1);
Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI, Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
Dst, InFlag); Dst, InFlag);
@ -253,9 +275,9 @@ SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
SmallVector<SDValue, 4> Results; SmallVector<SDValue, 4> Results;
Results.push_back(RepMovs); Results.push_back(RepMovs);
if (BytesLeft) { if (Repeats.BytesLeft) {
// Handle the last 1 - 7 bytes. // Handle the last 1 - 7 bytes.
unsigned Offset = SizeVal - BytesLeft; unsigned Offset = SizeVal - Repeats.BytesLeft;
EVT DstVT = Dst.getValueType(); EVT DstVT = Dst.getValueType();
EVT SrcVT = Src.getValueType(); EVT SrcVT = Src.getValueType();
EVT SizeVT = Size.getValueType(); EVT SizeVT = Size.getValueType();
@ -266,7 +288,8 @@ SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
DAG.getNode(ISD::ADD, dl, SrcVT, Src, DAG.getNode(ISD::ADD, dl, SrcVT, Src,
DAG.getConstant(Offset, dl, DAG.getConstant(Offset, dl,
SrcVT)), SrcVT)),
DAG.getConstant(BytesLeft, dl, SizeVT), DAG.getConstant(Repeats.BytesLeft, dl,
SizeVT),
Align, isVolatile, AlwaysInline, false, Align, isVolatile, AlwaysInline, false,
DstPtrInfo.getWithOffset(Offset), DstPtrInfo.getWithOffset(Offset),
SrcPtrInfo.getWithOffset(Offset))); SrcPtrInfo.getWithOffset(Offset)));

View File

@ -17,3 +17,29 @@ define void @test1(%struct.large* nocapture %x) nounwind {
; FAST: rep;movsb ; FAST: rep;movsb
; HASWELL: rep;movsb ; HASWELL: rep;movsb
} }
define void @test2(%struct.large* nocapture %x) nounwind minsize {
call void @foo(%struct.large* align 8 byval %x)
ret void
; ALL-LABEL: test2:
; NOFAST: rep;movsq
; GENERIC: rep;movsq
; FAST: rep;movsb
; HASWELL: rep;movsb
}
%struct.large_oddsize = type { [4095 x i8] }
declare void @foo_oddsize(%struct.large_oddsize* align 8 byval) nounwind
define void @test3(%struct.large_oddsize* nocapture %x) nounwind minsize {
call void @foo_oddsize(%struct.large_oddsize* align 8 byval %x)
ret void
; ALL-LABEL: test3:
; NOFAST: rep;movsb
; GENERIC: rep;movsb
; FAST: rep;movsb
; HASWELL: rep;movsb
}