-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.cpp
More file actions
178 lines (158 loc) · 5.9 KB
/
optimization.cpp
File metadata and controls
178 lines (158 loc) · 5.9 KB
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
170
171
172
173
174
175
176
177
178
#include "optimization.h"
#include <unordered_set>
#include <unordered_map>
// Dead Code Elimination Pass
void DeadCodeElimination::runOnFunction(Function& func) {
bool changed;
do {
changed = false;
std::unordered_set<BasicBlock*> reachable;
std::vector<BasicBlock*> workList;
// Start from entry block
if (!func.blocks.empty()) {
workList.push_back(func.blocks[0].get());
reachable.insert(func.blocks[0].get());
}
// Find all reachable blocks
while (!workList.empty()) {
BasicBlock* block = workList.back();
workList.pop_back();
for (BasicBlock* succ : block->successors) {
if (reachable.insert(succ).second) {
workList.push_back(succ);
}
}
}
// Remove unreachable blocks
auto it = func.blocks.begin();
while (it != func.blocks.end()) {
if (reachable.count(it->get()) == 0) {
it = func.blocks.erase(it);
changed = true;
} else {
++it;
}
}
// Remove dead instructions within blocks
for (auto& block : func.blocks) {
auto instIt = block->instructions.begin();
while (instIt != block->instructions.end()) {
bool isDead = true;
// Check if instruction results are used
for (Value* result : (*instIt)->getResults()) {
for (auto& otherBlock : func.blocks) {
for (auto& otherInst : otherBlock->instructions) {
for (Value* operand : otherInst->getOperands()) {
if (operand == result) {
isDead = false;
break;
}
}
if (!isDead) break;
}
if (!isDead) break;
}
if (!isDead) break;
}
// Remove dead instruction
if (isDead && !(*instIt)->isTerminator()) {
instIt = block->instructions.erase(instIt);
changed = true;
} else {
++instIt;
}
}
}
} while (changed);
}
void DeadCodeElimination::runOnModule(Module& module) {
for (auto& func : module.functions) {
runOnFunction(*func);
}
}
// Constant Folding Pass
void ConstantFolding::runOnFunction(Function& func) {
bool changed;
do {
changed = false;
for (auto& block : func.blocks) {
auto instIt = block->instructions.begin();
while (instIt != block->instructions.end()) {
bool folded = false;
// Try to evaluate constant expressions
// This is a placeholder - actual implementation would handle specific IR instructions
if (folded) {
changed = true;
instIt = block->instructions.erase(instIt);
} else {
++instIt;
}
}
}
} while (changed);
}
void ConstantFolding::runOnModule(Module& module) {
for (auto& func : module.functions) {
runOnFunction(*func);
}
}
// Common Subexpression Elimination Pass
struct ExpressionHash {
size_t operator()(const IRInstruction* inst) const {
// Hash based on instruction type and operands
size_t hash = std::hash<std::string>{}(inst->toString());
for (Value* operand : inst->getOperands()) {
hash ^= std::hash<Value*>{}(operand) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
}
return hash;
}
};
struct ExpressionEqual {
bool operator()(const IRInstruction* lhs, const IRInstruction* rhs) const {
// Compare instruction type and operands
if (lhs->toString() != rhs->toString()) return false;
auto lhsOps = lhs->getOperands();
auto rhsOps = rhs->getOperands();
if (lhsOps.size() != rhsOps.size()) return false;
for (size_t i = 0; i < lhsOps.size(); ++i) {
if (lhsOps[i] != rhsOps[i]) return false;
}
return true;
}
};
void CommonSubexpressionElimination::runOnFunction(Function& func) {
bool changed;
do {
changed = false;
std::unordered_map<IRInstruction*, Value*, ExpressionHash, ExpressionEqual> availableExpr;
for (auto& block : func.blocks) {
auto instIt = block->instructions.begin();
while (instIt != block->instructions.end()) {
auto* inst = instIt->get();
// Look for available expression
auto it = availableExpr.find(inst);
if (it != availableExpr.end()) {
// Replace instruction results with available value
auto results = inst->getResults();
if (!results.empty()) {
// This is a placeholder - actual implementation would handle value replacement
changed = true;
}
instIt = block->instructions.erase(instIt);
} else {
// Add to available expressions
auto results = inst->getResults();
if (!results.empty()) {
availableExpr[inst] = results[0];
}
++instIt;
}
}
}
} while (changed);
}
void CommonSubexpressionElimination::runOnModule(Module& module) {
for (auto& func : module.functions) {
runOnFunction(*func);
}
}