// shortcircuit finds situations where branch directions // are always correlated and rewrites the CFG to take // advantage of that fact. // This optimization is useful for compiling && and || expressions. func shortcircuit(f *Func) { … } // shortcircuitBlock checks for a CFG in which an If block // has as its control value a Phi that has a ConstBool arg. // In some such cases, we can rewrite the CFG into a flatter form. // // (1) Look for a CFG of the form // // p other pred(s) // \ / // b // / \ // t other succ // // in which b is an If block containing a single phi value with a single use (b's Control), // which has a ConstBool arg. // p is the predecessor corresponding to the argument slot in which the ConstBool is found. // t is the successor corresponding to the value of the ConstBool arg. // // Rewrite this into // // p other pred(s) // | / // | b // |/ \ // t u // // and remove the appropriate phi arg(s). // // (2) Look for a CFG of the form // // p q // \ / // b // / \ // t u // // in which b is as described in (1). // However, b may also contain other phi values. // The CFG will be modified as described in (1). // However, in order to handle those other phi values, // for each other phi value w, we must be able to eliminate w from b. // We can do that though a combination of moving w to a different block // and rewriting uses of w to use a different value instead. // See shortcircuitPhiPlan for details. func shortcircuitBlock(b *Block) bool { … } // shortcircuitPhiPlan returns a function to handle non-ctl phi values in b, // where b is as described in shortcircuitBlock. // The returned function accepts a value v // and the index i of v in v.Block: v.Block.Values[i] == v. // If the returned function moves v to a different block, it will use v.moveTo. // cidx is the index in ctl of the ConstBool arg. // ti is the index in b.Succs of the always taken branch when arriving from p. // If shortcircuitPhiPlan returns nil, there is no plan available, // and the CFG modifications must not proceed. // The returned function assumes that shortcircuitBlock has completed its CFG modifications. func shortcircuitPhiPlan(b *Block, ctl *Value, cidx int, ti int64) func(*Value, int) { … } // replaceUses replaces all uses of old in b with new. func (b *Block) replaceUses(old, new *Value) { … } // moveTo moves v to dst, adjusting the appropriate Block.Values slices. // The caller is responsible for ensuring that this is safe. // i is the index of v in v.Block.Values. func (v *Value) moveTo(dst *Block, i int) { … }