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