github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/compile/internal/ssa/passbm_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  package ssa
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  const (
    12  	blockCount = 1000
    13  	passCount  = 15000
    14  )
    15  
    16  type passFunc func(*Func)
    17  
    18  func BenchmarkDSEPass(b *testing.B)           { benchFnPass(b, dse, blockCount, genFunction) }
    19  func BenchmarkDSEPassBlock(b *testing.B)      { benchFnBlock(b, dse, genFunction) }
    20  func BenchmarkCSEPass(b *testing.B)           { benchFnPass(b, cse, blockCount, genFunction) }
    21  func BenchmarkCSEPassBlock(b *testing.B)      { benchFnBlock(b, cse, genFunction) }
    22  func BenchmarkDeadcodePass(b *testing.B)      { benchFnPass(b, deadcode, blockCount, genFunction) }
    23  func BenchmarkDeadcodePassBlock(b *testing.B) { benchFnBlock(b, deadcode, genFunction) }
    24  
    25  func multi(f *Func) {
    26  	cse(f)
    27  	dse(f)
    28  	deadcode(f)
    29  }
    30  func BenchmarkMultiPass(b *testing.B)      { benchFnPass(b, multi, blockCount, genFunction) }
    31  func BenchmarkMultiPassBlock(b *testing.B) { benchFnBlock(b, multi, genFunction) }
    32  
    33  // benchFnPass runs passFunc b.N times across a single function.
    34  func benchFnPass(b *testing.B, fn passFunc, size int, bg blockGen) {
    35  	b.ReportAllocs()
    36  	c := testConfig(b)
    37  	fun := c.Fun("entry", bg(size)...)
    38  	CheckFunc(fun.f)
    39  	b.ResetTimer()
    40  	for i := 0; i < b.N; i++ {
    41  		fn(fun.f)
    42  		b.StopTimer()
    43  		CheckFunc(fun.f)
    44  		b.StartTimer()
    45  	}
    46  }
    47  
    48  // benchFnPass runs passFunc across a function with b.N blocks.
    49  func benchFnBlock(b *testing.B, fn passFunc, bg blockGen) {
    50  	b.ReportAllocs()
    51  	c := testConfig(b)
    52  	fun := c.Fun("entry", bg(b.N)...)
    53  	CheckFunc(fun.f)
    54  	b.ResetTimer()
    55  	for i := 0; i < passCount; i++ {
    56  		fn(fun.f)
    57  	}
    58  	b.StopTimer()
    59  }
    60  
    61  func genFunction(size int) []bloc {
    62  	var blocs []bloc
    63  	elemType := &TypeImpl{Size_: 8, Name: "testtype"}
    64  	ptrType := &TypeImpl{Size_: 8, Ptr: true, Name: "testptr", Elem_: elemType} // dummy for testing
    65  
    66  	valn := func(s string, m, n int) string { return fmt.Sprintf("%s%d-%d", s, m, n) }
    67  	blocs = append(blocs,
    68  		Bloc("entry",
    69  			Valu(valn("store", 0, 4), OpInitMem, TypeMem, 0, nil),
    70  			Valu("sb", OpSB, TypeInvalid, 0, nil),
    71  			Goto(blockn(1)),
    72  		),
    73  	)
    74  	for i := 1; i < size+1; i++ {
    75  		blocs = append(blocs, Bloc(blockn(i),
    76  			Valu(valn("v", i, 0), OpConstBool, TypeBool, 1, nil),
    77  			Valu(valn("addr", i, 1), OpAddr, ptrType, 0, nil, "sb"),
    78  			Valu(valn("addr", i, 2), OpAddr, ptrType, 0, nil, "sb"),
    79  			Valu(valn("addr", i, 3), OpAddr, ptrType, 0, nil, "sb"),
    80  			Valu(valn("zero", i, 1), OpZero, TypeMem, 8, elemType, valn("addr", i, 3),
    81  				valn("store", i-1, 4)),
    82  			Valu(valn("store", i, 1), OpStore, TypeMem, 0, elemType, valn("addr", i, 1),
    83  				valn("v", i, 0), valn("zero", i, 1)),
    84  			Valu(valn("store", i, 2), OpStore, TypeMem, 0, elemType, valn("addr", i, 2),
    85  				valn("v", i, 0), valn("store", i, 1)),
    86  			Valu(valn("store", i, 3), OpStore, TypeMem, 0, elemType, valn("addr", i, 1),
    87  				valn("v", i, 0), valn("store", i, 2)),
    88  			Valu(valn("store", i, 4), OpStore, TypeMem, 0, elemType, valn("addr", i, 3),
    89  				valn("v", i, 0), valn("store", i, 3)),
    90  			Goto(blockn(i+1))))
    91  	}
    92  
    93  	blocs = append(blocs,
    94  		Bloc(blockn(size+1), Goto("exit")),
    95  		Bloc("exit", Exit("store0-4")),
    96  	)
    97  
    98  	return blocs
    99  }