transform preprocess: remove unused global variables
MODIFY
ir/function.cpp
ir/function.cpp
@@ -120,8 +120,7 @@ void Function::addConstant(unique_ptr<Value> &&c) {
vector<GlobalVariable *> Function::getGlobalVars() const {
vector<GlobalVariable *> gvs;
for (auto I = constants.begin(), E = constants.end(); I != E; ++I) {
- const unique_ptr<Value> &c = *I;
- if (auto *gv = dynamic_cast<GlobalVariable *>(c.get()))
+ if (auto *gv = dynamic_cast<GlobalVariable*>(I->get()))
gvs.push_back(gv);
}
return gvs;
@@ -215,7 +214,7 @@ multimap<Value*, Value*> Function::getUsers() const {
return users;
}
-bool Function::removeUnusedAggs(const multimap<Value*, Value*> &users) {
+bool Function::removeUnusedAggsAndGVars(const multimap<Value*, Value*> &users) {
bool changed = false;
for (auto I = aggregates.begin(); I != aggregates.end(); ) {
if (users.count(I->get())) {
@@ -225,6 +224,16 @@ bool Function::removeUnusedAggs(const multimap<Value*, Value*> &users) {
changed = true;
}
}
+
+ for (auto I = constants.begin(); I != constants.end(); ) {
+ if (dynamic_cast<GlobalVariable*>(I->get()) && !users.count(I->get())) {
+ I = constants.erase(I);
+ changed = true;
+ } else {
+ ++I;
+ }
+ }
+
return changed;
}

MODIFY
ir/function.h
ir/function.h
@@ -153,7 +153,7 @@ public:
instr_helper instrs() const { return *this; }
std::multimap<Value*, Value*> getUsers() const;
- bool removeUnusedAggs(const std::multimap<Value*, Value*> &users);
+ bool removeUnusedAggsAndGVars(const std::multimap<Value*, Value*> &users);
void print(std::ostream &os, bool print_header = true) const;
friend std::ostream &operator<<(std::ostream &os, const Function &f);

MODIFY
tools/transform.cpp
tools/transform.cpp
@@ -978,7 +978,7 @@ void Transform::preprocess() {
to_remove.clear();
}
- changed |= fn->removeUnusedAggs(users);
+ changed |= fn->removeUnusedAggsAndGVars(users);
} while (changed);
}
}

transform preprocess: remove unused global variables