github.com/liujq9674git/golang-src-1.7@v0.0.0-20230517174348-17f6ec47f3f8/src/cmd/compile/internal/ssa/deadstore.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ssa 6 7 // dse does dead-store elimination on the Function. 8 // Dead stores are those which are unconditionally followed by 9 // another store to the same location, with no intervening load. 10 // This implementation only works within a basic block. TODO: use something more global. 11 func dse(f *Func) { 12 var stores []*Value 13 loadUse := f.newSparseSet(f.NumValues()) 14 defer f.retSparseSet(loadUse) 15 storeUse := f.newSparseSet(f.NumValues()) 16 defer f.retSparseSet(storeUse) 17 shadowed := newSparseMap(f.NumValues()) // TODO: cache 18 for _, b := range f.Blocks { 19 // Find all the stores in this block. Categorize their uses: 20 // loadUse contains stores which are used by a subsequent load. 21 // storeUse contains stores which are used by a subsequent store. 22 loadUse.clear() 23 storeUse.clear() 24 stores = stores[:0] 25 for _, v := range b.Values { 26 if v.Op == OpPhi { 27 // Ignore phis - they will always be first and can't be eliminated 28 continue 29 } 30 if v.Type.IsMemory() { 31 stores = append(stores, v) 32 for _, a := range v.Args { 33 if a.Block == b && a.Type.IsMemory() { 34 storeUse.add(a.ID) 35 if v.Op != OpStore && v.Op != OpZero && v.Op != OpVarDef && v.Op != OpVarKill { 36 // CALL, DUFFCOPY, etc. are both 37 // reads and writes. 38 loadUse.add(a.ID) 39 } 40 } 41 } 42 } else { 43 for _, a := range v.Args { 44 if a.Block == b && a.Type.IsMemory() { 45 loadUse.add(a.ID) 46 } 47 } 48 } 49 } 50 if len(stores) == 0 { 51 continue 52 } 53 54 // find last store in the block 55 var last *Value 56 for _, v := range stores { 57 if storeUse.contains(v.ID) { 58 continue 59 } 60 if last != nil { 61 b.Fatalf("two final stores - simultaneous live stores %s %s", last, v) 62 } 63 last = v 64 } 65 if last == nil { 66 b.Fatalf("no last store found - cycle?") 67 } 68 69 // Walk backwards looking for dead stores. Keep track of shadowed addresses. 70 // An "address" is an SSA Value which encodes both the address and size of 71 // the write. This code will not remove dead stores to the same address 72 // of different types. 73 shadowed.clear() 74 v := last 75 76 walkloop: 77 if loadUse.contains(v.ID) { 78 // Someone might be reading this memory state. 79 // Clear all shadowed addresses. 80 shadowed.clear() 81 } 82 if v.Op == OpStore || v.Op == OpZero { 83 sz := v.AuxInt 84 if shadowedSize := int64(shadowed.get(v.Args[0].ID)); shadowedSize != -1 && shadowedSize >= sz { 85 // Modify store into a copy 86 if v.Op == OpStore { 87 // store addr value mem 88 v.SetArgs1(v.Args[2]) 89 } else { 90 // zero addr mem 91 typesz := v.Args[0].Type.ElemType().Size() 92 if sz != typesz { 93 f.Fatalf("mismatched zero/store sizes: %d and %d [%s]", 94 sz, typesz, v.LongString()) 95 } 96 v.SetArgs1(v.Args[1]) 97 } 98 v.Aux = nil 99 v.AuxInt = 0 100 v.Op = OpCopy 101 } else { 102 if sz > 0x7fffffff { // work around sparseMap's int32 value type 103 sz = 0x7fffffff 104 } 105 shadowed.set(v.Args[0].ID, int32(sz)) 106 } 107 } 108 // walk to previous store 109 if v.Op == OpPhi { 110 continue // At start of block. Move on to next block. 111 } 112 for _, a := range v.Args { 113 if a.Block == b && a.Type.IsMemory() { 114 v = a 115 goto walkloop 116 } 117 } 118 } 119 }