Make _fast explicit in copy_n_from_history_fast

This commit is contained in:
Nigel Tao committed 2018-07-08 21:39:18 +10:00
1 parent 8a449fa92e
commit e49d464432
6 files changed
+131 -150

No files matched your search

-130
View File
@@ -44,136 +44,6 @@ func splitReceiverMethodArgs(n *a.Expr) (receiver *a.Expr, method t.ID, args []*
return n.LHS().AsExpr(), n.Ident(), args
}
// TODO: should optimizeNonSuspendible be optimizeExpr and check both
// suspendible and non-suspendible expressions?
//
// See also "be principled about checking for provenNotToSuspend".
func (q *checker) optimizeNonSuspendible(n *a.Expr) error {
// TODO: check n.CallSuspendible()?
//
// Should we have an explicit "foo = call bar?()" syntax?
nReceiver, nMethod, _ := splitReceiverMethodArgs(n)
if nReceiver == nil {
return nil
}
// TODO: check that nReceiver's type is actually a io_reader or io_writer.
switch nMethod {
case t.IDCopyNFromHistory:
return q.optimizeCopyNFromHistory(n)
}
return nil
}
// optimizeCopyNFromHistory checks if the code generator can call the Bounds
// Check Optimized version of CopyFromHistory. As per cgen's base-private.h,
// the conditions are:
// - distance > 0
// - distance <= (*ptr_ptr - start)
// - length <= (end - *ptr_ptr)
func (q *checker) optimizeCopyNFromHistory(n *a.Expr) error {
// The arguments are (distance, length).
nReceiver, _, args := splitReceiverMethodArgs(n)
if len(args) != 2 {
return nil
}
l := args[0].AsArg().Value()
d := args[1].AsArg().Value()
// Check "distance > 0".
check0:
for {
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryGreaterThan {
continue
}
if lhs := x.LHS().AsExpr(); !lhs.Eq(d) {
continue
}
if rcv := x.RHS().AsExpr().ConstValue(); rcv == nil || rcv.Sign() != 0 {
continue
}
break check0
}
return nil
}
// Check "distance <= (*ptr_ptr - start)".
check1:
for {
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryLessEq {
continue
}
// Check that the LHS is "d as base.u64".
lhs := x.LHS().AsExpr()
if lhs.Operator() != t.IDXBinaryAs {
continue
}
llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr()
if !llhs.Eq(d) || !lrhs.Eq(typeExprU64) {
continue
}
// Check that the RHS is "nReceiver.since_mark().length()".
y, method, yArgs := splitReceiverMethodArgs(x.RHS().AsExpr())
if method != t.IDLength || len(yArgs) != 0 {
continue
}
z, method, zArgs := splitReceiverMethodArgs(y)
if method != t.IDSinceMark || len(zArgs) != 0 {
continue
}
if !z.Eq(nReceiver) {
continue
}
break check1
}
return nil
}
// Check "length <= (end - *ptr_ptr)".
check2:
for {
for _, x := range q.facts {
if x.Operator() != t.IDXBinaryLessEq {
continue
}
// Check that the LHS is "l as base.u64".
lhs := x.LHS().AsExpr()
if lhs.Operator() != t.IDXBinaryAs {
continue
}
llhs, lrhs := lhs.LHS().AsExpr(), lhs.RHS().AsTypeExpr()
if !llhs.Eq(l) || !lrhs.Eq(typeExprU64) {
continue
}
// Check that the RHS is "nReceiver.available()".
y, method, yArgs := splitReceiverMethodArgs(x.RHS().AsExpr())
if method != t.IDAvailable || len(yArgs) != 0 {
continue
}
if !y.Eq(nReceiver) {
continue
}
break check2
}
return nil
}
n.SetBoundsCheckOptimized()
return nil
}
func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, update bool) (retOK bool, retErr error) {
// TODO: do two passes? The first one to non-destructively check retOK. The
// second one to drop facts if indeed retOK? Otherwise, an advance