FindIfReturn returns the first occurrence of a 1-way conditional with a body return statement in g, and a boolean indicating if such a primitive was found.
(g graph.Directed, dom cfg.DominatorTree)
| 71 | // return statement in g, and a boolean indicating if such a primitive was |
| 72 | // found. |
| 73 | func FindIfReturn(g graph.Directed, dom cfg.DominatorTree) (prim IfReturn, ok bool) { |
| 74 | // Range through cond node candidates. |
| 75 | for _, cond := range g.Nodes() { |
| 76 | // Verify that cond has two successors (body and exit). |
| 77 | condSuccs := g.From(cond) |
| 78 | if len(condSuccs) != 2 { |
| 79 | continue |
| 80 | } |
| 81 | prim.Cond = cond |
| 82 | |
| 83 | // Select body and exit node candidates. |
| 84 | prim.Body, prim.Exit = condSuccs[0], condSuccs[1] |
| 85 | if prim.IsValid(g, dom) { |
| 86 | return prim, true |
| 87 | } |
| 88 | |
| 89 | // Swap body and exit node candidates and try again. |
| 90 | prim.Body, prim.Exit = prim.Exit, prim.Body |
| 91 | if prim.IsValid(g, dom) { |
| 92 | return prim, true |
| 93 | } |
| 94 | } |
| 95 | return IfReturn{}, false |
| 96 | } |
| 97 | |
| 98 | // IsValid reports whether the cond, body and exit node candidates of prim form |
| 99 | // a valid 1-way conditional with a body return statement in g. |