Debian Codegraph @

internal/engine/wazevo/ssa/ssa_test.go

raw
1 package ssa
2
3 import "sort"
4
5 // edgesCase is a map from BasicBlockID to its successors.
6 type edgesCase map[BasicBlockID][]BasicBlockID
7
8 // constructGraphFromEdges constructs a graph from edgesCase.
9 // This comes in handy when we want to test the CFG related passes.
10 func constructGraphFromEdges(edges edgesCase) (b *builder) {
11 b = NewBuilder().(*builder)
12
13 // Collect edges.
14 var maxID BasicBlockID
15 var pairs [][2]BasicBlockID
16 for fromID, toIDs := range edges {
17 if maxID < fromID {
18 maxID = fromID
19 }
20 for _, toID := range toIDs {
21 pairs = append(pairs, [2]BasicBlockID{fromID, toID})
22 if maxID < toID {
23 maxID = toID
24 }
25 }
26 }
27
28 // Allocate blocks.
29 blocks := make(map[BasicBlockID]*basicBlock, maxID+1)
30 for i := 0; i < int(maxID)+1; i++ {
31 blk := b.AllocateBasicBlock().(*basicBlock)
32 blocks[blk.id] = blk
33 }
34
35 // To have a consistent behavior in test, we sort the pairs by fromID.
36 sort.Slice(pairs, func(i, j int) bool {
37 xf, yf := pairs[i][0], pairs[j][0]
38 return xf < yf
39 })
40
41 // Add edges.
42 for _, p := range pairs {
43 from, to := blocks[p[0]], blocks[p[1]]
44 to.addPred(from, &Instruction{})
45 }
46 return
47 }