IsValid reports whether the cond, body_true, body_false and exit node candidates of prim form a valid 2-way conditional statement in g. Control flow graph: cond ↙ ↘ body_true body_false ↘ ↙ exit
(g graph.Directed, dom cfg.DominatorTree)
| 117 | // ↘ ↙ |
| 118 | // exit |
| 119 | func (prim IfElse) IsValid(g graph.Directed, dom cfg.DominatorTree) bool { |
| 120 | // Dominator sanity check. |
| 121 | cond, bodyTrue, bodyFalse, exit := prim.Cond, prim.BodyTrue, prim.BodyFalse, prim.Exit |
| 122 | if !dom.Dominates(cond, bodyTrue) || !dom.Dominates(cond, bodyFalse) || !dom.Dominates(cond, exit) { |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | // Verify that cond has two successors (body_true and body_false). |
| 127 | condSuccs := g.From(cond) |
| 128 | if len(condSuccs) != 2 || !g.HasEdgeFromTo(cond, bodyTrue) || !g.HasEdgeFromTo(cond, bodyFalse) { |
| 129 | return false |
| 130 | } |
| 131 | |
| 132 | // Verify that body_true has one predecessor (cond) and one successor (exit). |
| 133 | bodyTrueSuccs := g.From(bodyTrue) |
| 134 | bodyTruePreds := g.To(bodyTrue) |
| 135 | if len(bodyTruePreds) != 1 || len(bodyTrueSuccs) != 1 || !g.HasEdgeFromTo(bodyTrue, exit) { |
| 136 | return false |
| 137 | } |
| 138 | |
| 139 | // Verify that body_false has one predecessor (cond) and one successor (exit). |
| 140 | bodyFalseSuccs := g.From(bodyFalse) |
| 141 | bodyFalsePreds := g.To(bodyFalse) |
| 142 | if len(bodyFalsePreds) != 1 || len(bodyFalseSuccs) != 1 || !g.HasEdgeFromTo(bodyFalse, exit) { |
| 143 | return false |
| 144 | } |
| 145 | |
| 146 | // Verify that exit has two predecessor (body_true and body_false). |
| 147 | exitPreds := g.To(exit) |
| 148 | return len(exitPreds) == 2 |
| 149 | } |