github.com/euank/go@v0.0.0-20160829210321-495514729181/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 := NewConfig("amd64", DummyFrontend{b}, nil, true)
    37  	fun := Fun(c, "entry", bg(size)...)
    38  	domTree(fun.f)
    39  	CheckFunc(fun.f)
    40  	b.ResetTimer()
    41  	for i := 0; i < b.N; i++ {
    42  		fn(fun.f)
    43  		b.StopTimer()
    44  		CheckFunc(fun.f)
    45  		b.StartTimer()
    46  	}
    47  }
    48  
    49  // benchFnPass runs passFunc across a function with b.N blocks.
    50  func benchFnBlock(b *testing.B, fn passFunc, bg blockGen) {
    51  	b.ReportAllocs()
    52  	c := NewConfig("amd64", DummyFrontend{b}, nil, true)
    53  	fun := Fun(c, "entry", bg(b.N)...)
    54  	domTree(fun.f)
    55  	CheckFunc(fun.f)
    56  	b.ResetTimer()
    57  	for i := 0; i < passCount; i++ {
    58  		fn(fun.f)
    59  	}
    60  	b.StopTimer()
    61  }
    62  
    63  func genFunction(size int) []bloc {
    64  	var blocs []bloc
    65  	elemType := &TypeImpl{Size_: 8, Name: "testtype"}
    66  	ptrType := &TypeImpl{Size_: 8, Ptr: true, Name: "testptr", Elem_: elemType} // dummy for testing
    67  
    68  	valn := func(s string, m, n int) string { return fmt.Sprintf("%s%d-%d", s, m, n) }
    69  	blocs = append(blocs,
    70  		Bloc("entry",
    71  			Valu(valn("store", 0, 4), OpInitMem, TypeMem, 0, nil),
    72  			Valu("sb", OpSB, TypeInvalid, 0, nil),
    73  			Goto(blockn(1)),
    74  		),
    75  	)
    76  	for i := 1; i < size+1; i++ {
    77  		blocs = append(blocs, Bloc(blockn(i),
    78  			Valu(valn("v", i, 0), OpConstBool, TypeBool, 1, nil),
    79  			Valu(valn("addr", i, 1), OpAddr, ptrType, 0, nil, "sb"),
    80  			Valu(valn("addr", i, 2), OpAddr, ptrType, 0, nil, "sb"),
    81  			Valu(valn("addr", i, 3), OpAddr, ptrType, 0, nil, "sb"),
    82  			Valu(valn("zero", i, 1), OpZero, TypeMem, 8, nil, valn("addr", i, 3),
    83  				valn("store", i-1, 4)),
    84  			Valu(valn("store", i, 1), OpStore, TypeMem, 0, nil, valn("addr", i, 1),
    85  				valn("v", i, 0), valn("zero", i, 1)),
    86  			Valu(valn("store", i, 2), OpStore, TypeMem, 0, nil, valn("addr", i, 2),
    87  				valn("v", i, 0), valn("store", i, 1)),
    88  			Valu(valn("store", i, 3), OpStore, TypeMem, 0, nil, valn("addr", i, 1),
    89  				valn("v", i, 0), valn("store", i, 2)),
    90  			Valu(valn("store", i, 4), OpStore, TypeMem, 0, nil, valn("addr", i, 3),
    91  				valn("v", i, 0), valn("store", i, 3)),
    92  			Goto(blockn(i+1))))
    93  	}
    94  
    95  	blocs = append(blocs,
    96  		Bloc(blockn(size+1), Goto("exit")),
    97  		Bloc("exit", Exit("store0-4")),
    98  	)
    99  
   100  	return blocs
   101  }