| |
| |
| |
|
|
| package liveness |
|
|
| import "cmd/compile/internal/bitvec" |
|
|
| |
| const ( |
| h0 = 2166136261 |
| hp = 16777619 |
| ) |
|
|
| |
| type bvecSet struct { |
| index []int |
| uniq []bitvec.BitVec |
| } |
|
|
| func (m *bvecSet) grow() { |
| |
| n := len(m.index) * 2 |
| if n == 0 { |
| n = 32 |
| } |
| newIndex := make([]int, n) |
| for i := range newIndex { |
| newIndex[i] = -1 |
| } |
|
|
| |
| for i, bv := range m.uniq { |
| h := hashbitmap(h0, bv) % uint32(len(newIndex)) |
| for { |
| j := newIndex[h] |
| if j < 0 { |
| newIndex[h] = i |
| break |
| } |
| h++ |
| if h == uint32(len(newIndex)) { |
| h = 0 |
| } |
| } |
| } |
| m.index = newIndex |
| } |
|
|
| |
| |
| |
| func (m *bvecSet) add(bv bitvec.BitVec) (int, bool) { |
| if len(m.uniq)*4 >= len(m.index) { |
| m.grow() |
| } |
|
|
| index := m.index |
| h := hashbitmap(h0, bv) % uint32(len(index)) |
| for { |
| j := index[h] |
| if j < 0 { |
| |
| index[h] = len(m.uniq) |
| m.uniq = append(m.uniq, bv) |
| return len(m.uniq) - 1, true |
| } |
| jlive := m.uniq[j] |
| if bv.Eq(jlive) { |
| |
| return j, false |
| } |
|
|
| h++ |
| if h == uint32(len(index)) { |
| h = 0 |
| } |
| } |
| } |
|
|
| |
| |
| func (m *bvecSet) extractUnique() []bitvec.BitVec { |
| return m.uniq |
| } |
|
|
| func hashbitmap(h uint32, bv bitvec.BitVec) uint32 { |
| n := int((bv.N + 31) / 32) |
| for i := 0; i < n; i++ { |
| w := bv.B[i] |
| h = (h * hp) ^ (w & 0xff) |
| h = (h * hp) ^ ((w >> 8) & 0xff) |
| h = (h * hp) ^ ((w >> 16) & 0xff) |
| h = (h * hp) ^ ((w >> 24) & 0xff) |
| } |
|
|
| return h |
| } |
|
|