Index: lib/Transforms/Utils/LoopUtils.cpp =================================================================== --- lib/Transforms/Utils/LoopUtils.cpp +++ lib/Transforms/Utils/LoopUtils.cpp @@ -565,7 +565,8 @@ auto *I = Phi->user_back(); if (I->isCast() && (I->getParent() == Phi->getParent()) && I->hasOneUse() && DT->dominates(Previous, I->user_back())) { - SinkAfter[I] = Previous; + if (!DT->dominates(Previous, I)) // Otherwise we're good w/o sinking. + SinkAfter[I] = Previous; return true; } } Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -7653,7 +7653,7 @@ // 2. Copy and widen instructions from the old loop into the new loop. // Move instructions to handle first-order recurrences. - DenseMap SinkAfter = Legal->getSinkAfter(); + DenseMap &SinkAfter = Legal->getSinkAfter(); for (auto &Entry : SinkAfter) { Entry.first->removeFromParent(); Entry.first->insertAfter(Entry.second);