-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeadCodeEliminationVisitor.java
126 lines (98 loc) · 4.53 KB
/
DeadCodeEliminationVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package pt.up.fe.comp.ollir.optimization.optimizers;
import pt.up.fe.comp.analysis.table.AstNode;
import pt.up.fe.comp.jmm.ast.AJmmVisitor;
import pt.up.fe.comp.jmm.ast.JmmNode;
public class DeadCodeEliminationVisitor extends AJmmVisitor<String, Boolean> {
public DeadCodeEliminationVisitor() {
setDefaultVisit(this::defaultVisit);
addVisit(AstNode.WHILE, this::whileVisit);
addVisit(AstNode.IF_STATEMENT, this::ifStatementVisit);
}
public Boolean defaultVisit(JmmNode jmmNode, String dummy) {
boolean changes = false;
for (JmmNode child : jmmNode.getChildren()) {
changes = visit(child) || changes;
}
return changes;
}
private Boolean whileVisit(JmmNode whileNode, String dummy) {
JmmNode conditionNode = whileNode.getJmmChild(whileNode.getNumChildren() - 2);
JmmNode properConditionNode = conditionNode.getJmmChild(0);
// while (false) {}
if (properConditionNode.getKind().equals(AstNode.BOOL)) {
if (properConditionNode.get("value").equals("false")) {
whileNode.delete();
return true;
}
// while (id1 < id1) {}
} else if (properConditionNode.getKind().equals(AstNode.BIN_OP)) {
JmmNode leftNode = properConditionNode.getJmmChild(0);
JmmNode rightNode = properConditionNode.getJmmChild(1);
if (properConditionNode.get("op") != null && properConditionNode.get("op").equals("lessThan")) {
if (leftNode.getKind().equals(AstNode.ID) && rightNode.getKind().equals(AstNode.ID)) {
if (leftNode.get("name") != null && rightNode.get("name") != null
&& leftNode.get("name").equals(rightNode.get("name"))) {
whileNode.delete();
return true;
}
}
}
}
return defaultVisit(whileNode.getJmmChild(whileNode.getNumChildren() - 1), null);
}
private Boolean ifStatementVisit(JmmNode ifStatementNode, String dummy) {
JmmNode ifNode = ifStatementNode.getJmmChild(0);
JmmNode conditionNode = ifNode.getJmmChild(0);
JmmNode properConditionNode = conditionNode.getJmmChild(0);
JmmNode ifScopeNode = ifNode.getJmmChild(1);
JmmNode elseNode = ifStatementNode.getJmmChild(1);
JmmNode elseScopeNode = elseNode.getJmmChild(0);
JmmNode toMaintain = null;
// if (true)
// if (false)
if (properConditionNode.getKind().equals(AstNode.BOOL)) {
String value = properConditionNode.get("value");
if (value.equals("true")) {
toMaintain = ifScopeNode;
} else if (value.equals("false")) {
toMaintain = elseScopeNode;
}
// if (id1 < id2)
} else if (properConditionNode.getKind().equals(AstNode.BIN_OP)) {
JmmNode leftNode = properConditionNode.getJmmChild(0);
JmmNode rightNode = properConditionNode.getJmmChild(1);
if (properConditionNode.get("op") != null && properConditionNode.get("op").equals("lessThan")) {
if (leftNode.getKind().equals(AstNode.ID) && rightNode.getKind().equals(AstNode.ID)) {
if (leftNode.get("name") != null && rightNode.get("name") != null
&& leftNode.get("name").equals(rightNode.get("name"))) {
toMaintain = elseScopeNode;
}
}
}
}
if (toMaintain != null) {
return removeIfButMaintainScopeThatIsAlwaysExecuted(ifStatementNode, toMaintain);
} else {
boolean changes = false;
if (ifScopeNode != null && elseScopeNode != null) {
changes = defaultVisit(ifScopeNode, null);
changes = defaultVisit(elseScopeNode, null) || changes;
}
return changes;
}
}
private boolean removeIfButMaintainScopeThatIsAlwaysExecuted(JmmNode ifStatementNode, JmmNode toMaintain) {
if (ifStatementNode != null && toMaintain != null) {
JmmNode ifStatementParentNode = ifStatementNode.getJmmParent();
int index = ifStatementNode.getIndexOfSelf();
int i = 1;
for (JmmNode child : toMaintain.getChildren()) {
ifStatementParentNode.add(child, index + i);
i += 1;
}
ifStatementNode.delete();
return true;
}
return false;
}
}