Lab4: Implement basic scalar optimizations and lower Phi nodes to assembly

This commit is contained in:
2026-05-05 10:20:15 +08:00
committed by CGH0S7
parent 0b0bc04be3
commit 8f7e0ac5b4
17 changed files with 1318 additions and 35 deletions

View File

@@ -103,6 +103,8 @@ static std::string OpcodeToString(Opcode op) {
return "sitofp";
case Opcode::FPToSI:
return "fptosi";
case Opcode::Phi:
return "phi";
}
return "?";
}
@@ -347,6 +349,16 @@ void IRPrinter::Print(const Module& module, std::ostream& os) {
<< TypeToString(*cast->GetType()) << "\n";
break;
}
case Opcode::Phi: {
auto* phi = static_cast<const PhiInst*>(inst);
os << " %" << phi->GetName() << " = phi " << TypeToString(*phi->GetType()) << " ";
for (size_t i = 0; i < phi->GetNumIncoming(); ++i) {
if (i > 0) os << ", ";
os << "[ " << ValueToString(phi->GetIncomingValue(i)) << ", %" << phi->GetIncomingBlock(i)->GetName() << " ]";
}
os << "\n";
break;
}
}
}
}