<commit_before>package opt

import (
	"sexp"
)

// InlineCalls inlines suitable functions calls inside form.
func InlineCalls(form sexp.Form) sexp.Form {
	return sexp.Rewrite(form, func(form sexp.Form) sexp.Form {
		if form, ok := form.(*sexp.Call); ok {
			return inlineCall(form.Fn, form.Args)
		}
		return nil
	})
}

func inlineCall(fn *sexp.Func, args []sexp.Form) sexp.Form {
	body := fn.Body
	expr := inlineableForm(body)
	if expr == nil {
		return nil
	}

	bindings := make([]*sexp.Bind, 0, len(fn.Params))
	for i, param := range fn.Params {
		arg := InlineCalls(args[i])
		if sexp.Cost(arg) == 1 {
			expr = injectValue(param, arg, expr)
		} else if countUsages(param, expr) == 1 {
			expr = injectValue(param, arg, expr)
		} else {
			bindings = append(bindings, &sexp.Bind{
				Name: param,
				Init: arg,
			})
		}
	}

	if len(bindings) == 0 {
		// Perfect inlining, no let wrapper is needed.
		return expr
	}
	return &sexp.Let{
		Bindings: bindings,
		Expr:     expr,
	}
}

func inlineableForm(body *sexp.Block) sexp.Form {
	if len(body.Forms) == 0 {
		return nil
	}

	worthToInline := func(form sexp.Form) bool {
		return sexp.Cost(form) <= 15
	}

	switch form := body.Forms[0].(type) {
	case *sexp.Return:
		if len(form.Results) != 1 {
			return nil
		}
		if worthToInline(form.Results[0]) {
			form.Results[0].Copy()
		}

	case *sexp.ExprStmt:
		if !worthToInline(form.Expr) {
			return nil
		}
		// Sole statement is inlineable.
		if len(body.Forms) == 1 {
			return form.Expr.Copy()
		}
		// Trailing "return" is permitted (auto-inserted for void functions).
		if len(body.Forms) == 2 {
			ret, ok := body.Forms[1].(*sexp.Return)
			if ok && len(ret.Results) == 0 {
				return form.Expr.Copy()
			}
		}<commit_msg>fix bug in inliner; reduce max inlineable form cost<commit_after>package opt

import (
	"sexp"
)

// InlineCalls inlines suitable functions calls inside form.
func InlineCalls(form sexp.Form) sexp.Form {
	return sexp.Rewrite(form, func(form sexp.Form) sexp.Form {
		if form, ok := form.(*sexp.Call); ok {
			return inlineCall(form.Fn, form.Args)
		}
		return nil
	})
}

func inlineCall(fn *sexp.Func, args []sexp.Form) sexp.Form {
	body := fn.Body
	expr := inlineableForm(body)
	if expr == nil {
		return nil
	}

	bindings := make([]*sexp.Bind, 0, len(fn.Params))
	for i, param := range fn.Params {
		arg := InlineCalls(args[i])
		if sexp.Cost(arg) == 1 {
			expr = injectValue(param, arg, expr)
		} else if countUsages(param, expr) == 1 {
			expr = injectValue(param, arg, expr)
		} else {
			bindings = append(bindings, &sexp.Bind{
				Name: param,
				Init: arg,
			})
		}
	}

	if len(bindings) == 0 {
		// Perfect inlining, no let wrapper is needed.
		return expr
	}
	return &sexp.Let{
		Bindings: bindings,
		Expr:     expr,
	}
}

func inlineableForm(body *sexp.Block) sexp.Form {
	if len(body.Forms) == 0 {
		return nil
	}

	worthToInline := func(form sexp.Form) bool {
		return sexp.Cost(form) <= 12
	}

	switch form := body.Forms[0].(type) {
	case *sexp.Return:
		if len(form.Results) != 1 {
			return nil
		}
		if worthToInline(form.Results[0]) {
			return form.Results[0].Copy()
		}

	case *sexp.ExprStmt:
		if !worthToInline(form.Expr) {
			return nil
		}
		// Sole statement is inlineable.
		if len(body.Forms) == 1 {
			return form.Expr.Copy()
		}
		// Trailing "return" is permitted (auto-inserted for void functions).
		if len(body.Forms) == 2 {
			ret, ok := body.Forms[1].(*sexp.Return)
			if ok && len(ret.Results) == 0 {
				return form.Expr.Copy()
			}
		}
{'fraction_non_alphanumeric': 0.1508411747932706, 'fraction_numerical': 0.00855431993156544, 'mean_word_length': 3.107728337236534, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3479938', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1339, 'n_words': 333}