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