[EXT_EP-11795] Compiler incorrectly optimizes implicit cast to pointer expression (int32)(uint16+constant) to within expression ((int32)uint16+constant) Created: 18/Jun/24  Updated: 08/Jul/25  Resolved: 08/Jul/25

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

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

Product: Code Generation Tools
Internal ID: CODEGEN-12562
Forum URL: https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/171/t/1372424
Found In Release: C2000_16.9.0.LTS
C2000_18.1.0.LTS
C2000_22.6.0.LTS
C2000_15.12.0.LTS
C2000_6.4.0
C2000_21.6.0.LTS
C2000_18.12.0.LTS
Fix In Release: C2000_25.3.0.LTS*
C2000_22.6.3.LTS*
C2000_21.6.2.LTS*
Affected Platform/Device: default
Workaround: Use --opt_level=1 or lower

 Description   

For pointer expressions with an unsigned 16bit int variable plus/minus a
constant, eg: (uint16 + k), that also have an implicit cast to 32bit signed
int, eg: (int32)(uint16 + k), the optimizer will incorrectly push the
cast to within the expression: ((int32)uint16 + k)

With option, --opt_level=2, this C code:

MyStruct *struct = MyStructPtrs[uint16 - constant];

Incorrectly optimizes to below:

; StructPtr = MyStructPtrs[(long)MyStructID+22284];
        MOV       ACC,AL << 1           ; [CPU_ALU] |24| 
        ADDL      ACC,@||MyStructPtrs|| ; [CPU_ALU] |24| 
        MOVL      XAR5,ACC              ; [CPU_ALU] |24| 
        MOVL      XAR0,#44568           ; [CPU_ALU] |24| 
        MOVL      ACC,*+XAR5[AR0]       ; [CPU_ALU] |24| 
        MOVL      *-SP[6],ACC           ; [CPU_ALU] |24| 

And here's what should be generated instead:

; StructPtr = MyStructPtrs[(long)(MyStructID+22284u)];
        MOV       AL,#22284             ; [CPU_ALU] |24| 
        ADD       AL,*-SP[13]           ; [CPU_ALU] |24| 
        MOV       ACC,AL << 1           ; [CPU_ALU] |24| 
        ADDL      ACC,@||MyStructPtrs|| ; [CPU_ALU] |24| 
        MOVL      XAR5,ACC              ; [CPU_ALU] |24| 
        MOVL      ACC,*+XAR5[0]         ; [CPU_ALU] |24| 
        MOVL      *-SP[6],ACC           ; [CPU_ALU] |24| 

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