Skip to content

Commit 9670ddd

Browse files
Only unsigned
1 parent ed5f297 commit 9670ddd

File tree

2 files changed

+57
-109
lines changed

2 files changed

+57
-109
lines changed

src/coreclr/jit/lower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8445,7 +8445,7 @@ void Lowering::LowerShift(GenTreeOp* shift)
84458445
GenTreeIntCon* cns = op2->AsIntCon();
84468446

84478447
if (!cast->isContained() && !cast->IsRegOptional() && !cast->gtOverflow() &&
8448-
cast->CastOp()->TypeIs(TYP_INT))
8448+
cast->CastOp()->TypeIs(TYP_INT) && varTypeIsUnsigned(cast->CastToType()))
84498449
{
84508450
unsigned srcBits = genTypeSize(cast->CastToType()) * BITS_PER_BYTE;
84518451
if (cns->IconValue() >= srcBits)

src/tests/JIT/opt/InstructionCombining/CastRightShift.cs

Lines changed: 56 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -15,157 +15,125 @@ public static int CheckCastRightShift()
1515
{
1616
bool fail = false;
1717

18-
{
19-
var r = CastASR7_byte_int(255);
20-
if (r == 0) { Console.WriteLine($"CastASR7_byte_int(255) = {r} should be non-zero"); fail = true; }
21-
}
22-
23-
{
24-
var r = CastASR8_byte_int(255);
25-
if (r != 0) { Console.WriteLine($"CastASR8_byte_int(255) = {r} should be 0"); fail = true; }
26-
}
2718

19+
if (CastASR7_byte_int(255) == 0)
2820
{
29-
var r = CastLSR7_byte_uint(255);
30-
if (r != 1U) { Console.WriteLine($"CastLSR7_byte_uint(255) = {r} should be 1"); fail = true; }
21+
fail = true;
3122
}
3223

24+
if (CastASR8_byte_int(255) != 0)
3325
{
34-
var r = CastLSR8_byte_uint(255);
35-
if (r != 0U) { Console.WriteLine($"CastLSR8_byte_uint(255) = {r} should be 0"); fail = true; }
26+
fail = true;
3627
}
3728

29+
if (CastLSR7_byte_uint(255) != 1)
3830
{
39-
var r = CastASR7_byte_long(255);
40-
if (r == 0L) { Console.WriteLine($"CastASR7_byte_long(255) = {r} should be non-zero"); fail = true; }
31+
fail = true;
4132
}
4233

34+
if (CastLSR8_byte_uint(255) != 0)
4335
{
44-
var r = CastASR8_byte_long(255);
45-
if (r != 0L) { Console.WriteLine($"CastASR8_byte_long(255) = {r} should be 0"); fail = true; }
36+
fail = true;
4637
}
4738

39+
if (CastASR7_byte_long(255) == 0)
4840
{
49-
var r = CastLSR7_byte_ulong(255);
50-
if (r == 0UL) { Console.WriteLine($"CastLSR7_byte_ulong(255) = {r} should be non-zero"); fail = true; }
41+
fail = true;
5142
}
5243

44+
if (CastASR8_byte_long(255) != 0)
5345
{
54-
var r = CastLSR8_byte_ulong(255);
55-
if (r != 0UL) { Console.WriteLine($"CastLSR8_byte_ulong(255) = {r} should be 0"); fail = true; }
46+
fail = true;
5647
}
5748

49+
if (CastLSR7_byte_ulong(255) == 0)
5850
{
59-
var r = CastASR7_sbyte_int(-127);
60-
if (r == 0) { Console.WriteLine($"CastASR7_sbyte_int(-127) = {r} should be non-zero"); fail = true; }
51+
fail = true;
6152
}
6253

63-
// Console.WriteLine($"{-127 >> 6}");
64-
// Console.WriteLine($"{-127 >> 7}");
65-
54+
if (CastLSR8_byte_ulong(255) != 0)
6655
{
67-
var x = foo6(-127);
68-
Console.WriteLine($"foo6(-127) = {x}");
56+
fail = true;
6957
}
7058

59+
if (CastASR7_sbyte_int(-127) != -1)
7160
{
72-
var x = foo7(-127);
73-
Console.WriteLine($"foo7(-127) = {x}");
61+
fail = true;
7462
}
7563

64+
if (CastASR8_sbyte_int(-127) != -1)
7665
{
77-
var x = foo8(-127);
78-
Console.WriteLine($"foo8(-127) = {x}");
66+
fail = true;
7967
}
8068

69+
if (CastASR7_sbyte_long(-127) != -1)
8170
{
82-
var x = CastASR6_sbyte_int(-127);
83-
Console.WriteLine($"CastASR6_sbyte_int(-127) = {x}");
71+
fail = true;
8472
}
8573

74+
if (CastASR8_sbyte_long(-127) != -1)
8675
{
87-
var x = CastASR7_sbyte_int(-127);
88-
Console.WriteLine($"CastASR7_sbyte_int(-127) = {x}");
76+
fail = true;
8977
}
9078

79+
if (CastASR15_ushort_int(65535) == 0)
9180
{
92-
var x = CastASR8_sbyte_int(-127);
93-
Console.WriteLine($"CastASR8_sbyte_int(-127) = {x}");
81+
fail = true;
9482
}
9583

84+
if (CastASR16_ushort_int(65535) != 0)
9685
{
97-
var r = CastASR8_sbyte_int(-127);
98-
if (r != 0) { Console.WriteLine($"CastASR8_sbyte_int(-127) = {r} should be 0"); fail = true; }
86+
fail = true;
9987
}
10088

89+
if (CastLSR15_ushort_uint(65535) == 0)
10190
{
102-
var r = CastASR7_sbyte_long(-127);
103-
if (r == 0L) { Console.WriteLine($"CastASR7_sbyte_long(-127) = {r} should be non-zero"); fail = true; }
91+
fail = true;
10492
}
10593

94+
if (CastLSR16_ushort_uint(65535) != 0)
10695
{
107-
var r = CastASR8_sbyte_long(-127);
108-
if (r != 0L) { Console.WriteLine($"CastASR8_sbyte_long(-127) = {r} should be 0"); fail = true; }
96+
fail = true;
10997
}
11098

99+
if (CastASR15_ushort_long(65535) == 0)
111100
{
112-
var r = CastASR15_ushort_int(65535);
113-
if (r == 0) { Console.WriteLine($"CastASR15_ushort_int(65535) = {r} should be non-zero"); fail = true; }
101+
fail = true;
114102
}
115103

104+
if (CastASR16_ushort_long(65535) != 0)
116105
{
117-
var r = CastASR16_ushort_int(65535);
118-
if (r != 0) { Console.WriteLine($"CastASR16_ushort_int(65535) = {r} should be 0"); fail = true; }
106+
fail = true;
119107
}
120108

109+
if (CastLSR15_ushort_ulong(65535) == 0)
121110
{
122-
var r = CastLSR15_ushort_uint(65535);
123-
if (r == 0U) { Console.WriteLine($"CastLSR15_ushort_uint(65535) = {r} should be non-zero"); fail = true; }
111+
fail = true;
124112
}
125113

114+
if (CastLSR16_ushort_ulong(65535) != 0)
126115
{
127-
var r = CastLSR16_ushort_uint(65535);
128-
if (r != 0U) { Console.WriteLine($"CastLSR16_ushort_uint(65535) = {r} should be 0"); fail = true; }
116+
fail = true;
129117
}
130118

119+
if (CastASR15_short_int(-1) != -1)
131120
{
132-
var r = CastASR15_ushort_long(65535);
133-
if (r == 0L) { Console.WriteLine($"CastASR15_ushort_long(65535) = {r} should be non-zero"); fail = true; }
121+
fail = true;
134122
}
135123

124+
if (CastASR16_short_int(-1) != -1)
136125
{
137-
var r = CastASR16_ushort_long(65535);
138-
if (r != 0L) { Console.WriteLine($"CastASR16_ushort_long(65535) = {r} should be 0"); fail = true; }
126+
fail = true;
139127
}
140128

129+
if (CastASR15_short_long(-1) != -1)
141130
{
142-
var r = CastLSR15_ushort_ulong(65535);
143-
if (r == 0UL) { Console.WriteLine($"CastLSR15_ushort_ulong(65535) = {r} should be non-zero"); fail = true; }
131+
fail = true;
144132
}
145133

134+
if (CastASR16_short_long(-1) != -1)
146135
{
147-
var r = CastLSR16_ushort_ulong(65535);
148-
if (r != 0UL) { Console.WriteLine($"CastLSR16_ushort_ulong(65535) = {r} should be 0"); fail = true; }
149-
}
150-
151-
{
152-
var r = CastASR15_short_int(-1);
153-
if (r == 0) { Console.WriteLine($"CastASR15_short_int(-1) = {r} should be non-zero"); fail = true; }
154-
}
155-
156-
{
157-
var r = CastASR16_short_int(-1);
158-
if (r != 0) { Console.WriteLine($"CastASR16_short_int(-1) = {r} should be 0"); fail = true; }
159-
}
160-
161-
{
162-
var r = CastASR15_short_long(-1);
163-
if (r == 0L) { Console.WriteLine($"CastASR15_short_long(-1) = {r} should be non-zero"); fail = true; }
164-
}
165-
166-
{
167-
var r = CastASR16_short_long(-1);
168-
if (r != 0L) { Console.WriteLine($"CastASR16_short_long(-1) = {r} should be 0"); fail = true; }
136+
fail = true;
169137
}
170138

171139
if (fail)
@@ -248,12 +216,6 @@ static ulong CastLSR8_byte_ulong(int x)
248216
return (ulong)(byte)x >> 8;
249217
}
250218

251-
[MethodImpl(MethodImplOptions.NoInlining)]
252-
static int CastASR6_sbyte_int(int x)
253-
{
254-
return (sbyte)x >> 6;
255-
}
256-
257219
[MethodImpl(MethodImplOptions.NoInlining)]
258220
static int CastASR7_sbyte_int(int x)
259221
{
@@ -262,28 +224,11 @@ static int CastASR7_sbyte_int(int x)
262224
return (sbyte)x >> 7;
263225
}
264226

265-
[MethodImpl(MethodImplOptions.NoInlining)]
266-
static int foo6(int x)
267-
{
268-
return x >> 6;
269-
}
270-
271-
[MethodImpl(MethodImplOptions.NoInlining)]
272-
static int foo7(int x)
273-
{
274-
return x >> 7;
275-
}
276-
277-
[MethodImpl(MethodImplOptions.NoInlining)]
278-
static int foo8(int x)
279-
{
280-
return x >> 8;
281-
}
282-
283227
[MethodImpl(MethodImplOptions.NoInlining)]
284228
static int CastASR8_sbyte_int(int x)
285229
{
286-
//ARM64-FULL-LINE: mov {{w[0-9]+}}, wzr
230+
//ARM64-FULL-LINE: sxtb {{w[0-9]+}}, {{w[0-9]+}}
231+
//ARM64-FULL-LINE: asr {{w[0-9]+}}, {{w[0-9]+}}, #8
287232
return (sbyte)x >> 8;
288233
}
289234

@@ -299,7 +244,8 @@ static long CastASR7_sbyte_long(int x)
299244
[MethodImpl(MethodImplOptions.NoInlining)]
300245
static long CastASR8_sbyte_long(int x)
301246
{
302-
//ARM64-FULL-LINE: mov {{w[0-9]+}}, wzr
247+
//ARM64-FULL-LINE: sxtb {{w[0-9]+}}, {{w[0-9]+}}
248+
//ARM64-FULL-LINE: asr {{w[0-9]+}}, {{w[0-9]+}}, #8
303249
//ARM64-FULL-LINE: sxtw {{x[0-9]+}}, {{w[0-9]+}}
304250
return (sbyte)x >> 8;
305251
}
@@ -380,7 +326,8 @@ static int CastASR15_short_int(int x)
380326
[MethodImpl(MethodImplOptions.NoInlining)]
381327
static int CastASR16_short_int(int x)
382328
{
383-
//ARM64-FULL-LINE: mov {{w[0-9]+}}, wzr
329+
//ARM64-FULL-LINE: sxth {{w[0-9]+}}, {{w[0-9]+}}
330+
//ARM64-FULL-LINE: asr {{w[0-9]+}}, {{w[0-9]+}}, #16
384331
return (short)x >> 16;
385332
}
386333

@@ -396,7 +343,8 @@ static long CastASR15_short_long(int x)
396343
[MethodImpl(MethodImplOptions.NoInlining)]
397344
static long CastASR16_short_long(int x)
398345
{
399-
//ARM64-FULL-LINE: mov {{w[0-9]+}}, wzr
346+
//ARM64-FULL-LINE: sxth {{w[0-9]+}}, {{w[0-9]+}}
347+
//ARM64-FULL-LINE: asr {{w[0-9]+}}, {{w[0-9]+}}, #16
400348
//ARM64-FULL-LINE: sxtw {{x[0-9]+}}, {{w[0-9]+}}
401349
return (short)x >> 16;
402350
}

0 commit comments

Comments
 (0)