Commit cfc2aa56 authored by Keith Randall's avatar Keith Randall

[dev.ssa] cmd/internal/ssa: Handle more instructions + some cleanup

Add & as an input op.  Add several output ops (loads & stores, TESTB,
LEAQglobal, branches, memcopy)

Some other small things:
- Add exprAddr to builder to generate addresses of expressions.  Use it in
  various places that had ad-hoc code.
- Separate out nil & bounds check generation to separate functions.
- Add explicit FP and SP ops so we dont need specialized *FP and *SP opcodes.
- Fix fallthrough at end of functions with no return values.
- rematerialization of more opcodes.

Change-Id: I781decfcef9770fb15f0cd6b061547f7824a2d5e
Reviewed-on: https://go-review.googlesource.com/10213Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 083a646f
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
func buildssa(fn *Node) *ssa.Func { func buildssa(fn *Node) *ssa.Func {
dumplist("buildssa", Curfn.Nbody) dumplist("buildssa", Curfn.Nbody)
var s ssaState var s state
// TODO(khr): build config just once at the start of the compiler binary // TODO(khr): build config just once at the start of the compiler binary
s.config = ssa.NewConfig(Thearch.Thestring) s.config = ssa.NewConfig(Thearch.Thestring)
...@@ -33,8 +33,10 @@ func buildssa(fn *Node) *ssa.Func { ...@@ -33,8 +33,10 @@ func buildssa(fn *Node) *ssa.Func {
// Allocate exit block // Allocate exit block
s.exit = s.f.NewBlock(ssa.BlockExit) s.exit = s.f.NewBlock(ssa.BlockExit)
// TODO(khr): all args. Make a struct containing args/returnvals, declare // Allocate starting values
// an FP which contains a pointer to that struct. s.startmem = s.f.Entry.NewValue(ssa.OpArg, ssa.TypeMem, ".mem")
s.fp = s.f.Entry.NewValue(ssa.OpFP, s.config.Uintptr, nil) // TODO: use generic pointer type (unsafe.Pointer?) instead
s.sp = s.f.Entry.NewValue(ssa.OpSP, s.config.Uintptr, nil)
s.vars = map[string]*ssa.Value{} s.vars = map[string]*ssa.Value{}
s.labels = map[string]*ssa.Block{} s.labels = map[string]*ssa.Block{}
...@@ -44,6 +46,11 @@ func buildssa(fn *Node) *ssa.Func { ...@@ -44,6 +46,11 @@ func buildssa(fn *Node) *ssa.Func {
s.startBlock(s.f.Entry) s.startBlock(s.f.Entry)
s.stmtList(fn.Nbody) s.stmtList(fn.Nbody)
// fallthrough to exit
if b := s.endBlock(); b != nil {
addEdge(b, s.exit)
}
// Finish up exit block // Finish up exit block
s.startBlock(s.exit) s.startBlock(s.exit)
s.exit.Control = s.mem() s.exit.Control = s.mem()
...@@ -58,7 +65,7 @@ func buildssa(fn *Node) *ssa.Func { ...@@ -58,7 +65,7 @@ func buildssa(fn *Node) *ssa.Func {
return s.f return s.f
} }
type ssaState struct { type state struct {
// configuration (arch) information // configuration (arch) information
config *ssa.Config config *ssa.Config
...@@ -83,10 +90,18 @@ type ssaState struct { ...@@ -83,10 +90,18 @@ type ssaState struct {
// offsets of argument slots // offsets of argument slots
// unnamed and unused args are not listed. // unnamed and unused args are not listed.
argOffsets map[string]int64 argOffsets map[string]int64
// starting values. Memory, frame pointer, and stack pointer
startmem *ssa.Value
fp *ssa.Value
sp *ssa.Value
} }
// startBlock sets the current block we're generating code in to b. // startBlock sets the current block we're generating code in to b.
func (s *ssaState) startBlock(b *ssa.Block) { func (s *state) startBlock(b *ssa.Block) {
if s.curBlock != nil {
log.Fatalf("starting block %v when block %v has not ended", b, s.curBlock)
}
s.curBlock = b s.curBlock = b
s.vars = map[string]*ssa.Value{} s.vars = map[string]*ssa.Value{}
} }
...@@ -94,7 +109,7 @@ func (s *ssaState) startBlock(b *ssa.Block) { ...@@ -94,7 +109,7 @@ func (s *ssaState) startBlock(b *ssa.Block) {
// endBlock marks the end of generating code for the current block. // endBlock marks the end of generating code for the current block.
// Returns the (former) current block. Returns nil if there is no current // Returns the (former) current block. Returns nil if there is no current
// block, i.e. if no code flows to the current execution point. // block, i.e. if no code flows to the current execution point.
func (s *ssaState) endBlock() *ssa.Block { func (s *state) endBlock() *ssa.Block {
b := s.curBlock b := s.curBlock
if b == nil { if b == nil {
return nil return nil
...@@ -109,14 +124,14 @@ func (s *ssaState) endBlock() *ssa.Block { ...@@ -109,14 +124,14 @@ func (s *ssaState) endBlock() *ssa.Block {
} }
// ssaStmtList converts the statement n to SSA and adds it to s. // ssaStmtList converts the statement n to SSA and adds it to s.
func (s *ssaState) stmtList(l *NodeList) { func (s *state) stmtList(l *NodeList) {
for ; l != nil; l = l.Next { for ; l != nil; l = l.Next {
s.stmt(l.N) s.stmt(l.N)
} }
} }
// ssaStmt converts the statement n to SSA and adds it to s. // ssaStmt converts the statement n to SSA and adds it to s.
func (s *ssaState) stmt(n *Node) { func (s *state) stmt(n *Node) {
s.stmtList(n.Ninit) s.stmtList(n.Ninit)
switch n.Op { switch n.Op {
...@@ -145,35 +160,15 @@ func (s *ssaState) stmt(n *Node) { ...@@ -145,35 +160,15 @@ func (s *ssaState) stmt(n *Node) {
case OAS: case OAS:
// TODO(khr): colas? // TODO(khr): colas?
val := s.expr(n.Right) val := s.expr(n.Right)
if n.Left.Op == OINDREG { if n.Left.Op == ONAME && !n.Left.Addrtaken && n.Left.Class&PHEAP == 0 && n.Left.Class != PEXTERN && n.Left.Class != PPARAMOUT {
// indirect off a register (TODO: always SP?) // ssa-able variable.
// used for storing arguments to callees
addr := s.f.Entry.NewValue(ssa.OpSPAddr, Ptrto(n.Right.Type), n.Left.Xoffset)
s.vars[".mem"] = s.curBlock.NewValue3(ssa.OpStore, ssa.TypeMem, nil, addr, val, s.mem())
} else if n.Left.Op != ONAME {
// some more complicated expression. Rewrite to a store. TODO
addr := s.expr(n.Left) // TODO: wrap in &
// TODO(khr): nil check
s.vars[".mem"] = s.curBlock.NewValue3(ssa.OpStore, n.Right.Type, nil, addr, val, s.mem())
} else if !n.Left.Addable {
// TODO
log.Fatalf("assignment to non-addable value")
} else if n.Left.Class&PHEAP != 0 {
// TODO
log.Fatalf("assignment to heap value")
} else if n.Left.Class == PEXTERN {
// assign to global variable
addr := s.f.Entry.NewValue(ssa.OpGlobal, Ptrto(n.Left.Type), n.Left.Sym)
s.vars[".mem"] = s.curBlock.NewValue3(ssa.OpStore, ssa.TypeMem, nil, addr, val, s.mem())
} else if n.Left.Class == PPARAMOUT {
// store to parameter slot
addr := s.f.Entry.NewValue(ssa.OpFPAddr, Ptrto(n.Right.Type), n.Left.Xoffset)
s.vars[".mem"] = s.curBlock.NewValue3(ssa.OpStore, ssa.TypeMem, nil, addr, val, s.mem())
} else {
// normal variable
s.vars[n.Left.Sym.Name] = val s.vars[n.Left.Sym.Name] = val
return
} }
// not ssa-able. Treat as a store.
addr := s.addr(n.Left)
s.vars[".mem"] = s.curBlock.NewValue3(ssa.OpStore, ssa.TypeMem, nil, addr, val, s.mem())
// TODO: try to make more variables registerizeable.
case OIF: case OIF:
cond := s.expr(n.Ntest) cond := s.expr(n.Ntest)
b := s.endBlock() b := s.endBlock()
...@@ -254,7 +249,7 @@ func (s *ssaState) stmt(n *Node) { ...@@ -254,7 +249,7 @@ func (s *ssaState) stmt(n *Node) {
} }
// expr converts the expression n to ssa, adds it to s and returns the ssa result. // expr converts the expression n to ssa, adds it to s and returns the ssa result.
func (s *ssaState) expr(n *Node) *ssa.Value { func (s *state) expr(n *Node) *ssa.Value {
if n == nil { if n == nil {
// TODO(khr): is this nil??? // TODO(khr): is this nil???
return s.f.Entry.NewValue(ssa.OpConst, n.Type, nil) return s.f.Entry.NewValue(ssa.OpConst, n.Type, nil)
...@@ -269,7 +264,6 @@ func (s *ssaState) expr(n *Node) *ssa.Value { ...@@ -269,7 +264,6 @@ func (s *ssaState) expr(n *Node) *ssa.Value {
} }
s.argOffsets[n.Sym.Name] = n.Xoffset s.argOffsets[n.Sym.Name] = n.Xoffset
return s.variable(n.Sym.Name, n.Type) return s.variable(n.Sym.Name, n.Type)
// binary ops
case OLITERAL: case OLITERAL:
switch n.Val.Ctype { switch n.Val.Ctype {
case CTINT: case CTINT:
...@@ -278,6 +272,8 @@ func (s *ssaState) expr(n *Node) *ssa.Value { ...@@ -278,6 +272,8 @@ func (s *ssaState) expr(n *Node) *ssa.Value {
log.Fatalf("unhandled OLITERAL %v", n.Val.Ctype) log.Fatalf("unhandled OLITERAL %v", n.Val.Ctype)
return nil return nil
} }
// binary ops
case OLT: case OLT:
a := s.expr(n.Left) a := s.expr(n.Left)
b := s.expr(n.Right) b := s.expr(n.Right)
...@@ -286,56 +282,36 @@ func (s *ssaState) expr(n *Node) *ssa.Value { ...@@ -286,56 +282,36 @@ func (s *ssaState) expr(n *Node) *ssa.Value {
a := s.expr(n.Left) a := s.expr(n.Left)
b := s.expr(n.Right) b := s.expr(n.Right)
return s.curBlock.NewValue2(ssa.OpAdd, a.Type, nil, a, b) return s.curBlock.NewValue2(ssa.OpAdd, a.Type, nil, a, b)
case OSUB: case OSUB:
// TODO:(khr) fold code for all binary ops together somehow // TODO:(khr) fold code for all binary ops together somehow
a := s.expr(n.Left) a := s.expr(n.Left)
b := s.expr(n.Right) b := s.expr(n.Right)
return s.curBlock.NewValue2(ssa.OpSub, a.Type, nil, a, b) return s.curBlock.NewValue2(ssa.OpSub, a.Type, nil, a, b)
case OADDR:
return s.addr(n.Left)
case OIND: case OIND:
p := s.expr(n.Left) p := s.expr(n.Left)
c := s.curBlock.NewValue1(ssa.OpIsNonNil, ssa.TypeBool, nil, p) s.nilCheck(p)
b := s.endBlock()
b.Kind = ssa.BlockIf
b.Control = c
bNext := s.f.NewBlock(ssa.BlockPlain)
addEdge(b, bNext)
addEdge(b, s.exit)
s.startBlock(bNext)
// TODO(khr): if ptr check fails, don't go directly to exit.
// Instead, go to a call to panicnil or something.
// TODO: implicit nil checks somehow?
return s.curBlock.NewValue2(ssa.OpLoad, n.Type, nil, p, s.mem()) return s.curBlock.NewValue2(ssa.OpLoad, n.Type, nil, p, s.mem())
case ODOTPTR: case ODOTPTR:
p := s.expr(n.Left) p := s.expr(n.Left)
// TODO: nilcheck s.nilCheck(p)
p = s.curBlock.NewValue2(ssa.OpAdd, p.Type, nil, p, s.f.ConstInt(s.config.UIntPtr, n.Xoffset)) p = s.curBlock.NewValue2(ssa.OpAdd, p.Type, nil, p, s.f.ConstInt(s.config.Uintptr, n.Xoffset))
return s.curBlock.NewValue2(ssa.OpLoad, n.Type, nil, p, s.mem()) return s.curBlock.NewValue2(ssa.OpLoad, n.Type, nil, p, s.mem())
case OINDEX: case OINDEX:
// TODO: slice vs array? Map index is already reduced to a function call if n.Left.Type.Bound >= 0 { // array
a := s.expr(n.Left) a := s.expr(n.Left)
i := s.expr(n.Right) i := s.expr(n.Right)
// convert index to full width s.boundsCheck(i, s.f.ConstInt(s.config.Uintptr, n.Left.Type.Bound))
// TODO: if index is 64-bit and we're compiling to 32-bit, check that high return s.curBlock.NewValue2(ssa.OpArrayIndex, n.Left.Type.Type, nil, a, i)
// 32 bits are zero (and use a low32 op instead of convnop here). } else { // slice
i = s.curBlock.NewValue1(ssa.OpConvNop, s.config.UIntPtr, nil, i) p := s.addr(n)
return s.curBlock.NewValue2(ssa.OpLoad, n.Left.Type.Type, nil, p, s.mem())
// bounds check }
len := s.curBlock.NewValue1(ssa.OpSliceLen, s.config.UIntPtr, nil, a)
cmp := s.curBlock.NewValue2(ssa.OpIsInBounds, ssa.TypeBool, nil, i, len)
b := s.endBlock()
b.Kind = ssa.BlockIf
b.Control = cmp
bNext := s.f.NewBlock(ssa.BlockPlain)
addEdge(b, bNext)
addEdge(b, s.exit)
s.startBlock(bNext)
// TODO: don't go directly to s.exit. Go to a stub that calls panicindex first.
return s.curBlock.NewValue3(ssa.OpSliceIndex, n.Left.Type.Type, nil, a, i, s.mem())
case OCALLFUNC: case OCALLFUNC:
// run all argument assignments // run all argument assignments
...@@ -359,7 +335,7 @@ func (s *ssaState) expr(n *Node) *ssa.Value { ...@@ -359,7 +335,7 @@ func (s *ssaState) expr(n *Node) *ssa.Value {
s.startBlock(bNext) s.startBlock(bNext)
var titer Iter var titer Iter
fp := Structfirst(&titer, Getoutarg(n.Left.Type)) fp := Structfirst(&titer, Getoutarg(n.Left.Type))
a := s.f.Entry.NewValue(ssa.OpSPAddr, Ptrto(fp.Type), fp.Width) a := s.f.Entry.NewValue1(ssa.OpOffPtr, Ptrto(fp.Type), fp.Width, s.sp)
return s.curBlock.NewValue2(ssa.OpLoad, fp.Type, nil, a, call) return s.curBlock.NewValue2(ssa.OpLoad, fp.Type, nil, a, call)
default: default:
log.Fatalf("unhandled expr %s", opnames[n.Op]) log.Fatalf("unhandled expr %s", opnames[n.Op])
...@@ -367,8 +343,81 @@ func (s *ssaState) expr(n *Node) *ssa.Value { ...@@ -367,8 +343,81 @@ func (s *ssaState) expr(n *Node) *ssa.Value {
} }
} }
// expr converts the address of the expression n to SSA, adds it to s and returns the SSA result.
func (s *state) addr(n *Node) *ssa.Value {
switch n.Op {
case ONAME:
if n.Class == PEXTERN {
// global variable
return s.f.Entry.NewValue(ssa.OpGlobal, Ptrto(n.Type), n.Sym)
}
if n.Class == PPARAMOUT {
// store to parameter slot
return s.f.Entry.NewValue1(ssa.OpOffPtr, Ptrto(n.Type), n.Xoffset, s.fp)
}
// TODO: address of locals
log.Fatalf("variable address of %v not implemented", n)
return nil
case OINDREG:
// indirect off a register (TODO: always SP?)
// used for storing/loading arguments/returns to/from callees
return s.f.Entry.NewValue1(ssa.OpOffPtr, Ptrto(n.Type), n.Xoffset, s.sp)
case OINDEX:
if n.Left.Type.Bound >= 0 { // array
a := s.addr(n.Left)
i := s.expr(n.Right)
len := s.f.ConstInt(s.config.Uintptr, n.Left.Type.Bound)
s.boundsCheck(i, len)
return s.curBlock.NewValue2(ssa.OpPtrIndex, Ptrto(n.Left.Type.Type), nil, a, i)
} else { // slice
a := s.expr(n.Left)
i := s.expr(n.Right)
len := s.curBlock.NewValue1(ssa.OpSliceLen, s.config.Uintptr, nil, a)
s.boundsCheck(i, len)
p := s.curBlock.NewValue1(ssa.OpSlicePtr, Ptrto(n.Left.Type.Type), nil, a)
return s.curBlock.NewValue2(ssa.OpPtrIndex, Ptrto(n.Left.Type.Type), nil, p, i)
}
default:
log.Fatalf("addr: bad op %v", n.Op)
return nil
}
}
// nilCheck generates nil pointer checking code.
// Starts a new block on return.
func (s *state) nilCheck(ptr *ssa.Value) {
c := s.curBlock.NewValue1(ssa.OpIsNonNil, ssa.TypeBool, nil, ptr)
b := s.endBlock()
b.Kind = ssa.BlockIf
b.Control = c
bNext := s.f.NewBlock(ssa.BlockPlain)
addEdge(b, bNext)
addEdge(b, s.exit)
s.startBlock(bNext)
// TODO(khr): Don't go directly to exit. Go to a stub that calls panicmem first.
// TODO: implicit nil checks somehow?
}
// boundsCheck generates bounds checking code. Checks if 0 <= idx < len, branches to exit if not.
// Starts a new block on return.
func (s *state) boundsCheck(idx, len *ssa.Value) {
// TODO: convert index to full width?
// TODO: if index is 64-bit and we're compiling to 32-bit, check that high 32 bits are zero.
// bounds check
cmp := s.curBlock.NewValue2(ssa.OpIsInBounds, ssa.TypeBool, nil, idx, len)
b := s.endBlock()
b.Kind = ssa.BlockIf
b.Control = cmp
bNext := s.f.NewBlock(ssa.BlockPlain)
addEdge(b, bNext)
addEdge(b, s.exit)
// TODO: don't go directly to s.exit. Go to a stub that calls panicindex first.
s.startBlock(bNext)
}
// variable returns the value of a variable at the current location. // variable returns the value of a variable at the current location.
func (s *ssaState) variable(name string, t ssa.Type) *ssa.Value { func (s *state) variable(name string, t ssa.Type) *ssa.Value {
if s.curBlock == nil { if s.curBlock == nil {
log.Fatalf("nil curblock!") log.Fatalf("nil curblock!")
} }
...@@ -381,11 +430,11 @@ func (s *ssaState) variable(name string, t ssa.Type) *ssa.Value { ...@@ -381,11 +430,11 @@ func (s *ssaState) variable(name string, t ssa.Type) *ssa.Value {
return v return v
} }
func (s *ssaState) mem() *ssa.Value { func (s *state) mem() *ssa.Value {
return s.variable(".mem", ssa.TypeMem) return s.variable(".mem", ssa.TypeMem)
} }
func (s *ssaState) linkForwardReferences() { func (s *state) linkForwardReferences() {
// Build ssa graph. Each variable on its first use in a basic block // Build ssa graph. Each variable on its first use in a basic block
// leaves a FwdRef in that block representing the incoming value // leaves a FwdRef in that block representing the incoming value
// of that variable. This function links that ref up with possible definitions, // of that variable. This function links that ref up with possible definitions,
...@@ -406,17 +455,16 @@ func (s *ssaState) linkForwardReferences() { ...@@ -406,17 +455,16 @@ func (s *ssaState) linkForwardReferences() {
} }
// lookupVarIncoming finds the variable's value at the start of block b. // lookupVarIncoming finds the variable's value at the start of block b.
func (s *ssaState) lookupVarIncoming(b *ssa.Block, t ssa.Type, name string) *ssa.Value { func (s *state) lookupVarIncoming(b *ssa.Block, t ssa.Type, name string) *ssa.Value {
// TODO(khr): have lookupVarIncoming overwrite the fwdRef or copy it // TODO(khr): have lookupVarIncoming overwrite the fwdRef or copy it
// will be used in, instead of having the result used in a copy value. // will be used in, instead of having the result used in a copy value.
if b == s.f.Entry { if b == s.f.Entry {
if name == ".mem" { if name == ".mem" {
return b.NewValue(ssa.OpArg, t, name) return s.startmem
} }
// variable is live at the entry block. Load it. // variable is live at the entry block. Load it.
a := s.f.Entry.NewValue(ssa.OpFPAddr, Ptrto(t.(*Type)), s.argOffsets[name]) addr := s.f.Entry.NewValue1(ssa.OpOffPtr, Ptrto(t.(*Type)), s.argOffsets[name], s.fp)
m := b.NewValue(ssa.OpArg, ssa.TypeMem, ".mem") // TODO: reuse mem starting value return b.NewValue2(ssa.OpLoad, t, nil, addr, s.startmem)
return b.NewValue2(ssa.OpLoad, t, nil, a, m)
} }
var vals []*ssa.Value var vals []*ssa.Value
for _, p := range b.Preds { for _, p := range b.Preds {
...@@ -435,7 +483,7 @@ func (s *ssaState) lookupVarIncoming(b *ssa.Block, t ssa.Type, name string) *ssa ...@@ -435,7 +483,7 @@ func (s *ssaState) lookupVarIncoming(b *ssa.Block, t ssa.Type, name string) *ssa
} }
// lookupVarOutgoing finds the variable's value at the end of block b. // lookupVarOutgoing finds the variable's value at the end of block b.
func (s *ssaState) lookupVarOutgoing(b *ssa.Block, t ssa.Type, name string) *ssa.Value { func (s *state) lookupVarOutgoing(b *ssa.Block, t ssa.Type, name string) *ssa.Value {
m := s.defvars[b.ID] m := s.defvars[b.ID]
if v, ok := m[name]; ok { if v, ok := m[name]; ok {
return v return v
...@@ -568,13 +616,23 @@ func genValue(v *ssa.Value, frameSize int64) { ...@@ -568,13 +616,23 @@ func genValue(v *ssa.Value, frameSize int64) {
p.To.Type = obj.TYPE_REG p.To.Type = obj.TYPE_REG
p.To.Reg = r p.To.Reg = r
case ssa.OpCMPQ: case ssa.OpCMPQ:
x := regnum(v.Args[0])
y := regnum(v.Args[1])
p := Prog(x86.ACMPQ) p := Prog(x86.ACMPQ)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
p.From.Reg = x p.From.Reg = regnum(v.Args[0])
p.To.Type = obj.TYPE_REG
p.To.Reg = regnum(v.Args[1])
case ssa.OpCMPCQ:
p := Prog(x86.ACMPQ)
p.From.Type = obj.TYPE_REG
p.From.Reg = regnum(v.Args[0])
p.To.Type = obj.TYPE_CONST
p.To.Offset = v.Aux.(int64)
case ssa.OpTESTB:
p := Prog(x86.ATESTB)
p.From.Type = obj.TYPE_REG
p.From.Reg = regnum(v.Args[0])
p.To.Type = obj.TYPE_REG p.To.Type = obj.TYPE_REG
p.To.Reg = y p.To.Reg = regnum(v.Args[1])
case ssa.OpMOVQconst: case ssa.OpMOVQconst:
x := regnum(v) x := regnum(v)
p := Prog(x86.AMOVQ) p := Prog(x86.AMOVQ)
...@@ -582,22 +640,57 @@ func genValue(v *ssa.Value, frameSize int64) { ...@@ -582,22 +640,57 @@ func genValue(v *ssa.Value, frameSize int64) {
p.From.Offset = v.Aux.(int64) p.From.Offset = v.Aux.(int64)
p.To.Type = obj.TYPE_REG p.To.Type = obj.TYPE_REG
p.To.Reg = x p.To.Reg = x
case ssa.OpMOVQloadFP: case ssa.OpMOVQload:
x := regnum(v)
p := Prog(x86.AMOVQ) p := Prog(x86.AMOVQ)
p.From.Type = obj.TYPE_MEM p.From.Type = obj.TYPE_MEM
p.From.Reg = x86.REG_SP if v.Block.Func.RegAlloc[v.Args[0].ID].Name() == "FP" {
p.From.Offset = v.Aux.(int64) + frameSize // TODO: do the fp/sp adjustment somewhere else?
p.From.Reg = x86.REG_SP
p.From.Offset = v.Aux.(int64) + frameSize
} else {
p.From.Reg = regnum(v.Args[0])
p.From.Offset = v.Aux.(int64)
}
p.To.Type = obj.TYPE_REG p.To.Type = obj.TYPE_REG
p.To.Reg = x p.To.Reg = regnum(v)
case ssa.OpMOVQstoreFP: case ssa.OpMOVBload:
x := regnum(v.Args[0]) p := Prog(x86.AMOVB)
p.From.Type = obj.TYPE_MEM
if v.Block.Func.RegAlloc[v.Args[0].ID].Name() == "FP" {
p.From.Reg = x86.REG_SP
p.From.Offset = v.Aux.(int64) + frameSize
} else {
p.From.Reg = regnum(v.Args[0])
p.From.Offset = v.Aux.(int64)
}
p.To.Type = obj.TYPE_REG
p.To.Reg = regnum(v)
case ssa.OpMOVQloadidx8:
p := Prog(x86.AMOVQ)
p.From.Type = obj.TYPE_MEM
if v.Block.Func.RegAlloc[v.Args[0].ID].Name() == "FP" {
p.From.Reg = x86.REG_SP
p.From.Offset = v.Aux.(int64) + frameSize
} else {
p.From.Reg = regnum(v.Args[0])
p.From.Offset = v.Aux.(int64)
}
p.From.Scale = 8
p.From.Index = regnum(v.Args[1])
p.To.Type = obj.TYPE_REG
p.To.Reg = regnum(v)
case ssa.OpMOVQstore:
p := Prog(x86.AMOVQ) p := Prog(x86.AMOVQ)
p.From.Type = obj.TYPE_REG p.From.Type = obj.TYPE_REG
p.From.Reg = x p.From.Reg = regnum(v.Args[1])
p.To.Type = obj.TYPE_MEM p.To.Type = obj.TYPE_MEM
p.To.Reg = x86.REG_SP if v.Block.Func.RegAlloc[v.Args[0].ID].Name() == "FP" {
p.To.Offset = v.Aux.(int64) + frameSize p.To.Reg = x86.REG_SP
p.To.Offset = v.Aux.(int64) + frameSize
} else {
p.To.Reg = regnum(v.Args[0])
p.To.Offset = v.Aux.(int64)
}
case ssa.OpCopy: case ssa.OpCopy:
x := regnum(v.Args[0]) x := regnum(v.Args[0])
y := regnum(v) y := regnum(v)
...@@ -638,8 +731,19 @@ func genValue(v *ssa.Value, frameSize int64) { ...@@ -638,8 +731,19 @@ func genValue(v *ssa.Value, frameSize int64) {
case ssa.OpArg: case ssa.OpArg:
// memory arg needs no code // memory arg needs no code
// TODO: only mem arg goes here. // TODO: only mem arg goes here.
case ssa.OpLEAQglobal:
g := v.Aux.(ssa.GlobalOffset)
p := Prog(x86.ALEAQ)
p.From.Type = obj.TYPE_MEM
p.From.Name = obj.NAME_EXTERN
p.From.Sym = Linksym(g.Global.(*Sym))
p.From.Offset = g.Offset
p.To.Type = obj.TYPE_REG
p.To.Reg = regnum(v)
case ssa.OpFP, ssa.OpSP:
// nothing to do
default: default:
log.Fatalf("value %v not implemented yet", v) log.Fatalf("value %s not implemented yet", v.LongString())
} }
} }
...@@ -653,6 +757,40 @@ func genBlock(b, next *ssa.Block, branches []branch) []branch { ...@@ -653,6 +757,40 @@ func genBlock(b, next *ssa.Block, branches []branch) []branch {
} }
case ssa.BlockExit: case ssa.BlockExit:
Prog(obj.ARET) Prog(obj.ARET)
case ssa.BlockEQ:
if b.Succs[0] == next {
p := Prog(x86.AJNE)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[1]})
} else if b.Succs[1] == next {
p := Prog(x86.AJEQ)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
} else {
p := Prog(x86.AJEQ)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
q := Prog(obj.AJMP)
q.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{q, b.Succs[1]})
}
case ssa.BlockNE:
if b.Succs[0] == next {
p := Prog(x86.AJEQ)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[1]})
} else if b.Succs[1] == next {
p := Prog(x86.AJNE)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
} else {
p := Prog(x86.AJNE)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
q := Prog(obj.AJMP)
q.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{q, b.Succs[1]})
}
case ssa.BlockLT: case ssa.BlockLT:
if b.Succs[0] == next { if b.Succs[0] == next {
p := Prog(x86.AJGE) p := Prog(x86.AJGE)
...@@ -670,8 +808,43 @@ func genBlock(b, next *ssa.Block, branches []branch) []branch { ...@@ -670,8 +808,43 @@ func genBlock(b, next *ssa.Block, branches []branch) []branch {
q.To.Type = obj.TYPE_BRANCH q.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{q, b.Succs[1]}) branches = append(branches, branch{q, b.Succs[1]})
} }
case ssa.BlockULT:
if b.Succs[0] == next {
p := Prog(x86.AJCC)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[1]})
} else if b.Succs[1] == next {
p := Prog(x86.AJCS)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
} else {
p := Prog(x86.AJCS)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
q := Prog(obj.AJMP)
q.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{q, b.Succs[1]})
}
case ssa.BlockUGT:
if b.Succs[0] == next {
p := Prog(x86.AJLS)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[1]})
} else if b.Succs[1] == next {
p := Prog(x86.AJHI)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
} else {
p := Prog(x86.AJHI)
p.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{p, b.Succs[0]})
q := Prog(obj.AJMP)
q.To.Type = obj.TYPE_BRANCH
branches = append(branches, branch{q, b.Succs[1]})
}
default: default:
log.Fatalf("branch at %v not implemented yet", b) log.Fatalf("branch %s not implemented yet", b.LongString())
} }
return branches return branches
} }
......
...@@ -110,23 +110,23 @@ const ( ...@@ -110,23 +110,23 @@ const (
AINTO AINTO
AIRETL AIRETL
AIRETW AIRETW
AJCC AJCC // >= unsigned
AJCS AJCS // < unsigned
AJCXZL AJCXZL
AJEQ AJEQ // == (zero)
AJGE AJGE // >= signed
AJGT AJGT // > signed
AJHI AJHI // > unsigned
AJLE AJLE // <= signed
AJLS AJLS // <= unsigned
AJLT AJLT // < signed
AJMI AJMI // sign bit set (negative)
AJNE AJNE // != (nonzero)
AJOC AJOC // overflow clear
AJOS AJOS // overflow set
AJPC AJPC // parity clear
AJPL AJPL // sign bit clear (positive)
AJPS AJPS // parity set
ALAHF ALAHF
ALARL ALARL
ALARW ALARW
......
...@@ -58,7 +58,7 @@ func checkFunc(f *Func) { ...@@ -58,7 +58,7 @@ func checkFunc(f *Func) {
if b.Control == nil { if b.Control == nil {
log.Panicf("exit block %s has no control value", b) log.Panicf("exit block %s has no control value", b)
} }
if b.Control.Type != TypeMem { if !b.Control.Type.IsMemory() {
log.Panicf("exit block %s has non-memory control value %s", b, b.Control.LongString()) log.Panicf("exit block %s has non-memory control value %s", b, b.Control.LongString())
} }
case BlockPlain: case BlockPlain:
...@@ -75,7 +75,7 @@ func checkFunc(f *Func) { ...@@ -75,7 +75,7 @@ func checkFunc(f *Func) {
if b.Control == nil { if b.Control == nil {
log.Panicf("if block %s has no control value", b) log.Panicf("if block %s has no control value", b)
} }
if b.Control.Type != TypeBool { if !b.Control.Type.IsBoolean() {
log.Panicf("if block %s has non-bool control value %s", b, b.Control.LongString()) log.Panicf("if block %s has non-bool control value %s", b, b.Control.LongString())
} }
case BlockCall: case BlockCall:
...@@ -85,7 +85,7 @@ func checkFunc(f *Func) { ...@@ -85,7 +85,7 @@ func checkFunc(f *Func) {
if b.Control == nil { if b.Control == nil {
log.Panicf("call block %s has no control value", b) log.Panicf("call block %s has no control value", b)
} }
if b.Control.Type != TypeMem { if !b.Control.Type.IsMemory() {
log.Panicf("call block %s has non-memory control value %s", b, b.Control.LongString()) log.Panicf("call block %s has non-memory control value %s", b, b.Control.LongString())
} }
if b.Succs[1].Kind != BlockExit { if b.Succs[1].Kind != BlockExit {
......
...@@ -9,7 +9,7 @@ import "log" ...@@ -9,7 +9,7 @@ import "log"
type Config struct { type Config struct {
arch string // "amd64", etc. arch string // "amd64", etc.
ptrSize int64 // 4 or 8 ptrSize int64 // 4 or 8
UIntPtr Type // pointer arithmetic type Uintptr Type // pointer arithmetic type
lower func(*Value) bool // lowering function lower func(*Value) bool // lowering function
// TODO: more stuff. Compiler flags of interest, ... // TODO: more stuff. Compiler flags of interest, ...
...@@ -30,9 +30,9 @@ func NewConfig(arch string) *Config { ...@@ -30,9 +30,9 @@ func NewConfig(arch string) *Config {
} }
// cache the intptr type in the config // cache the intptr type in the config
c.UIntPtr = TypeUInt32 c.Uintptr = TypeUInt32
if c.ptrSize == 8 { if c.ptrSize == 8 {
c.UIntPtr = TypeUInt64 c.Uintptr = TypeUInt64
} }
return c return c
......
...@@ -6,20 +6,20 @@ func genericRules(v *Value) bool { ...@@ -6,20 +6,20 @@ func genericRules(v *Value) bool {
switch v.Op { switch v.Op {
case OpAdd: case OpAdd:
// match: (Add <t> (Const [c]) (Const [d])) // match: (Add <t> (Const [c]) (Const [d]))
// cond: is64BitInt(t) && isSigned(t) // cond: is64BitInt(t)
// result: (Const [{c.(int64)+d.(int64)}]) // result: (Const [{c.(int64)+d.(int64)}])
{ {
t := v.Type t := v.Type
if v.Args[0].Op != OpConst { if v.Args[0].Op != OpConst {
goto endc86f5c160a87f6f5ec90b6551ec099d9 goto end8d047ed0ae9537b840adc79ea82c6e05
} }
c := v.Args[0].Aux c := v.Args[0].Aux
if v.Args[1].Op != OpConst { if v.Args[1].Op != OpConst {
goto endc86f5c160a87f6f5ec90b6551ec099d9 goto end8d047ed0ae9537b840adc79ea82c6e05
} }
d := v.Args[1].Aux d := v.Args[1].Aux
if !(is64BitInt(t) && isSigned(t)) { if !(is64BitInt(t)) {
goto endc86f5c160a87f6f5ec90b6551ec099d9 goto end8d047ed0ae9537b840adc79ea82c6e05
} }
v.Op = OpConst v.Op = OpConst
v.Aux = nil v.Aux = nil
...@@ -27,100 +27,141 @@ func genericRules(v *Value) bool { ...@@ -27,100 +27,141 @@ func genericRules(v *Value) bool {
v.Aux = c.(int64) + d.(int64) v.Aux = c.(int64) + d.(int64)
return true return true
} }
goto endc86f5c160a87f6f5ec90b6551ec099d9 goto end8d047ed0ae9537b840adc79ea82c6e05
endc86f5c160a87f6f5ec90b6551ec099d9: end8d047ed0ae9537b840adc79ea82c6e05:
; ;
// match: (Add <t> (Const [c]) (Const [d])) case OpArrayIndex:
// cond: is64BitInt(t) && !isSigned(t) // match: (ArrayIndex (Load ptr mem) idx)
// result: (Const [{c.(uint64)+d.(uint64)}]) // cond:
// result: (Load (PtrIndex <ptr.Type.Elem().Elem().PtrTo()> ptr idx) mem)
{
if v.Args[0].Op != OpLoad {
goto end3809f4c52270a76313e4ea26e6f0b753
}
ptr := v.Args[0].Args[0]
mem := v.Args[0].Args[1]
idx := v.Args[1]
v.Op = OpLoad
v.Aux = nil
v.resetArgs()
v0 := v.Block.NewValue(OpPtrIndex, TypeInvalid, nil)
v0.Type = ptr.Type.Elem().Elem().PtrTo()
v0.AddArg(ptr)
v0.AddArg(idx)
v.AddArg(v0)
v.AddArg(mem)
return true
}
goto end3809f4c52270a76313e4ea26e6f0b753
end3809f4c52270a76313e4ea26e6f0b753:
;
case OpIsInBounds:
// match: (IsInBounds (Const [c]) (Const [d]))
// cond:
// result: (Const [inBounds(c.(int64),d.(int64))])
{
if v.Args[0].Op != OpConst {
goto enddbd1a394d9b71ee64335361b8384865c
}
c := v.Args[0].Aux
if v.Args[1].Op != OpConst {
goto enddbd1a394d9b71ee64335361b8384865c
}
d := v.Args[1].Aux
v.Op = OpConst
v.Aux = nil
v.resetArgs()
v.Aux = inBounds(c.(int64), d.(int64))
return true
}
goto enddbd1a394d9b71ee64335361b8384865c
enddbd1a394d9b71ee64335361b8384865c:
;
case OpMul:
// match: (Mul <t> (Const [c]) (Const [d]))
// cond: is64BitInt(t)
// result: (Const [{c.(int64)*d.(int64)}])
{ {
t := v.Type t := v.Type
if v.Args[0].Op != OpConst { if v.Args[0].Op != OpConst {
goto end8941c2a515c1bd38530b7fd96862bac4 goto end776610f88cf04f438242d76ed2b14f1c
} }
c := v.Args[0].Aux c := v.Args[0].Aux
if v.Args[1].Op != OpConst { if v.Args[1].Op != OpConst {
goto end8941c2a515c1bd38530b7fd96862bac4 goto end776610f88cf04f438242d76ed2b14f1c
} }
d := v.Args[1].Aux d := v.Args[1].Aux
if !(is64BitInt(t) && !isSigned(t)) { if !(is64BitInt(t)) {
goto end8941c2a515c1bd38530b7fd96862bac4 goto end776610f88cf04f438242d76ed2b14f1c
} }
v.Op = OpConst v.Op = OpConst
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = c.(uint64) + d.(uint64) v.Aux = c.(int64) * d.(int64)
return true return true
} }
goto end8941c2a515c1bd38530b7fd96862bac4 goto end776610f88cf04f438242d76ed2b14f1c
end8941c2a515c1bd38530b7fd96862bac4: end776610f88cf04f438242d76ed2b14f1c:
; ;
case OpSliceCap: case OpPtrIndex:
// match: (SliceCap (Load ptr mem)) // match: (PtrIndex <t> ptr idx)
// cond: // cond:
// result: (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.UIntPtr> [int64(v.Block.Func.Config.ptrSize*2)])) mem) // result: (Add ptr (Mul <v.Block.Func.Config.Uintptr> idx (Const <v.Block.Func.Config.Uintptr> [t.Elem().Size()])))
{ {
if v.Args[0].Op != OpLoad { t := v.Type
goto ende03f9b79848867df439b56889bb4e55d ptr := v.Args[0]
} idx := v.Args[1]
ptr := v.Args[0].Args[0] v.Op = OpAdd
mem := v.Args[0].Args[1]
v.Op = OpLoad
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v0 := v.Block.NewValue(OpAdd, TypeInvalid, nil) v.AddArg(ptr)
v0.Type = ptr.Type v0 := v.Block.NewValue(OpMul, TypeInvalid, nil)
v0.AddArg(ptr) v0.Type = v.Block.Func.Config.Uintptr
v0.AddArg(idx)
v1 := v.Block.NewValue(OpConst, TypeInvalid, nil) v1 := v.Block.NewValue(OpConst, TypeInvalid, nil)
v1.Type = v.Block.Func.Config.UIntPtr v1.Type = v.Block.Func.Config.Uintptr
v1.Aux = int64(v.Block.Func.Config.ptrSize * 2) v1.Aux = t.Elem().Size()
v0.AddArg(v1) v0.AddArg(v1)
v.AddArg(v0) v.AddArg(v0)
v.AddArg(mem)
return true return true
} }
goto ende03f9b79848867df439b56889bb4e55d goto end383c68c41e72d22ef00c4b7b0fddcbb8
ende03f9b79848867df439b56889bb4e55d: end383c68c41e72d22ef00c4b7b0fddcbb8:
; ;
case OpSliceIndex: case OpSliceCap:
// match: (SliceIndex s i mem) // match: (SliceCap (Load ptr mem))
// cond: // cond:
// result: (Load (Add <s.Type.Elem().PtrTo()> (SlicePtr <s.Type.Elem().PtrTo()> s) (Mul <v.Block.Func.Config.UIntPtr> i (Const <v.Block.Func.Config.UIntPtr> [s.Type.Elem().Size()]))) mem) // result: (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.Uintptr> [int64(v.Block.Func.Config.ptrSize*2)])) mem)
{ {
s := v.Args[0] if v.Args[0].Op != OpLoad {
i := v.Args[1] goto endbf1d4db93c4664ed43be3f73afb4dfa3
mem := v.Args[2] }
ptr := v.Args[0].Args[0]
mem := v.Args[0].Args[1]
v.Op = OpLoad v.Op = OpLoad
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v0 := v.Block.NewValue(OpAdd, TypeInvalid, nil) v0 := v.Block.NewValue(OpAdd, TypeInvalid, nil)
v0.Type = s.Type.Elem().PtrTo() v0.Type = ptr.Type
v1 := v.Block.NewValue(OpSlicePtr, TypeInvalid, nil) v0.AddArg(ptr)
v1.Type = s.Type.Elem().PtrTo() v1 := v.Block.NewValue(OpConst, TypeInvalid, nil)
v1.AddArg(s) v1.Type = v.Block.Func.Config.Uintptr
v1.Aux = int64(v.Block.Func.Config.ptrSize * 2)
v0.AddArg(v1) v0.AddArg(v1)
v2 := v.Block.NewValue(OpMul, TypeInvalid, nil)
v2.Type = v.Block.Func.Config.UIntPtr
v2.AddArg(i)
v3 := v.Block.NewValue(OpConst, TypeInvalid, nil)
v3.Type = v.Block.Func.Config.UIntPtr
v3.Aux = s.Type.Elem().Size()
v2.AddArg(v3)
v0.AddArg(v2)
v.AddArg(v0) v.AddArg(v0)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end733704831a61760840348f790b3ab045 goto endbf1d4db93c4664ed43be3f73afb4dfa3
end733704831a61760840348f790b3ab045: endbf1d4db93c4664ed43be3f73afb4dfa3:
; ;
case OpSliceLen: case OpSliceLen:
// match: (SliceLen (Load ptr mem)) // match: (SliceLen (Load ptr mem))
// cond: // cond:
// result: (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.UIntPtr> [int64(v.Block.Func.Config.ptrSize)])) mem) // result: (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.Uintptr> [int64(v.Block.Func.Config.ptrSize)])) mem)
{ {
if v.Args[0].Op != OpLoad { if v.Args[0].Op != OpLoad {
goto ende94950a57eca1871c93afdeaadb90223 goto end9190b1ecbda4c5dd6d3e05d2495fb297
} }
ptr := v.Args[0].Args[0] ptr := v.Args[0].Args[0]
mem := v.Args[0].Args[1] mem := v.Args[0].Args[1]
...@@ -131,15 +172,15 @@ func genericRules(v *Value) bool { ...@@ -131,15 +172,15 @@ func genericRules(v *Value) bool {
v0.Type = ptr.Type v0.Type = ptr.Type
v0.AddArg(ptr) v0.AddArg(ptr)
v1 := v.Block.NewValue(OpConst, TypeInvalid, nil) v1 := v.Block.NewValue(OpConst, TypeInvalid, nil)
v1.Type = v.Block.Func.Config.UIntPtr v1.Type = v.Block.Func.Config.Uintptr
v1.Aux = int64(v.Block.Func.Config.ptrSize) v1.Aux = int64(v.Block.Func.Config.ptrSize)
v0.AddArg(v1) v0.AddArg(v1)
v.AddArg(v0) v.AddArg(v0)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto ende94950a57eca1871c93afdeaadb90223 goto end9190b1ecbda4c5dd6d3e05d2495fb297
ende94950a57eca1871c93afdeaadb90223: end9190b1ecbda4c5dd6d3e05d2495fb297:
; ;
case OpSlicePtr: case OpSlicePtr:
// match: (SlicePtr (Load ptr mem)) // match: (SlicePtr (Load ptr mem))
...@@ -160,6 +201,36 @@ func genericRules(v *Value) bool { ...@@ -160,6 +201,36 @@ func genericRules(v *Value) bool {
} }
goto end459613b83f95b65729d45c2ed663a153 goto end459613b83f95b65729d45c2ed663a153
end459613b83f95b65729d45c2ed663a153: end459613b83f95b65729d45c2ed663a153:
;
case OpStore:
// match: (Store dst (Load <t> src mem) mem)
// cond: t.Size() > 8
// result: (Move [t.Size()] dst src mem)
{
dst := v.Args[0]
if v.Args[1].Op != OpLoad {
goto end324ffb6d2771808da4267f62c854e9c8
}
t := v.Args[1].Type
src := v.Args[1].Args[0]
mem := v.Args[1].Args[1]
if v.Args[2] != v.Args[1].Args[1] {
goto end324ffb6d2771808da4267f62c854e9c8
}
if !(t.Size() > 8) {
goto end324ffb6d2771808da4267f62c854e9c8
}
v.Op = OpMove
v.Aux = nil
v.resetArgs()
v.Aux = t.Size()
v.AddArg(dst)
v.AddArg(src)
v.AddArg(mem)
return true
}
goto end324ffb6d2771808da4267f62c854e9c8
end324ffb6d2771808da4267f62c854e9c8:
} }
return false return false
} }
...@@ -16,41 +16,88 @@ func lower(f *Func) { ...@@ -16,41 +16,88 @@ func lower(f *Func) {
// additional pass for 386/amd64, link condition codes directly to blocks // additional pass for 386/amd64, link condition codes directly to blocks
// TODO: do generically somehow? Special "block" rewrite rules? // TODO: do generically somehow? Special "block" rewrite rules?
for _, b := range f.Blocks { for _, b := range f.Blocks {
switch b.Kind { for {
case BlockIf: switch b.Kind {
switch b.Control.Op { case BlockIf:
case OpSETL: switch b.Control.Op {
b.Kind = BlockLT case OpSETL:
b.Control = b.Control.Args[0] b.Kind = BlockLT
case OpSETNE: b.Control = b.Control.Args[0]
b.Kind = BlockNE continue
b.Control = b.Control.Args[0] case OpSETNE:
case OpSETB: b.Kind = BlockNE
b.Kind = BlockULT b.Control = b.Control.Args[0]
b.Control = b.Control.Args[0] continue
// TODO: others case OpSETB:
b.Kind = BlockULT
b.Control = b.Control.Args[0]
continue
case OpMOVBload:
b.Kind = BlockNE
b.Control = b.NewValue2(OpTESTB, TypeFlags, nil, b.Control, b.Control)
continue
// TODO: others
}
case BlockLT:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockGT
b.Control = b.Control.Args[0]
continue
}
case BlockGT:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockLT
b.Control = b.Control.Args[0]
continue
}
case BlockLE:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockGE
b.Control = b.Control.Args[0]
continue
}
case BlockGE:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockLE
b.Control = b.Control.Args[0]
continue
}
case BlockULT:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockUGT
b.Control = b.Control.Args[0]
continue
}
case BlockUGT:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockULT
b.Control = b.Control.Args[0]
continue
}
case BlockULE:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockUGE
b.Control = b.Control.Args[0]
continue
}
case BlockUGE:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockULE
b.Control = b.Control.Args[0]
continue
}
case BlockEQ:
if b.Control.Op == OpInvertFlags {
b.Control = b.Control.Args[0]
continue
}
case BlockNE:
if b.Control.Op == OpInvertFlags {
b.Control = b.Control.Args[0]
continue
}
} }
case BlockLT: break
if b.Control.Op == OpInvertFlags {
b.Kind = BlockGE
b.Control = b.Control.Args[0]
}
case BlockULT:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockUGE
b.Control = b.Control.Args[0]
}
case BlockEQ:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockNE
b.Control = b.Control.Args[0]
}
case BlockNE:
if b.Control.Op == OpInvertFlags {
b.Kind = BlockEQ
b.Control = b.Control.Args[0]
}
// TODO: others
} }
} }
} }
...@@ -7,11 +7,11 @@ func lowerAmd64(v *Value) bool { ...@@ -7,11 +7,11 @@ func lowerAmd64(v *Value) bool {
case OpADDCQ: case OpADDCQ:
// match: (ADDCQ [c] (LEAQ8 [d] x y)) // match: (ADDCQ [c] (LEAQ8 [d] x y))
// cond: // cond:
// result: (LEAQ8 [c.(int64)+d.(int64)] x y) // result: (LEAQ8 [addOff(c, d)] x y)
{ {
c := v.Aux c := v.Aux
if v.Args[0].Op != OpLEAQ8 { if v.Args[0].Op != OpLEAQ8 {
goto end16348939e556e99e8447227ecb986f01 goto end3bc1457811adc0cb81ad6b88a7461c60
} }
d := v.Args[0].Aux d := v.Args[0].Aux
x := v.Args[0].Args[0] x := v.Args[0].Args[0]
...@@ -19,58 +19,40 @@ func lowerAmd64(v *Value) bool { ...@@ -19,58 +19,40 @@ func lowerAmd64(v *Value) bool {
v.Op = OpLEAQ8 v.Op = OpLEAQ8
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = c.(int64) + d.(int64) v.Aux = addOff(c, d)
v.AddArg(x) v.AddArg(x)
v.AddArg(y) v.AddArg(y)
return true return true
} }
goto end16348939e556e99e8447227ecb986f01 goto end3bc1457811adc0cb81ad6b88a7461c60
end16348939e556e99e8447227ecb986f01: end3bc1457811adc0cb81ad6b88a7461c60:
; ;
// match: (ADDCQ [off1] (FPAddr [off2])) // match: (ADDCQ [off] x)
// cond: // cond: off.(int64) == 0
// result: (FPAddr [off1.(int64)+off2.(int64)]) // result: (Copy x)
{
off1 := v.Aux
if v.Args[0].Op != OpFPAddr {
goto end28e093ab0618066e6b2609db7aaf309b
}
off2 := v.Args[0].Aux
v.Op = OpFPAddr
v.Aux = nil
v.resetArgs()
v.Aux = off1.(int64) + off2.(int64)
return true
}
goto end28e093ab0618066e6b2609db7aaf309b
end28e093ab0618066e6b2609db7aaf309b:
;
// match: (ADDCQ [off1] (SPAddr [off2]))
// cond:
// result: (SPAddr [off1.(int64)+off2.(int64)])
{ {
off1 := v.Aux off := v.Aux
if v.Args[0].Op != OpSPAddr { x := v.Args[0]
goto endd0c27c62d150b88168075c5ba113d1fa if !(off.(int64) == 0) {
goto end6710a6679c47b70577ecea7ad00dae87
} }
off2 := v.Args[0].Aux v.Op = OpCopy
v.Op = OpSPAddr
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = off1.(int64) + off2.(int64) v.AddArg(x)
return true return true
} }
goto endd0c27c62d150b88168075c5ba113d1fa goto end6710a6679c47b70577ecea7ad00dae87
endd0c27c62d150b88168075c5ba113d1fa: end6710a6679c47b70577ecea7ad00dae87:
; ;
case OpADDQ: case OpADDQ:
// match: (ADDQ x (Const [c])) // match: (ADDQ x (MOVQconst [c]))
// cond: // cond:
// result: (ADDCQ [c] x) // result: (ADDCQ [c] x)
{ {
x := v.Args[0] x := v.Args[0]
if v.Args[1].Op != OpConst { if v.Args[1].Op != OpMOVQconst {
goto endef6908cfdf56e102cc327a3ddc14393d goto end39b79e84f20a6d44b5c4136aae220ac2
} }
c := v.Args[1].Aux c := v.Args[1].Aux
v.Op = OpADDCQ v.Op = OpADDCQ
...@@ -80,15 +62,15 @@ func lowerAmd64(v *Value) bool { ...@@ -80,15 +62,15 @@ func lowerAmd64(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
goto endef6908cfdf56e102cc327a3ddc14393d goto end39b79e84f20a6d44b5c4136aae220ac2
endef6908cfdf56e102cc327a3ddc14393d: end39b79e84f20a6d44b5c4136aae220ac2:
; ;
// match: (ADDQ (Const [c]) x) // match: (ADDQ (MOVQconst [c]) x)
// cond: // cond:
// result: (ADDCQ [c] x) // result: (ADDCQ [c] x)
{ {
if v.Args[0].Op != OpConst { if v.Args[0].Op != OpMOVQconst {
goto endb54a32cf3147f424f08b46db62c69b23 goto endc05ff5a2a132241b69d00c852001d820
} }
c := v.Args[0].Aux c := v.Args[0].Aux
x := v.Args[1] x := v.Args[1]
...@@ -99,8 +81,8 @@ func lowerAmd64(v *Value) bool { ...@@ -99,8 +81,8 @@ func lowerAmd64(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
goto endb54a32cf3147f424f08b46db62c69b23 goto endc05ff5a2a132241b69d00c852001d820
endb54a32cf3147f424f08b46db62c69b23: endc05ff5a2a132241b69d00c852001d820:
; ;
// match: (ADDQ x (SHLCQ [shift] y)) // match: (ADDQ x (SHLCQ [shift] y))
// cond: shift.(int64) == 3 // cond: shift.(int64) == 3
...@@ -168,13 +150,13 @@ func lowerAmd64(v *Value) bool { ...@@ -168,13 +150,13 @@ func lowerAmd64(v *Value) bool {
end35a02a1587264e40cf1055856ff8445a: end35a02a1587264e40cf1055856ff8445a:
; ;
case OpCMPQ: case OpCMPQ:
// match: (CMPQ x (Const [c])) // match: (CMPQ x (MOVQconst [c]))
// cond: // cond:
// result: (CMPCQ x [c]) // result: (CMPCQ x [c])
{ {
x := v.Args[0] x := v.Args[0]
if v.Args[1].Op != OpConst { if v.Args[1].Op != OpMOVQconst {
goto end1770a40e4253d9f669559a360514613e goto endf180bae15b3d24c0213520d7f7aa98b4
} }
c := v.Args[1].Aux c := v.Args[1].Aux
v.Op = OpCMPCQ v.Op = OpCMPCQ
...@@ -184,15 +166,15 @@ func lowerAmd64(v *Value) bool { ...@@ -184,15 +166,15 @@ func lowerAmd64(v *Value) bool {
v.Aux = c v.Aux = c
return true return true
} }
goto end1770a40e4253d9f669559a360514613e goto endf180bae15b3d24c0213520d7f7aa98b4
end1770a40e4253d9f669559a360514613e: endf180bae15b3d24c0213520d7f7aa98b4:
; ;
// match: (CMPQ (Const [c]) x) // match: (CMPQ (MOVQconst [c]) x)
// cond: // cond:
// result: (InvertFlags (CMPCQ <TypeFlags> x [c])) // result: (InvertFlags (CMPCQ <TypeFlags> x [c]))
{ {
if v.Args[0].Op != OpConst { if v.Args[0].Op != OpMOVQconst {
goto enda4e64c7eaeda16c1c0db9dac409cd126 goto end8fc58bffa73b3df80b3de72c91844884
} }
c := v.Args[0].Aux c := v.Args[0].Aux
x := v.Args[1] x := v.Args[1]
...@@ -206,8 +188,42 @@ func lowerAmd64(v *Value) bool { ...@@ -206,8 +188,42 @@ func lowerAmd64(v *Value) bool {
v.AddArg(v0) v.AddArg(v0)
return true return true
} }
goto enda4e64c7eaeda16c1c0db9dac409cd126 goto end8fc58bffa73b3df80b3de72c91844884
enda4e64c7eaeda16c1c0db9dac409cd126: end8fc58bffa73b3df80b3de72c91844884:
;
case OpConst:
// match: (Const <t> [val])
// cond: is64BitInt(t)
// result: (MOVQconst [val])
{
t := v.Type
val := v.Aux
if !(is64BitInt(t)) {
goto end7f5c5b34093fbc6860524cb803ee51bf
}
v.Op = OpMOVQconst
v.Aux = nil
v.resetArgs()
v.Aux = val
return true
}
goto end7f5c5b34093fbc6860524cb803ee51bf
end7f5c5b34093fbc6860524cb803ee51bf:
;
case OpGlobal:
// match: (Global [sym])
// cond:
// result: (LEAQglobal [GlobalOffset{sym,0}])
{
sym := v.Aux
v.Op = OpLEAQglobal
v.Aux = nil
v.resetArgs()
v.Aux = GlobalOffset{sym, 0}
return true
}
goto end3a3c76fac0e2e53c0e1c60b9524e6f1c
end3a3c76fac0e2e53c0e1c60b9524e6f1c:
; ;
case OpIsInBounds: case OpIsInBounds:
// match: (IsInBounds idx len) // match: (IsInBounds idx len)
...@@ -273,16 +289,16 @@ func lowerAmd64(v *Value) bool { ...@@ -273,16 +289,16 @@ func lowerAmd64(v *Value) bool {
; ;
case OpLoad: case OpLoad:
// match: (Load <t> ptr mem) // match: (Load <t> ptr mem)
// cond: (is64BitInt(t) || isPtr(t)) // cond: t.IsBoolean()
// result: (MOVQload [int64(0)] ptr mem) // result: (MOVBload [int64(0)] ptr mem)
{ {
t := v.Type t := v.Type
ptr := v.Args[0] ptr := v.Args[0]
mem := v.Args[1] mem := v.Args[1]
if !(is64BitInt(t) || isPtr(t)) { if !(t.IsBoolean()) {
goto end581ce5a20901df1b8143448ba031685b goto end73f21632e56c3614902d3c29c82dc4ea
} }
v.Op = OpMOVQload v.Op = OpMOVBload
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = int64(0) v.Aux = int64(0)
...@@ -290,77 +306,38 @@ func lowerAmd64(v *Value) bool { ...@@ -290,77 +306,38 @@ func lowerAmd64(v *Value) bool {
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end581ce5a20901df1b8143448ba031685b goto end73f21632e56c3614902d3c29c82dc4ea
end581ce5a20901df1b8143448ba031685b: end73f21632e56c3614902d3c29c82dc4ea:
;
case OpMOVQload:
// match: (MOVQload [off1] (FPAddr [off2]) mem)
// cond:
// result: (MOVQloadFP [off1.(int64)+off2.(int64)] mem)
{
off1 := v.Aux
if v.Args[0].Op != OpFPAddr {
goto endce972b1aa84b56447978c43def87fa57
}
off2 := v.Args[0].Aux
mem := v.Args[1]
v.Op = OpMOVQloadFP
v.Aux = nil
v.resetArgs()
v.Aux = off1.(int64) + off2.(int64)
v.AddArg(mem)
return true
}
goto endce972b1aa84b56447978c43def87fa57
endce972b1aa84b56447978c43def87fa57:
; ;
// match: (MOVQload [off1] (SPAddr [off2]) mem) // match: (Load <t> ptr mem)
// cond: // cond: (is64BitInt(t) || isPtr(t))
// result: (MOVQloadSP [off1.(int64)+off2.(int64)] mem) // result: (MOVQload [int64(0)] ptr mem)
{ {
off1 := v.Aux t := v.Type
if v.Args[0].Op != OpSPAddr { ptr := v.Args[0]
goto end3d8628a6536350a123be81240b8a1376
}
off2 := v.Args[0].Aux
mem := v.Args[1] mem := v.Args[1]
v.Op = OpMOVQloadSP if !(is64BitInt(t) || isPtr(t)) {
v.Aux = nil goto end581ce5a20901df1b8143448ba031685b
v.resetArgs()
v.Aux = off1.(int64) + off2.(int64)
v.AddArg(mem)
return true
}
goto end3d8628a6536350a123be81240b8a1376
end3d8628a6536350a123be81240b8a1376:
;
// match: (MOVQload [off] (Global [sym]) mem)
// cond:
// result: (MOVQloadglobal [GlobalOffset{sym,off.(int64)}] mem)
{
off := v.Aux
if v.Args[0].Op != OpGlobal {
goto end20693899317f3f8d1b47fefa64087654
} }
sym := v.Args[0].Aux v.Op = OpMOVQload
mem := v.Args[1]
v.Op = OpMOVQloadglobal
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = GlobalOffset{sym, off.(int64)} v.Aux = int64(0)
v.AddArg(ptr)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end20693899317f3f8d1b47fefa64087654 goto end581ce5a20901df1b8143448ba031685b
end20693899317f3f8d1b47fefa64087654: end581ce5a20901df1b8143448ba031685b:
; ;
case OpMOVQload:
// match: (MOVQload [off1] (ADDCQ [off2] ptr) mem) // match: (MOVQload [off1] (ADDCQ [off2] ptr) mem)
// cond: // cond:
// result: (MOVQload [off1.(int64)+off2.(int64)] ptr mem) // result: (MOVQload [addOff(off1, off2)] ptr mem)
{ {
off1 := v.Aux off1 := v.Aux
if v.Args[0].Op != OpADDCQ { if v.Args[0].Op != OpADDCQ {
goto enda68a39292ba2a05b3436191cb0bb0516 goto end218ceec16b8299d573d3c9ccaa69b086
} }
off2 := v.Args[0].Aux off2 := v.Args[0].Aux
ptr := v.Args[0].Args[0] ptr := v.Args[0].Args[0]
...@@ -368,21 +345,21 @@ func lowerAmd64(v *Value) bool { ...@@ -368,21 +345,21 @@ func lowerAmd64(v *Value) bool {
v.Op = OpMOVQload v.Op = OpMOVQload
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = off1.(int64) + off2.(int64) v.Aux = addOff(off1, off2)
v.AddArg(ptr) v.AddArg(ptr)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto enda68a39292ba2a05b3436191cb0bb0516 goto end218ceec16b8299d573d3c9ccaa69b086
enda68a39292ba2a05b3436191cb0bb0516: end218ceec16b8299d573d3c9ccaa69b086:
; ;
// match: (MOVQload [off1] (LEAQ8 [off2] ptr idx) mem) // match: (MOVQload [off1] (LEAQ8 [off2] ptr idx) mem)
// cond: // cond:
// result: (MOVQloadidx8 [off1.(int64)+off2.(int64)] ptr idx mem) // result: (MOVQloadidx8 [addOff(off1, off2)] ptr idx mem)
{ {
off1 := v.Aux off1 := v.Aux
if v.Args[0].Op != OpLEAQ8 { if v.Args[0].Op != OpLEAQ8 {
goto endba0e5cee85021614041016b1a2709ab8 goto end02f5ad148292c46463e7c20d3b821735
} }
off2 := v.Args[0].Aux off2 := v.Args[0].Aux
ptr := v.Args[0].Args[0] ptr := v.Args[0].Args[0]
...@@ -391,131 +368,117 @@ func lowerAmd64(v *Value) bool { ...@@ -391,131 +368,117 @@ func lowerAmd64(v *Value) bool {
v.Op = OpMOVQloadidx8 v.Op = OpMOVQloadidx8
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = off1.(int64) + off2.(int64) v.Aux = addOff(off1, off2)
v.AddArg(ptr) v.AddArg(ptr)
v.AddArg(idx) v.AddArg(idx)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto endba0e5cee85021614041016b1a2709ab8 goto end02f5ad148292c46463e7c20d3b821735
endba0e5cee85021614041016b1a2709ab8: end02f5ad148292c46463e7c20d3b821735:
; ;
case OpMOVQstore: case OpMOVQloadidx8:
// match: (MOVQstore [off1] (FPAddr [off2]) val mem) // match: (MOVQloadidx8 [off1] (ADDCQ [off2] ptr) idx mem)
// cond: // cond:
// result: (MOVQstoreFP [off1.(int64)+off2.(int64)] val mem) // result: (MOVQloadidx8 [addOff(off1, off2)] ptr idx mem)
{ {
off1 := v.Aux off1 := v.Aux
if v.Args[0].Op != OpFPAddr { if v.Args[0].Op != OpADDCQ {
goto end0a2a81a20558dfc93790aecb1e9cc81a goto ende47e8d742e2615f39fb6509a5749e414
} }
off2 := v.Args[0].Aux off2 := v.Args[0].Aux
val := v.Args[1] ptr := v.Args[0].Args[0]
idx := v.Args[1]
mem := v.Args[2] mem := v.Args[2]
v.Op = OpMOVQstoreFP v.Op = OpMOVQloadidx8
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = off1.(int64) + off2.(int64) v.Aux = addOff(off1, off2)
v.AddArg(val) v.AddArg(ptr)
v.AddArg(idx)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end0a2a81a20558dfc93790aecb1e9cc81a goto ende47e8d742e2615f39fb6509a5749e414
end0a2a81a20558dfc93790aecb1e9cc81a: ende47e8d742e2615f39fb6509a5749e414:
; ;
// match: (MOVQstore [off1] (SPAddr [off2]) val mem) case OpMOVQstore:
// match: (MOVQstore [off1] (ADDCQ [off2] ptr) val mem)
// cond: // cond:
// result: (MOVQstoreSP [off1.(int64)+off2.(int64)] val mem) // result: (MOVQstore [addOff(off1, off2)] ptr val mem)
{ {
off1 := v.Aux off1 := v.Aux
if v.Args[0].Op != OpSPAddr { if v.Args[0].Op != OpADDCQ {
goto end1cb5b7e766f018270fa434c6f46f607f goto enddfd4c7a20fd3b84eb9dcf84b98c661fc
} }
off2 := v.Args[0].Aux off2 := v.Args[0].Aux
ptr := v.Args[0].Args[0]
val := v.Args[1] val := v.Args[1]
mem := v.Args[2] mem := v.Args[2]
v.Op = OpMOVQstoreSP v.Op = OpMOVQstore
v.Aux = nil
v.resetArgs()
v.Aux = off1.(int64) + off2.(int64)
v.AddArg(val)
v.AddArg(mem)
return true
}
goto end1cb5b7e766f018270fa434c6f46f607f
end1cb5b7e766f018270fa434c6f46f607f:
;
// match: (MOVQstore [off] (Global [sym]) val mem)
// cond:
// result: (MOVQstoreglobal [GlobalOffset{sym,off.(int64)}] val mem)
{
off := v.Aux
if v.Args[0].Op != OpGlobal {
goto end657d07e37c720a8fbb108a31bb48090d
}
sym := v.Args[0].Aux
val := v.Args[1]
mem := v.Args[2]
v.Op = OpMOVQstoreglobal
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = GlobalOffset{sym, off.(int64)} v.Aux = addOff(off1, off2)
v.AddArg(ptr)
v.AddArg(val) v.AddArg(val)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end657d07e37c720a8fbb108a31bb48090d goto enddfd4c7a20fd3b84eb9dcf84b98c661fc
end657d07e37c720a8fbb108a31bb48090d: enddfd4c7a20fd3b84eb9dcf84b98c661fc:
; ;
// match: (MOVQstore [off1] (ADDCQ [off2] ptr) val mem) // match: (MOVQstore [off1] (LEAQ8 [off2] ptr idx) val mem)
// cond: // cond:
// result: (MOVQstore [off1.(int64)+off2.(int64)] ptr val mem) // result: (MOVQstoreidx8 [addOff(off1, off2)] ptr idx val mem)
{ {
off1 := v.Aux off1 := v.Aux
if v.Args[0].Op != OpADDCQ { if v.Args[0].Op != OpLEAQ8 {
goto end271e3052de832e22b1f07576af2854de goto endce1db8c8d37c8397c500a2068a65c215
} }
off2 := v.Args[0].Aux off2 := v.Args[0].Aux
ptr := v.Args[0].Args[0] ptr := v.Args[0].Args[0]
idx := v.Args[0].Args[1]
val := v.Args[1] val := v.Args[1]
mem := v.Args[2] mem := v.Args[2]
v.Op = OpMOVQstore v.Op = OpMOVQstoreidx8
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = off1.(int64) + off2.(int64) v.Aux = addOff(off1, off2)
v.AddArg(ptr) v.AddArg(ptr)
v.AddArg(idx)
v.AddArg(val) v.AddArg(val)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end271e3052de832e22b1f07576af2854de goto endce1db8c8d37c8397c500a2068a65c215
end271e3052de832e22b1f07576af2854de: endce1db8c8d37c8397c500a2068a65c215:
; ;
// match: (MOVQstore [off1] (LEAQ8 [off2] ptr idx) val mem) case OpMOVQstoreidx8:
// match: (MOVQstoreidx8 [off1] (ADDCQ [off2] ptr) idx val mem)
// cond: // cond:
// result: (MOVQstoreidx8 [off1.(int64)+off2.(int64)] ptr idx val mem) // result: (MOVQstoreidx8 [addOff(off1, off2)] ptr idx val mem)
{ {
off1 := v.Aux off1 := v.Aux
if v.Args[0].Op != OpLEAQ8 { if v.Args[0].Op != OpADDCQ {
goto end4ad469f534c7369f6ac36bdace3462ad goto endcdb222707a568ad468f7fff2fc42fc39
} }
off2 := v.Args[0].Aux off2 := v.Args[0].Aux
ptr := v.Args[0].Args[0] ptr := v.Args[0].Args[0]
idx := v.Args[0].Args[1] idx := v.Args[1]
val := v.Args[1] val := v.Args[2]
mem := v.Args[2] mem := v.Args[3]
v.Op = OpMOVQstoreidx8 v.Op = OpMOVQstoreidx8
v.Aux = nil v.Aux = nil
v.resetArgs() v.resetArgs()
v.Aux = off1.(int64) + off2.(int64) v.Aux = addOff(off1, off2)
v.AddArg(ptr) v.AddArg(ptr)
v.AddArg(idx) v.AddArg(idx)
v.AddArg(val) v.AddArg(val)
v.AddArg(mem) v.AddArg(mem)
return true return true
} }
goto end4ad469f534c7369f6ac36bdace3462ad goto endcdb222707a568ad468f7fff2fc42fc39
end4ad469f534c7369f6ac36bdace3462ad: endcdb222707a568ad468f7fff2fc42fc39:
; ;
case OpMULCQ: case OpMULCQ:
// match: (MULCQ [c] x) // match: (MULCQ [c] x)
...@@ -538,13 +501,13 @@ func lowerAmd64(v *Value) bool { ...@@ -538,13 +501,13 @@ func lowerAmd64(v *Value) bool {
end90a1c055d9658aecacce5e101c1848b4: end90a1c055d9658aecacce5e101c1848b4:
; ;
case OpMULQ: case OpMULQ:
// match: (MULQ x (Const [c])) // match: (MULQ x (MOVQconst [c]))
// cond: // cond:
// result: (MULCQ [c] x) // result: (MULCQ [c] x)
{ {
x := v.Args[0] x := v.Args[0]
if v.Args[1].Op != OpConst { if v.Args[1].Op != OpMOVQconst {
goto endc427f4838d2e83c00cc097b20bd20a37 goto endce35d001482ea209e62e9394bd07c7cb
} }
c := v.Args[1].Aux c := v.Args[1].Aux
v.Op = OpMULCQ v.Op = OpMULCQ
...@@ -554,15 +517,15 @@ func lowerAmd64(v *Value) bool { ...@@ -554,15 +517,15 @@ func lowerAmd64(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
goto endc427f4838d2e83c00cc097b20bd20a37 goto endce35d001482ea209e62e9394bd07c7cb
endc427f4838d2e83c00cc097b20bd20a37: endce35d001482ea209e62e9394bd07c7cb:
; ;
// match: (MULQ (Const [c]) x) // match: (MULQ (MOVQconst [c]) x)
// cond: // cond:
// result: (MULCQ [c] x) // result: (MULCQ [c] x)
{ {
if v.Args[0].Op != OpConst { if v.Args[0].Op != OpMOVQconst {
goto endd70de938e71150d1c9e8173c2a5b2d95 goto end804f58b1f6a7cce19d48379999ec03f1
} }
c := v.Args[0].Aux c := v.Args[0].Aux
x := v.Args[1] x := v.Args[1]
...@@ -573,8 +536,32 @@ func lowerAmd64(v *Value) bool { ...@@ -573,8 +536,32 @@ func lowerAmd64(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
goto endd70de938e71150d1c9e8173c2a5b2d95 goto end804f58b1f6a7cce19d48379999ec03f1
endd70de938e71150d1c9e8173c2a5b2d95: end804f58b1f6a7cce19d48379999ec03f1:
;
case OpMove:
// match: (Move [size] dst src mem)
// cond:
// result: (REPMOVSB dst src (Const <TypeUInt64> [size.(int64)]) mem)
{
size := v.Aux
dst := v.Args[0]
src := v.Args[1]
mem := v.Args[2]
v.Op = OpREPMOVSB
v.Aux = nil
v.resetArgs()
v.AddArg(dst)
v.AddArg(src)
v0 := v.Block.NewValue(OpConst, TypeInvalid, nil)
v0.Type = TypeUInt64
v0.Aux = size.(int64)
v.AddArg(v0)
v.AddArg(mem)
return true
}
goto end48909259b265a6bb2a076bc2c2dc7d1f
end48909259b265a6bb2a076bc2c2dc7d1f:
; ;
case OpMul: case OpMul:
// match: (Mul <t> x y) // match: (Mul <t> x y)
...@@ -597,6 +584,23 @@ func lowerAmd64(v *Value) bool { ...@@ -597,6 +584,23 @@ func lowerAmd64(v *Value) bool {
goto endfab0d598f376ecba45a22587d50f7aff goto endfab0d598f376ecba45a22587d50f7aff
endfab0d598f376ecba45a22587d50f7aff: endfab0d598f376ecba45a22587d50f7aff:
; ;
case OpOffPtr:
// match: (OffPtr [off] ptr)
// cond:
// result: (ADDCQ [off] ptr)
{
off := v.Aux
ptr := v.Args[0]
v.Op = OpADDCQ
v.Aux = nil
v.resetArgs()
v.Aux = off
v.AddArg(ptr)
return true
}
goto endfe8f713b1d237a23311fb721ee46bedb
endfe8f713b1d237a23311fb721ee46bedb:
;
case OpSETL: case OpSETL:
// match: (SETL (InvertFlags x)) // match: (SETL (InvertFlags x))
// cond: // cond:
...@@ -616,13 +620,13 @@ func lowerAmd64(v *Value) bool { ...@@ -616,13 +620,13 @@ func lowerAmd64(v *Value) bool {
end456c7681d48305698c1ef462d244bdc6: end456c7681d48305698c1ef462d244bdc6:
; ;
case OpSUBQ: case OpSUBQ:
// match: (SUBQ x (Const [c])) // match: (SUBQ x (MOVQconst [c]))
// cond: // cond:
// result: (SUBCQ x [c]) // result: (SUBCQ x [c])
{ {
x := v.Args[0] x := v.Args[0]
if v.Args[1].Op != OpConst { if v.Args[1].Op != OpMOVQconst {
goto endb31e242f283867de4722665a5796008c goto endc96cd1cb2dd98427c34fb9543feca4fe
} }
c := v.Args[1].Aux c := v.Args[1].Aux
v.Op = OpSUBCQ v.Op = OpSUBCQ
...@@ -632,16 +636,16 @@ func lowerAmd64(v *Value) bool { ...@@ -632,16 +636,16 @@ func lowerAmd64(v *Value) bool {
v.Aux = c v.Aux = c
return true return true
} }
goto endb31e242f283867de4722665a5796008c goto endc96cd1cb2dd98427c34fb9543feca4fe
endb31e242f283867de4722665a5796008c: endc96cd1cb2dd98427c34fb9543feca4fe:
; ;
// match: (SUBQ <t> (Const [c]) x) // match: (SUBQ <t> (MOVQconst [c]) x)
// cond: // cond:
// result: (NEGQ (SUBCQ <t> x [c])) // result: (NEGQ (SUBCQ <t> x [c]))
{ {
t := v.Type t := v.Type
if v.Args[0].Op != OpConst { if v.Args[0].Op != OpMOVQconst {
goto end569cc755877d1f89a701378bec05c08d goto end900aaaf28cefac6bb62e76b5151611cf
} }
c := v.Args[0].Aux c := v.Args[0].Aux
x := v.Args[1] x := v.Args[1]
...@@ -655,8 +659,8 @@ func lowerAmd64(v *Value) bool { ...@@ -655,8 +659,8 @@ func lowerAmd64(v *Value) bool {
v.AddArg(v0) v.AddArg(v0)
return true return true
} }
goto end569cc755877d1f89a701378bec05c08d goto end900aaaf28cefac6bb62e76b5151611cf
end569cc755877d1f89a701378bec05c08d: end900aaaf28cefac6bb62e76b5151611cf:
; ;
case OpStore: case OpStore:
// match: (Store ptr val mem) // match: (Store ptr val mem)
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
package ssa package ssa
import "fmt"
// An Op encodes the specific operation that a Value performs. // An Op encodes the specific operation that a Value performs.
// Opcodes' semantics can be modified by the type and aux fields of the Value. // Opcodes' semantics can be modified by the type and aux fields of the Value.
// For instance, OpAdd can be 32 or 64 bit, signed or unsigned, float or complex, depending on Value.Type. // For instance, OpAdd can be 32 or 64 bit, signed or unsigned, float or complex, depending on Value.Type.
...@@ -47,8 +49,11 @@ const ( ...@@ -47,8 +49,11 @@ const (
OpArg // address of a function parameter/result. Memory input is an arg called ".mem". aux is a string (TODO: make it something other than a string?) OpArg // address of a function parameter/result. Memory input is an arg called ".mem". aux is a string (TODO: make it something other than a string?)
OpGlobal // the address of a global variable aux.(*gc.Sym) OpGlobal // the address of a global variable aux.(*gc.Sym)
OpFunc // entry address of a function OpFunc // entry address of a function
OpFP // frame pointer
OpSP // stack pointer
OpCopy // output = arg0 OpCopy // output = arg0
OpMove // arg0=destptr, arg1=srcptr, arg2=mem, aux.(int64)=size. Returns memory.
OpPhi // select an argument based on which predecessor block we came from OpPhi // select an argument based on which predecessor block we came from
OpSliceMake // arg0=ptr, arg1=len, arg2=cap OpSliceMake // arg0=ptr, arg1=len, arg2=cap
...@@ -62,7 +67,8 @@ const ( ...@@ -62,7 +67,8 @@ const (
OpLoad // Load from arg0+aux.(int64). arg1=memory OpLoad // Load from arg0+aux.(int64). arg1=memory
OpStore // Store arg1 to arg0+aux.(int64). arg2=memory. Returns memory. OpStore // Store arg1 to arg0+aux.(int64). arg2=memory. Returns memory.
OpSliceIndex // arg0=slice, arg1=index, arg2=memory OpArrayIndex // arg0=array, arg1=index. Returns a[i]
OpPtrIndex // arg0=ptr, arg1=index. Computes ptr+sizeof(*v.type)*index, where index is extended to ptrwidth type
OpIsNonNil // arg0 != nil OpIsNonNil // arg0 != nil
OpIsInBounds // 0 <= arg0 < arg1 OpIsInBounds // 0 <= arg0 < arg1
...@@ -75,6 +81,8 @@ const ( ...@@ -75,6 +81,8 @@ const (
OpConvert // convert arg0 to another type OpConvert // convert arg0 to another type
OpConvNop // interpret arg0 as another type OpConvNop // interpret arg0 as another type
OpOffPtr // arg0 + aux.(int64) (arg0 and result are pointers)
// These ops return a pointer to a location on the stack. // These ops return a pointer to a location on the stack.
OpFPAddr // FP + aux.(int64) (+ == args from caller, - == locals) OpFPAddr // FP + aux.(int64) (+ == args from caller, - == locals)
OpSPAddr // SP + aux.(int64) OpSPAddr // SP + aux.(int64)
...@@ -96,6 +104,15 @@ type GlobalOffset struct { ...@@ -96,6 +104,15 @@ type GlobalOffset struct {
Offset int64 Offset int64
} }
// offset adds x to the location specified by g and returns it.
func (g GlobalOffset) offset(x int64) GlobalOffset {
return GlobalOffset{g.Global, g.Offset + x}
}
func (g GlobalOffset) String() string {
return fmt.Sprintf("%v+%d", g.Global, g.Offset)
}
//go:generate stringer -type=Op //go:generate stringer -type=Op
type opInfo struct { type opInfo struct {
......
...@@ -6,16 +6,16 @@ import "fmt" ...@@ -6,16 +6,16 @@ import "fmt"
const ( const (
_Op_name_0 = "opInvalid" _Op_name_0 = "opInvalid"
_Op_name_1 = "opGenericBaseOpAddOpSubOpMulOpLessOpConstOpArgOpGlobalOpFuncOpCopyOpPhiOpSliceMakeOpSlicePtrOpSliceLenOpSliceCapOpStringMakeOpStringPtrOpStringLenOpLoadOpStoreOpSliceIndexOpIsNonNilOpIsInBoundsOpCallOpStaticCallOpConvertOpConvNopOpFPAddrOpSPAddrOpStoreReg8OpLoadReg8OpFwdRef" _Op_name_1 = "opGenericBaseOpAddOpSubOpMulOpLessOpConstOpArgOpGlobalOpFuncOpFPOpSPOpCopyOpMoveOpPhiOpSliceMakeOpSlicePtrOpSliceLenOpSliceCapOpStringMakeOpStringPtrOpStringLenOpLoadOpStoreOpArrayIndexOpPtrIndexOpIsNonNilOpIsInBoundsOpCallOpStaticCallOpConvertOpConvNopOpOffPtrOpFPAddrOpSPAddrOpStoreReg8OpLoadReg8OpFwdRef"
_Op_name_2 = "opAMD64BaseOpADDQOpSUBQOpADDCQOpSUBCQOpMULQOpMULCQOpSHLQOpSHLCQOpNEGQOpADDLOpCMPQOpCMPCQOpTESTQOpSETEQOpSETNEOpSETLOpSETGEOpSETBOpInvertFlagsOpLEAQOpLEAQ2OpLEAQ4OpLEAQ8OpMOVQloadOpMOVQstoreOpMOVQloadidx8OpMOVQstoreidx8OpMOVQloadglobalOpMOVQstoreglobalOpMOVQloadFPOpMOVQloadSPOpMOVQstoreFPOpMOVQstoreSPOpMOVQconst" _Op_name_2 = "opAMD64BaseOpADDQOpSUBQOpADDCQOpSUBCQOpMULQOpMULCQOpSHLQOpSHLCQOpNEGQOpADDLOpCMPQOpCMPCQOpTESTQOpTESTBOpSETEQOpSETNEOpSETLOpSETGEOpSETBOpInvertFlagsOpLEAQOpLEAQ2OpLEAQ4OpLEAQ8OpLEAQglobalOpMOVBloadOpMOVBQZXloadOpMOVBQSXloadOpMOVQloadOpMOVQstoreOpMOVQloadidx8OpMOVQstoreidx8OpMOVQloadglobalOpMOVQstoreglobalOpMOVQconstOpREPMOVSB"
_Op_name_3 = "op386Base" _Op_name_3 = "op386Base"
_Op_name_4 = "opMax" _Op_name_4 = "opMax"
) )
var ( var (
_Op_index_0 = [...]uint8{0, 9} _Op_index_0 = [...]uint8{0, 9}
_Op_index_1 = [...]uint16{0, 13, 18, 23, 28, 34, 41, 46, 54, 60, 66, 71, 82, 92, 102, 112, 124, 135, 146, 152, 159, 171, 181, 193, 199, 211, 220, 229, 237, 245, 256, 266, 274} _Op_index_1 = [...]uint16{0, 13, 18, 23, 28, 34, 41, 46, 54, 60, 64, 68, 74, 80, 85, 96, 106, 116, 126, 138, 149, 160, 166, 173, 185, 195, 205, 217, 223, 235, 244, 253, 261, 269, 277, 288, 298, 306}
_Op_index_2 = [...]uint16{0, 11, 17, 23, 30, 37, 43, 50, 56, 63, 69, 75, 81, 88, 95, 102, 109, 115, 122, 128, 141, 147, 154, 161, 168, 178, 189, 203, 218, 234, 251, 263, 275, 288, 301, 312} _Op_index_2 = [...]uint16{0, 11, 17, 23, 30, 37, 43, 50, 56, 63, 69, 75, 81, 88, 95, 102, 109, 116, 122, 129, 135, 148, 154, 161, 168, 175, 187, 197, 210, 223, 233, 244, 258, 273, 289, 306, 317, 327}
_Op_index_3 = [...]uint8{0, 9} _Op_index_3 = [...]uint8{0, 9}
_Op_index_4 = [...]uint8{0, 5} _Op_index_4 = [...]uint8{0, 5}
) )
...@@ -24,10 +24,10 @@ func (i Op) String() string { ...@@ -24,10 +24,10 @@ func (i Op) String() string {
switch { switch {
case i == 0: case i == 0:
return _Op_name_0 return _Op_name_0
case 1001 <= i && i <= 1032: case 1001 <= i && i <= 1037:
i -= 1001 i -= 1001
return _Op_name_1[_Op_index_1[i]:_Op_index_1[i+1]] return _Op_name_1[_Op_index_1[i]:_Op_index_1[i+1]]
case 2001 <= i && i <= 2035: case 2001 <= i && i <= 2037:
i -= 2001 i -= 2001
return _Op_name_2[_Op_index_2[i]:_Op_index_2[i+1]] return _Op_name_2[_Op_index_2[i]:_Op_index_2[i+1]]
case i == 3001: case i == 3001:
......
...@@ -30,6 +30,7 @@ const ( ...@@ -30,6 +30,7 @@ const (
OpCMPQ // arg0 compare to arg1 OpCMPQ // arg0 compare to arg1
OpCMPCQ // arg0 compare to aux.(int64) OpCMPCQ // arg0 compare to aux.(int64)
OpTESTQ // (arg0 & arg1) compare to 0 OpTESTQ // (arg0 & arg1) compare to 0
OpTESTB // (arg0 & arg1) compare to 0
// These opcodes extract a particular boolean condition from a flags value. // These opcodes extract a particular boolean condition from a flags value.
OpSETEQ // extract == condition from arg0 OpSETEQ // extract == condition from arg0
...@@ -43,29 +44,30 @@ const ( ...@@ -43,29 +44,30 @@ const (
// This is a pseudo-op which can't appear in assembly output. // This is a pseudo-op which can't appear in assembly output.
OpInvertFlags // reverse direction of arg0 OpInvertFlags // reverse direction of arg0
OpLEAQ // arg0 + arg1 + aux.(int64) OpLEAQ // arg0 + arg1 + aux.(int64)
OpLEAQ2 // arg0 + 2*arg1 + aux.(int64) OpLEAQ2 // arg0 + 2*arg1 + aux.(int64)
OpLEAQ4 // arg0 + 4*arg1 + aux.(int64) OpLEAQ4 // arg0 + 4*arg1 + aux.(int64)
OpLEAQ8 // arg0 + 8*arg1 + aux.(int64) OpLEAQ8 // arg0 + 8*arg1 + aux.(int64)
OpLEAQglobal // no args. address of aux.(GlobalOffset)
// Load/store from general address // Load/store from general address
OpMOVQload // Load from arg0+aux.(int64). arg1=memory OpMOVBload // Load from arg0+aux.(int64). arg1=memory
OpMOVBQZXload
OpMOVBQSXload
OpMOVQload
OpMOVQstore // Store arg1 to arg0+aux.(int64). arg2=memory, returns memory. OpMOVQstore // Store arg1 to arg0+aux.(int64). arg2=memory, returns memory.
OpMOVQloadidx8 // Load from arg0+arg1*8+aux.(int64). arg2=memory OpMOVQloadidx8 // Load from arg0+arg1*8+aux.(int64). arg2=memory
OpMOVQstoreidx8 // Store arg2 to arg0+arg1*8+aux.(int64). arg3=memory, returns memory. OpMOVQstoreidx8 // Store arg2 to arg0+arg1*8+aux.(int64). arg3=memory, returns memory.
// Load/store from global. aux.(GlobalOffset) encodes the global location. // Load/store from global. Same as the above loads, but arg0 is missing and aux is a GlobalOffset instead of an int64.
OpMOVQloadglobal // arg0 = memory OpMOVQloadglobal // arg0 = memory
OpMOVQstoreglobal // store arg0. arg1=memory, returns memory. OpMOVQstoreglobal // store arg0. arg1=memory, returns memory.
// Load/store from stack slot.
OpMOVQloadFP // load from FP+aux.(int64). arg0=memory
OpMOVQloadSP // load from SP+aux.(int64). arg0=memory
OpMOVQstoreFP // store arg0 to FP+aux.(int64). arg1=memory, returns memory.
OpMOVQstoreSP // store arg0 to SP+aux.(int64). arg1=memory, returns memory.
// materialize a constant into a register // materialize a constant into a register
OpMOVQconst // (takes no arguments) OpMOVQconst // (takes no arguments)
// move memory
OpREPMOVSB // arg0=destptr, arg1=srcptr, arg2=len, arg3=mem
) )
type regMask uint64 type regMask uint64
...@@ -89,13 +91,16 @@ var regsAMD64 = [...]string{ ...@@ -89,13 +91,16 @@ var regsAMD64 = [...]string{
"R15", "R15",
// pseudo registers // pseudo registers
"FP",
"FLAGS", "FLAGS",
"OVERWRITE0", // the same register as the first input "OVERWRITE0", // the same register as the first input
} }
var gp regMask = 0xef // all integer registers except SP var gp regMask = 0x1ffff // all integer registers (including SP&FP)
var cx regMask = 0x2 var cx regMask = 1 << 1
var flags regMask = 1 << 16 var si regMask = 1 << 6
var di regMask = 1 << 7
var flags regMask = 1 << 17
var ( var (
// gp = general purpose (integer) registers // gp = general purpose (integer) registers
...@@ -129,13 +134,16 @@ var amd64Table = map[Op]opInfo{ ...@@ -129,13 +134,16 @@ var amd64Table = map[Op]opInfo{
OpCMPQ: {asm: "CMPQ\t%I0,%I1", reg: gp2_flags}, // compute arg[0]-arg[1] and produce flags OpCMPQ: {asm: "CMPQ\t%I0,%I1", reg: gp2_flags}, // compute arg[0]-arg[1] and produce flags
OpCMPCQ: {asm: "CMPQ\t$%A,%I0", reg: gp1_flags}, OpCMPCQ: {asm: "CMPQ\t$%A,%I0", reg: gp1_flags},
OpTESTQ: {asm: "TESTQ\t%I0,%I1", reg: gp2_flags}, OpTESTQ: {asm: "TESTQ\t%I0,%I1", reg: gp2_flags},
OpTESTB: {asm: "TESTB\t%I0,%I1", reg: gp2_flags},
OpLEAQ: {flags: OpFlagCommutative, asm: "LEAQ\t%A(%I0)(%I1*1),%O0", reg: gp21}, // aux = int64 constant to add OpLEAQ: {flags: OpFlagCommutative, asm: "LEAQ\t%A(%I0)(%I1*1),%O0", reg: gp21}, // aux = int64 constant to add
OpLEAQ2: {asm: "LEAQ\t%A(%I0)(%I1*2),%O0"}, OpLEAQ2: {asm: "LEAQ\t%A(%I0)(%I1*2),%O0"},
OpLEAQ4: {asm: "LEAQ\t%A(%I0)(%I1*4),%O0"}, OpLEAQ4: {asm: "LEAQ\t%A(%I0)(%I1*4),%O0"},
OpLEAQ8: {asm: "LEAQ\t%A(%I0)(%I1*8),%O0"}, OpLEAQ8: {asm: "LEAQ\t%A(%I0)(%I1*8),%O0"},
OpLEAQglobal: {asm: "LEAQ\t%A(SB),%O0", reg: gp01},
// loads and stores // loads and stores
OpMOVBload: {asm: "MOVB\t%A(%I0),%O0", reg: gpload},
OpMOVQload: {asm: "MOVQ\t%A(%I0),%O0", reg: gpload}, OpMOVQload: {asm: "MOVQ\t%A(%I0),%O0", reg: gpload},
OpMOVQstore: {asm: "MOVQ\t%I1,%A(%I0)", reg: gpstore}, OpMOVQstore: {asm: "MOVQ\t%I1,%A(%I0)", reg: gpstore},
OpMOVQloadidx8: {asm: "MOVQ\t%A(%I0)(%I1*8),%O0", reg: gploadidx}, OpMOVQloadidx8: {asm: "MOVQ\t%A(%I0)(%I1*8),%O0", reg: gploadidx},
...@@ -145,23 +153,20 @@ var amd64Table = map[Op]opInfo{ ...@@ -145,23 +153,20 @@ var amd64Table = map[Op]opInfo{
OpStaticCall: {asm: "CALL\t%A(SB)"}, OpStaticCall: {asm: "CALL\t%A(SB)"},
OpCopy: {asm: "MOVQ\t%I0,%O0", reg: gp11}, OpCopy: {asm: "MOVQ\t%I0,%O0", reg: gp11}, // TODO: make arch-specific
OpConvNop: {asm: "MOVQ\t%I0,%O0", reg: gp11}, // TODO: make arch-specific. Or get rid of this altogether.
// convert from flags back to boolean // convert from flags back to boolean
OpSETL: {}, OpSETL: {},
// ops for load/store to stack
OpMOVQloadFP: {asm: "MOVQ\t%A(FP),%O0", reg: gpload_stack}, // mem -> value
OpMOVQloadSP: {asm: "MOVQ\t%A(SP),%O0", reg: gpload_stack}, // mem -> value
OpMOVQstoreFP: {asm: "MOVQ\t%I0,%A(FP)", reg: gpstore_stack}, // mem, value -> mem
OpMOVQstoreSP: {asm: "MOVQ\t%I0,%A(SP)", reg: gpstore_stack}, // mem, value -> mem
// ops for spilling of registers // ops for spilling of registers
// unlike regular loads & stores, these take no memory argument. // unlike regular loads & stores, these take no memory argument.
// They are just like OpCopy but we use them during register allocation. // They are just like OpCopy but we use them during register allocation.
// TODO: different widths, float // TODO: different widths, float
OpLoadReg8: {asm: "MOVQ\t%I0,%O0"}, OpLoadReg8: {asm: "MOVQ\t%I0,%O0"},
OpStoreReg8: {asm: "MOVQ\t%I0,%O0"}, OpStoreReg8: {asm: "MOVQ\t%I0,%O0"},
OpREPMOVSB: {asm: "REP MOVSB", reg: [2][]regMask{{di, si, cx, 0}, {0}}}, // TODO: record that si/di/cx are clobbered
} }
func init() { func init() {
......
...@@ -39,8 +39,9 @@ var registers = [...]Register{ ...@@ -39,8 +39,9 @@ var registers = [...]Register{
// TODO X0, ... // TODO X0, ...
// TODO: make arch-dependent // TODO: make arch-dependent
Register{16, "FLAGS"}, Register{16, "FP"}, // pseudo-register, actually a constant offset from SP
Register{17, "OVERWRITE"}, Register{17, "FLAGS"},
Register{18, "OVERWRITE"},
} }
// countRegs returns the number of set bits in the register mask. // countRegs returns the number of set bits in the register mask.
...@@ -84,6 +85,19 @@ func regalloc(f *Func) { ...@@ -84,6 +85,19 @@ func regalloc(f *Func) {
var oldSched []*Value var oldSched []*Value
// Hack to find fp, sp Values and assign them a register. (TODO: make not so hacky)
var fp, sp *Value
for _, v := range f.Entry.Values {
switch v.Op {
case OpSP:
sp = v
home = setloc(home, v, &registers[4]) // TODO: arch-dependent
case OpFP:
fp = v
home = setloc(home, v, &registers[16]) // TODO: arch-dependent
}
}
// Register allocate each block separately. All live values will live // Register allocate each block separately. All live values will live
// in home locations (stack slots) between blocks. // in home locations (stack slots) between blocks.
for _, b := range f.Blocks { for _, b := range f.Blocks {
...@@ -115,6 +129,10 @@ func regalloc(f *Func) { ...@@ -115,6 +129,10 @@ func regalloc(f *Func) {
} }
regs := make([]regInfo, numRegs) regs := make([]regInfo, numRegs)
// TODO: hack: initialize fixed registers
regs[4] = regInfo{sp, sp, false}
regs[16] = regInfo{fp, fp, false}
var used regMask // has a 1 for each non-nil entry in regs var used regMask // has a 1 for each non-nil entry in regs
var dirty regMask // has a 1 for each dirty entry in regs var dirty regMask // has a 1 for each dirty entry in regs
...@@ -133,9 +151,6 @@ func regalloc(f *Func) { ...@@ -133,9 +151,6 @@ func regalloc(f *Func) {
// - definition of v. c will be identical to v but will live in // - definition of v. c will be identical to v but will live in
// a register. v will be modified into a spill of c. // a register. v will be modified into a spill of c.
regspec := opcodeTable[v.Op].reg regspec := opcodeTable[v.Op].reg
if v.Op == OpConvNop {
regspec = opcodeTable[v.Args[0].Op].reg
}
inputs := regspec[0] inputs := regspec[0]
outputs := regspec[1] outputs := regspec[1]
if len(inputs) == 0 && len(outputs) == 0 { if len(inputs) == 0 && len(outputs) == 0 {
...@@ -154,6 +169,7 @@ func regalloc(f *Func) { ...@@ -154,6 +169,7 @@ func regalloc(f *Func) {
// nospill contains registers that we can't spill because // nospill contains registers that we can't spill because
// we already set them up for use by the current instruction. // we already set them up for use by the current instruction.
var nospill regMask var nospill regMask
nospill |= 0x10010 // SP and FP can't be spilled (TODO: arch-specific)
// Move inputs into registers // Move inputs into registers
for _, o := range order { for _, o := range order {
...@@ -215,10 +231,16 @@ func regalloc(f *Func) { ...@@ -215,10 +231,16 @@ func regalloc(f *Func) {
// Load w into this register // Load w into this register
var c *Value var c *Value
if w.Op == OpConst { if len(w.Args) == 0 {
// Materialize w // Materialize w
// TODO: arch-specific MOV op if w.Op == OpFP || w.Op == OpSP || w.Op == OpGlobal {
c = b.NewValue(OpMOVQconst, w.Type, w.Aux) c = b.NewValue1(OpCopy, w.Type, nil, w)
} else {
c = b.NewValue(w.Op, w.Type, w.Aux)
}
} else if len(w.Args) == 1 && (w.Args[0].Op == OpFP || w.Args[0].Op == OpSP || w.Args[0].Op == OpGlobal) {
// Materialize offsets from SP/FP/Global
c = b.NewValue1(w.Op, w.Type, w.Aux, w.Args[0])
} else if wreg != 0 { } else if wreg != 0 {
// Copy from another register. // Copy from another register.
// Typically just an optimization, but this is // Typically just an optimization, but this is
...@@ -317,6 +339,10 @@ func regalloc(f *Func) { ...@@ -317,6 +339,10 @@ func regalloc(f *Func) {
v := regs[r].v v := regs[r].v
c := regs[r].c c := regs[r].c
if lastUse[v.ID] <= len(oldSched) { if lastUse[v.ID] <= len(oldSched) {
if v == v.Block.Control {
// link control value to register version
v.Block.Control = c
}
continue // not live after block continue // not live after block
} }
...@@ -334,6 +360,7 @@ func regalloc(f *Func) { ...@@ -334,6 +360,7 @@ func regalloc(f *Func) {
} }
} }
f.RegAlloc = home f.RegAlloc = home
deadcode(f) // remove values that had all of their uses rematerialized. TODO: separate pass?
} }
// addPhiCopies adds copies of phi inputs in the blocks // addPhiCopies adds copies of phi inputs in the blocks
......
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
package ssa package ssa
import "fmt" import "log"
func applyRewrite(f *Func, r func(*Value) bool) { func applyRewrite(f *Func, r func(*Value) bool) {
// repeat rewrites until we find no more rewrites // repeat rewrites until we find no more rewrites
var curv *Value var curv *Value
defer func() { defer func() {
if curv != nil { if curv != nil {
fmt.Printf("panic during rewrite of %s\n", curv.LongString()) log.Printf("panic during rewrite of %s\n", curv.LongString())
// TODO(khr): print source location also // TODO(khr): print source location also
} }
}() }()
...@@ -19,6 +19,18 @@ func applyRewrite(f *Func, r func(*Value) bool) { ...@@ -19,6 +19,18 @@ func applyRewrite(f *Func, r func(*Value) bool) {
change := false change := false
for _, b := range f.Blocks { for _, b := range f.Blocks {
for _, v := range b.Values { for _, v := range b.Values {
// elide any copies generated during rewriting
for i, a := range v.Args {
if a.Op != OpCopy {
continue
}
for a.Op == OpCopy {
a = a.Args[0]
}
v.Args[i] = a
}
// apply rewrite function
curv = v curv = v
if r(v) { if r(v) {
change = true change = true
...@@ -26,6 +38,7 @@ func applyRewrite(f *Func, r func(*Value) bool) { ...@@ -26,6 +38,7 @@ func applyRewrite(f *Func, r func(*Value) bool) {
} }
} }
if !change { if !change {
curv = nil
return return
} }
} }
...@@ -52,3 +65,19 @@ func isSigned(t Type) bool { ...@@ -52,3 +65,19 @@ func isSigned(t Type) bool {
func typeSize(t Type) int64 { func typeSize(t Type) int64 {
return t.Size() return t.Size()
} }
// addOff adds two offset aux values. Each should be an int64. Fails if wraparound happens.
func addOff(a, b interface{}) interface{} {
x := a.(int64)
y := b.(int64)
z := x + y
// x and y have same sign and z has a different sign => overflow
if x^y >= 0 && x^z < 0 {
log.Panicf("offset overflow %d %d\n", x, y)
}
return z
}
func inBounds(idx, len int64) bool {
return idx >= 0 && idx < len
}
...@@ -3,17 +3,22 @@ ...@@ -3,17 +3,22 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// constant folding // constant folding
(Add <t> (Const [c]) (Const [d])) && is64BitInt(t) && isSigned(t) -> (Const [{c.(int64)+d.(int64)}]) (Add <t> (Const [c]) (Const [d])) && is64BitInt(t) -> (Const [{c.(int64)+d.(int64)}])
(Add <t> (Const [c]) (Const [d])) && is64BitInt(t) && !isSigned(t) -> (Const [{c.(uint64)+d.(uint64)}]) (Mul <t> (Const [c]) (Const [d])) && is64BitInt(t) -> (Const [{c.(int64)*d.(int64)}])
(IsInBounds (Const [c]) (Const [d])) -> (Const [inBounds(c.(int64),d.(int64))])
// tear apart slices // tear apart slices
// TODO: anything that generates a slice needs to go in here. // TODO: anything that generates a slice needs to go in here.
(SlicePtr (Load ptr mem)) -> (Load ptr mem) (SlicePtr (Load ptr mem)) -> (Load ptr mem)
(SliceLen (Load ptr mem)) -> (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.UIntPtr> [int64(v.Block.Func.Config.ptrSize)])) mem) (SliceLen (Load ptr mem)) -> (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.Uintptr> [int64(v.Block.Func.Config.ptrSize)])) mem)
(SliceCap (Load ptr mem)) -> (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.UIntPtr> [int64(v.Block.Func.Config.ptrSize*2)])) mem) (SliceCap (Load ptr mem)) -> (Load (Add <ptr.Type> ptr (Const <v.Block.Func.Config.Uintptr> [int64(v.Block.Func.Config.ptrSize*2)])) mem)
// expand array indexing
// others? Depends on what is already done by frontend
// indexing operations
// Note: bounds check has already been done // Note: bounds check has already been done
(SliceIndex s i mem) -> (Load (Add <s.Type.Elem().PtrTo()> (SlicePtr <s.Type.Elem().PtrTo()> s) (Mul <v.Block.Func.Config.UIntPtr> i (Const <v.Block.Func.Config.UIntPtr> [s.Type.Elem().Size()]))) mem) (ArrayIndex (Load ptr mem) idx) -> (Load (PtrIndex <ptr.Type.Elem().Elem().PtrTo()> ptr idx) mem)
(PtrIndex <t> ptr idx) -> (Add ptr (Mul <v.Block.Func.Config.Uintptr> idx (Const <v.Block.Func.Config.Uintptr> [t.Elem().Size()])))
// TODO: hopefully this will get rid of all full-width array copies.
// big-object moves
// TODO: fix size
(Store dst (Load <t> src mem) mem) && t.Size() > 8 -> (Move [t.Size()] dst src mem)
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
(Less x y) && is64BitInt(v.Args[0].Type) && isSigned(v.Args[0].Type) -> (SETL (CMPQ <TypeFlags> x y)) (Less x y) && is64BitInt(v.Args[0].Type) && isSigned(v.Args[0].Type) -> (SETL (CMPQ <TypeFlags> x y))
(Load <t> ptr mem) && t.IsBoolean() -> (MOVBload [int64(0)] ptr mem)
(Load <t> ptr mem) && (is64BitInt(t) || isPtr(t)) -> (MOVQload [int64(0)] ptr mem) (Load <t> ptr mem) && (is64BitInt(t) || isPtr(t)) -> (MOVQload [int64(0)] ptr mem)
(Store ptr val mem) && (is64BitInt(val.Type) || isPtr(val.Type)) -> (MOVQstore [int64(0)] ptr val mem) (Store ptr val mem) && (is64BitInt(val.Type) || isPtr(val.Type)) -> (MOVQstore [int64(0)] ptr val mem)
...@@ -37,28 +38,27 @@ ...@@ -37,28 +38,27 @@
(IsNonNil p) -> (SETNE (TESTQ <TypeFlags> p p)) (IsNonNil p) -> (SETNE (TESTQ <TypeFlags> p p))
(IsInBounds idx len) -> (SETB (CMPQ <TypeFlags> idx len)) (IsInBounds idx len) -> (SETB (CMPQ <TypeFlags> idx len))
(Move [size] dst src mem) -> (REPMOVSB dst src (Const <TypeUInt64> [size.(int64)]) mem)
(OffPtr [off] ptr) -> (ADDCQ [off] ptr)
(Const <t> [val]) && is64BitInt(t) -> (MOVQconst [val])
// Rules below here apply some simple optimizations after lowering. // Rules below here apply some simple optimizations after lowering.
// TODO: Should this be a separate pass? // TODO: Should this be a separate pass?
// stack loads/stores
(MOVQload [off1] (FPAddr [off2]) mem) -> (MOVQloadFP [off1.(int64)+off2.(int64)] mem)
(MOVQload [off1] (SPAddr [off2]) mem) -> (MOVQloadSP [off1.(int64)+off2.(int64)] mem)
(MOVQstore [off1] (FPAddr [off2]) val mem) -> (MOVQstoreFP [off1.(int64)+off2.(int64)] val mem)
(MOVQstore [off1] (SPAddr [off2]) val mem) -> (MOVQstoreSP [off1.(int64)+off2.(int64)] val mem)
// global loads/stores // global loads/stores
(MOVQload [off] (Global [sym]) mem) -> (MOVQloadglobal [GlobalOffset{sym,off.(int64)}] mem) (Global [sym]) -> (LEAQglobal [GlobalOffset{sym,0}])
(MOVQstore [off] (Global [sym]) val mem) -> (MOVQstoreglobal [GlobalOffset{sym,off.(int64)}] val mem)
// fold constants into instructions // fold constants into instructions
(ADDQ x (Const [c])) -> (ADDCQ [c] x) // TODO: restrict c to int32 range? (ADDQ x (MOVQconst [c])) -> (ADDCQ [c] x) // TODO: restrict c to int32 range?
(ADDQ (Const [c]) x) -> (ADDCQ [c] x) (ADDQ (MOVQconst [c]) x) -> (ADDCQ [c] x)
(SUBQ x (Const [c])) -> (SUBCQ x [c]) (SUBQ x (MOVQconst [c])) -> (SUBCQ x [c])
(SUBQ <t> (Const [c]) x) -> (NEGQ (SUBCQ <t> x [c])) (SUBQ <t> (MOVQconst [c]) x) -> (NEGQ (SUBCQ <t> x [c]))
(MULQ x (Const [c])) -> (MULCQ [c] x) (MULQ x (MOVQconst [c])) -> (MULCQ [c] x)
(MULQ (Const [c]) x) -> (MULCQ [c] x) (MULQ (MOVQconst [c]) x) -> (MULCQ [c] x)
(CMPQ x (Const [c])) -> (CMPCQ x [c]) (CMPQ x (MOVQconst [c])) -> (CMPCQ x [c])
(CMPQ (Const [c]) x) -> (InvertFlags (CMPCQ <TypeFlags> x [c])) (CMPQ (MOVQconst [c]) x) -> (InvertFlags (CMPCQ <TypeFlags> x [c]))
// strength reduction // strength reduction
// TODO: do this a lot more generically // TODO: do this a lot more generically
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
// fold add/shift into leaq // fold add/shift into leaq
(ADDQ x (SHLCQ [shift] y)) && shift.(int64) == 3 -> (LEAQ8 [int64(0)] x y) (ADDQ x (SHLCQ [shift] y)) && shift.(int64) == 3 -> (LEAQ8 [int64(0)] x y)
(ADDCQ [c] (LEAQ8 [d] x y)) -> (LEAQ8 [c.(int64)+d.(int64)] x y) (ADDCQ [c] (LEAQ8 [d] x y)) -> (LEAQ8 [addOff(c, d)] x y)
// reverse ordering of compare instruction // reverse ordering of compare instruction
(SETL (InvertFlags x)) -> (SETGE x) (SETL (InvertFlags x)) -> (SETGE x)
...@@ -76,13 +76,14 @@ ...@@ -76,13 +76,14 @@
// the ADDCQ get eliminated, we still have to compute the ADDCQ and we now // the ADDCQ get eliminated, we still have to compute the ADDCQ and we now
// have potentially two live values (ptr and (ADDCQ [off] ptr)) instead of one. // have potentially two live values (ptr and (ADDCQ [off] ptr)) instead of one.
// Nevertheless, let's do it! // Nevertheless, let's do it!
(MOVQload [off1] (ADDCQ [off2] ptr) mem) -> (MOVQload [off1.(int64)+off2.(int64)] ptr mem) (MOVQload [off1] (ADDCQ [off2] ptr) mem) -> (MOVQload [addOff(off1, off2)] ptr mem)
(MOVQstore [off1] (ADDCQ [off2] ptr) val mem) -> (MOVQstore [off1.(int64)+off2.(int64)] ptr val mem) (MOVQstore [off1] (ADDCQ [off2] ptr) val mem) -> (MOVQstore [addOff(off1, off2)] ptr val mem)
// indexed loads and stores // indexed loads and stores
(MOVQload [off1] (LEAQ8 [off2] ptr idx) mem) -> (MOVQloadidx8 [off1.(int64)+off2.(int64)] ptr idx mem) (MOVQload [off1] (LEAQ8 [off2] ptr idx) mem) -> (MOVQloadidx8 [addOff(off1, off2)] ptr idx mem)
(MOVQstore [off1] (LEAQ8 [off2] ptr idx) val mem) -> (MOVQstoreidx8 [off1.(int64)+off2.(int64)] ptr idx val mem) (MOVQstore [off1] (LEAQ8 [off2] ptr idx) val mem) -> (MOVQstoreidx8 [addOff(off1, off2)] ptr idx val mem)
(MOVQloadidx8 [off1] (ADDCQ [off2] ptr) idx mem) -> (MOVQloadidx8 [addOff(off1, off2)] ptr idx mem)
(MOVQstoreidx8 [off1] (ADDCQ [off2] ptr) idx val mem) -> (MOVQstoreidx8 [addOff(off1, off2)] ptr idx val mem)
// Combine the offset of a stack object with the offset within a stack object (ADDCQ [off] x) && off.(int64) == 0 -> (Copy x)
(ADDCQ [off1] (FPAddr [off2])) -> (FPAddr [off1.(int64)+off2.(int64)])
(ADDCQ [off1] (SPAddr [off2])) -> (SPAddr [off1.(int64)+off2.(int64)])
...@@ -245,6 +245,12 @@ func genResult(w io.Writer, result string) { ...@@ -245,6 +245,12 @@ func genResult(w io.Writer, result string) {
func genResult0(w io.Writer, result string, alloc *int, top bool) string { func genResult0(w io.Writer, result string, alloc *int, top bool) string {
if result[0] != '(' { if result[0] != '(' {
// variable // variable
if top {
fmt.Fprintf(w, "v.Op = %s.Op\n", result)
fmt.Fprintf(w, "v.Aux = %s.Aux\n", result)
fmt.Fprintf(w, "v.resetArgs()\n")
fmt.Fprintf(w, "v.AddArgs(%s.Args...)\n", result)
}
return result return result
} }
...@@ -297,20 +303,33 @@ func split(s string) []string { ...@@ -297,20 +303,33 @@ func split(s string) []string {
outer: outer:
for s != "" { for s != "" {
d := 0 // depth of ({[< d := 0 // depth of ({[<
nonsp := false // found a non-space char so far var open, close byte // opening and closing markers ({[< or )}]>
nonsp := false // found a non-space char so far
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
switch s[i] { switch {
case '(', '{', '[', '<': case d == 0 && s[i] == '(':
open, close = '(', ')'
d++ d++
case ')', '}', ']', '>': case d == 0 && s[i] == '<':
d-- open, close = '<', '>'
case ' ', '\t': d++
if d == 0 && nonsp { case d == 0 && s[i] == '[':
open, close = '[', ']'
d++
case d == 0 && s[i] == '{':
open, close = '{', '}'
d++
case d == 0 && (s[i] == ' ' || s[i] == '\t'):
if nonsp {
r = append(r, strings.TrimSpace(s[:i])) r = append(r, strings.TrimSpace(s[:i]))
s = s[i:] s = s[i:]
continue outer continue outer
} }
case d > 0 && s[i] == open:
d++
case d > 0 && s[i] == close:
d--
default: default:
nonsp = true nonsp = true
} }
......
...@@ -15,6 +15,9 @@ func stackalloc(f *Func) { ...@@ -15,6 +15,9 @@ func stackalloc(f *Func) {
if v.Op != OpPhi { if v.Op != OpPhi {
continue continue
} }
if v.Type.IsMemory() { // TODO: only "regallocable" types
continue
}
n += v.Type.Size() n += v.Type.Size()
// a := v.Type.Align() // a := v.Type.Align()
// n = (n + a - 1) / a * a TODO // n = (n + a - 1) / a * a TODO
...@@ -35,10 +38,11 @@ func stackalloc(f *Func) { ...@@ -35,10 +38,11 @@ func stackalloc(f *Func) {
if v.Type.IsMemory() { // TODO: only "regallocable" types if v.Type.IsMemory() { // TODO: only "regallocable" types
continue continue
} }
if v.Op == OpConst { if len(v.Args) == 0 {
// don't allocate space for OpConsts. They should // v will have been materialized wherever it is needed.
// have been rematerialized everywhere. continue
// TODO: is this the right thing to do? }
if len(v.Args) == 1 && (v.Args[0].Op == OpFP || v.Args[0].Op == OpSP || v.Args[0].Op == OpGlobal) {
continue continue
} }
// a := v.Type.Align() // a := v.Type.Align()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment