Source file src/cmd/compile/internal/ssacompile/licm_test.go

     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 ssacompile
     6  
     7  import (
     8  	"testing"
     9  
    10  	"cmd/compile/internal/ssa/ssaop"
    11  	"cmd/compile/internal/types"
    12  )
    13  
    14  func TestLICM(t *testing.T) {
    15  	c := testConfig(t)
    16  	fun := c.Fun("entry",
    17  		Bloc("entry",
    18  			Valu("mem", ssaop.OpInitMem, types.TypeMem, 0, nil),
    19  			Valu("sp", ssaop.OpSP, c.config.Types.Uintptr, 0, nil),
    20  			Valu("a", ssaop.OpConst64, c.config.Types.Int64, 14, nil),
    21  			Goto("loop")),
    22  		Bloc("loop",
    23  			Valu("b", ssaop.OpAMD64MOVQconst, c.config.Types.Int64, 26, nil),
    24  			Valu("sum", ssaop.OpAMD64ADDQ, c.config.Types.Int64, 0, nil, "a", "b"),
    25  			Valu("load", ssaop.OpLoad, c.config.Types.BytePtr, 0, nil, "sp", "mem"),
    26  			Valu("nilptr", ssaop.OpConstNil, c.config.Types.BytePtr, 0, nil),
    27  			Valu("bool", ssaop.OpNeqPtr, c.config.Types.Bool, 0, nil, "load", "nilptr"),
    28  			If("bool", "loop", "exit")),
    29  		Bloc("exit",
    30  			Exit("mem")))
    31  
    32  	CheckFunc(fun.f)
    33  	licm(fun.f)
    34  	CheckFunc(fun.f)
    35  
    36  	b := fun.blocks["entry"]
    37  	if len(b.Values) != 5 {
    38  		// b,sum should have been moved from loop to entry
    39  		t.Errorf("loop invariant code wasn't lifted, but should have")
    40  	}
    41  }
    42  
    43  func TestLICMNewBlock(t *testing.T) {
    44  	c := testConfig(t)
    45  	fun := c.Fun("entry",
    46  		Bloc("entry",
    47  			Valu("mem", ssaop.OpInitMem, types.TypeMem, 0, nil),
    48  			Valu("sp", ssaop.OpSP, c.config.Types.Uintptr, 0, nil),
    49  			Valu("a", ssaop.OpConst64, c.config.Types.Int64, 14, nil),
    50  			Valu("bool2", ssaop.OpConstBool, c.config.Types.Bool, 0, nil),
    51  			If("bool2", "loop", "exit")),
    52  		Bloc("loop",
    53  			Valu("b", ssaop.OpAMD64MOVQconst, c.config.Types.Int64, 26, nil),
    54  			Valu("sum", ssaop.OpAMD64ADDQ, c.config.Types.Int64, 0, nil, "a", "b"),
    55  			Valu("load", ssaop.OpLoad, c.config.Types.BytePtr, 0, nil, "sp", "mem"),
    56  			Valu("nilptr", ssaop.OpConstNil, c.config.Types.BytePtr, 0, nil),
    57  			Valu("bool", ssaop.OpNeqPtr, c.config.Types.Bool, 0, nil, "load", "nilptr"),
    58  			If("bool", "loop", "exit")),
    59  		Bloc("exit",
    60  			Exit("mem")))
    61  
    62  	CheckFunc(fun.f)
    63  	licm(fun.f)
    64  	CheckFunc(fun.f)
    65  
    66  	b := fun.blocks["entry"].Succs[0].B
    67  	if len(b.Values) != 2 {
    68  		// b,sum should have been moved from loop to new block between entry & loop
    69  		t.Errorf("loop invariant code wasn't lifted, but should have")
    70  	}
    71  }
    72  

View as plain text