From the original RFC:
- Use a worklist, not a recursive approach, to avoid needless revisitation and being repeatedly forced to jump back to the start of the BB if a handle is invalidated.
- Only insert operands to the worklist if they become unused after a dead instruction is removed, so we don’t have to visit them again in most cases.
- Use a SmallSetVector to track the worklist.
- Instead of pre-initting the SmallSetVector like in DeadCodeEliminationPass, only put things into the worklist if they have to be revisited after the first run-through. This minimizes how much the actual SmallSetVector gets used, which saves a lot of time.
One small change since the original is fix a bug where we were adding the users to the list even if they were the current instruction, which is possible with phi nodes (and causes a crash).
You need to pass in TLI here or you have a regression over the previous code when handling target library routines.