github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/compile/internal/ssa/cse_test.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  import "testing"
     8  
     9  type tstAux struct {
    10  	s string
    11  }
    12  
    13  // This tests for a bug found when partitioning, but not sorting by the Aux value.
    14  func TestCSEAuxPartitionBug(t *testing.T) {
    15  	c := testConfig(t)
    16  	arg1Aux := &tstAux{"arg1-aux"}
    17  	arg2Aux := &tstAux{"arg2-aux"}
    18  	arg3Aux := &tstAux{"arg3-aux"}
    19  
    20  	// construct lots of values with args that have aux values and place
    21  	// them in an order that triggers the bug
    22  	fun := c.Fun("entry",
    23  		Bloc("entry",
    24  			Valu("start", OpInitMem, TypeMem, 0, nil),
    25  			Valu("sp", OpSP, TypeBytePtr, 0, nil),
    26  			Valu("r7", OpAdd64, TypeInt64, 0, nil, "arg3", "arg1"),
    27  			Valu("r1", OpAdd64, TypeInt64, 0, nil, "arg1", "arg2"),
    28  			Valu("arg1", OpArg, TypeInt64, 0, arg1Aux),
    29  			Valu("arg2", OpArg, TypeInt64, 0, arg2Aux),
    30  			Valu("arg3", OpArg, TypeInt64, 0, arg3Aux),
    31  			Valu("r9", OpAdd64, TypeInt64, 0, nil, "r7", "r8"),
    32  			Valu("r4", OpAdd64, TypeInt64, 0, nil, "r1", "r2"),
    33  			Valu("r8", OpAdd64, TypeInt64, 0, nil, "arg3", "arg2"),
    34  			Valu("r2", OpAdd64, TypeInt64, 0, nil, "arg1", "arg2"),
    35  			Valu("raddr", OpAddr, TypeInt64Ptr, 0, nil, "sp"),
    36  			Valu("raddrdef", OpVarDef, TypeMem, 0, nil, "start"),
    37  			Valu("r6", OpAdd64, TypeInt64, 0, nil, "r4", "r5"),
    38  			Valu("r3", OpAdd64, TypeInt64, 0, nil, "arg1", "arg2"),
    39  			Valu("r5", OpAdd64, TypeInt64, 0, nil, "r2", "r3"),
    40  			Valu("r10", OpAdd64, TypeInt64, 0, nil, "r6", "r9"),
    41  			Valu("rstore", OpStore, TypeMem, 0, TypeInt64, "raddr", "r10", "raddrdef"),
    42  			Goto("exit")),
    43  		Bloc("exit",
    44  			Exit("rstore")))
    45  
    46  	CheckFunc(fun.f)
    47  	cse(fun.f)
    48  	deadcode(fun.f)
    49  	CheckFunc(fun.f)
    50  
    51  	s1Cnt := 2
    52  	// r1 == r2 == r3, needs to remove two of this set
    53  	s2Cnt := 1
    54  	// r4 == r5, needs to remove one of these
    55  	for k, v := range fun.values {
    56  		if v.Op == OpInvalid {
    57  			switch k {
    58  			case "r1":
    59  				fallthrough
    60  			case "r2":
    61  				fallthrough
    62  			case "r3":
    63  				if s1Cnt == 0 {
    64  					t.Errorf("cse removed all of r1,r2,r3")
    65  				}
    66  				s1Cnt--
    67  
    68  			case "r4":
    69  				fallthrough
    70  			case "r5":
    71  				if s2Cnt == 0 {
    72  					t.Errorf("cse removed all of r4,r5")
    73  				}
    74  				s2Cnt--
    75  			default:
    76  				t.Errorf("cse removed %s, but shouldn't have", k)
    77  			}
    78  		}
    79  	}
    80  
    81  	if s1Cnt != 0 || s2Cnt != 0 {
    82  		t.Errorf("%d values missed during cse", s1Cnt+s2Cnt)
    83  	}
    84  }
    85  
    86  // TestZCSE tests the zero arg cse.
    87  func TestZCSE(t *testing.T) {
    88  	c := testConfig(t)
    89  
    90  	fun := c.Fun("entry",
    91  		Bloc("entry",
    92  			Valu("start", OpInitMem, TypeMem, 0, nil),
    93  			Valu("sp", OpSP, TypeBytePtr, 0, nil),
    94  			Valu("sb1", OpSB, TypeBytePtr, 0, nil),
    95  			Valu("sb2", OpSB, TypeBytePtr, 0, nil),
    96  			Valu("addr1", OpAddr, TypeInt64Ptr, 0, nil, "sb1"),
    97  			Valu("addr2", OpAddr, TypeInt64Ptr, 0, nil, "sb2"),
    98  			Valu("a1ld", OpLoad, TypeInt64, 0, nil, "addr1", "start"),
    99  			Valu("a2ld", OpLoad, TypeInt64, 0, nil, "addr2", "start"),
   100  			Valu("c1", OpConst64, TypeInt64, 1, nil),
   101  			Valu("r1", OpAdd64, TypeInt64, 0, nil, "a1ld", "c1"),
   102  			Valu("c2", OpConst64, TypeInt64, 1, nil),
   103  			Valu("r2", OpAdd64, TypeInt64, 0, nil, "a2ld", "c2"),
   104  			Valu("r3", OpAdd64, TypeInt64, 0, nil, "r1", "r2"),
   105  			Valu("raddr", OpAddr, TypeInt64Ptr, 0, nil, "sp"),
   106  			Valu("raddrdef", OpVarDef, TypeMem, 0, nil, "start"),
   107  			Valu("rstore", OpStore, TypeMem, 0, TypeInt64, "raddr", "r3", "raddrdef"),
   108  			Goto("exit")),
   109  		Bloc("exit",
   110  			Exit("rstore")))
   111  
   112  	CheckFunc(fun.f)
   113  	zcse(fun.f)
   114  	deadcode(fun.f)
   115  	CheckFunc(fun.f)
   116  
   117  	if fun.values["c1"].Op != OpInvalid && fun.values["c2"].Op != OpInvalid {
   118  		t.Errorf("zsce should have removed c1 or c2")
   119  	}
   120  	if fun.values["sb1"].Op != OpInvalid && fun.values["sb2"].Op != OpInvalid {
   121  		t.Errorf("zsce should have removed sb1 or sb2")
   122  	}
   123  }