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

View as plain text