Bug 86847 - [9 Regression] Switch code size growth
Summary: [9 Regression] Switch code size growth
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: 9.0
Assignee: Martin Liška
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-08-03 17:09 UTC by Evgeny Kudryashov
Modified: 2018-08-27 12:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-08-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Evgeny Kudryashov 2018-08-03 17:09:55 UTC
I noticed that the size of code has grown in switch statements between 8.1.0
and trunk.  For the code shown below, gcc-trunk generates 2 jumps for each
switch statement (18 in total) when gcc-8.1.0 only 11. The difference appears
at switchlower1 pass. As I understand, gcc-8 prunes redundant tests when gcc-9
doesn't and instead relies on further tree optimizations
(https://gcc.gnu.org/ml/gcc-patches/2017-09/msg00891.html). But the
optimizations cannot manage with it as well as pruning in gcc-8 can and size
grows. Enabling -O2 does not change the situation significantly.

int cipher_to_alg(int cipher)        
{                                    
        switch (cipher)              
        {                            
                case 8:   return 2;  
                case 16:  return 3;  
                case 32:  return 4;  
                case 64:  return 6;  
                case 256: return 9;  
                case 512: return 10; 
                case 2048: return 11;
                case 4096: return 12;
                case 8192: return 13;
        }                            
        return 0;                    
}                                    

arm-none-eabi-gcc-8.1.0 cipher.c -Os -S

cipher_to_alg:                                   
        cmp     r0, #256                             
        beq     .L6                                  
        bgt     .L3                                  
        cmp     r0, #16                              
        beq     .L7                                  
        bgt     .L4                                  
        cmp     r0, #8                               
        moveq   r0, #2                               
.L15:                                                
        movne   r0, #0                               
        bx      lr                                   
.L4:                                                 
        cmp     r0, #32                              
        beq     .L9                                  
        cmp     r0, #64                              
        moveq   r0, #6                               
        b       .L15                                 
.L3:                                                 
        cmp     r0, #2048                            
        beq     .L11                                 
        bgt     .L5                                  
        cmp     r0, #512                             
        moveq   r0, #10                              
        b       .L15                                 
.L5:                                                 
        cmp     r0, #4096                            
        beq     .L13                                 
        cmp     r0, #8192                            
        moveq   r0, #13                              
        b       .L15                                 
.L6:                                                 
        mov     r0, #9                               
        bx      lr                                   
.L7:                                                 
        mov     r0, #3                               
        bx      lr                                   
.L9:                                                 
        mov     r0, #4                               
        bx      lr                                   
.L11:                                                
        mov     r0, #11                              
        bx      lr                                   
.L13:                                                
        mov     r0, #12                              
        bx      lr                                   

arm-none-eabi-gcc-9.0.0 cipher.c -Os -S

cipher_to_alg:                                  
        cmp     r0, #256                            
        bgt     .L2                                 
        beq     .L8                                 
        cmp     r0, #16                             
        bgt     .L4                                 
        beq     .L9                                 
        cmp     r0, #8                              
        bgt     .L20                                
        bne     .L20                                
        mov     r0, #2                              
        bx      lr                                  
.L4:                                                
        cmp     r0, #32                             
        bgt     .L5                                 
        bne     .L20                                
        mov     r0, #4                              
        bx      lr                                  
.L5:                                                
        cmp     r0, #64                             
        bgt     .L20                                
        bne     .L20                                
        mov     r0, #6                              
        bx      lr                                  
.L2:                                                
        cmp     r0, #2048                           
        bgt     .L6                                 
        beq     .L15                                
        cmp     r0, #512                            
        bgt     .L20                                
        bne     .L20                                
        mov     r0, #10                             
        bx      lr                                  
.L6:                                                
        cmp     r0, #4096                           
        bgt     .L7                                 
        bne     .L20                                
        mov     r0, #12                             
        bx      lr                                  
.L7:                                                
        cmp     r0, #8192                           
        bgt     .L20                                
        bne     .L20                                
        mov     r0, #13                             
        bx      lr                                  
.L8:                                                
        mov     r0, #9                              
        bx      lr                                  
.L9:                                                
        mov     r0, #3                              
        bx      lr                                  
.L15:                                               
        mov     r0, #11                             
        bx      lr                                  
.L20:                                               
        mov     r0, #0                              
        bx      lr
Comment 1 Martin Liška 2018-08-04 07:27:58 UTC
Mine
Comment 2 Martin Liška 2018-08-27 12:21:43 UTC
Author: marxin
Date: Mon Aug 27 12:21:11 2018
New Revision: 263879

URL: https://gcc.gnu.org/viewcvs?rev=263879&root=gcc&view=rev
Log:
Improve switch code emission for a balanced tree (PR tree-optimization/86847).

2018-08-27  Martin Liska  <mliska@suse.cz>

        PR tree-optimization/86847
	* tree-switch-conversion.c (switch_decision_tree::dump_case_nodes):
        Dump also subtree probability.
	(switch_decision_tree::do_jump_if_equal): New function.
	(switch_decision_tree::emit_case_nodes): Handle special
        situations in balanced tree that can be emitted much simpler.
        Fix calculation of probabilities that happen in tree expansion.
	* tree-switch-conversion.h (struct cluster): Add
        is_single_value_p.
	(struct simple_cluster): Likewise.
	(struct case_tree_node): Add new function has_child.
	(do_jump_if_equal): New.
2018-08-27  Martin Liska  <mliska@suse.cz>

        PR tree-optimization/86847
	* gcc.dg/tree-ssa/switch-3.c: New test.
	* gcc.dg/tree-ssa/vrp105.c: Remove.

Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/switch-3.c
Removed:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/vrp105.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-switch-conversion.c
    trunk/gcc/tree-switch-conversion.h
Comment 3 Martin Liška 2018-08-27 12:31:54 UTC
Fixed.