D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21833 - Optimizer incorrectly rewrites integer comparison to unsigned short comparison
Summary: Optimizer incorrectly rewrites integer comparison to unsigned short comparison
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: backend, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2021-04-16 10:06 UTC by Walter Bright
Modified: 2021-04-19 02:36 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Walter Bright 2021-04-16 10:06:25 UTC
The code:

void testit()
{
    pragma(inline, false);
    short[4] arr = [-1, 6, 0, 4];
    long1 A = *cast(long1*)(arr.ptr);
    assert(_mm_extract_pi16(A, 0) == 65535); // Error here
}

struct short4
{
    short[4] array;
}

struct long1
{
    long[1] array;
}

int _mm_extract_pi16 (long1 a, int imm8)
{
    return cast(ushort)((cast(short4)a).array[imm8]);
}

The error is the optimizer incorrectly rewrites 65535 to be an unsigned short and then does a signed short comparison, which fails to match the left hand side which is a short. Should rewrite it as a short.
void main()
{
    testit();
}
Comment 1 Dlang Bot 2021-04-16 10:12:05 UTC
@WalterBright created dlang/dmd pull request #12444 "fix Issue 21833 - Optimizer incorrectly rewrites integer comparison t…" fixing this issue:

- fix Issue 21833 - Optimizer incorrectly rewrites integer comparison to unsigned short comparison

https://github.com/dlang/dmd/pull/12444
Comment 2 Dlang Bot 2021-04-19 02:36:06 UTC
dlang/dmd pull request #12444 "fix Issue 21833 - Optimizer incorrectly rewrites integer comparison t…" was merged into master:

- 4adcfb79a21b01021589f99900e973fac1100820 by Walter Bright:
  fix Issue 21833 - Optimizer incorrectly rewrites integer comparison to unsigned short comparison

https://github.com/dlang/dmd/pull/12444