Sometimes LegalizeTypes knows about common subexpressions before SelectionDAG
does, leading to accidental SDValue removal before its reference count was
Looks OK to me, but I'm not that familiar with the legalizer.
Nit: for (auto const &I: ReplacedValues)
Do we need an assert at the beginning of the loop that Old != New? Otherwise we may be removing a valid map entry.
Don't you need to wrap the whole for loop inside #ifndef NDEBUG. Otherwise I would be a def without any uses in a no-assert build.
Looking at the for loop above there is no such protection for UI (but maybe that is because UI is incremented in the loop).
I wouldn't mind if you added #ifndef NDEBUG around both loops, as they only make sense when the asserts are active.
Reverted this in 3ce77142a6452d76d6f97c9a6c2da193e78841ba because one of the new assertions failed in the expensive-checks bots: http://lab.llvm.org:8011/builders/llvm-clang-x86_64-expensive-checks-debian/builds/4971/steps/test-check-all/logs/FAIL%3A%20LLVM%3A%3A2008-12-02-IllegalResultType.ll
This patch removes that check, which apparently wasn't correct.