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 
      5 package ssa
      6 
      7 import "testing"
      8 
      9 func TestSchedule(t *testing.T) {
     10 	c := testConfig(t)
     11 	cases := []fun{
     12 		Fun(c, "entry",
     13 			Bloc("entry",
     14 				Valu("mem0", OpInitMem, TypeMem, 0, nil),
     15 				Valu("ptr", OpConst64, TypeInt64, 0xABCD, nil),
     16 				Valu("v", OpConst64, TypeInt64, 12, nil),
     17 				Valu("mem1", OpStore, TypeMem, 8, nil, "ptr", "v", "mem0"),
     18 				Valu("mem2", OpStore, TypeMem, 8, nil, "ptr", "v", "mem1"),
     19 				Valu("mem3", OpStore, TypeInt64, 8, nil, "ptr", "sum", "mem2"),
     20 				Valu("l1", OpLoad, TypeInt64, 0, nil, "ptr", "mem1"),
     21 				Valu("l2", OpLoad, TypeInt64, 0, nil, "ptr", "mem2"),
     22 				Valu("sum", OpAdd64, TypeInt64, 0, nil, "l1", "l2"),
     23 				Goto("exit")),
     24 			Bloc("exit",
     25 				Exit("mem3"))),
     26 	}
     27 	for _, c := range cases {
     28 		schedule(c.f)
     29 		if !isSingleLiveMem(c.f) {
     30 			t.Error("single-live-mem restriction not enforced by schedule for func:")
     31 			printFunc(c.f)
     32 		}
     33 	}
     34 }
     35 
     36 func isSingleLiveMem(f *Func) bool {
     37 	for _, b := range f.Blocks {
     38 		var liveMem *Value
     39 		for _, v := range b.Values {
     40 			for _, w := range v.Args {
     41 				if w.Type.IsMemory() {
     42 					if liveMem == nil {
     43 						liveMem = w
     44 						continue
     45 					}
     46 					if w != liveMem {
     47 						return false
     48 					}
     49 				}
     50 			}
     51 			if v.Type.IsMemory() {
     52 				liveMem = v
     53 			}
     54 		}
     55 	}
     56 	return true
     57 }
     58