internal/cabi: option optimize
This commit is contained in:
@@ -303,8 +303,10 @@ func Do(args []string, conf *Config) ([]Package, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
buildMode := ssaBuildMode
|
buildMode := ssaBuildMode
|
||||||
|
cabiOptimize := true
|
||||||
if IsDbgEnabled() {
|
if IsDbgEnabled() {
|
||||||
buildMode |= ssa.GlobalDebug
|
buildMode |= ssa.GlobalDebug
|
||||||
|
cabiOptimize = false
|
||||||
}
|
}
|
||||||
if !IsOptimizeEnabled() {
|
if !IsOptimizeEnabled() {
|
||||||
buildMode |= ssa.NaiveForm
|
buildMode |= ssa.NaiveForm
|
||||||
@@ -324,7 +326,7 @@ func Do(args []string, conf *Config) ([]Package, error) {
|
|||||||
needPyInit: make(map[*packages.Package]bool),
|
needPyInit: make(map[*packages.Package]bool),
|
||||||
buildConf: conf,
|
buildConf: conf,
|
||||||
crossCompile: export,
|
crossCompile: export,
|
||||||
cTransformer: cabi.NewTransformer(prog, conf.AbiMode),
|
cTransformer: cabi.NewTransformer(prog, conf.AbiMode, cabiOptimize),
|
||||||
}
|
}
|
||||||
pkgs, err := buildAllPkgs(ctx, initial, verbose)
|
pkgs, err := buildAllPkgs(ctx, initial, verbose)
|
||||||
check(err)
|
check(err)
|
||||||
|
|||||||
@@ -15,14 +15,15 @@ const (
|
|||||||
ModeAllFunc
|
ModeAllFunc
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewTransformer(prog ssa.Program, mode Mode) *Transformer {
|
func NewTransformer(prog ssa.Program, mode Mode, optimize bool) *Transformer {
|
||||||
target := prog.Target()
|
target := prog.Target()
|
||||||
tr := &Transformer{
|
tr := &Transformer{
|
||||||
prog: prog,
|
prog: prog,
|
||||||
td: prog.TargetData(),
|
td: prog.TargetData(),
|
||||||
GOOS: target.GOOS,
|
GOOS: target.GOOS,
|
||||||
GOARCH: target.GOARCH,
|
GOARCH: target.GOARCH,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
|
optimize: optimize,
|
||||||
}
|
}
|
||||||
switch target.GOARCH {
|
switch target.GOARCH {
|
||||||
case "amd64":
|
case "amd64":
|
||||||
@@ -42,12 +43,13 @@ func NewTransformer(prog ssa.Program, mode Mode) *Transformer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Transformer struct {
|
type Transformer struct {
|
||||||
prog ssa.Program
|
prog ssa.Program
|
||||||
td llvm.TargetData
|
td llvm.TargetData
|
||||||
GOOS string
|
GOOS string
|
||||||
GOARCH string
|
GOARCH string
|
||||||
sys TypeInfoSys
|
sys TypeInfoSys
|
||||||
mode Mode
|
mode Mode
|
||||||
|
optimize bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Transformer) isCFunc(name string) bool {
|
func (p *Transformer) isCFunc(name string) bool {
|
||||||
@@ -369,13 +371,17 @@ func (p *Transformer) transformFuncBody(m llvm.Module, ctx llvm.Context, info *F
|
|||||||
// store %typ %1, ptr %2, align 4
|
// store %typ %1, ptr %2, align 4
|
||||||
nv = b.CreateLoad(ti.Type, params[index], "")
|
nv = b.CreateLoad(ti.Type, params[index], "")
|
||||||
// replace %0 to %2
|
// replace %0 to %2
|
||||||
replaceAllocaInstrs(fn.Param(i), params[index])
|
if p.optimize {
|
||||||
|
replaceAllocaInstrs(fn.Param(i), params[index])
|
||||||
|
}
|
||||||
case AttrWidthType:
|
case AttrWidthType:
|
||||||
iptr := llvm.CreateAlloca(b, ti.Type1)
|
iptr := llvm.CreateAlloca(b, ti.Type1)
|
||||||
b.CreateStore(params[index], iptr)
|
b.CreateStore(params[index], iptr)
|
||||||
ptr := b.CreateBitCast(iptr, llvm.PointerType(ti.Type, 0), "")
|
ptr := b.CreateBitCast(iptr, llvm.PointerType(ti.Type, 0), "")
|
||||||
nv = b.CreateLoad(ti.Type, ptr, "")
|
nv = b.CreateLoad(ti.Type, ptr, "")
|
||||||
replaceAllocaInstrs(fn.Param(i), ptr)
|
if p.optimize {
|
||||||
|
replaceAllocaInstrs(fn.Param(i), ptr)
|
||||||
|
}
|
||||||
case AttrWidthType2:
|
case AttrWidthType2:
|
||||||
typ := llvm.StructType([]llvm.Type{ti.Type1, ti.Type2}, false)
|
typ := llvm.StructType([]llvm.Type{ti.Type1, ti.Type2}, false)
|
||||||
iptr := llvm.CreateAlloca(b, typ)
|
iptr := llvm.CreateAlloca(b, typ)
|
||||||
@@ -384,7 +390,9 @@ func (p *Transformer) transformFuncBody(m llvm.Module, ctx llvm.Context, info *F
|
|||||||
b.CreateStore(params[index], b.CreateStructGEP(typ, iptr, 1, ""))
|
b.CreateStore(params[index], b.CreateStructGEP(typ, iptr, 1, ""))
|
||||||
ptr := b.CreateBitCast(iptr, llvm.PointerType(ti.Type, 0), "")
|
ptr := b.CreateBitCast(iptr, llvm.PointerType(ti.Type, 0), "")
|
||||||
nv = b.CreateLoad(ti.Type, ptr, "")
|
nv = b.CreateLoad(ti.Type, ptr, "")
|
||||||
replaceAllocaInstrs(fn.Param(i), ptr)
|
if p.optimize {
|
||||||
|
replaceAllocaInstrs(fn.Param(i), ptr)
|
||||||
|
}
|
||||||
case AttrExtract:
|
case AttrExtract:
|
||||||
nsubs := ti.Type.StructElementTypesCount()
|
nsubs := ti.Type.StructElementTypesCount()
|
||||||
nv = llvm.Undef(ti.Type)
|
nv = llvm.Undef(ti.Type)
|
||||||
@@ -426,22 +434,27 @@ func (p *Transformer) transformFuncBody(m llvm.Module, ctx llvm.Context, info *F
|
|||||||
// %2 = load %typ, ptr %1
|
// %2 = load %typ, ptr %1
|
||||||
// store %typ %2, ptr %0 # llvm.memcpy(ptr %0, ptr %1, i64 size, i1 false)
|
// store %typ %2, ptr %0 # llvm.memcpy(ptr %0, ptr %1, i64 size, i1 false)
|
||||||
// ret void
|
// ret void
|
||||||
if load := ret.IsALoadInst(); !load.IsNil() {
|
if p.optimize {
|
||||||
p.callMemcpy(m, ctx, b, params[0], ret.Operand(0), info.Return.Size)
|
if load := ret.IsALoadInst(); !load.IsNil() {
|
||||||
} else {
|
p.callMemcpy(m, ctx, b, params[0], ret.Operand(0), info.Return.Size)
|
||||||
b.CreateStore(ret, params[0])
|
rv = b.CreateRetVoid()
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
b.CreateStore(ret, params[0])
|
||||||
rv = b.CreateRetVoid()
|
rv = b.CreateRetVoid()
|
||||||
case AttrWidthType, AttrWidthType2:
|
case AttrWidthType, AttrWidthType2:
|
||||||
if load := ret.IsALoadInst(); !load.IsNil() {
|
if p.optimize {
|
||||||
iptr := b.CreateBitCast(ret.Operand(0), llvm.PointerType(nft.ReturnType(), 0), "")
|
if load := ret.IsALoadInst(); !load.IsNil() {
|
||||||
rv = b.CreateRet(b.CreateLoad(nft.ReturnType(), iptr, ""))
|
iptr := b.CreateBitCast(ret.Operand(0), llvm.PointerType(nft.ReturnType(), 0), "")
|
||||||
} else {
|
rv = b.CreateRet(b.CreateLoad(nft.ReturnType(), iptr, ""))
|
||||||
ptr := llvm.CreateAlloca(b, info.Return.Type)
|
break
|
||||||
b.CreateStore(ret, ptr)
|
}
|
||||||
iptr := b.CreateBitCast(ptr, llvm.PointerType(nft.ReturnType(), 0), "")
|
|
||||||
rv = b.CreateRet(b.CreateLoad(nft.ReturnType(), iptr, ""))
|
|
||||||
}
|
}
|
||||||
|
ptr := llvm.CreateAlloca(b, info.Return.Type)
|
||||||
|
b.CreateStore(ret, ptr)
|
||||||
|
iptr := b.CreateBitCast(ptr, llvm.PointerType(nft.ReturnType(), 0), "")
|
||||||
|
rv = b.CreateRet(b.CreateLoad(nft.ReturnType(), iptr, ""))
|
||||||
}
|
}
|
||||||
instr.ReplaceAllUsesWith(rv)
|
instr.ReplaceAllUsesWith(rv)
|
||||||
instr.EraseFromParentAsInstruction()
|
instr.EraseFromParentAsInstruction()
|
||||||
@@ -478,21 +491,22 @@ func (p *Transformer) transformCallInstr(m llvm.Module, ctx llvm.Context, call l
|
|||||||
case AttrVoid:
|
case AttrVoid:
|
||||||
// none
|
// none
|
||||||
case AttrPointer:
|
case AttrPointer:
|
||||||
// check ptr
|
if p.optimize {
|
||||||
if rv := param.IsALoadInst(); !rv.IsNil() {
|
if rv := param.IsALoadInst(); !rv.IsNil() {
|
||||||
ptr := rv.Operand(0)
|
ptr := rv.Operand(0)
|
||||||
if p.sys.SupportByVal() {
|
if p.sys.SupportByVal() {
|
||||||
nparams = append(nparams, ptr)
|
nparams = append(nparams, ptr)
|
||||||
} else {
|
} else {
|
||||||
nptr := createAlloca(ti.Type)
|
nptr := createAlloca(ti.Type)
|
||||||
p.callMemcpy(m, ctx, b, nptr, ptr, ti.Size)
|
p.callMemcpy(m, ctx, b, nptr, ptr, ti.Size)
|
||||||
nparams = append(nparams, nptr)
|
nparams = append(nparams, nptr)
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ptr := createAlloca(ti.Type)
|
|
||||||
b.CreateStore(param, ptr)
|
|
||||||
nparams = append(nparams, ptr)
|
|
||||||
}
|
}
|
||||||
|
ptr := createAlloca(ti.Type)
|
||||||
|
b.CreateStore(param, ptr)
|
||||||
|
nparams = append(nparams, ptr)
|
||||||
case AttrWidthType:
|
case AttrWidthType:
|
||||||
ptr := createAlloca(ti.Type)
|
ptr := createAlloca(ti.Type)
|
||||||
b.CreateStore(param, ptr)
|
b.CreateStore(param, ptr)
|
||||||
|
|||||||
Reference in New Issue
Block a user