Skip to content

Commit 321bd8c

Browse files
committed
cmd/compile: in prove, simplify logic of branch pushing
prove used a complex logic when trying to prove branch conditions: tryPushBranch() was sometimes leaving a checkpoint on the factsTable, sometimes not, and the caller was supposed to check the return value to know what to do. Since we're going to make the prove descend logic a little bit more complex by adding also induction variables, simplify the tryPushBranch logic, by removing any factsTable checkpoint handling from it. Passes toolstash -cmp. Change-Id: Idfb1703df8a455f612f93158328b36c461560781 Reviewed-on: https://go-review.googlesource.com/104035 Run-TryBot: Giovanni Bajo <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 26e0e8a commit 321bd8c

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

src/cmd/compile/internal/ssa/prove.go

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,15 @@ func prove(f *Func) {
594594

595595
switch node.state {
596596
case descend:
597+
ft.checkpoint()
597598
if branch != unknown {
598-
if !tryPushBranch(ft, parent, branch) {
599+
addBranchRestrictions(ft, parent, branch)
600+
if ft.unsat {
599601
// node.block is unreachable.
600602
// Remove it and don't visit
601603
// its children.
602604
removeBranch(parent, branch)
605+
ft.restore()
603606
break
604607
}
605608
// Otherwise, we can now commit to
@@ -620,10 +623,7 @@ func prove(f *Func) {
620623

621624
case simplify:
622625
simplifyBlock(sdom, ft, node.block)
623-
624-
if branch != unknown {
625-
popBranch(ft)
626-
}
626+
ft.restore()
627627
}
628628
}
629629
}
@@ -649,41 +649,22 @@ func getBranch(sdom SparseTree, p *Block, b *Block) branch {
649649
return unknown
650650
}
651651

652-
// tryPushBranch tests whether it is possible to branch from Block b
653-
// in direction br and, if so, pushes the branch conditions in the
654-
// factsTable and returns true. A successful tryPushBranch must be
655-
// paired with a popBranch.
656-
func tryPushBranch(ft *factsTable, b *Block, br branch) bool {
657-
ft.checkpoint()
652+
// addBranchRestrictions updates the factsTables ft with the facts learned when
653+
// branching from Block b in direction br.
654+
func addBranchRestrictions(ft *factsTable, b *Block, br branch) {
658655
c := b.Control
659-
updateRestrictions(b, ft, boolean, nil, c, lt|gt, br)
656+
addRestrictions(b, ft, boolean, nil, c, lt|gt, br)
660657
if tr, has := domainRelationTable[b.Control.Op]; has {
661658
// When we branched from parent we learned a new set of
662659
// restrictions. Update the factsTable accordingly.
663-
updateRestrictions(b, ft, tr.d, c.Args[0], c.Args[1], tr.r, br)
660+
addRestrictions(b, ft, tr.d, c.Args[0], c.Args[1], tr.r, br)
664661
}
665-
if ft.unsat {
666-
// This branch's conditions contradict some known
667-
// fact, so it cannot be taken. Unwind the facts.
668-
//
669-
// (Since we never checkpoint an unsat factsTable, we
670-
// don't really need factsTable.unsatDepth, but
671-
// there's no cost to keeping checkpoint/restore more
672-
// general.)
673-
ft.restore()
674-
return false
675-
}
676-
return true
677-
}
678662

679-
// popBranch undoes the effects of a successful tryPushBranch.
680-
func popBranch(ft *factsTable) {
681-
ft.restore()
682663
}
683664

684-
// updateRestrictions updates restrictions from the immediate
665+
// addRestrictions updates restrictions from the immediate
685666
// dominating block (p) using r. r is adjusted according to the branch taken.
686-
func updateRestrictions(parent *Block, ft *factsTable, t domain, v, w *Value, r relation, branch branch) {
667+
func addRestrictions(parent *Block, ft *factsTable, t domain, v, w *Value, r relation, branch branch) {
687668
if t == 0 || branch == unknown {
688669
// Trivial case: nothing to do, or branch unknown.
689670
// Shoult not happen, but just in case.
@@ -797,7 +778,11 @@ func simplifyBlock(sdom SparseTree, ft *factsTable, b *Block) {
797778
}
798779
// For edges to other blocks, this can trim a branch
799780
// even if we couldn't get rid of the child itself.
800-
if !tryPushBranch(ft, parent, branch) {
781+
ft.checkpoint()
782+
addBranchRestrictions(ft, parent, branch)
783+
unsat := ft.unsat
784+
ft.restore()
785+
if unsat {
801786
// This branch is impossible, so remove it
802787
// from the block.
803788
removeBranch(parent, branch)
@@ -808,7 +793,6 @@ func simplifyBlock(sdom SparseTree, ft *factsTable, b *Block) {
808793
// BlockExit, but it doesn't seem worth it.)
809794
break
810795
}
811-
popBranch(ft)
812796
}
813797
}
814798

0 commit comments

Comments
 (0)