-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.cpp
169 lines (141 loc) · 4.94 KB
/
optimizer.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "optimizer.h"
#include "TreeNode.h"
double OptimizationVisitor::visit(StatementListNode *node) {
stack.push(node);
for (TreeNode *statement : node->statements) {
statement->accept(this);
}
stack.pop();
return 0;
}
double OptimizationVisitor::visit(ConstantNode *node) {
// There is nothing to optimize.
return 0;
}
double OptimizationVisitor::visit(VariableNode *node) {
// There is nothing to optimize.
return 0;
}
#pragma clang diagnostic push
#pragma ide diagnostic ignored "hicpp-multiway-paths-covered" // Ignore "not all switch paths considered (add default)"
double OptimizationVisitor::visit(OperatorNode *node) {
TreeNode *parent = stack.peek();
// Try to optimize children first.
stack.push(node);
node->left->accept(this);
node->right->accept(this);
stack.pop();
char signature = node->oper->getSignature();
// Try optimize right first because `0^0 = 1`
if (node->right->getType() == TreeNode::Type::CONSTANT) {
auto value = dynamic_cast<ConstantNode *>(node->right)->value->getValue();
if (value == 0) {
switch (signature) {
// expression [*] 0 -> 0
case '*': {
auto constantZero = new ConstantNode(new Number(0));
parent->replaceChild(node, constantZero);
delete node;
break;
}
// expression [+-] 0 -> expression
case '+':
case '-':
parent->replaceChild(node, node->left);
// Avoid deletion of left subtree because we still use it.
node->left = nullptr;
delete node;
break;
// expression ^ 0 -> 1
case '^': {
auto constantOne = new ConstantNode(new Number(1));
parent->replaceChild(node, constantOne);
delete node;
break;
}
}
} else if (value == 1) {
switch (signature) {
// expression [*/^] 1 -> expression
case '*':
case '/':
case '^':
parent->replaceChild(node, node->left);
node->left = nullptr;
delete node;
break;
}
}
} else if (node->left->getType() == TreeNode::Type::CONSTANT) {
auto value = dynamic_cast<ConstantNode *>(node->left)->value->getValue();
if (value == 0) {
switch (signature) {
// 0 [*/^] expression -> 0
case '*':
case '/':
case '^': {
auto constantZero = new ConstantNode(new Number(0));
parent->replaceChild(node, constantZero);
delete node;
break;
}
// 0 [+] expression -> expression
case '+':
parent->replaceChild(node, node->right);
node->right = nullptr;
delete node;
break;
}
} else if (value == 1)
switch (signature) {
// 1 * expression -> expression
case '*':
parent->replaceChild(node, node->right);
node->right = nullptr;
delete node;
break;
// 1 ^ expression -> 1
case '^': {
auto constantOne = new ConstantNode(new Number(1));
parent->replaceChild(node, constantOne);
delete node;
break;
}
}
}
return 0;
}
#pragma clang diagnostic pop
double OptimizationVisitor::visit(AssignmentNode *node) {
stack.push(node);
// Try to optimize expression.
node->expression->accept(this);
stack.pop();
return 0;
}
double OptimizationVisitor::visit(BranchNode *node) {
TreeNode *parent = stack.peek();
stack.push(node);
node->condition->accept(this);
node->ifTrue->accept(this);
if (node->ifFalse != nullptr) {
node->ifFalse->accept(this);
}
if (node->condition->getType() == TreeNode::Type::CONSTANT) {
auto value = dynamic_cast<ConstantNode *>(node->condition)->value->getValue();
if (isTrue(value)) {
parent->replaceChild(node, node->ifTrue);
node->ifTrue = nullptr;
delete node;
} else if (node->ifFalse != nullptr) {
parent->replaceChild(node, node->ifFalse);
node->ifFalse = nullptr;
delete node;
} else {
// Removing node from parent will cause shift of other nodes which is very inefficient.
// It's better to just leave as is.
}
}
stack.pop();
return 0;
}