Bug 444110 - priv/guest_ppc_toIR.c:36198:31: warning: duplicated 'if' condition
Summary: priv/guest_ppc_toIR.c:36198:31: warning: duplicated 'if' condition
Status: RESOLVED FIXED
Alias: None
Product: valgrind
Classification: Developer tools
Component: vex (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Julian Seward
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-10-20 13:40 UTC by dcb314
Modified: 2022-10-31 18:29 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dcb314 2021-10-20 13:40:00 UTC
SUMMARY

gcc with compiler flag -Wduplicated-cond says:

priv/guest_ppc_toIR.c: In function 'disInstr_PPC_WRK':
priv/guest_ppc_toIR.c:36198:31: warning: duplicated 'if' condition [-Wduplicated-cond]
36198 |          } else if (is_prefix && ( ptype == pType1 ) ) {  // plbz:  load instruction
      |                     ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
priv/guest_ppc_toIR.c:36193:24: note: previously used here
36193 |          if (is_prefix && ( ptype == pType1 ) ) {
      |              ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~


EXPECTED RESULT


SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version: 

ADDITIONAL INFORMATION
Comment 1 dcb314 2022-10-24 15:20:40 UTC
Still there, over a year later.
Comment 2 Paul Floyd 2022-10-26 09:08:22 UTC
This is probably harmless, but I don't know whether the else should be removed or the condition changed. I've added Carl to the cc list.
Comment 3 Carl Love 2022-10-31 15:57:59 UTC
Thanks for bringing this to my attention.  Yes, something looks wrong.  I think there is an issue with the condition, will need to look at it a bit more.  Don't think removing the if statement is correct.  I will work on it shortly, just getting back from vacation at the moment.
Comment 4 Carl Love 2022-10-31 18:00:09 UTC
The current code:
        if (is_prefix && ( ptype == pType1 ) ) {
            if ( !(allow_isa_3_1) ) goto decode_noIsa3_1;
            // splat instructions: xxpermx                                      
            if (dis_vector_permute_prefix( prefix, theInstr, abiinfo ))
	       goto decode_success;
         } else if (is_prefix && ( ptype == pType1 ) ) {  // plbz:  load instruction                                                                           
            if ( !(allow_isa_3_1) ) goto decode_noIsa3_1;
            if (dis_int_load_prefix( prefix, theInstr ))
               goto decode_success;
         } else {  // lbz:  load instruction                    
            if (dis_int_load_prefix( prefix, theInstr ))
               goto decode_success;
         }

The above code is wrong.  The else if should be checking for the plbz instruction.  The plbz instruction is pType2 not 1.    The else if case fails for the plbz instruction and we hit the else statement.  The else statement is supposed to only succeed for the lpz instruction.  However, it is actually
handling both the plbz and lbz instructions.  The same function dis_int_load_prefix() handles both the prefix and non-prefixed versions of the instruction.  They are distinquished by the prefix argument.  The else statement should only succeed for the lbz instruction, i.e. not a prefixed instruction.  So we really have two errors, the else if should check for pType2 and the else should really be else if (!prefix).

The code should be:
        if (is_prefix && ( ptype == pType1 ) ) {
            if ( !(allow_isa_3_1) ) goto decode_noIsa3_1;
            // splat instructions: xxpermx                                      
            if (dis_vector_permute_prefix( prefix, theInstr, abiinfo ))
	       goto decode_success;
         } else if (is_prefix && ( ptype == pType2 ) ) {  // plbz:  load instruction                                                                           
            if ( !(allow_isa_3_1) ) goto decode_noIsa3_1;
            if (dis_int_load_prefix( prefix, theInstr ))
               goto decode_success;
         } else if (!is_prefix) {  // lbz:  load instruction                    
            if (dis_int_load_prefix( prefix, theInstr ))
               goto decode_success;
         }

Tested the original code and verified the else was handling both the prefixed and non-prefixed lbz instructions.  Tested the correct code to verify that the prefixed instruction matches is_prefix and ptype == pType2.   Ran the regression tests to verify the correct code does not introduce any failures.

In this case, I just got lucky and the code did the right thing in spite of the coding error.  Better to be lucky than good!  :-)
Comment 5 Carl Love 2022-10-31 18:29:26 UTC
Patch committed.

commit 873f3766955c4f5caacc014dbe3d4fa0a4f6f712 (HEAD -> master, origin/master, origin/HEAD)
Author: Carl Love <cel@us.ibm.com>
Date:   Mon Oct 31 13:29:31 2022 -0400

    Bug 444110 priv/guest_ppc_toIR.c: warning: duplicated 'if' condition
    
    The compiler reported a duplicated condition in VEX/priv/guest_ppc_toIR.c
    The handling of the plbz and xxpermx instructions have the same if/elseif
    conditions.  The else if condition for the plbz instruction was wrong.  The
    elseif statement should be checking for pType2 not pType1.  The plbz
    instruction was inadvertently being handled by the else statement for
    the lbz instruction.
    
    This patch fixes the checking for the plbz and lbz instructions.