Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

wurstscript / WurstScript / 645

3 Aug 2018 - 7:21 coverage increased (+0.06%) to 60.856%
645

Pull #701

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
add Player() to useless function calls

Doesn't allocate handles
Pull Request #701: Get rid of empty init functions

13459 of 22116 relevant lines covered (60.86%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.68
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imoptimizer/ImOptimizer.java
1
package de.peeeq.wurstscript.translation.imoptimizer;
2

3
import com.google.common.collect.Lists;
4
import de.peeeq.wurstscript.WLogger;
5
import de.peeeq.wurstscript.intermediatelang.optimizer.*;
6
import de.peeeq.wurstscript.jassIm.*;
7
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
8
import de.peeeq.wurstscript.utils.Pair;
9

10
import java.util.ArrayList;
11
import java.util.HashMap;
12
import java.util.List;
13

14
public class ImOptimizer {
15
    private int totalFunctionsRemoved = 0;
1×
16
    private int totalGlobalsRemoved = 0;
1×
17

18
    private static final ArrayList<OptimizerPass> localPasses = new ArrayList<>();
1×
19
    private static final HashMap<String, Integer> totalCount = new HashMap<>();
1×
20

21
    static {
22
        localPasses.add(new ConstantAndCopyPropagation());
1×
23
        localPasses.add(new UselessFunctionCallsRemover());
1×
24
        localPasses.add(new GlobalsInliner());
1×
25
        localPasses.add(new BranchMerger());
1×
26
        localPasses.add(new SimpleRewrites());
1×
27
        localPasses.add(new TempMerger());
1×
28
        localPasses.add(new LocalMerger());
1×
29
    }
1×
30

31

32
    ImTranslator trans;
33

34
    public ImOptimizer(ImTranslator trans) {
1×
35
        this.trans = trans;
1×
36
    }
1×
37

38
    public void optimize() {
39
        removeGarbage();
!
40
        ImCompressor compressor = new ImCompressor(trans);
!
41
        compressor.compressNames();
!
42
    }
!
43

44
    public void doInlining() {
45
        // remove garbage to reduce work for the inliner
46
        removeGarbage();
1×
47
        GlobalsInliner globalsInliner = new GlobalsInliner();
1×
48
        globalsInliner.optimize(trans);
1×
49
        ImInliner inliner = new ImInliner(trans);
1×
50
        inliner.doInlining();
1×
51
        trans.assertProperties();
1×
52
        // remove garbage, because inlined functions can be removed
53
        removeGarbage();
1×
54
    }
1×
55

56
    private int optCount = 1;
1×
57

58
    public void localOptimizations() {
59
        totalCount.clear();
1×
60
        removeGarbage();
1×
61

62
        int finalItr = 0;
1×
63
        for (int i = 1; i <= 10 && optCount > 0; i++) {
1×
64
            optCount = 0;
1×
65
            localPasses.forEach(pass -> {
1×
66
                int count = pass.optimize(trans);
1×
67
                optCount += count;
1×
68
                totalCount.put(pass.getName(), totalCount.getOrDefault(pass.getName(), 0) + count);
1×
69
            });
1×
70
            trans.getImProg().flatten(trans);
1×
71
            removeGarbage();
1×
72
            finalItr = i;
1×
73
            WLogger.info("=== Optimization pass: " + i + " opts: " + optCount + " ===");
1×
74
        }
75
        WLogger.info("=== Local optimizations done! Ran " + finalItr + " passes. ===");
1×
76
        totalCount.forEach((k, v) -> {
1×
77
            WLogger.info("== " + k + ":   " + v);
1×
78
        });
1×
79

80
        InitFunctionCleaner.clean(trans.getImProg());
1×
81
    }
1×
82

83
    public void doNullsetting() {
84
        NullSetter ns = new NullSetter(trans);
1×
85
        ns.optimize();
1×
86
        trans.assertProperties();
1×
87
    }
1×
88

89
    public void removeGarbage() {
90
        boolean changes = true;
1×
91
        int iterations = 0;
1×
92
        while (changes && iterations++ < 10) {
1×
93
            ImProg prog = trans.imProg();
1×
94
            trans.calculateCallRelationsAndUsedVariables();
1×
95

96
            // keep only used variables
97
            int globalsBefore = prog.getGlobals().size();
1×
98
            changes = prog.getGlobals().retainAll(trans.getReadVariables());
1×
99
            int globalsAfter = prog.getGlobals().size();
1×
100
            int globalsRemoved = globalsBefore - globalsAfter;
1×
101
            totalGlobalsRemoved += globalsRemoved;
1×
102
            // keep only functions reachable from main and config
103
            int functionsBefore = prog.getFunctions().size();
1×
104
            changes |= prog.getFunctions().retainAll(trans.getUsedFunctions());
1×
105
            int functionsAfter = prog.getFunctions().size();
1×
106
            int functionsRemoved = functionsBefore - functionsAfter;
1×
107
            totalFunctionsRemoved += functionsRemoved;
1×
108
            for (ImFunction f : prog.getFunctions()) {
1×
109
                // remove set statements to unread variables
110
                final List<Pair<ImStmt, ImStmt>> replacements = Lists.newArrayList();
1×
111
                f.accept(new ImFunction.DefaultVisitor() {
1×
112
                    @Override
113
                    public void visit(ImSet e) {
114
                        super.visit(e);
1×
115
                        if (!trans.getReadVariables().contains(e.getLeft())) {
1×
116
                            replacements.add(Pair.<ImStmt, ImStmt>create(e, e.getRight()));
1×
117
                        }
118
                    }
1×
119

120
                    @Override
121
                    public void visit(ImSetArrayTuple e) {
122
                        super.visit(e);
1×
123
                        if (!trans.getReadVariables().contains(e.getLeft())) {
1×
UNCOV
124
                            replacements.add(Pair.<ImStmt, ImStmt>create(e, e.getRight()));
!
125
                        }
126
                    }
1×
127

128
                    @Override
129
                    public void visit(ImSetArray e) {
130
                        super.visit(e);
1×
131
                        if (!trans.getReadVariables().contains(e.getLeft())) {
1×
132
                            replacements.add(Pair.<ImStmt, ImStmt>create(e, e.getRight()));
1×
133
                        }
134
                    }
1×
135

136
                    @Override
137
                    public void visit(ImSetTuple e) {
138
                        super.visit(e);
1×
139
                        if (!trans.getReadVariables().contains(e.getLeft())) {
1×
UNCOV
140
                            replacements.add(Pair.<ImStmt, ImStmt>create(e, e.getRight()));
!
141
                        }
142
                    }
1×
143
                });
144
                for (Pair<ImStmt, ImStmt> pair : replacements) {
1×
145
                    changes = true;
1×
146
                    pair.getB().setParent(null);
1×
147
                    pair.getA().replaceBy(pair.getB());
1×
148
                }
1×
149

150
                // keep only read local variables
151
                changes |= f.getLocals().retainAll(trans.getReadVariables());
1×
152
            }
1×
153
        }
1×
154
    }
1×
155

156
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc