[EXT_EP-11805] Compiler may generate incorrect code for ternary max idiom with integers Created: 03/Jul/24  Updated: 21/Jul/25  Resolved: 18/Jul/24

Status: Fixed
Project: Embedded Software & Tools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: High
Reporter: TI User Assignee: TI User
Resolution: Fixed Votes: 1
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Product: Code Generation Tools
Internal ID: CODEGEN-12663
Forum URL: https://e2e.ti.com/support/processors-group/processors---internal/f/1045/t/1382533
Found In Release: C2000_25.3.0.LTS*
MSP430_NEXT*
MSP430_20.2.0.LTS
C7000_2.1.0.LTS
MSP430_21.6.0.LTS
C7000_5.0.0.LTS
C2000_21.6.0.LTS
C7000_3.1.0.LTS
ARM_20.2.0.LTS
C7000_4.1.0.LTS
C2000_20.2.0.LTS
C7000_1.4.0.LTS
C2000_22.6.0.LTS
C6000_8.5.0.LTS
PRU_2.4.0*
Fix In Release: C7000_3.1.2.LTS
C2000_25.3.0.LTS*
MSP430_20.2.8.LTS*
MSP430_NEXT*
MSP430_21.6.2.LTS*
C6000_8.5.0.LTS
ARM_20.2.8.LTS*
C7000_5.0.0.LTS
PRU_2.4.0*
C2000_22.6.2.LTS
C2000_21.6.2.LTS*
C7000_4.1.1.LTS
Affected Platform/Device: default
Workaround: This bug may trigger when all of the following conditions are met:
1. Generating code for a ternary max operation and
2. When integer immediate values are used in the max operation and
3. -o2 or higher is used

To avoid this bug, the following workarounds are available:
1. Use an intrinsic to implement the max operation if available on your target. For example, the __max() intrinsic for C7000. This avoids using a ternary idiom.
2. Use a function to implement the max operation that is marked __attribute__((noinline)). This avoids immediate values being used directly.
3. Reduce the optimization level.

 Description   

A ternary operator is often used in C to implement a max operation. For example, `x < y ? y : x`.

In rare cases, when:
1. Generating code for a ternary max operation and
2. When integer immediate values are used in the max operation
Then the compiler may generate incorrect code.

For example, `x > y ? x : y` may generate incorrect code.

If the inputs to the max operation are not immediate values, incorrect code will not be generated. However, due to optimization, it may be difficult to identify if inputs are immediates or not.

In these cases, incorrect code may manifest as an off-by-two error. For example, `a < 10` when `a < 12` is expected.

Ternary min idioms are not affected by this bug.


Generated at Sat Dec 13 11:34:37 CST 2025 using Jira 10.3.7#10030007-sha1:a563685562f94d165eb4e158cfb2a142338d8c54.