Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 1.9 (in progress)

- The local optimisations no longer skip the condition of an if whose branches both start with a return or a loop
exit. The branch merger moved the equal first statement in front of the if, so `if eff(b) > 0 ... return else
return` became a plain `return` and `eff` was never called (Jass and Lua, `-localOptimizations`). A statement which
may leave the block is moved now only when the condition has no effect. Castle fight compiles to the same script.

- On Lua, a compile-time expression can run the generic keyed-map intrinsics (`keyedMapPutNative`,
`keyedMapGetNative`) again when nothing calls their Jass fallbacks at run time. The interpreter runs them through
those fallbacks and finds them by name, but the tree shake in front of the compile-time run kept the intrinsic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public void visit(ImStmts stmts) {
// and has no side-effects that could affect the if-condition:
if (firstStmtThen.structuralEquals(firstStmtElse)
&& !localPlayerContextAnalyzer.isLocalPlayerDependent(ifStmt.getCondition())
&& !sideEffectAnalyzer.mightAffect(firstStmtThen, ifStmt.getCondition())) {
&& !sideEffectAnalyzer.mightAffect(firstStmtThen, ifStmt.getCondition())
&& (!mayLeave(firstStmtThen)
|| !SideEffectAnalyzer.quickcheckHasSideeffects(ifStmt.getCondition()))) {
// remove statements
ifStmt.getThenBlock().remove(0);
ifStmt.getElseBlock().remove(0);
Expand All @@ -78,6 +80,45 @@ public void visit(ImStmts stmts) {



/**
* Whether control may leave the statements around {@code s} from inside it: by a return, or by an exitwhen of a
* loop outside {@code s}. Moved in front of an if, such a statement runs before the condition, which then is
* not evaluated at all when it leaves.
*/
private static boolean mayLeave(ImStmt s) {
boolean[] leaves = {false};
s.accept(new Element.DefaultVisitor() {
private int loops = 0;

@Override
public void visit(ImReturn r) {
leaves[0] = true;
}

@Override
public void visit(ImExitwhen e) {
if (loops == 0) {
leaves[0] = true;
}
}

@Override
public void visit(ImLoop l) {
loops++;
super.visit(l);
loops--;
}

@Override
public void visit(ImVarargLoop l) {
loops++;
super.visit(l);
loops--;
}
});
return leaves[0];
}

@Override
public String getName() {
return "Branches merged";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,74 @@ public void branchMergerMustNotHoistAcrossStoredLocalPlayerCondition() throws Ex
"identical branches controlled by local-player data must remain separate");
}

/**
* In front of the if, a statement runs before the condition: a return there means the condition is never
* evaluated, so its call must stay.
*/
@Test
public void branchMergerKeepsTheConditionBeforeEqualReturns() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package test",
"native testSuccess()",
"int trace = 0",
"@noinline function eff(int k) returns int",
" trace += k",
" return k",
"@noinline function f(int b)",
" if eff(b) > 0",
" return",
" else",
" return",
"init",
" f(5)",
" if trace == 5",
" testSuccess()");
}

/** The same for an exit of the loop around the if. */
@Test
public void branchMergerKeepsTheConditionBeforeEqualExits() {
test().testLua(true).luaOnly(false).executeProg().lines(
"package test",
"native testSuccess()",
"int trace = 0",
"@noinline function eff(int k) returns int",
" trace += k",
" return k",
"@noinline function f(int b)",
" while true",
" if eff(b) > 0",
" break",
" else",
" break",
"init",
" f(5)",
" if trace == 5",
" testSuccess()");
}

/** A condition without effects is not missed, so equal returns are still merged and the if goes. */
@Test
public void branchMergerMergesEqualReturnsUnderAConditionWithoutEffects() throws Exception {
test().lines(
"package test",
"native print(int i)",
"@noinline function f(int b)",
" if b > 0",
" return",
" else",
" return",
"init",
" f(5)",
" print(1)");
String optimized = Files.toString(
new File("test-output/OptimizerTests_branchMergerMergesEqualReturnsUnderAConditionWithoutEffects_opt.j"),
Charsets.UTF_8);
String f = optimized.substring(optimized.indexOf("function f takes"));
f = f.substring(0, f.indexOf("endfunction"));
assertFalse(f.contains("if "), "the equal returns are merged:\n" + f);
}

@Test
public void branchMergerMustTrackLocalPlayerThroughFunctionParameters() throws Exception {
test().lines(
Expand Down
Loading