fix crash with unreachable BBs referenced by phi nodes
MODIFY
ir/instr.cpp
ir/instr.cpp
@@ -1887,6 +1887,15 @@ void Phi::addValue(Value &val, string &&BB_name) {
values.emplace_back(&val, move(BB_name));
}
+void Phi::removeValue(const string &BB_name) {
+ for (auto I = values.begin(), E = values.end(); I != E; ++I) {
+ if (I->second == BB_name) {
+ values.erase(I);
+ break;
+ }
+ }
+}
+
vector<Value*> Phi::operands() const {
vector<Value*> v;
for (auto &[val, bb] : values) {

MODIFY
ir/instr.h
ir/instr.h
@@ -312,6 +312,7 @@ public:
Phi(Type &type, std::string &&name) : Instr(type, std::move(name)) {}
void addValue(Value &val, std::string &&BB_name);
+ void removeValue(const std::string &BB_name);
std::vector<Value*> operands() const override;
void rauw(const Value &what, Value &with) override;

MODIFY
tools/transform.cpp
tools/transform.cpp
@@ -1032,12 +1032,23 @@ static void remove_unreachable_bbs(Function &f) {
} while (!wl.empty());
auto all_bbs = f.getBBs(); // copy intended
+ vector<string> unreachable;
for (auto bb : all_bbs) {
- if (!reachable.count(bb))
+ if (!reachable.count(bb)) {
+ unreachable.emplace_back(bb->getName());
f.removeBB(*bb);
}
}
+ for (auto &i : f.instrs()) {
+ if (auto phi = dynamic_cast<const Phi*>(&i)) {
+ for (auto &bb : unreachable) {
+ const_cast<Phi*>(phi)->removeValue(bb);
+ }
+ }
+ }
+}
+
void Transform::preprocess() {
remove_unreachable_bbs(src);
remove_unreachable_bbs(tgt);

fix crash with unreachable BBs referenced by phi nodes