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

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

View as plain text