github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/compile/internal/ssa/shift_test.go (about)

     1  // Copyright 2015 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  	"testing"
     9  )
    10  
    11  func TestShiftConstAMD64(t *testing.T) {
    12  	c := testConfig(t)
    13  	fun := makeConstShiftFunc(c, 18, OpLsh64x64, TypeUInt64)
    14  	checkOpcodeCounts(t, fun.f, map[Op]int{OpAMD64SHLQconst: 1, OpAMD64CMPQconst: 0, OpAMD64ANDQconst: 0})
    15  	fun.f.Free()
    16  	fun = makeConstShiftFunc(c, 66, OpLsh64x64, TypeUInt64)
    17  	checkOpcodeCounts(t, fun.f, map[Op]int{OpAMD64SHLQconst: 0, OpAMD64CMPQconst: 0, OpAMD64ANDQconst: 0})
    18  	fun.f.Free()
    19  	fun = makeConstShiftFunc(c, 18, OpRsh64Ux64, TypeUInt64)
    20  	checkOpcodeCounts(t, fun.f, map[Op]int{OpAMD64SHRQconst: 1, OpAMD64CMPQconst: 0, OpAMD64ANDQconst: 0})
    21  	fun.f.Free()
    22  	fun = makeConstShiftFunc(c, 66, OpRsh64Ux64, TypeUInt64)
    23  	checkOpcodeCounts(t, fun.f, map[Op]int{OpAMD64SHRQconst: 0, OpAMD64CMPQconst: 0, OpAMD64ANDQconst: 0})
    24  	fun.f.Free()
    25  	fun = makeConstShiftFunc(c, 18, OpRsh64x64, TypeInt64)
    26  	checkOpcodeCounts(t, fun.f, map[Op]int{OpAMD64SARQconst: 1, OpAMD64CMPQconst: 0})
    27  	fun.f.Free()
    28  	fun = makeConstShiftFunc(c, 66, OpRsh64x64, TypeInt64)
    29  	checkOpcodeCounts(t, fun.f, map[Op]int{OpAMD64SARQconst: 1, OpAMD64CMPQconst: 0})
    30  	fun.f.Free()
    31  }
    32  
    33  func makeConstShiftFunc(c *Config, amount int64, op Op, typ Type) fun {
    34  	ptyp := &TypeImpl{Size_: 8, Ptr: true, Name: "ptr"}
    35  	fun := Fun(c, "entry",
    36  		Bloc("entry",
    37  			Valu("mem", OpInitMem, TypeMem, 0, nil),
    38  			Valu("SP", OpSP, TypeUInt64, 0, nil),
    39  			Valu("argptr", OpOffPtr, ptyp, 8, nil, "SP"),
    40  			Valu("resptr", OpOffPtr, ptyp, 16, nil, "SP"),
    41  			Valu("load", OpLoad, typ, 0, nil, "argptr", "mem"),
    42  			Valu("c", OpConst64, TypeUInt64, amount, nil),
    43  			Valu("shift", op, typ, 0, nil, "load", "c"),
    44  			Valu("store", OpStore, TypeMem, 8, nil, "resptr", "shift", "mem"),
    45  			Exit("store")))
    46  	Compile(fun.f)
    47  	return fun
    48  }