Open
Description
The following
static int[] Test(int[] a, int n)
{
int y;
if (n == 3)
y = n;
else
y = n + 1;
a[2] = y;
return a;
}
produces the following conditional (with QuickJIT
off)
G_M32566_IG02:
71000C5F cmp w2, dotnet/coreclr#3
54000061 bne G_M32566_IG03
52800061 mov w1, dotnet/coreclr#3
14000002 b G_M32566_IG04
so for both the if and else case it branches, it has no fall through case that doesn't
require the branch.
but also it has generated an extra mov here because it knows n is 3. However change the
condition to
static int[] Test(int[] a, int n)
{
int y;
if (n > 3)
y = n;
else
y = n + 1;
a[2] = y;
return a;
}
and it now just re-uses the register containing n since it's dead after the if anyway
G_M32566_IG02:
71000C5F cmp w2, dotnet/coreclr#3
5400004D ble G_M32566_IG03
14000002 b G_M32566_IG04
G_M32566_IG03:
11000442 add w2, w2, dotnet/coreclr#1
G_M32566_IG04:
52800041 mov w1, dotnet/coreclr#2
B9800803 ldrsw x3, [x0,#8]
6B03003F cmp w1, w3
54000082 bhs G_M32566_IG06
B9001802 str w2, [x0,#24]
So not quite sure why the second case generates smaller code.
But again the conditional can just be re-arranged to
G_M32566_IG02:
71000C5F cmp w2, dotnet/coreclr#3
14000002 bgt G_M32566_IG04
and remove a branch.
category:cq
theme:basic-cq
skill-level:intermediate
cost:medium