| |
| |
| |
|
|
| package ssa |
|
|
| import ( |
| "cmd/compile/internal/types" |
| "cmd/internal/src" |
| "cmp" |
| "fmt" |
| "slices" |
| ) |
|
|
| |
| |
| |
| func cse(f *Func) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| a := f.Cache.allocValueSlice(f.NumValues()) |
| defer func() { f.Cache.freeValueSlice(a) }() |
| a = a[:0] |
| o := f.Cache.allocInt32Slice(f.NumValues()) |
| defer func() { f.Cache.freeInt32Slice(o) }() |
| if f.auxmap == nil { |
| f.auxmap = auxmap{} |
| } |
| for _, b := range f.Blocks { |
| for _, v := range b.Values { |
| if v.Type.IsMemory() { |
| continue |
| } |
| if f.auxmap[v.Aux] == 0 { |
| f.auxmap[v.Aux] = int32(len(f.auxmap)) + 1 |
| } |
| a = append(a, v) |
| } |
| } |
| partition := partitionValues(a, f.auxmap) |
|
|
| |
| valueEqClass := f.Cache.allocIDSlice(f.NumValues()) |
| defer f.Cache.freeIDSlice(valueEqClass) |
| for _, b := range f.Blocks { |
| for _, v := range b.Values { |
| |
| valueEqClass[v.ID] = -v.ID |
| } |
| } |
| var pNum ID = 1 |
| for _, e := range partition { |
| if f.pass.debug > 1 && len(e) > 500 { |
| fmt.Printf("CSE.large partition (%d): ", len(e)) |
| for j := 0; j < 3; j++ { |
| fmt.Printf("%s ", e[j].LongString()) |
| } |
| fmt.Println() |
| } |
|
|
| for _, v := range e { |
| valueEqClass[v.ID] = pNum |
| } |
| if f.pass.debug > 2 && len(e) > 1 { |
| fmt.Printf("CSE.partition #%d:", pNum) |
| for _, v := range e { |
| fmt.Printf(" %s", v.String()) |
| } |
| fmt.Printf("\n") |
| } |
| pNum++ |
| } |
|
|
| |
| |
| |
| var splitPoints []int |
| for { |
| changed := false |
|
|
| |
| |
| for i := 0; i < len(partition); i++ { |
| e := partition[i] |
|
|
| if opcodeTable[e[0].Op].commutative { |
| |
| for _, v := range e { |
| if valueEqClass[v.Args[0].ID] > valueEqClass[v.Args[1].ID] { |
| v.Args[0], v.Args[1] = v.Args[1], v.Args[0] |
| } |
| } |
| } |
|
|
| |
| slices.SortFunc(e, func(v, w *Value) int { |
| for i, a := range v.Args { |
| b := w.Args[i] |
| if valueEqClass[a.ID] < valueEqClass[b.ID] { |
| return -1 |
| } |
| if valueEqClass[a.ID] > valueEqClass[b.ID] { |
| return +1 |
| } |
| } |
| return 0 |
| }) |
|
|
| |
| splitPoints = append(splitPoints[:0], 0) |
| for j := 1; j < len(e); j++ { |
| v, w := e[j-1], e[j] |
| |
| eqArgs := true |
| for k, a := range v.Args { |
| if v.Op == OpLocalAddr && k == 1 { |
| continue |
| } |
| b := w.Args[k] |
| if valueEqClass[a.ID] != valueEqClass[b.ID] { |
| eqArgs = false |
| break |
| } |
| } |
| if !eqArgs { |
| splitPoints = append(splitPoints, j) |
| } |
| } |
| if len(splitPoints) == 1 { |
| continue |
| } |
|
|
| |
| partition[i] = partition[len(partition)-1] |
| partition = partition[:len(partition)-1] |
| i-- |
|
|
| |
| splitPoints = append(splitPoints, len(e)) |
| for j := 0; j < len(splitPoints)-1; j++ { |
| f := e[splitPoints[j]:splitPoints[j+1]] |
| if len(f) == 1 { |
| |
| valueEqClass[f[0].ID] = -f[0].ID |
| continue |
| } |
| for _, v := range f { |
| valueEqClass[v.ID] = pNum |
| } |
| pNum++ |
| partition = append(partition, f) |
| } |
| changed = true |
| } |
|
|
| if !changed { |
| break |
| } |
| } |
|
|
| sdom := f.Sdom() |
|
|
| |
| |
| rewrite := f.Cache.allocValueSlice(f.NumValues()) |
| defer f.Cache.freeValueSlice(rewrite) |
| for _, e := range partition { |
| slices.SortFunc(e, func(v, w *Value) int { |
| c := cmp.Compare(sdom.domorder(v.Block), sdom.domorder(w.Block)) |
| if c != 0 { |
| return c |
| } |
| if v.Op == OpLocalAddr { |
| |
| vm := v.Args[1] |
| wm := w.Args[1] |
| if vm == wm { |
| return 0 |
| } |
| |
| |
| |
| if vm.Block != v.Block { |
| return -1 |
| } |
| if wm.Block != w.Block { |
| return +1 |
| } |
| |
| vs := storeOrdering(vm, o) |
| ws := storeOrdering(wm, o) |
| if vs <= 0 { |
| f.Fatalf("unable to determine the order of %s", vm.LongString()) |
| } |
| if ws <= 0 { |
| f.Fatalf("unable to determine the order of %s", wm.LongString()) |
| } |
| return cmp.Compare(vs, ws) |
| } |
| vStmt := v.Pos.IsStmt() == src.PosIsStmt |
| wStmt := w.Pos.IsStmt() == src.PosIsStmt |
| if vStmt != wStmt { |
| if vStmt { |
| return -1 |
| } |
| return +1 |
| } |
| return 0 |
| }) |
|
|
| for i := 0; i < len(e)-1; i++ { |
| |
| v := e[i] |
| if v == nil { |
| continue |
| } |
|
|
| e[i] = nil |
| |
| for j := i + 1; j < len(e); j++ { |
| w := e[j] |
| if w == nil { |
| continue |
| } |
| if sdom.IsAncestorEq(v.Block, w.Block) { |
| rewrite[w.ID] = v |
| e[j] = nil |
| } else { |
| |
| break |
| } |
| } |
| } |
| } |
|
|
| rewrites := int64(0) |
|
|
| |
| for _, b := range f.Blocks { |
| for _, v := range b.Values { |
| for i, w := range v.Args { |
| if x := rewrite[w.ID]; x != nil { |
| if w.Pos.IsStmt() == src.PosIsStmt { |
| |
| |
| |
| if w.Block == v.Block && w.Pos.Line() == v.Pos.Line() { |
| v.Pos = v.Pos.WithIsStmt() |
| w.Pos = w.Pos.WithNotStmt() |
| } |
| } |
| v.SetArg(i, x) |
| rewrites++ |
| } |
| } |
| } |
| for i, v := range b.ControlValues() { |
| if x := rewrite[v.ID]; x != nil { |
| if v.Op == OpNilCheck { |
| |
| |
| continue |
| } |
| b.ReplaceControl(i, x) |
| } |
| } |
| } |
|
|
| if f.pass.stats > 0 { |
| f.LogStat("CSE REWRITES", rewrites) |
| } |
| } |
|
|
| |
| |
| |
| |
| func storeOrdering(v *Value, cache []int32) int32 { |
| const minScore int32 = 1 |
| score := minScore |
| w := v |
| for { |
| if s := cache[w.ID]; s >= minScore { |
| score += s |
| break |
| } |
| if w.Op == OpPhi || w.Op == OpInitMem { |
| break |
| } |
| a := w.MemoryArg() |
| if a.Block != w.Block { |
| break |
| } |
| w = a |
| score++ |
| } |
| w = v |
| for cache[w.ID] == 0 { |
| cache[w.ID] = score |
| if score == minScore { |
| break |
| } |
| w = w.MemoryArg() |
| score-- |
| } |
| return cache[v.ID] |
| } |
|
|
| |
| |
| |
| type eqclass []*Value |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func partitionValues(a []*Value, auxIDs auxmap) []eqclass { |
| slices.SortFunc(a, func(v, w *Value) int { |
| switch cmpVal(v, w, auxIDs) { |
| case types.CMPlt: |
| return -1 |
| case types.CMPgt: |
| return +1 |
| default: |
| |
| return cmp.Compare(v.ID, w.ID) |
| } |
| }) |
|
|
| var partition []eqclass |
| for len(a) > 0 { |
| v := a[0] |
| j := 1 |
| for ; j < len(a); j++ { |
| w := a[j] |
| if cmpVal(v, w, auxIDs) != types.CMPeq { |
| break |
| } |
| } |
| if j > 1 { |
| partition = append(partition, a[:j]) |
| } |
| a = a[j:] |
| } |
|
|
| return partition |
| } |
| func lt2Cmp(isLt bool) types.Cmp { |
| if isLt { |
| return types.CMPlt |
| } |
| return types.CMPgt |
| } |
|
|
| type auxmap map[Aux]int32 |
|
|
| func cmpVal(v, w *Value, auxIDs auxmap) types.Cmp { |
| |
| if v.Op != w.Op { |
| return lt2Cmp(v.Op < w.Op) |
| } |
| if v.AuxInt != w.AuxInt { |
| return lt2Cmp(v.AuxInt < w.AuxInt) |
| } |
| if len(v.Args) != len(w.Args) { |
| return lt2Cmp(len(v.Args) < len(w.Args)) |
| } |
| if v.Op == OpPhi && v.Block != w.Block { |
| return lt2Cmp(v.Block.ID < w.Block.ID) |
| } |
| if v.Type.IsMemory() { |
| |
| |
| return lt2Cmp(v.ID < w.ID) |
| } |
| |
| |
| |
| if v.Op != OpSelect0 && v.Op != OpSelect1 && v.Op != OpSelectN { |
| if tc := v.Type.Compare(w.Type); tc != types.CMPeq { |
| return tc |
| } |
| } |
|
|
| if v.Aux != w.Aux { |
| if v.Aux == nil { |
| return types.CMPlt |
| } |
| if w.Aux == nil { |
| return types.CMPgt |
| } |
| return lt2Cmp(auxIDs[v.Aux] < auxIDs[w.Aux]) |
| } |
|
|
| return types.CMPeq |
| } |
|
|