| 1625 | } |
| 1626 | |
| 1627 | static void remove_unreachable_bbs(Function &f) { |
| 1628 | vector<BasicBlock*> wl = { &f.getFirstBB() }; |
| 1629 | set<BasicBlock*> reachable; |
| 1630 | |
| 1631 | do { |
| 1632 | auto bb = wl.back(); |
| 1633 | wl.pop_back(); |
| 1634 | if (!reachable.emplace(bb).second || bb->empty()) |
| 1635 | continue; |
| 1636 | |
| 1637 | if (auto instr = dynamic_cast<JumpInstr*>(&bb->back())) { |
| 1638 | for (auto &target : instr->targets()) { |
| 1639 | wl.emplace_back(const_cast<BasicBlock*>(&target)); |
| 1640 | } |
| 1641 | } |
| 1642 | } while (!wl.empty()); |
| 1643 | |
| 1644 | auto all_bbs = f.getBBs(); // copy intended |
| 1645 | vector<string> unreachable; |
| 1646 | vector<const Value*> removed_instrs; |
| 1647 | for (auto bb : all_bbs) { |
| 1648 | if (!reachable.count(bb)) { |
| 1649 | unreachable.emplace_back(bb->getName()); |
| 1650 | for (auto &i : bb->instrs()) { |
| 1651 | removed_instrs.emplace_back(&i); |
| 1652 | } |
| 1653 | f.removeBB(*bb); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | for (auto &i : f.instrs()) { |
| 1658 | if (auto phi = dynamic_cast<const Phi*>(&i)) { |
| 1659 | for (auto &bb : unreachable) { |
| 1660 | const_cast<Phi*>(phi)->removeValue(bb); |
| 1661 | } |
| 1662 | for (auto &i : removed_instrs) { |
| 1663 | const_cast<Phi*>(phi)->removeValue(i); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | static void optimize_ptrcmp(Function &f) { |
| 1670 | auto is_inbounds = [](const Value &v) { |
no test coverage detected