const top … const constant … const bottom … type lattice … type worklist … // sccp stands for sparse conditional constant propagation, it propagates constants // through CFG conditionally and applies constant folding, constant replacement and // dead code elimination all together. func sccp(f *Func) { … } func equals(a, b lattice) bool { … } // possibleConst checks if Value can be folded to const. For those Values that can // never become constants(e.g. StaticCall), we don't make futile efforts. func possibleConst(val *Value) bool { … } func (t *worklist) getLatticeCell(val *Value) lattice { … } func isConst(val *Value) bool { … } // buildDefUses builds def-use chain for some values early, because once the // lattice of a value is changed, we need to update lattices of use. But we don't // need all uses of it, only uses that can become constants would be added into // re-visit worklist since no matter how many times they are revisited, uses which // can't become constants lattice remains unchanged, i.e. Bottom. func (t *worklist) buildDefUses() { … } // addUses finds all uses of value and appends them into work list for further process func (t *worklist) addUses(val *Value) { … } // meet meets all of phi arguments and computes result lattice func (t *worklist) meet(val *Value) lattice { … } func computeLattice(f *Func, val *Value, args ...*Value) lattice { … } func (t *worklist) visitValue(val *Value) { … } // propagate propagates constants facts through CFG. If the block has single successor, // add the successor anyway. If the block has multiple successors, only add the // branch destination corresponding to lattice value of condition value. func (t *worklist) propagate(block *Block) { … } // rewireSuccessor rewires corresponding successors according to constant value // discovered by previous analysis. As the result, some successors become unreachable // and thus can be removed in further deadcode phase func rewireSuccessor(block *Block, constVal *Value) bool { … } // replaceConst will replace non-constant values that have been proven by sccp // to be constants. func (t *worklist) replaceConst() (int, int) { … }