Skip to content

Commit 4a99422

Browse files
committed
PR feedback
1 parent e35aa3c commit 4a99422

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

src/coreclr/jit/codegenxarch.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ void CodeGen::genCodeForBswap(GenTree* tree)
575575
{
576576
// 16-bit byte swaps use "ror reg.16, 8"
577577
inst_RV_IV(INS_ror_N, targetReg, 8 /* val */, emitAttr::EA_2BYTE);
578+
579+
if (!genCanOmitNormalizationForBswap16(tree))
580+
{
581+
GetEmitter()->emitIns_Mov(INS_movzx, EA_2BYTE, targetReg, targetReg, /* canSkip */ false);
582+
}
578583
}
579584
}
580585
else

src/coreclr/jit/lowerxarch.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,27 +3965,9 @@ void Lowering::LowerBswapOp(GenTreeOp* node)
39653965
}
39663966

39673967
GenTree* operand = node->gtGetOp1();
3968-
if (IsContainableMemoryOp(operand) && IsSafeToContainMem(node, operand))
3968+
if (node->TypeGet() == operand->TypeGet() && IsContainableMemoryOp(operand) && IsSafeToContainMem(node, operand))
39693969
{
39703970
MakeSrcContained(node, operand);
3971-
3972-
// Return early so we don't do double-containing
3973-
return;
3974-
}
3975-
3976-
// Due to this instruction being rare we can perform TryGetUse here
3977-
// to simplify the pattern recognition required for GT_STOREIND
3978-
// In cases where TP matters, it should be performed inside the user
3979-
LIR::Use use;
3980-
if (!BlockRange().TryGetUse(node, &use))
3981-
{
3982-
return;
3983-
}
3984-
3985-
GenTree* user = use.User();
3986-
if (user->OperIs(GT_STOREIND))
3987-
{
3988-
MakeSrcContained(user, node);
39893971
}
39903972
}
39913973

@@ -4619,6 +4601,12 @@ void Lowering::ContainCheckStoreIndir(GenTreeStoreInd* node)
46194601
MakeSrcContained(node, src);
46204602
}
46214603

4604+
if (comp->opts.OptimizationEnabled() && src->OperIs(GT_BSWAP, GT_BSWAP16) &&
4605+
comp->compOpportunisticallyDependsOn(InstructionSet_MOVBE) && IsSafeToContainMem(node, src))
4606+
{
4607+
MakeSrcContained(node, src);
4608+
}
4609+
46224610
ContainCheckIndir(node);
46234611
}
46244612

src/tests/JIT/Intrinsics/BinaryPrimitivesReverseEndianness.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,37 @@ static int Main(string[] args)
181181
return Fail;
182182
}
183183

184+
/*
185+
* WRITE TESTS CAST
186+
*/
187+
188+
byte writeCastUInt16Input = (byte)DateTime.UtcNow.Ticks;
189+
ushort writeCastUInt16Output = ByteSwapUInt16_WriteCast(ref writeCastUInt16Input);
190+
ushort writeCastUInt16Expected = ByteSwapUInt16_Control(writeCastUInt16Input);
191+
if (writeCastUInt16Output != writeCastUInt16Expected)
192+
{
193+
ReportError("write cast UInt16", writeCastUInt16Input, writeCastUInt16Output, writeCastUInt16Expected);
194+
return Fail;
195+
}
196+
197+
ushort writeCastUInt32Input = (ushort)DateTime.UtcNow.Ticks;
198+
uint writeCastUInt32Output = ByteSwapUInt32_WriteCast(ref writeCastUInt32Input);
199+
uint writeCastUInt32Expected = ByteSwapUInt32_Control(writeCastUInt32Input);
200+
if (writeCastUInt32Output != writeCastUInt32Expected)
201+
{
202+
ReportError("write cast UInt32", writeCastUInt32Input, writeCastUInt32Output, writeCastUInt32Expected);
203+
return Fail;
204+
}
205+
206+
uint writeCastUInt64Input = (uint)DateTime.UtcNow.Ticks;
207+
ulong writeCastUInt64Output = ByteSwapUInt64_WriteCast(ref writeCastUInt64Input);
208+
ulong writeCastUInt64Expected = ByteSwapUInt64_Control(writeCastUInt64Input);
209+
if (writeCastUInt64Output != writeCastUInt64Expected)
210+
{
211+
ReportError("write cast UInt64", writeCastUInt64Input, writeCastUInt64Output, writeCastUInt64Expected);
212+
return Fail;
213+
}
214+
184215
return Pass;
185216
}
186217

@@ -250,7 +281,6 @@ private static ulong ByteSwapUInt64_Write(ref ulong output)
250281
return input;
251282
}
252283

253-
254284
[MethodImpl(MethodImplOptions.NoInlining)]
255285
private static ushort ByteSwapUInt16_WriteLea(ref ushort output, int offset)
256286
{
@@ -281,6 +311,24 @@ private static ulong ByteSwapUInt64_WriteLea(ref ulong output, int offset)
281311
return input;
282312
}
283313

314+
[MethodImpl(MethodImplOptions.NoInlining)]
315+
private static ushort ByteSwapUInt16_WriteCast(ref byte input)
316+
{
317+
return BinaryPrimitives.ReverseEndianness((ushort)input);
318+
}
319+
320+
[MethodImpl(MethodImplOptions.NoInlining)]
321+
private static uint ByteSwapUInt32_WriteCast(ref ushort input)
322+
{
323+
return BinaryPrimitives.ReverseEndianness((uint)input);
324+
}
325+
326+
[MethodImpl(MethodImplOptions.NoInlining)]
327+
private static ulong ByteSwapUInt64_WriteCast(ref uint input)
328+
{
329+
return BinaryPrimitives.ReverseEndianness((ulong)input);
330+
}
331+
284332
private static string GetHexString<T>(T value)
285333
{
286334
if (typeof(T) == typeof(short))

0 commit comments

Comments
 (0)