LEFT | RIGHT |
1 // Copyright 2014 The Go Authors. All rights reserved. | 1 // Copyright 2014 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Removal of dead code and data. | 5 // Removal of dead code and data. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 import "debug/goobj" | 9 import "debug/goobj" |
10 | 10 |
11 // dead removes unreachable code and data from the program. | 11 // dead removes unreachable code and data from the program. |
12 // It is basically a mark-sweep garbage collection: traverse all the | 12 // It is basically a mark-sweep garbage collection: traverse all the |
13 // symbols reachable from the entry (startSymID) and then delete | 13 // symbols reachable from the entry (startSymID) and then delete |
14 // the rest. | 14 // the rest. |
15 func (p *Prog) dead() { | 15 func (p *Prog) dead() { |
| 16 p.Dead = make(map[goobj.SymID]bool) |
16 reachable := make(map[goobj.SymID]bool) | 17 reachable := make(map[goobj.SymID]bool) |
17 p.walkDead(p.startSym, reachable) | 18 p.walkDead(p.startSym, reachable) |
18 | 19 |
19 for sym := range p.Syms { | 20 for sym := range p.Syms { |
20 if !reachable[sym] { | 21 if !reachable[sym] { |
21 delete(p.Syms, sym) | 22 delete(p.Syms, sym) |
| 23 p.Dead[sym] = true |
22 } | 24 } |
23 } | 25 } |
24 | 26 |
25 for sym := range p.Missing { | 27 for sym := range p.Missing { |
26 if !reachable[sym] { | 28 if !reachable[sym] { |
27 delete(p.Missing, sym) | 29 delete(p.Missing, sym) |
| 30 p.Dead[sym] = true |
28 } | 31 } |
29 } | 32 } |
30 | 33 |
31 p.SymOrder = removeDead(p.SymOrder, reachable) | 34 p.SymOrder = removeDead(p.SymOrder, reachable) |
32 | 35 |
33 for _, pkg := range p.Packages { | 36 for _, pkg := range p.Packages { |
34 pkg.Syms = removeDead(pkg.Syms, reachable) | 37 pkg.Syms = removeDead(pkg.Syms, reachable) |
35 } | 38 } |
36 } | 39 } |
37 | 40 |
(...skipping 24 matching lines...) Expand all Loading... |
62 // returning a shortened slice using the same underlying array. | 65 // returning a shortened slice using the same underlying array. |
63 func removeDead(syms []*Sym, reachable map[goobj.SymID]bool) []*Sym { | 66 func removeDead(syms []*Sym, reachable map[goobj.SymID]bool) []*Sym { |
64 keep := syms[:0] | 67 keep := syms[:0] |
65 for _, sym := range syms { | 68 for _, sym := range syms { |
66 if reachable[sym.SymID] { | 69 if reachable[sym.SymID] { |
67 keep = append(keep, sym) | 70 keep = append(keep, sym) |
68 } | 71 } |
69 } | 72 } |
70 return keep | 73 return keep |
71 } | 74 } |
LEFT | RIGHT |