reg,pass: refactor allocation of aliased registers (#121)

Issue #100 demonstrated that register allocation for aliased registers is
fundamentally broken. The root of the issue is that currently accesses to the
same virtual register with different masks are treated as different registers.
This PR takes a different approach:

* Liveness analysis is masked: we now properly consider which parts of a register are live
* Register allocation produces a mapping from virtual to physical ID, and aliasing is applied later

In addition, a new pass ZeroExtend32BitOutputs accounts for the fact that 32-bit writes in 64-bit mode should actually be treated as 64-bit writes (the result is zero-extended).

Closes #100
This commit is contained in:
Michael McLoughlin
2020-01-22 22:50:40 -08:00
committed by GitHub
parent 126469f13d
commit f40d602170
33 changed files with 1241 additions and 362 deletions

View File

@@ -1,69 +1,99 @@
package reg
// Set is a set of registers.
type Set map[Register]bool
// MaskSet maps register IDs to masks.
type MaskSet map[ID]uint16
// NewEmptySet builds an empty register set.
func NewEmptySet() Set {
return Set{}
// NewEmptyMaskSet builds an empty register mask set.
func NewEmptyMaskSet() MaskSet {
return MaskSet{}
}
// NewSetFromSlice forms a set from the given register list.
func NewSetFromSlice(rs []Register) Set {
s := NewEmptySet()
// NewMaskSetFromRegisters forms a mask set from the given register list.
func NewMaskSetFromRegisters(rs []Register) MaskSet {
s := NewEmptyMaskSet()
for _, r := range rs {
s.Add(r)
s.AddRegister(r)
}
return s
}
// Clone returns a copy of s.
func (s Set) Clone() Set {
c := NewEmptySet()
for r := range s {
c.Add(r)
func (s MaskSet) Clone() MaskSet {
c := NewEmptyMaskSet()
for id, mask := range s {
c.Add(id, mask)
}
return c
}
// Add r to s.
func (s Set) Add(r Register) {
s[r] = true
}
// Discard removes r from s, if present.
func (s Set) Discard(r Register) {
delete(s, r)
}
// Update adds every register in t to s.
func (s Set) Update(t Set) {
for r := range t {
s.Add(r)
// Add mask to the given register ID.
// Reports whether this made any change to the set.
func (s MaskSet) Add(id ID, mask uint16) bool {
if (s[id] & mask) == mask {
return false
}
s[id] |= mask
return true
}
// AddRegister is a convenience for adding the register's (ID, mask) to the set.
// Reports whether this made any change to the set.
func (s MaskSet) AddRegister(r Register) bool {
return s.Add(r.ID(), r.Mask())
}
// Discard clears masked bits from register ID.
// Reports whether this made any change to the set.
func (s MaskSet) Discard(id ID, mask uint16) bool {
if curr, found := s[id]; !found || (curr&mask) == 0 {
return false
}
s[id] &^= mask
if s[id] == 0 {
delete(s, id)
}
return true
}
// DiscardRegister is a convenience for discarding the register's (ID, mask) from the set.
// Reports whether this made any change to the set.
func (s MaskSet) DiscardRegister(r Register) bool {
return s.Discard(r.ID(), r.Mask())
}
// Update adds masks in t to s.
// Reports whether this made any change to the set.
func (s MaskSet) Update(t MaskSet) bool {
change := false
for id, mask := range t {
change = s.Add(id, mask) || change
}
return change
}
// Difference returns the set of registers in s but not t.
func (s Set) Difference(t Set) Set {
func (s MaskSet) Difference(t MaskSet) MaskSet {
d := s.Clone()
d.DifferenceUpdate(t)
return d
}
// DifferenceUpdate removes every element of t from s.
func (s Set) DifferenceUpdate(t Set) {
for r := range t {
s.Discard(r)
func (s MaskSet) DifferenceUpdate(t MaskSet) bool {
change := false
for id, mask := range t {
change = s.Discard(id, mask) || change
}
return change
}
// Equals returns true if s and t contain the same registers.
func (s Set) Equals(t Set) bool {
// Equals returns true if s and t contain the same masks.
func (s MaskSet) Equals(t MaskSet) bool {
if len(s) != len(t) {
return false
}
for r := range s {
if _, found := t[r]; !found {
for id, mask := range s {
if _, found := t[id]; !found || mask != t[id] {
return false
}
}
@@ -71,11 +101,11 @@ func (s Set) Equals(t Set) bool {
}
// OfKind returns the set of elements of s with kind k.
func (s Set) OfKind(k Kind) Set {
t := NewEmptySet()
for r := range s {
if r.Kind() == k {
t.Add(r)
func (s MaskSet) OfKind(k Kind) MaskSet {
t := NewEmptyMaskSet()
for id, mask := range s {
if id.Kind() == k {
t.Add(id, mask)
}
}
return t