Skip to content

fix loopbce bug #74479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/cmd/compile/internal/ssa/loopbce.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,25 @@ func findIndVar(f *Func) []indVar {
// Two conditions must happen listed below to accept ind
// as an induction variable.

// First condition: loop entry has a single predecessor, which
// is the header block. This implies that b.Succs[0] is
// reached iff ind < limit.
if len(b.Succs[0].b.Preds) != 1 {
// b.Succs[1] must exit the loop.
// Identify which successor is the *loop entry* (where nxt is computed)
var entry *Block
if sdom.IsAncestorEq(b.Succs[0].b, nxt.Block) {
entry = b.Succs[0].b
} else if sdom.IsAncestorEq(b.Succs[1].b, nxt.Block) {
entry = b.Succs[1].b
} else {
// Neither successor dominates nxt; shape is not a simple for-loop header.
continue
}

// First condition: loop entry has exactly one predecessor (the header).
if len(entry.Preds) != 1 {
continue
}

// Second condition: b.Succs[0] dominates nxt so that
// nxt is computed when inc < limit.
if !sdom.IsAncestorEq(b.Succs[0].b, nxt.Block) {
// inc+ind can only be reached through the branch that enters the loop.
// Second condition: entry must dominate nxt so that nxt = ind+inc
// is only reachable when the loop is taken.
if !sdom.IsAncestorEq(entry, nxt.Block) {
continue
}

Expand Down Expand Up @@ -298,7 +305,7 @@ func findIndVar(f *Func) []indVar {
nxt: nxt,
min: min,
max: max,
entry: b.Succs[0].b,
entry: entry,
flags: flags,
})
b.Logf("found induction variable %v (inc = %v, min = %v, max = %v)\n", ind, inc, min, max)
Expand Down