From b22b27b984545259d139e6ff8b8e85e9f067251c Mon Sep 17 00:00:00 2001 From: sivarv Date: Mon, 5 Dec 2016 11:59:57 -0800 Subject: [PATCH] Compare opt against zero involving a shift oper. --- src/jit/lowerxarch.cpp | 16 +++++--- .../Regression/JitBlue/GitHub_8460/GitHub_8460.cs | 38 ++++++++++++++++++ .../JitBlue/GitHub_8460/GitHub_8460.csproj | 46 ++++++++++++++++++++++ 3 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.cs create mode 100644 tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.csproj diff --git a/src/jit/lowerxarch.cpp b/src/jit/lowerxarch.cpp index d2bf092..8665e2e 100644 --- a/src/jit/lowerxarch.cpp +++ b/src/jit/lowerxarch.cpp @@ -1174,13 +1174,17 @@ void Lowering::TreeNodeInfoInitShiftRotate(GenTree* tree) else { MakeSrcContained(tree, shiftBy); - } - // Codegen of shift oper sets ZF and SF flags. - // Note that Rotate Left/Right instructions don't set ZF and SF flags. - if (tree->OperIsShift()) - { - tree->gtFlags |= GTF_ZSF_SET; + // Note that Rotate Left/Right instructions don't set ZF and SF flags. + // + // If the operand being shifted is 32-bits then upper three bits are masked + // by hardware to get actual shift count. Similarly for 64-bit operands + // shift count is narrowed to [0..63]. If the resulting shift count is zero, + // then shift operation won't modify flags. + // + // TODO-CQ-XARCH: We can optimize generating 'test' instruction for GT_EQ/NE(shift, 0) + // if the shift count is known to be non-zero and in the range depending on the + // operand size. } } diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.cs b/tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.cs new file mode 100644 index 0000000..f5c9aa2 --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +namespace bug +{ + class Program + { + static int Pass = 100; + static int Fail = -1; + + // This test is meant to check that in case of + // GT_EQ/NE(shift, 0), JIT doesn't optimize out + // 'test' instruction incorrectly, because shift + // operations on xarch don't modify flags if the + // shift count is zero. + static int Main(string[] args) + { + // Absolute bits + int bitCount = 0; + while ((0 != (100 >> bitCount)) && (31 > bitCount)) + { + bitCount++; + } + // Sign bit + bitCount++; + + if (bitCount != 8) + { + return Fail; + } + + return Pass; + } + } +} diff --git a/tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.csproj b/tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.csproj new file mode 100644 index 0000000..b174dea --- /dev/null +++ b/tests/src/JIT/Regression/JitBlue/GitHub_8460/GitHub_8460.csproj @@ -0,0 +1,46 @@ + + + + + Debug + AnyCPU + $(MSBuildProjectName) + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + + 7a9bfb7d + + + + + + + + + False + + + + + True + + + + + + + + + $(JitPackagesConfigFileDirectory)minimal\project.json + $(JitPackagesConfigFileDirectory)minimal\project.lock.json + + + + + -- 2.7.4