D44571 changed SimplificationTracker to use SmallSetVector to keep phi nodes. As a result, when the number of phi nodes is large, the build time performance suffers badly. When building for power pc, we have a case where there are more than 600.000 nodes, and it takes too long to compile.
In this change, I partially revert D44571 to use SmallPtrSet, which does an acceptable job with any number of elements. In the original patch, having a deterministic iteration order was mentioned as a motivation, however I think it only applies to the nodes already matched in MatchPhiSet method, which I did not touch.
Imagine that we insert the value 1,2,3,4,5 into the set, then the NodeList will be [1, 2, 3, 4, 5].
Now we remove "3", then the NodeList is untouched and FirstValidElement still points at the beginning of the NodeList.
If we later inset "3" again, then the NodeList will become [1, 2, 3, 4, 5, 3].
When iterating we now will get the order 1,2,3,4,5 and not the expected 1,2,4,5,3 (assuming that we really expect to get the insertion order).
I'm not sure if the above will ever happen for the specific use case in CodeGenPrepare, but looking at this as a general set implementation it seems faulty.
One solution, seeing this as a set of pointers, could be to now allow nullptr in the set (so add an assert for the in "insert"). And then we should not only move FirstValidElement forward here, we also need to scan the NodeList starting at FirstValidElement and replace any found entry matching Ptr with a nullptr (there should be exactly one such entry since it is a unique set).