Skip to content
Merged
16 changes: 15 additions & 1 deletion minzc/pkg/mir2/adl_index_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ package mir2
// 2. Adjacent elements: ptr_add B, scale(ext(i + c)) with a dominating
// ptr_add P = B + scale(ext(i)) becomes field P, c<<k when i + c is
// proven not to wrap at the width of i (a loop induction counting up by
// 1 from a constant under an unsigned exit test, adlIndexNoWrap). Without
// 1 from a constant under an unsigned exit test, adlIndexNoWrap, or an
// upper bound on i from rangeFacts, nowrap_range.go). Without
// the proof the two forms differ (the byte sum wraps, the address does
// not), so the rewrite is skipped.
//
Expand Down Expand Up @@ -53,6 +54,7 @@ type adlIndexShare struct {
hasCall map[string]bool
cse bool
changed int
ranges *rangeFacts // built on first use (nowrap_range.go)
}

type adlValueKey struct {
Expand Down Expand Up @@ -384,6 +386,18 @@ func (s *adlIndexShare) noWrap(i Reg, c int64, b *Block) bool {
return false
}
m := int64(1) << uint(t.Width())
if s.inductionNoWrap(i, c, b, t, m) {
return true
}
// Otherwise the range facts: i's upper bound from compares, masks,
// byte extensions and constants (nowrap_range.go).
if s.ranges == nil {
s.ranges = newRangeFacts(s.f)
}
return s.ranges.AddNoWrap(i, c, b, m-1)
}

func (s *adlIndexShare) inductionNoWrap(i Reg, c int64, b *Block, t Ty, m int64) bool {
for _, l := range s.loops {
// Calls in the loop (Opaque) do not matter: the induction is SSA.
if !l.Blocks[b.Label] {
Expand Down
5 changes: 5 additions & 0 deletions minzc/pkg/mir2/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type Func struct {

nextReg Reg // next available virtual register
mod *Module // parent module; non-nil when created via AddFunc

// noWrapAdds holds the no-wrap proofs MarkADLNoWrapAdds recorded on SSA
// MIR for word adds of a constant (nowrap_range.go); the eZ80 address
// fold reads them after allocation, where the MIR is no longer SSA.
noWrapAdds map[*Inst]noWrapProof
}

// ── Contract ──────────────────────────────────────────────────────────────────
Expand Down
Loading
Loading