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"

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum class A : unsigned short
{
A1 = 0xFFFFu
};
int test(A a)
{
switch (a)
{
case A::A1:
return 42;
default:
return 0;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Disassembly:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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:

Fullscreen
1
2
3
4
enum A : unsigned short
{
A1 = 0xFFFFu
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • Thank you for notifying us of this problem, and for submitting a concise test case.  I am able to reproduce the same behavior.  I filed the entry EXT_EP-12185 to have this investigated.  You are welcome to follow it with that link.

    Thanks and regards,

    -George