transform preprocess: also delete unused constants
this fixes a crash in GVNSink/struct.ll where all visibles occurences of null
were deleted but null constant was left behind
MODIFY
ir/function.cpp
ir/function.cpp
@@ -211,29 +211,33 @@ multimap<Value*, Value*> Function::getUsers() const {
users.emplace(val, agg.get());
}
}
+ for (auto &c : constants) {
+ if (auto agg = dynamic_cast<AggregateValue*>(c.get())) {
+ for (auto val : agg->getVals()) {
+ users.emplace(val, agg);
+ }
+ }
+ }
return users;
}
-bool Function::removeUnusedAggsAndGVars(const multimap<Value*, Value*> &users) {
+template <typename T>
+static bool removeUnused(T &data, const multimap<Value*, Value*> &users) {
bool changed = false;
- for (auto I = aggregates.begin(); I != aggregates.end(); ) {
+ for (auto I = data.begin(); I != data.end(); ) {
if (users.count(I->get())) {
++I;
} else {
- I = aggregates.erase(I);
+ I = data.erase(I);
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;
}
+bool Function::removeUnusedStuff(const multimap<Value*, Value*> &users) {
+ bool changed = removeUnused(aggregates, users);
+ changed |= removeUnused(constants, users);
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 removeUnusedAggsAndGVars(const std::multimap<Value*, Value*> &users);
+ bool removeUnusedStuff(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
@@ -982,7 +982,7 @@ void Transform::preprocess() {
to_remove.clear();
}
- changed |= fn->removeUnusedAggsAndGVars(users);
+ changed |= fn->removeUnusedStuff(users);
} while (changed);
}
}

transform preprocess: also delete unused constants
this fixes a crash in GVNSink/struct.ll where all visibles occurences of null
were deleted but null constant was left behind