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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.peeeq.datastructures.NodeWorklist;
import de.peeeq.wurstscript.intermediatelang.optimizer.ControlFlowGraph.Node;
import de.peeeq.wurstscript.jassIm.*;
import de.peeeq.wurstscript.translation.imoptimizer.OptimizerPass;
import de.peeeq.wurstscript.translation.imtranslation.ImHelper;
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
import de.peeeq.wurstscript.translation.imtranslation.LuaMultipleResults;
Expand All @@ -14,13 +15,17 @@

import java.util.*;

public class ConstantAndCopyPropagation implements LocalPlayerAwareOptimizerPass {
/**
* Replaces a read of a local by the constant or the other local it is known to hold. It needs no locality analysis:
* a read which is replaced reads the same value on every client, and no computation moves, so a value which depends
* on the local player is propagated like any other.
*/
public class ConstantAndCopyPropagation implements OptimizerPass {
private int totalPropagated = 0;
private @Nullable LocalPlayerContextAnalyzer localPlayerContextAnalyzer;

public int optimize(ImTranslator trans, LocalPlayerContextAnalyzer analyzer) {
@Override
public int optimize(ImTranslator trans) {
ImProg prog = trans.getImProg();
localPlayerContextAnalyzer = analyzer;

totalPropagated = 0;
for (ImFunction func : ImHelper.calculateFunctionsOfProg(prog)) {
Expand Down Expand Up @@ -153,11 +158,6 @@ void optimizeFunc(ImFunction func) {
rewriteCode(cfg, knowledge);
}

void optimizeFunc(ImFunction func, LocalPlayerContextAnalyzer analyzer) {
localPlayerContextAnalyzer = analyzer;
optimizeFunc(func);
}

private void rewriteCode(ControlFlowGraph cfg, Map<Node, Knowledge> knowledge) {
for (Node node : cfg.getNodes()) {
ImStmt stmt = node.getStmt();
Expand Down Expand Up @@ -325,13 +325,7 @@ private void analyzeComponent(List<Node> scc, Map<Node, Knowledge> knowledge) {

// Constant folding is intentionally centralized in SimpleRewrites.
// This pass performs propagation only to keep fold semantics in one place.
if (localPlayerContextAnalyzer != null
&& localPlayerContextAnalyzer.isLocalPlayerDependent(right)) {
// Keep GetLocalPlayer-derived values at their original
// variable boundary. This is deliberately conservative:
// later rewrites must still be able to see that boundary.
newValue = null;
} else if (right instanceof ImConst) {
if (right instanceof ImConst) {
newValue = Value.tryValue(right);
} else if (right instanceof ImVarAccess imVarAccess) {
ImVar varRight = imVarAccess.getVar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ private void optimize() {
Preconditions.checkArgument(func.getReturnType() instanceof ImVoid, "func must return void");
// run some basic optimizations first:
func.flatten(tr);
// Both passes ask the locality analysis only about locals and the values assigned to them (the function has
new ConstantAndCopyPropagation().optimizeFunc(func);
// The merge asks the locality analysis only about locals and the values assigned to them (the function has
// no parameters), and the compile-time state usually has none, so the analysis is made only when it has some.
LocalPlayerContextAnalyzer localPlayerContextAnalyzer = func.getLocals().isEmpty()
? null
: new LocalPlayerContextAnalyzer(tr.getImProg());
new ConstantAndCopyPropagation().optimizeFunc(func, localPlayerContextAnalyzer);
new LocalMerger().optimizeFunc(func, localPlayerContextAnalyzer, tr);
Set<ImVar> usedVars = UsedVariables.calculate(func);
func.getLocals().removeIf(v -> !usedVars.contains(v));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ public boolean functionUsesLocalPlayer(ImFunction function) {
* consulted. A function reachable from a client-local branch has a tainted return fact, but
* inlining substitutes its body at the call site, where it runs under exactly the control the
* call already had, so nothing crosses a boundary. The passes which do move code
* ({@link LocalMerger}, {@link ConstantAndCopyPropagation}, ...) run after inlining and re-analyse the inlined
* program, where that control is explicit.
* ({@link LocalMerger}, ...) run after inlining and re-analyse the inlined program, where that control is
* explicit.
*/
public boolean functionInliningIsLocalPlayerSensitive(ImFunction function) {
if (function == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class ImOptimizer {
static {
localPasses.add(new SimpleRewrites());
localPasses.add(new LocalMerger());
localPasses.add(new ConstantAndCopyPropagation());
// After the passes which use the local-player analysis: a pass which does not use it discards it, and none
// After the pass which uses the local-player analysis: a pass which does not use it discards it, and none
// after this one needs it again in the same sweep.
localPasses.add(new ConstantAndCopyPropagation());
localPasses.add(new RedundantFieldStores());
localPasses.add(new GlobalsInliner());
localPasses.add(new DispatchCheckDeduplicator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3486,6 +3486,41 @@ public void directGetLocalPlayerConditionMustNotBeDiscarded() throws Exception {
"local-player-dependent expressions must not be discarded");
}

/**
* A copy of a value which depends on the local player is propagated like any other: the read which replaces it
* reads the same value on every client, and nothing moves. The source stays live after the copy, so the local
* merger cannot remove the copy; only the propagation can.
*/
@Test
public void copyOfLocalPlayerValueIsPropagated() throws Exception {
test().lines(
"type player extends handle",
"package test",
"@extern native GetLocalPlayer() returns player",
"@extern native GetPlayerId(player p) returns integer",
"native print(integer i)",
"class Box",
" player owner",
"@noinline function show(Box b)",
" player voter = b.owner",
" player q = voter",
" print(GetPlayerId(q))",
" print(GetPlayerId(voter) + 1)",
" print(GetPlayerId(q) + 2)",
"init",
" let b = new Box",
" b.owner = GetLocalPlayer()",
" show(b)"
);

String optimized = Files.toString(
new File("test-output/OptimizerTests_copyOfLocalPlayerValueIsPropagated_opt.j"),
Charsets.UTF_8);
assertTrue(optimized.contains("GetLocalPlayer()"), optimized);
assertFalse(optimized.contains("q = voter"), optimized);
assertTrue(optimized.contains("GetPlayerId(voter)"), optimized);
}

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