tiarmclang optimizes call to strcmp into call to memcpy with incorrect parameters

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Medium

      Use the attached files. file.c includes these lines ...

          get_sn(sn);
          // sn[0] = '0', sn[1] = '\0'
          if ((strlen(sn) > 0U) && (strcmp(sn, "0") != 0))
      

      Build it.

      tiarmclang @options.txt file.c
      

      Inspect the assembly code in file.s. Search for the call to get_sn.

      	bl	get_sn
      .Ltmp1:
      	.loc	1 11 10                         @ file.c:11:10
      	ldrb.w	r0, [sp]
      	.loc	1 11 27 is_stmt 0               @ file.c:11:27
      	cbz	r0, .LBB0_9
      

      The call to get_sn returns the string "0". The next two instructions check that the length of sn is greater than 0 bytes.

      The subsequent call to strcmp is not found.  Instead, the following instructions are the start of an inline call to memcmp between sn and nvm_sn:

      @ %bb.1:                                @ %while.cond.i.preheader                
              movw    r0, :lower16:nvm_sn                                              
              movs    r1, #0                                                           
              movt    r0, :upper16:nvm_sn                                              
      .LBB0_2:                                @ %while.cond.i                          
                                              @ =>This Inner Loop Header: Depth=1      
              ldrb    r2, [r5, r1]                                                     
              ldrb    r3, [r0, r1]                                                     
              cmp     r2, r3                                                           
              bne     .LBB0_4                                                          
      @ %bb.3:                                @ %while.cond.i                          
                                              @   in Loop: Header=BB0_2 Depth=1        
              adds    r4, r1, #1                                                       
              cmp     r1, #11                                                          
              mov     r1, r4                                                           
              bne     .LBB0_2                                                          
      .LBB0_4:                                @ %memcmp.exit     
      

      While it is legitimate for the compiler to optimize a call to strcmp into a call to memcpy, in this case, the compiler should have optimized the call to strcmp into an inlined call to memcmp between sn and "0".

            Assignee:
            TI User
            Reporter:
            TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                Connection: Intermediate to External PROD System
                EXTSYNC-7145 - tiarmclang optimizes call to strcmp...
                SYNCHRONIZED
                • Last Sync Date: