| |
| |
| |
|
|
| package ssa |
|
|
| |
| |
|
|
| |
| |
| func postorder(f *Func) []*Block { |
| return postorderWithNumbering(f, nil) |
| } |
|
|
| type blockAndIndex struct { |
| b *Block |
| index int |
| } |
|
|
| |
| |
| func postorderWithNumbering(f *Func, ponums []int32) []*Block { |
| seen := f.Cache.allocBoolSlice(f.NumBlocks()) |
| defer f.Cache.freeBoolSlice(seen) |
|
|
| |
| order := make([]*Block, 0, len(f.Blocks)) |
|
|
| |
| |
| |
| s := make([]blockAndIndex, 0, 32) |
| s = append(s, blockAndIndex{b: f.Entry}) |
| seen[f.Entry.ID] = true |
| for len(s) > 0 { |
| tos := len(s) - 1 |
| x := s[tos] |
| b := x.b |
| if i := x.index; i < len(b.Succs) { |
| s[tos].index++ |
| bb := b.Succs[i].Block() |
| if !seen[bb.ID] { |
| seen[bb.ID] = true |
| s = append(s, blockAndIndex{b: bb}) |
| } |
| continue |
| } |
| s = s[:tos] |
| if ponums != nil { |
| ponums[b.ID] = int32(len(order)) |
| } |
| order = append(order, b) |
| } |
| return order |
| } |
|
|
| func dominators(f *Func) []*Block { |
| |
| |
| return f.dominatorsLTOrig(f.Entry) |
| } |
|
|
| |
| func (f *Func) dominatorsLTOrig(entry *Block) []*Block { |
| |
|
|
| maxBlockID := entry.Func.NumBlocks() |
| scratch := f.Cache.allocIDSlice(7 * maxBlockID) |
| defer f.Cache.freeIDSlice(scratch) |
| semi := scratch[0*maxBlockID : 1*maxBlockID] |
| vertex := scratch[1*maxBlockID : 2*maxBlockID] |
| label := scratch[2*maxBlockID : 3*maxBlockID] |
| parent := scratch[3*maxBlockID : 4*maxBlockID] |
| ancestor := scratch[4*maxBlockID : 5*maxBlockID] |
| bucketHead := scratch[5*maxBlockID : 6*maxBlockID] |
| bucketLink := scratch[6*maxBlockID : 7*maxBlockID] |
|
|
| |
| |
| |
| fromID := f.Cache.allocBlockSlice(maxBlockID) |
| defer f.Cache.freeBlockSlice(fromID) |
| for _, v := range f.Blocks { |
| fromID[v.ID] = v |
| } |
| idom := make([]*Block, maxBlockID) |
|
|
| |
| |
| n := f.dfsOrig(entry, semi, vertex, label, parent) |
|
|
| for i := n; i >= 2; i-- { |
| w := vertex[i] |
|
|
| |
| for _, e := range fromID[w].Preds { |
| v := e.b |
| if semi[v.ID] == 0 { |
| |
| |
| continue |
| } |
| u := evalOrig(v.ID, ancestor, semi, label) |
| if semi[u] < semi[w] { |
| semi[w] = semi[u] |
| } |
| } |
|
|
| |
| |
| |
| vsw := vertex[semi[w]] |
| bucketLink[w] = bucketHead[vsw] |
| bucketHead[vsw] = w |
|
|
| linkOrig(parent[w], w, ancestor) |
|
|
| |
| for v := bucketHead[parent[w]]; v != 0; v = bucketLink[v] { |
| u := evalOrig(v, ancestor, semi, label) |
| if semi[u] < semi[v] { |
| idom[v] = fromID[u] |
| } else { |
| idom[v] = fromID[parent[w]] |
| } |
| } |
| } |
| |
| for i := ID(2); i <= n; i++ { |
| w := vertex[i] |
| if idom[w].ID != vertex[semi[w]] { |
| idom[w] = idom[idom[w].ID] |
| } |
| } |
|
|
| return idom |
| } |
|
|
| |
| |
| |
| |
| func (f *Func) dfsOrig(entry *Block, semi, vertex, label, parent []ID) ID { |
| n := ID(0) |
| s := make([]*Block, 0, 256) |
| s = append(s, entry) |
|
|
| for len(s) > 0 { |
| v := s[len(s)-1] |
| s = s[:len(s)-1] |
| |
|
|
| if semi[v.ID] != 0 { |
| continue |
| } |
| n++ |
| semi[v.ID] = n |
| vertex[n] = v.ID |
| label[v.ID] = v.ID |
| |
| for _, e := range v.Succs { |
| w := e.b |
| |
| if semi[w.ID] == 0 { |
| |
| s = append(s, w) |
| parent[w.ID] = v.ID |
| } |
| } |
| } |
| return n |
| } |
|
|
| |
| func compressOrig(v ID, ancestor, semi, label []ID) { |
| if ancestor[ancestor[v]] != 0 { |
| compressOrig(ancestor[v], ancestor, semi, label) |
| if semi[label[ancestor[v]]] < semi[label[v]] { |
| label[v] = label[ancestor[v]] |
| } |
| ancestor[v] = ancestor[ancestor[v]] |
| } |
| } |
|
|
| |
| func evalOrig(v ID, ancestor, semi, label []ID) ID { |
| if ancestor[v] == 0 { |
| return v |
| } |
| compressOrig(v, ancestor, semi, label) |
| return label[v] |
| } |
|
|
| func linkOrig(v, w ID, ancestor []ID) { |
| ancestor[w] = v |
| } |
|
|
| |
| |
| |
| func dominatorsSimple(f *Func) []*Block { |
| |
| |
| idom := make([]*Block, f.NumBlocks()) |
|
|
| |
| post := f.postorder() |
|
|
| |
| postnum := f.Cache.allocIntSlice(f.NumBlocks()) |
| defer f.Cache.freeIntSlice(postnum) |
| for i, b := range post { |
| postnum[b.ID] = i |
| } |
|
|
| |
| idom[f.Entry.ID] = f.Entry |
| if postnum[f.Entry.ID] != len(post)-1 { |
| f.Fatalf("entry block %v not last in postorder", f.Entry) |
| } |
|
|
| |
| for { |
| changed := false |
|
|
| for i := len(post) - 2; i >= 0; i-- { |
| b := post[i] |
| var d *Block |
| for _, e := range b.Preds { |
| p := e.b |
| if idom[p.ID] == nil { |
| continue |
| } |
| if d == nil { |
| d = p |
| continue |
| } |
| d = intersect(d, p, postnum, idom) |
| } |
| if d != idom[b.ID] { |
| idom[b.ID] = d |
| changed = true |
| } |
| } |
| if !changed { |
| break |
| } |
| } |
| |
| idom[f.Entry.ID] = nil |
| return idom |
| } |
|
|
| |
| |
| func intersect(b, c *Block, postnum []int, idom []*Block) *Block { |
| |
| |
| for b != c { |
| if postnum[b.ID] < postnum[c.ID] { |
| b = idom[b.ID] |
| } else { |
| c = idom[c.ID] |
| } |
| } |
| return b |
| } |
|
|