This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C6000-CGT: C6000-CGT 8.3.13: Miscompilation of swtich-case with enum class

Part Number: C6000-CGT

Tool/software:

Compiler: C6000-CGT 8.3.13

This code gets miscompiled if no optimization settings are passed:

e.g. "cl6x.exe enumClass.cpp"

enum class A : unsigned short
{
    A1 = 0xFFFFu
};

int test(A a)
{
    switch (a)
    {
        case A::A1:
            return 42;
        default:
            return 0;
    }
}

Disassembly:

Disassembly of enumClass.obj:

TEXT Section .text (Little Endian), 0x40 bytes at 0x00000000
00000000            _Z4test1A:
00000000   07bf005b           SUB.L2        B15,0x8,B15
00000004   000c1fd8 ||        MV.L1X        B3,A0
00000008   023c42d4           STH.D2T1      A4,*+B15[2]
0000000c   000ca120           BNOP.S1       $C$L3 (PC+24 = 0x00000018),5
00000010            $C$L1:
00000010       858a           BNOP.S1       $C$L4 (PC+44 = 0x0000002c),4
00000012       4a32           MVK.S1        42,A4
00000014            $C$L2:
00000014       858a           BNOP.S1       $C$L4 (PC+44 = 0x0000002c),4
00000016       0626           MVK.L1        0,A4
00000018            $C$L3:
00000018   023c4286           LDHU.D2T2     *+B15[2],B4
0000001c   e6108000           .fphead       p, l, W, BU, br, nosat, 0110000b
00000020   0013ea5a           CMPEQ.L2      -1,B4,B0
00000024   2ffca120    [ B0]  BNOP.S1       $C$L1 (PC-16 = 0x00000010),5
00000028   0ffda120           BNOP.S1       $C$L2 (PC-12 = 0x00000014),5
0000002c            $C$L4:
0000002c   00809362           BNOP.S2X      A0,4
00000030   07bd005a           ADD.L2        8,B15,B15
00000034   00000000           NOP
00000038   00000000           NOP
0000003c   00000000           NOP

The broken part:

00000018   023c4286           LDHU.D2T2     *+B15[2],B4
0000001c   e6108000           .fphead       p, l, W, BU, br, nosat, 0110000b
00000020   0013ea5a           CMPEQ.L2      -1,B4,B0

The combination of LDHU (Load Halfword From Memory - without sign extension) in combination with a comparison against -1 is obviously incorrect.

The bug is triggered if the highest bit in the enum value is set. In other words, if the signed value of the same size would be negative.

The code is compiled correctly if a higher "-O" setting than "-Ooff" is used.

The miscompliation also goes away if the enum class is changed to an (unscoped) enum.

The enum definition like that avoids the miscompliation:

enum A : unsigned short
{
    A1 = 0xFFFFu
};