| 1548 | } |
| 1549 | |
| 1550 | static map<string_view, Instr*> can_remove_init(Function &fn) { |
| 1551 | map<string_view, Instr*> to_remove; |
| 1552 | auto &bb = fn.getFirstBB(); |
| 1553 | if (bb.getName() != "#init") |
| 1554 | return to_remove; |
| 1555 | |
| 1556 | bool has_int2ptr = false; |
| 1557 | for (auto &i : fn.instrs()) { |
| 1558 | if (isCast(ConversionOp::Int2Ptr, i)) { |
| 1559 | has_int2ptr = true; |
| 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | vector<Value*> worklist; |
| 1565 | set<const Value*> seen; |
| 1566 | auto users = fn.getUsers(); |
| 1567 | |
| 1568 | for (auto &i : bb.instrs()) { |
| 1569 | if (!dynamic_cast<const Store*>(&i)) |
| 1570 | continue; |
| 1571 | auto gvar = i.operands()[1]; |
| 1572 | worklist.emplace_back(gvar); |
| 1573 | seen.emplace(&i); |
| 1574 | |
| 1575 | bool needed = false; |
| 1576 | do { |
| 1577 | auto user = worklist.back(); |
| 1578 | worklist.pop_back(); |
| 1579 | if (!seen.emplace(user).second) |
| 1580 | continue; |
| 1581 | |
| 1582 | // OK, we can't observe which memory it reads |
| 1583 | if (dynamic_cast<FnCall*>(user)) |
| 1584 | continue; |
| 1585 | |
| 1586 | if (isCast(ConversionOp::Ptr2Int, *user)) { |
| 1587 | // int2ptr can potentially alias with anything, so play on the safe side |
| 1588 | if (has_int2ptr) { |
| 1589 | needed = true; |
| 1590 | break; |
| 1591 | } |
| 1592 | continue; |
| 1593 | } |
| 1594 | |
| 1595 | // if (p == @const) read(load p) ; this should read @const (or raise UB) |
| 1596 | if (dynamic_cast<ICmp*>(user)) { |
| 1597 | needed = true; |
| 1598 | break; |
| 1599 | } |
| 1600 | |
| 1601 | // no useful users |
| 1602 | if (dynamic_cast<Return*>(user)) |
| 1603 | continue; |
| 1604 | |
| 1605 | if (dynamic_cast<MemInstr*>(user) && !dynamic_cast<GEP*>(user)) { |
| 1606 | needed = true; |
| 1607 | break; |