Skip to content

Commit

Permalink
backend: removes unnecessary ValueDefinition fields
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake committed Jul 10, 2024
1 parent 2c53c6b commit cef1fd7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 59 deletions.
9 changes: 3 additions & 6 deletions internal/engine/wazevo/backend/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,13 @@ func (c *compiler) assignVirtualRegisters() {
typ := p.Type()
vreg := c.AllocateVReg(typ)
c.ssaValueToVRegs[pid] = vreg
c.ssaValueDefinitions[pid] = SSAValueDefinition{BlockParamValue: p, BlkParamVReg: vreg}
c.ssaValueDefinitions[pid] = SSAValueDefinition{V: p}
c.ssaTypeOfVRegID[vreg.ID()] = p.Type()
}

// Assigns each value to a virtual register produced by instructions.
for cur := blk.Root(); cur != nil; cur = cur.Next() {
r, rs := cur.Returns()
var N int
if r.Valid() {
id := r.ID()
ssaTyp := r.Type()
Expand All @@ -238,11 +237,10 @@ func (c *compiler) assignVirtualRegisters() {
c.ssaValueToVRegs[id] = vReg
c.ssaValueDefinitions[id] = SSAValueDefinition{
Instr: cur,
N: 0,
V: r,
RefCount: refCounts[id].RefCount,
}
c.ssaTypeOfVRegID[vReg.ID()] = ssaTyp
N++
}
for _, r := range rs {
id := r.ID()
Expand All @@ -251,11 +249,10 @@ func (c *compiler) assignVirtualRegisters() {
c.ssaValueToVRegs[id] = vReg
c.ssaValueDefinitions[id] = SSAValueDefinition{
Instr: cur,
N: N,
V: r,
RefCount: refCounts[id].RefCount,
}
c.ssaTypeOfVRegID[vReg.ID()] = ssaTyp
N++
}
}
}
Expand Down
44 changes: 17 additions & 27 deletions internal/engine/wazevo/backend/isa/arm64/lower_instr_operands.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func (o operand) assignReg(v regalloc.VReg) operand {
// `mode` is used to extend the operand if the bit length is smaller than mode.bits().
// If the operand can be expressed as operandKindImm12, `mode` is ignored.
func (m *machine) getOperand_Imm12_ER_SR_NR(def *backend.SSAValueDefinition, mode extMode) (op operand) {
if def.IsFromBlockParam() {
return operandNR(def.BlkParamVReg)
if !def.IsFromInstr() {
return operandNR(m.compiler.VRegOf(def.V))
}

instr := def.Instr
Expand All @@ -180,8 +180,8 @@ func (m *machine) getOperand_Imm12_ER_SR_NR(def *backend.SSAValueDefinition, mod
// getOperand_MaybeNegatedImm12_ER_SR_NR is almost the same as getOperand_Imm12_ER_SR_NR, but this might negate the immediate value.
// If the immediate value is negated, the second return value is true, otherwise always false.
func (m *machine) getOperand_MaybeNegatedImm12_ER_SR_NR(def *backend.SSAValueDefinition, mode extMode) (op operand, negatedImm12 bool) {
if def.IsFromBlockParam() {
return operandNR(def.BlkParamVReg), false
if !def.IsFromInstr() {
return operandNR(m.compiler.VRegOf(def.V)), false
}

instr := def.Instr
Expand All @@ -193,7 +193,7 @@ func (m *machine) getOperand_MaybeNegatedImm12_ER_SR_NR(def *backend.SSAValueDef
}

signExtended := int64(c)
if def.SSAValue().Type().Bits() == 32 {
if def.V.Type().Bits() == 32 {
signExtended = (signExtended << 32) >> 32
}
negatedWithoutSign := -signExtended
Expand All @@ -209,8 +209,8 @@ func (m *machine) getOperand_MaybeNegatedImm12_ER_SR_NR(def *backend.SSAValueDef
//
// `mode` is used to extend the operand if the bit length is smaller than mode.bits().
func (m *machine) getOperand_ER_SR_NR(def *backend.SSAValueDefinition, mode extMode) (op operand) {
if def.IsFromBlockParam() {
return operandNR(def.BlkParamVReg)
if !def.IsFromInstr() {
return operandNR(m.compiler.VRegOf(def.V))
}

if m.compiler.MatchInstr(def, ssa.OpcodeSExtend) || m.compiler.MatchInstr(def, ssa.OpcodeUExtend) {
Expand Down Expand Up @@ -252,8 +252,8 @@ func (m *machine) getOperand_ER_SR_NR(def *backend.SSAValueDefinition, mode extM
//
// `mode` is used to extend the operand if the bit length is smaller than mode.bits().
func (m *machine) getOperand_SR_NR(def *backend.SSAValueDefinition, mode extMode) (op operand) {
if def.IsFromBlockParam() {
return operandNR(def.BlkParamVReg)
if !def.IsFromInstr() {
return operandNR(m.compiler.VRegOf(def.V))
}

if m.compiler.MatchInstr(def, ssa.OpcodeIshl) {
Expand All @@ -274,8 +274,8 @@ func (m *machine) getOperand_SR_NR(def *backend.SSAValueDefinition, mode extMode

// getOperand_ShiftImm_NR returns an operand of either operandKindShiftImm or operandKindNR from the given value (defined by `def).
func (m *machine) getOperand_ShiftImm_NR(def *backend.SSAValueDefinition, mode extMode, shiftBitWidth byte) (op operand) {
if def.IsFromBlockParam() {
return operandNR(def.BlkParamVReg)
if !def.IsFromInstr() {
return operandNR(m.compiler.VRegOf(def.V))
}

instr := def.Instr
Expand All @@ -291,26 +291,16 @@ func (m *machine) getOperand_ShiftImm_NR(def *backend.SSAValueDefinition, mode e
// `mode` is used to extend the operand if the bit length is smaller than mode.bits().
func (m *machine) getOperand_NR(def *backend.SSAValueDefinition, mode extMode) (op operand) {
var v regalloc.VReg
if def.IsFromBlockParam() {
v = def.BlkParamVReg
if def.IsFromInstr() && def.Instr.Constant() {
// We inline all the constant instructions so that we could reduce the register usage.
v = m.lowerConstant(def.Instr)
def.Instr.MarkLowered()
} else {
instr := def.Instr
if instr.Constant() {
// We inline all the constant instructions so that we could reduce the register usage.
v = m.lowerConstant(instr)
instr.MarkLowered()
} else {
if n := def.N; n == 0 {
v = m.compiler.VRegOf(instr.Return())
} else {
_, rs := instr.Returns()
v = m.compiler.VRegOf(rs[n-1])
}
}
v = m.compiler.VRegOf(def.V)
}

r := v
switch inBits := def.SSAValue().Type().Bits(); {
switch inBits := def.V.Type().Bits(); {
case mode == extModeNone:
case inBits == 32 && (mode == extModeZeroExtend32 || mode == extModeSignExtend32):
case inBits == 32 && mode == extModeZeroExtend64:
Expand Down
28 changes: 2 additions & 26 deletions internal/engine/wazevo/backend/vdef.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
package backend

import (
"github.com/tetratelabs/wazero/internal/engine/wazevo/backend/regalloc"
"github.com/tetratelabs/wazero/internal/engine/wazevo/ssa"
)

// SSAValueDefinition represents a definition of an SSA value.
// TODO: this eventually should be deleted.
type SSAValueDefinition struct {
// BlockParamValue is valid if Instr == nil
BlockParamValue ssa.Value

// BlkParamVReg is valid if Instr == nil
BlkParamVReg regalloc.VReg

V ssa.Value
// Instr is not nil if this is a definition from an instruction.
Instr *ssa.Instruction
// N is the index of the return value in the instr's return values list.
N int
// RefCount is the number of references to the result.
RefCount uint32
}

func (d *SSAValueDefinition) IsFromInstr() bool {
return d.Instr != nil
}

func (d *SSAValueDefinition) IsFromBlockParam() bool {
return d.Instr == nil
}

func (d *SSAValueDefinition) SSAValue() ssa.Value {
if d.IsFromBlockParam() {
return d.BlockParamValue
} else {
r, rs := d.Instr.Returns()
if d.N == 0 {
return r
} else {
return rs[d.N-1]
}
}
}

0 comments on commit cef1fd7

Please sign in to comment.