Skip to content

Optimize recursive calls when unoptimizable branches are present in the return path #1

Open
@Sipkab

Description

@Sipkab

There may be cases when a tail recursive call has multiple execution paths after the call itself. The optimization should be performed in cases where it is possible.

One example is such:

public static int count(int n) {
	if (n == 0) {
		return 0;
	}
	int v = count(n - 1);
	if (n == 2) {
		callSomeOtherMethod();
	}
	return v;
}

Should be optimized to:

public static int count(int n) {
start:
	if (n == 0) {
		return 0;
	}
	
	if (n == 2) {
		int v = count(n - 1);
		callSomeOtherMethod();
		return v;
	}
	// this below is the same as 
	//     return count(n - 1);
	n = n - 1;
	goto start;
}

Current state

Currently we allow branches to be present in the return path, but they all need to be side-effect free. This issue requires individual modifications and analysis to the subsequent execution paths.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions