| |
| |
| |
|
|
| package ssa |
|
|
| import ( |
| "cmd/internal/src" |
| "fmt" |
| ) |
|
|
| |
| func fuseEarly(f *Func) { |
| fuse(f, fuseTypePlain|fuseTypeIntInRange|fuseTypeSingleBitDifference|fuseTypeNanCheck) |
| } |
|
|
| |
| func fuseLate(f *Func) { fuse(f, fuseTypePlain|fuseTypeIf|fuseTypeBranchRedirect) } |
|
|
| type fuseType uint8 |
|
|
| const ( |
| fuseTypePlain fuseType = 1 << iota |
| fuseTypeIf |
| fuseTypeIntInRange |
| fuseTypeSingleBitDifference |
| fuseTypeNanCheck |
| fuseTypeBranchRedirect |
| fuseTypeShortCircuit |
| ) |
|
|
| |
| func fuse(f *Func, typ fuseType) { |
| for changed := true; changed; { |
| changed = false |
| |
| |
| |
| for i := len(f.Blocks) - 1; i >= 0; i-- { |
| b := f.Blocks[i] |
| if typ&fuseTypeIf != 0 { |
| changed = fuseBlockIf(b) || changed |
| } |
| if typ&fuseTypeIntInRange != 0 { |
| changed = fuseIntInRange(b) || changed |
| } |
| if typ&fuseTypeSingleBitDifference != 0 { |
| changed = fuseSingleBitDifference(b) || changed |
| } |
| if typ&fuseTypeNanCheck != 0 { |
| changed = fuseNanCheck(b) || changed |
| } |
| if typ&fuseTypePlain != 0 { |
| changed = fuseBlockPlain(b) || changed |
| } |
| if typ&fuseTypeShortCircuit != 0 { |
| changed = shortcircuitBlock(b) || changed |
| } |
| } |
|
|
| if typ&fuseTypeBranchRedirect != 0 { |
| changed = fuseBranchRedirect(f) || changed |
| } |
| if changed { |
| f.invalidateCFG() |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func fuseBlockIf(b *Block) bool { |
| if b.Kind != BlockIf { |
| return false |
| } |
| |
| var ss0, ss1 *Block |
| s0 := b.Succs[0].b |
| i0 := b.Succs[0].i |
| if s0.Kind != BlockPlain || !isEmpty(s0) { |
| s0, ss0 = b, s0 |
| } else { |
| ss0 = s0.Succs[0].b |
| i0 = s0.Succs[0].i |
| } |
| s1 := b.Succs[1].b |
| i1 := b.Succs[1].i |
| if s1.Kind != BlockPlain || !isEmpty(s1) { |
| s1, ss1 = b, s1 |
| } else { |
| ss1 = s1.Succs[0].b |
| i1 = s1.Succs[0].i |
| } |
| if ss0 != ss1 { |
| if s0.Kind == BlockPlain && isEmpty(s0) && s1.Kind == BlockPlain && isEmpty(s1) { |
| |
| if s0 == ss1 { |
| s0, ss0 = b, ss1 |
| } else if ss0 == s1 { |
| s1, ss1 = b, ss0 |
| } else { |
| return false |
| } |
| } else { |
| return false |
| } |
| } |
| ss := ss0 |
|
|
| |
| |
|
|
| for _, v := range ss.Values { |
| if v.Op == OpPhi && v.Uses > 0 && v.Args[i0] != v.Args[i1] { |
| return false |
| } |
| } |
|
|
| |
| |
| b.removeEdge(0) |
| if s0 != b && len(s0.Preds) == 0 { |
| s0.removeEdge(0) |
| |
| |
| for _, v := range s0.Values { |
| v.Block = b |
| } |
| b.Values = append(b.Values, s0.Values...) |
| |
| s0.Kind = BlockInvalid |
| s0.Values = nil |
| s0.Succs = nil |
| s0.Preds = nil |
| } |
|
|
| b.Kind = BlockPlain |
| b.Likely = BranchUnknown |
| b.ResetControls() |
| |
| |
| |
| |
| |
| walkValues := []*Value{} |
| for _, v := range b.Values { |
| if v.Uses == 0 && v.removeable() { |
| walkValues = append(walkValues, v) |
| } |
| } |
| for len(walkValues) != 0 { |
| v := walkValues[len(walkValues)-1] |
| walkValues = walkValues[:len(walkValues)-1] |
| if v.Uses == 0 && v.removeable() { |
| walkValues = append(walkValues, v.Args...) |
| v.reset(OpInvalid) |
| } |
| } |
| return true |
| } |
|
|
| |
| |
| func isEmpty(b *Block) bool { |
| for _, v := range b.Values { |
| if v.Uses > 0 || v.Op.IsCall() || v.Op.HasSideEffects() || v.Type.IsVoid() || opcodeTable[v.Op].nilCheck { |
| return false |
| } |
| } |
| return true |
| } |
|
|
| |
| |
| |
| |
| |
| func fuseBlockPlain(b *Block) bool { |
| if b.Kind != BlockPlain { |
| return false |
| } |
|
|
| c := b.Succs[0].b |
| if len(c.Preds) != 1 || c == b { |
| return false |
| } |
|
|
| |
| for len(b.Preds) == 1 && b.Preds[0].b != c && b.Preds[0].b.Kind == BlockPlain { |
| b = b.Preds[0].b |
| } |
|
|
| |
| for { |
| if c.Kind != BlockPlain { |
| break |
| } |
| cNext := c.Succs[0].b |
| if cNext == b { |
| break |
| } |
| if len(cNext.Preds) != 1 { |
| break |
| } |
| c = cNext |
| } |
|
|
| |
| var b_next *Block |
| for bx := b; bx != c; bx = b_next { |
| |
| |
| |
| b_next = bx.Succs[0].b |
| if bx.Pos.IsStmt() == src.PosIsStmt { |
| l := bx.Pos.Line() |
| outOfOrder := false |
| for _, v := range b_next.Values { |
| if v.Pos.IsStmt() == src.PosNotStmt { |
| continue |
| } |
| if l == v.Pos.Line() { |
| v.Pos = v.Pos.WithIsStmt() |
| l = 0 |
| break |
| } |
| if l < v.Pos.Line() { |
| |
| |
| outOfOrder = true |
| } |
| } |
| if l != 0 && !outOfOrder && (b_next.Pos.Line() == l || b_next.Pos.IsStmt() != src.PosIsStmt) { |
| b_next.Pos = bx.Pos.WithIsStmt() |
| } |
| } |
| |
| for _, v := range bx.Values { |
| v.Block = c |
| } |
| } |
|
|
| |
| total := 0 |
| totalBeforeMax := 0 |
| max_b := b |
|
|
| for bx := b; ; bx = bx.Succs[0].b { |
| if cap(bx.Values) > cap(max_b.Values) { |
| totalBeforeMax = total |
| max_b = bx |
| } |
| total += len(bx.Values) |
| if bx == c { |
| break |
| } |
| } |
|
|
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| var t []*Value |
| if total <= len(c.valstorage) { |
| t = c.valstorage[:total] |
| max_b = c |
| totalBeforeMax = total - len(c.Values) |
| copy(t[totalBeforeMax:], c.Values) |
| } else if total <= cap(max_b.Values) { |
| t = max_b.Values[0:total] |
| copy(t[totalBeforeMax:], max_b.Values) |
| } else { |
| t = make([]*Value, total) |
| max_b = nil |
| } |
|
|
| |
| copyTo := 0 |
| for bx := b; ; bx = bx.Succs[0].b { |
| if bx != max_b { |
| copy(t[copyTo:], bx.Values) |
| } else if copyTo != totalBeforeMax { |
| panic(fmt.Errorf("totalBeforeMax (%d) != copyTo (%d), max_b=%v, b=%v, c=%v", totalBeforeMax, copyTo, max_b, b, c)) |
| } |
| if bx == c { |
| break |
| } |
| copyTo += len(bx.Values) |
| } |
| c.Values = t |
|
|
| |
| c.predstorage[0] = Edge{} |
| if len(b.Preds) > len(b.predstorage) { |
| c.Preds = b.Preds |
| } else { |
| c.Preds = append(c.predstorage[:0], b.Preds...) |
| } |
| for i, e := range c.Preds { |
| p := e.b |
| p.Succs[e.i] = Edge{c, i} |
| } |
| f := b.Func |
| if f.Entry == b { |
| f.Entry = c |
| } |
|
|
| |
| for bx := b; bx != c; bx = b_next { |
| b_next = bx.Succs[0].b |
|
|
| bx.Kind = BlockInvalid |
| bx.Values = nil |
| bx.Preds = nil |
| bx.Succs = nil |
| } |
| return true |
| } |
|
|